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

Generated by: LCOV version 1.13