ATTACH PARTITION leaves a parent index valid over an invalid subpartition index =============================================================================== AI-generated bug report from the AI Annex Report ID: 01a1cbeb-fc5f-75e6-9767-03f967054166 Filed: 2026-11-11 18:43:22 UTC Severity: 80/100 Blamed commit: 8b08f7d4820fd7a8ef6152a9dd8c6e3cb01e5f99 ATTACH PARTITION of a sub-partitioned table can leave the parent's primary key or unique index marked valid while a descendant index is invalid and a leaf partition has no index at all. Inserts into the parent then accept duplicate keys. The planner trusts the valid parent index and can remove joins, so queries return the wrong row count. Creating a foreign key to the parent fails, and ON CONFLICT cannot use the index. Repairing the leaf later with ALTER INDEX ATTACH PARTITION reaches an assertion failure in validatePartitionedIndex because that code requires every ancestor index to be invalid. The sequence is an intermediate partition that itself has subpartitions, with a primary key or unique index created with ONLY on one of those subpartitions, followed by attaching the intermediate table to a parent that already has a valid index on the same key. DefineIndex, called from AttachPartitionEnsureIndexes, matches the invalid ONLY index and attaches it. It clears indisvalid only on the index it just created for the attach. The index that already existed on the top parent stays valid. The leaf under the ONLY index is never given an index, so uniqueness is not enforced there. This dates from 8b08f7d4820fd7a8ef6152a9dd8c6e3cb01e5f99 ("Local partitioned indexes"), which taught DefineIndex to adopt an existing matching child index and to clear indisvalid on the new parent when that child was invalid. That is the right behavior when CREATE INDEX is building the parent: the new index should not be advertised as valid over an incomplete tree. It is the wrong behavior when the parent index already exists and is valid, which is the ATTACH PARTITION case. Later, fc55c7ff8d122feb48aae5889e26499ffe53c0b7 stopped ATTACH PARTITION from reusing an invalid index on the table being attached directly. The same check does not run for indexes on subpartitions, and the top parent index is not the one whose indisvalid bit is cleared. Affected branches are master and REL_14_STABLE through REL_19_STABLE. ## Reproduction ```sql CREATE TABLE p (a int PRIMARY KEY) PARTITION BY LIST (a); CREATE TABLE p2 (a int NOT NULL) PARTITION BY LIST (a); CREATE TABLE p21 PARTITION OF p2 FOR VALUES IN (1) PARTITION BY LIST (a); CREATE TABLE p211 PARTITION OF p21 FOR VALUES IN (1); ALTER TABLE ONLY p21 ADD PRIMARY KEY (a); ALTER TABLE p ATTACH PARTITION p2 FOR VALUES IN (1); SELECT c.relname, i.indisvalid, pp.relname AS parent FROM pg_index i JOIN pg_class c ON c.oid = i.indexrelid LEFT JOIN pg_inherits inh ON inh.inhrelid = i.indexrelid LEFT JOIN pg_class pp ON pp.oid = inh.inhparent WHERE c.relnamespace = 'public'::regnamespace ORDER BY 1; INSERT INTO p VALUES (1); INSERT INTO p VALUES (1); CREATE TABLE fk (a int REFERENCES p); INSERT INTO p VALUES (1) ON CONFLICT (a) DO NOTHING; CREATE TABLE t (x int); INSERT INTO t VALUES (1); SELECT count(*) FROM t LEFT JOIN p ON p.a = t.x; ``` Expected: every index in the p_pkey tree is valid, including a new index on p211, and the second insert fails with a duplicate-key error. The foreign key is created, ON CONFLICT does nothing, and the join count is 2 when the leaf holds two copies of the key. Actual: p_pkey stays indisvalid, the ONLY index on p21 and the index created on p2 are invalid, and p211 has no index. Both inserts succeed. The foreign key fails with "index for not found in partition p211". ON CONFLICT fails with "invalid arbiter index list". With two duplicate rows stored, the join count is 1 because the planner removes the join. The same shape occurs for a non-primary unique index created with ONLY. Repairing the hole trips an assertion: ```sql ALTER TABLE p211 ADD PRIMARY KEY (a); ALTER INDEX p21_pkey ATTACH PARTITION p211_pkey; ``` ``` TRAP: failed Assert("!parentIdx->rd_index->indisvalid"), File: "tablecmds.c", Line: 23304 ``` If the leaf already contains duplicates, the right outcome is for ATTACH PARTITION to fail while building the missing unique index and to roll back, rather than to commit a valid parent over those rows. ## Diagnosis AttachPartitionEnsureIndexes skips an invalid index only when it is on the relation being attached. For any other partition it calls DefineIndex. Inside DefineIndex the child-match loop treats an invalid partitioned index as a successful match, sets invalidate_parent, and attaches it. The flag only updates the index DefineIndex is creating. validatePartitionedIndex later walks that chain and asserts that no parent is already valid, which is how the repair command aborts. CREATE INDEX on a partitioned table must keep attaching an invalid child and marking the new parent invalid. A failed CREATE INDEX CONCURRENTLY on a leaf, followed by CREATE INDEX on the top table, depends on that, as does adding a primary key when a partition already has an invalid ONLY index that should be reused and should keep the new parent invalid. ## Fix Give ATTACH PARTITION a separate entry point that does not adopt an invalid leaf. When the match is an invalid partitioned index, build the missing descendant indexes and mark that index valid, then attach it. If the missing unique index cannot be built, the attach fails and rolls back. If no invalid partitioned index can be completed, ignore it and create a new index instead of clearing indisvalid on an index whose parent is already valid. CREATE INDEX is unchanged. Severity is 80. Duplicate keys and a removed join are wrong results, which is the floor for this class of bug. The statement sequence is uncommon multi-level partition DDL, so the score stays at that floor rather than moving up.