From ed743097555ddf8f3ffd7662a608e6765c6820cf Mon Sep 17 00:00:00 2001 From: Alvaro Herrera Date: Wed, 24 Jul 2024 12:38:18 +0200 Subject: [PATCH] Reset relhassubclass upon attaching table as a partition We don't allow inheritance parents as partitions, and have checks to prevent this; but if a table _was_ in the past an inheritance parents and all their children are removed, the pg_class.relhassubclass flag may remain set, which confuses the partition pruning code (most obviously, it results in an assertion failure; in production builds it may be worse.) Fix by resetting relhassubclass on attach. Backpatch to all supported versions. Reported-by: Alexander Lakhin Reviewed-by: Tom Lane Discussion: https://postgr.es/m/18550-d5e047e9a897a889@postgresql.org --- src/backend/catalog/heap.c | 8 ++++++++ src/test/regress/expected/alter_table.out | 10 +++++++++- src/test/regress/sql/alter_table.sql | 7 +++++++ 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c index 987b53ba19d..3bfd4c62450 100644 --- a/src/backend/catalog/heap.c +++ b/src/backend/catalog/heap.c @@ -3808,6 +3808,14 @@ StorePartitionBound(Relation rel, Relation parent, PartitionBoundSpec *bound) new_val, new_null, new_repl); /* Also set the flag */ ((Form_pg_class) GETSTRUCT(newtuple))->relispartition = true; + + /* + * We already checked for no inheritance children, but reset + * relhassubclass in case it was left over. + */ + if (rel->rd_rel->relkind == RELKIND_RELATION && rel->rd_rel->relhassubclass) + ((Form_pg_class) GETSTRUCT(newtuple))->relhassubclass = false; + CatalogTupleUpdate(classRel, &newtuple->t_self, newtuple); heap_freetuple(newtuple); table_close(classRel, RowExclusiveLock); diff --git a/src/test/regress/expected/alter_table.out b/src/test/regress/expected/alter_table.out index 47d26ef632c..28afffad2f0 100644 --- a/src/test/regress/expected/alter_table.out +++ b/src/test/regress/expected/alter_table.out @@ -3911,8 +3911,16 @@ ALTER TABLE list_parted ATTACH PARTITION child FOR VALUES IN (1); ERROR: cannot attach inheritance child as partition ALTER TABLE list_parted ATTACH PARTITION parent FOR VALUES IN (1); ERROR: cannot attach inheritance parent as partition +DROP TABLE child; +-- now it should work, with a little tweak +ALTER TABLE parent ADD CONSTRAINT check_a CHECK (a > 0); +ALTER TABLE list_parted ATTACH PARTITION parent FOR VALUES IN (1); +-- test insert/update, per bug #18550 +INSERT INTO parent VALUES (1); +UPDATE parent SET a = 2 WHERE a = 1; +ERROR: new row for relation "parent" violates partition constraint +DETAIL: Failing row contains (2, null). DROP TABLE parent CASCADE; -NOTICE: drop cascades to table child -- check any TEMP-ness CREATE TEMP TABLE temp_parted (a int) PARTITION BY LIST (a); CREATE TABLE perm_part (a int); diff --git a/src/test/regress/sql/alter_table.sql b/src/test/regress/sql/alter_table.sql index 53e62c5ff9b..d1b9f067152 100644 --- a/src/test/regress/sql/alter_table.sql +++ b/src/test/regress/sql/alter_table.sql @@ -2413,6 +2413,13 @@ CREATE TABLE parent (LIKE list_parted); CREATE TABLE child () INHERITS (parent); ALTER TABLE list_parted ATTACH PARTITION child FOR VALUES IN (1); ALTER TABLE list_parted ATTACH PARTITION parent FOR VALUES IN (1); +DROP TABLE child; +-- now it should work, with a little tweak +ALTER TABLE parent ADD CONSTRAINT check_a CHECK (a > 0); +ALTER TABLE list_parted ATTACH PARTITION parent FOR VALUES IN (1); +-- test insert/update, per bug #18550 +INSERT INTO parent VALUES (1); +UPDATE parent SET a = 2 WHERE a = 1; DROP TABLE parent CASCADE; -- check any TEMP-ness