LCOV - code coverage report
Current view: top level - src/backend/storage/sync - sync.c (source / functions) Hit Total Coverage
Test: PostgreSQL 15devel Lines: 96 115 83.5 %
Date: 2021-09-17 16:07:28 Functions: 6 6 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*-------------------------------------------------------------------------
       2             :  *
       3             :  * sync.c
       4             :  *    File synchronization management code.
       5             :  *
       6             :  * Portions Copyright (c) 1996-2021, PostgreSQL Global Development Group
       7             :  * Portions Copyright (c) 1994, Regents of the University of California
       8             :  *
       9             :  *
      10             :  * IDENTIFICATION
      11             :  *    src/backend/storage/sync/sync.c
      12             :  *
      13             :  *-------------------------------------------------------------------------
      14             :  */
      15             : #include "postgres.h"
      16             : 
      17             : #include <unistd.h>
      18             : #include <fcntl.h>
      19             : #include <sys/file.h>
      20             : 
      21             : #include "access/commit_ts.h"
      22             : #include "access/clog.h"
      23             : #include "access/multixact.h"
      24             : #include "access/xlog.h"
      25             : #include "access/xlogutils.h"
      26             : #include "commands/tablespace.h"
      27             : #include "miscadmin.h"
      28             : #include "pgstat.h"
      29             : #include "portability/instr_time.h"
      30             : #include "postmaster/bgwriter.h"
      31             : #include "storage/bufmgr.h"
      32             : #include "storage/ipc.h"
      33             : #include "storage/md.h"
      34             : #include "utils/hsearch.h"
      35             : #include "utils/inval.h"
      36             : #include "utils/memutils.h"
      37             : 
      38             : static MemoryContext pendingOpsCxt; /* context for the pending ops state  */
      39             : 
      40             : /*
      41             :  * In some contexts (currently, standalone backends and the checkpointer)
      42             :  * we keep track of pending fsync operations: we need to remember all relation
      43             :  * segments that have been written since the last checkpoint, so that we can
      44             :  * fsync them down to disk before completing the next checkpoint.  This hash
      45             :  * table remembers the pending operations.  We use a hash table mostly as
      46             :  * a convenient way of merging duplicate requests.
      47             :  *
      48             :  * We use a similar mechanism to remember no-longer-needed files that can
      49             :  * be deleted after the next checkpoint, but we use a linked list instead of
      50             :  * a hash table, because we don't expect there to be any duplicate requests.
      51             :  *
      52             :  * These mechanisms are only used for non-temp relations; we never fsync
      53             :  * temp rels, nor do we need to postpone their deletion (see comments in
      54             :  * mdunlink).
      55             :  *
      56             :  * (Regular backends do not track pending operations locally, but forward
      57             :  * them to the checkpointer.)
      58             :  */
      59             : typedef uint16 CycleCtr;        /* can be any convenient integer size */
      60             : 
      61             : typedef struct
      62             : {
      63             :     FileTag     tag;            /* identifies handler and file */
      64             :     CycleCtr    cycle_ctr;      /* sync_cycle_ctr of oldest request */
      65             :     bool        canceled;       /* canceled is true if we canceled "recently" */
      66             : } PendingFsyncEntry;
      67             : 
      68             : typedef struct
      69             : {
      70             :     FileTag     tag;            /* identifies handler and file */
      71             :     CycleCtr    cycle_ctr;      /* checkpoint_cycle_ctr when request was made */
      72             : } PendingUnlinkEntry;
      73             : 
      74             : static HTAB *pendingOps = NULL;
      75             : static List *pendingUnlinks = NIL;
      76             : static MemoryContext pendingOpsCxt; /* context for the above  */
      77             : 
      78             : static CycleCtr sync_cycle_ctr = 0;
      79             : static CycleCtr checkpoint_cycle_ctr = 0;
      80             : 
      81             : /* Intervals for calling AbsorbSyncRequests */
      82             : #define FSYNCS_PER_ABSORB       10
      83             : #define UNLINKS_PER_ABSORB      10
      84             : 
      85             : /*
      86             :  * Function pointers for handling sync and unlink requests.
      87             :  */
      88             : typedef struct SyncOps
      89             : {
      90             :     int         (*sync_syncfiletag) (const FileTag *ftag, char *path);
      91             :     int         (*sync_unlinkfiletag) (const FileTag *ftag, char *path);
      92             :     bool        (*sync_filetagmatches) (const FileTag *ftag,
      93             :                                         const FileTag *candidate);
      94             : } SyncOps;
      95             : 
      96             : /*
      97             :  * These indexes must correspond to the values of the SyncRequestHandler enum.
      98             :  */
      99             : static const SyncOps syncsw[] = {
     100             :     /* magnetic disk */
     101             :     [SYNC_HANDLER_MD] = {
     102             :         .sync_syncfiletag = mdsyncfiletag,
     103             :         .sync_unlinkfiletag = mdunlinkfiletag,
     104             :         .sync_filetagmatches = mdfiletagmatches
     105             :     },
     106             :     /* pg_xact */
     107             :     [SYNC_HANDLER_CLOG] = {
     108             :         .sync_syncfiletag = clogsyncfiletag
     109             :     },
     110             :     /* pg_commit_ts */
     111             :     [SYNC_HANDLER_COMMIT_TS] = {
     112             :         .sync_syncfiletag = committssyncfiletag
     113             :     },
     114             :     /* pg_multixact/offsets */
     115             :     [SYNC_HANDLER_MULTIXACT_OFFSET] = {
     116             :         .sync_syncfiletag = multixactoffsetssyncfiletag
     117             :     },
     118             :     /* pg_multixact/members */
     119             :     [SYNC_HANDLER_MULTIXACT_MEMBER] = {
     120             :         .sync_syncfiletag = multixactmemberssyncfiletag
     121             :     }
     122             : };
     123             : 
     124             : /*
     125             :  * Initialize data structures for the file sync tracking.
     126             :  */
     127             : void
     128       17264 : InitSync(void)
     129             : {
     130             :     /*
     131             :      * Create pending-operations hashtable if we need it.  Currently, we need
     132             :      * it if we are standalone (not under a postmaster) or if we are a
     133             :      * checkpointer auxiliary process.
     134             :      */
     135       17264 :     if (!IsUnderPostmaster || AmCheckpointerProcess())
     136             :     {
     137             :         HASHCTL     hash_ctl;
     138             : 
     139             :         /*
     140             :          * XXX: The checkpointer needs to add entries to the pending ops table
     141             :          * when absorbing fsync requests.  That is done within a critical
     142             :          * section, which isn't usually allowed, but we make an exception. It
     143             :          * means that there's a theoretical possibility that you run out of
     144             :          * memory while absorbing fsync requests, which leads to a PANIC.
     145             :          * Fortunately the hash table is small so that's unlikely to happen in
     146             :          * practice.
     147             :          */
     148        1458 :         pendingOpsCxt = AllocSetContextCreate(TopMemoryContext,
     149             :                                               "Pending ops context",
     150             :                                               ALLOCSET_DEFAULT_SIZES);
     151        1458 :         MemoryContextAllowInCriticalSection(pendingOpsCxt, true);
     152             : 
     153        1458 :         hash_ctl.keysize = sizeof(FileTag);
     154        1458 :         hash_ctl.entrysize = sizeof(PendingFsyncEntry);
     155        1458 :         hash_ctl.hcxt = pendingOpsCxt;
     156        1458 :         pendingOps = hash_create("Pending Ops Table",
     157             :                                  100L,
     158             :                                  &hash_ctl,
     159             :                                  HASH_ELEM | HASH_BLOBS | HASH_CONTEXT);
     160        1458 :         pendingUnlinks = NIL;
     161             :     }
     162             : 
     163       17264 : }
     164             : 
     165             : /*
     166             :  * SyncPreCheckpoint() -- Do pre-checkpoint work
     167             :  *
     168             :  * To distinguish unlink requests that arrived before this checkpoint
     169             :  * started from those that arrived during the checkpoint, we use a cycle
     170             :  * counter similar to the one we use for fsync requests. That cycle
     171             :  * counter is incremented here.
     172             :  *
     173             :  * This must be called *before* the checkpoint REDO point is determined.
     174             :  * That ensures that we won't delete files too soon.
     175             :  *
     176             :  * Note that we can't do anything here that depends on the assumption
     177             :  * that the checkpoint will be completed.
     178             :  */
     179             : void
     180        3916 : SyncPreCheckpoint(void)
     181             : {
     182             :     /*
     183             :      * Any unlink requests arriving after this point will be assigned the next
     184             :      * cycle counter, and won't be unlinked until next checkpoint.
     185             :      */
     186        3916 :     checkpoint_cycle_ctr++;
     187        3916 : }
     188             : 
     189             : /*
     190             :  * SyncPostCheckpoint() -- Do post-checkpoint work
     191             :  *
     192             :  * Remove any lingering files that can now be safely removed.
     193             :  */
     194             : void
     195        3906 : SyncPostCheckpoint(void)
     196             : {
     197             :     int         absorb_counter;
     198             : 
     199        3906 :     absorb_counter = UNLINKS_PER_ABSORB;
     200       40170 :     while (pendingUnlinks != NIL)
     201             :     {
     202       36290 :         PendingUnlinkEntry *entry = (PendingUnlinkEntry *) linitial(pendingUnlinks);
     203             :         char        path[MAXPGPATH];
     204             : 
     205             :         /*
     206             :          * New entries are appended to the end, so if the entry is new we've
     207             :          * reached the end of old entries.
     208             :          *
     209             :          * Note: if just the right number of consecutive checkpoints fail, we
     210             :          * could be fooled here by cycle_ctr wraparound.  However, the only
     211             :          * consequence is that we'd delay unlinking for one more checkpoint,
     212             :          * which is perfectly tolerable.
     213             :          */
     214       36290 :         if (entry->cycle_ctr == checkpoint_cycle_ctr)
     215          26 :             break;
     216             : 
     217             :         /* Unlink the file */
     218       36264 :         if (syncsw[entry->tag.handler].sync_unlinkfiletag(&entry->tag,
     219             :                                                           path) < 0)
     220             :         {
     221             :             /*
     222             :              * There's a race condition, when the database is dropped at the
     223             :              * same time that we process the pending unlink requests. If the
     224             :              * DROP DATABASE deletes the file before we do, we will get ENOENT
     225             :              * here. rmtree() also has to ignore ENOENT errors, to deal with
     226             :              * the possibility that we delete the file first.
     227             :              */
     228           4 :             if (errno != ENOENT)
     229           0 :                 ereport(WARNING,
     230             :                         (errcode_for_file_access(),
     231             :                          errmsg("could not remove file \"%s\": %m", path)));
     232             :         }
     233             : 
     234             :         /* And remove the list entry */
     235       36264 :         pendingUnlinks = list_delete_first(pendingUnlinks);
     236       36264 :         pfree(entry);
     237             : 
     238             :         /*
     239             :          * As in ProcessSyncRequests, we don't want to stop absorbing fsync
     240             :          * requests for a long time when there are many deletions to be done.
     241             :          * We can safely call AbsorbSyncRequests() at this point in the loop
     242             :          * (note it might try to delete list entries).
     243             :          */
     244       36264 :         if (--absorb_counter <= 0)
     245             :         {
     246        3560 :             AbsorbSyncRequests();
     247        3560 :             absorb_counter = UNLINKS_PER_ABSORB;
     248             :         }
     249             :     }
     250        3906 : }
     251             : 
     252             : /*
     253             : 
     254             :  *  ProcessSyncRequests() -- Process queued fsync requests.
     255             :  */
     256             : void
     257        3944 : ProcessSyncRequests(void)
     258             : {
     259             :     static bool sync_in_progress = false;
     260             : 
     261             :     HASH_SEQ_STATUS hstat;
     262             :     PendingFsyncEntry *entry;
     263             :     int         absorb_counter;
     264             : 
     265             :     /* Statistics on sync times */
     266        3944 :     int         processed = 0;
     267             :     instr_time  sync_start,
     268             :                 sync_end,
     269             :                 sync_diff;
     270             :     uint64      elapsed;
     271        3944 :     uint64      longest = 0;
     272        3944 :     uint64      total_elapsed = 0;
     273             : 
     274             :     /*
     275             :      * This is only called during checkpoints, and checkpoints should only
     276             :      * occur in processes that have created a pendingOps.
     277             :      */
     278        3944 :     if (!pendingOps)
     279           0 :         elog(ERROR, "cannot sync without a pendingOps table");
     280             : 
     281             :     /*
     282             :      * If we are in the checkpointer, the sync had better include all fsync
     283             :      * requests that were queued by backends up to this point.  The tightest
     284             :      * race condition that could occur is that a buffer that must be written
     285             :      * and fsync'd for the checkpoint could have been dumped by a backend just
     286             :      * before it was visited by BufferSync().  We know the backend will have
     287             :      * queued an fsync request before clearing the buffer's dirtybit, so we
     288             :      * are safe as long as we do an Absorb after completing BufferSync().
     289             :      */
     290        3944 :     AbsorbSyncRequests();
     291             : 
     292             :     /*
     293             :      * To avoid excess fsync'ing (in the worst case, maybe a never-terminating
     294             :      * checkpoint), we want to ignore fsync requests that are entered into the
     295             :      * hashtable after this point --- they should be processed next time,
     296             :      * instead.  We use sync_cycle_ctr to tell old entries apart from new
     297             :      * ones: new ones will have cycle_ctr equal to the incremented value of
     298             :      * sync_cycle_ctr.
     299             :      *
     300             :      * In normal circumstances, all entries present in the table at this point
     301             :      * will have cycle_ctr exactly equal to the current (about to be old)
     302             :      * value of sync_cycle_ctr.  However, if we fail partway through the
     303             :      * fsync'ing loop, then older values of cycle_ctr might remain when we
     304             :      * come back here to try again.  Repeated checkpoint failures would
     305             :      * eventually wrap the counter around to the point where an old entry
     306             :      * might appear new, causing us to skip it, possibly allowing a checkpoint
     307             :      * to succeed that should not have.  To forestall wraparound, any time the
     308             :      * previous ProcessSyncRequests() failed to complete, run through the
     309             :      * table and forcibly set cycle_ctr = sync_cycle_ctr.
     310             :      *
     311             :      * Think not to merge this loop with the main loop, as the problem is
     312             :      * exactly that that loop may fail before having visited all the entries.
     313             :      * From a performance point of view it doesn't matter anyway, as this path
     314             :      * will never be taken in a system that's functioning normally.
     315             :      */
     316        3944 :     if (sync_in_progress)
     317             :     {
     318             :         /* prior try failed, so update any stale cycle_ctr values */
     319           0 :         hash_seq_init(&hstat, pendingOps);
     320           0 :         while ((entry = (PendingFsyncEntry *) hash_seq_search(&hstat)) != NULL)
     321             :         {
     322           0 :             entry->cycle_ctr = sync_cycle_ctr;
     323             :         }
     324             :     }
     325             : 
     326             :     /* Advance counter so that new hashtable entries are distinguishable */
     327        3944 :     sync_cycle_ctr++;
     328             : 
     329             :     /* Set flag to detect failure if we don't reach the end of the loop */
     330        3944 :     sync_in_progress = true;
     331             : 
     332             :     /* Now scan the hashtable for fsync requests to process */
     333        3944 :     absorb_counter = FSYNCS_PER_ABSORB;
     334        3944 :     hash_seq_init(&hstat, pendingOps);
     335      156254 :     while ((entry = (PendingFsyncEntry *) hash_seq_search(&hstat)) != NULL)
     336             :     {
     337             :         int         failures;
     338             : 
     339             :         /*
     340             :          * If the entry is new then don't process it this time; it is new.
     341             :          * Note "continue" bypasses the hash-remove call at the bottom of the
     342             :          * loop.
     343             :          */
     344      152310 :         if (entry->cycle_ctr == sync_cycle_ctr)
     345           0 :             continue;
     346             : 
     347             :         /* Else assert we haven't missed it */
     348             :         Assert((CycleCtr) (entry->cycle_ctr + 1) == sync_cycle_ctr);
     349             : 
     350             :         /*
     351             :          * If fsync is off then we don't have to bother opening the file at
     352             :          * all.  (We delay checking until this point so that changing fsync on
     353             :          * the fly behaves sensibly.)
     354             :          */
     355      152310 :         if (enableFsync)
     356             :         {
     357             :             /*
     358             :              * If in checkpointer, we want to absorb pending requests every so
     359             :              * often to prevent overflow of the fsync request queue.  It is
     360             :              * unspecified whether newly-added entries will be visited by
     361             :              * hash_seq_search, but we don't care since we don't need to
     362             :              * process them anyway.
     363             :              */
     364          68 :             if (--absorb_counter <= 0)
     365             :             {
     366           4 :                 AbsorbSyncRequests();
     367           4 :                 absorb_counter = FSYNCS_PER_ABSORB;
     368             :             }
     369             : 
     370             :             /*
     371             :              * The fsync table could contain requests to fsync segments that
     372             :              * have been deleted (unlinked) by the time we get to them. Rather
     373             :              * than just hoping an ENOENT (or EACCES on Windows) error can be
     374             :              * ignored, what we do on error is absorb pending requests and
     375             :              * then retry. Since mdunlink() queues a "cancel" message before
     376             :              * actually unlinking, the fsync request is guaranteed to be
     377             :              * marked canceled after the absorb if it really was this case.
     378             :              * DROP DATABASE likewise has to tell us to forget fsync requests
     379             :              * before it starts deletions.
     380             :              */
     381          68 :             for (failures = 0; !entry->canceled; failures++)
     382             :             {
     383             :                 char        path[MAXPGPATH];
     384             : 
     385          68 :                 INSTR_TIME_SET_CURRENT(sync_start);
     386          68 :                 if (syncsw[entry->tag.handler].sync_syncfiletag(&entry->tag,
     387             :                                                                 path) == 0)
     388             :                 {
     389             :                     /* Success; update statistics about sync timing */
     390          68 :                     INSTR_TIME_SET_CURRENT(sync_end);
     391          68 :                     sync_diff = sync_end;
     392          68 :                     INSTR_TIME_SUBTRACT(sync_diff, sync_start);
     393          68 :                     elapsed = INSTR_TIME_GET_MICROSEC(sync_diff);
     394          68 :                     if (elapsed > longest)
     395           6 :                         longest = elapsed;
     396          68 :                     total_elapsed += elapsed;
     397          68 :                     processed++;
     398             : 
     399          68 :                     if (log_checkpoints)
     400           0 :                         elog(DEBUG1, "checkpoint sync: number=%d file=%s time=%.3f ms",
     401             :                              processed,
     402             :                              path,
     403             :                              (double) elapsed / 1000);
     404             : 
     405          68 :                     break;      /* out of retry loop */
     406             :                 }
     407             : 
     408             :                 /*
     409             :                  * It is possible that the relation has been dropped or
     410             :                  * truncated since the fsync request was entered. Therefore,
     411             :                  * allow ENOENT, but only if we didn't fail already on this
     412             :                  * file.
     413             :                  */
     414           0 :                 if (!FILE_POSSIBLY_DELETED(errno) || failures > 0)
     415           0 :                     ereport(data_sync_elevel(ERROR),
     416             :                             (errcode_for_file_access(),
     417             :                              errmsg("could not fsync file \"%s\": %m",
     418             :                                     path)));
     419             :                 else
     420           0 :                     ereport(DEBUG1,
     421             :                             (errcode_for_file_access(),
     422             :                              errmsg_internal("could not fsync file \"%s\" but retrying: %m",
     423             :                                              path)));
     424             : 
     425             :                 /*
     426             :                  * Absorb incoming requests and check to see if a cancel
     427             :                  * arrived for this relation fork.
     428             :                  */
     429           0 :                 AbsorbSyncRequests();
     430           0 :                 absorb_counter = FSYNCS_PER_ABSORB; /* might as well... */
     431             :             }                   /* end retry loop */
     432             :         }
     433             : 
     434             :         /* We are done with this entry, remove it */
     435      152310 :         if (hash_search(pendingOps, &entry->tag, HASH_REMOVE, NULL) == NULL)
     436           0 :             elog(ERROR, "pendingOps corrupted");
     437             :     }                           /* end loop over hashtable entries */
     438             : 
     439             :     /* Return sync performance metrics for report at checkpoint end */
     440        3944 :     CheckpointStats.ckpt_sync_rels = processed;
     441        3944 :     CheckpointStats.ckpt_longest_sync = longest;
     442        3944 :     CheckpointStats.ckpt_agg_sync_time = total_elapsed;
     443             : 
     444             :     /* Flag successful completion of ProcessSyncRequests */
     445        3944 :     sync_in_progress = false;
     446        3944 : }
     447             : 
     448             : /*
     449             :  * RememberSyncRequest() -- callback from checkpointer side of sync request
     450             :  *
     451             :  * We stuff fsync requests into the local hash table for execution
     452             :  * during the checkpointer's next checkpoint.  UNLINK requests go into a
     453             :  * separate linked list, however, because they get processed separately.
     454             :  *
     455             :  * See sync.h for more information on the types of sync requests supported.
     456             :  */
     457             : void
     458     1205756 : RememberSyncRequest(const FileTag *ftag, SyncRequestType type)
     459             : {
     460             :     Assert(pendingOps);
     461             : 
     462     1205756 :     if (type == SYNC_FORGET_REQUEST)
     463             :     {
     464             :         PendingFsyncEntry *entry;
     465             : 
     466             :         /* Cancel previously entered request */
     467      109022 :         entry = (PendingFsyncEntry *) hash_search(pendingOps,
     468             :                                                   (void *) ftag,
     469             :                                                   HASH_FIND,
     470             :                                                   NULL);
     471      109022 :         if (entry != NULL)
     472        1356 :             entry->canceled = true;
     473             :     }
     474     1096734 :     else if (type == SYNC_FILTER_REQUEST)
     475             :     {
     476             :         HASH_SEQ_STATUS hstat;
     477             :         PendingFsyncEntry *entry;
     478             :         ListCell   *cell;
     479             : 
     480             :         /* Cancel matching fsync requests */
     481           6 :         hash_seq_init(&hstat, pendingOps);
     482          16 :         while ((entry = (PendingFsyncEntry *) hash_seq_search(&hstat)) != NULL)
     483             :         {
     484          16 :             if (entry->tag.handler == ftag->handler &&
     485           6 :                 syncsw[ftag->handler].sync_filetagmatches(ftag, &entry->tag))
     486           2 :                 entry->canceled = true;
     487             :         }
     488             : 
     489             :         /* Remove matching unlink requests */
     490           6 :         foreach(cell, pendingUnlinks)
     491             :         {
     492           0 :             PendingUnlinkEntry *entry = (PendingUnlinkEntry *) lfirst(cell);
     493             : 
     494           0 :             if (entry->tag.handler == ftag->handler &&
     495           0 :                 syncsw[ftag->handler].sync_filetagmatches(ftag, &entry->tag))
     496             :             {
     497           0 :                 pendingUnlinks = foreach_delete_current(pendingUnlinks, cell);
     498           0 :                 pfree(entry);
     499             :             }
     500             :         }
     501             :     }
     502     1096728 :     else if (type == SYNC_UNLINK_REQUEST)
     503             :     {
     504             :         /* Unlink request: put it in the linked list */
     505       36264 :         MemoryContext oldcxt = MemoryContextSwitchTo(pendingOpsCxt);
     506             :         PendingUnlinkEntry *entry;
     507             : 
     508       36264 :         entry = palloc(sizeof(PendingUnlinkEntry));
     509       36264 :         entry->tag = *ftag;
     510       36264 :         entry->cycle_ctr = checkpoint_cycle_ctr;
     511             : 
     512       36264 :         pendingUnlinks = lappend(pendingUnlinks, entry);
     513             : 
     514       36264 :         MemoryContextSwitchTo(oldcxt);
     515             :     }
     516             :     else
     517             :     {
     518             :         /* Normal case: enter a request to fsync this segment */
     519     1060464 :         MemoryContext oldcxt = MemoryContextSwitchTo(pendingOpsCxt);
     520             :         PendingFsyncEntry *entry;
     521             :         bool        found;
     522             : 
     523             :         Assert(type == SYNC_REQUEST);
     524             : 
     525     1060464 :         entry = (PendingFsyncEntry *) hash_search(pendingOps,
     526             :                                                   (void *) ftag,
     527             :                                                   HASH_ENTER,
     528             :                                                   &found);
     529             :         /* if new entry, or was previously canceled, initialize it */
     530     1060464 :         if (!found || entry->canceled)
     531             :         {
     532      152316 :             entry->cycle_ctr = sync_cycle_ctr;
     533      152316 :             entry->canceled = false;
     534             :         }
     535             : 
     536             :         /*
     537             :          * NB: it's intentional that we don't change cycle_ctr if the entry
     538             :          * already exists.  The cycle_ctr must represent the oldest fsync
     539             :          * request that could be in the entry.
     540             :          */
     541             : 
     542     1060464 :         MemoryContextSwitchTo(oldcxt);
     543             :     }
     544     1205756 : }
     545             : 
     546             : /*
     547             :  * Register the sync request locally, or forward it to the checkpointer.
     548             :  *
     549             :  * If retryOnError is true, we'll keep trying if there is no space in the
     550             :  * queue.  Return true if we succeeded, or false if there wasn't space.
     551             :  */
     552             : bool
     553     1241406 : RegisterSyncRequest(const FileTag *ftag, SyncRequestType type,
     554             :                     bool retryOnError)
     555             : {
     556             :     bool        ret;
     557             : 
     558     1241406 :     if (pendingOps != NULL)
     559             :     {
     560             :         /* standalone backend or startup process: fsync state is local */
     561      852452 :         RememberSyncRequest(ftag, type);
     562      852452 :         return true;
     563             :     }
     564             : 
     565             :     for (;;)
     566             :     {
     567             :         /*
     568             :          * Notify the checkpointer about it.  If we fail to queue a message in
     569             :          * retryOnError mode, we have to sleep and try again ... ugly, but
     570             :          * hopefully won't happen often.
     571             :          *
     572             :          * XXX should we CHECK_FOR_INTERRUPTS in this loop?  Escaping with an
     573             :          * error in the case of SYNC_UNLINK_REQUEST would leave the
     574             :          * no-longer-used file still present on disk, which would be bad, so
     575             :          * I'm inclined to assume that the checkpointer will always empty the
     576             :          * queue soon.
     577             :          */
     578      388954 :         ret = ForwardSyncRequest(ftag, type);
     579             : 
     580             :         /*
     581             :          * If we are successful in queueing the request, or we failed and were
     582             :          * instructed not to retry on error, break.
     583             :          */
     584      388954 :         if (ret || (!ret && !retryOnError))
     585             :             break;
     586             : 
     587           0 :         pg_usleep(10000L);
     588             :     }
     589             : 
     590      388954 :     return ret;
     591             : }

Generated by: LCOV version 1.13