aboutsummaryrefslogtreecommitdiff
path: root/src/backend/executor/execReplication.c
diff options
context:
space:
mode:
authorAmit Kapila <akapila@postgresql.org>2025-04-08 15:35:42 +0530
committerAmit Kapila <akapila@postgresql.org>2025-04-08 15:35:42 +0530
commit12eece5fd54c2aa3dbdefb6de7f18566f5c00357 (patch)
tree005de6ab36fb26933fd67cc01d1c74cc461b07dc /src/backend/executor/execReplication.c
parent7ea21f4ee2721be4239657e74c2ae8f88fb5a3ef (diff)
downloadpostgresql-12eece5fd54c2aa3dbdefb6de7f18566f5c00357.tar.gz
postgresql-12eece5fd54c2aa3dbdefb6de7f18566f5c00357.zip
Fix uninitialized index information access during apply.
The issue happens when building conflict information during apply of INSERT or UPDATE operations that violate unique constraints on leaf partitions. The problem was introduced in commit 9ff68679b5, which removed the redundant calls to ExecOpenIndices/ExecCloseIndices. The previous code was relying on the redundant ExecOpenIndices call in apply_handle_tuple_routing() to build the index information required for unique key conflict detection. The fix is to delay building the index information until a conflict is detected instead of relying on ExecOpenIndices to do the same. The additional benefit of this approach is that it avoids building index information when there is no conflict. Author: Hou Zhijie <houzj.fnst@fujitsu.com> Reviewed-by:Reviewed-by: Amit Kapila <amit.kapila16@gmail.com> Discussion: https://postgr.es/m/TYAPR01MB57244ADA33DDA57119B9D26494A62@TYAPR01MB5724.jpnprd01.prod.outlook.com
Diffstat (limited to 'src/backend/executor/execReplication.c')
-rw-r--r--src/backend/executor/execReplication.c30
1 files changed, 30 insertions, 0 deletions
diff --git a/src/backend/executor/execReplication.c b/src/backend/executor/execReplication.c
index ede89ea3cf9..53ddd25c42d 100644
--- a/src/backend/executor/execReplication.c
+++ b/src/backend/executor/execReplication.c
@@ -432,6 +432,30 @@ retry:
}
/*
+ * Build additional index information necessary for conflict detection.
+ */
+static void
+BuildConflictIndexInfo(ResultRelInfo *resultRelInfo, Oid conflictindex)
+{
+ for (int i = 0; i < resultRelInfo->ri_NumIndices; i++)
+ {
+ Relation indexRelation = resultRelInfo->ri_IndexRelationDescs[i];
+ IndexInfo *indexRelationInfo = resultRelInfo->ri_IndexRelationInfo[i];
+
+ if (conflictindex != RelationGetRelid(indexRelation))
+ continue;
+
+ /*
+ * This Assert will fail if BuildSpeculativeIndexInfo() is called
+ * twice for the given index.
+ */
+ Assert(indexRelationInfo->ii_UniqueOps == NULL);
+
+ BuildSpeculativeIndexInfo(indexRelation, indexRelationInfo);
+ }
+}
+
+/*
* Find the tuple that violates the passed unique index (conflictindex).
*
* If the conflicting tuple is found return true, otherwise false.
@@ -452,6 +476,12 @@ FindConflictTuple(ResultRelInfo *resultRelInfo, EState *estate,
*conflictslot = NULL;
+ /*
+ * Build additional information required to check constraints violations.
+ * See check_exclusion_or_unique_constraint().
+ */
+ BuildConflictIndexInfo(resultRelInfo, conflictindex);
+
retry:
if (ExecCheckIndexConstraints(resultRelInfo, slot, estate,
&conflictTid, &slot->tts_tid,