PostgreSQL Source Code  git master
decode.c File Reference
Include dependency graph for decode.c:

Go to the source code of this file.

Data Structures

struct  XLogRecordBuffer
 

Typedefs

typedef struct XLogRecordBuffer XLogRecordBuffer
 

Functions

static void DecodeXLogOp (LogicalDecodingContext *ctx, XLogRecordBuffer *buf)
 
static void DecodeHeapOp (LogicalDecodingContext *ctx, XLogRecordBuffer *buf)
 
static void DecodeHeap2Op (LogicalDecodingContext *ctx, XLogRecordBuffer *buf)
 
static void DecodeXactOp (LogicalDecodingContext *ctx, XLogRecordBuffer *buf)
 
static void DecodeStandbyOp (LogicalDecodingContext *ctx, XLogRecordBuffer *buf)
 
static void DecodeLogicalMsgOp (LogicalDecodingContext *ctx, XLogRecordBuffer *buf)
 
static void DecodeInsert (LogicalDecodingContext *ctx, XLogRecordBuffer *buf)
 
static void DecodeUpdate (LogicalDecodingContext *ctx, XLogRecordBuffer *buf)
 
static void DecodeDelete (LogicalDecodingContext *ctx, XLogRecordBuffer *buf)
 
static void DecodeTruncate (LogicalDecodingContext *ctx, XLogRecordBuffer *buf)
 
static void DecodeMultiInsert (LogicalDecodingContext *ctx, XLogRecordBuffer *buf)
 
static void DecodeSpecConfirm (LogicalDecodingContext *ctx, XLogRecordBuffer *buf)
 
static void DecodeCommit (LogicalDecodingContext *ctx, XLogRecordBuffer *buf, xl_xact_parsed_commit *parsed, TransactionId xid, bool two_phase)
 
static void DecodeAbort (LogicalDecodingContext *ctx, XLogRecordBuffer *buf, xl_xact_parsed_abort *parsed, TransactionId xid, bool two_phase)
 
static void DecodePrepare (LogicalDecodingContext *ctx, XLogRecordBuffer *buf, xl_xact_parsed_prepare *parsed)
 
static void DecodeXLogTuple (char *data, Size len, ReorderBufferTupleBuf *tup)
 
static bool FilterPrepare (LogicalDecodingContext *ctx, TransactionId xid, const char *gid)
 
static bool DecodeTXNNeedSkip (LogicalDecodingContext *ctx, XLogRecordBuffer *buf, Oid dbId, RepOriginId origin_id)
 
void LogicalDecodingProcessRecord (LogicalDecodingContext *ctx, XLogReaderState *record)
 
static bool FilterByOrigin (LogicalDecodingContext *ctx, RepOriginId origin_id)
 

Typedef Documentation

◆ XLogRecordBuffer

Function Documentation

◆ DecodeAbort()

static void DecodeAbort ( LogicalDecodingContext ctx,
XLogRecordBuffer buf,
xl_xact_parsed_abort parsed,
TransactionId  xid,
bool  two_phase 
)
static

Definition at line 848 of file decode.c.

References xl_xact_parsed_abort::dbId, DecodeTXNNeedSkip(), XLogRecordBuffer::endptr, XLogReaderState::EndRecPtr, i, InvalidXLogRecPtr, xl_xact_parsed_abort::nsubxacts, xl_xact_parsed_abort::origin_lsn, xl_xact_parsed_abort::origin_timestamp, XLogRecordBuffer::origptr, XLogRecordBuffer::record, LogicalDecodingContext::reorder, ReorderBufferAbort(), ReorderBufferFinishPrepared(), xl_xact_parsed_abort::subxacts, xl_xact_parsed_abort::twophase_gid, UpdateDecodingStats(), xl_xact_parsed_abort::xact_time, XACT_XINFO_HAS_ORIGIN, xl_xact_parsed_abort::xinfo, and XLogRecGetOrigin.

Referenced by DecodeXactOp().

851 {
852  int i;
853  XLogRecPtr origin_lsn = InvalidXLogRecPtr;
854  TimestampTz abort_time = parsed->xact_time;
855  XLogRecPtr origin_id = XLogRecGetOrigin(buf->record);
856  bool skip_xact;
857 
858  if (parsed->xinfo & XACT_XINFO_HAS_ORIGIN)
859  {
860  origin_lsn = parsed->origin_lsn;
861  abort_time = parsed->origin_timestamp;
862  }
863 
864  /*
865  * Check whether we need to process this transaction. See
866  * DecodeTXNNeedSkip for the reasons why we sometimes want to skip the
867  * transaction.
868  */
869  skip_xact = DecodeTXNNeedSkip(ctx, buf, parsed->dbId, origin_id);
870 
871  /*
872  * Send the final rollback record for a prepared transaction unless we
873  * need to skip it. For non-two-phase xacts, simply forget the xact.
874  */
875  if (two_phase && !skip_xact)
876  {
877  ReorderBufferFinishPrepared(ctx->reorder, xid, buf->origptr, buf->endptr,
878  abort_time, origin_id, origin_lsn,
880  parsed->twophase_gid, false);
881  }
882  else
883  {
884  for (i = 0; i < parsed->nsubxacts; i++)
885  {
886  ReorderBufferAbort(ctx->reorder, parsed->subxacts[i],
887  buf->record->EndRecPtr);
888  }
889 
890  ReorderBufferAbort(ctx->reorder, xid, buf->record->EndRecPtr);
891  }
892 
893  /* update the decoding stats */
894  UpdateDecodingStats(ctx);
895 }
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
#define XACT_XINFO_HAS_ORIGIN
Definition: xact.h:172
struct ReorderBuffer * reorder
Definition: logical.h:42
int64 TimestampTz
Definition: timestamp.h:39
void ReorderBufferFinishPrepared(ReorderBuffer *rb, TransactionId xid, XLogRecPtr commit_lsn, XLogRecPtr end_lsn, XLogRecPtr initial_consistent_point, TimestampTz commit_time, RepOriginId origin_id, XLogRecPtr origin_lsn, char *gid, bool is_commit)
#define XLogRecGetOrigin(decoder)
Definition: xlogreader.h:308
XLogRecPtr EndRecPtr
Definition: xlogreader.h:176
XLogRecPtr endptr
Definition: decode.c:49
XLogReaderState * record
Definition: decode.c:50
TransactionId * subxacts
Definition: xact.h:360
static bool DecodeTXNNeedSkip(LogicalDecodingContext *ctx, XLogRecordBuffer *buf, Oid dbId, RepOriginId origin_id)
Definition: decode.c:1311
void UpdateDecodingStats(LogicalDecodingContext *ctx)
Definition: logical.c:1763
XLogRecPtr origin_lsn
Definition: xact.h:368
uint64 XLogRecPtr
Definition: xlogdefs.h:21
TimestampTz xact_time
Definition: xact.h:353
XLogRecPtr origptr
Definition: decode.c:48
void ReorderBufferAbort(ReorderBuffer *rb, TransactionId xid, XLogRecPtr lsn)
int i
TimestampTz origin_timestamp
Definition: xact.h:369
char twophase_gid[GIDSIZE]
Definition: xact.h:366

◆ DecodeCommit()

static void DecodeCommit ( LogicalDecodingContext ctx,
XLogRecordBuffer buf,
xl_xact_parsed_commit parsed,
TransactionId  xid,
bool  two_phase 
)
static

Definition at line 678 of file decode.c.

References xl_xact_parsed_commit::dbId, DecodeTXNNeedSkip(), XLogRecordBuffer::endptr, i, InvalidXLogRecPtr, xl_xact_parsed_commit::nsubxacts, xl_xact_parsed_commit::origin_lsn, xl_xact_parsed_commit::origin_timestamp, XLogRecordBuffer::origptr, XLogRecordBuffer::record, LogicalDecodingContext::reorder, ReorderBufferCommit(), ReorderBufferCommitChild(), ReorderBufferFinishPrepared(), ReorderBufferForget(), SnapBuildCommitTxn(), SnapBuildInitialConsistentPoint(), LogicalDecodingContext::snapshot_builder, xl_xact_parsed_commit::subxacts, xl_xact_parsed_commit::twophase_gid, UpdateDecodingStats(), xl_xact_parsed_commit::xact_time, XACT_XINFO_HAS_ORIGIN, xl_xact_parsed_commit::xinfo, and XLogRecGetOrigin.

Referenced by DecodeXactOp().

681 {
682  XLogRecPtr origin_lsn = InvalidXLogRecPtr;
683  TimestampTz commit_time = parsed->xact_time;
684  RepOriginId origin_id = XLogRecGetOrigin(buf->record);
685  int i;
686 
687  if (parsed->xinfo & XACT_XINFO_HAS_ORIGIN)
688  {
689  origin_lsn = parsed->origin_lsn;
690  commit_time = parsed->origin_timestamp;
691  }
692 
694  parsed->nsubxacts, parsed->subxacts);
695 
696  /* ----
697  * Check whether we are interested in this specific transaction, and tell
698  * the reorderbuffer to forget the content of the (sub-)transactions
699  * if not.
700  *
701  * We can't just use ReorderBufferAbort() here, because we need to execute
702  * the transaction's invalidations. This currently won't be needed if
703  * we're just skipping over the transaction because currently we only do
704  * so during startup, to get to the first transaction the client needs. As
705  * we have reset the catalog caches before starting to read WAL, and we
706  * haven't yet touched any catalogs, there can't be anything to invalidate.
707  * But if we're "forgetting" this commit because it happened in another
708  * database, the invalidations might be important, because they could be
709  * for shared catalogs and we might have loaded data into the relevant
710  * syscaches.
711  * ---
712  */
713  if (DecodeTXNNeedSkip(ctx, buf, parsed->dbId, origin_id))
714  {
715  for (i = 0; i < parsed->nsubxacts; i++)
716  {
717  ReorderBufferForget(ctx->reorder, parsed->subxacts[i], buf->origptr);
718  }
719  ReorderBufferForget(ctx->reorder, xid, buf->origptr);
720 
721  return;
722  }
723 
724  /* tell the reorderbuffer about the surviving subtransactions */
725  for (i = 0; i < parsed->nsubxacts; i++)
726  {
727  ReorderBufferCommitChild(ctx->reorder, xid, parsed->subxacts[i],
728  buf->origptr, buf->endptr);
729  }
730 
731  /*
732  * Send the final commit record if the transaction data is already
733  * decoded, otherwise, process the entire transaction.
734  */
735  if (two_phase)
736  {
737  ReorderBufferFinishPrepared(ctx->reorder, xid, buf->origptr, buf->endptr,
739  commit_time, origin_id, origin_lsn,
740  parsed->twophase_gid, true);
741  }
742  else
743  {
744  ReorderBufferCommit(ctx->reorder, xid, buf->origptr, buf->endptr,
745  commit_time, origin_id, origin_lsn);
746  }
747 
748  /*
749  * Update the decoding stats at transaction prepare/commit/abort.
750  * Additionally we send the stats when we spill or stream the changes to
751  * avoid losing them in case the decoding is interrupted. It is not clear
752  * that sending more or less frequently than this would be better.
753  */
754  UpdateDecodingStats(ctx);
755 }
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
#define XACT_XINFO_HAS_ORIGIN
Definition: xact.h:172
struct ReorderBuffer * reorder
Definition: logical.h:42
void ReorderBufferForget(ReorderBuffer *rb, TransactionId xid, XLogRecPtr lsn)
void ReorderBufferCommit(ReorderBuffer *rb, TransactionId xid, XLogRecPtr commit_lsn, XLogRecPtr end_lsn, TimestampTz commit_time, RepOriginId origin_id, XLogRecPtr origin_lsn)
TimestampTz origin_timestamp
Definition: xact.h:346
void SnapBuildCommitTxn(SnapBuild *builder, XLogRecPtr lsn, TransactionId xid, int nsubxacts, TransactionId *subxacts)
Definition: snapbuild.c:912
XLogRecPtr SnapBuildInitialConsistentPoint(SnapBuild *builder)
Definition: snapbuild.c:376
int64 TimestampTz
Definition: timestamp.h:39
TransactionId * subxacts
Definition: xact.h:332
uint16 RepOriginId
Definition: xlogdefs.h:65
XLogRecPtr origin_lsn
Definition: xact.h:345
void ReorderBufferFinishPrepared(ReorderBuffer *rb, TransactionId xid, XLogRecPtr commit_lsn, XLogRecPtr end_lsn, XLogRecPtr initial_consistent_point, TimestampTz commit_time, RepOriginId origin_id, XLogRecPtr origin_lsn, char *gid, bool is_commit)
#define XLogRecGetOrigin(decoder)
Definition: xlogreader.h:308
XLogRecPtr endptr
Definition: decode.c:49
XLogReaderState * record
Definition: decode.c:50
static bool DecodeTXNNeedSkip(LogicalDecodingContext *ctx, XLogRecordBuffer *buf, Oid dbId, RepOriginId origin_id)
Definition: decode.c:1311
void ReorderBufferCommitChild(ReorderBuffer *rb, TransactionId xid, TransactionId subxid, XLogRecPtr commit_lsn, XLogRecPtr end_lsn)
void UpdateDecodingStats(LogicalDecodingContext *ctx)
Definition: logical.c:1763
struct SnapBuild * snapshot_builder
Definition: logical.h:43
uint64 XLogRecPtr
Definition: xlogdefs.h:21
XLogRecPtr origptr
Definition: decode.c:48
int i
char twophase_gid[GIDSIZE]
Definition: xact.h:341
TimestampTz xact_time
Definition: xact.h:325

