When creating a foreign key in a partitioned table, if some partitions already have equivalent constraints, we wastefully create duplicates of the constraints instead of attaching to the existing ones. That's inconsistent with the de-duplication that is applied when a table is attached as a partition. To fix, reuse the FK-cloning code instead of having a separate code path.
Backpatch to Postgres 11. This is a subtle behavior change, but surely a welcome one since there's no use in having duplicate foreign keys.
Discovered by Álvaro Herrera while thinking about a different problem reported by Jesper Pedersen (bug #15587).
Author: Álvaro Herrera Discussion: https://email@example.com
0325d7a595 Fix creation of duplicate foreign keys on partitions
src/backend/commands/tablecmds.c | 39 +++++++++++----
src/test/regress/expected/foreign_key.out | 83 +++++++++++++++++++++++++++++++
src/test/regress/sql/foreign_key.sql | 38 ++++++++++++++
3 files changed, 150 insertions(+), 10 deletions(-)