LCOV - code coverage report
Current view: top level - src/backend/catalog - storage.c (source / functions) Hit Total Coverage
Test: PostgreSQL 17devel Lines: 304 313 97.1 %
Date: 2024-04-26 00:11:43 Functions: 19 19 100.0 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.14