Skip to content

Commit 7884d54

Browse files
committed
Fix buffer access bug in OnConflict
1 parent e780704 commit 7884d54

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

src/backend/executor/nodeModifyTable.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1087,6 +1087,13 @@ ExecOnConflictUpdate(ModifyTableState *mtstate,
10871087
test = heap_lock_tuple(relation, &tuple, estate->es_output_cid,
10881088
lockmode, LockWaitBlock, false, &buffer,
10891089
&hufd);
1090+
/*
1091+
* We must hold share lock on the buffer content while examining tuple
1092+
* visibility. Afterwards, however, the tuples we have found to be
1093+
* visible are guaranteed good as long as we hold the buffer pin.
1094+
*/
1095+
LockBuffer(buffer, BUFFER_LOCK_SHARE);
1096+
10901097
switch (test)
10911098
{
10921099
case HeapTupleMayBeUpdated:
@@ -1142,6 +1149,7 @@ ExecOnConflictUpdate(ModifyTableState *mtstate,
11421149
* loop here, as the new version of the row might not conflict
11431150
* anymore, or the conflicting tuple has actually been deleted.
11441151
*/
1152+
LockBuffer(buffer, BUFFER_LOCK_UNLOCK);
11451153
ReleaseBuffer(buffer);
11461154
return false;
11471155

@@ -1175,6 +1183,8 @@ ExecOnConflictUpdate(ModifyTableState *mtstate,
11751183
/* Store target's existing tuple in the state's dedicated slot */
11761184
ExecStoreTuple(&tuple, mtstate->mt_existing, buffer, false);
11771185

1186+
LockBuffer(buffer, BUFFER_LOCK_UNLOCK);
1187+
11781188
/*
11791189
* Make tuple and any needed join variables available to ExecQual and
11801190
* ExecProject. The EXCLUDED tuple is installed in ecxt_innertuple, while

0 commit comments

Comments
 (0)