Skip to content

Commit e3154aa

Browse files
committed
Set ReorderBufferTXN->final_lsn more eagerly
... specifically, set it incrementally as each individual change is spilled down to disk. This way, it is set correctly when the transaction disappears without trace, ie. without leaving an XACT_ABORT wal record. (This happens when the server crashes midway through a transaction.) Failing to have final_lsn prevents ReorderBufferRestoreCleanup() from working, since it needs the final_lsn in order to know the endpoint of its iteration through spilled files. Commit df9f682 already tried to fix the problem, but it didn't set the final_lsn in all cases. Revert that, since it's no longer needed. Author: Vignesh C Reviewed-by: Amit Kapila, Dilip Kumar Discussion: https://postgr.es/m/CALDaNm2CLk+K9JDwjYST0sPbGg5AQdvhUt0jbKyX_HdAE0jk3A@mail.gmail.com
1 parent a801452 commit e3154aa

File tree

2 files changed

+16
-20
lines changed

2 files changed

+16
-20
lines changed

src/backend/replication/logical/reorderbuffer.c

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1877,21 +1877,6 @@ ReorderBufferAbortOld(ReorderBuffer *rb, TransactionId oldestRunningXid)
18771877

18781878
if (TransactionIdPrecedes(txn->xid, oldestRunningXid))
18791879
{
1880-
/*
1881-
* We set final_lsn on a transaction when we decode its commit or
1882-
* abort record, but we never see those records for crashed
1883-
* transactions. To ensure cleanup of these transactions, set
1884-
* final_lsn to that of their last change; this causes
1885-
* ReorderBufferRestoreCleanup to do the right thing.
1886-
*/
1887-
if (txn->serialized && txn->final_lsn == 0)
1888-
{
1889-
ReorderBufferChange *last =
1890-
dlist_tail_element(ReorderBufferChange, node, &txn->changes);
1891-
1892-
txn->final_lsn = last->lsn;
1893-
}
1894-
18951880
elog(DEBUG2, "aborting old transaction %u", txn->xid);
18961881

18971882
/* remove potential on-disk data, and deallocate this tx */
@@ -2414,8 +2399,7 @@ ReorderBufferSerializeChange(ReorderBuffer *rb, ReorderBufferTXN *txn,
24142399

24152400
sz += sizeof(SnapshotData) +
24162401
sizeof(TransactionId) * snap->xcnt +
2417-
sizeof(TransactionId) * snap->subxcnt
2418-
;
2402+
sizeof(TransactionId) * snap->subxcnt;
24192403

24202404
/* make sure we have enough space */
24212405
ReorderBufferSerializeReserve(rb, sz);
@@ -2467,6 +2451,17 @@ ReorderBufferSerializeChange(ReorderBuffer *rb, ReorderBufferTXN *txn,
24672451
}
24682452
pgstat_report_wait_end();
24692453

2454+
/*
2455+
* Keep the transaction's final_lsn up to date with each change we send to
2456+
* disk, so that ReorderBufferRestoreCleanup works correctly. (We used to
2457+
* only do this on commit and abort records, but that doesn't work if a
2458+
* system crash leaves a transaction without its abort record).
2459+
*
2460+
* Make sure not to move it backwards.
2461+
*/
2462+
if (txn->final_lsn < change->lsn)
2463+
txn->final_lsn = change->lsn;
2464+
24702465
Assert(ondisk->change.action == change->action);
24712466
}
24722467

src/include/replication/reorderbuffer.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,9 +166,10 @@ typedef struct ReorderBufferTXN
166166
* * prepared transaction commit
167167
* * plain abort record
168168
* * prepared transaction abort
169-
* * error during decoding
170-
* * for a crashed transaction, the LSN of the last change, regardless of
171-
* what it was.
169+
*
170+
* This can also become set to earlier values than transaction end when
171+
* a transaction is spilled to disk; specifically it's set to the LSN of
172+
* the latest change written to disk so far.
172173
* ----
173174
*/
174175
XLogRecPtr final_lsn;

0 commit comments

Comments
 (0)