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 xlog2_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 867 of file decode.c.

870{
871 int i;
872 XLogRecPtr origin_lsn = InvalidXLogRecPtr;
873 TimestampTz abort_time = parsed->xact_time;
874 ReplOriginId origin_id = XLogRecGetOrigin(buf->record);
875 bool skip_xact;
876
877 if (parsed->xinfo & XACT_XINFO_HAS_ORIGIN)
878 {
879 origin_lsn = parsed->origin_lsn;
880 abort_time = parsed->origin_timestamp;
881 }
882
883 /*
884 * Check whether we need to process this transaction. See
885 * DecodeTXNNeedSkip for the reasons why we sometimes want to skip the
886 * transaction.
887 */
888 skip_xact = DecodeTXNNeedSkip(ctx, buf, parsed->dbId, origin_id);
889
890 /*
891 * Send the final rollback record for a prepared transaction unless we
892 * need to skip it. For non-two-phase xacts, simply forget the xact.
893 */
894 if (two_phase && !skip_xact)
895 {
896 ReorderBufferFinishPrepared(ctx->reorder, xid, buf->origptr, buf->endptr,
898 abort_time, origin_id, origin_lsn,
899 parsed->twophase_gid, false);
900 }
901 else
902 {
903 for (i = 0; i < parsed->nsubxacts; i++)
904 {
905 ReorderBufferAbort(ctx->reorder, parsed->subxacts[i],
906 buf->record->EndRecPtr, abort_time);
907 }
908
909 ReorderBufferAbort(ctx->reorder, xid, buf->record->EndRecPtr,
910 abort_time);
911 }
912
913 /* update the decoding stats */
915}
int64 TimestampTz
Definition timestamp.h:39
static bool DecodeTXNNeedSkip(LogicalDecodingContext *ctx, XLogRecordBuffer *buf, Oid txn_dbid, ReplOriginId origin_id)
Definition decode.c:1334
int i
Definition isn.c:77
void UpdateDecodingStats(LogicalDecodingContext *ctx)
Definition logical.c:1948
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:413

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 696 of file decode.c.

699{
700 XLogRecPtr origin_lsn = InvalidXLogRecPtr;
701 TimestampTz commit_time = parsed->xact_time;
702 ReplOriginId origin_id = XLogRecGetOrigin(buf->record);
703 int i;
704
705 if (parsed->xinfo & XACT_XINFO_HAS_ORIGIN)
706 {
707 origin_lsn = parsed->origin_lsn;
708 commit_time = parsed->origin_timestamp;
709 }
710
711 SnapBuildCommitTxn(ctx->snapshot_builder, buf->origptr, xid,
712 parsed->nsubxacts, parsed->subxacts,
713 parsed->xinfo);
714
715 /* ----
716 * Check whether we are interested in this specific transaction, and tell
717 * the reorderbuffer to forget the content of the (sub-)transactions
718 * if not.
719 *
720 * We can't just use ReorderBufferAbort() here, because we need to execute
721 * the transaction's invalidations. This currently won't be needed if
722 * we're just skipping over the transaction because currently we only do
723 * so during startup, to get to the first transaction the client needs. As
724 * we have reset the catalog caches before starting to read WAL, and we
725 * haven't yet touched any catalogs, there can't be anything to invalidate.
726 * But if we're "forgetting" this commit because it happened in another
727 * database, the invalidations might be important, because they could be
728 * for shared catalogs and we might have loaded data into the relevant
729 * syscaches.
730 * ---
731 */
732 if (DecodeTXNNeedSkip(ctx, buf, parsed->dbId, origin_id))
733 {
734 for (i = 0; i < parsed->nsubxacts; i++)
735 {
736 ReorderBufferForget(ctx->reorder, parsed->subxacts[i], buf->origptr);
737 }
738 ReorderBufferForget(ctx->reorder, xid, buf->origptr);
739
740 return;
741 }
742
743 /* tell the reorderbuffer about the surviving subtransactions */
744 for (i = 0; i < parsed->nsubxacts; i++)
745 {
746 ReorderBufferCommitChild(ctx->reorder, xid, parsed->subxacts[i],
747 buf->origptr, buf->endptr);
748 }
749
750 /*
751 * Send the final commit record if the transaction data is already
752 * decoded, otherwise, process the entire transaction.
753 */
754 if (two_phase)
755 {
756 ReorderBufferFinishPrepared(ctx->reorder, xid, buf->origptr, buf->endptr,
758 commit_time, origin_id, origin_lsn,
759 parsed->twophase_gid, true);
760 }
761 else
762 {
763 ReorderBufferCommit(ctx->reorder, xid, buf->origptr, buf->endptr,
764 commit_time, origin_id, origin_lsn);
765 }
766
767 /*
768 * Update the decoding stats at transaction prepare/commit/abort.
769 * Additionally we send the stats when we spill or stream the changes to
770 * avoid losing them in case the decoding is interrupted. It is not clear
771 * that sending more or less frequently than this would be better.
772 */
774}
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:304
void SnapBuildCommitTxn(SnapBuild *builder, XLogRecPtr lsn, TransactionId xid, int nsubxacts, TransactionId *subxacts, uint32 xinfo)
Definition snapbuild.c:958
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 1049 of file decode.c.

