Skip to content

Commit acf4958

Browse files
committed
WAL-log inplace update before revealing it to other sessions.
A buffer lock won't stop a reader having already checked tuple visibility. If a vac_update_datfrozenid() and then a crash happened during inplace update of a relfrozenxid value, datfrozenxid could overtake relfrozenxid. That could lead to "could not access status of transaction" errors. Back-patch to v12 (all supported versions). In v14 and earlier, this also back-patches the assertion removal from commit 7fcf2fa. Discussion: https://postgr.es/m/20240620012908.92.nmisch@google.com
1 parent e3914bd commit acf4958

File tree

3 files changed

+46
-18
lines changed

3 files changed

+46
-18
lines changed

src/backend/access/heap/README.tuplock

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,4 @@ 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. XXX such a
207-
caller may also read a value that has not reached WAL; see
208-
systable_inplace_update_finish().
206+
fields. Hence, they get by with just fetching each field once.

src/backend/access/heap/heapam.c

Lines changed: 45 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6376,13 +6376,18 @@ heap_inplace_update_and_unlock(Relation relation,
63766376
HeapTupleHeader htup = oldtup->t_data;
63776377
uint32 oldlen;
63786378
uint32 newlen;
6379+
char *dst;
6380+
char *src;
63796381

63806382
Assert(ItemPointerEquals(&oldtup->t_self, &tuple->t_self));
63816383
oldlen = oldtup->t_len - htup->t_hoff;
63826384
newlen = tuple->t_len - tuple->t_data->t_hoff;
63836385
if (oldlen != newlen || htup->t_hoff != tuple->t_data->t_hoff)
63846386
elog(ERROR, "wrong tuple length");
63856387

6388+
dst = (char *) htup + htup->t_hoff;
6389+
src = (char *) tuple->t_data + tuple->t_data->t_hoff;
6390+
63866391
/*
63876392
* Construct shared cache inval if necessary. Note that because we only
63886393
* pass the new version of the tuple, this mustn't be used for any
@@ -6401,15 +6406,15 @@ heap_inplace_update_and_unlock(Relation relation,
64016406
*/
64026407
PreInplace_Inval();
64036408

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

64266436
/* XLOG stuff */
64276437
if (RelationNeedsWAL(relation))
64286438
{
64296439
xl_heap_inplace xlrec;
6440+
PGAlignedBlock copied_buffer;
6441+
char *origdata = (char *) BufferGetBlock(buffer);
6442+
Page page = BufferGetPage(buffer);
6443+
uint16 lower = ((PageHeader) page)->pd_lower;
6444+
uint16 upper = ((PageHeader) page)->pd_upper;
6445+
uintptr_t dst_offset_in_block;
6446+
RelFileNode rnode;
6447+
ForkNumber forkno;
6448+
BlockNumber blkno;
64306449
XLogRecPtr recptr;
64316450

64326451
xlrec.offnum = ItemPointerGetOffsetNumber(&tuple->t_self);
64336452

64346453
XLogBeginInsert();
64356454
XLogRegisterData((char *) &xlrec, SizeOfHeapInplace);
64366455

6437-
XLogRegisterBuffer(0, buffer, REGBUF_STANDARD);
6438-
XLogRegisterBufData(0, (char *) htup + htup->t_hoff, newlen);
6456+
/* register block matching what buffer will look like after changes */
6457+
memcpy(copied_buffer.data, origdata, lower);
6458+
memcpy(copied_buffer.data + upper, origdata + upper, BLCKSZ - upper);
6459+
dst_offset_in_block = dst - origdata;
6460+
memcpy(copied_buffer.data + dst_offset_in_block, src, newlen);
6461+
BufferGetTag(buffer, &rnode, &forkno, &blkno);
6462+
Assert(forkno == MAIN_FORKNUM);
6463+
XLogRegisterBlock(0, &rnode, forkno, blkno, copied_buffer.data,
6464+
REGBUF_STANDARD);
6465+
XLogRegisterBufData(0, src, newlen);
64396466

64406467
/* inplace updates aren't decoded atm, don't log the origin */
64416468

64426469
recptr = XLogInsert(RM_HEAP_ID, XLOG_HEAP_INPLACE);
64436470

6444-
PageSetLSN(BufferGetPage(buffer), recptr);
6471+
PageSetLSN(page, recptr);
64456472
}
64466473

6474+
memcpy(dst, src, newlen);
6475+
6476+
MarkBufferDirty(buffer);
6477+
64476478
LockBuffer(buffer, BUFFER_LOCK_UNLOCK);
64486479

64496480
/*
@@ -6456,6 +6487,7 @@ heap_inplace_update_and_unlock(Relation relation,
64566487
*/
64576488
AtInplace_Inval();
64586489

6490+
MyProc->delayChkpt = false;
64596491
END_CRIT_SECTION();
64606492
UnlockTuple(relation, &tuple->t_self, InplaceUpdateTupleLock);
64616493

src/backend/access/transam/xloginsert.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -275,8 +275,6 @@ 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);
280278
Assert(begininsert_called);
281279

282280
if (block_id >= max_registered_block_id)

0 commit comments

Comments
 (0)