LCOV - code coverage report
Current view: top level - src/backend/storage/smgr - md.c (source / functions) Hit Total Coverage
Test: PostgreSQL 12devel Lines: 331 464 71.3 %
Date: 2018-10-17 03:20:55 Functions: 28 30 93.3 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*-------------------------------------------------------------------------
       2             :  *
       3             :  * md.c
       4             :  *    This code manages relations that reside on magnetic disk.
       5             :  *
       6             :  * Or at least, that was what the Berkeley folk had in mind when they named
       7             :  * this file.  In reality, what this code provides is an interface from
       8             :  * the smgr API to Unix-like filesystem APIs, so it will work with any type
       9             :  * of device for which the operating system provides filesystem support.
      10             :  * It doesn't matter whether the bits are on spinning rust or some other
      11             :  * storage technology.
      12             :  *
      13             :  * Portions Copyright (c) 1996-2018, PostgreSQL Global Development Group
      14             :  * Portions Copyright (c) 1994, Regents of the University of California
      15             :  *
      16             :  *
      17             :  * IDENTIFICATION
      18             :  *    src/backend/storage/smgr/md.c
      19             :  *
      20             :  *-------------------------------------------------------------------------
      21             :  */
      22             : #include "postgres.h"
      23             : 
      24             : #include <unistd.h>
      25             : #include <fcntl.h>
      26             : #include <sys/file.h>
      27             : 
      28             : #include "miscadmin.h"
      29             : #include "access/xlogutils.h"
      30             : #include "access/xlog.h"
      31             : #include "pgstat.h"
      32             : #include "portability/instr_time.h"
      33             : #include "postmaster/bgwriter.h"
      34             : #include "storage/fd.h"
      35             : #include "storage/bufmgr.h"
      36             : #include "storage/relfilenode.h"
      37             : #include "storage/smgr.h"
      38             : #include "utils/hsearch.h"
      39             : #include "utils/memutils.h"
      40             : #include "pg_trace.h"
      41             : 
      42             : 
      43             : /* intervals for calling AbsorbFsyncRequests in mdsync and mdpostckpt */
      44             : #define FSYNCS_PER_ABSORB       10
      45             : #define UNLINKS_PER_ABSORB      10
      46             : 
      47             : /*
      48             :  * Special values for the segno arg to RememberFsyncRequest.
      49             :  *
      50             :  * Note that CompactCheckpointerRequestQueue assumes that it's OK to remove an
      51             :  * fsync request from the queue if an identical, subsequent request is found.
      52             :  * See comments there before making changes here.
      53             :  */
      54             : #define FORGET_RELATION_FSYNC   (InvalidBlockNumber)
      55             : #define FORGET_DATABASE_FSYNC   (InvalidBlockNumber-1)
      56             : #define UNLINK_RELATION_REQUEST (InvalidBlockNumber-2)
      57             : 
      58             : /*
      59             :  * On Windows, we have to interpret EACCES as possibly meaning the same as
      60             :  * ENOENT, because if a file is unlinked-but-not-yet-gone on that platform,
      61             :  * that's what you get.  Ugh.  This code is designed so that we don't
      62             :  * actually believe these cases are okay without further evidence (namely,
      63             :  * a pending fsync request getting canceled ... see mdsync).
      64             :  */
      65             : #ifndef WIN32
      66             : #define FILE_POSSIBLY_DELETED(err)  ((err) == ENOENT)
      67             : #else
      68             : #define FILE_POSSIBLY_DELETED(err)  ((err) == ENOENT || (err) == EACCES)
      69             : #endif
      70             : 
      71             : /*
      72             :  *  The magnetic disk storage manager keeps track of open file
      73             :  *  descriptors in its own descriptor pool.  This is done to make it
      74             :  *  easier to support relations that are larger than the operating
      75             :  *  system's file size limit (often 2GBytes).  In order to do that,
      76             :  *  we break relations up into "segment" files that are each shorter than
      77             :  *  the OS file size limit.  The segment size is set by the RELSEG_SIZE
      78             :  *  configuration constant in pg_config.h.
      79             :  *
      80             :  *  On disk, a relation must consist of consecutively numbered segment
      81             :  *  files in the pattern
      82             :  *      -- Zero or more full segments of exactly RELSEG_SIZE blocks each
      83             :  *      -- Exactly one partial segment of size 0 <= size < RELSEG_SIZE blocks
      84             :  *      -- Optionally, any number of inactive segments of size 0 blocks.
      85             :  *  The full and partial segments are collectively the "active" segments.
      86             :  *  Inactive segments are those that once contained data but are currently
      87             :  *  not needed because of an mdtruncate() operation.  The reason for leaving
      88             :  *  them present at size zero, rather than unlinking them, is that other
      89             :  *  backends and/or the checkpointer might be holding open file references to
      90             :  *  such segments.  If the relation expands again after mdtruncate(), such
      91             :  *  that a deactivated segment becomes active again, it is important that
      92             :  *  such file references still be valid --- else data might get written
      93             :  *  out to an unlinked old copy of a segment file that will eventually
      94             :  *  disappear.
      95             :  *
      96             :  *  File descriptors are stored in the per-fork md_seg_fds arrays inside
      97             :  *  SMgrRelation. The length of these arrays is stored in md_num_open_segs.
      98             :  *  Note that a fork's md_num_open_segs having a specific value does not
      99             :  *  necessarily mean the relation doesn't have additional segments; we may
     100             :  *  just not have opened the next segment yet.  (We could not have "all
     101             :  *  segments are in the array" as an invariant anyway, since another backend
     102             :  *  could extend the relation while we aren't looking.)  We do not have
     103             :  *  entries for inactive segments, however; as soon as we find a partial
     104             :  *  segment, we assume that any subsequent segments are inactive.
     105             :  *
     106             :  *  The entire MdfdVec array is palloc'd in the MdCxt memory context.
     107             :  */
     108             : 
     109             : typedef struct _MdfdVec
     110             : {
     111             :     File        mdfd_vfd;       /* fd number in fd.c's pool */
     112             :     BlockNumber mdfd_segno;     /* segment number, from 0 */
     113             : } MdfdVec;
     114             : 
     115             : static MemoryContext MdCxt;     /* context for all MdfdVec objects */
     116             : 
     117             : 
     118             : /*
     119             :  * In some contexts (currently, standalone backends and the checkpointer)
     120             :  * we keep track of pending fsync operations: we need to remember all relation
     121             :  * segments that have been written since the last checkpoint, so that we can
     122             :  * fsync them down to disk before completing the next checkpoint.  This hash
     123             :  * table remembers the pending operations.  We use a hash table mostly as
     124             :  * a convenient way of merging duplicate requests.
     125             :  *
     126             :  * We use a similar mechanism to remember no-longer-needed files that can
     127             :  * be deleted after the next checkpoint, but we use a linked list instead of
     128             :  * a hash table, because we don't expect there to be any duplicate requests.
     129             :  *
     130             :  * These mechanisms are only used for non-temp relations; we never fsync
     131             :  * temp rels, nor do we need to postpone their deletion (see comments in
     132             :  * mdunlink).
     133             :  *
     134             :  * (Regular backends do not track pending operations locally, but forward
     135             :  * them to the checkpointer.)
     136             :  */
     137             : typedef uint16 CycleCtr;        /* can be any convenient integer size */
     138             : 
     139             : typedef struct
     140             : {
     141             :     RelFileNode rnode;          /* hash table key (must be first!) */
     142             :     CycleCtr    cycle_ctr;      /* mdsync_cycle_ctr of oldest request */
     143             :     /* requests[f] has bit n set if we need to fsync segment n of fork f */
     144             :     Bitmapset  *requests[MAX_FORKNUM + 1];
     145             :     /* canceled[f] is true if we canceled fsyncs for fork "recently" */
     146             :     bool        canceled[MAX_FORKNUM + 1];
     147             : } PendingOperationEntry;
     148             : 
     149             : typedef struct
     150             : {
     151             :     RelFileNode rnode;          /* the dead relation to delete */
     152             :     CycleCtr    cycle_ctr;      /* mdckpt_cycle_ctr when request was made */
     153             : } PendingUnlinkEntry;
     154             : 
     155             : static HTAB *pendingOpsTable = NULL;
     156             : static List *pendingUnlinks = NIL;
     157             : static MemoryContext pendingOpsCxt; /* context for the above  */
     158             : 
     159             : static CycleCtr mdsync_cycle_ctr = 0;
     160             : static CycleCtr mdckpt_cycle_ctr = 0;
     161             : 
     162             : 
     163             : /*** behavior for mdopen & _mdfd_getseg ***/
     164             : /* ereport if segment not present */
     165             : #define EXTENSION_FAIL              (1 << 0)
     166             : /* return NULL if segment not present */
     167             : #define EXTENSION_RETURN_NULL       (1 << 1)
     168             : /* create new segments as needed */
     169             : #define EXTENSION_CREATE            (1 << 2)
     170             : /* create new segments if needed during recovery */
     171             : #define EXTENSION_CREATE_RECOVERY   (1 << 3)
     172             : /*
     173             :  * Allow opening segments which are preceded by segments smaller than
     174             :  * RELSEG_SIZE, e.g. inactive segments (see above). Note that this is breaks
     175             :  * mdnblocks() and related functionality henceforth - which currently is ok,
     176             :  * because this is only required in the checkpointer which never uses
     177             :  * mdnblocks().
     178             :  */
     179             : #define EXTENSION_DONT_CHECK_SIZE   (1 << 4)
     180             : 
     181             : 
     182             : /* local routines */
     183             : static void mdunlinkfork(RelFileNodeBackend rnode, ForkNumber forkNum,
     184             :              bool isRedo);
     185             : static MdfdVec *mdopen(SMgrRelation reln, ForkNumber forknum, int behavior);
     186             : static void register_dirty_segment(SMgrRelation reln, ForkNumber forknum,
     187             :                        MdfdVec *seg);
     188             : static void register_unlink(RelFileNodeBackend rnode);
     189             : static void _fdvec_resize(SMgrRelation reln,
     190             :               ForkNumber forknum,
     191             :               int nseg);
     192             : static char *_mdfd_segpath(SMgrRelation reln, ForkNumber forknum,
     193             :               BlockNumber segno);
     194             : static MdfdVec *_mdfd_openseg(SMgrRelation reln, ForkNumber forkno,
     195             :               BlockNumber segno, int oflags);
     196             : static MdfdVec *_mdfd_getseg(SMgrRelation reln, ForkNumber forkno,
     197             :              BlockNumber blkno, bool skipFsync, int behavior);
     198             : static BlockNumber _mdnblocks(SMgrRelation reln, ForkNumber forknum,
     199             :            MdfdVec *seg);
     200             : 
     201             : 
     202             : /*
     203             :  *  mdinit() -- Initialize private state for magnetic disk storage manager.
     204             :  */
     205             : void
     206       11332 : mdinit(void)
     207             : {
     208       11332 :     MdCxt = AllocSetContextCreate(TopMemoryContext,
     209             :                                   "MdSmgr",
     210             :                                   ALLOCSET_DEFAULT_SIZES);
     211             : 
     212             :     /*
     213             :      * Create pending-operations hashtable if we need it.  Currently, we need
     214             :      * it if we are standalone (not under a postmaster) or if we are a startup
     215             :      * or checkpointer auxiliary process.
     216             :      */
     217       11332 :     if (!IsUnderPostmaster || AmStartupProcess() || AmCheckpointerProcess())
     218             :     {
     219             :         HASHCTL     hash_ctl;
     220             : 
     221             :         /*
     222             :          * XXX: The checkpointer needs to add entries to the pending ops table
     223             :          * when absorbing fsync requests.  That is done within a critical
     224             :          * section, which isn't usually allowed, but we make an exception. It
     225             :          * means that there's a theoretical possibility that you run out of
     226             :          * memory while absorbing fsync requests, which leads to a PANIC.
     227             :          * Fortunately the hash table is small so that's unlikely to happen in
     228             :          * practice.
     229             :          */
     230        1980 :         pendingOpsCxt = AllocSetContextCreate(MdCxt,
     231             :                                               "Pending ops context",
     232             :                                               ALLOCSET_DEFAULT_SIZES);
     233        1980 :         MemoryContextAllowInCriticalSection(pendingOpsCxt, true);
     234             : 
     235        1980 :         MemSet(&hash_ctl, 0, sizeof(hash_ctl));
     236        1980 :         hash_ctl.keysize = sizeof(RelFileNode);
     237        1980 :         hash_ctl.entrysize = sizeof(PendingOperationEntry);
     238        1980 :         hash_ctl.hcxt = pendingOpsCxt;
     239        1980 :         pendingOpsTable = hash_create("Pending Ops Table",
     240             :                                       100L,
     241             :                                       &hash_ctl,
     242             :                                       HASH_ELEM | HASH_BLOBS | HASH_CONTEXT);
     243        1980 :         pendingUnlinks = NIL;
     244             :     }
     245       11332 : }
     246             : 
     247             : /*
     248             :  * In archive recovery, we rely on checkpointer to do fsyncs, but we will have
     249             :  * already created the pendingOpsTable during initialization of the startup
     250             :  * process.  Calling this function drops the local pendingOpsTable so that
     251             :  * subsequent requests will be forwarded to checkpointer.
     252             :  */
     253             : void
     254          62 : SetForwardFsyncRequests(void)
     255             : {
     256             :     /* Perform any pending fsyncs we may have queued up, then drop table */
     257          62 :     if (pendingOpsTable)
     258             :     {
     259          62 :         mdsync();
     260          62 :         hash_destroy(pendingOpsTable);
     261             :     }
     262          62 :     pendingOpsTable = NULL;
     263             : 
     264             :     /*
     265             :      * We should not have any pending unlink requests, since mdunlink doesn't
     266             :      * queue unlink requests when isRedo.
     267             :      */
     268             :     Assert(pendingUnlinks == NIL);
     269          62 : }
     270             : 
     271             : /*
     272             :  *  mdexists() -- Does the physical file exist?
     273             :  *
     274             :  * Note: this will return true for lingering files, with pending deletions
     275             :  */
     276             : bool
     277      374892 : mdexists(SMgrRelation reln, ForkNumber forkNum)
     278             : {
     279             :     /*
     280             :      * Close it first, to ensure that we notice if the fork has been unlinked
     281             :      * since we opened it.
     282             :      */
     283      374892 :     mdclose(reln, forkNum);
     284             : 
     285      374892 :     return (mdopen(reln, forkNum, EXTENSION_RETURN_NULL) != NULL);
     286             : }
     287             : 
     288             : /*
     289             :  *  mdcreate() -- Create a new relation on magnetic disk.
     290             :  *
     291             :  * If isRedo is true, it's okay for the relation to exist already.
     292             :  */
     293             : void
     294      133586 : mdcreate(SMgrRelation reln, ForkNumber forkNum, bool isRedo)
     295             : {
     296             :     MdfdVec    *mdfd;
     297             :     char       *path;
     298             :     File        fd;
     299             : 
     300      133586 :     if (isRedo && reln->md_num_open_segs[forkNum] > 0)
     301           0 :         return;                 /* created and opened already... */
     302             : 
     303             :     Assert(reln->md_num_open_segs[forkNum] == 0);
     304             : 
     305      133586 :     path = relpath(reln->smgr_rnode, forkNum);
     306             : 
     307      133586 :     fd = PathNameOpenFile(path, O_RDWR | O_CREAT | O_EXCL | PG_BINARY);
     308             : 
     309      133586 :     if (fd < 0)
     310             :     {
     311         612 :         int         save_errno = errno;
     312             : 
     313             :         /*
     314             :          * During bootstrap, there are cases where a system relation will be
     315             :          * accessed (by internal backend processes) before the bootstrap
     316             :          * script nominally creates it.  Therefore, allow the file to exist
     317             :          * already, even if isRedo is not set.  (See also mdopen)
     318             :          */
     319         612 :         if (isRedo || IsBootstrapProcessingMode())
     320         612 :             fd = PathNameOpenFile(path, O_RDWR | PG_BINARY);
     321         612 :         if (fd < 0)
     322             :         {
     323             :             /* be sure to report the error reported by create, not open */
     324           0 :             errno = save_errno;
     325           0 :             ereport(ERROR,
     326             :                     (errcode_for_file_access(),
     327             :                      errmsg("could not create file \"%s\": %m", path)));
     328             :         }
     329             :     }
     330             : 
     331      133586 :     pfree(path);
     332             : 
     333      133586 :     _fdvec_resize(reln, forkNum, 1);
     334      133586 :     mdfd = &reln->md_seg_fds[forkNum][0];
     335      133586 :     mdfd->mdfd_vfd = fd;
     336      133586 :     mdfd->mdfd_segno = 0;
     337             : }
     338             : 
     339             : /*
     340             :  *  mdunlink() -- Unlink a relation.
     341             :  *
     342             :  * Note that we're passed a RelFileNodeBackend --- by the time this is called,
     343             :  * there won't be an SMgrRelation hashtable entry anymore.
     344             :  *
     345             :  * forkNum can be a fork number to delete a specific fork, or InvalidForkNumber
     346             :  * to delete all forks.
     347             :  *
     348             :  * For regular relations, we don't unlink the first segment file of the rel,
     349             :  * but just truncate it to zero length, and record a request to unlink it after
     350             :  * the next checkpoint.  Additional segments can be unlinked immediately,
     351             :  * however.  Leaving the empty file in place prevents that relfilenode
     352             :  * number from being reused.  The scenario this protects us from is:
     353             :  * 1. We delete a relation (and commit, and actually remove its file).
     354             :  * 2. We create a new relation, which by chance gets the same relfilenode as
     355             :  *    the just-deleted one (OIDs must've wrapped around for that to happen).
     356             :  * 3. We crash before another checkpoint occurs.
     357             :  * During replay, we would delete the file and then recreate it, which is fine
     358             :  * if the contents of the file were repopulated by subsequent WAL entries.
     359             :  * But if we didn't WAL-log insertions, but instead relied on fsyncing the
     360             :  * file after populating it (as for instance CLUSTER and CREATE INDEX do),
     361             :  * the contents of the file would be lost forever.  By leaving the empty file
     362             :  * until after the next checkpoint, we prevent reassignment of the relfilenode
     363             :  * number until it's safe, because relfilenode assignment skips over any
     364             :  * existing file.
     365             :  *
     366             :  * We do not need to go through this dance for temp relations, though, because
     367             :  * we never make WAL entries for temp rels, and so a temp rel poses no threat
     368             :  * to the health of a regular rel that has taken over its relfilenode number.
     369             :  * The fact that temp rels and regular rels have different file naming
     370             :  * patterns provides additional safety.
     371             :  *
     372             :  * All the above applies only to the relation's main fork; other forks can
     373             :  * just be removed immediately, since they are not needed to prevent the
     374             :  * relfilenode number from being recycled.  Also, we do not carefully
     375             :  * track whether other forks have been created or not, but just attempt to
     376             :  * unlink them unconditionally; so we should never complain about ENOENT.
     377             :  *
     378             :  * If isRedo is true, it's unsurprising for the relation to be already gone.
     379             :  * Also, we should remove the file immediately instead of queuing a request
     380             :  * for later, since during redo there's no possibility of creating a
     381             :  * conflicting relation.
     382             :  *
     383             :  * Note: any failure should be reported as WARNING not ERROR, because
     384             :  * we are usually not in a transaction anymore when this is called.
     385             :  */
     386             : void
     387      144744 : mdunlink(RelFileNodeBackend rnode, ForkNumber forkNum, bool isRedo)
     388             : {
     389             :     /*
     390             :      * We have to clean out any pending fsync requests for the doomed
     391             :      * relation, else the next mdsync() will fail.  There can't be any such
     392             :      * requests for a temp relation, though.  We can send just one request
     393             :      * even when deleting multiple forks, since the fsync queuing code accepts
     394             :      * the "InvalidForkNumber = all forks" convention.
     395             :      */
     396      144744 :     if (!RelFileNodeBackendIsTemp(rnode))
     397      127096 :         ForgetRelationFsyncRequests(rnode.node, forkNum);
     398             : 
     399             :     /* Now do the per-fork work */
     400      144744 :     if (forkNum == InvalidForkNumber)
     401             :     {
     402           0 :         for (forkNum = 0; forkNum <= MAX_FORKNUM; forkNum++)
     403           0 :             mdunlinkfork(rnode, forkNum, isRedo);
     404             :     }
     405             :     else
     406      144744 :         mdunlinkfork(rnode, forkNum, isRedo);
     407      144744 : }
     408             : 
     409             : static void
     410      144744 : mdunlinkfork(RelFileNodeBackend rnode, ForkNumber forkNum, bool isRedo)
     411             : {
     412             :     char       *path;
     413             :     int         ret;
     414             : 
     415      144744 :     path = relpath(rnode, forkNum);
     416             : 
     417             :     /*
     418             :      * Delete or truncate the first segment.
     419             :      */
     420      144744 :     if (isRedo || forkNum != MAIN_FORKNUM || RelFileNodeBackendIsTemp(rnode))
     421             :     {
     422      112984 :         ret = unlink(path);
     423      225968 :         if (ret < 0 && errno != ENOENT)
     424           0 :             ereport(WARNING,
     425             :                     (errcode_for_file_access(),
     426             :                      errmsg("could not remove file \"%s\": %m", path)));
     427             :     }
     428             :     else
     429             :     {
     430             :         /* truncate(2) would be easier here, but Windows hasn't got it */
     431             :         int         fd;
     432             : 
     433       31760 :         fd = OpenTransientFile(path, O_RDWR | PG_BINARY);
     434       31760 :         if (fd >= 0)
     435             :         {
     436             :             int         save_errno;
     437             : 
     438       31760 :             ret = ftruncate(fd, 0);
     439       31760 :             save_errno = errno;
     440       31760 :             CloseTransientFile(fd);
     441       31760 :             errno = save_errno;
     442             :         }
     443             :         else
     444           0 :             ret = -1;
     445       31760 :         if (ret < 0 && errno != ENOENT)
     446           0 :             ereport(WARNING,
     447             :                     (errcode_for_file_access(),
     448             :                      errmsg("could not truncate file \"%s\": %m", path)));
     449             : 
     450             :         /* Register request to unlink first segment later */
     451       31760 :         register_unlink(rnode);
     452             :     }
     453             : 
     454             :     /*
     455             :      * Delete any additional segments.
     456             :      */
     457      144744 :     if (ret >= 0)
     458             :     {
     459       38114 :         char       *segpath = (char *) palloc(strlen(path) + 12);
     460             :         BlockNumber segno;
     461             : 
     462             :         /*
     463             :          * Note that because we loop until getting ENOENT, we will correctly
     464             :          * remove all inactive segments as well as active ones.
     465             :          */
     466       38114 :         for (segno = 1;; segno++)
     467             :         {
     468       38114 :             sprintf(segpath, "%s.%u", path, segno);
     469       38114 :             if (unlink(segpath) < 0)
     470             :             {
     471             :                 /* ENOENT is expected after the last segment... */
     472       38114 :                 if (errno != ENOENT)
     473           0 :                     ereport(WARNING,
     474             :                             (errcode_for_file_access(),
     475             :                              errmsg("could not remove file \"%s\": %m", segpath)));
     476       38114 :                 break;
     477             :             }
     478             :         }
     479       38114 :         pfree(segpath);
     480             :     }
     481             : 
     482      144744 :     pfree(path);
     483      144744 : }
     484             : 
     485             : /*
     486             :  *  mdextend() -- Add a block to the specified relation.
     487             :  *
     488             :  *      The semantics are nearly the same as mdwrite(): write at the
     489             :  *      specified position.  However, this is to be used for the case of
     490             :  *      extending a relation (i.e., blocknum is at or beyond the current
     491             :  *      EOF).  Note that we assume writing a block beyond current EOF
     492             :  *      causes intervening file space to become filled with zeroes.
     493             :  */
     494             : void
     495      535010 : mdextend(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum,
     496             :          char *buffer, bool skipFsync)
     497             : {
     498             :     off_t       seekpos;
     499             :     int         nbytes;
     500             :     MdfdVec    *v;
     501             : 
     502             :     /* This assert is too expensive to have on normally ... */
     503             : #ifdef CHECK_WRITE_VS_EXTEND
     504             :     Assert(blocknum >= mdnblocks(reln, forknum));
     505             : #endif
     506             : 
     507             :     /*
     508             :      * If a relation manages to grow to 2^32-1 blocks, refuse to extend it any
     509             :      * more --- we mustn't create a block whose number actually is
     510             :      * InvalidBlockNumber.
     511             :      */
     512      535010 :     if (blocknum == InvalidBlockNumber)
     513           0 :         ereport(ERROR,
     514             :                 (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
     515             :                  errmsg("cannot extend file \"%s\" beyond %u blocks",
     516             :                         relpath(reln->smgr_rnode, forknum),
     517             :                         InvalidBlockNumber)));
     518             : 
     519      535010 :     v = _mdfd_getseg(reln, forknum, blocknum, skipFsync, EXTENSION_CREATE);
     520             : 
     521      535010 :     seekpos = (off_t) BLCKSZ * (blocknum % ((BlockNumber) RELSEG_SIZE));
     522             : 
     523             :     Assert(seekpos < (off_t) BLCKSZ * RELSEG_SIZE);
     524             : 
     525             :     /*
     526             :      * Note: because caller usually obtained blocknum by calling mdnblocks,
     527             :      * which did a seek(SEEK_END), this seek is often redundant and will be
     528             :      * optimized away by fd.c.  It's not redundant, however, if there is a
     529             :      * partial page at the end of the file. In that case we want to try to
     530             :      * overwrite the partial page with a full page.  It's also not redundant
     531             :      * if bufmgr.c had to dump another buffer of the same file to make room
     532             :      * for the new page's buffer.
     533             :      */
     534      535010 :     if (FileSeek(v->mdfd_vfd, seekpos, SEEK_SET) != seekpos)
     535           0 :         ereport(ERROR,
     536             :                 (errcode_for_file_access(),
     537             :                  errmsg("could not seek to block %u in file \"%s\": %m",
     538             :                         blocknum, FilePathName(v->mdfd_vfd))));
     539             : 
     540      535010 :     if ((nbytes = FileWrite(v->mdfd_vfd, buffer, BLCKSZ, WAIT_EVENT_DATA_FILE_EXTEND)) != BLCKSZ)
     541             :     {
     542           0 :         if (nbytes < 0)
     543           0 :             ereport(ERROR,
     544             :                     (errcode_for_file_access(),
     545             :                      errmsg("could not extend file \"%s\": %m",
     546             :                             FilePathName(v->mdfd_vfd)),
     547             :                      errhint("Check free disk space.")));
     548             :         /* short write: complain appropriately */
     549           0 :         ereport(ERROR,
     550             :                 (errcode(ERRCODE_DISK_FULL),
     551             :                  errmsg("could not extend file \"%s\": wrote only %d of %d bytes at block %u",
     552             :                         FilePathName(v->mdfd_vfd),
     553             :                         nbytes, BLCKSZ, blocknum),
     554             :                  errhint("Check free disk space.")));
     555             :     }
     556             : 
     557      535010 :     if (!skipFsync && !SmgrIsTemp(reln))
     558      383786 :         register_dirty_segment(reln, forknum, v);
     559             : 
     560             :     Assert(_mdnblocks(reln, forknum, v) <= ((BlockNumber) RELSEG_SIZE));
     561      535010 : }
     562             : 
     563             : /*
     564             :  *  mdopen() -- Open the specified relation.
     565             :  *
     566             :  * Note we only open the first segment, when there are multiple segments.
     567             :  *
     568             :  * If first segment is not present, either ereport or return NULL according
     569             :  * to "behavior".  We treat EXTENSION_CREATE the same as EXTENSION_FAIL;
     570             :  * EXTENSION_CREATE means it's OK to extend an existing relation, not to
     571             :  * invent one out of whole cloth.
     572             :  */
     573             : static MdfdVec *
     574     3922094 : mdopen(SMgrRelation reln, ForkNumber forknum, int behavior)
     575             : {
     576             :     MdfdVec    *mdfd;
     577             :     char       *path;
     578             :     File        fd;
     579             : 
     580             :     /* No work if already open */
     581     3922094 :     if (reln->md_num_open_segs[forknum] > 0)
     582     2850512 :         return &reln->md_seg_fds[forknum][0];
     583             : 
     584     1071582 :     path = relpath(reln->smgr_rnode, forknum);
     585             : 
     586     1071582 :     fd = PathNameOpenFile(path, O_RDWR | PG_BINARY);
     587             : 
     588     1071582 :     if (fd < 0)
     589             :     {
     590             :         /*
     591             :          * During bootstrap, there are cases where a system relation will be
     592             :          * accessed (by internal backend processes) before the bootstrap
     593             :          * script nominally creates it.  Therefore, accept mdopen() as a
     594             :          * substitute for mdcreate() in bootstrap mode only. (See mdcreate)
     595             :          */
     596      188534 :         if (IsBootstrapProcessingMode())
     597       35868 :             fd = PathNameOpenFile(path, O_RDWR | O_CREAT | O_EXCL | PG_BINARY);
     598      188534 :         if (fd < 0)
     599             :         {
     600      305332 :             if ((behavior & EXTENSION_RETURN_NULL) &&
     601      152666 :                 FILE_POSSIBLY_DELETED(errno))
     602             :             {
     603      152666 :                 pfree(path);
     604      152666 :                 return NULL;
     605             :             }
     606           0 :             ereport(ERROR,
     607             :                     (errcode_for_file_access(),
     608             :                      errmsg("could not open file \"%s\": %m", path)));
     609             :         }
     610             :     }
     611             : 
     612      918916 :     pfree(path);
     613             : 
     614      918916 :     _fdvec_resize(reln, forknum, 1);
     615      918916 :     mdfd = &reln->md_seg_fds[forknum][0];
     616      918916 :     mdfd->mdfd_vfd = fd;
     617      918916 :     mdfd->mdfd_segno = 0;
     618             : 
     619             :     Assert(_mdnblocks(reln, forknum, mdfd) <= ((BlockNumber) RELSEG_SIZE));
     620             : 
     621      918916 :     return mdfd;
     622             : }
     623             : 
     624             : /*
     625             :  *  mdclose() -- Close the specified relation, if it isn't closed already.
     626             :  */
     627             : void
     628     3295700 : mdclose(SMgrRelation reln, ForkNumber forknum)
     629             : {
     630     3295700 :     int         nopensegs = reln->md_num_open_segs[forknum];
     631             : 
     632             :     /* No work if already closed */
     633     3295700 :     if (nopensegs == 0)
     634     2483226 :         return;
     635             : 
     636             :     /* close segments starting from the end */
     637     2437422 :     while (nopensegs > 0)
     638             :     {
     639      812474 :         MdfdVec    *v = &reln->md_seg_fds[forknum][nopensegs - 1];
     640             : 
     641             :         /* if not closed already */
     642      812474 :         if (v->mdfd_vfd >= 0)
     643             :         {
     644      812474 :             FileClose(v->mdfd_vfd);
     645      812474 :             v->mdfd_vfd = -1;
     646             :         }
     647             : 
     648      812474 :         nopensegs--;
     649             :     }
     650             : 
     651             :     /* resize just once, avoids pointless reallocations */
     652      812474 :     _fdvec_resize(reln, forknum, 0);
     653             : }
     654             : 
     655             : /*
     656             :  *  mdprefetch() -- Initiate asynchronous read of the specified block of a relation
     657             :  */
     658             : void
     659         118 : mdprefetch(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum)
     660             : {
     661             : #ifdef USE_PREFETCH
     662             :     off_t       seekpos;
     663             :     MdfdVec    *v;
     664             : 
     665         118 :     v = _mdfd_getseg(reln, forknum, blocknum, false, EXTENSION_FAIL);
     666             : 
     667         118 :     seekpos = (off_t) BLCKSZ * (blocknum % ((BlockNumber) RELSEG_SIZE));
     668             : 
     669             :     Assert(seekpos < (off_t) BLCKSZ * RELSEG_SIZE);
     670             : 
     671         118 :     (void) FilePrefetch(v->mdfd_vfd, seekpos, BLCKSZ, WAIT_EVENT_DATA_FILE_PREFETCH);
     672             : #endif                          /* USE_PREFETCH */
     673         118 : }
     674             : 
     675             : /*
     676             :  * mdwriteback() -- Tell the kernel to write pages back to storage.
     677             :  *
     678             :  * This accepts a range of blocks because flushing several pages at once is
     679             :  * considerably more efficient than doing so individually.
     680             :  */
     681             : void
     682      137100 : mdwriteback(SMgrRelation reln, ForkNumber forknum,
     683             :             BlockNumber blocknum, BlockNumber nblocks)
     684             : {
     685             :     /*
     686             :      * Issue flush requests in as few requests as possible; have to split at
     687             :      * segment boundaries though, since those are actually separate files.
     688             :      */
     689      411300 :     while (nblocks > 0)
     690             :     {
     691      137100 :         BlockNumber nflush = nblocks;
     692             :         off_t       seekpos;
     693             :         MdfdVec    *v;
     694             :         int         segnum_start,
     695             :                     segnum_end;
     696             : 
     697      137100 :         v = _mdfd_getseg(reln, forknum, blocknum, true /* not used */ ,
     698             :                          EXTENSION_RETURN_NULL);
     699             : 
     700             :         /*
     701             :          * We might be flushing buffers of already removed relations, that's
     702             :          * ok, just ignore that case.
     703             :          */
     704      137100 :         if (!v)
     705           0 :             return;
     706             : 
     707             :         /* compute offset inside the current segment */
     708      137100 :         segnum_start = blocknum / RELSEG_SIZE;
     709             : 
     710             :         /* compute number of desired writes within the current segment */
     711      137100 :         segnum_end = (blocknum + nblocks - 1) / RELSEG_SIZE;
     712      137100 :         if (segnum_start != segnum_end)
     713           0 :             nflush = RELSEG_SIZE - (blocknum % ((BlockNumber) RELSEG_SIZE));
     714             : 
     715             :         Assert(nflush >= 1);
     716             :         Assert(nflush <= nblocks);
     717             : 
     718      137100 :         seekpos = (off_t) BLCKSZ * (blocknum % ((BlockNumber) RELSEG_SIZE));
     719             : 
     720      137100 :         FileWriteback(v->mdfd_vfd, seekpos, (off_t) BLCKSZ * nflush, WAIT_EVENT_DATA_FILE_FLUSH);
     721             : 
     722      137100 :         nblocks -= nflush;
     723      137100 :         blocknum += nflush;
     724             :     }
     725             : }
     726             : 
     727             : /*
     728             :  *  mdread() -- Read the specified block from a relation.
     729             :  */
     730             : void
     731      442834 : mdread(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum,
     732             :        char *buffer)
     733             : {
     734             :     off_t       seekpos;
     735             :     int         nbytes;
     736             :     MdfdVec    *v;
     737             : 
     738             :     TRACE_POSTGRESQL_SMGR_MD_READ_START(forknum, blocknum,
     739             :                                         reln->smgr_rnode.node.spcNode,
     740             :                                         reln->smgr_rnode.node.dbNode,
     741             :                                         reln->smgr_rnode.node.relNode,
     742             :                                         reln->smgr_rnode.backend);
     743             : 
     744      442834 :     v = _mdfd_getseg(reln, forknum, blocknum, false,
     745             :                      EXTENSION_FAIL | EXTENSION_CREATE_RECOVERY);
     746             : 
     747      442834 :     seekpos = (off_t) BLCKSZ * (blocknum % ((BlockNumber) RELSEG_SIZE));
     748             : 
     749             :     Assert(seekpos < (off_t) BLCKSZ * RELSEG_SIZE);
     750             : 
     751      442834 :     if (FileSeek(v->mdfd_vfd, seekpos, SEEK_SET) != seekpos)
     752           0 :         ereport(ERROR,
     753             :                 (errcode_for_file_access(),
     754             :                  errmsg("could not seek to block %u in file \"%s\": %m",
     755             :                         blocknum, FilePathName(v->mdfd_vfd))));
     756             : 
     757      442834 :     nbytes = FileRead(v->mdfd_vfd, buffer, BLCKSZ, WAIT_EVENT_DATA_FILE_READ);
     758             : 
     759             :     TRACE_POSTGRESQL_SMGR_MD_READ_DONE(forknum, blocknum,
     760             :                                        reln->smgr_rnode.node.spcNode,
     761             :                                        reln->smgr_rnode.node.dbNode,
     762             :                                        reln->smgr_rnode.node.relNode,
     763             :                                        reln->smgr_rnode.backend,
     764             :                                        nbytes,
     765             :                                        BLCKSZ);
     766             : 
     767      442834 :     if (nbytes != BLCKSZ)
     768             :     {
     769           0 :         if (nbytes < 0)
     770           0 :             ereport(ERROR,
     771             :                     (errcode_for_file_access(),
     772             :                      errmsg("could not read block %u in file \"%s\": %m",
     773             :                             blocknum, FilePathName(v->mdfd_vfd))));
     774             : 
     775             :         /*
     776             :          * Short read: we are at or past EOF, or we read a partial block at
     777             :          * EOF.  Normally this is an error; upper levels should never try to
     778             :          * read a nonexistent block.  However, if zero_damaged_pages is ON or
     779             :          * we are InRecovery, we should instead return zeroes without
     780             :          * complaining.  This allows, for example, the case of trying to
     781             :          * update a block that was later truncated away.
     782             :          */
     783           0 :         if (zero_damaged_pages || InRecovery)
     784           0 :             MemSet(buffer, 0, BLCKSZ);
     785             :         else
     786           0 :             ereport(ERROR,
     787             :                     (errcode(ERRCODE_DATA_CORRUPTED),
     788             :                      errmsg("could not read block %u in file \"%s\": read only %d of %d bytes",
     789             :                             blocknum, FilePathName(v->mdfd_vfd),
     790             :                             nbytes, BLCKSZ)));
     791             :     }
     792      442834 : }
     793             : 
     794             : /*
     795             :  *  mdwrite() -- Write the supplied block at the appropriate location.
     796             :  *
     797             :  *      This is to be used only for updating already-existing blocks of a
     798             :  *      relation (ie, those before the current EOF).  To extend a relation,
     799             :  *      use mdextend().
     800             :  */
     801             : void
     802      443882 : mdwrite(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum,
     803             :         char *buffer, bool skipFsync)
     804             : {
     805             :     off_t       seekpos;
     806             :     int         nbytes;
     807             :     MdfdVec    *v;
     808             : 
     809             :     /* This assert is too expensive to have on normally ... */
     810             : #ifdef CHECK_WRITE_VS_EXTEND
     811             :     Assert(blocknum < mdnblocks(reln, forknum));
     812             : #endif
     813             : 
     814             :     TRACE_POSTGRESQL_SMGR_MD_WRITE_START(forknum, blocknum,
     815             :                                          reln->smgr_rnode.node.spcNode,
     816             :                                          reln->smgr_rnode.node.dbNode,
     817             :                                          reln->smgr_rnode.node.relNode,
     818             :                                          reln->smgr_rnode.backend);
     819             : 
     820      443882 :     v = _mdfd_getseg(reln, forknum, blocknum, skipFsync,
     821             :                      EXTENSION_FAIL | EXTENSION_CREATE_RECOVERY);
     822             : 
     823      443882 :     seekpos = (off_t) BLCKSZ * (blocknum % ((BlockNumber) RELSEG_SIZE));
     824             : 
     825             :     Assert(seekpos < (off_t) BLCKSZ * RELSEG_SIZE);
     826             : 
     827      443882 :     if (FileSeek(v->mdfd_vfd, seekpos, SEEK_SET) != seekpos)
     828           0 :         ereport(ERROR,
     829             :                 (errcode_for_file_access(),
     830             :                  errmsg("could not seek to block %u in file \"%s\": %m",
     831             :                         blocknum, FilePathName(v->mdfd_vfd))));
     832             : 
     833      443882 :     nbytes = FileWrite(v->mdfd_vfd, buffer, BLCKSZ, WAIT_EVENT_DATA_FILE_WRITE);
     834             : 
     835             :     TRACE_POSTGRESQL_SMGR_MD_WRITE_DONE(forknum, blocknum,
     836             :                                         reln->smgr_rnode.node.spcNode,
     837             :                                         reln->smgr_rnode.node.dbNode,
     838             :                                         reln->smgr_rnode.node.relNode,
     839             :                                         reln->smgr_rnode.backend,
     840             :                                         nbytes,
     841             :                                         BLCKSZ);
     842             : 
     843      443882 :     if (nbytes != BLCKSZ)
     844             :     {
     845           0 :         if (nbytes < 0)
     846           0 :             ereport(ERROR,
     847             :                     (errcode_for_file_access(),
     848             :                      errmsg("could not write block %u in file \"%s\": %m",
     849             :                             blocknum, FilePathName(v->mdfd_vfd))));
     850             :         /* short write: complain appropriately */
     851           0 :         ereport(ERROR,
     852             :                 (errcode(ERRCODE_DISK_FULL),
     853             :                  errmsg("could not write block %u in file \"%s\": wrote only %d of %d bytes",
     854             :                         blocknum,
     855             :                         FilePathName(v->mdfd_vfd),
     856             :                         nbytes, BLCKSZ),
     857             :                  errhint("Check free disk space.")));
     858             :     }
     859             : 
     860      443882 :     if (!skipFsync && !SmgrIsTemp(reln))
     861      422716 :         register_dirty_segment(reln, forknum, v);
     862      443882 : }
     863             : 
     864             : /*
     865             :  *  mdnblocks() -- Get the number of blocks stored in a relation.
     866             :  *
     867             :  *      Important side effect: all active segments of the relation are opened
     868             :  *      and added to the mdfd_seg_fds array.  If this routine has not been
     869             :  *      called, then only segments up to the last one actually touched
     870             :  *      are present in the array.
     871             :  */
     872             : BlockNumber
     873     3413082 : mdnblocks(SMgrRelation reln, ForkNumber forknum)
     874             : {
     875     3413082 :     MdfdVec    *v = mdopen(reln, forknum, EXTENSION_FAIL);
     876             :     BlockNumber nblocks;
     877     3413082 :     BlockNumber segno = 0;
     878             : 
     879             :     /* mdopen has opened the first segment */
     880             :     Assert(reln->md_num_open_segs[forknum] > 0);
     881             : 
     882             :     /*
     883             :      * Start from the last open segments, to avoid redundant seeks.  We have
     884             :      * previously verified that these segments are exactly RELSEG_SIZE long,
     885             :      * and it's useless to recheck that each time.
     886             :      *
     887             :      * NOTE: this assumption could only be wrong if another backend has
     888             :      * truncated the relation.  We rely on higher code levels to handle that
     889             :      * scenario by closing and re-opening the md fd, which is handled via
     890             :      * relcache flush.  (Since the checkpointer doesn't participate in
     891             :      * relcache flush, it could have segment entries for inactive segments;
     892             :      * that's OK because the checkpointer never needs to compute relation
     893             :      * size.)
     894             :      */
     895     3413082 :     segno = reln->md_num_open_segs[forknum] - 1;
     896     3413082 :     v = &reln->md_seg_fds[forknum][segno];
     897             : 
     898             :     for (;;)
     899             :     {
     900     3413082 :         nblocks = _mdnblocks(reln, forknum, v);
     901     3413082 :         if (nblocks > ((BlockNumber) RELSEG_SIZE))
     902           0 :             elog(FATAL, "segment too big");
     903     3413082 :         if (nblocks < ((BlockNumber) RELSEG_SIZE))
     904     3413082 :             return (segno * ((BlockNumber) RELSEG_SIZE)) + nblocks;
     905             : 
     906             :         /*
     907             :          * If segment is exactly RELSEG_SIZE, advance to next one.
     908             :          */
     909           0 :         segno++;
     910             : 
     911             :         /*
     912             :          * We used to pass O_CREAT here, but that's has the disadvantage that
     913             :          * it might create a segment which has vanished through some operating
     914             :          * system misadventure.  In such a case, creating the segment here
     915             :          * undermines _mdfd_getseg's attempts to notice and report an error
     916             :          * upon access to a missing segment.
     917             :          */
     918           0 :         v = _mdfd_openseg(reln, forknum, segno, 0);
     919           0 :         if (v == NULL)
     920           0 :             return segno * ((BlockNumber) RELSEG_SIZE);
     921             :     }
     922             : }
     923             : 
     924             : /*
     925             :  *  mdtruncate() -- Truncate relation to specified number of blocks.
     926             :  */
     927             : void
     928         372 : mdtruncate(SMgrRelation reln, ForkNumber forknum, BlockNumber nblocks)
     929             : {
     930             :     BlockNumber curnblk;
     931             :     BlockNumber priorblocks;
     932             :     int         curopensegs;
     933             : 
     934             :     /*
     935             :      * NOTE: mdnblocks makes sure we have opened all active segments, so that
     936             :      * truncation loop will get them all!
     937             :      */
     938         372 :     curnblk = mdnblocks(reln, forknum);
     939         372 :     if (nblocks > curnblk)
     940             :     {
     941             :         /* Bogus request ... but no complaint if InRecovery */
     942           0 :         if (InRecovery)
     943           0 :             return;
     944           0 :         ereport(ERROR,
     945             :                 (errmsg("could not truncate file \"%s\" to %u blocks: it's only %u blocks now",
     946             :                         relpath(reln->smgr_rnode, forknum),
     947             :                         nblocks, curnblk)));
     948             :     }
     949         372 :     if (nblocks == curnblk)
     950         132 :         return;                 /* no work */
     951             : 
     952             :     /*
     953             :      * Truncate segments, starting at the last one. Starting at the end makes
     954             :      * managing the memory for the fd array easier, should there be errors.
     955             :      */
     956         240 :     curopensegs = reln->md_num_open_segs[forknum];
     957         720 :     while (curopensegs > 0)
     958             :     {
     959             :         MdfdVec    *v;
     960             : 
     961         240 :         priorblocks = (curopensegs - 1) * RELSEG_SIZE;
     962             : 
     963         240 :         v = &reln->md_seg_fds[forknum][curopensegs - 1];
     964             : 
     965         240 :         if (priorblocks > nblocks)
     966             :         {
     967             :             /*
     968             :              * This segment is no longer active. We truncate the file, but do
     969             :              * not delete it, for reasons explained in the header comments.
     970             :              */
     971           0 :             if (FileTruncate(v->mdfd_vfd, 0, WAIT_EVENT_DATA_FILE_TRUNCATE) < 0)
     972           0 :                 ereport(ERROR,
     973             :                         (errcode_for_file_access(),
     974             :                          errmsg("could not truncate file \"%s\": %m",
     975             :                                 FilePathName(v->mdfd_vfd))));
     976             : 
     977           0 :             if (!SmgrIsTemp(reln))
     978           0 :                 register_dirty_segment(reln, forknum, v);
     979             : 
     980             :             /* we never drop the 1st segment */
     981             :             Assert(v != &reln->md_seg_fds[forknum][0]);
     982             : 
     983           0 :             FileClose(v->mdfd_vfd);
     984           0 :             _fdvec_resize(reln, forknum, curopensegs - 1);
     985             :         }
     986         240 :         else if (priorblocks + ((BlockNumber) RELSEG_SIZE) > nblocks)
     987             :         {
     988             :             /*
     989             :              * This is the last segment we want to keep. Truncate the file to
     990             :              * the right length. NOTE: if nblocks is exactly a multiple K of
     991             :              * RELSEG_SIZE, we will truncate the K+1st segment to 0 length but
     992             :              * keep it. This adheres to the invariant given in the header
     993             :              * comments.
     994             :              */
     995         240 :             BlockNumber lastsegblocks = nblocks - priorblocks;
     996             : 
     997         240 :             if (FileTruncate(v->mdfd_vfd, (off_t) lastsegblocks * BLCKSZ, WAIT_EVENT_DATA_FILE_TRUNCATE) < 0)
     998           0 :                 ereport(ERROR,
     999             :                         (errcode_for_file_access(),
    1000             :                          errmsg("could not truncate file \"%s\" to %u blocks: %m",
    1001             :                                 FilePathName(v->mdfd_vfd),
    1002             :                                 nblocks)));
    1003         240 :             if (!SmgrIsTemp(reln))
    1004         216 :                 register_dirty_segment(reln, forknum, v);
    1005             :         }
    1006             :         else
    1007             :         {
    1008             :             /*
    1009             :              * We still need this segment, so nothing to do for this and any
    1010             :              * earlier segment.
    1011             :              */
    1012           0 :             break;
    1013             :         }
    1014         240 :         curopensegs--;
    1015             :     }
    1016             : }
    1017             : 
    1018             : /*
    1019             :  *  mdimmedsync() -- Immediately sync a relation to stable storage.
    1020             :  *
    1021             :  * Note that only writes already issued are synced; this routine knows
    1022             :  * nothing of dirty buffers that may exist inside the buffer manager.
    1023             :  */
    1024             : void
    1025       63500 : mdimmedsync(SMgrRelation reln, ForkNumber forknum)
    1026             : {
    1027             :     int         segno;
    1028             : 
    1029             :     /*
    1030             :      * NOTE: mdnblocks makes sure we have opened all active segments, so that
    1031             :      * fsync loop will get them all!
    1032             :      */
    1033       63500 :     mdnblocks(reln, forknum);
    1034             : 
    1035       63500 :     segno = reln->md_num_open_segs[forknum];
    1036             : 
    1037      190500 :     while (segno > 0)
    1038             :     {
    1039       63500 :         MdfdVec    *v = &reln->md_seg_fds[forknum][segno - 1];
    1040             : 
    1041       63500 :         if (FileSync(v->mdfd_vfd, WAIT_EVENT_DATA_FILE_IMMEDIATE_SYNC) < 0)
    1042           0 :             ereport(ERROR,
    1043             :                     (errcode_for_file_access(),
    1044             :                      errmsg("could not fsync file \"%s\": %m",
    1045             :                             FilePathName(v->mdfd_vfd))));
    1046       63500 :         segno--;
    1047             :     }
    1048       63500 : }
    1049             : 
    1050             : /*
    1051             :  *  mdsync() -- Sync previous writes to stable storage.
    1052             :  */
    1053             : void
    1054        2668 : mdsync(void)
    1055             : {
    1056             :     static bool mdsync_in_progress = false;
    1057             : 
    1058             :     HASH_SEQ_STATUS hstat;
    1059             :     PendingOperationEntry *entry;
    1060             :     int         absorb_counter;
    1061             : 
    1062             :     /* Statistics on sync times */
    1063        2668 :     int         processed = 0;
    1064             :     instr_time  sync_start,
    1065             :                 sync_end,
    1066             :                 sync_diff;
    1067             :     uint64      elapsed;
    1068        2668 :     uint64      longest = 0;
    1069        2668 :     uint64      total_elapsed = 0;
    1070             : 
    1071             :     /*
    1072             :      * This is only called during checkpoints, and checkpoints should only
    1073             :      * occur in processes that have created a pendingOpsTable.
    1074             :      */
    1075        2668 :     if (!pendingOpsTable)
    1076           0 :         elog(ERROR, "cannot sync without a pendingOpsTable");
    1077             : 
    1078             :     /*
    1079             :      * If we are in the checkpointer, the sync had better include all fsync
    1080             :      * requests that were queued by backends up to this point.  The tightest
    1081             :      * race condition that could occur is that a buffer that must be written
    1082             :      * and fsync'd for the checkpoint could have been dumped by a backend just
    1083             :      * before it was visited by BufferSync().  We know the backend will have
    1084             :      * queued an fsync request before clearing the buffer's dirtybit, so we
    1085             :      * are safe as long as we do an Absorb after completing BufferSync().
    1086             :      */
    1087        2668 :     AbsorbFsyncRequests();
    1088             : 
    1089             :     /*
    1090             :      * To avoid excess fsync'ing (in the worst case, maybe a never-terminating
    1091             :      * checkpoint), we want to ignore fsync requests that are entered into the
    1092             :      * hashtable after this point --- they should be processed next time,
    1093             :      * instead.  We use mdsync_cycle_ctr to tell old entries apart from new
    1094             :      * ones: new ones will have cycle_ctr equal to the incremented value of
    1095             :      * mdsync_cycle_ctr.
    1096             :      *
    1097             :      * In normal circumstances, all entries present in the table at this point
    1098             :      * will have cycle_ctr exactly equal to the current (about to be old)
    1099             :      * value of mdsync_cycle_ctr.  However, if we fail partway through the
    1100             :      * fsync'ing loop, then older values of cycle_ctr might remain when we
    1101             :      * come back here to try again.  Repeated checkpoint failures would
    1102             :      * eventually wrap the counter around to the point where an old entry
    1103             :      * might appear new, causing us to skip it, possibly allowing a checkpoint
    1104             :      * to succeed that should not have.  To forestall wraparound, any time the
    1105             :      * previous mdsync() failed to complete, run through the table and
    1106             :      * forcibly set cycle_ctr = mdsync_cycle_ctr.
    1107             :      *
    1108             :      * Think not to merge this loop with the main loop, as the problem is
    1109             :      * exactly that that loop may fail before having visited all the entries.
    1110             :      * From a performance point of view it doesn't matter anyway, as this path
    1111             :      * will never be taken in a system that's functioning normally.
    1112             :      */
    1113        2668 :     if (mdsync_in_progress)
    1114             :     {
    1115             :         /* prior try failed, so update any stale cycle_ctr values */
    1116           0 :         hash_seq_init(&hstat, pendingOpsTable);
    1117           0 :         while ((entry = (PendingOperationEntry *) hash_seq_search(&hstat)) != NULL)
    1118             :         {
    1119           0 :             entry->cycle_ctr = mdsync_cycle_ctr;
    1120             :         }
    1121             :     }
    1122             : 
    1123             :     /* Advance counter so that new hashtable entries are distinguishable */
    1124        2668 :     mdsync_cycle_ctr++;
    1125             : 
    1126             :     /* Set flag to detect failure if we don't reach the end of the loop */
    1127        2668 :     mdsync_in_progress = true;
    1128             : 
    1129             :     /* Now scan the hashtable for fsync requests to process */
    1130        2668 :     absorb_counter = FSYNCS_PER_ABSORB;
    1131        2668 :     hash_seq_init(&hstat, pendingOpsTable);
    1132        2668 :     while ((entry = (PendingOperationEntry *) hash_seq_search(&hstat)) != NULL)
    1133             :     {
    1134             :         ForkNumber  forknum;
    1135             : 
    1136             :         /*
    1137             :          * If the entry is new then don't process it this time; it might
    1138             :          * contain multiple fsync-request bits, but they are all new.  Note
    1139             :          * "continue" bypasses the hash-remove call at the bottom of the loop.
    1140             :          */
    1141       96014 :         if (entry->cycle_ctr == mdsync_cycle_ctr)
    1142           0 :             continue;
    1143             : 
    1144             :         /* Else assert we haven't missed it */
    1145             :         Assert((CycleCtr) (entry->cycle_ctr + 1) == mdsync_cycle_ctr);
    1146             : 
    1147             :         /*
    1148             :          * Scan over the forks and segments represented by the entry.
    1149             :          *
    1150             :          * The bitmap manipulations are slightly tricky, because we can call
    1151             :          * AbsorbFsyncRequests() inside the loop and that could result in
    1152             :          * bms_add_member() modifying and even re-palloc'ing the bitmapsets.
    1153             :          * This is okay because we unlink each bitmapset from the hashtable
    1154             :          * entry before scanning it.  That means that any incoming fsync
    1155             :          * requests will be processed now if they reach the table before we
    1156             :          * begin to scan their fork.
    1157             :          */
    1158      480070 :         for (forknum = 0; forknum <= MAX_FORKNUM; forknum++)
    1159             :         {
    1160      384056 :             Bitmapset  *requests = entry->requests[forknum];
    1161             :             int         segno;
    1162             : 
    1163      384056 :             entry->requests[forknum] = NULL;
    1164      384056 :             entry->canceled[forknum] = false;
    1165             : 
    1166      887500 :             while ((segno = bms_first_member(requests)) >= 0)
    1167             :             {
    1168             :                 int         failures;
    1169             : 
    1170             :                 /*
    1171             :                  * If fsync is off then we don't have to bother opening the
    1172             :                  * file at all.  (We delay checking until this point so that
    1173             :                  * changing fsync on the fly behaves sensibly.)
    1174             :                  */
    1175      119388 :                 if (!enableFsync)
    1176      119328 :                     continue;
    1177             : 
    1178             :                 /*
    1179             :                  * If in checkpointer, we want to absorb pending requests
    1180             :                  * every so often to prevent overflow of the fsync request
    1181             :                  * queue.  It is unspecified whether newly-added entries will
    1182             :                  * be visited by hash_seq_search, but we don't care since we
    1183             :                  * don't need to process them anyway.
    1184             :                  */
    1185          60 :                 if (--absorb_counter <= 0)
    1186             :                 {
    1187           4 :                     AbsorbFsyncRequests();
    1188           4 :                     absorb_counter = FSYNCS_PER_ABSORB;
    1189             :                 }
    1190             : 
    1191             :                 /*
    1192             :                  * The fsync table could contain requests to fsync segments
    1193             :                  * that have been deleted (unlinked) by the time we get to
    1194             :                  * them. Rather than just hoping an ENOENT (or EACCES on
    1195             :                  * Windows) error can be ignored, what we do on error is
    1196             :                  * absorb pending requests and then retry.  Since mdunlink()
    1197             :                  * queues a "cancel" message before actually unlinking, the
    1198             :                  * fsync request is guaranteed to be marked canceled after the
    1199             :                  * absorb if it really was this case. DROP DATABASE likewise
    1200             :                  * has to tell us to forget fsync requests before it starts
    1201             :                  * deletions.
    1202             :                  */
    1203          60 :                 for (failures = 0;; failures++) /* loop exits at "break" */
    1204           0 :                 {
    1205             :                     SMgrRelation reln;
    1206             :                     MdfdVec    *seg;
    1207             :                     char       *path;
    1208             :                     int         save_errno;
    1209             : 
    1210             :                     /*
    1211             :                      * Find or create an smgr hash entry for this relation.
    1212             :                      * This may seem a bit unclean -- md calling smgr?  But
    1213             :                      * it's really the best solution.  It ensures that the
    1214             :                      * open file reference isn't permanently leaked if we get
    1215             :                      * an error here. (You may say "but an unreferenced
    1216             :                      * SMgrRelation is still a leak!" Not really, because the
    1217             :                      * only case in which a checkpoint is done by a process
    1218             :                      * that isn't about to shut down is in the checkpointer,
    1219             :                      * and it will periodically do smgrcloseall(). This fact
    1220             :                      * justifies our not closing the reln in the success path
    1221             :                      * either, which is a good thing since in non-checkpointer
    1222             :                      * cases we couldn't safely do that.)
    1223             :                      */
    1224          60 :                     reln = smgropen(entry->rnode, InvalidBackendId);
    1225             : 
    1226             :                     /* Attempt to open and fsync the target segment */
    1227          60 :                     seg = _mdfd_getseg(reln, forknum,
    1228          60 :                                        (BlockNumber) segno * (BlockNumber) RELSEG_SIZE,
    1229             :                                        false,
    1230             :                                        EXTENSION_RETURN_NULL
    1231             :                                        | EXTENSION_DONT_CHECK_SIZE);
    1232             : 
    1233          60 :                     INSTR_TIME_SET_CURRENT(sync_start);
    1234             : 
    1235         120 :                     if (seg != NULL &&
    1236          60 :                         FileSync(seg->mdfd_vfd, WAIT_EVENT_DATA_FILE_SYNC) >= 0)
    1237             :                     {
    1238             :                         /* Success; update statistics about sync timing */
    1239          60 :                         INSTR_TIME_SET_CURRENT(sync_end);
    1240          60 :                         sync_diff = sync_end;
    1241          60 :                         INSTR_TIME_SUBTRACT(sync_diff, sync_start);
    1242          60 :                         elapsed = INSTR_TIME_GET_MICROSEC(sync_diff);
    1243          60 :                         if (elapsed > longest)
    1244          10 :                             longest = elapsed;
    1245          60 :                         total_elapsed += elapsed;
    1246          60 :                         processed++;
    1247          60 :                         if (log_checkpoints)
    1248           0 :                             elog(DEBUG1, "checkpoint sync: number=%d file=%s time=%.3f msec",
    1249             :                                  processed,
    1250             :                                  FilePathName(seg->mdfd_vfd),
    1251             :                                  (double) elapsed / 1000);
    1252             : 
    1253          60 :                         break;  /* out of retry loop */
    1254             :                     }
    1255             : 
    1256             :                     /* Compute file name for use in message */
    1257           0 :                     save_errno = errno;
    1258           0 :                     path = _mdfd_segpath(reln, forknum, (BlockNumber) segno);
    1259           0 :                     errno = save_errno;
    1260             : 
    1261             :                     /*
    1262             :                      * It is possible that the relation has been dropped or
    1263             :                      * truncated since the fsync request was entered.
    1264             :                      * Therefore, allow ENOENT, but only if we didn't fail
    1265             :                      * already on this file.  This applies both for
    1266             :                      * _mdfd_getseg() and for FileSync, since fd.c might have
    1267             :                      * closed the file behind our back.
    1268             :                      *
    1269             :                      * XXX is there any point in allowing more than one retry?
    1270             :                      * Don't see one at the moment, but easy to change the
    1271             :                      * test here if so.
    1272             :                      */
    1273           0 :                     if (!FILE_POSSIBLY_DELETED(errno) ||
    1274             :                         failures > 0)
    1275           0 :                         ereport(ERROR,
    1276             :                                 (errcode_for_file_access(),
    1277             :                                  errmsg("could not fsync file \"%s\": %m",
    1278             :                                         path)));
    1279             :                     else
    1280           0 :                         ereport(DEBUG1,
    1281             :                                 (errcode_for_file_access(),
    1282             :                                  errmsg("could not fsync file \"%s\" but retrying: %m",
    1283             :                                         path)));
    1284           0 :                     pfree(path);
    1285             : 
    1286             :                     /*
    1287             :                      * Absorb incoming requests and check to see if a cancel
    1288             :                      * arrived for this relation fork.
    1289             :                      */
    1290           0 :                     AbsorbFsyncRequests();
    1291           0 :                     absorb_counter = FSYNCS_PER_ABSORB; /* might as well... */
    1292             : 
    1293           0 :                     if (entry->canceled[forknum])
    1294           0 :                         break;
    1295             :                 }               /* end retry loop */
    1296             :             }
    1297      384056 :             bms_free(requests);
    1298             :         }
    1299             : 
    1300             :         /*
    1301             :          * We've finished everything that was requested before we started to
    1302             :          * scan the entry.  If no new requests have been inserted meanwhile,
    1303             :          * remove the entry.  Otherwise, update its cycle counter, as all the
    1304             :          * requests now in it must have arrived during this cycle.
    1305             :          */
    1306      480070 :         for (forknum = 0; forknum <= MAX_FORKNUM; forknum++)
    1307             :         {
    1308      384056 :             if (entry->requests[forknum] != NULL)
    1309           0 :                 break;
    1310             :         }
    1311       96014 :         if (forknum <= MAX_FORKNUM)
    1312           0 :             entry->cycle_ctr = mdsync_cycle_ctr;
    1313             :         else
    1314             :         {
    1315             :             /* Okay to remove it */
    1316       96014 :             if (hash_search(pendingOpsTable, &entry->rnode,
    1317             :                             HASH_REMOVE, NULL) == NULL)
    1318           0 :                 elog(ERROR, "pendingOpsTable corrupted");
    1319             :         }
    1320             :     }                           /* end loop over hashtable entries */
    1321             : 
    1322             :     /* Return sync performance metrics for report at checkpoint end */
    1323        2668 :     CheckpointStats.ckpt_sync_rels = processed;
    1324        2668 :     CheckpointStats.ckpt_longest_sync = longest;
    1325        2668 :     CheckpointStats.ckpt_agg_sync_time = total_elapsed;
    1326             : 
    1327             :     /* Flag successful completion of mdsync */
    1328        2668 :     mdsync_in_progress = false;
    1329        2668 : }
    1330             : 
    1331             : /*
    1332             :  * mdpreckpt() -- Do pre-checkpoint work
    1333             :  *
    1334             :  * To distinguish unlink requests that arrived before this checkpoint
    1335             :  * started from those that arrived during the checkpoint, we use a cycle
    1336             :  * counter similar to the one we use for fsync requests. That cycle
    1337             :  * counter is incremented here.
    1338             :  *
    1339             :  * This must be called *before* the checkpoint REDO point is determined.
    1340             :  * That ensures that we won't delete files too soon.
    1341             :  *
    1342             :  * Note that we can't do anything here that depends on the assumption
    1343             :  * that the checkpoint will be completed.
    1344             :  */
    1345             : void
    1346        2590 : mdpreckpt(void)
    1347             : {
    1348             :     /*
    1349             :      * Any unlink requests arriving after this point will be assigned the next
    1350             :      * cycle counter, and won't be unlinked until next checkpoint.
    1351             :      */
    1352        2590 :     mdckpt_cycle_ctr++;
    1353        2590 : }
    1354             : 
    1355             : /*
    1356             :  * mdpostckpt() -- Do post-checkpoint work
    1357             :  *
    1358             :  * Remove any lingering files that can now be safely removed.
    1359             :  */
    1360             : void
    1361        2590 : mdpostckpt(void)
    1362             : {
    1363             :     int         absorb_counter;
    1364             : 
    1365        2590 :     absorb_counter = UNLINKS_PER_ABSORB;
    1366       31960 :     while (pendingUnlinks != NIL)
    1367             :     {
    1368       26790 :         PendingUnlinkEntry *entry = (PendingUnlinkEntry *) linitial(pendingUnlinks);
    1369             :         char       *path;
    1370             : 
    1371             :         /*
    1372             :          * New entries are appended to the end, so if the entry is new we've
    1373             :          * reached the end of old entries.
    1374             :          *
    1375             :          * Note: if just the right number of consecutive checkpoints fail, we
    1376             :          * could be fooled here by cycle_ctr wraparound.  However, the only
    1377             :          * consequence is that we'd delay unlinking for one more checkpoint,
    1378             :          * which is perfectly tolerable.
    1379             :          */
    1380       26790 :         if (entry->cycle_ctr == mdckpt_cycle_ctr)
    1381          10 :             break;
    1382             : 
    1383             :         /* Unlink the file */
    1384       26780 :         path = relpathperm(entry->rnode, MAIN_FORKNUM);
    1385       26780 :         if (unlink(path) < 0)
    1386             :         {
    1387             :             /*
    1388             :              * There's a race condition, when the database is dropped at the
    1389             :              * same time that we process the pending unlink requests. If the
    1390             :              * DROP DATABASE deletes the file before we do, we will get ENOENT
    1391             :              * here. rmtree() also has to ignore ENOENT errors, to deal with
    1392             :              * the possibility that we delete the file first.
    1393             :              */
    1394           0 :             if (errno != ENOENT)
    1395           0 :                 ereport(WARNING,
    1396             :                         (errcode_for_file_access(),
    1397             :                          errmsg("could not remove file \"%s\": %m", path)));
    1398             :         }
    1399       26780 :         pfree(path);
    1400             : 
    1401             :         /* And remove the list entry */
    1402       26780 :         pendingUnlinks = list_delete_first(pendingUnlinks);
    1403       26780 :         pfree(entry);
    1404             : 
    1405             :         /*
    1406             :          * As in mdsync, we don't want to stop absorbing fsync requests for a
    1407             :          * long time when there are many deletions to be done.  We can safely
    1408             :          * call AbsorbFsyncRequests() at this point in the loop (note it might
    1409             :          * try to delete list entries).
    1410             :          */
    1411       26780 :         if (--absorb_counter <= 0)
    1412             :         {
    1413        2636 :             AbsorbFsyncRequests();
    1414        2636 :             absorb_counter = UNLINKS_PER_ABSORB;
    1415             :         }
    1416             :     }
    1417        2590 : }
    1418             : 
    1419             : /*
    1420             :  * register_dirty_segment() -- Mark a relation segment as needing fsync
    1421             :  *
    1422             :  * If there is a local pending-ops table, just make an entry in it for
    1423             :  * mdsync to process later.  Otherwise, try to pass off the fsync request
    1424             :  * to the checkpointer process.  If that fails, just do the fsync
    1425             :  * locally before returning (we hope this will not happen often enough
    1426             :  * to be a performance problem).
    1427             :  */
    1428             : static void
    1429      806718 : register_dirty_segment(SMgrRelation reln, ForkNumber forknum, MdfdVec *seg)
    1430             : {
    1431             :     /* Temp relations should never be fsync'd */
    1432             :     Assert(!SmgrIsTemp(reln));
    1433             : 
    1434      806718 :     if (pendingOpsTable)
    1435             :     {
    1436             :         /* push it into local pending-ops table */
    1437      628160 :         RememberFsyncRequest(reln->smgr_rnode.node, forknum, seg->mdfd_segno);
    1438             :     }
    1439             :     else
    1440             :     {
    1441      178558 :         if (ForwardFsyncRequest(reln->smgr_rnode.node, forknum, seg->mdfd_segno))
    1442      178558 :             return;             /* passed it off successfully */
    1443             : 
    1444           0 :         ereport(DEBUG1,
    1445             :                 (errmsg("could not forward fsync request because request queue is full")));
    1446             : 
    1447           0 :         if (FileSync(seg->mdfd_vfd, WAIT_EVENT_DATA_FILE_SYNC) < 0)
    1448           0 :             ereport(ERROR,
    1449             :                     (errcode_for_file_access(),
    1450             :                      errmsg("could not fsync file \"%s\": %m",
    1451             :                             FilePathName(seg->mdfd_vfd))));
    1452             :     }
    1453             : }
    1454             : 
    1455             : /*
    1456             :  * register_unlink() -- Schedule a file to be deleted after next checkpoint
    1457             :  *
    1458             :  * We don't bother passing in the fork number, because this is only used
    1459             :  * with main forks.
    1460             :  *
    1461             :  * As with register_dirty_segment, this could involve either a local or
    1462             :  * a remote pending-ops table.
    1463             :  */
    1464             : static void
    1465       31760 : register_unlink(RelFileNodeBackend rnode)
    1466             : {
    1467             :     /* Should never be used with temp relations */
    1468             :     Assert(!RelFileNodeBackendIsTemp(rnode));
    1469             : 
    1470       31760 :     if (pendingOpsTable)
    1471             :     {
    1472             :         /* push it into local pending-ops table */
    1473           0 :         RememberFsyncRequest(rnode.node, MAIN_FORKNUM,
    1474             :                              UNLINK_RELATION_REQUEST);
    1475             :     }
    1476             :     else
    1477             :     {
    1478             :         /*
    1479             :          * Notify the checkpointer about it.  If we fail to queue the request
    1480             :          * message, we have to sleep and try again, because we can't simply
    1481             :          * delete the file now.  Ugly, but hopefully won't happen often.
    1482             :          *
    1483             :          * XXX should we just leave the file orphaned instead?
    1484             :          */
    1485             :         Assert(IsUnderPostmaster);
    1486       63520 :         while (!ForwardFsyncRequest(rnode.node, MAIN_FORKNUM,
    1487             :                                     UNLINK_RELATION_REQUEST))
    1488           0 :             pg_usleep(10000L);  /* 10 msec seems a good number */
    1489             :     }
    1490       31760 : }
    1491             : 
    1492             : /*
    1493             :  * RememberFsyncRequest() -- callback from checkpointer side of fsync request
    1494             :  *
    1495             :  * We stuff fsync requests into the local hash table for execution
    1496             :  * during the checkpointer's next checkpoint.  UNLINK requests go into a
    1497             :  * separate linked list, however, because they get processed separately.
    1498             :  *
    1499             :  * The range of possible segment numbers is way less than the range of
    1500             :  * BlockNumber, so we can reserve high values of segno for special purposes.
    1501             :  * We define three:
    1502             :  * - FORGET_RELATION_FSYNC means to cancel pending fsyncs for a relation,
    1503             :  *   either for one fork, or all forks if forknum is InvalidForkNumber
    1504             :  * - FORGET_DATABASE_FSYNC means to cancel pending fsyncs for a whole database
    1505             :  * - UNLINK_RELATION_REQUEST is a request to delete the file after the next
    1506             :  *   checkpoint.
    1507             :  * Note also that we're assuming real segment numbers don't exceed INT_MAX.
    1508             :  *
    1509             :  * (Handling FORGET_DATABASE_FSYNC requests is a tad slow because the hash
    1510             :  * table has to be searched linearly, but dropping a database is a pretty
    1511             :  * heavyweight operation anyhow, so we'll live with it.)
    1512             :  */
    1513             : void
    1514      937590 : RememberFsyncRequest(RelFileNode rnode, ForkNumber forknum, BlockNumber segno)
    1515             : {
    1516             :     Assert(pendingOpsTable);
    1517             : 
    1518      937590 :     if (segno == FORGET_RELATION_FSYNC)
    1519             :     {
    1520             :         /* Remove any pending requests for the relation (one or all forks) */
    1521             :         PendingOperationEntry *entry;
    1522             : 
    1523      107152 :         entry = (PendingOperationEntry *) hash_search(pendingOpsTable,
    1524             :                                                       &rnode,
    1525             :                                                       HASH_FIND,
    1526             :                                                       NULL);
    1527      107152 :         if (entry)
    1528             :         {
    1529             :             /*
    1530             :              * We can't just delete the entry since mdsync could have an
    1531             :              * active hashtable scan.  Instead we delete the bitmapsets; this
    1532             :              * is safe because of the way mdsync is coded.  We also set the
    1533             :              * "canceled" flags so that mdsync can tell that a cancel arrived
    1534             :              * for the fork(s).
    1535             :              */
    1536       48864 :             if (forknum == InvalidForkNumber)
    1537             :             {
    1538             :                 /* remove requests for all forks */
    1539           0 :                 for (forknum = 0; forknum <= MAX_FORKNUM; forknum++)
    1540             :                 {
    1541           0 :                     bms_free(entry->requests[forknum]);
    1542           0 :                     entry->requests[forknum] = NULL;
    1543           0 :                     entry->canceled[forknum] = true;
    1544             :                 }
    1545             :             }
    1546             :             else
    1547             :             {
    1548             :                 /* remove requests for single fork */
    1549       48864 :                 bms_free(entry->requests[forknum]);
    1550       48864 :                 entry->requests[forknum] = NULL;
    1551       48864 :                 entry->canceled[forknum] = true;
    1552             :             }
    1553             :         }
    1554             :     }
    1555      830438 :     else if (segno == FORGET_DATABASE_FSYNC)
    1556             :     {
    1557             :         /* Remove any pending requests for the entire database */
    1558             :         HASH_SEQ_STATUS hstat;
    1559             :         PendingOperationEntry *entry;
    1560             :         ListCell   *cell,
    1561             :                    *prev,
    1562             :                    *next;
    1563             : 
    1564             :         /* Remove fsync requests */
    1565           6 :         hash_seq_init(&hstat, pendingOpsTable);
    1566          24 :         while ((entry = (PendingOperationEntry *) hash_seq_search(&hstat)) != NULL)
    1567             :         {
    1568          12 :             if (entry->rnode.dbNode == rnode.dbNode)
    1569             :             {
    1570             :                 /* remove requests for all forks */
    1571          20 :                 for (forknum = 0; forknum <= MAX_FORKNUM; forknum++)
    1572             :                 {
    1573          16 :                     bms_free(entry->requests[forknum]);
    1574          16 :                     entry->requests[forknum] = NULL;
    1575          16 :                     entry->canceled[forknum] = true;
    1576             :                 }
    1577             :             }
    1578             :         }
    1579             : 
    1580             :         /* Remove unlink requests */
    1581           6 :         prev = NULL;
    1582           6 :         for (cell = list_head(pendingUnlinks); cell; cell = next)
    1583             :         {
    1584           0 :             PendingUnlinkEntry *entry = (PendingUnlinkEntry *) lfirst(cell);
    1585             : 
    1586           0 :             next = lnext(cell);
    1587           0 :             if (entry->rnode.dbNode == rnode.dbNode)
    1588             :             {
    1589           0 :                 pendingUnlinks = list_delete_cell(pendingUnlinks, cell, prev);
    1590           0 :                 pfree(entry);
    1591             :             }
    1592             :             else
    1593           0 :                 prev = cell;
    1594             :         }
    1595             :     }
    1596      830432 :     else if (segno == UNLINK_RELATION_REQUEST)
    1597             :     {
    1598             :         /* Unlink request: put it in the linked list */
    1599       26780 :         MemoryContext oldcxt = MemoryContextSwitchTo(pendingOpsCxt);
    1600             :         PendingUnlinkEntry *entry;
    1601             : 
    1602             :         /* PendingUnlinkEntry doesn't store forknum, since it's always MAIN */
    1603             :         Assert(forknum == MAIN_FORKNUM);
    1604             : 
    1605       26780 :         entry = palloc(sizeof(PendingUnlinkEntry));
    1606       26780 :         entry->rnode = rnode;
    1607       26780 :         entry->cycle_ctr = mdckpt_cycle_ctr;
    1608             : 
    1609       26780 :         pendingUnlinks = lappend(pendingUnlinks, entry);
    1610             : 
    1611       26780 :         MemoryContextSwitchTo(oldcxt);
    1612             :     }
    1613             :     else
    1614             :     {
    1615             :         /* Normal case: enter a request to fsync this segment */
    1616      803652 :         MemoryContext oldcxt = MemoryContextSwitchTo(pendingOpsCxt);
    1617             :         PendingOperationEntry *entry;
    1618             :         bool        found;
    1619             : 
    1620      803652 :         entry = (PendingOperationEntry *) hash_search(pendingOpsTable,
    1621             :                                                       &rnode,
    1622             :                                                       HASH_ENTER,
    1623             :                                                       &found);
    1624             :         /* if new entry, initialize it */
    1625      803652 :         if (!found)
    1626             :         {
    1627       96016 :             entry->cycle_ctr = mdsync_cycle_ctr;
    1628       96016 :             MemSet(entry->requests, 0, sizeof(entry->requests));
    1629       96016 :             MemSet(entry->canceled, 0, sizeof(entry->canceled));
    1630             :         }
    1631             : 
    1632             :         /*
    1633             :          * NB: it's intentional that we don't change cycle_ctr if the entry
    1634             :          * already exists.  The cycle_ctr must represent the oldest fsync
    1635             :          * request that could be in the entry.
    1636             :          */
    1637             : 
    1638      803652 :         entry->requests[forknum] = bms_add_member(entry->requests[forknum],
    1639             :                                                   (int) segno);
    1640             : 
    1641      803652 :         MemoryContextSwitchTo(oldcxt);
    1642             :     }
    1643      937590 : }
    1644             : 
    1645             : /*
    1646             :  * ForgetRelationFsyncRequests -- forget any fsyncs for a relation fork
    1647             :  *
    1648             :  * forknum == InvalidForkNumber means all forks, although this code doesn't
    1649             :  * actually know that, since it's just forwarding the request elsewhere.
    1650             :  */
    1651             : void
    1652      127096 : ForgetRelationFsyncRequests(RelFileNode rnode, ForkNumber forknum)
    1653             : {
    1654      127096 :     if (pendingOpsTable)
    1655             :     {
    1656             :         /* standalone backend or startup process: fsync state is local */
    1657          32 :         RememberFsyncRequest(rnode, forknum, FORGET_RELATION_FSYNC);
    1658             :     }
    1659      127064 :     else if (IsUnderPostmaster)
    1660             :     {
    1661             :         /*
    1662             :          * Notify the checkpointer about it.  If we fail to queue the cancel
    1663             :          * message, we have to sleep and try again ... ugly, but hopefully
    1664             :          * won't happen often.
    1665             :          *
    1666             :          * XXX should we CHECK_FOR_INTERRUPTS in this loop?  Escaping with an
    1667             :          * error would leave the no-longer-used file still present on disk,
    1668             :          * which would be bad, so I'm inclined to assume that the checkpointer
    1669             :          * will always empty the queue soon.
    1670             :          */
    1671      254128 :         while (!ForwardFsyncRequest(rnode, forknum, FORGET_RELATION_FSYNC))
    1672           0 :             pg_usleep(10000L);  /* 10 msec seems a good number */
    1673             : 
    1674             :         /*
    1675             :          * Note we don't wait for the checkpointer to actually absorb the
    1676             :          * cancel message; see mdsync() for the implications.
    1677             :          */
    1678             :     }
    1679      127096 : }
    1680             : 
    1681             : /*
    1682             :  * ForgetDatabaseFsyncRequests -- forget any fsyncs and unlinks for a DB
    1683             :  */
    1684             : void
    1685          18 : ForgetDatabaseFsyncRequests(Oid dbid)
    1686             : {
    1687             :     RelFileNode rnode;
    1688             : 
    1689          18 :     rnode.dbNode = dbid;
    1690          18 :     rnode.spcNode = 0;
    1691          18 :     rnode.relNode = 0;
    1692             : 
    1693          18 :     if (pendingOpsTable)
    1694             :     {
    1695             :         /* standalone backend or startup process: fsync state is local */
    1696           0 :         RememberFsyncRequest(rnode, InvalidForkNumber, FORGET_DATABASE_FSYNC);
    1697             :     }
    1698          18 :     else if (IsUnderPostmaster)
    1699             :     {
    1700             :         /* see notes in ForgetRelationFsyncRequests */
    1701          36 :         while (!ForwardFsyncRequest(rnode, InvalidForkNumber,
    1702             :                                     FORGET_DATABASE_FSYNC))
    1703           0 :             pg_usleep(10000L);  /* 10 msec seems a good number */
    1704             :     }
    1705          18 : }
    1706             : 
    1707             : /*
    1708             :  * DropRelationFiles -- drop files of all given relations
    1709             :  */
    1710             : void
    1711          70 : DropRelationFiles(RelFileNode *delrels, int ndelrels, bool isRedo)
    1712             : {
    1713             :     SMgrRelation *srels;
    1714             :     int         i;
    1715             : 
    1716          70 :     srels = palloc(sizeof(SMgrRelation) * ndelrels);
    1717          86 :     for (i = 0; i < ndelrels; i++)
    1718             :     {
    1719          16 :         SMgrRelation srel = smgropen(delrels[i], InvalidBackendId);
    1720             : 
    1721          16 :         if (isRedo)
    1722             :         {
    1723             :             ForkNumber  fork;
    1724             : 
    1725          70 :             for (fork = 0; fork <= MAX_FORKNUM; fork++)
    1726          56 :                 XLogDropRelation(delrels[i], fork);
    1727             :         }
    1728          16 :         srels[i] = srel;
    1729             :     }
    1730             : 
    1731          70 :     smgrdounlinkall(srels, ndelrels, isRedo);
    1732             : 
    1733             :     /*
    1734             :      * Call smgrclose() in reverse order as when smgropen() is called.
    1735             :      * This trick enables remove_from_unowned_list() in smgrclose()
    1736             :      * to search the SMgrRelation from the unowned list,
    1737             :      * with O(1) performance.
    1738             :      */
    1739          86 :     for (i = ndelrels - 1; i >= 0; i--)
    1740          16 :         smgrclose(srels[i]);
    1741          70 :     pfree(srels);
    1742          70 : }
    1743             : 
    1744             : 
    1745             : /*
    1746             :  *  _fdvec_resize() -- Resize the fork's open segments array
    1747             :  */
    1748             : static void
    1749     1864976 : _fdvec_resize(SMgrRelation reln,
    1750             :               ForkNumber forknum,
    1751             :               int nseg)
    1752             : {
    1753     1864976 :     if (nseg == 0)
    1754             :     {
    1755      812474 :         if (reln->md_num_open_segs[forknum] > 0)
    1756             :         {
    1757      812474 :             pfree(reln->md_seg_fds[forknum]);
    1758      812474 :             reln->md_seg_fds[forknum] = NULL;
    1759             :         }
    1760             :     }
    1761     1052502 :     else if (reln->md_num_open_segs[forknum] == 0)
    1762             :     {
    1763     1052502 :         reln->md_seg_fds[forknum] =
    1764     1052502 :             MemoryContextAlloc(MdCxt, sizeof(MdfdVec) * nseg);
    1765             :     }
    1766             :     else
    1767             :     {
    1768             :         /*
    1769             :          * It doesn't seem worthwhile complicating the code by having a more
    1770             :          * aggressive growth strategy here; the number of segments doesn't
    1771             :          * grow that fast, and the memory context internally will sometimes
    1772             :          * avoid doing an actual reallocation.
    1773             :          */
    1774           0 :         reln->md_seg_fds[forknum] =
    1775           0 :             repalloc(reln->md_seg_fds[forknum],
    1776             :                      sizeof(MdfdVec) * nseg);
    1777             :     }
    1778             : 
    1779     1864976 :     reln->md_num_open_segs[forknum] = nseg;
    1780     1864976 : }
    1781             : 
    1782             : /*
    1783             :  * Return the filename for the specified segment of the relation. The
    1784             :  * returned string is palloc'd.
    1785             :  */
    1786             : static char *
    1787           0 : _mdfd_segpath(SMgrRelation reln, ForkNumber forknum, BlockNumber segno)
    1788             : {
    1789             :     char       *path,
    1790             :                *fullpath;
    1791             : 
    1792           0 :     path = relpath(reln->smgr_rnode, forknum);
    1793             : 
    1794           0 :     if (segno > 0)
    1795             :     {
    1796           0 :         fullpath = psprintf("%s.%u", path, segno);
    1797           0 :         pfree(path);
    1798             :     }
    1799             :     else
    1800           0 :         fullpath = path;
    1801             : 
    1802           0 :     return fullpath;
    1803             : }
    1804             : 
    1805             : /*
    1806             :  * Open the specified segment of the relation,
    1807             :  * and make a MdfdVec object for it.  Returns NULL on failure.
    1808             :  */
    1809             : static MdfdVec *
    1810           0 : _mdfd_openseg(SMgrRelation reln, ForkNumber forknum, BlockNumber segno,
    1811             :               int oflags)
    1812             : {
    1813             :     MdfdVec    *v;
    1814             :     int         fd;
    1815             :     char       *fullpath;
    1816             : 
    1817           0 :     fullpath = _mdfd_segpath(reln, forknum, segno);
    1818             : 
    1819             :     /* open the file */
    1820           0 :     fd = PathNameOpenFile(fullpath, O_RDWR | PG_BINARY | oflags);
    1821             : 
    1822           0 :     pfree(fullpath);
    1823             : 
    1824           0 :     if (fd < 0)
    1825           0 :         return NULL;
    1826             : 
    1827           0 :     if (segno <= reln->md_num_open_segs[forknum])
    1828           0 :         _fdvec_resize(reln, forknum, segno + 1);
    1829             : 
    1830             :     /* fill the entry */
    1831           0 :     v = &reln->md_seg_fds[forknum][segno];
    1832           0 :     v->mdfd_vfd = fd;
    1833           0 :     v->mdfd_segno = segno;
    1834             : 
    1835             :     Assert(_mdnblocks(reln, forknum, v) <= ((BlockNumber) RELSEG_SIZE));
    1836             : 
    1837             :     /* all done */
    1838           0 :     return v;
    1839             : }
    1840             : 
    1841             : /*
    1842             :  *  _mdfd_getseg() -- Find the segment of the relation holding the
    1843             :  *      specified block.
    1844             :  *
    1845             :  * If the segment doesn't exist, we ereport, return NULL, or create the
    1846             :  * segment, according to "behavior".  Note: skipFsync is only used in the
    1847             :  * EXTENSION_CREATE case.
    1848             :  */
    1849             : static MdfdVec *
    1850     1559004 : _mdfd_getseg(SMgrRelation reln, ForkNumber forknum, BlockNumber blkno,
    1851             :              bool skipFsync, int behavior)
    1852             : {
    1853             :     MdfdVec    *v;
    1854             :     BlockNumber targetseg;
    1855             :     BlockNumber nextsegno;
    1856             : 
    1857             :     /* some way to handle non-existent segments needs to be specified */
    1858             :     Assert(behavior &
    1859             :            (EXTENSION_FAIL | EXTENSION_CREATE | EXTENSION_RETURN_NULL));
    1860             : 
    1861     1559004 :     targetseg = blkno / ((BlockNumber) RELSEG_SIZE);
    1862             : 
    1863             :     /* if an existing and opened segment, we're done */
    1864     1559004 :     if (targetseg < reln->md_num_open_segs[forknum])
    1865             :     {
    1866     1424884 :         v = &reln->md_seg_fds[forknum][targetseg];
    1867     1424884 :         return v;
    1868             :     }
    1869             : 
    1870             :     /*
    1871             :      * The target segment is not yet open. Iterate over all the segments
    1872             :      * between the last opened and the target segment. This way missing
    1873             :      * segments either raise an error, or get created (according to
    1874             :      * 'behavior'). Start with either the last opened, or the first segment if
    1875             :      * none was opened before.
    1876             :      */
    1877      134120 :     if (reln->md_num_open_segs[forknum] > 0)
    1878           0 :         v = &reln->md_seg_fds[forknum][reln->md_num_open_segs[forknum] - 1];
    1879             :     else
    1880             :     {
    1881      134120 :         v = mdopen(reln, forknum, behavior);
    1882      134120 :         if (!v)
    1883           0 :             return NULL;        /* if behavior & EXTENSION_RETURN_NULL */
    1884             :     }
    1885             : 
    1886      268240 :     for (nextsegno = reln->md_num_open_segs[forknum];
    1887           0 :          nextsegno <= targetseg; nextsegno++)
    1888             :     {
    1889           0 :         BlockNumber nblocks = _mdnblocks(reln, forknum, v);
    1890           0 :         int         flags = 0;
    1891             : 
    1892             :         Assert(nextsegno == v->mdfd_segno + 1);
    1893             : 
    1894           0 :         if (nblocks > ((BlockNumber) RELSEG_SIZE))
    1895           0 :             elog(FATAL, "segment too big");
    1896             : 
    1897           0 :         if ((behavior & EXTENSION_CREATE) ||
    1898           0 :             (InRecovery && (behavior & EXTENSION_CREATE_RECOVERY)))
    1899             :         {
    1900             :             /*
    1901             :              * Normally we will create new segments only if authorized by the
    1902             :              * caller (i.e., we are doing mdextend()).  But when doing WAL
    1903             :              * recovery, create segments anyway; this allows cases such as
    1904             :              * replaying WAL data that has a write into a high-numbered
    1905             :              * segment of a relation that was later deleted. We want to go
    1906             :              * ahead and create the segments so we can finish out the replay.
    1907             :              * However if the caller has specified
    1908             :              * EXTENSION_REALLY_RETURN_NULL, then extension is not desired
    1909             :              * even in recovery; we won't reach this point in that case.
    1910             :              *
    1911             :              * We have to maintain the invariant that segments before the last
    1912             :              * active segment are of size RELSEG_SIZE; therefore, if
    1913             :              * extending, pad them out with zeroes if needed.  (This only
    1914             :              * matters if in recovery, or if the caller is extending the
    1915             :              * relation discontiguously, but that can happen in hash indexes.)
    1916             :              */
    1917           0 :             if (nblocks < ((BlockNumber) RELSEG_SIZE))
    1918             :             {
    1919           0 :                 char       *zerobuf = palloc0(BLCKSZ);
    1920             : 
    1921           0 :                 mdextend(reln, forknum,
    1922           0 :                          nextsegno * ((BlockNumber) RELSEG_SIZE) - 1,
    1923             :                          zerobuf, skipFsync);
    1924           0 :                 pfree(zerobuf);
    1925             :             }
    1926           0 :             flags = O_CREAT;
    1927             :         }
    1928           0 :         else if (!(behavior & EXTENSION_DONT_CHECK_SIZE) &&
    1929             :                  nblocks < ((BlockNumber) RELSEG_SIZE))
    1930             :         {
    1931             :             /*
    1932             :              * When not extending (or explicitly including truncated
    1933             :              * segments), only open the next segment if the current one is
    1934             :              * exactly RELSEG_SIZE.  If not (this branch), either return NULL
    1935             :              * or fail.
    1936             :              */
    1937           0 :             if (behavior & EXTENSION_RETURN_NULL)
    1938             :             {
    1939             :                 /*
    1940             :                  * Some callers discern between reasons for _mdfd_getseg()
    1941             :                  * returning NULL based on errno. As there's no failing
    1942             :                  * syscall involved in this case, explicitly set errno to
    1943             :                  * ENOENT, as that seems the closest interpretation.
    1944             :                  */
    1945           0 :                 errno = ENOENT;
    1946           0 :                 return NULL;
    1947             :             }
    1948             : 
    1949           0 :             ereport(ERROR,
    1950             :                     (errcode_for_file_access(),
    1951             :                      errmsg("could not open file \"%s\" (target block %u): previous segment is only %u blocks",
    1952             :                             _mdfd_segpath(reln, forknum, nextsegno),
    1953             :                             blkno, nblocks)));
    1954             :         }
    1955             : 
    1956           0 :         v = _mdfd_openseg(reln, forknum, nextsegno, flags);
    1957             : 
    1958           0 :         if (v == NULL)
    1959             :         {
    1960           0 :             if ((behavior & EXTENSION_RETURN_NULL) &&
    1961           0 :                 FILE_POSSIBLY_DELETED(errno))
    1962           0 :                 return NULL;
    1963           0 :             ereport(ERROR,
    1964             :                     (errcode_for_file_access(),
    1965             :                      errmsg("could not open file \"%s\" (target block %u): %m",
    1966             :                             _mdfd_segpath(reln, forknum, nextsegno),
    1967             :                             blkno)));
    1968             :         }
    1969             :     }
    1970             : 
    1971      134120 :     return v;
    1972             : }
    1973             : 
    1974             : /*
    1975             :  * Get number of blocks present in a single disk file
    1976             :  */
    1977             : static BlockNumber
    1978     3413082 : _mdnblocks(SMgrRelation reln, ForkNumber forknum, MdfdVec *seg)
    1979             : {
    1980             :     off_t       len;
    1981             : 
    1982     3413082 :     len = FileSeek(seg->mdfd_vfd, 0L, SEEK_END);
    1983     3413082 :     if (len < 0)
    1984           0 :         ereport(ERROR,
    1985             :                 (errcode_for_file_access(),
    1986             :                  errmsg("could not seek to end of file \"%s\": %m",
    1987             :                         FilePathName(seg->mdfd_vfd))));
    1988             :     /* note that this calculation will ignore any partial block at EOF */
    1989     3413082 :     return (BlockNumber) (len / BLCKSZ);
    1990             : }

Generated by: LCOV version 1.13