1050{
1053 ReorderBufferChange *change;
1054 RelFileLocator target_locator;
1055
1057
1058 /*
1059 * Skip changes that were marked as ignorable at origin.
1060 *
1061 * (This is used for changes that affect relations not visible to other
1062 * transactions, such as the transient table during concurrent repack.)
1063 */
1064 if (xlrec->flags & XLH_DELETE_NO_LOGICAL)
1065 return;
1066
1067 /* only interested in our database */
1068 XLogRecGetBlockTag(r, 0, &target_locator, NULL, NULL);
1069 if (target_locator.dbOid != ctx->slot->data.database)
1070 return;
1071
1072 /* output plugin doesn't look for this origin, no need to queue */
1073 if (FilterByOrigin(ctx, XLogRecGetOrigin(r)))
1074 return;
1075
1076 change = ReorderBufferAllocChange(ctx->reorder);
1077
1078 if (xlrec->flags & XLH_DELETE_IS_SUPER)
1080 else
1082
1083 change->origin_id = XLogRecGetOrigin(r);
1084
1085 memcpy(&change->data.tp.rlocator, &target_locator, sizeof(RelFileLocator));
1086
1087 /* old primary key stored */
1088 if (xlrec->flags & XLH_DELETE_CONTAINS_OLD)
1089 {
1091 Size tuplelen = datalen - SizeOfHeapHeader;
1092
1094
1095 change->data.tp.oldtuple =
1097
1099 datalen, change->data.tp.oldtuple);
1100 }
1101
1102 change->data.tp.clear_toast_afterwards = true;
1103
1105 change, false);
1106}
#define Assert(condition)
Definition c.h:943
size_t Size
Definition c.h:689
memcpy(sums, checksumBaseOffsets, sizeof(checksumBaseOffsets))
static bool FilterByOrigin(LogicalDecodingContext *ctx, ReplOriginId origin_id)
Definition decode.c:603
static void DecodeXLogTuple(char *data, Size len, HeapTuple tuple)
Definition decode.c:1290
#define XLH_DELETE_CONTAINS_OLD
#define SizeOfHeapHeader
#define SizeOfHeapDelete
#define XLH_DELETE_IS_SUPER
#define XLH_DELETE_NO_LOGICAL
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
ReplOriginId origin_id
struct ReorderBufferChange::@118::@119 tp
union ReorderBufferChange::@118 data
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:416
#define XLogRecGetData(decoder)
Definition xlogreader.h:415
#define XLogRecGetXid(decoder)
Definition xlogreader.h:412

References ReorderBufferChange::action, Assert, buf, ReorderBufferChange::clear_toast_afterwards, ReorderBufferChange::data, ReplicationSlot::data, ReplicationSlotPersistentData::database, RelFileLocator::dbOid, DecodeXLogTuple(), fb(), FilterByOrigin(), memcpy(), 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, XLH_DELETE_NO_LOGICAL, XLogRecGetBlockTag(), XLogRecGetData, XLogRecGetDataLen, XLogRecGetOrigin, and XLogRecGetXid.

Referenced by heap_decode().

◆ DecodeInsert()

static void DecodeInsert ( LogicalDecodingContext ctx,
XLogRecordBuffer buf 
)
static

Definition at line 923 of file decode.c.

924{
925 Size datalen;
926 char *tupledata;
930 ReorderBufferChange *change;
931 RelFileLocator target_locator;
932
934
935 /*
936 * Ignore insert records without new tuples (this does happen when
937 * raw_heap_insert marks the TOAST record as HEAP_INSERT_NO_LOGICAL).
938 */
939 if (!(xlrec->flags & XLH_INSERT_CONTAINS_NEW_TUPLE))
940 return;
941
942 /* only interested in our database */
943 XLogRecGetBlockTag(r, 0, &target_locator, NULL, NULL);
944 if (target_locator.dbOid != ctx->slot->data.database)
945 return;
946
947 /* output plugin doesn't look for this origin, no need to queue */
948 if (FilterByOrigin(ctx, XLogRecGetOrigin(r)))
949 return;
950
951 change = ReorderBufferAllocChange(ctx->reorder);
952 if (!(xlrec->flags & XLH_INSERT_IS_SPECULATIVE))
954 else
956 change->origin_id = XLogRecGetOrigin(r);
957
958 memcpy(&change->data.tp.rlocator, &target_locator, sizeof(RelFileLocator));
959
960 tupledata = XLogRecGetBlockData(r, 0, &datalen);
961 tuplelen = datalen - SizeOfHeapHeader;
962
963 change->data.tp.newtuple =
965
966 DecodeXLogTuple(tupledata, datalen, change->data.tp.newtuple);
967
968 change->data.tp.clear_toast_afterwards = true;
969
971 change,
973}
#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(), memcpy(), 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 1150 of file decode.c.

