PostgreSQL Source Code git master
Loading...
Searching...
No Matches
decode.c File Reference
Include dependency graph for decode.c:

Go to the source code of this file.

Functions

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, HeapTuple tuple)
 
static bool FilterPrepare (LogicalDecodingContext *ctx, TransactionId xid, const char *gid)
 
static bool DecodeTXNNeedSkip (LogicalDecodingContext *ctx, XLogRecordBuffer *buf, Oid txn_dbid, ReplOriginId origin_id)
 
void LogicalDecodingProcessRecord (LogicalDecodingContext *ctx, XLogReaderState *record)
 
void xlog_decode (LogicalDecodingContext *ctx, XLogRecordBuffer *buf)
 
void xact_decode (LogicalDecodingContext *ctx, XLogRecordBuffer *buf)
 
void standby_decode (LogicalDecodingContext *ctx, XLogRecordBuffer *buf)
 
void heap2_decode (LogicalDecodingContext *ctx, XLogRecordBuffer *buf)
 
void heap_decode (LogicalDecodingContext *ctx, XLogRecordBuffer *buf)
 
static bool FilterByOrigin (LogicalDecodingContext *ctx, ReplOriginId origin_id)
 
void logicalmsg_decode (LogicalDecodingContext *ctx, XLogRecordBuffer *buf)
 

Function Documentation

◆ DecodeAbort()

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

Definition at line 832 of file decode.c.

835{
836 int i;
837 XLogRecPtr origin_lsn = InvalidXLogRecPtr;
838 TimestampTz abort_time = parsed->xact_time;
839 ReplOriginId origin_id = XLogRecGetOrigin(buf->record);
840 bool skip_xact;
841
842 if (parsed->xinfo & XACT_XINFO_HAS_ORIGIN)
843 {
844 origin_lsn = parsed->origin_lsn;
845 abort_time = parsed->origin_timestamp;
846 }
847
848 /*
849 * Check whether we need to process this transaction. See
850 * DecodeTXNNeedSkip for the reasons why we sometimes want to skip the
851 * transaction.
852 */
853 skip_xact = DecodeTXNNeedSkip(ctx, buf, parsed->dbId, origin_id);
854
855 /*
856 * Send the final rollback record for a prepared transaction unless we
857 * need to skip it. For non-two-phase xacts, simply forget the xact.
858 */
859 if (two_phase && !skip_xact)
860 {
861 ReorderBufferFinishPrepared(ctx->reorder, xid, buf->origptr, buf->endptr,
863 abort_time, origin_id, origin_lsn,
864 parsed->twophase_gid, false);
865 }
866 else
867 {
868 for (i = 0; i < parsed->nsubxacts; i++)
869 {
870 ReorderBufferAbort(ctx->reorder, parsed->subxacts[i],
871 buf->record->EndRecPtr, abort_time);
872 }
873
874 ReorderBufferAbort(ctx->reorder, xid, buf->record->EndRecPtr,
875 abort_time);
876 }
877
878 /* update the decoding stats */
880}
int64 TimestampTz
Definition timestamp.h:39
static bool DecodeTXNNeedSkip(LogicalDecodingContext *ctx, XLogRecordBuffer *buf, Oid txn_dbid, ReplOriginId origin_id)
Definition decode.c:1290
int i
Definition isn.c:77
void UpdateDecodingStats(LogicalDecodingContext *ctx)
Definition logical.c:1943
static bool two_phase
static char buf[DEFAULT_XLOG_SEG_SIZE]
static int fb(int x)
void ReorderBufferAbort(ReorderBuffer *rb, TransactionId xid, XLogRecPtr lsn, TimestampTz abort_time)
void ReorderBufferFinishPrepared(ReorderBuffer *rb, TransactionId xid, XLogRecPtr commit_lsn, XLogRecPtr end_lsn, XLogRecPtr two_phase_at, TimestampTz commit_time, ReplOriginId origin_id, XLogRecPtr origin_lsn, char *gid, bool is_commit)
struct ReorderBuffer * reorder
Definition logical.h:43
#define XACT_XINFO_HAS_ORIGIN
Definition xact.h:194
uint16 ReplOriginId
Definition xlogdefs.h:69
uint64 XLogRecPtr
Definition xlogdefs.h:21
#define InvalidXLogRecPtr
Definition xlogdefs.h:28
#define XLogRecGetOrigin(decoder)
Definition xlogreader.h:412

References buf, DecodeTXNNeedSkip(), fb(), i, InvalidXLogRecPtr, LogicalDecodingContext::reorder, ReorderBufferAbort(), ReorderBufferFinishPrepared(), two_phase, UpdateDecodingStats(), XACT_XINFO_HAS_ORIGIN, and XLogRecGetOrigin.

Referenced by xact_decode().

◆ DecodeCommit()

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

Definition at line 661 of file decode.c.

664{
665 XLogRecPtr origin_lsn = InvalidXLogRecPtr;
666 TimestampTz commit_time = parsed->xact_time;
667 ReplOriginId origin_id = XLogRecGetOrigin(buf->record);
668 int i;
669
670 if (parsed->xinfo & XACT_XINFO_HAS_ORIGIN)
671 {
672 origin_lsn = parsed->origin_lsn;
673 commit_time = parsed->origin_timestamp;
674 }
675
676 SnapBuildCommitTxn(ctx->snapshot_builder, buf->origptr, xid,
677 parsed->nsubxacts, parsed->subxacts,
678 parsed->xinfo);
679
680 /* ----
681 * Check whether we are interested in this specific transaction, and tell
682 * the reorderbuffer to forget the content of the (sub-)transactions
683 * if not.
684 *
685 * We can't just use ReorderBufferAbort() here, because we need to execute
686 * the transaction's invalidations. This currently won't be needed if
687 * we're just skipping over the transaction because currently we only do
688 * so during startup, to get to the first transaction the client needs. As
689 * we have reset the catalog caches before starting to read WAL, and we
690 * haven't yet touched any catalogs, there can't be anything to invalidate.
691 * But if we're "forgetting" this commit because it happened in another
692 * database, the invalidations might be important, because they could be
693 * for shared catalogs and we might have loaded data into the relevant
694 * syscaches.
695 * ---
696 */
697 if (DecodeTXNNeedSkip(ctx, buf, parsed->dbId, origin_id))
698 {
699 for (i = 0; i < parsed->nsubxacts; i++)
700 {
701 ReorderBufferForget(ctx->reorder, parsed->subxacts[i], buf->origptr);
702 }
703 ReorderBufferForget(ctx->reorder, xid, buf->origptr);
704
705 return;
706 }
707
708 /* tell the reorderbuffer about the surviving subtransactions */
709 for (i = 0; i < parsed->nsubxacts; i++)
710 {
711 ReorderBufferCommitChild(ctx->reorder, xid, parsed->subxacts[i],
712 buf->origptr, buf->endptr);
713 }
714
715 /*
716 * Send the final commit record if the transaction data is already
717 * decoded, otherwise, process the entire transaction.
718 */
719 if (two_phase)
720 {
721 ReorderBufferFinishPrepared(ctx->reorder, xid, buf->origptr, buf->endptr,
723 commit_time, origin_id, origin_lsn,
724 parsed->twophase_gid, true);
725 }
726 else
727 {
728 ReorderBufferCommit(ctx->reorder, xid, buf->origptr, buf->endptr,
729 commit_time, origin_id, origin_lsn);
730 }
731
732 /*
733 * Update the decoding stats at transaction prepare/commit/abort.
734 * Additionally we send the stats when we spill or stream the changes to
735 * avoid losing them in case the decoding is interrupted. It is not clear
736 * that sending more or less frequently than this would be better.
737 */
739}
void ReorderBufferForget(ReorderBuffer *rb, TransactionId xid, XLogRecPtr lsn)
void ReorderBufferCommitChild(ReorderBuffer *rb, TransactionId xid, TransactionId subxid, XLogRecPtr commit_lsn, XLogRecPtr end_lsn)
void ReorderBufferCommit(ReorderBuffer *rb, TransactionId xid, XLogRecPtr commit_lsn, XLogRecPtr end_lsn, TimestampTz commit_time, ReplOriginId origin_id, XLogRecPtr origin_lsn)
XLogRecPtr SnapBuildGetTwoPhaseAt(SnapBuild *builder)
Definition snapbuild.c:286
void SnapBuildCommitTxn(SnapBuild *builder, XLogRecPtr lsn, TransactionId xid, int nsubxacts, TransactionId *subxacts, uint32 xinfo)
Definition snapbuild.c:939
struct SnapBuild * snapshot_builder
Definition logical.h:44

