]> granicus.if.org Git - postgresql/blobdiff - src/backend/access/heap/rewriteheap.c
Modify BufferGetPage() to prepare for "snapshot too old" feature
[postgresql] / src / backend / access / heap / rewriteheap.c
index a9088ac9288feccd8970f56c375eaed6535aae4e..f9ce9861e2cacb520c48d39892e3a176144ad7da 100644 (file)
@@ -10,7 +10,7 @@
  *
  * The caller is responsible for creating the new heap, all catalog
  * changes, supplying the tuples to be written to the new heap, and
- * rebuilding indexes. The caller must hold AccessExclusiveLock on the
+ * rebuilding indexes.  The caller must hold AccessExclusiveLock on the
  * target table, because we assume no one else is writing into it.
  *
  * To use the facility:
@@ -43,7 +43,7 @@
  * to substitute the correct ctid instead.
  *
  * For each ctid reference from A -> B, we might encounter either A first
- * or B first. (Note that a tuple in the middle of a chain is both A and B
+ * or B first.  (Note that a tuple in the middle of a chain is both A and B
  * of different pairs.)
  *
  * If we encounter A first, we'll store the tuple in the unresolved_tups
  * and can write A immediately with the correct ctid.
  *
  * Entries in the hash tables can be removed as soon as the later tuple
- * is encountered.     That helps to keep the memory usage down.  At the end,
+ * is encountered.  That helps to keep the memory usage down.  At the end,
  * both tables are usually empty; we should have encountered both A and B
  * of each pair.  However, it's possible for A to be RECENTLY_DEAD and B
  * entirely DEAD according to HeapTupleSatisfiesVacuum, because the test
- * for deadness using OldestXmin is not exact. In such a case we might
+ * for deadness using OldestXmin is not exact.  In such a case we might
  * encounter B first, and skip it, and find A later.  Then A would be added
  * to unresolved_tups, and stay there until end of the rewrite.  Since
  * this case is very unusual, we don't worry about the memory usage.
@@ -78,7 +78,7 @@
  * of CLUSTERing on an unchanging key column, we'll see all the versions
  * of a given tuple together anyway, and so the peak memory usage is only
  * proportional to the number of RECENTLY_DEAD versions of a single row, not
- * in the whole table. Note that if we do fail halfway through a CLUSTER,
+ * in the whole table.  Note that if we do fail halfway through a CLUSTER,
  * the old table is still valid, so failure is not catastrophic.
  *
  * We can't use the normal heap_insert function to insert into the new
  * heap's TOAST table will go through the normal bufmgr.
  *
  *
- * Portions Copyright (c) 1996-2007, PostgreSQL Global Development Group
+ * Portions Copyright (c) 1996-2016, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994-5, Regents of the University of California
  *
  * IDENTIFICATION
- *       $PostgreSQL: pgsql/src/backend/access/heap/rewriteheap.c,v 1.9 2007/11/15 22:25:15 momjian Exp $
+ *       src/backend/access/heap/rewriteheap.c
  *
  *-------------------------------------------------------------------------
  */
 #include "postgres.h"
 
+#include <sys/stat.h>
+#include <unistd.h>
+
+#include "miscadmin.h"
+
 #include "access/heapam.h"
+#include "access/heapam_xlog.h"
 #include "access/rewriteheap.h"
 #include "access/transam.h"
 #include "access/tuptoaster.h"
+#include "access/xact.h"
+#include "access/xloginsert.h"
+
+#include "catalog/catalog.h"
+
+#include "lib/ilist.h"
+
+#include "replication/logical.h"
+#include "replication/slot.h"
+
+#include "storage/bufmgr.h"
+#include "storage/fd.h"
 #include "storage/smgr.h"
+
 #include "utils/memutils.h"
+#include "utils/rel.h"
+#include "utils/tqual.h"
 
+#include "storage/procarray.h"
 
 /*
  * State associated with a rewrite operation. This is opaque to the user
  */
 typedef struct RewriteStateData
 {
+       Relation        rs_old_rel;             /* source heap */
        Relation        rs_new_rel;             /* destination heap */
        Page            rs_buffer;              /* page currently being built */
        BlockNumber rs_blockno;         /* block where page will go */
        bool            rs_buffer_valid;        /* T if any tuples in buffer */
        bool            rs_use_wal;             /* must we WAL-log inserts? */
+       bool            rs_logical_rewrite;             /* do we need to do logical rewriting */
        TransactionId rs_oldest_xmin;           /* oldest xmin used by caller to
                                                                                 * determine tuple visibility */
        TransactionId rs_freeze_xid;/* Xid that will be used as freeze cutoff
                                                                 * point */
+       TransactionId rs_logical_xmin;          /* Xid that will be used as cutoff
+                                                                                * point for logical rewrites */
+       MultiXactId rs_cutoff_multi;/* MultiXactId that will be used as cutoff
+                                                                * point for multixacts */
        MemoryContext rs_cxt;           /* for hash tables and entries and tuples in
                                                                 * them */
+       XLogRecPtr      rs_begin_lsn;   /* XLogInsertLsn when starting the rewrite */
        HTAB       *rs_unresolved_tups;         /* unmatched A tuples */
        HTAB       *rs_old_new_tid_map;         /* unmatched B tuples */
-} RewriteStateData;
+       HTAB       *rs_logical_mappings;        /* logical remapping files */
+       uint32          rs_num_rewrite_mappings;                /* # in memory mappings */
+}      RewriteStateData;
 
 /*
  * The lookup keys for the hash tables are tuple TID and xmin (we must check
@@ -163,25 +194,58 @@ typedef struct
 
 typedef OldToNewMappingData *OldToNewMapping;
 
+/*
+ * In-Memory data for an xid that might need logical remapping entries
+ * to be logged.
+ */
+typedef struct RewriteMappingFile
+{
+       TransactionId xid;                      /* xid that might need to see the row */
+       int                     vfd;                    /* fd of mappings file */
+       off_t           off;                    /* how far have we written yet */
+       uint32          num_mappings;   /* number of in-memory mappings */
+       dlist_head      mappings;               /* list of in-memory mappings */
+       char            path[MAXPGPATH];        /* path, for error messages */
+} RewriteMappingFile;
+
+/*
+ * A single In-Memeory logical rewrite mapping, hanging of
+ * RewriteMappingFile->mappings.
+ */
+typedef struct RewriteMappingDataEntry
+{
+       LogicalRewriteMappingData map;          /* map between old and new location of
+                                                                                * the tuple */
+       dlist_node      node;
+} RewriteMappingDataEntry;
+
 
 /* prototypes for internal functions */
 static void raw_heap_insert(RewriteState state, HeapTuple tup);
 
