Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit b86d148

Browse files
committed
Do not decode TOAST data for table rewrites
During table rewrites (VACUUM FULL and CLUSTER), the main heap is logged using XLOG / FPI records, and thus (correctly) ignored in decoding. But the associated TOAST table is WAL-logged as plain INSERT records, and so was logically decoded and passed to reorder buffer. That has severe consequences with TOAST tables of non-trivial size. Firstly, reorder buffer has to keep all those changes, possibly spilling them to a file, incurring I/O costs and disk space. Secondly, ReoderBufferCommit() was stashing all those TOAST chunks into a hash table, which got discarded only after processing the row from the main heap. But as the main heap is not decoded for rewrites, this never happened, so all the TOAST data accumulated in memory, resulting either in excessive memory consumption or OOM. The fix is simple, as commit e9edc1b already introduced infrastructure (namely HEAP_INSERT_NO_LOGICAL flag) to skip logical decoding of TOAST tables, but it only applied it to system tables. So simply use it for all TOAST data in raw_heap_insert(). That would however solve only the memory consumption issue - the TOAST changes would still be decoded and added to the reorder buffer, and spilled to disk (although without TOAST tuple data, so much smaller). But we can solve that by tweaking DecodeInsert() to just ignore such INSERT records altogether, using XLH_INSERT_CONTAINS_NEW_TUPLE flag, instead of skipping them later in ReorderBufferCommit(). Review: Masahiko Sawada Discussion: https://www.postgresql.org/message-id/flat/1a17c643-e9af-3dba-486b-fbe31bc1823a%402ndquadrant.com Backpatch: 9.4-, where logical decoding was introduced
1 parent a52c31b commit b86d148

File tree

3 files changed

+24
-24
lines changed

3 files changed

+24
-24
lines changed

src/backend/access/heap/rewriteheap.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -656,12 +656,11 @@ raw_heap_insert(RewriteState state, HeapTuple tup)
656656
options |= HEAP_INSERT_SKIP_WAL;
657657

658658
/*
659-
* The new relfilenode's relcache entrye doesn't have the necessary
660-
* information to determine whether a relation should emit data for
661-
* logical decoding. Force it to off if necessary.
659+
* While rewriting the heap for VACUUM FULL / CLUSTER, make sure data
660+
* for the TOAST table are not logically decoded. The main heap is
661+
* WAL-logged as XLOG FPI records, which are not logically decoded.
662662
*/
663-
if (!RelationIsLogicallyLogged(state->rs_old_rel))
664-
options |= HEAP_INSERT_NO_LOGICAL;
663+
options |= HEAP_INSERT_NO_LOGICAL;
665664

666665
heaptup = toast_insert_or_update(state->rs_new_rel, tup, NULL,
667666
options);

src/backend/replication/logical/decode.c

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -639,13 +639,23 @@ DecodeAbort(LogicalDecodingContext *ctx, XLogRecordBuffer *buf,
639639
static void
640640
DecodeInsert(LogicalDecodingContext *ctx, XLogRecordBuffer *buf)
641641
{
642+
Size datalen;
643+
char *tupledata;
644+
Size tuplelen;
642645
XLogReaderState *r = buf->record;
643646
xl_heap_insert *xlrec;
644647
ReorderBufferChange *change;
645648
RelFileNode target_node;
646649

647650
xlrec = (xl_heap_insert *) XLogRecGetData(r);
648651

652+
/*
653+
* Ignore insert records without new tuples (this does happen when
654+
* raw_heap_insert marks the TOAST record as HEAP_INSERT_NO_LOGICAL).
655+
*/
656+
if (!(xlrec->flags & XLH_INSERT_CONTAINS_NEW_TUPLE))
657+
return;
658+
649659
/* only interested in our database */
650660
XLogRecGetBlockTag(r, 0, &target_node, NULL, NULL);
651661
if (target_node.dbNode != ctx->slot->data.database)
@@ -664,17 +674,13 @@ DecodeInsert(LogicalDecodingContext *ctx, XLogRecordBuffer *buf)
664674

665675
memcpy(&change->data.tp.relnode, &target_node, sizeof(RelFileNode));
666676

667-
if (xlrec->flags & XLH_INSERT_CONTAINS_NEW_TUPLE)
668-
{
669-
Size datalen;
670-
char *tupledata = XLogRecGetBlockData(r, 0, &datalen);
671-
Size tuplelen = datalen - SizeOfHeapHeader;
677+
tupledata = XLogRecGetBlockData(r, 0, &datalen);
678+
tuplelen = datalen - SizeOfHeapHeader;
672679

673-
change->data.tp.newtuple =
674-
ReorderBufferGetTupleBuf(ctx->reorder, tuplelen);
680+
change->data.tp.newtuple =
681+
ReorderBufferGetTupleBuf(ctx->reorder, tuplelen);
675682

676-
DecodeXLogTuple(tupledata, datalen, change->data.tp.newtuple);
677-
}
683+
DecodeXLogTuple(tupledata, datalen, change->data.tp.newtuple);
678684

679685
change->data.tp.clear_toast_afterwards = true;
680686

src/backend/replication/logical/reorderbuffer.c

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1652,17 +1652,12 @@ ReorderBufferCommit(ReorderBuffer *rb, TransactionId xid,
16521652
* transaction's changes. Otherwise it will get
16531653
* freed/reused while restoring spooled data from
16541654
* disk.
1655-
*
1656-
* But skip doing so if there's no tuple-data. That
1657-
* happens if a non-mapped system catalog with a toast
1658-
* table is rewritten.
16591655
*/
1660-
if (change->data.tp.newtuple != NULL)
1661-
{
1662-
dlist_delete(&change->node);
1663-
ReorderBufferToastAppendChunk(rb, txn, relation,
1664-
change);
1665-
}
1656+
Assert(change->data.tp.newtuple != NULL);
1657+
1658+
dlist_delete(&change->node);
1659+
ReorderBufferToastAppendChunk(rb, txn, relation,
1660+
change);
16661661
}
16671662

16681663
change_done:

0 commit comments

Comments
 (0)