References buf, DecodeTXNNeedSkip(), fb(), i, InvalidXLogRecPtr, LogicalDecodingContext::reorder, ReorderBufferCommit(), ReorderBufferCommitChild(), ReorderBufferFinishPrepared(), ReorderBufferForget(), SnapBuildCommitTxn(), SnapBuildGetTwoPhaseAt(), LogicalDecodingContext::snapshot_builder, two_phase, UpdateDecodingStats(), XACT_XINFO_HAS_ORIGIN, and XLogRecGetOrigin.

Referenced by xact_decode().

◆ DecodeDelete()

static void DecodeDelete ( LogicalDecodingContext ctx,
XLogRecordBuffer buf 
)
static

Definition at line 1014 of file decode.c.

1015{
1018 ReorderBufferChange *change;
1019 RelFileLocator target_locator;
1020
1022
1023 /* only interested in our database */
1024 XLogRecGetBlockTag(r, 0, &target_locator, NULL, NULL);
1025 if (target_locator.dbOid != ctx->slot->data.database)
1026 return;
1027
1028 /* output plugin doesn't look for this origin, no need to queue */
1029 if (FilterByOrigin(ctx, XLogRecGetOrigin(r)))
1030 return;
1031
1032 change = ReorderBufferAllocChange(ctx->reorder);
1033
1034 if (xlrec->flags & XLH_DELETE_IS_SUPER)
1036 else
1038
1039 change->origin_id = XLogRecGetOrigin(r);
1040
1041 memcpy(&change->data.tp.rlocator, &target_locator, sizeof(RelFileLocator));
1042
1043 /* old primary key stored */
1044 if (xlrec->flags & XLH_DELETE_CONTAINS_OLD)
1045 {
1047 Size tuplelen = datalen - SizeOfHeapHeader;
1048
1050
1051 change->data.tp.oldtuple =
1053
1055 datalen, change->data.tp.oldtuple);
1056 }
1057
1058 change->data.tp.clear_toast_afterwards = true;
1059
1061 change, false);
1062}
#define Assert(condition)
Definition c.h:885
size_t Size
Definition c.h:631
static bool FilterByOrigin(LogicalDecodingContext *ctx, ReplOriginId origin_id)
Definition decode.c:568
static void DecodeXLogTuple(char *data, Size len, HeapTuple tuple)
Definition decode.c:1246
#define XLH_DELETE_CONTAINS_OLD
#define SizeOfHeapHeader
#define SizeOfHeapDelete
#define XLH_DELETE_IS_SUPER
void ReorderBufferQueueChange(ReorderBuffer *rb, TransactionId xid, XLogRecPtr lsn, ReorderBufferChange *change, bool toast_insert)
HeapTuple ReorderBufferAllocTupleBuf(ReorderBuffer *rb, Size tuple_len)
ReorderBufferChange * ReorderBufferAllocChange(ReorderBuffer *rb)
@ REORDER_BUFFER_CHANGE_INTERNAL_SPEC_ABORT
@ REORDER_BUFFER_CHANGE_DELETE
ReplicationSlot * slot
Definition logical.h:39
ReorderBufferChangeType action
RelFileLocator rlocator
union ReorderBufferChange::@117 data
ReplOriginId origin_id
struct ReorderBufferChange::@117::@118 tp
ReplicationSlotPersistentData data
Definition slot.h:213
DecodedXLogRecord * record
Definition xlogreader.h:235
void XLogRecGetBlockTag(XLogReaderState *record, uint8 block_id, RelFileLocator *rlocator, ForkNumber *forknum, BlockNumber *blknum)
#define XLogRecGetDataLen(decoder)
Definition xlogreader.h:415
#define XLogRecGetData(decoder)
Definition xlogreader.h:414
#define XLogRecGetXid(decoder)
Definition xlogreader.h:411

References ReorderBufferChange::action, Assert, buf, ReorderBufferChange::clear_toast_afterwards, ReorderBufferChange::data, ReplicationSlot::data, ReplicationSlotPersistentData::database, RelFileLocator::dbOid, DecodeXLogTuple(), fb(), FilterByOrigin(), ReorderBufferChange::oldtuple, ReorderBufferChange::origin_id, XLogReaderState::record, LogicalDecodingContext::reorder, REORDER_BUFFER_CHANGE_DELETE, REORDER_BUFFER_CHANGE_INTERNAL_SPEC_ABORT, ReorderBufferAllocChange(), ReorderBufferAllocTupleBuf(), ReorderBufferQueueChange(), ReorderBufferChange::rlocator, SizeOfHeapDelete, SizeOfHeapHeader, LogicalDecodingContext::slot, ReorderBufferChange::tp, XLH_DELETE_CONTAINS_OLD, XLH_DELETE_IS_SUPER, XLogRecGetBlockTag(), XLogRecGetData, XLogRecGetDataLen, XLogRecGetOrigin, and XLogRecGetXid.

Referenced by heap_decode().

◆ DecodeInsert()

static void DecodeInsert ( LogicalDecodingContext ctx,
XLogRecordBuffer buf 
)
static

Definition at line 888 of file decode.c.

889{
890 Size datalen;
891 char *tupledata;
895 ReorderBufferChange *change;
896 RelFileLocator target_locator;
897
899
900 /*
901 * Ignore insert records without new tuples (this does happen when
902 * raw_heap_insert marks the TOAST record as HEAP_INSERT_NO_LOGICAL).
903 */
904 if (!(xlrec->flags & XLH_INSERT_CONTAINS_NEW_TUPLE))
905 return;
906
907 /* only interested in our database */
908 XLogRecGetBlockTag(r, 0, &target_locator, NULL, NULL);
909 if (target_locator.dbOid != ctx->slot->data.database)
910 return;
911
912 /* output plugin doesn't look for this origin, no need to queue */
913 if (FilterByOrigin(ctx, XLogRecGetOrigin(r)))
914 return;
915
916 change = ReorderBufferAllocChange(ctx->reorder);
917 if (!(xlrec->flags & XLH_INSERT_IS_SPECULATIVE))
919 else
921 change->origin_id = XLogRecGetOrigin(r);
922
923 memcpy(&change->data.tp.rlocator, &target_locator, sizeof(RelFileLocator));
924
925 tupledata = XLogRecGetBlockData(r, 0, &datalen);
926 tuplelen = datalen - SizeOfHeapHeader;
927
928 change->data.tp.newtuple =
930
931 DecodeXLogTuple(tupledata, datalen, change->data.tp.newtuple);
932
933 change->data.tp.clear_toast_afterwards = true;
934
936 change,
938}
#define XLH_INSERT_ON_TOAST_RELATION
Definition heapam_xlog.h:76
#define XLH_INSERT_IS_SPECULATIVE
Definition heapam_xlog.h:74
#define XLH_INSERT_CONTAINS_NEW_TUPLE
Definition heapam_xlog.h:75
@ REORDER_BUFFER_CHANGE_INSERT
@ REORDER_BUFFER_CHANGE_INTERNAL_SPEC_INSERT
char * XLogRecGetBlockData(XLogReaderState *record, uint8 block_id, Size *len)

