Re: BUG #19817: CREATE UNIQUE INDEX on a partitioned table can silently attach an invalid, not-ready child index, le

From: Joao Rito <joaovictor(dot)rito(at)gmail(dot)com>
To: Dilip Kumar <dilipbalaut(at)gmail(dot)com>
Cc: shihao zhong <zhong950419(at)gmail(dot)com>, Kirill Reshke <reshkekirill(at)gmail(dot)com>, 303677365(at)qq(dot)com, pgsql-bugs(at)lists(dot)postgresql(dot)org
Subject: Re: BUG #19817: CREATE UNIQUE INDEX on a partitioned table can silently attach an invalid, not-ready child index, le
Date: 2026-11-16 14:02:39
Message-ID: 20261116140239.1145-1-joaovictor.rito@gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

Hi Dilip,

Thank you for the patch. I tested v3-0001 on current master. It
fixes the report for me. These are my results.

Setup: master at 7fc2f0423320 (2026-11-16), Debian, gcc 14,
configure with --enable-cassert --enable-debug
--enable-tap-tests --without-icu. I ran the cases on plain
master, then applied v3-0001, rebuilt, and ran them again.

Reproduction from the report (b2_root / b2_p1):

- Without the patch, CREATE UNIQUE INDEX b2_root_ui returns
CREATE INDEX. b2_p1_bad is attached to b2_root_ui with
indisvalid = f and indisready = f, and b2_root_ui has
indisvalid = f. The insert of (1,'x'),(1,'x') succeeds (3 rows
with a = 1), and ON CONFLICT (a) fails with "there is no unique
or exclusion constraint matching the ON CONFLICT
specification".
- With the patch, a new leaf index b2_p1_a_idx is built, it is
attached, and b2_root_ui is valid. The duplicate insert fails
with "duplicate key value violates unique constraint
"b2_p1_a_idx"". ON CONFLICT (a) DO NOTHING works. The old
b2_p1_bad stays behind as an invalid, unattached index, and
it can be dropped.

Other cases, all with the patch. Shihao already confirmed
the PK case on v3; I list it for completeness:

- A three-level tree (root, a partitioned mid level, two leaves
under it, plus one leaf directly under root) with a leftover
invalid index on one lower leaf. Both partitioned indexes are
valid, and the leftover is not attached. The unique index
rejects a duplicate on the two leaves I tried (the one with
the leftover and its sibling).
- ALTER TABLE ONLY s ADD PRIMARY KEY (a) on a partitioned
sub-table, then ALTER TABLE r ADD PRIMARY KEY (a), and then
ADD PRIMARY KEY on the leaf. No error. This is the case
Shihao raised on v2 and confirmed on v3. The output is the
same as on master.
- ALTER TABLE ... ATTACH PARTITION with a leftover invalid
index on the table being attached: master and patched give
the same result (a new valid index is attached, the leftover
stays out).
- CREATE UNIQUE INDEX ... ON ONLY, CIC on each leaf, then
ALTER INDEX ... ATTACH PARTITION: the parent becomes valid.
CIC directly on the partitioned table still gives the same
error as before.
- A failed non-unique CIC (expression 10/a with a row a = 0),
then CREATE INDEX on the parent: the invalid leaf index is
skipped as well, and a new valid one is attached.
- A leftover named s8_p1_a_idx (failed unique CIC), then
CREATE UNIQUE INDEX on the parent: the new child is named
s8_p1_a_idx1, so there is no name collision.
- After removing the duplicate with the leftover still in
place, REINDEX INDEX CONCURRENTLY on the leftover makes it
valid, and a later CREATE UNIQUE INDEX on the parent attaches
it. If the duplicate is still there, CREATE UNIQUE INDEX
on the parent fails on the leaf and leaves nothing behind.
- pg_dump of the patched result restores with no errors. The
restored database has 2 invalid indexes, and the original
has 7 (5 leftovers and 2 ON ONLY parents). I did not list
the 2; I expect they are the ON ONLY parents from the
primary key case. The duplicate insert fails in the
restored database.
- amcheck bt_index_check(..., heapallindexed) passes on the
13 valid btree indexes from these cases.

Test suites with the patch:

- make check: all 243 tests passed.
- make -C src/test/isolation check: all 139 tests passed.
- contrib/amcheck check: all 4 tests passed.
- src/bin/pg_dump t/002_pg_dump.pl: 13361 tests, all passed.

The patch applies with git apply and "git diff --check" is
clean. The build has no warnings.

On the change: skipping only invalid indexes that are not
partitioned indexes looks right to me. It keeps the ON ONLY
partitioned indexes attachable, and it matches the ATTACH
PARTITION path, which already skips invalid indexes
(e90e9275f56). The new test expects errors in the right places
(the CIC on duplicate keys, the parent CREATE UNIQUE INDEX and
the duplicate insert). It uses a duplicate key, not the
expression index from v1 that Kirill found to be flaky. I ran
make check only once, so I cannot say more about flakiness.

Two small points, both optional:

- The leftover invalid index stays in place, and the command
does not say so. A NOTICE or a doc sentence that says to
drop the leftover would help users who see two indexes on
the same column. The new doc sentence, "Invalid indexes on
leaf partitions are skipped.", could say "are skipped, not
attached and not dropped".
- The commit message says that the broken child "could no
longer be dropped directly" once attached. I did not test
that on the unpatched tree, so I take it from the message.

What I did not test:

- Other branches (the report says 18.6 is affected too), so I
cannot say how the patch looks as a backpatch.
- Concurrent DDL or DML while CREATE INDEX recurses over the
partitions.
- Hash and list partitioned tables, non-btree access methods,
partial indexes, and indexes with INCLUDE columns.
- Exclusion constraints on partitioned tables.
- Tables with a large number of partitions, and performance.
- Standby or logical replication behaviour.
- Windows and 32-bit builds.
- A run of make check on plain master without the patch.
- Builds with ICU (I used --without-icu).

Thanks,
Joao Rito

In response to

Browse pgsql-bugs by date

  From Date Subject
Next Message Nisha Moond 2026-11-16 15:15:40 Re: BUG #19892: Logical replication skips UPDATE/DELETE of rows with NULL in a hash-indexed subscriber column
Previous Message Rajesh Kumar Thandapani 2026-11-16 13:36:18 Re: Re: Re: BUG #19776: pg_combinebackup ignores the truncation_block_length of an intermediate incremental backup