I have this select, that collects data from all local databases that have a table named operation
:
select
db_name,
unnest((select *
from dblink(conn_string,
'select array_agg(row(opr.id, opr.descricao))
from Operacao opr')
as (result text array))) as result
from (
select
db_name,
table_name,
conn_string,
(select table_exists
from dblink(db.conn_string,
'(select Count(*) > 0 from information_schema.tables where table_name = ''' || db.table_name || ''')') as (table_exists Boolean))
from (
SELECT
datname as db_name,
'user=postgres password=postgres dbname=' || datname as conn_string,
'operacao'::text as table_name
FROM pg_database
WHERE datistemplate = false
limit 4
) db
) db
where table_exists;
This works somewhat okay, except it is bringing the id
and descricao
fields as a single column. I tried various variants of the result text array
, but couldn't manage to turn the result into a record. Is there any way to split those columns (other than the obvious string manipulation)?