Skip to content

Commit d26e1eb

Browse files
committed
Fix bugs in information_schema.referential_constraints view.
This view was being insufficiently careful about matching the FK constraint to the depended-on primary or unique key constraint. That could result in failure to show an FK constraint at all, or showing it multiple times, or claiming that it depended on a different constraint than the one it really does. Fix by joining via pg_depend to ensure that we find only the correct dependency. Back-patch, but don't bump catversion because we can't force initdb in back branches. The next minor-version release notes should explain that if you need to fix this in an existing installation, you can drop the information_schema schema then re-create it by sourcing $SHAREDIR/information_schema.sql in each database (as a superuser of course).
1 parent e6858e6 commit d26e1eb

File tree

1 file changed

+18
-14
lines changed

1 file changed

+18
-14
lines changed

src/backend/catalog/information_schema.sql

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1176,20 +1176,24 @@ CREATE VIEW referential_constraints AS
11761176

11771177
FROM (pg_namespace ncon
11781178
INNER JOIN pg_constraint con ON ncon.oid = con.connamespace
1179-
INNER JOIN pg_class c ON con.conrelid = c.oid)
1180-
LEFT JOIN
1181-
(pg_constraint pkc
1182-
INNER JOIN pg_namespace npkc ON pkc.connamespace = npkc.oid)
1183-
ON con.confrelid = pkc.conrelid
1184-
AND _pg_keysequal(con.confkey, pkc.conkey)
1185-
1186-
WHERE c.relkind = 'r'
1187-
AND con.contype = 'f'
1188-
AND (pkc.contype IN ('p', 'u') OR pkc.contype IS NULL)
1189-
AND (pg_has_role(c.relowner, 'USAGE')
1190-
-- SELECT privilege omitted, per SQL standard
1191-
OR has_table_privilege(c.oid, 'INSERT, UPDATE, DELETE, TRUNCATE, REFERENCES, TRIGGER')
1192-
OR has_any_column_privilege(c.oid, 'INSERT, UPDATE, REFERENCES') );
1179+
INNER JOIN pg_class c ON con.conrelid = c.oid AND con.contype = 'f')
1180+
LEFT JOIN pg_depend d1 -- find constraint's dependency on an index
1181+
ON d1.objid = con.oid AND d1.classid = 'pg_constraint'::regclass
1182+
AND d1.refclassid = 'pg_class'::regclass AND d1.refobjsubid = 0
1183+
LEFT JOIN pg_depend d2 -- find pkey/unique constraint for that index
1184+
ON d2.refclassid = 'pg_constraint'::regclass
1185+
AND d2.classid = 'pg_class'::regclass
1186+
AND d2.objid = d1.refobjid AND d2.objsubid = 0
1187+
AND d2.deptype = 'i'
1188+
LEFT JOIN pg_constraint pkc ON pkc.oid = d2.refobjid
1189+
AND pkc.contype IN ('p', 'u')
1190+
AND pkc.conrelid = con.confrelid
1191+
LEFT JOIN pg_namespace npkc ON pkc.connamespace = npkc.oid
1192+
1193+
WHERE pg_has_role(c.relowner, 'USAGE')
1194+
-- SELECT privilege omitted, per SQL standard
1195+
OR has_table_privilege(c.oid, 'INSERT, UPDATE, DELETE, TRUNCATE, REFERENCES, TRIGGER')
1196+
OR has_any_column_privilege(c.oid, 'INSERT, UPDATE, REFERENCES') ;
11931197

11941198
GRANT SELECT ON referential_constraints TO PUBLIC;
11951199

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy