Skip to content

Commit 20a1dc1

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 9be6fcb commit 20a1dc1

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
@@ -1794,21 +1794,6 @@ ReorderBufferAbortOld(ReorderBuffer *rb, TransactionId oldestRunningXid)
17941794

17951795
if (TransactionIdPrecedes(txn->xid, oldestRunningXid))
17961796
{
1797-
/*
1798-
* We set final_lsn on a transaction when we decode its commit or
1799-
* abort record, but we never see those records for crashed
1800-
* transactions. To ensure cleanup of these transactions, set
1801-
* final_lsn to that of their last change; this causes
1802-
* ReorderBufferRestoreCleanup to do the right thing.
1803-
*/
1804-
if (txn->serialized && txn->final_lsn == 0)
1805-
{
1806-
ReorderBufferChange *last =
1807-
dlist_tail_element(ReorderBufferChange, node, &txn->changes);
1808-
1809-
txn->final_lsn = last->lsn;
1810-
}
1811-
18121797
elog(DEBUG2, "aborting old transaction %u", txn->xid);
18131798

18141799
/* remove potential on-disk data, and deallocate this tx */
@@ -2288,8 +2273,7 @@ ReorderBufferSerializeChange(ReorderBuffer *rb, ReorderBufferTXN *txn,
22882273

22892274
sz += sizeof(SnapshotData) +
22902275
sizeof(TransactionId) * snap->xcnt +
2291-
sizeof(TransactionId) * snap->subxcnt
2292-
;
2276+
sizeof(TransactionId) * snap->subxcnt;
22932277

22942278
/* make sure we have enough space */
22952279
ReorderBufferSerializeReserve(rb, sz);
@@ -2340,6 +2324,17 @@ ReorderBufferSerializeChange(ReorderBuffer *rb, ReorderBufferTXN *txn,
23402324
txn->xid)));
23412325
}
23422326

2327+
/*
2328+
* Keep the transaction's final_lsn up to date with each change we send to
2329+
* disk, so that ReorderBufferRestoreCleanup works correctly. (We used to
2330+
* only do this on commit and abort records, but that doesn't work if a
2331+
* system crash leaves a transaction without its abort record).
2332+
*
2333+
* Make sure not to move it backwards.
2334+
*/
2335+
if (txn->final_lsn < change->lsn)
2336+
txn->final_lsn = change->lsn;
2337+
23432338
Assert(ondisk->change.action == change->action);
23442339
}
23452340

src/include/replication/reorderbuffer.h

+4-3
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,10 @@ typedef struct ReorderBufferTXN
151151
* * prepared transaction commit
152152
* * plain abort record
153153
* * prepared transaction abort
154-
* * error during decoding
155-
* * for a crashed transaction, the LSN of the last change, regardless of
156-
* what it was.
154+
*
155+
* This can also become set to earlier values than transaction end when
156+
* a transaction is spilled to disk; specifically it's set to the LSN of
157+
* the latest change written to disk so far.
157158
* ----
158159
*/
159160
XLogRecPtr final_lsn;

0 commit comments

Comments
 (0)