From ddfc5d199626759695f4d7f1fa78c58fafae5e54 Mon Sep 17 00:00:00 2001 From: nkey Date: Sun, 24 Nov 2024 14:49:35 +0100 Subject: [PATCH v5 2/4] Modify the infer_arbiter_indexes function to consider both indisvalid and indisready indexes. Ensure that at least one indisvalid index is still required. The change ensures that all concurrent transactions utilize the same set of indexes as arbiters. This uniformity is required to avoid conditions that could lead to "duplicate key value violates unique constraint" errors during UPSERT operations. The patch resolves the issues in the following specs: * reindex_concurrently_upsert * index_concurrently_upsert * index_concurrently_upsert_predicate Despite the patch, the following specs are still affected: * reindex_concurrently_upsert_partitioned * reindex_concurrently_upsert_on_constraint --- src/backend/optimizer/util/plancat.c | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/src/backend/optimizer/util/plancat.c b/src/backend/optimizer/util/plancat.c index 37b0ca2e439..c835813290a 100644 --- a/src/backend/optimizer/util/plancat.c +++ b/src/backend/optimizer/util/plancat.c @@ -719,6 +719,7 @@ infer_arbiter_indexes(PlannerInfo *root) /* Results */ List *results = NIL; + bool foundValid = false; /* * Quickly return NIL for ON CONFLICT DO NOTHING without an inference @@ -812,7 +813,13 @@ infer_arbiter_indexes(PlannerInfo *root) idxRel = index_open(indexoid, rte->rellockmode); idxForm = idxRel->rd_index; - if (!idxForm->indisvalid) + /* + * We need to consider both indisvalid and indisready indexes because + * them may become indisvalid before execution phase. It is required + * to keep set of indexes used as arbiter to be the same for all + * concurrent transactions. + */ + if (!idxForm->indisready) goto next; /* @@ -834,10 +841,9 @@ infer_arbiter_indexes(PlannerInfo *root) errmsg("ON CONFLICT DO UPDATE not supported with exclusion constraints"))); results = lappend_oid(results, idxForm->indexrelid); - list_free(indexList); + foundValid |= idxForm->indisvalid; index_close(idxRel, NoLock); - table_close(relation, NoLock); - return results; + break; } else if (indexOidFromConstraint != InvalidOid) { @@ -938,6 +944,7 @@ infer_arbiter_indexes(PlannerInfo *root) goto next; results = lappend_oid(results, idxForm->indexrelid); + foundValid |= idxForm->indisvalid; next: index_close(idxRel, NoLock); } @@ -945,7 +952,8 @@ next: list_free(indexList); table_close(relation, NoLock); - if (results == NIL) + /* It is required to have at least one indisvalid index during the planning. */ + if (results == NIL || !foundValid) ereport(ERROR, (errcode(ERRCODE_INVALID_COLUMN_REFERENCE), errmsg("there is no unique or exclusion constraint matching the ON CONFLICT specification"))); -- 2.43.0