]> granicus.if.org Git - postgresql/blob - src/backend/replication/logical/reorderbuffer.c
logical decoding: beware of an unset specinsert change
[postgresql] / src / backend / replication / logical / reorderbuffer.c
1 /*-------------------------------------------------------------------------
2  *
3  * reorderbuffer.c
4  *        PostgreSQL logical replay/reorder buffer management
5  *
6  *
7  * Copyright (c) 2012-2018, PostgreSQL Global Development Group
8  *
9  *
10  * IDENTIFICATION
11  *        src/backend/replication/reorderbuffer.c
12  *
13  * NOTES
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 (cf. ReorderBufferCommit()) with the
19  *        individual changes. The output plugins rely on snapshots built by
20  *        snapbuild.c which hands them to us.
21  *
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.
32  *
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
37  *        chunks.
38  *
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
45  *        details.
46  *
47  *        ReorderBuffer uses two special memory context types - SlabContext for
48  *        allocations of fixed-length structures (changes and transactions), and
49  *        GenerationContext for the variable-length transaction data (allocated
50  *        and freed in groups with similar lifespan).
51  *
52  * -------------------------------------------------------------------------
53  */
54 #include "postgres.h"
55
56 #include <unistd.h>
57 #include <sys/stat.h>
58
59 #include "access/rewriteheap.h"
60 #include "access/transam.h"
61 #include "access/tuptoaster.h"
62 #include "access/xact.h"
63 #include "access/xlog_internal.h"
64 #include "catalog/catalog.h"
65 #include "lib/binaryheap.h"
66 #include "miscadmin.h"
67 #include "pgstat.h"
68 #include "replication/logical.h"
69 #include "replication/reorderbuffer.h"
70 #include "replication/slot.h"
71 #include "replication/snapbuild.h"      /* just for SnapBuildSnapDecRefcount */
72 #include "storage/bufmgr.h"
73 #include "storage/fd.h"
74 #include "storage/sinval.h"
75 #include "utils/builtins.h"
76 #include "utils/combocid.h"
77 #include "utils/memdebug.h"
78 #include "utils/memutils.h"
79 #include "utils/rel.h"
80 #include "utils/relfilenodemap.h"
81 #include "utils/tqual.h"
82
83
84 /* entry for a hash table we use to map from xid to our transaction state */
85 typedef struct ReorderBufferTXNByIdEnt
86 {
87         TransactionId xid;
88         ReorderBufferTXN *txn;
89 } ReorderBufferTXNByIdEnt;
90
91 /* data structures for (relfilenode, ctid) => (cmin, cmax) mapping */
92 typedef struct ReorderBufferTupleCidKey
93 {
94         RelFileNode relnode;
95         ItemPointerData tid;
96 } ReorderBufferTupleCidKey;
97
98 typedef struct ReorderBufferTupleCidEnt
99 {
100         ReorderBufferTupleCidKey key;
101         CommandId       cmin;
102         CommandId       cmax;
103         CommandId       combocid;               /* just for debugging */
104 } ReorderBufferTupleCidEnt;
105
106 /* k-way in-order change iteration support structures */
107 typedef struct ReorderBufferIterTXNEntry
108 {
109         XLogRecPtr      lsn;
110         ReorderBufferChange *change;
111         ReorderBufferTXN *txn;
112         int                     fd;
113         XLogSegNo       segno;
114 } ReorderBufferIterTXNEntry;
115
116 typedef struct ReorderBufferIterTXNState
117 {
118         binaryheap *heap;
119         Size            nr_txns;
120         dlist_head      old_change;
121         ReorderBufferIterTXNEntry entries[FLEXIBLE_ARRAY_MEMBER];
122 } ReorderBufferIterTXNState;
123
124 /* toast datastructures */
125 typedef struct ReorderBufferToastEnt
126 {
127         Oid                     chunk_id;               /* toast_table.chunk_id */
128         int32           last_chunk_seq; /* toast_table.chunk_seq of the last chunk we
129                                                                  * have seen */
130         Size            num_chunks;             /* number of chunks we've already seen */
131         Size            size;                   /* combined size of chunks seen */
132         dlist_head      chunks;                 /* linked list of chunks */
133         struct varlena *reconstructed;  /* reconstructed varlena now pointed to in
134                                                                          * main tup */
135 } ReorderBufferToastEnt;
136
137 /* Disk serialization support datastructures */
138 typedef struct ReorderBufferDiskChange
139 {
140         Size            size;
141         ReorderBufferChange change;
142         /* data follows */
143 } ReorderBufferDiskChange;
144
145 /*
146  * Maximum number of changes kept in memory, per transaction. After that,
147  * changes are spooled to disk.
148  *
149  * The current value should be sufficient to decode the entire transaction
150  * without hitting disk in OLTP workloads, while starting to spool to disk in
151  * other workloads reasonably fast.
152  *
153  * At some point in the future it probably makes sense to have a more elaborate
154  * resource management here, but it's not entirely clear what that would look
155  * like.
156  */
157 static const Size max_changes_in_memory = 4096;
158
159 /* ---------------------------------------
160  * primary reorderbuffer support routines
161  * ---------------------------------------
162  */
163 static ReorderBufferTXN *ReorderBufferGetTXN(ReorderBuffer *rb);
164 static void ReorderBufferReturnTXN(ReorderBuffer *rb, ReorderBufferTXN *txn);
165 static ReorderBufferTXN *ReorderBufferTXNByXid(ReorderBuffer *rb,
166                                           TransactionId xid, bool create, bool *is_new,
167                                           XLogRecPtr lsn, bool create_as_top);
168 static void ReorderBufferTransferSnapToParent(ReorderBufferTXN *txn,
169                                                                   ReorderBufferTXN *subtxn);
170
171 static void AssertTXNLsnOrder(ReorderBuffer *rb);
172
173 /* ---------------------------------------
174  * support functions for lsn-order iterating over the ->changes of a
175  * transaction and its subtransactions
176  *
177  * used for iteration over the k-way heap merge of a transaction and its
178  * subtransactions
179  * ---------------------------------------
180  */
181 static ReorderBufferIterTXNState *ReorderBufferIterTXNInit(ReorderBuffer *rb, ReorderBufferTXN *txn);
182 static ReorderBufferChange *ReorderBufferIterTXNNext(ReorderBuffer *rb, ReorderBufferIterTXNState *state);
183 static void ReorderBufferIterTXNFinish(ReorderBuffer *rb,
184                                                    ReorderBufferIterTXNState *state);
185 static void ReorderBufferExecuteInvalidations(ReorderBuffer *rb, ReorderBufferTXN *txn);
186
187 /*
188  * ---------------------------------------
189  * Disk serialization support functions
190  * ---------------------------------------
191  */
192 static void ReorderBufferCheckSerializeTXN(ReorderBuffer *rb, ReorderBufferTXN *txn);
193 static void ReorderBufferSerializeTXN(ReorderBuffer *rb, ReorderBufferTXN *txn);
194 static void ReorderBufferSerializeChange(ReorderBuffer *rb, ReorderBufferTXN *txn,
195                                                          int fd, ReorderBufferChange *change);
196 static Size ReorderBufferRestoreChanges(ReorderBuffer *rb, ReorderBufferTXN *txn,
197                                                         int *fd, XLogSegNo *segno);
198 static void ReorderBufferRestoreChange(ReorderBuffer *rb, ReorderBufferTXN *txn,
199                                                    char *change);
200 static void ReorderBufferRestoreCleanup(ReorderBuffer *rb, ReorderBufferTXN *txn);
201 static void ReorderBufferCleanupSerializedTXNs(const char *slotname);
202 static void ReorderBufferSerializedPath(char *path, ReplicationSlot *slot,
203                                                         TransactionId xid, XLogSegNo segno);
204
205 static void ReorderBufferFreeSnap(ReorderBuffer *rb, Snapshot snap);
206 static Snapshot ReorderBufferCopySnap(ReorderBuffer *rb, Snapshot orig_snap,
207                                           ReorderBufferTXN *txn, CommandId cid);
208
209 /* ---------------------------------------
210  * toast reassembly support
211  * ---------------------------------------
212  */
213 static void ReorderBufferToastInitHash(ReorderBuffer *rb, ReorderBufferTXN *txn);
214 static void ReorderBufferToastReset(ReorderBuffer *rb, ReorderBufferTXN *txn);
215 static void ReorderBufferToastReplace(ReorderBuffer *rb, ReorderBufferTXN *txn,
216                                                   Relation relation, ReorderBufferChange *change);
217 static void ReorderBufferToastAppendChunk(ReorderBuffer *rb, ReorderBufferTXN *txn,
218                                                           Relation relation, ReorderBufferChange *change);
219
220
221 /*
222  * Allocate a new ReorderBuffer and clean out any old serialized state from
223  * prior ReorderBuffer instances for the same slot.
224  */
225 ReorderBuffer *
226 ReorderBufferAllocate(void)
227 {
228         ReorderBuffer *buffer;
229         HASHCTL         hash_ctl;
230         MemoryContext new_ctx;
231
232         Assert(MyReplicationSlot != NULL);
233
234         /* allocate memory in own context, to have better accountability */
235         new_ctx = AllocSetContextCreate(CurrentMemoryContext,
236                                                                         "ReorderBuffer",
237                                                                         ALLOCSET_DEFAULT_SIZES);
238
239         buffer =
240                 (ReorderBuffer *) MemoryContextAlloc(new_ctx, sizeof(ReorderBuffer));
241
242         memset(&hash_ctl, 0, sizeof(hash_ctl));
243
244         buffer->context = new_ctx;
245
246         buffer->change_context = SlabContextCreate(new_ctx,
247                                                                                            "Change",
248                                                                                            SLAB_DEFAULT_BLOCK_SIZE,
249                                                                                            sizeof(ReorderBufferChange));
250
251         buffer->txn_context = SlabContextCreate(new_ctx,
252                                                                                         "TXN",
253                                                                                         SLAB_DEFAULT_BLOCK_SIZE,
254                                                                                         sizeof(ReorderBufferTXN));
255
256         buffer->tup_context = GenerationContextCreate(new_ctx,
257                                                                                                   "Tuples",
258                                                                                                   SLAB_LARGE_BLOCK_SIZE);
259
260         hash_ctl.keysize = sizeof(TransactionId);
261         hash_ctl.entrysize = sizeof(ReorderBufferTXNByIdEnt);
262         hash_ctl.hcxt = buffer->context;
263
264         buffer->by_txn = hash_create("ReorderBufferByXid", 1000, &hash_ctl,
265                                                                  HASH_ELEM | HASH_BLOBS | HASH_CONTEXT);
266
267         buffer->by_txn_last_xid = InvalidTransactionId;
268         buffer->by_txn_last_txn = NULL;
269
270         buffer->outbuf = NULL;
271         buffer->outbufsize = 0;
272
273         buffer->current_restart_decoding_lsn = InvalidXLogRecPtr;
274
275         dlist_init(&buffer->toplevel_by_lsn);
276         dlist_init(&buffer->txns_by_base_snapshot_lsn);
277
278         /*
279          * Ensure there's no stale data from prior uses of this slot, in case some
280          * prior exit avoided calling ReorderBufferFree. Failure to do this can
281          * produce duplicated txns, and it's very cheap if there's nothing there.
282          */
283         ReorderBufferCleanupSerializedTXNs(NameStr(MyReplicationSlot->data.name));
284
285         return buffer;
286 }
287
288 /*
289  * Free a ReorderBuffer
290  */
291 void
292 ReorderBufferFree(ReorderBuffer *rb)
293 {
294         MemoryContext context = rb->context;
295
296         /*
297          * We free separately allocated data by entirely scrapping reorderbuffer's
298          * memory context.
299          */
300         MemoryContextDelete(context);
301
302         /* Free disk space used by unconsumed reorder buffers */
303         ReorderBufferCleanupSerializedTXNs(NameStr(MyReplicationSlot->data.name));
304 }
305
306 /*
307  * Get an unused, possibly preallocated, ReorderBufferTXN.
308  */
309 static ReorderBufferTXN *
310 ReorderBufferGetTXN(ReorderBuffer *rb)
311 {
312         ReorderBufferTXN *txn;
313
314         txn = (ReorderBufferTXN *)
315                 MemoryContextAlloc(rb->txn_context, sizeof(ReorderBufferTXN));
316
317         memset(txn, 0, sizeof(ReorderBufferTXN));
318
319         dlist_init(&txn->changes);
320         dlist_init(&txn->tuplecids);
321         dlist_init(&txn->subtxns);
322
323         return txn;
324 }
325
326 /*
327  * Free a ReorderBufferTXN.
328  */
329 static void
330 ReorderBufferReturnTXN(ReorderBuffer *rb, ReorderBufferTXN *txn)
331 {
332         /* clean the lookup cache if we were cached (quite likely) */
333         if (rb->by_txn_last_xid == txn->xid)
334         {
335                 rb->by_txn_last_xid = InvalidTransactionId;
336                 rb->by_txn_last_txn = NULL;
337         }
338
339         /* free data that's contained */
340
341         if (txn->tuplecid_hash != NULL)
342         {
343                 hash_destroy(txn->tuplecid_hash);
344                 txn->tuplecid_hash = NULL;
345         }
346
347         if (txn->invalidations)
348         {
349                 pfree(txn->invalidations);
350                 txn->invalidations = NULL;
351         }
352
353         pfree(txn);
354 }
355
356 /*
357  * Get an fresh ReorderBufferChange.
358  */
359 ReorderBufferChange *
360 ReorderBufferGetChange(ReorderBuffer *rb)
361 {
362         ReorderBufferChange *change;
363
364         change = (ReorderBufferChange *)
365                 MemoryContextAlloc(rb->change_context, sizeof(ReorderBufferChange));
366
367         memset(change, 0, sizeof(ReorderBufferChange));
368         return change;
369 }
370
371 /*
372  * Free an ReorderBufferChange.
373  */
374 void
375 ReorderBufferReturnChange(ReorderBuffer *rb, ReorderBufferChange *change)
376 {
377         /* free contained data */
378         switch (change->action)
379         {
380                 case REORDER_BUFFER_CHANGE_INSERT:
381                 case REORDER_BUFFER_CHANGE_UPDATE:
382                 case REORDER_BUFFER_CHANGE_DELETE:
383                 case REORDER_BUFFER_CHANGE_INTERNAL_SPEC_INSERT:
384                         if (change->data.tp.newtuple)
385                         {
386                                 ReorderBufferReturnTupleBuf(rb, change->data.tp.newtuple);
387                                 change->data.tp.newtuple = NULL;
388                         }
389
390                         if (change->data.tp.oldtuple)
391                         {
392                                 ReorderBufferReturnTupleBuf(rb, change->data.tp.oldtuple);
393                                 change->data.tp.oldtuple = NULL;
394                         }
395                         break;
396                 case REORDER_BUFFER_CHANGE_MESSAGE:
397                         if (change->data.msg.prefix != NULL)
398                                 pfree(change->data.msg.prefix);
399                         change->data.msg.prefix = NULL;
400                         if (change->data.msg.message != NULL)
401                                 pfree(change->data.msg.message);
402                         change->data.msg.message = NULL;
403                         break;
404                 case REORDER_BUFFER_CHANGE_INTERNAL_SNAPSHOT:
405                         if (change->data.snapshot)
406                         {
407                                 ReorderBufferFreeSnap(rb, change->data.snapshot);
408                                 change->data.snapshot = NULL;
409                         }
410                         break;
411                         /* no data in addition to the struct itself */
412                 case REORDER_BUFFER_CHANGE_INTERNAL_SPEC_CONFIRM:
413                 case REORDER_BUFFER_CHANGE_INTERNAL_COMMAND_ID:
414                 case REORDER_BUFFER_CHANGE_INTERNAL_TUPLECID:
415                 case REORDER_BUFFER_CHANGE_TRUNCATE:
416                         break;
417         }
418
419         pfree(change);
420 }
421
422 /*
423  * Get a fresh ReorderBufferTupleBuf fitting at least a tuple of size
424  * tuple_len (excluding header overhead).
425  */
426 ReorderBufferTupleBuf *
427 ReorderBufferGetTupleBuf(ReorderBuffer *rb, Size tuple_len)
428 {
429         ReorderBufferTupleBuf *tuple;
430         Size            alloc_len;
431
432         alloc_len = tuple_len + SizeofHeapTupleHeader;
433
434         tuple = (ReorderBufferTupleBuf *)
435                 MemoryContextAlloc(rb->tup_context,
436                                                    sizeof(ReorderBufferTupleBuf) +
437                                                    MAXIMUM_ALIGNOF + alloc_len);
438         tuple->alloc_tuple_size = alloc_len;
439         tuple->tuple.t_data = ReorderBufferTupleBufData(tuple);
440
441         return tuple;
442 }
443
444 /*
445  * Free an ReorderBufferTupleBuf.
446  */
447 void
448 ReorderBufferReturnTupleBuf(ReorderBuffer *rb, ReorderBufferTupleBuf *tuple)
449 {
450         pfree(tuple);
451 }
452
453 /*
454  * Return the ReorderBufferTXN from the given buffer, specified by Xid.
455  * If create is true, and a transaction doesn't already exist, create it
456  * (with the given LSN, and as top transaction if that's specified);
457  * when this happens, is_new is set to true.
458  */
459 static ReorderBufferTXN *
460 ReorderBufferTXNByXid(ReorderBuffer *rb, TransactionId xid, bool create,
461                                           bool *is_new, XLogRecPtr lsn, bool create_as_top)
462 {
463         ReorderBufferTXN *txn;
464         ReorderBufferTXNByIdEnt *ent;
465         bool            found;
466
467         Assert(TransactionIdIsValid(xid));
468
469         /*
470          * Check the one-entry lookup cache first
471          */
472         if (TransactionIdIsValid(rb->by_txn_last_xid) &&
473                 rb->by_txn_last_xid == xid)
474         {
475                 txn = rb->by_txn_last_txn;
476
477                 if (txn != NULL)
478                 {
479                         /* found it, and it's valid */
480                         if (is_new)
481                                 *is_new = false;
482                         return txn;
483                 }
484
485                 /*
486                  * cached as non-existent, and asked not to create? Then nothing else
487                  * to do.
488                  */
489                 if (!create)
490                         return NULL;
491                 /* otherwise fall through to create it */
492         }
493
494         /*
495          * If the cache wasn't hit or it yielded an "does-not-exist" and we want
496          * to create an entry.
497          */
498
499         /* search the lookup table */
500         ent = (ReorderBufferTXNByIdEnt *)
501                 hash_search(rb->by_txn,
502                                         (void *) &xid,
503                                         create ? HASH_ENTER : HASH_FIND,
504                                         &found);
505         if (found)
506                 txn = ent->txn;
507         else if (create)
508         {
509                 /* initialize the new entry, if creation was requested */
510                 Assert(ent != NULL);
511                 Assert(lsn != InvalidXLogRecPtr);
512
513                 ent->txn = ReorderBufferGetTXN(rb);
514                 ent->txn->xid = xid;
515                 txn = ent->txn;
516                 txn->first_lsn = lsn;
517                 txn->restart_decoding_lsn = rb->current_restart_decoding_lsn;
518
519                 if (create_as_top)
520                 {
521                         dlist_push_tail(&rb->toplevel_by_lsn, &txn->node);
522                         AssertTXNLsnOrder(rb);
523                 }
524         }
525         else
526                 txn = NULL;                             /* not found and not asked to create */
527
528         /* update cache */
529         rb->by_txn_last_xid = xid;
530         rb->by_txn_last_txn = txn;
531
532         if (is_new)
533                 *is_new = !found;
534
535         Assert(!create || txn != NULL);
536         return txn;
537 }
538
539 /*
540  * Queue a change into a transaction so it can be replayed upon commit.
541  */
542 void
543 ReorderBufferQueueChange(ReorderBuffer *rb, TransactionId xid, XLogRecPtr lsn,
544                                                  ReorderBufferChange *change)
545 {
546         ReorderBufferTXN *txn;
547
548         txn = ReorderBufferTXNByXid(rb, xid, true, NULL, lsn, true);
549
550         change->lsn = lsn;
551         Assert(InvalidXLogRecPtr != lsn);
552         dlist_push_tail(&txn->changes, &change->node);
553         txn->nentries++;
554         txn->nentries_mem++;
555
556         ReorderBufferCheckSerializeTXN(rb, txn);
557 }
558
559 /*
560  * Queue message into a transaction so it can be processed upon commit.
561  */
562 void
563 ReorderBufferQueueMessage(ReorderBuffer *rb, TransactionId xid,
564                                                   Snapshot snapshot, XLogRecPtr lsn,
565                                                   bool transactional, const char *prefix,
566                                                   Size message_size, const char *message)
567 {
568         if (transactional)
569         {
570                 MemoryContext oldcontext;
571                 ReorderBufferChange *change;
572
573                 Assert(xid != InvalidTransactionId);
574
575                 oldcontext = MemoryContextSwitchTo(rb->context);
576
577                 change = ReorderBufferGetChange(rb);
578                 change->action = REORDER_BUFFER_CHANGE_MESSAGE;
579                 change->data.msg.prefix = pstrdup(prefix);
580                 change->data.msg.message_size = message_size;
581                 change->data.msg.message = palloc(message_size);
582                 memcpy(change->data.msg.message, message, message_size);
583
584                 ReorderBufferQueueChange(rb, xid, lsn, change);
585
586                 MemoryContextSwitchTo(oldcontext);
587         }
588         else
589         {
590                 ReorderBufferTXN *txn = NULL;
591                 volatile Snapshot snapshot_now = snapshot;
592
593                 if (xid != InvalidTransactionId)
594                         txn = ReorderBufferTXNByXid(rb, xid, true, NULL, lsn, true);
595
596                 /* setup snapshot to allow catalog access */
597                 SetupHistoricSnapshot(snapshot_now, NULL);
598                 PG_TRY();
599                 {
600                         rb->message(rb, txn, lsn, false, prefix, message_size, message);
601
602                         TeardownHistoricSnapshot(false);
603                 }
604                 PG_CATCH();
605                 {
606                         TeardownHistoricSnapshot(true);
607                         PG_RE_THROW();
608                 }
609                 PG_END_TRY();
610         }
611 }
612
613 /*
614  * AssertTXNLsnOrder
615  *              Verify LSN ordering of transaction lists in the reorderbuffer
616  *
617  * Other LSN-related invariants are checked too.
618  *
619  * No-op if assertions are not in use.
620  */
621 static void
622 AssertTXNLsnOrder(ReorderBuffer *rb)
623 {
624 #ifdef USE_ASSERT_CHECKING
625         dlist_iter      iter;
626         XLogRecPtr      prev_first_lsn = InvalidXLogRecPtr;
627         XLogRecPtr      prev_base_snap_lsn = InvalidXLogRecPtr;
628
629         dlist_foreach(iter, &rb->toplevel_by_lsn)
630         {
631                 ReorderBufferTXN *cur_txn = dlist_container(ReorderBufferTXN, node,
632                                                                                                         iter.cur);
633
634                 /* start LSN must be set */
635                 Assert(cur_txn->first_lsn != InvalidXLogRecPtr);
636
637                 /* If there is an end LSN, it must be higher than start LSN */
638                 if (cur_txn->end_lsn != InvalidXLogRecPtr)
639                         Assert(cur_txn->first_lsn <= cur_txn->end_lsn);
640
641                 /* Current initial LSN must be strictly higher than previous */
642                 if (prev_first_lsn != InvalidXLogRecPtr)
643                         Assert(prev_first_lsn < cur_txn->first_lsn);
644
645                 /* known-as-subtxn txns must not be listed */
646                 Assert(!cur_txn->is_known_as_subxact);
647
648                 prev_first_lsn = cur_txn->first_lsn;
649         }
650
651         dlist_foreach(iter, &rb->txns_by_base_snapshot_lsn)
652         {
653                 ReorderBufferTXN *cur_txn = dlist_container(ReorderBufferTXN,
654                                                                                                         base_snapshot_node,
655                                                                                                         iter.cur);
656
657                 /* base snapshot (and its LSN) must be set */
658                 Assert(cur_txn->base_snapshot != NULL);
659                 Assert(cur_txn->base_snapshot_lsn != InvalidXLogRecPtr);
660
661                 /* current LSN must be strictly higher than previous */
662                 if (prev_base_snap_lsn != InvalidXLogRecPtr)
663                         Assert(prev_base_snap_lsn < cur_txn->base_snapshot_lsn);
664
665                 /* known-as-subtxn txns must not be listed */
666                 Assert(!cur_txn->is_known_as_subxact);
667
668                 prev_base_snap_lsn = cur_txn->base_snapshot_lsn;
669         }
670 #endif
671 }
672
673 /*
674  * ReorderBufferGetOldestTXN
675  *              Return oldest transaction in reorderbuffer
676  */
677 ReorderBufferTXN *
678 ReorderBufferGetOldestTXN(ReorderBuffer *rb)
679 {
680         ReorderBufferTXN *txn;
681
682         AssertTXNLsnOrder(rb);
683
684         if (dlist_is_empty(&rb->toplevel_by_lsn))
685                 return NULL;
686
687         txn = dlist_head_element(ReorderBufferTXN, node, &rb->toplevel_by_lsn);
688
689         Assert(!txn->is_known_as_subxact);
690         Assert(txn->first_lsn != InvalidXLogRecPtr);
691         return txn;
692 }
693
694 /*
695  * ReorderBufferGetOldestXmin
696  *              Return oldest Xmin in reorderbuffer
697  *
698  * Returns oldest possibly running Xid from the point of view of snapshots
699  * used in the transactions kept by reorderbuffer, or InvalidTransactionId if
700  * there are none.
701  *
702  * Since snapshots are assigned monotonically, this equals the Xmin of the
703  * base snapshot with minimal base_snapshot_lsn.
704  */
705 TransactionId
706 ReorderBufferGetOldestXmin(ReorderBuffer *rb)
707 {
708         ReorderBufferTXN *txn;
709
710         AssertTXNLsnOrder(rb);
711
712         if (dlist_is_empty(&rb->txns_by_base_snapshot_lsn))
713                 return InvalidTransactionId;
714
715         txn = dlist_head_element(ReorderBufferTXN, base_snapshot_node,
716                                                          &rb->txns_by_base_snapshot_lsn);
717         return txn->base_snapshot->xmin;
718 }
719
720 void
721 ReorderBufferSetRestartPoint(ReorderBuffer *rb, XLogRecPtr ptr)
722 {
723         rb->current_restart_decoding_lsn = ptr;
724 }
725
726 /*
727  * ReorderBufferAssignChild
728  *
729  * Make note that we know that subxid is a subtransaction of xid, seen as of
730  * the given lsn.
731  */
732 void
733 ReorderBufferAssignChild(ReorderBuffer *rb, TransactionId xid,
734                                                  TransactionId subxid, XLogRecPtr lsn)
735 {
736         ReorderBufferTXN *txn;
737         ReorderBufferTXN *subtxn;
738         bool            new_top;
739         bool            new_sub;
740
741         txn = ReorderBufferTXNByXid(rb, xid, true, &new_top, lsn, true);
742         subtxn = ReorderBufferTXNByXid(rb, subxid, true, &new_sub, lsn, false);
743
744         if (new_top && !new_sub)
745                 elog(ERROR, "subtransaction logged without previous top-level txn record");
746
747         if (!new_sub)
748         {
749                 if (subtxn->is_known_as_subxact)
750                 {
751                         /* already associated, nothing to do */
752                         return;
753                 }
754                 else
755                 {
756                         /*
757                          * We already saw this transaction, but initially added it to the
758                          * list of top-level txns.  Now that we know it's not top-level,
759                          * remove it from there.
760                          */
761                         dlist_delete(&subtxn->node);
762                 }
763         }
764
765         subtxn->is_known_as_subxact = true;
766         subtxn->toplevel_xid = xid;
767         Assert(subtxn->nsubtxns == 0);
768
769         /* add to subtransaction list */
770         dlist_push_tail(&txn->subtxns, &subtxn->node);
771         txn->nsubtxns++;
772
773         /* Possibly transfer the subtxn's snapshot to its top-level txn. */
774         ReorderBufferTransferSnapToParent(txn, subtxn);
775
776         /* Verify LSN-ordering invariant */
777         AssertTXNLsnOrder(rb);
778 }
779
780 /*
781  * ReorderBufferTransferSnapToParent
782  *              Transfer base snapshot from subtxn to top-level txn, if needed
783  *
784  * This is done if the top-level txn doesn't have a base snapshot, or if the
785  * subtxn's base snapshot has an earlier LSN than the top-level txn's base
786  * snapshot's LSN.  This can happen if there are no changes in the toplevel
787  * txn but there are some in the subtxn, or the first change in subtxn has
788  * earlier LSN than first change in the top-level txn and we learned about
789  * their kinship only now.
790  *
791  * The subtransaction's snapshot is cleared regardless of the transfer
792  * happening, since it's not needed anymore in either case.
793  *
794  * We do this as soon as we become aware of their kinship, to avoid queueing
795  * extra snapshots to txns known-as-subtxns -- only top-level txns will
796  * receive further snapshots.
797  */
798 static void
799 ReorderBufferTransferSnapToParent(ReorderBufferTXN *txn,
800                                                                   ReorderBufferTXN *subtxn)
801 {
802         Assert(subtxn->toplevel_xid == txn->xid);
803
804         if (subtxn->base_snapshot != NULL)
805         {
806                 if (txn->base_snapshot == NULL ||
807                         subtxn->base_snapshot_lsn < txn->base_snapshot_lsn)
808                 {
809                         /*
810                          * If the toplevel transaction already has a base snapshot but
811                          * it's newer than the subxact's, purge it.
812                          */
813                         if (txn->base_snapshot != NULL)
814                         {
815                                 SnapBuildSnapDecRefcount(txn->base_snapshot);
816                                 dlist_delete(&txn->base_snapshot_node);
817                         }
818
819                         /*
820                          * The snapshot is now the top transaction's; transfer it, and
821                          * adjust the list position of the top transaction in the list by
822                          * moving it to where the subtransaction is.
823                          */
824                         txn->base_snapshot = subtxn->base_snapshot;
825                         txn->base_snapshot_lsn = subtxn->base_snapshot_lsn;
826                         dlist_insert_before(&subtxn->base_snapshot_node,
827                                                                 &txn->base_snapshot_node);
828
829                         /*
830                          * The subtransaction doesn't have a snapshot anymore (so it
831                          * mustn't be in the list.)
832                          */
833                         subtxn->base_snapshot = NULL;
834                         subtxn->base_snapshot_lsn = InvalidXLogRecPtr;
835                         dlist_delete(&subtxn->base_snapshot_node);
836                 }
837                 else
838                 {
839                         /* Base snap of toplevel is fine, so subxact's is not needed */
840                         SnapBuildSnapDecRefcount(subtxn->base_snapshot);
841                         dlist_delete(&subtxn->base_snapshot_node);
842                         subtxn->base_snapshot = NULL;
843                         subtxn->base_snapshot_lsn = InvalidXLogRecPtr;
844                 }
845         }
846 }
847
848 /*
849  * Associate a subtransaction with its toplevel transaction at commit
850  * time. There may be no further changes added after this.
851  */
852 void
853 ReorderBufferCommitChild(ReorderBuffer *rb, TransactionId xid,
854                                                  TransactionId subxid, XLogRecPtr commit_lsn,
855                                                  XLogRecPtr end_lsn)
856 {
857         ReorderBufferTXN *subtxn;
858
859         subtxn = ReorderBufferTXNByXid(rb, subxid, false, NULL,
860                                                                    InvalidXLogRecPtr, false);
861
862         /*
863          * No need to do anything if that subtxn didn't contain any changes
864          */
865         if (!subtxn)
866                 return;
867
868         subtxn->final_lsn = commit_lsn;
869         subtxn->end_lsn = end_lsn;
870
871         /*
872          * Assign this subxact as a child of the toplevel xact (no-op if already
873          * done.)
874          */
875         ReorderBufferAssignChild(rb, xid, subxid, InvalidXLogRecPtr);
876 }
877
878
879 /*
880  * Support for efficiently iterating over a transaction's and its
881  * subtransactions' changes.
882  *
883  * We do by doing a k-way merge between transactions/subtransactions. For that
884  * we model the current heads of the different transactions as a binary heap
885  * so we easily know which (sub-)transaction has the change with the smallest
886  * lsn next.
887  *
888  * We assume the changes in individual transactions are already sorted by LSN.
889  */
890
891 /*
892  * Binary heap comparison function.
893  */
894 static int
895 ReorderBufferIterCompare(Datum a, Datum b, void *arg)
896 {
897         ReorderBufferIterTXNState *state = (ReorderBufferIterTXNState *) arg;
898         XLogRecPtr      pos_a = state->entries[DatumGetInt32(a)].lsn;
899         XLogRecPtr      pos_b = state->entries[DatumGetInt32(b)].lsn;
900
901         if (pos_a < pos_b)
902                 return 1;
903         else if (pos_a == pos_b)
904                 return 0;
905         return -1;
906 }
907
908 /*
909  * Allocate & initialize an iterator which iterates in lsn order over a
910  * transaction and all its subtransactions.
911  */
912 static ReorderBufferIterTXNState *
913 ReorderBufferIterTXNInit(ReorderBuffer *rb, ReorderBufferTXN *txn)
914 {
915         Size            nr_txns = 0;
916         ReorderBufferIterTXNState *state;
917         dlist_iter      cur_txn_i;
918         int32           off;
919
920         /*
921          * Calculate the size of our heap: one element for every transaction that
922          * contains changes.  (Besides the transactions already in the reorder
923          * buffer, we count the one we were directly passed.)
924          */
925         if (txn->nentries > 0)
926                 nr_txns++;
927
928         dlist_foreach(cur_txn_i, &txn->subtxns)
929         {
930                 ReorderBufferTXN *cur_txn;
931
932                 cur_txn = dlist_container(ReorderBufferTXN, node, cur_txn_i.cur);
933
934                 if (cur_txn->nentries > 0)
935                         nr_txns++;
936         }
937
938         /*
939          * TODO: Consider adding fastpath for the rather common nr_txns=1 case, no
940          * need to allocate/build a heap then.
941          */
942
943         /* allocate iteration state */
944         state = (ReorderBufferIterTXNState *)
945                 MemoryContextAllocZero(rb->context,
946                                                            sizeof(ReorderBufferIterTXNState) +
947                                                            sizeof(ReorderBufferIterTXNEntry) * nr_txns);
948
949         state->nr_txns = nr_txns;
950         dlist_init(&state->old_change);
951
952         for (off = 0; off < state->nr_txns; off++)
953         {
954                 state->entries[off].fd = -1;
955                 state->entries[off].segno = 0;
956         }
957
958         /* allocate heap */
959         state->heap = binaryheap_allocate(state->nr_txns,
960                                                                           ReorderBufferIterCompare,
961                                                                           state);
962
963         /*
964          * Now insert items into the binary heap, in an unordered fashion.  (We
965          * will run a heap assembly step at the end; this is more efficient.)
966          */
967
968         off = 0;
969
970         /* add toplevel transaction if it contains changes */
971         if (txn->nentries > 0)
972         {
973                 ReorderBufferChange *cur_change;
974
975                 if (txn->serialized)
976                 {
977                         /* serialize remaining changes */
978                         ReorderBufferSerializeTXN(rb, txn);
979                         ReorderBufferRestoreChanges(rb, txn, &state->entries[off].fd,
980                                                                                 &state->entries[off].segno);
981                 }
982
983                 cur_change = dlist_head_element(ReorderBufferChange, node,
984                                                                                 &txn->changes);
985
986                 state->entries[off].lsn = cur_change->lsn;
987                 state->entries[off].change = cur_change;
988                 state->entries[off].txn = txn;
989
990                 binaryheap_add_unordered(state->heap, Int32GetDatum(off++));
991         }
992
993         /* add subtransactions if they contain changes */
994         dlist_foreach(cur_txn_i, &txn->subtxns)
995         {
996                 ReorderBufferTXN *cur_txn;
997
998                 cur_txn = dlist_container(ReorderBufferTXN, node, cur_txn_i.cur);
999
1000                 if (cur_txn->nentries > 0)
1001                 {
1002                         ReorderBufferChange *cur_change;
1003
1004                         if (cur_txn->serialized)
1005                         {
1006                                 /* serialize remaining changes */
1007                                 ReorderBufferSerializeTXN(rb, cur_txn);
1008                                 ReorderBufferRestoreChanges(rb, cur_txn,
1009                                                                                         &state->entries[off].fd,
1010                                                                                         &state->entries[off].segno);
1011                         }
1012                         cur_change = dlist_head_element(ReorderBufferChange, node,
1013                                                                                         &cur_txn->changes);
1014
1015                         state->entries[off].lsn = cur_change->lsn;
1016                         state->entries[off].change = cur_change;
1017                         state->entries[off].txn = cur_txn;
1018
1019                         binaryheap_add_unordered(state->heap, Int32GetDatum(off++));
1020                 }
1021         }
1022
1023         /* assemble a valid binary heap */
1024         binaryheap_build(state->heap);
1025
1026         return state;
1027 }
1028
1029 /*
1030  * Return the next change when iterating over a transaction and its
1031  * subtransactions.
1032  *
1033  * Returns NULL when no further changes exist.
1034  */
1035 static ReorderBufferChange *
1036 ReorderBufferIterTXNNext(ReorderBuffer *rb, ReorderBufferIterTXNState *state)
1037 {
1038         ReorderBufferChange *change;
1039         ReorderBufferIterTXNEntry *entry;
1040         int32           off;
1041
1042         /* nothing there anymore */
1043         if (state->heap->bh_size == 0)
1044                 return NULL;
1045
1046         off = DatumGetInt32(binaryheap_first(state->heap));
1047         entry = &state->entries[off];
1048
1049         /* free memory we might have "leaked" in the previous *Next call */
1050         if (!dlist_is_empty(&state->old_change))
1051         {
1052                 change = dlist_container(ReorderBufferChange, node,
1053                                                                  dlist_pop_head_node(&state->old_change));
1054                 ReorderBufferReturnChange(rb, change);
1055                 Assert(dlist_is_empty(&state->old_change));
1056         }
1057
1058         change = entry->change;
1059
1060         /*
1061          * update heap with information about which transaction has the next
1062          * relevant change in LSN order
1063          */
1064
1065         /* there are in-memory changes */
1066         if (dlist_has_next(&entry->txn->changes, &entry->change->node))
1067         {
1068                 dlist_node *next = dlist_next_node(&entry->txn->changes, &change->node);
1069                 ReorderBufferChange *next_change =
1070                 dlist_container(ReorderBufferChange, node, next);
1071
1072                 /* txn stays the same */
1073                 state->entries[off].lsn = next_change->lsn;
1074                 state->entries[off].change = next_change;
1075
1076                 binaryheap_replace_first(state->heap, Int32GetDatum(off));
1077                 return change;
1078         }
1079
1080         /* try to load changes from disk */
1081         if (entry->txn->nentries != entry->txn->nentries_mem)
1082         {
1083                 /*
1084                  * Ugly: restoring changes will reuse *Change records, thus delete the
1085                  * current one from the per-tx list and only free in the next call.
1086                  */
1087                 dlist_delete(&change->node);
1088                 dlist_push_tail(&state->old_change, &change->node);
1089
1090                 if (ReorderBufferRestoreChanges(rb, entry->txn, &entry->fd,
1091                                                                                 &state->entries[off].segno))
1092                 {
1093                         /* successfully restored changes from disk */
1094                         ReorderBufferChange *next_change =
1095                         dlist_head_element(ReorderBufferChange, node,
1096                                                            &entry->txn->changes);
1097
1098                         elog(DEBUG2, "restored %u/%u changes from disk",
1099                                  (uint32) entry->txn->nentries_mem,
1100                                  (uint32) entry->txn->nentries);
1101
1102                         Assert(entry->txn->nentries_mem);
1103                         /* txn stays the same */
1104                         state->entries[off].lsn = next_change->lsn;
1105                         state->entries[off].change = next_change;
1106                         binaryheap_replace_first(state->heap, Int32GetDatum(off));
1107
1108                         return change;
1109                 }
1110         }
1111
1112         /* ok, no changes there anymore, remove */
1113         binaryheap_remove_first(state->heap);
1114
1115         return change;
1116 }
1117
1118 /*
1119  * Deallocate the iterator
1120  */
1121 static void
1122 ReorderBufferIterTXNFinish(ReorderBuffer *rb,
1123                                                    ReorderBufferIterTXNState *state)
1124 {
1125         int32           off;
1126
1127         for (off = 0; off < state->nr_txns; off++)
1128         {
1129                 if (state->entries[off].fd != -1)
1130                         CloseTransientFile(state->entries[off].fd);
1131         }
1132
1133         /* free memory we might have "leaked" in the last *Next call */
1134         if (!dlist_is_empty(&state->old_change))
1135         {
1136                 ReorderBufferChange *change;
1137
1138                 change = dlist_container(ReorderBufferChange, node,
1139                                                                  dlist_pop_head_node(&state->old_change));
1140                 ReorderBufferReturnChange(rb, change);
1141                 Assert(dlist_is_empty(&state->old_change));
1142         }
1143
1144         binaryheap_free(state->heap);
1145         pfree(state);
1146 }
1147
1148 /*
1149  * Cleanup the contents of a transaction, usually after the transaction
1150  * committed or aborted.
1151  */
1152 static void
1153 ReorderBufferCleanupTXN(ReorderBuffer *rb, ReorderBufferTXN *txn)
1154 {
1155         bool            found;
1156         dlist_mutable_iter iter;
1157
1158         /* cleanup subtransactions & their changes */
1159         dlist_foreach_modify(iter, &txn->subtxns)
1160         {
1161                 ReorderBufferTXN *subtxn;
1162
1163                 subtxn = dlist_container(ReorderBufferTXN, node, iter.cur);
1164
1165                 /*
1166                  * Subtransactions are always associated to the toplevel TXN, even if
1167                  * they originally were happening inside another subtxn, so we won't
1168                  * ever recurse more than one level deep here.
1169                  */
1170                 Assert(subtxn->is_known_as_subxact);
1171                 Assert(subtxn->nsubtxns == 0);
1172
1173                 ReorderBufferCleanupTXN(rb, subtxn);
1174         }
1175
1176         /* cleanup changes in the toplevel txn */
1177         dlist_foreach_modify(iter, &txn->changes)
1178         {
1179                 ReorderBufferChange *change;
1180
1181                 change = dlist_container(ReorderBufferChange, node, iter.cur);
1182
1183                 ReorderBufferReturnChange(rb, change);
1184         }
1185
1186         /*
1187          * Cleanup the tuplecids we stored for decoding catalog snapshot access.
1188          * They are always stored in the toplevel transaction.
1189          */
1190         dlist_foreach_modify(iter, &txn->tuplecids)
1191         {
1192                 ReorderBufferChange *change;
1193
1194                 change = dlist_container(ReorderBufferChange, node, iter.cur);
1195                 Assert(change->action == REORDER_BUFFER_CHANGE_INTERNAL_TUPLECID);
1196                 ReorderBufferReturnChange(rb, change);
1197         }
1198
1199         /*
1200          * Cleanup the base snapshot, if set.
1201          */
1202         if (txn->base_snapshot != NULL)
1203         {
1204                 SnapBuildSnapDecRefcount(txn->base_snapshot);
1205                 dlist_delete(&txn->base_snapshot_node);
1206         }
1207
1208         /*
1209          * Remove TXN from its containing list.
1210          *
1211          * Note: if txn->is_known_as_subxact, we are deleting the TXN from its
1212          * parent's list of known subxacts; this leaves the parent's nsubxacts
1213          * count too high, but we don't care.  Otherwise, we are deleting the TXN
1214          * from the LSN-ordered list of toplevel TXNs.
1215          */
1216         dlist_delete(&txn->node);
1217
1218         /* now remove reference from buffer */
1219         hash_search(rb->by_txn,
1220                                 (void *) &txn->xid,
1221                                 HASH_REMOVE,
1222                                 &found);
1223         Assert(found);
1224
1225         /* remove entries spilled to disk */
1226         if (txn->serialized)
1227                 ReorderBufferRestoreCleanup(rb, txn);
1228
1229         /* deallocate */
1230         ReorderBufferReturnTXN(rb, txn);
1231 }
1232
1233 /*
1234  * Build a hash with a (relfilenode, ctid) -> (cmin, cmax) mapping for use by
1235  * tqual.c's HeapTupleSatisfiesHistoricMVCC.
1236  */
1237 static void
1238 ReorderBufferBuildTupleCidHash(ReorderBuffer *rb, ReorderBufferTXN *txn)
1239 {
1240         dlist_iter      iter;
1241         HASHCTL         hash_ctl;
1242
1243         if (!txn->has_catalog_changes || dlist_is_empty(&txn->tuplecids))
1244                 return;
1245
1246         memset(&hash_ctl, 0, sizeof(hash_ctl));
1247
1248         hash_ctl.keysize = sizeof(ReorderBufferTupleCidKey);
1249         hash_ctl.entrysize = sizeof(ReorderBufferTupleCidEnt);
1250         hash_ctl.hcxt = rb->context;
1251
1252         /*
1253          * create the hash with the exact number of to-be-stored tuplecids from
1254          * the start
1255          */
1256         txn->tuplecid_hash =
1257                 hash_create("ReorderBufferTupleCid", txn->ntuplecids, &hash_ctl,
1258                                         HASH_ELEM | HASH_BLOBS | HASH_CONTEXT);
1259
1260         dlist_foreach(iter, &txn->tuplecids)
1261         {
1262                 ReorderBufferTupleCidKey key;
1263                 ReorderBufferTupleCidEnt *ent;
1264                 bool            found;
1265                 ReorderBufferChange *change;
1266
1267                 change = dlist_container(ReorderBufferChange, node, iter.cur);
1268
1269                 Assert(change->action == REORDER_BUFFER_CHANGE_INTERNAL_TUPLECID);
1270
1271                 /* be careful about padding */
1272                 memset(&key, 0, sizeof(ReorderBufferTupleCidKey));
1273
1274                 key.relnode = change->data.tuplecid.node;
1275
1276                 ItemPointerCopy(&change->data.tuplecid.tid,
1277                                                 &key.tid);
1278
1279                 ent = (ReorderBufferTupleCidEnt *)
1280                         hash_search(txn->tuplecid_hash,
1281                                                 (void *) &key,
1282                                                 HASH_ENTER | HASH_FIND,
1283                                                 &found);
1284                 if (!found)
1285                 {
1286                         ent->cmin = change->data.tuplecid.cmin;
1287                         ent->cmax = change->data.tuplecid.cmax;
1288                         ent->combocid = change->data.tuplecid.combocid;
1289                 }
1290                 else
1291                 {
1292                         Assert(ent->cmin == change->data.tuplecid.cmin);
1293                         Assert(ent->cmax == InvalidCommandId ||
1294                                    ent->cmax == change->data.tuplecid.cmax);
1295
1296                         /*
1297                          * if the tuple got valid in this transaction and now got deleted
1298                          * we already have a valid cmin stored. The cmax will be
1299                          * InvalidCommandId though.
1300                          */
1301                         ent->cmax = change->data.tuplecid.cmax;
1302                 }
1303         }
1304 }
1305
1306 /*
1307  * Copy a provided snapshot so we can modify it privately. This is needed so
1308  * that catalog modifying transactions can look into intermediate catalog
1309  * states.
1310  */
1311 static Snapshot
1312 ReorderBufferCopySnap(ReorderBuffer *rb, Snapshot orig_snap,
1313                                           ReorderBufferTXN *txn, CommandId cid)
1314 {
1315         Snapshot        snap;
1316         dlist_iter      iter;
1317         int                     i = 0;
1318         Size            size;
1319
1320         size = sizeof(SnapshotData) +
1321                 sizeof(TransactionId) * orig_snap->xcnt +
1322                 sizeof(TransactionId) * (txn->nsubtxns + 1);
1323
1324         snap = MemoryContextAllocZero(rb->context, size);
1325         memcpy(snap, orig_snap, sizeof(SnapshotData));
1326
1327         snap->copied = true;
1328         snap->active_count = 1;         /* mark as active so nobody frees it */
1329         snap->regd_count = 0;
1330         snap->xip = (TransactionId *) (snap + 1);
1331
1332         memcpy(snap->xip, orig_snap->xip, sizeof(TransactionId) * snap->xcnt);
1333
1334         /*
1335          * snap->subxip contains all txids that belong to our transaction which we
1336          * need to check via cmin/cmax. That's why we store the toplevel
1337          * transaction in there as well.
1338          */
1339         snap->subxip = snap->xip + snap->xcnt;
1340         snap->subxip[i++] = txn->xid;
1341
1342         /*
1343          * nsubxcnt isn't decreased when subtransactions abort, so count manually.
1344          * Since it's an upper boundary it is safe to use it for the allocation
1345          * above.
1346          */
1347         snap->subxcnt = 1;
1348
1349         dlist_foreach(iter, &txn->subtxns)
1350         {
1351                 ReorderBufferTXN *sub_txn;
1352
1353                 sub_txn = dlist_container(ReorderBufferTXN, node, iter.cur);
1354                 snap->subxip[i++] = sub_txn->xid;
1355                 snap->subxcnt++;
1356         }
1357
1358         /* sort so we can bsearch() later */
1359         qsort(snap->subxip, snap->subxcnt, sizeof(TransactionId), xidComparator);
1360
1361         /* store the specified current CommandId */
1362         snap->curcid = cid;
1363
1364         return snap;
1365 }
1366
1367 /*
1368  * Free a previously ReorderBufferCopySnap'ed snapshot
1369  */
1370 static void
1371 ReorderBufferFreeSnap(ReorderBuffer *rb, Snapshot snap)
1372 {
1373         if (snap->copied)
1374                 pfree(snap);
1375         else
1376                 SnapBuildSnapDecRefcount(snap);
1377 }
1378
1379 /*
1380  * Perform the replay of a transaction and its non-aborted subtransactions.
1381  *
1382  * Subtransactions previously have to be processed by
1383  * ReorderBufferCommitChild(), even if previously assigned to the toplevel
1384  * transaction with ReorderBufferAssignChild.
1385  *
1386  * We currently can only decode a transaction's contents when its commit
1387  * record is read because that's the only place where we know about cache
1388  * invalidations. Thus, once a toplevel commit is read, we iterate over the top
1389  * and subtransactions (using a k-way merge) and replay the changes in lsn
1390  * order.
1391  */
1392 void
1393 ReorderBufferCommit(ReorderBuffer *rb, TransactionId xid,
1394                                         XLogRecPtr commit_lsn, XLogRecPtr end_lsn,
1395                                         TimestampTz commit_time,
1396                                         RepOriginId origin_id, XLogRecPtr origin_lsn)
1397 {
1398         ReorderBufferTXN *txn;
1399         volatile Snapshot snapshot_now;
1400         volatile CommandId command_id = FirstCommandId;
1401         bool            using_subtxn;
1402         ReorderBufferIterTXNState *volatile iterstate = NULL;
1403
1404         txn = ReorderBufferTXNByXid(rb, xid, false, NULL, InvalidXLogRecPtr,
1405                                                                 false);
1406
1407         /* unknown transaction, nothing to replay */
1408         if (txn == NULL)
1409                 return;
1410
1411         txn->final_lsn = commit_lsn;
1412         txn->end_lsn = end_lsn;
1413         txn->commit_time = commit_time;
1414         txn->origin_id = origin_id;
1415         txn->origin_lsn = origin_lsn;
1416
1417         /*
1418          * If this transaction has no snapshot, it didn't make any changes to the
1419          * database, so there's nothing to decode.  Note that
1420          * ReorderBufferCommitChild will have transferred any snapshots from
1421          * subtransactions if there were any.
1422          */
1423         if (txn->base_snapshot == NULL)
1424         {
1425                 Assert(txn->ninvalidations == 0);
1426                 ReorderBufferCleanupTXN(rb, txn);
1427                 return;
1428         }
1429
1430         snapshot_now = txn->base_snapshot;
1431
1432         /* build data to be able to lookup the CommandIds of catalog tuples */
1433         ReorderBufferBuildTupleCidHash(rb, txn);
1434
1435         /* setup the initial snapshot */
1436         SetupHistoricSnapshot(snapshot_now, txn->tuplecid_hash);
1437
1438         /*
1439          * Decoding needs access to syscaches et al., which in turn use
1440          * heavyweight locks and such. Thus we need to have enough state around to
1441          * keep track of those.  The easiest way is to simply use a transaction
1442          * internally.  That also allows us to easily enforce that nothing writes
1443          * to the database by checking for xid assignments.
1444          *
1445          * When we're called via the SQL SRF there's already a transaction
1446          * started, so start an explicit subtransaction there.
1447          */
1448         using_subtxn = IsTransactionOrTransactionBlock();
1449
1450         PG_TRY();
1451         {
1452                 ReorderBufferChange *change;
1453                 ReorderBufferChange *specinsert = NULL;
1454
1455                 if (using_subtxn)
1456                         BeginInternalSubTransaction("replay");
1457                 else
1458                         StartTransactionCommand();
1459
1460                 rb->begin(rb, txn);
1461
1462                 iterstate = ReorderBufferIterTXNInit(rb, txn);
1463                 while ((change = ReorderBufferIterTXNNext(rb, iterstate)) != NULL)
1464                 {
1465                         Relation        relation = NULL;
1466                         Oid                     reloid;
1467
1468                         switch (change->action)
1469                         {
1470                                 case REORDER_BUFFER_CHANGE_INTERNAL_SPEC_CONFIRM:
1471
1472                                         /*
1473                                          * Confirmation for speculative insertion arrived. Simply
1474                                          * use as a normal record. It'll be cleaned up at the end
1475                                          * of INSERT processing.
1476                                          */
1477                                         if (specinsert == NULL)
1478                                                 elog(ERROR, "invalid ordering of speculative insertion changes");
1479                                         Assert(specinsert->data.tp.oldtuple == NULL);
1480                                         change = specinsert;
1481                                         change->action = REORDER_BUFFER_CHANGE_INSERT;
1482
1483                                         /* intentionally fall through */
1484                                 case REORDER_BUFFER_CHANGE_INSERT:
1485                                 case REORDER_BUFFER_CHANGE_UPDATE:
1486                                 case REORDER_BUFFER_CHANGE_DELETE:
1487                                         Assert(snapshot_now);
1488
1489                                         reloid = RelidByRelfilenode(change->data.tp.relnode.spcNode,
1490                                                                                                 change->data.tp.relnode.relNode);
1491
1492                                         /*
1493                                          * Catalog tuple without data, emitted while catalog was
1494                                          * in the process of being rewritten.
1495                                          */
1496                                         if (reloid == InvalidOid &&
1497                                                 change->data.tp.newtuple == NULL &&
1498                                                 change->data.tp.oldtuple == NULL)
1499                                                 goto change_done;
1500                                         else if (reloid == InvalidOid)
1501                                                 elog(ERROR, "could not map filenode \"%s\" to relation OID",
1502                                                          relpathperm(change->data.tp.relnode,
1503                                                                                  MAIN_FORKNUM));
1504
1505                                         relation = RelationIdGetRelation(reloid);
1506
1507                                         if (relation == NULL)
1508                                                 elog(ERROR, "could not open relation with OID %u (for filenode \"%s\")",
1509                                                          reloid,
1510                                                          relpathperm(change->data.tp.relnode,
1511                                                                                  MAIN_FORKNUM));
1512
1513                                         if (!RelationIsLogicallyLogged(relation))
1514                                                 goto change_done;
1515
1516                                         /*
1517                                          * Ignore temporary heaps created during DDL unless the
1518                                          * plugin has asked for them.
1519                                          */
1520                                         if (relation->rd_rel->relrewrite && !rb->output_rewrites)
1521                                                 goto change_done;
1522
1523                                         /*
1524                                          * For now ignore sequence changes entirely. Most of the
1525                                          * time they don't log changes using records we
1526                                          * understand, so it doesn't make sense to handle the few
1527                                          * cases we do.
1528                                          */
1529                                         if (relation->rd_rel->relkind == RELKIND_SEQUENCE)
1530                                                 goto change_done;
1531
1532                                         /* user-triggered change */
1533                                         if (!IsToastRelation(relation))
1534                                         {
1535                                                 ReorderBufferToastReplace(rb, txn, relation, change);
1536                                                 rb->apply_change(rb, txn, relation, change);
1537
1538                                                 /*
1539                                                  * Only clear reassembled toast chunks if we're sure
1540                                                  * they're not required anymore. The creator of the
1541                                                  * tuple tells us.
1542                                                  */
1543                                                 if (change->data.tp.clear_toast_afterwards)
1544                                                         ReorderBufferToastReset(rb, txn);
1545                                         }
1546                                         /* we're not interested in toast deletions */
1547                                         else if (change->action == REORDER_BUFFER_CHANGE_INSERT)
1548                                         {
1549                                                 /*
1550                                                  * Need to reassemble the full toasted Datum in
1551                                                  * memory, to ensure the chunks don't get reused till
1552                                                  * we're done remove it from the list of this
1553                                                  * transaction's changes. Otherwise it will get
1554                                                  * freed/reused while restoring spooled data from
1555                                                  * disk.
1556                                                  */
1557                                                 dlist_delete(&change->node);
1558                                                 ReorderBufferToastAppendChunk(rb, txn, relation,
1559                                                                                                           change);
1560                                         }
1561
1562                         change_done:
1563
1564                                         /*
1565                                          * Either speculative insertion was confirmed, or it was
1566                                          * unsuccessful and the record isn't needed anymore.
1567                                          */
1568                                         if (specinsert != NULL)
1569                                         {
1570                                                 ReorderBufferReturnChange(rb, specinsert);
1571                                                 specinsert = NULL;
1572                                         }
1573
1574                                         if (relation != NULL)
1575                                         {
1576                                                 RelationClose(relation);
1577                                                 relation = NULL;
1578                                         }
1579                                         break;
1580
1581                                 case REORDER_BUFFER_CHANGE_INTERNAL_SPEC_INSERT:
1582
1583                                         /*
1584                                          * Speculative insertions are dealt with by delaying the
1585                                          * processing of the insert until the confirmation record
1586                                          * arrives. For that we simply unlink the record from the
1587                                          * chain, so it does not get freed/reused while restoring
1588                                          * spooled data from disk.
1589                                          *
1590                                          * This is safe in the face of concurrent catalog changes
1591                                          * because the relevant relation can't be changed between
1592                                          * speculative insertion and confirmation due to
1593                                          * CheckTableNotInUse() and locking.
1594                                          */
1595
1596                                         /* clear out a pending (and thus failed) speculation */
1597                                         if (specinsert != NULL)
1598                                         {
1599                                                 ReorderBufferReturnChange(rb, specinsert);
1600                                                 specinsert = NULL;
1601                                         }
1602
1603                                         /* and memorize the pending insertion */
1604                                         dlist_delete(&change->node);
1605                                         specinsert = change;
1606                                         break;
1607
1608                                 case REORDER_BUFFER_CHANGE_TRUNCATE:
1609                                         {
1610                                                 int                     i;
1611                                                 int                     nrelids = change->data.truncate.nrelids;
1612                                                 int                     nrelations = 0;
1613                                                 Relation   *relations;
1614
1615                                                 relations = palloc0(nrelids * sizeof(Relation));
1616                                                 for (i = 0; i < nrelids; i++)
1617                                                 {
1618                                                         Oid                     relid = change->data.truncate.relids[i];
1619                                                         Relation        relation;
1620
1621                                                         relation = RelationIdGetRelation(relid);
1622
1623                                                         if (relation == NULL)
1624                                                                 elog(ERROR, "could not open relation with OID %u", relid);
1625
1626                                                         if (!RelationIsLogicallyLogged(relation))
1627                                                                 continue;
1628
1629                                                         relations[nrelations++] = relation;
1630                                                 }
1631
1632                                                 rb->apply_truncate(rb, txn, nrelations, relations, change);
1633
1634                                                 for (i = 0; i < nrelations; i++)
1635                                                         RelationClose(relations[i]);
1636
1637                                                 break;
1638                                         }
1639
1640                                 case REORDER_BUFFER_CHANGE_MESSAGE:
1641                                         rb->message(rb, txn, change->lsn, true,
1642                                                                 change->data.msg.prefix,
1643                                                                 change->data.msg.message_size,
1644                                                                 change->data.msg.message);
1645                                         break;
1646
1647                                 case REORDER_BUFFER_CHANGE_INTERNAL_SNAPSHOT:
1648                                         /* get rid of the old */
1649                                         TeardownHistoricSnapshot(false);
1650
1651                                         if (snapshot_now->copied)
1652                                         {
1653                                                 ReorderBufferFreeSnap(rb, snapshot_now);
1654                                                 snapshot_now =
1655                                                         ReorderBufferCopySnap(rb, change->data.snapshot,
1656                                                                                                   txn, command_id);
1657                                         }
1658
1659                                         /*
1660                                          * Restored from disk, need to be careful not to double
1661                                          * free. We could introduce refcounting for that, but for
1662                                          * now this seems infrequent enough not to care.
1663                                          */
1664                                         else if (change->data.snapshot->copied)
1665                                         {
1666                                                 snapshot_now =
1667                                                         ReorderBufferCopySnap(rb, change->data.snapshot,
1668                                                                                                   txn, command_id);
1669                                         }
1670                                         else
1671                                         {
1672                                                 snapshot_now = change->data.snapshot;
1673                                         }
1674
1675
1676                                         /* and continue with the new one */
1677                                         SetupHistoricSnapshot(snapshot_now, txn->tuplecid_hash);
1678                                         break;
1679
1680                                 case REORDER_BUFFER_CHANGE_INTERNAL_COMMAND_ID:
1681                                         Assert(change->data.command_id != InvalidCommandId);
1682
1683                                         if (command_id < change->data.command_id)
1684                                         {
1685                                                 command_id = change->data.command_id;
1686
1687                                                 if (!snapshot_now->copied)
1688                                                 {
1689                                                         /* we don't use the global one anymore */
1690                                                         snapshot_now = ReorderBufferCopySnap(rb, snapshot_now,
1691                                                                                                                                  txn, command_id);
1692                                                 }
1693
1694                                                 snapshot_now->curcid = command_id;
1695
1696                                                 TeardownHistoricSnapshot(false);
1697                                                 SetupHistoricSnapshot(snapshot_now, txn->tuplecid_hash);
1698
1699                                                 /*
1700                                                  * Every time the CommandId is incremented, we could
1701                                                  * see new catalog contents, so execute all
1702                                                  * invalidations.
1703                                                  */
1704                                                 ReorderBufferExecuteInvalidations(rb, txn);
1705                                         }
1706
1707                                         break;
1708
1709                                 case REORDER_BUFFER_CHANGE_INTERNAL_TUPLECID:
1710                                         elog(ERROR, "tuplecid value in changequeue");
1711                                         break;
1712                         }
1713                 }
1714
1715                 /*
1716                  * There's a speculative insertion remaining, just clean in up, it
1717                  * can't have been successful, otherwise we'd gotten a confirmation
1718                  * record.
1719                  */
1720                 if (specinsert)
1721                 {
1722                         ReorderBufferReturnChange(rb, specinsert);
1723                         specinsert = NULL;
1724                 }
1725
1726                 /* clean up the iterator */
1727                 ReorderBufferIterTXNFinish(rb, iterstate);
1728                 iterstate = NULL;
1729
1730                 /* call commit callback */
1731                 rb->commit(rb, txn, commit_lsn);
1732
1733                 /* this is just a sanity check against bad output plugin behaviour */
1734                 if (GetCurrentTransactionIdIfAny() != InvalidTransactionId)
1735                         elog(ERROR, "output plugin used XID %u",
1736                                  GetCurrentTransactionId());
1737
1738                 /* cleanup */
1739                 TeardownHistoricSnapshot(false);
1740
1741                 /*
1742                  * Aborting the current (sub-)transaction as a whole has the right
1743                  * semantics. We want all locks acquired in here to be released, not
1744                  * reassigned to the parent and we do not want any database access
1745                  * have persistent effects.
1746                  */
1747                 AbortCurrentTransaction();
1748
1749                 /* make sure there's no cache pollution */
1750                 ReorderBufferExecuteInvalidations(rb, txn);
1751
1752                 if (using_subtxn)
1753                         RollbackAndReleaseCurrentSubTransaction();
1754
1755                 if (snapshot_now->copied)
1756                         ReorderBufferFreeSnap(rb, snapshot_now);
1757
1758                 /* remove potential on-disk data, and deallocate */
1759                 ReorderBufferCleanupTXN(rb, txn);
1760         }
1761         PG_CATCH();
1762         {
1763                 /* TODO: Encapsulate cleanup from the PG_TRY and PG_CATCH blocks */
1764                 if (iterstate)
1765                         ReorderBufferIterTXNFinish(rb, iterstate);
1766
1767                 TeardownHistoricSnapshot(true);
1768
1769                 /*
1770                  * Force cache invalidation to happen outside of a valid transaction
1771                  * to prevent catalog access as we just caught an error.
1772                  */
1773                 AbortCurrentTransaction();
1774
1775                 /* make sure there's no cache pollution */
1776                 ReorderBufferExecuteInvalidations(rb, txn);
1777
1778                 if (using_subtxn)
1779                         RollbackAndReleaseCurrentSubTransaction();
1780
1781                 if (snapshot_now->copied)
1782                         ReorderBufferFreeSnap(rb, snapshot_now);
1783
1784                 /* remove potential on-disk data, and deallocate */
1785                 ReorderBufferCleanupTXN(rb, txn);
1786
1787                 PG_RE_THROW();
1788         }
1789         PG_END_TRY();
1790 }
1791
1792 /*
1793  * Abort a transaction that possibly has previous changes. Needs to be first
1794  * called for subtransactions and then for the toplevel xid.
1795  *
1796  * NB: Transactions handled here have to have actively aborted (i.e. have
1797  * produced an abort record). Implicitly aborted transactions are handled via
1798  * ReorderBufferAbortOld(); transactions we're just not interested in, but
1799  * which have committed are handled in ReorderBufferForget().
1800  *
1801  * This function purges this transaction and its contents from memory and
1802  * disk.
1803  */
1804 void
1805 ReorderBufferAbort(ReorderBuffer *rb, TransactionId xid, XLogRecPtr lsn)
1806 {
1807         ReorderBufferTXN *txn;
1808
1809         txn = ReorderBufferTXNByXid(rb, xid, false, NULL, InvalidXLogRecPtr,
1810                                                                 false);
1811
1812         /* unknown, nothing to remove */
1813         if (txn == NULL)
1814                 return;
1815
1816         /* cosmetic... */
1817         txn->final_lsn = lsn;
1818
1819         /* remove potential on-disk data, and deallocate */
1820         ReorderBufferCleanupTXN(rb, txn);
1821 }
1822
1823 /*
1824  * Abort all transactions that aren't actually running anymore because the
1825  * server restarted.
1826  *
1827  * NB: These really have to be transactions that have aborted due to a server
1828  * crash/immediate restart, as we don't deal with invalidations here.
1829  */
1830 void
1831 ReorderBufferAbortOld(ReorderBuffer *rb, TransactionId oldestRunningXid)
1832 {
1833         dlist_mutable_iter it;
1834
1835         /*
1836          * Iterate through all (potential) toplevel TXNs and abort all that are
1837          * older than what possibly can be running. Once we've found the first
1838          * that is alive we stop, there might be some that acquired an xid earlier
1839          * but started writing later, but it's unlikely and they will be cleaned
1840          * up in a later call to this function.
1841          */
1842         dlist_foreach_modify(it, &rb->toplevel_by_lsn)
1843         {
1844                 ReorderBufferTXN *txn;
1845
1846                 txn = dlist_container(ReorderBufferTXN, node, it.cur);
1847
1848                 if (TransactionIdPrecedes(txn->xid, oldestRunningXid))
1849                 {
1850                         /*
1851                          * We set final_lsn on a transaction when we decode its commit or
1852                          * abort record, but we never see those records for crashed
1853                          * transactions.  To ensure cleanup of these transactions, set
1854                          * final_lsn to that of their last change; this causes
1855                          * ReorderBufferRestoreCleanup to do the right thing.
1856                          */
1857                         if (txn->serialized && txn->final_lsn == 0)
1858                         {
1859                                 ReorderBufferChange *last =
1860                                 dlist_tail_element(ReorderBufferChange, node, &txn->changes);
1861
1862                                 txn->final_lsn = last->lsn;
1863                         }
1864
1865                         elog(DEBUG2, "aborting old transaction %u", txn->xid);
1866
1867                         /* remove potential on-disk data, and deallocate this tx */
1868                         ReorderBufferCleanupTXN(rb, txn);
1869                 }
1870                 else
1871                         return;
1872         }
1873 }
1874
1875 /*
1876  * Forget the contents of a transaction if we aren't interested in it's
1877  * contents. Needs to be first called for subtransactions and then for the
1878  * toplevel xid.
1879  *
1880  * This is significantly different to ReorderBufferAbort() because
1881  * transactions that have committed need to be treated differently from aborted
1882  * ones since they may have modified the catalog.
1883  *
1884  * Note that this is only allowed to be called in the moment a transaction
1885  * commit has just been read, not earlier; otherwise later records referring
1886  * to this xid might re-create the transaction incompletely.
1887  */
1888 void
1889 ReorderBufferForget(ReorderBuffer *rb, TransactionId xid, XLogRecPtr lsn)
1890 {
1891         ReorderBufferTXN *txn;
1892
1893         txn = ReorderBufferTXNByXid(rb, xid, false, NULL, InvalidXLogRecPtr,
1894                                                                 false);
1895
1896         /* unknown, nothing to forget */
1897         if (txn == NULL)
1898                 return;
1899
1900         /* cosmetic... */
1901         txn->final_lsn = lsn;
1902
1903         /*
1904          * Process cache invalidation messages if there are any. Even if we're not
1905          * interested in the transaction's contents, it could have manipulated the
1906          * catalog and we need to update the caches according to that.
1907          */
1908         if (txn->base_snapshot != NULL && txn->ninvalidations > 0)
1909                 ReorderBufferImmediateInvalidation(rb, txn->ninvalidations,
1910                                                                                    txn->invalidations);
1911         else
1912                 Assert(txn->ninvalidations == 0);
1913
1914         /* remove potential on-disk data, and deallocate */
1915         ReorderBufferCleanupTXN(rb, txn);
1916 }
1917
1918 /*
1919  * Execute invalidations happening outside the context of a decoded
1920  * transaction. That currently happens either for xid-less commits
1921  * (cf. RecordTransactionCommit()) or for invalidations in uninteresting
1922  * transactions (via ReorderBufferForget()).
1923  */
1924 void
1925 ReorderBufferImmediateInvalidation(ReorderBuffer *rb, uint32 ninvalidations,
1926                                                                    SharedInvalidationMessage *invalidations)
1927 {
1928         bool            use_subtxn = IsTransactionOrTransactionBlock();
1929         int                     i;
1930
1931         if (use_subtxn)
1932                 BeginInternalSubTransaction("replay");
1933
1934         /*
1935          * Force invalidations to happen outside of a valid transaction - that way
1936          * entries will just be marked as invalid without accessing the catalog.
1937          * That's advantageous because we don't need to setup the full state
1938          * necessary for catalog access.
1939          */
1940         if (use_subtxn)
1941                 AbortCurrentTransaction();
1942
1943         for (i = 0; i < ninvalidations; i++)
1944                 LocalExecuteInvalidationMessage(&invalidations[i]);
1945
1946         if (use_subtxn)
1947                 RollbackAndReleaseCurrentSubTransaction();
1948 }
1949
1950 /*
1951  * Tell reorderbuffer about an xid seen in the WAL stream. Has to be called at
1952  * least once for every xid in XLogRecord->xl_xid (other places in records
1953  * may, but do not have to be passed through here).
1954  *
1955  * Reorderbuffer keeps some datastructures about transactions in LSN order,
1956  * for efficiency. To do that it has to know about when transactions are seen
1957  * first in the WAL. As many types of records are not actually interesting for
1958  * logical decoding, they do not necessarily pass though here.
1959  */
1960 void
1961 ReorderBufferProcessXid(ReorderBuffer *rb, TransactionId xid, XLogRecPtr lsn)
1962 {
1963         /* many records won't have an xid assigned, centralize check here */
1964         if (xid != InvalidTransactionId)
1965                 ReorderBufferTXNByXid(rb, xid, true, NULL, lsn, true);
1966 }
1967
1968 /*
1969  * Add a new snapshot to this transaction that may only used after lsn 'lsn'
1970  * because the previous snapshot doesn't describe the catalog correctly for
1971  * following rows.
1972  */
1973 void
1974 ReorderBufferAddSnapshot(ReorderBuffer *rb, TransactionId xid,
1975                                                  XLogRecPtr lsn, Snapshot snap)
1976 {
1977         ReorderBufferChange *change = ReorderBufferGetChange(rb);
1978
1979         change->data.snapshot = snap;
1980         change->action = REORDER_BUFFER_CHANGE_INTERNAL_SNAPSHOT;
1981
1982         ReorderBufferQueueChange(rb, xid, lsn, change);
1983 }
1984
1985 /*
1986  * Set up the transaction's base snapshot.
1987  *
1988  * If we know that xid is a subtransaction, set the base snapshot on the
1989  * top-level transaction instead.
1990  */
1991 void
1992 ReorderBufferSetBaseSnapshot(ReorderBuffer *rb, TransactionId xid,
1993                                                          XLogRecPtr lsn, Snapshot snap)
1994 {
1995         ReorderBufferTXN *txn;
1996         bool            is_new;
1997
1998         AssertArg(snap != NULL);
1999
2000         /*
2001          * Fetch the transaction to operate on.  If we know it's a subtransaction,
2002          * operate on its top-level transaction instead.
2003          */
2004         txn = ReorderBufferTXNByXid(rb, xid, true, &is_new, lsn, true);
2005         if (txn->is_known_as_subxact)
2006                 txn = ReorderBufferTXNByXid(rb, txn->toplevel_xid, false,
2007                                                                         NULL, InvalidXLogRecPtr, false);
2008         Assert(txn->base_snapshot == NULL);
2009
2010         txn->base_snapshot = snap;
2011         txn->base_snapshot_lsn = lsn;
2012         dlist_push_tail(&rb->txns_by_base_snapshot_lsn, &txn->base_snapshot_node);
2013
2014         AssertTXNLsnOrder(rb);
2015 }
2016
2017 /*
2018  * Access the catalog with this CommandId at this point in the changestream.
2019  *
2020  * May only be called for command ids > 1
2021  */
2022 void
2023 ReorderBufferAddNewCommandId(ReorderBuffer *rb, TransactionId xid,
2024                                                          XLogRecPtr lsn, CommandId cid)
2025 {
2026         ReorderBufferChange *change = ReorderBufferGetChange(rb);
2027
2028         change->data.command_id = cid;
2029         change->action = REORDER_BUFFER_CHANGE_INTERNAL_COMMAND_ID;
2030
2031         ReorderBufferQueueChange(rb, xid, lsn, change);
2032 }
2033
2034
2035 /*
2036  * Add new (relfilenode, tid) -> (cmin, cmax) mappings.
2037  */
2038 void
2039 ReorderBufferAddNewTupleCids(ReorderBuffer *rb, TransactionId xid,
2040                                                          XLogRecPtr lsn, RelFileNode node,
2041                                                          ItemPointerData tid, CommandId cmin,
2042                                                          CommandId cmax, CommandId combocid)
2043 {
2044         ReorderBufferChange *change = ReorderBufferGetChange(rb);
2045         ReorderBufferTXN *txn;
2046
2047         txn = ReorderBufferTXNByXid(rb, xid, true, NULL, lsn, true);
2048
2049         change->data.tuplecid.node = node;
2050         change->data.tuplecid.tid = tid;
2051         change->data.tuplecid.cmin = cmin;
2052         change->data.tuplecid.cmax = cmax;
2053         change->data.tuplecid.combocid = combocid;
2054         change->lsn = lsn;
2055         change->action = REORDER_BUFFER_CHANGE_INTERNAL_TUPLECID;
2056
2057         dlist_push_tail(&txn->tuplecids, &change->node);
2058         txn->ntuplecids++;
2059 }
2060
2061 /*
2062  * Setup the invalidation of the toplevel transaction.
2063  *
2064  * This needs to be done before ReorderBufferCommit is called!
2065  */
2066 void
2067 ReorderBufferAddInvalidations(ReorderBuffer *rb, TransactionId xid,
2068                                                           XLogRecPtr lsn, Size nmsgs,
2069                                                           SharedInvalidationMessage *msgs)
2070 {
2071         ReorderBufferTXN *txn;
2072
2073         txn = ReorderBufferTXNByXid(rb, xid, true, NULL, lsn, true);
2074
2075         if (txn->ninvalidations != 0)
2076                 elog(ERROR, "only ever add one set of invalidations");
2077
2078         Assert(nmsgs > 0);
2079
2080         txn->ninvalidations = nmsgs;
2081         txn->invalidations = (SharedInvalidationMessage *)
2082                 MemoryContextAlloc(rb->context,
2083                                                    sizeof(SharedInvalidationMessage) * nmsgs);
2084         memcpy(txn->invalidations, msgs,
2085                    sizeof(SharedInvalidationMessage) * nmsgs);
2086 }
2087
2088 /*
2089  * Apply all invalidations we know. Possibly we only need parts at this point
2090  * in the changestream but we don't know which those are.
2091  */
2092 static void
2093 ReorderBufferExecuteInvalidations(ReorderBuffer *rb, ReorderBufferTXN *txn)
2094 {
2095         int                     i;
2096
2097         for (i = 0; i < txn->ninvalidations; i++)
2098                 LocalExecuteInvalidationMessage(&txn->invalidations[i]);
2099 }
2100
2101 /*
2102  * Mark a transaction as containing catalog changes
2103  */
2104 void
2105 ReorderBufferXidSetCatalogChanges(ReorderBuffer *rb, TransactionId xid,
2106                                                                   XLogRecPtr lsn)
2107 {
2108         ReorderBufferTXN *txn;
2109
2110         txn = ReorderBufferTXNByXid(rb, xid, true, NULL, lsn, true);
2111
2112         txn->has_catalog_changes = true;
2113 }
2114
2115 /*
2116  * Query whether a transaction is already *known* to contain catalog
2117  * changes. This can be wrong until directly before the commit!
2118  */
2119 bool
2120 ReorderBufferXidHasCatalogChanges(ReorderBuffer *rb, TransactionId xid)
2121 {
2122         ReorderBufferTXN *txn;
2123
2124         txn = ReorderBufferTXNByXid(rb, xid, false, NULL, InvalidXLogRecPtr,
2125                                                                 false);
2126         if (txn == NULL)
2127                 return false;
2128
2129         return txn->has_catalog_changes;
2130 }
2131
2132 /*
2133  * ReorderBufferXidHasBaseSnapshot
2134  *              Have we already set the base snapshot for the given txn/subtxn?
2135  */
2136 bool
2137 ReorderBufferXidHasBaseSnapshot(ReorderBuffer *rb, TransactionId xid)
2138 {
2139         ReorderBufferTXN *txn;
2140
2141         txn = ReorderBufferTXNByXid(rb, xid, false,
2142                                                                 NULL, InvalidXLogRecPtr, false);
2143
2144         /* transaction isn't known yet, ergo no snapshot */
2145         if (txn == NULL)
2146                 return false;
2147
2148         /* a known subtxn? operate on top-level txn instead */
2149         if (txn->is_known_as_subxact)
2150                 txn = ReorderBufferTXNByXid(rb, txn->toplevel_xid, false,
2151                                                                         NULL, InvalidXLogRecPtr, false);
2152
2153         return txn->base_snapshot != NULL;
2154 }
2155
2156
2157 /*
2158  * ---------------------------------------
2159  * Disk serialization support
2160  * ---------------------------------------
2161  */
2162
2163 /*
2164  * Ensure the IO buffer is >= sz.
2165  */
2166 static void
2167 ReorderBufferSerializeReserve(ReorderBuffer *rb, Size sz)
2168 {
2169         if (!rb->outbufsize)
2170         {
2171                 rb->outbuf = MemoryContextAlloc(rb->context, sz);
2172                 rb->outbufsize = sz;
2173         }
2174         else if (rb->outbufsize < sz)
2175         {
2176                 rb->outbuf = repalloc(rb->outbuf, sz);
2177                 rb->outbufsize = sz;
2178         }
2179 }
2180
2181 /*
2182  * Check whether the transaction tx should spill its data to disk.
2183  */
2184 static void
2185 ReorderBufferCheckSerializeTXN(ReorderBuffer *rb, ReorderBufferTXN *txn)
2186 {
2187         /*
2188          * TODO: improve accounting so we cheaply can take subtransactions into
2189          * account here.
2190          */
2191         if (txn->nentries_mem >= max_changes_in_memory)
2192         {
2193                 ReorderBufferSerializeTXN(rb, txn);
2194                 Assert(txn->nentries_mem == 0);
2195         }
2196 }
2197
2198 /*
2199  * Spill data of a large transaction (and its subtransactions) to disk.
2200  */
2201 static void
2202 ReorderBufferSerializeTXN(ReorderBuffer *rb, ReorderBufferTXN *txn)
2203 {
2204         dlist_iter      subtxn_i;
2205         dlist_mutable_iter change_i;
2206         int                     fd = -1;
2207         XLogSegNo       curOpenSegNo = 0;
2208         Size            spilled = 0;
2209
2210         elog(DEBUG2, "spill %u changes in XID %u to disk",
2211                  (uint32) txn->nentries_mem, txn->xid);
2212
2213         /* do the same to all child TXs */
2214         dlist_foreach(subtxn_i, &txn->subtxns)
2215         {
2216                 ReorderBufferTXN *subtxn;
2217
2218                 subtxn = dlist_container(ReorderBufferTXN, node, subtxn_i.cur);
2219                 ReorderBufferSerializeTXN(rb, subtxn);
2220         }
2221
2222         /* serialize changestream */
2223         dlist_foreach_modify(change_i, &txn->changes)
2224         {
2225                 ReorderBufferChange *change;
2226
2227                 change = dlist_container(ReorderBufferChange, node, change_i.cur);
2228
2229                 /*
2230                  * store in segment in which it belongs by start lsn, don't split over
2231                  * multiple segments tho
2232                  */
2233                 if (fd == -1 ||
2234                         !XLByteInSeg(change->lsn, curOpenSegNo, wal_segment_size))
2235                 {
2236                         char            path[MAXPGPATH];
2237
2238                         if (fd != -1)
2239                                 CloseTransientFile(fd);
2240
2241                         XLByteToSeg(change->lsn, curOpenSegNo, wal_segment_size);
2242
2243                         /*
2244                          * No need to care about TLIs here, only used during a single run,
2245                          * so each LSN only maps to a specific WAL record.
2246                          */
2247                         ReorderBufferSerializedPath(path, MyReplicationSlot, txn->xid,
2248                                                                                 curOpenSegNo);
2249
2250                         /* open segment, create it if necessary */
2251                         fd = OpenTransientFile(path,
2252                                                                    O_CREAT | O_WRONLY | O_APPEND | PG_BINARY);
2253
2254                         if (fd < 0)
2255                                 ereport(ERROR,
2256                                                 (errcode_for_file_access(),
2257                                                  errmsg("could not open file \"%s\": %m", path)));
2258                 }
2259
2260                 ReorderBufferSerializeChange(rb, txn, fd, change);
2261                 dlist_delete(&change->node);
2262                 ReorderBufferReturnChange(rb, change);
2263
2264                 spilled++;
2265         }
2266
2267         Assert(spilled == txn->nentries_mem);
2268         Assert(dlist_is_empty(&txn->changes));
2269         txn->nentries_mem = 0;
2270         txn->serialized = true;
2271
2272         if (fd != -1)
2273                 CloseTransientFile(fd);
2274 }
2275
2276 /*
2277  * Serialize individual change to disk.
2278  */
2279 static void
2280 ReorderBufferSerializeChange(ReorderBuffer *rb, ReorderBufferTXN *txn,
2281                                                          int fd, ReorderBufferChange *change)
2282 {
2283         ReorderBufferDiskChange *ondisk;
2284         Size            sz = sizeof(ReorderBufferDiskChange);
2285
2286         ReorderBufferSerializeReserve(rb, sz);
2287
2288         ondisk = (ReorderBufferDiskChange *) rb->outbuf;
2289         memcpy(&ondisk->change, change, sizeof(ReorderBufferChange));
2290
2291         switch (change->action)
2292         {
2293                         /* fall through these, they're all similar enough */
2294                 case REORDER_BUFFER_CHANGE_INSERT:
2295                 case REORDER_BUFFER_CHANGE_UPDATE:
2296                 case REORDER_BUFFER_CHANGE_DELETE:
2297                 case REORDER_BUFFER_CHANGE_INTERNAL_SPEC_INSERT:
2298                         {
2299                                 char       *data;
2300                                 ReorderBufferTupleBuf *oldtup,
2301                                                    *newtup;
2302                                 Size            oldlen = 0;
2303                                 Size            newlen = 0;
2304
2305                                 oldtup = change->data.tp.oldtuple;
2306                                 newtup = change->data.tp.newtuple;
2307
2308                                 if (oldtup)
2309                                 {
2310                                         sz += sizeof(HeapTupleData);
2311                                         oldlen = oldtup->tuple.t_len;
2312                                         sz += oldlen;
2313                                 }
2314
2315                                 if (newtup)
2316                                 {
2317                                         sz += sizeof(HeapTupleData);
2318                                         newlen = newtup->tuple.t_len;
2319                                         sz += newlen;
2320                                 }
2321
2322                                 /* make sure we have enough space */
2323                                 ReorderBufferSerializeReserve(rb, sz);
2324
2325                                 data = ((char *) rb->outbuf) + sizeof(ReorderBufferDiskChange);
2326                                 /* might have been reallocated above */
2327                                 ondisk = (ReorderBufferDiskChange *) rb->outbuf;
2328
2329                                 if (oldlen)
2330                                 {
2331                                         memcpy(data, &oldtup->tuple, sizeof(HeapTupleData));
2332                                         data += sizeof(HeapTupleData);
2333
2334                                         memcpy(data, oldtup->tuple.t_data, oldlen);
2335                                         data += oldlen;
2336                                 }
2337
2338                                 if (newlen)
2339                                 {
2340                                         memcpy(data, &newtup->tuple, sizeof(HeapTupleData));
2341                                         data += sizeof(HeapTupleData);
2342
2343                                         memcpy(data, newtup->tuple.t_data, newlen);
2344                                         data += newlen;
2345                                 }
2346                                 break;
2347                         }
2348                 case REORDER_BUFFER_CHANGE_MESSAGE:
2349                         {
2350                                 char       *data;
2351                                 Size            prefix_size = strlen(change->data.msg.prefix) + 1;
2352
2353                                 sz += prefix_size + change->data.msg.message_size +
2354                                         sizeof(Size) + sizeof(Size);
2355                                 ReorderBufferSerializeReserve(rb, sz);
2356
2357                                 data = ((char *) rb->outbuf) + sizeof(ReorderBufferDiskChange);
2358
2359                                 /* might have been reallocated above */
2360                                 ondisk = (ReorderBufferDiskChange *) rb->outbuf;
2361
2362                                 /* write the prefix including the size */
2363                                 memcpy(data, &prefix_size, sizeof(Size));
2364                                 data += sizeof(Size);
2365                                 memcpy(data, change->data.msg.prefix,
2366                                            prefix_size);
2367                                 data += prefix_size;
2368
2369                                 /* write the message including the size */
2370                                 memcpy(data, &change->data.msg.message_size, sizeof(Size));
2371                                 data += sizeof(Size);
2372                                 memcpy(data, change->data.msg.message,
2373                                            change->data.msg.message_size);
2374                                 data += change->data.msg.message_size;
2375
2376                                 break;
2377                         }
2378                 case REORDER_BUFFER_CHANGE_INTERNAL_SNAPSHOT:
2379                         {
2380                                 Snapshot        snap;
2381                                 char       *data;
2382
2383                                 snap = change->data.snapshot;
2384
2385                                 sz += sizeof(SnapshotData) +
2386                                         sizeof(TransactionId) * snap->xcnt +
2387                                         sizeof(TransactionId) * snap->subxcnt
2388                                         ;
2389
2390                                 /* make sure we have enough space */
2391                                 ReorderBufferSerializeReserve(rb, sz);
2392                                 data = ((char *) rb->outbuf) + sizeof(ReorderBufferDiskChange);
2393                                 /* might have been reallocated above */
2394                                 ondisk = (ReorderBufferDiskChange *) rb->outbuf;
2395
2396                                 memcpy(data, snap, sizeof(SnapshotData));
2397                                 data += sizeof(SnapshotData);
2398
2399                                 if (snap->xcnt)
2400                                 {
2401                                         memcpy(data, snap->xip,
2402                                                    sizeof(TransactionId) * snap->xcnt);
2403                                         data += sizeof(TransactionId) * snap->xcnt;
2404                                 }
2405
2406                                 if (snap->subxcnt)
2407                                 {
2408                                         memcpy(data, snap->subxip,
2409                                                    sizeof(TransactionId) * snap->subxcnt);
2410                                         data += sizeof(TransactionId) * snap->subxcnt;
2411                                 }
2412                                 break;
2413                         }
2414                 case REORDER_BUFFER_CHANGE_TRUNCATE:
2415                 case REORDER_BUFFER_CHANGE_INTERNAL_SPEC_CONFIRM:
2416                 case REORDER_BUFFER_CHANGE_INTERNAL_COMMAND_ID:
2417                 case REORDER_BUFFER_CHANGE_INTERNAL_TUPLECID:
2418                         /* ReorderBufferChange contains everything important */
2419                         break;
2420         }
2421
2422         ondisk->size = sz;
2423
2424         pgstat_report_wait_start(WAIT_EVENT_REORDER_BUFFER_WRITE);
2425         if (write(fd, rb->outbuf, ondisk->size) != ondisk->size)
2426         {
2427                 int                     save_errno = errno;
2428
2429                 CloseTransientFile(fd);
2430
2431                 /* if write didn't set errno, assume problem is no disk space */
2432                 errno = save_errno ? save_errno : ENOSPC;
2433                 ereport(ERROR,
2434                                 (errcode_for_file_access(),
2435                                  errmsg("could not write to data file for XID %u: %m",
2436                                                 txn->xid)));
2437         }
2438         pgstat_report_wait_end();
2439
2440         Assert(ondisk->change.action == change->action);
2441 }
2442
2443 /*
2444  * Restore a number of changes spilled to disk back into memory.
2445  */
2446 static Size
2447 ReorderBufferRestoreChanges(ReorderBuffer *rb, ReorderBufferTXN *txn,
2448                                                         int *fd, XLogSegNo *segno)
2449 {
2450         Size            restored = 0;
2451         XLogSegNo       last_segno;
2452         dlist_mutable_iter cleanup_iter;
2453
2454         Assert(txn->first_lsn != InvalidXLogRecPtr);
2455         Assert(txn->final_lsn != InvalidXLogRecPtr);
2456
2457         /* free current entries, so we have memory for more */
2458         dlist_foreach_modify(cleanup_iter, &txn->changes)
2459         {
2460                 ReorderBufferChange *cleanup =
2461                 dlist_container(ReorderBufferChange, node, cleanup_iter.cur);
2462
2463                 dlist_delete(&cleanup->node);
2464                 ReorderBufferReturnChange(rb, cleanup);
2465         }
2466         txn->nentries_mem = 0;
2467         Assert(dlist_is_empty(&txn->changes));
2468
2469         XLByteToSeg(txn->final_lsn, last_segno, wal_segment_size);
2470
2471         while (restored < max_changes_in_memory && *segno <= last_segno)
2472         {
2473                 int                     readBytes;
2474                 ReorderBufferDiskChange *ondisk;
2475
2476                 if (*fd == -1)
2477                 {
2478                         char            path[MAXPGPATH];
2479
2480                         /* first time in */
2481                         if (*segno == 0)
2482                                 XLByteToSeg(txn->first_lsn, *segno, wal_segment_size);
2483
2484                         Assert(*segno != 0 || dlist_is_empty(&txn->changes));
2485
2486                         /*
2487                          * No need to care about TLIs here, only used during a single run,
2488                          * so each LSN only maps to a specific WAL record.
2489                          */
2490                         ReorderBufferSerializedPath(path, MyReplicationSlot, txn->xid,
2491                                                                                 *segno);
2492
2493                         *fd = OpenTransientFile(path, O_RDONLY | PG_BINARY);
2494                         if (*fd < 0 && errno == ENOENT)
2495                         {
2496                                 *fd = -1;
2497                                 (*segno)++;
2498                                 continue;
2499                         }
2500                         else if (*fd < 0)
2501                                 ereport(ERROR,
2502                                                 (errcode_for_file_access(),
2503                                                  errmsg("could not open file \"%s\": %m",
2504                                                                 path)));
2505                 }
2506
2507                 /*
2508                  * Read the statically sized part of a change which has information
2509                  * about the total size. If we couldn't read a record, we're at the
2510                  * end of this file.
2511                  */
2512                 ReorderBufferSerializeReserve(rb, sizeof(ReorderBufferDiskChange));
2513                 pgstat_report_wait_start(WAIT_EVENT_REORDER_BUFFER_READ);
2514                 readBytes = read(*fd, rb->outbuf, sizeof(ReorderBufferDiskChange));
2515                 pgstat_report_wait_end();
2516
2517                 /* eof */
2518                 if (readBytes == 0)
2519                 {
2520                         CloseTransientFile(*fd);
2521                         *fd = -1;
2522                         (*segno)++;
2523                         continue;
2524                 }
2525                 else if (readBytes < 0)
2526                         ereport(ERROR,
2527                                         (errcode_for_file_access(),
2528                                          errmsg("could not read from reorderbuffer spill file: %m")));
2529                 else if (readBytes != sizeof(ReorderBufferDiskChange))
2530                         ereport(ERROR,
2531                                         (errcode_for_file_access(),
2532                                          errmsg("could not read from reorderbuffer spill file: read %d instead of %u bytes",
2533                                                         readBytes,
2534                                                         (uint32) sizeof(ReorderBufferDiskChange))));
2535
2536                 ondisk = (ReorderBufferDiskChange *) rb->outbuf;
2537
2538                 ReorderBufferSerializeReserve(rb,
2539                                                                           sizeof(ReorderBufferDiskChange) + ondisk->size);
2540                 ondisk = (ReorderBufferDiskChange *) rb->outbuf;
2541
2542                 pgstat_report_wait_start(WAIT_EVENT_REORDER_BUFFER_READ);
2543                 readBytes = read(*fd, rb->outbuf + sizeof(ReorderBufferDiskChange),
2544                                                  ondisk->size - sizeof(ReorderBufferDiskChange));
2545                 pgstat_report_wait_end();
2546
2547                 if (readBytes < 0)
2548                         ereport(ERROR,
2549                                         (errcode_for_file_access(),
2550                                          errmsg("could not read from reorderbuffer spill file: %m")));
2551                 else if (readBytes != ondisk->size - sizeof(ReorderBufferDiskChange))
2552                         ereport(ERROR,
2553                                         (errcode_for_file_access(),
2554                                          errmsg("could not read from reorderbuffer spill file: read %d instead of %u bytes",
2555                                                         readBytes,
2556                                                         (uint32) (ondisk->size - sizeof(ReorderBufferDiskChange)))));
2557
2558                 /*
2559                  * ok, read a full change from disk, now restore it into proper
2560                  * in-memory format
2561                  */
2562                 ReorderBufferRestoreChange(rb, txn, rb->outbuf);
2563                 restored++;
2564         }
2565
2566         return restored;
2567 }
2568
2569 /*
2570  * Convert change from its on-disk format to in-memory format and queue it onto
2571  * the TXN's ->changes list.
2572  *
2573  * Note: although "data" is declared char*, at entry it points to a
2574  * maxalign'd buffer, making it safe in most of this function to assume
2575  * that the pointed-to data is suitably aligned for direct access.
2576  */
2577 static void
2578 ReorderBufferRestoreChange(ReorderBuffer *rb, ReorderBufferTXN *txn,
2579                                                    char *data)
2580 {
2581         ReorderBufferDiskChange *ondisk;
2582         ReorderBufferChange *change;
2583
2584         ondisk = (ReorderBufferDiskChange *) data;
2585
2586         change = ReorderBufferGetChange(rb);
2587
2588         /* copy static part */
2589         memcpy(change, &ondisk->change, sizeof(ReorderBufferChange));
2590
2591         data += sizeof(ReorderBufferDiskChange);
2592
2593         /* restore individual stuff */
2594         switch (change->action)
2595         {
2596                         /* fall through these, they're all similar enough */
2597                 case REORDER_BUFFER_CHANGE_INSERT:
2598                 case REORDER_BUFFER_CHANGE_UPDATE:
2599                 case REORDER_BUFFER_CHANGE_DELETE:
2600                 case REORDER_BUFFER_CHANGE_INTERNAL_SPEC_INSERT:
2601                         if (change->data.tp.oldtuple)
2602                         {
2603                                 uint32          tuplelen = ((HeapTuple) data)->t_len;
2604
2605                                 change->data.tp.oldtuple =
2606                                         ReorderBufferGetTupleBuf(rb, tuplelen - SizeofHeapTupleHeader);
2607
2608                                 /* restore ->tuple */
2609                                 memcpy(&change->data.tp.oldtuple->tuple, data,
2610                                            sizeof(HeapTupleData));
2611                                 data += sizeof(HeapTupleData);
2612
2613                                 /* reset t_data pointer into the new tuplebuf */
2614                                 change->data.tp.oldtuple->tuple.t_data =
2615                                         ReorderBufferTupleBufData(change->data.tp.oldtuple);
2616
2617                                 /* restore tuple data itself */
2618                                 memcpy(change->data.tp.oldtuple->tuple.t_data, data, tuplelen);
2619                                 data += tuplelen;
2620                         }
2621
2622                         if (change->data.tp.newtuple)
2623                         {
2624                                 /* here, data might not be suitably aligned! */
2625                                 uint32          tuplelen;
2626
2627                                 memcpy(&tuplelen, data + offsetof(HeapTupleData, t_len),
2628                                            sizeof(uint32));
2629
2630                                 change->data.tp.newtuple =
2631                                         ReorderBufferGetTupleBuf(rb, tuplelen - SizeofHeapTupleHeader);
2632
2633                                 /* restore ->tuple */
2634                                 memcpy(&change->data.tp.newtuple->tuple, data,
2635                                            sizeof(HeapTupleData));
2636                                 data += sizeof(HeapTupleData);
2637
2638                                 /* reset t_data pointer into the new tuplebuf */
2639                                 change->data.tp.newtuple->tuple.t_data =
2640                                         ReorderBufferTupleBufData(change->data.tp.newtuple);
2641
2642                                 /* restore tuple data itself */
2643                                 memcpy(change->data.tp.newtuple->tuple.t_data, data, tuplelen);
2644                                 data += tuplelen;
2645                         }
2646
2647                         break;
2648                 case REORDER_BUFFER_CHANGE_MESSAGE:
2649                         {
2650                                 Size            prefix_size;
2651
2652                                 /* read prefix */
2653                                 memcpy(&prefix_size, data, sizeof(Size));
2654                                 data += sizeof(Size);
2655                                 change->data.msg.prefix = MemoryContextAlloc(rb->context,
2656                                                                                                                          prefix_size);
2657                                 memcpy(change->data.msg.prefix, data, prefix_size);
2658                                 Assert(change->data.msg.prefix[prefix_size - 1] == '\0');
2659                                 data += prefix_size;
2660
2661                                 /* read the message */
2662                                 memcpy(&change->data.msg.message_size, data, sizeof(Size));
2663                                 data += sizeof(Size);
2664                                 change->data.msg.message = MemoryContextAlloc(rb->context,
2665                                                                                                                           change->data.msg.message_size);
2666                                 memcpy(change->data.msg.message, data,
2667                                            change->data.msg.message_size);
2668                                 data += change->data.msg.message_size;
2669
2670                                 break;
2671                         }
2672                 case REORDER_BUFFER_CHANGE_INTERNAL_SNAPSHOT:
2673                         {
2674                                 Snapshot        oldsnap;
2675                                 Snapshot        newsnap;
2676                                 Size            size;
2677
2678                                 oldsnap = (Snapshot) data;
2679
2680                                 size = sizeof(SnapshotData) +
2681                                         sizeof(TransactionId) * oldsnap->xcnt +
2682                                         sizeof(TransactionId) * (oldsnap->subxcnt + 0);
2683
2684                                 change->data.snapshot = MemoryContextAllocZero(rb->context, size);
2685
2686                                 newsnap = change->data.snapshot;
2687
2688                                 memcpy(newsnap, data, size);
2689                                 newsnap->xip = (TransactionId *)
2690                                         (((char *) newsnap) + sizeof(SnapshotData));
2691                                 newsnap->subxip = newsnap->xip + newsnap->xcnt;
2692                                 newsnap->copied = true;
2693                                 break;
2694                         }
2695                         /* the base struct contains all the data, easy peasy */
2696                 case REORDER_BUFFER_CHANGE_TRUNCATE:
2697                 case REORDER_BUFFER_CHANGE_INTERNAL_SPEC_CONFIRM:
2698                 case REORDER_BUFFER_CHANGE_INTERNAL_COMMAND_ID:
2699                 case REORDER_BUFFER_CHANGE_INTERNAL_TUPLECID:
2700                         break;
2701         }
2702
2703         dlist_push_tail(&txn->changes, &change->node);
2704         txn->nentries_mem++;
2705 }
2706
2707 /*
2708  * Remove all on-disk stored for the passed in transaction.
2709  */
2710 static void
2711 ReorderBufferRestoreCleanup(ReorderBuffer *rb, ReorderBufferTXN *txn)
2712 {
2713         XLogSegNo       first;
2714         XLogSegNo       cur;
2715         XLogSegNo       last;
2716
2717         Assert(txn->first_lsn != InvalidXLogRecPtr);
2718         Assert(txn->final_lsn != InvalidXLogRecPtr);
2719
2720         XLByteToSeg(txn->first_lsn, first, wal_segment_size);
2721         XLByteToSeg(txn->final_lsn, last, wal_segment_size);
2722
2723         /* iterate over all possible filenames, and delete them */
2724         for (cur = first; cur <= last; cur++)
2725         {
2726                 char            path[MAXPGPATH];
2727
2728                 ReorderBufferSerializedPath(path, MyReplicationSlot, txn->xid, cur);
2729                 if (unlink(path) != 0 && errno != ENOENT)
2730                         ereport(ERROR,
2731                                         (errcode_for_file_access(),
2732                                          errmsg("could not remove file \"%s\": %m", path)));
2733         }
2734 }
2735
2736 /*
2737  * Remove any leftover serialized reorder buffers from a slot directory after a
2738  * prior crash or decoding session exit.
2739  */
2740 static void
2741 ReorderBufferCleanupSerializedTXNs(const char *slotname)
2742 {
2743         DIR                *spill_dir;
2744         struct dirent *spill_de;
2745         struct stat statbuf;
2746         char            path[MAXPGPATH * 2 + 12];
2747
2748         sprintf(path, "pg_replslot/%s", slotname);
2749
2750         /* we're only handling directories here, skip if it's not ours */
2751         if (lstat(path, &statbuf) == 0 && !S_ISDIR(statbuf.st_mode))
2752                 return;
2753
2754         spill_dir = AllocateDir(path);
2755         while ((spill_de = ReadDirExtended(spill_dir, path, INFO)) != NULL)
2756         {
2757                 /* only look at names that can be ours */
2758                 if (strncmp(spill_de->d_name, "xid", 3) == 0)
2759                 {
2760                         snprintf(path, sizeof(path),
2761                                          "pg_replslot/%s/%s", slotname,
2762                                          spill_de->d_name);
2763
2764                         if (unlink(path) != 0)
2765                                 ereport(ERROR,
2766                                                 (errcode_for_file_access(),
2767                                                  errmsg("could not remove file \"%s\" during removal of pg_replslot/%s/*.xid: %m",
2768                                                                 path, slotname)));
2769                 }
2770         }
2771         FreeDir(spill_dir);
2772 }
2773
2774 /*
2775  * Given a replication slot, transaction ID and segment number, fill in the
2776  * corresponding spill file into 'path', which is a caller-owned buffer of size
2777  * at least MAXPGPATH.
2778  */
2779 static void
2780 ReorderBufferSerializedPath(char *path, ReplicationSlot *slot, TransactionId xid,
2781                                                         XLogSegNo segno)
2782 {
2783         XLogRecPtr      recptr;
2784
2785         XLogSegNoOffsetToRecPtr(segno, 0, recptr, wal_segment_size);
2786
2787         snprintf(path, MAXPGPATH, "pg_replslot/%s/xid-%u-lsn-%X-%X.snap",
2788                          NameStr(MyReplicationSlot->data.name),
2789                          xid,
2790                          (uint32) (recptr >> 32), (uint32) recptr);
2791 }
2792
2793 /*
2794  * Delete all data spilled to disk after we've restarted/crashed. It will be
2795  * recreated when the respective slots are reused.
2796  */
2797 void
2798 StartupReorderBuffer(void)
2799 {
2800         DIR                *logical_dir;
2801         struct dirent *logical_de;
2802
2803         logical_dir = AllocateDir("pg_replslot");
2804         while ((logical_de = ReadDir(logical_dir, "pg_replslot")) != NULL)
2805         {
2806                 if (strcmp(logical_de->d_name, ".") == 0 ||
2807                         strcmp(logical_de->d_name, "..") == 0)
2808                         continue;
2809
2810                 /* if it cannot be a slot, skip the directory */
2811                 if (!ReplicationSlotValidateName(logical_de->d_name, DEBUG2))
2812                         continue;
2813
2814                 /*
2815                  * ok, has to be a surviving logical slot, iterate and delete
2816                  * everything starting with xid-*
2817                  */
2818                 ReorderBufferCleanupSerializedTXNs(logical_de->d_name);
2819         }
2820         FreeDir(logical_dir);
2821 }
2822
2823 /* ---------------------------------------
2824  * toast reassembly support
2825  * ---------------------------------------
2826  */
2827
2828 /*
2829  * Initialize per tuple toast reconstruction support.
2830  */
2831 static void
2832 ReorderBufferToastInitHash(ReorderBuffer *rb, ReorderBufferTXN *txn)
2833 {
2834         HASHCTL         hash_ctl;
2835
2836         Assert(txn->toast_hash == NULL);
2837
2838         memset(&hash_ctl, 0, sizeof(hash_ctl));
2839         hash_ctl.keysize = sizeof(Oid);
2840         hash_ctl.entrysize = sizeof(ReorderBufferToastEnt);
2841         hash_ctl.hcxt = rb->context;
2842         txn->toast_hash = hash_create("ReorderBufferToastHash", 5, &hash_ctl,
2843                                                                   HASH_ELEM | HASH_BLOBS | HASH_CONTEXT);
2844 }
2845
2846 /*
2847  * Per toast-chunk handling for toast reconstruction
2848  *
2849  * Appends a toast chunk so we can reconstruct it when the tuple "owning" the
2850  * toasted Datum comes along.
2851  */
2852 static void
2853 ReorderBufferToastAppendChunk(ReorderBuffer *rb, ReorderBufferTXN *txn,
2854                                                           Relation relation, ReorderBufferChange *change)
2855 {
2856         ReorderBufferToastEnt *ent;
2857         ReorderBufferTupleBuf *newtup;
2858         bool            found;
2859         int32           chunksize;
2860         bool            isnull;
2861         Pointer         chunk;
2862         TupleDesc       desc = RelationGetDescr(relation);
2863         Oid                     chunk_id;
2864         int32           chunk_seq;
2865
2866         if (txn->toast_hash == NULL)
2867                 ReorderBufferToastInitHash(rb, txn);
2868
2869         Assert(IsToastRelation(relation));
2870
2871         newtup = change->data.tp.newtuple;
2872         chunk_id = DatumGetObjectId(fastgetattr(&newtup->tuple, 1, desc, &isnull));
2873         Assert(!isnull);
2874         chunk_seq = DatumGetInt32(fastgetattr(&newtup->tuple, 2, desc, &isnull));
2875         Assert(!isnull);
2876
2877         ent = (ReorderBufferToastEnt *)
2878                 hash_search(txn->toast_hash,
2879                                         (void *) &chunk_id,
2880                                         HASH_ENTER,
2881                                         &found);
2882
2883         if (!found)
2884         {
2885                 Assert(ent->chunk_id == chunk_id);
2886                 ent->num_chunks = 0;
2887                 ent->last_chunk_seq = 0;
2888                 ent->size = 0;
2889                 ent->reconstructed = NULL;
2890                 dlist_init(&ent->chunks);
2891
2892                 if (chunk_seq != 0)
2893                         elog(ERROR, "got sequence entry %d for toast chunk %u instead of seq 0",
2894                                  chunk_seq, chunk_id);
2895         }
2896         else if (found && chunk_seq != ent->last_chunk_seq + 1)
2897                 elog(ERROR, "got sequence entry %d for toast chunk %u instead of seq %d",
2898                          chunk_seq, chunk_id, ent->last_chunk_seq + 1);
2899
2900         chunk = DatumGetPointer(fastgetattr(&newtup->tuple, 3, desc, &isnull));
2901         Assert(!isnull);
2902
2903         /* calculate size so we can allocate the right size at once later */
2904         if (!VARATT_IS_EXTENDED(chunk))
2905                 chunksize = VARSIZE(chunk) - VARHDRSZ;
2906         else if (VARATT_IS_SHORT(chunk))
2907                 /* could happen due to heap_form_tuple doing its thing */
2908                 chunksize = VARSIZE_SHORT(chunk) - VARHDRSZ_SHORT;
2909         else
2910                 elog(ERROR, "unexpected type of toast chunk");
2911
2912         ent->size += chunksize;
2913         ent->last_chunk_seq = chunk_seq;
2914         ent->num_chunks++;
2915         dlist_push_tail(&ent->chunks, &change->node);
2916 }
2917
2918 /*
2919  * Rejigger change->newtuple to point to in-memory toast tuples instead to
2920  * on-disk toast tuples that may not longer exist (think DROP TABLE or VACUUM).
2921  *
2922  * We cannot replace unchanged toast tuples though, so those will still point
2923  * to on-disk toast data.
2924  */
2925 static void
2926 ReorderBufferToastReplace(ReorderBuffer *rb, ReorderBufferTXN *txn,
2927                                                   Relation relation, ReorderBufferChange *change)
2928 {
2929         TupleDesc       desc;
2930         int                     natt;
2931         Datum      *attrs;
2932         bool       *isnull;
2933         bool       *free;
2934         HeapTuple       tmphtup;
2935         Relation        toast_rel;
2936         TupleDesc       toast_desc;
2937         MemoryContext oldcontext;
2938         ReorderBufferTupleBuf *newtup;
2939
2940         /* no toast tuples changed */
2941         if (txn->toast_hash == NULL)
2942                 return;
2943
2944         oldcontext = MemoryContextSwitchTo(rb->context);
2945
2946         /* we should only have toast tuples in an INSERT or UPDATE */
2947         Assert(change->data.tp.newtuple);
2948
2949         desc = RelationGetDescr(relation);
2950
2951         toast_rel = RelationIdGetRelation(relation->rd_rel->reltoastrelid);
2952         toast_desc = RelationGetDescr(toast_rel);
2953
2954         /* should we allocate from stack instead? */
2955         attrs = palloc0(sizeof(Datum) * desc->natts);
2956         isnull = palloc0(sizeof(bool) * desc->natts);
2957         free = palloc0(sizeof(bool) * desc->natts);
2958
2959         newtup = change->data.tp.newtuple;
2960
2961         heap_deform_tuple(&newtup->tuple, desc, attrs, isnull);
2962
2963         for (natt = 0; natt < desc->natts; natt++)
2964         {
2965                 Form_pg_attribute attr = TupleDescAttr(desc, natt);
2966                 ReorderBufferToastEnt *ent;
2967                 struct varlena *varlena;
2968
2969                 /* va_rawsize is the size of the original datum -- including header */
2970                 struct varatt_external toast_pointer;
2971                 struct varatt_indirect redirect_pointer;
2972                 struct varlena *new_datum = NULL;
2973                 struct varlena *reconstructed;
2974                 dlist_iter      it;
2975                 Size            data_done = 0;
2976
2977                 /* system columns aren't toasted */
2978                 if (attr->attnum < 0)
2979                         continue;
2980
2981                 if (attr->attisdropped)
2982                         continue;
2983
2984                 /* not a varlena datatype */
2985                 if (attr->attlen != -1)
2986                         continue;
2987
2988                 /* no data */
2989                 if (isnull[natt])
2990                         continue;
2991
2992                 /* ok, we know we have a toast datum */
2993                 varlena = (struct varlena *) DatumGetPointer(attrs[natt]);
2994
2995                 /* no need to do anything if the tuple isn't external */
2996                 if (!VARATT_IS_EXTERNAL(varlena))
2997                         continue;
2998
2999                 VARATT_EXTERNAL_GET_POINTER(toast_pointer, varlena);
3000
3001                 /*
3002                  * Check whether the toast tuple changed, replace if so.
3003                  */
3004                 ent = (ReorderBufferToastEnt *)
3005                         hash_search(txn->toast_hash,
3006                                                 (void *) &toast_pointer.va_valueid,
3007                                                 HASH_FIND,
3008                                                 NULL);
3009                 if (ent == NULL)
3010                         continue;
3011
3012                 new_datum =
3013                         (struct varlena *) palloc0(INDIRECT_POINTER_SIZE);
3014
3015                 free[natt] = true;
3016
3017                 reconstructed = palloc0(toast_pointer.va_rawsize);
3018
3019                 ent->reconstructed = reconstructed;
3020
3021                 /* stitch toast tuple back together from its parts */
3022                 dlist_foreach(it, &ent->chunks)
3023                 {
3024                         bool            isnull;
3025                         ReorderBufferChange *cchange;
3026                         ReorderBufferTupleBuf *ctup;
3027                         Pointer         chunk;
3028
3029                         cchange = dlist_container(ReorderBufferChange, node, it.cur);
3030                         ctup = cchange->data.tp.newtuple;
3031                         chunk = DatumGetPointer(
3032                                                                         fastgetattr(&ctup->tuple, 3, toast_desc, &isnull));
3033
3034                         Assert(!isnull);
3035                         Assert(!VARATT_IS_EXTERNAL(chunk));
3036                         Assert(!VARATT_IS_SHORT(chunk));
3037
3038                         memcpy(VARDATA(reconstructed) + data_done,
3039                                    VARDATA(chunk),
3040                                    VARSIZE(chunk) - VARHDRSZ);
3041                         data_done += VARSIZE(chunk) - VARHDRSZ;
3042                 }
3043                 Assert(data_done == toast_pointer.va_extsize);
3044
3045                 /* make sure its marked as compressed or not */
3046                 if (VARATT_EXTERNAL_IS_COMPRESSED(toast_pointer))
3047                         SET_VARSIZE_COMPRESSED(reconstructed, data_done + VARHDRSZ);
3048                 else
3049                         SET_VARSIZE(reconstructed, data_done + VARHDRSZ);
3050
3051                 memset(&redirect_pointer, 0, sizeof(redirect_pointer));
3052                 redirect_pointer.pointer = reconstructed;
3053
3054                 SET_VARTAG_EXTERNAL(new_datum, VARTAG_INDIRECT);
3055                 memcpy(VARDATA_EXTERNAL(new_datum), &redirect_pointer,
3056                            sizeof(redirect_pointer));
3057
3058                 attrs[natt] = PointerGetDatum(new_datum);
3059         }
3060
3061         /*
3062          * Build tuple in separate memory & copy tuple back into the tuplebuf
3063          * passed to the output plugin. We can't directly heap_fill_tuple() into
3064          * the tuplebuf because attrs[] will point back into the current content.
3065          */
3066         tmphtup = heap_form_tuple(desc, attrs, isnull);
3067         Assert(newtup->tuple.t_len <= MaxHeapTupleSize);
3068         Assert(ReorderBufferTupleBufData(newtup) == newtup->tuple.t_data);
3069
3070         memcpy(newtup->tuple.t_data, tmphtup->t_data, tmphtup->t_len);
3071         newtup->tuple.t_len = tmphtup->t_len;
3072
3073         /*
3074          * free resources we won't further need, more persistent stuff will be
3075          * free'd in ReorderBufferToastReset().
3076          */
3077         RelationClose(toast_rel);
3078         pfree(tmphtup);
3079         for (natt = 0; natt < desc->natts; natt++)
3080         {
3081                 if (free[natt])
3082                         pfree(DatumGetPointer(attrs[natt]));
3083         }
3084         pfree(attrs);
3085         pfree(free);
3086         pfree(isnull);
3087
3088         MemoryContextSwitchTo(oldcontext);
3089 }
3090
3091 /*
3092  * Free all resources allocated for toast reconstruction.
3093  */
3094 static void
3095 ReorderBufferToastReset(ReorderBuffer *rb, ReorderBufferTXN *txn)
3096 {
3097         HASH_SEQ_STATUS hstat;
3098         ReorderBufferToastEnt *ent;
3099
3100         if (txn->toast_hash == NULL)
3101                 return;
3102
3103         /* sequentially walk over the hash and free everything */
3104         hash_seq_init(&hstat, txn->toast_hash);
3105         while ((ent = (ReorderBufferToastEnt *) hash_seq_search(&hstat)) != NULL)
3106         {
3107                 dlist_mutable_iter it;
3108
3109                 if (ent->reconstructed != NULL)
3110                         pfree(ent->reconstructed);
3111
3112                 dlist_foreach_modify(it, &ent->chunks)
3113                 {
3114                         ReorderBufferChange *change =
3115                         dlist_container(ReorderBufferChange, node, it.cur);
3116
3117                         dlist_delete(&change->node);
3118                         ReorderBufferReturnChange(rb, change);
3119                 }
3120         }
3121
3122         hash_destroy(txn->toast_hash);
3123         txn->toast_hash = NULL;
3124 }
3125
3126
3127 /* ---------------------------------------
3128  * Visibility support for logical decoding
3129  *
3130  *
3131  * Lookup actual cmin/cmax values when using decoding snapshot. We can't
3132  * always rely on stored cmin/cmax values because of two scenarios:
3133  *
3134  * * A tuple got changed multiple times during a single transaction and thus
3135  *       has got a combocid. Combocid's are only valid for the duration of a
3136  *       single transaction.
3137  * * A tuple with a cmin but no cmax (and thus no combocid) got
3138  *       deleted/updated in another transaction than the one which created it
3139  *       which we are looking at right now. As only one of cmin, cmax or combocid
3140  *       is actually stored in the heap we don't have access to the value we
3141  *       need anymore.
3142  *
3143  * To resolve those problems we have a per-transaction hash of (cmin,
3144  * cmax) tuples keyed by (relfilenode, ctid) which contains the actual
3145  * (cmin, cmax) values. That also takes care of combocids by simply
3146  * not caring about them at all. As we have the real cmin/cmax values
3147  * combocids aren't interesting.
3148  *
3149  * As we only care about catalog tuples here the overhead of this
3150  * hashtable should be acceptable.
3151  *
3152  * Heap rewrites complicate this a bit, check rewriteheap.c for
3153  * details.
3154  * -------------------------------------------------------------------------
3155  */
3156
3157 /* struct for qsort()ing mapping files by lsn somewhat efficiently */
3158 typedef struct RewriteMappingFile
3159 {
3160         XLogRecPtr      lsn;
3161         char            fname[MAXPGPATH];
3162 } RewriteMappingFile;
3163
3164 #if NOT_USED
3165 static void
3166 DisplayMapping(HTAB *tuplecid_data)
3167 {
3168         HASH_SEQ_STATUS hstat;
3169         ReorderBufferTupleCidEnt *ent;
3170
3171         hash_seq_init(&hstat, tuplecid_data);
3172         while ((ent = (ReorderBufferTupleCidEnt *) hash_seq_search(&hstat)) != NULL)
3173         {
3174                 elog(DEBUG3, "mapping: node: %u/%u/%u tid: %u/%u cmin: %u, cmax: %u",
3175                          ent->key.relnode.dbNode,
3176                          ent->key.relnode.spcNode,
3177                          ent->key.relnode.relNode,
3178                          ItemPointerGetBlockNumber(&ent->key.tid),
3179                          ItemPointerGetOffsetNumber(&ent->key.tid),
3180                          ent->cmin,
3181                          ent->cmax
3182                         );
3183         }
3184 }
3185 #endif
3186
3187 /*
3188  * Apply a single mapping file to tuplecid_data.
3189  *
3190  * The mapping file has to have been verified to be a) committed b) for our
3191  * transaction c) applied in LSN order.
3192  */
3193 static void
3194 ApplyLogicalMappingFile(HTAB *tuplecid_data, Oid relid, const char *fname)
3195 {
3196         char            path[MAXPGPATH];
3197         int                     fd;
3198         int                     readBytes;
3199         LogicalRewriteMappingData map;
3200
3201         sprintf(path, "pg_logical/mappings/%s", fname);
3202         fd = OpenTransientFile(path, O_RDONLY | PG_BINARY);
3203         if (fd < 0)
3204                 ereport(ERROR,
3205                                 (errcode_for_file_access(),
3206                                  errmsg("could not open file \"%s\": %m", path)));
3207
3208         while (true)
3209         {
3210                 ReorderBufferTupleCidKey key;
3211                 ReorderBufferTupleCidEnt *ent;
3212                 ReorderBufferTupleCidEnt *new_ent;
3213                 bool            found;
3214
3215                 /* be careful about padding */
3216                 memset(&key, 0, sizeof(ReorderBufferTupleCidKey));
3217
3218                 /* read all mappings till the end of the file */
3219                 pgstat_report_wait_start(WAIT_EVENT_REORDER_LOGICAL_MAPPING_READ);
3220                 readBytes = read(fd, &map, sizeof(LogicalRewriteMappingData));
3221                 pgstat_report_wait_end();
3222
3223                 if (readBytes < 0)
3224                         ereport(ERROR,
3225                                         (errcode_for_file_access(),
3226                                          errmsg("could not read file \"%s\": %m",
3227                                                         path)));
3228                 else if (readBytes == 0)        /* EOF */
3229                         break;
3230                 else if (readBytes != sizeof(LogicalRewriteMappingData))
3231                         ereport(ERROR,
3232                                         (errcode_for_file_access(),
3233                                          errmsg("could not read from file \"%s\": read %d instead of %d bytes",
3234                                                         path, readBytes,
3235                                                         (int32) sizeof(LogicalRewriteMappingData))));
3236
3237                 key.relnode = map.old_node;
3238                 ItemPointerCopy(&map.old_tid,
3239                                                 &key.tid);
3240
3241
3242                 ent = (ReorderBufferTupleCidEnt *)
3243                         hash_search(tuplecid_data,
3244                                                 (void *) &key,
3245                                                 HASH_FIND,
3246                                                 NULL);
3247
3248                 /* no existing mapping, no need to update */
3249                 if (!ent)
3250                         continue;
3251
3252                 key.relnode = map.new_node;
3253                 ItemPointerCopy(&map.new_tid,
3254                                                 &key.tid);
3255
3256                 new_ent = (ReorderBufferTupleCidEnt *)
3257                         hash_search(tuplecid_data,
3258                                                 (void *) &key,
3259                                                 HASH_ENTER,
3260                                                 &found);
3261
3262                 if (found)
3263                 {
3264                         /*
3265                          * Make sure the existing mapping makes sense. We sometime update
3266                          * old records that did not yet have a cmax (e.g. pg_class' own
3267                          * entry while rewriting it) during rewrites, so allow that.
3268                          */
3269                         Assert(ent->cmin == InvalidCommandId || ent->cmin == new_ent->cmin);
3270                         Assert(ent->cmax == InvalidCommandId || ent->cmax == new_ent->cmax);
3271                 }
3272                 else
3273                 {
3274                         /* update mapping */
3275                         new_ent->cmin = ent->cmin;
3276                         new_ent->cmax = ent->cmax;
3277                         new_ent->combocid = ent->combocid;
3278                 }
3279         }
3280 }
3281
3282
3283 /*
3284  * Check whether the TransactionOId 'xid' is in the pre-sorted array 'xip'.
3285  */
3286 static bool
3287 TransactionIdInArray(TransactionId xid, TransactionId *xip, Size num)
3288 {
3289         return bsearch(&xid, xip, num,
3290                                    sizeof(TransactionId), xidComparator) != NULL;
3291 }
3292
3293 /*
3294  * qsort() comparator for sorting RewriteMappingFiles in LSN order.
3295  */
3296 static int
3297 file_sort_by_lsn(const void *a_p, const void *b_p)
3298 {
3299         RewriteMappingFile *a = *(RewriteMappingFile **) a_p;
3300         RewriteMappingFile *b = *(RewriteMappingFile **) b_p;
3301
3302         if (a->lsn < b->lsn)
3303                 return -1;
3304         else if (a->lsn > b->lsn)
3305                 return 1;
3306         return 0;
3307 }
3308
3309 /*
3310  * Apply any existing logical remapping files if there are any targeted at our
3311  * transaction for relid.
3312  */
3313 static void
3314 UpdateLogicalMappings(HTAB *tuplecid_data, Oid relid, Snapshot snapshot)
3315 {
3316         DIR                *mapping_dir;
3317         struct dirent *mapping_de;
3318         List       *files = NIL;
3319         ListCell   *file;
3320         RewriteMappingFile **files_a;
3321         size_t          off;
3322         Oid                     dboid = IsSharedRelation(relid) ? InvalidOid : MyDatabaseId;
3323
3324         mapping_dir = AllocateDir("pg_logical/mappings");
3325         while ((mapping_de = ReadDir(mapping_dir, "pg_logical/mappings")) != NULL)
3326         {
3327                 Oid                     f_dboid;
3328                 Oid                     f_relid;
3329                 TransactionId f_mapped_xid;
3330                 TransactionId f_create_xid;
3331                 XLogRecPtr      f_lsn;
3332                 uint32          f_hi,
3333                                         f_lo;
3334                 RewriteMappingFile *f;
3335
3336                 if (strcmp(mapping_de->d_name, ".") == 0 ||
3337                         strcmp(mapping_de->d_name, "..") == 0)
3338                         continue;
3339
3340                 /* Ignore files that aren't ours */
3341                 if (strncmp(mapping_de->d_name, "map-", 4) != 0)
3342                         continue;
3343
3344                 if (sscanf(mapping_de->d_name, LOGICAL_REWRITE_FORMAT,
3345                                    &f_dboid, &f_relid, &f_hi, &f_lo,
3346                                    &f_mapped_xid, &f_create_xid) != 6)
3347                         elog(ERROR, "could not parse filename \"%s\"", mapping_de->d_name);
3348
3349                 f_lsn = ((uint64) f_hi) << 32 | f_lo;
3350
3351                 /* mapping for another database */
3352                 if (f_dboid != dboid)
3353                         continue;
3354
3355                 /* mapping for another relation */
3356                 if (f_relid != relid)
3357                         continue;
3358
3359                 /* did the creating transaction abort? */
3360                 if (!TransactionIdDidCommit(f_create_xid))
3361                         continue;
3362
3363                 /* not for our transaction */
3364                 if (!TransactionIdInArray(f_mapped_xid, snapshot->subxip, snapshot->subxcnt))
3365                         continue;
3366
3367                 /* ok, relevant, queue for apply */
3368                 f = palloc(sizeof(RewriteMappingFile));
3369                 f->lsn = f_lsn;
3370                 strcpy(f->fname, mapping_de->d_name);
3371                 files = lappend(files, f);
3372         }
3373         FreeDir(mapping_dir);
3374
3375         /* build array we can easily sort */
3376         files_a = palloc(list_length(files) * sizeof(RewriteMappingFile *));
3377         off = 0;
3378         foreach(file, files)
3379         {
3380                 files_a[off++] = lfirst(file);
3381         }
3382
3383         /* sort files so we apply them in LSN order */
3384         qsort(files_a, list_length(files), sizeof(RewriteMappingFile *),
3385                   file_sort_by_lsn);
3386
3387         for (off = 0; off < list_length(files); off++)
3388         {
3389                 RewriteMappingFile *f = files_a[off];
3390
3391                 elog(DEBUG1, "applying mapping: \"%s\" in %u", f->fname,
3392                          snapshot->subxip[0]);
3393                 ApplyLogicalMappingFile(tuplecid_data, relid, f->fname);
3394                 pfree(f);
3395         }
3396 }
3397
3398 /*
3399  * Lookup cmin/cmax of a tuple, during logical decoding where we can't rely on
3400  * combocids.
3401  */
3402 bool
3403 ResolveCminCmaxDuringDecoding(HTAB *tuplecid_data,
3404                                                           Snapshot snapshot,
3405                                                           HeapTuple htup, Buffer buffer,
3406                                                           CommandId *cmin, CommandId *cmax)
3407 {
3408         ReorderBufferTupleCidKey key;
3409         ReorderBufferTupleCidEnt *ent;
3410         ForkNumber      forkno;
3411         BlockNumber blockno;
3412         bool            updated_mapping = false;
3413
3414         /* be careful about padding */
3415         memset(&key, 0, sizeof(key));
3416
3417         Assert(!BufferIsLocal(buffer));
3418
3419         /*
3420          * get relfilenode from the buffer, no convenient way to access it other
3421          * than that.
3422          */
3423         BufferGetTag(buffer, &key.relnode, &forkno, &blockno);
3424
3425         /* tuples can only be in the main fork */
3426         Assert(forkno == MAIN_FORKNUM);
3427         Assert(blockno == ItemPointerGetBlockNumber(&htup->t_self));
3428
3429         ItemPointerCopy(&htup->t_self,
3430                                         &key.tid);
3431
3432 restart:
3433         ent = (ReorderBufferTupleCidEnt *)
3434                 hash_search(tuplecid_data,
3435                                         (void *) &key,
3436                                         HASH_FIND,
3437                                         NULL);
3438
3439         /*
3440          * failed to find a mapping, check whether the table was rewritten and
3441          * apply mapping if so, but only do that once - there can be no new
3442          * mappings while we are in here since we have to hold a lock on the
3443          * relation.
3444          */
3445         if (ent == NULL && !updated_mapping)
3446         {
3447                 UpdateLogicalMappings(tuplecid_data, htup->t_tableOid, snapshot);
3448                 /* now check but don't update for a mapping again */
3449                 updated_mapping = true;
3450                 goto restart;
3451         }
3452         else if (ent == NULL)
3453                 return false;
3454
3455         if (cmin)
3456                 *cmin = ent->cmin;
3457         if (cmax)
3458                 *cmax = ent->cmax;
3459         return true;
3460 }