+/* internal logical remapping prototypes */
+static void logical_begin_heap_rewrite(RewriteState state);
+static void logical_rewrite_heap_tuple(RewriteState state, ItemPointerData old_tid, HeapTuple new_tuple);
+static void logical_end_heap_rewrite(RewriteState state);
+
 
 /*
  * Begin a rewrite of a table
  *
+ * old_heap            old, locked heap relation tuples will be read from
  * new_heap            new, locked heap relation to insert tuples to
  * oldest_xmin xid used by the caller to determine which tuples are dead
  * freeze_xid  xid before which tuples will be frozen
+ * min_multi   multixact before which multis will be removed
  * use_wal             should the inserts to the new heap be WAL-logged?
  *
  * Returns an opaque RewriteState, allocated in current memory context,
  * to be used in subsequent calls to the other functions.
  */
 RewriteState
-begin_heap_rewrite(Relation new_heap, TransactionId oldest_xmin,
-                                  TransactionId freeze_xid, bool use_wal)
+begin_heap_rewrite(Relation old_heap, Relation new_heap, TransactionId oldest_xmin,
+                                  TransactionId freeze_xid, MultiXactId cutoff_multi,
+                                  bool use_wal)
 {
        RewriteState state;
        MemoryContext rw_cxt;
@@ -202,6 +266,7 @@ begin_heap_rewrite(Relation new_heap, TransactionId oldest_xmin,
        /* Create and fill in the state struct */
        state = palloc0(sizeof(RewriteStateData));
 
+       state->rs_old_rel = old_heap;
        state->rs_new_rel = new_heap;
        state->rs_buffer = (Page) palloc(BLCKSZ);
        /* new_heap needn't be empty, just locked */
@@ -210,6 +275,7 @@ begin_heap_rewrite(Relation new_heap, TransactionId oldest_xmin,
        state->rs_use_wal = use_wal;
        state->rs_oldest_xmin = oldest_xmin;
        state->rs_freeze_xid = freeze_xid;
+       state->rs_cutoff_multi = cutoff_multi;
        state->rs_cxt = rw_cxt;
 
        /* Initialize hash tables used to track update chains */
@@ -217,13 +283,12 @@ begin_heap_rewrite(Relation new_heap, TransactionId oldest_xmin,
        hash_ctl.keysize = sizeof(TidHashKey);
        hash_ctl.entrysize = sizeof(UnresolvedTupData);
        hash_ctl.hcxt = state->rs_cxt;
-       hash_ctl.hash = tag_hash;
 
        state->rs_unresolved_tups =
                hash_create("Rewrite / Unresolved ctids",
                                        128,            /* arbitrary initial size */
                                        &hash_ctl,
-                                       HASH_ELEM | HASH_FUNCTION | HASH_CONTEXT);
+                                       HASH_ELEM | HASH_BLOBS | HASH_CONTEXT);
 
        hash_ctl.entrysize = sizeof(OldToNewMappingData);
 
@@ -231,10 +296,12 @@ begin_heap_rewrite(Relation new_heap, TransactionId oldest_xmin,
                hash_create("Rewrite / Old to new tid map",
                                        128,            /* arbitrary initial size */
                                        &hash_ctl,
-                                       HASH_ELEM | HASH_FUNCTION | HASH_CONTEXT);
+                                       HASH_ELEM | HASH_BLOBS | HASH_CONTEXT);
 
        MemoryContextSwitchTo(old_cxt);
 
+       logical_begin_heap_rewrite(state);
+
        return state;
 }
 
@@ -252,8 +319,6 @@ end_heap_rewrite(RewriteState state)
        /*
         * Write any remaining tuples in the UnresolvedTups table. If we have any
         * left, they should in fact be dead, but let's err on the safe side.
-        *
-        * XXX this really is a waste of code no?
         */
        hash_seq_init(&seq_status, state->rs_unresolved_tups);
 
@@ -268,16 +333,21 @@ end_heap_rewrite(RewriteState state)
        {
                if (state->rs_use_wal)
                        log_newpage(&state->rs_new_rel->rd_node,
+                                               MAIN_FORKNUM,
                                                state->rs_blockno,
-                                               state->rs_buffer);
+                                               state->rs_buffer,
+                                               true);
                RelationOpenSmgr(state->rs_new_rel);
-               smgrextend(state->rs_new_rel->rd_smgr, state->rs_blockno,
+
+               PageSetChecksumInplace(state->rs_buffer, state->rs_blockno);
+
+               smgrextend(state->rs_new_rel->rd_smgr, MAIN_FORKNUM, state->rs_blockno,
                                   (char *) state->rs_buffer, true);
        }
 
        /*
-        * If the rel isn't temp, must fsync before commit.  We use heap_sync to
-        * ensure that the toast table gets fsync'd too.
+        * If the rel is WAL-logged, must fsync before commit.  We use heap_sync
+        * to ensure that the toast table gets fsync'd too.
         *
         * It's obvious that we must do this when not WAL-logging. It's less
         * obvious that we have to do it even if we did WAL-log the pages. The
@@ -286,9 +356,11 @@ end_heap_rewrite(RewriteState state)
         * occurring during the rewriteheap operation won't have fsync'd data we
         * wrote before the checkpoint.
         */
-       if (!state->rs_new_rel->rd_istemp)
+       if (RelationNeedsWAL(state->rs_new_rel))
                heap_sync(state->rs_new_rel);
 
+       logical_end_heap_rewrite(state);
+
        /* Deleting the context frees everything */
        MemoryContextDelete(state->rs_cxt);
 }
@@ -333,14 +405,10 @@ rewrite_heap_tuple(RewriteState state,
 
        /*
         * While we have our hands on the tuple, we may as well freeze any
-        * very-old xmin or xmax, so that future VACUUM effort can be saved.
-        *
-        * Note we abuse heap_freeze_tuple() a bit here, since it's expecting to
-        * be given a pointer to a tuple in a disk buffer.      It happens though that
-        * we can get the right things to happen by passing InvalidBuffer for the
-        * buffer.
+        * eligible xmin or xmax, so that future VACUUM effort can be saved.
         */
-       heap_freeze_tuple(new_tuple->t_data, state->rs_freeze_xid, InvalidBuffer);
+       heap_freeze_tuple(new_tuple->t_data, state->rs_freeze_xid,
+                                         state->rs_cutoff_multi);
 
        /*
         * Invalid ctid means that ctid should point to the tuple itself. We'll
@@ -351,15 +419,15 @@ rewrite_heap_tuple(RewriteState state,
        /*
         * If the tuple has been updated, check the old-to-new mapping hash table.
         */
-       if (!(old_tuple->t_data->t_infomask & (HEAP_XMAX_INVALID |
-                                                                                  HEAP_IS_LOCKED)) &&
+       if (!((old_tuple->t_data->t_infomask & HEAP_XMAX_INVALID) ||
+                 HeapTupleHeaderIsOnlyLocked(old_tuple->t_data)) &&
                !(ItemPointerEquals(&(old_tuple->t_self),
                                                        &(old_tuple->t_data->t_ctid))))
        {
                OldToNewMapping mapping;
 
                memset(&hashkey, 0, sizeof(hashkey));
-               hashkey.xmin = HeapTupleHeaderGetXmax(old_tuple->t_data);
+               hashkey.xmin = HeapTupleHeaderGetUpdateXid(old_tuple->t_data);
                hashkey.tid = old_tuple->t_data->t_ctid;
 
                mapping = (OldToNewMapping)
@@ -421,6 +489,8 @@ rewrite_heap_tuple(RewriteState state,
                raw_heap_insert(state, new_tuple);
                new_tid = new_tuple->t_self;
 
+               logical_rewrite_heap_tuple(state, old_tid, new_tuple);
+
                /*
                 * If the tuple is the updated version of a row, and the prior version
                 * wouldn't be DEAD yet, then we need to either resolve the prior
@@ -499,8 +569,12 @@ rewrite_heap_tuple(RewriteState state,
  * Register a dead tuple with an ongoing rewrite. Dead tuples are not
  * copied to the new table, but we still make note of them so that we
  * can release some resources earlier.
+ *
+ * Returns true if a tuple was removed from the unresolved_tups table.
+ * This indicates that that tuple, previously thought to be "recently dead",
+ * is now known really dead and won't be written to the output.
  */
-void
+bool
 rewrite_heap_dead_tuple(RewriteState state, HeapTuple old_tuple)
 {
        /*
@@ -536,11 +610,14 @@ rewrite_heap_dead_tuple(RewriteState state, HeapTuple old_tuple)
                hash_search(state->rs_unresolved_tups, &hashkey,
                                        HASH_REMOVE, &found);
                Assert(found);
+               return true;
        }
+
+       return false;
 }
 
 /*
- * Insert a tuple to the new relation. This has to track heap_insert
+ * Insert a tuple to the new relation.  This has to track heap_insert
  * and its subsidiary functions!
  *
  * t_self of the tuple is set to the new TID of the tuple. If t_ctid of the
@@ -572,7 +649,9 @@ raw_heap_insert(RewriteState state, HeapTuple tup)
        }
        else if (HeapTupleHasExternal(tup) || tup->t_len > TOAST_TUPLE_THRESHOLD)
                heaptup = toast_insert_or_update(state->rs_new_rel, tup, NULL,
-                                                                                state->rs_use_wal, false);
+                                                                                HEAP_INSERT_SKIP_FSM |
+                                                                                (state->rs_use_wal ?
+                                                                                 0 : HEAP_INSERT_SKIP_WAL));
        else
                heaptup = tup;
 
@@ -584,9 +663,8 @@ raw_heap_insert(RewriteState state, HeapTuple tup)
        if (len > MaxHeapTupleSize)
                ereport(ERROR,
                                (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
-                                errmsg("row is too big: size %lu, maximum size %lu",
-                                               (unsigned long) len,
-                                               (unsigned long) MaxHeapTupleSize)));
+                                errmsg("row is too big: size %zu, maximum size %zu",
+                                               len, MaxHeapTupleSize)));
 
        /* Compute desired extra freespace due to fillfactor option */
        saveFreeSpace = RelationGetTargetPageFreeSpace(state->rs_new_rel,
@@ -604,8 +682,10 @@ raw_heap_insert(RewriteState state, HeapTuple tup)
                        /* XLOG stuff */
                        if (state->rs_use_wal)
                                log_newpage(&state->rs_new_rel->rd_node,
+                                                       MAIN_FORKNUM,
                                                        state->rs_blockno,
-                                                       page);
+                                                       page,
+                                                       true);
 
                        /*
                         * Now write the page. We say isTemp = true even if it's not a
@@ -614,8 +694,11 @@ raw_heap_insert(RewriteState state, HeapTuple tup)
                         * end_heap_rewrite.
                         */
                        RelationOpenSmgr(state->rs_new_rel);
-                       smgrextend(state->rs_new_rel->rd_smgr, state->rs_blockno,
-                                          (char *) page, true);
+
+                       PageSetChecksumInplace(page, state->rs_blockno);
+
+                       smgrextend(state->rs_new_rel->rd_smgr, MAIN_FORKNUM,
+                                          state->rs_blockno, (char *) page, true);
 
                        state->rs_blockno++;
                        state->rs_buffer_valid = false;
@@ -630,7 +713,7 @@ raw_heap_insert(RewriteState state, HeapTuple tup)
        }
 
        /* And now we can insert the tuple into the page */
-       newoff = PageAddItem(page, (Item) heaptup->t_data, len,
+       newoff = PageAddItem(page, (Item) heaptup->t_data, heaptup->t_len,
                                                 InvalidOffsetNumber, false, true);
        if (newoff == InvalidOffsetNumber)
                elog(ERROR, "failed to add tuple");
@@ -657,3 +740,540 @@ raw_heap_insert(RewriteState state, HeapTuple tup)
        if (heaptup != tup)
                heap_freetuple(heaptup);
 }
+
+/* ------------------------------------------------------------------------
+ * Logical rewrite support
+ *
+ * When doing logical decoding - which relies on using cmin/cmax of catalog
+ * tuples, via xl_heap_new_cid records - heap rewrites have to log enough
+ * information to allow the decoding backend to updates its internal mapping
+ * of (relfilenode,ctid) => (cmin, cmax) to be correct for the rewritten heap.
+ *
+ * For that, every time we find a tuple that's been modified in a catalog
+ * relation within the xmin horizon of any decoding slot, we log a mapping
+ * from the old to the new location.
+ *
+ * To deal with rewrites that abort the filename of a mapping file contains
+ * the xid of the transaction performing the rewrite, which then can be
+ * checked before being read in.
+ *
+ * For efficiency we don't immediately spill every single map mapping for a
+ * row to disk but only do so in batches when we've collected several of them
+ * in memory or when end_heap_rewrite() has been called.
+ *
+ * Crash-Safety: This module diverts from the usual patterns of doing WAL
+ * since it cannot rely on checkpoint flushing out all buffers and thus
+ * waiting for exclusive locks on buffers. Usually the XLogInsert() covering
+ * buffer modifications is performed while the buffer(s) that are being
+ * modified are exclusively locked guaranteeing that both the WAL record and
+ * the modified heap are on either side of the checkpoint. But since the
+ * mapping files we log aren't in shared_buffers that interlock doesn't work.
+ *
+ * Instead we simply write the mapping files out to disk, *before* the
+ * XLogInsert() is performed. That guarantees that either the XLogInsert() is
+ * inserted after the checkpoint's redo pointer or that the checkpoint (via
+ * LogicalRewriteHeapCheckpoint()) has flushed the (partial) mapping file to
+ * disk. That leaves the tail end that has not yet been flushed open to
+ * corruption, which is solved by including the current offset in the
+ * xl_heap_rewrite_mapping records and truncating the mapping file to it
+ * during replay. Every time a rewrite is finished all generated mapping files
+ * are synced to disk.
+ *
+ * Note that if we were only concerned about crash safety we wouldn't have to
+ * deal with WAL logging at all - an fsync() at the end of a rewrite would be
+ * sufficient for crash safety. Any mapping that hasn't been safely flushed to
+ * disk has to be by an aborted (explicitly or via a crash) transaction and is
+ * ignored by virtue of the xid in its name being subject to a
+ * TransactionDidCommit() check. But we want to support having standbys via
+ * physical replication, both for availability and to do logical decoding
+ * there.
+ * ------------------------------------------------------------------------
+ */
+
+/*
+ * Do preparations for logging logical mappings during a rewrite if
+ * necessary. If we detect that we don't need to log anything we'll prevent
+ * any further action by the various logical rewrite functions.
+ */
+static void
+logical_begin_heap_rewrite(RewriteState state)
+{
+       HASHCTL         hash_ctl;
+       TransactionId logical_xmin;
+
+       /*
+        * We only need to persist these mappings if the rewritten table can be
+        * accessed during logical decoding, if not, we can skip doing any
+        * additional work.
+        */
+       state->rs_logical_rewrite =
+               RelationIsAccessibleInLogicalDecoding(state->rs_old_rel);
+
+       if (!state->rs_logical_rewrite)
+               return;
+
+       ProcArrayGetReplicationSlotXmin(NULL, &logical_xmin);
+
+       /*
+        * If there are no logical slots in progress we don't need to do anything,
+        * there cannot be any remappings for relevant rows yet. The relation's
+        * lock protects us against races.
+        */
+       if (logical_xmin == InvalidTransactionId)
+       {
+               state->rs_logical_rewrite = false;
+               return;
+       }
+
+       state->rs_logical_xmin = logical_xmin;
+       state->rs_begin_lsn = GetXLogInsertRecPtr();
+       state->rs_num_rewrite_mappings = 0;
+
+       memset(&hash_ctl, 0, sizeof(hash_ctl));
+       hash_ctl.keysize = sizeof(TransactionId);
+       hash_ctl.entrysize = sizeof(RewriteMappingFile);
+       hash_ctl.hcxt = state->rs_cxt;
+
+       state->rs_logical_mappings =
+               hash_create("Logical rewrite mapping",
+                                       128,            /* arbitrary initial size */
+                                       &hash_ctl,
+                                       HASH_ELEM | HASH_BLOBS | HASH_CONTEXT);
+}
+
+/*
+ * Flush all logical in-memory mappings to disk, but don't fsync them yet.
+ */
+static void
+logical_heap_rewrite_flush_mappings(RewriteState state)
+{
+       HASH_SEQ_STATUS seq_status;
+       RewriteMappingFile *src;
+       dlist_mutable_iter iter;
+
+       Assert(state->rs_logical_rewrite);
+
+       /* no logical rewrite in progress, no need to iterate over mappings */
+       if (state->rs_num_rewrite_mappings == 0)
+               return;
+
+       elog(DEBUG1, "flushing %u logical rewrite mapping entries",
+                state->rs_num_rewrite_mappings);
+
+       hash_seq_init(&seq_status, state->rs_logical_mappings);
+       while ((src = (RewriteMappingFile *) hash_seq_search(&seq_status)) != NULL)
+       {
+               char       *waldata;
+               char       *waldata_start;
+               xl_heap_rewrite_mapping xlrec;
+               Oid                     dboid;
+               uint32          len;
+               int                     written;
+
+               /* this file hasn't got any new mappings */
+               if (src->num_mappings == 0)
+                       continue;
+
+               if (state->rs_old_rel->rd_rel->relisshared)
+                       dboid = InvalidOid;
+               else
+                       dboid = MyDatabaseId;
+
+               xlrec.num_mappings = src->num_mappings;
+               xlrec.mapped_rel = RelationGetRelid(state->rs_old_rel);
+               xlrec.mapped_xid = src->xid;
+               xlrec.mapped_db = dboid;
+               xlrec.offset = src->off;
+               xlrec.start_lsn = state->rs_begin_lsn;
+
+               /* write all mappings consecutively */
+               len = src->num_mappings * sizeof(LogicalRewriteMappingData);
+               waldata_start = waldata = palloc(len);
+
+               /*
+                * collect data we need to write out, but don't modify ondisk data yet
+                */
+               dlist_foreach_modify(iter, &src->mappings)
+               {
+                       RewriteMappingDataEntry *pmap;
+
+                       pmap = dlist_container(RewriteMappingDataEntry, node, iter.cur);
+
+                       memcpy(waldata, &pmap->map, sizeof(pmap->map));
+                       waldata += sizeof(pmap->map);
+
+                       /* remove from the list and free */
+                       dlist_delete(&pmap->node);
+                       pfree(pmap);
+
+                       /* update bookkeeping */
+                       state->rs_num_rewrite_mappings--;
+                       src->num_mappings--;
+               }
+
+               Assert(src->num_mappings == 0);
+               Assert(waldata == waldata_start + len);
+
+               /*
+                * Note that we deviate from the usual WAL coding practices here,
+                * check the above "Logical rewrite support" comment for reasoning.
+                */
+               written = FileWrite(src->vfd, waldata_start, len);
+               if (written != len)
+                       ereport(ERROR,
+                                       (errcode_for_file_access(),
+                                        errmsg("could not write to file \"%s\", wrote %d of %d: %m", src->path,
+                                                       written, len)));
+               src->off += len;
+
+               XLogBeginInsert();
+               XLogRegisterData((char *) (&xlrec), sizeof(xlrec));
+               XLogRegisterData(waldata_start, len);
+
+               /* write xlog record */
+               XLogInsert(RM_HEAP2_ID, XLOG_HEAP2_REWRITE);
+
+               pfree(waldata_start);
+       }
+       Assert(state->rs_num_rewrite_mappings == 0);
+}
+
+/*
+ * Logical remapping part of end_heap_rewrite().
+ */
+static void
+logical_end_heap_rewrite(RewriteState state)
+{
+       HASH_SEQ_STATUS seq_status;
+       RewriteMappingFile *src;
+
+       /* done, no logical rewrite in progress */
+       if (!state->rs_logical_rewrite)
+               return;
+
+       /* writeout remaining in-memory entries */
+       if (state->rs_num_rewrite_mappings > 0)
+               logical_heap_rewrite_flush_mappings(state);
+
+       /* Iterate over all mappings we have written and fsync the files. */
+       hash_seq_init(&seq_status, state->rs_logical_mappings);
+       while ((src = (RewriteMappingFile *) hash_seq_search(&seq_status)) != NULL)
+       {
+               if (FileSync(src->vfd) != 0)
+                       ereport(ERROR,
+                                       (errcode_for_file_access(),
+                                        errmsg("could not fsync file \"%s\": %m", src->path)));
+               FileClose(src->vfd);
+       }
+       /* memory context cleanup will deal with the rest */
+}
+
+/*
+ * Log a single (old->new) mapping for 'xid'.
+ */
+static void
+logical_rewrite_log_mapping(RewriteState state, TransactionId xid,
+                                                       LogicalRewriteMappingData *map)
+{
+       RewriteMappingFile *src;
+       RewriteMappingDataEntry *pmap;
+       Oid                     relid;
+       bool            found;
+
+       relid = RelationGetRelid(state->rs_old_rel);
+
+       /* look for existing mappings for this 'mapped' xid */
+       src = hash_search(state->rs_logical_mappings, &xid,
+                                         HASH_ENTER, &found);
+
+       /*
+        * We haven't yet had the need to map anything for this xid, create
+        * per-xid data structures.
+        */
+       if (!found)
+       {
+               char            path[MAXPGPATH];
+               Oid                     dboid;
+
+               if (state->rs_old_rel->rd_rel->relisshared)
+                       dboid = InvalidOid;
+               else
+                       dboid = MyDatabaseId;
+
+               snprintf(path, MAXPGPATH,
+                                "pg_logical/mappings/" LOGICAL_REWRITE_FORMAT,
+                                dboid, relid,
+                                (uint32) (state->rs_begin_lsn >> 32),
+                                (uint32) state->rs_begin_lsn,
+                                xid, GetCurrentTransactionId());
+
+               dlist_init(&src->mappings);
+               src->num_mappings = 0;
+               src->off = 0;
+               memcpy(src->path, path, sizeof(path));
+               src->vfd = PathNameOpenFile(path,
+                                                                       O_CREAT | O_EXCL | O_WRONLY | PG_BINARY,
+                                                                       S_IRUSR | S_IWUSR);
+               if (src->vfd < 0)
+                       ereport(ERROR,
+                                       (errcode_for_file_access(),
+                                        errmsg("could not create file \"%s\": %m", path)));
+       }
+
+       pmap = MemoryContextAlloc(state->rs_cxt,
+                                                         sizeof(RewriteMappingDataEntry));
+       memcpy(&pmap->map, map, sizeof(LogicalRewriteMappingData));
+       dlist_push_tail(&src->mappings, &pmap->node);
+       src->num_mappings++;
+       state->rs_num_rewrite_mappings++;
+
+       /*
+        * Write out buffer every time we've too many in-memory entries across all
+        * mapping files.
+        */
+       if (state->rs_num_rewrite_mappings >= 1000 /* arbitrary number */ )
+               logical_heap_rewrite_flush_mappings(state);
+}
+
+/*
+ * Perform logical remapping for a tuple that's mapped from old_tid to
+ * new_tuple->t_self by rewrite_heap_tuple() if necessary for the tuple.
+ */
+static void
+logical_rewrite_heap_tuple(RewriteState state, ItemPointerData old_tid,
+                                                  HeapTuple new_tuple)
+{
+       ItemPointerData new_tid = new_tuple->t_self;
+       TransactionId cutoff = state->rs_logical_xmin;
+       TransactionId xmin;
+       TransactionId xmax;
+       bool            do_log_xmin = false;
+       bool            do_log_xmax = false;
+       LogicalRewriteMappingData map;
+
+       /* no logical rewrite in progress, we don't need to log anything */
+       if (!state->rs_logical_rewrite)
+               return;
+
+       xmin = HeapTupleHeaderGetXmin(new_tuple->t_data);
+       /* use *GetUpdateXid to correctly deal with multixacts */
+       xmax = HeapTupleHeaderGetUpdateXid(new_tuple->t_data);
+
+       /*
+        * Log the mapping iff the tuple has been created recently.
+        */
+       if (TransactionIdIsNormal(xmin) && !TransactionIdPrecedes(xmin, cutoff))
+               do_log_xmin = true;
+
+       if (!TransactionIdIsNormal(xmax))
+       {
+               /*
+                * no xmax is set, can't have any permanent ones, so this check is
+                * sufficient
+                */
+       }
+       else if (HEAP_XMAX_IS_LOCKED_ONLY(new_tuple->t_data->t_infomask))
+       {
+               /* only locked, we don't care */
+       }
+       else if (!TransactionIdPrecedes(xmax, cutoff))
+       {
+               /* tuple has been deleted recently, log */
+               do_log_xmax = true;
+       }
+
+       /* if neither needs to be logged, we're done */
+       if (!do_log_xmin && !do_log_xmax)
+               return;
+
+       /* fill out mapping information */
+       map.old_node = state->rs_old_rel->rd_node;
+       map.old_tid = old_tid;
+       map.new_node = state->rs_new_rel->rd_node;
+       map.new_tid = new_tid;
+
+       /* ---
+        * Now persist the mapping for the individual xids that are affected. We
+        * need to log for both xmin and xmax if they aren't the same transaction
+        * since the mapping files are per "affected" xid.
+        * We don't muster all that much effort detecting whether xmin and xmax
+        * are actually the same transaction, we just check whether the xid is the
+        * same disregarding subtransactions. Logging too much is relatively
+        * harmless and we could never do the check fully since subtransaction
+        * data is thrown away during restarts.
+        * ---
+        */
+       if (do_log_xmin)
+               logical_rewrite_log_mapping(state, xmin, &map);
+       /* separately log mapping for xmax unless it'd be redundant */
+       if (do_log_xmax && !TransactionIdEquals(xmin, xmax))
+               logical_rewrite_log_mapping(state, xmax, &map);
+}
+
+/*
+ * Replay XLOG_HEAP2_REWRITE records
+ */
+void
+heap_xlog_logical_rewrite(XLogReaderState *r)
+{
+       char            path[MAXPGPATH];
+       int                     fd;
+       xl_heap_rewrite_mapping *xlrec;
+       uint32          len;
+       char       *data;
+
+       xlrec = (xl_heap_rewrite_mapping *) XLogRecGetData(r);
+
+       snprintf(path, MAXPGPATH,
+                        "pg_logical/mappings/" LOGICAL_REWRITE_FORMAT,
+                        xlrec->mapped_db, xlrec->mapped_rel,
+                        (uint32) (xlrec->start_lsn >> 32),
+                        (uint32) xlrec->start_lsn,
+                        xlrec->mapped_xid, XLogRecGetXid(r));
+
+       fd = OpenTransientFile(path,
+                                                  O_CREAT | O_WRONLY | PG_BINARY,
+                                                  S_IRUSR | S_IWUSR);
+       if (fd < 0)
+               ereport(ERROR,
+                               (errcode_for_file_access(),
+                                errmsg("could not create file \"%s\": %m", path)));
+
+       /*
+        * Truncate all data that's not guaranteed to have been safely fsynced (by
+        * previous record or by the last checkpoint).
+        */
+       if (ftruncate(fd, xlrec->offset) != 0)
+               ereport(ERROR,
+                               (errcode_for_file_access(),
+                                errmsg("could not truncate file \"%s\" to %u: %m",
+                                               path, (uint32) xlrec->offset)));
+
+       /* now seek to the position we want to write our data to */
+       if (lseek(fd, xlrec->offset, SEEK_SET) != xlrec->offset)
+               ereport(ERROR,
+                               (errcode_for_file_access(),
+                                errmsg("could not seek to end of file \"%s\": %m",
+                                               path)));
+
+       data = XLogRecGetData(r) + sizeof(*xlrec);
+
+       len = xlrec->num_mappings * sizeof(LogicalRewriteMappingData);
+
+       /* write out tail end of mapping file (again) */
+       if (write(fd, data, len) != len)
+               ereport(ERROR,
+                               (errcode_for_file_access(),
+                                errmsg("could not write to file \"%s\": %m", path)));
+
+       /*
+        * Now fsync all previously written data. We could improve things and only
+        * do this for the last write to a file, but the required bookkeeping
+        * doesn't seem worth the trouble.
+        */
+       if (pg_fsync(fd) != 0)
+               ereport(ERROR,
+                               (errcode_for_file_access(),
+                                errmsg("could not fsync file \"%s\": %m", path)));
+
+       CloseTransientFile(fd);
+}
+
+/* ---
+ * Perform a checkpoint for logical rewrite mappings
+ *
+ * This serves two tasks:
+ * 1) Remove all mappings not needed anymore based on the logical restart LSN
+ * 2) Flush all remaining mappings to disk, so that replay after a checkpoint
+ *       only has to deal with the parts of a mapping that have been written out
+ *       after the checkpoint started.
+ * ---
+ */
+void
+CheckPointLogicalRewriteHeap(void)
+{
+       XLogRecPtr      cutoff;
+       XLogRecPtr      redo;
+       DIR                *mappings_dir;
+       struct dirent *mapping_de;
+       char            path[MAXPGPATH];
+
+       /*
+        * We start of with a minimum of the last redo pointer. No new decoding
+        * slot will start before that, so that's a safe upper bound for removal.
+        */
+       redo = GetRedoRecPtr();
+
+       /* now check for the restart ptrs from existing slots */
+       cutoff = ReplicationSlotsComputeLogicalRestartLSN();
+
+       /* don't start earlier than the restart lsn */
+       if (cutoff != InvalidXLogRecPtr && redo < cutoff)
+               cutoff = redo;
+
+       mappings_dir = AllocateDir("pg_logical/mappings");
+       while ((mapping_de = ReadDir(mappings_dir, "pg_logical/mappings")) != NULL)
+       {
+               struct stat statbuf;
+               Oid                     dboid;
+               Oid                     relid;
+               XLogRecPtr      lsn;
+               TransactionId rewrite_xid;
+               TransactionId create_xid;
+               uint32          hi,
+                                       lo;
+
+               if (strcmp(mapping_de->d_name, ".") == 0 ||
+                       strcmp(mapping_de->d_name, "..") == 0)
+                       continue;
+
+               snprintf(path, MAXPGPATH, "pg_logical/mappings/%s", mapping_de->d_name);
+               if (lstat(path, &statbuf) == 0 && !S_ISREG(statbuf.st_mode))
+                       continue;
+
+               /* Skip over files that cannot be ours. */
+               if (strncmp(mapping_de->d_name, "map-", 4) != 0)
+                       continue;
+
+               if (sscanf(mapping_de->d_name, LOGICAL_REWRITE_FORMAT,
+                                  &dboid, &relid, &hi, &lo, &rewrite_xid, &create_xid) != 6)
+                       elog(ERROR, "could not parse filename \"%s\"", mapping_de->d_name);
+
+               lsn = ((uint64) hi) << 32 | lo;
+
+               if (lsn < cutoff || cutoff == InvalidXLogRecPtr)
+               {
+                       elog(DEBUG1, "removing logical rewrite file \"%s\"", path);
+                       if (unlink(path) < 0)
+                               ereport(ERROR,
+                                               (errcode_for_file_access(),
+                                                errmsg("could not remove file \"%s\": %m", path)));
+               }
+               else
+               {
+                       int                     fd = OpenTransientFile(path, O_RDONLY | PG_BINARY, 0);
+
+                       /*
+                        * The file cannot vanish due to concurrency since this function
+                        * is the only one removing logical mappings and it's run while
+                        * CheckpointLock is held exclusively.
+                        */
+                       if (fd < 0)
+                               ereport(ERROR,
+                                               (errcode_for_file_access(),
+                                                errmsg("could not open file \"%s\": %m", path)));
+
+                       /*
+                        * We could try to avoid fsyncing files that either haven't
+                        * changed or have only been created since the checkpoint's start,
+                        * but it's currently not deemed worth the effort.
+                        */
+                       else if (pg_fsync(fd) != 0)
+                               ereport(ERROR,
+                                               (errcode_for_file_access(),
+                                                errmsg("could not fsync file \"%s\": %m", path)));
+                       CloseTransientFile(fd);
+               }
+       }
+       FreeDir(mappings_dir);
+}