References ReorderBufferChange::action, buf, ReorderBufferChange::clear_toast_afterwards, ReorderBufferChange::data, ReplicationSlot::data, ReplicationSlotPersistentData::database, RelFileLocator::dbOid, DecodeXLogTuple(), fb(), FilterByOrigin(), ReorderBufferChange::newtuple, ReorderBufferChange::origin_id, XLogReaderState::record, LogicalDecodingContext::reorder, REORDER_BUFFER_CHANGE_INSERT, REORDER_BUFFER_CHANGE_INTERNAL_SPEC_INSERT, ReorderBufferAllocChange(), ReorderBufferAllocTupleBuf(), ReorderBufferQueueChange(), ReorderBufferChange::rlocator, 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 heap_decode().

◆ DecodeMultiInsert()

static void DecodeMultiInsert ( LogicalDecodingContext ctx,
XLogRecordBuffer buf 
)
static

Definition at line 1106 of file decode.c.

1107{
1110 int i;
1111 char *data;
1112 char *tupledata;
1113 Size tuplelen;
1114 RelFileLocator rlocator;
1115
1117
1118 /*
1119 * Ignore insert records without new tuples. This happens when a
1120 * multi_insert is done on a catalog or on a non-persistent relation.
1121 */
1122 if (!(xlrec->flags & XLH_INSERT_CONTAINS_NEW_TUPLE))
1123 return;
1124
1125 /* only interested in our database */
1126 XLogRecGetBlockTag(r, 0, &rlocator, NULL, NULL);
1127 if (rlocator.dbOid != ctx->slot->data.database)
1128 return;
1129
1130 /* output plugin doesn't look for this origin, no need to queue */
1131 if (FilterByOrigin(ctx, XLogRecGetOrigin(r)))
1132 return;
1133
1134 /*
1135 * We know that this multi_insert isn't for a catalog, so the block should
1136 * always have data even if a full-page write of it is taken.
1137 */
1138 tupledata = XLogRecGetBlockData(r, 0, &tuplelen);
1139 Assert(tupledata != NULL);
1140
1141 data = tupledata;
1142 for (i = 0; i < xlrec->ntuples; i++)
1143 {
1144 ReorderBufferChange *change;
1146 int datalen;
1147 HeapTuple tuple;
1148 HeapTupleHeader header;
1149
1150 change = ReorderBufferAllocChange(ctx->reorder);
1152 change->origin_id = XLogRecGetOrigin(r);
1153
1154 memcpy(&change->data.tp.rlocator, &rlocator, sizeof(RelFileLocator));
1155
1157 data = ((char *) xlhdr) + SizeOfMultiInsertTuple;
1158 datalen = xlhdr->datalen;
1159
1160 change->data.tp.newtuple =
1161 ReorderBufferAllocTupleBuf(ctx->reorder, datalen);
1162
1163 tuple = change->data.tp.newtuple;
1164 header = tuple->t_data;
1165
1166 /* not a disk based tuple */
1168
1169 /*
1170 * We can only figure this out after reassembling the transactions.
1171 */
1172 tuple->t_tableOid = InvalidOid;
1173
1174 tuple->t_len = datalen + SizeofHeapTupleHeader;
1175
1176 memset(header, 0, SizeofHeapTupleHeader);
1177
1178 memcpy((char *) tuple->t_data + SizeofHeapTupleHeader, data, datalen);
1179 header->t_infomask = xlhdr->t_infomask;
1180 header->t_infomask2 = xlhdr->t_infomask2;
1181 header->t_hoff = xlhdr->t_hoff;
1182
1183 /*
1184 * Reset toast reassembly state only after the last row in the last
1185 * xl_multi_insert_tuple record emitted by one heap_multi_insert()
1186 * call.
1187 */
1188 if (xlrec->flags & XLH_INSERT_LAST_IN_MULTI &&
1189 (i + 1) == xlrec->ntuples)
1190 change->data.tp.clear_toast_afterwards = true;
1191 else
1192 change->data.tp.clear_toast_afterwards = false;
1193
1195 buf->origptr, change, false);
1196
1197 /* move to the next xl_multi_insert_tuple entry */
1198 data += datalen;
1199 }
1200 Assert(data == tupledata + tuplelen);
1201}
#define SHORTALIGN(LEN)
Definition c.h:834
#define XLH_INSERT_LAST_IN_MULTI
Definition heapam_xlog.h:73
#define SizeOfMultiInsertTuple
#define SizeofHeapTupleHeader
static void ItemPointerSetInvalid(ItemPointerData *pointer)
Definition itemptr.h:184
const void * data
#define InvalidOid
ItemPointerData t_self
Definition htup.h:65
uint32 t_len
Definition htup.h:64
HeapTupleHeader t_data
Definition htup.h:68
Oid t_tableOid
Definition htup.h:66

References ReorderBufferChange::action, Assert, buf, ReorderBufferChange::clear_toast_afterwards, ReorderBufferChange::data, ReplicationSlot::data, data, ReplicationSlotPersistentData::database, RelFileLocator::dbOid, fb(), FilterByOrigin(), i, InvalidOid, ItemPointerSetInvalid(), ReorderBufferChange::newtuple, ReorderBufferChange::origin_id, XLogReaderState::record, LogicalDecodingContext::reorder, REORDER_BUFFER_CHANGE_INSERT, ReorderBufferAllocChange(), ReorderBufferAllocTupleBuf(), ReorderBufferQueueChange(), ReorderBufferChange::rlocator, SHORTALIGN, SizeofHeapTupleHeader, SizeOfMultiInsertTuple, LogicalDecodingContext::slot, HeapTupleData::t_data, HeapTupleHeaderData::t_hoff, HeapTupleHeaderData::t_infomask, HeapTupleHeaderData::t_infomask2, HeapTupleData::t_len, HeapTupleData::t_self, HeapTupleData::t_tableOid, ReorderBufferChange::tp, XLH_INSERT_CONTAINS_NEW_TUPLE, XLH_INSERT_LAST_IN_MULTI, XLogRecGetBlockData(), XLogRecGetBlockTag(), XLogRecGetData, XLogRecGetOrigin, and XLogRecGetXid.

Referenced by heap2_decode().

◆ DecodePrepare()

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

Definition at line 757 of file decode.c.

759{
760 SnapBuild *builder = ctx->snapshot_builder;
761 XLogRecPtr origin_lsn = parsed->origin_lsn;
762 TimestampTz prepare_time = parsed->xact_time;
763 ReplOriginId origin_id = XLogRecGetOrigin(buf->record);
764 int i;
765 TransactionId xid = parsed->twophase_xid;
766
767 if (parsed->origin_timestamp != 0)
768 prepare_time = parsed->origin_timestamp;
769
770 /*
771 * Remember the prepare info for a txn so that it can be used later in
772 * commit prepared if required. See ReorderBufferFinishPrepared.
773 */
774 if (!ReorderBufferRememberPrepareInfo(ctx->reorder, xid, buf->origptr,
775 buf->endptr, prepare_time, origin_id,
776 origin_lsn))
777 return;
778
779 /* We can't start streaming unless a consistent state is reached. */
781 {
783 return;
784 }
785
786 /*
787 * Check whether we need to process this transaction. See
788 * DecodeTXNNeedSkip for the reasons why we sometimes want to skip the
789 * transaction.
790 *
791 * We can't call ReorderBufferForget as we did in DecodeCommit as the txn
792 * hasn't yet been committed, removing this txn before a commit might
793 * result in the computation of an incorrect restart_lsn. See
794 * SnapBuildProcessRunningXacts. But we need to process cache
795 * invalidations if there are any for the reasons mentioned in
796 * DecodeCommit.
797 */
798 if (DecodeTXNNeedSkip(ctx, buf, parsed->dbId, origin_id))
799 {
801 ReorderBufferInvalidate(ctx->reorder, xid, buf->origptr);
802 return;
803 }
804
805 /* Tell the reorderbuffer about the surviving subtransactions. */
806 for (i = 0; i < parsed->nsubxacts; i++)
807 {
808 ReorderBufferCommitChild(ctx->reorder, xid, parsed->subxacts[i],
809 buf->origptr, buf->endptr);
810 }
811
812 /* replay actions of all transaction + subtransactions in order */
813 ReorderBufferPrepare(ctx->reorder, xid, parsed->twophase_gid);
814
815 /*
816 * Update the decoding stats at transaction prepare/commit/abort.
817 * Additionally we send the stats when we spill or stream the changes to
818 * avoid losing them in case the decoding is interrupted. It is not clear
819 * that sending more or less frequently than this would be better.
820 */
822}
uint32 TransactionId
Definition c.h:678
void ReorderBufferInvalidate(ReorderBuffer *rb, TransactionId xid, XLogRecPtr lsn)
void ReorderBufferPrepare(ReorderBuffer *rb, TransactionId xid, char *gid)
void ReorderBufferSkipPrepare(ReorderBuffer *rb, TransactionId xid)
bool ReorderBufferRememberPrepareInfo(ReorderBuffer *rb, TransactionId xid, XLogRecPtr prepare_lsn, XLogRecPtr end_lsn, TimestampTz prepare_time, ReplOriginId origin_id, XLogRecPtr origin_lsn)
SnapBuildState SnapBuildCurrentState(SnapBuild *builder)
Definition snapbuild.c:277
@ SNAPBUILD_CONSISTENT
Definition snapbuild.h:50

