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.
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.
--
Best Regards,
Zhijie Hou
From 8c76d6d16bcb1ec2034d314f38b65b69aaa05b48 Mon Sep 17 00:00:00 2001
From: Zhijie Hou <[email protected]>
Date: Fri, 27 Nov 2026 23:20:00 +0800
Subject: [PATCH v1] Fix parallel build of exclusion constraint indexes in
REPACK (CONCURRENTLY).
During REPACK (CONCURRENTLY), build_new_indexes() called
index_create_copy() before copy_index_constraints(). Because
index_create_copy() did not set INDEX_CREATE_SKIP_BUILD, index_create()
inserted the pg_index entry for the new index with indisexclusion set to
true, advanced the command counter, and immediately invoked
index_build() before copy_index_constraints() had created the matching
pg_constraint entry on the transient table.
When index_build() launched parallel workers for an exclusion constraint
index (for example, EXCLUDE USING btree), the parallel workers called
BuildIndexInfo() and RelationGetExclusionInfo() on the new index, failed
to find its pg_constraint entry on the transient table, and errored out
with:
ERROR: exclusion constraint record missing for rel <index>
Fix 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().
Author: Zhijie Hou <[email protected]>
Backpatch-through: 19
---
contrib/test_decoding/expected/repack.out | 20 +++++++++++++++++
contrib/test_decoding/sql/repack.sql | 13 +++++++++++
src/backend/commands/repack.c | 27 ++++++++++++++++++-----
3 files changed, 55 insertions(+), 5 deletions(-)
diff --git a/contrib/test_decoding/expected/repack.out
b/contrib/test_decoding/expected/repack.out
index e7060a7e577..423de064a7a 100644
--- a/contrib/test_decoding/expected/repack.out
+++ b/contrib/test_decoding/expected/repack.out
@@ -51,6 +51,26 @@ SELECT * FROM rpk_missing;
(3 rows)
DROP TABLE rpk_missing;
+-- Verify that REPACK (CONCURRENTLY) can rebuild an exclusion constraint index
+-- using 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;
+SELECT id, a FROM rpk_excl ORDER BY id;
+ id | a
+----+----
+ 1 | 10
+ 2 | 20
+(2 rows)
+
+INSERT INTO rpk_excl VALUES (3, 10);
+ERROR: conflicting key value violates exclusion constraint "rpk_excl_a_excl"
+DETAIL: Key (a)=(10) conflicts with existing key (a)=(10).
+DROP TABLE rpk_excl;
-- Verify handling of !indisready indexes
CREATE TABLE repack_conc_invidx (i int PRIMARY KEY, j int);
INSERT INTO repack_conc_invidx VALUES (1, 0), (2, 0);
diff --git a/contrib/test_decoding/sql/repack.sql
b/contrib/test_decoding/sql/repack.sql
index 0107e0bf89d..de39014117d 100644
--- a/contrib/test_decoding/sql/repack.sql
+++ b/contrib/test_decoding/sql/repack.sql
@@ -33,6 +33,19 @@ REPACK (CONCURRENTLY) rpk_missing;
SELECT * FROM rpk_missing;
DROP TABLE rpk_missing;
+-- Verify that REPACK (CONCURRENTLY) can rebuild an exclusion constraint index
+-- using 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;
+SELECT id, a FROM rpk_excl ORDER BY id;
+INSERT INTO rpk_excl VALUES (3, 10);
+DROP TABLE rpk_excl;
+
-- Verify handling of !indisready indexes
CREATE TABLE repack_conc_invidx (i int PRIMARY KEY, j int);
INSERT INTO repack_conc_invidx VALUES (1, 0), (2, 0);
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index eee8b646029..3ad3701a99b 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -3653,7 +3653,8 @@ build_new_indexes(Relation NewHeap, List *OldIndexes)
{
Oid newindex;
char *newName;
- Relation ind;
+ Relation ind,
+ newind;
ind = index_open(oldindex, ShareUpdateExclusiveLock);
@@ -3662,10 +3663,26 @@ build_new_indexes(Relation NewHeap, List *OldIndexes)
"repacknew",
get_rel_namespace(ind->rd_index->indrelid),
false);
- newindex = index_create_copy(NewHeap,
INDEX_CREATE_SUPPRESS_PROGRESS,
+
+ /*
+ * 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.
+ */
+ newindex = index_create_copy(NewHeap,
+
INDEX_CREATE_SKIP_BUILD |
+
INDEX_CREATE_SUPPRESS_PROGRESS,
oldindex, ind->rd_rel->reltablespace,
newName);
copy_index_constraints(ind, newindex,
RelationGetRelid(NewHeap));
+
+ newind = index_open(newindex, NoLock);
+ index_build(NewHeap, newind, BuildIndexInfo(newind), false,
true,
+ false);
+ index_close(newind, NoLock);
+
result = lappend_oid(result, newindex);
index_close(ind, NoLock);
@@ -3680,9 +3697,9 @@ build_new_indexes(Relation NewHeap, List *OldIndexes)
* Create a transient copy of a constraint -- supported by a transient
* copy of the index that supports the original constraint.
*
- * When repacking a table that contains exclusion constraints, the executor
- * relies on these constraints being properly catalogued. These copies are
- * to support that.
+ * When repacking a table that contains exclusion constraints, parallel index
+ * builds and the executor rely on these constraints being properly
+ * catalogued. These copies are to support that.
*
* We don't need the constraints for anything else (the original constraints
* will be there once repack completes), so we add pg_depend entries so that
--
2.54.0 (Apple Git-157)