Skip to content

Commit c1a5cae

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 6427bfb commit c1a5cae

File tree

2 files changed

+20
-15
lines changed

2 files changed

+20
-15
lines changed

src/backend/access/heap/rewriteheap.c

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

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

668667
heaptup = toast_insert_or_update(state->rs_new_rel, tup, NULL,
669668
options);

src/backend/replication/logical/decode.c

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -607,12 +607,21 @@ DecodeAbort(LogicalDecodingContext *ctx, XLogRecPtr lsn, TransactionId xid,
607607
static void
608608
DecodeInsert(LogicalDecodingContext *ctx, XLogRecordBuffer *buf)
609609
{
610+
Size datalen;
611+
Size tuplelen;
610612
XLogRecord *r = &buf->record;
611613
xl_heap_insert *xlrec;
612614
ReorderBufferChange *change;
613615

614616
xlrec = (xl_heap_insert *) buf->record_data;
615617

618+
/*
619+
* Ignore insert records without new tuples (this does happen when
620+
* raw_heap_insert marks the TOAST record as HEAP_INSERT_NO_LOGICAL).
621+
*/
622+
if (!(xlrec->flags & XLOG_HEAP_CONTAINS_NEW_TUPLE))
623+
return;
624+
616625
/* only interested in our database */
617626
if (xlrec->target.node.dbNode != ctx->slot->data.database)
618627
return;
@@ -621,19 +630,16 @@ DecodeInsert(LogicalDecodingContext *ctx, XLogRecordBuffer *buf)
621630
change->action = REORDER_BUFFER_CHANGE_INSERT;
622631
memcpy(&change->data.tp.relnode, &xlrec->target.node, sizeof(RelFileNode));
623632

624-
if (xlrec->flags & XLOG_HEAP_CONTAINS_NEW_TUPLE)
625-
{
626-
Size datalen = r->xl_len - SizeOfHeapInsert;
627-
Size tuplelen = datalen - SizeOfHeapHeader;
633+
datalen = r->xl_len - SizeOfHeapInsert;
634+
tuplelen = datalen - SizeOfHeapHeader;
628635

629-
Assert(r->xl_len > (SizeOfHeapInsert + SizeOfHeapHeader));
636+
Assert(r->xl_len > (SizeOfHeapInsert + SizeOfHeapHeader));
630637

631-
change->data.tp.newtuple =
632-
ReorderBufferGetTupleBuf(ctx->reorder, tuplelen);
638+
change->data.tp.newtuple =
639+
ReorderBufferGetTupleBuf(ctx->reorder, tuplelen);
633640

634-
DecodeXLogTuple((char *) xlrec + SizeOfHeapInsert,
635-
datalen, change->data.tp.newtuple);
636-
}
641+
DecodeXLogTuple((char *) xlrec + SizeOfHeapInsert,
642+
datalen, change->data.tp.newtuple);
637643

638644
change->data.tp.clear_toast_afterwards = true;
639645

0 commit comments

Comments
 (0)