References buf, DecodeTXNNeedSkip(), fb(), i, LogicalDecodingContext::reorder, ReorderBufferCommitChild(), ReorderBufferInvalidate(), ReorderBufferPrepare(), ReorderBufferRememberPrepareInfo(), ReorderBufferSkipPrepare(), SNAPBUILD_CONSISTENT, SnapBuildCurrentState(), LogicalDecodingContext::snapshot_builder, UpdateDecodingStats(), and XLogRecGetOrigin.

Referenced by xact_decode().

◆ DecodeSpecConfirm()

static void DecodeSpecConfirm ( LogicalDecodingContext ctx,
XLogRecordBuffer buf 
)
static

Definition at line 1210 of file decode.c.

1211{
1213 ReorderBufferChange *change;
1214 RelFileLocator target_locator;
1215
1216 /* only interested in our database */
1217 XLogRecGetBlockTag(r, 0, &target_locator, NULL, NULL);
1218 if (target_locator.dbOid != ctx->slot->data.database)
1219 return;
1220
1221 /* output plugin doesn't look for this origin, no need to queue */
1222 if (FilterByOrigin(ctx, XLogRecGetOrigin(r)))
1223 return;
1224
1225 change = ReorderBufferAllocChange(ctx->reorder);
1227 change->origin_id = XLogRecGetOrigin(r);
1228
1229 memcpy(&change->data.tp.rlocator, &target_locator, sizeof(RelFileLocator));
1230
1231 change->data.tp.clear_toast_afterwards = true;
1232
1234 change, false);
1235}
@ REORDER_BUFFER_CHANGE_INTERNAL_SPEC_CONFIRM

References ReorderBufferChange::action, buf, ReorderBufferChange::clear_toast_afterwards, ReorderBufferChange::data, ReplicationSlot::data, ReplicationSlotPersistentData::database, RelFileLocator::dbOid, fb(), FilterByOrigin(), ReorderBufferChange::origin_id, XLogReaderState::record, LogicalDecodingContext::reorder, REORDER_BUFFER_CHANGE_INTERNAL_SPEC_CONFIRM, ReorderBufferAllocChange(), ReorderBufferQueueChange(), ReorderBufferChange::rlocator, LogicalDecodingContext::slot, ReorderBufferChange::tp, XLogRecGetBlockTag(), XLogRecGetOrigin, and XLogRecGetXid.

Referenced by heap_decode().

◆ DecodeTruncate()

static void DecodeTruncate ( LogicalDecodingContext ctx,
XLogRecordBuffer buf 
)
static

Definition at line 1068 of file decode.c.

1069{
1072 ReorderBufferChange *change;
1073
1075
1076 /* only interested in our database */
1077 if (xlrec->dbId != ctx->slot->data.database)
1078 return;
1079
1080 /* output plugin doesn't look for this origin, no need to queue */
1081 if (FilterByOrigin(ctx, XLogRecGetOrigin(r)))
1082 return;
1083
1084 change = ReorderBufferAllocChange(ctx->reorder);
1086 change->origin_id = XLogRecGetOrigin(r);
1087 if (xlrec->flags & XLH_TRUNCATE_CASCADE)
1088 change->data.truncate.cascade = true;
1089 if (xlrec->flags & XLH_TRUNCATE_RESTART_SEQS)
1090 change->data.truncate.restart_seqs = true;
1091 change->data.truncate.nrelids = xlrec->nrelids;
1093 xlrec->nrelids);
1094 memcpy(change->data.truncate.relids, xlrec->relids,
1095 xlrec->nrelids * sizeof(Oid));
1097 buf->origptr, change, false);
1098}
#define XLH_TRUNCATE_RESTART_SEQS
#define XLH_TRUNCATE_CASCADE
unsigned int Oid
Oid * ReorderBufferAllocRelids(ReorderBuffer *rb, int nrelids)
@ REORDER_BUFFER_CHANGE_TRUNCATE
struct ReorderBufferChange::@117::@119 truncate

References ReorderBufferChange::action, buf, ReorderBufferChange::cascade, ReorderBufferChange::data, ReplicationSlot::data, ReplicationSlotPersistentData::database, fb(), FilterByOrigin(), ReorderBufferChange::nrelids, ReorderBufferChange::origin_id, XLogReaderState::record, ReorderBufferChange::relids, LogicalDecodingContext::reorder, REORDER_BUFFER_CHANGE_TRUNCATE, ReorderBufferAllocChange(), ReorderBufferAllocRelids(), ReorderBufferQueueChange(), ReorderBufferChange::restart_seqs, LogicalDecodingContext::slot, ReorderBufferChange::truncate, XLH_TRUNCATE_CASCADE, XLH_TRUNCATE_RESTART_SEQS, XLogRecGetData, XLogRecGetOrigin, and XLogRecGetXid.

Referenced by heap_decode().

◆ DecodeTXNNeedSkip()

static bool DecodeTXNNeedSkip ( LogicalDecodingContext ctx,
XLogRecordBuffer buf,
Oid  txn_dbid,
ReplOriginId  origin_id 
)
static

Definition at line 1290 of file decode.c.

1292{
1293 if (SnapBuildXactNeedsSkip(ctx->snapshot_builder, buf->origptr) ||
1294 (txn_dbid != InvalidOid && txn_dbid != ctx->slot->data.database) ||
1295 FilterByOrigin(ctx, origin_id))
1296 return true;
1297
1298 /*
1299 * We also skip decoding in fast_forward mode. In passing set the
1300 * processing_required flag to indicate that if it were not for
1301 * fast_forward mode, processing would have been required.
1302 */
1303 if (ctx->fast_forward)
1304 {
1305 ctx->processing_required = true;
1306 return true;
1307 }
1308
1309 return false;
1310}
bool SnapBuildXactNeedsSkip(SnapBuild *builder, XLogRecPtr ptr)
Definition snapbuild.c:304

References buf, ReplicationSlot::data, ReplicationSlotPersistentData::database, LogicalDecodingContext::fast_forward, fb(), FilterByOrigin(), InvalidOid, LogicalDecodingContext::processing_required, LogicalDecodingContext::slot, SnapBuildXactNeedsSkip(), and LogicalDecodingContext::snapshot_builder.

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

◆ DecodeUpdate()

static void DecodeUpdate ( LogicalDecodingContext ctx,
XLogRecordBuffer buf 
)
static

Definition at line 947 of file decode.c.

