LCOV - differential code coverage report
Current view: top level - src/backend/access/heap - rewriteheap.c (source / functions) Coverage Total Hit UBC GNC CBC DCB
Current: Differential Code Coverage 16@8cea358b128 vs 17@8cea358b128 Lines: 83.2 % 321 267 54 14 253 19
Current Date: 2024-04-14 14:21:10 Functions: 91.7 % 12 11 1 3 8
Baseline: 16@8cea358b128 Branches: 55.4 % 242 134 108 6 128
Baseline Date: 2024-04-14 14:21:09 Line coverage date bins:
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed [..60] days: 100.0 % 15 15 14 1
(240..) days: 82.4 % 306 252 54 252
Function coverage date bins:
(240..) days: 91.7 % 12 11 1 3 8
Branch coverage date bins:
[..60] days: 100.0 % 8 8 6 2
(240..) days: 53.8 % 234 126 108 126

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

Generated by: LCOV version 2.1-beta2-3-g6141622