| From: | Chao Li <li(dot)evan(dot)chao(at)gmail(dot)com> |
|---|---|
| To: | Zhijie Hou <houzhijie22(at)gmail(dot)com> |
| Cc: | "pgsql-hackers(at)lists(dot)postgresql(dot)org" <pgsql-hackers(at)lists(dot)postgresql(dot)org>, Álvaro Herrera <alvherre(at)kurilemu(dot)de> |
| Subject: | Re: REPACK (CONCURRENTLY) fails during parallel build of exclusion constraint indexes |
| Date: | 2026-12-01 01:06:36 |
| Message-ID: | D2372352-26AF-4FB9-99D4-47052C2DFC4A@gmail.com |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-hackers |
> On Nov 28, 2026, at 00:08, Zhijie Hou <houzhijie22(at)gmail(dot)com> wrote:
>
> Hi,
>
> While testing REPACK (CONCURRENTLY), I noticed that it fails when rebuilding an exclusion constraint index with parallel workers:
>
> CREATE TABLE rpk_excl (
> id int PRIMARY KEY,
> a int,
> EXCLUDE USING btree (a WITH =)
> ) WITH (parallel_workers = 2);
> INSERT INTO rpk_excl VALUES (1, 10), (2, 20);
> REPACK (CONCURRENTLY) rpk_excl;
> ERROR: exclusion constraint record missing for rel rpk_excl_a_excl_repacknew
>
> In build_new_indexes(), index_create_copy() is called without INDEX_CREATE_SKIP_BUILD before copy_index_constraints(). As a result, index_create() inserts the pg_index entry for the new index with indisexclusion set to true, advances the command counter, and immediately runs index_build() before copy_index_constraints() creates the corresponding pg_constraint entry on the transient table. When the parallel index build workers open the new index and call BuildIndexInfo(), RelationGetExclusionInfo() cannot find the pg_constraint entry on the transient table and raises the error above.
This exactly matches what I saw from my debugging.
>
> The attached patch fixes this by passing INDEX_CREATE_SKIP_BUILD to index_create_copy(), copying the index constraints onto the transient table first, and then invoking index_build(). This issue affects master and REL_19_STABLE.
I think the direction of the fix is correct. It moves the index_build() part out of index_create_copy(), so that index_create_copy() only creates the new index, then copy_index_constraints() creates constraints for the new index by copying them from the old index. After that, index_build() has all the information it needs to build the index.
I noticed that there are two call sites of index_create_copy(), and after this patch, both use INDEX_CREATE_SKIP_BUILD. So I'm thinking that maybe index_create_copy() should always skip index building, which may help prevent similar bugs in the future. WDYT? For this point, I just wonder your opinion, rather than asking you to make this change as part of this patch.
> --
> Best Regards,
> Zhijie Hou
> <v1-0001-Fix-parallel-build-of-exclusion-constraint-indexe.patch>
Overall v1 looks good to me. A few nit comments:
1
```
+ newind = index_open(newindex, NoLock);
+ index_build(NewHeap, newind, BuildIndexInfo(newind), false, true,
+ false);
+ index_close(newind, NoLock);
```
When I first read this code, my initial thought was that building the new index doesn't require a lock. But after tracing into index_create_copy(), I found that the new index is already locked there. So, I'd suggest adding a brief comment to explain why NoLock is used with index_open(), for example:
```
/* The new index is already locked by index_create_copy(). */
newind = index_open(newindex, NoLock);
```
2
```
+CREATE TABLE rpk_excl (
+ id int PRIMARY KEY,
+ a int,
+ EXCLUDE USING btree (a WITH =)
+) WITH (parallel_workers = 2);
```
The test intentionally sets parallel_workers = 2, as this bug only affects parallel workers. But the number of parallel workers is still limited by max_parallel_maintenance_workers. If a previous test sets max_parallel_maintenance_workers to 0, REPACK will run the index build in serial mode, and the test will pass even without this fix. So, maybe explicitly set max_parallel_maintenance_workers = 2 before the test and reset it after the test.
3
```
+ newindex = index_create_copy(NewHeap,
+ INDEX_CREATE_SKIP_BUILD |
+ INDEX_CREATE_SUPPRESS_PROGRESS,
oldindex, ind->rd_rel->reltablespace,
newName);
```
I think INDEX_CREATE_SUPPRESS_PROGRESS only affects index_build(), so when INDEX_CREATE_SKIP_BUILD is specified, INDEX_CREATE_SUPPRESS_PROGRESS becomes redundant and can be removed.
4
```
+ /*
+ * Create the catalog entries for the new index without building it
+ * yet, and copy any associated constraints first. BuildIndexInfo()
+ * (called below and in parallel index build workers) looks up the
+ * exclusion constraint record in pg_constraint when indisexclusion is
+ * set.
+ */
```
My feeling about this comment is that it's more of an explanation of what this patch does than a natural comment for this piece of code. Not sure if other folks have the same feeling.
I'd suggest rephrasing it as:
```
/*
* Defer building the index until its associated constraints have been
* copied, so that BuildIndexInfo() can find the exclusion constraint
* record in pg_constraint.
*/
```
Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/
| From | Date | Subject | |
|---|---|---|---|
| Next Message | shihao zhong | 2026-12-01 01:21:46 | hashname() ignores nondeterministic collations |
| Previous Message | shihao zhong | 2026-12-01 01:01:01 | Re: [PATCH] Planner support function for generate_subscripts() |