LCOV - code coverage report
Current view: top level - src/backend/catalog - storage.c (source / functions) Coverage Total Hit
Test: Code coverage Lines: 80.8 % 447 361
Test Date: 2026-01-26 10:56:24 Functions: 89.5 % 19 17
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 65.5 % 226 148

             Branch data     Line data    Source code
       1                 :             : /*-------------------------------------------------------------------------
       2                 :             :  *
       3                 :             :  * storage.c
       4                 :             :  *        code to create and destroy physical storage for relations
       5                 :             :  *
       6                 :             :  * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
       7                 :             :  * Portions Copyright (c) 1994, Regents of the University of California
       8                 :             :  *
       9                 :             :  *
      10                 :             :  * IDENTIFICATION
      11                 :             :  *        src/backend/catalog/storage.c
      12                 :             :  *
      13                 :             :  * NOTES
      14                 :             :  *        Some of this code used to be in storage/smgr/smgr.c, and the
      15                 :             :  *        function names still reflect that.
      16                 :             :  *
      17                 :             :  *-------------------------------------------------------------------------
      18                 :             :  */
      19                 :             : 
      20                 :             : #include "postgres.h"
      21                 :             : 
      22                 :             : #include "access/visibilitymap.h"
      23                 :             : #include "access/xact.h"
      24                 :             : #include "access/xlog.h"
      25                 :             : #include "access/xloginsert.h"
      26                 :             : #include "access/xlogutils.h"
      27                 :             : #include "catalog/storage.h"
      28                 :             : #include "catalog/storage_xlog.h"
      29                 :             : #include "miscadmin.h"
      30                 :             : #include "pgstat.h"
      31                 :             : #include "storage/bulk_write.h"
      32                 :             : #include "storage/freespace.h"
      33                 :             : #include "storage/proc.h"
      34                 :             : #include "storage/smgr.h"
      35                 :             : #include "utils/hsearch.h"
      36                 :             : #include "utils/memutils.h"
      37                 :             : #include "utils/rel.h"
      38                 :             : 
      39                 :             : /* GUC variables */
      40                 :             : int                     wal_skip_threshold = 2048;      /* in kilobytes */
      41                 :             : 
      42                 :             : /*
      43                 :             :  * We keep a list of all relations (represented as RelFileLocator values)
      44                 :             :  * that have been created or deleted in the current transaction.  When
      45                 :             :  * a relation is created, we create the physical file immediately, but
      46                 :             :  * remember it so that we can delete the file again if the current
      47                 :             :  * transaction is aborted.  Conversely, a deletion request is NOT
      48                 :             :  * executed immediately, but is just entered in the list.  When and if
      49                 :             :  * the transaction commits, we can delete the physical file.
      50                 :             :  *
      51                 :             :  * To handle subtransactions, every entry is marked with its transaction
      52                 :             :  * nesting level.  At subtransaction commit, we reassign the subtransaction's
      53                 :             :  * entries to the parent nesting level.  At subtransaction abort, we can
      54                 :             :  * immediately execute the abort-time actions for all entries of the current
      55                 :             :  * nesting level.
      56                 :             :  *
      57                 :             :  * NOTE: the list is kept in TopMemoryContext to be sure it won't disappear
      58                 :             :  * unbetimes.  It'd probably be OK to keep it in TopTransactionContext,
      59                 :             :  * but I'm being paranoid.
      60                 :             :  */
      61                 :             : 
      62                 :             : typedef struct PendingRelDelete
      63                 :             : {
      64                 :             :         RelFileLocator rlocator;        /* relation that may need to be deleted */
      65                 :             :         ProcNumber      procNumber;             /* INVALID_PROC_NUMBER if not a temp rel */
      66                 :             :         bool            atCommit;               /* T=delete at commit; F=delete at abort */
      67                 :             :         int                     nestLevel;              /* xact nesting level of request */
      68                 :             :         struct PendingRelDelete *next;  /* linked-list link */
      69                 :             : } PendingRelDelete;
      70                 :             : 
      71                 :             : typedef struct PendingRelSync
      72                 :             : {
      73                 :             :         RelFileLocator rlocator;
      74                 :             :         bool            is_truncated;   /* Has the file experienced truncation? */
      75                 :             : } PendingRelSync;
      76                 :             : 
      77                 :             : static PendingRelDelete *pendingDeletes = NULL; /* head of linked list */
      78                 :             : static HTAB *pendingSyncHash = NULL;
      79                 :             : 
      80                 :             : 
      81                 :             : /*
      82                 :             :  * AddPendingSync
      83                 :             :  *              Queue an at-commit fsync.
      84                 :             :  */
      85                 :             : static void
      86                 :       11657 : AddPendingSync(const RelFileLocator *rlocator)
      87                 :             : {
      88                 :       11657 :         PendingRelSync *pending;
      89                 :       11657 :         bool            found;
      90                 :             : 
      91                 :             :         /* create the hash if not yet */
      92         [ +  + ]:       11657 :         if (!pendingSyncHash)
      93                 :             :         {
      94                 :        4828 :                 HASHCTL         ctl;
      95                 :             : 
      96                 :        4828 :                 ctl.keysize = sizeof(RelFileLocator);
      97                 :        4828 :                 ctl.entrysize = sizeof(PendingRelSync);
      98                 :        4828 :                 ctl.hcxt = TopTransactionContext;
      99                 :        4828 :                 pendingSyncHash = hash_create("pending sync hash", 16, &ctl,
     100                 :             :                                                                           HASH_ELEM | HASH_BLOBS | HASH_CONTEXT);
     101                 :        4828 :         }
     102                 :             : 
     103                 :       11657 :         pending = hash_search(pendingSyncHash, rlocator, HASH_ENTER, &found);
     104         [ +  - ]:       11657 :         Assert(!found);
     105                 :       11657 :         pending->is_truncated = false;
     106                 :       11657 : }
     107                 :             : 
     108                 :             : /*
     109                 :             :  * RelationCreateStorage
     110                 :             :  *              Create physical storage for a relation.
     111                 :             :  *
     112                 :             :  * Create the underlying disk file storage for the relation. This only
     113                 :             :  * creates the main fork; additional forks are created lazily by the
     114                 :             :  * modules that need them.
     115                 :             :  *
     116                 :             :  * This function is transactional. The creation is WAL-logged, and if the
     117                 :             :  * transaction aborts later on, the storage will be destroyed.  A caller
     118                 :             :  * that does not want the storage to be destroyed in case of an abort may
     119                 :             :  * pass register_delete = false.
     120                 :             :  */
     121                 :             : SMgrRelation
     122                 :       11453 : RelationCreateStorage(RelFileLocator rlocator, char relpersistence,
     123                 :             :                                           bool register_delete)
     124                 :             : {
     125                 :       11453 :         SMgrRelation srel;
     126                 :       11453 :         ProcNumber      procNumber;
     127                 :       11453 :         bool            needs_wal;
     128                 :             : 
     129         [ +  - ]:       11453 :         Assert(!IsInParallelMode());    /* couldn't update pendingSyncHash */
     130                 :             : 
     131   [ +  +  +  - ]:       11453 :         switch (relpersistence)
     132                 :             :         {
     133                 :             :                 case RELPERSISTENCE_TEMP:
     134         [ -  + ]:        1012 :                         procNumber = ProcNumberForTempRelations();
     135                 :        1012 :                         needs_wal = false;
     136                 :        1012 :                         break;
     137                 :             :                 case RELPERSISTENCE_UNLOGGED:
     138                 :          64 :                         procNumber = INVALID_PROC_NUMBER;
     139                 :          64 :                         needs_wal = false;
     140                 :          64 :                         break;
     141                 :             :                 case RELPERSISTENCE_PERMANENT:
     142                 :       10377 :                         procNumber = INVALID_PROC_NUMBER;
     143                 :       10377 :                         needs_wal = true;
     144                 :       10377 :                         break;
     145                 :             :                 default:
     146   [ #  #  #  # ]:           0 :                         elog(ERROR, "invalid relpersistence: %c", relpersistence);
     147                 :           0 :                         return NULL;            /* placate compiler */
     148                 :             :         }
     149                 :             : 
     150                 :       11453 :         srel = smgropen(rlocator, procNumber);
     151                 :       11453 :         smgrcreate(srel, MAIN_FORKNUM, false);
     152                 :             : 
     153         [ +  + ]:       11453 :         if (needs_wal)
     154                 :       10377 :                 log_smgrcreate(&srel->smgr_rlocator.locator, MAIN_FORKNUM);
     155                 :             : 
     156                 :             :         /*
     157                 :             :          * Add the relation to the list of stuff to delete at abort, if we are
     158                 :             :          * asked to do so.
     159                 :             :          */
     160         [ +  + ]:       11453 :         if (register_delete)
     161                 :             :         {
     162                 :       10781 :                 PendingRelDelete *pending;
     163                 :             : 
     164                 :       10781 :                 pending = (PendingRelDelete *)
     165                 :       10781 :                         MemoryContextAlloc(TopMemoryContext, sizeof(PendingRelDelete));
     166                 :       10781 :                 pending->rlocator = rlocator;
     167                 :       10781 :                 pending->procNumber = procNumber;
     168                 :       10781 :                 pending->atCommit = false;   /* delete if abort */
     169                 :       10781 :                 pending->nestLevel = GetCurrentTransactionNestLevel();
     170                 :       10781 :                 pending->next = pendingDeletes;
     171                 :       10781 :                 pendingDeletes = pending;
     172                 :       10781 :         }
     173                 :             : 
     174   [ +  +  +  + ]:       11453 :         if (relpersistence == RELPERSISTENCE_PERMANENT && !XLogIsNeeded())
     175                 :             :         {
     176         [ +  - ]:        9797 :                 Assert(procNumber == INVALID_PROC_NUMBER);
     177                 :        9797 :                 AddPendingSync(&rlocator);
     178                 :        9797 :         }
     179                 :             : 
     180                 :       11453 :         return srel;
     181                 :       11453 : }
     182                 :             : 
     183                 :             : /*
     184                 :             :  * Perform XLogInsert of an XLOG_SMGR_CREATE record to WAL.
     185                 :             :  */
     186                 :             : void
     187                 :       10665 : log_smgrcreate(const RelFileLocator *rlocator, ForkNumber forkNum)
     188                 :             : {
     189                 :       10665 :         xl_smgr_create xlrec;
     190                 :             : 
     191                 :             :         /*
     192                 :             :          * Make an XLOG entry reporting the file creation.
     193                 :             :          */
     194                 :       10665 :         xlrec.rlocator = *rlocator;
     195                 :       10665 :         xlrec.forkNum = forkNum;
     196                 :             : 
     197                 :       10665 :         XLogBeginInsert();
     198                 :       10665 :         XLogRegisterData(&xlrec, sizeof(xlrec));
     199                 :       10665 :         XLogInsert(RM_SMGR_ID, XLOG_SMGR_CREATE | XLR_SPECIAL_REL_UPDATE);
     200                 :       10665 : }
     201                 :             : 
     202                 :             : /*
     203                 :             :  * RelationDropStorage
     204                 :             :  *              Schedule unlinking of physical storage at transaction commit.
     205                 :             :  */
     206                 :             : void
     207                 :        8434 : RelationDropStorage(Relation rel)
     208                 :             : {
     209                 :        8434 :         PendingRelDelete *pending;
     210                 :             : 
     211                 :             :         /* Add the relation to the list of stuff to delete at commit */
     212                 :        8434 :         pending = (PendingRelDelete *)
     213                 :        8434 :                 MemoryContextAlloc(TopMemoryContext, sizeof(PendingRelDelete));
     214                 :        8434 :         pending->rlocator = rel->rd_locator;
     215                 :        8434 :         pending->procNumber = rel->rd_backend;
     216                 :        8434 :         pending->atCommit = true;    /* delete if commit */
     217                 :        8434 :         pending->nestLevel = GetCurrentTransactionNestLevel();
     218                 :        8434 :         pending->next = pendingDeletes;
     219                 :        8434 :         pendingDeletes = pending;
     220                 :             : 
     221                 :             :         /*
     222                 :             :          * NOTE: if the relation was created in this transaction, it will now be
     223                 :             :          * present in the pending-delete list twice, once with atCommit true and
     224                 :             :          * once with atCommit false.  Hence, it will be physically deleted at end
     225                 :             :          * of xact in either case (and the other entry will be ignored by
     226                 :             :          * smgrDoPendingDeletes, so no error will occur).  We could instead remove
     227                 :             :          * the existing list entry and delete the physical file immediately, but
     228                 :             :          * for now I'll keep the logic simple.
     229                 :             :          */
     230                 :             : 
     231                 :        8434 :         RelationCloseSmgr(rel);
     232                 :        8434 : }
     233                 :             : 
     234                 :             : /*
     235                 :             :  * RelationPreserveStorage
     236                 :             :  *              Mark a relation as not to be deleted after all.
     237                 :             :  *
     238                 :             :  * We need this function because relation mapping changes are committed
     239                 :             :  * separately from commit of the whole transaction, so it's still possible
     240                 :             :  * for the transaction to abort after the mapping update is done.
     241                 :             :  * When a new physical relation is installed in the map, it would be
     242                 :             :  * scheduled for delete-on-abort, so we'd delete it, and be in trouble.
     243                 :             :  * The relation mapper fixes this by telling us to not delete such relations
     244                 :             :  * after all as part of its commit.
     245                 :             :  *
     246                 :             :  * We also use this to reuse an old build of an index during ALTER TABLE, this
     247                 :             :  * time removing the delete-at-commit entry.
     248                 :             :  *
     249                 :             :  * No-op if the relation is not among those scheduled for deletion.
     250                 :             :  */
     251                 :             : void
     252                 :          75 : RelationPreserveStorage(RelFileLocator rlocator, bool atCommit)
     253                 :             : {
     254                 :          75 :         PendingRelDelete *pending;
     255                 :          75 :         PendingRelDelete *prev;
     256                 :          75 :         PendingRelDelete *next;
     257                 :             : 
     258                 :          75 :         prev = NULL;
     259         [ +  + ]:         527 :         for (pending = pendingDeletes; pending != NULL; pending = next)
     260                 :             :         {
     261                 :         452 :                 next = pending->next;
     262   [ +  +  +  - ]:         452 :                 if (RelFileLocatorEquals(rlocator, pending->rlocator)
     263   [ +  -  +  + ]:          22 :                         && pending->atCommit == atCommit)
     264                 :             :                 {
     265                 :             :                         /* unlink and delete list entry */
     266         [ +  + ]:          21 :                         if (prev)
     267                 :          15 :                                 prev->next = next;
     268                 :             :                         else
     269                 :           6 :                                 pendingDeletes = next;
     270                 :          21 :                         pfree(pending);
     271                 :             :                         /* prev does not change */
     272                 :          21 :                 }
     273                 :             :                 else
     274                 :             :                 {
     275                 :             :                         /* unrelated entry, don't touch it */
     276                 :         431 :                         prev = pending;
     277                 :             :                 }
     278                 :         452 :         }
     279                 :          75 : }
     280                 :             : 
     281                 :             : /*
     282                 :             :  * RelationTruncate
     283                 :             :  *              Physically truncate a relation to the specified number of blocks.
     284                 :             :  *
     285                 :             :  * This includes getting rid of any buffers for the blocks that are to be
     286                 :             :  * dropped.
     287                 :             :  */
     288                 :             : void
     289                 :         155 : RelationTruncate(Relation rel, BlockNumber nblocks)
     290                 :             : {
     291                 :         155 :         bool            fsm;
     292                 :         155 :         bool            vm;
     293                 :         155 :         bool            need_fsm_vacuum = false;
     294                 :         155 :         ForkNumber      forks[MAX_FORKNUM];
     295                 :         155 :         BlockNumber old_blocks[MAX_FORKNUM];
     296                 :         155 :         BlockNumber blocks[MAX_FORKNUM];
     297                 :         155 :         int                     nforks = 0;
     298                 :         155 :         SMgrRelation reln;
     299                 :             : 
     300                 :             :         /*
     301                 :             :          * Make sure smgr_targblock etc aren't pointing somewhere past new end.
     302                 :             :          * (Note: don't rely on this reln pointer below this loop.)
     303                 :             :          */
     304                 :         155 :         reln = RelationGetSmgr(rel);
     305                 :         155 :         reln->smgr_targblock = InvalidBlockNumber;
     306         [ +  + ]:         775 :         for (int i = 0; i <= MAX_FORKNUM; ++i)
     307                 :         620 :                 reln->smgr_cached_nblocks[i] = InvalidBlockNumber;
     308                 :             : 
     309                 :             :         /* Prepare for truncation of MAIN fork of the relation */
     310                 :         155 :         forks[nforks] = MAIN_FORKNUM;
     311                 :         155 :         old_blocks[nforks] = smgrnblocks(reln, MAIN_FORKNUM);
     312                 :         155 :         blocks[nforks] = nblocks;
     313                 :         155 :         nforks++;
     314                 :             : 
     315                 :             :         /* Prepare for truncation of the FSM if it exists */
     316                 :         155 :         fsm = smgrexists(RelationGetSmgr(rel), FSM_FORKNUM);
     317         [ +  + ]:         155 :         if (fsm)
     318                 :             :         {
     319                 :          33 :                 blocks[nforks] = FreeSpaceMapPrepareTruncateRel(rel, nblocks);
     320         [ -  + ]:          33 :                 if (BlockNumberIsValid(blocks[nforks]))
     321                 :             :                 {
     322                 :          33 :                         forks[nforks] = FSM_FORKNUM;
     323                 :          33 :                         old_blocks[nforks] = smgrnblocks(reln, FSM_FORKNUM);
     324                 :          33 :                         nforks++;
     325                 :          33 :                         need_fsm_vacuum = true;
     326                 :          33 :                 }
     327                 :          33 :         }
     328                 :             : 
     329                 :             :         /* Prepare for truncation of the visibility map too if it exists */
     330                 :         155 :         vm = smgrexists(RelationGetSmgr(rel), VISIBILITYMAP_FORKNUM);
     331         [ +  + ]:         155 :         if (vm)
     332                 :             :         {
     333                 :          33 :                 blocks[nforks] = visibilitymap_prepare_truncate(rel, nblocks);
     334         [ +  + ]:          33 :                 if (BlockNumberIsValid(blocks[nforks]))
     335                 :             :                 {
     336                 :          12 :                         forks[nforks] = VISIBILITYMAP_FORKNUM;
     337                 :          12 :                         old_blocks[nforks] = smgrnblocks(reln, VISIBILITYMAP_FORKNUM);
     338                 :          12 :                         nforks++;
     339                 :          12 :                 }
     340                 :          33 :         }
     341                 :             : 
     342                 :         155 :         RelationPreTruncate(rel);
     343                 :             : 
     344                 :             :         /*
     345                 :             :          * The code which follows can interact with concurrent checkpoints in two
     346                 :             :          * separate ways.
     347                 :             :          *
     348                 :             :          * First, the truncation operation might drop buffers that the checkpoint
     349                 :             :          * otherwise would have flushed. If it does, then it's essential that the
     350                 :             :          * files actually get truncated on disk before the checkpoint record is
     351                 :             :          * written. Otherwise, if reply begins from that checkpoint, the
     352                 :             :          * to-be-truncated blocks might still exist on disk but have older
     353                 :             :          * contents than expected, which can cause replay to fail. It's OK for the
     354                 :             :          * blocks to not exist on disk at all, but not for them to have the wrong
     355                 :             :          * contents. For this reason, we need to set DELAY_CHKPT_COMPLETE while
     356                 :             :          * this code executes.
     357                 :             :          *
     358                 :             :          * Second, the call to smgrtruncate() below will in turn call
     359                 :             :          * RegisterSyncRequest(). We need the sync request created by that call to
     360                 :             :          * be processed before the checkpoint completes. CheckPointGuts() will
     361                 :             :          * call ProcessSyncRequests(), but if we register our sync request after
     362                 :             :          * that happens, then the WAL record for the truncation could end up
     363                 :             :          * preceding the checkpoint record, while the actual sync doesn't happen
     364                 :             :          * until the next checkpoint. To prevent that, we need to set
     365                 :             :          * DELAY_CHKPT_START here. That way, if the XLOG_SMGR_TRUNCATE precedes
     366                 :             :          * the redo pointer of a concurrent checkpoint, we're guaranteed that the
     367                 :             :          * corresponding sync request will be processed before the checkpoint
     368                 :             :          * completes.
     369                 :             :          */
     370         [ +  - ]:         155 :         Assert((MyProc->delayChkptFlags & (DELAY_CHKPT_START | DELAY_CHKPT_COMPLETE)) == 0);
     371                 :         155 :         MyProc->delayChkptFlags |= DELAY_CHKPT_START | DELAY_CHKPT_COMPLETE;
     372                 :             : 
     373                 :             :         /*
     374                 :             :          * We WAL-log the truncation first and then truncate in a critical
     375                 :             :          * section. Truncation drops buffers, even if dirty, and then truncates
     376                 :             :          * disk files. All of that work needs to complete before the lock is
     377                 :             :          * released, or else old versions of pages on disk that are missing recent
     378                 :             :          * changes would become accessible again.  We'll try the whole operation
     379                 :             :          * again in crash recovery if we panic, but even then we can't give up
     380                 :             :          * because we don't want standbys' relation sizes to diverge and break
     381                 :             :          * replay or visibility invariants downstream.  The critical section also
     382                 :             :          * suppresses interrupts.
     383                 :             :          *
     384                 :             :          * (See also visibilitymap.c if changing this code.)
     385                 :             :          */
     386                 :         155 :         START_CRIT_SECTION();
     387                 :             : 
     388   [ +  +  +  -  :         155 :         if (RelationNeedsWAL(rel))
             +  +  -  + ]
     389                 :             :         {
     390                 :             :                 /*
     391                 :             :                  * Make an XLOG entry reporting the file truncation.
     392                 :             :                  */
     393                 :          29 :                 XLogRecPtr      lsn;
     394                 :          29 :                 xl_smgr_truncate xlrec;
     395                 :             : 
     396                 :          29 :                 xlrec.blkno = nblocks;
     397                 :          29 :                 xlrec.rlocator = rel->rd_locator;
     398                 :          29 :                 xlrec.flags = SMGR_TRUNCATE_ALL;
     399                 :             : 
     400                 :          29 :                 XLogBeginInsert();
     401                 :          29 :                 XLogRegisterData(&xlrec, sizeof(xlrec));
     402                 :             : 
     403                 :          29 :                 lsn = XLogInsert(RM_SMGR_ID,
     404                 :             :                                                  XLOG_SMGR_TRUNCATE | XLR_SPECIAL_REL_UPDATE);
     405                 :             : 
     406                 :             :                 /*
     407                 :             :                  * Flush, because otherwise the truncation of the main relation might
     408                 :             :                  * hit the disk before the WAL record, and the truncation of the FSM
     409                 :             :                  * or visibility map. If we crashed during that window, we'd be left
     410                 :             :                  * with a truncated heap, but the FSM or visibility map would still
     411                 :             :                  * contain entries for the non-existent heap pages, and standbys would
     412                 :             :                  * also never replay the truncation.
     413                 :             :                  */
     414                 :          29 :                 XLogFlush(lsn);
     415                 :          29 :         }
     416                 :             : 
     417                 :             :         /*
     418                 :             :          * This will first remove any buffers from the buffer pool that should no
     419                 :             :          * longer exist after truncation is complete, and then truncate the
     420                 :             :          * corresponding files on disk.
     421                 :             :          */
     422                 :         155 :         smgrtruncate(RelationGetSmgr(rel), forks, nforks, old_blocks, blocks);
     423                 :             : 
     424         [ +  - ]:         155 :         END_CRIT_SECTION();
     425                 :             : 
     426                 :             :         /* We've done all the critical work, so checkpoints are OK now. */
     427                 :         155 :         MyProc->delayChkptFlags &= ~(DELAY_CHKPT_START | DELAY_CHKPT_COMPLETE);
     428                 :             : 
     429                 :             :         /*
     430                 :             :          * Update upper-level FSM pages to account for the truncation. This is
     431                 :             :          * important because the just-truncated pages were likely marked as
     432                 :             :          * all-free, and would be preferentially selected.
     433                 :             :          *
     434                 :             :          * NB: There's no point in delaying checkpoints until this is done.
     435                 :             :          * Because the FSM is not WAL-logged, we have to be prepared for the
     436                 :             :          * possibility of corruption after a crash anyway.
     437                 :             :          */
     438         [ +  + ]:         155 :         if (need_fsm_vacuum)
     439                 :          33 :                 FreeSpaceMapVacuumRange(rel, nblocks, InvalidBlockNumber);
     440                 :         155 : }
     441                 :             : 
     442                 :             : /*
     443                 :             :  * RelationPreTruncate
     444                 :             :  *              Perform AM-independent work before a physical truncation.
     445                 :             :  *
     446                 :             :  * If an access method's relation_nontransactional_truncate does not call
     447                 :             :  * RelationTruncate(), it must call this before decreasing the table size.
     448                 :             :  */
     449                 :             : void
     450                 :         155 : RelationPreTruncate(Relation rel)
     451                 :             : {
     452                 :         155 :         PendingRelSync *pending;
     453                 :             : 
     454         [ +  + ]:         155 :         if (!pendingSyncHash)
     455                 :         152 :                 return;
     456                 :             : 
     457                 :           6 :         pending = hash_search(pendingSyncHash,
     458                 :           3 :                                                   &(RelationGetSmgr(rel)->smgr_rlocator.locator),
     459                 :             :                                                   HASH_FIND, NULL);
     460         [ -  + ]:           3 :         if (pending)
     461                 :           3 :                 pending->is_truncated = true;
     462         [ -  + ]:         155 : }
     463                 :             : 
     464                 :             : /*
     465                 :             :  * Copy a fork's data, block by block.
     466                 :             :  *
     467                 :             :  * Note that this requires that there is no dirty data in shared buffers. If
     468                 :             :  * it's possible that there are, callers need to flush those using
     469                 :             :  * e.g. FlushRelationBuffers(rel).
     470                 :             :  *
     471                 :             :  * Also note that this is frequently called via locutions such as
     472                 :             :  *              RelationCopyStorage(RelationGetSmgr(rel), ...);
     473                 :             :  * That's safe only because we perform only smgr and WAL operations here.
     474                 :             :  * If we invoked anything else, a relcache flush could cause our SMgrRelation
     475                 :             :  * argument to become a dangling pointer.
     476                 :             :  */
     477                 :             : void
     478                 :          27 : RelationCopyStorage(SMgrRelation src, SMgrRelation dst,
     479                 :             :                                         ForkNumber forkNum, char relpersistence)
     480                 :             : {
     481                 :          27 :         bool            use_wal;
     482                 :          27 :         bool            copying_initfork;
     483                 :          27 :         BlockNumber nblocks;
     484                 :          27 :         BlockNumber blkno;
     485                 :          27 :         BulkWriteState *bulkstate;
     486                 :             : 
     487                 :             :         /*
     488                 :             :          * The init fork for an unlogged relation in many respects has to be
     489                 :             :          * treated the same as normal relation, changes need to be WAL logged and
     490                 :             :          * it needs to be synced to disk.
     491                 :             :          */
     492         [ +  - ]:          27 :         copying_initfork = relpersistence == RELPERSISTENCE_UNLOGGED &&
     493                 :           0 :                 forkNum == INIT_FORKNUM;
     494                 :             : 
     495                 :             :         /*
     496                 :             :          * We need to log the copied data in WAL iff WAL archiving/streaming is
     497                 :             :          * enabled AND it's a permanent relation.  This gives the same answer as
     498                 :             :          * "RelationNeedsWAL(rel) || copying_initfork", because we know the
     499                 :             :          * current operation created new relation storage.
     500                 :             :          */
     501         [ +  - ]:          27 :         use_wal = XLogIsNeeded() &&
     502         [ #  # ]:           0 :                 (relpersistence == RELPERSISTENCE_PERMANENT || copying_initfork);
     503                 :             : 
     504                 :          27 :         bulkstate = smgr_bulk_start_smgr(dst, forkNum, use_wal);
     505                 :             : 
     506                 :          27 :         nblocks = smgrnblocks(src, forkNum);
     507                 :             : 
     508         [ +  + ]:         204 :         for (blkno = 0; blkno < nblocks; blkno++)
     509                 :             :         {
     510                 :         177 :                 BulkWriteBuffer buf;
     511                 :         177 :                 int                     piv_flags;
     512                 :         177 :                 bool            checksum_failure;
     513                 :         177 :                 bool            verified;
     514                 :             : 
     515                 :             :                 /* If we got a cancel signal during the copy of the data, quit */
     516         [ +  - ]:         177 :                 CHECK_FOR_INTERRUPTS();
     517                 :             : 
     518                 :         177 :                 buf = smgr_bulk_get_buf(bulkstate);
     519                 :         177 :                 smgrread(src, forkNum, blkno, (Page) buf);
     520                 :             : 
     521                 :         177 :                 piv_flags = PIV_LOG_WARNING;
     522         [ +  - ]:         177 :                 if (ignore_checksum_failure)
     523                 :           0 :                         piv_flags |= PIV_IGNORE_CHECKSUM_FAILURE;
     524                 :         177 :                 verified = PageIsVerified((Page) buf, blkno, piv_flags,
     525                 :             :                                                                   &checksum_failure);
     526         [ +  - ]:         177 :                 if (checksum_failure)
     527                 :             :                 {
     528                 :           0 :                         RelFileLocatorBackend rloc = src->smgr_rlocator;
     529                 :             : 
     530                 :           0 :                         pgstat_prepare_report_checksum_failure(rloc.locator.dbOid);
     531                 :           0 :                         pgstat_report_checksum_failures_in_db(rloc.locator.dbOid, 1);
     532                 :           0 :                 }
     533                 :             : 
     534         [ +  - ]:         177 :                 if (!verified)
     535                 :             :                 {
     536                 :             :                         /*
     537                 :             :                          * For paranoia's sake, capture the file path before invoking the
     538                 :             :                          * ereport machinery.  This guards against the possibility of a
     539                 :             :                          * relcache flush caused by, e.g., an errcontext callback.
     540                 :             :                          * (errcontext callbacks shouldn't be risking any such thing, but
     541                 :             :                          * people have been known to forget that rule.)
     542                 :             :                          */
     543                 :           0 :                         RelPathStr      relpath = relpathbackend(src->smgr_rlocator.locator,
     544                 :             :                                                                                                  src->smgr_rlocator.backend,
     545                 :             :                                                                                                  forkNum);
     546                 :             : 
     547   [ #  #  #  # ]:           0 :                         ereport(ERROR,
     548                 :             :                                         (errcode(ERRCODE_DATA_CORRUPTED),
     549                 :             :                                          errmsg("invalid page in block %u of relation \"%s\"",
     550                 :             :                                                         blkno, relpath.str)));
     551                 :           0 :                 }
     552                 :             : 
     553                 :             :                 /*
     554                 :             :                  * Queue the page for WAL-logging and writing out.  Unfortunately we
     555                 :             :                  * don't know what kind of a page this is, so we have to log the full
     556                 :             :                  * page including any unused space.
     557                 :             :                  */
     558                 :         177 :                 smgr_bulk_write(bulkstate, blkno, buf, false);
     559                 :         177 :         }
     560                 :          27 :         smgr_bulk_finish(bulkstate);
     561                 :          27 : }
     562                 :             : 
     563                 :             : /*
     564                 :             :  * RelFileLocatorSkippingWAL
     565                 :             :  *              Check if a BM_PERMANENT relfilelocator is using WAL.
     566                 :             :  *
     567                 :             :  * Changes to certain relations must not write WAL; see "Skipping WAL for
     568                 :             :  * New RelFileLocator" in src/backend/access/transam/README.  Though it is
     569                 :             :  * known from Relation efficiently, this function is intended for the code
     570                 :             :  * paths not having access to Relation.
     571                 :             :  */
     572                 :             : bool
     573                 :      772062 : RelFileLocatorSkippingWAL(RelFileLocator rlocator)
     574                 :             : {
     575   [ +  +  +  + ]:      772062 :         if (!pendingSyncHash ||
     576                 :      763126 :                 hash_search(pendingSyncHash, &rlocator, HASH_FIND, NULL) == NULL)
     577                 :      761399 :                 return false;
     578                 :             : 
     579                 :       10663 :         return true;
     580                 :      772062 : }
     581                 :             : 
     582                 :             : /*
     583                 :             :  * EstimatePendingSyncsSpace
     584                 :             :  *              Estimate space needed to pass syncs to parallel workers.
     585                 :             :  */
     586                 :             : Size
     587                 :         155 : EstimatePendingSyncsSpace(void)
     588                 :             : {
     589                 :         155 :         int64           entries;
     590                 :             : 
     591         [ +  + ]:         155 :         entries = pendingSyncHash ? hash_get_num_entries(pendingSyncHash) : 0;
     592                 :         310 :         return mul_size(1 + entries, sizeof(RelFileLocator));
     593                 :         155 : }
     594                 :             : 
     595                 :             : /*
     596                 :             :  * SerializePendingSyncs
     597                 :             :  *              Serialize syncs for parallel workers.
     598                 :             :  */
     599                 :             : void
     600                 :         155 : SerializePendingSyncs(Size maxSize, char *startAddress)
     601                 :             : {
     602                 :         155 :         HTAB       *tmphash;
     603                 :         155 :         HASHCTL         ctl;
     604                 :         155 :         HASH_SEQ_STATUS scan;
     605                 :         155 :         PendingRelSync *sync;
     606                 :         155 :         PendingRelDelete *delete;
     607                 :         155 :         RelFileLocator *src;
     608                 :         155 :         RelFileLocator *dest = (RelFileLocator *) startAddress;
     609                 :             : 
     610         [ +  + ]:         155 :         if (!pendingSyncHash)
     611                 :          54 :                 goto terminate;
     612                 :             : 
     613                 :             :         /* Create temporary hash to collect active relfilelocators */
     614                 :         101 :         ctl.keysize = sizeof(RelFileLocator);
     615                 :         101 :         ctl.entrysize = sizeof(RelFileLocator);
     616                 :         101 :         ctl.hcxt = CurrentMemoryContext;
     617                 :         101 :         tmphash = hash_create("tmp relfilelocators",
     618                 :         101 :                                                   hash_get_num_entries(pendingSyncHash), &ctl,
     619                 :             :                                                   HASH_ELEM | HASH_BLOBS | HASH_CONTEXT);
     620                 :             : 
     621                 :             :         /* collect all rlocator from pending syncs */
     622                 :         101 :         hash_seq_init(&scan, pendingSyncHash);
     623         [ +  + ]:         849 :         while ((sync = (PendingRelSync *) hash_seq_search(&scan)))
     624                 :         748 :                 (void) hash_search(tmphash, &sync->rlocator, HASH_ENTER, NULL);
     625                 :             : 
     626                 :             :         /* remove deleted rnodes */
     627         [ +  + ]:        1019 :         for (delete = pendingDeletes; delete != NULL; delete = delete->next)
     628         [ +  + ]:        1083 :                 if (delete->atCommit)
     629                 :         165 :                         (void) hash_search(tmphash, &delete->rlocator,
     630                 :             :                                                            HASH_REMOVE, NULL);
     631                 :             : 
     632                 :         101 :         hash_seq_init(&scan, tmphash);
     633         [ +  + ]:         688 :         while ((src = (RelFileLocator *) hash_seq_search(&scan)))
     634                 :         587 :                 *dest++ = *src;
     635                 :             : 
     636                 :         101 :         hash_destroy(tmphash);
     637                 :             : 
     638                 :             : terminate:
     639   [ +  +  -  +  :         155 :         MemSet(dest, 0, sizeof(RelFileLocator));
          #  #  #  #  #  
                      # ]
     640                 :         155 : }
     641                 :             : 
     642                 :             : /*
     643                 :             :  * RestorePendingSyncs
     644                 :             :  *              Restore syncs within a parallel worker.
     645                 :             :  *
     646                 :             :  * RelationNeedsWAL() and RelFileLocatorSkippingWAL() must offer the correct
     647                 :             :  * answer to parallel workers.  Only smgrDoPendingSyncs() reads the
     648                 :             :  * is_truncated field, at end of transaction.  Hence, don't restore it.
     649                 :             :  */
     650                 :             : void
     651                 :         477 : RestorePendingSyncs(char *startAddress)
     652                 :             : {
     653                 :         477 :         RelFileLocator *rlocator;
     654                 :             : 
     655         [ +  - ]:         477 :         Assert(pendingSyncHash == NULL);
     656         [ +  + ]:        2337 :         for (rlocator = (RelFileLocator *) startAddress; rlocator->relNumber != 0;
     657                 :        1860 :                  rlocator++)
     658                 :        1860 :                 AddPendingSync(rlocator);
     659                 :         477 : }
     660                 :             : 
     661                 :             : /*
     662                 :             :  *      smgrDoPendingDeletes() -- Take care of relation deletes at end of xact.
     663                 :             :  *
     664                 :             :  * This also runs when aborting a subxact; we want to clean up a failed
     665                 :             :  * subxact immediately.
     666                 :             :  *
     667                 :             :  * Note: It's possible that we're being asked to remove a relation that has
     668                 :             :  * no physical storage in any fork. In particular, it's possible that we're
     669                 :             :  * cleaning up an old temporary relation for which RemovePgTempFiles has
     670                 :             :  * already recovered the physical storage.
     671                 :             :  */
     672                 :             : void
     673                 :       59100 : smgrDoPendingDeletes(bool isCommit)
     674                 :             : {
     675                 :       59100 :         int                     nestLevel = GetCurrentTransactionNestLevel();
     676                 :       59100 :         PendingRelDelete *pending;
     677                 :       59100 :         PendingRelDelete *prev;
     678                 :       59100 :         PendingRelDelete *next;
     679                 :       59100 :         int                     nrels = 0,
     680                 :       59100 :                                 maxrels = 0;
     681                 :       59100 :         SMgrRelation *srels = NULL;
     682                 :             : 
     683                 :       59100 :         prev = NULL;
     684         [ +  + ]:       79648 :         for (pending = pendingDeletes; pending != NULL; pending = next)
     685                 :             :         {
     686                 :       20548 :                 next = pending->next;
     687         [ +  + ]:       20548 :                 if (pending->nestLevel < nestLevel)
     688                 :             :                 {
     689                 :             :                         /* outer-level entries should not be processed yet */
     690                 :        1354 :                         prev = pending;
     691                 :        1354 :                 }
     692                 :             :                 else
     693                 :             :                 {
     694                 :             :                         /* unlink list entry first, so we don't retry on failure */
     695         [ -  + ]:       19194 :                         if (prev)
     696                 :           0 :                                 prev->next = next;
     697                 :             :                         else
     698                 :       19194 :                                 pendingDeletes = next;
     699                 :             :                         /* do deletion if called for */
     700         [ +  + ]:       19194 :                         if (pending->atCommit == isCommit)
     701                 :             :                         {
     702                 :        8924 :                                 SMgrRelation srel;
     703                 :             : 
     704                 :        8924 :                                 srel = smgropen(pending->rlocator, pending->procNumber);
     705                 :             : 
     706                 :             :                                 /* allocate the initial array, or extend it, if needed */
     707         [ +  + ]:        8924 :                                 if (maxrels == 0)
     708                 :             :                                 {
     709                 :        2704 :                                         maxrels = 8;
     710                 :        2704 :                                         srels = palloc_array(SMgrRelation, maxrels);
     711                 :        2704 :                                 }
     712         [ +  + ]:        6220 :                                 else if (maxrels <= nrels)
     713                 :             :                                 {
     714                 :         209 :                                         maxrels *= 2;
     715                 :         209 :                                         srels = repalloc_array(srels, SMgrRelation, maxrels);
     716                 :         209 :                                 }
     717                 :             : 
     718                 :        8924 :                                 srels[nrels++] = srel;
     719                 :        8924 :                         }
     720                 :             :                         /* must explicitly free the list entry */
     721                 :       19194 :                         pfree(pending);
     722                 :             :                         /* prev does not change */
     723                 :             :                 }
     724                 :       20548 :         }
     725                 :             : 
     726         [ +  + ]:       59100 :         if (nrels > 0)
     727                 :             :         {
     728                 :        2704 :                 smgrdounlinkall(srels, nrels, false);
     729                 :             : 
     730         [ +  + ]:       11628 :                 for (int i = 0; i < nrels; i++)
     731                 :        8924 :                         smgrclose(srels[i]);
     732                 :             : 
     733                 :        2704 :                 pfree(srels);
     734                 :        2704 :         }
     735                 :       59100 : }
     736                 :             : 
     737                 :             : /*
     738                 :             :  *      smgrDoPendingSyncs() -- Take care of relation syncs at end of xact.
     739                 :             :  */
     740                 :             : void
     741                 :       57944 : smgrDoPendingSyncs(bool isCommit, bool isParallelWorker)
     742                 :             : {
     743                 :       57944 :         PendingRelDelete *pending;
     744                 :       57944 :         int                     nrels = 0,
     745                 :       57944 :                                 maxrels = 0;
     746                 :       57944 :         SMgrRelation *srels = NULL;
     747                 :       57944 :         HASH_SEQ_STATUS scan;
     748                 :       57944 :         PendingRelSync *pendingsync;
     749                 :             : 
     750         [ +  - ]:       57944 :         Assert(GetCurrentTransactionNestLevel() == 1);
     751                 :             : 
     752         [ +  + ]:       57944 :         if (!pendingSyncHash)
     753                 :       53116 :                 return;                                 /* no relation needs sync */
     754                 :             : 
     755                 :             :         /* Abort -- just throw away all pending syncs */
     756         [ +  + ]:        4828 :         if (!isCommit)
     757                 :             :         {
     758                 :         305 :                 pendingSyncHash = NULL;
     759                 :         305 :                 return;
     760                 :             :         }
     761                 :             : 
     762                 :        4523 :         AssertPendingSyncs_RelationCache();
     763                 :             : 
     764                 :             :         /* Parallel worker -- just throw away all pending syncs */
     765         [ +  + ]:        4523 :         if (isParallelWorker)
     766                 :             :         {
     767                 :         219 :                 pendingSyncHash = NULL;
     768                 :         219 :                 return;
     769                 :             :         }
     770                 :             : 
     771                 :             :         /* Skip syncing nodes that smgrDoPendingDeletes() will delete. */
     772         [ +  + ]:       14908 :         for (pending = pendingDeletes; pending != NULL; pending = pending->next)
     773         [ +  + ]:       12454 :                 if (pending->atCommit)
     774                 :        1850 :                         (void) hash_search(pendingSyncHash, &pending->rlocator,
     775                 :             :                                                            HASH_REMOVE, NULL);
     776                 :             : 
     777                 :        4304 :         hash_seq_init(&scan, pendingSyncHash);
     778         [ +  + ]:       13336 :         while ((pendingsync = (PendingRelSync *) hash_seq_search(&scan)))
     779                 :             :         {
     780                 :        9032 :                 ForkNumber      fork;
     781                 :        9032 :                 BlockNumber nblocks[MAX_FORKNUM + 1];
     782                 :        9032 :                 uint64          total_blocks = 0;
     783                 :        9032 :                 SMgrRelation srel;
     784                 :             : 
     785                 :        9032 :                 srel = smgropen(pendingsync->rlocator, INVALID_PROC_NUMBER);
     786                 :             : 
     787                 :             :                 /*
     788                 :             :                  * We emit newpage WAL records for smaller relations.
     789                 :             :                  *
     790                 :             :                  * Small WAL records have a chance to be flushed along with other
     791                 :             :                  * backends' WAL records.  We emit WAL records instead of syncing for
     792                 :             :                  * files that are smaller than a certain threshold, expecting faster
     793                 :             :                  * commit.  The threshold is defined by the GUC wal_skip_threshold.
     794                 :             :                  */
     795         [ -  + ]:        9032 :                 if (!pendingsync->is_truncated)
     796                 :             :                 {
     797         [ +  + ]:       45160 :                         for (fork = 0; fork <= MAX_FORKNUM; fork++)
     798                 :             :                         {
     799         [ +  + ]:       36128 :                                 if (smgrexists(srel, fork))
     800                 :             :                                 {
     801                 :        9269 :                                         BlockNumber n = smgrnblocks(srel, fork);
     802                 :             : 
     803                 :             :                                         /* we shouldn't come here for unlogged relations */
     804         [ -  + ]:        9269 :                                         Assert(fork != INIT_FORKNUM);
     805                 :        9269 :                                         nblocks[fork] = n;
     806                 :        9269 :                                         total_blocks += n;
     807                 :        9269 :                                 }
     808                 :             :                                 else
     809                 :       26859 :                                         nblocks[fork] = InvalidBlockNumber;
     810                 :       36128 :                         }
     811                 :        9032 :                 }
     812                 :             : 
     813                 :             :                 /*
     814                 :             :                  * Sync file or emit WAL records for its contents.
     815                 :             :                  *
     816                 :             :                  * Although we emit WAL record if the file is small enough, do file
     817                 :             :                  * sync regardless of the size if the file has experienced a
     818                 :             :                  * truncation. It is because the file would be followed by trailing
     819                 :             :                  * garbage blocks after a crash recovery if, while a past longer file
     820                 :             :                  * had been flushed out, we omitted syncing-out of the file and
     821                 :             :                  * emitted WAL instead.  You might think that we could choose WAL if
     822                 :             :                  * the current main fork is longer than ever, but there's a case where
     823                 :             :                  * main fork is longer than ever but FSM fork gets shorter.
     824                 :             :                  */
     825   [ +  -  +  + ]:        9032 :                 if (pendingsync->is_truncated ||
     826                 :        9032 :                         total_blocks >= wal_skip_threshold * (uint64) 1024 / BLCKSZ)
     827                 :             :                 {
     828                 :             :                         /* allocate the initial array, or extend it, if needed */
     829         [ +  - ]:           8 :                         if (maxrels == 0)
     830                 :             :                         {
     831                 :           8 :                                 maxrels = 8;
     832                 :           8 :                                 srels = palloc_array(SMgrRelation, maxrels);
     833                 :           8 :                         }
     834         [ #  # ]:           0 :                         else if (maxrels <= nrels)
     835                 :             :                         {
     836                 :           0 :                                 maxrels *= 2;
     837                 :           0 :                                 srels = repalloc_array(srels, SMgrRelation, maxrels);
     838                 :           0 :                         }
     839                 :             : 
     840                 :           8 :                         srels[nrels++] = srel;
     841                 :           8 :                 }
     842                 :             :                 else
     843                 :             :                 {
     844                 :             :                         /* Emit WAL records for all blocks.  The file is small enough. */
     845         [ +  + ]:       45120 :                         for (fork = 0; fork <= MAX_FORKNUM; fork++)
     846                 :             :                         {
     847                 :       36096 :                                 int                     n = nblocks[fork];
     848                 :       36096 :                                 Relation        rel;
     849                 :             : 
     850         [ +  + ]:       36096 :                                 if (!BlockNumberIsValid(n))
     851                 :       26835 :                                         continue;
     852                 :             : 
     853                 :             :                                 /*
     854                 :             :                                  * Emit WAL for the whole file.  Unfortunately we don't know
     855                 :             :                                  * what kind of a page this is, so we have to log the full
     856                 :             :                                  * page including any unused space.  ReadBufferExtended()
     857                 :             :                                  * counts some pgstat events; unfortunately, we discard them.
     858                 :             :                                  */
     859                 :        9261 :                                 rel = CreateFakeRelcacheEntry(srel->smgr_rlocator.locator);
     860                 :        9261 :                                 log_newpage_range(rel, fork, 0, n, false);
     861                 :        9261 :                                 FreeFakeRelcacheEntry(rel);
     862         [ +  + ]:       36096 :                         }
     863                 :             :                 }
     864                 :        9032 :         }
     865                 :             : 
     866                 :        4304 :         pendingSyncHash = NULL;
     867                 :             : 
     868         [ +  + ]:        4304 :         if (nrels > 0)
     869                 :             :         {
     870                 :           8 :                 smgrdosyncall(srels, nrels);
     871                 :           8 :                 pfree(srels);
     872                 :           8 :         }
     873                 :       57944 : }
     874                 :             : 
     875                 :             : /*
     876                 :             :  * smgrGetPendingDeletes() -- Get a list of non-temp relations to be deleted.
     877                 :             :  *
     878                 :             :  * The return value is the number of relations scheduled for termination.
     879                 :             :  * *ptr is set to point to a freshly-palloc'd array of RelFileLocators.
     880                 :             :  * If there are no relations to be deleted, *ptr is set to NULL.
     881                 :             :  *
     882                 :             :  * Only non-temporary relations are included in the returned list.  This is OK
     883                 :             :  * because the list is used only in contexts where temporary relations don't
     884                 :             :  * matter: we're either writing to the two-phase state file (and transactions
     885                 :             :  * that have touched temp tables can't be prepared) or we're writing to xlog
     886                 :             :  * (and all temporary files will be zapped if we restart anyway, so no need
     887                 :             :  * for redo to do it also).
     888                 :             :  *
     889                 :             :  * Note that the list does not include anything scheduled for termination
     890                 :             :  * by upper-level transactions.
     891                 :             :  */
     892                 :             : int
     893                 :       51808 : smgrGetPendingDeletes(bool forCommit, RelFileLocator **ptr)
     894                 :             : {
     895                 :       51808 :         int                     nestLevel = GetCurrentTransactionNestLevel();
     896                 :       51808 :         int                     nrels;
     897                 :       51808 :         RelFileLocator *rptr;
     898                 :       51808 :         PendingRelDelete *pending;
     899                 :             : 
     900                 :       51808 :         nrels = 0;
     901         [ +  + ]:       71875 :         for (pending = pendingDeletes; pending != NULL; pending = pending->next)
     902                 :             :         {
     903         [ +  + ]:       20067 :                 if (pending->nestLevel >= nestLevel && pending->atCommit == forCommit
     904   [ +  +  +  + ]:       19194 :                         && pending->procNumber == INVALID_PROC_NUMBER)
     905                 :        7912 :                         nrels++;
     906                 :       20067 :         }
     907         [ +  + ]:       51808 :         if (nrels == 0)
     908                 :             :         {
     909                 :       49361 :                 *ptr = NULL;
     910                 :       49361 :                 return 0;
     911                 :             :         }
     912                 :        2447 :         rptr = palloc_array(RelFileLocator, nrels);
     913                 :        2447 :         *ptr = rptr;
     914         [ +  + ]:       12507 :         for (pending = pendingDeletes; pending != NULL; pending = pending->next)
     915                 :             :         {
     916         [ +  + ]:       10060 :                 if (pending->nestLevel >= nestLevel && pending->atCommit == forCommit
     917   [ +  +  +  + ]:       10013 :                         && pending->procNumber == INVALID_PROC_NUMBER)
     918                 :             :                 {
     919                 :        7912 :                         *rptr = pending->rlocator;
     920                 :        7912 :                         rptr++;
     921                 :        7912 :                 }
     922                 :       10060 :         }
     923                 :        2447 :         return nrels;
     924                 :       51808 : }
     925                 :             : 
     926                 :             : /*
     927                 :             :  *      PostPrepare_smgr -- Clean up after a successful PREPARE
     928                 :             :  *
     929                 :             :  * What we have to do here is throw away the in-memory state about pending
     930                 :             :  * relation deletes.  It's all been recorded in the 2PC state file and
     931                 :             :  * it's no longer smgr's job to worry about it.
     932                 :             :  */
     933                 :             : void
     934                 :           0 : PostPrepare_smgr(void)
     935                 :             : {
     936                 :           0 :         PendingRelDelete *pending;
     937                 :           0 :         PendingRelDelete *next;
     938                 :             : 
     939         [ #  # ]:           0 :         for (pending = pendingDeletes; pending != NULL; pending = next)
     940                 :             :         {
     941                 :           0 :                 next = pending->next;
     942                 :           0 :                 pendingDeletes = next;
     943                 :             :                 /* must explicitly free the list entry */
     944                 :           0 :                 pfree(pending);
     945                 :           0 :         }
     946                 :           0 : }
     947                 :             : 
     948                 :             : 
     949                 :             : /*
     950                 :             :  * AtSubCommit_smgr() --- Take care of subtransaction commit.
     951                 :             :  *
     952                 :             :  * Reassign all items in the pending-deletes list to the parent transaction.
     953                 :             :  */
     954                 :             : void
     955                 :         482 : AtSubCommit_smgr(void)
     956                 :             : {
     957                 :         482 :         int                     nestLevel = GetCurrentTransactionNestLevel();
     958                 :         482 :         PendingRelDelete *pending;
     959                 :             : 
     960         [ +  + ]:         555 :         for (pending = pendingDeletes; pending != NULL; pending = pending->next)
     961                 :             :         {
     962         [ +  + ]:          73 :                 if (pending->nestLevel >= nestLevel)
     963                 :          33 :                         pending->nestLevel = nestLevel - 1;
     964                 :          73 :         }
     965                 :         482 : }
     966                 :             : 
     967                 :             : /*
     968                 :             :  * AtSubAbort_smgr() --- Take care of subtransaction abort.
     969                 :             :  *
     970                 :             :  * Delete created relations and forget about deleted relations.
     971                 :             :  * We can execute these operations immediately because we know this
     972                 :             :  * subtransaction will not commit.
     973                 :             :  */
     974                 :             : void
     975                 :        1183 : AtSubAbort_smgr(void)
     976                 :             : {
     977                 :        1183 :         smgrDoPendingDeletes(false);
     978                 :        1183 : }
     979                 :             : 
     980                 :             : void
     981                 :           0 : smgr_redo(XLogReaderState *record)
     982                 :             : {
     983                 :           0 :         XLogRecPtr      lsn = record->EndRecPtr;
     984                 :           0 :         uint8           info = XLogRecGetInfo(record) & ~XLR_INFO_MASK;
     985                 :             : 
     986                 :             :         /* Backup blocks are not used in smgr records */
     987         [ #  # ]:           0 :         Assert(!XLogRecHasAnyBlockRefs(record));
     988                 :             : 
     989         [ #  # ]:           0 :         if (info == XLOG_SMGR_CREATE)
     990                 :             :         {
     991                 :           0 :                 xl_smgr_create *xlrec = (xl_smgr_create *) XLogRecGetData(record);
     992                 :           0 :                 SMgrRelation reln;
     993                 :             : 
     994                 :           0 :                 reln = smgropen(xlrec->rlocator, INVALID_PROC_NUMBER);
     995                 :           0 :                 smgrcreate(reln, xlrec->forkNum, true);
     996                 :           0 :         }
     997         [ #  # ]:           0 :         else if (info == XLOG_SMGR_TRUNCATE)
     998                 :             :         {
     999                 :           0 :                 xl_smgr_truncate *xlrec = (xl_smgr_truncate *) XLogRecGetData(record);
    1000                 :           0 :                 SMgrRelation reln;
    1001                 :           0 :                 Relation        rel;
    1002                 :           0 :                 ForkNumber      forks[MAX_FORKNUM];
    1003                 :           0 :                 BlockNumber blocks[MAX_FORKNUM];
    1004                 :           0 :                 BlockNumber old_blocks[MAX_FORKNUM];
    1005                 :           0 :                 int                     nforks = 0;
    1006                 :           0 :                 bool            need_fsm_vacuum = false;
    1007                 :             : 
    1008                 :           0 :                 reln = smgropen(xlrec->rlocator, INVALID_PROC_NUMBER);
    1009                 :             : 
    1010                 :             :                 /*
    1011                 :             :                  * Forcibly create relation if it doesn't exist (which suggests that
    1012                 :             :                  * it was dropped somewhere later in the WAL sequence).  As in
    1013                 :             :                  * XLogReadBufferForRedo, we prefer to recreate the rel and replay the
    1014                 :             :                  * log as best we can until the drop is seen.
    1015                 :             :                  */
    1016                 :           0 :                 smgrcreate(reln, MAIN_FORKNUM, true);
    1017                 :             : 
    1018                 :             :                 /*
    1019                 :             :                  * Before we perform the truncation, update minimum recovery point to
    1020                 :             :                  * cover this WAL record. Once the relation is truncated, there's no
    1021                 :             :                  * going back. The buffer manager enforces the WAL-first rule for
    1022                 :             :                  * normal updates to relation files, so that the minimum recovery
    1023                 :             :                  * point is always updated before the corresponding change in the data
    1024                 :             :                  * file is flushed to disk. We have to do the same manually here.
    1025                 :             :                  *
    1026                 :             :                  * Doing this before the truncation means that if the truncation fails
    1027                 :             :                  * for some reason, you cannot start up the system even after restart,
    1028                 :             :                  * until you fix the underlying situation so that the truncation will
    1029                 :             :                  * succeed. Alternatively, we could update the minimum recovery point
    1030                 :             :                  * after truncation, but that would leave a small window where the
    1031                 :             :                  * WAL-first rule could be violated.
    1032                 :             :                  */
    1033                 :           0 :                 XLogFlush(lsn);
    1034                 :             : 
    1035                 :             :                 /* Prepare for truncation of MAIN fork */
    1036         [ #  # ]:           0 :                 if ((xlrec->flags & SMGR_TRUNCATE_HEAP) != 0)
    1037                 :             :                 {
    1038                 :           0 :                         forks[nforks] = MAIN_FORKNUM;
    1039                 :           0 :                         old_blocks[nforks] = smgrnblocks(reln, MAIN_FORKNUM);
    1040                 :           0 :                         blocks[nforks] = xlrec->blkno;
    1041                 :           0 :                         nforks++;
    1042                 :             : 
    1043                 :             :                         /* Also tell xlogutils.c about it */
    1044                 :           0 :                         XLogTruncateRelation(xlrec->rlocator, MAIN_FORKNUM, xlrec->blkno);
    1045                 :           0 :                 }
    1046                 :             : 
    1047                 :             :                 /* Prepare for truncation of FSM and VM too */
    1048                 :           0 :                 rel = CreateFakeRelcacheEntry(xlrec->rlocator);
    1049                 :             : 
    1050   [ #  #  #  # ]:           0 :                 if ((xlrec->flags & SMGR_TRUNCATE_FSM) != 0 &&
    1051                 :           0 :                         smgrexists(reln, FSM_FORKNUM))
    1052                 :             :                 {
    1053                 :           0 :                         blocks[nforks] = FreeSpaceMapPrepareTruncateRel(rel, xlrec->blkno);
    1054         [ #  # ]:           0 :                         if (BlockNumberIsValid(blocks[nforks]))
    1055                 :             :                         {
    1056                 :           0 :                                 forks[nforks] = FSM_FORKNUM;
    1057                 :           0 :                                 old_blocks[nforks] = smgrnblocks(reln, FSM_FORKNUM);
    1058                 :           0 :                                 nforks++;
    1059                 :           0 :                                 need_fsm_vacuum = true;
    1060                 :           0 :                         }
    1061                 :           0 :                 }
    1062   [ #  #  #  # ]:           0 :                 if ((xlrec->flags & SMGR_TRUNCATE_VM) != 0 &&
    1063                 :           0 :                         smgrexists(reln, VISIBILITYMAP_FORKNUM))
    1064                 :             :                 {
    1065                 :           0 :                         blocks[nforks] = visibilitymap_prepare_truncate(rel, xlrec->blkno);
    1066         [ #  # ]:           0 :                         if (BlockNumberIsValid(blocks[nforks]))
    1067                 :             :                         {
    1068                 :           0 :                                 forks[nforks] = VISIBILITYMAP_FORKNUM;
    1069                 :           0 :                                 old_blocks[nforks] = smgrnblocks(reln, VISIBILITYMAP_FORKNUM);
    1070                 :           0 :                                 nforks++;
    1071                 :           0 :                         }
    1072                 :           0 :                 }
    1073                 :             : 
    1074                 :             :                 /* Do the real work to truncate relation forks */
    1075         [ #  # ]:           0 :                 if (nforks > 0)
    1076                 :             :                 {
    1077                 :           0 :                         START_CRIT_SECTION();
    1078                 :           0 :                         smgrtruncate(reln, forks, nforks, old_blocks, blocks);
    1079         [ #  # ]:           0 :                         END_CRIT_SECTION();
    1080                 :           0 :                 }
    1081                 :             : 
    1082                 :             :                 /*
    1083                 :             :                  * Update upper-level FSM pages to account for the truncation. This is
    1084                 :             :                  * important because the just-truncated pages were likely marked as
    1085                 :             :                  * all-free, and would be preferentially selected.
    1086                 :             :                  */
    1087         [ #  # ]:           0 :                 if (need_fsm_vacuum)
    1088                 :           0 :                         FreeSpaceMapVacuumRange(rel, xlrec->blkno,
    1089                 :             :                                                                         InvalidBlockNumber);
    1090                 :             : 
    1091                 :           0 :                 FreeFakeRelcacheEntry(rel);
    1092                 :           0 :         }
    1093                 :             :         else
    1094   [ #  #  #  # ]:           0 :                 elog(PANIC, "smgr_redo: unknown op code %u", info);
    1095                 :           0 : }
        

Generated by: LCOV version 2.3.2-1