CREATE FUNCTION schemaone.get_pg_stat_activity() RETURNS SETOF pg_stat_activity
LANGUAGE sql SECURITY DEFINER
AS $$ SELECT * FROM pg_catalog.pg_stat_activity; $$;
CREATE VIEW schematwo.pg_stat_activity AS
SELECT
get_pg_stat_activity.datid,
get_pg_stat_activity.datname,
get_pg_stat_activity.pid,
get_pg_stat_activity.usesysid,
get_pg_stat_activity.usename,
get_pg_stat_activity.application_name,
get_pg_stat_activity.client_addr,
get_pg_stat_activity.client_hostname,
get_pg_stat_activity.client_port,
get_pg_stat_activity.backend_start,
get_pg_stat_activity.xact_start,
get_pg_stat_activity.query_start,
get_pg_stat_activity.state_change,
get_pg_stat_activity.wait_event_type,
get_pg_stat_activity.wait_event,
get_pg_stat_activity.state,
get_pg_stat_activity.backend_xid,
get_pg_stat_activity.backend_xmin,
get_pg_stat_activity.query,
get_pg_stat_activity.backend_type
FROM schemaone.get_pg_stat_activity() get_pg_stat_activity(datid, datname, pid, usesysid, usename, application_name, client_addr, client_hostname, client_port, backend_start, xact_start, query_start, state_change, wait_event_type, wait_event, state, backend_xid, backend_xmin, query, backend_type);
I have these queries here; I get the following error when I try using pg_restore.
10:33:56 1251111 ERROR string_utils.c:650
pg_restore: error: could not execute query: ERROR: column reference "query" is ambiguous
10:33:56 1251111 ERROR string_utils.c:650
LINE 20: get_pg_stat_activity.query,
You are probably trying to re-use PostgreSQL version 10-12 definition of
pg_stat_activityin your alias, while being on version 14+ so the alias list doesn't match the names and amount of output columns, leaving you with 2 stray, unaliased columns causing the conflict. Demo:4th column of
pg_stat_activityisleader_pidand by ommitting it in your list, you're still selecting it but renaming asusesysidand shifting all the column names that follow, one up/to the left. 20th column isquery_idthat you also skipped, so at the end, you're still selecting everything that comes out ofpg_stat_activitythrough yourschemaone.get_pg_stat_activity()function, but renaming columns 4-20, then leaving the last two unaliased, which happen to bequeryandbackend_type.So the last 4 columns that result from this:
are
query,backend_type,query,backend_type, causing the conflict.You need to make your aliases match:
or remove that list entirely