948{
951 ReorderBufferChange *change;
952 char *data;
953 RelFileLocator target_locator;
954
956
957 /* only interested in our database */
958 XLogRecGetBlockTag(r, 0, &target_locator, NULL, NULL);
959 if (target_locator.dbOid != ctx->slot->data.database)
960 return;
961
962 /* output plugin doesn't look for this origin, no need to queue */
963 if (FilterByOrigin(ctx, XLogRecGetOrigin(r)))
964 return;
965
966 change = ReorderBufferAllocChange(ctx->reorder);
968 change->origin_id = XLogRecGetOrigin(r);
969 memcpy(&change->data.tp.rlocator, &target_locator, sizeof(RelFileLocator));
970
972 {
973 Size datalen;
975
976 data = XLogRecGetBlockData(r, 0, &datalen);
977
978 tuplelen = datalen - SizeOfHeapHeader;
979
980 change->data.tp.newtuple =
982
983 DecodeXLogTuple(data, datalen, change->data.tp.newtuple);
984 }
985
986 if (xlrec->flags & XLH_UPDATE_CONTAINS_OLD)
987 {
988 Size datalen;
990
991 /* caution, remaining data in record is not aligned */
993 datalen = XLogRecGetDataLen(r) - SizeOfHeapUpdate;
994 tuplelen = datalen - SizeOfHeapHeader;
995
996 change->data.tp.oldtuple =
998
999 DecodeXLogTuple(data, datalen, change->data.tp.oldtuple);
1000 }
1001
1002 change->data.tp.clear_toast_afterwards = true;
1003
1005 change, false);
1006}
#define SizeOfHeapUpdate
#define XLH_UPDATE_CONTAINS_NEW_TUPLE
Definition heapam_xlog.h:90
#define XLH_UPDATE_CONTAINS_OLD
Definition heapam_xlog.h:95
@ REORDER_BUFFER_CHANGE_UPDATE

References ReorderBufferChange::action, buf, ReorderBufferChange::clear_toast_afterwards, ReorderBufferChange::data, ReplicationSlot::data, data, ReplicationSlotPersistentData::database, RelFileLocator::dbOid, DecodeXLogTuple(), fb(), FilterByOrigin(), ReorderBufferChange::newtuple, ReorderBufferChange::oldtuple, ReorderBufferChange::origin_id, XLogReaderState::record, LogicalDecodingContext::reorder, REORDER_BUFFER_CHANGE_UPDATE, ReorderBufferAllocChange(), ReorderBufferAllocTupleBuf(), ReorderBufferQueueChange(), ReorderBufferChange::rlocator, SizeOfHeapHeader, SizeOfHeapUpdate, LogicalDecodingContext::slot, ReorderBufferChange::tp, XLH_UPDATE_CONTAINS_NEW_TUPLE, XLH_UPDATE_CONTAINS_OLD, XLogRecGetBlockData(), XLogRecGetBlockTag(), XLogRecGetData, XLogRecGetDataLen, XLogRecGetOrigin, and XLogRecGetXid.

Referenced by heap_decode().

◆ DecodeXLogTuple()

static void DecodeXLogTuple ( char data,
Size  len,
HeapTuple  tuple 
)
static

Definition at line 1246 of file decode.c.

1247{
1249 int datalen = len - SizeOfHeapHeader;
1250 HeapTupleHeader header;
1251
1252 Assert(datalen >= 0);
1253
1254 tuple->t_len = datalen + SizeofHeapTupleHeader;
1255 header = tuple->t_data;
1256
1257 /* not a disk based tuple */
1259
1260 /* we can only figure this out after reassembling the transactions */
1261 tuple->t_tableOid = InvalidOid;
1262
1263 /* data is not stored aligned, copy to aligned storage */
1265
1266 memset(header, 0, SizeofHeapTupleHeader);
1267
1268 memcpy(((char *) tuple->t_data) + SizeofHeapTupleHeader,
1270 datalen);
1271
1272 header->t_infomask = xlhdr.t_infomask;
1273 header->t_infomask2 = xlhdr.t_infomask2;
1274 header->t_hoff = xlhdr.t_hoff;
1275}
const void size_t len

References Assert, data, fb(), InvalidOid, ItemPointerSetInvalid(), len, SizeOfHeapHeader, SizeofHeapTupleHeader, HeapTupleData::t_data, HeapTupleHeaderData::t_hoff, HeapTupleHeaderData::t_infomask, HeapTupleHeaderData::t_infomask2, HeapTupleData::t_len, HeapTupleData::t_self, and HeapTupleData::t_tableOid.

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

◆ FilterByOrigin()

static bool FilterByOrigin ( LogicalDecodingContext ctx,
ReplOriginId  origin_id 
)
inlinestatic

Definition at line 568 of file decode.c.

569{
571 return false;
572
573 return filter_by_origin_cb_wrapper(ctx, origin_id);
574}
bool filter_by_origin_cb_wrapper(LogicalDecodingContext *ctx, ReplOriginId origin_id)
Definition logical.c:1190
OutputPluginCallbacks callbacks
Definition logical.h:53
LogicalDecodeFilterByOriginCB filter_by_origin_cb

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

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

◆ FilterPrepare()

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

Definition at line 545 of file decode.c.

547{
548 /*
549 * Skip if decoding of two-phase transactions at PREPARE time is not
550 * enabled. In that case, all two-phase transactions are considered
551 * filtered out and will be applied as regular transactions at COMMIT
552 * PREPARED.
553 */
554 if (!ctx->twophase)
555 return true;
556
557 /*
558 * The filter_prepare callback is optional. When not supplied, all
559 * prepared transactions should go through.
560 */
561 if (ctx->callbacks.filter_prepare_cb == NULL)
562 return false;
563
564 return filter_prepare_cb_wrapper(ctx, xid, gid);
565}
bool filter_prepare_cb_wrapper(LogicalDecodingContext *ctx, TransactionId xid, const char *gid)
Definition logical.c:1158
LogicalDecodeFilterPrepareCB filter_prepare_cb

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

Referenced by xact_decode().

◆ heap2_decode()

void heap2_decode ( LogicalDecodingContext ctx,
XLogRecordBuffer buf 
)

Definition at line 400 of file decode.c.

401{
402 uint8 info = XLogRecGetInfo(buf->record) & XLOG_HEAP_OPMASK;
403 TransactionId xid = XLogRecGetXid(buf->record);
404 SnapBuild *builder = ctx->snapshot_builder;
405
406 ReorderBufferProcessXid(ctx->reorder, xid, buf->origptr);
407
408 /*
409 * If we don't have snapshot or we are just fast-forwarding, there is no
410 * point in decoding data changes. However, it's crucial to build the base
411 * snapshot during fast-forward mode (as is done in
412 * SnapBuildProcessChange()) because we require the snapshot's xmin when
413 * determining the candidate catalog_xmin for the replication slot. See
414 * SnapBuildProcessRunningXacts().
415 */
417 return;
418
419 switch (info)
420 {
422 if (SnapBuildProcessChange(builder, xid, buf->origptr) &&
423 !ctx->fast_forward)
425 break;
427 if (!ctx->fast_forward)
428 {
430
432 SnapBuildProcessNewCid(builder, xid, buf->origptr, xlrec);
433 }
434 break;
436
437 /*
438 * Although these records only exist to serve the needs of logical
439 * decoding, all the work happens as part of crash or archive
440 * recovery, so we don't need to do anything here.
441 */
442 break;
443
444 /*
445 * Everything else here is just low level physical stuff we're not
446 * interested in.
447 */
453 break;
454 default:
455 elog(ERROR, "unexpected RM_HEAP2_ID record type: %u", info);
456 }
457}
uint8_t uint8
Definition c.h:556
static void DecodeMultiInsert(LogicalDecodingContext *ctx, XLogRecordBuffer *buf)
Definition decode.c:1106
#define ERROR
Definition elog.h:39
#define elog(elevel,...)
Definition elog.h:226
#define XLOG_HEAP2_MULTI_INSERT
Definition heapam_xlog.h:64
#define XLOG_HEAP2_REWRITE
Definition heapam_xlog.h:59
#define XLOG_HEAP_OPMASK
Definition heapam_xlog.h:42
#define XLOG_HEAP2_PRUNE_VACUUM_SCAN
Definition heapam_xlog.h:61
#define XLOG_HEAP2_LOCK_UPDATED
Definition heapam_xlog.h:65
#define XLOG_HEAP2_PRUNE_ON_ACCESS
Definition heapam_xlog.h:60
#define XLOG_HEAP2_NEW_CID
Definition heapam_xlog.h:66
#define XLOG_HEAP2_PRUNE_VACUUM_CLEANUP
Definition heapam_xlog.h:62
#define XLOG_HEAP2_VISIBLE
Definition heapam_xlog.h:63
void ReorderBufferProcessXid(ReorderBuffer *rb, TransactionId xid, XLogRecPtr lsn)
bool SnapBuildProcessChange(SnapBuild *builder, TransactionId xid, XLogRecPtr lsn)
Definition snapbuild.c:638
void SnapBuildProcessNewCid(SnapBuild *builder, TransactionId xid, XLogRecPtr lsn, xl_heap_new_cid *xlrec)
Definition snapbuild.c:688
@ SNAPBUILD_FULL_SNAPSHOT
Definition snapbuild.h:43
#define XLogRecGetInfo(decoder)
Definition xlogreader.h:409

