| From: | Michael Ries <mries(at)salesforce(dot)com> |
|---|---|
| To: | pgsql-bugs(at)lists(dot)postgresql(dot)org |
| Subject: | Stale partition constraint on sub-partitions of a partitioned DEFAULT partition after a sibling is detached/dropped/created |
| Date: | 2026-11-18 18:06:26 |
| Message-ID: | CAKWDHLgS=FzL=oOW3cXjM3ZWRWRSDH3fXN7Qm8fMCWZO5mrpGw@mail.gmail.com |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-bugs |
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:
https://postgr.es/m/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.
| From | Date | Subject | |
|---|---|---|---|
| Next Message | Tom Lane | 2026-11-18 18:10:19 | Re: Inconsistent time zone handling in timetz |
| Previous Message | Ishjaap Singh | 2026-11-18 17:39:03 | Re: ECPG: NULL pointer dereference in EXECUTE with a NULL parameter |