LCOV - code coverage report
Current view: top level - src/backend/replication/logical - reorderbuffer.c (source / functions) Hit Total Coverage
Test: PostgreSQL 19devel Lines: 1511 1618 93.4 %
Date: 2025-12-25 17:19:02 Functions: 94 94 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*-------------------------------------------------------------------------
       2             :  *
       3             :  * reorderbuffer.c
       4             :  *    PostgreSQL logical replay/reorder buffer management
       5             :  *
       6             :  *
       7             :  * Copyright (c) 2012-2025, PostgreSQL Global Development Group
       8             :  *
       9             :  *
      10             :  * IDENTIFICATION
      11             :  *    src/backend/replication/logical/reorderbuffer.c
      12             :  *
      13             :  * NOTES
      14             :  *    This module gets handed individual pieces of transactions in the order
      15             :  *    they are written to the WAL and is responsible to reassemble them into
      16             :  *    toplevel transaction sized pieces. When a transaction is completely
      17             :  *    reassembled - signaled by reading the transaction commit record - it
      18             :  *    will then call the output plugin (cf. ReorderBufferCommit()) with the
      19             :  *    individual changes. The output plugins rely on snapshots built by
      20             :  *    snapbuild.c which hands them to us.
      21             :  *
      22             :  *    Transactions and subtransactions/savepoints in postgres are not
      23             :  *    immediately linked to each other from outside the performing
      24             :  *    backend. Only at commit/abort (or special xact_assignment records) they
      25             :  *    are linked together. Which means that we will have to splice together a
      26             :  *    toplevel transaction from its subtransactions. To do that efficiently we
      27             :  *    build a binary heap indexed by the smallest current lsn of the individual
      28             :  *    subtransactions' changestreams. As the individual streams are inherently
      29             :  *    ordered by LSN - since that is where we build them from - the transaction
      30             :  *    can easily be reassembled by always using the subtransaction with the
      31             :  *    smallest current LSN from the heap.
      32             :  *
      33             :  *    In order to cope with large transactions - which can be several times as
      34             :  *    big as the available memory - this module supports spooling the contents
      35             :  *    of large transactions to disk. When the transaction is replayed the
      36             :  *    contents of individual (sub-)transactions will be read from disk in
      37             :  *    chunks.
      38             :  *
      39             :  *    This module also has to deal with reassembling toast records from the
      40             :  *    individual chunks stored in WAL. When a new (or initial) version of a
      41             :  *    tuple is stored in WAL it will always be preceded by the toast chunks
      42             :  *    emitted for the columns stored out of line. Within a single toplevel
      43             :  *    transaction there will be no other data carrying records between a row's
      44             :  *    toast chunks and the row data itself. See ReorderBufferToast* for
      45             :  *    details.
      46             :  *
      47             :  *    ReorderBuffer uses two special memory context types - SlabContext for
      48             :  *    allocations of fixed-length structures (changes and transactions), and
      49             :  *    GenerationContext for the variable-length transaction data (allocated
      50             :  *    and freed in groups with similar lifespans).
      51             :  *
      52             :  *    To limit the amount of memory used by decoded changes, we track memory
      53             :  *    used at the reorder buffer level (i.e. total amount of memory), and for
      54             :  *    each transaction. When the total amount of used memory exceeds the
      55             :  *    limit, the transaction consuming the most memory is then serialized to
      56             :  *    disk.
      57             :  *
      58             :  *    Only decoded changes are evicted from memory (spilled to disk), not the
      59             :  *    transaction records. The number of toplevel transactions is limited,
      60             :  *    but a transaction with many subtransactions may still consume significant
      61             :  *    amounts of memory. However, the transaction records are fairly small and
      62             :  *    are not included in the memory limit.
      63             :  *
      64             :  *    The current eviction algorithm is very simple - the transaction is
      65             :  *    picked merely by size, while it might be useful to also consider age
      66             :  *    (LSN) of the changes for example. With the new Generational memory
      67             :  *    allocator, evicting the oldest changes would make it more likely the
      68             :  *    memory gets actually freed.
      69             :  *
      70             :  *    We use a max-heap with transaction size as the key to efficiently find
      71             :  *    the largest transaction. We update the max-heap whenever the memory
      72             :  *    counter is updated; however transactions with size 0 are not stored in
      73             :  *    the heap, because they have no changes to evict.
      74             :  *
      75             :  *    We still rely on max_changes_in_memory when loading serialized changes
      76             :  *    back into memory. At that point we can't use the memory limit directly
      77             :  *    as we load the subxacts independently. One option to deal with this
      78             :  *    would be to count the subxacts, and allow each to allocate 1/N of the
      79             :  *    memory limit. That however does not seem very appealing, because with
      80             :  *    many subtransactions it may easily cause thrashing (short cycles of
      81             :  *    deserializing and applying very few changes). We probably should give
      82             :  *    a bit more memory to the oldest subtransactions, because it's likely
      83             :  *    they are the source for the next sequence of changes.
      84             :  *
      85             :  * -------------------------------------------------------------------------
      86             :  */
      87             : #include "postgres.h"
      88             : 
      89             : #include <unistd.h>
      90             : #include <sys/stat.h>
      91             : 
      92             : #include "access/detoast.h"
      93             : #include "access/heapam.h"
      94             : #include "access/rewriteheap.h"
      95             : #include "access/transam.h"
      96             : #include "access/xact.h"
      97             : #include "access/xlog_internal.h"
      98             : #include "catalog/catalog.h"
      99             : #include "common/int.h"
     100             : #include "lib/binaryheap.h"
     101             : #include "miscadmin.h"
     102             : #include "pgstat.h"
     103             : #include "replication/logical.h"
     104             : #include "replication/reorderbuffer.h"
     105             : #include "replication/slot.h"
     106             : #include "replication/snapbuild.h"    /* just for SnapBuildSnapDecRefcount */
     107             : #include "storage/bufmgr.h"
     108             : #include "storage/fd.h"
     109             : #include "storage/procarray.h"
     110             : #include "storage/sinval.h"
     111             : #include "utils/builtins.h"
     112             : #include "utils/inval.h"
     113             : #include "utils/memutils.h"
     114             : #include "utils/rel.h"
     115             : #include "utils/relfilenumbermap.h"
     116             : 
     117             : /*
     118             :  * Each transaction has an 8MB limit for invalidation messages distributed from
     119             :  * other transactions. This limit is set considering scenarios with many
     120             :  * concurrent logical decoding operations. When the distributed invalidation
     121             :  * messages reach this threshold, the transaction is marked as
     122             :  * RBTXN_DISTR_INVAL_OVERFLOWED to invalidate the complete cache as we have lost
     123             :  * some inval messages and hence don't know what needs to be invalidated.
     124             :  */
     125             : #define MAX_DISTR_INVAL_MSG_PER_TXN \
     126             :     ((8 * 1024 * 1024) / sizeof(SharedInvalidationMessage))
     127             : 
     128             : /* entry for a hash table we use to map from xid to our transaction state */
     129             : typedef struct ReorderBufferTXNByIdEnt
     130             : {
     131             :     TransactionId xid;
     132             :     ReorderBufferTXN *txn;
     133             : } ReorderBufferTXNByIdEnt;
     134             : 
     135             : /* data structures for (relfilelocator, ctid) => (cmin, cmax) mapping */
     136             : typedef struct ReorderBufferTupleCidKey
     137             : {
     138             :     RelFileLocator rlocator;
     139             :     ItemPointerData tid;
     140             : } ReorderBufferTupleCidKey;
     141             : 
     142             : typedef struct ReorderBufferTupleCidEnt
     143             : {
     144             :     ReorderBufferTupleCidKey key;
     145             :     CommandId   cmin;
     146             :     CommandId   cmax;
     147             :     CommandId   combocid;       /* just for debugging */
     148             : } ReorderBufferTupleCidEnt;
     149             : 
     150             : /* Virtual file descriptor with file offset tracking */
     151             : typedef struct TXNEntryFile
     152             : {
     153             :     File        vfd;            /* -1 when the file is closed */
     154             :     off_t       curOffset;      /* offset for next write or read. Reset to 0
     155             :                                  * when vfd is opened. */
     156             : } TXNEntryFile;
     157             : 
     158             : /* k-way in-order change iteration support structures */
     159             : typedef struct ReorderBufferIterTXNEntry
     160             : {
     161             :     XLogRecPtr  lsn;
     162             :     ReorderBufferChange *change;
     163             :     ReorderBufferTXN *txn;
     164             :     TXNEntryFile file;
     165             :     XLogSegNo   segno;
     166             : } ReorderBufferIterTXNEntry;
     167             : 
     168             : typedef struct ReorderBufferIterTXNState
     169             : {
     170             :     binaryheap *heap;
     171             :     Size        nr_txns;
     172             :     dlist_head  old_change;
     173             :     ReorderBufferIterTXNEntry entries[FLEXIBLE_ARRAY_MEMBER];
     174             : } ReorderBufferIterTXNState;
     175             : 
     176             : /* toast datastructures */
     177             : typedef struct ReorderBufferToastEnt
     178             : {
     179             :     Oid         chunk_id;       /* toast_table.chunk_id */
     180             :     int32       last_chunk_seq; /* toast_table.chunk_seq of the last chunk we
     181             :                                  * have seen */
     182             :     Size        num_chunks;     /* number of chunks we've already seen */
     183             :     Size        size;           /* combined size of chunks seen */
     184             :     dlist_head  chunks;         /* linked list of chunks */
     185             :     struct varlena *reconstructed;  /* reconstructed varlena now pointed to in
     186             :                                      * main tup */
     187             : } ReorderBufferToastEnt;
     188             : 
     189             : /* Disk serialization support datastructures */
     190             : typedef struct ReorderBufferDiskChange
     191             : {
     192             :     Size        size;
     193             :     ReorderBufferChange change;
     194             :     /* data follows */
     195             : } ReorderBufferDiskChange;
     196             : 
     197             : #define IsSpecInsert(action) \
     198             : ( \
     199             :     ((action) == REORDER_BUFFER_CHANGE_INTERNAL_SPEC_INSERT) \
     200             : )
     201             : #define IsSpecConfirmOrAbort(action) \
     202             : ( \
     203             :     (((action) == REORDER_BUFFER_CHANGE_INTERNAL_SPEC_CONFIRM) || \
     204             :     ((action) == REORDER_BUFFER_CHANGE_INTERNAL_SPEC_ABORT)) \
     205             : )
     206             : #define IsInsertOrUpdate(action) \
     207             : ( \
     208             :     (((action) == REORDER_BUFFER_CHANGE_INSERT) || \
     209             :     ((action) == REORDER_BUFFER_CHANGE_UPDATE) || \
     210             :     ((action) == REORDER_BUFFER_CHANGE_INTERNAL_SPEC_INSERT)) \
     211             : )
     212             : 
     213             : /*
     214             :  * Maximum number of changes kept in memory, per transaction. After that,
     215             :  * changes are spooled to disk.
     216             :  *
     217             :  * The current value should be sufficient to decode the entire transaction
     218             :  * without hitting disk in OLTP workloads, while starting to spool to disk in
     219             :  * other workloads reasonably fast.
     220             :  *
     221             :  * At some point in the future it probably makes sense to have a more elaborate
     222             :  * resource management here, but it's not entirely clear what that would look
     223             :  * like.
     224             :  */
     225             : int         logical_decoding_work_mem;
     226             : static const Size max_changes_in_memory = 4096; /* XXX for restore only */
     227             : 
     228             : /* GUC variable */
     229             : int         debug_logical_replication_streaming = DEBUG_LOGICAL_REP_STREAMING_BUFFERED;
     230             : 
     231             : /* ---------------------------------------
     232             :  * primary reorderbuffer support routines
     233             :  * ---------------------------------------
     234             :  */
     235             : static ReorderBufferTXN *ReorderBufferAllocTXN(ReorderBuffer *rb);
     236             : static void ReorderBufferFreeTXN(ReorderBuffer *rb, ReorderBufferTXN *txn);
     237             : static ReorderBufferTXN *ReorderBufferTXNByXid(ReorderBuffer *rb,
     238             :                                                TransactionId xid, bool create, bool *is_new,
     239             :                                                XLogRecPtr lsn, bool create_as_top);
     240             : static void ReorderBufferTransferSnapToParent(ReorderBufferTXN *txn,
     241             :                                               ReorderBufferTXN *subtxn);
     242             : 
     243             : static void AssertTXNLsnOrder(ReorderBuffer *rb);
     244             : 
     245             : /* ---------------------------------------
     246             :  * support functions for lsn-order iterating over the ->changes of a
     247             :  * transaction and its subtransactions
     248             :  *
     249             :  * used for iteration over the k-way heap merge of a transaction and its
     250             :  * subtransactions
     251             :  * ---------------------------------------
     252             :  */
     253             : static void ReorderBufferIterTXNInit(ReorderBuffer *rb, ReorderBufferTXN *txn,
     254             :                                      ReorderBufferIterTXNState *volatile *iter_state);
     255             : static ReorderBufferChange *ReorderBufferIterTXNNext(ReorderBuffer *rb, ReorderBufferIterTXNState *state);
     256             : static void ReorderBufferIterTXNFinish(ReorderBuffer *rb,
     257             :                                        ReorderBufferIterTXNState *state);
     258             : static void ReorderBufferExecuteInvalidations(uint32 nmsgs, SharedInvalidationMessage *msgs);
     259             : 
     260             : /*
     261             :  * ---------------------------------------
     262             :  * Disk serialization support functions
     263             :  * ---------------------------------------
     264             :  */
     265             : static void ReorderBufferCheckMemoryLimit(ReorderBuffer *rb);
     266             : static void ReorderBufferSerializeTXN(ReorderBuffer *rb, ReorderBufferTXN *txn);
     267             : static void ReorderBufferSerializeChange(ReorderBuffer *rb, ReorderBufferTXN *txn,
     268             :                                          int fd, ReorderBufferChange *change);
     269             : static Size ReorderBufferRestoreChanges(ReorderBuffer *rb, ReorderBufferTXN *txn,
     270             :                                         TXNEntryFile *file, XLogSegNo *segno);
     271             : static void ReorderBufferRestoreChange(ReorderBuffer *rb, ReorderBufferTXN *txn,
     272             :                                        char *data);
     273             : static void ReorderBufferRestoreCleanup(ReorderBuffer *rb, ReorderBufferTXN *txn);
     274             : static void ReorderBufferTruncateTXN(ReorderBuffer *rb, ReorderBufferTXN *txn,
     275             :                                      bool txn_prepared);
     276             : static void ReorderBufferMaybeMarkTXNStreamed(ReorderBuffer *rb, ReorderBufferTXN *txn);
     277             : static bool ReorderBufferCheckAndTruncateAbortedTXN(ReorderBuffer *rb, ReorderBufferTXN *txn);
     278             : static void ReorderBufferCleanupSerializedTXNs(const char *slotname);
     279             : static void ReorderBufferSerializedPath(char *path, ReplicationSlot *slot,
     280             :                                         TransactionId xid, XLogSegNo segno);
     281             : static int  ReorderBufferTXNSizeCompare(const pairingheap_node *a, const pairingheap_node *b, void *arg);
     282             : 
     283             : static void ReorderBufferFreeSnap(ReorderBuffer *rb, Snapshot snap);
     284             : static Snapshot ReorderBufferCopySnap(ReorderBuffer *rb, Snapshot orig_snap,
     285             :                                       ReorderBufferTXN *txn, CommandId cid);
     286             : 
     287             : /*
     288             :  * ---------------------------------------
     289             :  * Streaming support functions
     290             :  * ---------------------------------------
     291             :  */
     292             : static inline bool ReorderBufferCanStream(ReorderBuffer *rb);
     293             : static inline bool ReorderBufferCanStartStreaming(ReorderBuffer *rb);
     294             : static void ReorderBufferStreamTXN(ReorderBuffer *rb, ReorderBufferTXN *txn);
     295             : static void ReorderBufferStreamCommit(ReorderBuffer *rb, ReorderBufferTXN *txn);
     296             : 
     297             : /* ---------------------------------------
     298             :  * toast reassembly support
     299             :  * ---------------------------------------
     300             :  */
     301             : static void ReorderBufferToastInitHash(ReorderBuffer *rb, ReorderBufferTXN *txn);
     302             : static void ReorderBufferToastReset(ReorderBuffer *rb, ReorderBufferTXN *txn);
     303             : static void ReorderBufferToastReplace(ReorderBuffer *rb, ReorderBufferTXN *txn,
     304             :                                       Relation relation, ReorderBufferChange *change);
     305             : static void ReorderBufferToastAppendChunk(ReorderBuffer *rb, ReorderBufferTXN *txn,
     306             :                                           Relation relation, ReorderBufferChange *change);
     307             : 
     308             : /*
     309             :  * ---------------------------------------
     310             :  * memory accounting
     311             :  * ---------------------------------------
     312             :  */
     313             : static Size ReorderBufferChangeSize(ReorderBufferChange *change);
     314             : static void ReorderBufferChangeMemoryUpdate(ReorderBuffer *rb,
     315             :                                             ReorderBufferChange *change,
     316             :                                             ReorderBufferTXN *txn,
     317             :                                             bool addition, Size sz);
     318             : 
     319             : /*
     320             :  * Allocate a new ReorderBuffer and clean out any old serialized state from
     321             :  * prior ReorderBuffer instances for the same slot.
     322             :  */
     323             : ReorderBuffer *
     324        2240 : ReorderBufferAllocate(void)
     325             : {
     326             :     ReorderBuffer *buffer;
     327             :     HASHCTL     hash_ctl;
     328             :     MemoryContext new_ctx;
     329             : 
     330             :     Assert(MyReplicationSlot != NULL);
     331             : 
     332             :     /* allocate memory in own context, to have better accountability */
     333        2240 :     new_ctx = AllocSetContextCreate(CurrentMemoryContext,
     334             :                                     "ReorderBuffer",
     335             :                                     ALLOCSET_DEFAULT_SIZES);
     336             : 
     337             :     buffer =
     338        2240 :         (ReorderBuffer *) MemoryContextAlloc(new_ctx, sizeof(ReorderBuffer));
     339             : 
     340        2240 :     memset(&hash_ctl, 0, sizeof(hash_ctl));
     341             : 
     342        2240 :     buffer->context = new_ctx;
     343             : 
     344        2240 :     buffer->change_context = SlabContextCreate(new_ctx,
     345             :                                                "Change",
     346             :                                                SLAB_DEFAULT_BLOCK_SIZE,
     347             :                                                sizeof(ReorderBufferChange));
     348             : 
     349        2240 :     buffer->txn_context = SlabContextCreate(new_ctx,
     350             :                                             "TXN",
     351             :                                             SLAB_DEFAULT_BLOCK_SIZE,
     352             :                                             sizeof(ReorderBufferTXN));
     353             : 
     354             :     /*
     355             :      * To minimize memory fragmentation caused by long-running transactions
     356             :      * with changes spanning multiple memory blocks, we use a single
     357             :      * fixed-size memory block for decoded tuple storage. The performance
     358             :      * testing showed that the default memory block size maintains logical
     359             :      * decoding performance without causing fragmentation due to concurrent
     360             :      * transactions. One might think that we can use the max size as
     361             :      * SLAB_LARGE_BLOCK_SIZE but the test also showed it doesn't help resolve
     362             :      * the memory fragmentation.
     363             :      */
     364        2240 :     buffer->tup_context = GenerationContextCreate(new_ctx,
     365             :                                                   "Tuples",
     366             :                                                   SLAB_DEFAULT_BLOCK_SIZE,
     367             :                                                   SLAB_DEFAULT_BLOCK_SIZE,
     368             :                                                   SLAB_DEFAULT_BLOCK_SIZE);
     369             : 
     370        2240 :     hash_ctl.keysize = sizeof(TransactionId);
     371        2240 :     hash_ctl.entrysize = sizeof(ReorderBufferTXNByIdEnt);
     372        2240 :     hash_ctl.hcxt = buffer->context;
     373             : 
     374        2240 :     buffer->by_txn = hash_create("ReorderBufferByXid", 1000, &hash_ctl,
     375             :                                  HASH_ELEM | HASH_BLOBS | HASH_CONTEXT);
     376             : 
     377        2240 :     buffer->by_txn_last_xid = InvalidTransactionId;
     378        2240 :     buffer->by_txn_last_txn = NULL;
     379             : 
     380        2240 :     buffer->outbuf = NULL;
     381        2240 :     buffer->outbufsize = 0;
     382        2240 :     buffer->size = 0;
     383             : 
     384             :     /* txn_heap is ordered by transaction size */
     385        2240 :     buffer->txn_heap = pairingheap_allocate(ReorderBufferTXNSizeCompare, NULL);
     386             : 
     387        2240 :     buffer->spillTxns = 0;
     388        2240 :     buffer->spillCount = 0;
     389        2240 :     buffer->spillBytes = 0;
     390        2240 :     buffer->streamTxns = 0;
     391        2240 :     buffer->streamCount = 0;
     392        2240 :     buffer->streamBytes = 0;
     393        2240 :     buffer->memExceededCount = 0;
     394        2240 :     buffer->totalTxns = 0;
     395        2240 :     buffer->totalBytes = 0;
     396             : 
     397        2240 :     buffer->current_restart_decoding_lsn = InvalidXLogRecPtr;
     398             : 
     399        2240 :     dlist_init(&buffer->toplevel_by_lsn);
     400        2240 :     dlist_init(&buffer->txns_by_base_snapshot_lsn);
     401        2240 :     dclist_init(&buffer->catchange_txns);
     402             : 
     403             :     /*
     404             :      * Ensure there's no stale data from prior uses of this slot, in case some
     405             :      * prior exit avoided calling ReorderBufferFree. Failure to do this can
     406             :      * produce duplicated txns, and it's very cheap if there's nothing there.
     407             :      */
     408        2240 :     ReorderBufferCleanupSerializedTXNs(NameStr(MyReplicationSlot->data.name));
     409             : 
     410        2240 :     return buffer;
     411             : }
     412             : 
     413             : /*
     414             :  * Free a ReorderBuffer
     415             :  */
     416             : void
     417        1782 : ReorderBufferFree(ReorderBuffer *rb)
     418             : {
     419        1782 :     MemoryContext context = rb->context;
     420             : 
     421             :     /*
     422             :      * We free separately allocated data by entirely scrapping reorderbuffer's
     423             :      * memory context.
     424             :      */
     425        1782 :     MemoryContextDelete(context);
     426             : 
     427             :     /* Free disk space used by unconsumed reorder buffers */
     428        1782 :     ReorderBufferCleanupSerializedTXNs(NameStr(MyReplicationSlot->data.name));
     429        1782 : }
     430             : 
     431             : /*
     432             :  * Allocate a new ReorderBufferTXN.
     433             :  */
     434             : static ReorderBufferTXN *
     435        7960 : ReorderBufferAllocTXN(ReorderBuffer *rb)
     436             : {
     437             :     ReorderBufferTXN *txn;
     438             : 
     439             :     txn = (ReorderBufferTXN *)
     440        7960 :         MemoryContextAlloc(rb->txn_context, sizeof(ReorderBufferTXN));
     441             : 
     442        7960 :     memset(txn, 0, sizeof(ReorderBufferTXN));
     443             : 
     444        7960 :     dlist_init(&txn->changes);
     445        7960 :     dlist_init(&txn->tuplecids);
     446        7960 :     dlist_init(&txn->subtxns);
     447             : 
     448             :     /* InvalidCommandId is not zero, so set it explicitly */
     449        7960 :     txn->command_id = InvalidCommandId;
     450        7960 :     txn->output_plugin_private = NULL;
     451             : 
     452        7960 :     return txn;
     453             : }
     454             : 
     455             : /*
     456             :  * Free a ReorderBufferTXN.
     457             :  */
     458             : static void
     459        7782 : ReorderBufferFreeTXN(ReorderBuffer *rb, ReorderBufferTXN *txn)
     460             : {
     461             :     /* clean the lookup cache if we were cached (quite likely) */
     462        7782 :     if (rb->by_txn_last_xid == txn->xid)
     463             :     {
     464        7412 :         rb->by_txn_last_xid = InvalidTransactionId;
     465        7412 :         rb->by_txn_last_txn = NULL;
     466             :     }
     467             : 
     468             :     /* free data that's contained */
     469             : 
     470        7782 :     if (txn->gid != NULL)
     471             :     {
     472          82 :         pfree(txn->gid);
     473          82 :         txn->gid = NULL;
     474             :     }
     475             : 
     476        7782 :     if (txn->tuplecid_hash != NULL)
     477             :     {
     478        1112 :         hash_destroy(txn->tuplecid_hash);
     479        1112 :         txn->tuplecid_hash = NULL;
     480             :     }
     481             : 
     482        7782 :     if (txn->invalidations)
     483             :     {
     484        2294 :         pfree(txn->invalidations);
     485        2294 :         txn->invalidations = NULL;
     486             :     }
     487             : 
     488        7782 :     if (txn->invalidations_distributed)
     489             :     {
     490          42 :         pfree(txn->invalidations_distributed);
     491          42 :         txn->invalidations_distributed = NULL;
     492             :     }
     493             : 
     494             :     /* Reset the toast hash */
     495        7782 :     ReorderBufferToastReset(rb, txn);
     496             : 
     497             :     /* All changes must be deallocated */
     498             :     Assert(txn->size == 0);
     499             : 
     500        7782 :     pfree(txn);
     501        7782 : }
     502             : 
     503             : /*
     504             :  * Allocate a ReorderBufferChange.
     505             :  */
     506             : ReorderBufferChange *
     507     3552116 : ReorderBufferAllocChange(ReorderBuffer *rb)
     508             : {
     509             :     ReorderBufferChange *change;
     510             : 
     511             :     change = (ReorderBufferChange *)
     512     3552116 :         MemoryContextAlloc(rb->change_context, sizeof(ReorderBufferChange));
     513             : 
     514     3552116 :     memset(change, 0, sizeof(ReorderBufferChange));
     515     3552116 :     return change;
     516             : }
     517             : 
     518             : /*
     519             :  * Free a ReorderBufferChange and update memory accounting, if requested.
     520             :  */
     521             : void
     522     3551588 : ReorderBufferFreeChange(ReorderBuffer *rb, ReorderBufferChange *change,
     523             :                         bool upd_mem)
     524             : {
     525             :     /* update memory accounting info */
     526     3551588 :     if (upd_mem)
     527      403094 :         ReorderBufferChangeMemoryUpdate(rb, change, NULL, false,
     528             :                                         ReorderBufferChangeSize(change));
     529             : 
     530             :     /* free contained data */
     531     3551588 :     switch (change->action)
     532             :     {
     533     3406792 :         case REORDER_BUFFER_CHANGE_INSERT:
     534             :         case REORDER_BUFFER_CHANGE_UPDATE:
     535             :         case REORDER_BUFFER_CHANGE_DELETE:
     536             :         case REORDER_BUFFER_CHANGE_INTERNAL_SPEC_INSERT:
     537     3406792 :             if (change->data.tp.newtuple)
     538             :             {
     539     2941122 :                 ReorderBufferFreeTupleBuf(change->data.tp.newtuple);
     540     2941122 :                 change->data.tp.newtuple = NULL;
     541             :             }
     542             : 
     543     3406792 :             if (change->data.tp.oldtuple)
     544             :             {
     545      328994 :                 ReorderBufferFreeTupleBuf(change->data.tp.oldtuple);
     546      328994 :                 change->data.tp.oldtuple = NULL;
     547             :             }
     548     3406792 :             break;
     549          80 :         case REORDER_BUFFER_CHANGE_MESSAGE:
     550          80 :             if (change->data.msg.prefix != NULL)
     551          80 :                 pfree(change->data.msg.prefix);
     552          80 :             change->data.msg.prefix = NULL;
     553          80 :             if (change->data.msg.message != NULL)
     554          80 :                 pfree(change->data.msg.message);
     555          80 :             change->data.msg.message = NULL;
     556          80 :             break;
     557        9808 :         case REORDER_BUFFER_CHANGE_INVALIDATION:
     558        9808 :             if (change->data.inval.invalidations)
     559        9808 :                 pfree(change->data.inval.invalidations);
     560        9808 :             change->data.inval.invalidations = NULL;
     561        9808 :             break;
     562        2352 :         case REORDER_BUFFER_CHANGE_INTERNAL_SNAPSHOT:
     563        2352 :             if (change->data.snapshot)
     564             :             {
     565        2352 :                 ReorderBufferFreeSnap(rb, change->data.snapshot);
     566        2352 :                 change->data.snapshot = NULL;
     567             :             }
     568        2352 :             break;
     569             :             /* no data in addition to the struct itself */
     570          88 :         case REORDER_BUFFER_CHANGE_TRUNCATE:
     571          88 :             if (change->data.truncate.relids != NULL)
     572             :             {
     573          88 :                 ReorderBufferFreeRelids(rb, change->data.truncate.relids);
     574          88 :                 change->data.truncate.relids = NULL;
     575             :             }
     576          88 :             break;
     577      132468 :         case REORDER_BUFFER_CHANGE_INTERNAL_SPEC_CONFIRM:
     578             :         case REORDER_BUFFER_CHANGE_INTERNAL_SPEC_ABORT:
     579             :         case REORDER_BUFFER_CHANGE_INTERNAL_COMMAND_ID:
     580             :         case REORDER_BUFFER_CHANGE_INTERNAL_TUPLECID:
     581      132468 :             break;
     582             :     }
     583             : 
     584     3551588 :     pfree(change);
     585     3551588 : }
     586             : 
     587             : /*
     588             :  * Allocate a HeapTuple fitting a tuple of size tuple_len (excluding header
     589             :  * overhead).
     590             :  */
     591             : HeapTuple
     592     3270268 : ReorderBufferAllocTupleBuf(ReorderBuffer *rb, Size tuple_len)
     593             : {
     594             :     HeapTuple   tuple;
     595             :     Size        alloc_len;
     596             : 
     597     3270268 :     alloc_len = tuple_len + SizeofHeapTupleHeader;
     598             : 
     599     3270268 :     tuple = (HeapTuple) MemoryContextAlloc(rb->tup_context,
     600             :                                            HEAPTUPLESIZE + alloc_len);
     601     3270268 :     tuple->t_data = (HeapTupleHeader) ((char *) tuple + HEAPTUPLESIZE);
     602             : 
     603     3270268 :     return tuple;
     604             : }
     605             : 
     606             : /*
     607             :  * Free a HeapTuple returned by ReorderBufferAllocTupleBuf().
     608             :  */
     609             : void
     610     3270116 : ReorderBufferFreeTupleBuf(HeapTuple tuple)
     611             : {
     612     3270116 :     pfree(tuple);
     613     3270116 : }
     614             : 
     615             : /*
     616             :  * Allocate an array for relids of truncated relations.
     617             :  *
     618             :  * We use the global memory context (for the whole reorder buffer), because
     619             :  * none of the existing ones seems like a good match (some are SLAB, so we
     620             :  * can't use those, and tup_context is meant for tuple data, not relids). We
     621             :  * could add yet another context, but it seems like an overkill - TRUNCATE is
     622             :  * not particularly common operation, so it does not seem worth it.
     623             :  */
     624             : Oid *
     625          98 : ReorderBufferAllocRelids(ReorderBuffer *rb, int nrelids)
     626             : {
     627             :     Oid        *relids;
     628             :     Size        alloc_len;
     629             : 
     630          98 :     alloc_len = sizeof(Oid) * nrelids;
     631             : 
     632          98 :     relids = (Oid *) MemoryContextAlloc(rb->context, alloc_len);
     633             : 
     634          98 :     return relids;
     635             : }
     636             : 
     637             : /*
     638             :  * Free an array of relids.
     639             :  */
     640             : void
     641          88 : ReorderBufferFreeRelids(ReorderBuffer *rb, Oid *relids)
     642             : {
     643          88 :     pfree(relids);
     644          88 : }
     645             : 
     646             : /*
     647             :  * Return the ReorderBufferTXN from the given buffer, specified by Xid.
     648             :  * If create is true, and a transaction doesn't already exist, create it
     649             :  * (with the given LSN, and as top transaction if that's specified);
     650             :  * when this happens, is_new is set to true.
     651             :  */
     652             : static ReorderBufferTXN *
     653    11816624 : ReorderBufferTXNByXid(ReorderBuffer *rb, TransactionId xid, bool create,
     654             :                       bool *is_new, XLogRecPtr lsn, bool create_as_top)
     655             : {
     656             :     ReorderBufferTXN *txn;
     657             :     ReorderBufferTXNByIdEnt *ent;
     658             :     bool        found;
     659             : 
     660             :     Assert(TransactionIdIsValid(xid));
     661             : 
     662             :     /*
     663             :      * Check the one-entry lookup cache first
     664             :      */
     665    11816624 :     if (TransactionIdIsValid(rb->by_txn_last_xid) &&
     666    11809076 :         rb->by_txn_last_xid == xid)
     667             :     {
     668     9829292 :         txn = rb->by_txn_last_txn;
     669             : 
     670     9829292 :         if (txn != NULL)
     671             :         {
     672             :             /* found it, and it's valid */
     673     9829254 :             if (is_new)
     674        6430 :                 *is_new = false;
     675     9829254 :             return txn;
     676             :         }
     677             : 
     678             :         /*
     679             :          * cached as non-existent, and asked not to create? Then nothing else
     680             :          * to do.
     681             :          */
     682          38 :         if (!create)
     683          32 :             return NULL;
     684             :         /* otherwise fall through to create it */
     685             :     }
     686             : 
     687             :     /*
     688             :      * If the cache wasn't hit or it yielded a "does-not-exist" and we want to
     689             :      * create an entry.
     690             :      */
     691             : 
     692             :     /* search the lookup table */
     693             :     ent = (ReorderBufferTXNByIdEnt *)
     694     1987338 :         hash_search(rb->by_txn,
     695             :                     &xid,
     696             :                     create ? HASH_ENTER : HASH_FIND,
     697             :                     &found);
     698     1987338 :     if (found)
     699     1976798 :         txn = ent->txn;
     700       10540 :     else if (create)
     701             :     {
     702             :         /* initialize the new entry, if creation was requested */
     703             :         Assert(ent != NULL);
     704             :         Assert(XLogRecPtrIsValid(lsn));
     705             : 
     706        7960 :         ent->txn = ReorderBufferAllocTXN(rb);
     707        7960 :         ent->txn->xid = xid;
     708        7960 :         txn = ent->txn;
     709        7960 :         txn->first_lsn = lsn;
     710        7960 :         txn->restart_decoding_lsn = rb->current_restart_decoding_lsn;
     711             : 
     712        7960 :         if (create_as_top)
     713             :         {
     714        6594 :             dlist_push_tail(&rb->toplevel_by_lsn, &txn->node);
     715        6594 :             AssertTXNLsnOrder(rb);
     716             :         }
     717             :     }
     718             :     else
     719        2580 :         txn = NULL;             /* not found and not asked to create */
     720             : 
     721             :     /* update cache */
     722     1987338 :     rb->by_txn_last_xid = xid;
     723     1987338 :     rb->by_txn_last_txn = txn;
     724             : 
     725     1987338 :     if (is_new)
     726        3590 :         *is_new = !found;
     727             : 
     728             :     Assert(!create || txn != NULL);
     729     1987338 :     return txn;
     730             : }
     731             : 
     732             : /*
     733             :  * Record the partial change for the streaming of in-progress transactions.  We
     734             :  * can stream only complete changes so if we have a partial change like toast
     735             :  * table insert or speculative insert then we mark such a 'txn' so that it
     736             :  * can't be streamed.  We also ensure that if the changes in such a 'txn' can
     737             :  * be streamed and are above logical_decoding_work_mem threshold then we stream
     738             :  * them as soon as we have a complete change.
     739             :  */
     740             : static void
     741     3129998 : ReorderBufferProcessPartialChange(ReorderBuffer *rb, ReorderBufferTXN *txn,
     742             :                                   ReorderBufferChange *change,
     743             :                                   bool toast_insert)
     744             : {
     745             :     ReorderBufferTXN *toptxn;
     746             : 
     747             :     /*
     748             :      * The partial changes need to be processed only while streaming
     749             :      * in-progress transactions.
     750             :      */
     751     3129998 :     if (!ReorderBufferCanStream(rb))
     752     2420388 :         return;
     753             : 
     754             :     /* Get the top transaction. */
     755      709610 :     toptxn = rbtxn_get_toptxn(txn);
     756             : 
     757             :     /*
     758             :      * Indicate a partial change for toast inserts.  The change will be
     759             :      * considered as complete once we get the insert or update on the main
     760             :      * table and we are sure that the pending toast chunks are not required
     761             :      * anymore.
     762             :      *
     763             :      * If we allow streaming when there are pending toast chunks then such
     764             :      * chunks won't be released till the insert (multi_insert) is complete and
     765             :      * we expect the txn to have streamed all changes after streaming.  This
     766             :      * restriction is mainly to ensure the correctness of streamed
     767             :      * transactions and it doesn't seem worth uplifting such a restriction
     768             :      * just to allow this case because anyway we will stream the transaction
     769             :      * once such an insert is complete.
     770             :      */
     771      709610 :     if (toast_insert)
     772        3332 :         toptxn->txn_flags |= RBTXN_HAS_PARTIAL_CHANGE;
     773      706278 :     else if (rbtxn_has_partial_change(toptxn) &&
     774         126 :              IsInsertOrUpdate(change->action) &&
     775         126 :              change->data.tp.clear_toast_afterwards)
     776          86 :         toptxn->txn_flags &= ~RBTXN_HAS_PARTIAL_CHANGE;
     777             : 
     778             :     /*
     779             :      * Indicate a partial change for speculative inserts.  The change will be
     780             :      * considered as complete once we get the speculative confirm or abort
     781             :      * token.
     782             :      */
     783      709610 :     if (IsSpecInsert(change->action))
     784           0 :         toptxn->txn_flags |= RBTXN_HAS_PARTIAL_CHANGE;
     785      709610 :     else if (rbtxn_has_partial_change(toptxn) &&
     786        3372 :              IsSpecConfirmOrAbort(change->action))
     787           0 :         toptxn->txn_flags &= ~RBTXN_HAS_PARTIAL_CHANGE;
     788             : 
     789             :     /*
     790             :      * Stream the transaction if it is serialized before and the changes are
     791             :      * now complete in the top-level transaction.
     792             :      *
     793             :      * The reason for doing the streaming of such a transaction as soon as we
     794             :      * get the complete change for it is that previously it would have reached
     795             :      * the memory threshold and wouldn't get streamed because of incomplete
     796             :      * changes.  Delaying such transactions would increase apply lag for them.
     797             :      */
     798      709610 :     if (ReorderBufferCanStartStreaming(rb) &&
     799      338098 :         !(rbtxn_has_partial_change(toptxn)) &&
     800      335026 :         rbtxn_is_serialized(txn) &&
     801          78 :         rbtxn_has_streamable_change(toptxn))
     802          18 :         ReorderBufferStreamTXN(rb, toptxn);
     803             : }
     804             : 
     805             : /*
     806             :  * Queue a change into a transaction so it can be replayed upon commit or will be
     807             :  * streamed when we reach logical_decoding_work_mem threshold.
     808             :  */
     809             : void
     810     3148816 : ReorderBufferQueueChange(ReorderBuffer *rb, TransactionId xid, XLogRecPtr lsn,
     811             :                          ReorderBufferChange *change, bool toast_insert)
     812             : {
     813             :     ReorderBufferTXN *txn;
     814             : 
     815     3148816 :     txn = ReorderBufferTXNByXid(rb, xid, true, NULL, lsn, true);
     816             : 
     817             :     /*
     818             :      * If we have detected that the transaction is aborted while streaming the
     819             :      * previous changes or by checking its CLOG, there is no point in
     820             :      * collecting further changes for it.
     821             :      */
     822     3148816 :     if (rbtxn_is_aborted(txn))
     823             :     {
     824             :         /*
     825             :          * We don't need to update memory accounting for this change as we
     826             :          * have not added it to the queue yet.
     827             :          */
     828       18818 :         ReorderBufferFreeChange(rb, change, false);
     829       18818 :         return;
     830             :     }
     831             : 
     832             :     /*
     833             :      * The changes that are sent downstream are considered streamable.  We
     834             :      * remember such transactions so that only those will later be considered
     835             :      * for streaming.
     836             :      */
     837     3129998 :     if (change->action == REORDER_BUFFER_CHANGE_INSERT ||
     838      906648 :         change->action == REORDER_BUFFER_CHANGE_UPDATE ||
     839      572278 :         change->action == REORDER_BUFFER_CHANGE_DELETE ||
     840      130328 :         change->action == REORDER_BUFFER_CHANGE_INTERNAL_SPEC_INSERT ||
     841       94496 :         change->action == REORDER_BUFFER_CHANGE_TRUNCATE ||
     842       94418 :         change->action == REORDER_BUFFER_CHANGE_MESSAGE)
     843             :     {
     844     3035658 :         ReorderBufferTXN *toptxn = rbtxn_get_toptxn(txn);
     845             : 
     846     3035658 :         toptxn->txn_flags |= RBTXN_HAS_STREAMABLE_CHANGE;
     847             :     }
     848             : 
     849     3129998 :     change->lsn = lsn;
     850     3129998 :     change->txn = txn;
     851             : 
     852             :     Assert(XLogRecPtrIsValid(lsn));
     853     3129998 :     dlist_push_tail(&txn->changes, &change->node);
     854     3129998 :     txn->nentries++;
     855     3129998 :     txn->nentries_mem++;
     856             : 
     857             :     /* update memory accounting information */
     858     3129998 :     ReorderBufferChangeMemoryUpdate(rb, change, NULL, true,
     859             :                                     ReorderBufferChangeSize(change));
     860             : 
     861             :     /* process partial change */
     862     3129998 :     ReorderBufferProcessPartialChange(rb, txn, change, toast_insert);
     863             : 
     864             :     /* check the memory limits and evict something if needed */
     865     3129998 :     ReorderBufferCheckMemoryLimit(rb);
     866             : }
     867             : 
     868             : /*
     869             :  * A transactional message is queued to be processed upon commit and a
     870             :  * non-transactional message gets processed immediately.
     871             :  */
     872             : void
     873          94 : ReorderBufferQueueMessage(ReorderBuffer *rb, TransactionId xid,
     874             :                           Snapshot snap, XLogRecPtr lsn,
     875             :                           bool transactional, const char *prefix,
     876             :                           Size message_size, const char *message)
     877             : {
     878          94 :     if (transactional)
     879             :     {
     880             :         MemoryContext oldcontext;
     881             :         ReorderBufferChange *change;
     882             : 
     883             :         Assert(xid != InvalidTransactionId);
     884             : 
     885             :         /*
     886             :          * We don't expect snapshots for transactional changes - we'll use the
     887             :          * snapshot derived later during apply (unless the change gets
     888             :          * skipped).
     889             :          */
     890             :         Assert(!snap);
     891             : 
     892          78 :         oldcontext = MemoryContextSwitchTo(rb->context);
     893             : 
     894          78 :         change = ReorderBufferAllocChange(rb);
     895          78 :         change->action = REORDER_BUFFER_CHANGE_MESSAGE;
     896          78 :         change->data.msg.prefix = pstrdup(prefix);
     897          78 :         change->data.msg.message_size = message_size;
     898          78 :         change->data.msg.message = palloc(message_size);
     899          78 :         memcpy(change->data.msg.message, message, message_size);
     900             : 
     901          78 :         ReorderBufferQueueChange(rb, xid, lsn, change, false);
     902             : 
     903          78 :         MemoryContextSwitchTo(oldcontext);
     904             :     }
     905             :     else
     906             :     {
     907          16 :         ReorderBufferTXN *txn = NULL;
     908          16 :         volatile Snapshot snapshot_now = snap;
     909             : 
     910             :         /* Non-transactional changes require a valid snapshot. */
     911             :         Assert(snapshot_now);
     912             : 
     913          16 :         if (xid != InvalidTransactionId)
     914           6 :             txn = ReorderBufferTXNByXid(rb, xid, true, NULL, lsn, true);
     915             : 
     916             :         /* setup snapshot to allow catalog access */
     917          16 :         SetupHistoricSnapshot(snapshot_now, NULL);
     918          16 :         PG_TRY();
     919             :         {
     920          16 :             rb->message(rb, txn, lsn, false, prefix, message_size, message);
     921             : 
     922          16 :             TeardownHistoricSnapshot(false);
     923             :         }
     924           0 :         PG_CATCH();
     925             :         {
     926           0 :             TeardownHistoricSnapshot(true);
     927           0 :             PG_RE_THROW();
     928             :         }
     929          16 :         PG_END_TRY();
     930             :     }
     931          94 : }
     932             : 
     933             : /*
     934             :  * AssertTXNLsnOrder
     935             :  *      Verify LSN ordering of transaction lists in the reorderbuffer
     936             :  *
     937             :  * Other LSN-related invariants are checked too.
     938             :  *
     939             :  * No-op if assertions are not in use.
     940             :  */
     941             : static void
     942       16140 : AssertTXNLsnOrder(ReorderBuffer *rb)
     943             : {
     944             : #ifdef USE_ASSERT_CHECKING
     945             :     LogicalDecodingContext *ctx = rb->private_data;
     946             :     dlist_iter  iter;
     947             :     XLogRecPtr  prev_first_lsn = InvalidXLogRecPtr;
     948             :     XLogRecPtr  prev_base_snap_lsn = InvalidXLogRecPtr;
     949             : 
     950             :     /*
     951             :      * Skip the verification if we don't reach the LSN at which we start
     952             :      * decoding the contents of transactions yet because until we reach the
     953             :      * LSN, we could have transactions that don't have the association between
     954             :      * the top-level transaction and subtransaction yet and consequently have
     955             :      * the same LSN.  We don't guarantee this association until we try to
     956             :      * decode the actual contents of transaction. The ordering of the records
     957             :      * prior to the start_decoding_at LSN should have been checked before the
     958             :      * restart.
     959             :      */
     960             :     if (SnapBuildXactNeedsSkip(ctx->snapshot_builder, ctx->reader->EndRecPtr))
     961             :         return;
     962             : 
     963             :     dlist_foreach(iter, &rb->toplevel_by_lsn)
     964             :     {
     965             :         ReorderBufferTXN *cur_txn = dlist_container(ReorderBufferTXN, node,
     966             :                                                     iter.cur);
     967             : 
     968             :         /* start LSN must be set */
     969             :         Assert(XLogRecPtrIsValid(cur_txn->first_lsn));
     970             : 
     971             :         /* If there is an end LSN, it must be higher than start LSN */
     972             :         if (XLogRecPtrIsValid(cur_txn->end_lsn))
     973             :             Assert(cur_txn->first_lsn <= cur_txn->end_lsn);
     974             : 
     975             :         /* Current initial LSN must be strictly higher than previous */
     976             :         if (XLogRecPtrIsValid(prev_first_lsn))
     977             :             Assert(prev_first_lsn < cur_txn->first_lsn);
     978             : 
     979             :         /* known-as-subtxn txns must not be listed */
     980             :         Assert(!rbtxn_is_known_subxact(cur_txn));
     981             : 
     982             :         prev_first_lsn = cur_txn->first_lsn;
     983             :     }
     984             : 
     985             :     dlist_foreach(iter, &rb->txns_by_base_snapshot_lsn)
     986             :     {
     987             :         ReorderBufferTXN *cur_txn = dlist_container(ReorderBufferTXN,
     988             :                                                     base_snapshot_node,
     989             :                                                     iter.cur);
     990             : 
     991             :         /* base snapshot (and its LSN) must be set */
     992             :         Assert(cur_txn->base_snapshot != NULL);
     993             :         Assert(XLogRecPtrIsValid(cur_txn->base_snapshot_lsn));
     994             : 
     995             :         /* current LSN must be strictly higher than previous */
     996             :         if (XLogRecPtrIsValid(prev_base_snap_lsn))
     997             :             Assert(prev_base_snap_lsn < cur_txn->base_snapshot_lsn);
     998             : 
     999             :         /* known-as-subtxn txns must not be listed */
    1000             :         Assert(!rbtxn_is_known_subxact(cur_txn));
    1001             : 
    1002             :         prev_base_snap_lsn = cur_txn->base_snapshot_lsn;
    1003             :     }
    1004             : #endif
    1005       16140 : }
    1006             : 
    1007             : /*
    1008             :  * AssertChangeLsnOrder
    1009             :  *
    1010             :  * Check ordering of changes in the (sub)transaction.
    1011             :  */
    1012             : static void
    1013        4948 : AssertChangeLsnOrder(ReorderBufferTXN *txn)
    1014             : {
    1015             : #ifdef USE_ASSERT_CHECKING
    1016             :     dlist_iter  iter;
    1017             :     XLogRecPtr  prev_lsn = txn->first_lsn;
    1018             : 
    1019             :     dlist_foreach(iter, &txn->changes)
    1020             :     {
    1021             :         ReorderBufferChange *cur_change;
    1022             : 
    1023             :         cur_change = dlist_container(ReorderBufferChange, node, iter.cur);
    1024             : 
    1025             :         Assert(XLogRecPtrIsValid(txn->first_lsn));
    1026             :         Assert(XLogRecPtrIsValid(cur_change->lsn));
    1027             :         Assert(txn->first_lsn <= cur_change->lsn);
    1028             : 
    1029             :         if (XLogRecPtrIsValid(txn->end_lsn))
    1030             :             Assert(cur_change->lsn <= txn->end_lsn);
    1031             : 
    1032             :         Assert(prev_lsn <= cur_change->lsn);
    1033             : 
    1034             :         prev_lsn = cur_change->lsn;
    1035             :     }
    1036             : #endif
    1037        4948 : }
    1038             : 
    1039             : /*
    1040             :  * ReorderBufferGetOldestTXN
    1041             :  *      Return oldest transaction in reorderbuffer
    1042             :  */
    1043             : ReorderBufferTXN *
    1044         800 : ReorderBufferGetOldestTXN(ReorderBuffer *rb)
    1045             : {
    1046             :     ReorderBufferTXN *txn;
    1047             : 
    1048         800 :     AssertTXNLsnOrder(rb);
    1049             : 
    1050         800 :     if (dlist_is_empty(&rb->toplevel_by_lsn))
    1051         686 :         return NULL;
    1052             : 
    1053         114 :     txn = dlist_head_element(ReorderBufferTXN, node, &rb->toplevel_by_lsn);
    1054             : 
    1055             :     Assert(!rbtxn_is_known_subxact(txn));
    1056             :     Assert(XLogRecPtrIsValid(txn->first_lsn));
    1057         114 :     return txn;
    1058             : }
    1059             : 
    1060             : /*
    1061             :  * ReorderBufferGetOldestXmin
    1062             :  *      Return oldest Xmin in reorderbuffer
    1063             :  *
    1064             :  * Returns oldest possibly running Xid from the point of view of snapshots
    1065             :  * used in the transactions kept by reorderbuffer, or InvalidTransactionId if
    1066             :  * there are none.
    1067             :  *
    1068             :  * Since snapshots are assigned monotonically, this equals the Xmin of the
    1069             :  * base snapshot with minimal base_snapshot_lsn.
    1070             :  */
    1071             : TransactionId
    1072         836 : ReorderBufferGetOldestXmin(ReorderBuffer *rb)
    1073             : {
    1074             :     ReorderBufferTXN *txn;
    1075             : 
    1076         836 :     AssertTXNLsnOrder(rb);
    1077             : 
    1078         836 :     if (dlist_is_empty(&rb->txns_by_base_snapshot_lsn))
    1079         740 :         return InvalidTransactionId;
    1080             : 
    1081          96 :     txn = dlist_head_element(ReorderBufferTXN, base_snapshot_node,
    1082             :                              &rb->txns_by_base_snapshot_lsn);
    1083          96 :     return txn->base_snapshot->xmin;
    1084             : }
    1085             : 
    1086             : void
    1087         916 : ReorderBufferSetRestartPoint(ReorderBuffer *rb, XLogRecPtr ptr)
    1088             : {
    1089         916 :     rb->current_restart_decoding_lsn = ptr;
    1090         916 : }
    1091             : 
    1092             : /*
    1093             :  * ReorderBufferAssignChild
    1094             :  *
    1095             :  * Make note that we know that subxid is a subtransaction of xid, seen as of
    1096             :  * the given lsn.
    1097             :  */
    1098             : void
    1099        1738 : ReorderBufferAssignChild(ReorderBuffer *rb, TransactionId xid,
    1100             :                          TransactionId subxid, XLogRecPtr lsn)
    1101             : {
    1102             :     ReorderBufferTXN *txn;
    1103             :     ReorderBufferTXN *subtxn;
    1104             :     bool        new_top;
    1105             :     bool        new_sub;
    1106             : 
    1107        1738 :     txn = ReorderBufferTXNByXid(rb, xid, true, &new_top, lsn, true);
    1108        1738 :     subtxn = ReorderBufferTXNByXid(rb, subxid, true, &new_sub, lsn, false);
    1109             : 
    1110        1738 :     if (!new_sub)
    1111             :     {
    1112         372 :         if (rbtxn_is_known_subxact(subtxn))
    1113             :         {
    1114             :             /* already associated, nothing to do */
    1115         372 :             return;
    1116             :         }
    1117             :         else
    1118             :         {
    1119             :             /*
    1120             :              * We already saw this transaction, but initially added it to the
    1121             :              * list of top-level txns.  Now that we know it's not top-level,
    1122             :              * remove it from there.
    1123             :              */
    1124           0 :             dlist_delete(&subtxn->node);
    1125             :         }
    1126             :     }
    1127             : 
    1128        1366 :     subtxn->txn_flags |= RBTXN_IS_SUBXACT;
    1129        1366 :     subtxn->toplevel_xid = xid;
    1130             :     Assert(subtxn->nsubtxns == 0);
    1131             : 
    1132             :     /* set the reference to top-level transaction */
    1133        1366 :     subtxn->toptxn = txn;
    1134             : 
    1135             :     /* add to subtransaction list */
    1136        1366 :     dlist_push_tail(&txn->subtxns, &subtxn->node);
    1137        1366 :     txn->nsubtxns++;
    1138             : 
    1139             :     /* Possibly transfer the subtxn's snapshot to its top-level txn. */
    1140        1366 :     ReorderBufferTransferSnapToParent(txn, subtxn);
    1141             : 
    1142             :     /* Verify LSN-ordering invariant */
    1143        1366 :     AssertTXNLsnOrder(rb);
    1144             : }
    1145             : 
    1146             : /*
    1147             :  * ReorderBufferTransferSnapToParent
    1148             :  *      Transfer base snapshot from subtxn to top-level txn, if needed
    1149             :  *
    1150             :  * This is done if the top-level txn doesn't have a base snapshot, or if the
    1151             :  * subtxn's base snapshot has an earlier LSN than the top-level txn's base
    1152             :  * snapshot's LSN.  This can happen if there are no changes in the toplevel
    1153             :  * txn but there are some in the subtxn, or the first change in subtxn has
    1154             :  * earlier LSN than first change in the top-level txn and we learned about
    1155             :  * their kinship only now.
    1156             :  *
    1157             :  * The subtransaction's snapshot is cleared regardless of the transfer
    1158             :  * happening, since it's not needed anymore in either case.
    1159             :  *
    1160             :  * We do this as soon as we become aware of their kinship, to avoid queueing
    1161             :  * extra snapshots to txns known-as-subtxns -- only top-level txns will
    1162             :  * receive further snapshots.
    1163             :  */
    1164             : static void
    1165        1374 : ReorderBufferTransferSnapToParent(ReorderBufferTXN *txn,
    1166             :                                   ReorderBufferTXN *subtxn)
    1167             : {
    1168             :     Assert(subtxn->toplevel_xid == txn->xid);
    1169             : 
    1170        1374 :     if (subtxn->base_snapshot != NULL)
    1171             :     {
    1172           0 :         if (txn->base_snapshot == NULL ||
    1173           0 :             subtxn->base_snapshot_lsn < txn->base_snapshot_lsn)
    1174             :         {
    1175             :             /*
    1176             :              * If the toplevel transaction already has a base snapshot but
    1177             :              * it's newer than the subxact's, purge it.
    1178             :              */
    1179           0 :             if (txn->base_snapshot != NULL)
    1180             :             {
    1181           0 :                 SnapBuildSnapDecRefcount(txn->base_snapshot);
    1182           0 :                 dlist_delete(&txn->base_snapshot_node);
    1183             :             }
    1184             : 
    1185             :             /*
    1186             :              * The snapshot is now the top transaction's; transfer it, and
    1187             :              * adjust the list position of the top transaction in the list by
    1188             :              * moving it to where the subtransaction is.
    1189             :              */
    1190           0 :             txn->base_snapshot = subtxn->base_snapshot;
    1191           0 :             txn->base_snapshot_lsn = subtxn->base_snapshot_lsn;
    1192           0 :             dlist_insert_before(&subtxn->base_snapshot_node,
    1193             :                                 &txn->base_snapshot_node);
    1194             : 
    1195             :             /*
    1196             :              * The subtransaction doesn't have a snapshot anymore (so it
    1197             :              * mustn't be in the list.)
    1198             :              */
    1199           0 :             subtxn->base_snapshot = NULL;
    1200           0 :             subtxn->base_snapshot_lsn = InvalidXLogRecPtr;
    1201           0 :             dlist_delete(&subtxn->base_snapshot_node);
    1202             :         }
    1203             :         else
    1204             :         {
    1205             :             /* Base snap of toplevel is fine, so subxact's is not needed */
    1206           0 :             SnapBuildSnapDecRefcount(subtxn->base_snapshot);
    1207           0 :             dlist_delete(&subtxn->base_snapshot_node);
    1208           0 :             subtxn->base_snapshot = NULL;
    1209           0 :             subtxn->base_snapshot_lsn = InvalidXLogRecPtr;
    1210             :         }
    1211             :     }
    1212        1374 : }
    1213             : 
    1214             : /*
    1215             :  * Associate a subtransaction with its toplevel transaction at commit
    1216             :  * time. There may be no further changes added after this.
    1217             :  */
    1218             : void
    1219         534 : ReorderBufferCommitChild(ReorderBuffer *rb, TransactionId xid,
    1220             :                          TransactionId subxid, XLogRecPtr commit_lsn,
    1221             :                          XLogRecPtr end_lsn)
    1222             : {
    1223             :     ReorderBufferTXN *subtxn;
    1224             : 
    1225         534 :     subtxn = ReorderBufferTXNByXid(rb, subxid, false, NULL,
    1226             :                                    InvalidXLogRecPtr, false);
    1227             : 
    1228             :     /*
    1229             :      * No need to do anything if that subtxn didn't contain any changes
    1230             :      */
    1231         534 :     if (!subtxn)
    1232         162 :         return;
    1233             : 
    1234         372 :     subtxn->final_lsn = commit_lsn;
    1235         372 :     subtxn->end_lsn = end_lsn;
    1236             : 
    1237             :     /*
    1238             :      * Assign this subxact as a child of the toplevel xact (no-op if already
    1239             :      * done.)
    1240             :      */
    1241         372 :     ReorderBufferAssignChild(rb, xid, subxid, InvalidXLogRecPtr);
    1242             : }
    1243             : 
    1244             : 
    1245             : /*
    1246             :  * Support for efficiently iterating over a transaction's and its
    1247             :  * subtransactions' changes.
    1248             :  *
    1249             :  * We do by doing a k-way merge between transactions/subtransactions. For that
    1250             :  * we model the current heads of the different transactions as a binary heap
    1251             :  * so we easily know which (sub-)transaction has the change with the smallest
    1252             :  * lsn next.
    1253             :  *
    1254             :  * We assume the changes in individual transactions are already sorted by LSN.
    1255             :  */
    1256             : 
    1257             : /*
    1258             :  * Binary heap comparison function.
    1259             :  */
    1260             : static int
    1261      103136 : ReorderBufferIterCompare(Datum a, Datum b, void *arg)
    1262             : {
    1263      103136 :     ReorderBufferIterTXNState *state = (ReorderBufferIterTXNState *) arg;
    1264      103136 :     XLogRecPtr  pos_a = state->entries[DatumGetInt32(a)].lsn;
    1265      103136 :     XLogRecPtr  pos_b = state->entries[DatumGetInt32(b)].lsn;
    1266             : 
    1267      103136 :     if (pos_a < pos_b)
    1268      101424 :         return 1;
    1269        1712 :     else if (pos_a == pos_b)
    1270           0 :         return 0;
    1271        1712 :     return -1;
    1272             : }
    1273             : 
    1274             : /*
    1275             :  * Allocate & initialize an iterator which iterates in lsn order over a
    1276             :  * transaction and all its subtransactions.
    1277             :  *
    1278             :  * Note: The iterator state is returned through iter_state parameter rather
    1279             :  * than the function's return value.  This is because the state gets cleaned up
    1280             :  * in a PG_CATCH block in the caller, so we want to make sure the caller gets
    1281             :  * back the state even if this function throws an exception.
    1282             :  */
    1283             : static void
    1284        4022 : ReorderBufferIterTXNInit(ReorderBuffer *rb, ReorderBufferTXN *txn,
    1285             :                          ReorderBufferIterTXNState *volatile *iter_state)
    1286             : {
    1287        4022 :     Size        nr_txns = 0;
    1288             :     ReorderBufferIterTXNState *state;
    1289             :     dlist_iter  cur_txn_i;
    1290             :     int32       off;
    1291             : 
    1292        4022 :     *iter_state = NULL;
    1293             : 
    1294             :     /* Check ordering of changes in the toplevel transaction. */
    1295        4022 :     AssertChangeLsnOrder(txn);
    1296             : 
    1297             :     /*
    1298             :      * Calculate the size of our heap: one element for every transaction that
    1299             :      * contains changes.  (Besides the transactions already in the reorder
    1300             :      * buffer, we count the one we were directly passed.)
    1301             :      */
    1302        4022 :     if (txn->nentries > 0)
    1303        3660 :         nr_txns++;
    1304             : 
    1305        4948 :     dlist_foreach(cur_txn_i, &txn->subtxns)
    1306             :     {
    1307             :         ReorderBufferTXN *cur_txn;
    1308             : 
    1309         926 :         cur_txn = dlist_container(ReorderBufferTXN, node, cur_txn_i.cur);
    1310             : 
    1311             :         /* Check ordering of changes in this subtransaction. */
    1312         926 :         AssertChangeLsnOrder(cur_txn);
    1313             : 
    1314         926 :         if (cur_txn->nentries > 0)
    1315         602 :             nr_txns++;
    1316             :     }
    1317             : 
    1318             :     /* allocate iteration state */
    1319             :     state = (ReorderBufferIterTXNState *)
    1320        4022 :         MemoryContextAllocZero(rb->context,
    1321             :                                sizeof(ReorderBufferIterTXNState) +
    1322        4022 :                                sizeof(ReorderBufferIterTXNEntry) * nr_txns);
    1323             : 
    1324        4022 :     state->nr_txns = nr_txns;
    1325        4022 :     dlist_init(&state->old_change);
    1326             : 
    1327        8284 :     for (off = 0; off < state->nr_txns; off++)
    1328             :     {
    1329        4262 :         state->entries[off].file.vfd = -1;
    1330        4262 :         state->entries[off].segno = 0;
    1331             :     }
    1332             : 
    1333             :     /* allocate heap */
    1334        4022 :     state->heap = binaryheap_allocate(state->nr_txns,
    1335             :                                       ReorderBufferIterCompare,
    1336             :                                       state);
    1337             : 
    1338             :     /* Now that the state fields are initialized, it is safe to return it. */
    1339        4022 :     *iter_state = state;
    1340             : 
    1341             :     /*
    1342             :      * Now insert items into the binary heap, in an unordered fashion.  (We
    1343             :      * will run a heap assembly step at the end; this is more efficient.)
    1344             :      */
    1345             : 
    1346        4022 :     off = 0;
    1347             : 
    1348             :     /* add toplevel transaction if it contains changes */
    1349        4022 :     if (txn->nentries > 0)
    1350             :     {
    1351             :         ReorderBufferChange *cur_change;
    1352             : 
    1353        3660 :         if (rbtxn_is_serialized(txn))
    1354             :         {
    1355             :             /* serialize remaining changes */
    1356          46 :             ReorderBufferSerializeTXN(rb, txn);
    1357          46 :             ReorderBufferRestoreChanges(rb, txn, &state->entries[off].file,
    1358             :                                         &state->entries[off].segno);
    1359             :         }
    1360             : 
    1361        3660 :         cur_change = dlist_head_element(ReorderBufferChange, node,
    1362             :                                         &txn->changes);
    1363             : 
    1364        3660 :         state->entries[off].lsn = cur_change->lsn;
    1365        3660 :         state->entries[off].change = cur_change;
    1366        3660 :         state->entries[off].txn = txn;
    1367             : 
    1368        3660 :         binaryheap_add_unordered(state->heap, Int32GetDatum(off++));
    1369             :     }
    1370             : 
    1371             :     /* add subtransactions if they contain changes */
    1372        4948 :     dlist_foreach(cur_txn_i, &txn->subtxns)
    1373             :     {
    1374             :         ReorderBufferTXN *cur_txn;
    1375             : 
    1376         926 :         cur_txn = dlist_container(ReorderBufferTXN, node, cur_txn_i.cur);
    1377             : 
    1378         926 :         if (cur_txn->nentries > 0)
    1379             :         {
    1380             :             ReorderBufferChange *cur_change;
    1381             : 
    1382         602 :             if (rbtxn_is_serialized(cur_txn))
    1383             :             {
    1384             :                 /* serialize remaining changes */
    1385          34 :                 ReorderBufferSerializeTXN(rb, cur_txn);
    1386          34 :                 ReorderBufferRestoreChanges(rb, cur_txn,
    1387             :                                             &state->entries[off].file,
    1388             :                                             &state->entries[off].segno);
    1389             :             }
    1390         602 :             cur_change = dlist_head_element(ReorderBufferChange, node,
    1391             :                                             &cur_txn->changes);
    1392             : 
    1393         602 :             state->entries[off].lsn = cur_change->lsn;
    1394         602 :             state->entries[off].change = cur_change;
    1395         602 :             state->entries[off].txn = cur_txn;
    1396             : 
    1397         602 :             binaryheap_add_unordered(state->heap, Int32GetDatum(off++));
    1398             :         }
    1399             :     }
    1400             : 
    1401             :     /* assemble a valid binary heap */
    1402        4022 :     binaryheap_build(state->heap);
    1403        4022 : }
    1404             : 
    1405             : /*
    1406             :  * Return the next change when iterating over a transaction and its
    1407             :  * subtransactions.
    1408             :  *
    1409             :  * Returns NULL when no further changes exist.
    1410             :  */
    1411             : static ReorderBufferChange *
    1412      716600 : ReorderBufferIterTXNNext(ReorderBuffer *rb, ReorderBufferIterTXNState *state)
    1413             : {
    1414             :     ReorderBufferChange *change;
    1415             :     ReorderBufferIterTXNEntry *entry;
    1416             :     int32       off;
    1417             : 
    1418             :     /* nothing there anymore */
    1419      716600 :     if (binaryheap_empty(state->heap))
    1420        4002 :         return NULL;
    1421             : 
    1422      712598 :     off = DatumGetInt32(binaryheap_first(state->heap));
    1423      712598 :     entry = &state->entries[off];
    1424             : 
    1425             :     /* free memory we might have "leaked" in the previous *Next call */
    1426      712598 :     if (!dlist_is_empty(&state->old_change))
    1427             :     {
    1428          90 :         change = dlist_container(ReorderBufferChange, node,
    1429             :                                  dlist_pop_head_node(&state->old_change));
    1430          90 :         ReorderBufferFreeChange(rb, change, true);
    1431             :         Assert(dlist_is_empty(&state->old_change));
    1432             :     }
    1433             : 
    1434      712598 :     change = entry->change;
    1435             : 
    1436             :     /*
    1437             :      * update heap with information about which transaction has the next
    1438             :      * relevant change in LSN order
    1439             :      */
    1440             : 
    1441             :     /* there are in-memory changes */
    1442      712598 :     if (dlist_has_next(&entry->txn->changes, &entry->change->node))
    1443             :     {
    1444      708270 :         dlist_node *next = dlist_next_node(&entry->txn->changes, &change->node);
    1445      708270 :         ReorderBufferChange *next_change =
    1446             :             dlist_container(ReorderBufferChange, node, next);
    1447             : 
    1448             :         /* txn stays the same */
    1449      708270 :         state->entries[off].lsn = next_change->lsn;
    1450      708270 :         state->entries[off].change = next_change;
    1451             : 
    1452      708270 :         binaryheap_replace_first(state->heap, Int32GetDatum(off));
    1453      708270 :         return change;
    1454             :     }
    1455             : 
    1456             :     /* try to load changes from disk */
    1457        4328 :     if (entry->txn->nentries != entry->txn->nentries_mem)
    1458             :     {
    1459             :         /*
    1460             :          * Ugly: restoring changes will reuse *Change records, thus delete the
    1461             :          * current one from the per-tx list and only free in the next call.
    1462             :          */
    1463         130 :         dlist_delete(&change->node);
    1464         130 :         dlist_push_tail(&state->old_change, &change->node);
    1465             : 
    1466             :         /*
    1467             :          * Update the total bytes processed by the txn for which we are
    1468             :          * releasing the current set of changes and restoring the new set of
    1469             :          * changes.
    1470             :          */
    1471         130 :         rb->totalBytes += entry->txn->size;
    1472         130 :         if (ReorderBufferRestoreChanges(rb, entry->txn, &entry->file,
    1473             :                                         &state->entries[off].segno))
    1474             :         {
    1475             :             /* successfully restored changes from disk */
    1476             :             ReorderBufferChange *next_change =
    1477          72 :                 dlist_head_element(ReorderBufferChange, node,
    1478             :                                    &entry->txn->changes);
    1479             : 
    1480          72 :             elog(DEBUG2, "restored %u/%u changes from disk",
    1481             :                  (uint32) entry->txn->nentries_mem,
    1482             :                  (uint32) entry->txn->nentries);
    1483             : 
    1484             :             Assert(entry->txn->nentries_mem);
    1485             :             /* txn stays the same */
    1486          72 :             state->entries[off].lsn = next_change->lsn;
    1487          72 :             state->entries[off].change = next_change;
    1488          72 :             binaryheap_replace_first(state->heap, Int32GetDatum(off));
    1489             : 
    1490          72 :             return change;
    1491             :         }
    1492             :     }
    1493             : 
    1494             :     /* ok, no changes there anymore, remove */
    1495        4256 :     binaryheap_remove_first(state->heap);
    1496             : 
    1497        4256 :     return change;
    1498             : }
    1499             : 
    1500             : /*
    1501             :  * Deallocate the iterator
    1502             :  */
    1503             : static void
    1504        4020 : ReorderBufferIterTXNFinish(ReorderBuffer *rb,
    1505             :                            ReorderBufferIterTXNState *state)
    1506             : {
    1507             :     int32       off;
    1508             : 
    1509        8280 :     for (off = 0; off < state->nr_txns; off++)
    1510             :     {
    1511        4260 :         if (state->entries[off].file.vfd != -1)
    1512           0 :             FileClose(state->entries[off].file.vfd);
    1513             :     }
    1514             : 
    1515             :     /* free memory we might have "leaked" in the last *Next call */
    1516        4020 :     if (!dlist_is_empty(&state->old_change))
    1517             :     {
    1518             :         ReorderBufferChange *change;
    1519             : 
    1520          38 :         change = dlist_container(ReorderBufferChange, node,
    1521             :                                  dlist_pop_head_node(&state->old_change));
    1522          38 :         ReorderBufferFreeChange(rb, change, true);
    1523             :         Assert(dlist_is_empty(&state->old_change));
    1524             :     }
    1525             : 
    1526        4020 :     binaryheap_free(state->heap);
    1527        4020 :     pfree(state);
    1528        4020 : }
    1529             : 
    1530             : /*
    1531             :  * Cleanup the contents of a transaction, usually after the transaction
    1532             :  * committed or aborted.
    1533             :  */
    1534             : static void
    1535        7782 : ReorderBufferCleanupTXN(ReorderBuffer *rb, ReorderBufferTXN *txn)
    1536             : {
    1537             :     bool        found;
    1538             :     dlist_mutable_iter iter;
    1539        7782 :     Size        mem_freed = 0;
    1540             : 
    1541             :     /* cleanup subtransactions & their changes */
    1542        8152 :     dlist_foreach_modify(iter, &txn->subtxns)
    1543             :     {
    1544             :         ReorderBufferTXN *subtxn;
    1545             : 
    1546         370 :         subtxn = dlist_container(ReorderBufferTXN, node, iter.cur);
    1547             : 
    1548             :         /*
    1549             :          * Subtransactions are always associated to the toplevel TXN, even if
    1550             :          * they originally were happening inside another subtxn, so we won't
    1551             :          * ever recurse more than one level deep here.
    1552             :          */
    1553             :         Assert(rbtxn_is_known_subxact(subtxn));
    1554             :         Assert(subtxn->nsubtxns == 0);
    1555             : 
    1556         370 :         ReorderBufferCleanupTXN(rb, subtxn);
    1557             :     }
    1558             : 
    1559             :     /* cleanup changes in the txn */
    1560      150330 :     dlist_foreach_modify(iter, &txn->changes)
    1561             :     {
    1562             :         ReorderBufferChange *change;
    1563             : 
    1564      142548 :         change = dlist_container(ReorderBufferChange, node, iter.cur);
    1565             : 
    1566             :         /* Check we're not mixing changes from different transactions. */
    1567             :         Assert(change->txn == txn);
    1568             : 
    1569             :         /*
    1570             :          * Instead of updating the memory counter for individual changes, we
    1571             :          * sum up the size of memory to free so we can update the memory
    1572             :          * counter all together below. This saves costs of maintaining the
    1573             :          * max-heap.
    1574             :          */
    1575      142548 :         mem_freed += ReorderBufferChangeSize(change);
    1576             : 
    1577      142548 :         ReorderBufferFreeChange(rb, change, false);
    1578             :     }
    1579             : 
    1580             :     /* Update the memory counter */
    1581        7782 :     ReorderBufferChangeMemoryUpdate(rb, NULL, txn, false, mem_freed);
    1582             : 
    1583             :     /*
    1584             :      * Cleanup the tuplecids we stored for decoding catalog snapshot access.
    1585             :      * They are always stored in the toplevel transaction.
    1586             :      */
    1587       53872 :     dlist_foreach_modify(iter, &txn->tuplecids)
    1588             :     {
    1589             :         ReorderBufferChange *change;
    1590             : 
    1591       46090 :         change = dlist_container(ReorderBufferChange, node, iter.cur);
    1592             : 
    1593             :         /* Check we're not mixing changes from different transactions. */
    1594             :         Assert(change->txn == txn);
    1595             :         Assert(change->action == REORDER_BUFFER_CHANGE_INTERNAL_TUPLECID);
    1596             : 
    1597       46090 :         ReorderBufferFreeChange(rb, change, true);
    1598             :     }
    1599             : 
    1600             :     /*
    1601             :      * Cleanup the base snapshot, if set.
    1602             :      */
    1603        7782 :     if (txn->base_snapshot != NULL)
    1604             :     {
    1605        6386 :         SnapBuildSnapDecRefcount(txn->base_snapshot);
    1606        6386 :         dlist_delete(&txn->base_snapshot_node);
    1607             :     }
    1608             : 
    1609             :     /*
    1610             :      * Cleanup the snapshot for the last streamed run.
    1611             :      */
    1612        7782 :     if (txn->snapshot_now != NULL)
    1613             :     {
    1614             :         Assert(rbtxn_is_streamed(txn));
    1615         130 :         ReorderBufferFreeSnap(rb, txn->snapshot_now);
    1616             :     }
    1617             : 
    1618             :     /*
    1619             :      * Remove TXN from its containing lists.
    1620             :      *
    1621             :      * Note: if txn is known as subxact, we are deleting the TXN from its
    1622             :      * parent's list of known subxacts; this leaves the parent's nsubxacts
    1623             :      * count too high, but we don't care.  Otherwise, we are deleting the TXN
    1624             :      * from the LSN-ordered list of toplevel TXNs. We remove the TXN from the
    1625             :      * list of catalog modifying transactions as well.
    1626             :      */
    1627        7782 :     dlist_delete(&txn->node);
    1628        7782 :     if (rbtxn_has_catalog_changes(txn))
    1629        2412 :         dclist_delete_from(&rb->catchange_txns, &txn->catchange_node);
    1630             : 
    1631             :     /* now remove reference from buffer */
    1632        7782 :     hash_search(rb->by_txn, &txn->xid, HASH_REMOVE, &found);
    1633             :     Assert(found);
    1634             : 
    1635             :     /* remove entries spilled to disk */
    1636        7782 :     if (rbtxn_is_serialized(txn))
    1637         654 :         ReorderBufferRestoreCleanup(rb, txn);
    1638             : 
    1639             :     /* deallocate */
    1640        7782 :     ReorderBufferFreeTXN(rb, txn);
    1641        7782 : }
    1642             : 
    1643             : /*
    1644             :  * Discard changes from a transaction (and subtransactions), either after
    1645             :  * streaming, decoding them at PREPARE, or detecting the transaction abort.
    1646             :  * Keep the remaining info - transactions, tuplecids, invalidations and
    1647             :  * snapshots.
    1648             :  *
    1649             :  * We additionally remove tuplecids after decoding the transaction at prepare
    1650             :  * time as we only need to perform invalidation at rollback or commit prepared.
    1651             :  *
    1652             :  * 'txn_prepared' indicates that we have decoded the transaction at prepare
    1653             :  * time.
    1654             :  */
    1655             : static void
    1656        2086 : ReorderBufferTruncateTXN(ReorderBuffer *rb, ReorderBufferTXN *txn, bool txn_prepared)
    1657             : {
    1658             :     dlist_mutable_iter iter;
    1659        2086 :     Size        mem_freed = 0;
    1660             : 
    1661             :     /* cleanup subtransactions & their changes */
    1662        2680 :     dlist_foreach_modify(iter, &txn->subtxns)
    1663             :     {
    1664             :         ReorderBufferTXN *subtxn;
    1665             : 
    1666         594 :         subtxn = dlist_container(ReorderBufferTXN, node, iter.cur);
    1667             : 
    1668             :         /*
    1669             :          * Subtransactions are always associated to the toplevel TXN, even if
    1670             :          * they originally were happening inside another subtxn, so we won't
    1671             :          * ever recurse more than one level deep here.
    1672             :          */
    1673             :         Assert(rbtxn_is_known_subxact(subtxn));
    1674             :         Assert(subtxn->nsubtxns == 0);
    1675             : 
    1676         594 :         ReorderBufferMaybeMarkTXNStreamed(rb, subtxn);
    1677         594 :         ReorderBufferTruncateTXN(rb, subtxn, txn_prepared);
    1678             :     }
    1679             : 
    1680             :     /* cleanup changes in the txn */
    1681      317532 :     dlist_foreach_modify(iter, &txn->changes)
    1682             :     {
    1683             :         ReorderBufferChange *change;
    1684             : 
    1685      315446 :         change = dlist_container(ReorderBufferChange, node, iter.cur);
    1686             : 
    1687             :         /* Check we're not mixing changes from different transactions. */
    1688             :         Assert(change->txn == txn);
    1689             : 
    1690             :         /* remove the change from its containing list */
    1691      315446 :         dlist_delete(&change->node);
    1692             : 
    1693             :         /*
    1694             :          * Instead of updating the memory counter for individual changes, we
    1695             :          * sum up the size of memory to free so we can update the memory
    1696             :          * counter all together below. This saves costs of maintaining the
    1697             :          * max-heap.
    1698             :          */
    1699      315446 :         mem_freed += ReorderBufferChangeSize(change);
    1700             : 
    1701      315446 :         ReorderBufferFreeChange(rb, change, false);
    1702             :     }
    1703             : 
    1704             :     /* Update the memory counter */
    1705        2086 :     ReorderBufferChangeMemoryUpdate(rb, NULL, txn, false, mem_freed);
    1706             : 
    1707        2086 :     if (txn_prepared)
    1708             :     {
    1709             :         /*
    1710             :          * If this is a prepared txn, cleanup the tuplecids we stored for
    1711             :          * decoding catalog snapshot access. They are always stored in the
    1712             :          * toplevel transaction.
    1713             :          */
    1714         360 :         dlist_foreach_modify(iter, &txn->tuplecids)
    1715             :         {
    1716             :             ReorderBufferChange *change;
    1717             : 
    1718         246 :             change = dlist_container(ReorderBufferChange, node, iter.cur);
    1719             : 
    1720             :             /* Check we're not mixing changes from different transactions. */
    1721             :             Assert(change->txn == txn);
    1722             :             Assert(change->action == REORDER_BUFFER_CHANGE_INTERNAL_TUPLECID);
    1723             : 
    1724             :             /* Remove the change from its containing list. */
    1725         246 :             dlist_delete(&change->node);
    1726             : 
    1727         246 :             ReorderBufferFreeChange(rb, change, true);
    1728             :         }
    1729             :     }
    1730             : 
    1731             :     /*
    1732             :      * Destroy the (relfilelocator, ctid) hashtable, so that we don't leak any
    1733             :      * memory. We could also keep the hash table and update it with new ctid
    1734             :      * values, but this seems simpler and good enough for now.
    1735             :      */
    1736        2086 :     if (txn->tuplecid_hash != NULL)
    1737             :     {
    1738         102 :         hash_destroy(txn->tuplecid_hash);
    1739         102 :         txn->tuplecid_hash = NULL;
    1740             :     }
    1741             : 
    1742             :     /* If this txn is serialized then clean the disk space. */
    1743        2086 :     if (rbtxn_is_serialized(txn))
    1744             :     {
    1745          18 :         ReorderBufferRestoreCleanup(rb, txn);
    1746          18 :         txn->txn_flags &= ~RBTXN_IS_SERIALIZED;
    1747             : 
    1748             :         /*
    1749             :          * We set this flag to indicate if the transaction is ever serialized.
    1750             :          * We need this to accurately update the stats as otherwise the same
    1751             :          * transaction can be counted as serialized multiple times.
    1752             :          */
    1753          18 :         txn->txn_flags |= RBTXN_IS_SERIALIZED_CLEAR;
    1754             :     }
    1755             : 
    1756             :     /* also reset the number of entries in the transaction */
    1757        2086 :     txn->nentries_mem = 0;
    1758        2086 :     txn->nentries = 0;
    1759        2086 : }
    1760             : 
    1761             : /*
    1762             :  * Check the transaction status by CLOG lookup and discard all changes if
    1763             :  * the transaction is aborted. The transaction status is cached in
    1764             :  * txn->txn_flags so we can skip future changes and avoid CLOG lookups on the
    1765             :  * next call.
    1766             :  *
    1767             :  * Return true if the transaction is aborted, otherwise return false.
    1768             :  *
    1769             :  * When the 'debug_logical_replication_streaming' is set to "immediate", we
    1770             :  * don't check the transaction status, meaning the caller will always process
    1771             :  * this transaction.
    1772             :  */
    1773             : static bool
    1774        9222 : ReorderBufferCheckAndTruncateAbortedTXN(ReorderBuffer *rb, ReorderBufferTXN *txn)
    1775             : {
    1776             :     /* Quick return for regression tests */
    1777        9222 :     if (unlikely(debug_logical_replication_streaming == DEBUG_LOGICAL_REP_STREAMING_IMMEDIATE))
    1778        2212 :         return false;
    1779             : 
    1780             :     /*
    1781             :      * Quick return if the transaction status is already known.
    1782             :      */
    1783             : 
    1784        7010 :     if (rbtxn_is_committed(txn))
    1785        6042 :         return false;
    1786         968 :     if (rbtxn_is_aborted(txn))
    1787             :     {
    1788             :         /* Already-aborted transactions should not have any changes */
    1789             :         Assert(txn->size == 0);
    1790             : 
    1791           0 :         return true;
    1792             :     }
    1793             : 
    1794             :     /* Otherwise, check the transaction status using CLOG lookup */
    1795             : 
    1796         968 :     if (TransactionIdIsInProgress(txn->xid))
    1797         476 :         return false;
    1798             : 
    1799         492 :     if (TransactionIdDidCommit(txn->xid))
    1800             :     {
    1801             :         /*
    1802             :          * Remember the transaction is committed so that we can skip CLOG
    1803             :          * check next time, avoiding the pressure on CLOG lookup.
    1804             :          */
    1805             :         Assert(!rbtxn_is_aborted(txn));
    1806         474 :         txn->txn_flags |= RBTXN_IS_COMMITTED;
    1807         474 :         return false;
    1808             :     }
    1809             : 
    1810             :     /*
    1811             :      * The transaction aborted. We discard both the changes collected so far
    1812             :      * and the toast reconstruction data. The full cleanup will happen as part
    1813             :      * of decoding ABORT record of this transaction.
    1814             :      */
    1815          18 :     ReorderBufferTruncateTXN(rb, txn, rbtxn_is_prepared(txn));
    1816          18 :     ReorderBufferToastReset(rb, txn);
    1817             : 
    1818             :     /* All changes should be discarded */
    1819             :     Assert(txn->size == 0);
    1820             : 
    1821             :     /*
    1822             :      * Mark the transaction as aborted so we can ignore future changes of this
    1823             :      * transaction.
    1824             :      */
    1825             :     Assert(!rbtxn_is_committed(txn));
    1826          18 :     txn->txn_flags |= RBTXN_IS_ABORTED;
    1827             : 
    1828          18 :     return true;
    1829             : }
    1830             : 
    1831             : /*
    1832             :  * Build a hash with a (relfilelocator, ctid) -> (cmin, cmax) mapping for use by
    1833             :  * HeapTupleSatisfiesHistoricMVCC.
    1834             :  */
    1835             : static void
    1836        4022 : ReorderBufferBuildTupleCidHash(ReorderBuffer *rb, ReorderBufferTXN *txn)
    1837             : {
    1838             :     dlist_iter  iter;
    1839             :     HASHCTL     hash_ctl;
    1840             : 
    1841        4022 :     if (!rbtxn_has_catalog_changes(txn) || dlist_is_empty(&txn->tuplecids))
    1842        2808 :         return;
    1843             : 
    1844        1214 :     hash_ctl.keysize = sizeof(ReorderBufferTupleCidKey);
    1845        1214 :     hash_ctl.entrysize = sizeof(ReorderBufferTupleCidEnt);
    1846        1214 :     hash_ctl.hcxt = rb->context;
    1847             : 
    1848             :     /*
    1849             :      * create the hash with the exact number of to-be-stored tuplecids from
    1850             :      * the start
    1851             :      */
    1852        1214 :     txn->tuplecid_hash =
    1853        1214 :         hash_create("ReorderBufferTupleCid", txn->ntuplecids, &hash_ctl,
    1854             :                     HASH_ELEM | HASH_BLOBS | HASH_CONTEXT);
    1855             : 
    1856       24964 :     dlist_foreach(iter, &txn->tuplecids)
    1857             :     {
    1858             :         ReorderBufferTupleCidKey key;
    1859             :         ReorderBufferTupleCidEnt *ent;
    1860             :         bool        found;
    1861             :         ReorderBufferChange *change;
    1862             : 
    1863       23750 :         change = dlist_container(ReorderBufferChange, node, iter.cur);
    1864             : 
    1865             :         Assert(change->action == REORDER_BUFFER_CHANGE_INTERNAL_TUPLECID);
    1866             : 
    1867             :         /* be careful about padding */
    1868       23750 :         memset(&key, 0, sizeof(ReorderBufferTupleCidKey));
    1869             : 
    1870       23750 :         key.rlocator = change->data.tuplecid.locator;
    1871             : 
    1872       23750 :         ItemPointerCopy(&change->data.tuplecid.tid,
    1873             :                         &key.tid);
    1874             : 
    1875             :         ent = (ReorderBufferTupleCidEnt *)
    1876       23750 :             hash_search(txn->tuplecid_hash, &key, HASH_ENTER, &found);
    1877       23750 :         if (!found)
    1878             :         {
    1879       20466 :             ent->cmin = change->data.tuplecid.cmin;
    1880       20466 :             ent->cmax = change->data.tuplecid.cmax;
    1881       20466 :             ent->combocid = change->data.tuplecid.combocid;
    1882             :         }
    1883             :         else
    1884             :         {
    1885             :             /*
    1886             :              * Maybe we already saw this tuple before in this transaction, but
    1887             :              * if so it must have the same cmin.
    1888             :              */
    1889             :             Assert(ent->cmin == change->data.tuplecid.cmin);
    1890             : 
    1891             :             /*
    1892             :              * cmax may be initially invalid, but once set it can only grow,
    1893             :              * and never become invalid again.
    1894             :              */
    1895             :             Assert((ent->cmax == InvalidCommandId) ||
    1896             :                    ((change->data.tuplecid.cmax != InvalidCommandId) &&
    1897             :                     (change->data.tuplecid.cmax > ent->cmax)));
    1898        3284 :             ent->cmax = change->data.tuplecid.cmax;
    1899             :         }
    1900             :     }
    1901             : }
    1902             : 
    1903             : /*
    1904             :  * Copy a provided snapshot so we can modify it privately. This is needed so
    1905             :  * that catalog modifying transactions can look into intermediate catalog
    1906             :  * states.
    1907             :  */
    1908             : static Snapshot
    1909        3634 : ReorderBufferCopySnap(ReorderBuffer *rb, Snapshot orig_snap,
    1910             :                       ReorderBufferTXN *txn, CommandId cid)
    1911             : {
    1912             :     Snapshot    snap;
    1913             :     dlist_iter  iter;
    1914        3634 :     int         i = 0;
    1915             :     Size        size;
    1916             : 
    1917        3634 :     size = sizeof(SnapshotData) +
    1918        3634 :         sizeof(TransactionId) * orig_snap->xcnt +
    1919        3634 :         sizeof(TransactionId) * (txn->nsubtxns + 1);
    1920             : 
    1921        3634 :     snap = MemoryContextAllocZero(rb->context, size);
    1922        3634 :     memcpy(snap, orig_snap, sizeof(SnapshotData));
    1923             : 
    1924        3634 :     snap->copied = true;
    1925        3634 :     snap->active_count = 1;      /* mark as active so nobody frees it */
    1926        3634 :     snap->regd_count = 0;
    1927        3634 :     snap->xip = (TransactionId *) (snap + 1);
    1928             : 
    1929        3634 :     memcpy(snap->xip, orig_snap->xip, sizeof(TransactionId) * snap->xcnt);
    1930             : 
    1931             :     /*
    1932             :      * snap->subxip contains all txids that belong to our transaction which we
    1933             :      * need to check via cmin/cmax. That's why we store the toplevel
    1934             :      * transaction in there as well.
    1935             :      */
    1936        3634 :     snap->subxip = snap->xip + snap->xcnt;
    1937        3634 :     snap->subxip[i++] = txn->xid;
    1938             : 
    1939             :     /*
    1940             :      * txn->nsubtxns isn't decreased when subtransactions abort, so count
    1941             :      * manually. Since it's an upper boundary it is safe to use it for the
    1942             :      * allocation above.
    1943             :      */
    1944        3634 :     snap->subxcnt = 1;
    1945             : 
    1946        4252 :     dlist_foreach(iter, &txn->subtxns)
    1947             :     {
    1948             :         ReorderBufferTXN *sub_txn;
    1949             : 
    1950         618 :         sub_txn = dlist_container(ReorderBufferTXN, node, iter.cur);
    1951         618 :         snap->subxip[i++] = sub_txn->xid;
    1952         618 :         snap->subxcnt++;
    1953             :     }
    1954             : 
    1955             :     /* sort so we can bsearch() later */
    1956        3634 :     qsort(snap->subxip, snap->subxcnt, sizeof(TransactionId), xidComparator);
    1957             : 
    1958             :     /* store the specified current CommandId */
    1959        3634 :     snap->curcid = cid;
    1960             : 
    1961        3634 :     return snap;
    1962             : }
    1963             : 
    1964             : /*
    1965             :  * Free a previously ReorderBufferCopySnap'ed snapshot
    1966             :  */
    1967             : static void
    1968        5976 : ReorderBufferFreeSnap(ReorderBuffer *rb, Snapshot snap)
    1969             : {
    1970        5976 :     if (snap->copied)
    1971        3628 :         pfree(snap);
    1972             :     else
    1973        2348 :         SnapBuildSnapDecRefcount(snap);
    1974        5976 : }
    1975             : 
    1976             : /*
    1977             :  * If the transaction was (partially) streamed, we need to prepare or commit
    1978             :  * it in a 'streamed' way.  That is, we first stream the remaining part of the
    1979             :  * transaction, and then invoke stream_prepare or stream_commit message as per
    1980             :  * the case.
    1981             :  */
    1982             : static void
    1983         128 : ReorderBufferStreamCommit(ReorderBuffer *rb, ReorderBufferTXN *txn)
    1984             : {
    1985             :     /* we should only call this for previously streamed transactions */
    1986             :     Assert(rbtxn_is_streamed(txn));
    1987             : 
    1988         128 :     ReorderBufferStreamTXN(rb, txn);
    1989             : 
    1990         128 :     if (rbtxn_is_prepared(txn))
    1991             :     {
    1992             :         /*
    1993             :          * Note, we send stream prepare even if a concurrent abort is
    1994             :          * detected. See DecodePrepare for more information.
    1995             :          */
    1996             :         Assert(!rbtxn_sent_prepare(txn));
    1997          28 :         rb->stream_prepare(rb, txn, txn->final_lsn);
    1998          28 :         txn->txn_flags |= RBTXN_SENT_PREPARE;
    1999             : 
    2000             :         /*
    2001             :          * This is a PREPARED transaction, part of a two-phase commit. The
    2002             :          * full cleanup will happen as part of the COMMIT PREPAREDs, so now
    2003             :          * just truncate txn by removing changes and tuplecids.
    2004             :          */
    2005          28 :         ReorderBufferTruncateTXN(rb, txn, true);
    2006             :         /* Reset the CheckXidAlive */
    2007          28 :         CheckXidAlive = InvalidTransactionId;
    2008             :     }
    2009             :     else
    2010             :     {
    2011         100 :         rb->stream_commit(rb, txn, txn->final_lsn);
    2012         100 :         ReorderBufferCleanupTXN(rb, txn);
    2013             :     }
    2014         128 : }
    2015             : 
    2016             : /*
    2017             :  * Set xid to detect concurrent aborts.
    2018             :  *
    2019             :  * While streaming an in-progress transaction or decoding a prepared
    2020             :  * transaction there is a possibility that the (sub)transaction might get
    2021             :  * aborted concurrently.  In such case if the (sub)transaction has catalog
    2022             :  * update then we might decode the tuple using wrong catalog version.  For
    2023             :  * example, suppose there is one catalog tuple with (xmin: 500, xmax: 0).  Now,
    2024             :  * the transaction 501 updates the catalog tuple and after that we will have
    2025             :  * two tuples (xmin: 500, xmax: 501) and (xmin: 501, xmax: 0).  Now, if 501 is
    2026             :  * aborted and some other transaction say 502 updates the same catalog tuple
    2027             :  * then the first tuple will be changed to (xmin: 500, xmax: 502).  So, the
    2028             :  * problem is that when we try to decode the tuple inserted/updated in 501
    2029             :  * after the catalog update, we will see the catalog tuple with (xmin: 500,
    2030             :  * xmax: 502) as visible because it will consider that the tuple is deleted by
    2031             :  * xid 502 which is not visible to our snapshot.  And when we will try to
    2032             :  * decode with that catalog tuple, it can lead to a wrong result or a crash.
    2033             :  * So, it is necessary to detect concurrent aborts to allow streaming of
    2034             :  * in-progress transactions or decoding of prepared transactions.
    2035             :  *
    2036             :  * For detecting the concurrent abort we set CheckXidAlive to the current
    2037             :  * (sub)transaction's xid for which this change belongs to.  And, during
    2038             :  * catalog scan we can check the status of the xid and if it is aborted we will
    2039             :  * report a specific error so that we can stop streaming current transaction
    2040             :  * and discard the already streamed changes on such an error.  We might have
    2041             :  * already streamed some of the changes for the aborted (sub)transaction, but
    2042             :  * that is fine because when we decode the abort we will stream abort message
    2043             :  * to truncate the changes in the subscriber. Similarly, for prepared
    2044             :  * transactions, we stop decoding if concurrent abort is detected and then
    2045             :  * rollback the changes when rollback prepared is encountered. See
    2046             :  * DecodePrepare.
    2047             :  */
    2048             : static inline void
    2049      355704 : SetupCheckXidLive(TransactionId xid)
    2050             : {
    2051             :     /*
    2052             :      * If the input transaction id is already set as a CheckXidAlive then
    2053             :      * nothing to do.
    2054             :      */
    2055      355704 :     if (TransactionIdEquals(CheckXidAlive, xid))
    2056      195072 :         return;
    2057             : 
    2058             :     /*
    2059             :      * setup CheckXidAlive if it's not committed yet.  We don't check if the
    2060             :      * xid is aborted.  That will happen during catalog access.
    2061             :      */
    2062      160632 :     if (!TransactionIdDidCommit(xid))
    2063         966 :         CheckXidAlive = xid;
    2064             :     else
    2065      159666 :         CheckXidAlive = InvalidTransactionId;
    2066             : }
    2067             : 
    2068             : /*
    2069             :  * Helper function for ReorderBufferProcessTXN for applying change.
    2070             :  */
    2071             : static inline void
    2072      668108 : ReorderBufferApplyChange(ReorderBuffer *rb, ReorderBufferTXN *txn,
    2073             :                          Relation relation, ReorderBufferChange *change,
    2074             :                          bool streaming)
    2075             : {
    2076      668108 :     if (streaming)
    2077      351976 :         rb->stream_change(rb, txn, relation, change);
    2078             :     else
    2079      316132 :         rb->apply_change(rb, txn, relation, change);
    2080      668104 : }
    2081             : 
    2082             : /*
    2083             :  * Helper function for ReorderBufferProcessTXN for applying the truncate.
    2084             :  */
    2085             : static inline void
    2086          48 : ReorderBufferApplyTruncate(ReorderBuffer *rb, ReorderBufferTXN *txn,
    2087             :                            int nrelations, Relation *relations,
    2088             :                            ReorderBufferChange *change, bool streaming)
    2089             : {
    2090          48 :     if (streaming)
    2091           0 :         rb->stream_truncate(rb, txn, nrelations, relations, change);
    2092             :     else
    2093          48 :         rb->apply_truncate(rb, txn, nrelations, relations, change);
    2094          48 : }
    2095             : 
    2096             : /*
    2097             :  * Helper function for ReorderBufferProcessTXN for applying the message.
    2098             :  */
    2099             : static inline void
    2100          22 : ReorderBufferApplyMessage(ReorderBuffer *rb, ReorderBufferTXN *txn,
    2101             :                           ReorderBufferChange *change, bool streaming)
    2102             : {
    2103          22 :     if (streaming)
    2104           6 :         rb->stream_message(rb, txn, change->lsn, true,
    2105           6 :                            change->data.msg.prefix,
    2106             :                            change->data.msg.message_size,
    2107           6 :                            change->data.msg.message);
    2108             :     else
    2109          16 :         rb->message(rb, txn, change->lsn, true,
    2110          16 :                     change->data.msg.prefix,
    2111             :                     change->data.msg.message_size,
    2112          16 :                     change->data.msg.message);
    2113          22 : }
    2114             : 
    2115             : /*
    2116             :  * Function to store the command id and snapshot at the end of the current
    2117             :  * stream so that we can reuse the same while sending the next stream.
    2118             :  */
    2119             : static inline void
    2120        1392 : ReorderBufferSaveTXNSnapshot(ReorderBuffer *rb, ReorderBufferTXN *txn,
    2121             :                              Snapshot snapshot_now, CommandId command_id)
    2122             : {
    2123        1392 :     txn->command_id = command_id;
    2124             : 
    2125             :     /* Avoid copying if it's already copied. */
    2126        1392 :     if (snapshot_now->copied)
    2127        1392 :         txn->snapshot_now = snapshot_now;
    2128             :     else
    2129           0 :         txn->snapshot_now = ReorderBufferCopySnap(rb, snapshot_now,
    2130             :                                                   txn, command_id);
    2131        1392 : }
    2132             : 
    2133             : /*
    2134             :  * Mark the given transaction as streamed if it's a top-level transaction
    2135             :  * or has changes.
    2136             :  */
    2137             : static void
    2138        1986 : ReorderBufferMaybeMarkTXNStreamed(ReorderBuffer *rb, ReorderBufferTXN *txn)
    2139             : {
    2140             :     /*
    2141             :      * The top-level transaction, is marked as streamed always, even if it
    2142             :      * does not contain any changes (that is, when all the changes are in
    2143             :      * subtransactions).
    2144             :      *
    2145             :      * For subtransactions, we only mark them as streamed when there are
    2146             :      * changes in them.
    2147             :      *
    2148             :      * We do it this way because of aborts - we don't want to send aborts for
    2149             :      * XIDs the downstream is not aware of. And of course, it always knows
    2150             :      * about the top-level xact (we send the XID in all messages), but we
    2151             :      * never stream XIDs of empty subxacts.
    2152             :      */
    2153        1986 :     if (rbtxn_is_toptxn(txn) || (txn->nentries_mem != 0))
    2154        1662 :         txn->txn_flags |= RBTXN_IS_STREAMED;
    2155        1986 : }
    2156             : 
    2157             : /*
    2158             :  * Helper function for ReorderBufferProcessTXN to handle the concurrent
    2159             :  * abort of the streaming transaction.  This resets the TXN such that it
    2160             :  * can be used to stream the remaining data of transaction being processed.
    2161             :  * This can happen when the subtransaction is aborted and we still want to
    2162             :  * continue processing the main or other subtransactions data.
    2163             :  */
    2164             : static void
    2165          16 : ReorderBufferResetTXN(ReorderBuffer *rb, ReorderBufferTXN *txn,
    2166             :                       Snapshot snapshot_now,
    2167             :                       CommandId command_id,
    2168             :                       XLogRecPtr last_lsn,
    2169             :                       ReorderBufferChange *specinsert)
    2170             : {
    2171             :     /* Discard the changes that we just streamed */
    2172          16 :     ReorderBufferTruncateTXN(rb, txn, rbtxn_is_prepared(txn));
    2173             : 
    2174             :     /* Free all resources allocated for toast reconstruction */
    2175          16 :     ReorderBufferToastReset(rb, txn);
    2176             : 
    2177             :     /* Return the spec insert change if it is not NULL */
    2178          16 :     if (specinsert != NULL)
    2179             :     {
    2180           0 :         ReorderBufferFreeChange(rb, specinsert, true);
    2181           0 :         specinsert = NULL;
    2182             :     }
    2183             : 
    2184             :     /*
    2185             :      * For the streaming case, stop the stream and remember the command ID and
    2186             :      * snapshot for the streaming run.
    2187             :      */
    2188          16 :     if (rbtxn_is_streamed(txn))
    2189             :     {
    2190          16 :         rb->stream_stop(rb, txn, last_lsn);
    2191          16 :         ReorderBufferSaveTXNSnapshot(rb, txn, snapshot_now, command_id);
    2192             :     }
    2193             : 
    2194             :     /* All changes must be deallocated */
    2195             :     Assert(txn->size == 0);
    2196          16 : }
    2197             : 
    2198             : /*
    2199             :  * Helper function for ReorderBufferReplay and ReorderBufferStreamTXN.
    2200             :  *
    2201             :  * Send data of a transaction (and its subtransactions) to the
    2202             :  * output plugin. We iterate over the top and subtransactions (using a k-way
    2203             :  * merge) and replay the changes in lsn order.
    2204             :  *
    2205             :  * If streaming is true then data will be sent using stream API.
    2206             :  *
    2207             :  * Note: "volatile" markers on some parameters are to avoid trouble with
    2208             :  * PG_TRY inside the function.
    2209             :  */
    2210             : static void
    2211        4022 : ReorderBufferProcessTXN(ReorderBuffer *rb, ReorderBufferTXN *txn,
    2212             :                         XLogRecPtr commit_lsn,
    2213             :                         volatile Snapshot snapshot_now,
    2214             :                         volatile CommandId command_id,
    2215             :                         bool streaming)
    2216             : {
    2217             :     bool        using_subtxn;
    2218        4022 :     MemoryContext ccxt = CurrentMemoryContext;
    2219        4022 :     ResourceOwner cowner = CurrentResourceOwner;
    2220        4022 :     ReorderBufferIterTXNState *volatile iterstate = NULL;
    2221        4022 :     volatile XLogRecPtr prev_lsn = InvalidXLogRecPtr;
    2222        4022 :     ReorderBufferChange *volatile specinsert = NULL;
    2223        4022 :     volatile bool stream_started = false;
    2224        4022 :     ReorderBufferTXN *volatile curtxn = NULL;
    2225             : 
    2226             :     /* build data to be able to lookup the CommandIds of catalog tuples */
    2227        4022 :     ReorderBufferBuildTupleCidHash(rb, txn);
    2228             : 
    2229             :     /* setup the initial snapshot */
    2230        4022 :     SetupHistoricSnapshot(snapshot_now, txn->tuplecid_hash);
    2231             : 
    2232             :     /*
    2233             :      * Decoding needs access to syscaches et al., which in turn use
    2234             :      * heavyweight locks and such. Thus we need to have enough state around to
    2235             :      * keep track of those.  The easiest way is to simply use a transaction
    2236             :      * internally.  That also allows us to easily enforce that nothing writes
    2237             :      * to the database by checking for xid assignments.
    2238             :      *
    2239             :      * When we're called via the SQL SRF there's already a transaction
    2240             :      * started, so start an explicit subtransaction there.
    2241             :      */
    2242        4022 :     using_subtxn = IsTransactionOrTransactionBlock();
    2243             : 
    2244        4022 :     PG_TRY();
    2245             :     {
    2246             :         ReorderBufferChange *change;
    2247        4022 :         int         changes_count = 0;  /* used to accumulate the number of
    2248             :                                          * changes */
    2249             : 
    2250        4022 :         if (using_subtxn)
    2251         986 :             BeginInternalSubTransaction(streaming ? "stream" : "replay");
    2252             :         else
    2253        3036 :             StartTransactionCommand();
    2254             : 
    2255             :         /*
    2256             :          * We only need to send begin/begin-prepare for non-streamed
    2257             :          * transactions.
    2258             :          */
    2259        4022 :         if (!streaming)
    2260             :         {
    2261        2630 :             if (rbtxn_is_prepared(txn))
    2262          54 :                 rb->begin_prepare(rb, txn);
    2263             :             else
    2264        2576 :                 rb->begin(rb, txn);
    2265             :         }
    2266             : 
    2267        4022 :         ReorderBufferIterTXNInit(rb, txn, &iterstate);
    2268      720622 :         while ((change = ReorderBufferIterTXNNext(rb, iterstate)) != NULL)
    2269             :         {
    2270      712598 :             Relation    relation = NULL;
    2271             :             Oid         reloid;
    2272             : 
    2273      712598 :             CHECK_FOR_INTERRUPTS();
    2274             : 
    2275             :             /*
    2276             :              * We can't call start stream callback before processing first
    2277             :              * change.
    2278             :              */
    2279      712598 :             if (!XLogRecPtrIsValid(prev_lsn))
    2280             :             {
    2281        3946 :                 if (streaming)
    2282             :                 {
    2283        1318 :                     txn->origin_id = change->origin_id;
    2284        1318 :                     rb->stream_start(rb, txn, change->lsn);
    2285        1318 :                     stream_started = true;
    2286             :                 }
    2287             :             }
    2288             : 
    2289             :             /*
    2290             :              * Enforce correct ordering of changes, merged from multiple
    2291             :              * subtransactions. The changes may have the same LSN due to
    2292             :              * MULTI_INSERT xlog records.
    2293             :              */
    2294             :             Assert(!XLogRecPtrIsValid(prev_lsn) || prev_lsn <= change->lsn);
    2295             : 
    2296      712598 :             prev_lsn = change->lsn;
    2297             : 
    2298             :             /*
    2299             :              * Set the current xid to detect concurrent aborts. This is
    2300             :              * required for the cases when we decode the changes before the
    2301             :              * COMMIT record is processed.
    2302             :              */
    2303      712598 :             if (streaming || rbtxn_is_prepared(change->txn))
    2304             :             {
    2305      355704 :                 curtxn = change->txn;
    2306      355704 :                 SetupCheckXidLive(curtxn->xid);
    2307             :             }
    2308             : 
    2309      712598 :             switch (change->action)
    2310             :             {
    2311        3564 :                 case REORDER_BUFFER_CHANGE_INTERNAL_SPEC_CONFIRM:
    2312             : 
    2313             :                     /*
    2314             :                      * Confirmation for speculative insertion arrived. Simply
    2315             :                      * use as a normal record. It'll be cleaned up at the end
    2316             :                      * of INSERT processing.
    2317             :                      */
    2318        3564 :                     if (specinsert == NULL)
    2319           0 :                         elog(ERROR, "invalid ordering of speculative insertion changes");
    2320             :                     Assert(specinsert->data.tp.oldtuple == NULL);
    2321        3564 :                     change = specinsert;
    2322        3564 :                     change->action = REORDER_BUFFER_CHANGE_INSERT;
    2323             : 
    2324             :                     /* intentionally fall through */
    2325      681248 :                 case REORDER_BUFFER_CHANGE_INSERT:
    2326             :                 case REORDER_BUFFER_CHANGE_UPDATE:
    2327             :                 case REORDER_BUFFER_CHANGE_DELETE:
    2328             :                     Assert(snapshot_now);
    2329             : 
    2330      681248 :                     reloid = RelidByRelfilenumber(change->data.tp.rlocator.spcOid,
    2331             :                                                   change->data.tp.rlocator.relNumber);
    2332             : 
    2333             :                     /*
    2334             :                      * Mapped catalog tuple without data, emitted while
    2335             :                      * catalog table was in the process of being rewritten. We
    2336             :                      * can fail to look up the relfilenumber, because the
    2337             :                      * relmapper has no "historic" view, in contrast to the
    2338             :                      * normal catalog during decoding. Thus repeated rewrites
    2339             :                      * can cause a lookup failure. That's OK because we do not
    2340             :                      * decode catalog changes anyway. Normally such tuples
    2341             :                      * would be skipped over below, but we can't identify
    2342             :                      * whether the table should be logically logged without
    2343             :                      * mapping the relfilenumber to the oid.
    2344             :                      */
    2345      681232 :                     if (reloid == InvalidOid &&
    2346         166 :                         change->data.tp.newtuple == NULL &&
    2347         166 :                         change->data.tp.oldtuple == NULL)
    2348         166 :                         goto change_done;
    2349      681066 :                     else if (reloid == InvalidOid)
    2350           0 :                         elog(ERROR, "could not map filenumber \"%s\" to relation OID",
    2351             :                              relpathperm(change->data.tp.rlocator,
    2352             :                                          MAIN_FORKNUM).str);
    2353             : 
    2354      681066 :                     relation = RelationIdGetRelation(reloid);
    2355             : 
    2356      681066 :                     if (!RelationIsValid(relation))
    2357           0 :                         elog(ERROR, "could not open relation with OID %u (for filenumber \"%s\")",
    2358             :                              reloid,
    2359             :                              relpathperm(change->data.tp.rlocator,
    2360             :                                          MAIN_FORKNUM).str);
    2361             : 
    2362      681066 :                     if (!RelationIsLogicallyLogged(relation))
    2363        8784 :                         goto change_done;
    2364             : 
    2365             :                     /*
    2366             :                      * Ignore temporary heaps created during DDL unless the
    2367             :                      * plugin has asked for them.
    2368             :                      */
    2369      672282 :                     if (relation->rd_rel->relrewrite && !rb->output_rewrites)
    2370          52 :                         goto change_done;
    2371             : 
    2372             :                     /*
    2373             :                      * For now ignore sequence changes entirely. Most of the
    2374             :                      * time they don't log changes using records we
    2375             :                      * understand, so it doesn't make sense to handle the few
    2376             :                      * cases we do.
    2377             :                      */
    2378      672230 :                     if (relation->rd_rel->relkind == RELKIND_SEQUENCE)
    2379           0 :                         goto change_done;
    2380             : 
    2381             :                     /* user-triggered change */
    2382      672230 :                     if (!IsToastRelation(relation))
    2383             :                     {
    2384      668108 :                         ReorderBufferToastReplace(rb, txn, relation, change);
    2385      668108 :                         ReorderBufferApplyChange(rb, txn, relation, change,
    2386             :                                                  streaming);
    2387             : 
    2388             :                         /*
    2389             :                          * Only clear reassembled toast chunks if we're sure
    2390             :                          * they're not required anymore. The creator of the
    2391             :                          * tuple tells us.
    2392             :                          */
    2393      668104 :                         if (change->data.tp.clear_toast_afterwards)
    2394      667644 :                             ReorderBufferToastReset(rb, txn);
    2395             :                     }
    2396             :                     /* we're not interested in toast deletions */
    2397        4122 :                     else if (change->action == REORDER_BUFFER_CHANGE_INSERT)
    2398             :                     {
    2399             :                         /*
    2400             :                          * Need to reassemble the full toasted Datum in
    2401             :                          * memory, to ensure the chunks don't get reused till
    2402             :                          * we're done remove it from the list of this
    2403             :                          * transaction's changes. Otherwise it will get
    2404             :                          * freed/reused while restoring spooled data from
    2405             :                          * disk.
    2406             :                          */
    2407             :                         Assert(change->data.tp.newtuple != NULL);
    2408             : 
    2409        3660 :                         dlist_delete(&change->node);
    2410        3660 :                         ReorderBufferToastAppendChunk(rb, txn, relation,
    2411             :                                                       change);
    2412             :                     }
    2413             : 
    2414         462 :             change_done:
    2415             : 
    2416             :                     /*
    2417             :                      * If speculative insertion was confirmed, the record
    2418             :                      * isn't needed anymore.
    2419             :                      */
    2420      681228 :                     if (specinsert != NULL)
    2421             :                     {
    2422        3564 :                         ReorderBufferFreeChange(rb, specinsert, true);
    2423        3564 :                         specinsert = NULL;
    2424             :                     }
    2425             : 
    2426      681228 :                     if (RelationIsValid(relation))
    2427             :                     {
    2428      681062 :                         RelationClose(relation);
    2429      681062 :                         relation = NULL;
    2430             :                     }
    2431      681228 :                     break;
    2432             : 
    2433        3564 :                 case REORDER_BUFFER_CHANGE_INTERNAL_SPEC_INSERT:
    2434             : 
    2435             :                     /*
    2436             :                      * Speculative insertions are dealt with by delaying the
    2437             :                      * processing of the insert until the confirmation record
    2438             :                      * arrives. For that we simply unlink the record from the
    2439             :                      * chain, so it does not get freed/reused while restoring
    2440             :                      * spooled data from disk.
    2441             :                      *
    2442             :                      * This is safe in the face of concurrent catalog changes
    2443             :                      * because the relevant relation can't be changed between
    2444             :                      * speculative insertion and confirmation due to
    2445             :                      * CheckTableNotInUse() and locking.
    2446             :                      */
    2447             : 
    2448             :                     /* clear out a pending (and thus failed) speculation */
    2449        3564 :                     if (specinsert != NULL)
    2450             :                     {
    2451           0 :                         ReorderBufferFreeChange(rb, specinsert, true);
    2452           0 :                         specinsert = NULL;
    2453             :                     }
    2454             : 
    2455             :                     /* and memorize the pending insertion */
    2456        3564 :                     dlist_delete(&change->node);
    2457        3564 :                     specinsert = change;
    2458        3564 :                     break;
    2459             : 
    2460           0 :                 case REORDER_BUFFER_CHANGE_INTERNAL_SPEC_ABORT:
    2461             : 
    2462             :                     /*
    2463             :                      * Abort for speculative insertion arrived. So cleanup the
    2464             :                      * specinsert tuple and toast hash.
    2465             :                      *
    2466             :                      * Note that we get the spec abort change for each toast
    2467             :                      * entry but we need to perform the cleanup only the first
    2468             :                      * time we get it for the main table.
    2469             :                      */
    2470           0 :                     if (specinsert != NULL)
    2471             :                     {
    2472             :                         /*
    2473             :                          * We must clean the toast hash before processing a
    2474             :                          * completely new tuple to avoid confusion about the
    2475             :                          * previous tuple's toast chunks.
    2476             :                          */
    2477             :                         Assert(change->data.tp.clear_toast_afterwards);
    2478           0 :                         ReorderBufferToastReset(rb, txn);
    2479             : 
    2480             :                         /* We don't need this record anymore. */
    2481           0 :                         ReorderBufferFreeChange(rb, specinsert, true);
    2482           0 :                         specinsert = NULL;
    2483             :                     }
    2484           0 :                     break;
    2485             : 
    2486          48 :                 case REORDER_BUFFER_CHANGE_TRUNCATE:
    2487             :                     {
    2488             :                         int         i;
    2489          48 :                         int         nrelids = change->data.truncate.nrelids;
    2490          48 :                         int         nrelations = 0;
    2491             :                         Relation   *relations;
    2492             : 
    2493          48 :                         relations = palloc0(nrelids * sizeof(Relation));
    2494         136 :                         for (i = 0; i < nrelids; i++)
    2495             :                         {
    2496          88 :                             Oid         relid = change->data.truncate.relids[i];
    2497             :                             Relation    rel;
    2498             : 
    2499          88 :                             rel = RelationIdGetRelation(relid);
    2500             : 
    2501          88 :                             if (!RelationIsValid(rel))
    2502           0 :                                 elog(ERROR, "could not open relation with OID %u", relid);
    2503             : 
    2504          88 :                             if (!RelationIsLogicallyLogged(rel))
    2505           0 :                                 continue;
    2506             : 
    2507          88 :                             relations[nrelations++] = rel;
    2508             :                         }
    2509             : 
    2510             :                         /* Apply the truncate. */
    2511          48 :                         ReorderBufferApplyTruncate(rb, txn, nrelations,
    2512             :                                                    relations, change,
    2513             :                                                    streaming);
    2514             : 
    2515         136 :                         for (i = 0; i < nrelations; i++)
    2516          88 :                             RelationClose(relations[i]);
    2517             : 
    2518          48 :                         break;
    2519             :                     }
    2520             : 
    2521          22 :                 case REORDER_BUFFER_CHANGE_MESSAGE:
    2522          22 :                     ReorderBufferApplyMessage(rb, txn, change, streaming);
    2523          22 :                     break;
    2524             : 
    2525        4600 :                 case REORDER_BUFFER_CHANGE_INVALIDATION:
    2526             :                     /* Execute the invalidation messages locally */
    2527        4600 :                     ReorderBufferExecuteInvalidations(change->data.inval.ninvalidations,
    2528             :                                                       change->data.inval.invalidations);
    2529        4600 :                     break;
    2530             : 
    2531        1176 :                 case REORDER_BUFFER_CHANGE_INTERNAL_SNAPSHOT:
    2532             :                     /* get rid of the old */
    2533        1176 :                     TeardownHistoricSnapshot(false);
    2534             : 
    2535        1176 :                     if (snapshot_now->copied)
    2536             :                     {
    2537        1126 :                         ReorderBufferFreeSnap(rb, snapshot_now);
    2538        1126 :                         snapshot_now =
    2539        1126 :                             ReorderBufferCopySnap(rb, change->data.snapshot,
    2540             :                                                   txn, command_id);
    2541             :                     }
    2542             : 
    2543             :                     /*
    2544             :                      * Restored from disk, need to be careful not to double
    2545             :                      * free. We could introduce refcounting for that, but for
    2546             :                      * now this seems infrequent enough not to care.
    2547             :                      */
    2548          50 :                     else if (change->data.snapshot->copied)
    2549             :                     {
    2550           0 :                         snapshot_now =
    2551           0 :                             ReorderBufferCopySnap(rb, change->data.snapshot,
    2552             :                                                   txn, command_id);
    2553             :                     }
    2554             :                     else
    2555             :                     {
    2556          50 :                         snapshot_now = change->data.snapshot;
    2557             :                     }
    2558             : 
    2559             :                     /* and continue with the new one */
    2560        1176 :                     SetupHistoricSnapshot(snapshot_now, txn->tuplecid_hash);
    2561        1176 :                     break;
    2562             : 
    2563       21940 :                 case REORDER_BUFFER_CHANGE_INTERNAL_COMMAND_ID:
    2564             :                     Assert(change->data.command_id != InvalidCommandId);
    2565             : 
    2566       21940 :                     if (command_id < change->data.command_id)
    2567             :                     {
    2568        3966 :                         command_id = change->data.command_id;
    2569             : 
    2570        3966 :                         if (!snapshot_now->copied)
    2571             :                         {
    2572             :                             /* we don't use the global one anymore */
    2573        1116 :                             snapshot_now = ReorderBufferCopySnap(rb, snapshot_now,
    2574             :                                                                  txn, command_id);
    2575             :                         }
    2576             : 
    2577        3966 :                         snapshot_now->curcid = command_id;
    2578             : 
    2579        3966 :                         TeardownHistoricSnapshot(false);
    2580        3966 :                         SetupHistoricSnapshot(snapshot_now, txn->tuplecid_hash);
    2581             :                     }
    2582             : 
    2583       21940 :                     break;
    2584             : 
    2585           0 :                 case REORDER_BUFFER_CHANGE_INTERNAL_TUPLECID:
    2586           0 :                     elog(ERROR, "tuplecid value in changequeue");
    2587             :                     break;
    2588             :             }
    2589             : 
    2590             :             /*
    2591             :              * It is possible that the data is not sent to downstream for a
    2592             :              * long time either because the output plugin filtered it or there
    2593             :              * is a DDL that generates a lot of data that is not processed by
    2594             :              * the plugin. So, in such cases, the downstream can timeout. To
    2595             :              * avoid that we try to send a keepalive message if required.
    2596             :              * Trying to send a keepalive message after every change has some
    2597             :              * overhead, but testing showed there is no noticeable overhead if
    2598             :              * we do it after every ~100 changes.
    2599             :              */
    2600             : #define CHANGES_THRESHOLD 100
    2601             : 
    2602      712578 :             if (++changes_count >= CHANGES_THRESHOLD)
    2603             :             {
    2604        6216 :                 rb->update_progress_txn(rb, txn, prev_lsn);
    2605        6216 :                 changes_count = 0;
    2606             :             }
    2607             :         }
    2608             : 
    2609             :         /* speculative insertion record must be freed by now */
    2610             :         Assert(!specinsert);
    2611             : 
    2612             :         /* clean up the iterator */
    2613        4002 :         ReorderBufferIterTXNFinish(rb, iterstate);
    2614        4002 :         iterstate = NULL;
    2615             : 
    2616             :         /*
    2617             :          * Update total transaction count and total bytes processed by the
    2618             :          * transaction and its subtransactions. Ensure to not count the
    2619             :          * streamed transaction multiple times.
    2620             :          *
    2621             :          * Note that the statistics computation has to be done after
    2622             :          * ReorderBufferIterTXNFinish as it releases the serialized change
    2623             :          * which we have already accounted in ReorderBufferIterTXNNext.
    2624             :          */
    2625        4002 :         if (!rbtxn_is_streamed(txn))
    2626        2758 :             rb->totalTxns++;
    2627             : 
    2628        4002 :         rb->totalBytes += txn->total_size;
    2629             : 
    2630             :         /*
    2631             :          * Done with current changes, send the last message for this set of
    2632             :          * changes depending upon streaming mode.
    2633             :          */
    2634        4002 :         if (streaming)
    2635             :         {
    2636        1376 :             if (stream_started)
    2637             :             {
    2638        1302 :                 rb->stream_stop(rb, txn, prev_lsn);
    2639        1302 :                 stream_started = false;
    2640             :             }
    2641             :         }
    2642             :         else
    2643             :         {
    2644             :             /*
    2645             :              * Call either PREPARE (for two-phase transactions) or COMMIT (for
    2646             :              * regular ones).
    2647             :              */
    2648        2626 :             if (rbtxn_is_prepared(txn))
    2649             :             {
    2650             :                 Assert(!rbtxn_sent_prepare(txn));
    2651          54 :                 rb->prepare(rb, txn, commit_lsn);
    2652          54 :                 txn->txn_flags |= RBTXN_SENT_PREPARE;
    2653             :             }
    2654             :             else
    2655        2572 :                 rb->commit(rb, txn, commit_lsn);
    2656             :         }
    2657             : 
    2658             :         /* this is just a sanity check against bad output plugin behaviour */
    2659        3936 :         if (GetCurrentTransactionIdIfAny() != InvalidTransactionId)
    2660           0 :             elog(ERROR, "output plugin used XID %u",
    2661             :                  GetCurrentTransactionId());
    2662             : 
    2663             :         /*
    2664             :          * Remember the command ID and snapshot for the next set of changes in
    2665             :          * streaming mode.
    2666             :          */
    2667        3936 :         if (streaming)
    2668        1376 :             ReorderBufferSaveTXNSnapshot(rb, txn, snapshot_now, command_id);
    2669        2560 :         else if (snapshot_now->copied)
    2670        1116 :             ReorderBufferFreeSnap(rb, snapshot_now);
    2671             : 
    2672             :         /* cleanup */
    2673        3936 :         TeardownHistoricSnapshot(false);
    2674             : 
    2675             :         /*
    2676             :          * Aborting the current (sub-)transaction as a whole has the right
    2677             :          * semantics. We want all locks acquired in here to be released, not
    2678             :          * reassigned to the parent and we do not want any database access
    2679             :          * have persistent effects.
    2680             :          */
    2681        3936 :         AbortCurrentTransaction();
    2682             : 
    2683             :         /* make sure there's no cache pollution */
    2684        3936 :         if (rbtxn_distr_inval_overflowed(txn))
    2685             :         {
    2686             :             Assert(txn->ninvalidations_distributed == 0);
    2687           0 :             InvalidateSystemCaches();
    2688             :         }
    2689             :         else
    2690             :         {
    2691        3936 :             ReorderBufferExecuteInvalidations(txn->ninvalidations, txn->invalidations);
    2692        3936 :             ReorderBufferExecuteInvalidations(txn->ninvalidations_distributed,
    2693             :                                               txn->invalidations_distributed);
    2694             :         }
    2695             : 
    2696        3936 :         if (using_subtxn)
    2697             :         {
    2698         978 :             RollbackAndReleaseCurrentSubTransaction();
    2699         978 :             MemoryContextSwitchTo(ccxt);
    2700         978 :             CurrentResourceOwner = cowner;
    2701             :         }
    2702             : 
    2703             :         /*
    2704             :          * We are here due to one of the four reasons: 1. Decoding an
    2705             :          * in-progress txn. 2. Decoding a prepared txn. 3. Decoding of a
    2706             :          * prepared txn that was (partially) streamed. 4. Decoding a committed
    2707             :          * txn.
    2708             :          *
    2709             :          * For 1, we allow truncation of txn data by removing the changes
    2710             :          * already streamed but still keeping other things like invalidations,
    2711             :          * snapshot, and tuplecids. For 2 and 3, we indicate
    2712             :          * ReorderBufferTruncateTXN to do more elaborate truncation of txn
    2713             :          * data as the entire transaction has been decoded except for commit.
    2714             :          * For 4, as the entire txn has been decoded, we can fully clean up
    2715             :          * the TXN reorder buffer.
    2716             :          */
    2717        3936 :         if (streaming || rbtxn_is_prepared(txn))
    2718             :         {
    2719        1430 :             if (streaming)
    2720        1376 :                 ReorderBufferMaybeMarkTXNStreamed(rb, txn);
    2721             : 
    2722        1430 :             ReorderBufferTruncateTXN(rb, txn, rbtxn_is_prepared(txn));
    2723             :             /* Reset the CheckXidAlive */
    2724        1430 :             CheckXidAlive = InvalidTransactionId;
    2725             :         }
    2726             :         else
    2727        2506 :             ReorderBufferCleanupTXN(rb, txn);
    2728             :     }
    2729          18 :     PG_CATCH();
    2730             :     {
    2731          18 :         MemoryContext ecxt = MemoryContextSwitchTo(ccxt);
    2732          18 :         ErrorData  *errdata = CopyErrorData();
    2733             : 
    2734             :         /* TODO: Encapsulate cleanup from the PG_TRY and PG_CATCH blocks */
    2735          18 :         if (iterstate)
    2736          18 :             ReorderBufferIterTXNFinish(rb, iterstate);
    2737             : 
    2738          18 :         TeardownHistoricSnapshot(true);
    2739             : 
    2740             :         /*
    2741             :          * Force cache invalidation to happen outside of a valid transaction
    2742             :          * to prevent catalog access as we just caught an error.
    2743             :          */
    2744          18 :         AbortCurrentTransaction();
    2745             : 
    2746             :         /* make sure there's no cache pollution */
    2747          18 :         if (rbtxn_distr_inval_overflowed(txn))
    2748             :         {
    2749             :             Assert(txn->ninvalidations_distributed == 0);
    2750           0 :             InvalidateSystemCaches();
    2751             :         }
    2752             :         else
    2753             :         {
    2754          18 :             ReorderBufferExecuteInvalidations(txn->ninvalidations, txn->invalidations);
    2755          18 :             ReorderBufferExecuteInvalidations(txn->ninvalidations_distributed,
    2756             :                                               txn->invalidations_distributed);
    2757             :         }
    2758             : 
    2759          18 :         if (using_subtxn)
    2760             :         {
    2761           8 :             RollbackAndReleaseCurrentSubTransaction();
    2762           8 :             MemoryContextSwitchTo(ccxt);
    2763           8 :             CurrentResourceOwner = cowner;
    2764             :         }
    2765             : 
    2766             :         /*
    2767             :          * The error code ERRCODE_TRANSACTION_ROLLBACK indicates a concurrent
    2768             :          * abort of the (sub)transaction we are streaming or preparing. We
    2769             :          * need to do the cleanup and return gracefully on this error, see
    2770             :          * SetupCheckXidLive.
    2771             :          *
    2772             :          * This error code can be thrown by one of the callbacks we call
    2773             :          * during decoding so we need to ensure that we return gracefully only
    2774             :          * when we are sending the data in streaming mode and the streaming is
    2775             :          * not finished yet or when we are sending the data out on a PREPARE
    2776             :          * during a two-phase commit.
    2777             :          */
    2778          18 :         if (errdata->sqlerrcode == ERRCODE_TRANSACTION_ROLLBACK &&
    2779          16 :             (stream_started || rbtxn_is_prepared(txn)))
    2780             :         {
    2781             :             /* curtxn must be set for streaming or prepared transactions */
    2782             :             Assert(curtxn);
    2783             : 
    2784             :             /* Cleanup the temporary error state. */
    2785          16 :             FlushErrorState();
    2786          16 :             FreeErrorData(errdata);
    2787          16 :             errdata = NULL;
    2788             : 
    2789             :             /* Remember the transaction is aborted. */
    2790             :             Assert(!rbtxn_is_committed(curtxn));
    2791          16 :             curtxn->txn_flags |= RBTXN_IS_ABORTED;
    2792             : 
    2793             :             /* Mark the transaction is streamed if appropriate */
    2794          16 :             if (stream_started)
    2795          16 :                 ReorderBufferMaybeMarkTXNStreamed(rb, txn);
    2796             : 
    2797             :             /* Reset the TXN so that it is allowed to stream remaining data. */
    2798          16 :             ReorderBufferResetTXN(rb, txn, snapshot_now,
    2799             :                                   command_id, prev_lsn,
    2800             :                                   specinsert);
    2801             :         }
    2802             :         else
    2803             :         {
    2804           2 :             ReorderBufferCleanupTXN(rb, txn);
    2805           2 :             MemoryContextSwitchTo(ecxt);
    2806           2 :             PG_RE_THROW();
    2807             :         }
    2808             :     }
    2809        3952 :     PG_END_TRY();
    2810        3952 : }
    2811             : 
    2812             : /*
    2813             :  * Perform the replay of a transaction and its non-aborted subtransactions.
    2814             :  *
    2815             :  * Subtransactions previously have to be processed by
    2816             :  * ReorderBufferCommitChild(), even if previously assigned to the toplevel
    2817             :  * transaction with ReorderBufferAssignChild.
    2818             :  *
    2819             :  * This interface is called once a prepare or toplevel commit is read for both
    2820             :  * streamed as well as non-streamed transactions.
    2821             :  */
    2822             : static void
    2823        2764 : ReorderBufferReplay(ReorderBufferTXN *txn,
    2824             :                     ReorderBuffer *rb, TransactionId xid,
    2825             :                     XLogRecPtr commit_lsn, XLogRecPtr end_lsn,
    2826             :                     TimestampTz commit_time,
    2827             :                     RepOriginId origin_id, XLogRecPtr origin_lsn)
    2828             : {
    2829             :     Snapshot    snapshot_now;
    2830        2764 :     CommandId   command_id = FirstCommandId;
    2831             : 
    2832        2764 :     txn->final_lsn = commit_lsn;
    2833        2764 :     txn->end_lsn = end_lsn;
    2834        2764 :     txn->commit_time = commit_time;
    2835        2764 :     txn->origin_id = origin_id;
    2836        2764 :     txn->origin_lsn = origin_lsn;
    2837             : 
    2838             :     /*
    2839             :      * If the transaction was (partially) streamed, we need to commit it in a
    2840             :      * 'streamed' way. That is, we first stream the remaining part of the
    2841             :      * transaction, and then invoke stream_commit message.
    2842             :      *
    2843             :      * Called after everything (origin ID, LSN, ...) is stored in the
    2844             :      * transaction to avoid passing that information directly.
    2845             :      */
    2846        2764 :     if (rbtxn_is_streamed(txn))
    2847             :     {
    2848         128 :         ReorderBufferStreamCommit(rb, txn);
    2849         128 :         return;
    2850             :     }
    2851             : 
    2852             :     /*
    2853             :      * If this transaction has no snapshot, it didn't make any changes to the
    2854             :      * database, so there's nothing to decode.  Note that
    2855             :      * ReorderBufferCommitChild will have transferred any snapshots from
    2856             :      * subtransactions if there were any.
    2857             :      */
    2858        2636 :     if (txn->base_snapshot == NULL)
    2859             :     {
    2860             :         Assert(txn->ninvalidations == 0);
    2861             : 
    2862             :         /*
    2863             :          * Removing this txn before a commit might result in the computation
    2864             :          * of an incorrect restart_lsn. See SnapBuildProcessRunningXacts.
    2865             :          */
    2866           6 :         if (!rbtxn_is_prepared(txn))
    2867           6 :             ReorderBufferCleanupTXN(rb, txn);
    2868           6 :         return;
    2869             :     }
    2870             : 
    2871        2630 :     snapshot_now = txn->base_snapshot;
    2872             : 
    2873             :     /* Process and send the changes to output plugin. */
    2874        2630 :     ReorderBufferProcessTXN(rb, txn, commit_lsn, snapshot_now,
    2875             :                             command_id, false);
    2876             : }
    2877             : 
    2878             : /*
    2879             :  * Commit a transaction.
    2880             :  *
    2881             :  * See comments for ReorderBufferReplay().
    2882             :  */
    2883             : void
    2884        2694 : ReorderBufferCommit(ReorderBuffer *rb, TransactionId xid,
    2885             :                     XLogRecPtr commit_lsn, XLogRecPtr end_lsn,
    2886             :                     TimestampTz commit_time,
    2887             :                     RepOriginId origin_id, XLogRecPtr origin_lsn)
    2888             : {
    2889             :     ReorderBufferTXN *txn;
    2890             : 
    2891        2694 :     txn = ReorderBufferTXNByXid(rb, xid, false, NULL, InvalidXLogRecPtr,
    2892             :                                 false);
    2893             : 
    2894             :     /* unknown transaction, nothing to replay */
    2895        2694 :     if (txn == NULL)
    2896          12 :         return;
    2897             : 
    2898        2682 :     ReorderBufferReplay(txn, rb, xid, commit_lsn, end_lsn, commit_time,
    2899             :                         origin_id, origin_lsn);
    2900             : }
    2901             : 
    2902             : /*
    2903             :  * Record the prepare information for a transaction. Also, mark the transaction
    2904             :  * as a prepared transaction.
    2905             :  */
    2906             : bool
    2907         328 : ReorderBufferRememberPrepareInfo(ReorderBuffer *rb, TransactionId xid,
    2908             :                                  XLogRecPtr prepare_lsn, XLogRecPtr end_lsn,
    2909             :                                  TimestampTz prepare_time,
    2910             :                                  RepOriginId origin_id, XLogRecPtr origin_lsn)
    2911             : {
    2912             :     ReorderBufferTXN *txn;
    2913             : 
    2914         328 :     txn = ReorderBufferTXNByXid(rb, xid, false, NULL, InvalidXLogRecPtr, false);
    2915             : 
    2916             :     /* unknown transaction, nothing to do */
    2917         328 :     if (txn == NULL)
    2918           0 :         return false;
    2919             : 
    2920             :     /*
    2921             :      * Remember the prepare information to be later used by commit prepared in
    2922             :      * case we skip doing prepare.
    2923             :      */
    2924         328 :     txn->final_lsn = prepare_lsn;
    2925         328 :     txn->end_lsn = end_lsn;
    2926         328 :     txn->prepare_time = prepare_time;
    2927         328 :     txn->origin_id = origin_id;
    2928         328 :     txn->origin_lsn = origin_lsn;
    2929             : 
    2930             :     /* Mark this transaction as a prepared transaction */
    2931             :     Assert((txn->txn_flags & RBTXN_PREPARE_STATUS_MASK) == 0);
    2932         328 :     txn->txn_flags |= RBTXN_IS_PREPARED;
    2933             : 
    2934         328 :     return true;
    2935             : }
    2936             : 
    2937             : /* Remember that we have skipped prepare */
    2938             : void
    2939         252 : ReorderBufferSkipPrepare(ReorderBuffer *rb, TransactionId xid)
    2940             : {
    2941             :     ReorderBufferTXN *txn;
    2942             : 
    2943         252 :     txn = ReorderBufferTXNByXid(rb, xid, false, NULL, InvalidXLogRecPtr, false);
    2944             : 
    2945             :     /* unknown transaction, nothing to do */
    2946         252 :     if (txn == NULL)
    2947           0 :         return;
    2948             : 
    2949             :     /* txn must have been marked as a prepared transaction */
    2950             :     Assert((txn->txn_flags & RBTXN_PREPARE_STATUS_MASK) == RBTXN_IS_PREPARED);
    2951         252 :     txn->txn_flags |= RBTXN_SKIPPED_PREPARE;
    2952             : }
    2953             : 
    2954             : /*
    2955             :  * Prepare a two-phase transaction.
    2956             :  *
    2957             :  * See comments for ReorderBufferReplay().
    2958             :  */
    2959             : void
    2960          76 : ReorderBufferPrepare(ReorderBuffer *rb, TransactionId xid,
    2961             :                      char *gid)
    2962             : {
    2963             :     ReorderBufferTXN *txn;
    2964             : 
    2965          76 :     txn = ReorderBufferTXNByXid(rb, xid, false, NULL, InvalidXLogRecPtr,
    2966             :                                 false);
    2967             : 
    2968             :     /* unknown transaction, nothing to replay */
    2969          76 :     if (txn == NULL)
    2970           0 :         return;
    2971             : 
    2972             :     /*
    2973             :      * txn must have been marked as a prepared transaction and must have
    2974             :      * neither been skipped nor sent a prepare. Also, the prepare info must
    2975             :      * have been updated in it by now.
    2976             :      */
    2977             :     Assert((txn->txn_flags & RBTXN_PREPARE_STATUS_MASK) == RBTXN_IS_PREPARED);
    2978             :     Assert(XLogRecPtrIsValid(txn->final_lsn));
    2979             : 
    2980          76 :     txn->gid = pstrdup(gid);
    2981             : 
    2982          76 :     ReorderBufferReplay(txn, rb, xid, txn->final_lsn, txn->end_lsn,
    2983          76 :                         txn->prepare_time, txn->origin_id, txn->origin_lsn);
    2984             : 
    2985             :     /*
    2986             :      * Send a prepare if not already done so. This might occur if we have
    2987             :      * detected a concurrent abort while replaying the non-streaming
    2988             :      * transaction.
    2989             :      */
    2990          76 :     if (!rbtxn_sent_prepare(txn))
    2991             :     {
    2992           0 :         rb->prepare(rb, txn, txn->final_lsn);
    2993           0 :         txn->txn_flags |= RBTXN_SENT_PREPARE;
    2994             :     }
    2995             : }
    2996             : 
    2997             : /*
    2998             :  * This is used to handle COMMIT/ROLLBACK PREPARED.
    2999             :  */
    3000             : void
    3001          82 : ReorderBufferFinishPrepared(ReorderBuffer *rb, TransactionId xid,
    3002             :                             XLogRecPtr commit_lsn, XLogRecPtr end_lsn,
    3003             :                             XLogRecPtr two_phase_at,
    3004             :                             TimestampTz commit_time, RepOriginId origin_id,
    3005             :                             XLogRecPtr origin_lsn, char *gid, bool is_commit)
    3006             : {
    3007             :     ReorderBufferTXN *txn;
    3008             :     XLogRecPtr  prepare_end_lsn;
    3009             :     TimestampTz prepare_time;
    3010             : 
    3011          82 :     txn = ReorderBufferTXNByXid(rb, xid, false, NULL, commit_lsn, false);
    3012             : 
    3013             :     /* unknown transaction, nothing to do */
    3014          82 :     if (txn == NULL)
    3015           0 :         return;
    3016             : 
    3017             :     /*
    3018             :      * By this time the txn has the prepare record information, remember it to
    3019             :      * be later used for rollback.
    3020             :      */
    3021          82 :     prepare_end_lsn = txn->end_lsn;
    3022          82 :     prepare_time = txn->prepare_time;
    3023             : 
    3024             :     /* add the gid in the txn */
    3025          82 :     txn->gid = pstrdup(gid);
    3026             : 
    3027             :     /*
    3028             :      * It is possible that this transaction is not decoded at prepare time
    3029             :      * either because by that time we didn't have a consistent snapshot, or
    3030             :      * two_phase was not enabled, or it was decoded earlier but we have
    3031             :      * restarted. We only need to send the prepare if it was not decoded
    3032             :      * earlier. We don't need to decode the xact for aborts if it is not done
    3033             :      * already.
    3034             :      */
    3035          82 :     if ((txn->final_lsn < two_phase_at) && is_commit)
    3036             :     {
    3037             :         /*
    3038             :          * txn must have been marked as a prepared transaction and skipped but
    3039             :          * not sent a prepare. Also, the prepare info must have been updated
    3040             :          * in txn even if we skip prepare.
    3041             :          */
    3042             :         Assert((txn->txn_flags & RBTXN_PREPARE_STATUS_MASK) ==
    3043             :                (RBTXN_IS_PREPARED | RBTXN_SKIPPED_PREPARE));
    3044             :         Assert(XLogRecPtrIsValid(txn->final_lsn));
    3045             : 
    3046             :         /*
    3047             :          * By this time the txn has the prepare record information and it is
    3048             :          * important to use that so that downstream gets the accurate
    3049             :          * information. If instead, we have passed commit information here
    3050             :          * then downstream can behave as it has already replayed commit
    3051             :          * prepared after the restart.
    3052             :          */
    3053           6 :         ReorderBufferReplay(txn, rb, xid, txn->final_lsn, txn->end_lsn,
    3054           6 :                             txn->prepare_time, txn->origin_id, txn->origin_lsn);
    3055             :     }
    3056             : 
    3057          82 :     txn->final_lsn = commit_lsn;
    3058          82 :     txn->end_lsn = end_lsn;
    3059          82 :     txn->commit_time = commit_time;
    3060          82 :     txn->origin_id = origin_id;
    3061          82 :     txn->origin_lsn = origin_lsn;
    3062             : 
    3063          82 :     if (is_commit)
    3064          64 :         rb->commit_prepared(rb, txn, commit_lsn);
    3065             :     else
    3066          18 :         rb->rollback_prepared(rb, txn, prepare_end_lsn, prepare_time);
    3067             : 
    3068             :     /* cleanup: make sure there's no cache pollution */
    3069          82 :     ReorderBufferExecuteInvalidations(txn->ninvalidations,
    3070             :                                       txn->invalidations);
    3071          82 :     ReorderBufferCleanupTXN(rb, txn);
    3072             : }
    3073             : 
    3074             : /*
    3075             :  * Abort a transaction that possibly has previous changes. Needs to be first
    3076             :  * called for subtransactions and then for the toplevel xid.
    3077             :  *
    3078             :  * NB: Transactions handled here have to have actively aborted (i.e. have
    3079             :  * produced an abort record). Implicitly aborted transactions are handled via
    3080             :  * ReorderBufferAbortOld(); transactions we're just not interested in, but
    3081             :  * which have committed are handled in ReorderBufferForget().
    3082             :  *
    3083             :  * This function purges this transaction and its contents from memory and
    3084             :  * disk.
    3085             :  */
    3086             : void
    3087         422 : ReorderBufferAbort(ReorderBuffer *rb, TransactionId xid, XLogRecPtr lsn,
    3088             :                    TimestampTz abort_time)
    3089             : {
    3090             :     ReorderBufferTXN *txn;
    3091             : 
    3092         422 :     txn = ReorderBufferTXNByXid(rb, xid, false, NULL, InvalidXLogRecPtr,
    3093             :                                 false);
    3094             : 
    3095             :     /* unknown, nothing to remove */
    3096         422 :     if (txn == NULL)
    3097           0 :         return;
    3098             : 
    3099         422 :     txn->abort_time = abort_time;
    3100             : 
    3101             :     /* For streamed transactions notify the remote node about the abort. */
    3102         422 :     if (rbtxn_is_streamed(txn))
    3103             :     {
    3104          60 :         rb->stream_abort(rb, txn, lsn);
    3105             : 
    3106             :         /*
    3107             :          * We might have decoded changes for this transaction that could load
    3108             :          * the cache as per the current transaction's view (consider DDL's
    3109             :          * happened in this transaction). We don't want the decoding of future
    3110             :          * transactions to use those cache entries so execute only the inval
    3111             :          * messages in this transaction.
    3112             :          */
    3113          60 :         if (txn->ninvalidations > 0)
    3114           0 :             ReorderBufferImmediateInvalidation(rb, txn->ninvalidations,
    3115             :                                                txn->invalidations);
    3116             :     }
    3117             : 
    3118             :     /* cosmetic... */
    3119         422 :     txn->final_lsn = lsn;
    3120             : 
    3121             :     /* remove potential on-disk data, and deallocate */
    3122         422 :     ReorderBufferCleanupTXN(rb, txn);
    3123             : }
    3124             : 
    3125             : /*
    3126             :  * Abort all transactions that aren't actually running anymore because the
    3127             :  * server restarted.
    3128             :  *
    3129             :  * NB: These really have to be transactions that have aborted due to a server
    3130             :  * crash/immediate restart, as we don't deal with invalidations here.
    3131             :  */
    3132             : void
    3133        2802 : ReorderBufferAbortOld(ReorderBuffer *rb, TransactionId oldestRunningXid)
    3134             : {
    3135             :     dlist_mutable_iter it;
    3136             : 
    3137             :     /*
    3138             :      * Iterate through all (potential) toplevel TXNs and abort all that are
    3139             :      * older than what possibly can be running. Once we've found the first
    3140             :      * that is alive we stop, there might be some that acquired an xid earlier
    3141             :      * but started writing later, but it's unlikely and they will be cleaned
    3142             :      * up in a later call to this function.
    3143             :      */
    3144        2810 :     dlist_foreach_modify(it, &rb->toplevel_by_lsn)
    3145             :     {
    3146             :         ReorderBufferTXN *txn;
    3147             : 
    3148         124 :         txn = dlist_container(ReorderBufferTXN, node, it.cur);
    3149             : 
    3150         124 :         if (TransactionIdPrecedes(txn->xid, oldestRunningXid))
    3151             :         {
    3152           8 :             elog(DEBUG2, "aborting old transaction %u", txn->xid);
    3153             : 
    3154             :             /* Notify the remote node about the crash/immediate restart. */
    3155           8 :             if (rbtxn_is_streamed(txn))
    3156           0 :                 rb->stream_abort(rb, txn, InvalidXLogRecPtr);
    3157             : 
    3158             :             /* remove potential on-disk data, and deallocate this tx */
    3159           8 :             ReorderBufferCleanupTXN(rb, txn);
    3160             :         }
    3161             :         else
    3162         116 :             return;
    3163             :     }
    3164             : }
    3165             : 
    3166             : /*
    3167             :  * Forget the contents of a transaction if we aren't interested in its
    3168             :  * contents. Needs to be first called for subtransactions and then for the
    3169             :  * toplevel xid.
    3170             :  *
    3171             :  * This is significantly different to ReorderBufferAbort() because
    3172             :  * transactions that have committed need to be treated differently from aborted
    3173             :  * ones since they may have modified the catalog.
    3174             :  *
    3175             :  * Note that this is only allowed to be called in the moment a transaction
    3176             :  * commit has just been read, not earlier; otherwise later records referring
    3177             :  * to this xid might re-create the transaction incompletely.
    3178             :  */
    3179             : void
    3180        5412 : ReorderBufferForget(ReorderBuffer *rb, TransactionId xid, XLogRecPtr lsn)
    3181             : {
    3182             :     ReorderBufferTXN *txn;
    3183             : 
    3184        5412 :     txn = ReorderBufferTXNByXid(rb, xid, false, NULL, InvalidXLogRecPtr,
    3185             :                                 false);
    3186             : 
    3187             :     /* unknown, nothing to forget */
    3188        5412 :     if (txn == NULL)
    3189        1126 :         return;
    3190             : 
    3191             :     /* this transaction mustn't be streamed */
    3192             :     Assert(!rbtxn_is_streamed(txn));
    3193             : 
    3194             :     /* cosmetic... */
    3195        4286 :     txn->final_lsn = lsn;
    3196             : 
    3197             :     /*
    3198             :      * Process only cache invalidation messages in this transaction if there
    3199             :      * are any. Even if we're not interested in the transaction's contents, it
    3200             :      * could have manipulated the catalog and we need to update the caches
    3201             :      * according to that.
    3202             :      */
    3203        4286 :     if (txn->base_snapshot != NULL && txn->ninvalidations > 0)
    3204        1160 :         ReorderBufferImmediateInvalidation(rb, txn->ninvalidations,
    3205             :                                            txn->invalidations);
    3206             :     else
    3207             :         Assert(txn->ninvalidations == 0);
    3208             : 
    3209             :     /* remove potential on-disk data, and deallocate */
    3210        4286 :     ReorderBufferCleanupTXN(rb, txn);
    3211             : }
    3212             : 
    3213             : /*
    3214             :  * Invalidate cache for those transactions that need to be skipped just in case
    3215             :  * catalogs were manipulated as part of the transaction.
    3216             :  *
    3217             :  * Note that this is a special-purpose function for prepared transactions where
    3218             :  * we don't want to clean up the TXN even when we decide to skip it. See
    3219             :  * DecodePrepare.
    3220             :  */
    3221             : void
    3222         246 : ReorderBufferInvalidate(ReorderBuffer *rb, TransactionId xid, XLogRecPtr lsn)
    3223             : {
    3224             :     ReorderBufferTXN *txn;
    3225             : 
    3226         246 :     txn = ReorderBufferTXNByXid(rb, xid, false, NULL, InvalidXLogRecPtr,
    3227             :                                 false);
    3228             : 
    3229             :     /* unknown, nothing to do */
    3230         246 :     if (txn == NULL)
    3231           0 :         return;
    3232             : 
    3233             :     /*
    3234             :      * Process cache invalidation messages if there are any. Even if we're not
    3235             :      * interested in the transaction's contents, it could have manipulated the
    3236             :      * catalog and we need to update the caches according to that.
    3237             :      */
    3238         246 :     if (txn->base_snapshot != NULL && txn->ninvalidations > 0)
    3239          58 :         ReorderBufferImmediateInvalidation(rb, txn->ninvalidations,
    3240             :                                            txn->invalidations);
    3241             :     else
    3242             :         Assert(txn->ninvalidations == 0);
    3243             : }
    3244             : 
    3245             : 
    3246             : /*
    3247             :  * Execute invalidations happening outside the context of a decoded
    3248             :  * transaction. That currently happens either for xid-less commits
    3249             :  * (cf. RecordTransactionCommit()) or for invalidations in uninteresting
    3250             :  * transactions (via ReorderBufferForget()).
    3251             :  */
    3252             : void
    3253        1230 : ReorderBufferImmediateInvalidation(ReorderBuffer *rb, uint32 ninvalidations,
    3254             :                                    SharedInvalidationMessage *invalidations)
    3255             : {
    3256        1230 :     bool        use_subtxn = IsTransactionOrTransactionBlock();
    3257        1230 :     MemoryContext ccxt = CurrentMemoryContext;
    3258        1230 :     ResourceOwner cowner = CurrentResourceOwner;
    3259             :     int         i;
    3260             : 
    3261        1230 :     if (use_subtxn)
    3262         870 :         BeginInternalSubTransaction("replay");
    3263             : 
    3264             :     /*
    3265             :      * Force invalidations to happen outside of a valid transaction - that way
    3266             :      * entries will just be marked as invalid without accessing the catalog.
    3267             :      * That's advantageous because we don't need to setup the full state
    3268             :      * necessary for catalog access.
    3269             :      */
    3270        1230 :     if (use_subtxn)
    3271         870 :         AbortCurrentTransaction();
    3272             : 
    3273       47446 :     for (i = 0; i < ninvalidations; i++)
    3274       46216 :         LocalExecuteInvalidationMessage(&invalidations[i]);
    3275             : 
    3276        1230 :     if (use_subtxn)
    3277             :     {
    3278         870 :         RollbackAndReleaseCurrentSubTransaction();
    3279         870 :         MemoryContextSwitchTo(ccxt);
    3280         870 :         CurrentResourceOwner = cowner;
    3281             :     }
    3282        1230 : }
    3283             : 
    3284             : /*
    3285             :  * Tell reorderbuffer about an xid seen in the WAL stream. Has to be called at
    3286             :  * least once for every xid in XLogRecord->xl_xid (other places in records
    3287             :  * may, but do not have to be passed through here).
    3288             :  *
    3289             :  * Reorderbuffer keeps some data structures about transactions in LSN order,
    3290             :  * for efficiency. To do that it has to know about when transactions are seen
    3291             :  * first in the WAL. As many types of records are not actually interesting for
    3292             :  * logical decoding, they do not necessarily pass through here.
    3293             :  */
    3294             : void
    3295     4434272 : ReorderBufferProcessXid(ReorderBuffer *rb, TransactionId xid, XLogRecPtr lsn)
    3296             : {
    3297             :     /* many records won't have an xid assigned, centralize check here */
    3298     4434272 :     if (xid != InvalidTransactionId)
    3299     4430178 :         ReorderBufferTXNByXid(rb, xid, true, NULL, lsn, true);
    3300     4434272 : }
    3301             : 
    3302             : /*
    3303             :  * Add a new snapshot to this transaction that may only used after lsn 'lsn'
    3304             :  * because the previous snapshot doesn't describe the catalog correctly for
    3305             :  * following rows.
    3306             :  */
    3307             : void
    3308        2364 : ReorderBufferAddSnapshot(ReorderBuffer *rb, TransactionId xid,
    3309             :                          XLogRecPtr lsn, Snapshot snap)
    3310             : {
    3311        2364 :     ReorderBufferChange *change = ReorderBufferAllocChange(rb);
    3312             : 
    3313        2364 :     change->data.snapshot = snap;
    3314        2364 :     change->action = REORDER_BUFFER_CHANGE_INTERNAL_SNAPSHOT;
    3315             : 
    3316        2364 :     ReorderBufferQueueChange(rb, xid, lsn, change, false);
    3317        2364 : }
    3318             : 
    3319             : /*
    3320             :  * Set up the transaction's base snapshot.
    3321             :  *
    3322             :  * If we know that xid is a subtransaction, set the base snapshot on the
    3323             :  * top-level transaction instead.
    3324             :  */
    3325             : void
    3326        6544 : ReorderBufferSetBaseSnapshot(ReorderBuffer *rb, TransactionId xid,
    3327             :                              XLogRecPtr lsn, Snapshot snap)
    3328             : {
    3329             :     ReorderBufferTXN *txn;
    3330             :     bool        is_new;
    3331             : 
    3332             :     Assert(snap != NULL);
    3333             : 
    3334             :     /*
    3335             :      * Fetch the transaction to operate on.  If we know it's a subtransaction,
    3336             :      * operate on its top-level transaction instead.
    3337             :      */
    3338        6544 :     txn = ReorderBufferTXNByXid(rb, xid, true, &is_new, lsn, true);
    3339        6544 :     if (rbtxn_is_known_subxact(txn))
    3340         244 :         txn = ReorderBufferTXNByXid(rb, txn->toplevel_xid, false,
    3341             :                                     NULL, InvalidXLogRecPtr, false);
    3342             :     Assert(txn->base_snapshot == NULL);
    3343             : 
    3344        6544 :     txn->base_snapshot = snap;
    3345        6544 :     txn->base_snapshot_lsn = lsn;
    3346        6544 :     dlist_push_tail(&rb->txns_by_base_snapshot_lsn, &txn->base_snapshot_node);
    3347             : 
    3348        6544 :     AssertTXNLsnOrder(rb);
    3349        6544 : }
    3350             : 
    3351             : /*
    3352             :  * Access the catalog with this CommandId at this point in the changestream.
    3353             :  *
    3354             :  * May only be called for command ids > 1
    3355             :  */
    3356             : void
    3357       46568 : ReorderBufferAddNewCommandId(ReorderBuffer *rb, TransactionId xid,
    3358             :                              XLogRecPtr lsn, CommandId cid)
    3359             : {
    3360       46568 :     ReorderBufferChange *change = ReorderBufferAllocChange(rb);
    3361             : 
    3362       46568 :     change->data.command_id = cid;
    3363       46568 :     change->action = REORDER_BUFFER_CHANGE_INTERNAL_COMMAND_ID;
    3364             : 
    3365       46568 :     ReorderBufferQueueChange(rb, xid, lsn, change, false);
    3366       46568 : }
    3367             : 
    3368             : /*
    3369             :  * Update memory counters to account for the new or removed change.
    3370             :  *
    3371             :  * We update two counters - in the reorder buffer, and in the transaction
    3372             :  * containing the change. The reorder buffer counter allows us to quickly
    3373             :  * decide if we reached the memory limit, the transaction counter allows
    3374             :  * us to quickly pick the largest transaction for eviction.
    3375             :  *
    3376             :  * Either txn or change must be non-NULL at least. We update the memory
    3377             :  * counter of txn if it's non-NULL, otherwise change->txn.
    3378             :  *
    3379             :  * When streaming is enabled, we need to update the toplevel transaction
    3380             :  * counters instead - we don't really care about subtransactions as we
    3381             :  * can't stream them individually anyway, and we only pick toplevel
    3382             :  * transactions for eviction. So only toplevel transactions matter.
    3383             :  */
    3384             : static void
    3385     3909252 : ReorderBufferChangeMemoryUpdate(ReorderBuffer *rb,
    3386             :                                 ReorderBufferChange *change,
    3387             :                                 ReorderBufferTXN *txn,
    3388             :                                 bool addition, Size sz)
    3389             : {
    3390             :     ReorderBufferTXN *toptxn;
    3391             : 
    3392             :     Assert(txn || change);
    3393             : 
    3394             :     /*
    3395             :      * Ignore tuple CID changes, because those are not evicted when reaching
    3396             :      * memory limit. So we just don't count them, because it might easily
    3397             :      * trigger a pointless attempt to spill.
    3398             :      */
    3399     3909252 :     if (change && change->action == REORDER_BUFFER_CHANGE_INTERNAL_TUPLECID)
    3400       46336 :         return;
    3401             : 
    3402     3862916 :     if (sz == 0)
    3403        2236 :         return;
    3404             : 
    3405     3860680 :     if (txn == NULL)
    3406     3844472 :         txn = change->txn;
    3407             :     Assert(txn != NULL);
    3408             : 
    3409             :     /*
    3410             :      * Update the total size in top level as well. This is later used to
    3411             :      * compute the decoding stats.
    3412             :      */
    3413     3860680 :     toptxn = rbtxn_get_toptxn(txn);
    3414             : 
    3415     3860680 :     if (addition)
    3416             :     {
    3417     3487222 :         Size        oldsize = txn->size;
    3418             : 
    3419     3487222 :         txn->size += sz;
    3420     3487222 :         rb->size += sz;
    3421             : 
    3422             :         /* Update the total size in the top transaction. */
    3423     3487222 :         toptxn->total_size += sz;
    3424             : 
    3425             :         /* Update the max-heap */
    3426     3487222 :         if (oldsize != 0)
    3427     3470826 :             pairingheap_remove(rb->txn_heap, &txn->txn_node);
    3428     3487222 :         pairingheap_add(rb->txn_heap, &txn->txn_node);
    3429             :     }
    3430             :     else
    3431             :     {
    3432             :         Assert((rb->size >= sz) && (txn->size >= sz));
    3433      373458 :         txn->size -= sz;
    3434      373458 :         rb->size -= sz;
    3435             : 
    3436             :         /* Update the total size in the top transaction. */
    3437      373458 :         toptxn->total_size -= sz;
    3438             : 
    3439             :         /* Update the max-heap */
    3440      373458 :         pairingheap_remove(rb->txn_heap, &txn->txn_node);
    3441      373458 :         if (txn->size != 0)
    3442      357192 :             pairingheap_add(rb->txn_heap, &txn->txn_node);
    3443             :     }
    3444             : 
    3445             :     Assert(txn->size <= rb->size);
    3446             : }
    3447             : 
    3448             : /*
    3449             :  * Add new (relfilelocator, tid) -> (cmin, cmax) mappings.
    3450             :  *
    3451             :  * We do not include this change type in memory accounting, because we
    3452             :  * keep CIDs in a separate list and do not evict them when reaching
    3453             :  * the memory limit.
    3454             :  */
    3455             : void
    3456       46568 : ReorderBufferAddNewTupleCids(ReorderBuffer *rb, TransactionId xid,
    3457             :                              XLogRecPtr lsn, RelFileLocator locator,
    3458             :                              ItemPointerData tid, CommandId cmin,
    3459             :                              CommandId cmax, CommandId combocid)
    3460             : {
    3461       46568 :     ReorderBufferChange *change = ReorderBufferAllocChange(rb);
    3462             :     ReorderBufferTXN *txn;
    3463             : 
    3464       46568 :     txn = ReorderBufferTXNByXid(rb, xid, true, NULL, lsn, true);
    3465             : 
    3466       46568 :     change->data.tuplecid.locator = locator;
    3467       46568 :     change->data.tuplecid.tid = tid;
    3468       46568 :     change->data.tuplecid.cmin = cmin;
    3469       46568 :     change->data.tuplecid.cmax = cmax;
    3470       46568 :     change->data.tuplecid.combocid = combocid;
    3471       46568 :     change->lsn = lsn;
    3472       46568 :     change->txn = txn;
    3473       46568 :     change->action = REORDER_BUFFER_CHANGE_INTERNAL_TUPLECID;
    3474             : 
    3475       46568 :     dlist_push_tail(&txn->tuplecids, &change->node);
    3476       46568 :     txn->ntuplecids++;
    3477       46568 : }
    3478             : 
    3479             : /*
    3480             :  * Add new invalidation messages to the reorder buffer queue.
    3481             :  */
    3482             : static void
    3483        9798 : ReorderBufferQueueInvalidations(ReorderBuffer *rb, TransactionId xid,
    3484             :                                 XLogRecPtr lsn, Size nmsgs,
    3485             :                                 SharedInvalidationMessage *msgs)
    3486             : {
    3487             :     ReorderBufferChange *change;
    3488             : 
    3489        9798 :     change = ReorderBufferAllocChange(rb);
    3490        9798 :     change->action = REORDER_BUFFER_CHANGE_INVALIDATION;
    3491        9798 :     change->data.inval.ninvalidations = nmsgs;
    3492        9798 :     change->data.inval.invalidations = palloc_array(SharedInvalidationMessage, nmsgs);
    3493        9798 :     memcpy(change->data.inval.invalidations, msgs,
    3494             :            sizeof(SharedInvalidationMessage) * nmsgs);
    3495             : 
    3496        9798 :     ReorderBufferQueueChange(rb, xid, lsn, change, false);
    3497        9798 : }
    3498             : 
    3499             : /*
    3500             :  * A helper function for ReorderBufferAddInvalidations() and
    3501             :  * ReorderBufferAddDistributedInvalidations() to accumulate the invalidation
    3502             :  * messages to the **invals_out.
    3503             :  */
    3504             : static void
    3505        9798 : ReorderBufferAccumulateInvalidations(SharedInvalidationMessage **invals_out,
    3506             :                                      uint32 *ninvals_out,
    3507             :                                      SharedInvalidationMessage *msgs_new,
    3508             :                                      Size nmsgs_new)
    3509             : {
    3510        9798 :     if (*ninvals_out == 0)
    3511             :     {
    3512        2378 :         *ninvals_out = nmsgs_new;
    3513        2378 :         *invals_out = palloc_array(SharedInvalidationMessage, nmsgs_new);
    3514        2378 :         memcpy(*invals_out, msgs_new, sizeof(SharedInvalidationMessage) * nmsgs_new);
    3515             :     }
    3516             :     else
    3517             :     {
    3518             :         /* Enlarge the array of inval messages */
    3519        7420 :         *invals_out = (SharedInvalidationMessage *)
    3520        7420 :             repalloc(*invals_out, sizeof(SharedInvalidationMessage) *
    3521        7420 :                      (*ninvals_out + nmsgs_new));
    3522        7420 :         memcpy(*invals_out + *ninvals_out, msgs_new,
    3523             :                nmsgs_new * sizeof(SharedInvalidationMessage));
    3524        7420 :         *ninvals_out += nmsgs_new;
    3525             :     }
    3526        9798 : }
    3527             : 
    3528             : /*
    3529             :  * Accumulate the invalidations for executing them later.
    3530             :  *
    3531             :  * This needs to be called for each XLOG_XACT_INVALIDATIONS message and
    3532             :  * accumulates all the invalidation messages in the toplevel transaction, if
    3533             :  * available, otherwise in the current transaction, as well as in the form of
    3534             :  * change in reorder buffer.  We require to record it in form of the change
    3535             :  * so that we can execute only the required invalidations instead of executing
    3536             :  * all the invalidations on each CommandId increment.  We also need to
    3537             :  * accumulate these in the txn buffer because in some cases where we skip
    3538             :  * processing the transaction (see ReorderBufferForget), we need to execute
    3539             :  * all the invalidations together.
    3540             :  */
    3541             : void
    3542        9742 : ReorderBufferAddInvalidations(ReorderBuffer *rb, TransactionId xid,
    3543             :                               XLogRecPtr lsn, Size nmsgs,
    3544             :                               SharedInvalidationMessage *msgs)
    3545             : {
    3546             :     ReorderBufferTXN *txn;
    3547             :     MemoryContext oldcontext;
    3548             : 
    3549        9742 :     txn = ReorderBufferTXNByXid(rb, xid, true, NULL, lsn, true);
    3550             : 
    3551        9742 :     oldcontext = MemoryContextSwitchTo(rb->context);
    3552             : 
    3553             :     /*
    3554             :      * Collect all the invalidations under the top transaction, if available,
    3555             :      * so that we can execute them all together.  See comments atop this
    3556             :      * function.
    3557             :      */
    3558        9742 :     txn = rbtxn_get_toptxn(txn);
    3559             : 
    3560             :     Assert(nmsgs > 0);
    3561             : 
    3562        9742 :     ReorderBufferAccumulateInvalidations(&txn->invalidations,
    3563             :                                          &txn->ninvalidations,
    3564             :                                          msgs, nmsgs);
    3565             : 
    3566        9742 :     ReorderBufferQueueInvalidations(rb, xid, lsn, nmsgs, msgs);
    3567             : 
    3568        9742 :     MemoryContextSwitchTo(oldcontext);
    3569        9742 : }
    3570             : 
    3571             : /*
    3572             :  * Accumulate the invalidations distributed by other committed transactions
    3573             :  * for executing them later.
    3574             :  *
    3575             :  * This function is similar to ReorderBufferAddInvalidations() but stores
    3576             :  * the given inval messages to the txn->invalidations_distributed with the
    3577             :  * overflow check.
    3578             :  *
    3579             :  * This needs to be called by committed transactions to distribute their
    3580             :  * inval messages to in-progress transactions.
    3581             :  */
    3582             : void
    3583          56 : ReorderBufferAddDistributedInvalidations(ReorderBuffer *rb, TransactionId xid,
    3584             :                                          XLogRecPtr lsn, Size nmsgs,
    3585             :                                          SharedInvalidationMessage *msgs)
    3586             : {
    3587             :     ReorderBufferTXN *txn;
    3588             :     MemoryContext oldcontext;
    3589             : 
    3590          56 :     txn = ReorderBufferTXNByXid(rb, xid, true, NULL, lsn, true);
    3591             : 
    3592          56 :     oldcontext = MemoryContextSwitchTo(rb->context);
    3593             : 
    3594             :     /*
    3595             :      * Collect all the invalidations under the top transaction, if available,
    3596             :      * so that we can execute them all together.  See comments
    3597             :      * ReorderBufferAddInvalidations.
    3598             :      */
    3599          56 :     txn = rbtxn_get_toptxn(txn);
    3600             : 
    3601             :     Assert(nmsgs > 0);
    3602             : 
    3603          56 :     if (!rbtxn_distr_inval_overflowed(txn))
    3604             :     {
    3605             :         /*
    3606             :          * Check the transaction has enough space for storing distributed
    3607             :          * invalidation messages.
    3608             :          */
    3609          56 :         if (txn->ninvalidations_distributed + nmsgs >= MAX_DISTR_INVAL_MSG_PER_TXN)
    3610             :         {
    3611             :             /*
    3612             :              * Mark the invalidation message as overflowed and free up the
    3613             :              * messages accumulated so far.
    3614             :              */
    3615           0 :             txn->txn_flags |= RBTXN_DISTR_INVAL_OVERFLOWED;
    3616             : 
    3617           0 :             if (txn->invalidations_distributed)
    3618             :             {
    3619           0 :                 pfree(txn->invalidations_distributed);
    3620           0 :                 txn->invalidations_distributed = NULL;
    3621           0 :                 txn->ninvalidations_distributed = 0;
    3622             :             }
    3623             :         }
    3624             :         else
    3625          56 :             ReorderBufferAccumulateInvalidations(&txn->invalidations_distributed,
    3626             :                                                  &txn->ninvalidations_distributed,
    3627             :                                                  msgs, nmsgs);
    3628             :     }
    3629             : 
    3630             :     /* Queue the invalidation messages into the transaction */
    3631          56 :     ReorderBufferQueueInvalidations(rb, xid, lsn, nmsgs, msgs);
    3632             : 
    3633          56 :     MemoryContextSwitchTo(oldcontext);
    3634          56 : }
    3635             : 
    3636             : /*
    3637             :  * Apply all invalidations we know. Possibly we only need parts at this point
    3638             :  * in the changestream but we don't know which those are.
    3639             :  */
    3640             : static void
    3641       12590 : ReorderBufferExecuteInvalidations(uint32 nmsgs, SharedInvalidationMessage *msgs)
    3642             : {
    3643             :     int         i;
    3644             : 
    3645       97018 :     for (i = 0; i < nmsgs; i++)
    3646       84428 :         LocalExecuteInvalidationMessage(&msgs[i]);
    3647       12590 : }
    3648             : 
    3649             : /*
    3650             :  * Mark a transaction as containing catalog changes
    3651             :  */
    3652             : void
    3653       56378 : ReorderBufferXidSetCatalogChanges(ReorderBuffer *rb, TransactionId xid,
    3654             :                                   XLogRecPtr lsn)
    3655             : {
    3656             :     ReorderBufferTXN *txn;
    3657             : 
    3658       56378 :     txn = ReorderBufferTXNByXid(rb, xid, true, NULL, lsn, true);
    3659             : 
    3660       56378 :     if (!rbtxn_has_catalog_changes(txn))
    3661             :     {
    3662        2408 :         txn->txn_flags |= RBTXN_HAS_CATALOG_CHANGES;
    3663        2408 :         dclist_push_tail(&rb->catchange_txns, &txn->catchange_node);
    3664             :     }
    3665             : 
    3666             :     /*
    3667             :      * Mark top-level transaction as having catalog changes too if one of its
    3668             :      * children has so that the ReorderBufferBuildTupleCidHash can
    3669             :      * conveniently check just top-level transaction and decide whether to
    3670             :      * build the hash table or not.
    3671             :      */
    3672       56378 :     if (rbtxn_is_subtxn(txn))
    3673             :     {
    3674        1792 :         ReorderBufferTXN *toptxn = rbtxn_get_toptxn(txn);
    3675             : 
    3676        1792 :         if (!rbtxn_has_catalog_changes(toptxn))
    3677             :         {
    3678          40 :             toptxn->txn_flags |= RBTXN_HAS_CATALOG_CHANGES;
    3679          40 :             dclist_push_tail(&rb->catchange_txns, &toptxn->catchange_node);
    3680             :         }
    3681             :     }
    3682       56378 : }
    3683             : 
    3684             : /*
    3685             :  * Return palloc'ed array of the transactions that have changed catalogs.
    3686             :  * The returned array is sorted in xidComparator order.
    3687             :  *
    3688             :  * The caller must free the returned array when done with it.
    3689             :  */
    3690             : TransactionId *
    3691         618 : ReorderBufferGetCatalogChangesXacts(ReorderBuffer *rb)
    3692             : {
    3693             :     dlist_iter  iter;
    3694         618 :     TransactionId *xids = NULL;
    3695         618 :     size_t      xcnt = 0;
    3696             : 
    3697             :     /* Quick return if the list is empty */
    3698         618 :     if (dclist_count(&rb->catchange_txns) == 0)
    3699         598 :         return NULL;
    3700             : 
    3701             :     /* Initialize XID array */
    3702          20 :     xids = palloc_array(TransactionId, dclist_count(&rb->catchange_txns));
    3703          46 :     dclist_foreach(iter, &rb->catchange_txns)
    3704             :     {
    3705          26 :         ReorderBufferTXN *txn = dclist_container(ReorderBufferTXN,
    3706             :                                                  catchange_node,
    3707             :                                                  iter.cur);
    3708             : 
    3709             :         Assert(rbtxn_has_catalog_changes(txn));
    3710             : 
    3711          26 :         xids[xcnt++] = txn->xid;
    3712             :     }
    3713             : 
    3714          20 :     qsort(xids, xcnt, sizeof(TransactionId), xidComparator);
    3715             : 
    3716             :     Assert(xcnt == dclist_count(&rb->catchange_txns));
    3717          20 :     return xids;
    3718             : }
    3719             : 
    3720             : /*
    3721             :  * Query whether a transaction is already *known* to contain catalog
    3722             :  * changes. This can be wrong until directly before the commit!
    3723             :  */
    3724             : bool
    3725        8702 : ReorderBufferXidHasCatalogChanges(ReorderBuffer *rb, TransactionId xid)
    3726             : {
    3727             :     ReorderBufferTXN *txn;
    3728             : 
    3729        8702 :     txn = ReorderBufferTXNByXid(rb, xid, false, NULL, InvalidXLogRecPtr,
    3730             :                                 false);
    3731        8702 :     if (txn == NULL)
    3732        1306 :         return false;
    3733             : 
    3734        7396 :     return rbtxn_has_catalog_changes(txn);
    3735             : }
    3736             : 
    3737             : /*
    3738             :  * ReorderBufferXidHasBaseSnapshot
    3739             :  *      Have we already set the base snapshot for the given txn/subtxn?
    3740             :  */
    3741             : bool
    3742     3111740 : ReorderBufferXidHasBaseSnapshot(ReorderBuffer *rb, TransactionId xid)
    3743             : {
    3744             :     ReorderBufferTXN *txn;
    3745             : 
    3746     3111740 :     txn = ReorderBufferTXNByXid(rb, xid, false,
    3747             :                                 NULL, InvalidXLogRecPtr, false);
    3748             : 
    3749             :     /* transaction isn't known yet, ergo no snapshot */
    3750     3111740 :     if (txn == NULL)
    3751           6 :         return false;
    3752             : 
    3753             :     /* a known subtxn? operate on top-level txn instead */
    3754     3111734 :     if (rbtxn_is_known_subxact(txn))
    3755      984064 :         txn = ReorderBufferTXNByXid(rb, txn->toplevel_xid, false,
    3756             :                                     NULL, InvalidXLogRecPtr, false);
    3757             : 
    3758     3111734 :     return txn->base_snapshot != NULL;
    3759             : }
    3760             : 
    3761             : 
    3762             : /*
    3763             :  * ---------------------------------------
    3764             :  * Disk serialization support
    3765             :  * ---------------------------------------
    3766             :  */
    3767             : 
    3768             : /*
    3769             :  * Ensure the IO buffer is >= sz.
    3770             :  */
    3771             : static void
    3772     6022290 : ReorderBufferSerializeReserve(ReorderBuffer *rb, Size sz)
    3773             : {
    3774     6022290 :     if (!rb->outbufsize)
    3775             :     {
    3776          98 :         rb->outbuf = MemoryContextAlloc(rb->context, sz);
    3777          98 :         rb->outbufsize = sz;
    3778             :     }
    3779     6022192 :     else if (rb->outbufsize < sz)
    3780             :     {
    3781         584 :         rb->outbuf = repalloc(rb->outbuf, sz);
    3782         584 :         rb->outbufsize = sz;
    3783             :     }
    3784     6022290 : }
    3785             : 
    3786             : 
    3787             : /* Compare two transactions by size */
    3788             : static int
    3789      676396 : ReorderBufferTXNSizeCompare(const pairingheap_node *a, const pairingheap_node *b, void *arg)
    3790             : {
    3791      676396 :     const ReorderBufferTXN *ta = pairingheap_const_container(ReorderBufferTXN, txn_node, a);
    3792      676396 :     const ReorderBufferTXN *tb = pairingheap_const_container(ReorderBufferTXN, txn_node, b);
    3793             : 
    3794      676396 :     if (ta->size < tb->size)
    3795      483192 :         return -1;
    3796      193204 :     if (ta->size > tb->size)
    3797      191360 :         return 1;
    3798        1844 :     return 0;
    3799             : }
    3800             : 
    3801             : /*
    3802             :  * Find the largest transaction (toplevel or subxact) to evict (spill to disk).
    3803             :  */
    3804             : static ReorderBufferTXN *
    3805        7976 : ReorderBufferLargestTXN(ReorderBuffer *rb)
    3806             : {
    3807             :     ReorderBufferTXN *largest;
    3808             : 
    3809             :     /* Get the largest transaction from the max-heap */
    3810        7976 :     largest = pairingheap_container(ReorderBufferTXN, txn_node,
    3811             :                                     pairingheap_first(rb->txn_heap));
    3812             : 
    3813             :     Assert(largest);
    3814             :     Assert(largest->size > 0);
    3815             :     Assert(largest->size <= rb->size);
    3816             : 
    3817        7976 :     return largest;
    3818             : }
    3819             : 
    3820             : /*
    3821             :  * Find the largest streamable (and non-aborted) toplevel transaction to evict
    3822             :  * (by streaming).
    3823             :  *
    3824             :  * This can be seen as an optimized version of ReorderBufferLargestTXN, which
    3825             :  * should give us the same transaction (because we don't update memory account
    3826             :  * for subtransaction with streaming, so it's always 0). But we can simply
    3827             :  * iterate over the limited number of toplevel transactions that have a base
    3828             :  * snapshot. There is no use of selecting a transaction that doesn't have base
    3829             :  * snapshot because we don't decode such transactions.  Also, we do not select
    3830             :  * the transaction which doesn't have any streamable change.
    3831             :  *
    3832             :  * Note that, we skip transactions that contain incomplete changes. There
    3833             :  * is a scope of optimization here such that we can select the largest
    3834             :  * transaction which has incomplete changes.  But that will make the code and
    3835             :  * design quite complex and that might not be worth the benefit.  If we plan to
    3836             :  * stream the transactions that contain incomplete changes then we need to
    3837             :  * find a way to partially stream/truncate the transaction changes in-memory
    3838             :  * and build a mechanism to partially truncate the spilled files.
    3839             :  * Additionally, whenever we partially stream the transaction we need to
    3840             :  * maintain the last streamed lsn and next time we need to restore from that
    3841             :  * segment and the offset in WAL.  As we stream the changes from the top
    3842             :  * transaction and restore them subtransaction wise, we need to even remember
    3843             :  * the subxact from where we streamed the last change.
    3844             :  */
    3845             : static ReorderBufferTXN *
    3846        1600 : ReorderBufferLargestStreamableTopTXN(ReorderBuffer *rb)
    3847             : {
    3848             :     dlist_iter  iter;
    3849        1600 :     Size        largest_size = 0;
    3850        1600 :     ReorderBufferTXN *largest = NULL;
    3851             : 
    3852             :     /* Find the largest top-level transaction having a base snapshot. */
    3853        3424 :     dlist_foreach(iter, &rb->txns_by_base_snapshot_lsn)
    3854             :     {
    3855             :         ReorderBufferTXN *txn;
    3856             : 
    3857        1824 :         txn = dlist_container(ReorderBufferTXN, base_snapshot_node, iter.cur);
    3858             : 
    3859             :         /* must not be a subtxn */
    3860             :         Assert(!rbtxn_is_known_subxact(txn));
    3861             :         /* base_snapshot must be set */
    3862             :         Assert(txn->base_snapshot != NULL);
    3863             : 
    3864             :         /* Don't consider these kinds of transactions for eviction. */
    3865        1824 :         if (rbtxn_has_partial_change(txn) ||
    3866        1530 :             !rbtxn_has_streamable_change(txn) ||
    3867        1470 :             rbtxn_is_aborted(txn))
    3868         354 :             continue;
    3869             : 
    3870             :         /* Find the largest of the eviction candidates. */
    3871        1470 :         if ((largest == NULL || txn->total_size > largest_size) &&
    3872        1470 :             (txn->total_size > 0))
    3873             :         {
    3874        1378 :             largest = txn;
    3875        1378 :             largest_size = txn->total_size;
    3876             :         }
    3877             :     }
    3878             : 
    3879        1600 :     return largest;
    3880             : }
    3881             : 
    3882             : /*
    3883             :  * Check whether the logical_decoding_work_mem limit was reached, and if yes
    3884             :  * pick the largest (sub)transaction at-a-time to evict and spill its changes to
    3885             :  * disk or send to the output plugin until we reach under the memory limit.
    3886             :  *
    3887             :  * If debug_logical_replication_streaming is set to "immediate", stream or
    3888             :  * serialize the changes immediately.
    3889             :  *
    3890             :  * XXX At this point we select the transactions until we reach under the memory
    3891             :  * limit, but we might also adapt a more elaborate eviction strategy - for example
    3892             :  * evicting enough transactions to free certain fraction (e.g. 50%) of the memory
    3893             :  * limit.
    3894             :  */
    3895             : static void
    3896     3129998 : ReorderBufferCheckMemoryLimit(ReorderBuffer *rb)
    3897             : {
    3898             :     ReorderBufferTXN *txn;
    3899     3129998 :     bool        update_stats = true;
    3900             : 
    3901     3129998 :     if (rb->size >= logical_decoding_work_mem * (Size) 1024)
    3902             :     {
    3903             :         /*
    3904             :          * Update the statistics as the memory usage has reached the limit. We
    3905             :          * report the statistics update later in this function since we can
    3906             :          * update the slot statistics altogether while streaming or
    3907             :          * serializing transactions in most cases.
    3908             :          */
    3909        7010 :         rb->memExceededCount += 1;
    3910             :     }
    3911     3122988 :     else if (debug_logical_replication_streaming == DEBUG_LOGICAL_REP_STREAMING_BUFFERED)
    3912             :     {
    3913             :         /*
    3914             :          * Bail out if debug_logical_replication_streaming is buffered and we
    3915             :          * haven't exceeded the memory limit.
    3916             :          */
    3917     3120770 :         return;
    3918             :     }
    3919             : 
    3920             :     /*
    3921             :      * If debug_logical_replication_streaming is immediate, loop until there's
    3922             :      * no change. Otherwise, loop until we reach under the memory limit. One
    3923             :      * might think that just by evicting the largest (sub)transaction we will
    3924             :      * come under the memory limit based on assumption that the selected
    3925             :      * transaction is at least as large as the most recent change (which
    3926             :      * caused us to go over the memory limit). However, that is not true
    3927             :      * because a user can reduce the logical_decoding_work_mem to a smaller
    3928             :      * value before the most recent change.
    3929             :      */
    3930       18450 :     while (rb->size >= logical_decoding_work_mem * (Size) 1024 ||
    3931       11440 :            (debug_logical_replication_streaming == DEBUG_LOGICAL_REP_STREAMING_IMMEDIATE &&
    3932        4430 :             rb->size > 0))
    3933             :     {
    3934             :         /*
    3935             :          * Pick the largest non-aborted transaction and evict it from memory
    3936             :          * by streaming, if possible.  Otherwise, spill to disk.
    3937             :          */
    3938       10822 :         if (ReorderBufferCanStartStreaming(rb) &&
    3939        1600 :             (txn = ReorderBufferLargestStreamableTopTXN(rb)) != NULL)
    3940             :         {
    3941             :             /* we know there has to be one, because the size is not zero */
    3942             :             Assert(txn && rbtxn_is_toptxn(txn));
    3943             :             Assert(txn->total_size > 0);
    3944             :             Assert(rb->size >= txn->total_size);
    3945             : 
    3946             :             /* skip the transaction if aborted */
    3947        1246 :             if (ReorderBufferCheckAndTruncateAbortedTXN(rb, txn))
    3948           0 :                 continue;
    3949             : 
    3950        1246 :             ReorderBufferStreamTXN(rb, txn);
    3951             :         }
    3952             :         else
    3953             :         {
    3954             :             /*
    3955             :              * Pick the largest transaction (or subtransaction) and evict it
    3956             :              * from memory by serializing it to disk.
    3957             :              */
    3958        7976 :             txn = ReorderBufferLargestTXN(rb);
    3959             : 
    3960             :             /* we know there has to be one, because the size is not zero */
    3961             :             Assert(txn);
    3962             :             Assert(txn->size > 0);
    3963             :             Assert(rb->size >= txn->size);
    3964             : 
    3965             :             /* skip the transaction if aborted */
    3966        7976 :             if (ReorderBufferCheckAndTruncateAbortedTXN(rb, txn))
    3967          18 :                 continue;
    3968             : 
    3969        7958 :             ReorderBufferSerializeTXN(rb, txn);
    3970             :         }
    3971             : 
    3972             :         /*
    3973             :          * After eviction, the transaction should have no entries in memory,
    3974             :          * and should use 0 bytes for changes.
    3975             :          */
    3976             :         Assert(txn->size == 0);
    3977             :         Assert(txn->nentries_mem == 0);
    3978             : 
    3979             :         /*
    3980             :          * We've reported the memExceededCount update while streaming or
    3981             :          * serializing the transaction.
    3982             :          */
    3983        9204 :         update_stats = false;
    3984             :     }
    3985             : 
    3986        9228 :     if (update_stats)
    3987          24 :         UpdateDecodingStats((LogicalDecodingContext *) rb->private_data);
    3988             : 
    3989             :     /* We must be under the memory limit now. */
    3990             :     Assert(rb->size < logical_decoding_work_mem * (Size) 1024);
    3991             : }
    3992             : 
    3993             : /*
    3994             :  * Spill data of a large transaction (and its subtransactions) to disk.
    3995             :  */
    3996             : static void
    3997        8576 : ReorderBufferSerializeTXN(ReorderBuffer *rb, ReorderBufferTXN *txn)
    3998             : {
    3999             :     dlist_iter  subtxn_i;
    4000             :     dlist_mutable_iter change_i;
    4001        8576 :     int         fd = -1;
    4002        8576 :     XLogSegNo   curOpenSegNo = 0;
    4003        8576 :     Size        spilled = 0;
    4004        8576 :     Size        size = txn->size;
    4005             : 
    4006        8576 :     elog(DEBUG2, "spill %u changes in XID %u to disk",
    4007             :          (uint32) txn->nentries_mem, txn->xid);
    4008             : 
    4009             :     /* do the same to all child TXs */
    4010        9114 :     dlist_foreach(subtxn_i, &txn->subtxns)
    4011             :     {
    4012             :         ReorderBufferTXN *subtxn;
    4013             : 
    4014         538 :         subtxn = dlist_container(ReorderBufferTXN, node, subtxn_i.cur);
    4015         538 :         ReorderBufferSerializeTXN(rb, subtxn);
    4016             :     }
    4017             : 
    4018             :     /* serialize changestream */
    4019     2680258 :     dlist_foreach_modify(change_i, &txn->changes)
    4020             :     {
    4021             :         ReorderBufferChange *change;
    4022             : 
    4023     2671682 :         change = dlist_container(ReorderBufferChange, node, change_i.cur);
    4024             : 
    4025             :         /*
    4026             :          * store in segment in which it belongs by start lsn, don't split over
    4027             :          * multiple segments tho
    4028             :          */
    4029     2671682 :         if (fd == -1 ||
    4030     2663610 :             !XLByteInSeg(change->lsn, curOpenSegNo, wal_segment_size))
    4031             :         {
    4032             :             char        path[MAXPGPATH];
    4033             : 
    4034        8076 :             if (fd != -1)
    4035           4 :                 CloseTransientFile(fd);
    4036             : 
    4037        8076 :             XLByteToSeg(change->lsn, curOpenSegNo, wal_segment_size);
    4038             : 
    4039             :             /*
    4040             :              * No need to care about TLIs here, only used during a single run,
    4041             :              * so each LSN only maps to a specific WAL record.
    4042             :              */
    4043        8076 :             ReorderBufferSerializedPath(path, MyReplicationSlot, txn->xid,
    4044             :                                         curOpenSegNo);
    4045             : 
    4046             :             /* open segment, create it if necessary */
    4047        8076 :             fd = OpenTransientFile(path,
    4048             :                                    O_CREAT | O_WRONLY | O_APPEND | PG_BINARY);
    4049             : 
    4050        8076 :             if (fd < 0)
    4051           0 :                 ereport(ERROR,
    4052             :                         (errcode_for_file_access(),
    4053             :                          errmsg("could not open file \"%s\": %m", path)));
    4054             :         }
    4055             : 
    4056     2671682 :         ReorderBufferSerializeChange(rb, txn, fd, change);
    4057     2671682 :         dlist_delete(&change->node);
    4058     2671682 :         ReorderBufferFreeChange(rb, change, false);
    4059             : 
    4060     2671682 :         spilled++;
    4061             :     }
    4062             : 
    4063             :     /* Update the memory counter */
    4064        8576 :     ReorderBufferChangeMemoryUpdate(rb, NULL, txn, false, size);
    4065             : 
    4066             :     /* update the statistics iff we have spilled anything */
    4067        8576 :     if (spilled)
    4068             :     {
    4069        8072 :         rb->spillCount += 1;
    4070        8072 :         rb->spillBytes += size;
    4071             : 
    4072             :         /* don't consider already serialized transactions */
    4073        8072 :         rb->spillTxns += (rbtxn_is_serialized(txn) || rbtxn_is_serialized_clear(txn)) ? 0 : 1;
    4074             : 
    4075             :         /* update the decoding stats */
    4076        8072 :         UpdateDecodingStats((LogicalDecodingContext *) rb->private_data);
    4077             :     }
    4078             : 
    4079             :     Assert(spilled == txn->nentries_mem);
    4080             :     Assert(dlist_is_empty(&txn->changes));
    4081        8576 :     txn->nentries_mem = 0;
    4082        8576 :     txn->txn_flags |= RBTXN_IS_SERIALIZED;
    4083             : 
    4084        8576 :     if (fd != -1)
    4085        8072 :         CloseTransientFile(fd);
    4086        8576 : }
    4087             : 
    4088             : /*
    4089             :  * Serialize individual change to disk.
    4090             :  */
    4091             : static void
    4092     2671682 : ReorderBufferSerializeChange(ReorderBuffer *rb, ReorderBufferTXN *txn,
    4093             :                              int fd, ReorderBufferChange *change)
    4094             : {
    4095             :     ReorderBufferDiskChange *ondisk;
    4096     2671682 :     Size        sz = sizeof(ReorderBufferDiskChange);
    4097             : 
    4098     2671682 :     ReorderBufferSerializeReserve(rb, sz);
    4099             : 
    4100     2671682 :     ondisk = (ReorderBufferDiskChange *) rb->outbuf;
    4101     2671682 :     memcpy(&ondisk->change, change, sizeof(ReorderBufferChange));
    4102             : 
    4103     2671682 :     switch (change->action)
    4104             :     {
    4105             :             /* fall through these, they're all similar enough */
    4106     2636706 :         case REORDER_BUFFER_CHANGE_INSERT:
    4107             :         case REORDER_BUFFER_CHANGE_UPDATE:
    4108             :         case REORDER_BUFFER_CHANGE_DELETE:
    4109             :         case REORDER_BUFFER_CHANGE_INTERNAL_SPEC_INSERT:
    4110             :             {
    4111             :                 char       *data;
    4112             :                 HeapTuple   oldtup,
    4113             :                             newtup;
    4114     2636706 :                 Size        oldlen = 0;
    4115     2636706 :                 Size        newlen = 0;
    4116             : 
    4117     2636706 :                 oldtup = change->data.tp.oldtuple;
    4118     2636706 :                 newtup = change->data.tp.newtuple;
    4119             : 
    4120     2636706 :                 if (oldtup)
    4121             :                 {
    4122      230290 :                     sz += sizeof(HeapTupleData);
    4123      230290 :                     oldlen = oldtup->t_len;
    4124      230290 :                     sz += oldlen;
    4125             :                 }
    4126             : 
    4127     2636706 :                 if (newtup)
    4128             :                 {
    4129     2298986 :                     sz += sizeof(HeapTupleData);
    4130     2298986 :                     newlen = newtup->t_len;
    4131     2298986 :                     sz += newlen;
    4132             :                 }
    4133             : 
    4134             :                 /* make sure we have enough space */
    4135     2636706 :                 ReorderBufferSerializeReserve(rb, sz);
    4136             : 
    4137     2636706 :                 data = ((char *) rb->outbuf) + sizeof(ReorderBufferDiskChange);
    4138             :                 /* might have been reallocated above */
    4139     2636706 :                 ondisk = (ReorderBufferDiskChange *) rb->outbuf;
    4140             : 
    4141     2636706 :                 if (oldlen)
    4142             :                 {
    4143      230290 :                     memcpy(data, oldtup, sizeof(HeapTupleData));
    4144      230290 :                     data += sizeof(HeapTupleData);
    4145             : 
    4146      230290 :                     memcpy(data, oldtup->t_data, oldlen);
    4147      230290 :                     data += oldlen;
    4148             :                 }
    4149             : 
    4150     2636706 :                 if (newlen)
    4151             :                 {
    4152     2298986 :                     memcpy(data, newtup, sizeof(HeapTupleData));
    4153     2298986 :                     data += sizeof(HeapTupleData);
    4154             : 
    4155     2298986 :                     memcpy(data, newtup->t_data, newlen);
    4156     2298986 :                     data += newlen;
    4157             :                 }
    4158     2636706 :                 break;
    4159             :             }
    4160          26 :         case REORDER_BUFFER_CHANGE_MESSAGE:
    4161             :             {
    4162             :                 char       *data;
    4163          26 :                 Size        prefix_size = strlen(change->data.msg.prefix) + 1;
    4164             : 
    4165          26 :                 sz += prefix_size + change->data.msg.message_size +
    4166             :                     sizeof(Size) + sizeof(Size);
    4167          26 :                 ReorderBufferSerializeReserve(rb, sz);
    4168             : 
    4169          26 :                 data = ((char *) rb->outbuf) + sizeof(ReorderBufferDiskChange);
    4170             : 
    4171             :                 /* might have been reallocated above */
    4172          26 :                 ondisk = (ReorderBufferDiskChange *) rb->outbuf;
    4173             : 
    4174             :                 /* write the prefix including the size */
    4175          26 :                 memcpy(data, &prefix_size, sizeof(Size));
    4176          26 :                 data += sizeof(Size);
    4177          26 :                 memcpy(data, change->data.msg.prefix,
    4178             :                        prefix_size);
    4179          26 :                 data += prefix_size;
    4180             : 
    4181             :                 /* write the message including the size */
    4182          26 :                 memcpy(data, &change->data.msg.message_size, sizeof(Size));
    4183          26 :                 data += sizeof(Size);
    4184          26 :                 memcpy(data, change->data.msg.message,
    4185             :                        change->data.msg.message_size);
    4186          26 :                 data += change->data.msg.message_size;
    4187             : 
    4188          26 :                 break;
    4189             :             }
    4190         308 :         case REORDER_BUFFER_CHANGE_INVALIDATION:
    4191             :             {
    4192             :                 char       *data;
    4193         308 :                 Size        inval_size = sizeof(SharedInvalidationMessage) *
    4194         308 :                     change->data.inval.ninvalidations;
    4195             : 
    4196         308 :                 sz += inval_size;
    4197             : 
    4198         308 :                 ReorderBufferSerializeReserve(rb, sz);
    4199         308 :                 data = ((char *) rb->outbuf) + sizeof(ReorderBufferDiskChange);
    4200             : 
    4201             :                 /* might have been reallocated above */
    4202         308 :                 ondisk = (ReorderBufferDiskChange *) rb->outbuf;
    4203         308 :                 memcpy(data, change->data.inval.invalidations, inval_size);
    4204         308 :                 data += inval_size;
    4205             : 
    4206         308 :                 break;
    4207             :             }
    4208          16 :         case REORDER_BUFFER_CHANGE_INTERNAL_SNAPSHOT:
    4209             :             {
    4210             :                 Snapshot    snap;
    4211             :                 char       *data;
    4212             : 
    4213          16 :                 snap = change->data.snapshot;
    4214             : 
    4215          16 :                 sz += sizeof(SnapshotData) +
    4216          16 :                     sizeof(TransactionId) * snap->xcnt +
    4217          16 :                     sizeof(TransactionId) * snap->subxcnt;
    4218             : 
    4219             :                 /* make sure we have enough space */
    4220          16 :                 ReorderBufferSerializeReserve(rb, sz);
    4221          16 :                 data = ((char *) rb->outbuf) + sizeof(ReorderBufferDiskChange);
    4222             :                 /* might have been reallocated above */
    4223          16 :                 ondisk = (ReorderBufferDiskChange *) rb->outbuf;
    4224             : 
    4225          16 :                 memcpy(data, snap, sizeof(SnapshotData));
    4226          16 :                 data += sizeof(SnapshotData);
    4227             : 
    4228          16 :                 if (snap->xcnt)
    4229             :                 {
    4230          16 :                     memcpy(data, snap->xip,
    4231          16 :                            sizeof(TransactionId) * snap->xcnt);
    4232          16 :                     data += sizeof(TransactionId) * snap->xcnt;
    4233             :                 }
    4234             : 
    4235          16 :                 if (snap->subxcnt)
    4236             :                 {
    4237           0 :                     memcpy(data, snap->subxip,
    4238           0 :                            sizeof(TransactionId) * snap->subxcnt);
    4239           0 :                     data += sizeof(TransactionId) * snap->subxcnt;
    4240             :                 }
    4241          16 :                 break;
    4242             :             }
    4243           4 :         case REORDER_BUFFER_CHANGE_TRUNCATE:
    4244             :             {
    4245             :                 Size        size;
    4246             :                 char       *data;
    4247             : 
    4248             :                 /* account for the OIDs of truncated relations */
    4249           4 :                 size = sizeof(Oid) * change->data.truncate.nrelids;
    4250           4 :                 sz += size;
    4251             : 
    4252             :                 /* make sure we have enough space */
    4253           4 :                 ReorderBufferSerializeReserve(rb, sz);
    4254             : 
    4255           4 :                 data = ((char *) rb->outbuf) + sizeof(ReorderBufferDiskChange);
    4256             :                 /* might have been reallocated above */
    4257           4 :                 ondisk = (ReorderBufferDiskChange *) rb->outbuf;
    4258             : 
    4259           4 :                 memcpy(data, change->data.truncate.relids, size);
    4260           4 :                 data += size;
    4261             : 
    4262           4 :                 break;
    4263             :             }
    4264       34622 :         case REORDER_BUFFER_CHANGE_INTERNAL_SPEC_CONFIRM:
    4265             :         case REORDER_BUFFER_CHANGE_INTERNAL_SPEC_ABORT:
    4266             :         case REORDER_BUFFER_CHANGE_INTERNAL_COMMAND_ID:
    4267             :         case REORDER_BUFFER_CHANGE_INTERNAL_TUPLECID:
    4268             :             /* ReorderBufferChange contains everything important */
    4269       34622 :             break;
    4270             :     }
    4271             : 
    4272     2671682 :     ondisk->size = sz;
    4273             : 
    4274     2671682 :     errno = 0;
    4275     2671682 :     pgstat_report_wait_start(WAIT_EVENT_REORDER_BUFFER_WRITE);
    4276     2671682 :     if (write(fd, rb->outbuf, ondisk->size) != ondisk->size)
    4277             :     {
    4278           0 :         int         save_errno = errno;
    4279             : 
    4280           0 :         CloseTransientFile(fd);
    4281             : 
    4282             :         /* if write didn't set errno, assume problem is no disk space */
    4283           0 :         errno = save_errno ? save_errno : ENOSPC;
    4284           0 :         ereport(ERROR,
    4285             :                 (errcode_for_file_access(),
    4286             :                  errmsg("could not write to data file for XID %u: %m",
    4287             :                         txn->xid)));
    4288             :     }
    4289     2671682 :     pgstat_report_wait_end();
    4290             : 
    4291             :     /*
    4292             :      * Keep the transaction's final_lsn up to date with each change we send to
    4293             :      * disk, so that ReorderBufferRestoreCleanup works correctly.  (We used to
    4294             :      * only do this on commit and abort records, but that doesn't work if a
    4295             :      * system crash leaves a transaction without its abort record).
    4296             :      *
    4297             :      * Make sure not to move it backwards.
    4298             :      */
    4299     2671682 :     if (txn->final_lsn < change->lsn)
    4300     2662716 :         txn->final_lsn = change->lsn;
    4301             : 
    4302             :     Assert(ondisk->change.action == change->action);
    4303     2671682 : }
    4304             : 
    4305             : /* Returns true, if the output plugin supports streaming, false, otherwise. */
    4306             : static inline bool
    4307     3848830 : ReorderBufferCanStream(ReorderBuffer *rb)
    4308             : {
    4309     3848830 :     LogicalDecodingContext *ctx = rb->private_data;
    4310             : 
    4311     3848830 :     return ctx->streaming;
    4312             : }
    4313             : 
    4314             : /* Returns true, if the streaming can be started now, false, otherwise. */
    4315             : static inline bool
    4316      718832 : ReorderBufferCanStartStreaming(ReorderBuffer *rb)
    4317             : {
    4318      718832 :     LogicalDecodingContext *ctx = rb->private_data;
    4319      718832 :     SnapBuild  *builder = ctx->snapshot_builder;
    4320             : 
    4321             :     /* We can't start streaming unless a consistent state is reached. */
    4322      718832 :     if (SnapBuildCurrentState(builder) < SNAPBUILD_CONSISTENT)
    4323           0 :         return false;
    4324             : 
    4325             :     /*
    4326             :      * We can't start streaming immediately even if the streaming is enabled
    4327             :      * because we previously decoded this transaction and now just are
    4328             :      * restarting.
    4329             :      */
    4330      718832 :     if (ReorderBufferCanStream(rb) &&
    4331      713536 :         !SnapBuildXactNeedsSkip(builder, ctx->reader->ReadRecPtr))
    4332      339698 :         return true;
    4333             : 
    4334      379134 :     return false;
    4335             : }
    4336             : 
    4337             : /*
    4338             :  * Send data of a large transaction (and its subtransactions) to the
    4339             :  * output plugin, but using the stream API.
    4340             :  */
    4341             : static void
    4342        1392 : ReorderBufferStreamTXN(ReorderBuffer *rb, ReorderBufferTXN *txn)
    4343             : {
    4344             :     Snapshot    snapshot_now;
    4345             :     CommandId   command_id;
    4346             :     Size        stream_bytes;
    4347             :     bool        txn_is_streamed;
    4348             : 
    4349             :     /* We can never reach here for a subtransaction. */
    4350             :     Assert(rbtxn_is_toptxn(txn));
    4351             : 
    4352             :     /*
    4353             :      * We can't make any assumptions about base snapshot here, similar to what
    4354             :      * ReorderBufferCommit() does. That relies on base_snapshot getting
    4355             :      * transferred from subxact in ReorderBufferCommitChild(), but that was
    4356             :      * not yet called as the transaction is in-progress.
    4357             :      *
    4358             :      * So just walk the subxacts and use the same logic here. But we only need
    4359             :      * to do that once, when the transaction is streamed for the first time.
    4360             :      * After that we need to reuse the snapshot from the previous run.
    4361             :      *
    4362             :      * Unlike DecodeCommit which adds xids of all the subtransactions in
    4363             :      * snapshot's xip array via SnapBuildCommitTxn, we can't do that here but
    4364             :      * we do add them to subxip array instead via ReorderBufferCopySnap. This
    4365             :      * allows the catalog changes made in subtransactions decoded till now to
    4366             :      * be visible.
    4367             :      */
    4368        1392 :     if (txn->snapshot_now == NULL)
    4369             :     {
    4370             :         dlist_iter  subxact_i;
    4371             : 
    4372             :         /* make sure this transaction is streamed for the first time */
    4373             :         Assert(!rbtxn_is_streamed(txn));
    4374             : 
    4375             :         /* at the beginning we should have invalid command ID */
    4376             :         Assert(txn->command_id == InvalidCommandId);
    4377             : 
    4378         148 :         dlist_foreach(subxact_i, &txn->subtxns)
    4379             :         {
    4380             :             ReorderBufferTXN *subtxn;
    4381             : 
    4382           8 :             subtxn = dlist_container(ReorderBufferTXN, node, subxact_i.cur);
    4383           8 :             ReorderBufferTransferSnapToParent(txn, subtxn);
    4384             :         }
    4385             : 
    4386             :         /*
    4387             :          * If this transaction has no snapshot, it didn't make any changes to
    4388             :          * the database till now, so there's nothing to decode.
    4389             :          */
    4390         140 :         if (txn->base_snapshot == NULL)
    4391             :         {
    4392             :             Assert(txn->ninvalidations == 0);
    4393           0 :             return;
    4394             :         }
    4395             : 
    4396         140 :         command_id = FirstCommandId;
    4397         140 :         snapshot_now = ReorderBufferCopySnap(rb, txn->base_snapshot,
    4398             :                                              txn, command_id);
    4399             :     }
    4400             :     else
    4401             :     {
    4402             :         /* the transaction must have been already streamed */
    4403             :         Assert(rbtxn_is_streamed(txn));
    4404             : 
    4405             :         /*
    4406             :          * Nah, we already have snapshot from the previous streaming run. We
    4407             :          * assume new subxacts can't move the LSN backwards, and so can't beat
    4408             :          * the LSN condition in the previous branch (so no need to walk
    4409             :          * through subxacts again). In fact, we must not do that as we may be
    4410             :          * using snapshot half-way through the subxact.
    4411             :          */
    4412        1252 :         command_id = txn->command_id;
    4413             : 
    4414             :         /*
    4415             :          * We can't use txn->snapshot_now directly because after the last
    4416             :          * streaming run, we might have got some new sub-transactions. So we
    4417             :          * need to add them to the snapshot.
    4418             :          */
    4419        1252 :         snapshot_now = ReorderBufferCopySnap(rb, txn->snapshot_now,
    4420             :                                              txn, command_id);
    4421             : 
    4422             :         /* Free the previously copied snapshot. */
    4423             :         Assert(txn->snapshot_now->copied);
    4424        1252 :         ReorderBufferFreeSnap(rb, txn->snapshot_now);
    4425        1252 :         txn->snapshot_now = NULL;
    4426             :     }
    4427             : 
    4428             :     /*
    4429             :      * Remember this information to be used later to update stats. We can't
    4430             :      * update the stats here as an error while processing the changes would
    4431             :      * lead to the accumulation of stats even though we haven't streamed all
    4432             :      * the changes.
    4433             :      */
    4434        1392 :     txn_is_streamed = rbtxn_is_streamed(txn);
    4435        1392 :     stream_bytes = txn->total_size;
    4436             : 
    4437             :     /* Process and send the changes to output plugin. */
    4438        1392 :     ReorderBufferProcessTXN(rb, txn, InvalidXLogRecPtr, snapshot_now,
    4439             :                             command_id, true);
    4440             : 
    4441        1392 :     rb->streamCount += 1;
    4442        1392 :     rb->streamBytes += stream_bytes;
    4443             : 
    4444             :     /* Don't consider already streamed transaction. */
    4445        1392 :     rb->streamTxns += (txn_is_streamed) ? 0 : 1;
    4446             : 
    4447             :     /* update the decoding stats */
    4448        1392 :     UpdateDecodingStats((LogicalDecodingContext *) rb->private_data);
    4449             : 
    4450             :     Assert(dlist_is_empty(&txn->changes));
    4451             :     Assert(txn->nentries == 0);
    4452             :     Assert(txn->nentries_mem == 0);
    4453             : }
    4454             : 
    4455             : /*
    4456             :  * Size of a change in memory.
    4457             :  */
    4458             : static Size
    4459     4348802 : ReorderBufferChangeSize(ReorderBufferChange *change)
    4460             : {
    4461     4348802 :     Size        sz = sizeof(ReorderBufferChange);
    4462             : 
    4463     4348802 :     switch (change->action)
    4464             :     {
    4465             :             /* fall through these, they're all similar enough */
    4466     4140870 :         case REORDER_BUFFER_CHANGE_INSERT:
    4467             :         case REORDER_BUFFER_CHANGE_UPDATE:
    4468             :         case REORDER_BUFFER_CHANGE_DELETE:
    4469             :         case REORDER_BUFFER_CHANGE_INTERNAL_SPEC_INSERT:
    4470             :             {
    4471             :                 HeapTuple   oldtup,
    4472             :                             newtup;
    4473     4140870 :                 Size        oldlen = 0;
    4474     4140870 :                 Size        newlen = 0;
    4475             : 
    4476     4140870 :                 oldtup = change->data.tp.oldtuple;
    4477     4140870 :                 newtup = change->data.tp.newtuple;
    4478             : 
    4479     4140870 :                 if (oldtup)
    4480             :                 {
    4481      427720 :                     sz += sizeof(HeapTupleData);
    4482      427720 :                     oldlen = oldtup->t_len;
    4483      427720 :                     sz += oldlen;
    4484             :                 }
    4485             : 
    4486     4140870 :                 if (newtup)
    4487             :                 {
    4488     3547368 :                     sz += sizeof(HeapTupleData);
    4489     3547368 :                     newlen = newtup->t_len;
    4490     3547368 :                     sz += newlen;
    4491             :                 }
    4492             : 
    4493     4140870 :                 break;
    4494             :             }
    4495         134 :         case REORDER_BUFFER_CHANGE_MESSAGE:
    4496             :             {
    4497         134 :                 Size        prefix_size = strlen(change->data.msg.prefix) + 1;
    4498             : 
    4499         134 :                 sz += prefix_size + change->data.msg.message_size +
    4500             :                     sizeof(Size) + sizeof(Size);
    4501             : 
    4502         134 :                 break;
    4503             :             }
    4504       19172 :         case REORDER_BUFFER_CHANGE_INVALIDATION:
    4505             :             {
    4506       19172 :                 sz += sizeof(SharedInvalidationMessage) *
    4507       19172 :                     change->data.inval.ninvalidations;
    4508       19172 :                 break;
    4509             :             }
    4510        4704 :         case REORDER_BUFFER_CHANGE_INTERNAL_SNAPSHOT:
    4511             :             {
    4512             :                 Snapshot    snap;
    4513             : 
    4514        4704 :                 snap = change->data.snapshot;
    4515             : 
    4516        4704 :                 sz += sizeof(SnapshotData) +
    4517        4704 :                     sizeof(TransactionId) * snap->xcnt +
    4518        4704 :                     sizeof(TransactionId) * snap->subxcnt;
    4519             : 
    4520        4704 :                 break;
    4521             :             }
    4522         142 :         case REORDER_BUFFER_CHANGE_TRUNCATE:
    4523             :             {
    4524         142 :                 sz += sizeof(Oid) * change->data.truncate.nrelids;
    4525             : 
    4526         142 :                 break;
    4527             :             }
    4528      183780 :         case REORDER_BUFFER_CHANGE_INTERNAL_SPEC_CONFIRM:
    4529             :         case REORDER_BUFFER_CHANGE_INTERNAL_SPEC_ABORT:
    4530             :         case REORDER_BUFFER_CHANGE_INTERNAL_COMMAND_ID:
    4531             :         case REORDER_BUFFER_CHANGE_INTERNAL_TUPLECID:
    4532             :             /* ReorderBufferChange contains everything important */
    4533      183780 :             break;
    4534             :     }
    4535             : 
    4536     4348802 :     return sz;
    4537             : }
    4538             : 
    4539             : 
    4540             : /*
    4541             :  * Restore a number of changes spilled to disk back into memory.
    4542             :  */
    4543             : static Size
    4544         210 : ReorderBufferRestoreChanges(ReorderBuffer *rb, ReorderBufferTXN *txn,
    4545             :                             TXNEntryFile *file, XLogSegNo *segno)
    4546             : {
    4547         210 :     Size        restored = 0;
    4548             :     XLogSegNo   last_segno;
    4549             :     dlist_mutable_iter cleanup_iter;
    4550         210 :     File       *fd = &file->vfd;
    4551             : 
    4552             :     Assert(XLogRecPtrIsValid(txn->first_lsn));
    4553             :     Assert(XLogRecPtrIsValid(txn->final_lsn));
    4554             : 
    4555             :     /* free current entries, so we have memory for more */
    4556      349616 :     dlist_foreach_modify(cleanup_iter, &txn->changes)
    4557             :     {
    4558      349406 :         ReorderBufferChange *cleanup =
    4559      349406 :             dlist_container(ReorderBufferChange, node, cleanup_iter.cur);
    4560             : 
    4561      349406 :         dlist_delete(&cleanup->node);
    4562      349406 :         ReorderBufferFreeChange(rb, cleanup, true);
    4563             :     }
    4564         210 :     txn->nentries_mem = 0;
    4565             :     Assert(dlist_is_empty(&txn->changes));
    4566             : 
    4567         210 :     XLByteToSeg(txn->final_lsn, last_segno, wal_segment_size);
    4568             : 
    4569      357028 :     while (restored < max_changes_in_memory && *segno <= last_segno)
    4570             :     {
    4571             :         int         readBytes;
    4572             :         ReorderBufferDiskChange *ondisk;
    4573             : 
    4574      356818 :         CHECK_FOR_INTERRUPTS();
    4575             : 
    4576      356818 :         if (*fd == -1)
    4577             :         {
    4578             :             char        path[MAXPGPATH];
    4579             : 
    4580             :             /* first time in */
    4581          86 :             if (*segno == 0)
    4582          80 :                 XLByteToSeg(txn->first_lsn, *segno, wal_segment_size);
    4583             : 
    4584             :             Assert(*segno != 0 || dlist_is_empty(&txn->changes));
    4585             : 
    4586             :             /*
    4587             :              * No need to care about TLIs here, only used during a single run,
    4588             :              * so each LSN only maps to a specific WAL record.
    4589             :              */
    4590          86 :             ReorderBufferSerializedPath(path, MyReplicationSlot, txn->xid,
    4591             :                                         *segno);
    4592             : 
    4593          86 :             *fd = PathNameOpenFile(path, O_RDONLY | PG_BINARY);
    4594             : 
    4595             :             /* No harm in resetting the offset even in case of failure */
    4596          86 :             file->curOffset = 0;
    4597             : 
    4598          86 :             if (*fd < 0 && errno == ENOENT)
    4599             :             {
    4600           2 :                 *fd = -1;
    4601           2 :                 (*segno)++;
    4602           2 :                 continue;
    4603             :             }
    4604          84 :             else if (*fd < 0)
    4605           0 :                 ereport(ERROR,
    4606             :                         (errcode_for_file_access(),
    4607             :                          errmsg("could not open file \"%s\": %m",
    4608             :                                 path)));
    4609             :         }
    4610             : 
    4611             :         /*
    4612             :          * Read the statically sized part of a change which has information
    4613             :          * about the total size. If we couldn't read a record, we're at the
    4614             :          * end of this file.
    4615             :          */
    4616      356816 :         ReorderBufferSerializeReserve(rb, sizeof(ReorderBufferDiskChange));
    4617      356816 :         readBytes = FileRead(file->vfd, rb->outbuf,
    4618             :                              sizeof(ReorderBufferDiskChange),
    4619             :                              file->curOffset, WAIT_EVENT_REORDER_BUFFER_READ);
    4620             : 
    4621             :         /* eof */
    4622      356816 :         if (readBytes == 0)
    4623             :         {
    4624          84 :             FileClose(*fd);
    4625          84 :             *fd = -1;
    4626          84 :             (*segno)++;
    4627          84 :             continue;
    4628             :         }
    4629      356732 :         else if (readBytes < 0)
    4630           0 :             ereport(ERROR,
    4631             :                     (errcode_for_file_access(),
    4632             :                      errmsg("could not read from reorderbuffer spill file: %m")));
    4633      356732 :         else if (readBytes != sizeof(ReorderBufferDiskChange))
    4634           0 :             ereport(ERROR,
    4635             :                     (errcode_for_file_access(),
    4636             :                      errmsg("could not read from reorderbuffer spill file: read %d instead of %u bytes",
    4637             :                             readBytes,
    4638             :                             (uint32) sizeof(ReorderBufferDiskChange))));
    4639             : 
    4640      356732 :         file->curOffset += readBytes;
    4641             : 
    4642      356732 :         ondisk = (ReorderBufferDiskChange *) rb->outbuf;
    4643             : 
    4644      356732 :         ReorderBufferSerializeReserve(rb,
    4645      356732 :                                       sizeof(ReorderBufferDiskChange) + ondisk->size);
    4646      356732 :         ondisk = (ReorderBufferDiskChange *) rb->outbuf;
    4647             : 
    4648      713464 :         readBytes = FileRead(file->vfd,
    4649      356732 :                              rb->outbuf + sizeof(ReorderBufferDiskChange),
    4650      356732 :                              ondisk->size - sizeof(ReorderBufferDiskChange),
    4651             :                              file->curOffset,
    4652             :                              WAIT_EVENT_REORDER_BUFFER_READ);
    4653             : 
    4654      356732 :         if (readBytes < 0)
    4655           0 :             ereport(ERROR,
    4656             :                     (errcode_for_file_access(),
    4657             :                      errmsg("could not read from reorderbuffer spill file: %m")));
    4658      356732 :         else if (readBytes != ondisk->size - sizeof(ReorderBufferDiskChange))
    4659           0 :             ereport(ERROR,
    4660             :                     (errcode_for_file_access(),
    4661             :                      errmsg("could not read from reorderbuffer spill file: read %d instead of %u bytes",
    4662             :                             readBytes,
    4663             :                             (uint32) (ondisk->size - sizeof(ReorderBufferDiskChange)))));
    4664             : 
    4665      356732 :         file->curOffset += readBytes;
    4666             : 
    4667             :         /*
    4668             :          * ok, read a full change from disk, now restore it into proper
    4669             :          * in-memory format
    4670             :          */
    4671      356732 :         ReorderBufferRestoreChange(rb, txn, rb->outbuf);
    4672      356732 :         restored++;
    4673             :     }
    4674             : 
    4675         210 :     return restored;
    4676             : }
    4677             : 
    4678             : /*
    4679             :  * Convert change from its on-disk format to in-memory format and queue it onto
    4680             :  * the TXN's ->changes list.
    4681             :  *
    4682             :  * Note: although "data" is declared char*, at entry it points to a
    4683             :  * maxalign'd buffer, making it safe in most of this function to assume
    4684             :  * that the pointed-to data is suitably aligned for direct access.
    4685             :  */
    4686             : static void
    4687      356732 : ReorderBufferRestoreChange(ReorderBuffer *rb, ReorderBufferTXN *txn,
    4688             :                            char *data)
    4689             : {
    4690             :     ReorderBufferDiskChange *ondisk;
    4691             :     ReorderBufferChange *change;
    4692             : 
    4693      356732 :     ondisk = (ReorderBufferDiskChange *) data;
    4694             : 
    4695      356732 :     change = ReorderBufferAllocChange(rb);
    4696             : 
    4697             :     /* copy static part */
    4698      356732 :     memcpy(change, &ondisk->change, sizeof(ReorderBufferChange));
    4699             : 
    4700      356732 :     data += sizeof(ReorderBufferDiskChange);
    4701             : 
    4702             :     /* restore individual stuff */
    4703      356732 :     switch (change->action)
    4704             :     {
    4705             :             /* fall through these, they're all similar enough */
    4706      352874 :         case REORDER_BUFFER_CHANGE_INSERT:
    4707             :         case REORDER_BUFFER_CHANGE_UPDATE:
    4708             :         case REORDER_BUFFER_CHANGE_DELETE:
    4709             :         case REORDER_BUFFER_CHANGE_INTERNAL_SPEC_INSERT:
    4710      352874 :             if (change->data.tp.oldtuple)
    4711             :             {
    4712       10012 :                 uint32      tuplelen = ((HeapTuple) data)->t_len;
    4713             : 
    4714       10012 :                 change->data.tp.oldtuple =
    4715       10012 :                     ReorderBufferAllocTupleBuf(rb, tuplelen - SizeofHeapTupleHeader);
    4716             : 
    4717             :                 /* restore ->tuple */
    4718       10012 :                 memcpy(change->data.tp.oldtuple, data,
    4719             :                        sizeof(HeapTupleData));
    4720       10012 :                 data += sizeof(HeapTupleData);
    4721             : 
    4722             :                 /* reset t_data pointer into the new tuplebuf */
    4723       10012 :                 change->data.tp.oldtuple->t_data =
    4724       10012 :                     (HeapTupleHeader) ((char *) change->data.tp.oldtuple + HEAPTUPLESIZE);
    4725             : 
    4726             :                 /* restore tuple data itself */
    4727       10012 :                 memcpy(change->data.tp.oldtuple->t_data, data, tuplelen);
    4728       10012 :                 data += tuplelen;
    4729             :             }
    4730             : 
    4731      352874 :             if (change->data.tp.newtuple)
    4732             :             {
    4733             :                 /* here, data might not be suitably aligned! */
    4734             :                 uint32      tuplelen;
    4735             : 
    4736      332432 :                 memcpy(&tuplelen, data + offsetof(HeapTupleData, t_len),
    4737             :                        sizeof(uint32));
    4738             : 
    4739      332432 :                 change->data.tp.newtuple =
    4740      332432 :                     ReorderBufferAllocTupleBuf(rb, tuplelen - SizeofHeapTupleHeader);
    4741             : 
    4742             :                 /* restore ->tuple */
    4743      332432 :                 memcpy(change->data.tp.newtuple, data,
    4744             :                        sizeof(HeapTupleData));
    4745      332432 :                 data += sizeof(HeapTupleData);
    4746             : 
    4747             :                 /* reset t_data pointer into the new tuplebuf */
    4748      332432 :                 change->data.tp.newtuple->t_data =
    4749      332432 :                     (HeapTupleHeader) ((char *) change->data.tp.newtuple + HEAPTUPLESIZE);
    4750             : 
    4751             :                 /* restore tuple data itself */
    4752      332432 :                 memcpy(change->data.tp.newtuple->t_data, data, tuplelen);
    4753      332432 :                 data += tuplelen;
    4754             :             }
    4755             : 
    4756      352874 :             break;
    4757           2 :         case REORDER_BUFFER_CHANGE_MESSAGE:
    4758             :             {
    4759             :                 Size        prefix_size;
    4760             : 
    4761             :                 /* read prefix */
    4762           2 :                 memcpy(&prefix_size, data, sizeof(Size));
    4763           2 :                 data += sizeof(Size);
    4764           2 :                 change->data.msg.prefix = MemoryContextAlloc(rb->context,
    4765             :                                                              prefix_size);
    4766           2 :                 memcpy(change->data.msg.prefix, data, prefix_size);
    4767             :                 Assert(change->data.msg.prefix[prefix_size - 1] == '\0');
    4768           2 :                 data += prefix_size;
    4769             : 
    4770             :                 /* read the message */
    4771           2 :                 memcpy(&change->data.msg.message_size, data, sizeof(Size));
    4772           2 :                 data += sizeof(Size);
    4773           2 :                 change->data.msg.message = MemoryContextAlloc(rb->context,
    4774             :                                                               change->data.msg.message_size);
    4775           2 :                 memcpy(change->data.msg.message, data,
    4776             :                        change->data.msg.message_size);
    4777           2 :                 data += change->data.msg.message_size;
    4778             : 
    4779           2 :                 break;
    4780             :             }
    4781          46 :         case REORDER_BUFFER_CHANGE_INVALIDATION:
    4782             :             {
    4783          46 :                 Size        inval_size = sizeof(SharedInvalidationMessage) *
    4784          46 :                     change->data.inval.ninvalidations;
    4785             : 
    4786          46 :                 change->data.inval.invalidations =
    4787          46 :                     MemoryContextAlloc(rb->context, inval_size);
    4788             : 
    4789             :                 /* read the message */
    4790          46 :                 memcpy(change->data.inval.invalidations, data, inval_size);
    4791             : 
    4792          46 :                 break;
    4793             :             }
    4794           4 :         case REORDER_BUFFER_CHANGE_INTERNAL_SNAPSHOT:
    4795             :             {
    4796             :                 Snapshot    oldsnap;
    4797             :                 Snapshot    newsnap;
    4798             :                 Size        size;
    4799             : 
    4800           4 :                 oldsnap = (Snapshot) data;
    4801             : 
    4802           4 :                 size = sizeof(SnapshotData) +
    4803           4 :                     sizeof(TransactionId) * oldsnap->xcnt +
    4804           4 :                     sizeof(TransactionId) * (oldsnap->subxcnt + 0);
    4805             : 
    4806           4 :                 change->data.snapshot = MemoryContextAllocZero(rb->context, size);
    4807             : 
    4808           4 :                 newsnap = change->data.snapshot;
    4809             : 
    4810           4 :                 memcpy(newsnap, data, size);
    4811           4 :                 newsnap->xip = (TransactionId *)
    4812             :                     (((char *) newsnap) + sizeof(SnapshotData));
    4813           4 :                 newsnap->subxip = newsnap->xip + newsnap->xcnt;
    4814           4 :                 newsnap->copied = true;
    4815           4 :                 break;
    4816             :             }
    4817             :             /* the base struct contains all the data, easy peasy */
    4818           0 :         case REORDER_BUFFER_CHANGE_TRUNCATE:
    4819             :             {
    4820             :                 Oid        *relids;
    4821             : 
    4822           0 :                 relids = ReorderBufferAllocRelids(rb, change->data.truncate.nrelids);
    4823           0 :                 memcpy(relids, data, change->data.truncate.nrelids * sizeof(Oid));
    4824           0 :                 change->data.truncate.relids = relids;
    4825             : 
    4826           0 :                 break;
    4827             :             }
    4828        3806 :         case REORDER_BUFFER_CHANGE_INTERNAL_SPEC_CONFIRM:
    4829             :         case REORDER_BUFFER_CHANGE_INTERNAL_SPEC_ABORT:
    4830             :         case REORDER_BUFFER_CHANGE_INTERNAL_COMMAND_ID:
    4831             :         case REORDER_BUFFER_CHANGE_INTERNAL_TUPLECID:
    4832        3806 :             break;
    4833             :     }
    4834             : 
    4835      356732 :     dlist_push_tail(&txn->changes, &change->node);
    4836      356732 :     txn->nentries_mem++;
    4837             : 
    4838             :     /*
    4839             :      * Update memory accounting for the restored change.  We need to do this
    4840             :      * although we don't check the memory limit when restoring the changes in
    4841             :      * this branch (we only do that when initially queueing the changes after
    4842             :      * decoding), because we will release the changes later, and that will
    4843             :      * update the accounting too (subtracting the size from the counters). And
    4844             :      * we don't want to underflow there.
    4845             :      */
    4846      356732 :     ReorderBufferChangeMemoryUpdate(rb, change, NULL, true,
    4847             :                                     ReorderBufferChangeSize(change));
    4848      356732 : }
    4849             : 
    4850             : /*
    4851             :  * Remove all on-disk stored for the passed in transaction.
    4852             :  */
    4853             : static void
    4854         672 : ReorderBufferRestoreCleanup(ReorderBuffer *rb, ReorderBufferTXN *txn)
    4855             : {
    4856             :     XLogSegNo   first;
    4857             :     XLogSegNo   cur;
    4858             :     XLogSegNo   last;
    4859             : 
    4860             :     Assert(XLogRecPtrIsValid(txn->first_lsn));
    4861             :     Assert(XLogRecPtrIsValid(txn->final_lsn));
    4862             : 
    4863         672 :     XLByteToSeg(txn->first_lsn, first, wal_segment_size);
    4864         672 :     XLByteToSeg(txn->final_lsn, last, wal_segment_size);
    4865             : 
    4866             :     /* iterate over all possible filenames, and delete them */
    4867        1374 :     for (cur = first; cur <= last; cur++)
    4868             :     {
    4869             :         char        path[MAXPGPATH];
    4870             : 
    4871         702 :         ReorderBufferSerializedPath(path, MyReplicationSlot, txn->xid, cur);
    4872         702 :         if (unlink(path) != 0 && errno != ENOENT)
    4873           0 :             ereport(ERROR,
    4874             :                     (errcode_for_file_access(),
    4875             :                      errmsg("could not remove file \"%s\": %m", path)));
    4876             :     }
    4877         672 : }
    4878             : 
    4879             : /*
    4880             :  * Remove any leftover serialized reorder buffers from a slot directory after a
    4881             :  * prior crash or decoding session exit.
    4882             :  */
    4883             : static void
    4884        4252 : ReorderBufferCleanupSerializedTXNs(const char *slotname)
    4885             : {
    4886             :     DIR        *spill_dir;
    4887             :     struct dirent *spill_de;
    4888             :     struct stat statbuf;
    4889             :     char        path[MAXPGPATH * 2 + sizeof(PG_REPLSLOT_DIR)];
    4890             : 
    4891        4252 :     sprintf(path, "%s/%s", PG_REPLSLOT_DIR, slotname);
    4892             : 
    4893             :     /* we're only handling directories here, skip if it's not ours */
    4894        4252 :     if (lstat(path, &statbuf) == 0 && !S_ISDIR(statbuf.st_mode))
    4895           0 :         return;
    4896             : 
    4897        4252 :     spill_dir = AllocateDir(path);
    4898       21260 :     while ((spill_de = ReadDirExtended(spill_dir, path, INFO)) != NULL)
    4899             :     {
    4900             :         /* only look at names that can be ours */
    4901       12756 :         if (strncmp(spill_de->d_name, "xid", 3) == 0)
    4902             :         {
    4903           0 :             snprintf(path, sizeof(path),
    4904             :                      "%s/%s/%s", PG_REPLSLOT_DIR, slotname,
    4905           0 :                      spill_de->d_name);
    4906             : 
    4907           0 :             if (unlink(path) != 0)
    4908           0 :                 ereport(ERROR,
    4909             :                         (errcode_for_file_access(),
    4910             :                          errmsg("could not remove file \"%s\" during removal of %s/%s/xid*: %m",
    4911             :                                 path, PG_REPLSLOT_DIR, slotname)));
    4912             :         }
    4913             :     }
    4914        4252 :     FreeDir(spill_dir);
    4915             : }
    4916             : 
    4917             : /*
    4918             :  * Given a replication slot, transaction ID and segment number, fill in the
    4919             :  * corresponding spill file into 'path', which is a caller-owned buffer of size
    4920             :  * at least MAXPGPATH.
    4921             :  */
    4922             : static void
    4923        8864 : ReorderBufferSerializedPath(char *path, ReplicationSlot *slot, TransactionId xid,
    4924             :                             XLogSegNo segno)
    4925             : {
    4926             :     XLogRecPtr  recptr;
    4927             : 
    4928        8864 :     XLogSegNoOffsetToRecPtr(segno, 0, wal_segment_size, recptr);
    4929             : 
    4930        8864 :     snprintf(path, MAXPGPATH, "%s/%s/xid-%u-lsn-%X-%X.spill",
    4931             :              PG_REPLSLOT_DIR,
    4932        8864 :              NameStr(MyReplicationSlot->data.name),
    4933        8864 :              xid, LSN_FORMAT_ARGS(recptr));
    4934        8864 : }
    4935             : 
    4936             : /*
    4937             :  * Delete all data spilled to disk after we've restarted/crashed. It will be
    4938             :  * recreated when the respective slots are reused.
    4939             :  */
    4940             : void
    4941        1972 : StartupReorderBuffer(void)
    4942             : {
    4943             :     DIR        *logical_dir;
    4944             :     struct dirent *logical_de;
    4945             : 
    4946        1972 :     logical_dir = AllocateDir(PG_REPLSLOT_DIR);
    4947        6146 :     while ((logical_de = ReadDir(logical_dir, PG_REPLSLOT_DIR)) != NULL)
    4948             :     {
    4949        4174 :         if (strcmp(logical_de->d_name, ".") == 0 ||
    4950        2202 :             strcmp(logical_de->d_name, "..") == 0)
    4951        3944 :             continue;
    4952             : 
    4953             :         /* if it cannot be a slot, skip the directory */
    4954         230 :         if (!ReplicationSlotValidateName(logical_de->d_name, true, DEBUG2))
    4955           0 :             continue;
    4956             : 
    4957             :         /*
    4958             :          * ok, has to be a surviving logical slot, iterate and delete
    4959             :          * everything starting with xid-*
    4960             :          */
    4961         230 :         ReorderBufferCleanupSerializedTXNs(logical_de->d_name);
    4962             :     }
    4963        1972 :     FreeDir(logical_dir);
    4964        1972 : }
    4965             : 
    4966             : /* ---------------------------------------
    4967             :  * toast reassembly support
    4968             :  * ---------------------------------------
    4969             :  */
    4970             : 
    4971             : /*
    4972             :  * Initialize per tuple toast reconstruction support.
    4973             :  */
    4974             : static void
    4975          70 : ReorderBufferToastInitHash(ReorderBuffer *rb, ReorderBufferTXN *txn)
    4976             : {
    4977             :     HASHCTL     hash_ctl;
    4978             : 
    4979             :     Assert(txn->toast_hash == NULL);
    4980             : 
    4981          70 :     hash_ctl.keysize = sizeof(Oid);
    4982          70 :     hash_ctl.entrysize = sizeof(ReorderBufferToastEnt);
    4983          70 :     hash_ctl.hcxt = rb->context;
    4984          70 :     txn->toast_hash = hash_create("ReorderBufferToastHash", 5, &hash_ctl,
    4985             :                                   HASH_ELEM | HASH_BLOBS | HASH_CONTEXT);
    4986          70 : }
    4987             : 
    4988             : /*
    4989             :  * Per toast-chunk handling for toast reconstruction
    4990             :  *
    4991             :  * Appends a toast chunk so we can reconstruct it when the tuple "owning" the
    4992             :  * toasted Datum comes along.
    4993             :  */
    4994             : static void
    4995        3660 : ReorderBufferToastAppendChunk(ReorderBuffer *rb, ReorderBufferTXN *txn,
    4996             :                               Relation relation, ReorderBufferChange *change)
    4997             : {
    4998             :     ReorderBufferToastEnt *ent;
    4999             :     HeapTuple   newtup;
    5000             :     bool        found;
    5001             :     int32       chunksize;
    5002             :     bool        isnull;
    5003             :     Pointer     chunk;
    5004        3660 :     TupleDesc   desc = RelationGetDescr(relation);
    5005             :     Oid         chunk_id;
    5006             :     int32       chunk_seq;
    5007             : 
    5008        3660 :     if (txn->toast_hash == NULL)
    5009          70 :         ReorderBufferToastInitHash(rb, txn);
    5010             : 
    5011             :     Assert(IsToastRelation(relation));
    5012             : 
    5013        3660 :     newtup = change->data.tp.newtuple;
    5014        3660 :     chunk_id = DatumGetObjectId(fastgetattr(newtup, 1, desc, &isnull));
    5015             :     Assert(!isnull);
    5016        3660 :     chunk_seq = DatumGetInt32(fastgetattr(newtup, 2, desc, &isnull));
    5017             :     Assert(!isnull);
    5018             : 
    5019             :     ent = (ReorderBufferToastEnt *)
    5020        3660 :         hash_search(txn->toast_hash, &chunk_id, HASH_ENTER, &found);
    5021             : 
    5022        3660 :     if (!found)
    5023             :     {
    5024             :         Assert(ent->chunk_id == chunk_id);
    5025          98 :         ent->num_chunks = 0;
    5026          98 :         ent->last_chunk_seq = 0;
    5027          98 :         ent->size = 0;
    5028          98 :         ent->reconstructed = NULL;
    5029          98 :         dlist_init(&ent->chunks);
    5030             : 
    5031          98 :         if (chunk_seq != 0)
    5032           0 :             elog(ERROR, "got sequence entry %d for toast chunk %u instead of seq 0",
    5033             :                  chunk_seq, chunk_id);
    5034             :     }
    5035        3562 :     else if (found && chunk_seq != ent->last_chunk_seq + 1)
    5036           0 :         elog(ERROR, "got sequence entry %d for toast chunk %u instead of seq %d",
    5037             :              chunk_seq, chunk_id, ent->last_chunk_seq + 1);
    5038             : 
    5039        3660 :     chunk = DatumGetPointer(fastgetattr(newtup, 3, desc, &isnull));
    5040             :     Assert(!isnull);
    5041             : 
    5042             :     /* calculate size so we can allocate the right size at once later */
    5043        3660 :     if (!VARATT_IS_EXTENDED(chunk))
    5044        3660 :         chunksize = VARSIZE(chunk) - VARHDRSZ;
    5045           0 :     else if (VARATT_IS_SHORT(chunk))
    5046             :         /* could happen due to heap_form_tuple doing its thing */
    5047           0 :         chunksize = VARSIZE_SHORT(chunk) - VARHDRSZ_SHORT;
    5048             :     else
    5049           0 :         elog(ERROR, "unexpected type of toast chunk");
    5050             : 
    5051        3660 :     ent->size += chunksize;
    5052        3660 :     ent->last_chunk_seq = chunk_seq;
    5053        3660 :     ent->num_chunks++;
    5054        3660 :     dlist_push_tail(&ent->chunks, &change->node);
    5055        3660 : }
    5056             : 
    5057             : /*
    5058             :  * Rejigger change->newtuple to point to in-memory toast tuples instead of
    5059             :  * on-disk toast tuples that may no longer exist (think DROP TABLE or VACUUM).
    5060             :  *
    5061             :  * We cannot replace unchanged toast tuples though, so those will still point
    5062             :  * to on-disk toast data.
    5063             :  *
    5064             :  * While updating the existing change with detoasted tuple data, we need to
    5065             :  * update the memory accounting info, because the change size will differ.
    5066             :  * Otherwise the accounting may get out of sync, triggering serialization
    5067             :  * at unexpected times.
    5068             :  *
    5069             :  * We simply subtract size of the change before rejiggering the tuple, and
    5070             :  * then add the new size. This makes it look like the change was removed
    5071             :  * and then added back, except it only tweaks the accounting info.
    5072             :  *
    5073             :  * In particular it can't trigger serialization, which would be pointless
    5074             :  * anyway as it happens during commit processing right before handing
    5075             :  * the change to the output plugin.
    5076             :  */
    5077             : static void
    5078      668108 : ReorderBufferToastReplace(ReorderBuffer *rb, ReorderBufferTXN *txn,
    5079             :                           Relation relation, ReorderBufferChange *change)
    5080             : {
    5081             :     TupleDesc   desc;
    5082             :     int         natt;
    5083             :     Datum      *attrs;
    5084             :     bool       *isnull;
    5085             :     bool       *free;
    5086             :     HeapTuple   tmphtup;
    5087             :     Relation    toast_rel;
    5088             :     TupleDesc   toast_desc;
    5089             :     MemoryContext oldcontext;
    5090             :     HeapTuple   newtup;
    5091             :     Size        old_size;
    5092             : 
    5093             :     /* no toast tuples changed */
    5094      668108 :     if (txn->toast_hash == NULL)
    5095      667616 :         return;
    5096             : 
    5097             :     /*
    5098             :      * We're going to modify the size of the change. So, to make sure the
    5099             :      * accounting is correct we record the current change size and then after
    5100             :      * re-computing the change we'll subtract the recorded size and then
    5101             :      * re-add the new change size at the end. We don't immediately subtract
    5102             :      * the old size because if there is any error before we add the new size,
    5103             :      * we will release the changes and that will update the accounting info
    5104             :      * (subtracting the size from the counters). And we don't want to
    5105             :      * underflow there.
    5106             :      */
    5107         492 :     old_size = ReorderBufferChangeSize(change);
    5108             : 
    5109         492 :     oldcontext = MemoryContextSwitchTo(rb->context);
    5110             : 
    5111             :     /* we should only have toast tuples in an INSERT or UPDATE */
    5112             :     Assert(change->data.tp.newtuple);
    5113             : 
    5114         492 :     desc = RelationGetDescr(relation);
    5115             : 
    5116         492 :     toast_rel = RelationIdGetRelation(relation->rd_rel->reltoastrelid);
    5117         492 :     if (!RelationIsValid(toast_rel))
    5118           0 :         elog(ERROR, "could not open toast relation with OID %u (base relation \"%s\")",
    5119             :              relation->rd_rel->reltoastrelid, RelationGetRelationName(relation));
    5120             : 
    5121         492 :     toast_desc = RelationGetDescr(toast_rel);
    5122             : 
    5123             :     /* should we allocate from stack instead? */
    5124         492 :     attrs = palloc0_array(Datum, desc->natts);
    5125         492 :     isnull = palloc0_array(bool, desc->natts);
    5126         492 :     free = palloc0_array(bool, desc->natts);
    5127             : 
    5128         492 :     newtup = change->data.tp.newtuple;
    5129             : 
    5130         492 :     heap_deform_tuple(newtup, desc, attrs, isnull);
    5131             : 
    5132        1514 :     for (natt = 0; natt < desc->natts; natt++)
    5133             :     {
    5134        1022 :         CompactAttribute *attr = TupleDescCompactAttr(desc, natt);
    5135             :         ReorderBufferToastEnt *ent;
    5136             :         struct varlena *varlena;
    5137             : 
    5138             :         /* va_rawsize is the size of the original datum -- including header */
    5139             :         struct varatt_external toast_pointer;
    5140             :         struct varatt_indirect redirect_pointer;
    5141        1022 :         struct varlena *new_datum = NULL;
    5142             :         struct varlena *reconstructed;
    5143             :         dlist_iter  it;
    5144        1022 :         Size        data_done = 0;
    5145             : 
    5146        1022 :         if (attr->attisdropped)
    5147         926 :             continue;
    5148             : 
    5149             :         /* not a varlena datatype */
    5150        1022 :         if (attr->attlen != -1)
    5151         482 :             continue;
    5152             : 
    5153             :         /* no data */
    5154         540 :         if (isnull[natt])
    5155          24 :             continue;
    5156             : 
    5157             :         /* ok, we know we have a toast datum */
    5158         516 :         varlena = (struct varlena *) DatumGetPointer(attrs[natt]);
    5159             : 
    5160             :         /* no need to do anything if the tuple isn't external */
    5161         516 :         if (!VARATT_IS_EXTERNAL(varlena))
    5162         404 :             continue;
    5163             : 
    5164         112 :         VARATT_EXTERNAL_GET_POINTER(toast_pointer, varlena);
    5165             : 
    5166             :         /*
    5167             :          * Check whether the toast tuple changed, replace if so.
    5168             :          */
    5169             :         ent = (ReorderBufferToastEnt *)
    5170         112 :             hash_search(txn->toast_hash,
    5171             :                         &toast_pointer.va_valueid,
    5172             :                         HASH_FIND,
    5173             :                         NULL);
    5174         112 :         if (ent == NULL)
    5175          16 :             continue;
    5176             : 
    5177             :         new_datum =
    5178          96 :             (struct varlena *) palloc0(INDIRECT_POINTER_SIZE);
    5179             : 
    5180          96 :         free[natt] = true;
    5181             : 
    5182          96 :         reconstructed = palloc0(toast_pointer.va_rawsize);
    5183             : 
    5184          96 :         ent->reconstructed = reconstructed;
    5185             : 
    5186             :         /* stitch toast tuple back together from its parts */
    5187        3654 :         dlist_foreach(it, &ent->chunks)
    5188             :         {
    5189             :             bool        cisnull;
    5190             :             ReorderBufferChange *cchange;
    5191             :             HeapTuple   ctup;
    5192             :             Pointer     chunk;
    5193             : 
    5194        3558 :             cchange = dlist_container(ReorderBufferChange, node, it.cur);
    5195        3558 :             ctup = cchange->data.tp.newtuple;
    5196        3558 :             chunk = DatumGetPointer(fastgetattr(ctup, 3, toast_desc, &cisnull));
    5197             : 
    5198             :             Assert(!cisnull);
    5199             :             Assert(!VARATT_IS_EXTERNAL(chunk));
    5200             :             Assert(!VARATT_IS_SHORT(chunk));
    5201             : 
    5202        3558 :             memcpy(VARDATA(reconstructed) + data_done,
    5203        3558 :                    VARDATA(chunk),
    5204        3558 :                    VARSIZE(chunk) - VARHDRSZ);
    5205        3558 :             data_done += VARSIZE(chunk) - VARHDRSZ;
    5206             :         }
    5207             :         Assert(data_done == VARATT_EXTERNAL_GET_EXTSIZE(toast_pointer));
    5208             : 
    5209             :         /* make sure its marked as compressed or not */
    5210          96 :         if (VARATT_EXTERNAL_IS_COMPRESSED(toast_pointer))
    5211          10 :             SET_VARSIZE_COMPRESSED(reconstructed, data_done + VARHDRSZ);
    5212             :         else
    5213          86 :             SET_VARSIZE(reconstructed, data_done + VARHDRSZ);
    5214             : 
    5215          96 :         memset(&redirect_pointer, 0, sizeof(redirect_pointer));
    5216          96 :         redirect_pointer.pointer = reconstructed;
    5217             : 
    5218          96 :         SET_VARTAG_EXTERNAL(new_datum, VARTAG_INDIRECT);
    5219          96 :         memcpy(VARDATA_EXTERNAL(new_datum), &redirect_pointer,
    5220             :                sizeof(redirect_pointer));
    5221             : 
    5222          96 :         attrs[natt] = PointerGetDatum(new_datum);
    5223             :     }
    5224             : 
    5225             :     /*
    5226             :      * Build tuple in separate memory & copy tuple back into the tuplebuf
    5227             :      * passed to the output plugin. We can't directly heap_fill_tuple() into
    5228             :      * the tuplebuf because attrs[] will point back into the current content.
    5229             :      */
    5230         492 :     tmphtup = heap_form_tuple(desc, attrs, isnull);
    5231             :     Assert(newtup->t_len <= MaxHeapTupleSize);
    5232             :     Assert(newtup->t_data == (HeapTupleHeader) ((char *) newtup + HEAPTUPLESIZE));
    5233             : 
    5234         492 :     memcpy(newtup->t_data, tmphtup->t_data, tmphtup->t_len);
    5235         492 :     newtup->t_len = tmphtup->t_len;
    5236             : 
    5237             :     /*
    5238             :      * free resources we won't further need, more persistent stuff will be
    5239             :      * free'd in ReorderBufferToastReset().
    5240             :      */
    5241         492 :     RelationClose(toast_rel);
    5242         492 :     pfree(tmphtup);
    5243        1514 :     for (natt = 0; natt < desc->natts; natt++)
    5244             :     {
    5245        1022 :         if (free[natt])
    5246          96 :             pfree(DatumGetPointer(attrs[natt]));
    5247             :     }
    5248         492 :     pfree(attrs);
    5249         492 :     pfree(free);
    5250         492 :     pfree(isnull);
    5251             : 
    5252         492 :     MemoryContextSwitchTo(oldcontext);
    5253             : 
    5254             :     /* subtract the old change size */
    5255         492 :     ReorderBufferChangeMemoryUpdate(rb, change, NULL, false, old_size);
    5256             :     /* now add the change back, with the correct size */
    5257         492 :     ReorderBufferChangeMemoryUpdate(rb, change, NULL, true,
    5258             :                                     ReorderBufferChangeSize(change));
    5259             : }
    5260             : 
    5261             : /*
    5262             :  * Free all resources allocated for toast reconstruction.
    5263             :  */
    5264             : static void
    5265      675460 : ReorderBufferToastReset(ReorderBuffer *rb, ReorderBufferTXN *txn)
    5266             : {
    5267             :     HASH_SEQ_STATUS hstat;
    5268             :     ReorderBufferToastEnt *ent;
    5269             : 
    5270      675460 :     if (txn->toast_hash == NULL)
    5271      675390 :         return;
    5272             : 
    5273             :     /* sequentially walk over the hash and free everything */
    5274          70 :     hash_seq_init(&hstat, txn->toast_hash);
    5275         168 :     while ((ent = (ReorderBufferToastEnt *) hash_seq_search(&hstat)) != NULL)
    5276             :     {
    5277             :         dlist_mutable_iter it;
    5278             : 
    5279          98 :         if (ent->reconstructed != NULL)
    5280          96 :             pfree(ent->reconstructed);
    5281             : 
    5282        3758 :         dlist_foreach_modify(it, &ent->chunks)
    5283             :         {
    5284        3660 :             ReorderBufferChange *change =
    5285        3660 :                 dlist_container(ReorderBufferChange, node, it.cur);
    5286             : 
    5287        3660 :             dlist_delete(&change->node);
    5288        3660 :             ReorderBufferFreeChange(rb, change, true);
    5289             :         }
    5290             :     }
    5291             : 
    5292          70 :     hash_destroy(txn->toast_hash);
    5293          70 :     txn->toast_hash = NULL;
    5294             : }
    5295             : 
    5296             : 
    5297             : /* ---------------------------------------
    5298             :  * Visibility support for logical decoding
    5299             :  *
    5300             :  *
    5301             :  * Lookup actual cmin/cmax values when using decoding snapshot. We can't
    5302             :  * always rely on stored cmin/cmax values because of two scenarios:
    5303             :  *
    5304             :  * * A tuple got changed multiple times during a single transaction and thus
    5305             :  *   has got a combo CID. Combo CIDs are only valid for the duration of a
    5306             :  *   single transaction.
    5307             :  * * A tuple with a cmin but no cmax (and thus no combo CID) got
    5308             :  *   deleted/updated in another transaction than the one which created it
    5309             :  *   which we are looking at right now. As only one of cmin, cmax or combo CID
    5310             :  *   is actually stored in the heap we don't have access to the value we
    5311             :  *   need anymore.
    5312             :  *
    5313             :  * To resolve those problems we have a per-transaction hash of (cmin,
    5314             :  * cmax) tuples keyed by (relfilelocator, ctid) which contains the actual
    5315             :  * (cmin, cmax) values. That also takes care of combo CIDs by simply
    5316             :  * not caring about them at all. As we have the real cmin/cmax values
    5317             :  * combo CIDs aren't interesting.
    5318             :  *
    5319             :  * As we only care about catalog tuples here the overhead of this
    5320             :  * hashtable should be acceptable.
    5321             :  *
    5322             :  * Heap rewrites complicate this a bit, check rewriteheap.c for
    5323             :  * details.
    5324             :  * -------------------------------------------------------------------------
    5325             :  */
    5326             : 
    5327             : /* struct for sorting mapping files by LSN efficiently */
    5328             : typedef struct RewriteMappingFile
    5329             : {
    5330             :     XLogRecPtr  lsn;
    5331             :     char        fname[MAXPGPATH];
    5332             : } RewriteMappingFile;
    5333             : 
    5334             : #ifdef NOT_USED
    5335             : static void
    5336             : DisplayMapping(HTAB *tuplecid_data)
    5337             : {
    5338             :     HASH_SEQ_STATUS hstat;
    5339             :     ReorderBufferTupleCidEnt *ent;
    5340             : 
    5341             :     hash_seq_init(&hstat, tuplecid_data);
    5342             :     while ((ent = (ReorderBufferTupleCidEnt *) hash_seq_search(&hstat)) != NULL)
    5343             :     {
    5344             :         elog(DEBUG3, "mapping: node: %u/%u/%u tid: %u/%u cmin: %u, cmax: %u",
    5345             :              ent->key.rlocator.dbOid,
    5346             :              ent->key.rlocator.spcOid,
    5347             :              ent->key.rlocator.relNumber,
    5348             :              ItemPointerGetBlockNumber(&ent->key.tid),
    5349             :              ItemPointerGetOffsetNumber(&ent->key.tid),
    5350             :              ent->cmin,
    5351             :              ent->cmax
    5352             :             );
    5353             :     }
    5354             : }
    5355             : #endif
    5356             : 
    5357             : /*
    5358             :  * Apply a single mapping file to tuplecid_data.
    5359             :  *
    5360             :  * The mapping file has to have been verified to be a) committed b) for our
    5361             :  * transaction c) applied in LSN order.
    5362             :  */
    5363             : static void
    5364          54 : ApplyLogicalMappingFile(HTAB *tuplecid_data, Oid relid, const char *fname)
    5365             : {
    5366             :     char        path[MAXPGPATH];
    5367             :     int         fd;
    5368             :     int         readBytes;
    5369             :     LogicalRewriteMappingData map;
    5370             : 
    5371          54 :     sprintf(path, "%s/%s", PG_LOGICAL_MAPPINGS_DIR, fname);
    5372          54 :     fd = OpenTransientFile(path, O_RDONLY | PG_BINARY);
    5373          54 :     if (fd < 0)
    5374           0 :         ereport(ERROR,
    5375             :                 (errcode_for_file_access(),
    5376             :                  errmsg("could not open file \"%s\": %m", path)));
    5377             : 
    5378             :     while (true)
    5379         418 :     {
    5380             :         ReorderBufferTupleCidKey key;
    5381             :         ReorderBufferTupleCidEnt *ent;
    5382             :         ReorderBufferTupleCidEnt *new_ent;
    5383             :         bool        found;
    5384             : 
    5385             :         /* be careful about padding */
    5386         472 :         memset(&key, 0, sizeof(ReorderBufferTupleCidKey));
    5387             : 
    5388             :         /* read all mappings till the end of the file */
    5389         472 :         pgstat_report_wait_start(WAIT_EVENT_REORDER_LOGICAL_MAPPING_READ);
    5390         472 :         readBytes = read(fd, &map, sizeof(LogicalRewriteMappingData));
    5391         472 :         pgstat_report_wait_end();
    5392             : 
    5393         472 :         if (readBytes < 0)
    5394           0 :             ereport(ERROR,
    5395             :                     (errcode_for_file_access(),
    5396             :                      errmsg("could not read file \"%s\": %m",
    5397             :                             path)));
    5398         472 :         else if (readBytes == 0)    /* EOF */
    5399          54 :             break;
    5400         418 :         else if (readBytes != sizeof(LogicalRewriteMappingData))
    5401           0 :             ereport(ERROR,
    5402             :                     (errcode_for_file_access(),
    5403             :                      errmsg("could not read from file \"%s\": read %d instead of %d bytes",
    5404             :                             path, readBytes,
    5405             :                             (int32) sizeof(LogicalRewriteMappingData))));
    5406             : 
    5407         418 :         key.rlocator = map.old_locator;
    5408         418 :         ItemPointerCopy(&map.old_tid,
    5409             :                         &key.tid);
    5410             : 
    5411             : 
    5412             :         ent = (ReorderBufferTupleCidEnt *)
    5413         418 :             hash_search(tuplecid_data, &key, HASH_FIND, NULL);
    5414             : 
    5415             :         /* no existing mapping, no need to update */
    5416         418 :         if (!ent)
    5417           0 :             continue;
    5418             : 
    5419         418 :         key.rlocator = map.new_locator;
    5420         418 :         ItemPointerCopy(&map.new_tid,
    5421             :                         &key.tid);
    5422             : 
    5423             :         new_ent = (ReorderBufferTupleCidEnt *)
    5424         418 :             hash_search(tuplecid_data, &key, HASH_ENTER, &found);
    5425             : 
    5426         418 :         if (found)
    5427             :         {
    5428             :             /*
    5429             :              * Make sure the existing mapping makes sense. We sometime update
    5430             :              * old records that did not yet have a cmax (e.g. pg_class' own
    5431             :              * entry while rewriting it) during rewrites, so allow that.
    5432             :              */
    5433             :             Assert(ent->cmin == InvalidCommandId || ent->cmin == new_ent->cmin);
    5434             :             Assert(ent->cmax == InvalidCommandId || ent->cmax == new_ent->cmax);
    5435             :         }
    5436             :         else
    5437             :         {
    5438             :             /* update mapping */
    5439         406 :             new_ent->cmin = ent->cmin;
    5440         406 :             new_ent->cmax = ent->cmax;
    5441         406 :             new_ent->combocid = ent->combocid;
    5442             :         }
    5443             :     }
    5444             : 
    5445          54 :     if (CloseTransientFile(fd) != 0)
    5446           0 :         ereport(ERROR,
    5447             :                 (errcode_for_file_access(),
    5448             :                  errmsg("could not close file \"%s\": %m", path)));
    5449          54 : }
    5450             : 
    5451             : 
    5452             : /*
    5453             :  * Check whether the TransactionId 'xid' is in the pre-sorted array 'xip'.
    5454             :  */
    5455             : static bool
    5456         696 : TransactionIdInArray(TransactionId xid, TransactionId *xip, Size num)
    5457             : {
    5458         696 :     return bsearch(&xid, xip, num,
    5459         696 :                    sizeof(TransactionId), xidComparator) != NULL;
    5460             : }
    5461             : 
    5462             : /*
    5463             :  * list_sort() comparator for sorting RewriteMappingFiles in LSN order.
    5464             :  */
    5465             : static int
    5466          76 : file_sort_by_lsn(const ListCell *a_p, const ListCell *b_p)
    5467             : {
    5468          76 :     RewriteMappingFile *a = (RewriteMappingFile *) lfirst(a_p);
    5469          76 :     RewriteMappingFile *b = (RewriteMappingFile *) lfirst(b_p);
    5470             : 
    5471          76 :     return pg_cmp_u64(a->lsn, b->lsn);
    5472             : }
    5473             : 
    5474             : /*
    5475             :  * Apply any existing logical remapping files if there are any targeted at our
    5476             :  * transaction for relid.
    5477             :  */
    5478             : static void
    5479          22 : UpdateLogicalMappings(HTAB *tuplecid_data, Oid relid, Snapshot snapshot)
    5480             : {
    5481             :     DIR        *mapping_dir;
    5482             :     struct dirent *mapping_de;
    5483          22 :     List       *files = NIL;
    5484             :     ListCell   *file;
    5485          22 :     Oid         dboid = IsSharedRelation(relid) ? InvalidOid : MyDatabaseId;
    5486             : 
    5487          22 :     mapping_dir = AllocateDir(PG_LOGICAL_MAPPINGS_DIR);
    5488        1146 :     while ((mapping_de = ReadDir(mapping_dir, PG_LOGICAL_MAPPINGS_DIR)) != NULL)
    5489             :     {
    5490             :         Oid         f_dboid;
    5491             :         Oid         f_relid;
    5492             :         TransactionId f_mapped_xid;
    5493             :         TransactionId f_create_xid;
    5494             :         XLogRecPtr  f_lsn;
    5495             :         uint32      f_hi,
    5496             :                     f_lo;
    5497             :         RewriteMappingFile *f;
    5498             : 
    5499        1124 :         if (strcmp(mapping_de->d_name, ".") == 0 ||
    5500        1102 :             strcmp(mapping_de->d_name, "..") == 0)
    5501        1070 :             continue;
    5502             : 
    5503             :         /* Ignore files that aren't ours */
    5504        1080 :         if (strncmp(mapping_de->d_name, "map-", 4) != 0)
    5505           0 :             continue;
    5506             : 
    5507        1080 :         if (sscanf(mapping_de->d_name, LOGICAL_REWRITE_FORMAT,
    5508             :                    &f_dboid, &f_relid, &f_hi, &f_lo,
    5509             :                    &f_mapped_xid, &f_create_xid) != 6)
    5510           0 :             elog(ERROR, "could not parse filename \"%s\"", mapping_de->d_name);
    5511             : 
    5512        1080 :         f_lsn = ((uint64) f_hi) << 32 | f_lo;
    5513             : 
    5514             :         /* mapping for another database */
    5515        1080 :         if (f_dboid != dboid)
    5516           0 :             continue;
    5517             : 
    5518             :         /* mapping for another relation */
    5519        1080 :         if (f_relid != relid)
    5520         120 :             continue;
    5521             : 
    5522             :         /* did the creating transaction abort? */
    5523         960 :         if (!TransactionIdDidCommit(f_create_xid))
    5524         264 :             continue;
    5525             : 
    5526             :         /* not for our transaction */
    5527         696 :         if (!TransactionIdInArray(f_mapped_xid, snapshot->subxip, snapshot->subxcnt))
    5528         642 :             continue;
    5529             : 
    5530             :         /* ok, relevant, queue for apply */
    5531          54 :         f = palloc_object(RewriteMappingFile);
    5532          54 :         f->lsn = f_lsn;
    5533          54 :         strcpy(f->fname, mapping_de->d_name);
    5534          54 :         files = lappend(files, f);
    5535             :     }
    5536          22 :     FreeDir(mapping_dir);
    5537             : 
    5538             :     /* sort files so we apply them in LSN order */
    5539          22 :     list_sort(files, file_sort_by_lsn);
    5540             : 
    5541          76 :     foreach(file, files)
    5542             :     {
    5543          54 :         RewriteMappingFile *f = (RewriteMappingFile *) lfirst(file);
    5544             : 
    5545          54 :         elog(DEBUG1, "applying mapping: \"%s\" in %u", f->fname,
    5546             :              snapshot->subxip[0]);
    5547          54 :         ApplyLogicalMappingFile(tuplecid_data, relid, f->fname);
    5548          54 :         pfree(f);
    5549             :     }
    5550          22 : }
    5551             : 
    5552             : /*
    5553             :  * Lookup cmin/cmax of a tuple, during logical decoding where we can't rely on
    5554             :  * combo CIDs.
    5555             :  */
    5556             : bool
    5557        1538 : ResolveCminCmaxDuringDecoding(HTAB *tuplecid_data,
    5558             :                               Snapshot snapshot,
    5559             :                               HeapTuple htup, Buffer buffer,
    5560             :                               CommandId *cmin, CommandId *cmax)
    5561             : {
    5562             :     ReorderBufferTupleCidKey key;
    5563             :     ReorderBufferTupleCidEnt *ent;
    5564             :     ForkNumber  forkno;
    5565             :     BlockNumber blockno;
    5566        1538 :     bool        updated_mapping = false;
    5567             : 
    5568             :     /*
    5569             :      * Return unresolved if tuplecid_data is not valid.  That's because when
    5570             :      * streaming in-progress transactions we may run into tuples with the CID
    5571             :      * before actually decoding them.  Think e.g. about INSERT followed by
    5572             :      * TRUNCATE, where the TRUNCATE may not be decoded yet when applying the
    5573             :      * INSERT.  So in such cases, we assume the CID is from the future
    5574             :      * command.
    5575             :      */
    5576        1538 :     if (tuplecid_data == NULL)
    5577          22 :         return false;
    5578             : 
    5579             :     /* be careful about padding */
    5580        1516 :     memset(&key, 0, sizeof(key));
    5581             : 
    5582             :     Assert(!BufferIsLocal(buffer));
    5583             : 
    5584             :     /*
    5585             :      * get relfilelocator from the buffer, no convenient way to access it
    5586             :      * other than that.
    5587             :      */
    5588        1516 :     BufferGetTag(buffer, &key.rlocator, &forkno, &blockno);
    5589             : 
    5590             :     /* tuples can only be in the main fork */
    5591             :     Assert(forkno == MAIN_FORKNUM);
    5592             :     Assert(blockno == ItemPointerGetBlockNumber(&htup->t_self));
    5593             : 
    5594        1516 :     ItemPointerCopy(&htup->t_self,
    5595             :                     &key.tid);
    5596             : 
    5597        1538 : restart:
    5598             :     ent = (ReorderBufferTupleCidEnt *)
    5599        1538 :         hash_search(tuplecid_data, &key, HASH_FIND, NULL);
    5600             : 
    5601             :     /*
    5602             :      * failed to find a mapping, check whether the table was rewritten and
    5603             :      * apply mapping if so, but only do that once - there can be no new
    5604             :      * mappings while we are in here since we have to hold a lock on the
    5605             :      * relation.
    5606             :      */
    5607        1538 :     if (ent == NULL && !updated_mapping)
    5608             :     {
    5609          22 :         UpdateLogicalMappings(tuplecid_data, htup->t_tableOid, snapshot);
    5610             :         /* now check but don't update for a mapping again */
    5611          22 :         updated_mapping = true;
    5612          22 :         goto restart;
    5613             :     }
    5614        1516 :     else if (ent == NULL)
    5615          10 :         return false;
    5616             : 
    5617        1506 :     if (cmin)
    5618        1506 :         *cmin = ent->cmin;
    5619        1506 :     if (cmax)
    5620        1506 :         *cmax = ent->cmax;
    5621        1506 :     return true;
    5622             : }
    5623             : 
    5624             : /*
    5625             :  * Count invalidation messages of specified transaction.
    5626             :  *
    5627             :  * Returns number of messages, and msgs is set to the pointer of the linked
    5628             :  * list for the messages.
    5629             :  */
    5630             : uint32
    5631          64 : ReorderBufferGetInvalidations(ReorderBuffer *rb, TransactionId xid,
    5632             :                               SharedInvalidationMessage **msgs)
    5633             : {
    5634             :     ReorderBufferTXN *txn;
    5635             : 
    5636          64 :     txn = ReorderBufferTXNByXid(rb, xid, false, NULL, InvalidXLogRecPtr,
    5637             :                                 false);
    5638             : 
    5639          64 :     if (txn == NULL)
    5640           0 :         return 0;
    5641             : 
    5642          64 :     *msgs = txn->invalidations;
    5643             : 
    5644          64 :     return txn->ninvalidations;
    5645             : }

Generated by: LCOV version 1.16