LCOV - differential code coverage report
Current view: top level - src/backend/replication/logical - decode.c (source / functions) Coverage Total Hit UBC GBC CBC
Current: 806555e3000d0b0e0c536c1dc65548128d457d86 vs 1d325ad99cb2dec0e8b45ba36909ee0a497d2a57 Lines: 94.8 % 446 423 23 6 417
Current Date: 2025-12-17 08:58:58 +0900 Functions: 100.0 % 20 20 20
Baseline: lcov-20251217-005640-baseline Branches: 78.8 % 222 175 47 3 172
Baseline Date: 2025-12-16 12:57:12 -0800 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(30,360] days: 100.0 % 30 30 30
(360..) days: 94.5 % 416 393 23 6 387
Function coverage date bins:
(360..) days: 100.0 % 20 20 20
Branch coverage date bins:
(30,360] days: 76.7 % 30 23 7 23
(360..) days: 79.2 % 192 152 40 3 149

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

Generated by: LCOV version 2.4-beta