1151{
1154 int i;
1155 char *data;
1156 char *tupledata;
1157 Size tuplelen;
1158 RelFileLocator rlocator;
1159
1161
1162 /*
1163 * Ignore insert records without new tuples. This happens when a
1164 * multi_insert is done on a catalog or on a non-persistent relation.
1165 */
1166 if (!(xlrec->flags & XLH_INSERT_CONTAINS_NEW_TUPLE))
1167 return;
1168
1169 /* only interested in our database */
1170 XLogRecGetBlockTag(r, 0, &rlocator, NULL, NULL);
1171 if (rlocator.dbOid != ctx->slot->data.database)
1172 return;
1173
1174 /* output plugin doesn't look for this origin, no need to queue */
1175 if (FilterByOrigin(ctx, XLogRecGetOrigin(r)))
1176 return;
1177
1178 /*
1179 * We know that this multi_insert isn't for a catalog, so the block should
1180 * always have data even if a full-page write of it is taken.
1181 */
1182 tupledata = XLogRecGetBlockData(r, 0, &tuplelen);
1183 Assert(tupledata != NULL);
1184
1185 data = tupledata;
1186 for (i = 0; i < xlrec->ntuples; i++)
1187 {
1188 ReorderBufferChange *change;
1190 int datalen;
1191 HeapTuple tuple;
1192 HeapTupleHeader header;
1193
1194 change = ReorderBufferAllocChange(ctx->reorder);
1196 change->origin_id = XLogRecGetOrigin(r);
1197
1198 memcpy(&change->data.tp.rlocator, &rlocator, sizeof(RelFileLocator));
1199
1201 data = ((char *) xlhdr) + SizeOfMultiInsertTuple;
1202 datalen = xlhdr->datalen;
1203
1204 change->data.tp.newtuple =
1205 ReorderBufferAllocTupleBuf(ctx->reorder, datalen);
1206
1207 tuple = change->data.tp.newtuple;
1208 header = tuple->t_data;
1209
1210 /* not a disk based tuple */
1212
1213 /*
1214 * We can only figure this out after reassembling the transactions.
1215 */
1216 tuple->t_tableOid = InvalidOid;
1217
1218 tuple->t_len = datalen + SizeofHeapTupleHeader;
1219
1220 memset(header, 0, SizeofHeapTupleHeader);
1221
1222 memcpy((char *) tuple->t_data + SizeofHeapTupleHeader, data, datalen);
1223 header->t_infomask = xlhdr->t_infomask;
1224 header->t_infomask2 = xlhdr->t_infomask2;
1225 header->t_hoff = xlhdr->t_hoff;
1226
1227 /*
1228 * Reset toast reassembly state only after the last row in the last
1229 * xl_multi_insert_tuple record emitted by one heap_multi_insert()
1230 * call.
1231 */
1232 if (xlrec->flags & XLH_INSERT_LAST_IN_MULTI &&
1233 (i + 1) == xlrec->ntuples)
1234 change->data.tp.clear_toast_afterwards = true;
1235 else
1236 change->data.tp.clear_toast_afterwards = false;
1237
1239 buf->origptr, change, false);
1240
1241 /* move to the next xl_multi_insert_tuple entry */
1242 data += datalen;
1243 }
1244 Assert(data == tupledata + tuplelen);
1245}
#define SHORTALIGN(LEN)
Definition c.h:892
#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(), memcpy(), 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 792 of file decode.c.

794{
795 SnapBuild *builder = ctx->snapshot_builder;
796 XLogRecPtr origin_lsn = parsed->origin_lsn;
797 TimestampTz prepare_time = parsed->xact_time;
798 ReplOriginId origin_id = XLogRecGetOrigin(buf->record);
799 int i;
800 TransactionId xid = parsed->twophase_xid;
801
802 if (parsed->origin_timestamp != 0)
803 prepare_time = parsed->origin_timestamp;
804
805 /*
806 * Remember the prepare info for a txn so that it can be used later in
807 * commit prepared if required. See ReorderBufferFinishPrepared.
808 */
809 if (!ReorderBufferRememberPrepareInfo(ctx->reorder, xid, buf->origptr,
810 buf->endptr, prepare_time, origin_id,
811 origin_lsn))
812 return;
813
814 /* We can't start streaming unless a consistent state is reached. */
816 {
818 return;
819 }
820
821 /*
822 * Check whether we need to process this transaction. See
823 * DecodeTXNNeedSkip for the reasons why we sometimes want to skip the
824 * transaction.
825 *
826 * We can't call ReorderBufferForget as we did in DecodeCommit as the txn
827 * hasn't yet been committed, removing this txn before a commit might
828 * result in the computation of an incorrect restart_lsn. See
829 * SnapBuildProcessRunningXacts. But we need to process cache
830 * invalidations if there are any for the reasons mentioned in
831 * DecodeCommit.
832 */
833 if (DecodeTXNNeedSkip(ctx, buf, parsed->dbId, origin_id))
834 {
836 ReorderBufferInvalidate(ctx->reorder, xid, buf->origptr);
837 return;
838 }
839
840 /* Tell the reorderbuffer about the surviving subtransactions. */
841 for (i = 0; i < parsed->nsubxacts; i++)
842 {
843 ReorderBufferCommitChild(ctx->reorder, xid, parsed->subxacts[i],
844 buf->origptr, buf->endptr);
845 }
846
847 /* replay actions of all transaction + subtransactions in order */
848 ReorderBufferPrepare(ctx->reorder, xid, parsed->twophase_gid);
849
850 /*
851 * Update the decoding stats at transaction prepare/commit/abort.
852 * Additionally we send the stats when we spill or stream the changes to
853 * avoid losing them in case the decoding is interrupted. It is not clear
854 * that sending more or less frequently than this would be better.
855 */
857}
uint32 TransactionId
Definition c.h:736
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:295
@ SNAPBUILD_CONSISTENT
Definition snapbuild.h:58

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 1254 of file decode.c.

