From f2787eafd72b455ef79bb51e534571ededd210b8 Mon Sep 17 00:00:00 2001 From: Sai Pranav Date: Mon, 23 Nov 2026 13:49:41 +0530 Subject: [PATCH v1] Invalidate partitions of a partitioned default partition When a partition is added to or removed from a partitioned table that has a default partition, we invalidate the relcache entry of the default partition, because its partition constraint depends on the bounds of every other partition. But if the default partition is itself partitioned, its partitions were not invalidated, even though the partition constraint cached for them (rd_partcheck) includes the default partition's constraint. Until the backend reconnected, a routed or direct insert into such a partition could fail for a row that now belongs to the default partition, or succeed for a row that now belongs to the new sibling partition, which partition pruning would then hide. Fix by also invalidating all descendants of the default partition, in the three places that change the set of its siblings: CREATE TABLE ... PARTITION OF and ATTACH PARTITION (StorePartitionBound), DETACH PARTITION (DetachPartitionFinalize) and DROP TABLE (heap_drop_with_catalog). This is what d6f1e16c8fe already does for the partitions of a partitioned table being attached or detached. Reported-by: Michael Ries Discussion: https://postgr.es/m/CAKWDHLgS=FzL=oOW3cXjM3ZWRWRSDH3fXN7Qm8fMCWZO5mrpGw@mail.gmail.com --- src/backend/catalog/heap.c | 4 ++-- src/backend/catalog/partition.c | 22 +++++++++++++++++ src/backend/commands/tablecmds.c | 2 +- src/include/catalog/partition.h | 1 + src/test/regress/expected/alter_table.out | 29 +++++++++++++++++++++++ src/test/regress/sql/alter_table.sql | 21 ++++++++++++++++ 6 files changed, 76 insertions(+), 3 deletions(-) diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c index c8ec7b47c6..3acf430845 100644 --- a/src/backend/catalog/heap.c +++ b/src/backend/catalog/heap.c @@ -2220,7 +2220,7 @@ heap_drop_with_catalog(Oid relid) * space previously covered by the dropped partition. */ if (OidIsValid(defaultPartOid) && relid != defaultPartOid) - CacheInvalidateRelcacheByRelid(defaultPartOid); + invalidate_default_partition(defaultPartOid); /* * Invalidate the parent's relcache so that the partition is no longer @@ -4458,7 +4458,7 @@ StorePartitionBound(Relation rel, Relation parent, PartitionBoundSpec *bound) defaultPartOid = get_default_oid_from_partdesc(RelationGetPartitionDesc(parent, true)); if (OidIsValid(defaultPartOid)) - CacheInvalidateRelcacheByRelid(defaultPartOid); + invalidate_default_partition(defaultPartOid); CacheInvalidateRelcache(parent); } diff --git a/src/backend/catalog/partition.c b/src/backend/catalog/partition.c index 5efa7d7fa8..62d8e49324 100644 --- a/src/backend/catalog/partition.c +++ b/src/backend/catalog/partition.c @@ -27,6 +27,7 @@ #include "optimizer/optimizer.h" #include "rewrite/rewriteManip.h" #include "utils/fmgroids.h" +#include "utils/inval.h" #include "utils/partcache.h" #include "utils/rel.h" #include "utils/syscache.h" @@ -364,6 +365,27 @@ update_default_partition_oid(Oid parentId, Oid defaultPartId) table_close(pg_partitioned_table, RowExclusiveLock); } +/* + * invalidate_default_partition + * + * Invalidate the relcache entry of a default partition after a sibling + * partition was added or removed, since its partition constraint depends on + * the bounds of every other partition. If the default partition is itself + * partitioned, do the same for all of its descendants: the partition + * constraint cached for them (rd_partcheck) includes the constraints of + * their ancestors. The caller must hold AccessExclusiveLock on the default + * partition; its descendants are locked the same way here. + */ +void +invalidate_default_partition(Oid defaultPartId) +{ + List *parts; + + parts = find_all_inheritors(defaultPartId, AccessExclusiveLock, NULL); + foreach_oid(partId, parts) + CacheInvalidateRelcacheByRelid(partId); +} + /* * get_proposed_default_constraint * diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index b670092945..8927f4bfb4 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -22825,7 +22825,7 @@ DetachPartitionFinalize(Relation rel, Relation partRel, bool concurrent, if (RelationGetRelid(partRel) == defaultPartOid) update_default_partition_oid(RelationGetRelid(rel), InvalidOid); else - CacheInvalidateRelcacheByRelid(defaultPartOid); + invalidate_default_partition(defaultPartOid); } /* diff --git a/src/include/catalog/partition.h b/src/include/catalog/partition.h index a93cf081dd..44013e1b2b 100644 --- a/src/include/catalog/partition.h +++ b/src/include/catalog/partition.h @@ -29,6 +29,7 @@ extern bool has_partition_attrs(Relation rel, Bitmapset *attnums, extern Oid get_default_partition_oid(Oid parentId); extern void update_default_partition_oid(Oid parentId, Oid defaultPartId); +extern void invalidate_default_partition(Oid defaultPartId); extern List *get_proposed_default_constraint(List *new_part_constraints); #endif /* PARTITION_H */ diff --git a/src/test/regress/expected/alter_table.out b/src/test/regress/expected/alter_table.out index f99b2b9e66..9a5aab5f31 100644 --- a/src/test/regress/expected/alter_table.out +++ b/src/test/regress/expected/alter_table.out @@ -5179,6 +5179,35 @@ DETAIL: Failing row contains (2, 1). -- ...and doesn't when the partition is detached along with its own partition alter table target_parted detach partition attach_parted; insert into attach_parted_part1 values (2, 1); +-- Likewise, adding or removing a sibling of a partitioned default partition +-- must update the partition constraint of the default partition's partitions +create table defpart_parent (a int) partition by list (a); +create table defpart_p1 partition of defpart_parent for values in (1); +create table defpart_p2 partition of defpart_parent for values in (2); +create table defpart_def partition of defpart_parent default + partition by list (a); +create table defpart_def_def partition of defpart_def default; +-- build the leaf partition's constraint and store it in the relcache +insert into defpart_def_def values (0); +-- the following inserts must succeed after the sibling is detached or dropped +alter table defpart_parent detach partition defpart_p1; +insert into defpart_parent values (1); +drop table defpart_p2; +insert into defpart_def_def values (2); +-- ...and this one must fail after a new sibling is created +create table defpart_p3 partition of defpart_parent for values in (3); +insert into defpart_def_def values (3); +ERROR: new row for relation "defpart_def_def" violates partition constraint +DETAIL: Failing row contains (3). +select tableoid::regclass, a from defpart_parent order by a; + tableoid | a +-----------------+--- + defpart_def_def | 0 + defpart_def_def | 1 + defpart_def_def | 2 +(3 rows) + +drop table defpart_parent, defpart_p1; -- Test altering table having publication create schema alter1; create schema alter2; diff --git a/src/test/regress/sql/alter_table.sql b/src/test/regress/sql/alter_table.sql index a6b4265b81..ae5974014b 100644 --- a/src/test/regress/sql/alter_table.sql +++ b/src/test/regress/sql/alter_table.sql @@ -3389,6 +3389,27 @@ insert into attach_parted_part1 values (2, 1); alter table target_parted detach partition attach_parted; insert into attach_parted_part1 values (2, 1); +-- Likewise, adding or removing a sibling of a partitioned default partition +-- must update the partition constraint of the default partition's partitions +create table defpart_parent (a int) partition by list (a); +create table defpart_p1 partition of defpart_parent for values in (1); +create table defpart_p2 partition of defpart_parent for values in (2); +create table defpart_def partition of defpart_parent default + partition by list (a); +create table defpart_def_def partition of defpart_def default; +-- build the leaf partition's constraint and store it in the relcache +insert into defpart_def_def values (0); +-- the following inserts must succeed after the sibling is detached or dropped +alter table defpart_parent detach partition defpart_p1; +insert into defpart_parent values (1); +drop table defpart_p2; +insert into defpart_def_def values (2); +-- ...and this one must fail after a new sibling is created +create table defpart_p3 partition of defpart_parent for values in (3); +insert into defpart_def_def values (3); +select tableoid::regclass, a from defpart_parent order by a; +drop table defpart_parent, defpart_p1; + -- Test altering table having publication create schema alter1; create schema alter2; -- 2.50.1 (Apple Git-155)