Partitioned index marked valid while a partition is pending detach

Поиск
Список
Период
Сортировка
Искать

Partitioned index marked valid while a partition is pending detach

От:
shihao zhong <zhong950419@gmail.com>
Дата:
Вложения:
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

Re: Partitioned index marked valid while a partition is pending detach

От:
Michael Paquier <michael@paquier.xyz>
Дата:
Вложения:
On Tue, Nov 03, 2026 at 09:46:35PM -0500, shihao zhong wrote:
> A partitioned unique index can be marked valid while one partition has
> no index. That partition then accepts duplicate keys.
>
> 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.

Obviously that's wrong.  indisvalid on a partitioned index should only
be switched to true if all its partition indexes are valid.  If a
partitioned table has an index but no partitions, the flag could be
true.

> 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.

Yeah, I think that you are right here.  I was wondering if the test
should be simplified a bit, without a third partition, but what you
are doing may be cleaner overall.  The last "s2detachfinal s1valid"
steps in the new permutation don't seem strongly necessary if you want
to check your case.  Or do you specifically want to have coverage of a
FINALIZE with an attached index?  I would cut these two steps, just
wondering if you have an argument in favor of keeping them.

On top of that, indexcmds.c uses RelationGetPartitionDesc(true) as
well.  Could that be a source of problems when creating a unique index
on a partitioned table, for example?  It also looks like we could
finish with a broken unique constraint with duplicated data inserted
due to a miscount of the number of partitions, with a indisflag marked
as valid when we should not mark it as such?  Perhaps this should have
a separate investigation.
--
Michael

Re: Partitioned index marked valid while a partition is pending detach

От:
shihao zhong <zhong950419@gmail.com>
Дата:
Вложения:
Hi Michael,

Thanks for reviewing my patch..

> The last "s2detachfinal s1valid" steps in the new permutation don't
> seem strongly necessary if you want to check your case.  Or do you
> specifically want to have coverage of a FINALIZE with an attached
> index?  I would cut these two steps, just wondering if you have an
> argument in favor of keeping them.

Agreed. Those two steps check the index after FINALIZE,
and they pass without the fix. So v2 drops them.

> On top of that, indexcmds.c uses RelationGetPartitionDesc(true) as
> well. Could that be a source of problems when creating a unique index
> on a partitioned table, for example?

Yes. If CREATE UNIQUE INDEX runs while p1 is pending detach, p1 gets
no index but the parent index is marked valid. An old snapshot can
then insert duplicate keys into p1 through t. CREATE INDEX ON ONLY
has the same problem.

Building the index on p1 does not work, because the rest of ALTER
TABLE skips p1. ADD PRIMARY KEY would fail, since p1 gets no NOT NULL
constraint. So 0003 makes CREATE INDEX on t fail while a partition
is pending detach, with the same hint ALTER TABLE gives. 

This is a little trick because it did change the behavior, I dont think user
want to create an incorrect PK and running FINALIZE first is easy, 
so I think this is fine to backport.


Thanks,
Shihao

Re: Partitioned index marked valid while a partition is pending detach

От:
Michael Paquier <michael@paquier.xyz>
Дата:
Вложения:
On Wed, Nov 04, 2026 at 11:54:26PM -0500, shihao zhong wrote:
> Agreed. Those two steps check the index after FINALIZE,
> and they pass without the fix. So v2 drops them.

The comments still feel a bit confusing to me, but that can be tweaked
infinitely.  Saying that, the permutation steps look much cleaner now.
I'll go process this part, barring objections and/or comments of any
kind.

>> On top of that, indexcmds.c uses RelationGetPartitionDesc(true) as
>> well. Could that be a source of problems when creating a unique index
>> on a partitioned table, for example?
> 
> Yes. If CREATE UNIQUE INDEX runs while p1 is pending detach, p1 gets
> no index but the parent index is marked valid. An old snapshot can
> then insert duplicate keys into p1 through t. CREATE INDEX ON ONLY
> has the same problem.
> 
> This is a little trick because it did change the behavior, I dont think user
> want to create an incorrect PK and running FINALIZE first is easy,
> so I think this is fine to backport.

