Skip to content

Commit 9a1c736

Browse files
committed
Revert "WAL-log inplace update before revealing it to other sessions."
This reverts commit bfd5c6e (v17) and counterparts in each other non-master branch. This unblocks reverting a commit on which it depends. Discussion: https://postgr.es/m/10ec0bc3-5933-1189-6bb8-5dec4114558e@gmail.com
1 parent b165e71 commit 9a1c736

File tree

3 files changed

+18
-46
lines changed

3 files changed

+18
-46
lines changed

src/backend/access/heap/README.tuplock

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,4 +203,6 @@ Inplace updates create an exception to the rule that tuple data won't change
203203
under a reader holding a pin. A reader of a heap_fetch() result tuple may
204204
witness a torn read. Current inplace-updated fields are aligned and are no
205205
wider than four bytes, and current readers don't need consistency across
206-
fields. Hence, they get by with just fetching each field once.
206+
fields. Hence, they get by with just fetching each field once. XXX such a
207+
caller may also read a value that has not reached WAL; see
208+
systable_inplace_update_finish().

src/backend/access/heap/heapam.c

Lines changed: 13 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -6380,18 +6380,13 @@ heap_inplace_update_and_unlock(Relation relation,
63806380
HeapTupleHeader htup = oldtup->t_data;
63816381
uint32 oldlen;
63826382
uint32 newlen;
6383-
char *dst;
6384-
char *src;
63856383

63866384
Assert(ItemPointerEquals(&oldtup->t_self, &tuple->t_self));
63876385
oldlen = oldtup->t_len - htup->t_hoff;
63886386
newlen = tuple->t_len - tuple->t_data->t_hoff;
63896387
if (oldlen != newlen || htup->t_hoff != tuple->t_data->t_hoff)
63906388
elog(ERROR, "wrong tuple length");
63916389

6392-
dst = (char *) htup + htup->t_hoff;
6393-
src = (char *) tuple->t_data + tuple->t_data->t_hoff;
6394-
63956390
/*
63966391
* Construct shared cache inval if necessary. Note that because we only
63976392
* pass the new version of the tuple, this mustn't be used for any
@@ -6410,15 +6405,15 @@ heap_inplace_update_and_unlock(Relation relation,
64106405
*/
64116406
PreInplace_Inval();
64126407

6408+
/* NO EREPORT(ERROR) from here till changes are logged */
6409+
START_CRIT_SECTION();
6410+
6411+
memcpy((char *) htup + htup->t_hoff,
6412+
(char *) tuple->t_data + tuple->t_data->t_hoff,
6413+
newlen);
6414+
64136415
/*----------
6414-
* NO EREPORT(ERROR) from here till changes are complete
6415-
*
6416-
* Our buffer lock won't stop a reader having already pinned and checked
6417-
* visibility for this tuple. Hence, we write WAL first, then mutate the
6418-
* buffer. Like in MarkBufferDirtyHint() or RecordTransactionCommit(),
6419-
* checkpoint delay makes that acceptable. With the usual order of
6420-
* changes, a crash after memcpy() and before XLogInsert() could allow
6421-
* datfrozenxid to overtake relfrozenxid:
6416+
* XXX A crash here can allow datfrozenxid() to get ahead of relfrozenxid:
64226417
*
64236418
* ["D" is a VACUUM (ONLY_DATABASE_STATS)]
64246419
* ["R" is a VACUUM tbl]
@@ -6428,57 +6423,31 @@ heap_inplace_update_and_unlock(Relation relation,
64286423
* D: raise pg_database.datfrozenxid, XLogInsert(), finish
64296424
* [crash]
64306425
* [recovery restores datfrozenxid w/o relfrozenxid]
6431-
*
6432-
* Like in MarkBufferDirtyHint() subroutine XLogSaveBufferForHint(), copy
6433-
* the buffer to the stack before logging. Here, that facilitates a FPI
6434-
* of the post-mutation block before we accept other sessions seeing it.
64356426
*/
6436-
Assert(!MyProc->delayChkpt);
6437-
START_CRIT_SECTION();
6438-
MyProc->delayChkpt = true;
6427+
6428+
MarkBufferDirty(buffer);
64396429

64406430
/* XLOG stuff */
64416431
if (RelationNeedsWAL(relation))
64426432
{
64436433
xl_heap_inplace xlrec;
6444-
PGAlignedBlock copied_buffer;
6445-
char *origdata = (char *) BufferGetBlock(buffer);
6446-
Page page = BufferGetPage(buffer);
6447-
uint16 lower = ((PageHeader) page)->pd_lower;
6448-
uint16 upper = ((PageHeader) page)->pd_upper;
6449-
uintptr_t dst_offset_in_block;
6450-
RelFileNode rnode;
6451-
ForkNumber forkno;
6452-
BlockNumber blkno;
64536434
XLogRecPtr recptr;
64546435

64556436
xlrec.offnum = ItemPointerGetOffsetNumber(&tuple->t_self);
64566437

64576438
XLogBeginInsert();
64586439
XLogRegisterData((char *) &xlrec, SizeOfHeapInplace);
64596440

6460-
/* register block matching what buffer will look like after changes */
6461-
memcpy(copied_buffer.data, origdata, lower);
6462-
memcpy(copied_buffer.data + upper, origdata + upper, BLCKSZ - upper);
6463-
dst_offset_in_block = dst - origdata;
6464-
memcpy(copied_buffer.data + dst_offset_in_block, src, newlen);
6465-
BufferGetTag(buffer, &rnode, &forkno, &blkno);
6466-
Assert(forkno == MAIN_FORKNUM);
6467-
XLogRegisterBlock(0, &rnode, forkno, blkno, copied_buffer.data,
6468-
REGBUF_STANDARD);
6469-
XLogRegisterBufData(0, src, newlen);
6441+
XLogRegisterBuffer(0, buffer, REGBUF_STANDARD);
6442+
XLogRegisterBufData(0, (char *) htup + htup->t_hoff, newlen);
64706443

64716444
/* inplace updates aren't decoded atm, don't log the origin */
64726445

64736446
recptr = XLogInsert(RM_HEAP_ID, XLOG_HEAP_INPLACE);
64746447

6475-
PageSetLSN(page, recptr);
6448+
PageSetLSN(BufferGetPage(buffer), recptr);
64766449
}
64776450

6478-
memcpy(dst, src, newlen);
6479-
6480-
MarkBufferDirty(buffer);
6481-
64826451
LockBuffer(buffer, BUFFER_LOCK_UNLOCK);
64836452

64846453
/*
@@ -6491,7 +6460,6 @@ heap_inplace_update_and_unlock(Relation relation,
64916460
*/
64926461
AtInplace_Inval();
64936462

6494-
MyProc->delayChkpt = false;
64956463
END_CRIT_SECTION();
64966464
UnlockTuple(relation, &tuple->t_self, InplaceUpdateTupleLock);
64976465

src/backend/access/transam/xloginsert.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,8 @@ XLogRegisterBlock(uint8 block_id, RelFileNode *rnode, ForkNumber forknum,
275275
{
276276
registered_buffer *regbuf;
277277

278+
/* This is currently only used to WAL-log a full-page image of a page */
279+
Assert(flags & REGBUF_FORCE_IMAGE);
278280
Assert(begininsert_called);
279281

280282
if (block_id >= max_registered_block_id)

0 commit comments

Comments
 (0)