Skip to content

Commit 9a743ff

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 7a0c584 commit 9a743ff

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed

src/backend/catalog/information_schema.sql

+15-11
Original file line numberDiff line numberDiff line change
@@ -1099,17 +1099,21 @@ CREATE VIEW referential_constraints AS
10991099

11001100
FROM (pg_namespace ncon
11011101
INNER JOIN pg_constraint con ON ncon.oid = con.connamespace
1102-
INNER JOIN pg_class c ON con.conrelid = c.oid)
1103-
LEFT JOIN
1104-
(pg_constraint pkc
1105-
INNER JOIN pg_namespace npkc ON pkc.connamespace = npkc.oid)
1106-
ON con.confrelid = pkc.conrelid
1107-
AND _pg_keysequal(con.confkey, pkc.conkey)
1108-
1109-
WHERE c.relkind = 'r'
1110-
AND con.contype = 'f'
1111-
AND (pkc.contype IN ('p', 'u') OR pkc.contype IS NULL)
1112-
AND pg_has_role(c.relowner, 'USAGE');
1102+
INNER JOIN pg_class c ON con.conrelid = c.oid AND con.contype = 'f')
1103+
LEFT JOIN pg_depend d1 -- find constraint's dependency on an index
1104+
ON d1.objid = con.oid AND d1.classid = 'pg_constraint'::regclass
1105+
AND d1.refclassid = 'pg_class'::regclass AND d1.refobjsubid = 0
1106+
LEFT JOIN pg_depend d2 -- find pkey/unique constraint for that index
1107+
ON d2.refclassid = 'pg_constraint'::regclass
1108+
AND d2.classid = 'pg_class'::regclass
1109+
AND d2.objid = d1.refobjid AND d2.objsubid = 0
1110+
AND d2.deptype = 'i'
1111+
LEFT JOIN pg_constraint pkc ON pkc.oid = d2.refobjid
1112+
AND pkc.contype IN ('p', 'u')
1113+
AND pkc.conrelid = con.confrelid
1114+
LEFT JOIN pg_namespace npkc ON pkc.connamespace = npkc.oid
1115+
1116+
WHERE pg_has_role(c.relowner, 'USAGE');
11131117

11141118
GRANT SELECT ON referential_constraints TO PUBLIC;
11151119

0 commit comments

Comments
 (0)