References buf, DecodeMultiInsert(), elog, ERROR, LogicalDecodingContext::fast_forward, fb(), LogicalDecodingContext::reorder, ReorderBufferProcessXid(), SNAPBUILD_FULL_SNAPSHOT, SnapBuildCurrentState(), SnapBuildProcessChange(), SnapBuildProcessNewCid(), LogicalDecodingContext::snapshot_builder, XLOG_HEAP2_LOCK_UPDATED, XLOG_HEAP2_MULTI_INSERT, XLOG_HEAP2_NEW_CID, XLOG_HEAP2_PRUNE_ON_ACCESS, XLOG_HEAP2_PRUNE_VACUUM_CLEANUP, XLOG_HEAP2_PRUNE_VACUUM_SCAN, XLOG_HEAP2_REWRITE, XLOG_HEAP2_VISIBLE, XLOG_HEAP_OPMASK, XLogRecGetData, XLogRecGetInfo, and XLogRecGetXid.

◆ heap_decode()

void heap_decode ( LogicalDecodingContext ctx,
XLogRecordBuffer buf 
)

Definition at line 463 of file decode.c.

464{
465 uint8 info = XLogRecGetInfo(buf->record) & XLOG_HEAP_OPMASK;
466 TransactionId xid = XLogRecGetXid(buf->record);
467 SnapBuild *builder = ctx->snapshot_builder;
468
469 ReorderBufferProcessXid(ctx->reorder, xid, buf->origptr);
470
471 /*
472 * If we don't have snapshot or we are just fast-forwarding, there is no
473 * point in decoding data changes. However, it's crucial to build the base
474 * snapshot during fast-forward mode (as is done in
475 * SnapBuildProcessChange()) because we require the snapshot's xmin when
476 * determining the candidate catalog_xmin for the replication slot. See
477 * SnapBuildProcessRunningXacts().
478 */
480 return;
481
482 switch (info)
483 {
484 case XLOG_HEAP_INSERT:
485 if (SnapBuildProcessChange(builder, xid, buf->origptr) &&
486 !ctx->fast_forward)
487 DecodeInsert(ctx, buf);
488 break;
489
490 /*
491 * Treat HOT update as normal updates. There is no useful
492 * information in the fact that we could make it a HOT update
493 * locally and the WAL layout is compatible.
494 */
496 case XLOG_HEAP_UPDATE:
497 if (SnapBuildProcessChange(builder, xid, buf->origptr) &&
498 !ctx->fast_forward)
499 DecodeUpdate(ctx, buf);
500 break;
501
502 case XLOG_HEAP_DELETE:
503 if (SnapBuildProcessChange(builder, xid, buf->origptr) &&
504 !ctx->fast_forward)
505 DecodeDelete(ctx, buf);
506 break;
507
509 if (SnapBuildProcessChange(builder, xid, buf->origptr) &&
510 !ctx->fast_forward)
511 DecodeTruncate(ctx, buf);
512 break;
513
515
516 /*
517 * Inplace updates are only ever performed on catalog tuples and
518 * can, per definition, not change tuple visibility. Since we
519 * also don't decode catalog tuples, we're not interested in the
520 * record's contents.
521 */
522 break;
523
525 if (SnapBuildProcessChange(builder, xid, buf->origptr) &&
526 !ctx->fast_forward)
528 break;
529
530 case XLOG_HEAP_LOCK:
531 /* we don't care about row level locks for now */
532 break;
533
534 default:
535 elog(ERROR, "unexpected RM_HEAP_ID record type: %u", info);
536 break;
537 }
538}
static void DecodeDelete(LogicalDecodingContext *ctx, XLogRecordBuffer *buf)
Definition decode.c:1014
static void DecodeInsert(LogicalDecodingContext *ctx, XLogRecordBuffer *buf)
Definition decode.c:888
static void DecodeTruncate(LogicalDecodingContext *ctx, XLogRecordBuffer *buf)
Definition decode.c:1068
static void DecodeUpdate(LogicalDecodingContext *ctx, XLogRecordBuffer *buf)
Definition decode.c:947
static void DecodeSpecConfirm(LogicalDecodingContext *ctx, XLogRecordBuffer *buf)
Definition decode.c:1210
#define XLOG_HEAP_HOT_UPDATE
Definition heapam_xlog.h:37
#define XLOG_HEAP_DELETE
Definition heapam_xlog.h:34
#define XLOG_HEAP_TRUNCATE
Definition heapam_xlog.h:36
#define XLOG_HEAP_UPDATE
Definition heapam_xlog.h:35
#define XLOG_HEAP_INPLACE
Definition heapam_xlog.h:40
#define XLOG_HEAP_LOCK
Definition heapam_xlog.h:39
#define XLOG_HEAP_INSERT
Definition heapam_xlog.h:33
#define XLOG_HEAP_CONFIRM
Definition heapam_xlog.h:38

References buf, DecodeDelete(), DecodeInsert(), DecodeSpecConfirm(), DecodeTruncate(), DecodeUpdate(), elog, ERROR, LogicalDecodingContext::fast_forward, LogicalDecodingContext::reorder, ReorderBufferProcessXid(), SNAPBUILD_FULL_SNAPSHOT, SnapBuildCurrentState(), SnapBuildProcessChange(), LogicalDecodingContext::snapshot_builder, 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.

◆ LogicalDecodingProcessRecord()

void LogicalDecodingProcessRecord ( LogicalDecodingContext ctx,
XLogReaderState record 
)

Definition at line 88 of file decode.c.

89{
93
94 buf.origptr = ctx->reader->ReadRecPtr;
95 buf.endptr = ctx->reader->EndRecPtr;
96 buf.record = record;
97
98 txid = XLogRecGetTopXid(record);
99
100 /*
101 * If the top-level xid is valid, we need to assign the subxact to the
102 * top-level xact. We need to do this for all records, hence we do it
103 * before the switch.
104 */
106 {
108 txid,
109 XLogRecGetXid(record),
110 buf.origptr);
111 }
112
113 rmgr = GetRmgr(XLogRecGetRmid(record));
114
115 if (rmgr.rm_decode != NULL)
116 rmgr.rm_decode(ctx, &buf);
117 else
118 {
119 /* just deal with xid, and done */
121 buf.origptr);
122 }
123}
void ReorderBufferAssignChild(ReorderBuffer *rb, TransactionId xid, TransactionId subxid, XLogRecPtr lsn)
XLogReaderState * reader
Definition logical.h:42
XLogRecPtr EndRecPtr
Definition xlogreader.h:206
XLogRecPtr ReadRecPtr
Definition xlogreader.h:205
#define TransactionIdIsValid(xid)
Definition transam.h:41
static RmgrData GetRmgr(RmgrId rmid)
#define XLogRecGetRmid(decoder)
Definition xlogreader.h:410
#define XLogRecGetTopXid(decoder)
Definition xlogreader.h:413

