pg.ddx.io pgsql-committers@postgresql.org mailing list archivehelp / color / mirror / Atom feed
pgsql: Correct attach/detach logic for FKs in partitions 2+ messages / 1 participants [nested] [flat]
* pgsql: Correct attach/detach logic for FKs in partitions @ 2018-10-12 15:38 Alvaro Herrera <alvherre@alvh.no-ip.org> 0 siblings, 0 replies; 2+ messages in thread From: Alvaro Herrera @ 2018-10-12 15:38 UTC (permalink / raw) To: pgsql-committers@lists.postgresql.org 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 Branch ------ master Details ------- https://git.postgresql.org/pg/commitdiff/c7d43c4d8a5b7215ea0a32d95260188b5d3ae3f4 Modified Files -------------- src/backend/catalog/pg_constraint.c | 238 +++++++++++++++++++++++++----- src/backend/commands/tablecmds.c | 23 +++ src/backend/nodes/copyfuncs.c | 1 + src/backend/nodes/outfuncs.c | 1 + src/backend/utils/cache/relcache.c | 6 +- src/include/utils/rel.h | 5 +- src/test/regress/expected/foreign_key.out | 119 +++++++++++++++ src/test/regress/sql/foreign_key.sql | 50 +++++++ 8 files changed, 404 insertions(+), 39 deletions(-) ^ permalink raw reply [nested|flat] 2+ messages in thread
* pgsql: Correct attach/detach logic for FKs in partitions @ 2018-10-12 15:38 Alvaro Herrera <alvherre@alvh.no-ip.org> 0 siblings, 0 replies; 2+ messages in thread From: Alvaro Herrera @ 2018-10-12 15:38 UTC (permalink / raw) To: pgsql-committers@lists.postgresql.org 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 Branch ------ REL_11_STABLE Details ------- https://git.postgresql.org/pg/commitdiff/355684ee3fa3001adf0de3873fddadd119773819 Modified Files -------------- src/backend/catalog/pg_constraint.c | 238 +++++++++++++++++++++++++----- src/backend/commands/tablecmds.c | 23 +++ src/backend/nodes/copyfuncs.c | 1 + src/backend/nodes/outfuncs.c | 1 + src/backend/utils/cache/relcache.c | 6 +- src/include/utils/rel.h | 5 +- src/test/regress/expected/foreign_key.out | 119 +++++++++++++++ src/test/regress/sql/foreign_key.sql | 50 +++++++ 8 files changed, 404 insertions(+), 39 deletions(-) ^ permalink raw reply [nested|flat] 2+ messages in thread
end of thread, other threads:[~2018-10-12 15:38 UTC | newest] Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed) -- links below jump to the message on this page -- 2018-10-12 15:38 pgsql: Correct attach/detach logic for FKs in partitions Alvaro Herrera <alvherre@alvh.no-ip.org> 2018-10-12 15:38 pgsql: Correct attach/detach logic for FKs in partitions Alvaro Herrera <alvherre@alvh.no-ip.org>
This inbox is served by DDX for PostgreSQL; see mirroring instructions for how to clone and mirror all data and code used for this inbox