1
0
mirror of https://github.com/postgres/postgres.git synced 2025-05-05 09:19:17 +03:00

Optimize query for information_schema.constraint_column_usage

The way the old query was written prevented some join optimizations
because the join conditions were hidden inside a CASE expression.  With
a large number of constraints, the query became unreasonably slow.  The
new query performs much better.

From: Alexey Bashtanov <bashtanov@imap.cc>
Reviewed-by: Ashutosh Bapat <ashutosh.bapat@enterprisedb.com>
This commit is contained in:
Peter Eisentraut 2017-02-17 19:32:15 -05:00
parent 68f3dbc552
commit e3a58c8835

View File

@ -801,8 +801,8 @@ CREATE VIEW constraint_column_usage AS
WHERE nr.oid = r.relnamespace WHERE nr.oid = r.relnamespace
AND r.oid = a.attrelid AND r.oid = a.attrelid
AND nc.oid = c.connamespace AND nc.oid = c.connamespace
AND (CASE WHEN c.contype = 'f' THEN r.oid = c.confrelid AND a.attnum = ANY (c.confkey) AND r.oid = CASE c.contype WHEN 'f' THEN c.confrelid ELSE c.conrelid END
ELSE r.oid = c.conrelid AND a.attnum = ANY (c.conkey) END) AND a.attnum = ANY (CASE c.contype WHEN 'f' THEN c.confkey ELSE c.conkey END)
AND NOT a.attisdropped AND NOT a.attisdropped
AND c.contype IN ('p', 'u', 'f') AND c.contype IN ('p', 'u', 'f')
AND r.relkind IN ('r', 'P') AND r.relkind IN ('r', 'P')