LCOV - code coverage report
Current view: top level - src/backend/replication/logical - decode.c (source / functions) Coverage Total Hit
Test: PostgreSQL 20devel Lines: 92.8 % 460 427
Test Date: 2026-12-02 15:17:04 Functions: 95.2 % 21 20
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 78.9 % 232 183

             Branch data     Line data    Source code
       1                 :             : /* -------------------------------------------------------------------------
       2                 :             :  *
       3                 :             :  * decode.c
       4                 :             :  *      This module decodes WAL records read using xlogreader.h's APIs for the
       5                 :             :  *      purpose of logical decoding by passing information to the
       6                 :             :  *      reorderbuffer module (containing the actual changes) and to the
       7                 :             :  *      snapbuild module to build a fitting catalog snapshot (to be able to
       8                 :             :  *      properly decode the changes in the reorderbuffer).
       9                 :             :  *
      10                 :             :  * NOTE:
      11                 :             :  *      This basically tries to handle all low level xlog stuff for
      12                 :             :  *      reorderbuffer.c and snapbuild.c. There's some minor leakage where a
      13                 :             :  *      specific record's struct is used to pass data along, but those just
      14                 :             :  *      happen to contain the right amount of data in a convenient
      15                 :             :  *      format. There isn't and shouldn't be much intelligence about the
      16                 :             :  *      contents of records in here except turning them into a more usable
      17                 :             :  *      format.
      18                 :             :  *
      19                 :             :  * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
      20                 :             :  * Portions Copyright (c) 1994, Regents of the University of California
      21                 :             :  *
      22                 :             :  * IDENTIFICATION
      23                 :             :  *    src/backend/replication/logical/decode.c
      24                 :             :  *
      25                 :             :  * -------------------------------------------------------------------------
      26                 :             :  */
      27                 :             : #include "postgres.h"
      28                 :             : 
      29                 :             : #include "access/heapam_xlog.h"
      30                 :             : #include "access/transam.h"
      31                 :             : #include "access/xact.h"
      32                 :             : #include "access/xlog_internal.h"
      33                 :             : #include "access/xlogreader.h"
      34                 :             : #include "access/xlogrecord.h"
      35                 :             : #include "catalog/pg_control.h"
      36                 :             : #include "commands/repack.h"
      37                 :             : #include "replication/decode.h"
      38                 :             : #include "replication/logical.h"
      39                 :             : #include "replication/message.h"
      40                 :             : #include "replication/reorderbuffer.h"
      41                 :             : #include "replication/snapbuild.h"
      42                 :             : #include "storage/standbydefs.h"
      43                 :             : 
      44                 :             : /* individual record(group)'s handlers */
      45                 :             : static void DecodeInsert(LogicalDecodingContext *ctx, XLogRecordBuffer *buf);
      46                 :             : static void DecodeUpdate(LogicalDecodingContext *ctx, XLogRecordBuffer *buf);
      47                 :             : static void DecodeDelete(LogicalDecodingContext *ctx, XLogRecordBuffer *buf);
      48                 :             : static void DecodeTruncate(LogicalDecodingContext *ctx, XLogRecordBuffer *buf);
      49                 :             : static void DecodeMultiInsert(LogicalDecodingContext *ctx, XLogRecordBuffer *buf);
      50                 :             : static void DecodeSpecConfirm(LogicalDecodingContext *ctx, XLogRecordBuffer *buf);
      51                 :             : 
      52                 :             : static void DecodeCommit(LogicalDecodingContext *ctx, XLogRecordBuffer *buf,
      53                 :             :                          xl_xact_parsed_commit *parsed, TransactionId xid,
      54                 :             :                          bool two_phase);
      55                 :             : static void DecodeAbort(LogicalDecodingContext *ctx, XLogRecordBuffer *buf,
      56                 :             :                         xl_xact_parsed_abort *parsed, TransactionId xid,
      57                 :             :                         bool two_phase);
      58                 :             : static void DecodePrepare(LogicalDecodingContext *ctx, XLogRecordBuffer *buf,
      59                 :             :                           xl_xact_parsed_prepare *parsed);
      60                 :             : 
      61                 :             : 
      62                 :             : /* common function to decode tuples */
      63                 :             : static void DecodeXLogTuple(char *data, Size len, HeapTuple tuple);
      64                 :             : 
      65                 :             : /* helper functions for decoding transactions */
      66                 :             : static inline bool FilterPrepare(LogicalDecodingContext *ctx,
      67                 :             :                                  TransactionId xid, const char *gid);
      68                 :             : static bool DecodeTXNNeedSkip(LogicalDecodingContext *ctx,
      69                 :             :                               XLogRecordBuffer *buf, Oid txn_dbid,
      70                 :             :                               ReplOriginId origin_id);
      71                 :             : 
      72                 :             : /*
      73                 :             :  * Take every XLogReadRecord()ed record and perform the actions required to
      74                 :             :  * decode it using the output plugin already setup in the logical decoding
      75                 :             :  * context.
      76                 :             :  *
      77                 :             :  * NB: Note that every record's xid needs to be processed by reorderbuffer
      78                 :             :  * (xids contained in the content of records are not relevant for this rule).
      79                 :             :  * That means that for records which'd otherwise not go through the
      80                 :             :  * reorderbuffer ReorderBufferProcessXid() has to be called. We don't want to
      81                 :             :  * call ReorderBufferProcessXid for each record type by default, because
      82                 :             :  * e.g. empty xacts can be handled more efficiently if there's no previous
      83                 :             :  * state for them.
      84                 :             :  *
      85                 :             :  * We also support the ability to fast forward thru records, skipping some
      86                 :             :  * record types completely - see individual record types for details.
      87                 :             :  */
      88                 :             : void
      89                 :     2477435 : LogicalDecodingProcessRecord(LogicalDecodingContext *ctx, XLogReaderState *record)
      90                 :             : {
      91                 :             :     XLogRecordBuffer buf;
      92                 :             :     TransactionId txid;
      93                 :             :     RmgrData    rmgr;
      94                 :             : 
      95                 :     2477435 :     buf.origptr = ctx->reader->ReadRecPtr;
      96                 :     2477435 :     buf.endptr = ctx->reader->EndRecPtr;
      97                 :     2477435 :     buf.record = record;
      98                 :             : 
      99                 :     2477435 :     txid = XLogRecGetTopXid(record);
     100                 :             : 
     101                 :             :     /*
     102                 :             :      * If the top-level xid is valid, we need to assign the subxact to the
     103                 :             :      * top-level xact. We need to do this for all records, hence we do it
     104                 :             :      * before the switch.
     105                 :             :      */
     106         [ +  + ]:     2477435 :     if (TransactionIdIsValid(txid))
     107                 :             :     {
     108                 :         713 :         ReorderBufferAssignChild(ctx->reorder,
     109                 :             :                                  txid,
     110                 :         713 :                                  XLogRecGetXid(record),
     111                 :             :                                  buf.origptr);
     112                 :             :     }
     113                 :             : 
     114                 :     2477435 :     rmgr = GetRmgr(XLogRecGetRmid(record));
     115                 :             : 
     116         [ +  + ]:     2477435 :     if (rmgr.rm_decode != NULL)
     117                 :     1906046 :         rmgr.rm_decode(ctx, &buf);
     118                 :             :     else
     119                 :             :     {
     120                 :             :         /* just deal with xid, and done */
     121                 :      571389 :         ReorderBufferProcessXid(ctx->reorder, XLogRecGetXid(record),
     122                 :             :                                 buf.origptr);
     123                 :             :     }
     124                 :     2477408 : }
     125                 :             : 
     126                 :             : /*
     127                 :             :  * Handle rmgr XLOG_ID records for LogicalDecodingProcessRecord().
     128                 :             :  */
     129                 :             : void
     130                 :       11956 : xlog_decode(LogicalDecodingContext *ctx, XLogRecordBuffer *buf)
     131                 :             : {
     132                 :       11956 :     SnapBuild  *builder = ctx->snapshot_builder;
     133                 :       11956 :     uint8       info = XLogRecGetInfo(buf->record) & ~XLR_INFO_MASK;
     134                 :             : 
     135                 :       11956 :     ReorderBufferProcessXid(ctx->reorder, XLogRecGetXid(buf->record),
     136                 :             :                             buf->origptr);
     137                 :             : 
     138   [ +  +  -  +  :       11956 :     switch (info)
                      - ]
     139                 :             :     {
     140                 :             :             /* this is also used in END_OF_RECOVERY checkpoints */
     141                 :         144 :         case XLOG_CHECKPOINT_SHUTDOWN:
     142                 :             :         case XLOG_END_OF_RECOVERY:
     143                 :         144 :             SnapBuildSerializationPoint(builder, buf->origptr);
     144                 :             : 
     145                 :         144 :             break;
     146                 :          95 :         case XLOG_CHECKPOINT_ONLINE:
     147                 :             : 
     148                 :             :             /*
     149                 :             :              * a RUNNING_XACTS record will have been logged near to this, we
     150                 :             :              * can restart from there.
     151                 :             :              */
     152                 :          95 :             break;
     153                 :           0 :         case XLOG_LOGICAL_DECODING_STATUS_CHANGE:
     154                 :             :             {
     155                 :             :                 bool        logical_decoding;
     156                 :             : 
     157                 :           0 :                 memcpy(&logical_decoding, XLogRecGetData(buf->record), sizeof(bool));
     158                 :             : 
     159                 :             :                 /*
     160                 :             :                  * Error out as we should not decode this WAL record.
     161                 :             :                  *
     162                 :             :                  * Logical decoding is disabled, and existing logical slots on
     163                 :             :                  * the standby are invalidated when this WAL record is
     164                 :             :                  * replayed. No logical decoder can process this WAL record
     165                 :             :                  * until replay completes, and by then the slots are already
     166                 :             :                  * invalidated. Furthermore, no new logical slots can be
     167                 :             :                  * created while logical decoding is disabled. This cannot
     168                 :             :                  * occur even on primary either, since it will not restart
     169                 :             :                  * with wal_level < replica if any logical slots exist.
     170                 :             :                  */
     171         [ #  # ]:           0 :                 elog(ERROR, "unexpected logical decoding status change %d",
     172                 :             :                      logical_decoding);
     173                 :             : 
     174                 :             :                 break;
     175                 :             :             }
     176                 :       11717 :         case XLOG_NOOP:
     177                 :             :         case XLOG_NEXTOID:
     178                 :             :         case XLOG_SWITCH:
     179                 :             :         case XLOG_BACKUP_END:
     180                 :             :         case XLOG_PARAMETER_CHANGE:
     181                 :             :         case XLOG_RESTORE_POINT:
     182                 :             :         case XLOG_FPW_CHANGE:
     183                 :             :         case XLOG_FPI_FOR_HINT:
     184                 :             :         case XLOG_FPI:
     185                 :             :         case XLOG_OVERWRITE_CONTRECORD:
     186                 :             :         case XLOG_CHECKPOINT_REDO:
     187                 :       11717 :             break;
     188                 :           0 :         default:
     189         [ #  # ]:           0 :             elog(ERROR, "unexpected RM_XLOG_ID record type: %u", info);
     190                 :             :     }
     191                 :       11956 : }
     192                 :             : 
     193                 :             : void
     194                 :           0 : xlog2_decode(LogicalDecodingContext *ctx, XLogRecordBuffer *buf)
     195                 :             : {
     196                 :           0 :     uint8       info = XLogRecGetInfo(buf->record) & ~XLR_INFO_MASK;
     197                 :             : 
     198                 :           0 :     ReorderBufferProcessXid(ctx->reorder, XLogRecGetXid(buf->record), buf->origptr);
     199                 :             : 
     200         [ #  # ]:           0 :     switch (info)
     201                 :             :     {
     202                 :           0 :         case XLOG2_CHECKSUMS:
     203                 :           0 :             break;
     204                 :           0 :         default:
     205         [ #  # ]:           0 :             elog(ERROR, "unexpected RM_XLOG2_ID record type: %u", info);
     206                 :             :     }
     207                 :           0 : }
     208                 :             : 
     209                 :             : /*
     210                 :             :  * Handle rmgr XACT_ID records for LogicalDecodingProcessRecord().
     211                 :             :  */
     212                 :             : void
     213                 :       14478 : xact_decode(LogicalDecodingContext *ctx, XLogRecordBuffer *buf)
     214                 :             : {
     215                 :       14478 :     SnapBuild  *builder = ctx->snapshot_builder;
     216                 :       14478 :     ReorderBuffer *reorder = ctx->reorder;
     217                 :       14478 :     XLogReaderState *r = buf->record;
     218                 :       14478 :     uint8       info = XLogRecGetInfo(r) & XLOG_XACT_OPMASK;
     219                 :             : 
     220                 :             :     /*
     221                 :             :      * If the snapshot isn't yet fully built, we cannot decode anything, so
     222                 :             :      * bail out.
     223                 :             :      */
     224         [ +  + ]:       14478 :     if (SnapBuildCurrentState(builder) < SNAPBUILD_FULL_SNAPSHOT)
     225                 :          25 :         return;
     226                 :             : 
     227   [ +  +  +  +  :       14453 :     switch (info)
                   +  - ]
     228                 :             :     {
     229                 :        5370 :         case XLOG_XACT_COMMIT:
     230                 :             :         case XLOG_XACT_COMMIT_PREPARED:
     231                 :             :             {
     232                 :             :                 xl_xact_commit *xlrec;
     233                 :             :                 xl_xact_parsed_commit parsed;
     234                 :             :                 TransactionId xid;
     235                 :        5370 :                 bool        two_phase = false;
     236                 :             : 
     237                 :        5370 :                 xlrec = (xl_xact_commit *) XLogRecGetData(r);
     238                 :        5370 :                 ParseCommitRecord(XLogRecGetInfo(buf->record), xlrec, &parsed);
     239                 :             : 
     240         [ +  + ]:        5370 :                 if (!TransactionIdIsValid(parsed.twophase_xid))
     241                 :        5239 :                     xid = XLogRecGetXid(r);
     242                 :             :                 else
     243                 :         131 :                     xid = parsed.twophase_xid;
     244                 :             : 
     245                 :             :                 /*
     246                 :             :                  * We would like to process the transaction in a two-phase
     247                 :             :                  * manner iff output plugin supports two-phase commits and
     248                 :             :                  * doesn't filter the transaction at prepare time.
     249                 :             :                  */
     250         [ +  + ]:        5370 :                 if (info == XLOG_XACT_COMMIT_PREPARED)
     251                 :         131 :                     two_phase = !(FilterPrepare(ctx, xid,
     252                 :         131 :                                                 parsed.twophase_gid));
     253                 :             : 
     254                 :        5370 :                 DecodeCommit(ctx, buf, &parsed, xid, two_phase);
     255                 :        5345 :                 break;
     256                 :             :             }
     257                 :         346 :         case XLOG_XACT_ABORT:
     258                 :             :         case XLOG_XACT_ABORT_PREPARED:
     259                 :             :             {
     260                 :             :                 xl_xact_abort *xlrec;
     261                 :             :                 xl_xact_parsed_abort parsed;
     262                 :             :                 TransactionId xid;
     263                 :         346 :                 bool        two_phase = false;
     264                 :             : 
     265                 :         346 :                 xlrec = (xl_xact_abort *) XLogRecGetData(r);
     266                 :         346 :                 ParseAbortRecord(XLogRecGetInfo(buf->record), xlrec, &parsed);
     267                 :             : 
     268         [ +  + ]:         346 :                 if (!TransactionIdIsValid(parsed.twophase_xid))
     269                 :         295 :                     xid = XLogRecGetXid(r);
     270                 :             :                 else
     271                 :          51 :                     xid = parsed.twophase_xid;
     272                 :             : 
     273                 :             :                 /*
     274                 :             :                  * We would like to process the transaction in a two-phase
     275                 :             :                  * manner iff output plugin supports two-phase commits and
     276                 :             :                  * doesn't filter the transaction at prepare time.
     277                 :             :                  */
     278         [ +  + ]:         346 :                 if (info == XLOG_XACT_ABORT_PREPARED)
     279                 :          51 :                     two_phase = !(FilterPrepare(ctx, xid,
     280                 :          51 :                                                 parsed.twophase_gid));
     281                 :             : 
     282                 :         346 :                 DecodeAbort(ctx, buf, &parsed, xid, two_phase);
     283                 :         346 :                 break;
     284                 :             :             }
     285                 :         144 :         case XLOG_XACT_ASSIGNMENT:
     286                 :             : 
     287                 :             :             /*
     288                 :             :              * We assign subxact to the toplevel xact while processing each
     289                 :             :              * record if required.  So, we don't need to do anything here. See
     290                 :             :              * LogicalDecodingProcessRecord.
     291                 :             :              */
     292                 :         144 :             break;
     293                 :        8388 :         case XLOG_XACT_INVALIDATIONS:
     294                 :             :             {
     295                 :             :                 TransactionId xid;
     296                 :             :                 xl_xact_invals *invals;
     297                 :             : 
     298                 :        8388 :                 xid = XLogRecGetXid(r);
     299                 :        8388 :                 invals = (xl_xact_invals *) XLogRecGetData(r);
     300                 :             : 
     301                 :             :                 /*
     302                 :             :                  * Execute the invalidations for xid-less transactions,
     303                 :             :                  * otherwise, accumulate them so that they can be processed at
     304                 :             :                  * the commit time.
     305                 :             :                  */
     306         [ +  + ]:        8388 :                 if (TransactionIdIsValid(xid))
     307                 :             :                 {
     308         [ +  + ]:        8331 :                     if (!ctx->fast_forward)
     309                 :        8249 :                         ReorderBufferAddInvalidations(reorder, xid,
     310                 :             :                                                       buf->origptr,
     311                 :        8249 :                                                       invals->nmsgs,
     312                 :        8249 :                                                       invals->msgs);
     313                 :        8331 :                     ReorderBufferXidSetCatalogChanges(ctx->reorder, xid,
     314                 :             :                                                       buf->origptr);
     315                 :             :                 }
     316         [ +  - ]:          57 :                 else if (!ctx->fast_forward)
     317                 :          57 :                     ReorderBufferImmediateInvalidation(ctx->reorder,
     318                 :          57 :                                                        invals->nmsgs,
     319                 :          57 :                                                        invals->msgs);
     320                 :             : 
     321                 :        8388 :                 break;
     322                 :             :             }
     323                 :         205 :         case XLOG_XACT_PREPARE:
     324                 :             :             {
     325                 :             :                 xl_xact_parsed_prepare parsed;
     326                 :             :                 xl_xact_prepare *xlrec;
     327                 :             : 
     328                 :             :                 /* ok, parse it */
     329                 :         205 :                 xlrec = (xl_xact_prepare *) XLogRecGetData(r);
     330                 :         205 :                 ParsePrepareRecord(XLogRecGetInfo(buf->record),
     331                 :             :                                    xlrec, &parsed);
     332                 :             : 
     333                 :             :                 /*
     334                 :             :                  * We would like to process the transaction in a two-phase
     335                 :             :                  * manner iff output plugin supports two-phase commits and
     336                 :             :                  * doesn't filter the transaction at prepare time.
     337                 :             :                  */
     338         [ +  + ]:         205 :                 if (FilterPrepare(ctx, parsed.twophase_xid,
     339                 :             :                                   parsed.twophase_gid))
     340                 :             :                 {
     341                 :          21 :                     ReorderBufferProcessXid(reorder, parsed.twophase_xid,
     342                 :             :                                             buf->origptr);
     343                 :          21 :                     break;
     344                 :             :                 }
     345                 :             : 
     346                 :             :                 /*
     347                 :             :                  * Note that if the prepared transaction has locked [user]
     348                 :             :                  * catalog tables exclusively then decoding prepare can block
     349                 :             :                  * till the main transaction is committed because it needs to
     350                 :             :                  * lock the catalog tables.
     351                 :             :                  *
     352                 :             :                  * XXX Now, this can even lead to a deadlock if the prepare
     353                 :             :                  * transaction is waiting to get it logically replicated for
     354                 :             :                  * distributed 2PC. This can be avoided by disallowing
     355                 :             :                  * preparing transactions that have locked [user] catalog
     356                 :             :                  * tables exclusively but as of now, we ask users not to do
     357                 :             :                  * such an operation.
     358                 :             :                  */
     359                 :         184 :                 DecodePrepare(ctx, buf, &parsed);
     360                 :         184 :                 break;
     361                 :             :             }
     362                 :           0 :         default:
     363         [ #  # ]:           0 :             elog(ERROR, "unexpected RM_XACT_ID record type: %u", info);
     364                 :             :     }
     365                 :             : }
     366                 :             : 
     367                 :             : /*
     368                 :             :  * Handle rmgr STANDBY_ID records for LogicalDecodingProcessRecord().
     369                 :             :  */
     370                 :             : void
     371                 :        5660 : standby_decode(LogicalDecodingContext *ctx, XLogRecordBuffer *buf)
     372                 :             : {
     373                 :        5660 :     SnapBuild  *builder = ctx->snapshot_builder;
     374                 :        5660 :     XLogReaderState *r = buf->record;
     375                 :        5660 :     uint8       info = XLogRecGetInfo(r) & ~XLR_INFO_MASK;
     376                 :             : 
     377                 :        5660 :     ReorderBufferProcessXid(ctx->reorder, XLogRecGetXid(r), buf->origptr);
     378                 :             : 
     379   [ +  +  +  - ]:        5660 :     switch (info)
     380                 :             :     {
     381                 :        2020 :         case XLOG_RUNNING_XACTS:
     382                 :             :             {
     383                 :        2020 :                 xl_running_xacts *running = (xl_running_xacts *) XLogRecGetData(r);
     384                 :             : 
     385                 :             :                 /*
     386                 :             :                  * Update this decoder's idea of transactions currently
     387                 :             :                  * running.  In doing so we will determine whether we have
     388                 :             :                  * reached consistent status.
     389                 :             :                  */
     390                 :        2020 :                 SnapBuildProcessRunningXacts(builder, buf->origptr, running);
     391                 :             : 
     392                 :             :                 /*
     393                 :             :                  * Abort all transactions that we keep track of, that are
     394                 :             :                  * older than the record's oldestRunningXid. This is the most
     395                 :             :                  * convenient spot for doing so since, in contrast to shutdown
     396                 :             :                  * or end-of-recovery checkpoints, we have information about
     397                 :             :                  * all running transactions which includes prepared ones,
     398                 :             :                  * while shutdown checkpoints just know that no non-prepared
     399                 :             :                  * transactions are in progress.
     400                 :             :                  */
     401                 :        2018 :                 ReorderBufferAbortOld(ctx->reorder, running->oldestRunningXid);
     402                 :             :             }
     403                 :        2018 :             break;
     404                 :        3579 :         case XLOG_STANDBY_LOCK:
     405                 :        3579 :             break;
     406                 :          61 :         case XLOG_INVALIDATIONS:
     407                 :             : 
     408                 :             :             /*
     409                 :             :              * We are processing the invalidations at the command level via
     410                 :             :              * XLOG_XACT_INVALIDATIONS.  So we don't need to do anything here.
     411                 :             :              */
     412                 :          61 :             break;
     413                 :           0 :         default:
     414         [ #  # ]:           0 :             elog(ERROR, "unexpected RM_STANDBY_ID record type: %u", info);
     415                 :             :     }
     416                 :        5658 : }
     417                 :             : 
     418                 :             : /*
     419                 :             :  * Handle rmgr HEAP2_ID records for LogicalDecodingProcessRecord().
     420                 :             :  */
     421                 :             : void
     422                 :       49692 : heap2_decode(LogicalDecodingContext *ctx, XLogRecordBuffer *buf)
     423                 :             : {
     424                 :       49692 :     uint8       info = XLogRecGetInfo(buf->record) & XLOG_HEAP_OPMASK;
     425                 :       49692 :     TransactionId xid = XLogRecGetXid(buf->record);
     426                 :       49692 :     SnapBuild  *builder = ctx->snapshot_builder;
     427                 :             : 
     428                 :       49692 :     ReorderBufferProcessXid(ctx->reorder, xid, buf->origptr);
     429                 :             : 
     430                 :             :     /*
     431                 :             :      * If we don't have snapshot or we are just fast-forwarding, there is no
     432                 :             :      * point in decoding data changes. However, it's crucial to build the base
     433                 :             :      * snapshot during fast-forward mode (as is done in
     434                 :             :      * SnapBuildProcessChange()) because we require the snapshot's xmin when
     435                 :             :      * determining the candidate catalog_xmin for the replication slot. See
     436                 :             :      * SnapBuildProcessRunningXacts().
     437                 :             :      */
     438         [ +  + ]:       49692 :     if (SnapBuildCurrentState(builder) < SNAPBUILD_FULL_SNAPSHOT)
     439                 :          14 :         return;
     440                 :             : 
     441   [ +  +  +  +  :       49678 :     switch (info)
                      - ]
     442                 :             :     {
     443                 :        9254 :         case XLOG_HEAP2_MULTI_INSERT:
     444         [ +  - ]:        9254 :             if (SnapBuildProcessChange(builder, xid, buf->origptr) &&
     445         [ +  + ]:        9254 :                 !ctx->fast_forward &&
     446         [ +  + ]:        9138 :                 !change_useless_for_repack(buf))
     447                 :        8971 :                 DecodeMultiInsert(ctx, buf);
     448                 :        9254 :             break;
     449                 :       38757 :         case XLOG_HEAP2_NEW_CID:
     450         [ +  + ]:       38757 :             if (!ctx->fast_forward)
     451                 :             :             {
     452                 :             :                 xl_heap_new_cid *xlrec;
     453                 :             : 
     454                 :       38333 :                 xlrec = (xl_heap_new_cid *) XLogRecGetData(buf->record);
     455                 :       38333 :                 SnapBuildProcessNewCid(builder, xid, buf->origptr, xlrec);
     456                 :             :             }
     457                 :       38757 :             break;
     458                 :          90 :         case XLOG_HEAP2_REWRITE:
     459                 :             : 
     460                 :             :             /*
     461                 :             :              * Although these records only exist to serve the needs of logical
     462                 :             :              * decoding, all the work happens as part of crash or archive
     463                 :             :              * recovery, so we don't need to do anything here.
     464                 :             :              */
     465                 :          90 :             break;
     466                 :             : 
     467                 :             :             /*
     468                 :             :              * Everything else here is just low level physical stuff we're not
     469                 :             :              * interested in.
     470                 :             :              */
     471                 :        1577 :         case XLOG_HEAP2_PRUNE_ON_ACCESS:
     472                 :             :         case XLOG_HEAP2_PRUNE_VACUUM_SCAN:
     473                 :             :         case XLOG_HEAP2_PRUNE_VACUUM_CLEANUP:
     474                 :             :         case XLOG_HEAP2_LOCK_UPDATED:
     475                 :        1577 :             break;
     476                 :           0 :         default:
     477         [ #  # ]:           0 :             elog(ERROR, "unexpected RM_HEAP2_ID record type: %u", info);
     478                 :             :     }
     479                 :             : }
     480                 :             : 
     481                 :             : /*
     482                 :             :  * Handle rmgr HEAP_ID records for LogicalDecodingProcessRecord().
     483                 :             :  */
     484                 :             : void
     485                 :     1824138 : heap_decode(LogicalDecodingContext *ctx, XLogRecordBuffer *buf)
     486                 :             : {
     487                 :     1824138 :     uint8       info = XLogRecGetInfo(buf->record) & XLOG_HEAP_OPMASK;
     488                 :     1824138 :     TransactionId xid = XLogRecGetXid(buf->record);
     489                 :     1824138 :     SnapBuild  *builder = ctx->snapshot_builder;
     490                 :             : 
     491                 :     1824138 :     ReorderBufferProcessXid(ctx->reorder, xid, buf->origptr);
     492                 :             : 
     493                 :             :     /*
     494                 :             :      * If we don't have snapshot or we are just fast-forwarding, there is no
     495                 :             :      * point in decoding data changes. However, it's crucial to build the base
     496                 :             :      * snapshot during fast-forward mode (as is done in
     497                 :             :      * SnapBuildProcessChange()) because we require the snapshot's xmin when
     498                 :             :      * determining the candidate catalog_xmin for the replication slot. See
     499                 :             :      * SnapBuildProcessRunningXacts().
     500                 :             :      */
     501         [ +  + ]:     1824138 :     if (SnapBuildCurrentState(builder) < SNAPBUILD_FULL_SNAPSHOT)
     502                 :          11 :         return;
     503                 :             : 
     504   [ +  +  +  +  :     1824127 :     switch (info)
             +  +  +  - ]
     505                 :             :     {
     506                 :     1228832 :         case XLOG_HEAP_INSERT:
     507         [ +  - ]:     1228832 :             if (SnapBuildProcessChange(builder, xid, buf->origptr) &&
     508         [ +  + ]:     1228832 :                 !ctx->fast_forward &&
     509         [ +  + ]:     1228626 :                 !change_useless_for_repack(buf))
     510                 :     1228187 :                 DecodeInsert(ctx, buf);
     511                 :     1228832 :             break;
     512                 :             : 
     513                 :             :             /*
     514                 :             :              * Treat HOT update as normal updates. There is no useful
     515                 :             :              * information in the fact that we could make it a HOT update
     516                 :             :              * locally and the WAL layout is compatible.
     517                 :             :              */
     518                 :      162692 :         case XLOG_HEAP_HOT_UPDATE:
     519                 :             :         case XLOG_HEAP_UPDATE:
     520         [ +  - ]:      162692 :             if (SnapBuildProcessChange(builder, xid, buf->origptr) &&
     521         [ +  + ]:      162692 :                 !ctx->fast_forward &&
     522         [ +  + ]:      162675 :                 !change_useless_for_repack(buf))
     523                 :      162607 :                 DecodeUpdate(ctx, buf);
     524                 :      162692 :             break;
     525                 :             : 
     526                 :      241177 :         case XLOG_HEAP_DELETE:
     527         [ +  - ]:      241177 :             if (SnapBuildProcessChange(builder, xid, buf->origptr) &&
     528         [ +  + ]:      241177 :                 !ctx->fast_forward &&
     529         [ +  + ]:      241083 :                 !change_useless_for_repack(buf))
     530                 :      241031 :                 DecodeDelete(ctx, buf);
     531                 :      241177 :             break;
     532                 :             : 
     533                 :          65 :         case XLOG_HEAP_TRUNCATE:
     534         [ +  - ]:          65 :             if (SnapBuildProcessChange(builder, xid, buf->origptr) &&
     535         [ +  + ]:          65 :                 !ctx->fast_forward &&
     536         [ +  - ]:          63 :                 !change_useless_for_repack(buf))
     537                 :          63 :                 DecodeTruncate(ctx, buf);
     538                 :          65 :             break;
     539                 :             : 
     540                 :        1628 :         case XLOG_HEAP_INPLACE:
     541                 :             : 
     542                 :             :             /*
     543                 :             :              * Inplace updates are only ever performed on catalog tuples and
     544                 :             :              * can, per definition, not change tuple visibility.  Since we
     545                 :             :              * also don't decode catalog tuples, we're not interested in the
     546                 :             :              * record's contents.
     547                 :             :              */
     548                 :        1628 :             break;
     549                 :             : 
     550                 :       19702 :         case XLOG_HEAP_CONFIRM:
     551         [ +  - ]:       19702 :             if (SnapBuildProcessChange(builder, xid, buf->origptr) &&
     552         [ +  - ]:       19702 :                 !ctx->fast_forward &&
     553         [ +  - ]:       19702 :                 !change_useless_for_repack(buf))
     554                 :       19702 :                 DecodeSpecConfirm(ctx, buf);
     555                 :       19702 :             break;
     556                 :             : 
     557                 :      170031 :         case XLOG_HEAP_LOCK:
     558                 :             :             /* we don't care about row level locks for now */
     559                 :      170031 :             break;
     560                 :             : 
     561                 :           0 :         default:
     562         [ #  # ]:           0 :             elog(ERROR, "unexpected RM_HEAP_ID record type: %u", info);
     563                 :             :             break;
     564                 :             :     }
     565                 :             : }
     566                 :             : 
     567                 :             : /*
     568                 :             :  * Ask output plugin whether we want to skip this PREPARE and send
     569                 :             :  * this transaction as a regular commit later.
     570                 :             :  */
     571                 :             : static inline bool
     572                 :         387 : FilterPrepare(LogicalDecodingContext *ctx, TransactionId xid,
     573                 :             :               const char *gid)
     574                 :             : {
     575                 :             :     /*
     576                 :             :      * Skip if decoding of two-phase transactions at PREPARE time is not
     577                 :             :      * enabled. In that case, all two-phase transactions are considered
     578                 :             :      * filtered out and will be applied as regular transactions at COMMIT
     579                 :             :      * PREPARED.
     580                 :             :      */
     581         [ +  + ]:         387 :     if (!ctx->twophase)
     582                 :          23 :         return true;
     583                 :             : 
     584                 :             :     /*
     585                 :             :      * The filter_prepare callback is optional. When not supplied, all
     586                 :             :      * prepared transactions should go through.
     587                 :             :      */
     588         [ +  + ]:         364 :     if (ctx->callbacks.filter_prepare_cb == NULL)
     589                 :         194 :         return false;
     590                 :             : 
     591                 :         170 :     return filter_prepare_cb_wrapper(ctx, xid, gid);
     592                 :             : }
     593                 :             : 
     594                 :             : static inline bool
     595                 :     1645906 : FilterByOrigin(LogicalDecodingContext *ctx, ReplOriginId origin_id)
     596                 :             : {
     597         [ +  + ]:     1645906 :     if (ctx->callbacks.filter_by_origin_cb == NULL)
     598                 :         229 :         return false;
     599                 :             : 
     600                 :     1645677 :     return filter_by_origin_cb_wrapper(ctx, origin_id);
     601                 :             : }
     602                 :             : 
     603                 :             : /*
     604                 :             :  * Handle rmgr LOGICALMSG_ID records for LogicalDecodingProcessRecord().
     605                 :             :  */
     606                 :             : void
     607                 :         122 : logicalmsg_decode(LogicalDecodingContext *ctx, XLogRecordBuffer *buf)
     608                 :             : {
     609                 :         122 :     SnapBuild  *builder = ctx->snapshot_builder;
     610                 :         122 :     XLogReaderState *r = buf->record;
     611                 :         122 :     TransactionId xid = XLogRecGetXid(r);
     612                 :         122 :     uint8       info = XLogRecGetInfo(r) & ~XLR_INFO_MASK;
     613                 :         122 :     ReplOriginId origin_id = XLogRecGetOrigin(r);
     614                 :         122 :     Snapshot    snapshot = NULL;
     615                 :             :     xl_logical_message *message;
     616                 :             : 
     617         [ -  + ]:         122 :     if (info != XLOG_LOGICAL_MESSAGE)
     618         [ #  # ]:           0 :         elog(ERROR, "unexpected RM_LOGICALMSG_ID record type: %u", info);
     619                 :             : 
     620                 :         122 :     ReorderBufferProcessXid(ctx->reorder, XLogRecGetXid(r), buf->origptr);
     621                 :             : 
     622                 :             :     /* If we don't have snapshot, there is no point in decoding messages */
     623         [ -  + ]:         122 :     if (SnapBuildCurrentState(builder) < SNAPBUILD_FULL_SNAPSHOT)
     624                 :           0 :         return;
     625                 :             : 
     626                 :         122 :     message = (xl_logical_message *) XLogRecGetData(r);
     627                 :             : 
     628   [ +  +  +  + ]:         242 :     if (message->dbId != ctx->slot->data.database ||
     629                 :         120 :         FilterByOrigin(ctx, origin_id))
     630                 :           4 :         return;
     631                 :             : 
     632         [ +  + ]:         118 :     if (message->transactional &&
     633         [ -  + ]:          55 :         !SnapBuildProcessChange(builder, xid, buf->origptr))
     634                 :           0 :         return;
     635   [ +  +  +  - ]:         181 :     else if (!message->transactional &&
     636         [ +  + ]:         126 :              (SnapBuildCurrentState(builder) != SNAPBUILD_CONSISTENT ||
     637                 :          63 :               SnapBuildXactNeedsSkip(builder, buf->origptr)))
     638                 :          52 :         return;
     639                 :             : 
     640                 :             :     /*
     641                 :             :      * We also skip decoding in fast_forward mode. This check must be last
     642                 :             :      * because we don't want to set the processing_required flag unless we
     643                 :             :      * have a decodable message.
     644                 :             :      */
     645         [ +  + ]:          66 :     if (ctx->fast_forward)
     646                 :             :     {
     647                 :             :         /*
     648                 :             :          * We need to set processing_required flag to notify the message's
     649                 :             :          * existence to the caller. Usually, the flag is set when either the
     650                 :             :          * COMMIT or ABORT records are decoded, but this must be turned on
     651                 :             :          * here because the non-transactional logical message is decoded
     652                 :             :          * without waiting for these records.
     653                 :             :          */
     654         [ +  - ]:           2 :         if (!message->transactional)
     655                 :           2 :             ctx->processing_required = true;
     656                 :             : 
     657                 :           2 :         return;
     658                 :             :     }
     659                 :             : 
     660                 :             :     /*
     661                 :             :      * If this is a non-transactional change, get the snapshot we're expected
     662                 :             :      * to use. We only get here when the snapshot is consistent, and the
     663                 :             :      * change is not meant to be skipped.
     664                 :             :      *
     665                 :             :      * For transactional changes we don't need a snapshot, we'll use the
     666                 :             :      * regular snapshot maintained by ReorderBuffer. We just leave it NULL.
     667                 :             :      */
     668         [ +  + ]:          64 :     if (!message->transactional)
     669                 :           9 :         snapshot = SnapBuildGetOrBuildSnapshot(builder);
     670                 :             : 
     671                 :             :     /*
     672                 :             :      * Transactional messages use origptr as their LSN, like regular DML
     673                 :             :      * changes.
     674                 :             :      *
     675                 :             :      * Non-transactional messages use endptr instead, since we assume that
     676                 :             :      * each message is applied and confirmed as a unit of its own, like a
     677                 :             :      * commit. Using origptr would leave the replication slot's
     678                 :             :      * confirmed_flush pointing at the message record itself. Since decoding
     679                 :             :      * resumes from the confirmed position inclusive, the message could be
     680                 :             :      * decoded and sent again after a restart.
     681                 :             :      */
     682                 :          64 :     ReorderBufferQueueMessage(ctx->reorder, xid, snapshot,
     683                 :          64 :                               message->transactional ? buf->origptr : buf->endptr,
     684                 :          64 :                               message->transactional,
     685                 :          64 :                               message->message, /* first part of message is
     686                 :             :                                                  * prefix */
     687                 :             :                               message->message_size,
     688         [ +  + ]:          64 :                               message->message + message->prefix_size);
     689                 :             : }
     690                 :             : 
     691                 :             : /*
     692                 :             :  * Consolidated commit record handling between the different form of commit
     693                 :             :  * records.
     694                 :             :  *
     695                 :             :  * 'two_phase' indicates that caller wants to process the transaction in two
     696                 :             :  * phases, first process prepare if not already done and then process
     697                 :             :  * commit_prepared.
     698                 :             :  */
     699                 :             : static void
     700                 :        5370 : DecodeCommit(LogicalDecodingContext *ctx, XLogRecordBuffer *buf,
     701                 :             :              xl_xact_parsed_commit *parsed, TransactionId xid,
     702                 :             :              bool two_phase)
     703                 :             : {
     704                 :        5370 :     XLogRecPtr  origin_lsn = InvalidXLogRecPtr;
     705                 :        5370 :     TimestampTz commit_time = parsed->xact_time;
     706                 :        5370 :     ReplOriginId origin_id = XLogRecGetOrigin(buf->record);
     707                 :             :     int         i;
     708                 :             : 
     709         [ +  + ]:        5370 :     if (parsed->xinfo & XACT_XINFO_HAS_ORIGIN)
     710                 :             :     {
     711                 :         208 :         origin_lsn = parsed->origin_lsn;
     712                 :         208 :         commit_time = parsed->origin_timestamp;
     713                 :             :     }
     714                 :             : 
     715                 :        5370 :     SnapBuildCommitTxn(ctx->snapshot_builder, buf->origptr, xid,
     716                 :             :                        parsed->nsubxacts, parsed->subxacts,
     717                 :             :                        parsed->xinfo);
     718                 :             : 
     719                 :             :     /* ----
     720                 :             :      * Check whether we are interested in this specific transaction, and tell
     721                 :             :      * the reorderbuffer to forget the content of the (sub-)transactions
     722                 :             :      * if not.
     723                 :             :      *
     724                 :             :      * We can't just use ReorderBufferAbort() here, because we need to execute
     725                 :             :      * the transaction's invalidations.  This currently won't be needed if
     726                 :             :      * we're just skipping over the transaction because currently we only do
     727                 :             :      * so during startup, to get to the first transaction the client needs. As
     728                 :             :      * we have reset the catalog caches before starting to read WAL, and we
     729                 :             :      * haven't yet touched any catalogs, there can't be anything to invalidate.
     730                 :             :      * But if we're "forgetting" this commit because it happened in another
     731                 :             :      * database, the invalidations might be important, because they could be
     732                 :             :      * for shared catalogs and we might have loaded data into the relevant
     733                 :             :      * syscaches.
     734                 :             :      * ---
     735                 :             :      */
     736         [ +  + ]:        5370 :     if (DecodeTXNNeedSkip(ctx, buf, parsed->dbId, origin_id))
     737                 :             :     {
     738         [ +  + ]:        3517 :         for (i = 0; i < parsed->nsubxacts; i++)
     739                 :             :         {
     740                 :        1072 :             ReorderBufferForget(ctx->reorder, parsed->subxacts[i], buf->origptr);
     741                 :             :         }
     742                 :        2445 :         ReorderBufferForget(ctx->reorder, xid, buf->origptr);
     743                 :             : 
     744                 :        2445 :         return;
     745                 :             :     }
     746                 :             : 
     747                 :             :     /* tell the reorderbuffer about the surviving subtransactions */
     748         [ +  + ]:        3196 :     for (i = 0; i < parsed->nsubxacts; i++)
     749                 :             :     {
     750                 :         271 :         ReorderBufferCommitChild(ctx->reorder, xid, parsed->subxacts[i],
     751                 :             :                                  buf->origptr, buf->endptr);
     752                 :             :     }
     753                 :             : 
     754                 :             :     /*
     755                 :             :      * Send the final commit record if the transaction data is already
     756                 :             :      * decoded, otherwise, process the entire transaction.
     757                 :             :      */
     758         [ +  + ]:        2925 :     if (two_phase)
     759                 :             :     {
     760                 :          38 :         ReorderBufferFinishPrepared(ctx->reorder, xid, buf->origptr, buf->endptr,
     761                 :          38 :                                     SnapBuildGetTwoPhaseAt(ctx->snapshot_builder),
     762                 :             :                                     commit_time, origin_id, origin_lsn,
     763                 :          38 :                                     parsed->twophase_gid, true);
     764                 :             :     }
     765                 :             :     else
     766                 :             :     {
     767                 :        2887 :         ReorderBufferCommit(ctx->reorder, xid, buf->origptr, buf->endptr,
     768                 :             :                             commit_time, origin_id, origin_lsn);
     769                 :             :     }
     770                 :             : 
     771                 :             :     /*
     772                 :             :      * Update the decoding stats at transaction prepare/commit/abort.
     773                 :             :      * Additionally we send the stats when we spill or stream the changes to
     774                 :             :      * avoid losing them in case the decoding is interrupted. It is not clear
     775                 :             :      * that sending more or less frequently than this would be better.
     776                 :             :      */
     777                 :        2900 :     UpdateDecodingStats(ctx);
     778                 :             : }
     779                 :             : 
     780                 :             : /*
     781                 :             :  * Decode PREPARE record. Similar logic as in DecodeCommit.
     782                 :             :  *
     783                 :             :  * Note that we don't skip prepare even if have detected concurrent abort
     784                 :             :  * because it is quite possible that we had already sent some changes before we
     785                 :             :  * detect abort in which case we need to abort those changes in the subscriber.
     786                 :             :  * To abort such changes, we do send the prepare and then the rollback prepared
     787                 :             :  * which is what happened on the publisher-side as well. Now, we can invent a
     788                 :             :  * new abort API wherein in such cases we send abort and skip sending prepared
     789                 :             :  * and rollback prepared but then it is not that straightforward because we
     790                 :             :  * might have streamed this transaction by that time in which case it is
     791                 :             :  * handled when the rollback is encountered. It is not impossible to optimize
     792                 :             :  * the concurrent abort case but it can introduce design complexity w.r.t
     793                 :             :  * handling different cases so leaving it for now as it doesn't seem worth it.
     794                 :             :  */
     795                 :             : static void
     796                 :         184 : DecodePrepare(LogicalDecodingContext *ctx, XLogRecordBuffer *buf,
     797                 :             :               xl_xact_parsed_prepare *parsed)
     798                 :             : {
     799                 :         184 :     SnapBuild  *builder = ctx->snapshot_builder;
     800                 :         184 :     XLogRecPtr  origin_lsn = parsed->origin_lsn;
     801                 :         184 :     TimestampTz prepare_time = parsed->xact_time;
     802                 :         184 :     ReplOriginId origin_id = XLogRecGetOrigin(buf->record);
     803                 :             :     int         i;
     804                 :         184 :     TransactionId xid = parsed->twophase_xid;
     805                 :             : 
     806         [ +  + ]:         184 :     if (parsed->origin_timestamp != 0)
     807                 :           8 :         prepare_time = parsed->origin_timestamp;
     808                 :             : 
     809                 :             :     /*
     810                 :             :      * Remember the prepare info for a txn so that it can be used later in
     811                 :             :      * commit prepared if required. See ReorderBufferFinishPrepared.
     812                 :             :      */
     813         [ -  + ]:         184 :     if (!ReorderBufferRememberPrepareInfo(ctx->reorder, xid, buf->origptr,
     814                 :             :                                           buf->endptr, prepare_time, origin_id,
     815                 :             :                                           origin_lsn))
     816                 :           0 :         return;
     817                 :             : 
     818                 :             :     /* We can't start streaming unless a consistent state is reached. */
     819         [ +  + ]:         184 :     if (SnapBuildCurrentState(builder) < SNAPBUILD_CONSISTENT)
     820                 :             :     {
     821                 :           3 :         ReorderBufferSkipPrepare(ctx->reorder, xid);
     822                 :           3 :         return;
     823                 :             :     }
     824                 :             : 
     825                 :             :     /*
     826                 :             :      * Check whether we need to process this transaction. See
     827                 :             :      * DecodeTXNNeedSkip for the reasons why we sometimes want to skip the
     828                 :             :      * transaction.
     829                 :             :      *
     830                 :             :      * We can't call ReorderBufferForget as we did in DecodeCommit as the txn
     831                 :             :      * hasn't yet been committed, removing this txn before a commit might
     832                 :             :      * result in the computation of an incorrect restart_lsn. See
     833                 :             :      * SnapBuildProcessRunningXacts. But we need to process cache
     834                 :             :      * invalidations if there are any for the reasons mentioned in
     835                 :             :      * DecodeCommit.
     836                 :             :      */
     837         [ +  + ]:         181 :     if (DecodeTXNNeedSkip(ctx, buf, parsed->dbId, origin_id))
     838                 :             :     {
     839                 :         132 :         ReorderBufferSkipPrepare(ctx->reorder, xid);
     840                 :         132 :         ReorderBufferInvalidate(ctx->reorder, xid, buf->origptr);
     841                 :         132 :         return;
     842                 :             :     }
     843                 :             : 
     844                 :             :     /* Tell the reorderbuffer about the surviving subtransactions. */
     845         [ +  + ]:          50 :     for (i = 0; i < parsed->nsubxacts; i++)
     846                 :             :     {
     847                 :           1 :         ReorderBufferCommitChild(ctx->reorder, xid, parsed->subxacts[i],
     848                 :             :                                  buf->origptr, buf->endptr);
     849                 :             :     }
     850                 :             : 
     851                 :             :     /* replay actions of all transaction + subtransactions in order */
     852                 :          49 :     ReorderBufferPrepare(ctx->reorder, xid, parsed->twophase_gid);
     853                 :             : 
     854                 :             :     /*
     855                 :             :      * Update the decoding stats at transaction prepare/commit/abort.
     856                 :             :      * Additionally we send the stats when we spill or stream the changes to
     857                 :             :      * avoid losing them in case the decoding is interrupted. It is not clear
     858                 :             :      * that sending more or less frequently than this would be better.
     859                 :             :      */
     860                 :          49 :     UpdateDecodingStats(ctx);
     861                 :             : }
     862                 :             : 
     863                 :             : 
     864                 :             : /*
     865                 :             :  * Get the data from the various forms of abort records and pass it on to
     866                 :             :  * snapbuild.c and reorderbuffer.c.
     867                 :             :  *
     868                 :             :  * 'two_phase' indicates to finish prepared transaction.
     869                 :             :  */
     870                 :             : static void
     871                 :         346 : DecodeAbort(LogicalDecodingContext *ctx, XLogRecordBuffer *buf,
     872                 :             :             xl_xact_parsed_abort *parsed, TransactionId xid,
     873                 :             :             bool two_phase)
     874                 :             : {
     875                 :         346 :     XLogRecPtr  origin_lsn = InvalidXLogRecPtr;
     876                 :         346 :     TimestampTz abort_time = parsed->xact_time;
     877                 :         346 :     ReplOriginId origin_id = XLogRecGetOrigin(buf->record);
     878                 :             :     bool        skip_xact;
     879                 :             : 
     880         [ +  + ]:         346 :     if (parsed->xinfo & XACT_XINFO_HAS_ORIGIN)
     881                 :             :     {
     882                 :           4 :         origin_lsn = parsed->origin_lsn;
     883                 :           4 :         abort_time = parsed->origin_timestamp;
     884                 :             :     }
     885                 :             : 
     886                 :             :     /*
     887                 :             :      * Check whether we need to process this transaction. See
     888                 :             :      * DecodeTXNNeedSkip for the reasons why we sometimes want to skip the
     889                 :             :      * transaction.
     890                 :             :      */
     891                 :         346 :     skip_xact = DecodeTXNNeedSkip(ctx, buf, parsed->dbId, origin_id);
     892                 :             : 
     893                 :             :     /*
     894                 :             :      * Send the final rollback record for a prepared transaction unless we
     895                 :             :      * need to skip it. For non-two-phase xacts, simply forget the xact.
     896                 :             :      */
     897   [ +  +  +  + ]:         346 :     if (two_phase && !skip_xact)
     898                 :             :     {
     899                 :          13 :         ReorderBufferFinishPrepared(ctx->reorder, xid, buf->origptr, buf->endptr,
     900                 :             :                                     InvalidXLogRecPtr,
     901                 :             :                                     abort_time, origin_id, origin_lsn,
     902                 :          13 :                                     parsed->twophase_gid, false);
     903                 :             :     }
     904                 :             :     else
     905                 :             :     {
     906                 :             :         /*
     907                 :             :          * Remove tuplecid changes queued by the aborted subtransactions from
     908                 :             :          * the toplevel's list.
     909                 :             :          */
     910                 :         333 :         ReorderBufferCleanupAbortedTupleCids(ctx->reorder, xid,
     911                 :             :                                              parsed->nsubxacts,
     912                 :             :                                              parsed->subxacts);
     913                 :             : 
     914         [ +  + ]:         341 :         for (int i = 0; i < parsed->nsubxacts; i++)
     915                 :             :         {
     916                 :           8 :             ReorderBufferAbort(ctx->reorder, parsed->subxacts[i],
     917                 :           8 :                                buf->record->EndRecPtr, abort_time);
     918                 :             :         }
     919                 :             : 
     920                 :         333 :         ReorderBufferAbort(ctx->reorder, xid, buf->record->EndRecPtr,
     921                 :             :                            abort_time);
     922                 :             :     }
     923                 :             : 
     924                 :             :     /* update the decoding stats */
     925                 :         346 :     UpdateDecodingStats(ctx);
     926                 :         346 : }
     927                 :             : 
     928                 :             : /*
     929                 :             :  * Parse XLOG_HEAP_INSERT (not MULTI_INSERT!) records into tuplebufs.
     930                 :             :  *
     931                 :             :  * Inserts can contain the new tuple.
     932                 :             :  */
     933                 :             : static void
     934                 :     1228187 : DecodeInsert(LogicalDecodingContext *ctx, XLogRecordBuffer *buf)
     935                 :             : {
     936                 :             :     Size        datalen;
     937                 :             :     char       *tupledata;
     938                 :             :     Size        tuplelen;
     939                 :     1228187 :     XLogReaderState *r = buf->record;
     940                 :             :     xl_heap_insert *xlrec;
     941                 :             :     ReorderBufferChange *change;
     942                 :             :     RelFileLocator target_locator;
     943                 :             : 
     944                 :     1228187 :     xlrec = (xl_heap_insert *) XLogRecGetData(r);
     945                 :             : 
     946                 :             :     /*
     947                 :             :      * Ignore insert records without new tuples (this does happen when
     948                 :             :      * raw_heap_insert marks the TOAST record as HEAP_INSERT_NO_LOGICAL).
     949                 :             :      */
     950         [ +  + ]:     1228187 :     if (!(xlrec->flags & XLH_INSERT_CONTAINS_NEW_TUPLE))
     951                 :        6501 :         return;
     952                 :             : 
     953                 :             :     /* only interested in our database */
     954                 :     1221915 :     XLogRecGetBlockTag(r, 0, &target_locator, NULL, NULL);
     955         [ -  + ]:     1221915 :     if (target_locator.dbOid != ctx->slot->data.database)
     956                 :           0 :         return;
     957                 :             : 
     958                 :             :     /* output plugin doesn't look for this origin, no need to queue */
     959         [ +  + ]:     1221915 :     if (FilterByOrigin(ctx, XLogRecGetOrigin(r)))
     960                 :         229 :         return;
     961                 :             : 
     962                 :     1221686 :     change = ReorderBufferAllocChange(ctx->reorder);
     963         [ +  + ]:     1221686 :     if (!(xlrec->flags & XLH_INSERT_IS_SPECULATIVE))
     964                 :     1201984 :         change->action = REORDER_BUFFER_CHANGE_INSERT;
     965                 :             :     else
     966                 :       19702 :         change->action = REORDER_BUFFER_CHANGE_INTERNAL_SPEC_INSERT;
     967                 :     1221686 :     change->origin_id = XLogRecGetOrigin(r);
     968                 :             : 
     969                 :     1221686 :     memcpy(&change->data.tp.rlocator, &target_locator, sizeof(RelFileLocator));
     970                 :             : 
     971                 :     1221686 :     tupledata = XLogRecGetBlockData(r, 0, &datalen);
     972                 :     1221686 :     tuplelen = datalen - SizeOfHeapHeader;
     973                 :             : 
     974                 :     1221686 :     change->data.tp.newtuple =
     975                 :     1221686 :         ReorderBufferAllocTupleBuf(ctx->reorder, tuplelen);
     976                 :             : 
     977                 :     1221686 :     DecodeXLogTuple(tupledata, datalen, change->data.tp.newtuple);
     978                 :             : 
     979                 :     1221686 :     change->data.tp.clear_toast_afterwards = true;
     980                 :             : 
     981                 :     1221686 :     ReorderBufferQueueChange(ctx->reorder, XLogRecGetXid(r), buf->origptr,
     982                 :             :                              change,
     983                 :     1221686 :                              xlrec->flags & XLH_INSERT_ON_TOAST_RELATION);
     984                 :             : }
     985                 :             : 
     986                 :             : /*
     987                 :             :  * Parse XLOG_HEAP_UPDATE and XLOG_HEAP_HOT_UPDATE, which have the same layout
     988                 :             :  * in the record, from wal into proper tuplebufs.
     989                 :             :  *
     990                 :             :  * Updates can possibly contain a new tuple and the old primary key.
     991                 :             :  */
     992                 :             : static void
     993                 :      162607 : DecodeUpdate(LogicalDecodingContext *ctx, XLogRecordBuffer *buf)
     994                 :             : {
     995                 :      162607 :     XLogReaderState *r = buf->record;
     996                 :             :     xl_heap_update *xlrec;
     997                 :             :     ReorderBufferChange *change;
     998                 :             :     char       *data;
     999                 :             :     RelFileLocator target_locator;
    1000                 :             : 
    1001                 :      162607 :     xlrec = (xl_heap_update *) XLogRecGetData(r);
    1002                 :             : 
    1003                 :             :     /*
    1004                 :             :      * Ignore update records without a new tuple. This happens when the update
    1005                 :             :      * is done on a catalog relation, or when the caller of heap_update()
    1006                 :             :      * asked for the change not to be decoded, as REPACK (CONCURRENTLY) does
    1007                 :             :      * for the transient heap.
    1008                 :             :      */
    1009         [ +  + ]:      162607 :     if (!(xlrec->flags & XLH_UPDATE_CONTAINS_NEW_TUPLE))
    1010                 :        2691 :         return;
    1011                 :             : 
    1012                 :             :     /* only interested in our database */
    1013                 :      159918 :     XLogRecGetBlockTag(r, 0, &target_locator, NULL, NULL);
    1014         [ -  + ]:      159918 :     if (target_locator.dbOid != ctx->slot->data.database)
    1015                 :           0 :         return;
    1016                 :             : 
    1017                 :             :     /* output plugin doesn't look for this origin, no need to queue */
    1018         [ +  + ]:      159918 :     if (FilterByOrigin(ctx, XLogRecGetOrigin(r)))
    1019                 :           2 :         return;
    1020                 :             : 
    1021                 :      159916 :     change = ReorderBufferAllocChange(ctx->reorder);
    1022                 :      159916 :     change->action = REORDER_BUFFER_CHANGE_UPDATE;
    1023                 :      159916 :     change->origin_id = XLogRecGetOrigin(r);
    1024                 :      159916 :     memcpy(&change->data.tp.rlocator, &target_locator, sizeof(RelFileLocator));
    1025                 :             : 
    1026         [ +  - ]:      159916 :     if (xlrec->flags & XLH_UPDATE_CONTAINS_NEW_TUPLE)
    1027                 :             :     {
    1028                 :             :         Size        datalen;
    1029                 :             :         Size        tuplelen;
    1030                 :             : 
    1031                 :      159916 :         data = XLogRecGetBlockData(r, 0, &datalen);
    1032                 :             : 
    1033                 :      159916 :         tuplelen = datalen - SizeOfHeapHeader;
    1034                 :             : 
    1035                 :      159916 :         change->data.tp.newtuple =
    1036                 :      159916 :             ReorderBufferAllocTupleBuf(ctx->reorder, tuplelen);
    1037                 :             : 
    1038                 :      159916 :         DecodeXLogTuple(data, datalen, change->data.tp.newtuple);
    1039                 :             :     }
    1040                 :             : 
    1041         [ +  + ]:      159916 :     if (xlrec->flags & XLH_UPDATE_CONTAINS_OLD)
    1042                 :             :     {
    1043                 :             :         Size        datalen;
    1044                 :             :         Size        tuplelen;
    1045                 :             : 
    1046                 :             :         /* caution, remaining data in record is not aligned */
    1047                 :         500 :         data = XLogRecGetData(r) + SizeOfHeapUpdate;
    1048                 :         500 :         datalen = XLogRecGetDataLen(r) - SizeOfHeapUpdate;
    1049                 :         500 :         tuplelen = datalen - SizeOfHeapHeader;
    1050                 :             : 
    1051                 :         500 :         change->data.tp.oldtuple =
    1052                 :         500 :             ReorderBufferAllocTupleBuf(ctx->reorder, tuplelen);
    1053                 :             : 
    1054                 :         500 :         DecodeXLogTuple(data, datalen, change->data.tp.oldtuple);
    1055                 :             :     }
    1056                 :             : 
    1057                 :      159916 :     change->data.tp.clear_toast_afterwards = true;
    1058                 :             : 
    1059                 :      159916 :     ReorderBufferQueueChange(ctx->reorder, XLogRecGetXid(r), buf->origptr,
    1060                 :             :                              change, false);
    1061                 :             : }
    1062                 :             : 
    1063                 :             : /*
    1064                 :             :  * Parse XLOG_HEAP_DELETE from wal into proper tuplebufs.
    1065                 :             :  *
    1066                 :             :  * Deletes can possibly contain the old primary key.
    1067                 :             :  */
    1068                 :             : static void
    1069                 :      241031 : DecodeDelete(LogicalDecodingContext *ctx, XLogRecordBuffer *buf)
    1070                 :             : {
    1071                 :      241031 :     XLogReaderState *r = buf->record;
    1072                 :             :     xl_heap_delete *xlrec;
    1073                 :             :     ReorderBufferChange *change;
    1074                 :             :     RelFileLocator target_locator;
    1075                 :             : 
    1076                 :      241031 :     xlrec = (xl_heap_delete *) XLogRecGetData(r);
    1077                 :             : 
    1078                 :             :     /*
    1079                 :             :      * Skip changes that were marked as ignorable at origin.
    1080                 :             :      *
    1081                 :             :      * (This is used for changes that affect relations not visible to other
    1082                 :             :      * transactions, such as the transient table during concurrent repack.)
    1083                 :             :      */
    1084         [ -  + ]:      241031 :     if (xlrec->flags & XLH_DELETE_NO_LOGICAL)
    1085                 :          65 :         return;
    1086                 :             : 
    1087                 :             :     /* only interested in our database */
    1088                 :      241031 :     XLogRecGetBlockTag(r, 0, &target_locator, NULL, NULL);
    1089         [ +  + ]:      241031 :     if (target_locator.dbOid != ctx->slot->data.database)
    1090                 :          53 :         return;
    1091                 :             : 
    1092                 :             :     /* output plugin doesn't look for this origin, no need to queue */
    1093         [ +  + ]:      240978 :     if (FilterByOrigin(ctx, XLogRecGetOrigin(r)))
    1094                 :          12 :         return;
    1095                 :             : 
    1096                 :      240966 :     change = ReorderBufferAllocChange(ctx->reorder);
    1097                 :             : 
    1098         [ -  + ]:      240966 :     if (xlrec->flags & XLH_DELETE_IS_SUPER)
    1099                 :           0 :         change->action = REORDER_BUFFER_CHANGE_INTERNAL_SPEC_ABORT;
    1100                 :             :     else
    1101                 :      240966 :         change->action = REORDER_BUFFER_CHANGE_DELETE;
    1102                 :             : 
    1103                 :      240966 :     change->origin_id = XLogRecGetOrigin(r);
    1104                 :             : 
    1105                 :      240966 :     memcpy(&change->data.tp.rlocator, &target_locator, sizeof(RelFileLocator));
    1106                 :             : 
    1107                 :             :     /* old primary key stored */
    1108         [ +  + ]:      240966 :     if (xlrec->flags & XLH_DELETE_CONTAINS_OLD)
    1109                 :             :     {
    1110                 :      170950 :         Size        datalen = XLogRecGetDataLen(r) - SizeOfHeapDelete;
    1111                 :      170950 :         Size        tuplelen = datalen - SizeOfHeapHeader;
    1112                 :             : 
    1113                 :             :         Assert(XLogRecGetDataLen(r) > (SizeOfHeapDelete + SizeOfHeapHeader));
    1114                 :             : 
    1115                 :      170950 :         change->data.tp.oldtuple =
    1116                 :      170950 :             ReorderBufferAllocTupleBuf(ctx->reorder, tuplelen);
    1117                 :             : 
    1118                 :      170950 :         DecodeXLogTuple((char *) xlrec + SizeOfHeapDelete,
    1119                 :             :                         datalen, change->data.tp.oldtuple);
    1120                 :             :     }
    1121                 :             : 
    1122                 :      240966 :     change->data.tp.clear_toast_afterwards = true;
    1123                 :             : 
    1124                 :      240966 :     ReorderBufferQueueChange(ctx->reorder, XLogRecGetXid(r), buf->origptr,
    1125                 :             :                              change, false);
    1126                 :             : }
    1127                 :             : 
    1128                 :             : /*
    1129                 :             :  * Parse XLOG_HEAP_TRUNCATE from wal
    1130                 :             :  */
    1131                 :             : static void
    1132                 :          63 : DecodeTruncate(LogicalDecodingContext *ctx, XLogRecordBuffer *buf)
    1133                 :             : {
    1134                 :          63 :     XLogReaderState *r = buf->record;
    1135                 :             :     xl_heap_truncate *xlrec;
    1136                 :             :     ReorderBufferChange *change;
    1137                 :             : 
    1138                 :          63 :     xlrec = (xl_heap_truncate *) XLogRecGetData(r);
    1139                 :             : 
    1140                 :             :     /* only interested in our database */
    1141         [ -  + ]:          63 :     if (xlrec->dbId != ctx->slot->data.database)
    1142                 :           0 :         return;
    1143                 :             : 
    1144                 :             :     /* output plugin doesn't look for this origin, no need to queue */
    1145         [ +  + ]:          63 :     if (FilterByOrigin(ctx, XLogRecGetOrigin(r)))
    1146                 :           1 :         return;
    1147                 :             : 
    1148                 :          62 :     change = ReorderBufferAllocChange(ctx->reorder);
    1149                 :          62 :     change->action = REORDER_BUFFER_CHANGE_TRUNCATE;
    1150                 :          62 :     change->origin_id = XLogRecGetOrigin(r);
    1151         [ +  + ]:          62 :     if (xlrec->flags & XLH_TRUNCATE_CASCADE)
    1152                 :           1 :         change->data.truncate.cascade = true;
    1153         [ +  + ]:          62 :     if (xlrec->flags & XLH_TRUNCATE_RESTART_SEQS)
    1154                 :           2 :         change->data.truncate.restart_seqs = true;
    1155                 :          62 :     change->data.truncate.nrelids = xlrec->nrelids;
    1156                 :         124 :     change->data.truncate.relids = ReorderBufferAllocRelids(ctx->reorder,
    1157                 :          62 :                                                             xlrec->nrelids);
    1158                 :          62 :     memcpy(change->data.truncate.relids, xlrec->relids,
    1159                 :          62 :            xlrec->nrelids * sizeof(Oid));
    1160                 :          62 :     ReorderBufferQueueChange(ctx->reorder, XLogRecGetXid(r),
    1161                 :             :                              buf->origptr, change, false);
    1162                 :             : }
    1163                 :             : 
    1164                 :             : /*
    1165                 :             :  * Decode XLOG_HEAP2_MULTI_INSERT record into multiple tuplebufs.
    1166                 :             :  *
    1167                 :             :  * Currently MULTI_INSERT will always contain the full tuples.
    1168                 :             :  */
    1169                 :             : static void
    1170                 :        8971 : DecodeMultiInsert(LogicalDecodingContext *ctx, XLogRecordBuffer *buf)
    1171                 :             : {
    1172                 :        8971 :     XLogReaderState *r = buf->record;
    1173                 :             :     xl_heap_multi_insert *xlrec;
    1174                 :             :     int         i;
    1175                 :             :     char       *data;
    1176                 :             :     char       *tupledata;
    1177                 :             :     Size        tuplelen;
    1178                 :             :     RelFileLocator rlocator;
    1179                 :             : 
    1180                 :        8971 :     xlrec = (xl_heap_multi_insert *) XLogRecGetData(r);
    1181                 :             : 
    1182                 :             :     /*
    1183                 :             :      * Ignore insert records without new tuples.  This happens when a
    1184                 :             :      * multi_insert is done on a catalog or on a non-persistent relation.
    1185                 :             :      */
    1186         [ +  + ]:        8971 :     if (!(xlrec->flags & XLH_INSERT_CONTAINS_NEW_TUPLE))
    1187                 :        8953 :         return;
    1188                 :             : 
    1189                 :             :     /* only interested in our database */
    1190                 :          54 :     XLogRecGetBlockTag(r, 0, &rlocator, NULL, NULL);
    1191         [ +  + ]:          54 :     if (rlocator.dbOid != ctx->slot->data.database)
    1192                 :          36 :         return;
    1193                 :             : 
    1194                 :             :     /* output plugin doesn't look for this origin, no need to queue */
    1195         [ -  + ]:          18 :     if (FilterByOrigin(ctx, XLogRecGetOrigin(r)))
    1196                 :           0 :         return;
    1197                 :             : 
    1198                 :             :     /*
    1199                 :             :      * We know that this multi_insert isn't for a catalog, so the block should
    1200                 :             :      * always have data even if a full-page write of it is taken.
    1201                 :             :      */
    1202                 :          18 :     tupledata = XLogRecGetBlockData(r, 0, &tuplelen);
    1203                 :             :     Assert(tupledata != NULL);
    1204                 :             : 
    1205                 :          18 :     data = tupledata;
    1206         [ +  + ]:        1272 :     for (i = 0; i < xlrec->ntuples; i++)
    1207                 :             :     {
    1208                 :             :         ReorderBufferChange *change;
    1209                 :             :         xl_multi_insert_tuple *xlhdr;
    1210                 :             :         int         datalen;
    1211                 :             :         HeapTuple   tuple;
    1212                 :             :         HeapTupleHeader header;
    1213                 :             : 
    1214                 :        1254 :         change = ReorderBufferAllocChange(ctx->reorder);
    1215                 :        1254 :         change->action = REORDER_BUFFER_CHANGE_INSERT;
    1216                 :        1254 :         change->origin_id = XLogRecGetOrigin(r);
    1217                 :             : 
    1218                 :        1254 :         memcpy(&change->data.tp.rlocator, &rlocator, sizeof(RelFileLocator));
    1219                 :             : 
    1220                 :        1254 :         xlhdr = (xl_multi_insert_tuple *) SHORTALIGN(data);
    1221                 :        1254 :         data = ((char *) xlhdr) + SizeOfMultiInsertTuple;
    1222                 :        1254 :         datalen = xlhdr->datalen;
    1223                 :             : 
    1224                 :        1254 :         change->data.tp.newtuple =
    1225                 :        1254 :             ReorderBufferAllocTupleBuf(ctx->reorder, datalen);
    1226                 :             : 
    1227                 :        1254 :         tuple = change->data.tp.newtuple;
    1228                 :        1254 :         header = tuple->t_data;
    1229                 :             : 
    1230                 :             :         /* not a disk based tuple */
    1231                 :        1254 :         ItemPointerSetInvalid(&tuple->t_self);
    1232                 :             : 
    1233                 :             :         /*
    1234                 :             :          * We can only figure this out after reassembling the transactions.
    1235                 :             :          */
    1236                 :        1254 :         tuple->t_tableOid = InvalidOid;
    1237                 :             : 
    1238                 :        1254 :         tuple->t_len = datalen + SizeofHeapTupleHeader;
    1239                 :             : 
    1240                 :        1254 :         memset(header, 0, SizeofHeapTupleHeader);
    1241                 :             : 
    1242                 :        1254 :         memcpy((char *) tuple->t_data + SizeofHeapTupleHeader, data, datalen);
    1243                 :        1254 :         header->t_infomask = xlhdr->t_infomask;
    1244                 :        1254 :         header->t_infomask2 = xlhdr->t_infomask2;
    1245                 :        1254 :         header->t_hoff = xlhdr->t_hoff;
    1246                 :             : 
    1247                 :             :         /*
    1248                 :             :          * Reset toast reassembly state only after the last row in the last
    1249                 :             :          * xl_multi_insert_tuple record emitted by one heap_multi_insert()
    1250                 :             :          * call.
    1251                 :             :          */
    1252         [ +  + ]:        1254 :         if (xlrec->flags & XLH_INSERT_LAST_IN_MULTI &&
    1253         [ +  + ]:         222 :             (i + 1) == xlrec->ntuples)
    1254                 :          12 :             change->data.tp.clear_toast_afterwards = true;
    1255                 :             :         else
    1256                 :        1242 :             change->data.tp.clear_toast_afterwards = false;
    1257                 :             : 
    1258                 :        1254 :         ReorderBufferQueueChange(ctx->reorder, XLogRecGetXid(r),
    1259                 :             :                                  buf->origptr, change, false);
    1260                 :             : 
    1261                 :             :         /* move to the next xl_multi_insert_tuple entry */
    1262                 :        1254 :         data += datalen;
    1263                 :             :     }
    1264                 :             :     Assert(data == tupledata + tuplelen);
    1265                 :             : }
    1266                 :             : 
    1267                 :             : /*
    1268                 :             :  * Parse XLOG_HEAP_CONFIRM from wal into a confirmation change.
    1269                 :             :  *
    1270                 :             :  * This is pretty trivial, all the state essentially already setup by the
    1271                 :             :  * speculative insertion.
    1272                 :             :  */
    1273                 :             : static void
    1274                 :       19702 : DecodeSpecConfirm(LogicalDecodingContext *ctx, XLogRecordBuffer *buf)
    1275                 :             : {
    1276                 :       19702 :     XLogReaderState *r = buf->record;
    1277                 :             :     ReorderBufferChange *change;
    1278                 :             :     RelFileLocator target_locator;
    1279                 :             : 
    1280                 :             :     /* only interested in our database */
    1281                 :       19702 :     XLogRecGetBlockTag(r, 0, &target_locator, NULL, NULL);
    1282         [ -  + ]:       19702 :     if (target_locator.dbOid != ctx->slot->data.database)
    1283                 :           0 :         return;
    1284                 :             : 
    1285                 :             :     /* output plugin doesn't look for this origin, no need to queue */
    1286         [ -  + ]:       19702 :     if (FilterByOrigin(ctx, XLogRecGetOrigin(r)))
    1287                 :           0 :         return;
    1288                 :             : 
    1289                 :       19702 :     change = ReorderBufferAllocChange(ctx->reorder);
    1290                 :       19702 :     change->action = REORDER_BUFFER_CHANGE_INTERNAL_SPEC_CONFIRM;
    1291                 :       19702 :     change->origin_id = XLogRecGetOrigin(r);
    1292                 :             : 
    1293                 :       19702 :     memcpy(&change->data.tp.rlocator, &target_locator, sizeof(RelFileLocator));
    1294                 :             : 
    1295                 :       19702 :     change->data.tp.clear_toast_afterwards = true;
    1296                 :             : 
    1297                 :       19702 :     ReorderBufferQueueChange(ctx->reorder, XLogRecGetXid(r), buf->origptr,
    1298                 :             :                              change, false);
    1299                 :             : }
    1300                 :             : 
    1301                 :             : 
    1302                 :             : /*
    1303                 :             :  * Read a HeapTuple as WAL logged by heap_insert, heap_update and heap_delete
    1304                 :             :  * (but not by heap_multi_insert) into a tuplebuf.
    1305                 :             :  *
    1306                 :             :  * The size 'len' and the pointer 'data' in the record need to be
    1307                 :             :  * computed outside as they are record specific.
    1308                 :             :  */
    1309                 :             : static void
    1310                 :     1553052 : DecodeXLogTuple(char *data, Size len, HeapTuple tuple)
    1311                 :             : {
    1312                 :             :     xl_heap_header xlhdr;
    1313                 :     1553052 :     int         datalen = len - SizeOfHeapHeader;
    1314                 :             :     HeapTupleHeader header;
    1315                 :             : 
    1316                 :             :     Assert(datalen >= 0);
    1317                 :             : 
    1318                 :     1553052 :     tuple->t_len = datalen + SizeofHeapTupleHeader;
    1319                 :     1553052 :     header = tuple->t_data;
    1320                 :             : 
    1321                 :             :     /* not a disk based tuple */
    1322                 :     1553052 :     ItemPointerSetInvalid(&tuple->t_self);
    1323                 :             : 
    1324                 :             :     /* we can only figure this out after reassembling the transactions */
    1325                 :     1553052 :     tuple->t_tableOid = InvalidOid;
    1326                 :             : 
    1327                 :             :     /* data is not stored aligned, copy to aligned storage */
    1328                 :     1553052 :     memcpy(&xlhdr, data, SizeOfHeapHeader);
    1329                 :             : 
    1330                 :     1553052 :     memset(header, 0, SizeofHeapTupleHeader);
    1331                 :             : 
    1332                 :     1553052 :     memcpy(((char *) tuple->t_data) + SizeofHeapTupleHeader,
    1333                 :     1553052 :            data + SizeOfHeapHeader,
    1334                 :             :            datalen);
    1335                 :             : 
    1336                 :     1553052 :     header->t_infomask = xlhdr.t_infomask;
    1337                 :     1553052 :     header->t_infomask2 = xlhdr.t_infomask2;
    1338                 :     1553052 :     header->t_hoff = xlhdr.t_hoff;
    1339                 :     1553052 : }
    1340                 :             : 
    1341                 :             : /*
    1342                 :             :  * Check whether we are interested in this specific transaction.
    1343                 :             :  *
    1344                 :             :  * There can be several reasons we might not be interested in this
    1345                 :             :  * transaction:
    1346                 :             :  * 1) We might not be interested in decoding transactions up to this
    1347                 :             :  *    LSN. This can happen because we previously decoded it and now just
    1348                 :             :  *    are restarting or if we haven't assembled a consistent snapshot yet.
    1349                 :             :  * 2) The transaction happened in another database.
    1350                 :             :  * 3) The output plugin is not interested in the origin.
    1351                 :             :  * 4) We are doing fast-forwarding
    1352                 :             :  */
    1353                 :             : static bool
    1354                 :        5897 : DecodeTXNNeedSkip(LogicalDecodingContext *ctx, XLogRecordBuffer *buf,
    1355                 :             :                   Oid txn_dbid, ReplOriginId origin_id)
    1356                 :             : {
    1357   [ +  +  +  + ]:        5897 :     if (SnapBuildXactNeedsSkip(ctx->snapshot_builder, buf->origptr) ||
    1358   [ +  +  +  + ]:        6293 :         (txn_dbid != InvalidOid && txn_dbid != ctx->slot->data.database) ||
    1359                 :        3192 :         FilterByOrigin(ctx, origin_id))
    1360                 :        2757 :         return true;
    1361                 :             : 
    1362                 :             :     /*
    1363                 :             :      * We also skip decoding in fast_forward mode. In passing set the
    1364                 :             :      * processing_required flag to indicate that if it were not for
    1365                 :             :      * fast_forward mode, processing would have been required.
    1366                 :             :      */
    1367         [ +  + ]:        3140 :     if (ctx->fast_forward)
    1368                 :             :     {
    1369                 :          37 :         ctx->processing_required = true;
    1370                 :          37 :         return true;
    1371                 :             :     }
    1372                 :             : 
    1373                 :        3103 :     return false;
    1374                 :             : }
        

Generated by: LCOV version 2.0-1