+   if (PartitionHasPendingDetach(pd->oids[i]))
+       ereport(ERROR,
+               (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+                errmsg("cannot create index on partitioned table \"%s\"",
+                       RelationGetRelationName(rel)),
+                errdetail("Partition \"%s\" has an incomplete detach.",
+                          get_rel_name(pd->oids[i])),
+                errhint("Use ALTER TABLE ... DETACH PARTITION ... FINALIZE to complete the pending detach operation.")));

Still returning an error may be better than breaking a tree
consistency and even worse, break constraints under-the-hood.  I am
not sure if there is a better option here..  Alvaro, what do you
think?
--
Michael

Re: Partitioned index marked valid while a partition is pending detach

От:
Alvaro Herrera <alvherre@kurilemu.de>
Дата:
On 2026-Nov-05, Michael Paquier wrote:

> On Wed, Nov 04, 2026 at 11:54:26PM -0500, shihao zhong wrote:

> > This is a little trick because it did change the behavior, I dont think user
> > want to create an incorrect PK and running FINALIZE first is easy,
> > so I think this is fine to backport.
> 
> +   if (PartitionHasPendingDetach(pd->oids[i]))
> +       ereport(ERROR,
> +               (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
> +                errmsg("cannot create index on partitioned table \"%s\"",
> +                       RelationGetRelationName(rel)),
> +                errdetail("Partition \"%s\" has an incomplete detach.",
> +                          get_rel_name(pd->oids[i])),
> +                errhint("Use ALTER TABLE ... DETACH PARTITION ... FINALIZE to complete the pending detach operation.")));
> 
> Still returning an error may be better than breaking a tree
> consistency and even worse, break constraints under-the-hood.  I am
> not sure if there is a better option here..  Alvaro, what do you
> think?

I think this idea is good generally speaking, but we shouldn't limit it
to create index, but instead apply it to pretty much any DDL that you
want to run on such a table.  The risk of inconsistencies is just too
high and there's no functional reason to let users do anything before
finishing that detach.

I'm happy to listen to example scenarios where my argument fails, to
assess if any exceptions are needed.

-- 
Álvaro Herrera         PostgreSQL Developer  —  https://www.EnterpriseDB.com/
"Por suerte hoy explotó el califont porque si no me habría muerto
 de aburrido"  (Papelucho)


Re: Partitioned index marked valid while a partition is pending detach

От:
Michael Paquier <michael@paquier.xyz>
Дата:
Вложения:
On Thu, Nov 05, 2026 at 09:05:52AM +0100, Alvaro Herrera wrote:
> I think this idea is good generally speaking, but we shouldn't limit it
> to create index, but instead apply it to pretty much any DDL that you
> want to run on such a table.  The risk of inconsistencies is just too
> high and there's no functional reason to let users do anything before
> finishing that detach.

Yeah, I'd suspect that we have more cases like this one, so a more
common water-proof approach could be adapted so as all current DDLs
don't silently fail while future DDLs don't fall into this trap.

It also sounds to me that changing only validatePartitionedIndex() is
not the thing we should do, as we're only taking care of one code
path.  Perhaps we should try to work around simplifying
RelationGetPartitionDesc() instead, and try to get rid of the
omit_detached idea entirely?
--
Michael

Re: Partitioned index marked valid while a partition is pending detach

От:
shihao zhong <zhong950419@gmail.com>
Дата:
Вложения:
On Fri, Nov 06, 2026 at 10:58:41AM +0900, Michael Paquier wrote:
> Perhaps we should try to work around simplifying
> RelationGetPartitionDesc() instead, and try to get rid of the
> omit_detached idea entirely?

Thanks, that made the fix much simpler. v4-0003 removes the
omit_detached argument of RelationGetPartitionDesc(), so DDL on the
parent now reaches a partition pending detach. FINALIZE then works
after ADD CHECK or RENAME COLUMN on the parent. This holds when
the partition is two levels down too.

