Skip to content

Commit f50d2de

Browse files
knizhnikhlinnaka
authored andcommitted
Pin pages with speculative insert tuples to prevent their reconstruction because spec_token is not wal logged (#223)
* Pin pages with speculative insert tuples to prevent their reconstruction because spec_token is not wal logged refer ##2587 * Update src/backend/access/heap/heapam.c Co-authored-by: Heikki Linnakangas <heikki.linnakangas@iki.fi> Co-authored-by: Heikki Linnakangas <heikki.linnakangas@iki.fi>
1 parent ed3602e commit f50d2de

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

src/backend/access/heap/heapam.c

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2195,7 +2195,18 @@ heap_insert(Relation relation, HeapTuple tup, CommandId cid,
21952195

21962196
END_CRIT_SECTION();
21972197

2198-
UnlockReleaseBuffer(buffer);
2198+
if (options & HEAP_INSERT_SPECULATIVE)
2199+
{
2200+
/*
2201+
* NEON: speculative token is not stored in WAL, so if the page is evicted
2202+
* from the buffer cache, the token will be lost. To prevent that, we keep the
2203+
* buffer pinned. It will be unpinned in heapam_tuple_finish/abort_speculative.
2204+
*/
2205+
LockBuffer(buffer, BUFFER_LOCK_UNLOCK);
2206+
}
2207+
else
2208+
UnlockReleaseBuffer(buffer);
2209+
21992210
if (vmbuffer != InvalidBuffer)
22002211
ReleaseBuffer(vmbuffer);
22012212

@@ -5848,6 +5859,7 @@ heap_finish_speculative(Relation relation, ItemPointer tid)
58485859

58495860
END_CRIT_SECTION();
58505861

5862+
ReleaseBuffer(buffer); /* NEON: release buffer pinned by heap_insert */
58515863
UnlockReleaseBuffer(buffer);
58525864
}
58535865

@@ -5920,6 +5932,16 @@ heap_abort_speculative(Relation relation, ItemPointer tid)
59205932
elog(ERROR, "attempted to kill a non-speculative tuple");
59215933
Assert(!HeapTupleHeaderIsHeapOnly(tp.t_data));
59225934

5935+
/*
5936+
* NEON: release buffer pinned by heap_insert
5937+
*
5938+
* This function is also used on the toast tuples of an aborted speculative
5939+
* insertion. For those, there is no token on the tuple, and we didn' t keep
5940+
* the pin.
5941+
*/
5942+
if (HeapTupleHeaderIsSpeculative(tp.t_data))
5943+
ReleaseBuffer(buffer);
5944+
59235945
/*
59245946
* No need to check for serializable conflicts here. There is never a
59255947
* need for a combo CID, either. No need to extract replica identity, or
@@ -9134,7 +9156,7 @@ heap_xlog_insert(XLogReaderState *record)
91349156

91359157
XLogRecGetBlockTag(record, 0, &target_node, NULL, &blkno);
91369158
ItemPointerSetBlockNumber(&target_tid, blkno);
9137-
ItemPointerSetOffsetNumber(&target_tid, (xlrec->flags & XLH_INSERT_IS_SPECULATIVE) ? SpecTokenOffsetNumber : xlrec->offnum);
9159+
ItemPointerSetOffsetNumber(&target_tid, xlrec->offnum);
91389160

91399161
/*
91409162
* The visibility map may need to be fixed even if the heap page is

0 commit comments

Comments
 (0)