◆ DecodeDelete()

static void DecodeDelete ( LogicalDecodingContext ctx,
XLogRecordBuffer buf 
)
static

Definition at line 1029 of file decode.c.

References ReorderBufferChange::action, Assert, ReplicationSlot::data, ReorderBufferChange::data, ReplicationSlotPersistentData::database, RelFileNode::dbNode, DecodeXLogTuple(), FilterByOrigin(), xl_heap_delete::flags, ReorderBufferChange::origin_id, XLogRecordBuffer::origptr, XLogRecordBuffer::record, LogicalDecodingContext::reorder, REORDER_BUFFER_CHANGE_DELETE, ReorderBufferGetChange(), ReorderBufferGetTupleBuf(), ReorderBufferQueueChange(), SizeOfHeapDelete, SizeOfHeapHeader, LogicalDecodingContext::slot, ReorderBufferChange::tp, XLH_DELETE_CONTAINS_OLD, XLH_DELETE_IS_SUPER, XLogRecGetBlockTag(), XLogRecGetData, XLogRecGetDataLen, XLogRecGetOrigin, and XLogRecGetXid.

Referenced by DecodeHeapOp().

1030 {
1031  XLogReaderState *r = buf->record;
1032  xl_heap_delete *xlrec;
1033  ReorderBufferChange *change;
1034  RelFileNode target_node;
1035 
1036  xlrec = (xl_heap_delete *) XLogRecGetData(r);
1037 
1038  /* only interested in our database */
1039  XLogRecGetBlockTag(r, 0, &target_node, NULL, NULL);
1040  if (target_node.dbNode != ctx->slot->data.database)
1041  return;
1042 
1043  /*
1044  * Super deletions are irrelevant for logical decoding, it's driven by the
1045  * confirmation records.
1046  */
1047  if (xlrec->flags & XLH_DELETE_IS_SUPER)
1048  return;
1049 
1050  /* output plugin doesn't look for this origin, no need to queue */
1051  if (FilterByOrigin(ctx, XLogRecGetOrigin(r)))
1052  return;
1053 
1054  change = ReorderBufferGetChange(ctx->reorder);
1056  change->origin_id = XLogRecGetOrigin(r);
1057 
1058  memcpy(&change->data.tp.relnode, &target_node, sizeof(RelFileNode));
1059 
1060  /* old primary key stored */
1061  if (xlrec->flags & XLH_DELETE_CONTAINS_OLD)
1062  {
1063  Size datalen = XLogRecGetDataLen(r) - SizeOfHeapDelete;
1064  Size tuplelen = datalen - SizeOfHeapHeader;
1065 
1066  Assert(XLogRecGetDataLen(r) > (SizeOfHeapDelete + SizeOfHeapHeader));
1067 
1068  change->data.tp.oldtuple =
1069  ReorderBufferGetTupleBuf(ctx->reorder, tuplelen);
1070 
1071  DecodeXLogTuple((char *) xlrec + SizeOfHeapDelete,
1072  datalen, change->data.tp.oldtuple);
1073  }
1074 
1075  change->data.tp.clear_toast_afterwards = true;
1076 
1078  change, false);
1079 }
#define XLH_DELETE_IS_SUPER
Definition: heapam_xlog.h:99
struct ReorderBuffer * reorder
Definition: logical.h:42
static void DecodeXLogTuple(char *data, Size len, ReorderBufferTupleBuf *tup)
Definition: decode.c:1265
#define XLH_DELETE_CONTAINS_OLD
Definition: heapam_xlog.h:103
ReorderBufferChange * ReorderBufferGetChange(ReorderBuffer *rb)
ReplicationSlotPersistentData data
Definition: slot.h:156
static bool FilterByOrigin(LogicalDecodingContext *ctx, RepOriginId origin_id)
Definition: decode.c:611
enum ReorderBufferChangeType action
Definition: reorderbuffer.h:84
#define XLogRecGetOrigin(decoder)
Definition: xlogreader.h:308
#define XLogRecGetData(decoder)
Definition: xlogreader.h:310
#define XLogRecGetDataLen(decoder)
Definition: xlogreader.h:311
#define SizeOfHeapDelete
Definition: heapam_xlog.h:115
XLogReaderState * record
Definition: decode.c:50
union ReorderBufferChange::@97 data
RepOriginId origin_id
Definition: reorderbuffer.h:89
bool XLogRecGetBlockTag(XLogReaderState *record, uint8 block_id, RelFileNode *rnode, ForkNumber *forknum, BlockNumber *blknum)
Definition: xlogreader.c:1491
#define XLogRecGetXid(decoder)
Definition: xlogreader.h:307
void ReorderBufferQueueChange(ReorderBuffer *rb, TransactionId xid, XLogRecPtr lsn, ReorderBufferChange *change, bool toast_insert)
#define Assert(condition)
Definition: c.h:804
size_t Size
Definition: c.h:540
struct ReorderBufferChange::@97::@98 tp
XLogRecPtr origptr
Definition: decode.c:48
ReplicationSlot * slot
Definition: logical.h:38
#define SizeOfHeapHeader
Definition: heapam_xlog.h:151
ReorderBufferTupleBuf * ReorderBufferGetTupleBuf(ReorderBuffer *rb, Size tuple_len)

◆ DecodeHeap2Op()

static void DecodeHeap2Op ( LogicalDecodingContext ctx,
XLogRecordBuffer buf 
)
static

Definition at line 441 of file decode.c.

References DecodeMultiInsert(), elog, ERROR, LogicalDecodingContext::fast_forward, XLogRecordBuffer::origptr, XLogRecordBuffer::record, LogicalDecodingContext::reorder, ReorderBufferProcessXid(), SNAPBUILD_FULL_SNAPSHOT, SnapBuildCurrentState(), SnapBuildProcessChange(), SnapBuildProcessNewCid(), LogicalDecodingContext::snapshot_builder, XLOG_HEAP2_FREEZE_PAGE, XLOG_HEAP2_LOCK_UPDATED, XLOG_HEAP2_MULTI_INSERT, XLOG_HEAP2_NEW_CID, XLOG_HEAP2_PRUNE, XLOG_HEAP2_REWRITE, XLOG_HEAP2_VACUUM, XLOG_HEAP2_VISIBLE, XLOG_HEAP_OPMASK, XLogRecGetData, XLogRecGetInfo, and XLogRecGetXid.

Referenced by LogicalDecodingProcessRecord().

442 {
444  TransactionId xid = XLogRecGetXid(buf->record);
445  SnapBuild *builder = ctx->snapshot_builder;
446 
447  ReorderBufferProcessXid(ctx->reorder, xid, buf->origptr);
448 
449  /*
450  * If we don't have snapshot or we are just fast-forwarding, there is no
451  * point in decoding changes.
452  */
454  ctx->fast_forward)
455  return;
456 
457  switch (info)
458  {
460  if (!ctx->fast_forward &&
461  SnapBuildProcessChange(builder, xid, buf->origptr))
462  DecodeMultiInsert(ctx, buf);
463  break;
464  case XLOG_HEAP2_NEW_CID:
465  {
466  xl_heap_new_cid *xlrec;
467 
468  xlrec = (xl_heap_new_cid *) XLogRecGetData(buf->record);
469  SnapBuildProcessNewCid(builder, xid, buf->origptr, xlrec);
470 
471  break;
472  }
473  case XLOG_HEAP2_REWRITE:
474 
475  /*
476  * Although these records only exist to serve the needs of logical
477  * decoding, all the work happens as part of crash or archive
478  * recovery, so we don't need to do anything here.
479  */
480  break;
481 
482  /*
483  * Everything else here is just low level physical stuff we're not
484  * interested in.
485  */
487  case XLOG_HEAP2_PRUNE:
488  case XLOG_HEAP2_VACUUM:
489  case XLOG_HEAP2_VISIBLE:
491  break;
492  default:
493  elog(ERROR, "unexpected RM_HEAP2_ID record type: %u", info);
494  }
495 }
static void DecodeMultiInsert(LogicalDecodingContext *ctx, XLogRecordBuffer *buf)
Definition: decode.c:1123
struct ReorderBuffer * reorder
Definition: logical.h:42
#define XLOG_HEAP2_LOCK_UPDATED
Definition: heapam_xlog.h:59
uint32 TransactionId
Definition: c.h:587
SnapBuildState SnapBuildCurrentState(SnapBuild *builder)
Definition: snapbuild.c:367
#define XLOG_HEAP2_REWRITE
Definition: heapam_xlog.h:53
unsigned char uint8
Definition: c.h:439
#define XLOG_HEAP_OPMASK
Definition: heapam_xlog.h:41
#define XLOG_HEAP2_PRUNE
Definition: heapam_xlog.h:54
#define XLOG_HEAP2_MULTI_INSERT
Definition: heapam_xlog.h:58
bool SnapBuildProcessChange(SnapBuild *builder, TransactionId xid, XLogRecPtr lsn)
Definition: snapbuild.c:697
#define XLOG_HEAP2_VISIBLE
Definition: heapam_xlog.h:57
#define XLogRecGetData(decoder)
Definition: xlogreader.h:310
#define ERROR
Definition: elog.h:46
void SnapBuildProcessNewCid(SnapBuild *builder, TransactionId xid, XLogRecPtr lsn, xl_heap_new_cid *xlrec)
Definition: snapbuild.c:747
#define XLOG_HEAP2_NEW_CID
Definition: heapam_xlog.h:60
XLogReaderState * record
Definition: decode.c:50
#define XLOG_HEAP2_VACUUM
Definition: heapam_xlog.h:55
#define XLogRecGetInfo(decoder)
Definition: xlogreader.h:305
#define XLogRecGetXid(decoder)
Definition: xlogreader.h:307
struct SnapBuild * snapshot_builder
Definition: logical.h:43
#define XLOG_HEAP2_FREEZE_PAGE
Definition: heapam_xlog.h:56
XLogRecPtr origptr
Definition: decode.c:48
#define elog(elevel,...)
Definition: elog.h:232
void ReorderBufferProcessXid(ReorderBuffer *rb, TransactionId xid, XLogRecPtr lsn)

◆ DecodeHeapOp()

static void DecodeHeapOp ( LogicalDecodingContext ctx,
XLogRecordBuffer buf 
)
static

Definition at line 501 of file decode.c.

