pgAdmin is just a GUI. Largely irrelevant to this question. It just so happens that one or more sessions are bound to an SQL editor window and end when the window is closed.
If you want to keep the transaction open, just don't COMMIT
(or ROLLBACK
), yet.
If you really want to "keep" tha result set, write it to a table
- possibly a TEMPORARY
or UNLOGGED
table if you don't need to persist permanently. A temporary table lives and dies with the session by default.
SQL and PL/pgSQL are rather strict with their type system and some things that seem possible are not implemented (yet). But what you are trying to do can probably be solved without cursors. If you need to provide a table name to a function dynamically, use a polymorphic return type that's chained to an input parameter:
CREATE OR REPLACE FUNCTION f_dynamic_select(_tbl anyelement)
RETURNS SETOF anyelement AS
$func$
BEGIN
RETURN QUERY EXECUTE
'SELECT * FROM ' || pg_typeof(_tbl);
END
$func$ LANGUAGE plpgsql;
Call:
SELECT * FROM f_dynamic_select(NULL::my_schema.my_table)
This form is automatically safe against SQL injection, since pg_typeof()
returns a regtype
value that is automatically escaped (if necesaray) when it's automatically converted to text
during string concatenation.
I have written a closely related answer dealing with polymorphic types just yesterday. It has more explanation and links:
Insert values from a record variable into a table
Normally, a plain and simple SELECT
would do the job. It's a rare condition that table names have to be provided dynamically.
SELECT * FROM my_schema.my_table