I could not delete omit_detached entirely. Queries still need it, and
without it detach-partition-concurrently-1 to 4 fail. 

0001 and 0002 work as is on 15 to 19. 0003 changes an API, so master only

Thanks,
Shihao

Re: Partitioned index marked valid while a partition is pending detach

От:
surya poondla <suryapoondla4@gmail.com>
Дата:
Hi Shihao,

Thanks for the findings and the patch.
I tested all three v3 patches together, and 0001 by itself as this could be backported.
make check and the isolation tests pass in both cases.  

With only 0001, if p1's index isn't attached yet, the parent index stays invalid even after FINALIZE:

    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 unique index ti on only t (a);
    create unique index p1_a on p1 (a);
    create unique index p2_a on p2 (a);
    -- p1 pending detach
    alter index ti attach partition p2_a;
    alter index ti attach partition p1_a;
    ERROR:  cannot attach index "p1_a" as a partition of index "ti"
    alter table t detach partition p1 finalize;   -- ti still invalid

ATExecAttachPartitionIdx() still skips p1, and FINALIZE doesn't revalidate.  
Re-attaching p2_a fixes it, but nobody would guess that.
Maybe FINALIZE should revalidate the parent's indexes?  
Michael, this affects pushing 0001/0002 alone.

As you noted, without 0003 FINALIZE fails after RENAME or ADD COLUMN
on t, so that stays broken on the back branches.  DROP COLUMN is worse as FINALIZE succeeds but p1 keeps the column as inherited:

    create table t (a int, b 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);
      -- p1 pending detach
      alter table t drop column b;
      alter table t detach partition p1 finalize;
      alter table p1 drop column b;
      ERROR:  cannot drop inherited column "b"
      create table t9 (a int, b int) partition by list (a);
      alter table t9 attach partition p1 for values in (1);
      TRAP: failed Assert("child_att->attinhcount == 1")

Alvaro's idea of rejecting DDL while a detach is pending looks like what the back branches need.

With 0003, SET DEFAULT, SET STATISTICS and ALTER COLUMN TYPE on t now
fail with "cannot alter partition "p1" with an incomplete detach", while ADD COLUMN etc. go through to p1.  If that's intended, the spec
should cover it.  Also pg_publication_tables now lists p1 but pg_partition_tree() doesn't.

Regards,
Surya Poondla

Re: Partitioned index marked valid while a partition is pending detach

От:
shihao zhong <zhong950419@gmail.com>
Дата:
Вложения:
Hi Surya,

Thanks for testing each patch on its own. v4 is attached.

1. With only 0001 the index stays invalid after FINALIZE, which is safe. A plain DETACH on master does the same, so I would fix that separately. I don't think it blocks 0001 and 0002.

2. v4-0003 now lets ALTER TABLE on the parent reach p1, and the spec covers it. pg_publication_tables leaves p1 out again, like pg_partition_tree(). This also fixes your DROP COLUMN case.

3. For the stable branches I would rather not refuse DDL. A table already hit by this needs DDL on the parent to repair it, and that would be refused too. But I am happy to go with what Michael and Alvaro prefer.

Thanks,
Shihao

On Mon, Nov 09, 2026 07:51 PM, surya poondla <suryapoondla4@gmail.com> wrote:
Hi Shihao,

Thanks for the findings and the patch.
I tested all three v3 patches together, and 0001 by itself as this could be backported.
make check and the isolation tests pass in both cases.  

With only 0001, if p1's index isn't attached yet, the parent index stays invalid even after FINALIZE:

    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 unique index ti on only t (a);
    create unique index p1_a on p1 (a);
    create unique index p2_a on p2 (a);
    -- p1 pending detach
    alter index ti attach partition p2_a;
    alter index ti attach partition p1_a;
    ERROR:  cannot attach index "p1_a" as a partition of index "ti"
    alter table t detach partition p1 finalize;   -- ti still invalid

