8000 Fix bugs in information_schema.referential_constraints view. · jaylevitt/postgres@0134608 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0134608

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 7c64c9f commit 0134608

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
@@ -1158,20 +1158,24 @@ CREATE VIEW referential_constraints AS
11581158

11591159
FROM (pg_namespace ncon
11601160
INNER JOIN pg_constraint con ON ncon.oid = con.connamespace
1161-
INNER JOIN pg_class c ON con.conrelid = c.oid)
1162-
LEFT JOIN
1163-
(pg_constraint pkc
1164-
INNER JOIN pg_namespace npkc ON pkc.connamespace = npkc.oid)
1165-
ON con.confrelid = pkc.conrelid
1166-
AND _pg_keysequal(con.confkey, pkc.conkey)
1167-
1168-
WHERE c.relkind = 'r'
1169-
AND con.contype = 'f'
1170-
AND (pkc.contype IN ('p', 'u') OR pkc.contype IS NULL)
1171-
AND (pg_has_role(c.relowner, 'USAGE')
1172-
-- SELECT privilege omitted, per SQL standard
1173-
OR has_table_privilege(c.oid, 'INSERT, UPDATE, DELETE, TRUNCATE, REFERENCES, TRIGGER')
1174-
OR has_any_column_privilege(c.oid, 'INSERT, UPDATE, REFERENCES') );
1161+
INNER JOIN pg_class c ON con.conrelid = c.oid AND con.contype = 'f')
1162+
LEFT JOIN pg_depend d1 -- find constraint's dependency on an index
1163+
ON d1.objid = con.oid AND d1.classid = 'pg_constraint'::regclass
1164+
AND d1.refclassid = 'pg_class'::regclass AND d1.refobjsubid = 0
1165+
LEFT JOIN pg_depend d2 -- find pkey/unique constraint for that index
1166+
ON d2.refclassid = 'pg_constraint'::regclass
1167+
AND d2.classid = 'pg_class'::regclass
1168+
AND d2.objid = d1.refobjid AND d2.objsubid = 0
1169+
AND d2.deptype = 'i'
1170+
LEFT JOIN pg_constraint pkc ON pkc.oid = d2.refobjid
1171+
AND pkc.contype IN ('p', 'u')
1172+
AND pkc.conrelid = con.confrelid
1173+
LEFT JOIN pg_namespace npkc ON pkc.connamespace = npkc.oid
1174+
1175+
WH 5B30 ERE pg_has_role(c.relowner, 'USAGE')
1176+
-- SELECT privilege omitted, per SQL standard
1177+
OR has_table_privilege(c.oid, 'INSERT, UPDATE, DELETE, TRUNCATE, REFERENCES, TRIGGER')
1178+
OR has_any_column_privilege(c.oid, 'INSERT, UPDATE, REFERENCES') ;
11751179

11761180
GRANT SELECT ON referential_constraints TO PUBLIC;
11771181

0 commit comments

Comments
 (0)
0