References buf, XLogReaderState::EndRecPtr, fb(), GetRmgr(), LogicalDecodingContext::reader, XLogReaderState::ReadRecPtr, LogicalDecodingContext::reorder, ReorderBufferAssignChild(), ReorderBufferProcessXid(), TransactionIdIsValid, XLogRecGetRmid, XLogRecGetTopXid, and XLogRecGetXid.

Referenced by DecodingContextFindStartpoint(), LogicalReplicationSlotCheckPendingWal(), LogicalSlotAdvanceAndCheckSnapState(), pg_logical_slot_get_changes_guts(), and XLogSendLogical().

◆ logicalmsg_decode()

void logicalmsg_decode ( LogicalDecodingContext ctx,
XLogRecordBuffer buf 
)

Definition at line 580 of file decode.c.

581{
582 SnapBuild *builder = ctx->snapshot_builder;
586 ReplOriginId origin_id = XLogRecGetOrigin(r);
587 Snapshot snapshot = NULL;
588 xl_logical_message *message;
589
590 if (info != XLOG_LOGICAL_MESSAGE)
591 elog(ERROR, "unexpected RM_LOGICALMSG_ID record type: %u", info);
592
594
595 /* If we don't have snapshot, there is no point in decoding messages */
597 return;
598
599 message = (xl_logical_message *) XLogRecGetData(r);
600
601 if (message->dbId != ctx->slot->data.database ||
602 FilterByOrigin(ctx, origin_id))
603 return;
604
605 if (message->transactional &&
606 !SnapBuildProcessChange(builder, xid, buf->origptr))
607 return;
608 else if (!message->transactional &&
610 SnapBuildXactNeedsSkip(builder, buf->origptr)))
611 return;
612
613 /*
614 * We also skip decoding in fast_forward mode. This check must be last
615 * because we don't want to set the processing_required flag unless we
616 * have a decodable message.
617 */
618 if (ctx->fast_forward)
619 {
620 /*
621 * We need to set processing_required flag to notify the message's
622 * existence to the caller. Usually, the flag is set when either the
623 * COMMIT or ABORT records are decoded, but this must be turned on
624 * here because the non-transactional logical message is decoded
625 * without waiting for these records.
626 */
627 if (!message->transactional)
628 ctx->processing_required = true;
629
630 return;
631 }
632
633 /*
634 * If this is a non-transactional change, get the snapshot we're expected
635 * to use. We only get here when the snapshot is consistent, and the
636 * change is not meant to be skipped.
637 *
638 * For transactional changes we don't need a snapshot, we'll use the
639 * regular snapshot maintained by ReorderBuffer. We just leave it NULL.
640 */
641 if (!message->transactional)
642 snapshot = SnapBuildGetOrBuildSnapshot(builder);
643
644 ReorderBufferQueueMessage(ctx->reorder, xid, snapshot, buf->endptr,
645 message->transactional,
646 message->message, /* first part of message is
647 * prefix */
648 message->message_size,
649 message->message + message->prefix_size);
650}
#define XLOG_LOGICAL_MESSAGE
Definition message.h:37
void ReorderBufferQueueMessage(ReorderBuffer *rb, TransactionId xid, Snapshot snap, XLogRecPtr lsn, bool transactional, const char *prefix, Size message_size, const char *message)
Snapshot SnapBuildGetOrBuildSnapshot(SnapBuild *builder)
Definition snapbuild.c:578
char message[FLEXIBLE_ARRAY_MEMBER]
Definition message.h:27

References buf, ReplicationSlot::data, ReplicationSlotPersistentData::database, xl_logical_message::dbId, elog, ERROR, LogicalDecodingContext::fast_forward, fb(), FilterByOrigin(), xl_logical_message::message, xl_logical_message::message_size, xl_logical_message::prefix_size, LogicalDecodingContext::processing_required, XLogReaderState::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, and XLogRecGetXid.

◆ standby_decode()

void standby_decode ( LogicalDecodingContext ctx,
XLogRecordBuffer buf 
)

Definition at line 354 of file decode.c.

355{
356 SnapBuild *builder = ctx->snapshot_builder;
359
361
362 switch (info)
363 {
365 {
367
368 SnapBuildProcessRunningXacts(builder, buf->origptr, running);
369
370 /*
371 * Abort all transactions that we keep track of, that are
372 * older than the record's oldestRunningXid. This is the most
373 * convenient spot for doing so since, in contrast to shutdown
374 * or end-of-recovery checkpoints, we have information about
375 * all running transactions which includes prepared ones,
376 * while shutdown checkpoints just know that no non-prepared
377 * transactions are in progress.
378 */
380 }
381 break;
383 break;
385
386 /*
387 * We are processing the invalidations at the command level via
388 * XLOG_XACT_INVALIDATIONS. So we don't need to do anything here.
389 */
390 break;
391 default:
392 elog(ERROR, "unexpected RM_STANDBY_ID record type: %u", info);
393 }
394}
void ReorderBufferAbortOld(ReorderBuffer *rb, TransactionId oldestRunningXid)
void SnapBuildProcessRunningXacts(SnapBuild *builder, XLogRecPtr lsn, xl_running_xacts *running)
Definition snapbuild.c:1135
#define XLOG_INVALIDATIONS
Definition standbydefs.h:36
#define XLOG_STANDBY_LOCK
Definition standbydefs.h:34
#define XLOG_RUNNING_XACTS
Definition standbydefs.h:35
TransactionId oldestRunningXid
Definition standbydefs.h:53

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

◆ xact_decode()

void xact_decode ( LogicalDecodingContext ctx,
XLogRecordBuffer buf 
)

Definition at line 196 of file decode.c.

