Stale partition constraint on sub-partitions of a partitioned DEFAULT partition after a sibling is detached/dropped/created

Started by Michael Ries10 days ago2 messagesbugs

Thread notes

1 note#bug
hackorum-bot#bug
hackorum-bot
2 days ago

@global #bug

#bug
Jump to latest
#1Michael Ries
mries@salesforce.com

While working on generative tests in the Trellis project, I came
across a bug in partitioned tables:
https://github.com/salesforce-misc/trellis/issues/2106

PostgreSQL versions: reproduced on 12.5 through 12.22, 13.0, 13.23,
14.25, 15.20,
16.16, 17.12, 18.7, 19.0 and master (1e676702cac). Platform: Linux x86_64,
built from source.

A leaf partition under a *partitioned* DEFAULT partition keeps its old
cached partition constraint (rd_partcheck) after a sibling of that DEFAULT
is detached, dropped, created or attached. Any backend that had already
built the leaf's constraint keeps using the old one until it reconnects.

Repro (single session):

create table t0 (pk bigint not null, g bigint not null, primary key (pk, g))
partition by list (g);
create table t0_p0 partition of t0 for values in (0);
create table t0_m1 partition of t0 default partition by list (g);
create table t0_m1_d partition of t0_m1 default;
insert into t0 values (1, 2); -- builds t0_m1_d's partition qual
alter table t0 detach partition t0_p0; -- or: drop table t0_p0;
insert into t0 values (2, 0);

Expected: INSERT 0 1 (g = 0 now belongs to the default, t0_m1 -> t0_m1_d).
Actual: ERROR: new row for relation "t0_m1_d" violates partition constraint
DETAIL: Failing row contains (2, 0).
After \c, the same insert succeeds. The same happens across sessions: A
inserts (1,2), B detaches t0_p0, then A's insert of (2,0) fails.

The opposite direction lets a row into the wrong partition (11.22 and master):

-- same setup, then in one session:
insert into t0_m1_d values (1, 2);
create table t0_p5 partition of t0 for values in (5);
insert into t0_m1_d values (3, 5); -- succeeds; should fail
select count(*) from t0 where g = 5; -- 0 (pruned)
select count(*) from t0_m1_d where g = 5; -- 1

Cause, as far as I can tell: generate_partition_qual() includes the
ancestors' quals, so the leaf's cached qual contains the DEFAULT's
"NOT (g IN (...))" bound. heap_drop_with_catalog(), DetachPartitionFinalize()
and StorePartitionBound() send a relcache invalidation for the default
partition (CacheInvalidateRelcacheByRelid(defaultPartOid)) but not for
its descendants when it is partitioned. ATTACH already invalidates the
attached table's descendants (d6f1e16c8fe); nothing does this for the
default partition's descendants when a sibling changes.

Bisect: the direct-insert variant (insert into t0_m1_d ...) fails on
every version since 11. The routed-insert variant above starts failing at

f481d2823297e692e9d44c9edfd18e626e768b0c
"Check default partitions constraints while descending" (2020-09-08;
12.5 / 13.0 / 14), Discussion:
/messages/by-id/CA+HiwqFqBmcSSap4sFnCBUEL_VfOMmEKaQ3gwUhyfa4c7J_-nA@mail.gmail.com

That commit added ExecPartitionCheck() on the default partition while
descending, so ordinary INSERTs through the root now evaluate the stale
cached qual. 11.22 and 12.0-12.4 accept the routed insert. DETACH ...
CONCURRENTLY doesn't apply (refused when a default partition exists).

Suggested fix: when invalidating the default partition's relcache because a
sibling's bounds changed, and the default is partitioned, also invalidate all
of its descendants (find_all_inheritors), as ATTACH does for the attached table.

#2Raja Sai pranav
rajasaipranav0@gmail.com
In reply to: Michael Ries (#1)
Re: Stale partition constraint on sub-partitions of a partitioned DEFAULT partition after a sibling is detached/dropped/created

On Wed, Nov 18, 2026, Michael Ries wrote:

A leaf partition under a *partitioned* DEFAULT partition keeps its old
cached partition constraint (rd_partcheck) after a sibling of that DEFAULT
is detached, dropped, created or attached.

Thanks for the clear report. I can reproduce both cases on master: the
insert of (2, 0) fails after the detach, and after creating t0_p5 a row
with g = 5 can still be inserted into t0_m1_d and is then hidden by
pruning.

Suggested fix: when invalidating the default partition's relcache because

a

sibling's bounds changed, and the default is partitioned, also invalidate

all

of its descendants (find_all_inheritors), as ATTACH does for the attached

table.

That is what the attached patch does. It adds a small helper,
invalidate_default_partition(), and uses it in the three places that
used to invalidate only the default partition: StorePartitionBound()
(CREATE ... PARTITION OF and ATTACH), DetachPartitionFinalize() and
heap_drop_with_catalog(). The helper locks the descendants with
AccessExclusiveLock, like the detach code already does for the
partitions of a detached partitioned table. In the CREATE case they are
already locked by check_default_partition_contents(). In the DROP and
DETACH cases this lock is new, so comments on that are welcome.

The patch adds a test to alter_table.sql next to the one from
d6f1e16c8fe. Without the code change the test fails at all three steps
(detach, drop, create). With it, the regress, isolation and pg_upgrade
tests pass. The patch also applies cleanly to REL_18_STABLE and
REL_17_STABLE, but I have only built and tested it on master.

Regards,
Sai Pranav

Attachments:

t259383_2
v1-0001-Invalidate-partitions-of-a-partitioned-default-pa.patchapplication/octet-stream; name=v1-0001-Invalidate-partitions-of-a-partitioned-default-pa.patchDownload+76-4