1255{
1257 ReorderBufferChange *change;
1258 RelFileLocator target_locator;
1259
1260 /* only interested in our database */
1261 XLogRecGetBlockTag(r, 0, &target_locator, NULL, NULL);
1262 if (target_locator.dbOid != ctx->slot->data.database)
1263 return;
1264
1265 /* output plugin doesn't look for this origin, no need to queue */
1266 if (FilterByOrigin(ctx, XLogRecGetOrigin(r)))
1267 return;
1268
1269 change = ReorderBufferAllocChange(ctx->reorder);
1271 change->origin_id = XLogRecGetOrigin(r);
1272
1273 memcpy(&change->data.tp.rlocator, &target_locator, sizeof(RelFileLocator));
1274
1275 change->data.tp.clear_toast_afterwards = true;
1276
1278 change, false);
1279}
@ REORDER_BUFFER_CHANGE_INTERNAL_SPEC_CONFIRM

References ReorderBufferChange::action, buf, ReorderBufferChange::clear_toast_afterwards, ReorderBufferChange::data, ReplicationSlot::data, ReplicationSlotPersistentData::database, RelFileLocator::dbOid, fb(), FilterByOrigin(), memcpy(), 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 1112 of file decode.c.

1113{
1116 ReorderBufferChange *change;
1117
1119
1120 /* only interested in our database */
1121 if (xlrec->dbId != ctx->slot->data.database)
1122 return;
1123
1124 /* output plugin doesn't look for this origin, no need to queue */
1125 if (FilterByOrigin(ctx, XLogRecGetOrigin(r)))
1126 return;
1127
1128 change = ReorderBufferAllocChange(ctx->reorder);
1130 change->origin_id = XLogRecGetOrigin(r);
1131 if (xlrec->flags & XLH_TRUNCATE_CASCADE)
1132 change->data.truncate.cascade = true;
1133 if (xlrec->flags & XLH_TRUNCATE_RESTART_SEQS)
1134 change->data.truncate.restart_seqs = true;
1135 change->data.truncate.nrelids = xlrec->nrelids;
1137 xlrec->nrelids);
1138 memcpy(change->data.truncate.relids, xlrec->relids,
1139 xlrec->nrelids * sizeof(Oid));
1141 buf->origptr, change, false);
1142}
#define XLH_TRUNCATE_RESTART_SEQS
#define XLH_TRUNCATE_CASCADE
unsigned int Oid
Oid * ReorderBufferAllocRelids(ReorderBuffer *rb, int nrelids)
@ REORDER_BUFFER_CHANGE_TRUNCATE
struct ReorderBufferChange::@118::@120 truncate

References ReorderBufferChange::action, buf, ReorderBufferChange::cascade, ReorderBufferChange::data, ReplicationSlot::data, ReplicationSlotPersistentData::database, fb(), FilterByOrigin(), memcpy(), 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 1334 of file decode.c.

1336{
1337 if (SnapBuildXactNeedsSkip(ctx->snapshot_builder, buf->origptr) ||
1338 (txn_dbid != InvalidOid && txn_dbid != ctx->slot->data.database) ||
1339 FilterByOrigin(ctx, origin_id))
1340 return true;
1341
1342 /*
1343 * We also skip decoding in fast_forward mode. In passing set the
1344 * processing_required flag to indicate that if it were not for
1345 * fast_forward mode, processing would have been required.
1346 */
1347 if (ctx->fast_forward)
1348 {
1349 ctx->processing_required = true;
1350 return true;
1351 }
1352
1353 return false;
1354}
bool SnapBuildXactNeedsSkip(SnapBuild *builder, XLogRecPtr ptr)
Definition snapbuild.c:322

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 982 of file decode.c.