References DecodeDelete(), DecodeInsert(), DecodeSpecConfirm(), DecodeTruncate(), DecodeUpdate(), elog, ERROR, LogicalDecodingContext::fast_forward, XLogRecordBuffer::origptr, XLogRecordBuffer::record, LogicalDecodingContext::reorder, ReorderBufferProcessXid(), ReorderBufferXidSetCatalogChanges(), SNAPBUILD_FULL_SNAPSHOT, SnapBuildCurrentState(), SnapBuildProcessChange(), LogicalDecodingContext::snapshot_builder, TransactionIdIsValid, XLOG_HEAP_CONFIRM, XLOG_HEAP_DELETE, XLOG_HEAP_HOT_UPDATE, XLOG_HEAP_INPLACE, XLOG_HEAP_INSERT, XLOG_HEAP_LOCK, XLOG_HEAP_OPMASK, XLOG_HEAP_TRUNCATE, XLOG_HEAP_UPDATE, XLogRecGetInfo, and XLogRecGetXid.

Referenced by LogicalDecodingProcessRecord().

502 {
504  TransactionId xid = XLogRecGetXid(buf->record);
505  SnapBuild *builder = ctx->snapshot_builder;
506 
507  ReorderBufferProcessXid(ctx->reorder, xid, buf->origptr);
508 
509  /*
510  * If we don't have snapshot or we are just fast-forwarding, there is no
511  * point in decoding data changes.
512  */
514  ctx->fast_forward)
515  return;
516 
517  switch (info)
518  {
519  case XLOG_HEAP_INSERT:
520  if (SnapBuildProcessChange(builder, xid, buf->origptr))
521  DecodeInsert(ctx, buf);
522  break;
523 
524  /*
525  * Treat HOT update as normal updates. There is no useful
526  * information in the fact that we could make it a HOT update
527  * locally and the WAL layout is compatible.
528  */
530  case XLOG_HEAP_UPDATE:
531  if (SnapBuildProcessChange(builder, xid, buf->origptr))
532  DecodeUpdate(ctx, buf);
533  break;
534 
535  case XLOG_HEAP_DELETE:
536  if (SnapBuildProcessChange(builder, xid, buf->origptr))
537  DecodeDelete(ctx, buf);
538  break;
539 
540  case XLOG_HEAP_TRUNCATE:
541  if (SnapBuildProcessChange(builder, xid, buf->origptr))
542  DecodeTruncate(ctx, buf);
543  break;
544 
545  case XLOG_HEAP_INPLACE:
546 
547  /*
548  * Inplace updates are only ever performed on catalog tuples and
549  * can, per definition, not change tuple visibility. Since we
550  * don't decode catalog tuples, we're not interested in the
551  * record's contents.
552  *
553  * In-place updates can be used either by XID-bearing transactions
554  * (e.g. in CREATE INDEX CONCURRENTLY) or by XID-less
555  * transactions (e.g. VACUUM). In the former case, the commit
556  * record will include cache invalidations, so we mark the
557  * transaction as catalog modifying here. Currently that's
558  * redundant because the commit will do that as well, but once we
559  * support decoding in-progress relations, this will be important.
560  */
561  if (!TransactionIdIsValid(xid))
562  break;
563 
564  SnapBuildProcessChange(builder, xid, buf->origptr);
566  break;
567 
568  case XLOG_HEAP_CONFIRM:
569  if (SnapBuildProcessChange(builder, xid, buf->origptr))
570  DecodeSpecConfirm(ctx, buf);
571  break;
572 
573  case XLOG_HEAP_LOCK:
574  /* we don't care about row level locks for now */
575  break;
576 
577  default:
578  elog(ERROR, "unexpected RM_HEAP_ID record type: %u", info);
579  break;
580  }
581 }
#define XLOG_HEAP_INSERT
Definition: heapam_xlog.h:32
struct ReorderBuffer * reorder
Definition: logical.h:42
static void DecodeInsert(LogicalDecodingContext *ctx, XLogRecordBuffer *buf)
Definition: decode.c:903
uint32 TransactionId
Definition: c.h:587
SnapBuildState SnapBuildCurrentState(SnapBuild *builder)
Definition: snapbuild.c:367
unsigned char uint8
Definition: c.h:439
#define XLOG_HEAP_OPMASK
Definition: heapam_xlog.h:41
#define XLOG_HEAP_HOT_UPDATE
Definition: heapam_xlog.h:36
static void DecodeUpdate(LogicalDecodingContext *ctx, XLogRecordBuffer *buf)
Definition: decode.c:962
bool SnapBuildProcessChange(SnapBuild *builder, TransactionId xid, XLogRecPtr lsn)
Definition: snapbuild.c:697
static void DecodeSpecConfirm(LogicalDecodingContext *ctx, XLogRecordBuffer *buf)
Definition: decode.c:1229
#define ERROR
Definition: elog.h:46
static void DecodeDelete(LogicalDecodingContext *ctx, XLogRecordBuffer *buf)
Definition: decode.c:1029
XLogReaderState * record
Definition: decode.c:50
#define XLogRecGetInfo(decoder)
Definition: xlogreader.h:305
#define XLogRecGetXid(decoder)
Definition: xlogreader.h:307
#define XLOG_HEAP_TRUNCATE
Definition: heapam_xlog.h:35
struct SnapBuild * snapshot_builder
Definition: logical.h:43
#define XLOG_HEAP_UPDATE
Definition: heapam_xlog.h:34
static void DecodeTruncate(LogicalDecodingContext *ctx, XLogRecordBuffer *buf)
Definition: decode.c:1085
#define XLOG_HEAP_INPLACE
Definition: heapam_xlog.h:39
XLogRecPtr origptr
Definition: decode.c:48
#define XLOG_HEAP_LOCK
Definition: heapam_xlog.h:38
#define elog(elevel,...)
Definition: elog.h:232
void ReorderBufferXidSetCatalogChanges(ReorderBuffer *rb, TransactionId xid, XLogRecPtr lsn)
#define TransactionIdIsValid(xid)
Definition: transam.h:41
void ReorderBufferProcessXid(ReorderBuffer *rb, TransactionId xid, XLogRecPtr lsn)
#define XLOG_HEAP_DELETE
Definition: heapam_xlog.h:33
#define XLOG_HEAP_CONFIRM
Definition: heapam_xlog.h:37

◆ DecodeInsert()

static void DecodeInsert ( LogicalDecodingContext ctx,
XLogRecordBuffer buf 
)
static

Definition at line 903 of file decode.c.

References ReorderBufferChange::action, ReplicationSlot::data, ReorderBufferChange::data, ReplicationSlotPersistentData::database, RelFileNode::dbNode, DecodeXLogTuple(), FilterByOrigin(), xl_heap_insert::flags, ReorderBufferChange::origin_id, XLogRecordBuffer::origptr, XLogRecordBuffer::record, LogicalDecodingContext::reorder, REORDER_BUFFER_CHANGE_INSERT, REORDER_BUFFER_CHANGE_INTERNAL_SPEC_INSERT, ReorderBufferGetChange(), ReorderBufferGetTupleBuf(), ReorderBufferQueueChange(), SizeOfHeapHeader, LogicalDecodingContext::slot, ReorderBufferChange::tp, XLH_INSERT_CONTAINS_NEW_TUPLE, XLH_INSERT_IS_SPECULATIVE, XLH_INSERT_ON_TOAST_RELATION, XLogRecGetBlockData(), XLogRecGetBlockTag(), XLogRecGetData, XLogRecGetOrigin, and XLogRecGetXid.

Referenced by DecodeHeapOp().

904 {
905  Size datalen;
906  char *tupledata;
907  Size tuplelen;
908  XLogReaderState *r = buf->record;
909  xl_heap_insert *xlrec;
910  ReorderBufferChange *change;
911  RelFileNode target_node;
912 
913  xlrec = (xl_heap_insert *) XLogRecGetData(r);
914 
915  /*
916  * Ignore insert records without new tuples (this does happen when
917  * raw_heap_insert marks the TOAST record as HEAP_INSERT_NO_LOGICAL).
918  */
919  if (!(xlrec->flags & XLH_INSERT_CONTAINS_NEW_TUPLE))
920  return;
921 
922  /* only interested in our database */
923  XLogRecGetBlockTag(r, 0, &target_node, NULL, NULL);
924  if (target_node.dbNode != ctx->slot->data.database)
925  return;
926 
927  /* output plugin doesn't look for this origin, no need to queue */
928  if (FilterByOrigin(ctx, XLogRecGetOrigin(r)))
929  return;
930 
931  change = ReorderBufferGetChange(ctx->reorder);
932  if (!(xlrec->flags & XLH_INSERT_IS_SPECULATIVE))
934  else
936  change->origin_id = XLogRecGetOrigin(r);
937 
938  memcpy(&change->data.tp.relnode, &target_node, sizeof(RelFileNode));
939 
940  tupledata = XLogRecGetBlockData(r, 0, &datalen);
941  tuplelen = datalen - SizeOfHeapHeader;
942 
943  change->data.tp.newtuple =
944  ReorderBufferGetTupleBuf(ctx->reorder, tuplelen);
945 
946  DecodeXLogTuple(tupledata, datalen, change->data.tp.newtuple);
947 
948  change->data.tp.clear_toast_afterwards = true;
949 
951  change,
953 }
struct ReorderBuffer * reorder
Definition: logical.h:42
static void DecodeXLogTuple(char *data, Size len, ReorderBufferTupleBuf *tup)
Definition: decode.c:1265
#define XLH_INSERT_IS_SPECULATIVE
Definition: heapam_xlog.h:68
#define XLH_INSERT_ON_TOAST_RELATION
Definition: heapam_xlog.h:70
ReorderBufferChange * ReorderBufferGetChange(ReorderBuffer *rb)
ReplicationSlotPersistentData data
Definition: slot.h:156
static bool FilterByOrigin(LogicalDecodingContext *ctx, RepOriginId origin_id)
Definition: decode.c:611
enum ReorderBufferChangeType action
Definition: reorderbuffer.h:84
#define XLogRecGetOrigin(decoder)
Definition: xlogreader.h:308
#define XLogRecGetData(decoder)
Definition: xlogreader.h:310
#define XLH_INSERT_CONTAINS_NEW_TUPLE
Definition: heapam_xlog.h:69
XLogReaderState * record
Definition: decode.c:50
union ReorderBufferChange::@97 data
RepOriginId origin_id
Definition: reorderbuffer.h:89
bool XLogRecGetBlockTag(XLogReaderState *record, uint8 block_id, RelFileNode *rnode, ForkNumber *forknum, BlockNumber *blknum)
Definition: xlogreader.c:1491
#define XLogRecGetXid(decoder)
Definition: xlogreader.h:307
char * XLogRecGetBlockData(XLogReaderState *record, uint8 block_id, Size *len)
Definition: xlogreader.c:1515
void ReorderBufferQueueChange(ReorderBuffer *rb, TransactionId xid, XLogRecPtr lsn, ReorderBufferChange *change, bool toast_insert)
size_t Size
Definition: c.h:540
struct ReorderBufferChange::@97::@98 tp
XLogRecPtr origptr
Definition: decode.c:48
ReplicationSlot * slot
Definition: logical.h:38
#define SizeOfHeapHeader
Definition: heapam_xlog.h:151
ReorderBufferTupleBuf * ReorderBufferGetTupleBuf(ReorderBuffer *rb, Size tuple_len)

◆ DecodeLogicalMsgOp()

static void DecodeLogicalMsgOp ( LogicalDecodingContext ctx,
XLogRecordBuffer buf 
)
static

Definition at line 623 of file decode.c.

