Is it possible to simply pass the resulting records generated by dblink()
up, as a result of the calling function, something like:
create function execute_generic_sql_on_remote_databases(_sql text)
return set of record
language plpgsql
as $$
declare
r record; -- this is already not a real record
begin
-- get connections to several DBs
...
-- send _sql queries to the DBs
...
-- get results of query execution
loop through connections
for r in select MAGIC from dblink_get_results(conn, _sql) loop
return next r;
end loop;
end loop;
-- close connections
...
end;
$$;
One could call this function like dblink
actually:
select * from execute_generic_sql_on_remote_databases('SELECT 1') as r(i int);
But it needs some MAGIC
. :(
It is possible to do that using plProxy
easily, but the question is how to do it with dblink
, if it is possible at all with plpgsql
.