983{
986 ReorderBufferChange *change;
987 char *data;
988 RelFileLocator target_locator;
989
991
992 /* only interested in our database */
993 XLogRecGetBlockTag(r, 0, &target_locator, NULL, NULL);
994 if (target_locator.dbOid != ctx->slot->data.database)
995 return;
996
997 /* output plugin doesn't look for this origin, no need to queue */
998 if (FilterByOrigin(ctx, XLogRecGetOrigin(r)))
999 return;
1000
1001 change = ReorderBufferAllocChange(ctx->reorder);
1003 change->origin_id = XLogRecGetOrigin(r);
1004 memcpy(&change->data.tp.rlocator, &target_locator, sizeof(RelFileLocator));
1005
1007 {
1008 Size datalen;
1009 Size tuplelen;
1010
1011 data = XLogRecGetBlockData(r, 0, &datalen);
1012
1013 tuplelen = datalen - SizeOfHeapHeader;
1014
1015 change->data.tp.newtuple =
1017
1018 DecodeXLogTuple(data, datalen, change->data.tp.newtuple);
1019 }
1020
1021 if (xlrec->flags & XLH_UPDATE_CONTAINS_OLD)
1022 {
1023 Size datalen;
1024 Size tuplelen;
1025
1026 /* caution, remaining data in record is not aligned */
1028 datalen = XLogRecGetDataLen(r) - SizeOfHeapUpdate;
1029 tuplelen = datalen - SizeOfHeapHeader;
1030
1031 change->data.tp.oldtuple =
1033
1034 DecodeXLogTuple(data, datalen, change->data.tp.oldtuple);
1035 }
1036
1037 change->data.tp.clear_toast_afterwards = true;
1038
1040 change, false);
1041}
#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(), memcpy(), 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 1290 of file decode.c.

1291{
1293 int datalen = len - SizeOfHeapHeader;
1294 HeapTupleHeader header;
1295
1296 Assert(datalen >= 0);
1297
1298 tuple->t_len = datalen + SizeofHeapTupleHeader;
1299 header = tuple->t_data;
1300
1301 /* not a disk based tuple */
1303
1304 /* we can only figure this out after reassembling the transactions */
1305 tuple->t_tableOid = InvalidOid;
1306
1307 /* data is not stored aligned, copy to aligned storage */
1309
1310 memset(header, 0, SizeofHeapTupleHeader);
1311
1312 memcpy(((char *) tuple->t_data) + SizeofHeapTupleHeader,
1314 datalen);
1315
1316 header->t_infomask = xlhdr.t_infomask;
1317 header->t_infomask2 = xlhdr.t_infomask2;
1318 header->t_hoff = xlhdr.t_hoff;
1319}
const void size_t len

References Assert, data, fb(), InvalidOid, ItemPointerSetInvalid(), len, memcpy(), 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 603 of file decode.c.

604{
606 return false;
607
608 return filter_by_origin_cb_wrapper(ctx, origin_id);
609}
bool filter_by_origin_cb_wrapper(LogicalDecodingContext *ctx, ReplOriginId origin_id)
Definition logical.c:1195
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 580 of file decode.c.

582{
583 /*
584 * Skip if decoding of two-phase transactions at PREPARE time is not
585 * enabled. In that case, all two-phase transactions are considered
586 * filtered out and will be applied as regular transactions at COMMIT
587 * PREPARED.
588 */
589 if (!ctx->twophase)
590 return true;
591
592 /*
593 * The filter_prepare callback is optional. When not supplied, all
594 * prepared transactions should go through.
595 */
596 if (ctx->callbacks.filter_prepare_cb == NULL)
597 return false;
598
599 return filter_prepare_cb_wrapper(ctx, xid, gid);
600}
bool filter_prepare_cb_wrapper(LogicalDecodingContext *ctx, TransactionId xid, const char *gid)
Definition logical.c:1163
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 430 of file decode.c.