References ReplicationSlot::data, ReplicationSlotPersistentData::database, xl_logical_message::dbId, elog, XLogRecordBuffer::endptr, ERROR, LogicalDecodingContext::fast_forward, FilterByOrigin(), xl_logical_message::message, xl_logical_message::message_size, XLogRecordBuffer::origptr, xl_logical_message::prefix_size, XLogRecordBuffer::record, LogicalDecodingContext::reorder, ReorderBufferProcessXid(), ReorderBufferQueueMessage(), LogicalDecodingContext::slot, SNAPBUILD_CONSISTENT, SNAPBUILD_FULL_SNAPSHOT, SnapBuildCurrentState(), SnapBuildGetOrBuildSnapshot(), SnapBuildProcessChange(), SnapBuildXactNeedsSkip(), LogicalDecodingContext::snapshot_builder, xl_logical_message::transactional, XLOG_LOGICAL_MESSAGE, XLogRecGetData, XLogRecGetInfo, XLogRecGetOrigin, XLogRecGetXid, and XLR_INFO_MASK.

Referenced by LogicalDecodingProcessRecord().

624 {
625  SnapBuild *builder = ctx->snapshot_builder;
626  XLogReaderState *r = buf->record;
627  TransactionId xid = XLogRecGetXid(r);
628  uint8 info = XLogRecGetInfo(r) & ~XLR_INFO_MASK;
629  RepOriginId origin_id = XLogRecGetOrigin(r);
630  Snapshot snapshot;
631  xl_logical_message *message;
632 
633  if (info != XLOG_LOGICAL_MESSAGE)
634  elog(ERROR, "unexpected RM_LOGICALMSG_ID record type: %u", info);
635 
637 
638  /*
639  * If we don't have snapshot or we are just fast-forwarding, there is no
640  * point in decoding messages.
641  */
643  ctx->fast_forward)
644  return;
645 
646  message = (xl_logical_message *) XLogRecGetData(r);
647 
648  if (message->dbId != ctx->slot->data.database ||
649  FilterByOrigin(ctx, origin_id))
650  return;
651 
652  if (message->transactional &&
653  !SnapBuildProcessChange(builder, xid, buf->origptr))
654  return;
655  else if (!message->transactional &&
657  SnapBuildXactNeedsSkip(builder, buf->origptr)))
658  return;
659 
660  snapshot = SnapBuildGetOrBuildSnapshot(builder, xid);
661  ReorderBufferQueueMessage(ctx->reorder, xid, snapshot, buf->endptr,
662  message->transactional,
663  message->message, /* first part of message is
664  * prefix */
665  message->message_size,
666  message->message + message->prefix_size);
667 }
void ReorderBufferQueueMessage(ReorderBuffer *rb, TransactionId xid, Snapshot snapshot, XLogRecPtr lsn, bool transactional, const char *prefix, Size message_size, const char *message)
struct ReorderBuffer * reorder
Definition: logical.h:42
uint32 TransactionId
Definition: c.h:587
bool SnapBuildXactNeedsSkip(SnapBuild *builder, XLogRecPtr ptr)
Definition: snapbuild.c:385
SnapBuildState SnapBuildCurrentState(SnapBuild *builder)
Definition: snapbuild.c:367
unsigned char uint8
Definition: c.h:439
uint16 RepOriginId
Definition: xlogdefs.h:65
ReplicationSlotPersistentData data
Definition: slot.h:156
static bool FilterByOrigin(LogicalDecodingContext *ctx, RepOriginId origin_id)
Definition: decode.c:611
#define XLogRecGetOrigin(decoder)
Definition: xlogreader.h:308
char message[FLEXIBLE_ARRAY_MEMBER]
Definition: message.h:27
bool SnapBuildProcessChange(SnapBuild *builder, TransactionId xid, XLogRecPtr lsn)
Definition: snapbuild.c:697
#define XLogRecGetData(decoder)
Definition: xlogreader.h:310
#define ERROR
Definition: elog.h:46
XLogRecPtr endptr
Definition: decode.c:49
XLogReaderState * record
Definition: decode.c:50
#define XLogRecGetInfo(decoder)
Definition: xlogreader.h:305
bool transactional
Definition: message.h:23
#define XLogRecGetXid(decoder)
Definition: xlogreader.h:307
struct SnapBuild * snapshot_builder
Definition: logical.h:43
#define XLR_INFO_MASK
Definition: xlogrecord.h:62
XLogRecPtr origptr
Definition: decode.c:48
ReplicationSlot * slot
Definition: logical.h:38
#define elog(elevel,...)
Definition: elog.h:232
#define XLOG_LOGICAL_MESSAGE
Definition: message.h:36
void ReorderBufferProcessXid(ReorderBuffer *rb, TransactionId xid, XLogRecPtr lsn)
Snapshot SnapBuildGetOrBuildSnapshot(SnapBuild *builder, TransactionId xid)
Definition: snapbuild.c:653

◆ DecodeMultiInsert()

static void DecodeMultiInsert ( LogicalDecodingContext ctx,
XLogRecordBuffer buf 
)
static

Definition at line 1123 of file decode.c.

References ReorderBufferChange::action, Assert, ReplicationSlot::data, ReorderBufferChange::data, ReplicationSlotPersistentData::database, xl_multi_insert_tuple::datalen, RelFileNode::dbNode, FilterByOrigin(), xl_heap_multi_insert::flags, header(), i, InvalidOid, ItemPointerSetInvalid, xl_heap_multi_insert::ntuples, ReorderBufferChange::origin_id, XLogRecordBuffer::origptr, XLogRecordBuffer::record, LogicalDecodingContext::reorder, REORDER_BUFFER_CHANGE_INSERT, ReorderBufferGetChange(), ReorderBufferGetTupleBuf(), ReorderBufferQueueChange(), SHORTALIGN, SizeofHeapTupleHeader, SizeOfMultiInsertTuple, LogicalDecodingContext::slot, HeapTupleData::t_data, HeapTupleHeaderData::t_hoff, xl_multi_insert_tuple::t_hoff, HeapTupleHeaderData::t_infomask, xl_multi_insert_tuple::t_infomask, HeapTupleHeaderData::t_infomask2, xl_multi_insert_tuple::t_infomask2, HeapTupleData::t_len, HeapTupleData::t_self, HeapTupleData::t_tableOid, ReorderBufferChange::tp, ReorderBufferTupleBuf::tuple, XLH_INSERT_CONTAINS_NEW_TUPLE, XLH_INSERT_LAST_IN_MULTI, XLogRecGetBlockData(), XLogRecGetBlockTag(), XLogRecGetData, XLogRecGetOrigin, and XLogRecGetXid.

Referenced by DecodeHeap2Op().

