LCOV - code coverage report
Current view: top level - src/backend/catalog - storage.c (source / functions) Hit Total Coverage
Test: PostgreSQL 18devel Lines: 304 313 97.1 %
Date: 2024-11-21 08:14:44 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       69650 : AddPendingSync(const RelFileLocator *rlocator)
      86             : {
      87             :     PendingRelSync *pending;
      88             :     bool        found;
      89             : 
      90             :     /* create the hash if not yet */
      91       69650 :     if (!pendingSyncHash)
      92             :     {
      93             :         HASHCTL     ctl;
      94             : 
      95       11802 :         ctl.keysize = sizeof(RelFileLocator);
      96       11802 :         ctl.entrysize = sizeof(PendingRelSync);
      97       11802 :         ctl.hcxt = TopTransactionContext;
      98       11802 :         pendingSyncHash = hash_create("pending sync hash", 16, &ctl,
      99             :                                       HASH_ELEM | HASH_BLOBS | HASH_CONTEXT);
     100             :     }
     101             : 
     102       69650 :     pending = hash_search(pendingSyncHash, rlocator, HASH_ENTER, &found);
     103             :     Assert(!found);
     104       69650 :     pending->is_truncated = false;
     105       69650 : }
     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      209820 : 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      209820 :     switch (relpersistence)
     131             :     {
     132        5998 :         case RELPERSISTENCE_TEMP:
     133        5998 :             procNumber = ProcNumberForTempRelations();
     134        5998 :             needs_wal = false;
     135        5998 :             break;
     136         530 :         case RELPERSISTENCE_UNLOGGED:
     137         530 :             procNumber = INVALID_PROC_NUMBER;
     138         530 :             needs_wal = false;
     139         530 :             break;
     140      203292 :         case RELPERSISTENCE_PERMANENT:
     141      203292 :             procNumber = INVALID_PROC_NUMBER;
     142      203292 :             needs_wal = true;
     143      203292 :             break;
     144           0 :         default:
     145           0 :             elog(ERROR, "invalid relpersistence: %c", relpersistence);
     146             :             return NULL;        /* placate compiler */
     147             :     }
     148             : 
     149      209820 :     srel = smgropen(rlocator, procNumber);
     150      209820 :     smgrcreate(srel, MAIN_FORKNUM, false);
     151             : 
     152      209820 :     if (needs_wal)
     153      203292 :         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      209820 :     if (register_delete)
     160             :     {
     161             :         PendingRelDelete *pending;
     162             : 
     163             :         pending = (PendingRelDelete *)
     164      113020 :             MemoryContextAlloc(TopMemoryContext, sizeof(PendingRelDelete));
     165      113020 :         pending->rlocator = rlocator;
     166      113020 :         pending->procNumber = procNumber;
     167      113020 :         pending->atCommit = false;   /* delete if abort */
     168      113020 :         pending->nestLevel = GetCurrentTransactionNestLevel();
     169      113020 :         pending->next = pendingDeletes;
     170      113020 :         pendingDeletes = pending;
     171             :     }
     172             : 
     173      209820 :     if (relpersistence == RELPERSISTENCE_PERMANENT && !XLogIsNeeded())
     174             :     {
     175             :         Assert(procNumber == INVALID_PROC_NUMBER);
     176       66054 :         AddPendingSync(&rlocator);
     177             :     }
     178             : 
     179      209820 :     return srel;
     180             : }
     181             : 
     182             : /*
     183             :  * Perform XLogInsert of an XLOG_SMGR_CREATE record to WAL.
     184             :  */
     185             : void
     186      235806 : 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      235806 :     xlrec.rlocator = *rlocator;
     194      235806 :     xlrec.forkNum = forkNum;
     195             : 
     196      235806 :     XLogBeginInsert();
     197      235806 :     XLogRegisterData((char *) &xlrec, sizeof(xlrec));
     198      235806 :     XLogInsert(RM_SMGR_ID, XLOG_SMGR_CREATE | XLR_SPECIAL_REL_UPDATE);
     199      235806 : }
     200             : 
     201             : /*
     202             :  * RelationDropStorage
     203             :  *      Schedule unlinking of physical storage at transaction commit.
     204             :  */
     205             : void
     206       69734 : 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       69734 :         MemoryContextAlloc(TopMemoryContext, sizeof(PendingRelDelete));
     213       69734 :     pending->rlocator = rel->rd_locator;
     214       69734 :     pending->procNumber = rel->rd_backend;
     215       69734 :     pending->atCommit = true;    /* delete if commit */
     216       69734 :     pending->nestLevel = GetCurrentTransactionNestLevel();
     217       69734 :     pending->next = pendingDeletes;
     218       69734 :     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       69734 :     RelationCloseSmgr(rel);
     231       69734 : }
     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       12522 : RelationPreserveStorage(RelFileLocator rlocator, bool atCommit)
     252             : {
     253             :     PendingRelDelete *pending;
     254             :     PendingRelDelete *prev;
     255             :     PendingRelDelete *next;
     256             : 
     257       12522 :     prev = NULL;
     258       74260 :     for (pending = pendingDeletes; pending != NULL; pending = next)
     259             :     {
     260       61738 :         next = pending->next;
     261       61738 :         if (RelFileLocatorEquals(rlocator, pending->rlocator)
     262        1122 :             && pending->atCommit == atCommit)
     263             :         {
     264             :             /* unlink and delete list entry */
     265        1116 :             if (prev)
     266         816 :                 prev->next = next;
     267             :             else
     268         300 :                 pendingDeletes = next;
     269        1116 :             pfree(pending);
     270             :             /* prev does not change */
     271             :         }
     272             :         else
     273             :         {
     274             :             /* unrelated entry, don't touch it */
     275       60622 :             prev = pending;
     276             :         }
     277             :     }
     278       12522 : }
     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        1042 : RelationTruncate(Relation rel, BlockNumber nblocks)
     289             : {
     290             :     bool        fsm;
     291             :     bool        vm;
     292        1042 :     bool        need_fsm_vacuum = false;
     293             :     ForkNumber  forks[MAX_FORKNUM];
     294             :     BlockNumber blocks[MAX_FORKNUM];
     295        1042 :     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        1042 :     reln = RelationGetSmgr(rel);
     303        1042 :     reln->smgr_targblock = InvalidBlockNumber;
     304        5210 :     for (int i = 0; i <= MAX_FORKNUM; ++i)
     305        4168 :         reln->smgr_cached_nblocks[i] = InvalidBlockNumber;
     306             : 
     307             :     /* Prepare for truncation of MAIN fork of the relation */
     308        1042 :     forks[nforks] = MAIN_FORKNUM;
     309        1042 :     blocks[nforks] = nblocks;
     310        1042 :     nforks++;
     311             : 
     312             :     /* Prepare for truncation of the FSM if it exists */
     313        1042 :     fsm = smgrexists(RelationGetSmgr(rel), FSM_FORKNUM);
     314        1042 :     if (fsm)
     315             :     {
     316         262 :         blocks[nforks] = FreeSpaceMapPrepareTruncateRel(rel, nblocks);
     317         262 :         if (BlockNumberIsValid(blocks[nforks]))
     318             :         {
     319         262 :             forks[nforks] = FSM_FORKNUM;
     320         262 :             nforks++;
     321         262 :             need_fsm_vacuum = true;
     322             :         }
     323             :     }
     324             : 
     325             :     /* Prepare for truncation of the visibility map too if it exists */
     326        1042 :     vm = smgrexists(RelationGetSmgr(rel), VISIBILITYMAP_FORKNUM);
     327        1042 :     if (vm)
     328             :     {
     329         262 :         blocks[nforks] = visibilitymap_prepare_truncate(rel, nblocks);
     330         262 :         if (BlockNumberIsValid(blocks[nforks]))
     331             :         {
     332          98 :             forks[nforks] = VISIBILITYMAP_FORKNUM;
     333          98 :             nforks++;
     334             :         }
     335             :     }
     336             : 
     337        1042 :     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        1042 :     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        1042 :     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         378 :         xlrec.blkno = nblocks;
     373         378 :         xlrec.rlocator = rel->rd_locator;
     374         378 :         xlrec.flags = SMGR_TRUNCATE_ALL;
     375             : 
     376         378 :         XLogBeginInsert();
     377         378 :         XLogRegisterData((char *) &xlrec, sizeof(xlrec));
     378             : 
     379         378 :         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         378 :         if (fsm || vm)
     390         242 :             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        1042 :     smgrtruncate(RelationGetSmgr(rel), forks, nforks, blocks);
     399             : 
     400             :     /* We've done all the critical work, so checkpoints are OK now. */
     401        1042 :     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        1042 :     if (need_fsm_vacuum)
     413         262 :         FreeSpaceMapVacuumRange(rel, nblocks, InvalidBlockNumber);
     414        1042 : }
     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        1042 : RelationPreTruncate(Relation rel)
     425             : {
     426             :     PendingRelSync *pending;
     427             : 
     428        1042 :     if (!pendingSyncHash)
     429        1036 :         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         178 : 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         178 :     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         194 :     use_wal = XLogIsNeeded() &&
     476          16 :         (relpersistence == RELPERSISTENCE_PERMANENT || copying_initfork);
     477             : 
     478         178 :     bulkstate = smgr_bulk_start_smgr(dst, forkNum, use_wal);
     479             : 
     480         178 :     nblocks = smgrnblocks(src, forkNum);
     481             : 
     482        1372 :     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        1194 :         CHECK_FOR_INTERRUPTS();
     488             : 
     489        1194 :         buf = smgr_bulk_get_buf(bulkstate);
     490        1194 :         smgrread(src, forkNum, blkno, (Page) buf);
     491             : 
     492        1194 :         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        1194 :         smgr_bulk_write(bulkstate, blkno, buf, false);
     518             :     }
     519         178 :     smgr_bulk_finish(bulkstate);
     520         178 : }
     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      162114 : RelFileLocatorSkippingWAL(RelFileLocator rlocator)
     533             : {
     534      173798 :     if (!pendingSyncHash ||
     535       11684 :         hash_search(pendingSyncHash, &rlocator, HASH_FIND, NULL) == NULL)
     536      158536 :         return false;
     537             : 
     538        3578 :     return true;
     539             : }
     540             : 
     541             : /*
     542             :  * EstimatePendingSyncsSpace
     543             :  *      Estimate space needed to pass syncs to parallel workers.
     544             :  */
     545             : Size
     546         886 : EstimatePendingSyncsSpace(void)
     547             : {
     548             :     long        entries;
     549             : 
     550         886 :     entries = pendingSyncHash ? hash_get_num_entries(pendingSyncHash) : 0;
     551         886 :     return mul_size(1 + entries, sizeof(RelFileLocator));
     552             : }
     553             : 
     554             : /*
     555             :  * SerializePendingSyncs
     556             :  *      Serialize syncs for parallel workers.
     557             :  */
     558             : void
     559         886 : 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         886 :     RelFileLocator *dest = (RelFileLocator *) startAddress;
     568             : 
     569         886 :     if (!pendingSyncHash)
     570         694 :         goto terminate;
     571             : 
     572             :     /* Create temporary hash to collect active relfilelocators */
     573         192 :     ctl.keysize = sizeof(RelFileLocator);
     574         192 :     ctl.entrysize = sizeof(RelFileLocator);
     575         192 :     ctl.hcxt = CurrentMemoryContext;
     576         192 :     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         192 :     hash_seq_init(&scan, pendingSyncHash);
     582        1654 :     while ((sync = (PendingRelSync *) hash_seq_search(&scan)))
     583        1462 :         (void) hash_search(tmphash, &sync->rlocator, HASH_ENTER, NULL);
     584             : 
     585             :     /* remove deleted rnodes */
     586        1994 :     for (delete = pendingDeletes; delete != NULL; delete = delete->next)
     587        1802 :         if (delete->atCommit)
     588         330 :             (void) hash_search(tmphash, &delete->rlocator,
     589             :                                HASH_REMOVE, NULL);
     590             : 
     591         192 :     hash_seq_init(&scan, tmphash);
     592        1332 :     while ((src = (RelFileLocator *) hash_seq_search(&scan)))
     593        1140 :         *dest++ = *src;
     594             : 
     595         192 :     hash_destroy(tmphash);
     596             : 
     597         886 : terminate:
     598         886 :     MemSet(dest, 0, sizeof(RelFileLocator));
     599         886 : }
     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        2712 : RestorePendingSyncs(char *startAddress)
     611             : {
     612             :     RelFileLocator *rlocator;
     613             : 
     614             :     Assert(pendingSyncHash == NULL);
     615        6308 :     for (rlocator = (RelFileLocator *) startAddress; rlocator->relNumber != 0;
     616        3596 :          rlocator++)
     617        3596 :         AddPendingSync(rlocator);
     618        2712 : }
     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      755500 : smgrDoPendingDeletes(bool isCommit)
     633             : {
     634      755500 :     int         nestLevel = GetCurrentTransactionNestLevel();
     635             :     PendingRelDelete *pending;
     636             :     PendingRelDelete *prev;
     637             :     PendingRelDelete *next;
     638      755500 :     int         nrels = 0,
     639      755500 :                 maxrels = 0;
     640      755500 :     SMgrRelation *srels = NULL;
     641             : 
     642      755500 :     prev = NULL;
     643      945246 :     for (pending = pendingDeletes; pending != NULL; pending = next)
     644             :     {
     645      189746 :         next = pending->next;
     646      189746 :         if (pending->nestLevel < nestLevel)
     647             :         {
     648             :             /* outer-level entries should not be processed yet */
     649        8228 :             prev = pending;
     650             :         }
     651             :         else
     652             :         {
     653             :             /* unlink list entry first, so we don't retry on failure */
     654      181518 :             if (prev)
     655           0 :                 prev->next = next;
     656             :             else
     657      181518 :                 pendingDeletes = next;
     658             :             /* do deletion if called for */
     659      181518 :             if (pending->atCommit == isCommit)
     660             :             {
     661             :                 SMgrRelation srel;
     662             : 
     663       72418 :                 srel = smgropen(pending->rlocator, pending->procNumber);
     664             : 
     665             :                 /* allocate the initial array, or extend it, if needed */
     666       72418 :                 if (maxrels == 0)
     667             :                 {
     668       20762 :                     maxrels = 8;
     669       20762 :                     srels = palloc(sizeof(SMgrRelation) * maxrels);
     670             :                 }
     671       51656 :                 else if (maxrels <= nrels)
     672             :                 {
     673        1702 :                     maxrels *= 2;
     674        1702 :                     srels = repalloc(srels, sizeof(SMgrRelation) * maxrels);
     675             :                 }
     676             : 
     677       72418 :                 srels[nrels++] = srel;
     678             :             }
     679             :             /* must explicitly free the list entry */
     680      181518 :             pfree(pending);
     681             :             /* prev does not change */
     682             :         }
     683             :     }
     684             : 
     685      755500 :     if (nrels > 0)
     686             :     {
     687       20762 :         smgrdounlinkall(srels, nrels, false);
     688             : 
     689       93180 :         for (int i = 0; i < nrels; i++)
     690       72418 :             smgrclose(srels[i]);
     691             : 
     692       20762 :         pfree(srels);
     693             :     }
     694      755500 : }
     695             : 
     696             : /*
     697             :  *  smgrDoPendingSyncs() -- Take care of relation syncs at end of xact.
     698             :  */
     699             : void
     700      747382 : smgrDoPendingSyncs(bool isCommit, bool isParallelWorker)
     701             : {
     702             :     PendingRelDelete *pending;
     703      747382 :     int         nrels = 0,
     704      747382 :                 maxrels = 0;
     705      747382 :     SMgrRelation *srels = NULL;
     706             :     HASH_SEQ_STATUS scan;
     707             :     PendingRelSync *pendingsync;
     708             : 
     709             :     Assert(GetCurrentTransactionNestLevel() == 1);
     710             : 
     711      747382 :     if (!pendingSyncHash)
     712      736508 :         return;                 /* no relation needs sync */
     713             : 
     714             :     /* Abort -- just throw away all pending syncs */
     715       11802 :     if (!isCommit)
     716             :     {
     717         530 :         pendingSyncHash = NULL;
     718         530 :         return;
     719             :     }
     720             : 
     721             :     AssertPendingSyncs_RelationCache();
     722             : 
     723             :     /* Parallel worker -- just throw away all pending syncs */
     724       11272 :     if (isParallelWorker)
     725             :     {
     726         398 :         pendingSyncHash = NULL;
     727         398 :         return;
     728             :     }
     729             : 
     730             :     /* Skip syncing nodes that smgrDoPendingDeletes() will delete. */
     731       43328 :     for (pending = pendingDeletes; pending != NULL; pending = pending->next)
     732       32454 :         if (pending->atCommit)
     733        7292 :             (void) hash_search(pendingSyncHash, &pending->rlocator,
     734             :                                HASH_REMOVE, NULL);
     735             : 
     736       10874 :     hash_seq_init(&scan, pendingSyncHash);
     737       75340 :     while ((pendingsync = (PendingRelSync *) hash_seq_search(&scan)))
     738             :     {
     739             :         ForkNumber  fork;
     740             :         BlockNumber nblocks[MAX_FORKNUM + 1];
     741       64466 :         BlockNumber total_blocks = 0;
     742             :         SMgrRelation srel;
     743             : 
     744       64466 :         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       64466 :         if (!pendingsync->is_truncated)
     755             :         {
     756      322330 :             for (fork = 0; fork <= MAX_FORKNUM; fork++)
     757             :             {
     758      257864 :                 if (smgrexists(srel, fork))
     759             :                 {
     760       77990 :                     BlockNumber n = smgrnblocks(srel, fork);
     761             : 
     762             :                     /* we shouldn't come here for unlogged relations */
     763             :                     Assert(fork != INIT_FORKNUM);
     764       77990 :                     nblocks[fork] = n;
     765       77990 :                     total_blocks += n;
     766             :                 }
     767             :                 else
     768      179874 :                     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       64466 :         if (pendingsync->is_truncated ||
     785       64466 :             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      322240 :             for (fork = 0; fork <= MAX_FORKNUM; fork++)
     805             :             {
     806      257792 :                 int         n = nblocks[fork];
     807             :                 Relation    rel;
     808             : 
     809      257792 :                 if (!BlockNumberIsValid(n))
     810      179822 :                     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       77970 :                 rel = CreateFakeRelcacheEntry(srel->smgr_rlocator.locator);
     819       77970 :                 log_newpage_range(rel, fork, 0, n, false);
     820       77970 :                 FreeFakeRelcacheEntry(rel);
     821             :             }
     822             :         }
     823             :     }
     824             : 
     825       10874 :     pendingSyncHash = NULL;
     826             : 
     827       10874 :     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      709530 : smgrGetPendingDeletes(bool forCommit, RelFileLocator **ptr)
     853             : {
     854      709530 :     int         nestLevel = GetCurrentTransactionNestLevel();
     855             :     int         nrels;
     856             :     RelFileLocator *rptr;
     857             :     PendingRelDelete *pending;
     858             : 
     859      709530 :     nrels = 0;
     860      896432 :     for (pending = pendingDeletes; pending != NULL; pending = pending->next)
     861             :     {
     862      186902 :         if (pending->nestLevel >= nestLevel && pending->atCommit == forCommit
     863       72538 :             && pending->procNumber == INVALID_PROC_NUMBER)
     864       66540 :             nrels++;
     865             :     }
     866      709530 :     if (nrels == 0)
     867             :     {
     868      690214 :         *ptr = NULL;
     869      690214 :         return 0;
     870             :     }
     871       19316 :     rptr = (RelFileLocator *) palloc(nrels * sizeof(RelFileLocator));
     872       19316 :     *ptr = rptr;
     873      102616 :     for (pending = pendingDeletes; pending != NULL; pending = pending->next)
     874             :     {
     875       83300 :         if (pending->nestLevel >= nestLevel && pending->atCommit == forCommit
     876       66688 :             && pending->procNumber == INVALID_PROC_NUMBER)
     877             :         {
     878       66540 :             *rptr = pending->rlocator;
     879       66540 :             rptr++;
     880             :         }
     881             :     }
     882       19316 :     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         744 : PostPrepare_smgr(void)
     894             : {
     895             :     PendingRelDelete *pending;
     896             :     PendingRelDelete *next;
     897             : 
     898         864 :     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         744 : }
     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       10720 : AtSubCommit_smgr(void)
     915             : {
     916       10720 :     int         nestLevel = GetCurrentTransactionNestLevel();
     917             :     PendingRelDelete *pending;
     918             : 
     919       11176 :     for (pending = pendingDeletes; pending != NULL; pending = pending->next)
     920             :     {
     921         456 :         if (pending->nestLevel >= nestLevel)
     922         214 :             pending->nestLevel = nestLevel - 1;
     923             :     }
     924       10720 : }
     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        9268 : AtSubAbort_smgr(void)
     935             : {
     936        9268 :     smgrDoPendingDeletes(false);
     937        9268 : }
     938             : 
     939             : void
     940       30860 : smgr_redo(XLogReaderState *record)
     941             : {
     942       30860 :     XLogRecPtr  lsn = record->EndRecPtr;
     943       30860 :     uint8       info = XLogRecGetInfo(record) & ~XLR_INFO_MASK;
     944             : 
     945             :     /* Backup blocks are not used in smgr records */
     946             :     Assert(!XLogRecHasAnyBlockRefs(record));
     947             : 
     948       30860 :     if (info == XLOG_SMGR_CREATE)
     949             :     {
     950       30766 :         xl_smgr_create *xlrec = (xl_smgr_create *) XLogRecGetData(record);
     951             :         SMgrRelation reln;
     952             : 
     953       30766 :         reln = smgropen(xlrec->rlocator, INVALID_PROC_NUMBER);
     954       30766 :         smgrcreate(reln, xlrec->forkNum, true);
     955             :     }
     956          94 :     else if (info == XLOG_SMGR_TRUNCATE)
     957             :     {
     958          94 :         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          94 :         int         nforks = 0;
     964          94 :         bool        need_fsm_vacuum = false;
     965             : 
     966          94 :         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          94 :         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          94 :         XLogFlush(lsn);
     992             : 
     993             :         /* Prepare for truncation of MAIN fork */
     994          94 :         if ((xlrec->flags & SMGR_TRUNCATE_HEAP) != 0)
     995             :         {
     996          94 :             forks[nforks] = MAIN_FORKNUM;
     997          94 :             blocks[nforks] = xlrec->blkno;
     998          94 :             nforks++;
     999             : 
    1000             :             /* Also tell xlogutils.c about it */
    1001          94 :             XLogTruncateRelation(xlrec->rlocator, MAIN_FORKNUM, xlrec->blkno);
    1002             :         }
    1003             : 
    1004             :         /* Prepare for truncation of FSM and VM too */
    1005          94 :         rel = CreateFakeRelcacheEntry(xlrec->rlocator);
    1006             : 
    1007         188 :         if ((xlrec->flags & SMGR_TRUNCATE_FSM) != 0 &&
    1008          94 :             smgrexists(reln, FSM_FORKNUM))
    1009             :         {
    1010          58 :             blocks[nforks] = FreeSpaceMapPrepareTruncateRel(rel, xlrec->blkno);
    1011          58 :             if (BlockNumberIsValid(blocks[nforks]))
    1012             :             {
    1013          58 :                 forks[nforks] = FSM_FORKNUM;
    1014          58 :                 nforks++;
    1015          58 :                 need_fsm_vacuum = true;
    1016             :             }
    1017             :         }
    1018         188 :         if ((xlrec->flags & SMGR_TRUNCATE_VM) != 0 &&
    1019          94 :             smgrexists(reln, VISIBILITYMAP_FORKNUM))
    1020             :         {
    1021          48 :             blocks[nforks] = visibilitymap_prepare_truncate(rel, xlrec->blkno);
    1022          48 :             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          94 :         if (nforks > 0)
    1031          94 :             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          94 :         if (need_fsm_vacuum)
    1039          58 :             FreeSpaceMapVacuumRange(rel, xlrec->blkno,
    1040             :                                     InvalidBlockNumber);
    1041             : 
    1042          94 :         FreeFakeRelcacheEntry(rel);
    1043             :     }
    1044             :     else
    1045           0 :         elog(PANIC, "smgr_redo: unknown op code %u", info);
    1046       30860 : }

Generated by: LCOV version 1.14