SETOF RECORD
Here the record is defined within the function, keeping the parameter list a clean list of inputs. A limitation of this approach is that since the record definition is hidden from the caller, the column list must be provided as part of the retrieving
SELECT
.CREATE OR REPLACE FUNCTION squares(ct INT) RETURNS SETOF RECORD AS $$ DECLARE v_rec RECORD; BEGIN FOR i IN 0..ct-1 LOOP SELECT i, POWER(i,2)::INT INTO v_rec; RETURN NEXT v_rec; END LOOP; END; $$ LANGUAGE 'plpgsql' IMMUTABLE; SELECT * FROM squares(5) AS (A INT, B INT);
Line 5 creates a variable, of indeterminate shape, for the output record. Line 8 creates the record using a
SELECT .. INTO
, and inserts it into the variable, giving the variable its shape. Line 9 is a
RETURN NEXT..
, which adds the record to the list of records to eventually return. This form of RETURN
does not terminate the function, but rather augments the return value, which gets returned whenever the function returns. In this example, we just passively fall out the bottom of the function, but a bare RETURN
can be used to explicitly exit the function. The bare RETURN
does not interfere with the returning of the built-up result set.Predefined Types
If you define the record type outside of the function, and use it in the function definition, the caller no longer needs a column list, but determines columns from the record type.
CREATE TYPE square_type AS (a INT, b INT); CREATE OR REPLACE FUNCTION squares(ct INT) RETURNS SETOF square_type AS $$ DECLARE v_rec square_type; BEGIN FOR i IN 0..ct-1 LOOP SELECT i, POWER(i,2)::INT INTO v_rec; RETURN NEXT v_rec; END LOOP; END; $$ LANGUAGE 'plpgsql' IMMUTABLE; SELECT * FROM squares(5);
Line 1 is the type definition, defining 'square_type' as a pair of
INT
fields. Lines 4 and 7 define the return type and the variable type as 'square_type'. Because the caller can see the return type of the function from the function definition, it knows to treat the return values as pairs of INT
columns, and Line 17 can be appealingly spare. In my opinion, this is the most elegant of the three forms; it does require an additional line to define the type, and an additional addition to the namespace, but it reads easily. The next best is the OUT parameter method. Having to specify the row shape in the calling SELECT, as in the first example above, is just too awkward.
One caution is that there is no
CREATE OR REPLACE TYPE <typename>
, so you need to use a DROP TYPE <typename>
if you wish to repeat the execution of the above quoted code block complete. In another week or two, I plan to show how to use Exception handling to gracefully absorb the errors produced by redundantly creating a type.Thank you, Tzvi, for your assistance.
2 Feb changed function types to IMMUTABLE