Skip to content

Commit 15cac3a

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 543852f commit 15cac3a

File tree

2 files changed

+16
-20
lines changed

2 files changed

+16
-20
lines changed

src/backend/replication/logical/reorderbuffer.c

+12-17
Original file line numberDiff line numberDiff line change
@@ -1974,21 +1974,6 @@ ReorderBufferAbortOld(ReorderBuffer *rb, TransactionId oldestRunningXid)
19741974

19751975
if (TransactionIdPrecedes(txn->xid, oldestRunningXid))
19761976
{
1977-
/*
1978-
* We set final_lsn on a transaction when we decode its commit or
1979-
* abort record, but we never see those records for crashed
1980-
* transactions. To ensure cleanup of these transactions, set
1981-
* final_lsn to that of their last change; this causes
1982-
* ReorderBufferRestoreCleanup to do the right thing.
1983-
*/
1984-
if (rbtxn_is_serialized(txn) && txn->final_lsn == 0)
1985-
{
1986-
ReorderBufferChange *last =
1987-
dlist_tail_element(ReorderBufferChange, node, &txn->changes);
1988-
1989-
txn->final_lsn = last->lsn;
1990-
}
1991-
19921977
elog(DEBUG2, "aborting old transaction %u", txn->xid);
19931978

19941979
/* remove potential on-disk data, and deallocate this tx */
@@ -2623,8 +2608,7 @@ ReorderBufferSerializeChange(ReorderBuffer *rb, ReorderBufferTXN *txn,
26232608

26242609
sz += sizeof(SnapshotData) +
26252610
sizeof(TransactionId) * snap->xcnt +
2626-
sizeof(TransactionId) * snap->subxcnt
2627-
;
2611+
sizeof(TransactionId) * snap->subxcnt;
26282612

26292613
/* make sure we have enough space */
26302614
ReorderBufferSerializeReserve(rb, sz);
@@ -2697,6 +2681,17 @@ ReorderBufferSerializeChange(ReorderBuffer *rb, ReorderBufferTXN *txn,
26972681
}
26982682
pgstat_report_wait_end();
26992683

2684+
/*
2685+
* Keep the transaction's final_lsn up to date with each change we send to
2686+
* disk, so that ReorderBufferRestoreCleanup works correctly. (We used to
2687+
* only do this on commit and abort records, but that doesn't work if a
2688+
* system crash leaves a transaction without its abort record).
2689+
*
2690+
* Make sure not to move it backwards.
2691+
*/
2692+
if (txn->final_lsn < change->lsn)
2693+
txn->final_lsn = change->lsn;
2694+
27002695
Assert(ondisk->change.action == change->action);
27012696
}
27022697

src/include/replication/reorderbuffer.h

+4-3
Original file line numberDiff line numberDiff line change
@@ -207,9 +207,10 @@ typedef struct ReorderBufferTXN
207207
* * prepared transaction commit
208208
* * plain abort record
209209
* * prepared transaction abort
210-
* * error during decoding
211-
* * for a crashed transaction, the LSN of the last change, regardless of
212-
* what it was.
210+
*
211+
* This can also become set to earlier values than transaction end when
212+
* a transaction is spilled to disk; specifically it's set to the LSN of
213+
* the latest change written to disk so far.
213214
* ----
214215
*/
215216
XLogRecPtr final_lsn;

0 commit comments

Comments
 (0)