Postgresql – Difference between return next and return record

plpgsqlpostgresqlrecordstored-procedures

What is the difference between RETURN NEXT and RETURN RECORD in PL/pgSQL? I find the manual quite confusing in explaining what RETURN RECORD actually does. What is a RECORD type?

Best Answer

While RETURN NEXT is valid plpgsql syntax, RETURN RECORD is non-existent as such. It corresponds to how you declare the return type in the function header. Read the manual about how to return from a function.

In a function declaring RETURNS int you can:

RETURN 1;

For RETURNS SETOF int you can:

RETURN NEXT 1;

If you declare OUT parameters, you can just:

RETURN;
-- or do nothing: OUT params are returned at the end of the function anyway

Or if you combine OUT parameters with RETURNS SETOF record - effectively the same as RETURNS TABLE (...):

RETURN NEXT;

The current state of all OUT parameters is returned as next row.

Finally, to return a whole set of rows at once:

RETURN QUERY ...

record is a special data type. It's meaning depends on where you use it. Like I posted in my answer to your previous question:

The manual about the record type in PL/pgSQL.