1124 {
1125  XLogReaderState *r = buf->record;
1126  xl_heap_multi_insert *xlrec;
1127  int i;
1128  char *data;
1129  char *tupledata;
1130  Size tuplelen;
1131  RelFileNode rnode;
1132 
1133  xlrec = (xl_heap_multi_insert *) XLogRecGetData(r);
1134 
1135  /*
1136  * Ignore insert records without new tuples. This happens when a
1137  * multi_insert is done on a catalog or on a non-persistent relation.
1138  */
1139  if (!(xlrec->flags & XLH_INSERT_CONTAINS_NEW_TUPLE))
1140  return;
1141 
1142  /* only interested in our database */
1143  XLogRecGetBlockTag(r, 0, &rnode, NULL, NULL);
1144  if (rnode.dbNode != ctx->slot->data.database)
1145  return;
1146 
1147  /* output plugin doesn't look for this origin, no need to queue */
1148  if (FilterByOrigin(ctx, XLogRecGetOrigin(r)))
1149  return;
1150 
1151  /*
1152  * We know that this multi_insert isn't for a catalog, so the block should
1153  * always have data even if a full-page write of it is taken.
1154  */
1155  tupledata = XLogRecGetBlockData(r, 0, &tuplelen);
1156  Assert(tupledata != NULL);
1157 
1158  data = tupledata;
1159  for (i = 0; i < xlrec->ntuples; i++)
1160  {
1161  ReorderBufferChange *change;
1162  xl_multi_insert_tuple *xlhdr;
1163  int datalen;
1164  ReorderBufferTupleBuf *tuple;
1166 
1167  change = ReorderBufferGetChange(ctx->reorder);
1169  change->origin_id = XLogRecGetOrigin(r);
1170 
1171  memcpy(&change->data.tp.relnode, &rnode, sizeof(RelFileNode));
1172 
1173  xlhdr = (xl_multi_insert_tuple *) SHORTALIGN(data);
1174  data = ((char *) xlhdr) + SizeOfMultiInsertTuple;
1175  datalen = xlhdr->datalen;
1176 
1177  change->data.tp.newtuple =
1178  ReorderBufferGetTupleBuf(ctx->reorder, datalen);
1179 
1180  tuple = change->data.tp.newtuple;
1181  header = tuple->tuple.t_data;
1182 
1183  /* not a disk based tuple */
1185 
1186  /*
1187  * We can only figure this out after reassembling the transactions.
1188  */
1189  tuple->tuple.t_tableOid = InvalidOid;
1190 
1191  tuple->tuple.t_len = datalen + SizeofHeapTupleHeader;
1192 
1193  memset(header, 0, SizeofHeapTupleHeader);
1194 
1195  memcpy((char *) tuple->tuple.t_data + SizeofHeapTupleHeader,
1196  (char *) data,
1197  datalen);
1198  header->t_infomask = xlhdr->t_infomask;
1199  header->t_infomask2 = xlhdr->t_infomask2;
1200  header->t_hoff = xlhdr->t_hoff;
1201 
1202  /*
1203  * Reset toast reassembly state only after the last row in the last
1204  * xl_multi_insert_tuple record emitted by one heap_multi_insert()
1205  * call.
1206  */
1207  if (xlrec->flags & XLH_INSERT_LAST_IN_MULTI &&
1208  (i + 1) == xlrec->ntuples)
1209  change->data.tp.clear_toast_afterwards = true;
1210  else
1211  change->data.tp.clear_toast_afterwards = false;
1212 
1214  buf->origptr, change, false);
1215 
1216  /* move to the next xl_multi_insert_tuple entry */
1217  data += datalen;
1218  }
1219  Assert(data == tupledata + tuplelen);
1220 }
#define SizeofHeapTupleHeader
Definition: htup_details.h:184
struct ReorderBuffer * reorder
Definition: logical.h:42
ReorderBufferChange * ReorderBufferGetChange(ReorderBuffer *rb)
ReplicationSlotPersistentData data
Definition: slot.h:156
static bool FilterByOrigin(LogicalDecodingContext *ctx, RepOriginId origin_id)
Definition: decode.c:611
enum ReorderBufferChangeType action
Definition: reorderbuffer.h:84
#define XLogRecGetOrigin(decoder)
Definition: xlogreader.h:308
HeapTupleHeader t_data
Definition: htup.h:68
#define XLogRecGetData(decoder)
Definition: xlogreader.h:310
#define XLH_INSERT_CONTAINS_NEW_TUPLE
Definition: heapam_xlog.h:69
ItemPointerData t_self
Definition: htup.h:65
uint32 t_len
Definition: htup.h:64
HeapTupleData tuple
Definition: reorderbuffer.h:29
Oid t_tableOid
Definition: htup.h:66
XLogReaderState * record
Definition: decode.c:50
union ReorderBufferChange::@97 data
RepOriginId origin_id
Definition: reorderbuffer.h:89
#define XLH_INSERT_LAST_IN_MULTI
Definition: heapam_xlog.h:67
bool XLogRecGetBlockTag(XLogReaderState *record, uint8 block_id, RelFileNode *rnode, ForkNumber *forknum, BlockNumber *blknum)
Definition: xlogreader.c:1491
#define XLogRecGetXid(decoder)
Definition: xlogreader.h:307
char * XLogRecGetBlockData(XLogReaderState *record, uint8 block_id, Size *len)
Definition: xlogreader.c:1515
void ReorderBufferQueueChange(ReorderBuffer *rb, TransactionId xid, XLogRecPtr lsn, ReorderBufferChange *change, bool toast_insert)
#define InvalidOid
Definition: postgres_ext.h:36
#define Assert(condition)
Definition: c.h:804
size_t Size
Definition: c.h:540
static void header(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:210
struct ReorderBufferChange::@97::@98 tp
XLogRecPtr origptr
Definition: decode.c:48
#define ItemPointerSetInvalid(pointer)
Definition: itemptr.h:172
ReplicationSlot * slot
Definition: logical.h:38
int i
#define SHORTALIGN(LEN)
Definition: c.h:753
#define SizeOfMultiInsertTuple
Definition: heapam_xlog.h:193
ReorderBufferTupleBuf * ReorderBufferGetTupleBuf(ReorderBuffer *rb, Size tuple_len)

◆ DecodePrepare()

static void DecodePrepare ( LogicalDecodingContext ctx,
XLogRecordBuffer buf,
xl_xact_parsed_prepare parsed 
)
static

Definition at line 773 of file decode.c.

References xl_xact_parsed_commit::dbId, DecodeTXNNeedSkip(), XLogRecordBuffer::endptr, i, xl_xact_parsed_commit::nsubxacts, xl_xact_parsed_commit::origin_lsn, xl_xact_parsed_commit::origin_timestamp, XLogRecordBuffer::origptr, XLogRecordBuffer::record, LogicalDecodingContext::reorder, ReorderBufferCommitChild(), ReorderBufferInvalidate(), ReorderBufferPrepare(), ReorderBufferRememberPrepareInfo(), ReorderBufferSkipPrepare(), SNAPBUILD_CONSISTENT, SnapBuildCurrentState(), LogicalDecodingContext::snapshot_builder, xl_xact_parsed_commit::subxacts, xl_xact_parsed_commit::twophase_gid, xl_xact_parsed_commit::twophase_xid, UpdateDecodingStats(), xl_xact_parsed_commit::xact_time, and XLogRecGetOrigin.

Referenced by DecodeXactOp().

775 {
776  SnapBuild *builder = ctx->snapshot_builder;
777  XLogRecPtr origin_lsn = parsed->origin_lsn;
778  TimestampTz prepare_time = parsed->xact_time;
779  XLogRecPtr origin_id = XLogRecGetOrigin(buf->record);
780  int i;
781  TransactionId xid = parsed->twophase_xid;
782 
783  if (parsed->origin_timestamp != 0)
784  prepare_time = parsed->origin_timestamp;
785 
786  /*
787  * Remember the prepare info for a txn so that it can be used later in
788  * commit prepared if required. See ReorderBufferFinishPrepared.
789  */
790  if (!ReorderBufferRememberPrepareInfo(ctx->reorder, xid, buf->origptr,
791  buf->endptr, prepare_time, origin_id,
792  origin_lsn))
793  return;
794 
795  /* We can't start streaming unless a consistent state is reached. */
797  {
799  return;
800  }
801 
802  /*
803  * Check whether we need to process this transaction. See
804  * DecodeTXNNeedSkip for the reasons why we sometimes want to skip the
805  * transaction.
806  *
807  * We can't call ReorderBufferForget as we did in DecodeCommit as the txn
808  * hasn't yet been committed, removing this txn before a commit might
809  * result in the computation of an incorrect restart_lsn. See
810  * SnapBuildProcessRunningXacts. But we need to process cache
811  * invalidations if there are any for the reasons mentioned in
812  * DecodeCommit.
813  */
814  if (DecodeTXNNeedSkip(ctx, buf, parsed->dbId, origin_id))
815  {
817  ReorderBufferInvalidate(ctx->reorder, xid, buf->origptr);
818  return;
819  }
820 
821  /* Tell the reorderbuffer about the surviving subtransactions. */
822  for (i = 0; i < parsed->nsubxacts; i++)
823  {
824  ReorderBufferCommitChild(ctx->reorder, xid, parsed->subxacts[i],
825  buf->origptr, buf->endptr);
826  }
827 
828  /* replay actions of all transaction + subtransactions in order */
829  ReorderBufferPrepare(ctx->reorder, xid, parsed->twophase_gid);
830 
831  /*
832  * Update the decoding stats at transaction prepare/commit/abort.
833  * Additionally we send the stats when we spill or stream the changes to
834  * avoid losing them in case the decoding is interrupted. It is not clear
835  * that sending more or less frequently than this would be better.
836  */
837  UpdateDecodingStats(ctx);
838 }
struct ReorderBuffer * reorder
Definition: logical.h:42
uint32 TransactionId
Definition: c.h:587
TimestampTz origin_timestamp
Definition: xact.h:346
SnapBuildState SnapBuildCurrentState(SnapBuild *builder)
Definition: snapbuild.c:367
int64 TimestampTz
Definition: timestamp.h:39
TransactionId twophase_xid
Definition: xact.h:340
TransactionId * subxacts
Definition: xact.h:332
XLogRecPtr origin_lsn
Definition: xact.h:345
#define XLogRecGetOrigin(decoder)
Definition: xlogreader.h:308
XLogRecPtr endptr
Definition: decode.c:49
void ReorderBufferSkipPrepare(ReorderBuffer *rb, TransactionId xid)
XLogReaderState * record
Definition: decode.c:50
static bool DecodeTXNNeedSkip(LogicalDecodingContext *ctx, XLogRecordBuffer *buf, Oid dbId, RepOriginId origin_id)
Definition: decode.c:1311
void ReorderBufferCommitChild(ReorderBuffer *rb, TransactionId xid, TransactionId subxid, XLogRecPtr commit_lsn, XLogRecPtr end_lsn)
void UpdateDecodingStats(LogicalDecodingContext *ctx)
Definition: logical.c:1763
struct SnapBuild * snapshot_builder
Definition: logical.h:43
uint64 XLogRecPtr
Definition: xlogdefs.h:21
bool ReorderBufferRememberPrepareInfo(ReorderBuffer *rb, TransactionId xid, XLogRecPtr prepare_lsn, XLogRecPtr end_lsn, TimestampTz prepare_time, RepOriginId origin_id, XLogRecPtr origin_lsn)
XLogRecPtr origptr
Definition: decode.c:48
int i
void ReorderBufferPrepare(ReorderBuffer *rb, TransactionId xid, char *gid)
void ReorderBufferInvalidate(ReorderBuffer *rb, TransactionId xid, XLogRecPtr lsn)
char twophase_gid[GIDSIZE]
Definition: xact.h:341
TimestampTz xact_time
Definition: xact.h:325

◆ DecodeSpecConfirm()

static void DecodeSpecConfirm ( LogicalDecodingContext ctx,
XLogRecordBuffer buf 
)
static

Definition at line 1229 of file decode.c.

References ReorderBufferChange::action, ReplicationSlot::data, ReorderBufferChange::data, ReplicationSlotPersistentData::database, RelFileNode::dbNode, FilterByOrigin(), ReorderBufferChange::origin_id, XLogRecordBuffer::origptr, XLogRecordBuffer::record, LogicalDecodingContext::reorder, REORDER_BUFFER_CHANGE_INTERNAL_SPEC_CONFIRM, ReorderBufferGetChange(), ReorderBufferQueueChange(), LogicalDecodingContext::slot, ReorderBufferChange::tp, XLogRecGetBlockTag(), XLogRecGetOrigin, and XLogRecGetXid.

Referenced by DecodeHeapOp().

1230 {
1231  XLogReaderState *r = buf->record;
1232  ReorderBufferChange *change;
1233  RelFileNode target_node;
1234 
1235  /* only interested in our database */
1236  XLogRecGetBlockTag(r, 0, &target_node, NULL, NULL);
1237  if (target_node.dbNode != ctx->slot->data.database)
1238  return;
1239 
1240  /* output plugin doesn't look for this origin, no need to queue */
1241  if (FilterByOrigin(ctx, XLogRecGetOrigin(r)))
1242  return;
1243 
1244  change = ReorderBufferGetChange(ctx->reorder);
1246  change->origin_id = XLogRecGetOrigin(r);
1247 
1248  memcpy(&change->data.tp.relnode, &target_node, sizeof(RelFileNode));
1249 
1250  change->data.tp.clear_toast_afterwards = true;
1251 
1253  change, false);
1254 }
struct ReorderBuffer * reorder
Definition: logical.h:42
ReorderBufferChange * ReorderBufferGetChange(ReorderBuffer *rb)
ReplicationSlotPersistentData data
Definition: slot.h:156
static bool FilterByOrigin(LogicalDecodingContext *ctx, RepOriginId origin_id)
Definition: decode.c:611
enum ReorderBufferChangeType action
Definition: reorderbuffer.h:84
#define XLogRecGetOrigin(decoder)
Definition: xlogreader.h:308
XLogReaderState * record
Definition: decode.c:50
union ReorderBufferChange::@97 data
RepOriginId origin_id
Definition: reorderbuffer.h:89
bool XLogRecGetBlockTag(XLogReaderState *record, uint8 block_id, RelFileNode *rnode, ForkNumber *forknum, BlockNumber *blknum)
Definition: xlogreader.c:1491
#define XLogRecGetXid(decoder)
Definition: xlogreader.h:307
void ReorderBufferQueueChange(ReorderBuffer *rb, TransactionId xid, XLogRecPtr lsn, ReorderBufferChange *change, bool toast_insert)
struct ReorderBufferChange::@97::@98 tp
XLogRecPtr origptr
Definition: decode.c:48
ReplicationSlot * slot
Definition: logical.h:38

◆ DecodeStandbyOp()

static void DecodeStandbyOp ( LogicalDecodingContext ctx,
XLogRecordBuffer buf 
)
static

Definition at line 395 of file decode.c.

References elog, ERROR, xl_running_xacts::oldestRunningXid, XLogRecordBuffer::origptr, XLogRecordBuffer::record, LogicalDecodingContext::reorder, ReorderBufferAbortOld(), ReorderBufferProcessXid(), SnapBuildProcessRunningXacts(), LogicalDecodingContext::snapshot_builder, XLOG_INVALIDATIONS, XLOG_RUNNING_XACTS, XLOG_STANDBY_LOCK, XLogRecGetData, XLogRecGetInfo, XLogRecGetXid, and XLR_INFO_MASK.

Referenced by LogicalDecodingProcessRecord().

396 {
397  SnapBuild *builder = ctx->snapshot_builder;
398  XLogReaderState *r = buf->record;
399  uint8 info = XLogRecGetInfo(r) & ~XLR_INFO_MASK;
400 
402 
403  switch (info)
404  {
405  case XLOG_RUNNING_XACTS:
406  {
408 
409  SnapBuildProcessRunningXacts(builder, buf->origptr, running);
410 
411  /*
412  * Abort all transactions that we keep track of, that are
413  * older than the record's oldestRunningXid. This is the most
414  * convenient spot for doing so since, in contrast to shutdown
415  * or end-of-recovery checkpoints, we have information about
416  * all running transactions which includes prepared ones,
417  * while shutdown checkpoints just know that no non-prepared
418  * transactions are in progress.
419  */
421  }
422  break;
423  case XLOG_STANDBY_LOCK:
424  break;
425  case XLOG_INVALIDATIONS:
426 
427  /*
428  * We are processing the invalidations at the command level via
429  * XLOG_XACT_INVALIDATIONS. So we don't need to do anything here.
430  */
431  break;
432  default:
433  elog(ERROR, "unexpected RM_STANDBY_ID record type: %u", info);
434  }
435 }
struct ReorderBuffer * reorder
Definition: logical.h:42
void ReorderBufferAbortOld(ReorderBuffer *rb, TransactionId oldestRunningXid)
#define XLOG_INVALIDATIONS
Definition: standbydefs.h:36
unsigned char uint8
Definition: c.h:439
#define XLOG_STANDBY_LOCK
Definition: standbydefs.h:34
#define XLogRecGetData(decoder)
Definition: xlogreader.h:310
#define ERROR
Definition: elog.h:46
void SnapBuildProcessRunningXacts(SnapBuild *builder, XLogRecPtr lsn, xl_running_xacts *running)
Definition: snapbuild.c:1079
XLogReaderState * record
Definition: decode.c:50
#define XLogRecGetInfo(decoder)
Definition: xlogreader.h:305
#define XLogRecGetXid(decoder)
Definition: xlogreader.h:307
struct SnapBuild * snapshot_builder
Definition: logical.h:43
#define XLR_INFO_MASK
Definition: xlogrecord.h:62
XLogRecPtr origptr
Definition: decode.c:48
#define elog(elevel,...)
Definition: elog.h:232
TransactionId oldestRunningXid
Definition: standbydefs.h:53
#define XLOG_RUNNING_XACTS
Definition: standbydefs.h:35
void ReorderBufferProcessXid(ReorderBuffer *rb, TransactionId xid, XLogRecPtr lsn)

