I am trying to save the result of a SELECT
query, pass it, and reuse it in another PL/pgSQL function:
DECLARE
table_holder my_table; --the typ
There are no "table variables" in plpgsql. That's something you would find in SQL Server.
Use a temporary table instead:
BEGIN
CREATE TEMP TABLE table_holder AS
SELECT * FROM table_holder
WHERE <some condition>
ORDER BY <some expression>
;
...
END
A temporary table exists for the lifetime of a session. To drop it at the end of the function (or an enclosing transaction) automatically, use ON COMMIT DROP
in the creating statement.
CREATE TEMP TABLE table_holder ON COMMIT DROP AS
SELECT ...
The temporary table is automatically visible to any other function in the same session.
One alternative would be to use cursors in plpgsql.