Re: [EXTERNAL] Re: BUG #18959: Name collisions of expression indexes during parallel Index creations on a pratitioned table. - Mailing list pgsql-bugs

From Dilip Kumar
Subject Re: [EXTERNAL] Re: BUG #18959: Name collisions of expression indexes during parallel Index creations on a pratitioned table.
Date
Msg-id CAFiTN-v+x4o_9C715FCYnvyKeqHMx1aYCTj=GGaJYaiW4PxBTQ@mail.gmail.com
Whole thread Raw
In response to BUG #18959: Name collisions of expression indexes during parallel Index creations on a pratitioned table.  (PG Bug reporting form <noreply@postgresql.org>)
List pgsql-bugs
On Thu, Jun 19, 2025 at 7:38 PM Chrzan, Maximilian
<maximilian.chrzan@here.com> wrote:
>
> We are working with very large partitioned tables (500M+ rows, >1 TB of data) and need to create multiple expression
indexeson them. 
>
> To avoid the issues with parallel index creation, we switched to sequential execution: as soon as one index finishes
(usuallyafter 1–2 hours), we immediately start the next (typically within a second). In this setup, there is no actual
parallelism— yet we occasionally still hit this error: 
>
> ERROR:  duplicate key value violates unique constraint "pg_class_relname_nsp_index"
> Detail: Key (relname, relnamespace) = (…) already exists.
>
> This suggests that the issue is not limited to concurrent execution. It can also occur when index creation happens in
quicksuccession. 
>
> Additionally, we noticed that two parallel index creations on a partitioned table will block each other — even if
theytarget different expressions. Here's a simplified example: 
>
> CREATE TABLE test (
>     jsondata JSONB,
>     version BIGINT NOT NULL DEFAULT 9223372036854775807
> ) PARTITION BY RANGE (version);
>
> CREATE TABLE test_p0 PARTITION OF test FOR VALUES FROM (0) TO (100000);
>
> Transaction 1:
>
> DO $$
> BEGIN
>     CREATE INDEX IF NOT EXISTS idx_1 ON test
>         (((jsondata -> 'properties') -> 'foo1') ASC NULLS LAST);
>     PERFORM pg_sleep(10);
> END;
> $$;
>
> Transaction 2 (started in parallel):
>
> DO $$
> BEGIN
>     CREATE INDEX IF NOT EXISTS idx_2 ON test
>         (((jsondata -> 'properties') -> 'foo2') ASC NULLS LAST);
> END;
> $$;
>
> Transaction 2 will block until Transaction 1 completes — and then fail with:

I believe this is fundamentally the same issue we're addressing here.
We're observing duplicate index name creation on child tables. If the
first transaction remains open, the second transaction waits for it to
commit or roll back because it's attempting to insert the same index
name key into the catalog. Once the first transaction commits, the
second will roll back due to a unique key violation. Conversely, if
the first transaction rolls back, the second will succeed.

--
Regards,
Dilip Kumar
Google



pgsql-bugs by date:

Previous
From: Ashutosh Bapat
Date:
Subject: Re: "unexpected duplicate for tablespace" problem in logical replication
Next
From: Junwang Zhao
Date:
Subject: Re: BUG #18959: Name collisions of expression indexes during parallel Index creations on a pratitioned table.