Avoid creating duplicate cached plans for inherited FK constraints.
authorTom Lane <tgl@sss.pgh.pa.us>
Wed, 10 Mar 2021 19:22:31 +0000 (14:22 -0500)
committerTom Lane <tgl@sss.pgh.pa.us>
Wed, 10 Mar 2021 19:22:31 +0000 (14:22 -0500)
commitc3ffe34863688115dd7878f118f2a123bafd8a26
tree7cf4577a73c9ae4c4e47f9bbeef25f4c188bb59c
parentb12436340adf27aa3d334c92579e6662dd3090ce
Avoid creating duplicate cached plans for inherited FK constraints.

When a foreign key constraint is applied to a partitioned table, each
leaf partition inherits a similar FK constraint.  We were processing all
of those constraints independently, meaning that in large partitioning
trees we'd build up large collections of cached FK-checking query plans.
However, in all cases but one, the generated queries are actually
identical for all members of the inheritance tree (because, in most
cases, the query only mentions the topmost table of the other side of
the FK relationship).  So we can share a single cached plan among all
the partitions, saving memory, not to mention time to build and maintain
the cached plans.

Keisuke Kuroda and Amit Langote

Discussion: http://postgr.es/m/cab4b85d-9292-967d-adf2-be0d803c3e23@nttcom.co.jp_1
src/backend/utils/adt/ri_triggers.c
src/test/regress/expected/foreign_key.out
src/test/regress/sql/foreign_key.sql