Partitioned index marked valid while a partition is pending detach

From: shihao zhong <zhong950419(at)gmail(dot)com>
To: pgsql-hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Cc: Álvaro Herrera <alvherre(at)kurilemu(dot)de>, Michael Paquier <michael(at)paquier(dot)xyz>
Subject: Partitioned index marked valid while a partition is pending detach
Date: 2026-11-04 02:46:35
Message-ID: CAGRkXqRJUOb--ZE4EB5=6XO7zECtBj-=09kp2m4g6Rx71ksbKQ@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi hackers,

A partitioned unique index can be marked valid while one partition has
no index. That partition then accepts duplicate keys.

create table t (a int) partition by list (a);
create table p1 partition of t for values in (1);
create table p2 partition of t for values in (2);
create table p3 partition of t for values in (3);
create unique index ti on only t (a);
create unique index p1_a on p1 (a);
create unique index p2_a on p2 (a);
alter index ti attach partition p1_a;

In another session, open a transaction and read from t. Run "alter
table t detach partition p1 concurrently". It waits for that
transaction. Cancel it, and p1 stays pending detach. Then:

alter index ti attach partition p2_a;

Now ti is valid, but p3 has no index.

Root cause:

validatePartitionedIndex() marks ti valid once it has as many valid
child indexes as t has partitions. It sees two child indexes, p1_a and
p2_a. It also sees two partitions, p2 and p3. It skips p1 because p1 is
pending detach. So two equals two and ti becomes valid, even though p3
has no index.

So we just need a one liner fix which is use false to count all partitions.
0001 is the fix and 0002 is the test on the existing detach test.

This need to go back to 14

Thanks,
Shihao

Attachment Content-Type Size
v1-0002-Test-partitioned-index-validity-while-a-partition-is.patch application/octet-stream 4.7 KB
v1-0001-Count-partitions-pending-detach-when-validating-a-pa.patch application/octet-stream 1.2 KB

Browse pgsql-hackers by date

  From Date Subject
Next Message Chao Li 2026-11-04 02:46:55 Re: -Wcast-qual home stretch
Previous Message Tomás Senart 2026-11-04 02:36:44 Re: [PATCH v1] Free oversized decoded WAL records in XLogReaderFree()