LCOV - differential code coverage report
Current view: top level - src/backend/replication/logical - decode.c (source / functions) Coverage Total Hit UNC LBC UBC GBC GIC GNC CBC ECB DUB DCB
Current: d36b728949bf4e37ada1cd23e0f2aaa94f609a70 vs 52e118fe2f7e3381bdaa479816a7f72eda2ae517 Lines: 93.0 % 460 428 12 20 1 1 20 406 1 2 16
Current Date: 2026-06-29 16:15:13 +0200 Functions: 95.2 % 21 20 1 10 10 1
Baseline: lcov-20260630-baseline Branches: 78.4 % 236 185 10 1 40 1 22 162
Baseline Date: 2026-06-29 13:01:57 +0200 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(30,360] days: 64.7 % 34 22 12 20 2
(360..) days: 95.3 % 426 406 20 1 1 404 1
Function coverage date bins:
(30,360] days: 50.0 % 2 1 1 1
(360..) days: 100.0 % 19 19 9 10
Branch coverage date bins:
(30,360] days: 68.8 % 32 22 10 22
(360..) days: 79.9 % 204 163 1 40 1 162

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

Generated by: LCOV version 2.0-1