◆ DecodeTruncate()

static void DecodeTruncate ( LogicalDecodingContext ctx,
XLogRecordBuffer buf 
)
static

Definition at line 1085 of file decode.c.

References ReorderBufferChange::action, ReplicationSlot::data, ReorderBufferChange::data, ReplicationSlotPersistentData::database, xl_heap_truncate::dbId, FilterByOrigin(), xl_heap_truncate::flags, xl_heap_truncate::nrelids, ReorderBufferChange::origin_id, XLogRecordBuffer::origptr, XLogRecordBuffer::record, xl_heap_truncate::relids, LogicalDecodingContext::reorder, REORDER_BUFFER_CHANGE_TRUNCATE, ReorderBufferGetChange(), ReorderBufferGetRelids(), ReorderBufferQueueChange(), LogicalDecodingContext::slot, ReorderBufferChange::truncate, XLH_TRUNCATE_CASCADE, XLH_TRUNCATE_RESTART_SEQS, XLogRecGetData, XLogRecGetOrigin, and XLogRecGetXid.

Referenced by DecodeHeapOp().

1086 {
1087  XLogReaderState *r = buf->record;
1088  xl_heap_truncate *xlrec;
1089  ReorderBufferChange *change;
1090 
1091  xlrec = (xl_heap_truncate *) XLogRecGetData(r);
1092 
1093  /* only interested in our database */
1094  if (xlrec->dbId != ctx->slot->data.database)
1095  return;
1096 
1097  /* output plugin doesn't look for this origin, no need to queue */
1098  if (FilterByOrigin(ctx, XLogRecGetOrigin(r)))
1099  return;
1100 
1101  change = ReorderBufferGetChange(ctx->reorder);
1103  change->origin_id = XLogRecGetOrigin(r);
1104  if (xlrec->flags & XLH_TRUNCATE_CASCADE)
1105  change->data.truncate.cascade = true;
1106  if (xlrec->flags & XLH_TRUNCATE_RESTART_SEQS)
1107  change->data.truncate.restart_seqs = true;
1108  change->data.truncate.nrelids = xlrec->nrelids;
1109  change->data.truncate.relids = ReorderBufferGetRelids(ctx->reorder,
1110  xlrec->nrelids);
1111  memcpy(change->data.truncate.relids, xlrec->relids,
1112  xlrec->nrelids * sizeof(Oid));
1114  buf->origptr, change, false);
1115 }
#define XLH_TRUNCATE_CASCADE
Definition: heapam_xlog.h:120
struct ReorderBuffer * reorder
Definition: logical.h:42
ReorderBufferChange * ReorderBufferGetChange(ReorderBuffer *rb)
ReplicationSlotPersistentData data
Definition: slot.h:156
unsigned int Oid
Definition: postgres_ext.h:31
static bool FilterByOrigin(LogicalDecodingContext *ctx, RepOriginId origin_id)
Definition: decode.c:611
enum ReorderBufferChangeType action
Definition: reorderbuffer.h:84
#define XLogRecGetOrigin(decoder)
Definition: xlogreader.h:308
Oid relids[FLEXIBLE_ARRAY_MEMBER]
Definition: heapam_xlog.h:133
#define XLogRecGetData(decoder)
Definition: xlogreader.h:310
XLogReaderState * record
Definition: decode.c:50
union ReorderBufferChange::@97 data
RepOriginId origin_id
Definition: reorderbuffer.h:89
#define XLogRecGetXid(decoder)
Definition: xlogreader.h:307
void ReorderBufferQueueChange(ReorderBuffer *rb, TransactionId xid, XLogRecPtr lsn, ReorderBufferChange *change, bool toast_insert)
#define XLH_TRUNCATE_RESTART_SEQS
Definition: heapam_xlog.h:121
XLogRecPtr origptr
Definition: decode.c:48
struct ReorderBufferChange::@97::@99 truncate
ReplicationSlot * slot
Definition: logical.h:38
Oid * ReorderBufferGetRelids(ReorderBuffer *rb, int nrelids)

◆ DecodeTXNNeedSkip()

static bool DecodeTXNNeedSkip ( LogicalDecodingContext ctx,
XLogRecordBuffer buf,
Oid  dbId,
RepOriginId  origin_id 
)
static

Definition at line 1311 of file decode.c.

References ReplicationSlot::data, ReplicationSlotPersistentData::database, LogicalDecodingContext::fast_forward, FilterByOrigin(), InvalidOid, XLogRecordBuffer::origptr, LogicalDecodingContext::slot, SnapBuildXactNeedsSkip(), and LogicalDecodingContext::snapshot_builder.

Referenced by DecodeAbort(), DecodeCommit(), and DecodePrepare().

1313 {
1314  return (SnapBuildXactNeedsSkip(ctx->snapshot_builder, buf->origptr) ||
1315  (txn_dbid != InvalidOid && txn_dbid != ctx->slot->data.database) ||
1316  ctx->fast_forward || FilterByOrigin(ctx, origin_id));
1317 }
bool SnapBuildXactNeedsSkip(SnapBuild *builder, XLogRecPtr ptr)
Definition: snapbuild.c:385
ReplicationSlotPersistentData data
Definition: slot.h:156
static bool FilterByOrigin(LogicalDecodingContext *ctx, RepOriginId origin_id)
Definition: decode.c:611
#define InvalidOid
Definition: postgres_ext.h:36
struct SnapBuild * snapshot_builder
Definition: logical.h:43
XLogRecPtr origptr
Definition: decode.c:48
ReplicationSlot * slot
Definition: logical.h:38

◆ DecodeUpdate()

static void DecodeUpdate ( LogicalDecodingContext ctx,
XLogRecordBuffer buf 
)
static

Definition at line 962 of file decode.c.

References ReorderBufferChange::action, ReplicationSlot::data, ReorderBufferChange::data, ReplicationSlotPersistentData::database, RelFileNode::dbNode, DecodeXLogTuple(), FilterByOrigin(), xl_heap_update::flags, ReorderBufferChange::origin_id, XLogRecordBuffer::origptr, XLogRecordBuffer::record, LogicalDecodingContext::reorder, REORDER_BUFFER_CHANGE_UPDATE, ReorderBufferGetChange(), ReorderBufferGetTupleBuf(), ReorderBufferQueueChange(), SizeOfHeapHeader, SizeOfHeapUpdate, LogicalDecodingContext::slot, ReorderBufferChange::tp, XLH_UPDATE_CONTAINS_NEW_TUPLE, XLH_UPDATE_CONTAINS_OLD, XLogRecGetBlockData(), XLogRecGetBlockTag(), XLogRecGetData, XLogRecGetDataLen, XLogRecGetOrigin, and XLogRecGetXid.

Referenced by DecodeHeapOp().

963 {
964  XLogReaderState *r = buf->record;
965  xl_heap_update *xlrec;
966  ReorderBufferChange *change;
967  char *data;
968  RelFileNode target_node;
969 
970  xlrec = (xl_heap_update *) XLogRecGetData(r);
971 
972  /* only interested in our database */
973  XLogRecGetBlockTag(r, 0, &target_node, NULL, NULL);
974  if (target_node.dbNode != ctx->slot->data.database)
975  return;
976 
977  /* output plugin doesn't look for this origin, no need to queue */
978  if (FilterByOrigin(ctx, XLogRecGetOrigin(r)))
979  return;
980 
981  change = ReorderBufferGetChange(ctx->reorder);
983  change->origin_id = XLogRecGetOrigin(r);
984  memcpy(&change->data.tp.relnode, &target_node, sizeof(RelFileNode));
985 
987  {
988  Size datalen;
989  Size tuplelen;
990 
991  data = XLogRecGetBlockData(r, 0, &datalen);
992 
993  tuplelen = datalen - SizeOfHeapHeader;
994 
995  change->data.tp.newtuple =
996  ReorderBufferGetTupleBuf(ctx->reorder, tuplelen);
997 
998  DecodeXLogTuple(data, datalen, change->data.tp.newtuple);
999  }
1000 
1001  if (xlrec->flags & XLH_UPDATE_CONTAINS_OLD)
1002  {
1003  Size datalen;
1004  Size tuplelen;
1005 
1006  /* caution, remaining data in record is not aligned */
1007  data = XLogRecGetData(r) + SizeOfHeapUpdate;
1008  datalen = XLogRecGetDataLen(r) - SizeOfHeapUpdate;
1009  tuplelen = datalen - SizeOfHeapHeader;
1010 
1011  change->data.tp.oldtuple =
1012  ReorderBufferGetTupleBuf(ctx->reorder, tuplelen);
1013 
1014  DecodeXLogTuple(data, datalen, change->data.tp.oldtuple);
1015  }
1016 
1017  change->data.tp.clear_toast_afterwards = true;
1018 
1020  change, false);
1021 }
struct ReorderBuffer * reorder
Definition: logical.h:42
static void DecodeXLogTuple(char *data, Size len, ReorderBufferTupleBuf *tup)
Definition: decode.c:1265
ReorderBufferChange * ReorderBufferGetChange(ReorderBuffer *rb)
ReplicationSlotPersistentData data
Definition: slot.h:156
static bool FilterByOrigin(LogicalDecodingContext *ctx, RepOriginId origin_id)
Definition: decode.c:611
enum ReorderBufferChangeType action
Definition: reorderbuffer.h:84
#define XLogRecGetOrigin(decoder)
Definition: xlogreader.h:308
#define XLH_UPDATE_CONTAINS_NEW_TUPLE
Definition: heapam_xlog.h:84
#define SizeOfHeapUpdate
Definition: heapam_xlog.h:227
#define XLogRecGetData(decoder)
Definition: xlogreader.h:310
#define XLogRecGetDataLen(decoder)
Definition: xlogreader.h:311
XLogReaderState * record
Definition: decode.c:50
union ReorderBufferChange::@97 data
RepOriginId origin_id
Definition: reorderbuffer.h:89
bool XLogRecGetBlockTag(XLogReaderState *record, uint8 block_id, RelFileNode *rnode, ForkNumber *forknum, BlockNumber *blknum)
Definition: xlogreader.c:1491
#define XLogRecGetXid(decoder)
Definition: xlogreader.h:307
char * XLogRecGetBlockData(XLogReaderState *record, uint8 block_id, Size *len)
Definition: xlogreader.c:1515
void ReorderBufferQueueChange(ReorderBuffer *rb, TransactionId xid, XLogRecPtr lsn, ReorderBufferChange *change, bool toast_insert)
size_t Size
Definition: c.h:540
struct ReorderBufferChange::@97::@98 tp
XLogRecPtr origptr
Definition: decode.c:48
ReplicationSlot * slot
Definition: logical.h:38
#define XLH_UPDATE_CONTAINS_OLD
Definition: heapam_xlog.h:89
#define SizeOfHeapHeader
Definition: heapam_xlog.h:151
ReorderBufferTupleBuf * ReorderBufferGetTupleBuf(ReorderBuffer *rb, Size tuple_len)

◆ DecodeXactOp()

static void DecodeXactOp ( LogicalDecodingContext ctx,
XLogRecordBuffer buf 
)
static

Definition at line 237 of file decode.c.