ATExecAttachPartitionIdx() still skips p1, and FINALIZE doesn't revalidate.  
Re-attaching p2_a fixes it, but nobody would guess that.
Maybe FINALIZE should revalidate the parent's indexes?  
Michael, this affects pushing 0001/0002 alone.

As you noted, without 0003 FINALIZE fails after RENAME or ADD COLUMN
on t, so that stays broken on the back branches.  DROP COLUMN is worse as FINALIZE succeeds but p1 keeps the column as inherited:

    create table t (a int, b 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);
      -- p1 pending detach
      alter table t drop column b;
      alter table t detach partition p1 finalize;
      alter table p1 drop column b;
      ERROR:  cannot drop inherited column "b"
      create table t9 (a int, b int) partition by list (a);
      alter table t9 attach partition p1 for values in (1);
      TRAP: failed Assert("child_att->attinhcount == 1")

Alvaro's idea of rejecting DDL while a detach is pending looks like what the back branches need.

With 0003, SET DEFAULT, SET STATISTICS and ALTER COLUMN TYPE on t now
fail with "cannot alter partition "p1" with an incomplete detach", while ADD COLUMN etc. go through to p1.  If that's intended, the spec
should cover it.  Also pg_publication_tables now lists p1 but pg_partition_tree() doesn't.

Regards,
Surya Poondla

Re: Partitioned index marked valid while a partition is pending detach

От:
shihao zhong <zhong950419@gmail.com>
Дата:
Вложения:
Hi Surya,

Thanks for testing each patch on its own. 

0001 alone leaves the index invalid, which is safe. A plain DETACH on
master does the same, so I would handle that separately.

v4-0003 now lets ALTER TABLE on the parent reach p1, and
pg_publication_tables leaves p1 out again. This also fixes the DROP
COLUMN case. I would rather not refuse DDL on the stable branches.

But I am happy to go with what Michael and Alvaro prefer.

Thanks,
Shihao
 

Re: Partitioned index marked valid while a partition is pending detach

От:
surya poondla <suryapoondla4@gmail.com>
Дата:
Hi Shihao,

Thanks for v4.  I tested it on the current master, it applies cleanly, make check and the
isolation tests pass.  

It fixes the 0003 cases I sent( SET STATISTICS, ALTER COLUMN TYPE) now reaches p1 and FINALIZE works
afterwards.  It also works when p1 is itself partitioned.  
I agree with leaving the 0001-only invalid index for a separate patch, since plain DETACH has the same gap.

Two small things on 0003:
1) The spec covers ADD CHECK and SET DEFAULT, but not the cases that
actually broke before.  Could you add ALTER COLUMN TYPE and DROP COLUMN on the parent followed by FINALIZE?

2) Parent DDL now locks the pending partition too, so a session that is
only reading p1 now blocks ALTER TABLE on t, and anything queued behind it on t.  That makes sense, but I think the commit message
should mention it, and maybe a sentence in the FINALIZE part of
alter_table.sgml saying ALTER TABLE on the parent also applies to a
partition pending detach. Just a suggestion not a blocker for me.

Overall the patch looks good.

Regards,
Surya Poondla

Re: Partitioned index marked valid while a partition is pending detach

От:
shihao zhong <zhong950419@gmail.com>
Дата:
Вложения:
Hi Surya,

Thanks for reviewing v4. Just realize I accidentally sent the v4 twice....

v5 address your 003 comments.

> 1)...
Added new ALTER COLUMN TYPE test

> 2)....
I added one sentence in alter_talbe.sgml to talk about that case.

Thanks,
Shihao

Re: Partitioned index marked valid while a partition is pending detach

От:
surya poondla <suryapoondla4@gmail.com>
Дата:

 Hi Shihao,

Thanks for v5.  I tested it on current master, make check and the
isolation tests pass, and both of my v4 points are covered (the new
spec step does ALTER TYPE and DROP COLUMN before FINALIZE, and the
locking is mentioned in the docs and the commit message).  

I also re-ran my earlier reproducers, including the DROP COLUMN one and a
sub-partitioned p1, and they all behave correctly now.

v5 looks good to me.

Regards,
Surya Poondla
FAQ