1 /* -------------------------------------------------------------------------
4 * This module decodes WAL records read using xlogreader.h's APIs for the
5 * purpose of logical decoding by passing information to the
6 * reorderbuffer module (containing the actual changes) and to the
7 * snapbuild module to build a fitting catalog snapshot (to be able to
8 * properly decode the changes in the reorderbuffer).
11 * This basically tries to handle all low level xlog stuff for
12 * reorderbuffer.c and snapbuild.c. There's some minor leakage where a
13 * specific record's struct is used to pass data along, but those just
14 * happen to contain the right amount of data in a convenient
15 * format. There isn't and shouldn't be much intelligence about the
16 * contents of records in here except turning them into a more usable
19 * Portions Copyright (c) 1996-2016, PostgreSQL Global Development Group
20 * Portions Copyright (c) 1994, Regents of the University of California
23 * src/backend/replication/logical/decode.c
25 * -------------------------------------------------------------------------
29 #include "access/heapam.h"
30 #include "access/heapam_xlog.h"
31 #include "access/transam.h"
32 #include "access/xact.h"
33 #include "access/xlog_internal.h"
34 #include "access/xlogutils.h"
35 #include "access/xlogreader.h"
36 #include "access/xlogrecord.h"
38 #include "catalog/pg_control.h"
40 #include "replication/decode.h"
41 #include "replication/logical.h"
42 #include "replication/message.h"
43 #include "replication/reorderbuffer.h"
44 #include "replication/origin.h"
45 #include "replication/snapbuild.h"
47 #include "storage/standby.h"
49 typedef struct XLogRecordBuffer
53 XLogReaderState *record;
57 static void DecodeXLogOp(LogicalDecodingContext *ctx, XLogRecordBuffer *buf);
58 static void DecodeHeapOp(LogicalDecodingContext *ctx, XLogRecordBuffer *buf);
59 static void DecodeHeap2Op(LogicalDecodingContext *ctx, XLogRecordBuffer *buf);
60 static void DecodeXactOp(LogicalDecodingContext *ctx, XLogRecordBuffer *buf);
61 static void DecodeStandbyOp(LogicalDecodingContext *ctx, XLogRecordBuffer *buf);
62 static void DecodeLogicalMsgOp(LogicalDecodingContext *ctx, XLogRecordBuffer *buf);
64 /* individual record(group)'s handlers */
65 static void DecodeInsert(LogicalDecodingContext *ctx, XLogRecordBuffer *buf);
66 static void DecodeUpdate(LogicalDecodingContext *ctx, XLogRecordBuffer *buf);
67 static void DecodeDelete(LogicalDecodingContext *ctx, XLogRecordBuffer *buf);
68 static void DecodeMultiInsert(LogicalDecodingContext *ctx, XLogRecordBuffer *buf);
69 static void DecodeSpecConfirm(LogicalDecodingContext *ctx, XLogRecordBuffer *buf);
71 static void DecodeCommit(LogicalDecodingContext *ctx, XLogRecordBuffer *buf,
72 xl_xact_parsed_commit *parsed, TransactionId xid);
73 static void DecodeAbort(LogicalDecodingContext *ctx, XLogRecordBuffer *buf,
74 xl_xact_parsed_abort *parsed, TransactionId xid);
76 /* common function to decode tuples */
77 static void DecodeXLogTuple(char *data, Size len, ReorderBufferTupleBuf *tup);
80 * Take every XLogReadRecord()ed record and perform the actions required to
81 * decode it using the output plugin already setup in the logical decoding
84 * NB: Note that every record's xid needs to be processed by reorderbuffer
85 * (xids contained in the content of records are not relevant for this rule).
86 * That means that for records which'd otherwise not go through the
87 * reorderbuffer ReorderBufferProcessXid() has to be called. We don't want to
88 * call ReorderBufferProcessXid for each record type by default, because
89 * e.g. empty xacts can be handled more efficiently if there's no previous
93 LogicalDecodingProcessRecord(LogicalDecodingContext *ctx, XLogReaderState *record)
97 buf.origptr = ctx->reader->ReadRecPtr;
98 buf.endptr = ctx->reader->EndRecPtr;
101 /* cast so we get a warning when new rmgrs are added */
102 switch ((RmgrIds) XLogRecGetRmid(record))
105 * Rmgrs we care about for logical decoding. Add new rmgrs in
106 * rmgrlist.h's order.
109 DecodeXLogOp(ctx, &buf);
113 DecodeXactOp(ctx, &buf);
117 DecodeStandbyOp(ctx, &buf);
121 DecodeHeap2Op(ctx, &buf);
125 DecodeHeapOp(ctx, &buf);
128 case RM_LOGICALMSG_ID:
129 DecodeLogicalMsgOp(ctx, &buf);
133 * Rmgrs irrelevant for logical decoding; they describe stuff not
134 * represented in logical decoding. Add new rmgrs in rmgrlist.h's
141 case RM_MULTIXACT_ID:
150 case RM_COMMIT_TS_ID:
151 case RM_REPLORIGIN_ID:
153 /* just deal with xid, and done */
154 ReorderBufferProcessXid(ctx->reorder, XLogRecGetXid(record),
158 elog(ERROR, "unexpected RM_NEXT_ID rmgr_id: %u", (RmgrIds) XLogRecGetRmid(buf.record));
163 * Handle rmgr XLOG_ID records for DecodeRecordIntoReorderBuffer().
166 DecodeXLogOp(LogicalDecodingContext *ctx, XLogRecordBuffer *buf)
168 SnapBuild *builder = ctx->snapshot_builder;
169 uint8 info = XLogRecGetInfo(buf->record) & ~XLR_INFO_MASK;
171 ReorderBufferProcessXid(ctx->reorder, XLogRecGetXid(buf->record),
176 /* this is also used in END_OF_RECOVERY checkpoints */
177 case XLOG_CHECKPOINT_SHUTDOWN:
178 case XLOG_END_OF_RECOVERY:
179 SnapBuildSerializationPoint(builder, buf->origptr);
182 case XLOG_CHECKPOINT_ONLINE:
185 * a RUNNING_XACTS record will have been logged near to this, we
186 * can restart from there.
192 case XLOG_BACKUP_END:
193 case XLOG_PARAMETER_CHANGE:
194 case XLOG_RESTORE_POINT:
195 case XLOG_FPW_CHANGE:
196 case XLOG_FPI_FOR_HINT:
200 elog(ERROR, "unexpected RM_XLOG_ID record type: %u", info);
205 * Handle rmgr XACT_ID records for DecodeRecordIntoReorderBuffer().
208 DecodeXactOp(LogicalDecodingContext *ctx, XLogRecordBuffer *buf)
210 SnapBuild *builder = ctx->snapshot_builder;
211 ReorderBuffer *reorder = ctx->reorder;
212 XLogReaderState *r = buf->record;
213 uint8 info = XLogRecGetInfo(r) & XLOG_XACT_OPMASK;
216 * No point in doing anything yet, data could not be decoded anyway. It's
217 * ok not to call ReorderBufferProcessXid() in that case, except in the
218 * assignment case there'll not be any later records with the same xid;
219 * and in the assignment case we'll not decode those xacts.
221 if (SnapBuildCurrentState(builder) < SNAPBUILD_FULL_SNAPSHOT)
226 case XLOG_XACT_COMMIT:
227 case XLOG_XACT_COMMIT_PREPARED:
229 xl_xact_commit *xlrec;
230 xl_xact_parsed_commit parsed;
233 xlrec = (xl_xact_commit *) XLogRecGetData(r);
234 ParseCommitRecord(XLogRecGetInfo(buf->record), xlrec, &parsed);
236 if (!TransactionIdIsValid(parsed.twophase_xid))
237 xid = XLogRecGetXid(r);
239 xid = parsed.twophase_xid;
241 DecodeCommit(ctx, buf, &parsed, xid);
244 case XLOG_XACT_ABORT:
245 case XLOG_XACT_ABORT_PREPARED:
247 xl_xact_abort *xlrec;
248 xl_xact_parsed_abort parsed;
251 xlrec = (xl_xact_abort *) XLogRecGetData(r);
252 ParseAbortRecord(XLogRecGetInfo(buf->record), xlrec, &parsed);
254 if (!TransactionIdIsValid(parsed.twophase_xid))
255 xid = XLogRecGetXid(r);
257 xid = parsed.twophase_xid;
259 DecodeAbort(ctx, buf, &parsed, xid);
262 case XLOG_XACT_ASSIGNMENT:
264 xl_xact_assignment *xlrec;
266 TransactionId *sub_xid;
268 xlrec = (xl_xact_assignment *) XLogRecGetData(r);
270 sub_xid = &xlrec->xsub[0];
272 for (i = 0; i < xlrec->nsubxacts; i++)
274 ReorderBufferAssignChild(reorder, xlrec->xtop,
275 *(sub_xid++), buf->origptr);
279 case XLOG_XACT_PREPARE:
282 * Currently decoding ignores PREPARE TRANSACTION and will just
283 * decode the transaction when the COMMIT PREPARED is sent or
284 * throw away the transaction's contents when a ROLLBACK PREPARED
285 * is received. In the future we could add code to expose prepared
286 * transactions in the changestream allowing for a kind of
289 ReorderBufferProcessXid(reorder, XLogRecGetXid(r), buf->origptr);
292 elog(ERROR, "unexpected RM_XACT_ID record type: %u", info);
297 * Handle rmgr STANDBY_ID records for DecodeRecordIntoReorderBuffer().
300 DecodeStandbyOp(LogicalDecodingContext *ctx, XLogRecordBuffer *buf)
302 SnapBuild *builder = ctx->snapshot_builder;
303 XLogReaderState *r = buf->record;
304 uint8 info = XLogRecGetInfo(r) & ~XLR_INFO_MASK;
306 ReorderBufferProcessXid(ctx->reorder, XLogRecGetXid(r), buf->origptr);
310 case XLOG_RUNNING_XACTS:
312 xl_running_xacts *running = (xl_running_xacts *) XLogRecGetData(r);
314 SnapBuildProcessRunningXacts(builder, buf->origptr, running);
317 * Abort all transactions that we keep track of, that are
318 * older than the record's oldestRunningXid. This is the most
319 * convenient spot for doing so since, in contrast to shutdown
320 * or end-of-recovery checkpoints, we have information about
321 * all running transactions which includes prepared ones,
322 * while shutdown checkpoints just know that no non-prepared
323 * transactions are in progress.
325 ReorderBufferAbortOld(ctx->reorder, running->oldestRunningXid);
328 case XLOG_STANDBY_LOCK:
330 case XLOG_INVALIDATIONS:
332 xl_invalidations *invalidations =
333 (xl_invalidations *) XLogRecGetData(r);
335 ReorderBufferImmediateInvalidation(
336 ctx->reorder, invalidations->nmsgs, invalidations->msgs);
340 elog(ERROR, "unexpected RM_STANDBY_ID record type: %u", info);
345 * Handle rmgr HEAP2_ID records for DecodeRecordIntoReorderBuffer().
348 DecodeHeap2Op(LogicalDecodingContext *ctx, XLogRecordBuffer *buf)
350 uint8 info = XLogRecGetInfo(buf->record) & XLOG_HEAP_OPMASK;
351 TransactionId xid = XLogRecGetXid(buf->record);
352 SnapBuild *builder = ctx->snapshot_builder;
354 ReorderBufferProcessXid(ctx->reorder, xid, buf->origptr);
356 /* no point in doing anything yet */
357 if (SnapBuildCurrentState(builder) < SNAPBUILD_FULL_SNAPSHOT)
362 case XLOG_HEAP2_MULTI_INSERT:
363 if (SnapBuildProcessChange(builder, xid, buf->origptr))
364 DecodeMultiInsert(ctx, buf);
366 case XLOG_HEAP2_NEW_CID:
368 xl_heap_new_cid *xlrec;
370 xlrec = (xl_heap_new_cid *) XLogRecGetData(buf->record);
371 SnapBuildProcessNewCid(builder, xid, buf->origptr, xlrec);
375 case XLOG_HEAP2_REWRITE:
378 * Although these records only exist to serve the needs of logical
379 * decoding, all the work happens as part of crash or archive
380 * recovery, so we don't need to do anything here.
385 * Everything else here is just low level physical stuff we're not
388 case XLOG_HEAP2_FREEZE_PAGE:
389 case XLOG_HEAP2_CLEAN:
390 case XLOG_HEAP2_CLEANUP_INFO:
391 case XLOG_HEAP2_VISIBLE:
392 case XLOG_HEAP2_LOCK_UPDATED:
395 elog(ERROR, "unexpected RM_HEAP2_ID record type: %u", info);
400 * Handle rmgr HEAP_ID records for DecodeRecordIntoReorderBuffer().
403 DecodeHeapOp(LogicalDecodingContext *ctx, XLogRecordBuffer *buf)
405 uint8 info = XLogRecGetInfo(buf->record) & XLOG_HEAP_OPMASK;
406 TransactionId xid = XLogRecGetXid(buf->record);
407 SnapBuild *builder = ctx->snapshot_builder;
409 ReorderBufferProcessXid(ctx->reorder, xid, buf->origptr);
411 /* no point in doing anything yet */
412 if (SnapBuildCurrentState(builder) < SNAPBUILD_FULL_SNAPSHOT)
417 case XLOG_HEAP_INSERT:
418 if (SnapBuildProcessChange(builder, xid, buf->origptr))
419 DecodeInsert(ctx, buf);
423 * Treat HOT update as normal updates. There is no useful
424 * information in the fact that we could make it a HOT update
425 * locally and the WAL layout is compatible.
427 case XLOG_HEAP_HOT_UPDATE:
428 case XLOG_HEAP_UPDATE:
429 if (SnapBuildProcessChange(builder, xid, buf->origptr))
430 DecodeUpdate(ctx, buf);
433 case XLOG_HEAP_DELETE:
434 if (SnapBuildProcessChange(builder, xid, buf->origptr))
435 DecodeDelete(ctx, buf);
438 case XLOG_HEAP_INPLACE:
441 * Inplace updates are only ever performed on catalog tuples and
442 * can, per definition, not change tuple visibility. Since we
443 * don't decode catalog tuples, we're not interested in the
446 * In-place updates can be used either by XID-bearing transactions
447 * (e.g. in CREATE INDEX CONCURRENTLY) or by XID-less
448 * transactions (e.g. VACUUM). In the former case, the commit
449 * record will include cache invalidations, so we mark the
450 * transaction as catalog modifying here. Currently that's
451 * redundant because the commit will do that as well, but once we
452 * support decoding in-progress relations, this will be important.
454 if (!TransactionIdIsValid(xid))
457 SnapBuildProcessChange(builder, xid, buf->origptr);
458 ReorderBufferXidSetCatalogChanges(ctx->reorder, xid, buf->origptr);
461 case XLOG_HEAP_CONFIRM:
462 if (SnapBuildProcessChange(builder, xid, buf->origptr))
463 DecodeSpecConfirm(ctx, buf);
467 /* we don't care about row level locks for now */
471 elog(ERROR, "unexpected RM_HEAP_ID record type: %u", info);
477 FilterByOrigin(LogicalDecodingContext *ctx, RepOriginId origin_id)
479 if (ctx->callbacks.filter_by_origin_cb == NULL)
482 return filter_by_origin_cb_wrapper(ctx, origin_id);
486 * Handle rmgr LOGICALMSG_ID records for DecodeRecordIntoReorderBuffer().
489 DecodeLogicalMsgOp(LogicalDecodingContext *ctx, XLogRecordBuffer *buf)
491 SnapBuild *builder = ctx->snapshot_builder;
492 XLogReaderState *r = buf->record;
493 TransactionId xid = XLogRecGetXid(r);
494 uint8 info = XLogRecGetInfo(r) & ~XLR_INFO_MASK;
495 RepOriginId origin_id = XLogRecGetOrigin(r);
497 xl_logical_message *message;
499 if (info != XLOG_LOGICAL_MESSAGE)
500 elog(ERROR, "unexpected RM_LOGICALMSG_ID record type: %u", info);
502 ReorderBufferProcessXid(ctx->reorder, XLogRecGetXid(r), buf->origptr);
504 /* No point in doing anything yet. */
505 if (SnapBuildCurrentState(builder) < SNAPBUILD_FULL_SNAPSHOT)
508 message = (xl_logical_message *) XLogRecGetData(r);
510 if (message->dbId != ctx->slot->data.database ||
511 FilterByOrigin(ctx, origin_id))
514 if (message->transactional &&
515 !SnapBuildProcessChange(builder, xid, buf->origptr))
517 else if (!message->transactional &&
518 (SnapBuildCurrentState(builder) != SNAPBUILD_CONSISTENT ||
519 SnapBuildXactNeedsSkip(builder, buf->origptr)))
522 snapshot = SnapBuildGetOrBuildSnapshot(builder, xid);
523 ReorderBufferQueueMessage(ctx->reorder, xid, snapshot, buf->endptr,
524 message->transactional,
525 message->message, /* first part of message is
527 message->message_size,
528 message->message + message->prefix_size);
532 * Consolidated commit record handling between the different form of commit
536 DecodeCommit(LogicalDecodingContext *ctx, XLogRecordBuffer *buf,
537 xl_xact_parsed_commit *parsed, TransactionId xid)
539 XLogRecPtr origin_lsn = InvalidXLogRecPtr;
540 TimestampTz commit_time = parsed->xact_time;
541 RepOriginId origin_id = XLogRecGetOrigin(buf->record);
544 if (parsed->xinfo & XACT_XINFO_HAS_ORIGIN)
546 origin_lsn = parsed->origin_lsn;
547 commit_time = parsed->origin_timestamp;
551 * Process invalidation messages, even if we're not interested in the
552 * transaction's contents, since the various caches need to always be
555 if (parsed->nmsgs > 0)
557 ReorderBufferAddInvalidations(ctx->reorder, xid, buf->origptr,
558 parsed->nmsgs, parsed->msgs);
559 ReorderBufferXidSetCatalogChanges(ctx->reorder, xid, buf->origptr);
562 SnapBuildCommitTxn(ctx->snapshot_builder, buf->origptr, xid,
563 parsed->nsubxacts, parsed->subxacts);
566 * Check whether we are interested in this specific transaction, and tell
567 * the reorderbuffer to forget the content of the (sub-)transactions
570 * There can be several reasons we might not be interested in this
572 * 1) We might not be interested in decoding transactions up to this
573 * LSN. This can happen because we previously decoded it and now just
574 * are restarting or if we haven't assembled a consistent snapshot yet.
575 * 2) The transaction happened in another database.
576 * 3) The output plugin is not interested in the origin.
578 * We can't just use ReorderBufferAbort() here, because we need to execute
579 * the transaction's invalidations. This currently won't be needed if
580 * we're just skipping over the transaction because currently we only do
581 * so during startup, to get to the first transaction the client needs. As
582 * we have reset the catalog caches before starting to read WAL, and we
583 * haven't yet touched any catalogs, there can't be anything to invalidate.
584 * But if we're "forgetting" this commit because it's it happened in
585 * another database, the invalidations might be important, because they
586 * could be for shared catalogs and we might have loaded data into the
587 * relevant syscaches.
590 if (SnapBuildXactNeedsSkip(ctx->snapshot_builder, buf->origptr) ||
591 (parsed->dbId != InvalidOid && parsed->dbId != ctx->slot->data.database) ||
592 FilterByOrigin(ctx, origin_id))
594 for (i = 0; i < parsed->nsubxacts; i++)
596 ReorderBufferForget(ctx->reorder, parsed->subxacts[i], buf->origptr);
598 ReorderBufferForget(ctx->reorder, xid, buf->origptr);
603 /* tell the reorderbuffer about the surviving subtransactions */
604 for (i = 0; i < parsed->nsubxacts; i++)
606 ReorderBufferCommitChild(ctx->reorder, xid, parsed->subxacts[i],
607 buf->origptr, buf->endptr);
610 /* replay actions of all transaction + subtransactions in order */
611 ReorderBufferCommit(ctx->reorder, xid, buf->origptr, buf->endptr,
612 commit_time, origin_id, origin_lsn);
616 * Get the data from the various forms of abort records and pass it on to
617 * snapbuild.c and reorderbuffer.c
620 DecodeAbort(LogicalDecodingContext *ctx, XLogRecordBuffer *buf,
621 xl_xact_parsed_abort *parsed, TransactionId xid)
625 SnapBuildAbortTxn(ctx->snapshot_builder, buf->record->EndRecPtr, xid,
626 parsed->nsubxacts, parsed->subxacts);
628 for (i = 0; i < parsed->nsubxacts; i++)
630 ReorderBufferAbort(ctx->reorder, parsed->subxacts[i],
631 buf->record->EndRecPtr);
634 ReorderBufferAbort(ctx->reorder, xid, buf->record->EndRecPtr);
638 * Parse XLOG_HEAP_INSERT (not MULTI_INSERT!) records into tuplebufs.
640 * Deletes can contain the new tuple.
643 DecodeInsert(LogicalDecodingContext *ctx, XLogRecordBuffer *buf)
645 XLogReaderState *r = buf->record;
646 xl_heap_insert *xlrec;
647 ReorderBufferChange *change;
648 RelFileNode target_node;
650 xlrec = (xl_heap_insert *) XLogRecGetData(r);
652 /* only interested in our database */
653 XLogRecGetBlockTag(r, 0, &target_node, NULL, NULL);
654 if (target_node.dbNode != ctx->slot->data.database)
657 /* output plugin doesn't look for this origin, no need to queue */
658 if (FilterByOrigin(ctx, XLogRecGetOrigin(r)))
661 change = ReorderBufferGetChange(ctx->reorder);
662 if (!(xlrec->flags & XLH_INSERT_IS_SPECULATIVE))
663 change->action = REORDER_BUFFER_CHANGE_INSERT;
665 change->action = REORDER_BUFFER_CHANGE_INTERNAL_SPEC_INSERT;
666 change->origin_id = XLogRecGetOrigin(r);
668 memcpy(&change->data.tp.relnode, &target_node, sizeof(RelFileNode));
670 if (xlrec->flags & XLH_INSERT_CONTAINS_NEW_TUPLE)
673 char *tupledata = XLogRecGetBlockData(r, 0, &datalen);
674 Size tuplelen = datalen - SizeOfHeapHeader;
676 change->data.tp.newtuple =
677 ReorderBufferGetTupleBuf(ctx->reorder, tuplelen);
679 DecodeXLogTuple(tupledata, datalen, change->data.tp.newtuple);
682 change->data.tp.clear_toast_afterwards = true;
684 ReorderBufferQueueChange(ctx->reorder, XLogRecGetXid(r), buf->origptr, change);
688 * Parse XLOG_HEAP_UPDATE and XLOG_HEAP_HOT_UPDATE, which have the same layout
689 * in the record, from wal into proper tuplebufs.
691 * Updates can possibly contain a new tuple and the old primary key.
694 DecodeUpdate(LogicalDecodingContext *ctx, XLogRecordBuffer *buf)
696 XLogReaderState *r = buf->record;
697 xl_heap_update *xlrec;
698 ReorderBufferChange *change;
700 RelFileNode target_node;
702 xlrec = (xl_heap_update *) XLogRecGetData(r);
704 /* only interested in our database */
705 XLogRecGetBlockTag(r, 0, &target_node, NULL, NULL);
706 if (target_node.dbNode != ctx->slot->data.database)
709 /* output plugin doesn't look for this origin, no need to queue */
710 if (FilterByOrigin(ctx, XLogRecGetOrigin(r)))
713 change = ReorderBufferGetChange(ctx->reorder);
714 change->action = REORDER_BUFFER_CHANGE_UPDATE;
715 change->origin_id = XLogRecGetOrigin(r);
716 memcpy(&change->data.tp.relnode, &target_node, sizeof(RelFileNode));
718 if (xlrec->flags & XLH_UPDATE_CONTAINS_NEW_TUPLE)
723 data = XLogRecGetBlockData(r, 0, &datalen);
725 tuplelen = datalen - SizeOfHeapHeader;
727 change->data.tp.newtuple =
728 ReorderBufferGetTupleBuf(ctx->reorder, tuplelen);
730 DecodeXLogTuple(data, datalen, change->data.tp.newtuple);
733 if (xlrec->flags & XLH_UPDATE_CONTAINS_OLD)
738 /* caution, remaining data in record is not aligned */
739 data = XLogRecGetData(r) + SizeOfHeapUpdate;
740 datalen = XLogRecGetDataLen(r) - SizeOfHeapUpdate;
741 tuplelen = datalen - SizeOfHeapHeader;
743 change->data.tp.oldtuple =
744 ReorderBufferGetTupleBuf(ctx->reorder, tuplelen);
746 DecodeXLogTuple(data, datalen, change->data.tp.oldtuple);
749 change->data.tp.clear_toast_afterwards = true;
751 ReorderBufferQueueChange(ctx->reorder, XLogRecGetXid(r), buf->origptr, change);
755 * Parse XLOG_HEAP_DELETE from wal into proper tuplebufs.
757 * Deletes can possibly contain the old primary key.
760 DecodeDelete(LogicalDecodingContext *ctx, XLogRecordBuffer *buf)
762 XLogReaderState *r = buf->record;
763 xl_heap_delete *xlrec;
764 ReorderBufferChange *change;
765 RelFileNode target_node;
767 xlrec = (xl_heap_delete *) XLogRecGetData(r);
769 /* only interested in our database */
770 XLogRecGetBlockTag(r, 0, &target_node, NULL, NULL);
771 if (target_node.dbNode != ctx->slot->data.database)
775 * Super deletions are irrelevant for logical decoding, it's driven by the
776 * confirmation records.
778 if (xlrec->flags & XLH_DELETE_IS_SUPER)
781 /* output plugin doesn't look for this origin, no need to queue */
782 if (FilterByOrigin(ctx, XLogRecGetOrigin(r)))
785 change = ReorderBufferGetChange(ctx->reorder);
786 change->action = REORDER_BUFFER_CHANGE_DELETE;
787 change->origin_id = XLogRecGetOrigin(r);
789 memcpy(&change->data.tp.relnode, &target_node, sizeof(RelFileNode));
791 /* old primary key stored */
792 if (xlrec->flags & XLH_DELETE_CONTAINS_OLD)
794 Size datalen = XLogRecGetDataLen(r) - SizeOfHeapDelete;
795 Size tuplelen = datalen - SizeOfHeapHeader;
797 Assert(XLogRecGetDataLen(r) > (SizeOfHeapDelete + SizeOfHeapHeader));
799 change->data.tp.oldtuple =
800 ReorderBufferGetTupleBuf(ctx->reorder, tuplelen);
802 DecodeXLogTuple((char *) xlrec + SizeOfHeapDelete,
803 datalen, change->data.tp.oldtuple);
806 change->data.tp.clear_toast_afterwards = true;
808 ReorderBufferQueueChange(ctx->reorder, XLogRecGetXid(r), buf->origptr, change);
812 * Decode XLOG_HEAP2_MULTI_INSERT_insert record into multiple tuplebufs.
814 * Currently MULTI_INSERT will always contain the full tuples.
817 DecodeMultiInsert(LogicalDecodingContext *ctx, XLogRecordBuffer *buf)
819 XLogReaderState *r = buf->record;
820 xl_heap_multi_insert *xlrec;
827 xlrec = (xl_heap_multi_insert *) XLogRecGetData(r);
829 /* only interested in our database */
830 XLogRecGetBlockTag(r, 0, &rnode, NULL, NULL);
831 if (rnode.dbNode != ctx->slot->data.database)
834 /* output plugin doesn't look for this origin, no need to queue */
835 if (FilterByOrigin(ctx, XLogRecGetOrigin(r)))
838 tupledata = XLogRecGetBlockData(r, 0, &tuplelen);
841 for (i = 0; i < xlrec->ntuples; i++)
843 ReorderBufferChange *change;
844 xl_multi_insert_tuple *xlhdr;
846 ReorderBufferTupleBuf *tuple;
848 change = ReorderBufferGetChange(ctx->reorder);
849 change->action = REORDER_BUFFER_CHANGE_INSERT;
850 change->origin_id = XLogRecGetOrigin(r);
852 memcpy(&change->data.tp.relnode, &rnode, sizeof(RelFileNode));
855 * CONTAINS_NEW_TUPLE will always be set currently as multi_insert
856 * isn't used for catalogs, but better be future proof.
858 * We decode the tuple in pretty much the same way as DecodeXLogTuple,
859 * but since the layout is slightly different, we can't use it here.
861 if (xlrec->flags & XLH_INSERT_CONTAINS_NEW_TUPLE)
863 HeapTupleHeader header;
865 xlhdr = (xl_multi_insert_tuple *) SHORTALIGN(data);
866 data = ((char *) xlhdr) + SizeOfMultiInsertTuple;
867 datalen = xlhdr->datalen;
869 change->data.tp.newtuple =
870 ReorderBufferGetTupleBuf(ctx->reorder, datalen);
872 tuple = change->data.tp.newtuple;
873 header = tuple->tuple.t_data;
875 /* not a disk based tuple */
876 ItemPointerSetInvalid(&tuple->tuple.t_self);
879 * We can only figure this out after reassembling the
882 tuple->tuple.t_tableOid = InvalidOid;
884 tuple->tuple.t_len = datalen + SizeofHeapTupleHeader;
886 memset(header, 0, SizeofHeapTupleHeader);
888 memcpy((char *) tuple->tuple.t_data + SizeofHeapTupleHeader,
893 header->t_infomask = xlhdr->t_infomask;
894 header->t_infomask2 = xlhdr->t_infomask2;
895 header->t_hoff = xlhdr->t_hoff;
899 * Reset toast reassembly state only after the last row in the last
900 * xl_multi_insert_tuple record emitted by one heap_multi_insert()
903 if (xlrec->flags & XLH_INSERT_LAST_IN_MULTI &&
904 (i + 1) == xlrec->ntuples)
905 change->data.tp.clear_toast_afterwards = true;
907 change->data.tp.clear_toast_afterwards = false;
909 ReorderBufferQueueChange(ctx->reorder, XLogRecGetXid(r),
910 buf->origptr, change);
912 Assert(data == tupledata + tuplelen);
916 * Parse XLOG_HEAP_CONFIRM from wal into a confirmation change.
918 * This is pretty trivial, all the state essentially already setup by the
919 * speculative insertion.
922 DecodeSpecConfirm(LogicalDecodingContext *ctx, XLogRecordBuffer *buf)
924 XLogReaderState *r = buf->record;
925 ReorderBufferChange *change;
926 RelFileNode target_node;
928 /* only interested in our database */
929 XLogRecGetBlockTag(r, 0, &target_node, NULL, NULL);
930 if (target_node.dbNode != ctx->slot->data.database)
933 /* output plugin doesn't look for this origin, no need to queue */
934 if (FilterByOrigin(ctx, XLogRecGetOrigin(r)))
937 change = ReorderBufferGetChange(ctx->reorder);
938 change->action = REORDER_BUFFER_CHANGE_INTERNAL_SPEC_CONFIRM;
939 change->origin_id = XLogRecGetOrigin(r);
941 memcpy(&change->data.tp.relnode, &target_node, sizeof(RelFileNode));
943 change->data.tp.clear_toast_afterwards = true;
945 ReorderBufferQueueChange(ctx->reorder, XLogRecGetXid(r), buf->origptr, change);
950 * Read a HeapTuple as WAL logged by heap_insert, heap_update and heap_delete
951 * (but not by heap_multi_insert) into a tuplebuf.
953 * The size 'len' and the pointer 'data' in the record need to be
954 * computed outside as they are record specific.
957 DecodeXLogTuple(char *data, Size len, ReorderBufferTupleBuf *tuple)
959 xl_heap_header xlhdr;
960 int datalen = len - SizeOfHeapHeader;
961 HeapTupleHeader header;
963 Assert(datalen >= 0);
965 tuple->tuple.t_len = datalen + SizeofHeapTupleHeader;
966 header = tuple->tuple.t_data;
968 /* not a disk based tuple */
969 ItemPointerSetInvalid(&tuple->tuple.t_self);
971 /* we can only figure this out after reassembling the transactions */
972 tuple->tuple.t_tableOid = InvalidOid;
974 /* data is not stored aligned, copy to aligned storage */
975 memcpy((char *) &xlhdr,
979 memset(header, 0, SizeofHeapTupleHeader);
981 memcpy(((char *) tuple->tuple.t_data) + SizeofHeapTupleHeader,
982 data + SizeOfHeapHeader,
985 header->t_infomask = xlhdr.t_infomask;
986 header->t_infomask2 = xlhdr.t_infomask2;
987 header->t_hoff = xlhdr.t_hoff;