References DecodeAbort(), DecodeCommit(), DecodePrepare(), elog, ERROR, LogicalDecodingContext::fast_forward, FilterPrepare(), xl_xact_invals::msgs, xl_xact_invals::nmsgs, XLogRecordBuffer::origptr, ParseAbortRecord(), ParseCommitRecord(), ParsePrepareRecord(), XLogRecordBuffer::record, LogicalDecodingContext::reorder, ReorderBufferAddInvalidations(), ReorderBufferImmediateInvalidation(), ReorderBufferProcessXid(), ReorderBufferXidSetCatalogChanges(), SNAPBUILD_FULL_SNAPSHOT, SnapBuildCurrentState(), LogicalDecodingContext::snapshot_builder, TransactionIdIsValid, xl_xact_parsed_commit::twophase_gid, xl_xact_parsed_abort::twophase_gid, xl_xact_parsed_commit::twophase_xid, xl_xact_parsed_abort::twophase_xid, XLOG_XACT_ABORT, XLOG_XACT_ABORT_PREPARED, XLOG_XACT_ASSIGNMENT, XLOG_XACT_COMMIT, XLOG_XACT_COMMIT_PREPARED, XLOG_XACT_INVALIDATIONS, XLOG_XACT_OPMASK, XLOG_XACT_PREPARE, XLogRecGetData, XLogRecGetInfo, and XLogRecGetXid.

Referenced by LogicalDecodingProcessRecord().

238 {
239  SnapBuild *builder = ctx->snapshot_builder;
240  ReorderBuffer *reorder = ctx->reorder;
241  XLogReaderState *r = buf->record;
243 
244  /*
245  * If the snapshot isn't yet fully built, we cannot decode anything, so
246  * bail out.
247  */
249  return;
250 
251  switch (info)
252  {
253  case XLOG_XACT_COMMIT:
255  {
256  xl_xact_commit *xlrec;
257  xl_xact_parsed_commit parsed;
258  TransactionId xid;
259  bool two_phase = false;
260 
261  xlrec = (xl_xact_commit *) XLogRecGetData(r);
262  ParseCommitRecord(XLogRecGetInfo(buf->record), xlrec, &parsed);
263 
264  if (!TransactionIdIsValid(parsed.twophase_xid))
265  xid = XLogRecGetXid(r);
266  else
267  xid = parsed.twophase_xid;
268 
269  /*
270  * We would like to process the transaction in a two-phase
271  * manner iff output plugin supports two-phase commits and
272  * doesn't filter the transaction at prepare time.
273  */
274  if (info == XLOG_XACT_COMMIT_PREPARED)
275  two_phase = !(FilterPrepare(ctx, xid,
276  parsed.twophase_gid));
277 
278  DecodeCommit(ctx, buf, &parsed, xid, two_phase);
279  break;
280  }
281  case XLOG_XACT_ABORT:
283  {
284  xl_xact_abort *xlrec;
285  xl_xact_parsed_abort parsed;
286  TransactionId xid;
287  bool two_phase = false;
288 
289  xlrec = (xl_xact_abort *) XLogRecGetData(r);
290  ParseAbortRecord(XLogRecGetInfo(buf->record), xlrec, &parsed);
291 
292  if (!TransactionIdIsValid(parsed.twophase_xid))
293  xid = XLogRecGetXid(r);
294  else
295  xid = parsed.twophase_xid;
296 
297  /*
298  * We would like to process the transaction in a two-phase
299  * manner iff output plugin supports two-phase commits and
300  * doesn't filter the transaction at prepare time.
301  */
302  if (info == XLOG_XACT_ABORT_PREPARED)
303  two_phase = !(FilterPrepare(ctx, xid,
304  parsed.twophase_gid));
305 
306  DecodeAbort(ctx, buf, &parsed, xid, two_phase);
307  break;
308  }
310 
311  /*
312  * We assign subxact to the toplevel xact while processing each
313  * record if required. So, we don't need to do anything here. See
314  * LogicalDecodingProcessRecord.
315  */
316  break;
318  {
319  TransactionId xid;
320  xl_xact_invals *invals;
321 
322  xid = XLogRecGetXid(r);
323  invals = (xl_xact_invals *) XLogRecGetData(r);
324 
325  /*
326  * Execute the invalidations for xid-less transactions,
327  * otherwise, accumulate them so that they can be processed at
328  * the commit time.
329  */
330  if (TransactionIdIsValid(xid))
331  {
332  if (!ctx->fast_forward)
333  ReorderBufferAddInvalidations(reorder, xid,
334  buf->origptr,
335  invals->nmsgs,
336  invals->msgs);
338  buf->origptr);
339  }
340  else if ((!ctx->fast_forward))
342  invals->nmsgs,
343  invals->msgs);
344  }
345  break;
346  case XLOG_XACT_PREPARE:
347  {
348  xl_xact_parsed_prepare parsed;
349  xl_xact_prepare *xlrec;
350 
351  /* ok, parse it */
352  xlrec = (xl_xact_prepare *) XLogRecGetData(r);
354  xlrec, &parsed);
355 
356  /*
357  * We would like to process the transaction in a two-phase
358  * manner iff output plugin supports two-phase commits and
359  * doesn't filter the transaction at prepare time.
360  */
361  if (FilterPrepare(ctx, parsed.twophase_xid,
362  parsed.twophase_gid))
363  {
364  ReorderBufferProcessXid(reorder, parsed.twophase_xid,
365  buf->origptr);
366  break;
367  }
368 
369  /*
370  * Note that if the prepared transaction has locked [user]
371  * catalog tables exclusively then decoding prepare can block
372  * till the main transaction is committed because it needs to
373  * lock the catalog tables.
374  *
375  * XXX Now, this can even lead to a deadlock if the prepare
376  * transaction is waiting to get it logically replicated for
377  * distributed 2PC. Currently, we don't have an in-core
378  * implementation of prepares for distributed 2PC but some
379  * out-of-core logical replication solution can have such an
380  * implementation. They need to inform users to not have locks
381  * on catalog tables in such transactions.
382  */
383  DecodePrepare(ctx, buf, &parsed);
384  break;
385  }
386  default:
387  elog(ERROR, "unexpected RM_XACT_ID record type: %u", info);
388  }
389 }
#define XLOG_XACT_COMMIT
Definition: xact.h:148
struct ReorderBuffer * reorder
Definition: logical.h:42
#define XLOG_XACT_PREPARE
Definition: xact.h:149
uint32 TransactionId
Definition: c.h:587
SnapBuildState SnapBuildCurrentState(SnapBuild *builder)
Definition: snapbuild.c:367
#define XLOG_XACT_INVALIDATIONS
Definition: xact.h:154
TransactionId twophase_xid
Definition: xact.h:340
unsigned char uint8
Definition: c.h:439
#define XLogRecGetData(decoder)
Definition: xlogreader.h:310
#define ERROR
Definition: elog.h:46
SharedInvalidationMessage msgs[FLEXIBLE_ARRAY_MEMBER]
Definition: xact.h:255
static void DecodeCommit(LogicalDecodingContext *ctx, XLogRecordBuffer *buf, xl_xact_parsed_commit *parsed, TransactionId xid, bool two_phase)
Definition: decode.c:678
void ReorderBufferImmediateInvalidation(ReorderBuffer *rb, uint32 ninvalidations, SharedInvalidationMessage *invalidations)
void ParseCommitRecord(uint8 info, xl_xact_commit *xlrec, xl_xact_parsed_commit *parsed)
Definition: xactdesc.c:34
#define XLOG_XACT_ABORT_PREPARED
Definition: xact.h:152
XLogReaderState * record
Definition: decode.c:50
#define XLogRecGetInfo(decoder)
Definition: xlogreader.h:305
#define XLogRecGetXid(decoder)
Definition: xlogreader.h:307
#define XLOG_XACT_ASSIGNMENT
Definition: xact.h:153
void ParsePrepareRecord(uint8 info, xl_xact_prepare *xlrec, xl_xact_parsed_prepare *parsed)
Definition: xactdesc.c:216
TransactionId twophase_xid
Definition: xact.h:365
struct SnapBuild * snapshot_builder
Definition: logical.h:43
#define XLOG_XACT_ABORT
Definition: xact.h:150
void ReorderBufferAddInvalidations(ReorderBuffer *rb, TransactionId xid, XLogRecPtr lsn, Size nmsgs, SharedInvalidationMessage *msgs)
static bool FilterPrepare(LogicalDecodingContext *ctx, TransactionId xid, const char *gid)
Definition: decode.c:588
#define XLOG_XACT_OPMASK
Definition: xact.h:158
void ParseAbortRecord(uint8 info, xl_xact_abort *xlrec, xl_xact_parsed_abort *parsed)
Definition: xactdesc.c:129
XLogRecPtr origptr
Definition: decode.c:48
#define elog(elevel,...)
Definition: elog.h:232
void ReorderBufferXidSetCatalogChanges(ReorderBuffer *rb, TransactionId xid, XLogRecPtr lsn)
#define TransactionIdIsValid(xid)
Definition: transam.h:41
#define XLOG_XACT_COMMIT_PREPARED
Definition: xact.h:151
void ReorderBufferProcessXid(ReorderBuffer *rb, TransactionId xid, XLogRecPtr lsn)
static void DecodePrepare(LogicalDecodingContext *ctx, XLogRecordBuffer *buf, xl_xact_parsed_prepare *parsed)
Definition: decode.c:773
int nmsgs
Definition: xact.h:254
static void DecodeAbort(LogicalDecodingContext *ctx, XLogRecordBuffer *buf, xl_xact_parsed_abort *parsed, TransactionId xid, bool two_phase)
Definition: decode.c:848
char twophase_gid[GIDSIZE]
Definition: xact.h:341
char twophase_gid[GIDSIZE]
Definition: xact.h:366

◆ DecodeXLogOp()

static void DecodeXLogOp ( LogicalDecodingContext ctx,
XLogRecordBuffer buf 
)
static

Definition at line 195 of file decode.c.

References elog, ERROR, XLogRecordBuffer::origptr, XLogRecordBuffer::record, LogicalDecodingContext::reorder, ReorderBufferProcessXid(), SnapBuildSerializationPoint(), LogicalDecodingContext::snapshot_builder, XLOG_BACKUP_END, XLOG_CHECKPOINT_ONLINE, XLOG_CHECKPOINT_SHUTDOWN, XLOG_END_OF_RECOVERY, XLOG_FPI, XLOG_FPI_FOR_HINT, XLOG_FPW_CHANGE, XLOG_NEXTOID, XLOG_NOOP, XLOG_PARAMETER_CHANGE, XLOG_RESTORE_POINT, XLOG_SWITCH, XLogRecGetInfo, XLogRecGetXid, and XLR_INFO_MASK.

Referenced by LogicalDecodingProcessRecord().

196 {
197  SnapBuild *builder = ctx->snapshot_builder;
198  uint8 info = XLogRecGetInfo(buf->record) & ~XLR_INFO_MASK;
199 
201  buf->origptr);
202 
203  switch (info)
204  {
205  /* this is also used in END_OF_RECOVERY checkpoints */
208  SnapBuildSerializationPoint(builder, buf->origptr);
209 
210  break;
212 
213  /*
214  * a RUNNING_XACTS record will have been logged near to this, we
215  * can restart from there.
216  */
217  break;
218  case XLOG_NOOP:
219  case XLOG_NEXTOID:
220  case XLOG_SWITCH:
221  case XLOG_BACKUP_END:
223  case XLOG_RESTORE_POINT:
224  case XLOG_FPW_CHANGE:
225  case XLOG_FPI_FOR_HINT:
226  case XLOG_FPI:
227  break;
228  default:
229  elog(ERROR, "unexpected RM_XLOG_ID record type: %u", info);
230  }
231 }
#define XLOG_FPW_CHANGE
Definition: pg_control.h:75
struct ReorderBuffer * reorder
Definition: logical.h:42
#define XLOG_CHECKPOINT_ONLINE
Definition: pg_control.h:68
unsigned char uint8
Definition: c.h:439
#define XLOG_RESTORE_POINT
Definition: pg_control.h:74
#define XLOG_END_OF_RECOVERY
Definition: pg_control.h:76
#define XLOG_NEXTOID
Definition: pg_control.h:70
#define ERROR
Definition: elog.h:46
#define XLOG_PARAMETER_CHANGE
Definition: pg_control.h:73
#define XLOG_CHECKPOINT_SHUTDOWN
Definition: pg_control.h:67
XLogReaderState * record
Definition: decode.c:50
#define XLOG_BACKUP_END
Definition: pg_control.h:72
#define XLogRecGetInfo(decoder)
Definition: xlogreader.h:305
#define XLogRecGetXid(decoder)
Definition: xlogreader.h:307
#define XLOG_NOOP
Definition: pg_control.h:69
struct SnapBuild * snapshot_builder
Definition: logical.h:43
#define XLR_INFO_MASK
Definition: xlogrecord.h:62
#define XLOG_FPI_FOR_HINT
Definition: pg_control.h:77
#define XLOG_SWITCH
Definition: pg_control.h:71
XLogRecPtr origptr
Definition: decode.c:48
#define XLOG_FPI
Definition: pg_control.h:78
#define elog(elevel,...)
Definition: elog.h:232
void SnapBuildSerializationPoint(SnapBuild *builder, XLogRecPtr lsn)
Definition: snapbuild.c:1456
void ReorderBufferProcessXid(ReorderBuffer *rb, TransactionId xid, XLogRecPtr lsn)

