LCOV - code coverage report
Current view: top level - src/backend/access/heap - rewriteheap.c (source / functions) Coverage Total Hit
Test: PostgreSQL 20devel Lines: 83.6 % 311 260
Test Date: 2026-07-03 19:57:34 Functions: 91.7 % 12 11
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 57.4 % 202 116

             Branch data     Line data    Source code
       1                 :             : /*-------------------------------------------------------------------------
       2                 :             :  *
       3                 :             :  * rewriteheap.c
       4                 :             :  *    Support functions to rewrite tables.
       5                 :             :  *
       6                 :             :  * These functions provide a facility to completely rewrite a heap, while
       7                 :             :  * preserving visibility information and update chains.
       8                 :             :  *
       9                 :             :  * INTERFACE
      10                 :             :  *
      11                 :             :  * The caller is responsible for creating the new heap, all catalog
      12                 :             :  * changes, supplying the tuples to be written to the new heap, and
      13                 :             :  * rebuilding indexes.  The caller must hold AccessExclusiveLock on the
      14                 :             :  * target table, because we assume no one else is writing into it.
      15                 :             :  *
      16                 :             :  * To use the facility:
      17                 :             :  *
      18                 :             :  * begin_heap_rewrite
      19                 :             :  * while (fetch next tuple)
      20                 :             :  * {
      21                 :             :  *     if (tuple is dead)
      22                 :             :  *         rewrite_heap_dead_tuple
      23                 :             :  *     else
      24                 :             :  *     {
      25                 :             :  *         // do any transformations here if required
      26                 :             :  *         rewrite_heap_tuple
      27                 :             :  *     }
      28                 :             :  * }
      29                 :             :  * end_heap_rewrite
      30                 :             :  *
      31                 :             :  * The contents of the new relation shouldn't be relied on until after
      32                 :             :  * end_heap_rewrite is called.
      33                 :             :  *
      34                 :             :  *
      35                 :             :  * IMPLEMENTATION
      36                 :             :  *
      37                 :             :  * This would be a fairly trivial affair, except that we need to maintain
      38                 :             :  * the ctid chains that link versions of an updated tuple together.
      39                 :             :  * Since the newly stored tuples will have tids different from the original
      40                 :             :  * ones, if we just copied t_ctid fields to the new table the links would
      41                 :             :  * be wrong.  When we are required to copy a (presumably recently-dead or
      42                 :             :  * delete-in-progress) tuple whose ctid doesn't point to itself, we have
      43                 :             :  * to substitute the correct ctid instead.
      44                 :             :  *
      45                 :             :  * For each ctid reference from A -> B, we might encounter either A first
      46                 :             :  * or B first.  (Note that a tuple in the middle of a chain is both A and B
      47                 :             :  * of different pairs.)
      48                 :             :  *
      49                 :             :  * If we encounter A first, we'll store the tuple in the unresolved_tups
      50                 :             :  * hash table. When we later encounter B, we remove A from the hash table,
      51                 :             :  * fix the ctid to point to the new location of B, and insert both A and B
      52                 :             :  * to the new heap.
      53                 :             :  *
      54                 :             :  * If we encounter B first, we can insert B to the new heap right away.
      55                 :             :  * We then add an entry to the old_new_tid_map hash table showing B's
      56                 :             :  * original tid (in the old heap) and new tid (in the new heap).
      57                 :             :  * When we later encounter A, we get the new location of B from the table,
      58                 :             :  * and can write A immediately with the correct ctid.
      59                 :             :  *
      60                 :             :  * Entries in the hash tables can be removed as soon as the later tuple
      61                 :             :  * is encountered.  That helps to keep the memory usage down.  At the end,
      62                 :             :  * both tables are usually empty; we should have encountered both A and B
      63                 :             :  * of each pair.  However, it's possible for A to be RECENTLY_DEAD and B
      64                 :             :  * entirely DEAD according to HeapTupleSatisfiesVacuum, because the test
      65                 :             :  * for deadness using OldestXmin is not exact.  In such a case we might
      66                 :             :  * encounter B first, and skip it, and find A later.  Then A would be added
      67                 :             :  * to unresolved_tups, and stay there until end of the rewrite.  Since
      68                 :             :  * this case is very unusual, we don't worry about the memory usage.
      69                 :             :  *
      70                 :             :  * Using in-memory hash tables means that we use some memory for each live
      71                 :             :  * update chain in the table, from the time we find one end of the
      72                 :             :  * reference until we find the other end.  That shouldn't be a problem in
      73                 :             :  * practice, but if you do something like an UPDATE without a where-clause
      74                 :             :  * on a large table, and then run CLUSTER in the same transaction, you
      75                 :             :  * could run out of memory.  It doesn't seem worthwhile to add support for
      76                 :             :  * spill-to-disk, as there shouldn't be that many RECENTLY_DEAD tuples in a
      77                 :             :  * table under normal circumstances.  Furthermore, in the typical scenario
      78                 :             :  * of CLUSTERing on an unchanging key column, we'll see all the versions
      79                 :             :  * of a given tuple together anyway, and so the peak memory usage is only
      80                 :             :  * proportional to the number of RECENTLY_DEAD versions of a single row, not
      81                 :             :  * in the whole table.  Note that if we do fail halfway through a CLUSTER,
      82                 :             :  * the old table is still valid, so failure is not catastrophic.
      83                 :             :  *
      84                 :             :  * We can't use the normal heap_insert function to insert into the new
      85                 :             :  * heap, because heap_insert overwrites the visibility information.
      86                 :             :  * We use a special-purpose raw_heap_insert function instead, which
      87                 :             :  * is optimized for bulk inserting a lot of tuples, knowing that we have
      88                 :             :  * exclusive access to the heap.  raw_heap_insert builds new pages in
      89                 :             :  * local storage.  When a page is full, or at the end of the process,
      90                 :             :  * we insert it to WAL as a single record and then write it to disk with
      91                 :             :  * the bulk smgr writer.  Note, however, that any data sent to the new
      92                 :             :  * heap's TOAST table will go through the normal bufmgr.
      93                 :             :  *
      94                 :             :  *
      95                 :             :  * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
      96                 :             :  * Portions Copyright (c) 1994-5, Regents of the University of California
      97                 :             :  *
      98                 :             :  * IDENTIFICATION
      99                 :             :  *    src/backend/access/heap/rewriteheap.c
     100                 :             :  *
     101                 :             :  *-------------------------------------------------------------------------
     102                 :             :  */
     103                 :             : #include "postgres.h"
     104                 :             : 
     105                 :             : #include <unistd.h>
     106                 :             : 
     107                 :             : #include "access/heapam.h"
     108                 :             : #include "access/heapam_xlog.h"
     109                 :             : #include "access/heaptoast.h"
     110                 :             : #include "access/rewriteheap.h"
     111                 :             : #include "access/transam.h"
     112                 :             : #include "access/xact.h"
     113                 :             : #include "access/xloginsert.h"
     114                 :             : #include "common/file_utils.h"
     115                 :             : #include "lib/ilist.h"
     116                 :             : #include "miscadmin.h"
     117                 :             : #include "pgstat.h"
     118                 :             : #include "replication/slot.h"
     119                 :             : #include "storage/bufmgr.h"
     120                 :             : #include "storage/bulk_write.h"
     121                 :             : #include "storage/fd.h"
     122                 :             : #include "storage/procarray.h"
     123                 :             : #include "utils/memutils.h"
     124                 :             : #include "utils/rel.h"
     125                 :             : #include "utils/wait_event.h"
     126                 :             : 
     127                 :             : /*
     128                 :             :  * State associated with a rewrite operation. This is opaque to the user
     129                 :             :  * of the rewrite facility.
     130                 :             :  */
     131                 :             : typedef struct RewriteStateData
     132                 :             : {
     133                 :             :     Relation    rs_old_rel;     /* source heap */
     134                 :             :     Relation    rs_new_rel;     /* destination heap */
     135                 :             :     BulkWriteState *rs_bulkstate;   /* writer for the destination */
     136                 :             :     BulkWriteBuffer rs_buffer;  /* page currently being built */
     137                 :             :     BlockNumber rs_blockno;     /* block where page will go */
     138                 :             :     bool        rs_logical_rewrite; /* do we need to do logical rewriting */
     139                 :             :     TransactionId rs_oldest_xmin;   /* oldest xmin used by caller to determine
     140                 :             :                                      * tuple visibility */
     141                 :             :     TransactionId rs_freeze_xid;    /* Xid that will be used as freeze cutoff
     142                 :             :                                      * point */
     143                 :             :     TransactionId rs_logical_xmin;  /* Xid that will be used as cutoff point
     144                 :             :                                      * for logical rewrites */
     145                 :             :     MultiXactId rs_cutoff_multi;    /* MultiXactId that will be used as cutoff
     146                 :             :                                      * point for multixacts */
     147                 :             :     MemoryContext rs_cxt;       /* for hash tables and entries and tuples in
     148                 :             :                                  * them */
     149                 :             :     XLogRecPtr  rs_begin_lsn;   /* XLogInsertLsn when starting the rewrite */
     150                 :             :     HTAB       *rs_unresolved_tups; /* unmatched A tuples */
     151                 :             :     HTAB       *rs_old_new_tid_map; /* unmatched B tuples */
     152                 :             :     HTAB       *rs_logical_mappings;    /* logical remapping files */
     153                 :             :     uint32      rs_num_rewrite_mappings;    /* # in memory mappings */
     154                 :             : } RewriteStateData;
     155                 :             : 
     156                 :             : /*
     157                 :             :  * The lookup keys for the hash tables are tuple TID and xmin (we must check
     158                 :             :  * both to avoid false matches from dead tuples).  Beware that there is
     159                 :             :  * probably some padding space in this struct; it must be zeroed out for
     160                 :             :  * correct hashtable operation.
     161                 :             :  */
     162                 :             : typedef struct
     163                 :             : {
     164                 :             :     TransactionId xmin;         /* tuple xmin */
     165                 :             :     ItemPointerData tid;        /* tuple location in old heap */
     166                 :             : } TidHashKey;
     167                 :             : 
     168                 :             : /*
     169                 :             :  * Entry structures for the hash tables
     170                 :             :  */
     171                 :             : typedef struct
     172                 :             : {
     173                 :             :     TidHashKey  key;            /* expected xmin/old location of B tuple */
     174                 :             :     ItemPointerData old_tid;    /* A's location in the old heap */
     175                 :             :     HeapTuple   tuple;          /* A's tuple contents */
     176                 :             : } UnresolvedTupData;
     177                 :             : 
     178                 :             : typedef UnresolvedTupData *UnresolvedTup;
     179                 :             : 
     180                 :             : typedef struct
     181                 :             : {
     182                 :             :     TidHashKey  key;            /* actual xmin/old location of B tuple */
     183                 :             :     ItemPointerData new_tid;    /* where we put it in the new heap */
     184                 :             : } OldToNewMappingData;
     185                 :             : 
     186                 :             : typedef OldToNewMappingData *OldToNewMapping;
     187                 :             : 
     188                 :             : /*
     189                 :             :  * In-Memory data for an xid that might need logical remapping entries
     190                 :             :  * to be logged.
     191                 :             :  */
     192                 :             : typedef struct RewriteMappingFile
     193                 :             : {
     194                 :             :     TransactionId xid;          /* xid that might need to see the row */
     195                 :             :     int         vfd;            /* fd of mappings file */
     196                 :             :     off_t       off;            /* how far have we written yet */
     197                 :             :     dclist_head mappings;       /* list of in-memory mappings */
     198                 :             :     char        path[MAXPGPATH];    /* path, for error messages */
     199                 :             : } RewriteMappingFile;
     200                 :             : 
     201                 :             : /*
     202                 :             :  * A single In-Memory logical rewrite mapping, hanging off
     203                 :             :  * RewriteMappingFile->mappings.
     204                 :             :  */
     205                 :             : typedef struct RewriteMappingDataEntry
     206                 :             : {
     207                 :             :     LogicalRewriteMappingData map;  /* map between old and new location of the
     208                 :             :                                      * tuple */
     209                 :             :     dlist_node  node;
     210                 :             : } RewriteMappingDataEntry;
     211                 :             : 
     212                 :             : 
     213                 :             : /* prototypes for internal functions */
     214                 :             : static void raw_heap_insert(RewriteState state, HeapTuple tup);
     215                 :             : 
     216                 :             : /* internal logical remapping prototypes */
     217                 :             : static void logical_begin_heap_rewrite(RewriteState state);
     218                 :             : static void logical_rewrite_heap_tuple(RewriteState state, ItemPointerData old_tid, HeapTuple new_tuple);
     219                 :             : static void logical_end_heap_rewrite(RewriteState state);
     220                 :             : 
     221                 :             : 
     222                 :             : /*
     223                 :             :  * Begin a rewrite of a table
     224                 :             :  *
     225                 :             :  * old_heap     old, locked heap relation tuples will be read from
     226                 :             :  * new_heap     new, locked heap relation to insert tuples to
     227                 :             :  * oldest_xmin  xid used by the caller to determine which tuples are dead
     228                 :             :  * freeze_xid   xid before which tuples will be frozen
     229                 :             :  * cutoff_multi multixact before which multis will be removed
     230                 :             :  *
     231                 :             :  * Returns an opaque RewriteState, allocated in current memory context,
     232                 :             :  * to be used in subsequent calls to the other functions.
     233                 :             :  */
     234                 :             : RewriteState
     235                 :         399 : begin_heap_rewrite(Relation old_heap, Relation new_heap, TransactionId oldest_xmin,
     236                 :             :                    TransactionId freeze_xid, MultiXactId cutoff_multi)
     237                 :             : {
     238                 :             :     RewriteState state;
     239                 :             :     MemoryContext rw_cxt;
     240                 :             :     MemoryContext old_cxt;
     241                 :             :     HASHCTL     hash_ctl;
     242                 :             : 
     243                 :             :     /*
     244                 :             :      * To ease cleanup, make a separate context that will contain the
     245                 :             :      * RewriteState struct itself plus all subsidiary data.
     246                 :             :      */
     247                 :         399 :     rw_cxt = AllocSetContextCreate(CurrentMemoryContext,
     248                 :             :                                    "Table rewrite",
     249                 :             :                                    ALLOCSET_DEFAULT_SIZES);
     250                 :         399 :     old_cxt = MemoryContextSwitchTo(rw_cxt);
     251                 :             : 
     252                 :             :     /* Create and fill in the state struct */
     253                 :         399 :     state = palloc0_object(RewriteStateData);
     254                 :             : 
     255                 :         399 :     state->rs_old_rel = old_heap;
     256                 :         399 :     state->rs_new_rel = new_heap;
     257                 :         399 :     state->rs_buffer = NULL;
     258                 :             :     /* new_heap needn't be empty, just locked */
     259                 :         399 :     state->rs_blockno = RelationGetNumberOfBlocks(new_heap);
     260                 :         399 :     state->rs_oldest_xmin = oldest_xmin;
     261                 :         399 :     state->rs_freeze_xid = freeze_xid;
     262                 :         399 :     state->rs_cutoff_multi = cutoff_multi;
     263                 :         399 :     state->rs_cxt = rw_cxt;
     264                 :         399 :     state->rs_bulkstate = smgr_bulk_start_rel(new_heap, MAIN_FORKNUM);
     265                 :             : 
     266                 :             :     /* Initialize hash tables used to track update chains */
     267                 :         399 :     hash_ctl.keysize = sizeof(TidHashKey);
     268                 :         399 :     hash_ctl.entrysize = sizeof(UnresolvedTupData);
     269                 :         399 :     hash_ctl.hcxt = state->rs_cxt;
     270                 :             : 
     271                 :         399 :     state->rs_unresolved_tups =
     272                 :         399 :         hash_create("Rewrite / Unresolved ctids",
     273                 :             :                     128,        /* arbitrary initial size */
     274                 :             :                     &hash_ctl,
     275                 :             :                     HASH_ELEM | HASH_BLOBS | HASH_CONTEXT);
     276                 :             : 
     277                 :         399 :     hash_ctl.entrysize = sizeof(OldToNewMappingData);
     278                 :             : 
     279                 :         399 :     state->rs_old_new_tid_map =
     280                 :         399 :         hash_create("Rewrite / Old to new tid map",
     281                 :             :                     128,        /* arbitrary initial size */
     282                 :             :                     &hash_ctl,
     283                 :             :                     HASH_ELEM | HASH_BLOBS | HASH_CONTEXT);
     284                 :             : 
     285                 :         399 :     MemoryContextSwitchTo(old_cxt);
     286                 :             : 
     287                 :         399 :     logical_begin_heap_rewrite(state);
     288                 :             : 
     289                 :         399 :     return state;
     290                 :             : }
     291                 :             : 
     292                 :             : /*
     293                 :             :  * End a rewrite.
     294                 :             :  *
     295                 :             :  * state and any other resources are freed.
     296                 :             :  */
     297                 :             : void
     298                 :         399 : end_heap_rewrite(RewriteState state)
     299                 :             : {
     300                 :             :     HASH_SEQ_STATUS seq_status;
     301                 :             :     UnresolvedTup unresolved;
     302                 :             : 
     303                 :             :     /*
     304                 :             :      * Write any remaining tuples in the UnresolvedTups table. If we have any
     305                 :             :      * left, they should in fact be dead, but let's err on the safe side.
     306                 :             :      */
     307                 :         399 :     hash_seq_init(&seq_status, state->rs_unresolved_tups);
     308                 :             : 
     309         [ -  + ]:         399 :     while ((unresolved = hash_seq_search(&seq_status)) != NULL)
     310                 :             :     {
     311                 :           0 :         ItemPointerSetInvalid(&unresolved->tuple->t_data->t_ctid);
     312                 :           0 :         raw_heap_insert(state, unresolved->tuple);
     313                 :             :     }
     314                 :             : 
     315                 :             :     /* Write the last page, if any */
     316         [ +  + ]:         399 :     if (state->rs_buffer)
     317                 :             :     {
     318                 :         272 :         smgr_bulk_write(state->rs_bulkstate, state->rs_blockno, state->rs_buffer, true);
     319                 :         272 :         state->rs_buffer = NULL;
     320                 :             :     }
     321                 :             : 
     322                 :         399 :     smgr_bulk_finish(state->rs_bulkstate);
     323                 :             : 
     324                 :         399 :     logical_end_heap_rewrite(state);
     325                 :             : 
     326                 :             :     /* Deleting the context frees everything */
     327                 :         399 :     MemoryContextDelete(state->rs_cxt);
     328                 :         399 : }
     329                 :             : 
     330                 :             : /*
     331                 :             :  * Add a tuple to the new heap.
     332                 :             :  *
     333                 :             :  * Visibility information is copied from the original tuple, except that
     334                 :             :  * we "freeze" very-old tuples.  Note that since we scribble on new_tuple,
     335                 :             :  * it had better be temp storage not a pointer to the original tuple.
     336                 :             :  *
     337                 :             :  * state        opaque state as returned by begin_heap_rewrite
     338                 :             :  * old_tuple    original tuple in the old heap
     339                 :             :  * new_tuple    new, rewritten tuple to be inserted to new heap
     340                 :             :  */
     341                 :             : void
     342                 :      470610 : rewrite_heap_tuple(RewriteState state,
     343                 :             :                    HeapTuple old_tuple, HeapTuple new_tuple)
     344                 :             : {
     345                 :             :     MemoryContext old_cxt;
     346                 :             :     ItemPointerData old_tid;
     347                 :             :     TidHashKey  hashkey;
     348                 :             :     bool        found;
     349                 :             :     bool        free_new;
     350                 :             : 
     351                 :      470610 :     old_cxt = MemoryContextSwitchTo(state->rs_cxt);
     352                 :             : 
     353                 :             :     /*
     354                 :             :      * Copy the original tuple's visibility information into new_tuple.
     355                 :             :      *
     356                 :             :      * XXX we might later need to copy some t_infomask2 bits, too? Right now,
     357                 :             :      * we intentionally clear the HOT status bits.
     358                 :             :      */
     359                 :      470610 :     memcpy(&new_tuple->t_data->t_choice.t_heap,
     360                 :      470610 :            &old_tuple->t_data->t_choice.t_heap,
     361                 :             :            sizeof(HeapTupleFields));
     362                 :             : 
     363                 :      470610 :     new_tuple->t_data->t_infomask &= ~HEAP_XACT_MASK;
     364                 :      470610 :     new_tuple->t_data->t_infomask2 &= ~HEAP2_XACT_MASK;
     365                 :      470610 :     new_tuple->t_data->t_infomask |=
     366                 :      470610 :         old_tuple->t_data->t_infomask & HEAP_XACT_MASK;
     367                 :             : 
     368                 :             :     /*
     369                 :             :      * While we have our hands on the tuple, we may as well freeze any
     370                 :             :      * eligible xmin or xmax, so that future VACUUM effort can be saved.
     371                 :             :      */
     372                 :      470610 :     heap_freeze_tuple(new_tuple->t_data,
     373                 :      470610 :                       state->rs_old_rel->rd_rel->relfrozenxid,
     374                 :      470610 :                       state->rs_old_rel->rd_rel->relminmxid,
     375                 :             :                       state->rs_freeze_xid,
     376                 :             :                       state->rs_cutoff_multi);
     377                 :             : 
     378                 :             :     /*
     379                 :             :      * Invalid ctid means that ctid should point to the tuple itself. We'll
     380                 :             :      * override it later if the tuple is part of an update chain.
     381                 :             :      */
     382                 :      470610 :     ItemPointerSetInvalid(&new_tuple->t_data->t_ctid);
     383                 :             : 
     384                 :             :     /*
     385                 :             :      * If the tuple has been updated, check the old-to-new mapping hash table.
     386                 :             :      *
     387                 :             :      * Note that this check relies on the HeapTupleSatisfiesVacuum() in
     388                 :             :      * heapam_relation_copy_for_cluster() to have set hint bits.
     389                 :             :      */
     390   [ +  +  +  - ]:      495800 :     if (!((old_tuple->t_data->t_infomask & HEAP_XMAX_INVALID) ||
     391                 :       25190 :           HeapTupleHeaderIsOnlyLocked(old_tuple->t_data)) &&
     392         [ +  - ]:       25190 :         !HeapTupleHeaderIndicatesMovedPartitions(old_tuple->t_data) &&
     393         [ +  + ]:       25190 :         !(ItemPointerEquals(&(old_tuple->t_self),
     394                 :       25190 :                             &(old_tuple->t_data->t_ctid))))
     395                 :             :     {
     396                 :             :         OldToNewMapping mapping;
     397                 :             : 
     398                 :         438 :         memset(&hashkey, 0, sizeof(hashkey));
     399                 :         438 :         hashkey.xmin = HeapTupleHeaderGetUpdateXid(old_tuple->t_data);
     400                 :         438 :         hashkey.tid = old_tuple->t_data->t_ctid;
     401                 :             : 
     402                 :             :         mapping = (OldToNewMapping)
     403                 :         438 :             hash_search(state->rs_old_new_tid_map, &hashkey,
     404                 :             :                         HASH_FIND, NULL);
     405                 :             : 
     406         [ +  + ]:         438 :         if (mapping != NULL)
     407                 :             :         {
     408                 :             :             /*
     409                 :             :              * We've already copied the tuple that t_ctid points to, so we can
     410                 :             :              * set the ctid of this tuple to point to the new location, and
     411                 :             :              * insert it right away.
     412                 :             :              */
     413                 :         254 :             new_tuple->t_data->t_ctid = mapping->new_tid;
     414                 :             : 
     415                 :             :             /* We don't need the mapping entry anymore */
     416                 :         254 :             hash_search(state->rs_old_new_tid_map, &hashkey,
     417                 :             :                         HASH_REMOVE, &found);
     418                 :             :             Assert(found);
     419                 :             :         }
     420                 :             :         else
     421                 :             :         {
     422                 :             :             /*
     423                 :             :              * We haven't seen the tuple t_ctid points to yet. Stash this
     424                 :             :              * tuple into unresolved_tups to be written later.
     425                 :             :              */
     426                 :             :             UnresolvedTup unresolved;
     427                 :             : 
     428                 :         184 :             unresolved = hash_search(state->rs_unresolved_tups, &hashkey,
     429                 :             :                                      HASH_ENTER, &found);
     430                 :             :             Assert(!found);
     431                 :             : 
     432                 :         184 :             unresolved->old_tid = old_tuple->t_self;
     433                 :         184 :             unresolved->tuple = heap_copytuple(new_tuple);
     434                 :             : 
     435                 :             :             /*
     436                 :             :              * We can't do anything more now, since we don't know where the
     437                 :             :              * tuple will be written.
     438                 :             :              */
     439                 :         184 :             MemoryContextSwitchTo(old_cxt);
     440                 :         184 :             return;
     441                 :             :         }
     442                 :             :     }
     443                 :             : 
     444                 :             :     /*
     445                 :             :      * Now we will write the tuple, and then check to see if it is the B tuple
     446                 :             :      * in any new or known pair.  When we resolve a known pair, we will be
     447                 :             :      * able to write that pair's A tuple, and then we have to check if it
     448                 :             :      * resolves some other pair.  Hence, we need a loop here.
     449                 :             :      */
     450                 :      470426 :     old_tid = old_tuple->t_self;
     451                 :      470426 :     free_new = false;
     452                 :             : 
     453                 :             :     for (;;)
     454                 :         184 :     {
     455                 :             :         ItemPointerData new_tid;
     456                 :             : 
     457                 :             :         /* Insert the tuple and find out where it's put in new_heap */
     458                 :      470610 :         raw_heap_insert(state, new_tuple);
     459                 :      470610 :         new_tid = new_tuple->t_self;
     460                 :             : 
     461                 :      470610 :         logical_rewrite_heap_tuple(state, old_tid, new_tuple);
     462                 :             : 
     463                 :             :         /*
     464                 :             :          * If the tuple is the updated version of a row, and the prior version
     465                 :             :          * wouldn't be DEAD yet, then we need to either resolve the prior
     466                 :             :          * version (if it's waiting in rs_unresolved_tups), or make an entry
     467                 :             :          * in rs_old_new_tid_map (so we can resolve it when we do see it). The
     468                 :             :          * previous tuple's xmax would equal this one's xmin, so it's
     469                 :             :          * RECENTLY_DEAD if and only if the xmin is not before OldestXmin.
     470                 :             :          */
     471         [ +  + ]:      470610 :         if ((new_tuple->t_data->t_infomask & HEAP_UPDATED) &&
     472         [ +  + ]:       10839 :             !TransactionIdPrecedes(HeapTupleHeaderGetXmin(new_tuple->t_data),
     473                 :             :                                    state->rs_oldest_xmin))
     474                 :             :         {
     475                 :             :             /*
     476                 :             :              * Okay, this is B in an update pair.  See if we've seen A.
     477                 :             :              */
     478                 :             :             UnresolvedTup unresolved;
     479                 :             : 
     480                 :         438 :             memset(&hashkey, 0, sizeof(hashkey));
     481                 :         438 :             hashkey.xmin = HeapTupleHeaderGetXmin(new_tuple->t_data);
     482                 :         438 :             hashkey.tid = old_tid;
     483                 :             : 
     484                 :         438 :             unresolved = hash_search(state->rs_unresolved_tups, &hashkey,
     485                 :             :                                      HASH_FIND, NULL);
     486                 :             : 
     487         [ +  + ]:         438 :             if (unresolved != NULL)
     488                 :             :             {
     489                 :             :                 /*
     490                 :             :                  * We have seen and memorized the previous tuple already. Now
     491                 :             :                  * that we know where we inserted the tuple its t_ctid points
     492                 :             :                  * to, fix its t_ctid and insert it to the new heap.
     493                 :             :                  */
     494         [ +  + ]:         184 :                 if (free_new)
     495                 :          49 :                     heap_freetuple(new_tuple);
     496                 :         184 :                 new_tuple = unresolved->tuple;
     497                 :         184 :                 free_new = true;
     498                 :         184 :                 old_tid = unresolved->old_tid;
     499                 :         184 :                 new_tuple->t_data->t_ctid = new_tid;
     500                 :             : 
     501                 :             :                 /*
     502                 :             :                  * We don't need the hash entry anymore, but don't free its
     503                 :             :                  * tuple just yet.
     504                 :             :                  */
     505                 :         184 :                 hash_search(state->rs_unresolved_tups, &hashkey,
     506                 :             :                             HASH_REMOVE, &found);
     507                 :             :                 Assert(found);
     508                 :             : 
     509                 :             :                 /* loop back to insert the previous tuple in the chain */
     510                 :         184 :                 continue;
     511                 :             :             }
     512                 :             :             else
     513                 :             :             {
     514                 :             :                 /*
     515                 :             :                  * Remember the new tid of this tuple. We'll use it to set the
     516                 :             :                  * ctid when we find the previous tuple in the chain.
     517                 :             :                  */
     518                 :             :                 OldToNewMapping mapping;
     519                 :             : 
     520                 :         254 :                 mapping = hash_search(state->rs_old_new_tid_map, &hashkey,
     521                 :             :                                       HASH_ENTER, &found);
     522                 :             :                 Assert(!found);
     523                 :             : 
     524                 :         254 :                 mapping->new_tid = new_tid;
     525                 :             :             }
     526                 :             :         }
     527                 :             : 
     528                 :             :         /* Done with this (chain of) tuples, for now */
     529         [ +  + ]:      470426 :         if (free_new)
     530                 :         135 :             heap_freetuple(new_tuple);
     531                 :      470426 :         break;
     532                 :             :     }
     533                 :             : 
     534                 :      470426 :     MemoryContextSwitchTo(old_cxt);
     535                 :             : }
     536                 :             : 
     537                 :             : /*
     538                 :             :  * Register a dead tuple with an ongoing rewrite. Dead tuples are not
     539                 :             :  * copied to the new table, but we still make note of them so that we
     540                 :             :  * can release some resources earlier.
     541                 :             :  *
     542                 :             :  * Returns true if a tuple was removed from the unresolved_tups table.
     543                 :             :  * This indicates that that tuple, previously thought to be "recently dead",
     544                 :             :  * is now known really dead and won't be written to the output.
     545                 :             :  */
     546                 :             : bool
     547                 :       21372 : rewrite_heap_dead_tuple(RewriteState state, HeapTuple old_tuple)
     548                 :             : {
     549                 :             :     /*
     550                 :             :      * If we have already seen an earlier tuple in the update chain that
     551                 :             :      * points to this tuple, let's forget about that earlier tuple. It's in
     552                 :             :      * fact dead as well, our simple xmax < OldestXmin test in
     553                 :             :      * HeapTupleSatisfiesVacuum just wasn't enough to detect it. It happens
     554                 :             :      * when xmin of a tuple is greater than xmax, which sounds
     555                 :             :      * counter-intuitive but is perfectly valid.
     556                 :             :      *
     557                 :             :      * We don't bother to try to detect the situation the other way round,
     558                 :             :      * when we encounter the dead tuple first and then the recently dead one
     559                 :             :      * that points to it. If that happens, we'll have some unmatched entries
     560                 :             :      * in the UnresolvedTups hash table at the end. That can happen anyway,
     561                 :             :      * because a vacuum might have removed the dead tuple in the chain before
     562                 :             :      * us.
     563                 :             :      */
     564                 :             :     UnresolvedTup unresolved;
     565                 :             :     TidHashKey  hashkey;
     566                 :             :     bool        found;
     567                 :             : 
     568                 :       21372 :     memset(&hashkey, 0, sizeof(hashkey));
     569                 :       21372 :     hashkey.xmin = HeapTupleHeaderGetXmin(old_tuple->t_data);
     570                 :       21372 :     hashkey.tid = old_tuple->t_self;
     571                 :             : 
     572                 :       21372 :     unresolved = hash_search(state->rs_unresolved_tups, &hashkey,
     573                 :             :                              HASH_FIND, NULL);
     574                 :             : 
     575         [ -  + ]:       21372 :     if (unresolved != NULL)
     576                 :             :     {
     577                 :             :         /* Need to free the contained tuple as well as the hashtable entry */
     578                 :           0 :         heap_freetuple(unresolved->tuple);
     579                 :           0 :         hash_search(state->rs_unresolved_tups, &hashkey,
     580                 :             :                     HASH_REMOVE, &found);
     581                 :             :         Assert(found);
     582                 :           0 :         return true;
     583                 :             :     }
     584                 :             : 
     585                 :       21372 :     return false;
     586                 :             : }
     587                 :             : 
     588                 :             : /*
     589                 :             :  * Insert a tuple to the new relation.  This has to track heap_insert
     590                 :             :  * and its subsidiary functions!
     591                 :             :  *
     592                 :             :  * t_self of the tuple is set to the new TID of the tuple. If t_ctid of the
     593                 :             :  * tuple is invalid on entry, it's replaced with the new TID as well (in
     594                 :             :  * the inserted data only, not in the caller's copy).
     595                 :             :  */
     596                 :             : static void
     597                 :      470610 : raw_heap_insert(RewriteState state, HeapTuple tup)
     598                 :             : {
     599                 :             :     Page        page;
     600                 :             :     Size        pageFreeSpace,
     601                 :             :                 saveFreeSpace;
     602                 :             :     Size        len;
     603                 :             :     OffsetNumber newoff;
     604                 :             :     HeapTuple   heaptup;
     605                 :             : 
     606                 :             :     /*
     607                 :             :      * If the new tuple is too big for storage or contains already toasted
     608                 :             :      * out-of-line attributes from some other relation, invoke the toaster.
     609                 :             :      *
     610                 :             :      * Note: below this point, heaptup is the data we actually intend to store
     611                 :             :      * into the relation; tup is the caller's original untoasted data.
     612                 :             :      */
     613         [ -  + ]:      470610 :     if (state->rs_new_rel->rd_rel->relkind == RELKIND_TOASTVALUE)
     614                 :             :     {
     615                 :             :         /* toast table entries should never be recursively toasted */
     616                 :             :         Assert(!HeapTupleHasExternal(tup));
     617                 :           0 :         heaptup = tup;
     618                 :             :     }
     619   [ +  +  +  + ]:      470610 :     else if (HeapTupleHasExternal(tup) || tup->t_len > TOAST_TUPLE_THRESHOLD)
     620                 :         399 :     {
     621                 :         399 :         uint32      options = HEAP_INSERT_SKIP_FSM;
     622                 :             : 
     623                 :             :         /*
     624                 :             :          * While rewriting the heap for REPACK, make sure data for the TOAST
     625                 :             :          * table are not logically decoded.  The main heap is WAL-logged as
     626                 :             :          * XLOG FPI records, which are not logically decoded.
     627                 :             :          */
     628                 :         399 :         options |= HEAP_INSERT_NO_LOGICAL;
     629                 :             : 
     630                 :         399 :         heaptup = heap_toast_insert_or_update(state->rs_new_rel, tup, NULL,
     631                 :             :                                               options);
     632                 :             :     }
     633                 :             :     else
     634                 :      470211 :         heaptup = tup;
     635                 :             : 
     636                 :      470610 :     len = MAXALIGN(heaptup->t_len); /* be conservative */
     637                 :             : 
     638                 :             :     /*
     639                 :             :      * If we're gonna fail for oversize tuple, do it right away
     640                 :             :      */
     641         [ -  + ]:      470610 :     if (len > MaxHeapTupleSize)
     642         [ #  # ]:           0 :         ereport(ERROR,
     643                 :             :                 (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
     644                 :             :                  errmsg("row is too big: size %zu, maximum size %zu",
     645                 :             :                         len, MaxHeapTupleSize)));
     646                 :             : 
     647                 :             :     /* Compute desired extra freespace due to fillfactor option */
     648         [ +  + ]:      470610 :     saveFreeSpace = RelationGetTargetPageFreeSpace(state->rs_new_rel,
     649                 :             :                                                    HEAP_DEFAULT_FILLFACTOR);
     650                 :             : 
     651                 :             :     /* Now we can check to see if there's enough free space already. */
     652                 :      470610 :     page = (Page) state->rs_buffer;
     653         [ +  + ]:      470610 :     if (page)
     654                 :             :     {
     655                 :      470338 :         pageFreeSpace = PageGetHeapFreeSpace(page);
     656                 :             : 
     657         [ +  + ]:      470338 :         if (len + saveFreeSpace > pageFreeSpace)
     658                 :             :         {
     659                 :             :             /*
     660                 :             :              * Doesn't fit, so write out the existing page.  It always
     661                 :             :              * contains a tuple.  Hence, unlike RelationGetBufferForTuple(),
     662                 :             :              * enforce saveFreeSpace unconditionally.
     663                 :             :              */
     664                 :        6605 :             smgr_bulk_write(state->rs_bulkstate, state->rs_blockno, state->rs_buffer, true);
     665                 :        6605 :             state->rs_buffer = NULL;
     666                 :        6605 :             page = NULL;
     667                 :        6605 :             state->rs_blockno++;
     668                 :             :         }
     669                 :             :     }
     670                 :             : 
     671         [ +  + ]:      470610 :     if (!page)
     672                 :             :     {
     673                 :             :         /* Initialize a new empty page */
     674                 :        6877 :         state->rs_buffer = smgr_bulk_get_buf(state->rs_bulkstate);
     675                 :        6877 :         page = (Page) state->rs_buffer;
     676                 :        6877 :         PageInit(page, BLCKSZ, 0);
     677                 :             :     }
     678                 :             : 
     679                 :             :     /* And now we can insert the tuple into the page */
     680                 :      470610 :     newoff = PageAddItem(page, heaptup->t_data, heaptup->t_len, InvalidOffsetNumber, false, true);
     681         [ -  + ]:      470610 :     if (newoff == InvalidOffsetNumber)
     682         [ #  # ]:           0 :         elog(ERROR, "failed to add tuple");
     683                 :             : 
     684                 :             :     /* Update caller's t_self to the actual position where it was stored */
     685                 :      470610 :     ItemPointerSet(&(tup->t_self), state->rs_blockno, newoff);
     686                 :             : 
     687                 :             :     /*
     688                 :             :      * Insert the correct position into CTID of the stored tuple, too, if the
     689                 :             :      * caller didn't supply a valid CTID.
     690                 :             :      */
     691         [ +  + ]:      470610 :     if (!ItemPointerIsValid(&tup->t_data->t_ctid))
     692                 :             :     {
     693                 :             :         ItemId      newitemid;
     694                 :             :         HeapTupleHeader onpage_tup;
     695                 :             : 
     696                 :      470172 :         newitemid = PageGetItemId(page, newoff);
     697                 :      470172 :         onpage_tup = (HeapTupleHeader) PageGetItem(page, newitemid);
     698                 :             : 
     699                 :      470172 :         onpage_tup->t_ctid = tup->t_self;
     700                 :             :     }
     701                 :             : 
     702                 :             :     /* If heaptup is a private copy, release it. */
     703         [ +  + ]:      470610 :     if (heaptup != tup)
     704                 :         399 :         heap_freetuple(heaptup);
     705                 :      470610 : }
     706                 :             : 
     707                 :             : /* ------------------------------------------------------------------------
     708                 :             :  * Logical rewrite support
     709                 :             :  *
     710                 :             :  * When doing logical decoding - which relies on using cmin/cmax of catalog
     711                 :             :  * tuples, via xl_heap_new_cid records - heap rewrites have to log enough
     712                 :             :  * information to allow the decoding backend to update its internal mapping
     713                 :             :  * of (relfilelocator,ctid) => (cmin, cmax) to be correct for the rewritten heap.
     714                 :             :  *
     715                 :             :  * For that, every time we find a tuple that's been modified in a catalog
     716                 :             :  * relation within the xmin horizon of any decoding slot, we log a mapping
     717                 :             :  * from the old to the new location.
     718                 :             :  *
     719                 :             :  * To deal with rewrites that abort the filename of a mapping file contains
     720                 :             :  * the xid of the transaction performing the rewrite, which then can be
     721                 :             :  * checked before being read in.
     722                 :             :  *
     723                 :             :  * For efficiency we don't immediately spill every single map mapping for a
     724                 :             :  * row to disk but only do so in batches when we've collected several of them
     725                 :             :  * in memory or when end_heap_rewrite() has been called.
     726                 :             :  *
     727                 :             :  * Crash-Safety: This module diverts from the usual patterns of doing WAL
     728                 :             :  * since it cannot rely on checkpoint flushing out all buffers and thus
     729                 :             :  * waiting for exclusive locks on buffers. Usually the XLogInsert() covering
     730                 :             :  * buffer modifications is performed while the buffer(s) that are being
     731                 :             :  * modified are exclusively locked guaranteeing that both the WAL record and
     732                 :             :  * the modified heap are on either side of the checkpoint. But since the
     733                 :             :  * mapping files we log aren't in shared_buffers that interlock doesn't work.
     734                 :             :  *
     735                 :             :  * Instead we simply write the mapping files out to disk, *before* the
     736                 :             :  * XLogInsert() is performed. That guarantees that either the XLogInsert() is
     737                 :             :  * inserted after the checkpoint's redo pointer or that the checkpoint (via
     738                 :             :  * CheckPointLogicalRewriteHeap()) has flushed the (partial) mapping file to
     739                 :             :  * disk. That leaves the tail end that has not yet been flushed open to
     740                 :             :  * corruption, which is solved by including the current offset in the
     741                 :             :  * xl_heap_rewrite_mapping records and truncating the mapping file to it
     742                 :             :  * during replay. Every time a rewrite is finished all generated mapping files
     743                 :             :  * are synced to disk.
     744                 :             :  *
     745                 :             :  * Note that if we were only concerned about crash safety we wouldn't have to
     746                 :             :  * deal with WAL logging at all - an fsync() at the end of a rewrite would be
     747                 :             :  * sufficient for crash safety. Any mapping that hasn't been safely flushed to
     748                 :             :  * disk has to be by an aborted (explicitly or via a crash) transaction and is
     749                 :             :  * ignored by virtue of the xid in its name being subject to a
     750                 :             :  * TransactionDidCommit() check. But we want to support having standbys via
     751                 :             :  * physical replication, both for availability and to do logical decoding
     752                 :             :  * there.
     753                 :             :  * ------------------------------------------------------------------------
     754                 :             :  */
     755                 :             : 
     756                 :             : /*
     757                 :             :  * Do preparations for logging logical mappings during a rewrite if
     758                 :             :  * necessary. If we detect that we don't need to log anything we'll prevent
     759                 :             :  * any further action by the various logical rewrite functions.
     760                 :             :  */
     761                 :             : static void
     762                 :         399 : logical_begin_heap_rewrite(RewriteState state)
     763                 :             : {
     764                 :             :     HASHCTL     hash_ctl;
     765                 :             :     TransactionId logical_xmin;
     766                 :             : 
     767                 :             :     /*
     768                 :             :      * We only need to persist these mappings if the rewritten table can be
     769                 :             :      * accessed during logical decoding, if not, we can skip doing any
     770                 :             :      * additional work.
     771                 :             :      */
     772                 :         399 :     state->rs_logical_rewrite =
     773   [ +  +  -  +  :         399 :         RelationIsAccessibleInLogicalDecoding(state->rs_old_rel);
          +  -  -  +  -  
          -  -  -  +  +  
          -  +  -  -  -  
                -  -  - ]
     774                 :             : 
     775         [ +  + ]:         399 :     if (!state->rs_logical_rewrite)
     776                 :         379 :         return;
     777                 :             : 
     778                 :          22 :     ProcArrayGetReplicationSlotXmin(NULL, &logical_xmin);
     779                 :             : 
     780                 :             :     /*
     781                 :             :      * If there are no logical slots in progress we don't need to do anything,
     782                 :             :      * there cannot be any remappings for relevant rows yet. The relation's
     783                 :             :      * lock protects us against races.
     784                 :             :      */
     785         [ +  + ]:          22 :     if (logical_xmin == InvalidTransactionId)
     786                 :             :     {
     787                 :           2 :         state->rs_logical_rewrite = false;
     788                 :           2 :         return;
     789                 :             :     }
     790                 :             : 
     791                 :          20 :     state->rs_logical_xmin = logical_xmin;
     792                 :          20 :     state->rs_begin_lsn = GetXLogInsertRecPtr();
     793                 :          20 :     state->rs_num_rewrite_mappings = 0;
     794                 :             : 
     795                 :          20 :     hash_ctl.keysize = sizeof(TransactionId);
     796                 :          20 :     hash_ctl.entrysize = sizeof(RewriteMappingFile);
     797                 :          20 :     hash_ctl.hcxt = state->rs_cxt;
     798                 :             : 
     799                 :          20 :     state->rs_logical_mappings =
     800                 :          20 :         hash_create("Logical rewrite mapping",
     801                 :             :                     128,        /* arbitrary initial size */
     802                 :             :                     &hash_ctl,
     803                 :             :                     HASH_ELEM | HASH_BLOBS | HASH_CONTEXT);
     804                 :             : }
     805                 :             : 
     806                 :             : /*
     807                 :             :  * Flush all logical in-memory mappings to disk, but don't fsync them yet.
     808                 :             :  */
     809                 :             : static void
     810                 :           9 : logical_heap_rewrite_flush_mappings(RewriteState state)
     811                 :             : {
     812                 :             :     HASH_SEQ_STATUS seq_status;
     813                 :             :     RewriteMappingFile *src;
     814                 :             :     dlist_mutable_iter iter;
     815                 :             : 
     816                 :             :     Assert(state->rs_logical_rewrite);
     817                 :             : 
     818                 :             :     /* no logical rewrite in progress, no need to iterate over mappings */
     819         [ -  + ]:           9 :     if (state->rs_num_rewrite_mappings == 0)
     820                 :           0 :         return;
     821                 :             : 
     822         [ -  + ]:           9 :     elog(DEBUG1, "flushing %u logical rewrite mapping entries",
     823                 :             :          state->rs_num_rewrite_mappings);
     824                 :             : 
     825                 :           9 :     hash_seq_init(&seq_status, state->rs_logical_mappings);
     826         [ +  + ]:          99 :     while ((src = (RewriteMappingFile *) hash_seq_search(&seq_status)) != NULL)
     827                 :             :     {
     828                 :             :         char       *waldata;
     829                 :             :         char       *waldata_start;
     830                 :             :         xl_heap_rewrite_mapping xlrec;
     831                 :             :         Oid         dboid;
     832                 :             :         uint32      len;
     833                 :             :         int         written;
     834                 :          90 :         uint32      num_mappings = dclist_count(&src->mappings);
     835                 :             : 
     836                 :             :         /* this file hasn't got any new mappings */
     837         [ -  + ]:          90 :         if (num_mappings == 0)
     838                 :           0 :             continue;
     839                 :             : 
     840         [ -  + ]:          90 :         if (state->rs_old_rel->rd_rel->relisshared)
     841                 :           0 :             dboid = InvalidOid;
     842                 :             :         else
     843                 :          90 :             dboid = MyDatabaseId;
     844                 :             : 
     845                 :          90 :         xlrec.num_mappings = num_mappings;
     846                 :          90 :         xlrec.mapped_rel = RelationGetRelid(state->rs_old_rel);
     847                 :          90 :         xlrec.mapped_xid = src->xid;
     848                 :          90 :         xlrec.mapped_db = dboid;
     849                 :          90 :         xlrec.offset = src->off;
     850                 :          90 :         xlrec.start_lsn = state->rs_begin_lsn;
     851                 :             : 
     852                 :             :         /* write all mappings consecutively */
     853                 :          90 :         len = num_mappings * sizeof(LogicalRewriteMappingData);
     854                 :          90 :         waldata_start = waldata = palloc(len);
     855                 :             : 
     856                 :             :         /*
     857                 :             :          * collect data we need to write out, but don't modify ondisk data yet
     858                 :             :          */
     859   [ +  -  +  + ]:         813 :         dclist_foreach_modify(iter, &src->mappings)
     860                 :             :         {
     861                 :             :             RewriteMappingDataEntry *pmap;
     862                 :             : 
     863                 :         723 :             pmap = dclist_container(RewriteMappingDataEntry, node, iter.cur);
     864                 :             : 
     865                 :         723 :             memcpy(waldata, &pmap->map, sizeof(pmap->map));
     866                 :         723 :             waldata += sizeof(pmap->map);
     867                 :             : 
     868                 :             :             /* remove from the list and free */
     869                 :         723 :             dclist_delete_from(&src->mappings, &pmap->node);
     870                 :         723 :             pfree(pmap);
     871                 :             : 
     872                 :             :             /* update bookkeeping */
     873                 :         723 :             state->rs_num_rewrite_mappings--;
     874                 :             :         }
     875                 :             : 
     876                 :             :         Assert(dclist_count(&src->mappings) == 0);
     877                 :             :         Assert(waldata == waldata_start + len);
     878                 :             : 
     879                 :             :         /*
     880                 :             :          * Note that we deviate from the usual WAL coding practices here,
     881                 :             :          * check the above "Logical rewrite support" comment for reasoning.
     882                 :             :          */
     883                 :          90 :         written = FileWrite(src->vfd, waldata_start, len, src->off,
     884                 :             :                             WAIT_EVENT_LOGICAL_REWRITE_WRITE);
     885         [ -  + ]:          90 :         if (written != len)
     886         [ #  # ]:           0 :             ereport(ERROR,
     887                 :             :                     (errcode_for_file_access(),
     888                 :             :                      errmsg("could not write to file \"%s\", wrote %d of %d: %m", src->path,
     889                 :             :                             written, len)));
     890                 :          90 :         src->off += len;
     891                 :             : 
     892                 :          90 :         XLogBeginInsert();
     893                 :          90 :         XLogRegisterData(&xlrec, sizeof(xlrec));
     894                 :          90 :         XLogRegisterData(waldata_start, len);
     895                 :             : 
     896                 :             :         /* write xlog record */
     897                 :          90 :         XLogInsert(RM_HEAP2_ID, XLOG_HEAP2_REWRITE);
     898                 :             : 
     899                 :          90 :         pfree(waldata_start);
     900                 :             :     }
     901                 :             :     Assert(state->rs_num_rewrite_mappings == 0);
     902                 :             : }
     903                 :             : 
     904                 :             : /*
     905                 :             :  * Logical remapping part of end_heap_rewrite().
     906                 :             :  */
     907                 :             : static void
     908                 :         399 : logical_end_heap_rewrite(RewriteState state)
     909                 :             : {
     910                 :             :     HASH_SEQ_STATUS seq_status;
     911                 :             :     RewriteMappingFile *src;
     912                 :             : 
     913                 :             :     /* done, no logical rewrite in progress */
     914         [ +  + ]:         399 :     if (!state->rs_logical_rewrite)
     915                 :         379 :         return;
     916                 :             : 
     917                 :             :     /* writeout remaining in-memory entries */
     918         [ +  + ]:          20 :     if (state->rs_num_rewrite_mappings > 0)
     919                 :           9 :         logical_heap_rewrite_flush_mappings(state);
     920                 :             : 
     921                 :             :     /* Iterate over all mappings we have written and fsync the files. */
     922                 :          20 :     hash_seq_init(&seq_status, state->rs_logical_mappings);
     923         [ +  + ]:         110 :     while ((src = (RewriteMappingFile *) hash_seq_search(&seq_status)) != NULL)
     924                 :             :     {
     925         [ -  + ]:          90 :         if (FileSync(src->vfd, WAIT_EVENT_LOGICAL_REWRITE_SYNC) != 0)
     926         [ #  # ]:           0 :             ereport(data_sync_elevel(ERROR),
     927                 :             :                     (errcode_for_file_access(),
     928                 :             :                      errmsg("could not fsync file \"%s\": %m", src->path)));
     929                 :          90 :         FileClose(src->vfd);
     930                 :             :     }
     931                 :             :     /* memory context cleanup will deal with the rest */
     932                 :             : }
     933                 :             : 
     934                 :             : /*
     935                 :             :  * Log a single (old->new) mapping for 'xid'.
     936                 :             :  */
     937                 :             : static void
     938                 :         723 : logical_rewrite_log_mapping(RewriteState state, TransactionId xid,
     939                 :             :                             LogicalRewriteMappingData *map)
     940                 :             : {
     941                 :             :     RewriteMappingFile *src;
     942                 :             :     RewriteMappingDataEntry *pmap;
     943                 :             :     Oid         relid;
     944                 :             :     bool        found;
     945                 :             : 
     946                 :         723 :     relid = RelationGetRelid(state->rs_old_rel);
     947                 :             : 
     948                 :             :     /* look for existing mappings for this 'mapped' xid */
     949                 :         723 :     src = hash_search(state->rs_logical_mappings, &xid,
     950                 :             :                       HASH_ENTER, &found);
     951                 :             : 
     952                 :             :     /*
     953                 :             :      * We haven't yet had the need to map anything for this xid, create
     954                 :             :      * per-xid data structures.
     955                 :             :      */
     956         [ +  + ]:         723 :     if (!found)
     957                 :             :     {
     958                 :             :         char        path[MAXPGPATH];
     959                 :             :         Oid         dboid;
     960                 :             : 
     961         [ -  + ]:          90 :         if (state->rs_old_rel->rd_rel->relisshared)
     962                 :           0 :             dboid = InvalidOid;
     963                 :             :         else
     964                 :          90 :             dboid = MyDatabaseId;
     965                 :             : 
     966                 :          90 :         snprintf(path, MAXPGPATH,
     967                 :             :                  "%s/" LOGICAL_REWRITE_FORMAT,
     968                 :             :                  PG_LOGICAL_MAPPINGS_DIR, dboid, relid,
     969                 :          90 :                  LSN_FORMAT_ARGS(state->rs_begin_lsn),
     970                 :             :                  xid, GetCurrentTransactionId());
     971                 :             : 
     972                 :          90 :         dclist_init(&src->mappings);
     973                 :          90 :         src->off = 0;
     974                 :          90 :         memcpy(src->path, path, sizeof(path));
     975                 :          90 :         src->vfd = PathNameOpenFile(path,
     976                 :             :                                     O_CREAT | O_EXCL | O_WRONLY | PG_BINARY);
     977         [ -  + ]:          90 :         if (src->vfd < 0)
     978         [ #  # ]:           0 :             ereport(ERROR,
     979                 :             :                     (errcode_for_file_access(),
     980                 :             :                      errmsg("could not create file \"%s\": %m", path)));
     981                 :             :     }
     982                 :             : 
     983                 :         723 :     pmap = MemoryContextAlloc(state->rs_cxt,
     984                 :             :                               sizeof(RewriteMappingDataEntry));
     985                 :         723 :     memcpy(&pmap->map, map, sizeof(LogicalRewriteMappingData));
     986                 :         723 :     dclist_push_tail(&src->mappings, &pmap->node);
     987                 :         723 :     state->rs_num_rewrite_mappings++;
     988                 :             : 
     989                 :             :     /*
     990                 :             :      * Write out buffer every time we've too many in-memory entries across all
     991                 :             :      * mapping files.
     992                 :             :      */
     993         [ -  + ]:         723 :     if (state->rs_num_rewrite_mappings >= 1000 /* arbitrary number */ )
     994                 :           0 :         logical_heap_rewrite_flush_mappings(state);
     995                 :         723 : }
     996                 :             : 
     997                 :             : /*
     998                 :             :  * Perform logical remapping for a tuple that's mapped from old_tid to
     999                 :             :  * new_tuple->t_self by rewrite_heap_tuple() if necessary for the tuple.
    1000                 :             :  */
    1001                 :             : static void
    1002                 :      470610 : logical_rewrite_heap_tuple(RewriteState state, ItemPointerData old_tid,
    1003                 :             :                            HeapTuple new_tuple)
    1004                 :             : {
    1005                 :      470610 :     ItemPointerData new_tid = new_tuple->t_self;
    1006                 :      470610 :     TransactionId cutoff = state->rs_logical_xmin;
    1007                 :             :     TransactionId xmin;
    1008                 :             :     TransactionId xmax;
    1009                 :      470610 :     bool        do_log_xmin = false;
    1010                 :      470610 :     bool        do_log_xmax = false;
    1011                 :             :     LogicalRewriteMappingData map;
    1012                 :             : 
    1013                 :             :     /* no logical rewrite in progress, we don't need to log anything */
    1014         [ +  + ]:      470610 :     if (!state->rs_logical_rewrite)
    1015                 :      469902 :         return;
    1016                 :             : 
    1017                 :       27133 :     xmin = HeapTupleHeaderGetXmin(new_tuple->t_data);
    1018                 :             :     /* use *GetUpdateXid to correctly deal with multixacts */
    1019                 :       27133 :     xmax = HeapTupleHeaderGetUpdateXid(new_tuple->t_data);
    1020                 :             : 
    1021                 :             :     /*
    1022                 :             :      * Log the mapping iff the tuple has been created recently.
    1023                 :             :      */
    1024   [ +  +  +  - ]:       27133 :     if (TransactionIdIsNormal(xmin) && !TransactionIdPrecedes(xmin, cutoff))
    1025                 :         535 :         do_log_xmin = true;
    1026                 :             : 
    1027         [ +  + ]:       27133 :     if (!TransactionIdIsNormal(xmax))
    1028                 :             :     {
    1029                 :             :         /*
    1030                 :             :          * no xmax is set, can't have any permanent ones, so this check is
    1031                 :             :          * sufficient
    1032                 :             :          */
    1033                 :             :     }
    1034         [ +  - ]:         501 :     else if (HEAP_XMAX_IS_LOCKED_ONLY(new_tuple->t_data->t_infomask))
    1035                 :             :     {
    1036                 :             :         /* only locked, we don't care */
    1037                 :             :     }
    1038         [ +  - ]:         501 :     else if (!TransactionIdPrecedes(xmax, cutoff))
    1039                 :             :     {
    1040                 :             :         /* tuple has been deleted recently, log */
    1041                 :         501 :         do_log_xmax = true;
    1042                 :             :     }
    1043                 :             : 
    1044                 :             :     /* if neither needs to be logged, we're done */
    1045   [ +  +  +  + ]:       27133 :     if (!do_log_xmin && !do_log_xmax)
    1046                 :       26425 :         return;
    1047                 :             : 
    1048                 :             :     /* fill out mapping information */
    1049                 :         708 :     map.old_locator = state->rs_old_rel->rd_locator;
    1050                 :         708 :     map.old_tid = old_tid;
    1051                 :         708 :     map.new_locator = state->rs_new_rel->rd_locator;
    1052                 :         708 :     map.new_tid = new_tid;
    1053                 :             : 
    1054                 :             :     /* ---
    1055                 :             :      * Now persist the mapping for the individual xids that are affected. We
    1056                 :             :      * need to log for both xmin and xmax if they aren't the same transaction
    1057                 :             :      * since the mapping files are per "affected" xid.
    1058                 :             :      * We don't muster all that much effort detecting whether xmin and xmax
    1059                 :             :      * are actually the same transaction, we just check whether the xid is the
    1060                 :             :      * same disregarding subtransactions. Logging too much is relatively
    1061                 :             :      * harmless and we could never do the check fully since subtransaction
    1062                 :             :      * data is thrown away during restarts.
    1063                 :             :      * ---
    1064                 :             :      */
    1065         [ +  + ]:         708 :     if (do_log_xmin)
    1066                 :         535 :         logical_rewrite_log_mapping(state, xmin, &map);
    1067                 :             :     /* separately log mapping for xmax unless it'd be redundant */
    1068   [ +  +  +  + ]:         708 :     if (do_log_xmax && !TransactionIdEquals(xmin, xmax))
    1069                 :         188 :         logical_rewrite_log_mapping(state, xmax, &map);
    1070                 :             : }
    1071                 :             : 
    1072                 :             : /*
    1073                 :             :  * Replay XLOG_HEAP2_REWRITE records
    1074                 :             :  */
    1075                 :             : void
    1076                 :           0 : heap_xlog_logical_rewrite(XLogReaderState *r)
    1077                 :             : {
    1078                 :             :     char        path[MAXPGPATH];
    1079                 :             :     int         fd;
    1080                 :             :     xl_heap_rewrite_mapping *xlrec;
    1081                 :             :     uint32      len;
    1082                 :             :     char       *data;
    1083                 :             : 
    1084                 :           0 :     xlrec = (xl_heap_rewrite_mapping *) XLogRecGetData(r);
    1085                 :             : 
    1086                 :           0 :     snprintf(path, MAXPGPATH,
    1087                 :             :              "%s/" LOGICAL_REWRITE_FORMAT,
    1088                 :             :              PG_LOGICAL_MAPPINGS_DIR, xlrec->mapped_db, xlrec->mapped_rel,
    1089                 :           0 :              LSN_FORMAT_ARGS(xlrec->start_lsn),
    1090                 :           0 :              xlrec->mapped_xid, XLogRecGetXid(r));
    1091                 :             : 
    1092                 :           0 :     fd = OpenTransientFile(path,
    1093                 :             :                            O_CREAT | O_WRONLY | PG_BINARY);
    1094         [ #  # ]:           0 :     if (fd < 0)
    1095         [ #  # ]:           0 :         ereport(ERROR,
    1096                 :             :                 (errcode_for_file_access(),
    1097                 :             :                  errmsg("could not create file \"%s\": %m", path)));
    1098                 :             : 
    1099                 :             :     /*
    1100                 :             :      * Truncate all data that's not guaranteed to have been safely fsynced (by
    1101                 :             :      * previous record or by the last checkpoint).
    1102                 :             :      */
    1103                 :           0 :     pgstat_report_wait_start(WAIT_EVENT_LOGICAL_REWRITE_TRUNCATE);
    1104         [ #  # ]:           0 :     if (ftruncate(fd, xlrec->offset) != 0)
    1105         [ #  # ]:           0 :         ereport(ERROR,
    1106                 :             :                 (errcode_for_file_access(),
    1107                 :             :                  errmsg("could not truncate file \"%s\" to %u: %m",
    1108                 :             :                         path, (uint32) xlrec->offset)));
    1109                 :           0 :     pgstat_report_wait_end();
    1110                 :             : 
    1111                 :           0 :     data = XLogRecGetData(r) + sizeof(*xlrec);
    1112                 :             : 
    1113                 :           0 :     len = xlrec->num_mappings * sizeof(LogicalRewriteMappingData);
    1114                 :             : 
    1115                 :             :     /* write out tail end of mapping file (again) */
    1116                 :           0 :     errno = 0;
    1117                 :           0 :     pgstat_report_wait_start(WAIT_EVENT_LOGICAL_REWRITE_MAPPING_WRITE);
    1118         [ #  # ]:           0 :     if (pg_pwrite(fd, data, len, xlrec->offset) != len)
    1119                 :             :     {
    1120                 :             :         /* if write didn't set errno, assume problem is no disk space */
    1121         [ #  # ]:           0 :         if (errno == 0)
    1122                 :           0 :             errno = ENOSPC;
    1123         [ #  # ]:           0 :         ereport(ERROR,
    1124                 :             :                 (errcode_for_file_access(),
    1125                 :             :                  errmsg("could not write to file \"%s\": %m", path)));
    1126                 :             :     }
    1127                 :           0 :     pgstat_report_wait_end();
    1128                 :             : 
    1129                 :             :     /*
    1130                 :             :      * Now fsync all previously written data. We could improve things and only
    1131                 :             :      * do this for the last write to a file, but the required bookkeeping
    1132                 :             :      * doesn't seem worth the trouble.
    1133                 :             :      */
    1134                 :           0 :     pgstat_report_wait_start(WAIT_EVENT_LOGICAL_REWRITE_MAPPING_SYNC);
    1135         [ #  # ]:           0 :     if (pg_fsync(fd) != 0)
    1136         [ #  # ]:           0 :         ereport(data_sync_elevel(ERROR),
    1137                 :             :                 (errcode_for_file_access(),
    1138                 :             :                  errmsg("could not fsync file \"%s\": %m", path)));
    1139                 :           0 :     pgstat_report_wait_end();
    1140                 :             : 
    1141         [ #  # ]:           0 :     if (CloseTransientFile(fd) != 0)
    1142         [ #  # ]:           0 :         ereport(ERROR,
    1143                 :             :                 (errcode_for_file_access(),
    1144                 :             :                  errmsg("could not close file \"%s\": %m", path)));
    1145                 :           0 : }
    1146                 :             : 
    1147                 :             : /* ---
    1148                 :             :  * Perform a checkpoint for logical rewrite mappings
    1149                 :             :  *
    1150                 :             :  * This serves two tasks:
    1151                 :             :  * 1) Remove all mappings not needed anymore based on the logical restart LSN
    1152                 :             :  * 2) Flush all remaining mappings to disk, so that replay after a checkpoint
    1153                 :             :  *    only has to deal with the parts of a mapping that have been written out
    1154                 :             :  *    after the checkpoint started.
    1155                 :             :  * ---
    1156                 :             :  */
    1157                 :             : void
    1158                 :        1944 : CheckPointLogicalRewriteHeap(void)
    1159                 :             : {
    1160                 :             :     XLogRecPtr  cutoff;
    1161                 :             :     XLogRecPtr  redo;
    1162                 :             :     DIR        *mappings_dir;
    1163                 :             :     struct dirent *mapping_de;
    1164                 :             :     char        path[MAXPGPATH + sizeof(PG_LOGICAL_MAPPINGS_DIR)];
    1165                 :             : 
    1166                 :             :     /*
    1167                 :             :      * We start of with a minimum of the last redo pointer. No new decoding
    1168                 :             :      * slot will start before that, so that's a safe upper bound for removal.
    1169                 :             :      */
    1170                 :        1944 :     redo = GetRedoRecPtr();
    1171                 :             : 
    1172                 :             :     /* now check for the restart ptrs from existing slots */
    1173                 :        1944 :     cutoff = ReplicationSlotsComputeLogicalRestartLSN();
    1174                 :             : 
    1175                 :             :     /* don't start earlier than the restart lsn */
    1176   [ +  +  +  + ]:        1944 :     if (XLogRecPtrIsValid(cutoff) && redo < cutoff)
    1177                 :           1 :         cutoff = redo;
    1178                 :             : 
    1179                 :        1944 :     mappings_dir = AllocateDir(PG_LOGICAL_MAPPINGS_DIR);
    1180         [ +  + ]:        6012 :     while ((mapping_de = ReadDir(mappings_dir, PG_LOGICAL_MAPPINGS_DIR)) != NULL)
    1181                 :             :     {
    1182                 :             :         Oid         dboid;
    1183                 :             :         Oid         relid;
    1184                 :             :         XLogRecPtr  lsn;
    1185                 :             :         TransactionId rewrite_xid;
    1186                 :             :         TransactionId create_xid;
    1187                 :             :         uint32      hi,
    1188                 :             :                     lo;
    1189                 :             :         PGFileType  de_type;
    1190                 :             : 
    1191         [ +  + ]:        4068 :         if (strcmp(mapping_de->d_name, ".") == 0 ||
    1192         [ +  + ]:        2124 :             strcmp(mapping_de->d_name, "..") == 0)
    1193                 :        3888 :             continue;
    1194                 :             : 
    1195                 :         180 :         snprintf(path, sizeof(path), "%s/%s", PG_LOGICAL_MAPPINGS_DIR, mapping_de->d_name);
    1196                 :         180 :         de_type = get_dirent_type(path, mapping_de, false, DEBUG1);
    1197                 :             : 
    1198   [ +  -  -  + ]:         180 :         if (de_type != PGFILETYPE_ERROR && de_type != PGFILETYPE_REG)
    1199                 :           0 :             continue;
    1200                 :             : 
    1201                 :             :         /* Skip over files that cannot be ours. */
    1202         [ -  + ]:         180 :         if (strncmp(mapping_de->d_name, "map-", 4) != 0)
    1203                 :           0 :             continue;
    1204                 :             : 
    1205         [ -  + ]:         180 :         if (sscanf(mapping_de->d_name, LOGICAL_REWRITE_FORMAT,
    1206                 :             :                    &dboid, &relid, &hi, &lo, &rewrite_xid, &create_xid) != 6)
    1207         [ #  # ]:           0 :             elog(ERROR, "could not parse filename \"%s\"", mapping_de->d_name);
    1208                 :             : 
    1209                 :         180 :         lsn = ((uint64) hi) << 32 | lo;
    1210                 :             : 
    1211   [ +  +  -  + ]:         180 :         if (lsn < cutoff || !XLogRecPtrIsValid(cutoff))
    1212                 :             :         {
    1213         [ -  + ]:          90 :             elog(DEBUG1, "removing logical rewrite file \"%s\"", path);
    1214         [ -  + ]:          90 :             if (unlink(path) < 0)
    1215         [ #  # ]:           0 :                 ereport(ERROR,
    1216                 :             :                         (errcode_for_file_access(),
    1217                 :             :                          errmsg("could not remove file \"%s\": %m", path)));
    1218                 :             :         }
    1219                 :             :         else
    1220                 :             :         {
    1221                 :             :             /* on some operating systems fsyncing a file requires O_RDWR */
    1222                 :          90 :             int         fd = OpenTransientFile(path, O_RDWR | PG_BINARY);
    1223                 :             : 
    1224                 :             :             /*
    1225                 :             :              * The file cannot vanish due to concurrency since this function
    1226                 :             :              * is the only one removing logical mappings and only one
    1227                 :             :              * checkpoint can be in progress at a time.
    1228                 :             :              */
    1229         [ -  + ]:          90 :             if (fd < 0)
    1230         [ #  # ]:           0 :                 ereport(ERROR,
    1231                 :             :                         (errcode_for_file_access(),
    1232                 :             :                          errmsg("could not open file \"%s\": %m", path)));
    1233                 :             : 
    1234                 :             :             /*
    1235                 :             :              * We could try to avoid fsyncing files that either haven't
    1236                 :             :              * changed or have only been created since the checkpoint's start,
    1237                 :             :              * but it's currently not deemed worth the effort.
    1238                 :             :              */
    1239                 :          90 :             pgstat_report_wait_start(WAIT_EVENT_LOGICAL_REWRITE_CHECKPOINT_SYNC);
    1240         [ -  + ]:          90 :             if (pg_fsync(fd) != 0)
    1241         [ #  # ]:           0 :                 ereport(data_sync_elevel(ERROR),
    1242                 :             :                         (errcode_for_file_access(),
    1243                 :             :                          errmsg("could not fsync file \"%s\": %m", path)));
    1244                 :          90 :             pgstat_report_wait_end();
    1245                 :             : 
    1246         [ -  + ]:          90 :             if (CloseTransientFile(fd) != 0)
    1247         [ #  # ]:           0 :                 ereport(ERROR,
    1248                 :             :                         (errcode_for_file_access(),
    1249                 :             :                          errmsg("could not close file \"%s\": %m", path)));
    1250                 :             :         }
    1251                 :             :     }
    1252                 :        1944 :     FreeDir(mappings_dir);
    1253                 :             : 
    1254                 :             :     /* persist directory entries to disk */
    1255                 :        1944 :     fsync_fname(PG_LOGICAL_MAPPINGS_DIR, true);
    1256                 :        1944 : }
        

Generated by: LCOV version 2.0-1