431{
432 uint8 info = XLogRecGetInfo(buf->record) & XLOG_HEAP_OPMASK;
433 TransactionId xid = XLogRecGetXid(buf->record);
434 SnapBuild *builder = ctx->snapshot_builder;
435
436 ReorderBufferProcessXid(ctx->reorder, xid, buf->origptr);
437
438 /*
439 * If we don't have snapshot or we are just fast-forwarding, there is no
440 * point in decoding data changes. However, it's crucial to build the base
441 * snapshot during fast-forward mode (as is done in
442 * SnapBuildProcessChange()) because we require the snapshot's xmin when
443 * determining the candidate catalog_xmin for the replication slot. See
444 * SnapBuildProcessRunningXacts().
445 */
447 return;
448
449 switch (info)
450 {
452 if (SnapBuildProcessChange(builder, xid, buf->origptr) &&
453 !ctx->fast_forward &&
456 break;
458 if (!ctx->fast_forward)
459 {
461
463 SnapBuildProcessNewCid(builder, xid, buf->origptr, xlrec);
464 }
465 break;
467
468 /*
469 * Although these records only exist to serve the needs of logical
470 * decoding, all the work happens as part of crash or archive
471 * recovery, so we don't need to do anything here.
472 */
473 break;
474
475 /*
476 * Everything else here is just low level physical stuff we're not
477 * interested in.
478 */
483 break;
484 default:
485 elog(ERROR, "unexpected RM_HEAP2_ID record type: %u", info);
486 }
487}
uint8_t uint8
Definition c.h:622
static void DecodeMultiInsert(LogicalDecodingContext *ctx, XLogRecordBuffer *buf)
Definition decode.c:1150
#define ERROR
Definition elog.h:39
#define elog(elevel,...)
Definition elog.h:227
#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
void ReorderBufferProcessXid(ReorderBuffer *rb, TransactionId xid, XLogRecPtr lsn)
bool change_useless_for_repack(XLogRecordBuffer *buf)
bool SnapBuildProcessChange(SnapBuild *builder, TransactionId xid, XLogRecPtr lsn)
Definition snapbuild.c:656
void SnapBuildProcessNewCid(SnapBuild *builder, TransactionId xid, XLogRecPtr lsn, xl_heap_new_cid *xlrec)
Definition snapbuild.c:706
@ SNAPBUILD_FULL_SNAPSHOT
Definition snapbuild.h:51
#define XLogRecGetInfo(decoder)
Definition xlogreader.h:410

References buf, change_useless_for_repack(), 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_HEAP_OPMASK, XLogRecGetData, XLogRecGetInfo, and XLogRecGetXid.

◆ heap_decode()

void heap_decode ( LogicalDecodingContext ctx,
XLogRecordBuffer buf 
)

Definition at line 493 of file decode.c.

494{
495 uint8 info = XLogRecGetInfo(buf->record) & XLOG_HEAP_OPMASK;
496 TransactionId xid = XLogRecGetXid(buf->record);
497 SnapBuild *builder = ctx->snapshot_builder;
498
499 ReorderBufferProcessXid(ctx->reorder, xid, buf->origptr);
500
501 /*
502 * If we don't have snapshot or we are just fast-forwarding, there is no
503 * point in decoding data changes. However, it's crucial to build the base
504 * snapshot during fast-forward mode (as is done in
505 * SnapBuildProcessChange()) because we require the snapshot's xmin when
506 * determining the candidate catalog_xmin for the replication slot. See
507 * SnapBuildProcessRunningXacts().
508 */
510 return;
511
512 switch (info)
513 {
514 case XLOG_HEAP_INSERT:
515 if (SnapBuildProcessChange(builder, xid, buf->origptr) &&
516 !ctx->fast_forward &&
518 DecodeInsert(ctx, buf);
519 break;
520
521 /*
522 * Treat HOT update as normal updates. There is no useful
523 * information in the fact that we could make it a HOT update
524 * locally and the WAL layout is compatible.
525 */
527 case XLOG_HEAP_UPDATE:
528 if (SnapBuildProcessChange(builder, xid, buf->origptr) &&
529 !ctx->fast_forward &&
531 DecodeUpdate(ctx, buf);
532 break;
533
534 case XLOG_HEAP_DELETE:
535 if (SnapBuildProcessChange(builder, xid, buf->origptr) &&
536 !ctx->fast_forward &&
538 DecodeDelete(ctx, buf);
539 break;
540
542 if (SnapBuildProcessChange(builder, xid, buf->origptr) &&
543 !ctx->fast_forward &&
545 DecodeTruncate(ctx, buf);
546 break;
547
549
550 /*
551 * Inplace updates are only ever performed on catalog tuples and
552 * can, per definition, not change tuple visibility. Since we
553 * also don't decode catalog tuples, we're not interested in the
554 * record's contents.
555 */
556 break;
557
559 if (SnapBuildProcessChange(builder, xid, buf->origptr) &&
560 !ctx->fast_forward &&
563 break;
564
565 case XLOG_HEAP_LOCK:
566 /* we don't care about row level locks for now */
567 break;
568
569 default:
570 elog(ERROR, "unexpected RM_HEAP_ID record type: %u", info);
571 break;
572 }
573}
static void DecodeDelete(LogicalDecodingContext *ctx, XLogRecordBuffer *buf)
Definition decode.c:1049
static void DecodeInsert(LogicalDecodingContext *ctx, XLogRecordBuffer *buf)
Definition decode.c:923
static void DecodeTruncate(LogicalDecodingContext *ctx, XLogRecordBuffer *buf)
Definition decode.c:1112
static void DecodeUpdate(LogicalDecodingContext *ctx, XLogRecordBuffer *buf)
Definition decode.c:982
static void DecodeSpecConfirm(LogicalDecodingContext *ctx, XLogRecordBuffer *buf)
Definition decode.c:1254
#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, change_useless_for_repack(), 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 89 of file decode.c.

90{
94
95 buf.origptr = ctx->reader->ReadRecPtr;
96 buf.endptr = ctx->reader->EndRecPtr;
97 buf.record = record;
98
99 txid = XLogRecGetTopXid(record);
100
101 /*
102 * If the top-level xid is valid, we need to assign the subxact to the
103 * top-level xact. We need to do this for all records, hence we do it
104 * before the switch.
105 */
107 {
109 txid,
110 XLogRecGetXid(record),
111 buf.origptr);
112 }
113
114 rmgr = GetRmgr(XLogRecGetRmid(record));
115
116 if (rmgr.rm_decode != NULL)
117 rmgr.rm_decode(ctx, &buf);
118 else
119 {
120 /* just deal with xid, and done */
122 buf.origptr);
123 }
124}
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:411
#define XLogRecGetTopXid(decoder)
Definition xlogreader.h:414

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

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

◆ logicalmsg_decode()

void logicalmsg_decode ( LogicalDecodingContext ctx,
XLogRecordBuffer buf 
)

Definition at line 615 of file decode.c.

616{
617 SnapBuild *builder = ctx->snapshot_builder;
621 ReplOriginId origin_id = XLogRecGetOrigin(r);
622 Snapshot snapshot = NULL;
623 xl_logical_message *message;
624
625 if (info != XLOG_LOGICAL_MESSAGE)
626 elog(ERROR, "unexpected RM_LOGICALMSG_ID record type: %u", info);
627
629
630 /* If we don't have snapshot, there is no point in decoding messages */
632 return;
633
634 message = (xl_logical_message *) XLogRecGetData(r);
635
636 if (message->dbId != ctx->slot->data.database ||
637 FilterByOrigin(ctx, origin_id))
638 return;
639
640 if (message->transactional &&
641 !SnapBuildProcessChange(builder, xid, buf->origptr))
642 return;
643 else if (!message->transactional &&
645 SnapBuildXactNeedsSkip(builder, buf->origptr)))
646 return;
647
648 /*
649 * We also skip decoding in fast_forward mode. This check must be last
650 * because we don't want to set the processing_required flag unless we
651 * have a decodable message.
652 */
653 if (ctx->fast_forward)
654 {
655 /*
656 * We need to set processing_required flag to notify the message's
657 * existence to the caller. Usually, the flag is set when either the
658 * COMMIT or ABORT records are decoded, but this must be turned on
659 * here because the non-transactional logical message is decoded
660 * without waiting for these records.
661 */
662 if (!message->transactional)
663 ctx->processing_required = true;
664
665 return;
666 }
667
668 /*
669 * If this is a non-transactional change, get the snapshot we're expected
670 * to use. We only get here when the snapshot is consistent, and the
671 * change is not meant to be skipped.
672 *
673 * For transactional changes we don't need a snapshot, we'll use the
674 * regular snapshot maintained by ReorderBuffer. We just leave it NULL.
675 */
676 if (!message->transactional)
677 snapshot = SnapBuildGetOrBuildSnapshot(builder);
678
679 ReorderBufferQueueMessage(ctx->reorder, xid, snapshot, buf->endptr,
680 message->transactional,
681 message->message, /* first part of message is
682 * prefix */
683 message->message_size,
684 message->message + message->prefix_size);
685}
#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:596
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 371 of file decode.c.

372{
373 SnapBuild *builder = ctx->snapshot_builder;
376
378
379 switch (info)
380 {
382 {
384
385 /*
386 * Update this decoder's idea of transactions currently
387 * running. In doing so we will determine whether we have
388 * reached consistent status.
389 *
390 * If the output plugin doesn't need access to shared
391 * catalogs, we can ignore transactions in other databases.
392 */
393 SnapBuildProcessRunningXacts(builder, buf->origptr, running,
395
396 /*
397 * Abort all transactions that we keep track of, that are
398 * older than the record's oldestRunningXid. This is the most
399 * convenient spot for doing so since, in contrast to shutdown
400 * or end-of-recovery checkpoints, we have information about
401 * all running transactions which includes prepared ones,
402 * while shutdown checkpoints just know that no non-prepared
403 * transactions are in progress.
404 *
405 * The database-specific records might work here too, but it's
406 * not their purpose.
407 */
408 if (!OidIsValid(running->dbid))
410 }
411 break;
413 break;
415
416 /*
417 * We are processing the invalidations at the command level via
418 * XLOG_XACT_INVALIDATIONS. So we don't need to do anything here.
419 */
420 break;
421 default:
422 elog(ERROR, "unexpected RM_STANDBY_ID record type: %u", info);
423 }
424}
#define OidIsValid(objectId)
Definition c.h:858
void ReorderBufferAbortOld(ReorderBuffer *rb, TransactionId oldestRunningXid)
void SnapBuildProcessRunningXacts(SnapBuild *builder, XLogRecPtr lsn, xl_running_xacts *running, bool db_specific)
Definition snapbuild.c:1154
#define XLOG_INVALIDATIONS
Definition standbydefs.h:36
#define XLOG_STANDBY_LOCK
Definition standbydefs.h:34
#define XLOG_RUNNING_XACTS
Definition standbydefs.h:35
OutputPluginOptions options
Definition logical.h:54
TransactionId oldestRunningXid
Definition standbydefs.h:54

References buf, xl_running_xacts::dbid, elog, ERROR, fb(), OutputPluginOptions::need_shared_catalogs, OidIsValid, xl_running_xacts::oldestRunningXid, LogicalDecodingContext::options, 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 213 of file decode.c.

214{
215 SnapBuild *builder = ctx->snapshot_builder;
216 ReorderBuffer *reorder = ctx->reorder;
219
220 /*
221 * If the snapshot isn't yet fully built, we cannot decode anything, so
222 * bail out.
223 */
225 return;
226
227 switch (info)
228 {
229 case XLOG_XACT_COMMIT:
231 {
234 TransactionId xid;
235 bool two_phase = false;
236
239
240 if (!TransactionIdIsValid(parsed.twophase_xid))
241 xid = XLogRecGetXid(r);
242 else
243 xid = parsed.twophase_xid;
244
245 /*
246 * We would like to process the transaction in a two-phase
247 * manner iff output plugin supports two-phase commits and
248 * doesn't filter the transaction at prepare time.
249 */
250 if (info == XLOG_XACT_COMMIT_PREPARED)
251 two_phase = !(FilterPrepare(ctx, xid,
252 parsed.twophase_gid));
253
254 DecodeCommit(ctx, buf, &parsed, xid, two_phase);
255 break;
256 }
257 case XLOG_XACT_ABORT:
259 {
262 TransactionId xid;
263 bool two_phase = false;
264
267
268 if (!TransactionIdIsValid(parsed.twophase_xid))
269 xid = XLogRecGetXid(r);
270 else
271 xid = parsed.twophase_xid;
272
273 /*
274 * We would like to process the transaction in a two-phase
275 * manner iff output plugin supports two-phase commits and
276 * doesn't filter the transaction at prepare time.
277 */
278 if (info == XLOG_XACT_ABORT_PREPARED)
279 two_phase = !(FilterPrepare(ctx, xid,
280 parsed.twophase_gid));
281
282 DecodeAbort(ctx, buf, &parsed, xid, two_phase);
283 break;
284 }
286
287 /*
288 * We assign subxact to the toplevel xact while processing each
289 * record if required. So, we don't need to do anything here. See
290 * LogicalDecodingProcessRecord.
291 */
292 break;
294 {
295 TransactionId xid;
297
298 xid = XLogRecGetXid(r);
300
301 /*
302 * Execute the invalidations for xid-less transactions,
303 * otherwise, accumulate them so that they can be processed at
304 * the commit time.
305 */
306 if (TransactionIdIsValid(xid))
307 {
308 if (!ctx->fast_forward)
310 buf->origptr,
311 invals->nmsgs,
312 invals->msgs);
314 buf->origptr);
315 }
316 else if (!ctx->fast_forward)
318 invals->nmsgs,
319 invals->msgs);
320
321 break;
322 }
324 {
327
328 /* ok, parse it */
331 xlrec, &parsed);
332
333 /*
334 * We would like to process the transaction in a two-phase
335 * manner iff output plugin supports two-phase commits and
336 * doesn't filter the transaction at prepare time.
337 */
338 if (FilterPrepare(ctx, parsed.twophase_xid,
339 parsed.twophase_gid))
340 {
341 ReorderBufferProcessXid(reorder, parsed.twophase_xid,
342 buf->origptr);
343 break;
344 }
345
346 /*
347 * Note that if the prepared transaction has locked [user]
348 * catalog tables exclusively then decoding prepare can block
349 * till the main transaction is committed because it needs to
350 * lock the catalog tables.
351 *
352 * XXX Now, this can even lead to a deadlock if the prepare
353 * transaction is waiting to get it logically replicated for
354 * distributed 2PC. This can be avoided by disallowing
355 * preparing transactions that have locked [user] catalog
356 * tables exclusively but as of now, we ask users not to do
357 * such an operation.
358 */
359 DecodePrepare(ctx, buf, &parsed);
360 break;
361 }
362 default:
363 elog(ERROR, "unexpected RM_XACT_ID record type: %u", info);
364 }
365}
static void DecodeAbort(LogicalDecodingContext *ctx, XLogRecordBuffer *buf, xl_xact_parsed_abort *parsed, TransactionId xid, bool two_phase)
Definition decode.c:867
static bool FilterPrepare(LogicalDecodingContext *ctx, TransactionId xid, const char *gid)
Definition decode.c:580
static void DecodeCommit(LogicalDecodingContext *ctx, XLogRecordBuffer *buf, xl_xact_parsed_commit *parsed, TransactionId xid, bool two_phase)
Definition decode.c:696
static void DecodePrepare(LogicalDecodingContext *ctx, XLogRecordBuffer *buf, xl_xact_parsed_prepare *parsed)
Definition decode.c:792
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.

◆ xlog2_decode()

void xlog2_decode ( LogicalDecodingContext ctx,
XLogRecordBuffer buf 
)

Definition at line 194 of file decode.c.

195{
196 uint8 info = XLogRecGetInfo(buf->record) & ~XLR_INFO_MASK;
197
198 ReorderBufferProcessXid(ctx->reorder, XLogRecGetXid(buf->record), buf->origptr);
199
200 switch (info)
201 {
202 case XLOG2_CHECKSUMS:
203 break;
204 default:
205 elog(ERROR, "unexpected RM_XLOG2_ID record type: %u", info);
206 }
207}
#define XLOG2_CHECKSUMS
Definition pg_control.h:90

References buf, elog, ERROR, fb(), LogicalDecodingContext::reorder, ReorderBufferProcessXid(), XLOG2_CHECKSUMS, XLogRecGetInfo, and XLogRecGetXid.

◆ xlog_decode()

void xlog_decode ( LogicalDecodingContext ctx,
XLogRecordBuffer buf 
)

Definition at line 130 of file decode.c.

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

References buf, elog, ERROR, fb(), memcpy(), 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.