LCOV - code coverage report
Current view: top level - src/backend/access/heap - rewriteheap.c (source / functions) Coverage Total Hit
Test: Code coverage Lines: 53.7 % 423 227
Test Date: 2026-01-26 10:56:24 Functions: 75.0 % 12 9
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 24.0 % 275 66

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

Generated by: LCOV version 2.3.2-1