Skip to content

Commit a27c566

Browse files
committed
Fix leaking of small spilled subtransactions during logical decoding.
When, during logical decoding, a transaction gets too big, it's contents get spilled to disk. Not just the top-transaction gets spilled, but *also* all of its subtransactions, even if they're not that large themselves. Unfortunately we didn't clean up such small spilled subtransactions from disk. Fix that, by keeping better track of whether a transaction has been spilled to disk. Author: Andres Freund Reported-By: Dmitriy Sarafannikov, Fabrízio de Royes Mello Discussion: https://postgr.es/m/1457621358.355011041@f382.i.mail.ru https://postgr.es/m/CAFcNs+qNMhNYii4nxpO6gqsndiyxNDYV0S=JNq0v_sEE+9PHXg@mail.gmail.com Backpatch: 9.4-, where logical decoding was introduced
1 parent 6338b50 commit a27c566

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

src/backend/replication/logical/reorderbuffer.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -871,7 +871,7 @@ ReorderBufferIterTXNInit(ReorderBuffer *rb, ReorderBufferTXN *txn)
871871
{
872872
ReorderBufferChange *cur_change;
873873

874-
if (txn->nentries != txn->nentries_mem)
874+
if (txn->serialized)
875875
{
876876
/* serialize remaining changes */
877877
ReorderBufferSerializeTXN(rb, txn);
@@ -900,7 +900,7 @@ ReorderBufferIterTXNInit(ReorderBuffer *rb, ReorderBufferTXN *txn)
900900
{
901901
ReorderBufferChange *cur_change;
902902

903-
if (cur_txn->nentries != cur_txn->nentries_mem)
903+
if (cur_txn->serialized)
904904
{
905905
/* serialize remaining changes */
906906
ReorderBufferSerializeTXN(rb, cur_txn);
@@ -1122,7 +1122,7 @@ ReorderBufferCleanupTXN(ReorderBuffer *rb, ReorderBufferTXN *txn)
11221122
Assert(found);
11231123

11241124
/* remove entries spilled to disk */
1125-
if (txn->nentries != txn->nentries_mem)
1125+
if (txn->serialized)
11261126
ReorderBufferRestoreCleanup(rb, txn);
11271127

11281128
/* deallocate */
@@ -2014,6 +2014,7 @@ ReorderBufferSerializeTXN(ReorderBuffer *rb, ReorderBufferTXN *txn)
20142014
Assert(spilled == txn->nentries_mem);
20152015
Assert(dlist_is_empty(&txn->changes));
20162016
txn->nentries_mem = 0;
2017+
txn->serialized = true;
20172018

20182019
if (fd != -1)
20192020
CloseTransientFile(fd);

src/include/replication/reorderbuffer.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,15 @@ typedef struct ReorderBufferTXN
193193
*/
194194
uint64 nentries_mem;
195195

196+
/*
197+
* Has this transaction been spilled to disk? It's not always possible to
198+
* deduce that fact by comparing nentries with nentries_mem, because
199+
* e.g. subtransactions of a large transaction might get serialized
200+
* together with the parent - if they're restored to memory they'd have
201+
* nentries_mem == nentries.
202+
*/
203+
bool serialized;
204+
196205
/*
197206
* List of ReorderBufferChange structs, including new Snapshots and new
198207
* CommandIds

0 commit comments

Comments
 (0)