I would like to create a function returning the number of records that an SQL expression passed as parameter can generate. Can anyone put me on the right path?
Postgresql function returning number of records
138 Views Asked by Jacopo Russo At
2
There are 2 best solutions below
0

Your function should return a SETOF
some (record) type.
You then use the RETURN NEXT
syntax to return each row.
See the example in the doc page for this. https://www.postgresql.org/docs/current/static/plpgsql-control-structures.html#id-1.8.8.8.3.4
In plain SQL you can get the number of returned rows using a derived table (placing the query in a subquery in the
FROM
clause) like this:Do the same in a plpgsql function. You need a dynamic command as the function should work for any valid SQL query:
Example: