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, 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 841 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().

844 {
845  int i;
846  XLogRecPtr origin_lsn = InvalidXLogRecPtr;
847  TimestampTz abort_time = parsed->xact_time;
848  XLogRecPtr origin_id = XLogRecGetOrigin(buf->record);
849  bool skip_xact;
850 
851  if (parsed->xinfo & XACT_XINFO_HAS_ORIGIN)
852  {
853  origin_lsn = parsed->origin_lsn;
854  abort_time = parsed->origin_timestamp;
855  }
856 
857  /*
858  * Check whether we need to process this transaction. See
859  * DecodeTXNNeedSkip for the reasons why we sometimes want to skip the
860  * transaction.
861  */
862  skip_xact = DecodeTXNNeedSkip(ctx, buf, parsed->dbId, origin_id);
863 
864  /*
865  * Send the final rollback record for a prepared transaction unless we
866  * need to skip it. For non-two-phase xacts, simply forget the xact.
867  */
868  if (two_phase && !skip_xact)
869  {
870  ReorderBufferFinishPrepared(ctx->reorder, xid, buf->origptr, buf->endptr,
871  abort_time, origin_id, origin_lsn,
873  parsed->twophase_gid, false);
874  }
875  else
876  {
877  for (i = 0; i < parsed->nsubxacts; i++)
878  {
879  ReorderBufferAbort(ctx->reorder, parsed->subxacts[i],
880  buf->record->EndRecPtr);
881  }
882 
883  ReorderBufferAbort(ctx->reorder, xid, buf->record->EndRecPtr);
884  }
885 
886  /* update the decoding stats */
887  UpdateDecodingStats(ctx);
888 }
#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:1304
void UpdateDecodingStats(LogicalDecodingContext *ctx)
Definition: logical.c:1762
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 673 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().

676 {
677  XLogRecPtr origin_lsn = InvalidXLogRecPtr;
678  TimestampTz commit_time = parsed->xact_time;
679  RepOriginId origin_id = XLogRecGetOrigin(buf->record);
680  int i;
681 
682  if (parsed->xinfo & XACT_XINFO_HAS_ORIGIN)
683  {
684  origin_lsn = parsed->origin_lsn;
685  commit_time = parsed->origin_timestamp;
686  }
687 
689  parsed->nsubxacts, parsed->subxacts);
690 
691  /* ----
692  * Check whether we are interested in this specific transaction, and tell
693  * the reorderbuffer to forget the content of the (sub-)transactions
694  * if not.
695  *
696  * We can't just use ReorderBufferAbort() here, because we need to execute
697  * the transaction's invalidations. This currently won't be needed if
698  * we're just skipping over the transaction because currently we only do
699  * so during startup, to get to the first transaction the client needs. As
700  * we have reset the catalog caches before starting to read WAL, and we
701  * haven't yet touched any catalogs, there can't be anything to invalidate.
702  * But if we're "forgetting" this commit because it happened in another
703  * database, the invalidations might be important, because they could be
704  * for shared catalogs and we might have loaded data into the relevant
705  * syscaches.
706  * ---
707  */
708  if (DecodeTXNNeedSkip(ctx, buf, parsed->dbId, origin_id))
709  {
710  for (i = 0; i < parsed->nsubxacts; i++)
711  {
712  ReorderBufferForget(ctx->reorder, parsed->subxacts[i], buf->origptr);
713  }
714  ReorderBufferForget(ctx->reorder, xid, buf->origptr);
715 
716  return;
717  }
718 
719  /* tell the reorderbuffer about the surviving subtransactions */
720  for (i = 0; i < parsed->nsubxacts; i++)
721  {
722  ReorderBufferCommitChild(ctx->reorder, xid, parsed->subxacts[i],
723  buf->origptr, buf->endptr);
724  }
725 
726  /*
727  * Send the final commit record if the transaction data is already
728  * decoded, otherwise, process the entire transaction.
729  */
730  if (two_phase)
731  {
732  ReorderBufferFinishPrepared(ctx->reorder, xid, buf->origptr, buf->endptr,
734  commit_time, origin_id, origin_lsn,
735  parsed->twophase_gid, true);
736  }
737  else
738  {
739  ReorderBufferCommit(ctx->reorder, xid, buf->origptr, buf->endptr,
740  commit_time, origin_id, origin_lsn);
741  }
742 
743  /*
744  * Update the decoding stats at transaction prepare/commit/abort. It is
745  * not clear that sending more or less frequently than this would be
746  * better.
747  */
748  UpdateDecodingStats(ctx);
749 }
#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:1304
void ReorderBufferCommitChild(ReorderBuffer *rb, TransactionId xid, TransactionId subxid, XLogRecPtr commit_lsn, XLogRecPtr end_lsn)
void UpdateDecodingStats(LogicalDecodingContext *ctx)
Definition: logical.c:1762
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 1022 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().

1023 {
1024  XLogReaderState *r = buf->record;
1025  xl_heap_delete *xlrec;
1026  ReorderBufferChange *change;
1027  RelFileNode target_node;
1028 
1029  xlrec = (xl_heap_delete *) XLogRecGetData(r);
1030 
1031  /* only interested in our database */
1032  XLogRecGetBlockTag(r, 0, &target_node, NULL, NULL);
1033  if (target_node.dbNode != ctx->slot->data.database)
1034  return;
1035 
1036  /*
1037  * Super deletions are irrelevant for logical decoding, it's driven by the
1038  * confirmation records.
1039  */
1040  if (xlrec->flags & XLH_DELETE_IS_SUPER)
1041  return;
1042 
1043  /* output plugin doesn't look for this origin, no need to queue */
1044  if (FilterByOrigin(ctx, XLogRecGetOrigin(r)))
1045  return;
1046 
1047  change = ReorderBufferGetChange(ctx->reorder);
1049  change->origin_id = XLogRecGetOrigin(r);
1050 
1051  memcpy(&change->data.tp.relnode, &target_node, sizeof(RelFileNode));
1052 
1053  /* old primary key stored */
1054  if (xlrec->flags & XLH_DELETE_CONTAINS_OLD)
1055  {
1056  Size datalen = XLogRecGetDataLen(r) - SizeOfHeapDelete;
1057  Size tuplelen = datalen - SizeOfHeapHeader;
1058 
1059  Assert(XLogRecGetDataLen(r) > (SizeOfHeapDelete + SizeOfHeapHeader));
1060 
1061  change->data.tp.oldtuple =
1062  ReorderBufferGetTupleBuf(ctx->reorder, tuplelen);
1063 
1064  DecodeXLogTuple((char *) xlrec + SizeOfHeapDelete,
1065  datalen, change->data.tp.oldtuple);
1066  }
1067 
1068  change->data.tp.clear_toast_afterwards = true;
1069 
1071  change, false);
1072 }
#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:1258
#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:606
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
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::@96::@97 tp
#define Assert(condition)
Definition: c.h:804
size_t Size
Definition: c.h:540
XLogRecPtr origptr
Definition: decode.c:48
ReplicationSlot * slot
Definition: logical.h:38
union ReorderBufferChange::@96 data
#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 437 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_CLEAN, XLOG_HEAP2_CLEANUP_INFO, XLOG_HEAP2_FREEZE_PAGE, XLOG_HEAP2_LOCK_UPDATED, XLOG_HEAP2_MULTI_INSERT, XLOG_HEAP2_NEW_CID, XLOG_HEAP2_REWRITE, XLOG_HEAP2_VISIBLE, XLOG_HEAP_OPMASK, XLogRecGetData, XLogRecGetInfo, and XLogRecGetXid.

Referenced by LogicalDecodingProcessRecord().

438 {
440  TransactionId xid = XLogRecGetXid(buf->record);
441  SnapBuild *builder = ctx->snapshot_builder;
442 
443  ReorderBufferProcessXid(ctx->reorder, xid, buf->origptr);
444 
445  /*
446  * If we don't have snapshot or we are just fast-forwarding, there is no
447  * point in decoding changes.
448  */
450  ctx->fast_forward)
451  return;
452 
453  switch (info)
454  {
456  if (!ctx->fast_forward &&
457  SnapBuildProcessChange(builder, xid, buf->origptr))
458  DecodeMultiInsert(ctx, buf);
459  break;
460  case XLOG_HEAP2_NEW_CID:
461  {
462  xl_heap_new_cid *xlrec;
463 
464  xlrec = (xl_heap_new_cid *) XLogRecGetData(buf->record);
465  SnapBuildProcessNewCid(builder, xid, buf->origptr, xlrec);
466 
467  break;
468  }
469  case XLOG_HEAP2_REWRITE:
470 
471  /*
472  * Although these records only exist to serve the needs of logical
473  * decoding, all the work happens as part of crash or archive
474  * recovery, so we don't need to do anything here.
475  */
476  break;
477 
478  /*
479  * Everything else here is just low level physical stuff we're not
480  * interested in.
481  */
483  case XLOG_HEAP2_CLEAN:
485  case XLOG_HEAP2_VISIBLE:
487  break;
488  default:
489  elog(ERROR, "unexpected RM_HEAP2_ID record type: %u", info);
490  }
491 }
static void DecodeMultiInsert(LogicalDecodingContext *ctx, XLogRecordBuffer *buf)
Definition: decode.c:1116
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_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:45
#define XLOG_HEAP2_CLEAN
Definition: heapam_xlog.h:54
#define XLOG_HEAP2_CLEANUP_INFO
Definition: heapam_xlog.h:56
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 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:55
XLogRecPtr origptr
Definition: decode.c:48
#define elog(elevel,...)
Definition: elog.h:227
void ReorderBufferProcessXid(ReorderBuffer *rb, TransactionId xid, XLogRecPtr lsn)

◆ DecodeHeapOp()

static void DecodeHeapOp ( LogicalDecodingContext ctx,
XLogRecordBuffer buf 
)
static

Definition at line 497 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().

498 {
500  TransactionId xid = XLogRecGetXid(buf->record);
501  SnapBuild *builder = ctx->snapshot_builder;
502 
503  ReorderBufferProcessXid(ctx->reorder, xid, buf->origptr);
504 
505  /*
506  * If we don't have snapshot or we are just fast-forwarding, there is no
507  * point in decoding data changes.
508  */
510  ctx->fast_forward)
511  return;
512 
513  switch (info)
514  {
515  case XLOG_HEAP_INSERT:
516  if (SnapBuildProcessChange(builder, xid, buf->origptr))
517  DecodeInsert(ctx, buf);
518  break;
519 
520  /*
521  * Treat HOT update as normal updates. There is no useful
522  * information in the fact that we could make it a HOT update
523  * locally and the WAL layout is compatible.
524  */
526  case XLOG_HEAP_UPDATE:
527  if (SnapBuildProcessChange(builder, xid, buf->origptr))
528  DecodeUpdate(ctx, buf);
529  break;
530 
531  case XLOG_HEAP_DELETE:
532  if (SnapBuildProcessChange(builder, xid, buf->origptr))
533  DecodeDelete(ctx, buf);
534  break;
535 
536  case XLOG_HEAP_TRUNCATE:
537  if (SnapBuildProcessChange(builder, xid, buf->origptr))
538  DecodeTruncate(ctx, buf);
539  break;
540 
541  case XLOG_HEAP_INPLACE:
542 
543  /*
544  * Inplace updates are only ever performed on catalog tuples and
545  * can, per definition, not change tuple visibility. Since we
546  * don't decode catalog tuples, we're not interested in the
547  * record's contents.
548  *
549  * In-place updates can be used either by XID-bearing transactions
550  * (e.g. in CREATE INDEX CONCURRENTLY) or by XID-less
551  * transactions (e.g. VACUUM). In the former case, the commit
552  * record will include cache invalidations, so we mark the
553  * transaction as catalog modifying here. Currently that's
554  * redundant because the commit will do that as well, but once we
555  * support decoding in-progress relations, this will be important.
556  */
557  if (!TransactionIdIsValid(xid))
558  break;
559 
560  SnapBuildProcessChange(builder, xid, buf->origptr);
562  break;
563 
564  case XLOG_HEAP_CONFIRM:
565  if (SnapBuildProcessChange(builder, xid, buf->origptr))
566  DecodeSpecConfirm(ctx, buf);
567  break;
568 
569  case XLOG_HEAP_LOCK:
570  /* we don't care about row level locks for now */
571  break;
572 
573  default:
574  elog(ERROR, "unexpected RM_HEAP_ID record type: %u", info);
575  break;
576  }
577 }
#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:896
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:955
bool SnapBuildProcessChange(SnapBuild *builder, TransactionId xid, XLogRecPtr lsn)
Definition: snapbuild.c:697
static void DecodeSpecConfirm(LogicalDecodingContext *ctx, XLogRecordBuffer *buf)
Definition: decode.c:1222
#define ERROR
Definition: elog.h:45
static void DecodeDelete(LogicalDecodingContext *ctx, XLogRecordBuffer *buf)
Definition: decode.c:1022
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:1078
#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:227
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 896 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().

897 {
898  Size datalen;
899  char *tupledata;
900  Size tuplelen;
901  XLogReaderState *r = buf->record;
902  xl_heap_insert *xlrec;
903  ReorderBufferChange *change;
904  RelFileNode target_node;
905 
906  xlrec = (xl_heap_insert *) XLogRecGetData(r);
907 
908  /*
909  * Ignore insert records without new tuples (this does happen when
910  * raw_heap_insert marks the TOAST record as HEAP_INSERT_NO_LOGICAL).
911  */
912  if (!(xlrec->flags & XLH_INSERT_CONTAINS_NEW_TUPLE))
913  return;
914 
915  /* only interested in our database */
916  XLogRecGetBlockTag(r, 0, &target_node, NULL, NULL);
917  if (target_node.dbNode != ctx->slot->data.database)
918  return;
919 
920  /* output plugin doesn't look for this origin, no need to queue */
921  if (FilterByOrigin(ctx, XLogRecGetOrigin(r)))
922  return;
923 
924  change = ReorderBufferGetChange(ctx->reorder);
925  if (!(xlrec->flags & XLH_INSERT_IS_SPECULATIVE))
927  else
929  change->origin_id = XLogRecGetOrigin(r);
930 
931  memcpy(&change->data.tp.relnode, &target_node, sizeof(RelFileNode));
932 
933  tupledata = XLogRecGetBlockData(r, 0, &datalen);
934  tuplelen = datalen - SizeOfHeapHeader;
935 
936  change->data.tp.newtuple =
937  ReorderBufferGetTupleBuf(ctx->reorder, tuplelen);
938 
939  DecodeXLogTuple(tupledata, datalen, change->data.tp.newtuple);
940 
941  change->data.tp.clear_toast_afterwards = true;
942 
944  change,
946 }
struct ReorderBuffer * reorder
Definition: logical.h:42
static void DecodeXLogTuple(char *data, Size len, ReorderBufferTupleBuf *tup)
Definition: decode.c:1258
#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:606
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
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)
struct ReorderBufferChange::@96::@97 tp
size_t Size
Definition: c.h:540
XLogRecPtr origptr
Definition: decode.c:48
ReplicationSlot * slot
Definition: logical.h:38
union ReorderBufferChange::@96 data
#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 618 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().

619 {
620  SnapBuild *builder = ctx->snapshot_builder;
621  XLogReaderState *r = buf->record;
622  TransactionId xid = XLogRecGetXid(r);
623  uint8 info = XLogRecGetInfo(r) & ~XLR_INFO_MASK;
624  RepOriginId origin_id = XLogRecGetOrigin(r);
625  Snapshot snapshot;
626  xl_logical_message *message;
627 
628  if (info != XLOG_LOGICAL_MESSAGE)
629  elog(ERROR, "unexpected RM_LOGICALMSG_ID record type: %u", info);
630 
632 
633  /*
634  * If we don't have snapshot or we are just fast-forwarding, there is no
635  * point in decoding messages.
636  */
638  ctx->fast_forward)
639  return;
640 
641  message = (xl_logical_message *) XLogRecGetData(r);
642 
643  if (message->dbId != ctx->slot->data.database ||
644  FilterByOrigin(ctx, origin_id))
645  return;
646 
647  if (message->transactional &&
648  !SnapBuildProcessChange(builder, xid, buf->origptr))
649  return;
650  else if (!message->transactional &&
652  SnapBuildXactNeedsSkip(builder, buf->origptr)))
653  return;
654 
655  snapshot = SnapBuildGetOrBuildSnapshot(builder, xid);
656  ReorderBufferQueueMessage(ctx->reorder, xid, snapshot, buf->endptr,
657  message->transactional,
658  message->message, /* first part of message is
659  * prefix */
660  message->message_size,
661  message->message + message->prefix_size);
662 }
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:606
#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:45
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:227
#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 1116 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().

1117 {
1118  XLogReaderState *r = buf->record;
1119  xl_heap_multi_insert *xlrec;
1120  int i;
1121  char *data;
1122  char *tupledata;
1123  Size tuplelen;
1124  RelFileNode rnode;
1125 
1126  xlrec = (xl_heap_multi_insert *) XLogRecGetData(r);
1127 
1128  /*
1129  * Ignore insert records without new tuples. This happens when a
1130  * multi_insert is done on a catalog or on a non-persistent relation.
1131  */
1132  if (!(xlrec->flags & XLH_INSERT_CONTAINS_NEW_TUPLE))
1133  return;
1134 
1135  /* only interested in our database */
1136  XLogRecGetBlockTag(r, 0, &rnode, NULL, NULL);
1137  if (rnode.dbNode != ctx->slot->data.database)
1138  return;
1139 
1140  /* output plugin doesn't look for this origin, no need to queue */
1141  if (FilterByOrigin(ctx, XLogRecGetOrigin(r)))
1142  return;
1143 
1144  /*
1145  * We know that this multi_insert isn't for a catalog, so the block should
1146  * always have data even if a full-page write of it is taken.
1147  */
1148  tupledata = XLogRecGetBlockData(r, 0, &tuplelen);
1149  Assert(tupledata != NULL);
1150 
1151  data = tupledata;
1152  for (i = 0; i < xlrec->ntuples; i++)
1153  {
1154  ReorderBufferChange *change;
1155  xl_multi_insert_tuple *xlhdr;
1156  int datalen;
1157  ReorderBufferTupleBuf *tuple;
1159 
1160  change = ReorderBufferGetChange(ctx->reorder);
1162  change->origin_id = XLogRecGetOrigin(r);
1163 
1164  memcpy(&change->data.tp.relnode, &rnode, sizeof(RelFileNode));
1165 
1166  xlhdr = (xl_multi_insert_tuple *) SHORTALIGN(data);
1167  data = ((char *) xlhdr) + SizeOfMultiInsertTuple;
1168  datalen = xlhdr->datalen;
1169 
1170  change->data.tp.newtuple =
1171  ReorderBufferGetTupleBuf(ctx->reorder, datalen);
1172 
1173  tuple = change->data.tp.newtuple;
1174  header = tuple->tuple.t_data;
1175 
1176  /* not a disk based tuple */
1178 
1179  /*
1180  * We can only figure this out after reassembling the transactions.
1181  */
1182  tuple->tuple.t_tableOid = InvalidOid;
1183 
1184  tuple->tuple.t_len = datalen + SizeofHeapTupleHeader;
1185 
1186  memset(header, 0, SizeofHeapTupleHeader);
1187 
1188  memcpy((char *) tuple->tuple.t_data + SizeofHeapTupleHeader,
1189  (char *) data,
1190  datalen);
1191  header->t_infomask = xlhdr->t_infomask;
1192  header->t_infomask2 = xlhdr->t_infomask2;
1193  header->t_hoff = xlhdr->t_hoff;
1194 
1195  /*
1196  * Reset toast reassembly state only after the last row in the last
1197  * xl_multi_insert_tuple record emitted by one heap_multi_insert()
1198  * call.
1199  */
1200  if (xlrec->flags & XLH_INSERT_LAST_IN_MULTI &&
1201  (i + 1) == xlrec->ntuples)
1202  change->data.tp.clear_toast_afterwards = true;
1203  else
1204  change->data.tp.clear_toast_afterwards = false;
1205 
1207  buf->origptr, change, false);
1208 
1209  /* move to the next xl_multi_insert_tuple entry */
1210  data += datalen;
1211  }
1212  Assert(data == tupledata + tuplelen);
1213 }
#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:606
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
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
struct ReorderBufferChange::@96::@97 tp
#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
XLogRecPtr origptr
Definition: decode.c:48
#define ItemPointerSetInvalid(pointer)
Definition: itemptr.h:172
ReplicationSlot * slot
Definition: logical.h:38
int i
union ReorderBufferChange::@96 data
#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 767 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().

769 {
770  SnapBuild *builder = ctx->snapshot_builder;
771  XLogRecPtr origin_lsn = parsed->origin_lsn;
772  TimestampTz prepare_time = parsed->xact_time;
773  XLogRecPtr origin_id = XLogRecGetOrigin(buf->record);
774  int i;
775  TransactionId xid = parsed->twophase_xid;
776 
777  if (parsed->origin_timestamp != 0)
778  prepare_time = parsed->origin_timestamp;
779 
780  /*
781  * Remember the prepare info for a txn so that it can be used later in
782  * commit prepared if required. See ReorderBufferFinishPrepared.
783  */
784  if (!ReorderBufferRememberPrepareInfo(ctx->reorder, xid, buf->origptr,
785  buf->endptr, prepare_time, origin_id,
786  origin_lsn))
787  return;
788 
789  /* We can't start streaming unless a consistent state is reached. */
791  {
793  return;
794  }
795 
796  /*
797  * Check whether we need to process this transaction. See
798  * DecodeTXNNeedSkip for the reasons why we sometimes want to skip the
799  * transaction.
800  *
801  * We can't call ReorderBufferForget as we did in DecodeCommit as the txn
802  * hasn't yet been committed, removing this txn before a commit might
803  * result in the computation of an incorrect restart_lsn. See
804  * SnapBuildProcessRunningXacts. But we need to process cache
805  * invalidations if there are any for the reasons mentioned in
806  * DecodeCommit.
807  */
808  if (DecodeTXNNeedSkip(ctx, buf, parsed->dbId, origin_id))
809  {
811  ReorderBufferInvalidate(ctx->reorder, xid, buf->origptr);
812  return;
813  }
814 
815  /* Tell the reorderbuffer about the surviving subtransactions. */
816  for (i = 0; i < parsed->nsubxacts; i++)
817  {
818  ReorderBufferCommitChild(ctx->reorder, xid, parsed->subxacts[i],
819  buf->origptr, buf->endptr);
820  }
821 
822  /* replay actions of all transaction + subtransactions in order */
823  ReorderBufferPrepare(ctx->reorder, xid, parsed->twophase_gid);
824 
825  /*
826  * Update the decoding stats at transaction prepare/commit/abort. It is
827  * not clear that sending more or less frequently than this would be
828  * better.
829  */
830  UpdateDecodingStats(ctx);
831 }
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:1304
void ReorderBufferCommitChild(ReorderBuffer *rb, TransactionId xid, TransactionId subxid, XLogRecPtr commit_lsn, XLogRecPtr end_lsn)
void UpdateDecodingStats(LogicalDecodingContext *ctx)
Definition: logical.c:1762
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 1222 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().

1223 {
1224  XLogReaderState *r = buf->record;
1225  ReorderBufferChange *change;
1226  RelFileNode target_node;
1227 
1228  /* only interested in our database */
1229  XLogRecGetBlockTag(r, 0, &target_node, NULL, NULL);
1230  if (target_node.dbNode != ctx->slot->data.database)
1231  return;
1232 
1233  /* output plugin doesn't look for this origin, no need to queue */
1234  if (FilterByOrigin(ctx, XLogRecGetOrigin(r)))
1235  return;
1236 
1237  change = ReorderBufferGetChange(ctx->reorder);
1239  change->origin_id = XLogRecGetOrigin(r);
1240 
1241  memcpy(&change->data.tp.relnode, &target_node, sizeof(RelFileNode));
1242 
1243  change->data.tp.clear_toast_afterwards = true;
1244 
1246  change, false);
1247 }
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:606
enum ReorderBufferChangeType action
Definition: reorderbuffer.h:84
#define XLogRecGetOrigin(decoder)
Definition: xlogreader.h:308
XLogReaderState * record
Definition: decode.c:50
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::@96::@97 tp
XLogRecPtr origptr
Definition: decode.c:48
ReplicationSlot * slot
Definition: logical.h:38
union ReorderBufferChange::@96 data

◆ DecodeStandbyOp()

static void DecodeStandbyOp ( LogicalDecodingContext ctx,
XLogRecordBuffer buf 
)
static

Definition at line 391 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().

392 {
393  SnapBuild *builder = ctx->snapshot_builder;
394  XLogReaderState *r = buf->record;
395  uint8 info = XLogRecGetInfo(r) & ~XLR_INFO_MASK;
396 
398 
399  switch (info)
400  {
401  case XLOG_RUNNING_XACTS:
402  {
404 
405  SnapBuildProcessRunningXacts(builder, buf->origptr, running);
406 
407  /*
408  * Abort all transactions that we keep track of, that are
409  * older than the record's oldestRunningXid. This is the most
410  * convenient spot for doing so since, in contrast to shutdown
411  * or end-of-recovery checkpoints, we have information about
412  * all running transactions which includes prepared ones,
413  * while shutdown checkpoints just know that no non-prepared
414  * transactions are in progress.
415  */
417  }
418  break;
419  case XLOG_STANDBY_LOCK:
420  break;
421  case XLOG_INVALIDATIONS:
422 
423  /*
424  * We are processing the invalidations at the command level via
425  * XLOG_XACT_INVALIDATIONS. So we don't need to do anything here.
426  */
427  break;
428  default:
429  elog(ERROR, "unexpected RM_STANDBY_ID record type: %u", info);
430  }
431 }
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:45
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:227
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 1078 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().

1079 {
1080  XLogReaderState *r = buf->record;
1081  xl_heap_truncate *xlrec;
1082  ReorderBufferChange *change;
1083 
1084  xlrec = (xl_heap_truncate *) XLogRecGetData(r);
1085 
1086  /* only interested in our database */
1087  if (xlrec->dbId != ctx->slot->data.database)
1088  return;
1089 
1090  /* output plugin doesn't look for this origin, no need to queue */
1091  if (FilterByOrigin(ctx, XLogRecGetOrigin(r)))
1092  return;
1093 
1094  change = ReorderBufferGetChange(ctx->reorder);
1096  change->origin_id = XLogRecGetOrigin(r);
1097  if (xlrec->flags & XLH_TRUNCATE_CASCADE)
1098  change->data.truncate.cascade = true;
1099  if (xlrec->flags & XLH_TRUNCATE_RESTART_SEQS)
1100  change->data.truncate.restart_seqs = true;
1101  change->data.truncate.nrelids = xlrec->nrelids;
1102  change->data.truncate.relids = ReorderBufferGetRelids(ctx->reorder,
1103  xlrec->nrelids);
1104  memcpy(change->data.truncate.relids, xlrec->relids,
1105  xlrec->nrelids * sizeof(Oid));
1107  buf->origptr, change, false);
1108 }
#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:606
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
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
struct ReorderBufferChange::@96::@98 truncate
XLogRecPtr origptr
Definition: decode.c:48
ReplicationSlot * slot
Definition: logical.h:38
Oid * ReorderBufferGetRelids(ReorderBuffer *rb, int nrelids)
union ReorderBufferChange::@96 data

◆ DecodeTXNNeedSkip()

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

Definition at line 1304 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().

1306 {
1307  return (SnapBuildXactNeedsSkip(ctx->snapshot_builder, buf->origptr) ||
1308  (txn_dbid != InvalidOid && txn_dbid != ctx->slot->data.database) ||
1309  ctx->fast_forward || FilterByOrigin(ctx, origin_id));
1310 }
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:606
#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 955 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().

956 {
957  XLogReaderState *r = buf->record;
958  xl_heap_update *xlrec;
959  ReorderBufferChange *change;
960  char *data;
961  RelFileNode target_node;
962 
963  xlrec = (xl_heap_update *) XLogRecGetData(r);
964 
965  /* only interested in our database */
966  XLogRecGetBlockTag(r, 0, &target_node, NULL, NULL);
967  if (target_node.dbNode != ctx->slot->data.database)
968  return;
969 
970  /* output plugin doesn't look for this origin, no need to queue */
971  if (FilterByOrigin(ctx, XLogRecGetOrigin(r)))
972  return;
973 
974  change = ReorderBufferGetChange(ctx->reorder);
976  change->origin_id = XLogRecGetOrigin(r);
977  memcpy(&change->data.tp.relnode, &target_node, sizeof(RelFileNode));
978 
980  {
981  Size datalen;
982  Size tuplelen;
983 
984  data = XLogRecGetBlockData(r, 0, &datalen);
985 
986  tuplelen = datalen - SizeOfHeapHeader;
987 
988  change->data.tp.newtuple =
989  ReorderBufferGetTupleBuf(ctx->reorder, tuplelen);
990 
991  DecodeXLogTuple(data, datalen, change->data.tp.newtuple);
992  }
993 
994  if (xlrec->flags & XLH_UPDATE_CONTAINS_OLD)
995  {
996  Size datalen;
997  Size tuplelen;
998 
999  /* caution, remaining data in record is not aligned */
1000  data = XLogRecGetData(r) + SizeOfHeapUpdate;
1001  datalen = XLogRecGetDataLen(r) - SizeOfHeapUpdate;
1002  tuplelen = datalen - SizeOfHeapHeader;
1003 
1004  change->data.tp.oldtuple =
1005  ReorderBufferGetTupleBuf(ctx->reorder, tuplelen);
1006 
1007  DecodeXLogTuple(data, datalen, change->data.tp.oldtuple);
1008  }
1009 
1010  change->data.tp.clear_toast_afterwards = true;
1011 
1013  change, false);
1014 }
struct ReorderBuffer * reorder
Definition: logical.h:42
static void DecodeXLogTuple(char *data, Size len, ReorderBufferTupleBuf *tup)
Definition: decode.c:1258
ReorderBufferChange * ReorderBufferGetChange(ReorderBuffer *rb)
ReplicationSlotPersistentData data
Definition: slot.h:156
static bool FilterByOrigin(LogicalDecodingContext *ctx, RepOriginId origin_id)
Definition: decode.c:606
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
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)
struct ReorderBufferChange::@96::@97 tp
size_t Size
Definition: c.h:540
XLogRecPtr origptr
Definition: decode.c:48
ReplicationSlot * slot
Definition: logical.h:38
union ReorderBufferChange::@96 data
#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 236 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().

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

195 {
196  SnapBuild *builder = ctx->snapshot_builder;
197  uint8 info = XLogRecGetInfo(buf->record) & ~XLR_INFO_MASK;
198 
200  buf->origptr);
201 
202  switch (info)
203  {
204  /* this is also used in END_OF_RECOVERY checkpoints */
207  SnapBuildSerializationPoint(builder, buf->origptr);
208 
209  break;
211 
212  /*
213  * a RUNNING_XACTS record will have been logged near to this, we
214  * can restart from there.
215  */
216  break;
217  case XLOG_NOOP:
218  case XLOG_NEXTOID:
219  case XLOG_SWITCH:
220  case XLOG_BACKUP_END:
222  case XLOG_RESTORE_POINT:
223  case XLOG_FPW_CHANGE:
224  case XLOG_FPI_FOR_HINT:
225  case XLOG_FPI:
226  break;
227  default:
228  elog(ERROR, "unexpected RM_XLOG_ID record type: %u", info);
229  }
230 }
#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:45
#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:227
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 1258 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().

1259 {
1260  xl_heap_header xlhdr;
1261  int datalen = len - SizeOfHeapHeader;
1263 
1264  Assert(datalen >= 0);
1265 
1266  tuple->tuple.t_len = datalen + SizeofHeapTupleHeader;
1267  header = tuple->tuple.t_data;
1268 
1269  /* not a disk based tuple */
1270  ItemPointerSetInvalid(&tuple->tuple.t_self);
1271 
1272  /* we can only figure this out after reassembling the transactions */
1273  tuple->tuple.t_tableOid = InvalidOid;
1274 
1275  /* data is not stored aligned, copy to aligned storage */
1276  memcpy((char *) &xlhdr,
1277  data,
1278  SizeOfHeapHeader);
1279 
1280  memset(header, 0, SizeofHeapTupleHeader);
1281 
1282  memcpy(((char *) tuple->tuple.t_data) + SizeofHeapTupleHeader,
1283  data + SizeOfHeapHeader,
1284  datalen);
1285 
1286  header->t_infomask = xlhdr.t_infomask;
1287  header->t_infomask2 = xlhdr.t_infomask2;
1288  header->t_hoff = xlhdr.t_hoff;
1289 }
#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 606 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().

607 {
608  if (ctx->callbacks.filter_by_origin_cb == NULL)
609  return false;
610 
611  return filter_by_origin_cb_wrapper(ctx, origin_id);
612 }
OutputPluginCallbacks callbacks
Definition: logical.h:52
bool filter_by_origin_cb_wrapper(LogicalDecodingContext *ctx, RepOriginId origin_id)
Definition: logical.c:1116
LogicalDecodeFilterByOriginCB filter_by_origin_cb

◆ FilterPrepare()

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

Definition at line 584 of file decode.c.

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

Referenced by DecodeXactOp().

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

◆ LogicalDecodingProcessRecord()

void LogicalDecodingProcessRecord ( LogicalDecodingContext ctx,
XLogReaderState record 
)

Definition at line 105 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().

106 {
108  TransactionId txid;
109 
110  buf.origptr = ctx->reader->ReadRecPtr;
111  buf.endptr = ctx->reader->EndRecPtr;
112  buf.record = record;
113 
114  txid = XLogRecGetTopXid(record);
115 
116  /*
117  * If the top-level xid is valid, we need to assign the subxact to the
118  * top-level xact. We need to do this for all records, hence we do it
119  * before the switch.
120  */
121  if (TransactionIdIsValid(txid))
122  {
124  txid,
125  record->decoded_record->xl_xid,
126  buf.origptr);
127  }
128 
129  /* cast so we get a warning when new rmgrs are added */
130  switch ((RmgrId) XLogRecGetRmid(record))
131  {
132  /*
133  * Rmgrs we care about for logical decoding. Add new rmgrs in
134  * rmgrlist.h's order.
135  */
136  case RM_XLOG_ID:
137  DecodeXLogOp(ctx, &buf);
138  break;
139 
140  case RM_XACT_ID:
141  DecodeXactOp(ctx, &buf);
142  break;
143 
144  case RM_STANDBY_ID:
145  DecodeStandbyOp(ctx, &buf);
146  break;
147 
148  case RM_HEAP2_ID:
149  DecodeHeap2Op(ctx, &buf);
150  break;
151 
152  case RM_HEAP_ID:
153  DecodeHeapOp(ctx, &buf);
154  break;
155 
156  case RM_LOGICALMSG_ID:
157  DecodeLogicalMsgOp(ctx, &buf);
158  break;
159 
160  /*
161  * Rmgrs irrelevant for logical decoding; they describe stuff not
162  * represented in logical decoding. Add new rmgrs in rmgrlist.h's
163  * order.
164  */
165  case RM_SMGR_ID:
166  case RM_CLOG_ID:
167  case RM_DBASE_ID:
168  case RM_TBLSPC_ID:
169  case RM_MULTIXACT_ID:
170  case RM_RELMAP_ID:
171  case RM_BTREE_ID:
172  case RM_HASH_ID:
173  case RM_GIN_ID:
174  case RM_GIST_ID:
175  case RM_SEQ_ID:
176  case RM_SPGIST_ID:
177  case RM_BRIN_ID:
178  case RM_COMMIT_TS_ID:
179  case RM_REPLORIGIN_ID:
180  case RM_GENERIC_ID:
181  /* just deal with xid, and done */
183  buf.origptr);
184  break;
185  case RM_NEXT_ID:
186  elog(ERROR, "unexpected RM_NEXT_ID rmgr_id: %u", (RmgrIds) XLogRecGetRmid(buf.record));
187  }
188 }
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:236
static void DecodeLogicalMsgOp(LogicalDecodingContext *ctx, XLogRecordBuffer *buf)
Definition: decode.c:618
#define ERROR
Definition: elog.h:45
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:194
void ReorderBufferAssignChild(ReorderBuffer *rb, TransactionId xid, TransactionId subxid, XLogRecPtr lsn)
static void DecodeHeap2Op(LogicalDecodingContext *ctx, XLogRecordBuffer *buf)
Definition: decode.c:437
#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:227
static void DecodeHeapOp(LogicalDecodingContext *ctx, XLogRecordBuffer *buf)
Definition: decode.c:497
#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:391
#define XLogRecGetRmid(decoder)
Definition: xlogreader.h:306