Until I started with JavaScript in PostgreSQL I have actually never seen this error in my “real PostgreSQL life” before. Error message looks like this: “return_next function calls in context that cannot accept a set”. And it happens when you call function which returns “SETOF” something using “return_next” and you try to catch result into variable. Basically – error is not in function returning data but in function catching result.

I tried to declare variable as Object or as Array but I still got this message that variable “cannot accept a set”. Type Set is not supported by plv8 at this moment (02/2016). Funny thing is that I got this error even if function returned only 1 row!

The only remedy I have found so far in plv8 is to use plain “return” in this case – instead of “return_next”. Looks like with “return_next” PostgreSQL is afraid that there could be other rows coming. You can declare function with returning type for example “returns setof text” and give it several ways to end – one with plain “return” with variable. And if you need to return more rows anyway you can use JSON format for it. Of course if you have a reasonable amount of rows.