1 /*-------------------------------------------------------------------------
4 * PostgreSQL logical replay/reorder buffer management
7 * Copyright (c) 2012-2016, PostgreSQL Global Development Group
11 * src/backend/replication/reorderbuffer.c
14 * This module gets handed individual pieces of transactions in the order
15 * they are written to the WAL and is responsible to reassemble them into
16 * toplevel transaction sized pieces. When a transaction is completely
17 * reassembled - signalled by reading the transaction commit record - it
18 * will then call the output plugin (c.f. ReorderBufferCommit()) with the
19 * individual changes. The output plugins rely on snapshots built by
20 * snapbuild.c which hands them to us.
22 * Transactions and subtransactions/savepoints in postgres are not
23 * immediately linked to each other from outside the performing
24 * backend. Only at commit/abort (or special xact_assignment records) they
25 * are linked together. Which means that we will have to splice together a
26 * toplevel transaction from its subtransactions. To do that efficiently we
27 * build a binary heap indexed by the smallest current lsn of the individual
28 * subtransactions' changestreams. As the individual streams are inherently
29 * ordered by LSN - since that is where we build them from - the transaction
30 * can easily be reassembled by always using the subtransaction with the
31 * smallest current LSN from the heap.
33 * In order to cope with large transactions - which can be several times as
34 * big as the available memory - this module supports spooling the contents
35 * of a large transactions to disk. When the transaction is replayed the
36 * contents of individual (sub-)transactions will be read from disk in
39 * This module also has to deal with reassembling toast records from the
40 * individual chunks stored in WAL. When a new (or initial) version of a
41 * tuple is stored in WAL it will always be preceded by the toast chunks
42 * emitted for the columns stored out of line. Within a single toplevel
43 * transaction there will be no other data carrying records between a row's
44 * toast chunks and the row data itself. See ReorderBufferToast* for
46 * -------------------------------------------------------------------------
53 #include "access/rewriteheap.h"
54 #include "access/transam.h"
55 #include "access/tuptoaster.h"
56 #include "access/xact.h"
57 #include "access/xlog_internal.h"
58 #include "catalog/catalog.h"
59 #include "lib/binaryheap.h"
60 #include "miscadmin.h"
61 #include "replication/logical.h"
62 #include "replication/reorderbuffer.h"
63 #include "replication/slot.h"
64 #include "replication/snapbuild.h" /* just for SnapBuildSnapDecRefcount */
65 #include "storage/bufmgr.h"
66 #include "storage/fd.h"
67 #include "storage/sinval.h"
68 #include "utils/builtins.h"
69 #include "utils/combocid.h"
70 #include "utils/memdebug.h"
71 #include "utils/memutils.h"
72 #include "utils/rel.h"
73 #include "utils/relfilenodemap.h"
74 #include "utils/tqual.h"
77 /* entry for a hash table we use to map from xid to our transaction state */
78 typedef struct ReorderBufferTXNByIdEnt
81 ReorderBufferTXN *txn;
82 } ReorderBufferTXNByIdEnt;
84 /* data structures for (relfilenode, ctid) => (cmin, cmax) mapping */
85 typedef struct ReorderBufferTupleCidKey
89 } ReorderBufferTupleCidKey;
91 typedef struct ReorderBufferTupleCidEnt
93 ReorderBufferTupleCidKey key;
96 CommandId combocid; /* just for debugging */
97 } ReorderBufferTupleCidEnt;
99 /* k-way in-order change iteration support structures */
100 typedef struct ReorderBufferIterTXNEntry
103 ReorderBufferChange *change;
104 ReorderBufferTXN *txn;
107 } ReorderBufferIterTXNEntry;
109 typedef struct ReorderBufferIterTXNState
113 dlist_head old_change;
114 ReorderBufferIterTXNEntry entries[FLEXIBLE_ARRAY_MEMBER];
115 } ReorderBufferIterTXNState;
117 /* toast datastructures */
118 typedef struct ReorderBufferToastEnt
120 Oid chunk_id; /* toast_table.chunk_id */
121 int32 last_chunk_seq; /* toast_table.chunk_seq of the last chunk we
123 Size num_chunks; /* number of chunks we've already seen */
124 Size size; /* combined size of chunks seen */
125 dlist_head chunks; /* linked list of chunks */
126 struct varlena *reconstructed; /* reconstructed varlena now pointed
128 } ReorderBufferToastEnt;
130 /* Disk serialization support datastructures */
131 typedef struct ReorderBufferDiskChange
134 ReorderBufferChange change;
136 } ReorderBufferDiskChange;
139 * Maximum number of changes kept in memory, per transaction. After that,
140 * changes are spooled to disk.
142 * The current value should be sufficient to decode the entire transaction
143 * without hitting disk in OLTP workloads, while starting to spool to disk in
144 * other workloads reasonably fast.
146 * At some point in the future it probably makes sense to have a more elaborate
147 * resource management here, but it's not entirely clear what that would look
150 static const Size max_changes_in_memory = 4096;
153 * We use a very simple form of a slab allocator for frequently allocated
154 * objects, simply keeping a fixed number in a linked list when unused,
155 * instead pfree()ing them. Without that in many workloads aset.c becomes a
156 * major bottleneck, especially when spilling to disk while decoding batch
159 static const Size max_cached_changes = 4096 * 2;
160 static const Size max_cached_tuplebufs = 4096 * 2; /* ~8MB */
161 static const Size max_cached_transactions = 512;
164 /* ---------------------------------------
165 * primary reorderbuffer support routines
166 * ---------------------------------------
168 static ReorderBufferTXN *ReorderBufferGetTXN(ReorderBuffer *rb);
169 static void ReorderBufferReturnTXN(ReorderBuffer *rb, ReorderBufferTXN *txn);
170 static ReorderBufferTXN *ReorderBufferTXNByXid(ReorderBuffer *rb,
171 TransactionId xid, bool create, bool *is_new,
172 XLogRecPtr lsn, bool create_as_top);
174 static void AssertTXNLsnOrder(ReorderBuffer *rb);
176 /* ---------------------------------------
177 * support functions for lsn-order iterating over the ->changes of a
178 * transaction and its subtransactions
180 * used for iteration over the k-way heap merge of a transaction and its
182 * ---------------------------------------
184 static ReorderBufferIterTXNState *ReorderBufferIterTXNInit(ReorderBuffer *rb, ReorderBufferTXN *txn);
185 static ReorderBufferChange *
186 ReorderBufferIterTXNNext(ReorderBuffer *rb, ReorderBufferIterTXNState *state);
187 static void ReorderBufferIterTXNFinish(ReorderBuffer *rb,
188 ReorderBufferIterTXNState *state);
189 static void ReorderBufferExecuteInvalidations(ReorderBuffer *rb, ReorderBufferTXN *txn);
192 * ---------------------------------------
193 * Disk serialization support functions
194 * ---------------------------------------
196 static void ReorderBufferCheckSerializeTXN(ReorderBuffer *rb, ReorderBufferTXN *txn);
197 static void ReorderBufferSerializeTXN(ReorderBuffer *rb, ReorderBufferTXN *txn);
198 static void ReorderBufferSerializeChange(ReorderBuffer *rb, ReorderBufferTXN *txn,
199 int fd, ReorderBufferChange *change);
200 static Size ReorderBufferRestoreChanges(ReorderBuffer *rb, ReorderBufferTXN *txn,
201 int *fd, XLogSegNo *segno);
202 static void ReorderBufferRestoreChange(ReorderBuffer *rb, ReorderBufferTXN *txn,
204 static void ReorderBufferRestoreCleanup(ReorderBuffer *rb, ReorderBufferTXN *txn);
206 static void ReorderBufferFreeSnap(ReorderBuffer *rb, Snapshot snap);
207 static Snapshot ReorderBufferCopySnap(ReorderBuffer *rb, Snapshot orig_snap,
208 ReorderBufferTXN *txn, CommandId cid);
210 /* ---------------------------------------
211 * toast reassembly support
212 * ---------------------------------------
214 static void ReorderBufferToastInitHash(ReorderBuffer *rb, ReorderBufferTXN *txn);
215 static void ReorderBufferToastReset(ReorderBuffer *rb, ReorderBufferTXN *txn);
216 static void ReorderBufferToastReplace(ReorderBuffer *rb, ReorderBufferTXN *txn,
217 Relation relation, ReorderBufferChange *change);
218 static void ReorderBufferToastAppendChunk(ReorderBuffer *rb, ReorderBufferTXN *txn,
219 Relation relation, ReorderBufferChange *change);
223 * Allocate a new ReorderBuffer
226 ReorderBufferAllocate(void)
228 ReorderBuffer *buffer;
230 MemoryContext new_ctx;
232 /* allocate memory in own context, to have better accountability */
233 new_ctx = AllocSetContextCreate(CurrentMemoryContext,
235 ALLOCSET_DEFAULT_MINSIZE,
236 ALLOCSET_DEFAULT_INITSIZE,
237 ALLOCSET_DEFAULT_MAXSIZE);
240 (ReorderBuffer *) MemoryContextAlloc(new_ctx, sizeof(ReorderBuffer));
242 memset(&hash_ctl, 0, sizeof(hash_ctl));
244 buffer->context = new_ctx;
246 hash_ctl.keysize = sizeof(TransactionId);
247 hash_ctl.entrysize = sizeof(ReorderBufferTXNByIdEnt);
248 hash_ctl.hcxt = buffer->context;
250 buffer->by_txn = hash_create("ReorderBufferByXid", 1000, &hash_ctl,
251 HASH_ELEM | HASH_BLOBS | HASH_CONTEXT);
253 buffer->by_txn_last_xid = InvalidTransactionId;
254 buffer->by_txn_last_txn = NULL;
256 buffer->nr_cached_transactions = 0;
257 buffer->nr_cached_changes = 0;
258 buffer->nr_cached_tuplebufs = 0;
260 buffer->outbuf = NULL;
261 buffer->outbufsize = 0;
263 buffer->current_restart_decoding_lsn = InvalidXLogRecPtr;
265 dlist_init(&buffer->toplevel_by_lsn);
266 dlist_init(&buffer->cached_transactions);
267 dlist_init(&buffer->cached_changes);
268 slist_init(&buffer->cached_tuplebufs);
274 * Free a ReorderBuffer
277 ReorderBufferFree(ReorderBuffer *rb)
279 MemoryContext context = rb->context;
282 * We free separately allocated data by entirely scrapping reorderbuffer's
285 MemoryContextDelete(context);
289 * Get an unused, possibly preallocated, ReorderBufferTXN.
291 static ReorderBufferTXN *
292 ReorderBufferGetTXN(ReorderBuffer *rb)
294 ReorderBufferTXN *txn;
296 /* check the slab cache */
297 if (rb->nr_cached_transactions > 0)
299 rb->nr_cached_transactions--;
300 txn = (ReorderBufferTXN *)
301 dlist_container(ReorderBufferTXN, node,
302 dlist_pop_head_node(&rb->cached_transactions));
306 txn = (ReorderBufferTXN *)
307 MemoryContextAlloc(rb->context, sizeof(ReorderBufferTXN));
310 memset(txn, 0, sizeof(ReorderBufferTXN));
312 dlist_init(&txn->changes);
313 dlist_init(&txn->tuplecids);
314 dlist_init(&txn->subtxns);
320 * Free a ReorderBufferTXN.
322 * Deallocation might be delayed for efficiency purposes, for details check
323 * the comments above max_cached_changes's definition.
326 ReorderBufferReturnTXN(ReorderBuffer *rb, ReorderBufferTXN *txn)
328 /* clean the lookup cache if we were cached (quite likely) */
329 if (rb->by_txn_last_xid == txn->xid)
331 rb->by_txn_last_xid = InvalidTransactionId;
332 rb->by_txn_last_txn = NULL;
335 /* free data that's contained */
337 if (txn->tuplecid_hash != NULL)
339 hash_destroy(txn->tuplecid_hash);
340 txn->tuplecid_hash = NULL;
343 if (txn->invalidations)
345 pfree(txn->invalidations);
346 txn->invalidations = NULL;
349 /* check whether to put into the slab cache */
350 if (rb->nr_cached_transactions < max_cached_transactions)
352 rb->nr_cached_transactions++;
353 dlist_push_head(&rb->cached_transactions, &txn->node);
354 VALGRIND_MAKE_MEM_UNDEFINED(txn, sizeof(ReorderBufferTXN));
355 VALGRIND_MAKE_MEM_DEFINED(&txn->node, sizeof(txn->node));
364 * Get an unused, possibly preallocated, ReorderBufferChange.
366 ReorderBufferChange *
367 ReorderBufferGetChange(ReorderBuffer *rb)
369 ReorderBufferChange *change;
371 /* check the slab cache */
372 if (rb->nr_cached_changes)
374 rb->nr_cached_changes--;
375 change = (ReorderBufferChange *)
376 dlist_container(ReorderBufferChange, node,
377 dlist_pop_head_node(&rb->cached_changes));
381 change = (ReorderBufferChange *)
382 MemoryContextAlloc(rb->context, sizeof(ReorderBufferChange));
385 memset(change, 0, sizeof(ReorderBufferChange));
390 * Free an ReorderBufferChange.
392 * Deallocation might be delayed for efficiency purposes, for details check
393 * the comments above max_cached_changes's definition.
396 ReorderBufferReturnChange(ReorderBuffer *rb, ReorderBufferChange *change)
398 /* free contained data */
399 switch (change->action)
401 case REORDER_BUFFER_CHANGE_INSERT:
402 case REORDER_BUFFER_CHANGE_UPDATE:
403 case REORDER_BUFFER_CHANGE_DELETE:
404 case REORDER_BUFFER_CHANGE_INTERNAL_SPEC_INSERT:
405 if (change->data.tp.newtuple)
407 ReorderBufferReturnTupleBuf(rb, change->data.tp.newtuple);
408 change->data.tp.newtuple = NULL;
411 if (change->data.tp.oldtuple)
413 ReorderBufferReturnTupleBuf(rb, change->data.tp.oldtuple);
414 change->data.tp.oldtuple = NULL;
417 case REORDER_BUFFER_CHANGE_INTERNAL_SNAPSHOT:
418 if (change->data.snapshot)
420 ReorderBufferFreeSnap(rb, change->data.snapshot);
421 change->data.snapshot = NULL;
424 /* no data in addition to the struct itself */
425 case REORDER_BUFFER_CHANGE_INTERNAL_SPEC_CONFIRM:
426 case REORDER_BUFFER_CHANGE_INTERNAL_COMMAND_ID:
427 case REORDER_BUFFER_CHANGE_INTERNAL_TUPLECID:
431 /* check whether to put into the slab cache */
432 if (rb->nr_cached_changes < max_cached_changes)
434 rb->nr_cached_changes++;
435 dlist_push_head(&rb->cached_changes, &change->node);
436 VALGRIND_MAKE_MEM_UNDEFINED(change, sizeof(ReorderBufferChange));
437 VALGRIND_MAKE_MEM_DEFINED(&change->node, sizeof(change->node));
447 * Get an unused, possibly preallocated, ReorderBufferTupleBuf fitting at
448 * least a tuple of size tuple_len (excluding header overhead).
450 ReorderBufferTupleBuf *
451 ReorderBufferGetTupleBuf(ReorderBuffer *rb, Size tuple_len)
453 ReorderBufferTupleBuf *tuple;
456 alloc_len = tuple_len + SizeofHeapTupleHeader;
459 * Most tuples are below MaxHeapTupleSize, so we use a slab allocator for
460 * those. Thus always allocate at least MaxHeapTupleSize. Note that tuples
461 * tuples generated for oldtuples can be bigger, as they don't have
462 * out-of-line toast columns.
464 if (alloc_len < MaxHeapTupleSize)
465 alloc_len = MaxHeapTupleSize;
468 /* if small enough, check the slab cache */
469 if (alloc_len <= MaxHeapTupleSize && rb->nr_cached_tuplebufs)
471 rb->nr_cached_tuplebufs--;
472 tuple = slist_container(ReorderBufferTupleBuf, node,
473 slist_pop_head_node(&rb->cached_tuplebufs));
474 #ifdef USE_ASSERT_CHECKING
475 memset(&tuple->tuple, 0xa9, sizeof(HeapTupleData));
477 tuple->tuple.t_data = ReorderBufferTupleBufData(tuple);
478 #ifdef USE_ASSERT_CHECKING
479 memset(tuple->tuple.t_data, 0xa8, tuple->alloc_tuple_size);
484 tuple = (ReorderBufferTupleBuf *)
485 MemoryContextAlloc(rb->context,
486 sizeof(ReorderBufferTupleBuf) + alloc_len);
487 tuple->alloc_tuple_size = alloc_len;
488 tuple->tuple.t_data = ReorderBufferTupleBufData(tuple);
495 * Free an ReorderBufferTupleBuf.
497 * Deallocation might be delayed for efficiency purposes, for details check
498 * the comments above max_cached_changes's definition.
501 ReorderBufferReturnTupleBuf(ReorderBuffer *rb, ReorderBufferTupleBuf *tuple)
503 /* check whether to put into the slab cache, oversized tuples never are */
504 if (tuple->alloc_tuple_size == MaxHeapTupleSize &&
505 rb->nr_cached_tuplebufs < max_cached_tuplebufs)
507 rb->nr_cached_tuplebufs++;
508 slist_push_head(&rb->cached_tuplebufs, &tuple->node);
509 VALGRIND_MAKE_MEM_UNDEFINED(tuple->tuple.t_data, tuple->alloc_tuple_size);
510 VALGRIND_MAKE_MEM_UNDEFINED(tuple, sizeof(ReorderBufferTupleBuf));
511 VALGRIND_MAKE_MEM_DEFINED(&tuple->node, sizeof(tuple->node));
512 VALGRIND_MAKE_MEM_DEFINED(&tuple->alloc_tuple_size, sizeof(tuple->alloc_tuple_size));
521 * Return the ReorderBufferTXN from the given buffer, specified by Xid.
522 * If create is true, and a transaction doesn't already exist, create it
523 * (with the given LSN, and as top transaction if that's specified);
524 * when this happens, is_new is set to true.
526 static ReorderBufferTXN *
527 ReorderBufferTXNByXid(ReorderBuffer *rb, TransactionId xid, bool create,
528 bool *is_new, XLogRecPtr lsn, bool create_as_top)
530 ReorderBufferTXN *txn;
531 ReorderBufferTXNByIdEnt *ent;
534 Assert(TransactionIdIsValid(xid));
535 Assert(!create || lsn != InvalidXLogRecPtr);
538 * Check the one-entry lookup cache first
540 if (TransactionIdIsValid(rb->by_txn_last_xid) &&
541 rb->by_txn_last_xid == xid)
543 txn = rb->by_txn_last_txn;
547 /* found it, and it's valid */
554 * cached as non-existant, and asked not to create? Then nothing else
559 /* otherwise fall through to create it */
563 * If the cache wasn't hit or it yielded an "does-not-exist" and we want
564 * to create an entry.
567 /* search the lookup table */
568 ent = (ReorderBufferTXNByIdEnt *)
569 hash_search(rb->by_txn,
571 create ? HASH_ENTER : HASH_FIND,
577 /* initialize the new entry, if creation was requested */
580 ent->txn = ReorderBufferGetTXN(rb);
583 txn->first_lsn = lsn;
584 txn->restart_decoding_lsn = rb->current_restart_decoding_lsn;
588 dlist_push_tail(&rb->toplevel_by_lsn, &txn->node);
589 AssertTXNLsnOrder(rb);
593 txn = NULL; /* not found and not asked to create */
596 rb->by_txn_last_xid = xid;
597 rb->by_txn_last_txn = txn;
602 Assert(!create || !!txn);
607 * Queue a change into a transaction so it can be replayed upon commit.
610 ReorderBufferQueueChange(ReorderBuffer *rb, TransactionId xid, XLogRecPtr lsn,
611 ReorderBufferChange *change)
613 ReorderBufferTXN *txn;
615 txn = ReorderBufferTXNByXid(rb, xid, true, NULL, lsn, true);
618 Assert(InvalidXLogRecPtr != lsn);
619 dlist_push_tail(&txn->changes, &change->node);
623 ReorderBufferCheckSerializeTXN(rb, txn);
627 AssertTXNLsnOrder(ReorderBuffer *rb)
629 #ifdef USE_ASSERT_CHECKING
631 XLogRecPtr prev_first_lsn = InvalidXLogRecPtr;
633 dlist_foreach(iter, &rb->toplevel_by_lsn)
635 ReorderBufferTXN *cur_txn;
637 cur_txn = dlist_container(ReorderBufferTXN, node, iter.cur);
638 Assert(cur_txn->first_lsn != InvalidXLogRecPtr);
640 if (cur_txn->end_lsn != InvalidXLogRecPtr)
641 Assert(cur_txn->first_lsn <= cur_txn->end_lsn);
643 if (prev_first_lsn != InvalidXLogRecPtr)
644 Assert(prev_first_lsn < cur_txn->first_lsn);
646 Assert(!cur_txn->is_known_as_subxact);
647 prev_first_lsn = cur_txn->first_lsn;
653 ReorderBufferGetOldestTXN(ReorderBuffer *rb)
655 ReorderBufferTXN *txn;
657 if (dlist_is_empty(&rb->toplevel_by_lsn))
660 AssertTXNLsnOrder(rb);
662 txn = dlist_head_element(ReorderBufferTXN, node, &rb->toplevel_by_lsn);
664 Assert(!txn->is_known_as_subxact);
665 Assert(txn->first_lsn != InvalidXLogRecPtr);
670 ReorderBufferSetRestartPoint(ReorderBuffer *rb, XLogRecPtr ptr)
672 rb->current_restart_decoding_lsn = ptr;
676 ReorderBufferAssignChild(ReorderBuffer *rb, TransactionId xid,
677 TransactionId subxid, XLogRecPtr lsn)
679 ReorderBufferTXN *txn;
680 ReorderBufferTXN *subtxn;
684 txn = ReorderBufferTXNByXid(rb, xid, true, &new_top, lsn, true);
685 subtxn = ReorderBufferTXNByXid(rb, subxid, true, &new_sub, lsn, false);
690 * we assign subtransactions to top level transaction even if we don't
691 * have data for it yet, assignment records frequently reference xids
692 * that have not yet produced any records. Knowing those aren't top
693 * level xids allows us to make processing cheaper in some places.
695 dlist_push_tail(&txn->subtxns, &subtxn->node);
698 else if (!subtxn->is_known_as_subxact)
700 subtxn->is_known_as_subxact = true;
701 Assert(subtxn->nsubtxns == 0);
703 /* remove from lsn order list of top-level transactions */
704 dlist_delete(&subtxn->node);
706 /* add to toplevel transaction */
707 dlist_push_tail(&txn->subtxns, &subtxn->node);
712 elog(ERROR, "existing subxact assigned to unknown toplevel xact");
717 * Associate a subtransaction with its toplevel transaction at commit
718 * time. There may be no further changes added after this.
721 ReorderBufferCommitChild(ReorderBuffer *rb, TransactionId xid,
722 TransactionId subxid, XLogRecPtr commit_lsn,
725 ReorderBufferTXN *txn;
726 ReorderBufferTXN *subtxn;
728 subtxn = ReorderBufferTXNByXid(rb, subxid, false, NULL,
729 InvalidXLogRecPtr, false);
732 * No need to do anything if that subtxn didn't contain any changes
737 txn = ReorderBufferTXNByXid(rb, xid, false, NULL, commit_lsn, true);
740 elog(ERROR, "subxact logged without previous toplevel record");
743 * Pass the our base snapshot to the parent transaction if it doesn't have
744 * one, or ours is older. That can happen if there are no changes in the
745 * toplevel transaction but in one of the child transactions. This allows
746 * the parent to simply use it's base snapshot initially.
748 if (txn->base_snapshot == NULL ||
749 txn->base_snapshot_lsn > subtxn->base_snapshot_lsn)
751 txn->base_snapshot = subtxn->base_snapshot;
752 txn->base_snapshot_lsn = subtxn->base_snapshot_lsn;
753 subtxn->base_snapshot = NULL;
754 subtxn->base_snapshot_lsn = InvalidXLogRecPtr;
757 subtxn->final_lsn = commit_lsn;
758 subtxn->end_lsn = end_lsn;
760 if (!subtxn->is_known_as_subxact)
762 subtxn->is_known_as_subxact = true;
763 Assert(subtxn->nsubtxns == 0);
765 /* remove from lsn order list of top-level transactions */
766 dlist_delete(&subtxn->node);
768 /* add to subtransaction list */
769 dlist_push_tail(&txn->subtxns, &subtxn->node);
776 * Support for efficiently iterating over a transaction's and its
777 * subtransactions' changes.
779 * We do by doing a k-way merge between transactions/subtransactions. For that
780 * we model the current heads of the different transactions as a binary heap
781 * so we easily know which (sub-)transaction has the change with the smallest
784 * We assume the changes in individual transactions are already sorted by LSN.
788 * Binary heap comparison function.
791 ReorderBufferIterCompare(Datum a, Datum b, void *arg)
793 ReorderBufferIterTXNState *state = (ReorderBufferIterTXNState *) arg;
794 XLogRecPtr pos_a = state->entries[DatumGetInt32(a)].lsn;
795 XLogRecPtr pos_b = state->entries[DatumGetInt32(b)].lsn;
799 else if (pos_a == pos_b)
805 * Allocate & initialize an iterator which iterates in lsn order over a
806 * transaction and all its subtransactions.
808 static ReorderBufferIterTXNState *
809 ReorderBufferIterTXNInit(ReorderBuffer *rb, ReorderBufferTXN *txn)
812 ReorderBufferIterTXNState *state;
813 dlist_iter cur_txn_i;
817 * Calculate the size of our heap: one element for every transaction that
818 * contains changes. (Besides the transactions already in the reorder
819 * buffer, we count the one we were directly passed.)
821 if (txn->nentries > 0)
824 dlist_foreach(cur_txn_i, &txn->subtxns)
826 ReorderBufferTXN *cur_txn;
828 cur_txn = dlist_container(ReorderBufferTXN, node, cur_txn_i.cur);
830 if (cur_txn->nentries > 0)
835 * TODO: Consider adding fastpath for the rather common nr_txns=1 case, no
836 * need to allocate/build a heap then.
839 /* allocate iteration state */
840 state = (ReorderBufferIterTXNState *)
841 MemoryContextAllocZero(rb->context,
842 sizeof(ReorderBufferIterTXNState) +
843 sizeof(ReorderBufferIterTXNEntry) * nr_txns);
845 state->nr_txns = nr_txns;
846 dlist_init(&state->old_change);
848 for (off = 0; off < state->nr_txns; off++)
850 state->entries[off].fd = -1;
851 state->entries[off].segno = 0;
855 state->heap = binaryheap_allocate(state->nr_txns,
856 ReorderBufferIterCompare,
860 * Now insert items into the binary heap, in an unordered fashion. (We
861 * will run a heap assembly step at the end; this is more efficient.)
866 /* add toplevel transaction if it contains changes */
867 if (txn->nentries > 0)
869 ReorderBufferChange *cur_change;
871 if (txn->nentries != txn->nentries_mem)
872 ReorderBufferRestoreChanges(rb, txn, &state->entries[off].fd,
873 &state->entries[off].segno);
875 cur_change = dlist_head_element(ReorderBufferChange, node,
878 state->entries[off].lsn = cur_change->lsn;
879 state->entries[off].change = cur_change;
880 state->entries[off].txn = txn;
882 binaryheap_add_unordered(state->heap, Int32GetDatum(off++));
885 /* add subtransactions if they contain changes */
886 dlist_foreach(cur_txn_i, &txn->subtxns)
888 ReorderBufferTXN *cur_txn;
890 cur_txn = dlist_container(ReorderBufferTXN, node, cur_txn_i.cur);
892 if (cur_txn->nentries > 0)
894 ReorderBufferChange *cur_change;
896 if (txn->nentries != txn->nentries_mem)
897 ReorderBufferRestoreChanges(rb, cur_txn,
898 &state->entries[off].fd,
899 &state->entries[off].segno);
901 cur_change = dlist_head_element(ReorderBufferChange, node,
904 state->entries[off].lsn = cur_change->lsn;
905 state->entries[off].change = cur_change;
906 state->entries[off].txn = cur_txn;
908 binaryheap_add_unordered(state->heap, Int32GetDatum(off++));
912 /* assemble a valid binary heap */
913 binaryheap_build(state->heap);
919 * Return the next change when iterating over a transaction and its
922 * Returns NULL when no further changes exist.
924 static ReorderBufferChange *
925 ReorderBufferIterTXNNext(ReorderBuffer *rb, ReorderBufferIterTXNState *state)
927 ReorderBufferChange *change;
928 ReorderBufferIterTXNEntry *entry;
931 /* nothing there anymore */
932 if (state->heap->bh_size == 0)
935 off = DatumGetInt32(binaryheap_first(state->heap));
936 entry = &state->entries[off];
938 /* free memory we might have "leaked" in the previous *Next call */
939 if (!dlist_is_empty(&state->old_change))
941 change = dlist_container(ReorderBufferChange, node,
942 dlist_pop_head_node(&state->old_change));
943 ReorderBufferReturnChange(rb, change);
944 Assert(dlist_is_empty(&state->old_change));
947 change = entry->change;
950 * update heap with information about which transaction has the next
951 * relevant change in LSN order
954 /* there are in-memory changes */
955 if (dlist_has_next(&entry->txn->changes, &entry->change->node))
957 dlist_node *next = dlist_next_node(&entry->txn->changes, &change->node);
958 ReorderBufferChange *next_change =
959 dlist_container(ReorderBufferChange, node, next);
961 /* txn stays the same */
962 state->entries[off].lsn = next_change->lsn;
963 state->entries[off].change = next_change;
965 binaryheap_replace_first(state->heap, Int32GetDatum(off));
969 /* try to load changes from disk */
970 if (entry->txn->nentries != entry->txn->nentries_mem)
973 * Ugly: restoring changes will reuse *Change records, thus delete the
974 * current one from the per-tx list and only free in the next call.
976 dlist_delete(&change->node);
977 dlist_push_tail(&state->old_change, &change->node);
979 if (ReorderBufferRestoreChanges(rb, entry->txn, &entry->fd,
980 &state->entries[off].segno))
982 /* successfully restored changes from disk */
983 ReorderBufferChange *next_change =
984 dlist_head_element(ReorderBufferChange, node,
985 &entry->txn->changes);
987 elog(DEBUG2, "restored %u/%u changes from disk",
988 (uint32) entry->txn->nentries_mem,
989 (uint32) entry->txn->nentries);
991 Assert(entry->txn->nentries_mem);
992 /* txn stays the same */
993 state->entries[off].lsn = next_change->lsn;
994 state->entries[off].change = next_change;
995 binaryheap_replace_first(state->heap, Int32GetDatum(off));
1001 /* ok, no changes there anymore, remove */
1002 binaryheap_remove_first(state->heap);
1008 * Deallocate the iterator
1011 ReorderBufferIterTXNFinish(ReorderBuffer *rb,
1012 ReorderBufferIterTXNState *state)
1016 for (off = 0; off < state->nr_txns; off++)
1018 if (state->entries[off].fd != -1)
1019 CloseTransientFile(state->entries[off].fd);
1022 /* free memory we might have "leaked" in the last *Next call */
1023 if (!dlist_is_empty(&state->old_change))
1025 ReorderBufferChange *change;
1027 change = dlist_container(ReorderBufferChange, node,
1028 dlist_pop_head_node(&state->old_change));
1029 ReorderBufferReturnChange(rb, change);
1030 Assert(dlist_is_empty(&state->old_change));
1033 binaryheap_free(state->heap);
1038 * Cleanup the contents of a transaction, usually after the transaction
1039 * committed or aborted.
1042 ReorderBufferCleanupTXN(ReorderBuffer *rb, ReorderBufferTXN *txn)
1045 dlist_mutable_iter iter;
1047 /* cleanup subtransactions & their changes */
1048 dlist_foreach_modify(iter, &txn->subtxns)
1050 ReorderBufferTXN *subtxn;
1052 subtxn = dlist_container(ReorderBufferTXN, node, iter.cur);
1055 * Subtransactions are always associated to the toplevel TXN, even if
1056 * they originally were happening inside another subtxn, so we won't
1057 * ever recurse more than one level deep here.
1059 Assert(subtxn->is_known_as_subxact);
1060 Assert(subtxn->nsubtxns == 0);
1062 ReorderBufferCleanupTXN(rb, subtxn);
1065 /* cleanup changes in the toplevel txn */
1066 dlist_foreach_modify(iter, &txn->changes)
1068 ReorderBufferChange *change;
1070 change = dlist_container(ReorderBufferChange, node, iter.cur);
1072 ReorderBufferReturnChange(rb, change);
1076 * Cleanup the tuplecids we stored for decoding catalog snapshot access.
1077 * They are always stored in the toplevel transaction.
1079 dlist_foreach_modify(iter, &txn->tuplecids)
1081 ReorderBufferChange *change;
1083 change = dlist_container(ReorderBufferChange, node, iter.cur);
1084 Assert(change->action == REORDER_BUFFER_CHANGE_INTERNAL_TUPLECID);
1085 ReorderBufferReturnChange(rb, change);
1088 if (txn->base_snapshot != NULL)
1090 SnapBuildSnapDecRefcount(txn->base_snapshot);
1091 txn->base_snapshot = NULL;
1092 txn->base_snapshot_lsn = InvalidXLogRecPtr;
1095 /* delete from list of known subxacts */
1096 if (txn->is_known_as_subxact)
1098 /* NB: nsubxacts count of parent will be too high now */
1099 dlist_delete(&txn->node);
1101 /* delete from LSN ordered list of toplevel TXNs */
1104 dlist_delete(&txn->node);
1107 /* now remove reference from buffer */
1108 hash_search(rb->by_txn,
1114 /* remove entries spilled to disk */
1115 if (txn->nentries != txn->nentries_mem)
1116 ReorderBufferRestoreCleanup(rb, txn);
1119 ReorderBufferReturnTXN(rb, txn);
1123 * Build a hash with a (relfilenode, ctid) -> (cmin, cmax) mapping for use by
1124 * tqual.c's HeapTupleSatisfiesHistoricMVCC.
1127 ReorderBufferBuildTupleCidHash(ReorderBuffer *rb, ReorderBufferTXN *txn)
1132 if (!txn->has_catalog_changes || dlist_is_empty(&txn->tuplecids))
1135 memset(&hash_ctl, 0, sizeof(hash_ctl));
1137 hash_ctl.keysize = sizeof(ReorderBufferTupleCidKey);
1138 hash_ctl.entrysize = sizeof(ReorderBufferTupleCidEnt);
1139 hash_ctl.hcxt = rb->context;
1142 * create the hash with the exact number of to-be-stored tuplecids from
1145 txn->tuplecid_hash =
1146 hash_create("ReorderBufferTupleCid", txn->ntuplecids, &hash_ctl,
1147 HASH_ELEM | HASH_BLOBS | HASH_CONTEXT);
1149 dlist_foreach(iter, &txn->tuplecids)
1151 ReorderBufferTupleCidKey key;
1152 ReorderBufferTupleCidEnt *ent;
1154 ReorderBufferChange *change;
1156 change = dlist_container(ReorderBufferChange, node, iter.cur);
1158 Assert(change->action == REORDER_BUFFER_CHANGE_INTERNAL_TUPLECID);
1160 /* be careful about padding */
1161 memset(&key, 0, sizeof(ReorderBufferTupleCidKey));
1163 key.relnode = change->data.tuplecid.node;
1165 ItemPointerCopy(&change->data.tuplecid.tid,
1168 ent = (ReorderBufferTupleCidEnt *)
1169 hash_search(txn->tuplecid_hash,
1171 HASH_ENTER | HASH_FIND,
1175 ent->cmin = change->data.tuplecid.cmin;
1176 ent->cmax = change->data.tuplecid.cmax;
1177 ent->combocid = change->data.tuplecid.combocid;
1181 Assert(ent->cmin == change->data.tuplecid.cmin);
1182 Assert(ent->cmax == InvalidCommandId ||
1183 ent->cmax == change->data.tuplecid.cmax);
1186 * if the tuple got valid in this transaction and now got deleted
1187 * we already have a valid cmin stored. The cmax will be
1188 * InvalidCommandId though.
1190 ent->cmax = change->data.tuplecid.cmax;
1196 * Copy a provided snapshot so we can modify it privately. This is needed so
1197 * that catalog modifying transactions can look into intermediate catalog
1201 ReorderBufferCopySnap(ReorderBuffer *rb, Snapshot orig_snap,
1202 ReorderBufferTXN *txn, CommandId cid)
1209 size = sizeof(SnapshotData) +
1210 sizeof(TransactionId) * orig_snap->xcnt +
1211 sizeof(TransactionId) * (txn->nsubtxns + 1);
1213 snap = MemoryContextAllocZero(rb->context, size);
1214 memcpy(snap, orig_snap, sizeof(SnapshotData));
1216 snap->copied = true;
1217 snap->active_count = 1; /* mark as active so nobody frees it */
1218 snap->regd_count = 0;
1219 snap->xip = (TransactionId *) (snap + 1);
1221 memcpy(snap->xip, orig_snap->xip, sizeof(TransactionId) * snap->xcnt);
1224 * snap->subxip contains all txids that belong to our transaction which we
1225 * need to check via cmin/cmax. Thats why we store the toplevel
1226 * transaction in there as well.
1228 snap->subxip = snap->xip + snap->xcnt;
1229 snap->subxip[i++] = txn->xid;
1232 * nsubxcnt isn't decreased when subtransactions abort, so count manually.
1233 * Since it's an upper boundary it is safe to use it for the allocation
1238 dlist_foreach(iter, &txn->subtxns)
1240 ReorderBufferTXN *sub_txn;
1242 sub_txn = dlist_container(ReorderBufferTXN, node, iter.cur);
1243 snap->subxip[i++] = sub_txn->xid;
1247 /* sort so we can bsearch() later */
1248 qsort(snap->subxip, snap->subxcnt, sizeof(TransactionId), xidComparator);
1250 /* store the specified current CommandId */
1257 * Free a previously ReorderBufferCopySnap'ed snapshot
1260 ReorderBufferFreeSnap(ReorderBuffer *rb, Snapshot snap)
1265 SnapBuildSnapDecRefcount(snap);
1269 * Perform the replay of a transaction and it's non-aborted subtransactions.
1271 * Subtransactions previously have to be processed by
1272 * ReorderBufferCommitChild(), even if previously assigned to the toplevel
1273 * transaction with ReorderBufferAssignChild.
1275 * We currently can only decode a transaction's contents in when their commit
1276 * record is read because that's currently the only place where we know about
1277 * cache invalidations. Thus, once a toplevel commit is read, we iterate over
1278 * the top and subtransactions (using a k-way merge) and replay the changes in
1282 ReorderBufferCommit(ReorderBuffer *rb, TransactionId xid,
1283 XLogRecPtr commit_lsn, XLogRecPtr end_lsn,
1284 TimestampTz commit_time,
1285 RepOriginId origin_id, XLogRecPtr origin_lsn)
1287 ReorderBufferTXN *txn;
1288 volatile Snapshot snapshot_now;
1289 volatile CommandId command_id = FirstCommandId;
1291 ReorderBufferIterTXNState *volatile iterstate = NULL;
1293 txn = ReorderBufferTXNByXid(rb, xid, false, NULL, InvalidXLogRecPtr,
1296 /* unknown transaction, nothing to replay */
1300 txn->final_lsn = commit_lsn;
1301 txn->end_lsn = end_lsn;
1302 txn->commit_time = commit_time;
1303 txn->origin_id = origin_id;
1304 txn->origin_lsn = origin_lsn;
1306 /* serialize the last bunch of changes if we need start earlier anyway */
1307 if (txn->nentries_mem != txn->nentries)
1308 ReorderBufferSerializeTXN(rb, txn);
1311 * If this transaction didn't have any real changes in our database, it's
1312 * OK not to have a snapshot. Note that ReorderBufferCommitChild will have
1313 * transferred its snapshot to this transaction if it had one and the
1314 * toplevel tx didn't.
1316 if (txn->base_snapshot == NULL)
1318 Assert(txn->ninvalidations == 0);
1319 ReorderBufferCleanupTXN(rb, txn);
1323 snapshot_now = txn->base_snapshot;
1325 /* build data to be able to lookup the CommandIds of catalog tuples */
1326 ReorderBufferBuildTupleCidHash(rb, txn);
1328 /* setup the initial snapshot */
1329 SetupHistoricSnapshot(snapshot_now, txn->tuplecid_hash);
1332 * Decoding needs access to syscaches et al., which in turn use
1333 * heavyweight locks and such. Thus we need to have enough state around to
1334 * keep track of those. The easiest way is to simply use a transaction
1335 * internally. That also allows us to easily enforce that nothing writes
1336 * to the database by checking for xid assignments.
1338 * When we're called via the SQL SRF there's already a transaction
1339 * started, so start an explicit subtransaction there.
1341 using_subtxn = IsTransactionOrTransactionBlock();
1345 ReorderBufferChange *change;
1346 ReorderBufferChange *specinsert = NULL;
1349 BeginInternalSubTransaction("replay");
1351 StartTransactionCommand();
1355 iterstate = ReorderBufferIterTXNInit(rb, txn);
1356 while ((change = ReorderBufferIterTXNNext(rb, iterstate)) != NULL)
1358 Relation relation = NULL;
1361 switch (change->action)
1363 case REORDER_BUFFER_CHANGE_INTERNAL_SPEC_CONFIRM:
1366 * Confirmation for speculative insertion arrived. Simply
1367 * use as a normal record. It'll be cleaned up at the end
1368 * of INSERT processing.
1370 Assert(specinsert->data.tp.oldtuple == NULL);
1371 change = specinsert;
1372 change->action = REORDER_BUFFER_CHANGE_INSERT;
1374 /* intentionally fall through */
1375 case REORDER_BUFFER_CHANGE_INSERT:
1376 case REORDER_BUFFER_CHANGE_UPDATE:
1377 case REORDER_BUFFER_CHANGE_DELETE:
1378 Assert(snapshot_now);
1380 reloid = RelidByRelfilenode(change->data.tp.relnode.spcNode,
1381 change->data.tp.relnode.relNode);
1384 * Catalog tuple without data, emitted while catalog was
1385 * in the process of being rewritten.
1387 if (reloid == InvalidOid &&
1388 change->data.tp.newtuple == NULL &&
1389 change->data.tp.oldtuple == NULL)
1391 else if (reloid == InvalidOid)
1392 elog(ERROR, "could not map filenode \"%s\" to relation OID",
1393 relpathperm(change->data.tp.relnode,
1396 relation = RelationIdGetRelation(reloid);
1398 if (relation == NULL)
1399 elog(ERROR, "could not open relation with OID %u (for filenode \"%s\")",
1401 relpathperm(change->data.tp.relnode,
1404 if (!RelationIsLogicallyLogged(relation))
1408 * For now ignore sequence changes entirely. Most of the
1409 * time they don't log changes using records we
1410 * understand, so it doesn't make sense to handle the few
1413 if (relation->rd_rel->relkind == RELKIND_SEQUENCE)
1416 /* user-triggered change */
1417 if (!IsToastRelation(relation))
1419 ReorderBufferToastReplace(rb, txn, relation, change);
1420 rb->apply_change(rb, txn, relation, change);
1423 * Only clear reassembled toast chunks if we're sure
1424 * they're not required anymore. The creator of the
1427 if (change->data.tp.clear_toast_afterwards)
1428 ReorderBufferToastReset(rb, txn);
1430 /* we're not interested in toast deletions */
1431 else if (change->action == REORDER_BUFFER_CHANGE_INSERT)
1434 * Need to reassemble the full toasted Datum in
1435 * memory, to ensure the chunks don't get reused till
1436 * we're done remove it from the list of this
1437 * transaction's changes. Otherwise it will get
1438 * freed/reused while restoring spooled data from
1441 dlist_delete(&change->node);
1442 ReorderBufferToastAppendChunk(rb, txn, relation,
1449 * Either speculative insertion was confirmed, or it was
1450 * unsuccessful and the record isn't needed anymore.
1452 if (specinsert != NULL)
1454 ReorderBufferReturnChange(rb, specinsert);
1458 if (relation != NULL)
1460 RelationClose(relation);
1465 case REORDER_BUFFER_CHANGE_INTERNAL_SPEC_INSERT:
1468 * Speculative insertions are dealt with by delaying the
1469 * processing of the insert until the confirmation record
1470 * arrives. For that we simply unlink the record from the
1471 * chain, so it does not get freed/reused while restoring
1472 * spooled data from disk.
1474 * This is safe in the face of concurrent catalog changes
1475 * because the relevant relation can't be changed between
1476 * speculative insertion and confirmation due to
1477 * CheckTableNotInUse() and locking.
1480 /* clear out a pending (and thus failed) speculation */
1481 if (specinsert != NULL)
1483 ReorderBufferReturnChange(rb, specinsert);
1487 /* and memorize the pending insertion */
1488 dlist_delete(&change->node);
1489 specinsert = change;
1492 case REORDER_BUFFER_CHANGE_INTERNAL_SNAPSHOT:
1493 /* get rid of the old */
1494 TeardownHistoricSnapshot(false);
1496 if (snapshot_now->copied)
1498 ReorderBufferFreeSnap(rb, snapshot_now);
1500 ReorderBufferCopySnap(rb, change->data.snapshot,
1505 * Restored from disk, need to be careful not to double
1506 * free. We could introduce refcounting for that, but for
1507 * now this seems infrequent enough not to care.
1509 else if (change->data.snapshot->copied)
1512 ReorderBufferCopySnap(rb, change->data.snapshot,
1517 snapshot_now = change->data.snapshot;
1521 /* and continue with the new one */
1522 SetupHistoricSnapshot(snapshot_now, txn->tuplecid_hash);
1525 case REORDER_BUFFER_CHANGE_INTERNAL_COMMAND_ID:
1526 Assert(change->data.command_id != InvalidCommandId);
1528 if (command_id < change->data.command_id)
1530 command_id = change->data.command_id;
1532 if (!snapshot_now->copied)
1534 /* we don't use the global one anymore */
1535 snapshot_now = ReorderBufferCopySnap(rb, snapshot_now,
1539 snapshot_now->curcid = command_id;
1541 TeardownHistoricSnapshot(false);
1542 SetupHistoricSnapshot(snapshot_now, txn->tuplecid_hash);
1545 * Every time the CommandId is incremented, we could
1546 * see new catalog contents, so execute all
1549 ReorderBufferExecuteInvalidations(rb, txn);
1554 case REORDER_BUFFER_CHANGE_INTERNAL_TUPLECID:
1555 elog(ERROR, "tuplecid value in changequeue");
1561 * There's a speculative insertion remaining, just clean in up, it
1562 * can't have been successful, otherwise we'd gotten a confirmation
1567 ReorderBufferReturnChange(rb, specinsert);
1571 /* clean up the iterator */
1572 ReorderBufferIterTXNFinish(rb, iterstate);
1575 /* call commit callback */
1576 rb->commit(rb, txn, commit_lsn);
1578 /* this is just a sanity check against bad output plugin behaviour */
1579 if (GetCurrentTransactionIdIfAny() != InvalidTransactionId)
1580 elog(ERROR, "output plugin used XID %u",
1581 GetCurrentTransactionId());
1584 TeardownHistoricSnapshot(false);
1587 * Aborting the current (sub-)transaction as a whole has the right
1588 * semantics. We want all locks acquired in here to be released, not
1589 * reassigned to the parent and we do not want any database access
1590 * have persistent effects.
1592 AbortCurrentTransaction();
1594 /* make sure there's no cache pollution */
1595 ReorderBufferExecuteInvalidations(rb, txn);
1598 RollbackAndReleaseCurrentSubTransaction();
1600 if (snapshot_now->copied)
1601 ReorderBufferFreeSnap(rb, snapshot_now);
1603 /* remove potential on-disk data, and deallocate */
1604 ReorderBufferCleanupTXN(rb, txn);
1608 /* TODO: Encapsulate cleanup from the PG_TRY and PG_CATCH blocks */
1610 ReorderBufferIterTXNFinish(rb, iterstate);
1612 TeardownHistoricSnapshot(true);
1615 * Force cache invalidation to happen outside of a valid transaction
1616 * to prevent catalog access as we just caught an error.
1618 AbortCurrentTransaction();
1620 /* make sure there's no cache pollution */
1621 ReorderBufferExecuteInvalidations(rb, txn);
1624 RollbackAndReleaseCurrentSubTransaction();
1626 if (snapshot_now->copied)
1627 ReorderBufferFreeSnap(rb, snapshot_now);
1629 /* remove potential on-disk data, and deallocate */
1630 ReorderBufferCleanupTXN(rb, txn);
1638 * Abort a transaction that possibly has previous changes. Needs to be first
1639 * called for subtransactions and then for the toplevel xid.
1641 * NB: Transactions handled here have to have actively aborted (i.e. have
1642 * produced an abort record). Implicitly aborted transactions are handled via
1643 * ReorderBufferAbortOld(); transactions we're just not interesteded in, but
1644 * which have committed are handled in ReorderBufferForget().
1646 * This function purges this transaction and its contents from memory and
1650 ReorderBufferAbort(ReorderBuffer *rb, TransactionId xid, XLogRecPtr lsn)
1652 ReorderBufferTXN *txn;
1654 txn = ReorderBufferTXNByXid(rb, xid, false, NULL, InvalidXLogRecPtr,
1657 /* unknown, nothing to remove */
1662 txn->final_lsn = lsn;
1664 /* remove potential on-disk data, and deallocate */
1665 ReorderBufferCleanupTXN(rb, txn);
1669 * Abort all transactions that aren't actually running anymore because the
1672 * NB: These really have to be transactions that have aborted due to a server
1673 * crash/immediate restart, as we don't deal with invalidations here.
1676 ReorderBufferAbortOld(ReorderBuffer *rb, TransactionId oldestRunningXid)
1678 dlist_mutable_iter it;
1681 * Iterate through all (potential) toplevel TXNs and abort all that are
1682 * older than what possibly can be running. Once we've found the first
1683 * that is alive we stop, there might be some that acquired an xid earlier
1684 * but started writing later, but it's unlikely and they will cleaned up
1685 * in a later call to ReorderBufferAbortOld().
1687 dlist_foreach_modify(it, &rb->toplevel_by_lsn)
1689 ReorderBufferTXN *txn;
1691 txn = dlist_container(ReorderBufferTXN, node, it.cur);
1693 if (TransactionIdPrecedes(txn->xid, oldestRunningXid))
1695 elog(DEBUG1, "aborting old transaction %u", txn->xid);
1697 /* remove potential on-disk data, and deallocate this tx */
1698 ReorderBufferCleanupTXN(rb, txn);
1706 * Forget the contents of a transaction if we aren't interested in it's
1707 * contents. Needs to be first called for subtransactions and then for the
1710 * This is significantly different to ReorderBufferAbort() because
1711 * transactions that have committed need to be treated differenly from aborted
1712 * ones since they may have modified the catalog.
1714 * Note that this is only allowed to be called in the moment a transaction
1715 * commit has just been read, not earlier; otherwise later records referring
1716 * to this xid might re-create the transaction incompletely.
1719 ReorderBufferForget(ReorderBuffer *rb, TransactionId xid, XLogRecPtr lsn)
1721 ReorderBufferTXN *txn;
1723 txn = ReorderBufferTXNByXid(rb, xid, false, NULL, InvalidXLogRecPtr,
1726 /* unknown, nothing to forget */
1731 txn->final_lsn = lsn;
1734 * Process cache invalidation messages if there are any. Even if we're not
1735 * interested in the transaction's contents, it could have manipulated the
1736 * catalog and we need to update the caches according to that.
1738 if (txn->base_snapshot != NULL && txn->ninvalidations > 0)
1740 bool use_subtxn = IsTransactionOrTransactionBlock();
1743 BeginInternalSubTransaction("replay");
1746 * Force invalidations to happen outside of a valid transaction - that
1747 * way entries will just be marked as invalid without accessing the
1748 * catalog. That's advantageous because we don't need to setup the
1749 * full state necessary for catalog access.
1752 AbortCurrentTransaction();
1754 ReorderBufferExecuteInvalidations(rb, txn);
1757 RollbackAndReleaseCurrentSubTransaction();
1760 Assert(txn->ninvalidations == 0);
1762 /* remove potential on-disk data, and deallocate */
1763 ReorderBufferCleanupTXN(rb, txn);
1768 * Tell reorderbuffer about an xid seen in the WAL stream. Has to be called at
1769 * least once for every xid in XLogRecord->xl_xid (other places in records
1770 * may, but do not have to be passed through here).
1772 * Reorderbuffer keeps some datastructures about transactions in LSN order,
1773 * for efficiency. To do that it has to know about when transactions are seen
1774 * first in the WAL. As many types of records are not actually interesting for
1775 * logical decoding, they do not necessarily pass though here.
1778 ReorderBufferProcessXid(ReorderBuffer *rb, TransactionId xid, XLogRecPtr lsn)
1780 /* many records won't have an xid assigned, centralize check here */
1781 if (xid != InvalidTransactionId)
1782 ReorderBufferTXNByXid(rb, xid, true, NULL, lsn, true);
1786 * Add a new snapshot to this transaction that may only used after lsn 'lsn'
1787 * because the previous snapshot doesn't describe the catalog correctly for
1791 ReorderBufferAddSnapshot(ReorderBuffer *rb, TransactionId xid,
1792 XLogRecPtr lsn, Snapshot snap)
1794 ReorderBufferChange *change = ReorderBufferGetChange(rb);
1796 change->data.snapshot = snap;
1797 change->action = REORDER_BUFFER_CHANGE_INTERNAL_SNAPSHOT;
1799 ReorderBufferQueueChange(rb, xid, lsn, change);
1803 * Setup the base snapshot of a transaction. The base snapshot is the snapshot
1804 * that is used to decode all changes until either this transaction modifies
1805 * the catalog or another catalog modifying transaction commits.
1807 * Needs to be called before any changes are added with
1808 * ReorderBufferQueueChange().
1811 ReorderBufferSetBaseSnapshot(ReorderBuffer *rb, TransactionId xid,
1812 XLogRecPtr lsn, Snapshot snap)
1814 ReorderBufferTXN *txn;
1817 txn = ReorderBufferTXNByXid(rb, xid, true, &is_new, lsn, true);
1818 Assert(txn->base_snapshot == NULL);
1819 Assert(snap != NULL);
1821 txn->base_snapshot = snap;
1822 txn->base_snapshot_lsn = lsn;
1826 * Access the catalog with this CommandId at this point in the changestream.
1828 * May only be called for command ids > 1
1831 ReorderBufferAddNewCommandId(ReorderBuffer *rb, TransactionId xid,
1832 XLogRecPtr lsn, CommandId cid)
1834 ReorderBufferChange *change = ReorderBufferGetChange(rb);
1836 change->data.command_id = cid;
1837 change->action = REORDER_BUFFER_CHANGE_INTERNAL_COMMAND_ID;
1839 ReorderBufferQueueChange(rb, xid, lsn, change);
1844 * Add new (relfilenode, tid) -> (cmin, cmax) mappings.
1847 ReorderBufferAddNewTupleCids(ReorderBuffer *rb, TransactionId xid,
1848 XLogRecPtr lsn, RelFileNode node,
1849 ItemPointerData tid, CommandId cmin,
1850 CommandId cmax, CommandId combocid)
1852 ReorderBufferChange *change = ReorderBufferGetChange(rb);
1853 ReorderBufferTXN *txn;
1855 txn = ReorderBufferTXNByXid(rb, xid, true, NULL, lsn, true);
1857 change->data.tuplecid.node = node;
1858 change->data.tuplecid.tid = tid;
1859 change->data.tuplecid.cmin = cmin;
1860 change->data.tuplecid.cmax = cmax;
1861 change->data.tuplecid.combocid = combocid;
1863 change->action = REORDER_BUFFER_CHANGE_INTERNAL_TUPLECID;
1865 dlist_push_tail(&txn->tuplecids, &change->node);
1870 * Setup the invalidation of the toplevel transaction.
1872 * This needs to be done before ReorderBufferCommit is called!
1875 ReorderBufferAddInvalidations(ReorderBuffer *rb, TransactionId xid,
1876 XLogRecPtr lsn, Size nmsgs,
1877 SharedInvalidationMessage *msgs)
1879 ReorderBufferTXN *txn;
1881 txn = ReorderBufferTXNByXid(rb, xid, true, NULL, lsn, true);
1883 if (txn->ninvalidations != 0)
1884 elog(ERROR, "only ever add one set of invalidations");
1888 txn->ninvalidations = nmsgs;
1889 txn->invalidations = (SharedInvalidationMessage *)
1890 MemoryContextAlloc(rb->context,
1891 sizeof(SharedInvalidationMessage) * nmsgs);
1892 memcpy(txn->invalidations, msgs,
1893 sizeof(SharedInvalidationMessage) * nmsgs);
1897 * Apply all invalidations we know. Possibly we only need parts at this point
1898 * in the changestream but we don't know which those are.
1901 ReorderBufferExecuteInvalidations(ReorderBuffer *rb, ReorderBufferTXN *txn)
1905 for (i = 0; i < txn->ninvalidations; i++)
1906 LocalExecuteInvalidationMessage(&txn->invalidations[i]);
1910 * Mark a transaction as containing catalog changes
1913 ReorderBufferXidSetCatalogChanges(ReorderBuffer *rb, TransactionId xid,
1916 ReorderBufferTXN *txn;
1918 txn = ReorderBufferTXNByXid(rb, xid, true, NULL, lsn, true);
1920 txn->has_catalog_changes = true;
1924 * Query whether a transaction is already *known* to contain catalog
1925 * changes. This can be wrong until directly before the commit!
1928 ReorderBufferXidHasCatalogChanges(ReorderBuffer *rb, TransactionId xid)
1930 ReorderBufferTXN *txn;
1932 txn = ReorderBufferTXNByXid(rb, xid, false, NULL, InvalidXLogRecPtr,
1937 return txn->has_catalog_changes;
1941 * Have we already added the first snapshot?
1944 ReorderBufferXidHasBaseSnapshot(ReorderBuffer *rb, TransactionId xid)
1946 ReorderBufferTXN *txn;
1948 txn = ReorderBufferTXNByXid(rb, xid, false, NULL, InvalidXLogRecPtr,
1951 /* transaction isn't known yet, ergo no snapshot */
1956 * TODO: It would be a nice improvement if we would check the toplevel
1957 * transaction in subtransactions, but we'd need to keep track of a bit
1960 return txn->base_snapshot != NULL;
1965 * ---------------------------------------
1966 * Disk serialization support
1967 * ---------------------------------------
1971 * Ensure the IO buffer is >= sz.
1974 ReorderBufferSerializeReserve(ReorderBuffer *rb, Size sz)
1976 if (!rb->outbufsize)
1978 rb->outbuf = MemoryContextAlloc(rb->context, sz);
1979 rb->outbufsize = sz;
1981 else if (rb->outbufsize < sz)
1983 rb->outbuf = repalloc(rb->outbuf, sz);
1984 rb->outbufsize = sz;
1989 * Check whether the transaction tx should spill its data to disk.
1992 ReorderBufferCheckSerializeTXN(ReorderBuffer *rb, ReorderBufferTXN *txn)
1995 * TODO: improve accounting so we cheaply can take subtransactions into
1998 if (txn->nentries_mem >= max_changes_in_memory)
2000 ReorderBufferSerializeTXN(rb, txn);
2001 Assert(txn->nentries_mem == 0);
2006 * Spill data of a large transaction (and its subtransactions) to disk.
2009 ReorderBufferSerializeTXN(ReorderBuffer *rb, ReorderBufferTXN *txn)
2011 dlist_iter subtxn_i;
2012 dlist_mutable_iter change_i;
2014 XLogSegNo curOpenSegNo = 0;
2016 char path[MAXPGPATH];
2018 elog(DEBUG2, "spill %u changes in XID %u to disk",
2019 (uint32) txn->nentries_mem, txn->xid);
2021 /* do the same to all child TXs */
2022 dlist_foreach(subtxn_i, &txn->subtxns)
2024 ReorderBufferTXN *subtxn;
2026 subtxn = dlist_container(ReorderBufferTXN, node, subtxn_i.cur);
2027 ReorderBufferSerializeTXN(rb, subtxn);
2030 /* serialize changestream */
2031 dlist_foreach_modify(change_i, &txn->changes)
2033 ReorderBufferChange *change;
2035 change = dlist_container(ReorderBufferChange, node, change_i.cur);
2038 * store in segment in which it belongs by start lsn, don't split over
2039 * multiple segments tho
2041 if (fd == -1 || !XLByteInSeg(change->lsn, curOpenSegNo))
2046 CloseTransientFile(fd);
2048 XLByteToSeg(change->lsn, curOpenSegNo);
2049 XLogSegNoOffsetToRecPtr(curOpenSegNo, 0, recptr);
2052 * No need to care about TLIs here, only used during a single run,
2053 * so each LSN only maps to a specific WAL record.
2055 sprintf(path, "pg_replslot/%s/xid-%u-lsn-%X-%X.snap",
2056 NameStr(MyReplicationSlot->data.name), txn->xid,
2057 (uint32) (recptr >> 32), (uint32) recptr);
2059 /* open segment, create it if necessary */
2060 fd = OpenTransientFile(path,
2061 O_CREAT | O_WRONLY | O_APPEND | PG_BINARY,
2066 (errcode_for_file_access(),
2067 errmsg("could not open file \"%s\": %m",
2071 ReorderBufferSerializeChange(rb, txn, fd, change);
2072 dlist_delete(&change->node);
2073 ReorderBufferReturnChange(rb, change);
2078 Assert(spilled == txn->nentries_mem);
2079 Assert(dlist_is_empty(&txn->changes));
2080 txn->nentries_mem = 0;
2083 CloseTransientFile(fd);
2087 * Serialize individual change to disk.
2090 ReorderBufferSerializeChange(ReorderBuffer *rb, ReorderBufferTXN *txn,
2091 int fd, ReorderBufferChange *change)
2093 ReorderBufferDiskChange *ondisk;
2094 Size sz = sizeof(ReorderBufferDiskChange);
2096 ReorderBufferSerializeReserve(rb, sz);
2098 ondisk = (ReorderBufferDiskChange *) rb->outbuf;
2099 memcpy(&ondisk->change, change, sizeof(ReorderBufferChange));
2101 switch (change->action)
2103 /* fall through these, they're all similar enough */
2104 case REORDER_BUFFER_CHANGE_INSERT:
2105 case REORDER_BUFFER_CHANGE_UPDATE:
2106 case REORDER_BUFFER_CHANGE_DELETE:
2107 case REORDER_BUFFER_CHANGE_INTERNAL_SPEC_INSERT:
2110 ReorderBufferTupleBuf *oldtup,
2115 oldtup = change->data.tp.oldtuple;
2116 newtup = change->data.tp.newtuple;
2120 sz += sizeof(HeapTupleData);
2121 oldlen = oldtup->tuple.t_len;
2127 sz += sizeof(HeapTupleData);
2128 newlen = newtup->tuple.t_len;
2132 /* make sure we have enough space */
2133 ReorderBufferSerializeReserve(rb, sz);
2135 data = ((char *) rb->outbuf) + sizeof(ReorderBufferDiskChange);
2136 /* might have been reallocated above */
2137 ondisk = (ReorderBufferDiskChange *) rb->outbuf;
2141 memcpy(data, &oldtup->tuple, sizeof(HeapTupleData));
2142 data += sizeof(HeapTupleData);
2144 memcpy(data, oldtup->tuple.t_data, oldlen);
2150 memcpy(data, &newtup->tuple, sizeof(HeapTupleData));
2151 data += sizeof(HeapTupleData);
2153 memcpy(data, newtup->tuple.t_data, newlen);
2158 case REORDER_BUFFER_CHANGE_INTERNAL_SNAPSHOT:
2163 snap = change->data.snapshot;
2165 sz += sizeof(SnapshotData) +
2166 sizeof(TransactionId) * snap->xcnt +
2167 sizeof(TransactionId) * snap->subxcnt
2170 /* make sure we have enough space */
2171 ReorderBufferSerializeReserve(rb, sz);
2172 data = ((char *) rb->outbuf) + sizeof(ReorderBufferDiskChange);
2173 /* might have been reallocated above */
2174 ondisk = (ReorderBufferDiskChange *) rb->outbuf;
2176 memcpy(data, snap, sizeof(SnapshotData));
2177 data += sizeof(SnapshotData);
2181 memcpy(data, snap->xip,
2182 sizeof(TransactionId) * snap->xcnt);
2183 data += sizeof(TransactionId) * snap->xcnt;
2188 memcpy(data, snap->subxip,
2189 sizeof(TransactionId) * snap->subxcnt);
2190 data += sizeof(TransactionId) * snap->subxcnt;
2194 case REORDER_BUFFER_CHANGE_INTERNAL_SPEC_CONFIRM:
2195 case REORDER_BUFFER_CHANGE_INTERNAL_COMMAND_ID:
2196 case REORDER_BUFFER_CHANGE_INTERNAL_TUPLECID:
2197 /* ReorderBufferChange contains everything important */
2203 if (write(fd, rb->outbuf, ondisk->size) != ondisk->size)
2205 CloseTransientFile(fd);
2207 (errcode_for_file_access(),
2208 errmsg("could not write to data file for XID %u: %m",
2212 Assert(ondisk->change.action == change->action);
2216 * Restore a number of changes spilled to disk back into memory.
2219 ReorderBufferRestoreChanges(ReorderBuffer *rb, ReorderBufferTXN *txn,
2220 int *fd, XLogSegNo *segno)
2223 XLogSegNo last_segno;
2224 dlist_mutable_iter cleanup_iter;
2226 Assert(txn->first_lsn != InvalidXLogRecPtr);
2227 Assert(txn->final_lsn != InvalidXLogRecPtr);
2229 /* free current entries, so we have memory for more */
2230 dlist_foreach_modify(cleanup_iter, &txn->changes)
2232 ReorderBufferChange *cleanup =
2233 dlist_container(ReorderBufferChange, node, cleanup_iter.cur);
2235 dlist_delete(&cleanup->node);
2236 ReorderBufferReturnChange(rb, cleanup);
2238 txn->nentries_mem = 0;
2239 Assert(dlist_is_empty(&txn->changes));
2241 XLByteToSeg(txn->final_lsn, last_segno);
2243 while (restored < max_changes_in_memory && *segno <= last_segno)
2246 ReorderBufferDiskChange *ondisk;
2251 char path[MAXPGPATH];
2256 XLByteToSeg(txn->first_lsn, *segno);
2259 Assert(*segno != 0 || dlist_is_empty(&txn->changes));
2260 XLogSegNoOffsetToRecPtr(*segno, 0, recptr);
2263 * No need to care about TLIs here, only used during a single run,
2264 * so each LSN only maps to a specific WAL record.
2266 sprintf(path, "pg_replslot/%s/xid-%u-lsn-%X-%X.snap",
2267 NameStr(MyReplicationSlot->data.name), txn->xid,
2268 (uint32) (recptr >> 32), (uint32) recptr);
2270 *fd = OpenTransientFile(path, O_RDONLY | PG_BINARY, 0);
2271 if (*fd < 0 && errno == ENOENT)
2279 (errcode_for_file_access(),
2280 errmsg("could not open file \"%s\": %m",
2286 * Read the statically sized part of a change which has information
2287 * about the total size. If we couldn't read a record, we're at the
2290 ReorderBufferSerializeReserve(rb, sizeof(ReorderBufferDiskChange));
2291 readBytes = read(*fd, rb->outbuf, sizeof(ReorderBufferDiskChange));
2296 CloseTransientFile(*fd);
2301 else if (readBytes < 0)
2303 (errcode_for_file_access(),
2304 errmsg("could not read from reorderbuffer spill file: %m")));
2305 else if (readBytes != sizeof(ReorderBufferDiskChange))
2307 (errcode_for_file_access(),
2308 errmsg("could not read from reorderbuffer spill file: read %d instead of %u bytes",
2310 (uint32) sizeof(ReorderBufferDiskChange))));
2312 ondisk = (ReorderBufferDiskChange *) rb->outbuf;
2314 ReorderBufferSerializeReserve(rb,
2315 sizeof(ReorderBufferDiskChange) + ondisk->size);
2316 ondisk = (ReorderBufferDiskChange *) rb->outbuf;
2318 readBytes = read(*fd, rb->outbuf + sizeof(ReorderBufferDiskChange),
2319 ondisk->size - sizeof(ReorderBufferDiskChange));
2323 (errcode_for_file_access(),
2324 errmsg("could not read from reorderbuffer spill file: %m")));
2325 else if (readBytes != ondisk->size - sizeof(ReorderBufferDiskChange))
2327 (errcode_for_file_access(),
2328 errmsg("could not read from reorderbuffer spill file: read %d instead of %u bytes",
2330 (uint32) (ondisk->size - sizeof(ReorderBufferDiskChange)))));
2333 * ok, read a full change from disk, now restore it into proper
2336 ReorderBufferRestoreChange(rb, txn, rb->outbuf);
2344 * Convert change from its on-disk format to in-memory format and queue it onto
2345 * the TXN's ->changes list.
2348 ReorderBufferRestoreChange(ReorderBuffer *rb, ReorderBufferTXN *txn,
2351 ReorderBufferDiskChange *ondisk;
2352 ReorderBufferChange *change;
2354 ondisk = (ReorderBufferDiskChange *) data;
2356 change = ReorderBufferGetChange(rb);
2358 /* copy static part */
2359 memcpy(change, &ondisk->change, sizeof(ReorderBufferChange));
2361 data += sizeof(ReorderBufferDiskChange);
2363 /* restore individual stuff */
2364 switch (change->action)
2366 /* fall through these, they're all similar enough */
2367 case REORDER_BUFFER_CHANGE_INSERT:
2368 case REORDER_BUFFER_CHANGE_UPDATE:
2369 case REORDER_BUFFER_CHANGE_DELETE:
2370 case REORDER_BUFFER_CHANGE_INTERNAL_SPEC_INSERT:
2371 if (change->data.tp.oldtuple)
2373 Size tuplelen = ((HeapTuple) data)->t_len;
2375 change->data.tp.oldtuple =
2376 ReorderBufferGetTupleBuf(rb, tuplelen - SizeofHeapTupleHeader);
2378 /* restore ->tuple */
2379 memcpy(&change->data.tp.oldtuple->tuple, data,
2380 sizeof(HeapTupleData));
2381 data += sizeof(HeapTupleData);
2383 /* reset t_data pointer into the new tuplebuf */
2384 change->data.tp.oldtuple->tuple.t_data =
2385 ReorderBufferTupleBufData(change->data.tp.oldtuple);
2387 /* restore tuple data itself */
2388 memcpy(change->data.tp.oldtuple->tuple.t_data, data, tuplelen);
2392 if (change->data.tp.newtuple)
2394 Size tuplelen = ((HeapTuple) data)->t_len;
2396 change->data.tp.newtuple =
2397 ReorderBufferGetTupleBuf(rb, tuplelen - SizeofHeapTupleHeader);
2399 /* restore ->tuple */
2400 memcpy(&change->data.tp.newtuple->tuple, data,
2401 sizeof(HeapTupleData));
2402 data += sizeof(HeapTupleData);
2404 /* reset t_data pointer into the new tuplebuf */
2405 change->data.tp.newtuple->tuple.t_data =
2406 ReorderBufferTupleBufData(change->data.tp.newtuple);
2408 /* restore tuple data itself */
2409 memcpy(change->data.tp.newtuple->tuple.t_data, data, tuplelen);
2414 case REORDER_BUFFER_CHANGE_INTERNAL_SNAPSHOT:
2420 oldsnap = (Snapshot) data;
2422 size = sizeof(SnapshotData) +
2423 sizeof(TransactionId) * oldsnap->xcnt +
2424 sizeof(TransactionId) * (oldsnap->subxcnt + 0);
2426 change->data.snapshot = MemoryContextAllocZero(rb->context, size);
2428 newsnap = change->data.snapshot;
2430 memcpy(newsnap, data, size);
2431 newsnap->xip = (TransactionId *)
2432 (((char *) newsnap) + sizeof(SnapshotData));
2433 newsnap->subxip = newsnap->xip + newsnap->xcnt;
2434 newsnap->copied = true;
2437 /* the base struct contains all the data, easy peasy */
2438 case REORDER_BUFFER_CHANGE_INTERNAL_SPEC_CONFIRM:
2439 case REORDER_BUFFER_CHANGE_INTERNAL_COMMAND_ID:
2440 case REORDER_BUFFER_CHANGE_INTERNAL_TUPLECID:
2444 dlist_push_tail(&txn->changes, &change->node);
2445 txn->nentries_mem++;
2449 * Remove all on-disk stored for the passed in transaction.
2452 ReorderBufferRestoreCleanup(ReorderBuffer *rb, ReorderBufferTXN *txn)
2458 Assert(txn->first_lsn != InvalidXLogRecPtr);
2459 Assert(txn->final_lsn != InvalidXLogRecPtr);
2461 XLByteToSeg(txn->first_lsn, first);
2462 XLByteToSeg(txn->final_lsn, last);
2464 /* iterate over all possible filenames, and delete them */
2465 for (cur = first; cur <= last; cur++)
2467 char path[MAXPGPATH];
2470 XLogSegNoOffsetToRecPtr(cur, 0, recptr);
2472 sprintf(path, "pg_replslot/%s/xid-%u-lsn-%X-%X.snap",
2473 NameStr(MyReplicationSlot->data.name), txn->xid,
2474 (uint32) (recptr >> 32), (uint32) recptr);
2475 if (unlink(path) != 0 && errno != ENOENT)
2477 (errcode_for_file_access(),
2478 errmsg("could not remove file \"%s\": %m", path)));
2483 * Delete all data spilled to disk after we've restarted/crashed. It will be
2484 * recreated when the respective slots are reused.
2487 StartupReorderBuffer(void)
2490 struct dirent *logical_de;
2493 struct dirent *spill_de;
2495 logical_dir = AllocateDir("pg_replslot");
2496 while ((logical_de = ReadDir(logical_dir, "pg_replslot")) != NULL)
2498 struct stat statbuf;
2499 char path[MAXPGPATH];
2501 if (strcmp(logical_de->d_name, ".") == 0 ||
2502 strcmp(logical_de->d_name, "..") == 0)
2505 /* if it cannot be a slot, skip the directory */
2506 if (!ReplicationSlotValidateName(logical_de->d_name, DEBUG2))
2510 * ok, has to be a surviving logical slot, iterate and delete
2511 * everythign starting with xid-*
2513 sprintf(path, "pg_replslot/%s", logical_de->d_name);
2515 /* we're only creating directories here, skip if it's not our's */
2516 if (lstat(path, &statbuf) == 0 && !S_ISDIR(statbuf.st_mode))
2519 spill_dir = AllocateDir(path);
2520 while ((spill_de = ReadDir(spill_dir, path)) != NULL)
2522 if (strcmp(spill_de->d_name, ".") == 0 ||
2523 strcmp(spill_de->d_name, "..") == 0)
2526 /* only look at names that can be ours */
2527 if (strncmp(spill_de->d_name, "xid", 3) == 0)
2529 sprintf(path, "pg_replslot/%s/%s", logical_de->d_name,
2532 if (unlink(path) != 0)
2534 (errcode_for_file_access(),
2535 errmsg("could not remove file \"%s\": %m",
2541 FreeDir(logical_dir);
2544 /* ---------------------------------------
2545 * toast reassembly support
2546 * ---------------------------------------
2550 * Initialize per tuple toast reconstruction support.
2553 ReorderBufferToastInitHash(ReorderBuffer *rb, ReorderBufferTXN *txn)
2557 Assert(txn->toast_hash == NULL);
2559 memset(&hash_ctl, 0, sizeof(hash_ctl));
2560 hash_ctl.keysize = sizeof(Oid);
2561 hash_ctl.entrysize = sizeof(ReorderBufferToastEnt);
2562 hash_ctl.hcxt = rb->context;
2563 txn->toast_hash = hash_create("ReorderBufferToastHash", 5, &hash_ctl,
2564 HASH_ELEM | HASH_BLOBS | HASH_CONTEXT);
2568 * Per toast-chunk handling for toast reconstruction
2570 * Appends a toast chunk so we can reconstruct it when the tuple "owning" the
2571 * toasted Datum comes along.
2574 ReorderBufferToastAppendChunk(ReorderBuffer *rb, ReorderBufferTXN *txn,
2575 Relation relation, ReorderBufferChange *change)
2577 ReorderBufferToastEnt *ent;
2578 ReorderBufferTupleBuf *newtup;
2583 TupleDesc desc = RelationGetDescr(relation);
2587 if (txn->toast_hash == NULL)
2588 ReorderBufferToastInitHash(rb, txn);
2590 Assert(IsToastRelation(relation));
2592 newtup = change->data.tp.newtuple;
2593 chunk_id = DatumGetObjectId(fastgetattr(&newtup->tuple, 1, desc, &isnull));
2595 chunk_seq = DatumGetInt32(fastgetattr(&newtup->tuple, 2, desc, &isnull));
2598 ent = (ReorderBufferToastEnt *)
2599 hash_search(txn->toast_hash,
2606 Assert(ent->chunk_id == chunk_id);
2607 ent->num_chunks = 0;
2608 ent->last_chunk_seq = 0;
2610 ent->reconstructed = NULL;
2611 dlist_init(&ent->chunks);
2614 elog(ERROR, "got sequence entry %d for toast chunk %u instead of seq 0",
2615 chunk_seq, chunk_id);
2617 else if (found && chunk_seq != ent->last_chunk_seq + 1)
2618 elog(ERROR, "got sequence entry %d for toast chunk %u instead of seq %d",
2619 chunk_seq, chunk_id, ent->last_chunk_seq + 1);
2621 chunk = DatumGetPointer(fastgetattr(&newtup->tuple, 3, desc, &isnull));
2624 /* calculate size so we can allocate the right size at once later */
2625 if (!VARATT_IS_EXTENDED(chunk))
2626 chunksize = VARSIZE(chunk) - VARHDRSZ;
2627 else if (VARATT_IS_SHORT(chunk))
2628 /* could happen due to heap_form_tuple doing its thing */
2629 chunksize = VARSIZE_SHORT(chunk) - VARHDRSZ_SHORT;
2631 elog(ERROR, "unexpected type of toast chunk");
2633 ent->size += chunksize;
2634 ent->last_chunk_seq = chunk_seq;
2636 dlist_push_tail(&ent->chunks, &change->node);
2640 * Rejigger change->newtuple to point to in-memory toast tuples instead to
2641 * on-disk toast tuples that may not longer exist (think DROP TABLE or VACUUM).
2643 * We cannot replace unchanged toast tuples though, so those will still point
2644 * to on-disk toast data.
2647 ReorderBufferToastReplace(ReorderBuffer *rb, ReorderBufferTXN *txn,
2648 Relation relation, ReorderBufferChange *change)
2657 TupleDesc toast_desc;
2658 MemoryContext oldcontext;
2659 ReorderBufferTupleBuf *newtup;
2661 /* no toast tuples changed */
2662 if (txn->toast_hash == NULL)
2665 oldcontext = MemoryContextSwitchTo(rb->context);
2667 /* we should only have toast tuples in an INSERT or UPDATE */
2668 Assert(change->data.tp.newtuple);
2670 desc = RelationGetDescr(relation);
2672 toast_rel = RelationIdGetRelation(relation->rd_rel->reltoastrelid);
2673 toast_desc = RelationGetDescr(toast_rel);
2675 /* should we allocate from stack instead? */
2676 attrs = palloc0(sizeof(Datum) * desc->natts);
2677 isnull = palloc0(sizeof(bool) * desc->natts);
2678 free = palloc0(sizeof(bool) * desc->natts);
2680 newtup = change->data.tp.newtuple;
2682 heap_deform_tuple(&newtup->tuple, desc, attrs, isnull);
2684 for (natt = 0; natt < desc->natts; natt++)
2686 Form_pg_attribute attr = desc->attrs[natt];
2687 ReorderBufferToastEnt *ent;
2688 struct varlena *varlena;
2690 /* va_rawsize is the size of the original datum -- including header */
2691 struct varatt_external toast_pointer;
2692 struct varatt_indirect redirect_pointer;
2693 struct varlena *new_datum = NULL;
2694 struct varlena *reconstructed;
2698 /* system columns aren't toasted */
2699 if (attr->attnum < 0)
2702 if (attr->attisdropped)
2705 /* not a varlena datatype */
2706 if (attr->attlen != -1)
2713 /* ok, we know we have a toast datum */
2714 varlena = (struct varlena *) DatumGetPointer(attrs[natt]);
2716 /* no need to do anything if the tuple isn't external */
2717 if (!VARATT_IS_EXTERNAL(varlena))
2720 VARATT_EXTERNAL_GET_POINTER(toast_pointer, varlena);
2723 * Check whether the toast tuple changed, replace if so.
2725 ent = (ReorderBufferToastEnt *)
2726 hash_search(txn->toast_hash,
2727 (void *) &toast_pointer.va_valueid,
2734 (struct varlena *) palloc0(INDIRECT_POINTER_SIZE);
2738 reconstructed = palloc0(toast_pointer.va_rawsize);
2740 ent->reconstructed = reconstructed;
2742 /* stitch toast tuple back together from its parts */
2743 dlist_foreach(it, &ent->chunks)
2746 ReorderBufferChange *cchange;
2747 ReorderBufferTupleBuf *ctup;
2750 cchange = dlist_container(ReorderBufferChange, node, it.cur);
2751 ctup = cchange->data.tp.newtuple;
2752 chunk = DatumGetPointer(
2753 fastgetattr(&ctup->tuple, 3, toast_desc, &isnull));
2756 Assert(!VARATT_IS_EXTERNAL(chunk));
2757 Assert(!VARATT_IS_SHORT(chunk));
2759 memcpy(VARDATA(reconstructed) + data_done,
2761 VARSIZE(chunk) - VARHDRSZ);
2762 data_done += VARSIZE(chunk) - VARHDRSZ;
2764 Assert(data_done == toast_pointer.va_extsize);
2766 /* make sure its marked as compressed or not */
2767 if (VARATT_EXTERNAL_IS_COMPRESSED(toast_pointer))
2768 SET_VARSIZE_COMPRESSED(reconstructed, data_done + VARHDRSZ);
2770 SET_VARSIZE(reconstructed, data_done + VARHDRSZ);
2772 memset(&redirect_pointer, 0, sizeof(redirect_pointer));
2773 redirect_pointer.pointer = reconstructed;
2775 SET_VARTAG_EXTERNAL(new_datum, VARTAG_INDIRECT);
2776 memcpy(VARDATA_EXTERNAL(new_datum), &redirect_pointer,
2777 sizeof(redirect_pointer));
2779 attrs[natt] = PointerGetDatum(new_datum);
2783 * Build tuple in separate memory & copy tuple back into the tuplebuf
2784 * passed to the output plugin. We can't directly heap_fill_tuple() into
2785 * the tuplebuf because attrs[] will point back into the current content.
2787 tmphtup = heap_form_tuple(desc, attrs, isnull);
2788 Assert(newtup->tuple.t_len <= MaxHeapTupleSize);
2789 Assert(ReorderBufferTupleBufData(newtup) == newtup->tuple.t_data);
2791 memcpy(newtup->tuple.t_data, tmphtup->t_data, tmphtup->t_len);
2792 newtup->tuple.t_len = tmphtup->t_len;
2795 * free resources we won't further need, more persistent stuff will be
2796 * free'd in ReorderBufferToastReset().
2798 RelationClose(toast_rel);
2800 for (natt = 0; natt < desc->natts; natt++)
2803 pfree(DatumGetPointer(attrs[natt]));
2809 MemoryContextSwitchTo(oldcontext);
2813 * Free all resources allocated for toast reconstruction.
2816 ReorderBufferToastReset(ReorderBuffer *rb, ReorderBufferTXN *txn)
2818 HASH_SEQ_STATUS hstat;
2819 ReorderBufferToastEnt *ent;
2821 if (txn->toast_hash == NULL)
2824 /* sequentially walk over the hash and free everything */
2825 hash_seq_init(&hstat, txn->toast_hash);
2826 while ((ent = (ReorderBufferToastEnt *) hash_seq_search(&hstat)) != NULL)
2828 dlist_mutable_iter it;
2830 if (ent->reconstructed != NULL)
2831 pfree(ent->reconstructed);
2833 dlist_foreach_modify(it, &ent->chunks)
2835 ReorderBufferChange *change =
2836 dlist_container(ReorderBufferChange, node, it.cur);
2838 dlist_delete(&change->node);
2839 ReorderBufferReturnChange(rb, change);
2843 hash_destroy(txn->toast_hash);
2844 txn->toast_hash = NULL;
2848 /* ---------------------------------------
2849 * Visibility support for logical decoding
2852 * Lookup actual cmin/cmax values when using decoding snapshot. We can't
2853 * always rely on stored cmin/cmax values because of two scenarios:
2855 * * A tuple got changed multiple times during a single transaction and thus
2856 * has got a combocid. Combocid's are only valid for the duration of a
2857 * single transaction.
2858 * * A tuple with a cmin but no cmax (and thus no combocid) got
2859 * deleted/updated in another transaction than the one which created it
2860 * which we are looking at right now. As only one of cmin, cmax or combocid
2861 * is actually stored in the heap we don't have access to the value we
2864 * To resolve those problems we have a per-transaction hash of (cmin,
2865 * cmax) tuples keyed by (relfilenode, ctid) which contains the actual
2866 * (cmin, cmax) values. That also takes care of combocids by simply
2867 * not caring about them at all. As we have the real cmin/cmax values
2868 * combocids aren't interesting.
2870 * As we only care about catalog tuples here the overhead of this
2871 * hashtable should be acceptable.
2873 * Heap rewrites complicate this a bit, check rewriteheap.c for
2875 * -------------------------------------------------------------------------
2878 /* struct for qsort()ing mapping files by lsn somewhat efficiently */
2879 typedef struct RewriteMappingFile
2882 char fname[MAXPGPATH];
2883 } RewriteMappingFile;
2887 DisplayMapping(HTAB *tuplecid_data)
2889 HASH_SEQ_STATUS hstat;
2890 ReorderBufferTupleCidEnt *ent;
2892 hash_seq_init(&hstat, tuplecid_data);
2893 while ((ent = (ReorderBufferTupleCidEnt *) hash_seq_search(&hstat)) != NULL)
2895 elog(DEBUG3, "mapping: node: %u/%u/%u tid: %u/%u cmin: %u, cmax: %u",
2896 ent->key.relnode.dbNode,
2897 ent->key.relnode.spcNode,
2898 ent->key.relnode.relNode,
2899 BlockIdGetBlockNumber(&ent->key.tid.ip_blkid),
2900 ent->key.tid.ip_posid,
2909 * Apply a single mapping file to tuplecid_data.
2911 * The mapping file has to have been verified to be a) committed b) for our
2912 * transaction c) applied in LSN order.
2915 ApplyLogicalMappingFile(HTAB *tuplecid_data, Oid relid, const char *fname)
2917 char path[MAXPGPATH];
2920 LogicalRewriteMappingData map;
2922 sprintf(path, "pg_logical/mappings/%s", fname);
2923 fd = OpenTransientFile(path, O_RDONLY | PG_BINARY, 0);
2926 (errmsg("could not open file \"%s\": %m", path)));
2930 ReorderBufferTupleCidKey key;
2931 ReorderBufferTupleCidEnt *ent;
2932 ReorderBufferTupleCidEnt *new_ent;
2935 /* be careful about padding */
2936 memset(&key, 0, sizeof(ReorderBufferTupleCidKey));
2938 /* read all mappings till the end of the file */
2939 readBytes = read(fd, &map, sizeof(LogicalRewriteMappingData));
2943 (errcode_for_file_access(),
2944 errmsg("could not read file \"%s\": %m",
2946 else if (readBytes == 0) /* EOF */
2948 else if (readBytes != sizeof(LogicalRewriteMappingData))
2950 (errcode_for_file_access(),
2951 errmsg("could not read from file \"%s\": read %d instead of %d bytes",
2953 (int32) sizeof(LogicalRewriteMappingData))));
2955 key.relnode = map.old_node;
2956 ItemPointerCopy(&map.old_tid,
2960 ent = (ReorderBufferTupleCidEnt *)
2961 hash_search(tuplecid_data,
2966 /* no existing mapping, no need to update */
2970 key.relnode = map.new_node;
2971 ItemPointerCopy(&map.new_tid,
2974 new_ent = (ReorderBufferTupleCidEnt *)
2975 hash_search(tuplecid_data,
2983 * Make sure the existing mapping makes sense. We sometime update
2984 * old records that did not yet have a cmax (e.g. pg_class' own
2985 * entry while rewriting it) during rewrites, so allow that.
2987 Assert(ent->cmin == InvalidCommandId || ent->cmin == new_ent->cmin);
2988 Assert(ent->cmax == InvalidCommandId || ent->cmax == new_ent->cmax);
2992 /* update mapping */
2993 new_ent->cmin = ent->cmin;
2994 new_ent->cmax = ent->cmax;
2995 new_ent->combocid = ent->combocid;
3002 * Check whether the TransactionOId 'xid' is in the pre-sorted array 'xip'.
3005 TransactionIdInArray(TransactionId xid, TransactionId *xip, Size num)
3007 return bsearch(&xid, xip, num,
3008 sizeof(TransactionId), xidComparator) != NULL;
3012 * qsort() comparator for sorting RewriteMappingFiles in LSN order.
3015 file_sort_by_lsn(const void *a_p, const void *b_p)
3017 RewriteMappingFile *a = *(RewriteMappingFile **) a_p;
3018 RewriteMappingFile *b = *(RewriteMappingFile **) b_p;
3020 if (a->lsn < b->lsn)
3022 else if (a->lsn > b->lsn)
3028 * Apply any existing logical remapping files if there are any targeted at our
3029 * transaction for relid.
3032 UpdateLogicalMappings(HTAB *tuplecid_data, Oid relid, Snapshot snapshot)
3035 struct dirent *mapping_de;
3038 RewriteMappingFile **files_a;
3040 Oid dboid = IsSharedRelation(relid) ? InvalidOid : MyDatabaseId;
3042 mapping_dir = AllocateDir("pg_logical/mappings");
3043 while ((mapping_de = ReadDir(mapping_dir, "pg_logical/mappings")) != NULL)
3047 TransactionId f_mapped_xid;
3048 TransactionId f_create_xid;
3052 RewriteMappingFile *f;
3054 if (strcmp(mapping_de->d_name, ".") == 0 ||
3055 strcmp(mapping_de->d_name, "..") == 0)
3058 /* Ignore files that aren't ours */
3059 if (strncmp(mapping_de->d_name, "map-", 4) != 0)
3062 if (sscanf(mapping_de->d_name, LOGICAL_REWRITE_FORMAT,
3063 &f_dboid, &f_relid, &f_hi, &f_lo,
3064 &f_mapped_xid, &f_create_xid) != 6)
3065 elog(ERROR, "could not parse filename \"%s\"", mapping_de->d_name);
3067 f_lsn = ((uint64) f_hi) << 32 | f_lo;
3069 /* mapping for another database */
3070 if (f_dboid != dboid)
3073 /* mapping for another relation */
3074 if (f_relid != relid)
3077 /* did the creating transaction abort? */
3078 if (!TransactionIdDidCommit(f_create_xid))
3081 /* not for our transaction */
3082 if (!TransactionIdInArray(f_mapped_xid, snapshot->subxip, snapshot->subxcnt))
3085 /* ok, relevant, queue for apply */
3086 f = palloc(sizeof(RewriteMappingFile));
3088 strcpy(f->fname, mapping_de->d_name);
3089 files = lappend(files, f);
3091 FreeDir(mapping_dir);
3093 /* build array we can easily sort */
3094 files_a = palloc(list_length(files) * sizeof(RewriteMappingFile *));
3096 foreach(file, files)
3098 files_a[off++] = lfirst(file);
3101 /* sort files so we apply them in LSN order */
3102 qsort(files_a, list_length(files), sizeof(RewriteMappingFile *),
3105 for (off = 0; off < list_length(files); off++)
3107 RewriteMappingFile *f = files_a[off];
3109 elog(DEBUG1, "applying mapping: \"%s\" in %u", f->fname,
3110 snapshot->subxip[0]);
3111 ApplyLogicalMappingFile(tuplecid_data, relid, f->fname);
3117 * Lookup cmin/cmax of a tuple, during logical decoding where we can't rely on
3121 ResolveCminCmaxDuringDecoding(HTAB *tuplecid_data,
3123 HeapTuple htup, Buffer buffer,
3124 CommandId *cmin, CommandId *cmax)
3126 ReorderBufferTupleCidKey key;
3127 ReorderBufferTupleCidEnt *ent;
3129 BlockNumber blockno;
3130 bool updated_mapping = false;
3132 /* be careful about padding */
3133 memset(&key, 0, sizeof(key));
3135 Assert(!BufferIsLocal(buffer));
3138 * get relfilenode from the buffer, no convenient way to access it other
3141 BufferGetTag(buffer, &key.relnode, &forkno, &blockno);
3143 /* tuples can only be in the main fork */
3144 Assert(forkno == MAIN_FORKNUM);
3145 Assert(blockno == ItemPointerGetBlockNumber(&htup->t_self));
3147 ItemPointerCopy(&htup->t_self,
3151 ent = (ReorderBufferTupleCidEnt *)
3152 hash_search(tuplecid_data,
3158 * failed to find a mapping, check whether the table was rewritten and
3159 * apply mapping if so, but only do that once - there can be no new
3160 * mappings while we are in here since we have to hold a lock on the
3163 if (ent == NULL && !updated_mapping)
3165 UpdateLogicalMappings(tuplecid_data, htup->t_tableOid, snapshot);
3166 /* now check but don't update for a mapping again */
3167 updated_mapping = true;
3170 else if (ent == NULL)