1 /*-------------------------------------------------------------------------
4 * PostgreSQL logical replay/reorder buffer management
7 * Copyright (c) 2012-2017, 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_SIZES);
238 (ReorderBuffer *) MemoryContextAlloc(new_ctx, sizeof(ReorderBuffer));
240 memset(&hash_ctl, 0, sizeof(hash_ctl));
242 buffer->context = new_ctx;
244 hash_ctl.keysize = sizeof(TransactionId);
245 hash_ctl.entrysize = sizeof(ReorderBufferTXNByIdEnt);
246 hash_ctl.hcxt = buffer->context;
248 buffer->by_txn = hash_create("ReorderBufferByXid", 1000, &hash_ctl,
249 HASH_ELEM | HASH_BLOBS | HASH_CONTEXT);
251 buffer->by_txn_last_xid = InvalidTransactionId;
252 buffer->by_txn_last_txn = NULL;
254 buffer->nr_cached_transactions = 0;
255 buffer->nr_cached_changes = 0;
256 buffer->nr_cached_tuplebufs = 0;
258 buffer->outbuf = NULL;
259 buffer->outbufsize = 0;
261 buffer->current_restart_decoding_lsn = InvalidXLogRecPtr;
263 dlist_init(&buffer->toplevel_by_lsn);
264 dlist_init(&buffer->cached_transactions);
265 dlist_init(&buffer->cached_changes);
266 slist_init(&buffer->cached_tuplebufs);
272 * Free a ReorderBuffer
275 ReorderBufferFree(ReorderBuffer *rb)
277 MemoryContext context = rb->context;
280 * We free separately allocated data by entirely scrapping reorderbuffer's
283 MemoryContextDelete(context);
287 * Get an unused, possibly preallocated, ReorderBufferTXN.
289 static ReorderBufferTXN *
290 ReorderBufferGetTXN(ReorderBuffer *rb)
292 ReorderBufferTXN *txn;
294 /* check the slab cache */
295 if (rb->nr_cached_transactions > 0)
297 rb->nr_cached_transactions--;
298 txn = (ReorderBufferTXN *)
299 dlist_container(ReorderBufferTXN, node,
300 dlist_pop_head_node(&rb->cached_transactions));
304 txn = (ReorderBufferTXN *)
305 MemoryContextAlloc(rb->context, sizeof(ReorderBufferTXN));
308 memset(txn, 0, sizeof(ReorderBufferTXN));
310 dlist_init(&txn->changes);
311 dlist_init(&txn->tuplecids);
312 dlist_init(&txn->subtxns);
318 * Free a ReorderBufferTXN.
320 * Deallocation might be delayed for efficiency purposes, for details check
321 * the comments above max_cached_changes's definition.
324 ReorderBufferReturnTXN(ReorderBuffer *rb, ReorderBufferTXN *txn)
326 /* clean the lookup cache if we were cached (quite likely) */
327 if (rb->by_txn_last_xid == txn->xid)
329 rb->by_txn_last_xid = InvalidTransactionId;
330 rb->by_txn_last_txn = NULL;
333 /* free data that's contained */
335 if (txn->tuplecid_hash != NULL)
337 hash_destroy(txn->tuplecid_hash);
338 txn->tuplecid_hash = NULL;
341 if (txn->invalidations)
343 pfree(txn->invalidations);
344 txn->invalidations = NULL;
347 /* check whether to put into the slab cache */
348 if (rb->nr_cached_transactions < max_cached_transactions)
350 rb->nr_cached_transactions++;
351 dlist_push_head(&rb->cached_transactions, &txn->node);
352 VALGRIND_MAKE_MEM_UNDEFINED(txn, sizeof(ReorderBufferTXN));
353 VALGRIND_MAKE_MEM_DEFINED(&txn->node, sizeof(txn->node));
362 * Get an unused, possibly preallocated, ReorderBufferChange.
364 ReorderBufferChange *
365 ReorderBufferGetChange(ReorderBuffer *rb)
367 ReorderBufferChange *change;
369 /* check the slab cache */
370 if (rb->nr_cached_changes)
372 rb->nr_cached_changes--;
373 change = (ReorderBufferChange *)
374 dlist_container(ReorderBufferChange, node,
375 dlist_pop_head_node(&rb->cached_changes));
379 change = (ReorderBufferChange *)
380 MemoryContextAlloc(rb->context, sizeof(ReorderBufferChange));
383 memset(change, 0, sizeof(ReorderBufferChange));
388 * Free an ReorderBufferChange.
390 * Deallocation might be delayed for efficiency purposes, for details check
391 * the comments above max_cached_changes's definition.
394 ReorderBufferReturnChange(ReorderBuffer *rb, ReorderBufferChange *change)
396 /* free contained data */
397 switch (change->action)
399 case REORDER_BUFFER_CHANGE_INSERT:
400 case REORDER_BUFFER_CHANGE_UPDATE:
401 case REORDER_BUFFER_CHANGE_DELETE:
402 case REORDER_BUFFER_CHANGE_INTERNAL_SPEC_INSERT:
403 if (change->data.tp.newtuple)
405 ReorderBufferReturnTupleBuf(rb, change->data.tp.newtuple);
406 change->data.tp.newtuple = NULL;
409 if (change->data.tp.oldtuple)
411 ReorderBufferReturnTupleBuf(rb, change->data.tp.oldtuple);
412 change->data.tp.oldtuple = NULL;
415 case REORDER_BUFFER_CHANGE_MESSAGE:
416 if (change->data.msg.prefix != NULL)
417 pfree(change->data.msg.prefix);
418 change->data.msg.prefix = NULL;
419 if (change->data.msg.message != NULL)
420 pfree(change->data.msg.message);
421 change->data.msg.message = NULL;
423 case REORDER_BUFFER_CHANGE_INTERNAL_SNAPSHOT:
424 if (change->data.snapshot)
426 ReorderBufferFreeSnap(rb, change->data.snapshot);
427 change->data.snapshot = NULL;
430 /* no data in addition to the struct itself */
431 case REORDER_BUFFER_CHANGE_INTERNAL_SPEC_CONFIRM:
432 case REORDER_BUFFER_CHANGE_INTERNAL_COMMAND_ID:
433 case REORDER_BUFFER_CHANGE_INTERNAL_TUPLECID:
437 /* check whether to put into the slab cache */
438 if (rb->nr_cached_changes < max_cached_changes)
440 rb->nr_cached_changes++;
441 dlist_push_head(&rb->cached_changes, &change->node);
442 VALGRIND_MAKE_MEM_UNDEFINED(change, sizeof(ReorderBufferChange));
443 VALGRIND_MAKE_MEM_DEFINED(&change->node, sizeof(change->node));
453 * Get an unused, possibly preallocated, ReorderBufferTupleBuf fitting at
454 * least a tuple of size tuple_len (excluding header overhead).
456 ReorderBufferTupleBuf *
457 ReorderBufferGetTupleBuf(ReorderBuffer *rb, Size tuple_len)
459 ReorderBufferTupleBuf *tuple;
462 alloc_len = tuple_len + SizeofHeapTupleHeader;
465 * Most tuples are below MaxHeapTupleSize, so we use a slab allocator for
466 * those. Thus always allocate at least MaxHeapTupleSize. Note that tuples
467 * generated for oldtuples can be bigger, as they don't have out-of-line
470 if (alloc_len < MaxHeapTupleSize)
471 alloc_len = MaxHeapTupleSize;
474 /* if small enough, check the slab cache */
475 if (alloc_len <= MaxHeapTupleSize && rb->nr_cached_tuplebufs)
477 rb->nr_cached_tuplebufs--;
478 tuple = slist_container(ReorderBufferTupleBuf, node,
479 slist_pop_head_node(&rb->cached_tuplebufs));
480 Assert(tuple->alloc_tuple_size == MaxHeapTupleSize);
481 #ifdef USE_ASSERT_CHECKING
482 memset(&tuple->tuple, 0xa9, sizeof(HeapTupleData));
483 VALGRIND_MAKE_MEM_UNDEFINED(&tuple->tuple, sizeof(HeapTupleData));
485 tuple->tuple.t_data = ReorderBufferTupleBufData(tuple);
486 #ifdef USE_ASSERT_CHECKING
487 memset(tuple->tuple.t_data, 0xa8, tuple->alloc_tuple_size);
488 VALGRIND_MAKE_MEM_UNDEFINED(tuple->tuple.t_data, tuple->alloc_tuple_size);
493 tuple = (ReorderBufferTupleBuf *)
494 MemoryContextAlloc(rb->context,
495 sizeof(ReorderBufferTupleBuf) +
496 MAXIMUM_ALIGNOF + alloc_len);
497 tuple->alloc_tuple_size = alloc_len;
498 tuple->tuple.t_data = ReorderBufferTupleBufData(tuple);
505 * Free an ReorderBufferTupleBuf.
507 * Deallocation might be delayed for efficiency purposes, for details check
508 * the comments above max_cached_changes's definition.
511 ReorderBufferReturnTupleBuf(ReorderBuffer *rb, ReorderBufferTupleBuf *tuple)
513 /* check whether to put into the slab cache, oversized tuples never are */
514 if (tuple->alloc_tuple_size == MaxHeapTupleSize &&
515 rb->nr_cached_tuplebufs < max_cached_tuplebufs)
517 rb->nr_cached_tuplebufs++;
518 slist_push_head(&rb->cached_tuplebufs, &tuple->node);
519 VALGRIND_MAKE_MEM_UNDEFINED(tuple->tuple.t_data, tuple->alloc_tuple_size);
520 VALGRIND_MAKE_MEM_UNDEFINED(tuple, sizeof(ReorderBufferTupleBuf));
521 VALGRIND_MAKE_MEM_DEFINED(&tuple->node, sizeof(tuple->node));
522 VALGRIND_MAKE_MEM_DEFINED(&tuple->alloc_tuple_size, sizeof(tuple->alloc_tuple_size));
531 * Return the ReorderBufferTXN from the given buffer, specified by Xid.
532 * If create is true, and a transaction doesn't already exist, create it
533 * (with the given LSN, and as top transaction if that's specified);
534 * when this happens, is_new is set to true.
536 static ReorderBufferTXN *
537 ReorderBufferTXNByXid(ReorderBuffer *rb, TransactionId xid, bool create,
538 bool *is_new, XLogRecPtr lsn, bool create_as_top)
540 ReorderBufferTXN *txn;
541 ReorderBufferTXNByIdEnt *ent;
544 Assert(TransactionIdIsValid(xid));
545 Assert(!create || lsn != InvalidXLogRecPtr);
548 * Check the one-entry lookup cache first
550 if (TransactionIdIsValid(rb->by_txn_last_xid) &&
551 rb->by_txn_last_xid == xid)
553 txn = rb->by_txn_last_txn;
557 /* found it, and it's valid */
564 * cached as non-existent, and asked not to create? Then nothing else
569 /* otherwise fall through to create it */
573 * If the cache wasn't hit or it yielded an "does-not-exist" and we want
574 * to create an entry.
577 /* search the lookup table */
578 ent = (ReorderBufferTXNByIdEnt *)
579 hash_search(rb->by_txn,
581 create ? HASH_ENTER : HASH_FIND,
587 /* initialize the new entry, if creation was requested */
590 ent->txn = ReorderBufferGetTXN(rb);
593 txn->first_lsn = lsn;
594 txn->restart_decoding_lsn = rb->current_restart_decoding_lsn;
598 dlist_push_tail(&rb->toplevel_by_lsn, &txn->node);
599 AssertTXNLsnOrder(rb);
603 txn = NULL; /* not found and not asked to create */
606 rb->by_txn_last_xid = xid;
607 rb->by_txn_last_txn = txn;
612 Assert(!create || txn != NULL);
617 * Queue a change into a transaction so it can be replayed upon commit.
620 ReorderBufferQueueChange(ReorderBuffer *rb, TransactionId xid, XLogRecPtr lsn,
621 ReorderBufferChange *change)
623 ReorderBufferTXN *txn;
625 txn = ReorderBufferTXNByXid(rb, xid, true, NULL, lsn, true);
628 Assert(InvalidXLogRecPtr != lsn);
629 dlist_push_tail(&txn->changes, &change->node);
633 ReorderBufferCheckSerializeTXN(rb, txn);
637 * Queue message into a transaction so it can be processed upon commit.
640 ReorderBufferQueueMessage(ReorderBuffer *rb, TransactionId xid,
641 Snapshot snapshot, XLogRecPtr lsn,
642 bool transactional, const char *prefix,
643 Size message_size, const char *message)
647 MemoryContext oldcontext;
648 ReorderBufferChange *change;
650 Assert(xid != InvalidTransactionId);
652 oldcontext = MemoryContextSwitchTo(rb->context);
654 change = ReorderBufferGetChange(rb);
655 change->action = REORDER_BUFFER_CHANGE_MESSAGE;
656 change->data.msg.prefix = pstrdup(prefix);
657 change->data.msg.message_size = message_size;
658 change->data.msg.message = palloc(message_size);
659 memcpy(change->data.msg.message, message, message_size);
661 ReorderBufferQueueChange(rb, xid, lsn, change);
663 MemoryContextSwitchTo(oldcontext);
667 ReorderBufferTXN *txn = NULL;
668 volatile Snapshot snapshot_now = snapshot;
670 if (xid != InvalidTransactionId)
671 txn = ReorderBufferTXNByXid(rb, xid, true, NULL, lsn, true);
673 /* setup snapshot to allow catalog access */
674 SetupHistoricSnapshot(snapshot_now, NULL);
677 rb->message(rb, txn, lsn, false, prefix, message_size, message);
679 TeardownHistoricSnapshot(false);
683 TeardownHistoricSnapshot(true);
692 AssertTXNLsnOrder(ReorderBuffer *rb)
694 #ifdef USE_ASSERT_CHECKING
696 XLogRecPtr prev_first_lsn = InvalidXLogRecPtr;
698 dlist_foreach(iter, &rb->toplevel_by_lsn)
700 ReorderBufferTXN *cur_txn;
702 cur_txn = dlist_container(ReorderBufferTXN, node, iter.cur);
703 Assert(cur_txn->first_lsn != InvalidXLogRecPtr);
705 if (cur_txn->end_lsn != InvalidXLogRecPtr)
706 Assert(cur_txn->first_lsn <= cur_txn->end_lsn);
708 if (prev_first_lsn != InvalidXLogRecPtr)
709 Assert(prev_first_lsn < cur_txn->first_lsn);
711 Assert(!cur_txn->is_known_as_subxact);
712 prev_first_lsn = cur_txn->first_lsn;
718 ReorderBufferGetOldestTXN(ReorderBuffer *rb)
720 ReorderBufferTXN *txn;
722 if (dlist_is_empty(&rb->toplevel_by_lsn))
725 AssertTXNLsnOrder(rb);
727 txn = dlist_head_element(ReorderBufferTXN, node, &rb->toplevel_by_lsn);
729 Assert(!txn->is_known_as_subxact);
730 Assert(txn->first_lsn != InvalidXLogRecPtr);
735 ReorderBufferSetRestartPoint(ReorderBuffer *rb, XLogRecPtr ptr)
737 rb->current_restart_decoding_lsn = ptr;
741 ReorderBufferAssignChild(ReorderBuffer *rb, TransactionId xid,
742 TransactionId subxid, XLogRecPtr lsn)
744 ReorderBufferTXN *txn;
745 ReorderBufferTXN *subtxn;
749 txn = ReorderBufferTXNByXid(rb, xid, true, &new_top, lsn, true);
750 subtxn = ReorderBufferTXNByXid(rb, subxid, true, &new_sub, lsn, false);
755 * we assign subtransactions to top level transaction even if we don't
756 * have data for it yet, assignment records frequently reference xids
757 * that have not yet produced any records. Knowing those aren't top
758 * level xids allows us to make processing cheaper in some places.
760 dlist_push_tail(&txn->subtxns, &subtxn->node);
763 else if (!subtxn->is_known_as_subxact)
765 subtxn->is_known_as_subxact = true;
766 Assert(subtxn->nsubtxns == 0);
768 /* remove from lsn order list of top-level transactions */
769 dlist_delete(&subtxn->node);
771 /* add to toplevel transaction */
772 dlist_push_tail(&txn->subtxns, &subtxn->node);
777 elog(ERROR, "existing subxact assigned to unknown toplevel xact");
782 * Associate a subtransaction with its toplevel transaction at commit
783 * time. There may be no further changes added after this.
786 ReorderBufferCommitChild(ReorderBuffer *rb, TransactionId xid,
787 TransactionId subxid, XLogRecPtr commit_lsn,
790 ReorderBufferTXN *txn;
791 ReorderBufferTXN *subtxn;
793 subtxn = ReorderBufferTXNByXid(rb, subxid, false, NULL,
794 InvalidXLogRecPtr, false);
797 * No need to do anything if that subtxn didn't contain any changes
802 txn = ReorderBufferTXNByXid(rb, xid, false, NULL, commit_lsn, true);
805 elog(ERROR, "subxact logged without previous toplevel record");
808 * Pass our base snapshot to the parent transaction if it doesn't have
809 * one, or ours is older. That can happen if there are no changes in the
810 * toplevel transaction but in one of the child transactions. This allows
811 * the parent to simply use its base snapshot initially.
813 if (subtxn->base_snapshot != NULL &&
814 (txn->base_snapshot == NULL ||
815 txn->base_snapshot_lsn > subtxn->base_snapshot_lsn))
817 txn->base_snapshot = subtxn->base_snapshot;
818 txn->base_snapshot_lsn = subtxn->base_snapshot_lsn;
819 subtxn->base_snapshot = NULL;
820 subtxn->base_snapshot_lsn = InvalidXLogRecPtr;
823 subtxn->final_lsn = commit_lsn;
824 subtxn->end_lsn = end_lsn;
826 if (!subtxn->is_known_as_subxact)
828 subtxn->is_known_as_subxact = true;
829 Assert(subtxn->nsubtxns == 0);
831 /* remove from lsn order list of top-level transactions */
832 dlist_delete(&subtxn->node);
834 /* add to subtransaction list */
835 dlist_push_tail(&txn->subtxns, &subtxn->node);
842 * Support for efficiently iterating over a transaction's and its
843 * subtransactions' changes.
845 * We do by doing a k-way merge between transactions/subtransactions. For that
846 * we model the current heads of the different transactions as a binary heap
847 * so we easily know which (sub-)transaction has the change with the smallest
850 * We assume the changes in individual transactions are already sorted by LSN.
854 * Binary heap comparison function.
857 ReorderBufferIterCompare(Datum a, Datum b, void *arg)
859 ReorderBufferIterTXNState *state = (ReorderBufferIterTXNState *) arg;
860 XLogRecPtr pos_a = state->entries[DatumGetInt32(a)].lsn;
861 XLogRecPtr pos_b = state->entries[DatumGetInt32(b)].lsn;
865 else if (pos_a == pos_b)
871 * Allocate & initialize an iterator which iterates in lsn order over a
872 * transaction and all its subtransactions.
874 static ReorderBufferIterTXNState *
875 ReorderBufferIterTXNInit(ReorderBuffer *rb, ReorderBufferTXN *txn)
878 ReorderBufferIterTXNState *state;
879 dlist_iter cur_txn_i;
883 * Calculate the size of our heap: one element for every transaction that
884 * contains changes. (Besides the transactions already in the reorder
885 * buffer, we count the one we were directly passed.)
887 if (txn->nentries > 0)
890 dlist_foreach(cur_txn_i, &txn->subtxns)
892 ReorderBufferTXN *cur_txn;
894 cur_txn = dlist_container(ReorderBufferTXN, node, cur_txn_i.cur);
896 if (cur_txn->nentries > 0)
901 * TODO: Consider adding fastpath for the rather common nr_txns=1 case, no
902 * need to allocate/build a heap then.
905 /* allocate iteration state */
906 state = (ReorderBufferIterTXNState *)
907 MemoryContextAllocZero(rb->context,
908 sizeof(ReorderBufferIterTXNState) +
909 sizeof(ReorderBufferIterTXNEntry) * nr_txns);
911 state->nr_txns = nr_txns;
912 dlist_init(&state->old_change);
914 for (off = 0; off < state->nr_txns; off++)
916 state->entries[off].fd = -1;
917 state->entries[off].segno = 0;
921 state->heap = binaryheap_allocate(state->nr_txns,
922 ReorderBufferIterCompare,
926 * Now insert items into the binary heap, in an unordered fashion. (We
927 * will run a heap assembly step at the end; this is more efficient.)
932 /* add toplevel transaction if it contains changes */
933 if (txn->nentries > 0)
935 ReorderBufferChange *cur_change;
937 if (txn->nentries != txn->nentries_mem)
939 /* serialize remaining changes */
940 ReorderBufferSerializeTXN(rb, txn);
941 ReorderBufferRestoreChanges(rb, txn, &state->entries[off].fd,
942 &state->entries[off].segno);
945 cur_change = dlist_head_element(ReorderBufferChange, node,
948 state->entries[off].lsn = cur_change->lsn;
949 state->entries[off].change = cur_change;
950 state->entries[off].txn = txn;
952 binaryheap_add_unordered(state->heap, Int32GetDatum(off++));
955 /* add subtransactions if they contain changes */
956 dlist_foreach(cur_txn_i, &txn->subtxns)
958 ReorderBufferTXN *cur_txn;
960 cur_txn = dlist_container(ReorderBufferTXN, node, cur_txn_i.cur);
962 if (cur_txn->nentries > 0)
964 ReorderBufferChange *cur_change;
966 if (cur_txn->nentries != cur_txn->nentries_mem)
968 /* serialize remaining changes */
969 ReorderBufferSerializeTXN(rb, cur_txn);
970 ReorderBufferRestoreChanges(rb, cur_txn,
971 &state->entries[off].fd,
972 &state->entries[off].segno);
974 cur_change = dlist_head_element(ReorderBufferChange, node,
977 state->entries[off].lsn = cur_change->lsn;
978 state->entries[off].change = cur_change;
979 state->entries[off].txn = cur_txn;
981 binaryheap_add_unordered(state->heap, Int32GetDatum(off++));
985 /* assemble a valid binary heap */
986 binaryheap_build(state->heap);
992 * Return the next change when iterating over a transaction and its
995 * Returns NULL when no further changes exist.
997 static ReorderBufferChange *
998 ReorderBufferIterTXNNext(ReorderBuffer *rb, ReorderBufferIterTXNState *state)
1000 ReorderBufferChange *change;
1001 ReorderBufferIterTXNEntry *entry;
1004 /* nothing there anymore */
1005 if (state->heap->bh_size == 0)
1008 off = DatumGetInt32(binaryheap_first(state->heap));
1009 entry = &state->entries[off];
1011 /* free memory we might have "leaked" in the previous *Next call */
1012 if (!dlist_is_empty(&state->old_change))
1014 change = dlist_container(ReorderBufferChange, node,
1015 dlist_pop_head_node(&state->old_change));
1016 ReorderBufferReturnChange(rb, change);
1017 Assert(dlist_is_empty(&state->old_change));
1020 change = entry->change;
1023 * update heap with information about which transaction has the next
1024 * relevant change in LSN order
1027 /* there are in-memory changes */
1028 if (dlist_has_next(&entry->txn->changes, &entry->change->node))
1030 dlist_node *next = dlist_next_node(&entry->txn->changes, &change->node);
1031 ReorderBufferChange *next_change =
1032 dlist_container(ReorderBufferChange, node, next);
1034 /* txn stays the same */
1035 state->entries[off].lsn = next_change->lsn;
1036 state->entries[off].change = next_change;
1038 binaryheap_replace_first(state->heap, Int32GetDatum(off));
1042 /* try to load changes from disk */
1043 if (entry->txn->nentries != entry->txn->nentries_mem)
1046 * Ugly: restoring changes will reuse *Change records, thus delete the
1047 * current one from the per-tx list and only free in the next call.
1049 dlist_delete(&change->node);
1050 dlist_push_tail(&state->old_change, &change->node);
1052 if (ReorderBufferRestoreChanges(rb, entry->txn, &entry->fd,
1053 &state->entries[off].segno))
1055 /* successfully restored changes from disk */
1056 ReorderBufferChange *next_change =
1057 dlist_head_element(ReorderBufferChange, node,
1058 &entry->txn->changes);
1060 elog(DEBUG2, "restored %u/%u changes from disk",
1061 (uint32) entry->txn->nentries_mem,
1062 (uint32) entry->txn->nentries);
1064 Assert(entry->txn->nentries_mem);
1065 /* txn stays the same */
1066 state->entries[off].lsn = next_change->lsn;
1067 state->entries[off].change = next_change;
1068 binaryheap_replace_first(state->heap, Int32GetDatum(off));
1074 /* ok, no changes there anymore, remove */
1075 binaryheap_remove_first(state->heap);
1081 * Deallocate the iterator
1084 ReorderBufferIterTXNFinish(ReorderBuffer *rb,
1085 ReorderBufferIterTXNState *state)
1089 for (off = 0; off < state->nr_txns; off++)
1091 if (state->entries[off].fd != -1)
1092 CloseTransientFile(state->entries[off].fd);
1095 /* free memory we might have "leaked" in the last *Next call */
1096 if (!dlist_is_empty(&state->old_change))
1098 ReorderBufferChange *change;
1100 change = dlist_container(ReorderBufferChange, node,
1101 dlist_pop_head_node(&state->old_change));
1102 ReorderBufferReturnChange(rb, change);
1103 Assert(dlist_is_empty(&state->old_change));
1106 binaryheap_free(state->heap);
1111 * Cleanup the contents of a transaction, usually after the transaction
1112 * committed or aborted.
1115 ReorderBufferCleanupTXN(ReorderBuffer *rb, ReorderBufferTXN *txn)
1118 dlist_mutable_iter iter;
1120 /* cleanup subtransactions & their changes */
1121 dlist_foreach_modify(iter, &txn->subtxns)
1123 ReorderBufferTXN *subtxn;
1125 subtxn = dlist_container(ReorderBufferTXN, node, iter.cur);
1128 * Subtransactions are always associated to the toplevel TXN, even if
1129 * they originally were happening inside another subtxn, so we won't
1130 * ever recurse more than one level deep here.
1132 Assert(subtxn->is_known_as_subxact);
1133 Assert(subtxn->nsubtxns == 0);
1135 ReorderBufferCleanupTXN(rb, subtxn);
1138 /* cleanup changes in the toplevel txn */
1139 dlist_foreach_modify(iter, &txn->changes)
1141 ReorderBufferChange *change;
1143 change = dlist_container(ReorderBufferChange, node, iter.cur);
1145 ReorderBufferReturnChange(rb, change);
1149 * Cleanup the tuplecids we stored for decoding catalog snapshot access.
1150 * They are always stored in the toplevel transaction.
1152 dlist_foreach_modify(iter, &txn->tuplecids)
1154 ReorderBufferChange *change;
1156 change = dlist_container(ReorderBufferChange, node, iter.cur);
1157 Assert(change->action == REORDER_BUFFER_CHANGE_INTERNAL_TUPLECID);
1158 ReorderBufferReturnChange(rb, change);
1161 if (txn->base_snapshot != NULL)
1163 SnapBuildSnapDecRefcount(txn->base_snapshot);
1164 txn->base_snapshot = NULL;
1165 txn->base_snapshot_lsn = InvalidXLogRecPtr;
1169 * Remove TXN from its containing list.
1171 * Note: if txn->is_known_as_subxact, we are deleting the TXN from its
1172 * parent's list of known subxacts; this leaves the parent's nsubxacts
1173 * count too high, but we don't care. Otherwise, we are deleting the TXN
1174 * from the LSN-ordered list of toplevel TXNs.
1176 dlist_delete(&txn->node);
1178 /* now remove reference from buffer */
1179 hash_search(rb->by_txn,
1185 /* remove entries spilled to disk */
1186 if (txn->nentries != txn->nentries_mem)
1187 ReorderBufferRestoreCleanup(rb, txn);
1190 ReorderBufferReturnTXN(rb, txn);
1194 * Build a hash with a (relfilenode, ctid) -> (cmin, cmax) mapping for use by
1195 * tqual.c's HeapTupleSatisfiesHistoricMVCC.
1198 ReorderBufferBuildTupleCidHash(ReorderBuffer *rb, ReorderBufferTXN *txn)
1203 if (!txn->has_catalog_changes || dlist_is_empty(&txn->tuplecids))
1206 memset(&hash_ctl, 0, sizeof(hash_ctl));
1208 hash_ctl.keysize = sizeof(ReorderBufferTupleCidKey);
1209 hash_ctl.entrysize = sizeof(ReorderBufferTupleCidEnt);
1210 hash_ctl.hcxt = rb->context;
1213 * create the hash with the exact number of to-be-stored tuplecids from
1216 txn->tuplecid_hash =
1217 hash_create("ReorderBufferTupleCid", txn->ntuplecids, &hash_ctl,
1218 HASH_ELEM | HASH_BLOBS | HASH_CONTEXT);
1220 dlist_foreach(iter, &txn->tuplecids)
1222 ReorderBufferTupleCidKey key;
1223 ReorderBufferTupleCidEnt *ent;
1225 ReorderBufferChange *change;
1227 change = dlist_container(ReorderBufferChange, node, iter.cur);
1229 Assert(change->action == REORDER_BUFFER_CHANGE_INTERNAL_TUPLECID);
1231 /* be careful about padding */
1232 memset(&key, 0, sizeof(ReorderBufferTupleCidKey));
1234 key.relnode = change->data.tuplecid.node;
1236 ItemPointerCopy(&change->data.tuplecid.tid,
1239 ent = (ReorderBufferTupleCidEnt *)
1240 hash_search(txn->tuplecid_hash,
1242 HASH_ENTER | HASH_FIND,
1246 ent->cmin = change->data.tuplecid.cmin;
1247 ent->cmax = change->data.tuplecid.cmax;
1248 ent->combocid = change->data.tuplecid.combocid;
1252 Assert(ent->cmin == change->data.tuplecid.cmin);
1253 Assert(ent->cmax == InvalidCommandId ||
1254 ent->cmax == change->data.tuplecid.cmax);
1257 * if the tuple got valid in this transaction and now got deleted
1258 * we already have a valid cmin stored. The cmax will be
1259 * InvalidCommandId though.
1261 ent->cmax = change->data.tuplecid.cmax;
1267 * Copy a provided snapshot so we can modify it privately. This is needed so
1268 * that catalog modifying transactions can look into intermediate catalog
1272 ReorderBufferCopySnap(ReorderBuffer *rb, Snapshot orig_snap,
1273 ReorderBufferTXN *txn, CommandId cid)
1280 size = sizeof(SnapshotData) +
1281 sizeof(TransactionId) * orig_snap->xcnt +
1282 sizeof(TransactionId) * (txn->nsubtxns + 1);
1284 snap = MemoryContextAllocZero(rb->context, size);
1285 memcpy(snap, orig_snap, sizeof(SnapshotData));
1287 snap->copied = true;
1288 snap->active_count = 1; /* mark as active so nobody frees it */
1289 snap->regd_count = 0;
1290 snap->xip = (TransactionId *) (snap + 1);
1292 memcpy(snap->xip, orig_snap->xip, sizeof(TransactionId) * snap->xcnt);
1295 * snap->subxip contains all txids that belong to our transaction which we
1296 * need to check via cmin/cmax. Thats why we store the toplevel
1297 * transaction in there as well.
1299 snap->subxip = snap->xip + snap->xcnt;
1300 snap->subxip[i++] = txn->xid;
1303 * nsubxcnt isn't decreased when subtransactions abort, so count manually.
1304 * Since it's an upper boundary it is safe to use it for the allocation
1309 dlist_foreach(iter, &txn->subtxns)
1311 ReorderBufferTXN *sub_txn;
1313 sub_txn = dlist_container(ReorderBufferTXN, node, iter.cur);
1314 snap->subxip[i++] = sub_txn->xid;
1318 /* sort so we can bsearch() later */
1319 qsort(snap->subxip, snap->subxcnt, sizeof(TransactionId), xidComparator);
1321 /* store the specified current CommandId */
1328 * Free a previously ReorderBufferCopySnap'ed snapshot
1331 ReorderBufferFreeSnap(ReorderBuffer *rb, Snapshot snap)
1336 SnapBuildSnapDecRefcount(snap);
1340 * Perform the replay of a transaction and it's non-aborted subtransactions.
1342 * Subtransactions previously have to be processed by
1343 * ReorderBufferCommitChild(), even if previously assigned to the toplevel
1344 * transaction with ReorderBufferAssignChild.
1346 * We currently can only decode a transaction's contents in when their commit
1347 * record is read because that's currently the only place where we know about
1348 * cache invalidations. Thus, once a toplevel commit is read, we iterate over
1349 * the top and subtransactions (using a k-way merge) and replay the changes in
1353 ReorderBufferCommit(ReorderBuffer *rb, TransactionId xid,
1354 XLogRecPtr commit_lsn, XLogRecPtr end_lsn,
1355 TimestampTz commit_time,
1356 RepOriginId origin_id, XLogRecPtr origin_lsn)
1358 ReorderBufferTXN *txn;
1359 volatile Snapshot snapshot_now;
1360 volatile CommandId command_id = FirstCommandId;
1362 ReorderBufferIterTXNState *volatile iterstate = NULL;
1364 txn = ReorderBufferTXNByXid(rb, xid, false, NULL, InvalidXLogRecPtr,
1367 /* unknown transaction, nothing to replay */
1371 txn->final_lsn = commit_lsn;
1372 txn->end_lsn = end_lsn;
1373 txn->commit_time = commit_time;
1374 txn->origin_id = origin_id;
1375 txn->origin_lsn = origin_lsn;
1378 * If this transaction didn't have any real changes in our database, it's
1379 * OK not to have a snapshot. Note that ReorderBufferCommitChild will have
1380 * transferred its snapshot to this transaction if it had one and the
1381 * toplevel tx didn't.
1383 if (txn->base_snapshot == NULL)
1385 Assert(txn->ninvalidations == 0);
1386 ReorderBufferCleanupTXN(rb, txn);
1390 snapshot_now = txn->base_snapshot;
1392 /* build data to be able to lookup the CommandIds of catalog tuples */
1393 ReorderBufferBuildTupleCidHash(rb, txn);
1395 /* setup the initial snapshot */
1396 SetupHistoricSnapshot(snapshot_now, txn->tuplecid_hash);
1399 * Decoding needs access to syscaches et al., which in turn use
1400 * heavyweight locks and such. Thus we need to have enough state around to
1401 * keep track of those. The easiest way is to simply use a transaction
1402 * internally. That also allows us to easily enforce that nothing writes
1403 * to the database by checking for xid assignments.
1405 * When we're called via the SQL SRF there's already a transaction
1406 * started, so start an explicit subtransaction there.
1408 using_subtxn = IsTransactionOrTransactionBlock();
1412 ReorderBufferChange *change;
1413 ReorderBufferChange *specinsert = NULL;
1416 BeginInternalSubTransaction("replay");
1418 StartTransactionCommand();
1422 iterstate = ReorderBufferIterTXNInit(rb, txn);
1423 while ((change = ReorderBufferIterTXNNext(rb, iterstate)) != NULL)
1425 Relation relation = NULL;
1428 switch (change->action)
1430 case REORDER_BUFFER_CHANGE_INTERNAL_SPEC_CONFIRM:
1433 * Confirmation for speculative insertion arrived. Simply
1434 * use as a normal record. It'll be cleaned up at the end
1435 * of INSERT processing.
1437 Assert(specinsert->data.tp.oldtuple == NULL);
1438 change = specinsert;
1439 change->action = REORDER_BUFFER_CHANGE_INSERT;
1441 /* intentionally fall through */
1442 case REORDER_BUFFER_CHANGE_INSERT:
1443 case REORDER_BUFFER_CHANGE_UPDATE:
1444 case REORDER_BUFFER_CHANGE_DELETE:
1445 Assert(snapshot_now);
1447 reloid = RelidByRelfilenode(change->data.tp.relnode.spcNode,
1448 change->data.tp.relnode.relNode);
1451 * Catalog tuple without data, emitted while catalog was
1452 * in the process of being rewritten.
1454 if (reloid == InvalidOid &&
1455 change->data.tp.newtuple == NULL &&
1456 change->data.tp.oldtuple == NULL)
1458 else if (reloid == InvalidOid)
1459 elog(ERROR, "could not map filenode \"%s\" to relation OID",
1460 relpathperm(change->data.tp.relnode,
1463 relation = RelationIdGetRelation(reloid);
1465 if (relation == NULL)
1466 elog(ERROR, "could not open relation with OID %u (for filenode \"%s\")",
1468 relpathperm(change->data.tp.relnode,
1471 if (!RelationIsLogicallyLogged(relation))
1475 * For now ignore sequence changes entirely. Most of the
1476 * time they don't log changes using records we
1477 * understand, so it doesn't make sense to handle the few
1480 if (relation->rd_rel->relkind == RELKIND_SEQUENCE)
1483 /* user-triggered change */
1484 if (!IsToastRelation(relation))
1486 ReorderBufferToastReplace(rb, txn, relation, change);
1487 rb->apply_change(rb, txn, relation, change);
1490 * Only clear reassembled toast chunks if we're sure
1491 * they're not required anymore. The creator of the
1494 if (change->data.tp.clear_toast_afterwards)
1495 ReorderBufferToastReset(rb, txn);
1497 /* we're not interested in toast deletions */
1498 else if (change->action == REORDER_BUFFER_CHANGE_INSERT)
1501 * Need to reassemble the full toasted Datum in
1502 * memory, to ensure the chunks don't get reused till
1503 * we're done remove it from the list of this
1504 * transaction's changes. Otherwise it will get
1505 * freed/reused while restoring spooled data from
1508 dlist_delete(&change->node);
1509 ReorderBufferToastAppendChunk(rb, txn, relation,
1516 * Either speculative insertion was confirmed, or it was
1517 * unsuccessful and the record isn't needed anymore.
1519 if (specinsert != NULL)
1521 ReorderBufferReturnChange(rb, specinsert);
1525 if (relation != NULL)
1527 RelationClose(relation);
1532 case REORDER_BUFFER_CHANGE_INTERNAL_SPEC_INSERT:
1535 * Speculative insertions are dealt with by delaying the
1536 * processing of the insert until the confirmation record
1537 * arrives. For that we simply unlink the record from the
1538 * chain, so it does not get freed/reused while restoring
1539 * spooled data from disk.
1541 * This is safe in the face of concurrent catalog changes
1542 * because the relevant relation can't be changed between
1543 * speculative insertion and confirmation due to
1544 * CheckTableNotInUse() and locking.
1547 /* clear out a pending (and thus failed) speculation */
1548 if (specinsert != NULL)
1550 ReorderBufferReturnChange(rb, specinsert);
1554 /* and memorize the pending insertion */
1555 dlist_delete(&change->node);
1556 specinsert = change;
1559 case REORDER_BUFFER_CHANGE_MESSAGE:
1560 rb->message(rb, txn, change->lsn, true,
1561 change->data.msg.prefix,
1562 change->data.msg.message_size,
1563 change->data.msg.message);
1566 case REORDER_BUFFER_CHANGE_INTERNAL_SNAPSHOT:
1567 /* get rid of the old */
1568 TeardownHistoricSnapshot(false);
1570 if (snapshot_now->copied)
1572 ReorderBufferFreeSnap(rb, snapshot_now);
1574 ReorderBufferCopySnap(rb, change->data.snapshot,
1579 * Restored from disk, need to be careful not to double
1580 * free. We could introduce refcounting for that, but for
1581 * now this seems infrequent enough not to care.
1583 else if (change->data.snapshot->copied)
1586 ReorderBufferCopySnap(rb, change->data.snapshot,
1591 snapshot_now = change->data.snapshot;
1595 /* and continue with the new one */
1596 SetupHistoricSnapshot(snapshot_now, txn->tuplecid_hash);
1599 case REORDER_BUFFER_CHANGE_INTERNAL_COMMAND_ID:
1600 Assert(change->data.command_id != InvalidCommandId);
1602 if (command_id < change->data.command_id)
1604 command_id = change->data.command_id;
1606 if (!snapshot_now->copied)
1608 /* we don't use the global one anymore */
1609 snapshot_now = ReorderBufferCopySnap(rb, snapshot_now,
1613 snapshot_now->curcid = command_id;
1615 TeardownHistoricSnapshot(false);
1616 SetupHistoricSnapshot(snapshot_now, txn->tuplecid_hash);
1619 * Every time the CommandId is incremented, we could
1620 * see new catalog contents, so execute all
1623 ReorderBufferExecuteInvalidations(rb, txn);
1628 case REORDER_BUFFER_CHANGE_INTERNAL_TUPLECID:
1629 elog(ERROR, "tuplecid value in changequeue");
1635 * There's a speculative insertion remaining, just clean in up, it
1636 * can't have been successful, otherwise we'd gotten a confirmation
1641 ReorderBufferReturnChange(rb, specinsert);
1645 /* clean up the iterator */
1646 ReorderBufferIterTXNFinish(rb, iterstate);
1649 /* call commit callback */
1650 rb->commit(rb, txn, commit_lsn);
1652 /* this is just a sanity check against bad output plugin behaviour */
1653 if (GetCurrentTransactionIdIfAny() != InvalidTransactionId)
1654 elog(ERROR, "output plugin used XID %u",
1655 GetCurrentTransactionId());
1658 TeardownHistoricSnapshot(false);
1661 * Aborting the current (sub-)transaction as a whole has the right
1662 * semantics. We want all locks acquired in here to be released, not
1663 * reassigned to the parent and we do not want any database access
1664 * have persistent effects.
1666 AbortCurrentTransaction();
1668 /* make sure there's no cache pollution */
1669 ReorderBufferExecuteInvalidations(rb, txn);
1672 RollbackAndReleaseCurrentSubTransaction();
1674 if (snapshot_now->copied)
1675 ReorderBufferFreeSnap(rb, snapshot_now);
1677 /* remove potential on-disk data, and deallocate */
1678 ReorderBufferCleanupTXN(rb, txn);
1682 /* TODO: Encapsulate cleanup from the PG_TRY and PG_CATCH blocks */
1684 ReorderBufferIterTXNFinish(rb, iterstate);
1686 TeardownHistoricSnapshot(true);
1689 * Force cache invalidation to happen outside of a valid transaction
1690 * to prevent catalog access as we just caught an error.
1692 AbortCurrentTransaction();
1694 /* make sure there's no cache pollution */
1695 ReorderBufferExecuteInvalidations(rb, txn);
1698 RollbackAndReleaseCurrentSubTransaction();
1700 if (snapshot_now->copied)
1701 ReorderBufferFreeSnap(rb, snapshot_now);
1703 /* remove potential on-disk data, and deallocate */
1704 ReorderBufferCleanupTXN(rb, txn);
1712 * Abort a transaction that possibly has previous changes. Needs to be first
1713 * called for subtransactions and then for the toplevel xid.
1715 * NB: Transactions handled here have to have actively aborted (i.e. have
1716 * produced an abort record). Implicitly aborted transactions are handled via
1717 * ReorderBufferAbortOld(); transactions we're just not interesteded in, but
1718 * which have committed are handled in ReorderBufferForget().
1720 * This function purges this transaction and its contents from memory and
1724 ReorderBufferAbort(ReorderBuffer *rb, TransactionId xid, XLogRecPtr lsn)
1726 ReorderBufferTXN *txn;
1728 txn = ReorderBufferTXNByXid(rb, xid, false, NULL, InvalidXLogRecPtr,
1731 /* unknown, nothing to remove */
1736 txn->final_lsn = lsn;
1738 /* remove potential on-disk data, and deallocate */
1739 ReorderBufferCleanupTXN(rb, txn);
1743 * Abort all transactions that aren't actually running anymore because the
1746 * NB: These really have to be transactions that have aborted due to a server
1747 * crash/immediate restart, as we don't deal with invalidations here.
1750 ReorderBufferAbortOld(ReorderBuffer *rb, TransactionId oldestRunningXid)
1752 dlist_mutable_iter it;
1755 * Iterate through all (potential) toplevel TXNs and abort all that are
1756 * older than what possibly can be running. Once we've found the first
1757 * that is alive we stop, there might be some that acquired an xid earlier
1758 * but started writing later, but it's unlikely and they will cleaned up
1759 * in a later call to ReorderBufferAbortOld().
1761 dlist_foreach_modify(it, &rb->toplevel_by_lsn)
1763 ReorderBufferTXN *txn;
1765 txn = dlist_container(ReorderBufferTXN, node, it.cur);
1767 if (TransactionIdPrecedes(txn->xid, oldestRunningXid))
1769 elog(DEBUG1, "aborting old transaction %u", txn->xid);
1771 /* remove potential on-disk data, and deallocate this tx */
1772 ReorderBufferCleanupTXN(rb, txn);
1780 * Forget the contents of a transaction if we aren't interested in it's
1781 * contents. Needs to be first called for subtransactions and then for the
1784 * This is significantly different to ReorderBufferAbort() because
1785 * transactions that have committed need to be treated differenly from aborted
1786 * ones since they may have modified the catalog.
1788 * Note that this is only allowed to be called in the moment a transaction
1789 * commit has just been read, not earlier; otherwise later records referring
1790 * to this xid might re-create the transaction incompletely.
1793 ReorderBufferForget(ReorderBuffer *rb, TransactionId xid, XLogRecPtr lsn)
1795 ReorderBufferTXN *txn;
1797 txn = ReorderBufferTXNByXid(rb, xid, false, NULL, InvalidXLogRecPtr,
1800 /* unknown, nothing to forget */
1805 txn->final_lsn = lsn;
1808 * Process cache invalidation messages if there are any. Even if we're not
1809 * interested in the transaction's contents, it could have manipulated the
1810 * catalog and we need to update the caches according to that.
1812 if (txn->base_snapshot != NULL && txn->ninvalidations > 0)
1813 ReorderBufferImmediateInvalidation(rb, txn->ninvalidations,
1814 txn->invalidations);
1816 Assert(txn->ninvalidations == 0);
1818 /* remove potential on-disk data, and deallocate */
1819 ReorderBufferCleanupTXN(rb, txn);
1823 * Execute invalidations happening outside the context of a decoded
1824 * transaction. That currently happens either for xid-less commits
1825 * (c.f. RecordTransactionCommit()) or for invalidations in uninteresting
1826 * transactions (via ReorderBufferForget()).
1829 ReorderBufferImmediateInvalidation(ReorderBuffer *rb, uint32 ninvalidations,
1830 SharedInvalidationMessage *invalidations)
1832 bool use_subtxn = IsTransactionOrTransactionBlock();
1836 BeginInternalSubTransaction("replay");
1839 * Force invalidations to happen outside of a valid transaction - that way
1840 * entries will just be marked as invalid without accessing the catalog.
1841 * That's advantageous because we don't need to setup the full state
1842 * necessary for catalog access.
1845 AbortCurrentTransaction();
1847 for (i = 0; i < ninvalidations; i++)
1848 LocalExecuteInvalidationMessage(&invalidations[i]);
1851 RollbackAndReleaseCurrentSubTransaction();
1855 * Tell reorderbuffer about an xid seen in the WAL stream. Has to be called at
1856 * least once for every xid in XLogRecord->xl_xid (other places in records
1857 * may, but do not have to be passed through here).
1859 * Reorderbuffer keeps some datastructures about transactions in LSN order,
1860 * for efficiency. To do that it has to know about when transactions are seen
1861 * first in the WAL. As many types of records are not actually interesting for
1862 * logical decoding, they do not necessarily pass though here.
1865 ReorderBufferProcessXid(ReorderBuffer *rb, TransactionId xid, XLogRecPtr lsn)
1867 /* many records won't have an xid assigned, centralize check here */
1868 if (xid != InvalidTransactionId)
1869 ReorderBufferTXNByXid(rb, xid, true, NULL, lsn, true);
1873 * Add a new snapshot to this transaction that may only used after lsn 'lsn'
1874 * because the previous snapshot doesn't describe the catalog correctly for
1878 ReorderBufferAddSnapshot(ReorderBuffer *rb, TransactionId xid,
1879 XLogRecPtr lsn, Snapshot snap)
1881 ReorderBufferChange *change = ReorderBufferGetChange(rb);
1883 change->data.snapshot = snap;
1884 change->action = REORDER_BUFFER_CHANGE_INTERNAL_SNAPSHOT;
1886 ReorderBufferQueueChange(rb, xid, lsn, change);
1890 * Setup the base snapshot of a transaction. The base snapshot is the snapshot
1891 * that is used to decode all changes until either this transaction modifies
1892 * the catalog or another catalog modifying transaction commits.
1894 * Needs to be called before any changes are added with
1895 * ReorderBufferQueueChange().
1898 ReorderBufferSetBaseSnapshot(ReorderBuffer *rb, TransactionId xid,
1899 XLogRecPtr lsn, Snapshot snap)
1901 ReorderBufferTXN *txn;
1904 txn = ReorderBufferTXNByXid(rb, xid, true, &is_new, lsn, true);
1905 Assert(txn->base_snapshot == NULL);
1906 Assert(snap != NULL);
1908 txn->base_snapshot = snap;
1909 txn->base_snapshot_lsn = lsn;
1913 * Access the catalog with this CommandId at this point in the changestream.
1915 * May only be called for command ids > 1
1918 ReorderBufferAddNewCommandId(ReorderBuffer *rb, TransactionId xid,
1919 XLogRecPtr lsn, CommandId cid)
1921 ReorderBufferChange *change = ReorderBufferGetChange(rb);
1923 change->data.command_id = cid;
1924 change->action = REORDER_BUFFER_CHANGE_INTERNAL_COMMAND_ID;
1926 ReorderBufferQueueChange(rb, xid, lsn, change);
1931 * Add new (relfilenode, tid) -> (cmin, cmax) mappings.
1934 ReorderBufferAddNewTupleCids(ReorderBuffer *rb, TransactionId xid,
1935 XLogRecPtr lsn, RelFileNode node,
1936 ItemPointerData tid, CommandId cmin,
1937 CommandId cmax, CommandId combocid)
1939 ReorderBufferChange *change = ReorderBufferGetChange(rb);
1940 ReorderBufferTXN *txn;
1942 txn = ReorderBufferTXNByXid(rb, xid, true, NULL, lsn, true);
1944 change->data.tuplecid.node = node;
1945 change->data.tuplecid.tid = tid;
1946 change->data.tuplecid.cmin = cmin;
1947 change->data.tuplecid.cmax = cmax;
1948 change->data.tuplecid.combocid = combocid;
1950 change->action = REORDER_BUFFER_CHANGE_INTERNAL_TUPLECID;
1952 dlist_push_tail(&txn->tuplecids, &change->node);
1957 * Setup the invalidation of the toplevel transaction.
1959 * This needs to be done before ReorderBufferCommit is called!
1962 ReorderBufferAddInvalidations(ReorderBuffer *rb, TransactionId xid,
1963 XLogRecPtr lsn, Size nmsgs,
1964 SharedInvalidationMessage *msgs)
1966 ReorderBufferTXN *txn;
1968 txn = ReorderBufferTXNByXid(rb, xid, true, NULL, lsn, true);
1970 if (txn->ninvalidations != 0)
1971 elog(ERROR, "only ever add one set of invalidations");
1975 txn->ninvalidations = nmsgs;
1976 txn->invalidations = (SharedInvalidationMessage *)
1977 MemoryContextAlloc(rb->context,
1978 sizeof(SharedInvalidationMessage) * nmsgs);
1979 memcpy(txn->invalidations, msgs,
1980 sizeof(SharedInvalidationMessage) * nmsgs);
1984 * Apply all invalidations we know. Possibly we only need parts at this point
1985 * in the changestream but we don't know which those are.
1988 ReorderBufferExecuteInvalidations(ReorderBuffer *rb, ReorderBufferTXN *txn)
1992 for (i = 0; i < txn->ninvalidations; i++)
1993 LocalExecuteInvalidationMessage(&txn->invalidations[i]);
1997 * Mark a transaction as containing catalog changes
2000 ReorderBufferXidSetCatalogChanges(ReorderBuffer *rb, TransactionId xid,
2003 ReorderBufferTXN *txn;
2005 txn = ReorderBufferTXNByXid(rb, xid, true, NULL, lsn, true);
2007 txn->has_catalog_changes = true;
2011 * Query whether a transaction is already *known* to contain catalog
2012 * changes. This can be wrong until directly before the commit!
2015 ReorderBufferXidHasCatalogChanges(ReorderBuffer *rb, TransactionId xid)
2017 ReorderBufferTXN *txn;
2019 txn = ReorderBufferTXNByXid(rb, xid, false, NULL, InvalidXLogRecPtr,
2024 return txn->has_catalog_changes;
2028 * Have we already added the first snapshot?
2031 ReorderBufferXidHasBaseSnapshot(ReorderBuffer *rb, TransactionId xid)
2033 ReorderBufferTXN *txn;
2035 txn = ReorderBufferTXNByXid(rb, xid, false, NULL, InvalidXLogRecPtr,
2038 /* transaction isn't known yet, ergo no snapshot */
2043 * TODO: It would be a nice improvement if we would check the toplevel
2044 * transaction in subtransactions, but we'd need to keep track of a bit
2047 return txn->base_snapshot != NULL;
2052 * ---------------------------------------
2053 * Disk serialization support
2054 * ---------------------------------------
2058 * Ensure the IO buffer is >= sz.
2061 ReorderBufferSerializeReserve(ReorderBuffer *rb, Size sz)
2063 if (!rb->outbufsize)
2065 rb->outbuf = MemoryContextAlloc(rb->context, sz);
2066 rb->outbufsize = sz;
2068 else if (rb->outbufsize < sz)
2070 rb->outbuf = repalloc(rb->outbuf, sz);
2071 rb->outbufsize = sz;
2076 * Check whether the transaction tx should spill its data to disk.
2079 ReorderBufferCheckSerializeTXN(ReorderBuffer *rb, ReorderBufferTXN *txn)
2082 * TODO: improve accounting so we cheaply can take subtransactions into
2085 if (txn->nentries_mem >= max_changes_in_memory)
2087 ReorderBufferSerializeTXN(rb, txn);
2088 Assert(txn->nentries_mem == 0);
2093 * Spill data of a large transaction (and its subtransactions) to disk.
2096 ReorderBufferSerializeTXN(ReorderBuffer *rb, ReorderBufferTXN *txn)
2098 dlist_iter subtxn_i;
2099 dlist_mutable_iter change_i;
2101 XLogSegNo curOpenSegNo = 0;
2103 char path[MAXPGPATH];
2105 elog(DEBUG2, "spill %u changes in XID %u to disk",
2106 (uint32) txn->nentries_mem, txn->xid);
2108 /* do the same to all child TXs */
2109 dlist_foreach(subtxn_i, &txn->subtxns)
2111 ReorderBufferTXN *subtxn;
2113 subtxn = dlist_container(ReorderBufferTXN, node, subtxn_i.cur);
2114 ReorderBufferSerializeTXN(rb, subtxn);
2117 /* serialize changestream */
2118 dlist_foreach_modify(change_i, &txn->changes)
2120 ReorderBufferChange *change;
2122 change = dlist_container(ReorderBufferChange, node, change_i.cur);
2125 * store in segment in which it belongs by start lsn, don't split over
2126 * multiple segments tho
2128 if (fd == -1 || !XLByteInSeg(change->lsn, curOpenSegNo))
2133 CloseTransientFile(fd);
2135 XLByteToSeg(change->lsn, curOpenSegNo);
2136 XLogSegNoOffsetToRecPtr(curOpenSegNo, 0, recptr);
2139 * No need to care about TLIs here, only used during a single run,
2140 * so each LSN only maps to a specific WAL record.
2142 sprintf(path, "pg_replslot/%s/xid-%u-lsn-%X-%X.snap",
2143 NameStr(MyReplicationSlot->data.name), txn->xid,
2144 (uint32) (recptr >> 32), (uint32) recptr);
2146 /* open segment, create it if necessary */
2147 fd = OpenTransientFile(path,
2148 O_CREAT | O_WRONLY | O_APPEND | PG_BINARY,
2153 (errcode_for_file_access(),
2154 errmsg("could not open file \"%s\": %m",
2158 ReorderBufferSerializeChange(rb, txn, fd, change);
2159 dlist_delete(&change->node);
2160 ReorderBufferReturnChange(rb, change);
2165 Assert(spilled == txn->nentries_mem);
2166 Assert(dlist_is_empty(&txn->changes));
2167 txn->nentries_mem = 0;
2170 CloseTransientFile(fd);
2174 * Serialize individual change to disk.
2177 ReorderBufferSerializeChange(ReorderBuffer *rb, ReorderBufferTXN *txn,
2178 int fd, ReorderBufferChange *change)
2180 ReorderBufferDiskChange *ondisk;
2181 Size sz = sizeof(ReorderBufferDiskChange);
2183 ReorderBufferSerializeReserve(rb, sz);
2185 ondisk = (ReorderBufferDiskChange *) rb->outbuf;
2186 memcpy(&ondisk->change, change, sizeof(ReorderBufferChange));
2188 switch (change->action)
2190 /* fall through these, they're all similar enough */
2191 case REORDER_BUFFER_CHANGE_INSERT:
2192 case REORDER_BUFFER_CHANGE_UPDATE:
2193 case REORDER_BUFFER_CHANGE_DELETE:
2194 case REORDER_BUFFER_CHANGE_INTERNAL_SPEC_INSERT:
2197 ReorderBufferTupleBuf *oldtup,
2202 oldtup = change->data.tp.oldtuple;
2203 newtup = change->data.tp.newtuple;
2207 sz += sizeof(HeapTupleData);
2208 oldlen = oldtup->tuple.t_len;
2214 sz += sizeof(HeapTupleData);
2215 newlen = newtup->tuple.t_len;
2219 /* make sure we have enough space */
2220 ReorderBufferSerializeReserve(rb, sz);
2222 data = ((char *) rb->outbuf) + sizeof(ReorderBufferDiskChange);
2223 /* might have been reallocated above */
2224 ondisk = (ReorderBufferDiskChange *) rb->outbuf;
2228 memcpy(data, &oldtup->tuple, sizeof(HeapTupleData));
2229 data += sizeof(HeapTupleData);
2231 memcpy(data, oldtup->tuple.t_data, oldlen);
2237 memcpy(data, &newtup->tuple, sizeof(HeapTupleData));
2238 data += sizeof(HeapTupleData);
2240 memcpy(data, newtup->tuple.t_data, newlen);
2245 case REORDER_BUFFER_CHANGE_MESSAGE:
2248 Size prefix_size = strlen(change->data.msg.prefix) + 1;
2250 sz += prefix_size + change->data.msg.message_size +
2251 sizeof(Size) + sizeof(Size);
2252 ReorderBufferSerializeReserve(rb, sz);
2254 data = ((char *) rb->outbuf) + sizeof(ReorderBufferDiskChange);
2256 /* might have been reallocated above */
2257 ondisk = (ReorderBufferDiskChange *) rb->outbuf;
2259 /* write the prefix including the size */
2260 memcpy(data, &prefix_size, sizeof(Size));
2261 data += sizeof(Size);
2262 memcpy(data, change->data.msg.prefix,
2264 data += prefix_size;
2266 /* write the message including the size */
2267 memcpy(data, &change->data.msg.message_size, sizeof(Size));
2268 data += sizeof(Size);
2269 memcpy(data, change->data.msg.message,
2270 change->data.msg.message_size);
2271 data += change->data.msg.message_size;
2275 case REORDER_BUFFER_CHANGE_INTERNAL_SNAPSHOT:
2280 snap = change->data.snapshot;
2282 sz += sizeof(SnapshotData) +
2283 sizeof(TransactionId) * snap->xcnt +
2284 sizeof(TransactionId) * snap->subxcnt
2287 /* make sure we have enough space */
2288 ReorderBufferSerializeReserve(rb, sz);
2289 data = ((char *) rb->outbuf) + sizeof(ReorderBufferDiskChange);
2290 /* might have been reallocated above */
2291 ondisk = (ReorderBufferDiskChange *) rb->outbuf;
2293 memcpy(data, snap, sizeof(SnapshotData));
2294 data += sizeof(SnapshotData);
2298 memcpy(data, snap->xip,
2299 sizeof(TransactionId) * snap->xcnt);
2300 data += sizeof(TransactionId) * snap->xcnt;
2305 memcpy(data, snap->subxip,
2306 sizeof(TransactionId) * snap->subxcnt);
2307 data += sizeof(TransactionId) * snap->subxcnt;
2311 case REORDER_BUFFER_CHANGE_INTERNAL_SPEC_CONFIRM:
2312 case REORDER_BUFFER_CHANGE_INTERNAL_COMMAND_ID:
2313 case REORDER_BUFFER_CHANGE_INTERNAL_TUPLECID:
2314 /* ReorderBufferChange contains everything important */
2320 if (write(fd, rb->outbuf, ondisk->size) != ondisk->size)
2322 int save_errno = errno;
2324 CloseTransientFile(fd);
2327 (errcode_for_file_access(),
2328 errmsg("could not write to data file for XID %u: %m",
2332 Assert(ondisk->change.action == change->action);
2336 * Restore a number of changes spilled to disk back into memory.
2339 ReorderBufferRestoreChanges(ReorderBuffer *rb, ReorderBufferTXN *txn,
2340 int *fd, XLogSegNo *segno)
2343 XLogSegNo last_segno;
2344 dlist_mutable_iter cleanup_iter;
2346 Assert(txn->first_lsn != InvalidXLogRecPtr);
2347 Assert(txn->final_lsn != InvalidXLogRecPtr);
2349 /* free current entries, so we have memory for more */
2350 dlist_foreach_modify(cleanup_iter, &txn->changes)
2352 ReorderBufferChange *cleanup =
2353 dlist_container(ReorderBufferChange, node, cleanup_iter.cur);
2355 dlist_delete(&cleanup->node);
2356 ReorderBufferReturnChange(rb, cleanup);
2358 txn->nentries_mem = 0;
2359 Assert(dlist_is_empty(&txn->changes));
2361 XLByteToSeg(txn->final_lsn, last_segno);
2363 while (restored < max_changes_in_memory && *segno <= last_segno)
2366 ReorderBufferDiskChange *ondisk;
2371 char path[MAXPGPATH];
2376 XLByteToSeg(txn->first_lsn, *segno);
2379 Assert(*segno != 0 || dlist_is_empty(&txn->changes));
2380 XLogSegNoOffsetToRecPtr(*segno, 0, recptr);
2383 * No need to care about TLIs here, only used during a single run,
2384 * so each LSN only maps to a specific WAL record.
2386 sprintf(path, "pg_replslot/%s/xid-%u-lsn-%X-%X.snap",
2387 NameStr(MyReplicationSlot->data.name), txn->xid,
2388 (uint32) (recptr >> 32), (uint32) recptr);
2390 *fd = OpenTransientFile(path, O_RDONLY | PG_BINARY, 0);
2391 if (*fd < 0 && errno == ENOENT)
2399 (errcode_for_file_access(),
2400 errmsg("could not open file \"%s\": %m",
2406 * Read the statically sized part of a change which has information
2407 * about the total size. If we couldn't read a record, we're at the
2410 ReorderBufferSerializeReserve(rb, sizeof(ReorderBufferDiskChange));
2411 readBytes = read(*fd, rb->outbuf, sizeof(ReorderBufferDiskChange));
2416 CloseTransientFile(*fd);
2421 else if (readBytes < 0)
2423 (errcode_for_file_access(),
2424 errmsg("could not read from reorderbuffer spill file: %m")));
2425 else if (readBytes != sizeof(ReorderBufferDiskChange))
2427 (errcode_for_file_access(),
2428 errmsg("could not read from reorderbuffer spill file: read %d instead of %u bytes",
2430 (uint32) sizeof(ReorderBufferDiskChange))));
2432 ondisk = (ReorderBufferDiskChange *) rb->outbuf;
2434 ReorderBufferSerializeReserve(rb,
2435 sizeof(ReorderBufferDiskChange) + ondisk->size);
2436 ondisk = (ReorderBufferDiskChange *) rb->outbuf;
2438 readBytes = read(*fd, rb->outbuf + sizeof(ReorderBufferDiskChange),
2439 ondisk->size - sizeof(ReorderBufferDiskChange));
2443 (errcode_for_file_access(),
2444 errmsg("could not read from reorderbuffer spill file: %m")));
2445 else if (readBytes != ondisk->size - sizeof(ReorderBufferDiskChange))
2447 (errcode_for_file_access(),
2448 errmsg("could not read from reorderbuffer spill file: read %d instead of %u bytes",
2450 (uint32) (ondisk->size - sizeof(ReorderBufferDiskChange)))));
2453 * ok, read a full change from disk, now restore it into proper
2456 ReorderBufferRestoreChange(rb, txn, rb->outbuf);
2464 * Convert change from its on-disk format to in-memory format and queue it onto
2465 * the TXN's ->changes list.
2467 * Note: although "data" is declared char*, at entry it points to a
2468 * maxalign'd buffer, making it safe in most of this function to assume
2469 * that the pointed-to data is suitably aligned for direct access.
2472 ReorderBufferRestoreChange(ReorderBuffer *rb, ReorderBufferTXN *txn,
2475 ReorderBufferDiskChange *ondisk;
2476 ReorderBufferChange *change;
2478 ondisk = (ReorderBufferDiskChange *) data;
2480 change = ReorderBufferGetChange(rb);
2482 /* copy static part */
2483 memcpy(change, &ondisk->change, sizeof(ReorderBufferChange));
2485 data += sizeof(ReorderBufferDiskChange);
2487 /* restore individual stuff */
2488 switch (change->action)
2490 /* fall through these, they're all similar enough */
2491 case REORDER_BUFFER_CHANGE_INSERT:
2492 case REORDER_BUFFER_CHANGE_UPDATE:
2493 case REORDER_BUFFER_CHANGE_DELETE:
2494 case REORDER_BUFFER_CHANGE_INTERNAL_SPEC_INSERT:
2495 if (change->data.tp.oldtuple)
2497 uint32 tuplelen = ((HeapTuple) data)->t_len;
2499 change->data.tp.oldtuple =
2500 ReorderBufferGetTupleBuf(rb, tuplelen - SizeofHeapTupleHeader);
2502 /* restore ->tuple */
2503 memcpy(&change->data.tp.oldtuple->tuple, data,
2504 sizeof(HeapTupleData));
2505 data += sizeof(HeapTupleData);
2507 /* reset t_data pointer into the new tuplebuf */
2508 change->data.tp.oldtuple->tuple.t_data =
2509 ReorderBufferTupleBufData(change->data.tp.oldtuple);
2511 /* restore tuple data itself */
2512 memcpy(change->data.tp.oldtuple->tuple.t_data, data, tuplelen);
2516 if (change->data.tp.newtuple)
2518 /* here, data might not be suitably aligned! */
2521 memcpy(&tuplelen, data + offsetof(HeapTupleData, t_len),
2524 change->data.tp.newtuple =
2525 ReorderBufferGetTupleBuf(rb, tuplelen - SizeofHeapTupleHeader);
2527 /* restore ->tuple */
2528 memcpy(&change->data.tp.newtuple->tuple, data,
2529 sizeof(HeapTupleData));
2530 data += sizeof(HeapTupleData);
2532 /* reset t_data pointer into the new tuplebuf */
2533 change->data.tp.newtuple->tuple.t_data =
2534 ReorderBufferTupleBufData(change->data.tp.newtuple);
2536 /* restore tuple data itself */
2537 memcpy(change->data.tp.newtuple->tuple.t_data, data, tuplelen);
2542 case REORDER_BUFFER_CHANGE_MESSAGE:
2547 memcpy(&prefix_size, data, sizeof(Size));
2548 data += sizeof(Size);
2549 change->data.msg.prefix = MemoryContextAlloc(rb->context,
2551 memcpy(change->data.msg.prefix, data, prefix_size);
2552 Assert(change->data.msg.prefix[prefix_size - 1] == '\0');
2553 data += prefix_size;
2555 /* read the message */
2556 memcpy(&change->data.msg.message_size, data, sizeof(Size));
2557 data += sizeof(Size);
2558 change->data.msg.message = MemoryContextAlloc(rb->context,
2559 change->data.msg.message_size);
2560 memcpy(change->data.msg.message, data,
2561 change->data.msg.message_size);
2562 data += change->data.msg.message_size;
2566 case REORDER_BUFFER_CHANGE_INTERNAL_SNAPSHOT:
2572 oldsnap = (Snapshot) data;
2574 size = sizeof(SnapshotData) +
2575 sizeof(TransactionId) * oldsnap->xcnt +
2576 sizeof(TransactionId) * (oldsnap->subxcnt + 0);
2578 change->data.snapshot = MemoryContextAllocZero(rb->context, size);
2580 newsnap = change->data.snapshot;
2582 memcpy(newsnap, data, size);
2583 newsnap->xip = (TransactionId *)
2584 (((char *) newsnap) + sizeof(SnapshotData));
2585 newsnap->subxip = newsnap->xip + newsnap->xcnt;
2586 newsnap->copied = true;
2589 /* the base struct contains all the data, easy peasy */
2590 case REORDER_BUFFER_CHANGE_INTERNAL_SPEC_CONFIRM:
2591 case REORDER_BUFFER_CHANGE_INTERNAL_COMMAND_ID:
2592 case REORDER_BUFFER_CHANGE_INTERNAL_TUPLECID:
2596 dlist_push_tail(&txn->changes, &change->node);
2597 txn->nentries_mem++;
2601 * Remove all on-disk stored for the passed in transaction.
2604 ReorderBufferRestoreCleanup(ReorderBuffer *rb, ReorderBufferTXN *txn)
2610 Assert(txn->first_lsn != InvalidXLogRecPtr);
2611 Assert(txn->final_lsn != InvalidXLogRecPtr);
2613 XLByteToSeg(txn->first_lsn, first);
2614 XLByteToSeg(txn->final_lsn, last);
2616 /* iterate over all possible filenames, and delete them */
2617 for (cur = first; cur <= last; cur++)
2619 char path[MAXPGPATH];
2622 XLogSegNoOffsetToRecPtr(cur, 0, recptr);
2624 sprintf(path, "pg_replslot/%s/xid-%u-lsn-%X-%X.snap",
2625 NameStr(MyReplicationSlot->data.name), txn->xid,
2626 (uint32) (recptr >> 32), (uint32) recptr);
2627 if (unlink(path) != 0 && errno != ENOENT)
2629 (errcode_for_file_access(),
2630 errmsg("could not remove file \"%s\": %m", path)));
2635 * Delete all data spilled to disk after we've restarted/crashed. It will be
2636 * recreated when the respective slots are reused.
2639 StartupReorderBuffer(void)
2642 struct dirent *logical_de;
2645 struct dirent *spill_de;
2647 logical_dir = AllocateDir("pg_replslot");
2648 while ((logical_de = ReadDir(logical_dir, "pg_replslot")) != NULL)
2650 struct stat statbuf;
2651 char path[MAXPGPATH];
2653 if (strcmp(logical_de->d_name, ".") == 0 ||
2654 strcmp(logical_de->d_name, "..") == 0)
2657 /* if it cannot be a slot, skip the directory */
2658 if (!ReplicationSlotValidateName(logical_de->d_name, DEBUG2))
2662 * ok, has to be a surviving logical slot, iterate and delete
2663 * everythign starting with xid-*
2665 sprintf(path, "pg_replslot/%s", logical_de->d_name);
2667 /* we're only creating directories here, skip if it's not our's */
2668 if (lstat(path, &statbuf) == 0 && !S_ISDIR(statbuf.st_mode))
2671 spill_dir = AllocateDir(path);
2672 while ((spill_de = ReadDir(spill_dir, path)) != NULL)
2674 if (strcmp(spill_de->d_name, ".") == 0 ||
2675 strcmp(spill_de->d_name, "..") == 0)
2678 /* only look at names that can be ours */
2679 if (strncmp(spill_de->d_name, "xid", 3) == 0)
2681 sprintf(path, "pg_replslot/%s/%s", logical_de->d_name,
2684 if (unlink(path) != 0)
2686 (errcode_for_file_access(),
2687 errmsg("could not remove file \"%s\": %m",
2693 FreeDir(logical_dir);
2696 /* ---------------------------------------
2697 * toast reassembly support
2698 * ---------------------------------------
2702 * Initialize per tuple toast reconstruction support.
2705 ReorderBufferToastInitHash(ReorderBuffer *rb, ReorderBufferTXN *txn)
2709 Assert(txn->toast_hash == NULL);
2711 memset(&hash_ctl, 0, sizeof(hash_ctl));
2712 hash_ctl.keysize = sizeof(Oid);
2713 hash_ctl.entrysize = sizeof(ReorderBufferToastEnt);
2714 hash_ctl.hcxt = rb->context;
2715 txn->toast_hash = hash_create("ReorderBufferToastHash", 5, &hash_ctl,
2716 HASH_ELEM | HASH_BLOBS | HASH_CONTEXT);
2720 * Per toast-chunk handling for toast reconstruction
2722 * Appends a toast chunk so we can reconstruct it when the tuple "owning" the
2723 * toasted Datum comes along.
2726 ReorderBufferToastAppendChunk(ReorderBuffer *rb, ReorderBufferTXN *txn,
2727 Relation relation, ReorderBufferChange *change)
2729 ReorderBufferToastEnt *ent;
2730 ReorderBufferTupleBuf *newtup;
2735 TupleDesc desc = RelationGetDescr(relation);
2739 if (txn->toast_hash == NULL)
2740 ReorderBufferToastInitHash(rb, txn);
2742 Assert(IsToastRelation(relation));
2744 newtup = change->data.tp.newtuple;
2745 chunk_id = DatumGetObjectId(fastgetattr(&newtup->tuple, 1, desc, &isnull));
2747 chunk_seq = DatumGetInt32(fastgetattr(&newtup->tuple, 2, desc, &isnull));
2750 ent = (ReorderBufferToastEnt *)
2751 hash_search(txn->toast_hash,
2758 Assert(ent->chunk_id == chunk_id);
2759 ent->num_chunks = 0;
2760 ent->last_chunk_seq = 0;
2762 ent->reconstructed = NULL;
2763 dlist_init(&ent->chunks);
2766 elog(ERROR, "got sequence entry %d for toast chunk %u instead of seq 0",
2767 chunk_seq, chunk_id);
2769 else if (found && chunk_seq != ent->last_chunk_seq + 1)
2770 elog(ERROR, "got sequence entry %d for toast chunk %u instead of seq %d",
2771 chunk_seq, chunk_id, ent->last_chunk_seq + 1);
2773 chunk = DatumGetPointer(fastgetattr(&newtup->tuple, 3, desc, &isnull));
2776 /* calculate size so we can allocate the right size at once later */
2777 if (!VARATT_IS_EXTENDED(chunk))
2778 chunksize = VARSIZE(chunk) - VARHDRSZ;
2779 else if (VARATT_IS_SHORT(chunk))
2780 /* could happen due to heap_form_tuple doing its thing */
2781 chunksize = VARSIZE_SHORT(chunk) - VARHDRSZ_SHORT;
2783 elog(ERROR, "unexpected type of toast chunk");
2785 ent->size += chunksize;
2786 ent->last_chunk_seq = chunk_seq;
2788 dlist_push_tail(&ent->chunks, &change->node);
2792 * Rejigger change->newtuple to point to in-memory toast tuples instead to
2793 * on-disk toast tuples that may not longer exist (think DROP TABLE or VACUUM).
2795 * We cannot replace unchanged toast tuples though, so those will still point
2796 * to on-disk toast data.
2799 ReorderBufferToastReplace(ReorderBuffer *rb, ReorderBufferTXN *txn,
2800 Relation relation, ReorderBufferChange *change)
2809 TupleDesc toast_desc;
2810 MemoryContext oldcontext;
2811 ReorderBufferTupleBuf *newtup;
2813 /* no toast tuples changed */
2814 if (txn->toast_hash == NULL)
2817 oldcontext = MemoryContextSwitchTo(rb->context);
2819 /* we should only have toast tuples in an INSERT or UPDATE */
2820 Assert(change->data.tp.newtuple);
2822 desc = RelationGetDescr(relation);
2824 toast_rel = RelationIdGetRelation(relation->rd_rel->reltoastrelid);
2825 toast_desc = RelationGetDescr(toast_rel);
2827 /* should we allocate from stack instead? */
2828 attrs = palloc0(sizeof(Datum) * desc->natts);
2829 isnull = palloc0(sizeof(bool) * desc->natts);
2830 free = palloc0(sizeof(bool) * desc->natts);
2832 newtup = change->data.tp.newtuple;
2834 heap_deform_tuple(&newtup->tuple, desc, attrs, isnull);
2836 for (natt = 0; natt < desc->natts; natt++)
2838 Form_pg_attribute attr = desc->attrs[natt];
2839 ReorderBufferToastEnt *ent;
2840 struct varlena *varlena;
2842 /* va_rawsize is the size of the original datum -- including header */
2843 struct varatt_external toast_pointer;
2844 struct varatt_indirect redirect_pointer;
2845 struct varlena *new_datum = NULL;
2846 struct varlena *reconstructed;
2850 /* system columns aren't toasted */
2851 if (attr->attnum < 0)
2854 if (attr->attisdropped)
2857 /* not a varlena datatype */
2858 if (attr->attlen != -1)
2865 /* ok, we know we have a toast datum */
2866 varlena = (struct varlena *) DatumGetPointer(attrs[natt]);
2868 /* no need to do anything if the tuple isn't external */
2869 if (!VARATT_IS_EXTERNAL(varlena))
2872 VARATT_EXTERNAL_GET_POINTER(toast_pointer, varlena);
2875 * Check whether the toast tuple changed, replace if so.
2877 ent = (ReorderBufferToastEnt *)
2878 hash_search(txn->toast_hash,
2879 (void *) &toast_pointer.va_valueid,
2886 (struct varlena *) palloc0(INDIRECT_POINTER_SIZE);
2890 reconstructed = palloc0(toast_pointer.va_rawsize);
2892 ent->reconstructed = reconstructed;
2894 /* stitch toast tuple back together from its parts */
2895 dlist_foreach(it, &ent->chunks)
2898 ReorderBufferChange *cchange;
2899 ReorderBufferTupleBuf *ctup;
2902 cchange = dlist_container(ReorderBufferChange, node, it.cur);
2903 ctup = cchange->data.tp.newtuple;
2904 chunk = DatumGetPointer(
2905 fastgetattr(&ctup->tuple, 3, toast_desc, &isnull));
2908 Assert(!VARATT_IS_EXTERNAL(chunk));
2909 Assert(!VARATT_IS_SHORT(chunk));
2911 memcpy(VARDATA(reconstructed) + data_done,
2913 VARSIZE(chunk) - VARHDRSZ);
2914 data_done += VARSIZE(chunk) - VARHDRSZ;
2916 Assert(data_done == toast_pointer.va_extsize);
2918 /* make sure its marked as compressed or not */
2919 if (VARATT_EXTERNAL_IS_COMPRESSED(toast_pointer))
2920 SET_VARSIZE_COMPRESSED(reconstructed, data_done + VARHDRSZ);
2922 SET_VARSIZE(reconstructed, data_done + VARHDRSZ);
2924 memset(&redirect_pointer, 0, sizeof(redirect_pointer));
2925 redirect_pointer.pointer = reconstructed;
2927 SET_VARTAG_EXTERNAL(new_datum, VARTAG_INDIRECT);
2928 memcpy(VARDATA_EXTERNAL(new_datum), &redirect_pointer,
2929 sizeof(redirect_pointer));
2931 attrs[natt] = PointerGetDatum(new_datum);
2935 * Build tuple in separate memory & copy tuple back into the tuplebuf
2936 * passed to the output plugin. We can't directly heap_fill_tuple() into
2937 * the tuplebuf because attrs[] will point back into the current content.
2939 tmphtup = heap_form_tuple(desc, attrs, isnull);
2940 Assert(newtup->tuple.t_len <= MaxHeapTupleSize);
2941 Assert(ReorderBufferTupleBufData(newtup) == newtup->tuple.t_data);
2943 memcpy(newtup->tuple.t_data, tmphtup->t_data, tmphtup->t_len);
2944 newtup->tuple.t_len = tmphtup->t_len;
2947 * free resources we won't further need, more persistent stuff will be
2948 * free'd in ReorderBufferToastReset().
2950 RelationClose(toast_rel);
2952 for (natt = 0; natt < desc->natts; natt++)
2955 pfree(DatumGetPointer(attrs[natt]));
2961 MemoryContextSwitchTo(oldcontext);
2965 * Free all resources allocated for toast reconstruction.
2968 ReorderBufferToastReset(ReorderBuffer *rb, ReorderBufferTXN *txn)
2970 HASH_SEQ_STATUS hstat;
2971 ReorderBufferToastEnt *ent;
2973 if (txn->toast_hash == NULL)
2976 /* sequentially walk over the hash and free everything */
2977 hash_seq_init(&hstat, txn->toast_hash);
2978 while ((ent = (ReorderBufferToastEnt *) hash_seq_search(&hstat)) != NULL)
2980 dlist_mutable_iter it;
2982 if (ent->reconstructed != NULL)
2983 pfree(ent->reconstructed);
2985 dlist_foreach_modify(it, &ent->chunks)
2987 ReorderBufferChange *change =
2988 dlist_container(ReorderBufferChange, node, it.cur);
2990 dlist_delete(&change->node);
2991 ReorderBufferReturnChange(rb, change);
2995 hash_destroy(txn->toast_hash);
2996 txn->toast_hash = NULL;
3000 /* ---------------------------------------
3001 * Visibility support for logical decoding
3004 * Lookup actual cmin/cmax values when using decoding snapshot. We can't
3005 * always rely on stored cmin/cmax values because of two scenarios:
3007 * * A tuple got changed multiple times during a single transaction and thus
3008 * has got a combocid. Combocid's are only valid for the duration of a
3009 * single transaction.
3010 * * A tuple with a cmin but no cmax (and thus no combocid) got
3011 * deleted/updated in another transaction than the one which created it
3012 * which we are looking at right now. As only one of cmin, cmax or combocid
3013 * is actually stored in the heap we don't have access to the value we
3016 * To resolve those problems we have a per-transaction hash of (cmin,
3017 * cmax) tuples keyed by (relfilenode, ctid) which contains the actual
3018 * (cmin, cmax) values. That also takes care of combocids by simply
3019 * not caring about them at all. As we have the real cmin/cmax values
3020 * combocids aren't interesting.
3022 * As we only care about catalog tuples here the overhead of this
3023 * hashtable should be acceptable.
3025 * Heap rewrites complicate this a bit, check rewriteheap.c for
3027 * -------------------------------------------------------------------------
3030 /* struct for qsort()ing mapping files by lsn somewhat efficiently */
3031 typedef struct RewriteMappingFile
3034 char fname[MAXPGPATH];
3035 } RewriteMappingFile;
3039 DisplayMapping(HTAB *tuplecid_data)
3041 HASH_SEQ_STATUS hstat;
3042 ReorderBufferTupleCidEnt *ent;
3044 hash_seq_init(&hstat, tuplecid_data);
3045 while ((ent = (ReorderBufferTupleCidEnt *) hash_seq_search(&hstat)) != NULL)
3047 elog(DEBUG3, "mapping: node: %u/%u/%u tid: %u/%u cmin: %u, cmax: %u",
3048 ent->key.relnode.dbNode,
3049 ent->key.relnode.spcNode,
3050 ent->key.relnode.relNode,
3051 BlockIdGetBlockNumber(&ent->key.tid.ip_blkid),
3052 ent->key.tid.ip_posid,
3061 * Apply a single mapping file to tuplecid_data.
3063 * The mapping file has to have been verified to be a) committed b) for our
3064 * transaction c) applied in LSN order.
3067 ApplyLogicalMappingFile(HTAB *tuplecid_data, Oid relid, const char *fname)
3069 char path[MAXPGPATH];
3072 LogicalRewriteMappingData map;
3074 sprintf(path, "pg_logical/mappings/%s", fname);
3075 fd = OpenTransientFile(path, O_RDONLY | PG_BINARY, 0);
3078 (errcode_for_file_access(),
3079 errmsg("could not open file \"%s\": %m", path)));
3083 ReorderBufferTupleCidKey key;
3084 ReorderBufferTupleCidEnt *ent;
3085 ReorderBufferTupleCidEnt *new_ent;
3088 /* be careful about padding */
3089 memset(&key, 0, sizeof(ReorderBufferTupleCidKey));
3091 /* read all mappings till the end of the file */
3092 readBytes = read(fd, &map, sizeof(LogicalRewriteMappingData));
3096 (errcode_for_file_access(),
3097 errmsg("could not read file \"%s\": %m",
3099 else if (readBytes == 0) /* EOF */
3101 else if (readBytes != sizeof(LogicalRewriteMappingData))
3103 (errcode_for_file_access(),
3104 errmsg("could not read from file \"%s\": read %d instead of %d bytes",
3106 (int32) sizeof(LogicalRewriteMappingData))));
3108 key.relnode = map.old_node;
3109 ItemPointerCopy(&map.old_tid,
3113 ent = (ReorderBufferTupleCidEnt *)
3114 hash_search(tuplecid_data,
3119 /* no existing mapping, no need to update */
3123 key.relnode = map.new_node;
3124 ItemPointerCopy(&map.new_tid,
3127 new_ent = (ReorderBufferTupleCidEnt *)
3128 hash_search(tuplecid_data,
3136 * Make sure the existing mapping makes sense. We sometime update
3137 * old records that did not yet have a cmax (e.g. pg_class' own
3138 * entry while rewriting it) during rewrites, so allow that.
3140 Assert(ent->cmin == InvalidCommandId || ent->cmin == new_ent->cmin);
3141 Assert(ent->cmax == InvalidCommandId || ent->cmax == new_ent->cmax);
3145 /* update mapping */
3146 new_ent->cmin = ent->cmin;
3147 new_ent->cmax = ent->cmax;
3148 new_ent->combocid = ent->combocid;
3155 * Check whether the TransactionOId 'xid' is in the pre-sorted array 'xip'.
3158 TransactionIdInArray(TransactionId xid, TransactionId *xip, Size num)
3160 return bsearch(&xid, xip, num,
3161 sizeof(TransactionId), xidComparator) != NULL;
3165 * qsort() comparator for sorting RewriteMappingFiles in LSN order.
3168 file_sort_by_lsn(const void *a_p, const void *b_p)
3170 RewriteMappingFile *a = *(RewriteMappingFile **) a_p;
3171 RewriteMappingFile *b = *(RewriteMappingFile **) b_p;
3173 if (a->lsn < b->lsn)
3175 else if (a->lsn > b->lsn)
3181 * Apply any existing logical remapping files if there are any targeted at our
3182 * transaction for relid.
3185 UpdateLogicalMappings(HTAB *tuplecid_data, Oid relid, Snapshot snapshot)
3188 struct dirent *mapping_de;
3191 RewriteMappingFile **files_a;
3193 Oid dboid = IsSharedRelation(relid) ? InvalidOid : MyDatabaseId;
3195 mapping_dir = AllocateDir("pg_logical/mappings");
3196 while ((mapping_de = ReadDir(mapping_dir, "pg_logical/mappings")) != NULL)
3200 TransactionId f_mapped_xid;
3201 TransactionId f_create_xid;
3205 RewriteMappingFile *f;
3207 if (strcmp(mapping_de->d_name, ".") == 0 ||
3208 strcmp(mapping_de->d_name, "..") == 0)
3211 /* Ignore files that aren't ours */
3212 if (strncmp(mapping_de->d_name, "map-", 4) != 0)
3215 if (sscanf(mapping_de->d_name, LOGICAL_REWRITE_FORMAT,
3216 &f_dboid, &f_relid, &f_hi, &f_lo,
3217 &f_mapped_xid, &f_create_xid) != 6)
3218 elog(ERROR, "could not parse filename \"%s\"", mapping_de->d_name);
3220 f_lsn = ((uint64) f_hi) << 32 | f_lo;
3222 /* mapping for another database */
3223 if (f_dboid != dboid)
3226 /* mapping for another relation */
3227 if (f_relid != relid)
3230 /* did the creating transaction abort? */
3231 if (!TransactionIdDidCommit(f_create_xid))
3234 /* not for our transaction */
3235 if (!TransactionIdInArray(f_mapped_xid, snapshot->subxip, snapshot->subxcnt))
3238 /* ok, relevant, queue for apply */
3239 f = palloc(sizeof(RewriteMappingFile));
3241 strcpy(f->fname, mapping_de->d_name);
3242 files = lappend(files, f);
3244 FreeDir(mapping_dir);
3246 /* build array we can easily sort */
3247 files_a = palloc(list_length(files) * sizeof(RewriteMappingFile *));
3249 foreach(file, files)
3251 files_a[off++] = lfirst(file);
3254 /* sort files so we apply them in LSN order */
3255 qsort(files_a, list_length(files), sizeof(RewriteMappingFile *),
3258 for (off = 0; off < list_length(files); off++)
3260 RewriteMappingFile *f = files_a[off];
3262 elog(DEBUG1, "applying mapping: \"%s\" in %u", f->fname,
3263 snapshot->subxip[0]);
3264 ApplyLogicalMappingFile(tuplecid_data, relid, f->fname);
3270 * Lookup cmin/cmax of a tuple, during logical decoding where we can't rely on
3274 ResolveCminCmaxDuringDecoding(HTAB *tuplecid_data,
3276 HeapTuple htup, Buffer buffer,
3277 CommandId *cmin, CommandId *cmax)
3279 ReorderBufferTupleCidKey key;
3280 ReorderBufferTupleCidEnt *ent;
3282 BlockNumber blockno;
3283 bool updated_mapping = false;
3285 /* be careful about padding */
3286 memset(&key, 0, sizeof(key));
3288 Assert(!BufferIsLocal(buffer));
3291 * get relfilenode from the buffer, no convenient way to access it other
3294 BufferGetTag(buffer, &key.relnode, &forkno, &blockno);
3296 /* tuples can only be in the main fork */
3297 Assert(forkno == MAIN_FORKNUM);
3298 Assert(blockno == ItemPointerGetBlockNumber(&htup->t_self));
3300 ItemPointerCopy(&htup->t_self,
3304 ent = (ReorderBufferTupleCidEnt *)
3305 hash_search(tuplecid_data,
3311 * failed to find a mapping, check whether the table was rewritten and
3312 * apply mapping if so, but only do that once - there can be no new
3313 * mappings while we are in here since we have to hold a lock on the
3316 if (ent == NULL && !updated_mapping)
3318 UpdateLogicalMappings(tuplecid_data, htup->t_tableOid, snapshot);
3319 /* now check but don't update for a mapping again */
3320 updated_mapping = true;
3323 else if (ent == NULL)