postgresql/src/backend/catalog
Alvaro Herrera c7d43c4d8a Correct attach/detach logic for FKs in partitions
There was no code to handle foreign key constraints on partitioned
tables in the case of ALTER TABLE DETACH; and if you happened to ATTACH
a partition that already had an equivalent constraint, that one was
ignored and a new constraint was created.  Adding this to the fact that
foreign key cloning reuses the constraint name on the partition instead
of generating a new name (as it probably should, to cater to SQL
standard rules about constraint naming within schemas), the result was a
pretty poor user experience -- the most visible failure was that just
detaching a partition and re-attaching it failed with an error such as

  ERROR:  duplicate key value violates unique constraint "pg_constraint_conrelid_contypid_conname_index"
  DETAIL:  Key (conrelid, contypid, conname)=(26702, 0, test_result_asset_id_fkey) already exists.

because it would try to create an identically-named constraint in the
partition.  To make matters worse, if you tried to drop the constraint
in the now-independent partition, that would fail because the constraint
was still seen as dependent on the constraint in its former parent
partitioned table:
  ERROR:  cannot drop inherited constraint "test_result_asset_id_fkey" of relation "test_result_cbsystem_0001_0050_monthly_2018_09"

This fix attacks the problem from two angles: first, when the partition
is detached, the constraint is also marked as independent, so the drop
now works.  Second, when the partition is re-attached, we scan existing
constraints searching for one matching the FK in the parent, and if one
exists, we link that one to the parent constraint.  So we don't end up
with a duplicate -- and better yet, we don't need to scan the referenced
table to verify that the constraint holds.

To implement this I made a small change to previously planner-only
struct ForeignKeyCacheInfo to contain the constraint OID; also relcache
now maintains the list of FKs for partitioned tables too.

Backpatch to 11.

Reported-by: Michael Vitale (bug #15425)
Discussion: https://postgr.es/m/15425-2dbc9d2aa999f816@postgresql.org
2018-10-12 12:37:37 -03:00
..
.gitignore Replace our traditional initial-catalog-data format with a better design. 2018-04-08 13:17:27 -04:00
aclchk.c Post-feature-freeze pgindent run. 2018-04-26 14:47:16 -04:00
catalog.c Add toast tables to most system catalogs 2018-07-20 07:43:41 +09:00
Catalog.pm Add a "return" statement to pacify perlcritic. 2018-09-20 16:10:23 -04:00
dependency.c Create an RTE field to record the query's lock mode for each relation. 2018-09-30 13:55:51 -04:00
genbki.pl Teach genbki.pl to auto-generate pg_type entries for array types. 2018-09-20 15:14:46 -04:00
heap.c Create an RTE field to record the query's lock mode for each relation. 2018-09-30 13:55:51 -04:00
index.c Fix event triggers for partitioned tables 2018-10-06 19:17:46 -03:00
indexing.c Centralize executor's opening/closing of Relations for rangetable entries. 2018-10-04 14:03:42 -04:00
information_schema.sql Add prokind column, replacing proisagg and proiswindow 2018-03-02 13:48:33 -05:00
Makefile Rearrange makefile rules for running Gen_fmgrtab.pl. 2018-05-03 17:54:18 -04:00
namespace.c Clarify comment about assignment and reset of temp namespace ID in MyProc 2018-08-21 08:32:18 +09:00
objectaccess.c Update copyright for 2018 2018-01-02 23:30:12 -05:00
objectaddress.c Refactor routines for subscription and publication lookups 2018-09-18 12:00:18 +09:00
partition.c Post-feature-freeze pgindent run. 2018-04-26 14:47:16 -04:00
pg_aggregate.c Fix type checking for support functions of parallel VARIADIC aggregates. 2018-05-15 15:06:53 -04:00
pg_collation.c Merge catalog/pg_foo_fn.h headers back into pg_foo.h headers. 2018-04-08 14:35:29 -04:00
pg_constraint.c Correct attach/detach logic for FKs in partitions 2018-10-12 12:37:37 -03:00
pg_conversion.c Merge catalog/pg_foo_fn.h headers back into pg_foo.h headers. 2018-04-08 14:35:29 -04:00
pg_db_role_setting.c Update copyright for 2018 2018-01-02 23:30:12 -05:00
pg_depend.c Local partitioned indexes 2018-01-19 11:49:22 -03:00
pg_enum.c Relax transactional restrictions on ALTER TYPE ... ADD VALUE (redux). 2018-10-09 12:51:01 +13:00
pg_inherits.c Post-feature-freeze pgindent run. 2018-04-26 14:47:16 -04:00
pg_largeobject.c Update copyright for 2018 2018-01-02 23:30:12 -05:00
pg_namespace.c Replace GrantObjectType with ObjectType 2018-01-19 14:01:14 -05:00
pg_operator.c Merge catalog/pg_foo_fn.h headers back into pg_foo.h headers. 2018-04-08 14:35:29 -04:00
pg_proc.c Improve error messages for CREATE OR REPLACE PROCEDURE 2018-08-18 22:05:43 +02:00
pg_publication.c Refactor routines for subscription and publication lookups 2018-09-18 12:00:18 +09:00
pg_range.c Update copyright for 2018 2018-01-02 23:30:12 -05:00
pg_shdepend.c Update copyright for 2018 2018-01-02 23:30:12 -05:00
pg_subscription.c Refactor routines for subscription and publication lookups 2018-09-18 12:00:18 +09:00
pg_type.c Merge catalog/pg_foo_fn.h headers back into pg_foo.h headers. 2018-04-08 14:35:29 -04:00
sql_feature_packages.txt > I have installed your patch and adjusted the names of the standards 2004-12-02 22:51:28 +00:00
sql_features.txt Update SQL features list 2018-05-21 15:29:22 -04:00
storage.c Further cleanup of client dependencies on src/include/catalog headers. 2018-04-09 14:39:58 -04:00
system_views.sql Remove deprecated abstime, reltime, tinterval datatypes. 2018-10-11 11:59:15 -07:00
toasting.c Rename IndexInfo.ii_KeyAttrNumbers array 2018-04-12 13:02:45 +03:00