We would like to upgrade our postgres to v17.
Using a migrated pg V16 Database, your SQL
select
TC.constraint_name,
TC.table_name as FK_TABLE_NAME,
TC.table_schema as FK_TABLE_SCHEMA,
KP.table_name as PK_TABLE_NAME,
KP.table_schema as PK_TABLE_SCHEMA,
KC.column_name as FK_COLUMN_NAME,
KP.column_name as PK_COLUMN_NAME
from
information_schema.table_constraints TC,
information_schema.referential_constraints RC,
information_schema.key_column_usage KC,
information_schema.key_column_usage KP
where
TC.constraint_name = RC.constraint_name
and TC.constraint_schema = RC.constraint_schema
and RC.constraint_schema = KC.constraint_schema
and RC.constraint_name = KC.constraint_name
and RC.unique_constraint_name = KP.constraint_name
and RC.unique_constraint_schema = KP.constraint_schema
and KC.table_name = TC.table_name
and KC.table_schema = TC.table_schema
and KC.position_in_unique_constraint = KP.ordinal_position
and TC.TABLE_SCHEMA not in ('pg_catalog', 'information_schema')
order by
TC.table_schema,
TC.table_name,
TC.constraint_name,
KC.ordinal_position
will not finish at all.
It needs about 0,5 secs in postgres v16.
In v17 I have to wait more than 5 min for first 200 entries.
rewriting this to ansi-joins we would get 200 entries within 20 secs:
select
TC.constraint_name,
TC.table_name as FK_TABLE_NAME,
TC.table_schema as FK_TABLE_SCHEMA,
KP.table_name as PK_TABLE_NAME,
KP.table_schema as PK_TABLE_SCHEMA,
KC.column_name as FK_COLUMN_NAME,
KP.column_name as PK_COLUMN_NAME
from
information_schema.table_constraints TC
join information_schema.referential_constraints RC
on
TC.constraint_name = RC.constraint_name
and TC.constraint_schema = RC.constraint_schema
join information_schema.key_column_usage KC
on
RC.constraint_schema = KC.constraint_schema
and RC.constraint_name = KC.constraint_name
and KC.table_name = TC.table_name
and KC.table_schema = TC.table_schema
join information_schema.key_column_usage KP
on
RC.unique_constraint_name = KP.constraint_name
and RC.unique_constraint_schema = KP.constraint_schema
and KC.position_in_unique_constraint = KP.ordinal_position
where
TC.TABLE_SCHEMA not in ('pg_catalog', 'information_schema')
order by
TC.table_schema,
TC.table_name,
TC.constraint_name,
KC.ordinal_position
I'm just checking your SQL with dbeaver.
Maybe there is a better way to fetch those data?
Edit:
This also depends on size.
for small databases it works fine, but we got at least some databases with more than 550 foreign keys.