◆ DecodeXLogTuple()

static void DecodeXLogTuple ( char *  data,
Size  len,
ReorderBufferTupleBuf tup 
)
static

Definition at line 1265 of file decode.c.

References Assert, header(), InvalidOid, ItemPointerSetInvalid, SizeOfHeapHeader, SizeofHeapTupleHeader, HeapTupleData::t_data, HeapTupleHeaderData::t_hoff, HeapTupleHeaderData::t_infomask, HeapTupleHeaderData::t_infomask2, HeapTupleData::t_len, HeapTupleData::t_self, HeapTupleData::t_tableOid, and ReorderBufferTupleBuf::tuple.

Referenced by DecodeDelete(), DecodeInsert(), and DecodeUpdate().

1266 {
1267  xl_heap_header xlhdr;
1268  int datalen = len - SizeOfHeapHeader;
1270 
1271  Assert(datalen >= 0);
1272 
1273  tuple->tuple.t_len = datalen + SizeofHeapTupleHeader;
1274  header = tuple->tuple.t_data;
1275 
1276  /* not a disk based tuple */
1277  ItemPointerSetInvalid(&tuple->tuple.t_self);
1278 
1279  /* we can only figure this out after reassembling the transactions */
1280  tuple->tuple.t_tableOid = InvalidOid;
1281 
1282  /* data is not stored aligned, copy to aligned storage */
1283  memcpy((char *) &xlhdr,
1284  data,
1285  SizeOfHeapHeader);
1286 
1287  memset(header, 0, SizeofHeapTupleHeader);
1288 
1289  memcpy(((char *) tuple->tuple.t_data) + SizeofHeapTupleHeader,
1290  data + SizeOfHeapHeader,
1291  datalen);
1292 
1293  header->t_infomask = xlhdr.t_infomask;
1294  header->t_infomask2 = xlhdr.t_infomask2;
1295  header->t_hoff = xlhdr.t_hoff;
1296 }
#define SizeofHeapTupleHeader
Definition: htup_details.h:184
uint16 t_infomask2
Definition: heapam_xlog.h:146
#define InvalidOid
Definition: postgres_ext.h:36
#define Assert(condition)
Definition: c.h:804
uint16 t_infomask
Definition: heapam_xlog.h:147
static void header(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:210
#define ItemPointerSetInvalid(pointer)
Definition: itemptr.h:172
#define SizeOfHeapHeader
Definition: heapam_xlog.h:151

◆ FilterByOrigin()

static bool FilterByOrigin ( LogicalDecodingContext ctx,
RepOriginId  origin_id 
)
inlinestatic

Definition at line 611 of file decode.c.

References LogicalDecodingContext::callbacks, OutputPluginCallbacks::filter_by_origin_cb, and filter_by_origin_cb_wrapper().

Referenced by DecodeDelete(), DecodeInsert(), DecodeLogicalMsgOp(), DecodeMultiInsert(), DecodeSpecConfirm(), DecodeTruncate(), DecodeTXNNeedSkip(), and DecodeUpdate().

612 {
613  if (ctx->callbacks.filter_by_origin_cb == NULL)
614  return false;
615 
616  return filter_by_origin_cb_wrapper(ctx, origin_id);
617 }
OutputPluginCallbacks callbacks
Definition: logical.h:52
bool filter_by_origin_cb_wrapper(LogicalDecodingContext *ctx, RepOriginId origin_id)
Definition: logical.c:1117
LogicalDecodeFilterByOriginCB filter_by_origin_cb

◆ FilterPrepare()

static bool FilterPrepare ( LogicalDecodingContext ctx,
TransactionId  xid,
const char *  gid 
)
inlinestatic

Definition at line 588 of file decode.c.

References LogicalDecodingContext::callbacks, OutputPluginCallbacks::filter_prepare_cb, filter_prepare_cb_wrapper(), and LogicalDecodingContext::twophase.

Referenced by DecodeXactOp().

590 {
591  /*
592  * Skip if decoding of two-phase transactions at PREPARE time is not
593  * enabled. In that case, all two-phase transactions are considered
594  * filtered out and will be applied as regular transactions at COMMIT
595  * PREPARED.
596  */
597  if (!ctx->twophase)
598  return true;
599 
600  /*
601  * The filter_prepare callback is optional. When not supplied, all
602  * prepared transactions should go through.
603  */
604  if (ctx->callbacks.filter_prepare_cb == NULL)
605  return false;
606 
607  return filter_prepare_cb_wrapper(ctx, xid, gid);
608 }
bool filter_prepare_cb_wrapper(LogicalDecodingContext *ctx, TransactionId xid, const char *gid)
Definition: logical.c:1086
OutputPluginCallbacks callbacks
Definition: logical.h:52
LogicalDecodeFilterPrepareCB filter_prepare_cb

◆ LogicalDecodingProcessRecord()

void LogicalDecodingProcessRecord ( LogicalDecodingContext ctx,
XLogReaderState record 
)

Definition at line 106 of file decode.c.

References buf, XLogReaderState::decoded_record, DecodeHeap2Op(), DecodeHeapOp(), DecodeLogicalMsgOp(), DecodeStandbyOp(), DecodeXactOp(), DecodeXLogOp(), elog, XLogRecordBuffer::endptr, XLogReaderState::EndRecPtr, ERROR, XLogRecordBuffer::origptr, LogicalDecodingContext::reader, XLogReaderState::ReadRecPtr, XLogRecordBuffer::record, LogicalDecodingContext::reorder, ReorderBufferAssignChild(), ReorderBufferProcessXid(), RM_NEXT_ID, TransactionIdIsValid, XLogRecord::xl_xid, XLogRecGetRmid, XLogRecGetTopXid, and XLogRecGetXid.

Referenced by DecodingContextFindStartpoint(), pg_logical_replication_slot_advance(), pg_logical_slot_get_changes_guts(), and XLogSendLogical().

107 {
109  TransactionId txid;
110 
111  buf.origptr = ctx->reader->ReadRecPtr;
112  buf.endptr = ctx->reader->EndRecPtr;
113  buf.record = record;
114 
115  txid = XLogRecGetTopXid(record);
116 
117  /*
118  * If the top-level xid is valid, we need to assign the subxact to the
119  * top-level xact. We need to do this for all records, hence we do it
120  * before the switch.
121  */
122  if (TransactionIdIsValid(txid))
123  {
125  txid,
126  record->decoded_record->xl_xid,
127  buf.origptr);
128  }
129 
130  /* cast so we get a warning when new rmgrs are added */
131  switch ((RmgrId) XLogRecGetRmid(record))
132  {
133  /*
134  * Rmgrs we care about for logical decoding. Add new rmgrs in
135  * rmgrlist.h's order.
136  */
137  case RM_XLOG_ID:
138  DecodeXLogOp(ctx, &buf);
139  break;
140 
141  case RM_XACT_ID:
142  DecodeXactOp(ctx, &buf);
143  break;
144 
145  case RM_STANDBY_ID:
146  DecodeStandbyOp(ctx, &buf);
147  break;
148 
149  case RM_HEAP2_ID:
150  DecodeHeap2Op(ctx, &buf);
151  break;
152 
153  case RM_HEAP_ID:
154  DecodeHeapOp(ctx, &buf);
155  break;
156 
157  case RM_LOGICALMSG_ID:
158  DecodeLogicalMsgOp(ctx, &buf);
159  break;
160 
161  /*
162  * Rmgrs irrelevant for logical decoding; they describe stuff not
163  * represented in logical decoding. Add new rmgrs in rmgrlist.h's
164  * order.
165  */
166  case RM_SMGR_ID:
167  case RM_CLOG_ID:
168  case RM_DBASE_ID:
169  case RM_TBLSPC_ID:
170  case RM_MULTIXACT_ID:
171  case RM_RELMAP_ID:
172  case RM_BTREE_ID:
173  case RM_HASH_ID:
174  case RM_GIN_ID:
175  case RM_GIST_ID:
176  case RM_SEQ_ID:
177  case RM_SPGIST_ID:
178  case RM_BRIN_ID:
179  case RM_COMMIT_TS_ID:
180  case RM_REPLORIGIN_ID:
181  case RM_GENERIC_ID:
182  /* just deal with xid, and done */
184  buf.origptr);
185  break;
186  case RM_NEXT_ID:
187  elog(ERROR, "unexpected RM_NEXT_ID rmgr_id: %u", (RmgrIds) XLogRecGetRmid(buf.record));
188  }
189 }
struct ReorderBuffer * reorder
Definition: logical.h:42
uint32 TransactionId
Definition: c.h:587
XLogRecPtr EndRecPtr
Definition: xlogreader.h:176
static void DecodeXactOp(LogicalDecodingContext *ctx, XLogRecordBuffer *buf)
Definition: decode.c:237
static void DecodeLogicalMsgOp(LogicalDecodingContext *ctx, XLogRecordBuffer *buf)
Definition: decode.c:623
#define ERROR
Definition: elog.h:46
XLogRecPtr endptr
Definition: decode.c:49
static char * buf
Definition: pg_test_fsync.c:68
XLogRecPtr ReadRecPtr
Definition: xlogreader.h:175
XLogRecord * decoded_record
Definition: xlogreader.h:186
XLogReaderState * record
Definition: decode.c:50
static void DecodeXLogOp(LogicalDecodingContext *ctx, XLogRecordBuffer *buf)
Definition: decode.c:195
void ReorderBufferAssignChild(ReorderBuffer *rb, TransactionId xid, TransactionId subxid, XLogRecPtr lsn)
static void DecodeHeap2Op(LogicalDecodingContext *ctx, XLogRecordBuffer *buf)
Definition: decode.c:441
#define XLogRecGetXid(decoder)
Definition: xlogreader.h:307
uint8 RmgrId
Definition: rmgr.h:11
TransactionId xl_xid
Definition: xlogrecord.h:44
XLogRecPtr origptr
Definition: decode.c:48
XLogReaderState * reader
Definition: logical.h:41
#define elog(elevel,...)
Definition: elog.h:232
static void DecodeHeapOp(LogicalDecodingContext *ctx, XLogRecordBuffer *buf)
Definition: decode.c:501
#define XLogRecGetTopXid(decoder)
Definition: xlogreader.h:309
#define TransactionIdIsValid(xid)
Definition: transam.h:41
void ReorderBufferProcessXid(ReorderBuffer *rb, TransactionId xid, XLogRecPtr lsn)
RmgrIds
Definition: rmgr.h:25
static void DecodeStandbyOp(LogicalDecodingContext *ctx, XLogRecordBuffer *buf)
Definition: decode.c:395
#define XLogRecGetRmid(decoder)
Definition: xlogreader.h:306