197{
198 SnapBuild *builder = ctx->snapshot_builder;
199 ReorderBuffer *reorder = ctx->reorder;
202
203 /*
204 * If the snapshot isn't yet fully built, we cannot decode anything, so
205 * bail out.
206 */
208 return;
209
210 switch (info)
211 {
212 case XLOG_XACT_COMMIT:
214 {
217 TransactionId xid;
218 bool two_phase = false;
219
222
223 if (!TransactionIdIsValid(parsed.twophase_xid))
224 xid = XLogRecGetXid(r);
225 else
226 xid = parsed.twophase_xid;
227
228 /*
229 * We would like to process the transaction in a two-phase
230 * manner iff output plugin supports two-phase commits and
231 * doesn't filter the transaction at prepare time.
232 */
233 if (info == XLOG_XACT_COMMIT_PREPARED)
234 two_phase = !(FilterPrepare(ctx, xid,
235 parsed.twophase_gid));
236
237 DecodeCommit(ctx, buf, &parsed, xid, two_phase);
238 break;
239 }
240 case XLOG_XACT_ABORT:
242 {
245 TransactionId xid;
246 bool two_phase = false;
247
250
251 if (!TransactionIdIsValid(parsed.twophase_xid))
252 xid = XLogRecGetXid(r);
253 else
254 xid = parsed.twophase_xid;
255
256 /*
257 * We would like to process the transaction in a two-phase
258 * manner iff output plugin supports two-phase commits and
259 * doesn't filter the transaction at prepare time.
260 */
261 if (info == XLOG_XACT_ABORT_PREPARED)
262 two_phase = !(FilterPrepare(ctx, xid,
263 parsed.twophase_gid));
264
265 DecodeAbort(ctx, buf, &parsed, xid, two_phase);
266 break;
267 }
269
270 /*
271 * We assign subxact to the toplevel xact while processing each
272 * record if required. So, we don't need to do anything here. See
273 * LogicalDecodingProcessRecord.
274 */
275 break;
277 {
278 TransactionId xid;
280
281 xid = XLogRecGetXid(r);
283
284 /*
285 * Execute the invalidations for xid-less transactions,
286 * otherwise, accumulate them so that they can be processed at
287 * the commit time.
288 */
289 if (TransactionIdIsValid(xid))
290 {
291 if (!ctx->fast_forward)
293 buf->origptr,
294 invals->nmsgs,
295 invals->msgs);
297 buf->origptr);
298 }
299 else if (!ctx->fast_forward)
301 invals->nmsgs,
302 invals->msgs);
303
304 break;
305 }
307 {
310
311 /* ok, parse it */
314 xlrec, &parsed);
315
316 /*
317 * We would like to process the transaction in a two-phase
318 * manner iff output plugin supports two-phase commits and
319 * doesn't filter the transaction at prepare time.
320 */
321 if (FilterPrepare(ctx, parsed.twophase_xid,
322 parsed.twophase_gid))
323 {
324 ReorderBufferProcessXid(reorder, parsed.twophase_xid,
325 buf->origptr);
326 break;
327 }
328
329 /*
330 * Note that if the prepared transaction has locked [user]
331 * catalog tables exclusively then decoding prepare can block
332 * till the main transaction is committed because it needs to
333 * lock the catalog tables.
334 *
335 * XXX Now, this can even lead to a deadlock if the prepare
336 * transaction is waiting to get it logically replicated for
337 * distributed 2PC. This can be avoided by disallowing
338 * preparing transactions that have locked [user] catalog
339 * tables exclusively but as of now, we ask users not to do
340 * such an operation.
341 */
342 DecodePrepare(ctx, buf, &parsed);
343 break;
344 }
345 default:
346 elog(ERROR, "unexpected RM_XACT_ID record type: %u", info);
347 }
348}
static void DecodeAbort(LogicalDecodingContext *ctx, XLogRecordBuffer *buf, xl_xact_parsed_abort *parsed, TransactionId xid, bool two_phase)
Definition decode.c:832
static bool FilterPrepare(LogicalDecodingContext *ctx, TransactionId xid, const char *gid)
Definition decode.c:545
static void DecodeCommit(LogicalDecodingContext *ctx, XLogRecordBuffer *buf, xl_xact_parsed_commit *parsed, TransactionId xid, bool two_phase)
Definition decode.c:661
static void DecodePrepare(LogicalDecodingContext *ctx, XLogRecordBuffer *buf, xl_xact_parsed_prepare *parsed)
Definition decode.c:757
void ReorderBufferXidSetCatalogChanges(ReorderBuffer *rb, TransactionId xid, XLogRecPtr lsn)
void ReorderBufferAddInvalidations(ReorderBuffer *rb, TransactionId xid, XLogRecPtr lsn, Size nmsgs, SharedInvalidationMessage *msgs)
void ReorderBufferImmediateInvalidation(ReorderBuffer *rb, uint32 ninvalidations, SharedInvalidationMessage *invalidations)
#define XLOG_XACT_COMMIT_PREPARED
Definition xact.h:173
#define XLOG_XACT_INVALIDATIONS
Definition xact.h:176
#define XLOG_XACT_PREPARE
Definition xact.h:171
#define XLOG_XACT_COMMIT
Definition xact.h:170
#define XLOG_XACT_OPMASK
Definition xact.h:180
#define XLOG_XACT_ABORT
Definition xact.h:172
#define XLOG_XACT_ASSIGNMENT
Definition xact.h:175
#define XLOG_XACT_ABORT_PREPARED
Definition xact.h:174
void ParseCommitRecord(uint8 info, xl_xact_commit *xlrec, xl_xact_parsed_commit *parsed)
Definition xactdesc.c:35
void ParseAbortRecord(uint8 info, xl_xact_abort *xlrec, xl_xact_parsed_abort *parsed)
Definition xactdesc.c:141
void ParsePrepareRecord(uint8 info, xl_xact_prepare *xlrec, xl_xact_parsed_prepare *parsed)
Definition xactdesc.c:239

References buf, DecodeAbort(), DecodeCommit(), DecodePrepare(), elog, ERROR, LogicalDecodingContext::fast_forward, fb(), FilterPrepare(), ParseAbortRecord(), ParseCommitRecord(), ParsePrepareRecord(), XLogReaderState::record, LogicalDecodingContext::reorder, ReorderBufferAddInvalidations(), ReorderBufferImmediateInvalidation(), ReorderBufferProcessXid(), ReorderBufferXidSetCatalogChanges(), SNAPBUILD_FULL_SNAPSHOT, SnapBuildCurrentState(), LogicalDecodingContext::snapshot_builder, TransactionIdIsValid, two_phase, 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.

◆ xlog_decode()

void xlog_decode ( LogicalDecodingContext ctx,
XLogRecordBuffer buf 
)

Definition at line 129 of file decode.c.

130{
131 SnapBuild *builder = ctx->snapshot_builder;
132 uint8 info = XLogRecGetInfo(buf->record) & ~XLR_INFO_MASK;
133
135 buf->origptr);
136
137 switch (info)
138 {
139 /* this is also used in END_OF_RECOVERY checkpoints */
142 SnapBuildSerializationPoint(builder, buf->origptr);
143
144 break;
146
147 /*
148 * a RUNNING_XACTS record will have been logged near to this, we
149 * can restart from there.
150 */
151 break;
153 {
154 bool logical_decoding;
155
156 memcpy(&logical_decoding, XLogRecGetData(buf->record), sizeof(bool));
157
158 /*
159 * Error out as we should not decode this WAL record.
160 *
161 * Logical decoding is disabled, and existing logical slots on
162 * the standby are invalidated when this WAL record is
163 * replayed. No logical decoder can process this WAL record
164 * until replay completes, and by then the slots are already
165 * invalidated. Furthermore, no new logical slots can be
166 * created while logical decoding is disabled. This cannot
167 * occur even on primary either, since it will not restart
168 * with wal_level < replica if any logical slots exist.
169 */
170 elog(ERROR, "unexpected logical decoding status change %d",
172
173 break;
174 }
175 case XLOG_NOOP:
176 case XLOG_NEXTOID:
177 case XLOG_SWITCH:
178 case XLOG_BACKUP_END:
181 case XLOG_FPW_CHANGE:
183 case XLOG_FPI:
186 break;
187 default:
188 elog(ERROR, "unexpected RM_XLOG_ID record type: %u", info);
189 }
190}
#define XLOG_RESTORE_POINT
Definition pg_control.h:76
#define XLOG_FPW_CHANGE
Definition pg_control.h:77
#define XLOG_CHECKPOINT_REDO
Definition pg_control.h:83
#define XLOG_OVERWRITE_CONTRECORD
Definition pg_control.h:82
#define XLOG_FPI
Definition pg_control.h:80
#define XLOG_FPI_FOR_HINT
Definition pg_control.h:79
#define XLOG_NEXTOID
Definition pg_control.h:72
#define XLOG_NOOP
Definition pg_control.h:71
#define XLOG_CHECKPOINT_SHUTDOWN
Definition pg_control.h:69
#define XLOG_SWITCH
Definition pg_control.h:73
#define XLOG_BACKUP_END
Definition pg_control.h:74
#define XLOG_PARAMETER_CHANGE
Definition pg_control.h:75
#define XLOG_LOGICAL_DECODING_STATUS_CHANGE
Definition pg_control.h:84
#define XLOG_CHECKPOINT_ONLINE
Definition pg_control.h:70
#define XLOG_END_OF_RECOVERY
Definition pg_control.h:78
void SnapBuildSerializationPoint(SnapBuild *builder, XLogRecPtr lsn)
Definition snapbuild.c:1483

References buf, elog, ERROR, fb(), LogicalDecodingContext::reorder, ReorderBufferProcessXid(), SnapBuildSerializationPoint(), LogicalDecodingContext::snapshot_builder, XLOG_BACKUP_END, XLOG_CHECKPOINT_ONLINE, XLOG_CHECKPOINT_REDO, XLOG_CHECKPOINT_SHUTDOWN, XLOG_END_OF_RECOVERY, XLOG_FPI, XLOG_FPI_FOR_HINT, XLOG_FPW_CHANGE, XLOG_LOGICAL_DECODING_STATUS_CHANGE, XLOG_NEXTOID, XLOG_NOOP, XLOG_OVERWRITE_CONTRECORD, XLOG_PARAMETER_CHANGE, XLOG_RESTORE_POINT, XLOG_SWITCH, XLogRecGetData, XLogRecGetInfo, and XLogRecGetXid.