LCOV - differential code coverage report
Current view: top level - src/backend/access/transam - slru.c (source / functions) Coverage Total Hit UNC LBC UBC GBC GNC CBC EUB ECB DUB DCB
Current: Differential Code Coverage 16@8cea358b128 vs 17@8cea358b128 Lines: 77.7 % 591 459 13 11 108 6 137 316 4 67
Current Date: 2024-04-14 14:21:10 Functions: 93.5 % 31 29 1 1 25 4 1 14
Baseline: 16@8cea358b128 Branches: 53.9 % 423 228 25 4 166 5 71 152 2 6
Baseline Date: 2024-04-14 14:21:09 Line coverage date bins:
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed [..60] days: 89.2 % 102 91 11 91
(60,120] days: 100.0 % 6 6 6
(120,180] days: 95.2 % 42 40 2 40
(240..) days: 73.0 % 441 322 11 108 6 316
Function coverage date bins:
[..60] days: 100.0 % 3 3 3
(120,180] days: 94.1 % 17 16 1 16
(240..) days: 90.9 % 11 10 1 6 4
Branch coverage date bins:
[..60] days: 74.3 % 70 52 18 52
(60,120] days: 83.3 % 6 5 1 5
(120,180] days: 70.0 % 20 14 6 14
(240..) days: 46.9 % 335 157 4 166 5 152 2 6

 Age         Owner                    Branch data    TLA  Line data    Source code
                                  1                 :                : /*-------------------------------------------------------------------------
                                  2                 :                :  *
                                  3                 :                :  * slru.c
                                  4                 :                :  *      Simple LRU buffering for wrap-around-able permanent metadata
                                  5                 :                :  *
                                  6                 :                :  * This module is used to maintain various pieces of transaction status
                                  7                 :                :  * indexed by TransactionId (such as commit status, parent transaction ID,
                                  8                 :                :  * commit timestamp), as well as storage for multixacts, serializable
                                  9                 :                :  * isolation locks and NOTIFY traffic.  Extensions can define their own
                                 10                 :                :  * SLRUs, too.
                                 11                 :                :  *
                                 12                 :                :  * Under ordinary circumstances we expect that write traffic will occur
                                 13                 :                :  * mostly to the latest page (and to the just-prior page, soon after a
                                 14                 :                :  * page transition).  Read traffic will probably touch a larger span of
                                 15                 :                :  * pages, but a relatively small number of buffers should be sufficient.
                                 16                 :                :  *
                                 17                 :                :  * We use a simple least-recently-used scheme to manage a pool of shared
                                 18                 :                :  * page buffers, split in banks by the lowest bits of the page number, and
                                 19                 :                :  * the management algorithm only processes the bank to which the desired
                                 20                 :                :  * page belongs, so a linear search is sufficient; there's no need for a
                                 21                 :                :  * hashtable or anything fancy.  The algorithm is straight LRU except that
                                 22                 :                :  * we will never swap out the latest page (since we know it's going to be
                                 23                 :                :  * hit again eventually).
                                 24                 :                :  *
                                 25                 :                :  * We use per-bank control LWLocks to protect the shared data structures,
                                 26                 :                :  * plus per-buffer LWLocks that synchronize I/O for each buffer.  The
                                 27                 :                :  * bank's control lock must be held to examine or modify any of the bank's
                                 28                 :                :  * shared state.  A process that is reading in or writing out a page
                                 29                 :                :  * buffer does not hold the control lock, only the per-buffer lock for the
                                 30                 :                :  * buffer it is working on.  One exception is latest_page_number, which is
                                 31                 :                :  * read and written using atomic ops.
                                 32                 :                :  *
                                 33                 :                :  * "Holding the bank control lock" means exclusive lock in all cases
                                 34                 :                :  * except for SimpleLruReadPage_ReadOnly(); see comments for
                                 35                 :                :  * SlruRecentlyUsed() for the implications of that.
                                 36                 :                :  *
                                 37                 :                :  * When initiating I/O on a buffer, we acquire the per-buffer lock exclusively
                                 38                 :                :  * before releasing the control lock.  The per-buffer lock is released after
                                 39                 :                :  * completing the I/O, re-acquiring the control lock, and updating the shared
                                 40                 :                :  * state.  (Deadlock is not possible here, because we never try to initiate
                                 41                 :                :  * I/O when someone else is already doing I/O on the same buffer.)
                                 42                 :                :  * To wait for I/O to complete, release the control lock, acquire the
                                 43                 :                :  * per-buffer lock in shared mode, immediately release the per-buffer lock,
                                 44                 :                :  * reacquire the control lock, and then recheck state (since arbitrary things
                                 45                 :                :  * could have happened while we didn't have the lock).
                                 46                 :                :  *
                                 47                 :                :  * As with the regular buffer manager, it is possible for another process
                                 48                 :                :  * to re-dirty a page that is currently being written out.  This is handled
                                 49                 :                :  * by re-setting the page's page_dirty flag.
                                 50                 :                :  *
                                 51                 :                :  *
                                 52                 :                :  * Portions Copyright (c) 1996-2024, PostgreSQL Global Development Group
                                 53                 :                :  * Portions Copyright (c) 1994, Regents of the University of California
                                 54                 :                :  *
                                 55                 :                :  * src/backend/access/transam/slru.c
                                 56                 :                :  *
                                 57                 :                :  *-------------------------------------------------------------------------
                                 58                 :                :  */
                                 59                 :                : #include "postgres.h"
                                 60                 :                : 
                                 61                 :                : #include <fcntl.h>
                                 62                 :                : #include <sys/stat.h>
                                 63                 :                : #include <unistd.h>
                                 64                 :                : 
                                 65                 :                : #include "access/slru.h"
                                 66                 :                : #include "access/transam.h"
                                 67                 :                : #include "access/xlog.h"
                                 68                 :                : #include "access/xlogutils.h"
                                 69                 :                : #include "miscadmin.h"
                                 70                 :                : #include "pgstat.h"
                                 71                 :                : #include "storage/fd.h"
                                 72                 :                : #include "storage/shmem.h"
                                 73                 :                : #include "utils/guc_hooks.h"
                                 74                 :                : 
                                 75                 :                : static inline int
  137 akorotkov@postgresql       76                 :GNC     7451716 : SlruFileName(SlruCtl ctl, char *path, int64 segno)
                                 77                 :                : {
                                 78         [ +  + ]:        7451716 :     if (ctl->long_segment_names)
                                 79                 :                :     {
                                 80                 :                :         /*
                                 81                 :                :          * We could use 16 characters here but the disadvantage would be that
                                 82                 :                :          * the SLRU segments will be hard to distinguish from WAL segments.
                                 83                 :                :          *
                                 84                 :                :          * For this reason we use 15 characters. It is enough but also means
                                 85                 :                :          * that in the future we can't decrease SLRU_PAGES_PER_SEGMENT easily.
                                 86                 :                :          */
                                 87   [ +  -  -  + ]:            147 :         Assert(segno >= 0 && segno <= INT64CONST(0xFFFFFFFFFFFFFFF));
                                 88                 :            147 :         return snprintf(path, MAXPGPATH, "%s/%015llX", ctl->Dir,
                                 89                 :                :                         (long long) segno);
                                 90                 :                :     }
                                 91                 :                :     else
                                 92                 :                :     {
                                 93                 :                :         /*
                                 94                 :                :          * Despite the fact that %04X format string is used up to 24 bit
                                 95                 :                :          * integers are allowed. See SlruCorrectSegmentFilenameLength()
                                 96                 :                :          */
                                 97   [ +  -  -  + ]:        7451569 :         Assert(segno >= 0 && segno <= INT64CONST(0xFFFFFF));
                                 98                 :        7451569 :         return snprintf(path, MAXPGPATH, "%s/%04X", (ctl)->Dir,
                                 99                 :                :                         (unsigned int) segno);
                                100                 :                :     }
                                101                 :                : }
                                102                 :                : 
                                103                 :                : /*
                                104                 :                :  * During SimpleLruWriteAll(), we will usually not need to write more than one
                                105                 :                :  * or two physical files, but we may need to write several pages per file.  We
                                106                 :                :  * can consolidate the I/O requests by leaving files open until control returns
                                107                 :                :  * to SimpleLruWriteAll().  This data structure remembers which files are open.
                                108                 :                :  */
                                109                 :                : #define MAX_WRITEALL_BUFFERS    16
                                110                 :                : 
                                111                 :                : typedef struct SlruWriteAllData
                                112                 :                : {
                                113                 :                :     int         num_files;      /* # files actually open */
                                114                 :                :     int         fd[MAX_WRITEALL_BUFFERS];   /* their FD's */
                                115                 :                :     int64       segno[MAX_WRITEALL_BUFFERS];    /* their log seg#s */
                                116                 :                : } SlruWriteAllData;
                                117                 :                : 
                                118                 :                : typedef struct SlruWriteAllData *SlruWriteAll;
                                119                 :                : 
                                120                 :                : 
                                121                 :                : /*
                                122                 :                :  * Bank size for the slot array.  Pages are assigned a bank according to their
                                123                 :                :  * page number, with each bank being this size.  We want a power of 2 so that
                                124                 :                :  * we can determine the bank number for a page with just bit shifting; we also
                                125                 :                :  * want to keep the bank size small so that LRU victim search is fast.  16
                                126                 :                :  * buffers per bank seems a good number.
                                127                 :                :  */
                                128                 :                : #define SLRU_BANK_BITSHIFT      4
                                129                 :                : #define SLRU_BANK_SIZE          (1 << SLRU_BANK_BITSHIFT)
                                130                 :                : 
                                131                 :                : /*
                                132                 :                :  * Macro to get the bank number to which the slot belongs.
                                133                 :                :  */
                                134                 :                : #define SlotGetBankNumber(slotno)   ((slotno) >> SLRU_BANK_BITSHIFT)
                                135                 :                : 
                                136                 :                : 
                                137                 :                : /*
                                138                 :                :  * Populate a file tag describing a segment file.  We only use the segment
                                139                 :                :  * number, since we can derive everything else we need by having separate
                                140                 :                :  * sync handler functions for clog, multixact etc.
                                141                 :                :  */
                                142                 :                : #define INIT_SLRUFILETAG(a,xx_handler,xx_segno) \
                                143                 :                : ( \
                                144                 :                :     memset(&(a), 0, sizeof(FileTag)), \
                                145                 :                :     (a).handler = (xx_handler), \
                                146                 :                :     (a).segno = (xx_segno) \
                                147                 :                : )
                                148                 :                : 
                                149                 :                : /* Saved info for SlruReportIOError */
                                150                 :                : typedef enum
                                151                 :                : {
                                152                 :                :     SLRU_OPEN_FAILED,
                                153                 :                :     SLRU_SEEK_FAILED,
                                154                 :                :     SLRU_READ_FAILED,
                                155                 :                :     SLRU_WRITE_FAILED,
                                156                 :                :     SLRU_FSYNC_FAILED,
                                157                 :                :     SLRU_CLOSE_FAILED,
                                158                 :                : } SlruErrorCause;
                                159                 :                : 
                                160                 :                : static SlruErrorCause slru_errcause;
                                161                 :                : static int  slru_errno;
                                162                 :                : 
                                163                 :                : 
                                164                 :                : static void SimpleLruZeroLSNs(SlruCtl ctl, int slotno);
                                165                 :                : static void SimpleLruWaitIO(SlruCtl ctl, int slotno);
                                166                 :                : static void SlruInternalWritePage(SlruCtl ctl, int slotno, SlruWriteAll fdata);
                                167                 :                : static bool SlruPhysicalReadPage(SlruCtl ctl, int64 pageno, int slotno);
                                168                 :                : static bool SlruPhysicalWritePage(SlruCtl ctl, int64 pageno, int slotno,
                                169                 :                :                                   SlruWriteAll fdata);
                                170                 :                : static void SlruReportIOError(SlruCtl ctl, int64 pageno, TransactionId xid);
                                171                 :                : static int  SlruSelectLRUPage(SlruCtl ctl, int64 pageno);
                                172                 :                : 
                                173                 :                : static bool SlruScanDirCbDeleteCutoff(SlruCtl ctl, char *filename,
                                174                 :                :                                       int64 segpage, void *data);
                                175                 :                : static void SlruInternalDeleteSegment(SlruCtl ctl, int64 segno);
                                176                 :                : static inline void SlruRecentlyUsed(SlruShared shared, int slotno);
                                177                 :                : 
                                178                 :                : 
                                179                 :                : /*
                                180                 :                :  * Initialization of shared memory
                                181                 :                :  */
                                182                 :                : 
                                183                 :                : Size
 6101 tgl@sss.pgh.pa.us         184                 :CBC       24328 : SimpleLruShmemSize(int nslots, int nlsns)
                                185                 :                : {
   46 alvherre@alvh.no-ip.      186                 :GNC       24328 :     int         nbanks = nslots / SLRU_BANK_SIZE;
                                187                 :                :     Size        sz;
                                188                 :                : 
                                189         [ -  + ]:          24328 :     Assert(nslots <= SLRU_MAX_ALLOWED_BUFFERS);
                                190         [ -  + ]:          24328 :     Assert(nslots % SLRU_BANK_SIZE == 0);
                                191                 :                : 
                                192                 :                :     /* we assume nslots isn't so large as to risk overflow */
 6704 tgl@sss.pgh.pa.us         193                 :CBC       24328 :     sz = MAXALIGN(sizeof(SlruSharedData));
 6402 bruce@momjian.us          194                 :          24328 :     sz += MAXALIGN(nslots * sizeof(char *));    /* page_buffer[] */
 6704 tgl@sss.pgh.pa.us         195                 :          24328 :     sz += MAXALIGN(nslots * sizeof(SlruPageStatus));    /* page_status[] */
 2489                           196                 :          24328 :     sz += MAXALIGN(nslots * sizeof(bool));  /* page_dirty[] */
  137 akorotkov@postgresql      197                 :GNC       24328 :     sz += MAXALIGN(nslots * sizeof(int64)); /* page_number[] */
 2489 tgl@sss.pgh.pa.us         198                 :CBC       24328 :     sz += MAXALIGN(nslots * sizeof(int));   /* page_lru_count[] */
                                199                 :          24328 :     sz += MAXALIGN(nslots * sizeof(LWLockPadded));  /* buffer_locks[] */
   46 alvherre@alvh.no-ip.      200                 :GNC       24328 :     sz += MAXALIGN(nbanks * sizeof(LWLockPadded));  /* bank_locks[] */
                                201                 :          24328 :     sz += MAXALIGN(nbanks * sizeof(int));   /* bank_cur_lru_count[] */
                                202                 :                : 
 6101 tgl@sss.pgh.pa.us         203         [ +  + ]:CBC       24328 :     if (nlsns > 0)
 5995 bruce@momjian.us          204                 :           3475 :         sz += MAXALIGN(nslots * nlsns * sizeof(XLogRecPtr));    /* group_lsn[] */
                                205                 :                : 
 6704 tgl@sss.pgh.pa.us         206                 :          24328 :     return BUFFERALIGN(sz) + BLCKSZ * nslots;
                                207                 :                : }
                                208                 :                : 
                                209                 :                : /*
                                210                 :                :  * Determine a number of SLRU buffers to use.
                                211                 :                :  *
                                212                 :                :  * We simply divide shared_buffers by the divisor given and cap
                                213                 :                :  * that at the maximum given; but always at least SLRU_BANK_SIZE.
                                214                 :                :  * Round down to the nearest multiple of SLRU_BANK_SIZE.
                                215                 :                :  */
                                216                 :                : int
   46 alvherre@alvh.no-ip.      217                 :GNC        7683 : SimpleLruAutotuneBuffers(int divisor, int max)
                                218                 :                : {
                                219                 :           7683 :     return Min(max - (max % SLRU_BANK_SIZE),
                                220                 :                :                Max(SLRU_BANK_SIZE,
                                221                 :                :                    NBuffers / divisor - (NBuffers / divisor) % SLRU_BANK_SIZE));
                                222                 :                : }
                                223                 :                : 
                                224                 :                : /*
                                225                 :                :  * Initialize, or attach to, a simple LRU cache in shared memory.
                                226                 :                :  *
                                227                 :                :  * ctl: address of local (unshared) control structure.
                                228                 :                :  * name: name of SLRU.  (This is user-visible, pick with care!)
                                229                 :                :  * nslots: number of page slots to use.
                                230                 :                :  * nlsns: number of LSN groups per page (set to zero if not relevant).
                                231                 :                :  * ctllock: LWLock to use to control access to the shared control structure.
                                232                 :                :  * subdir: PGDATA-relative subdirectory that will contain the files.
                                233                 :                :  * buffer_tranche_id: tranche ID to use for the SLRU's per-buffer LWLocks.
                                234                 :                :  * bank_tranche_id: tranche ID to use for the bank LWLocks.
                                235                 :                :  * sync_handler: which set of functions to use to handle sync requests
                                236                 :                :  */
                                237                 :                : void
 6101 tgl@sss.pgh.pa.us         238                 :CBC        6287 : SimpleLruInit(SlruCtl ctl, const char *name, int nslots, int nlsns,
                                239                 :                :               const char *subdir, int buffer_tranche_id, int bank_tranche_id,
                                240                 :                :               SyncRequestHandler sync_handler, bool long_segment_names)
                                241                 :                : {
                                242                 :                :     SlruShared  shared;
                                243                 :                :     bool        found;
   46 alvherre@alvh.no-ip.      244                 :GNC        6287 :     int         nbanks = nslots / SLRU_BANK_SIZE;
                                245                 :                : 
                                246         [ -  + ]:           6287 :     Assert(nslots <= SLRU_MAX_ALLOWED_BUFFERS);
                                247                 :                : 
 6704 tgl@sss.pgh.pa.us         248                 :CBC        6287 :     shared = (SlruShared) ShmemInitStruct(name,
                                249                 :                :                                           SimpleLruShmemSize(nslots, nlsns),
                                250                 :                :                                           &found);
                                251                 :                : 
 7613 bruce@momjian.us          252         [ +  - ]:           6287 :     if (!IsUnderPostmaster)
                                253                 :                :     {
                                254                 :                :         /* Initialize locks and shared memory area */
                                255                 :                :         char       *ptr;
                                256                 :                :         Size        offset;
                                257                 :                : 
                                258         [ -  + ]:           6287 :         Assert(!found);
                                259                 :                : 
                                260                 :           6287 :         memset(shared, 0, sizeof(SlruSharedData));
                                261                 :                : 
 6704 tgl@sss.pgh.pa.us         262                 :           6287 :         shared->num_slots = nslots;
 6101                           263                 :           6287 :         shared->lsn_groups_per_page = nlsns;
                                264                 :                : 
   68 alvherre@alvh.no-ip.      265                 :GNC        6287 :         pg_atomic_init_u64(&shared->latest_page_number, 0);
                                266                 :                : 
  739 andres@anarazel.de        267                 :CBC        6287 :         shared->slru_stats_idx = pgstat_get_slru_index(name);
                                268                 :                : 
 6704 tgl@sss.pgh.pa.us         269                 :           6287 :         ptr = (char *) shared;
                                270                 :           6287 :         offset = MAXALIGN(sizeof(SlruSharedData));
                                271                 :           6287 :         shared->page_buffer = (char **) (ptr + offset);
                                272                 :           6287 :         offset += MAXALIGN(nslots * sizeof(char *));
                                273                 :           6287 :         shared->page_status = (SlruPageStatus *) (ptr + offset);
                                274                 :           6287 :         offset += MAXALIGN(nslots * sizeof(SlruPageStatus));
                                275                 :           6287 :         shared->page_dirty = (bool *) (ptr + offset);
                                276                 :           6287 :         offset += MAXALIGN(nslots * sizeof(bool));
  137 akorotkov@postgresql      277                 :GNC        6287 :         shared->page_number = (int64 *) (ptr + offset);
                                278                 :           6287 :         offset += MAXALIGN(nslots * sizeof(int64));
 6704 tgl@sss.pgh.pa.us         279                 :CBC        6287 :         shared->page_lru_count = (int *) (ptr + offset);
                                280                 :           6287 :         offset += MAXALIGN(nslots * sizeof(int));
                                281                 :                : 
                                282                 :                :         /* Initialize LWLocks */
 2459 teodor@sigaev.ru          283                 :           6287 :         shared->buffer_locks = (LWLockPadded *) (ptr + offset);
                                284                 :           6287 :         offset += MAXALIGN(nslots * sizeof(LWLockPadded));
   46 alvherre@alvh.no-ip.      285                 :GNC        6287 :         shared->bank_locks = (LWLockPadded *) (ptr + offset);
                                286                 :           6287 :         offset += MAXALIGN(nbanks * sizeof(LWLockPadded));
                                287                 :           6287 :         shared->bank_cur_lru_count = (int *) (ptr + offset);
                                288                 :           6287 :         offset += MAXALIGN(nbanks * sizeof(int));
                                289                 :                : 
 6101 tgl@sss.pgh.pa.us         290         [ +  + ]:CBC        6287 :         if (nlsns > 0)
                                291                 :                :         {
                                292                 :            898 :             shared->group_lsn = (XLogRecPtr *) (ptr + offset);
                                293                 :            898 :             offset += MAXALIGN(nslots * nlsns * sizeof(XLogRecPtr));
                                294                 :                :         }
                                295                 :                : 
                                296                 :           6287 :         ptr += BUFFERALIGN(offset);
   48 alvherre@alvh.no-ip.      297         [ +  + ]:GNC      159711 :         for (int slotno = 0; slotno < nslots; slotno++)
                                298                 :                :         {
 3076 rhaas@postgresql.org      299                 :CBC      153424 :             LWLockInitialize(&shared->buffer_locks[slotno].lock,
                                300                 :                :                              buffer_tranche_id);
                                301                 :                : 
 6704 tgl@sss.pgh.pa.us         302                 :         153424 :             shared->page_buffer[slotno] = ptr;
 7613 bruce@momjian.us          303                 :         153424 :             shared->page_status[slotno] = SLRU_PAGE_EMPTY;
 6735 tgl@sss.pgh.pa.us         304                 :         153424 :             shared->page_dirty[slotno] = false;
 6704                           305                 :         153424 :             shared->page_lru_count[slotno] = 0;
                                306                 :         153424 :             ptr += BLCKSZ;
                                307                 :                :         }
                                308                 :                : 
                                309                 :                :         /* Initialize the slot banks. */
   46 alvherre@alvh.no-ip.      310         [ +  + ]:GNC       15876 :         for (int bankno = 0; bankno < nbanks; bankno++)
                                311                 :                :         {
                                312                 :           9589 :             LWLockInitialize(&shared->bank_locks[bankno].lock, bank_tranche_id);
                                313                 :           9589 :             shared->bank_cur_lru_count[bankno] = 0;
                                314                 :                :         }
                                315                 :                : 
                                316                 :                :         /* Should fit to estimated shmem size */
 2435 tgl@sss.pgh.pa.us         317         [ -  + ]:CBC        6287 :         Assert(ptr - (char *) shared <= SimpleLruShmemSize(nslots, nlsns));
                                318                 :                :     }
                                319                 :                :     else
                                320                 :                :     {
 7613 bruce@momjian.us          321         [ #  # ]:UBC           0 :         Assert(found);
   46 alvherre@alvh.no-ip.      322         [ #  # ]:UNC           0 :         Assert(shared->num_slots == nslots);
                                323                 :                :     }
                                324                 :                : 
                                325                 :                :     /*
                                326                 :                :      * Initialize the unshared control struct, including directory path. We
                                327                 :                :      * assume caller set PagePrecedes.
                                328                 :                :      */
 7613 bruce@momjian.us          329                 :CBC        6287 :     ctl->shared = shared;
 1297 tmunro@postgresql.or      330                 :           6287 :     ctl->sync_handler = sync_handler;
  137 akorotkov@postgresql      331                 :GNC        6287 :     ctl->long_segment_names = long_segment_names;
   46 alvherre@alvh.no-ip.      332                 :           6287 :     ctl->bank_mask = (nslots / SLRU_BANK_SIZE) - 1;
 1343 peter@eisentraut.org      333                 :CBC        6287 :     strlcpy(ctl->Dir, subdir, sizeof(ctl->Dir));
 7613 bruce@momjian.us          334                 :           6287 : }
                                335                 :                : 
                                336                 :                : /*
                                337                 :                :  * Helper function for GUC check_hook to check whether slru buffers are in
                                338                 :                :  * multiples of SLRU_BANK_SIZE.
                                339                 :                :  */
                                340                 :                : bool
   46 alvherre@alvh.no-ip.      341                 :GNC        9186 : check_slru_buffers(const char *name, int *newval)
                                342                 :                : {
                                343                 :                :     /* Valid values are multiples of SLRU_BANK_SIZE */
                                344         [ +  - ]:           9186 :     if (*newval % SLRU_BANK_SIZE == 0)
                                345                 :           9186 :         return true;
                                346                 :                : 
   46 alvherre@alvh.no-ip.      347                 :UNC           0 :     GUC_check_errdetail("\"%s\" must be a multiple of %d", name,
                                348                 :                :                         SLRU_BANK_SIZE);
                                349                 :              0 :     return false;
                                350                 :                : }
                                351                 :                : 
                                352                 :                : /*
                                353                 :                :  * Initialize (or reinitialize) a page to zeroes.
                                354                 :                :  *
                                355                 :                :  * The page is not actually written, just set up in shared memory.
                                356                 :                :  * The slot number of the new page is returned.
                                357                 :                :  *
                                358                 :                :  * Bank lock must be held at entry, and will be held at exit.
                                359                 :                :  */
                                360                 :                : int
  137 akorotkov@postgresql      361                 :GNC     7339405 : SimpleLruZeroPage(SlruCtl ctl, int64 pageno)
                                362                 :                : {
 7258 tgl@sss.pgh.pa.us         363                 :CBC     7339405 :     SlruShared  shared = ctl->shared;
                                364                 :                :     int         slotno;
                                365                 :                : 
   46 alvherre@alvh.no-ip.      366         [ -  + ]:GNC     7339405 :     Assert(LWLockHeldByMeInMode(SimpleLruGetBankLock(ctl, pageno), LW_EXCLUSIVE));
                                367                 :                : 
                                368                 :                :     /* Find a suitable buffer slot for the page */
 7613 bruce@momjian.us          369                 :CBC     7339405 :     slotno = SlruSelectLRUPage(ctl, pageno);
                                370   [ +  +  +  -  :        7339405 :     Assert(shared->page_status[slotno] == SLRU_PAGE_EMPTY ||
                                        +  +  -  + ]
                                371                 :                :            (shared->page_status[slotno] == SLRU_PAGE_VALID &&
                                372                 :                :             !shared->page_dirty[slotno]) ||
                                373                 :                :            shared->page_number[slotno] == pageno);
                                374                 :                : 
                                375                 :                :     /* Mark the slot as containing this page */
                                376                 :        7339405 :     shared->page_number[slotno] = pageno;
 6735 tgl@sss.pgh.pa.us         377                 :        7339405 :     shared->page_status[slotno] = SLRU_PAGE_VALID;
                                378                 :        7339405 :     shared->page_dirty[slotno] = true;
 7613 bruce@momjian.us          379         [ +  + ]:        7339405 :     SlruRecentlyUsed(shared, slotno);
                                380                 :                : 
                                381                 :                :     /* Set the buffer to zeroes */
                                382   [ +  -  +  -  :        7339405 :     MemSet(shared->page_buffer[slotno], 0, BLCKSZ);
                                     +  -  -  +  -  
                                                 - ]
                                383                 :                : 
                                384                 :                :     /* Set the LSNs for this new page to zero */
 6101 tgl@sss.pgh.pa.us         385                 :        7339405 :     SimpleLruZeroLSNs(ctl, slotno);
                                386                 :                : 
                                387                 :                :     /*
                                388                 :                :      * Assume this page is now the latest active page.
                                389                 :                :      *
                                390                 :                :      * Note that because both this routine and SlruSelectLRUPage run with
                                391                 :                :      * ControlLock held, it is not possible for this to be zeroing a page that
                                392                 :                :      * SlruSelectLRUPage is going to evict simultaneously.  Therefore, there's
                                393                 :                :      * no memory barrier here.
                                394                 :                :      */
   68 alvherre@alvh.no-ip.      395                 :GNC     7339405 :     pg_atomic_write_u64(&shared->latest_page_number, pageno);
                                396                 :                : 
                                397                 :                :     /* update the stats counter of zeroed pages */
 1432 tgl@sss.pgh.pa.us         398                 :CBC     7339405 :     pgstat_count_slru_page_zeroed(shared->slru_stats_idx);
                                399                 :                : 
 7613 bruce@momjian.us          400                 :        7339405 :     return slotno;
                                401                 :                : }
                                402                 :                : 
                                403                 :                : /*
                                404                 :                :  * Zero all the LSNs we store for this slru page.
                                405                 :                :  *
                                406                 :                :  * This should be called each time we create a new page, and each time we read
                                407                 :                :  * in a page from disk into an existing buffer.  (Such an old page cannot
                                408                 :                :  * have any interesting LSNs, since we'd have flushed them before writing
                                409                 :                :  * the page in the first place.)
                                410                 :                :  *
                                411                 :                :  * This assumes that InvalidXLogRecPtr is bitwise-all-0.
                                412                 :                :  */
                                413                 :                : static void
 6101 tgl@sss.pgh.pa.us         414                 :        7341095 : SimpleLruZeroLSNs(SlruCtl ctl, int slotno)
                                415                 :                : {
                                416                 :        7341095 :     SlruShared  shared = ctl->shared;
                                417                 :                : 
                                418         [ +  + ]:        7341095 :     if (shared->lsn_groups_per_page > 0)
                                419   [ +  -  +  -  :         432711 :         MemSet(&shared->group_lsn[slotno * shared->lsn_groups_per_page], 0,
                                     +  -  -  +  -  
                                                 - ]
                                420                 :                :                shared->lsn_groups_per_page * sizeof(XLogRecPtr));
                                421                 :        7341095 : }
                                422                 :                : 
                                423                 :                : /*
                                424                 :                :  * Wait for any active I/O on a page slot to finish.  (This does not
                                425                 :                :  * guarantee that new I/O hasn't been started before we return, though.
                                426                 :                :  * In fact the slot might not even contain the same page anymore.)
                                427                 :                :  *
                                428                 :                :  * Bank lock must be held at entry, and will be held at exit.
                                429                 :                :  */
                                430                 :                : static void
 6735 tgl@sss.pgh.pa.us         431                 :LBC         (1) : SimpleLruWaitIO(SlruCtl ctl, int slotno)
                                432                 :                : {
                                433                 :            (1) :     SlruShared  shared = ctl->shared;
   46 alvherre@alvh.no-ip.      434                 :UNC           0 :     int         bankno = SlotGetBankNumber(slotno);
                                435                 :                : 
   40                           436         [ #  # ]:              0 :     Assert(shared->page_status[slotno] != SLRU_PAGE_EMPTY);
                                437                 :                : 
                                438                 :                :     /* See notes at top of file */
   46                           439                 :              0 :     LWLockRelease(&shared->bank_locks[bankno].lock);
 3076 rhaas@postgresql.org      440                 :LBC         (1) :     LWLockAcquire(&shared->buffer_locks[slotno].lock, LW_SHARED);
                                441                 :            (1) :     LWLockRelease(&shared->buffer_locks[slotno].lock);
   46 alvherre@alvh.no-ip.      442                 :UNC           0 :     LWLockAcquire(&shared->bank_locks[bankno].lock, LW_EXCLUSIVE);
                                443                 :                : 
                                444                 :                :     /*
                                445                 :                :      * If the slot is still in an io-in-progress state, then either someone
                                446                 :                :      * already started a new I/O on the slot, or a previous I/O failed and
                                447                 :                :      * neglected to reset the page state.  That shouldn't happen, really, but
                                448                 :                :      * it seems worth a few extra cycles to check and recover from it. We can
                                449                 :                :      * cheaply test for failure by seeing if the buffer lock is still held (we
                                450                 :                :      * assume that transaction abort would release the lock).
                                451                 :                :      */
 6735 tgl@sss.pgh.pa.us         452         [ #  # ]:LBC         (1) :     if (shared->page_status[slotno] == SLRU_PAGE_READ_IN_PROGRESS ||
                                453         [ #  # ]:            (1) :         shared->page_status[slotno] == SLRU_PAGE_WRITE_IN_PROGRESS)
                                454                 :                :     {
 3076 rhaas@postgresql.org      455         [ #  # ]:UBC           0 :         if (LWLockConditionalAcquire(&shared->buffer_locks[slotno].lock, LW_SHARED))
                                456                 :                :         {
                                457                 :                :             /* indeed, the I/O must have failed */
 6735 tgl@sss.pgh.pa.us         458         [ #  # ]:              0 :             if (shared->page_status[slotno] == SLRU_PAGE_READ_IN_PROGRESS)
                                459                 :              0 :                 shared->page_status[slotno] = SLRU_PAGE_EMPTY;
                                460                 :                :             else                /* write_in_progress */
                                461                 :                :             {
                                462                 :              0 :                 shared->page_status[slotno] = SLRU_PAGE_VALID;
                                463                 :              0 :                 shared->page_dirty[slotno] = true;
                                464                 :                :             }
 3076 rhaas@postgresql.org      465                 :              0 :             LWLockRelease(&shared->buffer_locks[slotno].lock);
                                466                 :                :         }
                                467                 :                :     }
 6735 tgl@sss.pgh.pa.us         468                 :LBC         (1) : }
                                469                 :                : 
                                470                 :                : /*
                                471                 :                :  * Find a page in a shared buffer, reading it in if necessary.
                                472                 :                :  * The page number must correspond to an already-initialized page.
                                473                 :                :  *
                                474                 :                :  * If write_ok is true then it is OK to return a page that is in
                                475                 :                :  * WRITE_IN_PROGRESS state; it is the caller's responsibility to be sure
                                476                 :                :  * that modification of the page is safe.  If write_ok is false then we
                                477                 :                :  * will not return the page until it is not undergoing active I/O.
                                478                 :                :  *
                                479                 :                :  * The passed-in xid is used only for error reporting, and may be
                                480                 :                :  * InvalidTransactionId if no specific xid is associated with the action.
                                481                 :                :  *
                                482                 :                :  * Return value is the shared-buffer slot number now holding the page.
                                483                 :                :  * The buffer's LRU access info is updated.
                                484                 :                :  *
                                485                 :                :  * The correct bank lock must be held at entry, and will be held at exit.
                                486                 :                :  */
                                487                 :                : int
  137 akorotkov@postgresql      488                 :GNC      146561 : SimpleLruReadPage(SlruCtl ctl, int64 pageno, bool write_ok,
                                489                 :                :                   TransactionId xid)
                                490                 :                : {
 7258 tgl@sss.pgh.pa.us         491                 :CBC      146561 :     SlruShared  shared = ctl->shared;
   41 alvherre@alvh.no-ip.      492                 :GNC      146561 :     LWLock     *banklock = SimpleLruGetBankLock(ctl, pageno);
                                493                 :                : 
                                494         [ +  - ]:         146561 :     Assert(LWLockHeldByMeInMode(banklock, LW_EXCLUSIVE));
                                495                 :                : 
                                496                 :                :     /* Outer loop handles restart if we must wait for someone else's I/O */
                                497                 :                :     for (;;)
 7613 bruce@momjian.us          498                 :LBC         (1) :     {
                                499                 :                :         int         slotno;
                                500                 :                :         bool        ok;
                                501                 :                : 
                                502                 :                :         /* See if page already is in memory; if not, pick victim slot */
 7613 bruce@momjian.us          503                 :CBC      146561 :         slotno = SlruSelectLRUPage(ctl, pageno);
                                504                 :                : 
                                505                 :                :         /* Did we find the page in memory? */
   46 alvherre@alvh.no-ip.      506         [ +  + ]:GNC      146561 :         if (shared->page_status[slotno] != SLRU_PAGE_EMPTY &&
                                507         [ +  + ]:         145101 :             shared->page_number[slotno] == pageno)
                                508                 :                :         {
                                509                 :                :             /*
                                510                 :                :              * If page is still being read in, we must wait for I/O.  Likewise
                                511                 :                :              * if the page is being written and the caller said that's not OK.
                                512                 :                :              */
 6101 tgl@sss.pgh.pa.us         513         [ +  - ]:CBC      144871 :             if (shared->page_status[slotno] == SLRU_PAGE_READ_IN_PROGRESS ||
                                514         [ -  + ]:         144871 :                 (shared->page_status[slotno] == SLRU_PAGE_WRITE_IN_PROGRESS &&
 6101 tgl@sss.pgh.pa.us         515         [ #  # ]:LBC         (1) :                  !write_ok))
                                516                 :                :             {
 6735                           517                 :            (1) :                 SimpleLruWaitIO(ctl, slotno);
                                518                 :                :                 /* Now we must recheck state from the top */
                                519                 :            (1) :                 continue;
                                520                 :                :             }
                                521                 :                :             /* Otherwise, it's ready to use */
 6735 tgl@sss.pgh.pa.us         522         [ -  + ]:CBC      144871 :             SlruRecentlyUsed(shared, slotno);
                                523                 :                : 
                                524                 :                :             /* update the stats counter of pages found in the SLRU */
 1432                           525                 :         144871 :             pgstat_count_slru_page_hit(shared->slru_stats_idx);
                                526                 :                : 
 6735                           527                 :         144871 :             return slotno;
                                528                 :                :         }
                                529                 :                : 
                                530                 :                :         /* We found no match; assert we selected a freeable slot */
                                531   [ +  +  +  -  :           1690 :         Assert(shared->page_status[slotno] == SLRU_PAGE_EMPTY ||
                                              -  + ]
                                532                 :                :                (shared->page_status[slotno] == SLRU_PAGE_VALID &&
                                533                 :                :                 !shared->page_dirty[slotno]));
                                534                 :                : 
                                535                 :                :         /* Mark the slot read-busy */
 7613 bruce@momjian.us          536                 :           1690 :         shared->page_number[slotno] = pageno;
                                537                 :           1690 :         shared->page_status[slotno] = SLRU_PAGE_READ_IN_PROGRESS;
 6735 tgl@sss.pgh.pa.us         538                 :           1690 :         shared->page_dirty[slotno] = false;
                                539                 :                : 
                                540                 :                :         /* Acquire per-buffer lock (cannot deadlock, see notes at top) */
 3076 rhaas@postgresql.org      541                 :           1690 :         LWLockAcquire(&shared->buffer_locks[slotno].lock, LW_EXCLUSIVE);
                                542                 :                : 
                                543                 :                :         /* Release bank lock while doing I/O */
   41 alvherre@alvh.no-ip.      544                 :GNC        1690 :         LWLockRelease(banklock);
                                545                 :                : 
                                546                 :                :         /* Do the read */
 7613 bruce@momjian.us          547                 :CBC        1690 :         ok = SlruPhysicalReadPage(ctl, pageno, slotno);
                                548                 :                : 
                                549                 :                :         /* Set the LSNs for this newly read-in page to zero */
 6101 tgl@sss.pgh.pa.us         550                 :           1690 :         SimpleLruZeroLSNs(ctl, slotno);
                                551                 :                : 
                                552                 :                :         /* Re-acquire bank control lock and update page state */
   41 alvherre@alvh.no-ip.      553                 :GNC        1690 :         LWLockAcquire(banklock, LW_EXCLUSIVE);
                                554                 :                : 
 7613 bruce@momjian.us          555   [ +  -  +  -  :CBC        1690 :         Assert(shared->page_number[slotno] == pageno &&
                                              -  + ]
                                556                 :                :                shared->page_status[slotno] == SLRU_PAGE_READ_IN_PROGRESS &&
                                557                 :                :                !shared->page_dirty[slotno]);
                                558                 :                : 
 6735 tgl@sss.pgh.pa.us         559         [ +  - ]:           1690 :         shared->page_status[slotno] = ok ? SLRU_PAGE_VALID : SLRU_PAGE_EMPTY;
                                560                 :                : 
 3076 rhaas@postgresql.org      561                 :           1690 :         LWLockRelease(&shared->buffer_locks[slotno].lock);
                                562                 :                : 
                                563                 :                :         /* Now it's okay to ereport if we failed */
 7613 bruce@momjian.us          564         [ -  + ]:           1690 :         if (!ok)
 7613 bruce@momjian.us          565                 :UBC           0 :             SlruReportIOError(ctl, pageno, xid);
                                566                 :                : 
 7613 bruce@momjian.us          567         [ +  - ]:CBC        1690 :         SlruRecentlyUsed(shared, slotno);
                                568                 :                : 
                                569                 :                :         /* update the stats counter of pages not found in SLRU */
 1432 tgl@sss.pgh.pa.us         570                 :           1690 :         pgstat_count_slru_page_read(shared->slru_stats_idx);
                                571                 :                : 
 7174                           572                 :           1690 :         return slotno;
                                573                 :                :     }
                                574                 :                : }
                                575                 :                : 
                                576                 :                : /*
                                577                 :                :  * Find a page in a shared buffer, reading it in if necessary.
                                578                 :                :  * The page number must correspond to an already-initialized page.
                                579                 :                :  * The caller must intend only read-only access to the page.
                                580                 :                :  *
                                581                 :                :  * The passed-in xid is used only for error reporting, and may be
                                582                 :                :  * InvalidTransactionId if no specific xid is associated with the action.
                                583                 :                :  *
                                584                 :                :  * Return value is the shared-buffer slot number now holding the page.
                                585                 :                :  * The buffer's LRU access info is updated.
                                586                 :                :  *
                                587                 :                :  * Bank control lock must NOT be held at entry, but will be held at exit.
                                588                 :                :  * It is unspecified whether the lock will be shared or exclusive.
                                589                 :                :  */
                                590                 :                : int
  137 akorotkov@postgresql      591                 :GNC      643100 : SimpleLruReadPage_ReadOnly(SlruCtl ctl, int64 pageno, TransactionId xid)
                                592                 :                : {
 6704 tgl@sss.pgh.pa.us         593                 :CBC      643100 :     SlruShared  shared = ctl->shared;
   41 alvherre@alvh.no-ip.      594                 :GNC      643100 :     LWLock     *banklock = SimpleLruGetBankLock(ctl, pageno);
   46                           595                 :         643100 :     int         bankno = pageno & ctl->bank_mask;
                                596                 :         643100 :     int         bankstart = bankno * SLRU_BANK_SIZE;
                                597                 :         643100 :     int         bankend = bankstart + SLRU_BANK_SIZE;
                                598                 :                : 
                                599                 :                :     /* Try to find the page while holding only shared lock */
   41                           600                 :         643100 :     LWLockAcquire(banklock, LW_SHARED);
                                601                 :                : 
                                602                 :                :     /* See if page is already in a buffer */
   46                           603         [ +  + ]:         647473 :     for (int slotno = bankstart; slotno < bankend; slotno++)
                                604                 :                :     {
                                605         [ +  + ]:         647298 :         if (shared->page_status[slotno] != SLRU_PAGE_EMPTY &&
                                606         [ +  + ]:         646306 :             shared->page_number[slotno] == pageno &&
 6704 tgl@sss.pgh.pa.us         607         [ +  - ]:CBC      642925 :             shared->page_status[slotno] != SLRU_PAGE_READ_IN_PROGRESS)
                                608                 :                :         {
                                609                 :                :             /* See comments for SlruRecentlyUsed macro */
                                610         [ +  + ]:         642925 :             SlruRecentlyUsed(shared, slotno);
                                611                 :                : 
                                612                 :                :             /* update the stats counter of pages found in the SLRU */
 1432                           613                 :         642925 :             pgstat_count_slru_page_hit(shared->slru_stats_idx);
                                614                 :                : 
 6704                           615                 :         642925 :             return slotno;
                                616                 :                :         }
                                617                 :                :     }
                                618                 :                : 
                                619                 :                :     /* No luck, so switch to normal exclusive lock and do regular read */
   41 alvherre@alvh.no-ip.      620                 :GNC         175 :     LWLockRelease(banklock);
                                621                 :            175 :     LWLockAcquire(banklock, LW_EXCLUSIVE);
                                622                 :                : 
 6101 tgl@sss.pgh.pa.us         623                 :CBC         175 :     return SimpleLruReadPage(ctl, pageno, true, xid);
                                624                 :                : }
                                625                 :                : 
                                626                 :                : /*
                                627                 :                :  * Write a page from a shared buffer, if necessary.
                                628                 :                :  * Does nothing if the specified slot is not dirty.
                                629                 :                :  *
                                630                 :                :  * NOTE: only one write attempt is made here.  Hence, it is possible that
                                631                 :                :  * the page is still dirty at exit (if someone else re-dirtied it during
                                632                 :                :  * the write).  However, we *do* attempt a fresh write even if the page
                                633                 :                :  * is already being written; this is for checkpoints.
                                634                 :                :  *
                                635                 :                :  * Bank lock must be held at entry, and will be held at exit.
                                636                 :                :  */
                                637                 :                : static void
 1297 tmunro@postgresql.or      638                 :        7342134 : SlruInternalWritePage(SlruCtl ctl, int slotno, SlruWriteAll fdata)
                                639                 :                : {
 7174 tgl@sss.pgh.pa.us         640                 :        7342134 :     SlruShared  shared = ctl->shared;
  137 akorotkov@postgresql      641                 :GNC     7342134 :     int64       pageno = shared->page_number[slotno];
   46 alvherre@alvh.no-ip.      642                 :        7342134 :     int         bankno = SlotGetBankNumber(slotno);
                                643                 :                :     bool        ok;
                                644                 :                : 
                                645         [ -  + ]:        7342134 :     Assert(shared->page_status[slotno] != SLRU_PAGE_EMPTY);
                                646         [ -  + ]:        7342134 :     Assert(LWLockHeldByMeInMode(SimpleLruGetBankLock(ctl, pageno), LW_EXCLUSIVE));
                                647                 :                : 
                                648                 :                :     /* If a write is in progress, wait for it to finish */
 6735 tgl@sss.pgh.pa.us         649         [ -  + ]:CBC     7342134 :     while (shared->page_status[slotno] == SLRU_PAGE_WRITE_IN_PROGRESS &&
 6735 tgl@sss.pgh.pa.us         650         [ #  # ]:UBC           0 :            shared->page_number[slotno] == pageno)
                                651                 :                :     {
                                652                 :              0 :         SimpleLruWaitIO(ctl, slotno);
                                653                 :                :     }
                                654                 :                : 
                                655                 :                :     /*
                                656                 :                :      * Do nothing if page is not dirty, or if buffer no longer contains the
                                657                 :                :      * same page we were called for.
                                658                 :                :      */
 6735 tgl@sss.pgh.pa.us         659         [ +  + ]:CBC     7342134 :     if (!shared->page_dirty[slotno] ||
                                660         [ +  - ]:        7340843 :         shared->page_status[slotno] != SLRU_PAGE_VALID ||
                                661         [ -  + ]:        7340843 :         shared->page_number[slotno] != pageno)
 7613 bruce@momjian.us          662                 :           1291 :         return;
                                663                 :                : 
                                664                 :                :     /*
                                665                 :                :      * Mark the slot write-busy, and clear the dirtybit.  After this point, a
                                666                 :                :      * transaction status update on this page will mark it dirty again.
                                667                 :                :      */
                                668                 :        7340843 :     shared->page_status[slotno] = SLRU_PAGE_WRITE_IN_PROGRESS;
 6735 tgl@sss.pgh.pa.us         669                 :        7340843 :     shared->page_dirty[slotno] = false;
                                670                 :                : 
                                671                 :                :     /* Acquire per-buffer lock (cannot deadlock, see notes at top) */
 3076 rhaas@postgresql.org      672                 :        7340843 :     LWLockAcquire(&shared->buffer_locks[slotno].lock, LW_EXCLUSIVE);
                                673                 :                : 
                                674                 :                :     /* Release bank lock while doing I/O */
   46 alvherre@alvh.no-ip.      675                 :GNC     7340843 :     LWLockRelease(&shared->bank_locks[bankno].lock);
                                676                 :                : 
                                677                 :                :     /* Do the write */
 7258 tgl@sss.pgh.pa.us         678                 :CBC     7340843 :     ok = SlruPhysicalWritePage(ctl, pageno, slotno, fdata);
                                679                 :                : 
                                680                 :                :     /* If we failed, and we're in a flush, better close the files */
                                681   [ -  +  -  - ]:        7340843 :     if (!ok && fdata)
                                682                 :                :     {
   48 alvherre@alvh.no-ip.      683         [ #  # ]:UNC           0 :         for (int i = 0; i < fdata->num_files; i++)
 4156 heikki.linnakangas@i      684                 :UBC           0 :             CloseTransientFile(fdata->fd[i]);
                                685                 :                :     }
                                686                 :                : 
                                687                 :                :     /* Re-acquire bank lock and update page state */
   46 alvherre@alvh.no-ip.      688                 :GNC     7340843 :     LWLockAcquire(&shared->bank_locks[bankno].lock, LW_EXCLUSIVE);
                                689                 :                : 
 7613 bruce@momjian.us          690   [ +  -  -  + ]:CBC     7340843 :     Assert(shared->page_number[slotno] == pageno &&
                                691                 :                :            shared->page_status[slotno] == SLRU_PAGE_WRITE_IN_PROGRESS);
                                692                 :                : 
                                693                 :                :     /* If we failed to write, mark the page dirty again */
 6735 tgl@sss.pgh.pa.us         694         [ -  + ]:        7340843 :     if (!ok)
 6735 tgl@sss.pgh.pa.us         695                 :UBC           0 :         shared->page_dirty[slotno] = true;
                                696                 :                : 
 6735 tgl@sss.pgh.pa.us         697                 :CBC     7340843 :     shared->page_status[slotno] = SLRU_PAGE_VALID;
                                698                 :                : 
 3076 rhaas@postgresql.org      699                 :        7340843 :     LWLockRelease(&shared->buffer_locks[slotno].lock);
                                700                 :                : 
                                701                 :                :     /* Now it's okay to ereport if we failed */
 7613 bruce@momjian.us          702         [ -  + ]:        7340843 :     if (!ok)
 7613 bruce@momjian.us          703                 :UBC           0 :         SlruReportIOError(ctl, pageno, InvalidTransactionId);
                                704                 :                : 
                                705                 :                :     /* If part of a checkpoint, count this as a buffer written. */
 1297 tmunro@postgresql.or      706         [ +  + ]:CBC     7340843 :     if (fdata)
                                707                 :           2453 :         CheckpointStats.ckpt_bufs_written++;
                                708                 :                : }
                                709                 :                : 
                                710                 :                : /*
                                711                 :                :  * Wrapper of SlruInternalWritePage, for external callers.
                                712                 :                :  * fdata is always passed a NULL here.
                                713                 :                :  */
                                714                 :                : void
 4854 alvherre@alvh.no-ip.      715                 :            264 : SimpleLruWritePage(SlruCtl ctl, int slotno)
                                716                 :                : {
   40 alvherre@alvh.no-ip.      717         [ -  + ]:GNC         264 :     Assert(ctl->shared->page_status[slotno] != SLRU_PAGE_EMPTY);
                                718                 :                : 
 4854 alvherre@alvh.no-ip.      719                 :CBC         264 :     SlruInternalWritePage(ctl, slotno, NULL);
                                720                 :            264 : }
                                721                 :                : 
                                722                 :                : /*
                                723                 :                :  * Return whether the given page exists on disk.
                                724                 :                :  *
                                725                 :                :  * A false return means that either the file does not exist, or that it's not
                                726                 :                :  * large enough to contain the given page.
                                727                 :                :  */
                                728                 :                : bool
  137 akorotkov@postgresql      729                 :GNC          70 : SimpleLruDoesPhysicalPageExist(SlruCtl ctl, int64 pageno)
                                730                 :                : {
                                731                 :             70 :     int64       segno = pageno / SLRU_PAGES_PER_SEGMENT;
 3891 alvherre@alvh.no-ip.      732                 :CBC          70 :     int         rpageno = pageno % SLRU_PAGES_PER_SEGMENT;
                                733                 :             70 :     int         offset = rpageno * BLCKSZ;
                                734                 :                :     char        path[MAXPGPATH];
                                735                 :                :     int         fd;
                                736                 :                :     bool        result;
                                737                 :                :     off_t       endpos;
                                738                 :                : 
                                739                 :                :     /* update the stats counter of checked pages */
 1432 tgl@sss.pgh.pa.us         740                 :             70 :     pgstat_count_slru_page_exists(ctl->shared->slru_stats_idx);
                                741                 :                : 
 3891 alvherre@alvh.no-ip.      742                 :             70 :     SlruFileName(ctl, path, segno);
                                743                 :                : 
 1863 michael@paquier.xyz       744                 :             70 :     fd = OpenTransientFile(path, O_RDONLY | PG_BINARY);
 3891 alvherre@alvh.no-ip.      745         [ +  + ]:             70 :     if (fd < 0)
                                746                 :                :     {
                                747                 :                :         /* expected: file doesn't exist */
                                748         [ +  - ]:             20 :         if (errno == ENOENT)
                                749                 :             20 :             return false;
                                750                 :                : 
                                751                 :                :         /* report error normally */
 3891 alvherre@alvh.no-ip.      752                 :UBC           0 :         slru_errcause = SLRU_OPEN_FAILED;
                                753                 :              0 :         slru_errno = errno;
                                754                 :              0 :         SlruReportIOError(ctl, pageno, 0);
                                755                 :                :     }
                                756                 :                : 
 3891 alvherre@alvh.no-ip.      757         [ -  + ]:CBC          50 :     if ((endpos = lseek(fd, 0, SEEK_END)) < 0)
                                758                 :                :     {
 2139 alvherre@alvh.no-ip.      759                 :UBC           0 :         slru_errcause = SLRU_SEEK_FAILED;
 3891                           760                 :              0 :         slru_errno = errno;
                                761                 :              0 :         SlruReportIOError(ctl, pageno, 0);
                                762                 :                :     }
                                763                 :                : 
 3891 alvherre@alvh.no-ip.      764                 :CBC          50 :     result = endpos >= (off_t) (offset + BLCKSZ);
                                765                 :                : 
 1744 peter@eisentraut.org      766         [ -  + ]:             50 :     if (CloseTransientFile(fd) != 0)
                                767                 :                :     {
 1863 michael@paquier.xyz       768                 :UBC           0 :         slru_errcause = SLRU_CLOSE_FAILED;
                                769                 :              0 :         slru_errno = errno;
                                770                 :              0 :         return false;
                                771                 :                :     }
                                772                 :                : 
 3891 alvherre@alvh.no-ip.      773                 :CBC          50 :     return result;
                                774                 :                : }
                                775                 :                : 
                                776                 :                : /*
                                777                 :                :  * Physical read of a (previously existing) page into a buffer slot
                                778                 :                :  *
                                779                 :                :  * On failure, we cannot just ereport(ERROR) since caller has put state in
                                780                 :                :  * shared memory that must be undone.  So, we return false and save enough
                                781                 :                :  * info in static variables to let SlruReportIOError make the report.
                                782                 :                :  *
                                783                 :                :  * For now, assume it's not worth keeping a file pointer open across
                                784                 :                :  * read/write operations.  We could cache one virtual file pointer ...
                                785                 :                :  */
                                786                 :                : static bool
  137 akorotkov@postgresql      787                 :GNC        1690 : SlruPhysicalReadPage(SlruCtl ctl, int64 pageno, int slotno)
                                788                 :                : {
 7258 tgl@sss.pgh.pa.us         789                 :CBC        1690 :     SlruShared  shared = ctl->shared;
  137 akorotkov@postgresql      790                 :GNC        1690 :     int64       segno = pageno / SLRU_PAGES_PER_SEGMENT;
 7613 bruce@momjian.us          791                 :CBC        1690 :     int         rpageno = pageno % SLRU_PAGES_PER_SEGMENT;
 1352 tmunro@postgresql.or      792                 :           1690 :     off_t       offset = rpageno * BLCKSZ;
                                793                 :                :     char        path[MAXPGPATH];
                                794                 :                :     int         fd;
                                795                 :                : 
 7613 bruce@momjian.us          796                 :           1690 :     SlruFileName(ctl, path, segno);
                                797                 :                : 
                                798                 :                :     /*
                                799                 :                :      * In a crash-and-restart situation, it's possible for us to receive
                                800                 :                :      * commands to set the commit status of transactions whose bits are in
                                801                 :                :      * already-truncated segments of the commit log (see notes in
                                802                 :                :      * SlruPhysicalWritePage).  Hence, if we are InRecovery, allow the case
                                803                 :                :      * where the file doesn't exist, and return zeroes instead.
                                804                 :                :      */
 1863 michael@paquier.xyz       805                 :           1690 :     fd = OpenTransientFile(path, O_RDONLY | PG_BINARY);
 7613 bruce@momjian.us          806         [ -  + ]:           1690 :     if (fd < 0)
                                807                 :                :     {
 7613 bruce@momjian.us          808   [ #  #  #  # ]:UBC           0 :         if (errno != ENOENT || !InRecovery)
                                809                 :                :         {
                                810                 :              0 :             slru_errcause = SLRU_OPEN_FAILED;
                                811                 :              0 :             slru_errno = errno;
                                812                 :              0 :             return false;
                                813                 :                :         }
                                814                 :                : 
 7575 tgl@sss.pgh.pa.us         815         [ #  # ]:              0 :         ereport(LOG,
                                816                 :                :                 (errmsg("file \"%s\" doesn't exist, reading as zeroes",
                                817                 :                :                         path)));
 7613 bruce@momjian.us          818   [ #  #  #  #  :              0 :         MemSet(shared->page_buffer[slotno], 0, BLCKSZ);
                                     #  #  #  #  #  
                                                 # ]
                                819                 :              0 :         return true;
                                820                 :                :     }
                                821                 :                : 
 7613 bruce@momjian.us          822                 :CBC        1690 :     errno = 0;
 2584 rhaas@postgresql.org      823                 :           1690 :     pgstat_report_wait_start(WAIT_EVENT_SLRU_READ);
  563 tmunro@postgresql.or      824         [ -  + ]:           1690 :     if (pg_pread(fd, shared->page_buffer[slotno], BLCKSZ, offset) != BLCKSZ)
                                825                 :                :     {
 2584 rhaas@postgresql.org      826                 :UBC           0 :         pgstat_report_wait_end();
 7613 bruce@momjian.us          827                 :              0 :         slru_errcause = SLRU_READ_FAILED;
                                828                 :              0 :         slru_errno = errno;
 4156 heikki.linnakangas@i      829                 :              0 :         CloseTransientFile(fd);
 7613 bruce@momjian.us          830                 :              0 :         return false;
                                831                 :                :     }
 2584 rhaas@postgresql.org      832                 :CBC        1690 :     pgstat_report_wait_end();
                                833                 :                : 
 1744 peter@eisentraut.org      834         [ -  + ]:           1690 :     if (CloseTransientFile(fd) != 0)
                                835                 :                :     {
 7384 tgl@sss.pgh.pa.us         836                 :UBC           0 :         slru_errcause = SLRU_CLOSE_FAILED;
                                837                 :              0 :         slru_errno = errno;
                                838                 :              0 :         return false;
                                839                 :                :     }
                                840                 :                : 
 7613 bruce@momjian.us          841                 :CBC        1690 :     return true;
                                842                 :                : }
                                843                 :                : 
                                844                 :                : /*
                                845                 :                :  * Physical write of a page from a buffer slot
                                846                 :                :  *
                                847                 :                :  * On failure, we cannot just ereport(ERROR) since caller has put state in
                                848                 :                :  * shared memory that must be undone.  So, we return false and save enough
                                849                 :                :  * info in static variables to let SlruReportIOError make the report.
                                850                 :                :  *
                                851                 :                :  * For now, assume it's not worth keeping a file pointer open across
                                852                 :                :  * independent read/write operations.  We do batch operations during
                                853                 :                :  * SimpleLruWriteAll, though.
                                854                 :                :  *
                                855                 :                :  * fdata is NULL for a standalone write, pointer to open-file info during
                                856                 :                :  * SimpleLruWriteAll.
                                857                 :                :  */
                                858                 :                : static bool
  137 akorotkov@postgresql      859                 :GNC     7340843 : SlruPhysicalWritePage(SlruCtl ctl, int64 pageno, int slotno, SlruWriteAll fdata)
                                860                 :                : {
 7258 tgl@sss.pgh.pa.us         861                 :CBC     7340843 :     SlruShared  shared = ctl->shared;
  137 akorotkov@postgresql      862                 :GNC     7340843 :     int64       segno = pageno / SLRU_PAGES_PER_SEGMENT;
 7613 bruce@momjian.us          863                 :CBC     7340843 :     int         rpageno = pageno % SLRU_PAGES_PER_SEGMENT;
 1352 tmunro@postgresql.or      864                 :        7340843 :     off_t       offset = rpageno * BLCKSZ;
                                865                 :                :     char        path[MAXPGPATH];
 7258 tgl@sss.pgh.pa.us         866                 :        7340843 :     int         fd = -1;
                                867                 :                : 
                                868                 :                :     /* update the stats counter of written pages */
 1432                           869                 :        7340843 :     pgstat_count_slru_page_written(shared->slru_stats_idx);
                                870                 :                : 
                                871                 :                :     /*
                                872                 :                :      * Honor the write-WAL-before-data rule, if appropriate, so that we do not
                                873                 :                :      * write out data before associated WAL records.  This is the same action
                                874                 :                :      * performed during FlushBuffer() in the main buffer manager.
                                875                 :                :      */
 6101                           876         [ +  + ]:        7340843 :     if (shared->group_lsn != NULL)
                                877                 :                :     {
                                878                 :                :         /*
                                879                 :                :          * We must determine the largest async-commit LSN for the page. This
                                880                 :                :          * is a bit tedious, but since this entire function is a slow path
                                881                 :                :          * anyway, it seems better to do this here than to maintain a per-page
                                882                 :                :          * LSN variable (which'd need an extra comparison in the
                                883                 :                :          * transaction-commit path).
                                884                 :                :          */
                                885                 :                :         XLogRecPtr  max_lsn;
                                886                 :                :         int         lsnindex;
                                887                 :                : 
                                888                 :         432722 :         lsnindex = slotno * shared->lsn_groups_per_page;
                                889                 :         432722 :         max_lsn = shared->group_lsn[lsnindex++];
   48 alvherre@alvh.no-ip.      890         [ +  + ]:GNC   443107328 :         for (int lsnoff = 1; lsnoff < shared->lsn_groups_per_page; lsnoff++)
                                891                 :                :         {
 6101 tgl@sss.pgh.pa.us         892                 :CBC   442674606 :             XLogRecPtr  this_lsn = shared->group_lsn[lsnindex++];
                                893                 :                : 
 4125 alvherre@alvh.no-ip.      894         [ +  + ]:      442674606 :             if (max_lsn < this_lsn)
 6101 tgl@sss.pgh.pa.us         895                 :           5403 :                 max_lsn = this_lsn;
                                896                 :                :         }
                                897                 :                : 
                                898         [ +  + ]:         432722 :         if (!XLogRecPtrIsInvalid(max_lsn))
                                899                 :                :         {
                                900                 :                :             /*
                                901                 :                :              * As noted above, elog(ERROR) is not acceptable here, so if
                                902                 :                :              * XLogFlush were to fail, we must PANIC.  This isn't much of a
                                903                 :                :              * restriction because XLogFlush is just about all critical
                                904                 :                :              * section anyway, but let's make sure.
                                905                 :                :              */
                                906                 :            201 :             START_CRIT_SECTION();
                                907                 :            201 :             XLogFlush(max_lsn);
                                908         [ -  + ]:            201 :             END_CRIT_SECTION();
                                909                 :                :         }
                                910                 :                :     }
                                911                 :                : 
                                912                 :                :     /*
                                913                 :                :      * During a SimpleLruWriteAll, we may already have the desired file open.
                                914                 :                :      */
 7258                           915         [ +  + ]:        7340843 :     if (fdata)
                                916                 :                :     {
   48 alvherre@alvh.no-ip.      917         [ +  + ]:GNC        2549 :         for (int i = 0; i < fdata->num_files; i++)
                                918                 :                :         {
 7258 tgl@sss.pgh.pa.us         919         [ +  + ]:CBC         267 :             if (fdata->segno[i] == segno)
                                920                 :                :             {
                                921                 :            171 :                 fd = fdata->fd[i];
                                922                 :            171 :                 break;
                                923                 :                :             }
                                924                 :                :         }
                                925                 :                :     }
                                926                 :                : 
                                927         [ +  + ]:        7340843 :     if (fd < 0)
                                928                 :                :     {
                                929                 :                :         /*
                                930                 :                :          * If the file doesn't already exist, we should create it.  It is
                                931                 :                :          * possible for this to need to happen when writing a page that's not
                                932                 :                :          * first in its segment; we assume the OS can cope with that. (Note:
                                933                 :                :          * it might seem that it'd be okay to create files only when
                                934                 :                :          * SimpleLruZeroPage is called for the first page of a segment.
                                935                 :                :          * However, if after a crash and restart the REDO logic elects to
                                936                 :                :          * replay the log from a checkpoint before the latest one, then it's
                                937                 :                :          * possible that we will get commands to set transaction status of
                                938                 :                :          * transactions that have already been truncated from the commit log.
                                939                 :                :          * Easiest way to deal with that is to accept references to
                                940                 :                :          * nonexistent files here and in SlruPhysicalReadPage.)
                                941                 :                :          *
                                942                 :                :          * Note: it is possible for more than one backend to be executing this
                                943                 :                :          * code simultaneously for different pages of the same file. Hence,
                                944                 :                :          * don't use O_EXCL or O_TRUNC or anything like that.
                                945                 :                :          */
                                946                 :        7340672 :         SlruFileName(ctl, path, segno);
 2395 peter_e@gmx.net           947                 :        7340672 :         fd = OpenTransientFile(path, O_RDWR | O_CREAT | PG_BINARY);
 7613 bruce@momjian.us          948         [ -  + ]:        7340672 :         if (fd < 0)
                                949                 :                :         {
 6658 tgl@sss.pgh.pa.us         950                 :UBC           0 :             slru_errcause = SLRU_OPEN_FAILED;
                                951                 :              0 :             slru_errno = errno;
                                952                 :              0 :             return false;
                                953                 :                :         }
                                954                 :                : 
 7258 tgl@sss.pgh.pa.us         955         [ +  + ]:CBC     7340672 :         if (fdata)
                                956                 :                :         {
 1297 tmunro@postgresql.or      957         [ +  - ]:           2282 :             if (fdata->num_files < MAX_WRITEALL_BUFFERS)
                                958                 :                :             {
 6704 tgl@sss.pgh.pa.us         959                 :           2282 :                 fdata->fd[fdata->num_files] = fd;
                                960                 :           2282 :                 fdata->segno[fdata->num_files] = segno;
                                961                 :           2282 :                 fdata->num_files++;
                                962                 :                :             }
                                963                 :                :             else
                                964                 :                :             {
                                965                 :                :                 /*
                                966                 :                :                  * In the unlikely event that we exceed MAX_WRITEALL_BUFFERS,
                                967                 :                :                  * fall back to treating it as a standalone write.
                                968                 :                :                  */
 6704 tgl@sss.pgh.pa.us         969                 :UBC           0 :                 fdata = NULL;
                                970                 :                :             }
                                971                 :                :         }
                                972                 :                :     }
                                973                 :                : 
 7613 bruce@momjian.us          974                 :CBC     7340843 :     errno = 0;
 2584 rhaas@postgresql.org      975                 :        7340843 :     pgstat_report_wait_start(WAIT_EVENT_SLRU_WRITE);
  563 tmunro@postgresql.or      976         [ -  + ]:        7340843 :     if (pg_pwrite(fd, shared->page_buffer[slotno], BLCKSZ, offset) != BLCKSZ)
                                977                 :                :     {
 2584 rhaas@postgresql.org      978                 :UBC           0 :         pgstat_report_wait_end();
                                979                 :                :         /* if write didn't set errno, assume problem is no disk space */
 7613 bruce@momjian.us          980         [ #  # ]:              0 :         if (errno == 0)
                                981                 :              0 :             errno = ENOSPC;
                                982                 :              0 :         slru_errcause = SLRU_WRITE_FAILED;
                                983                 :              0 :         slru_errno = errno;
 7258 tgl@sss.pgh.pa.us         984         [ #  # ]:              0 :         if (!fdata)
 4156 heikki.linnakangas@i      985                 :              0 :             CloseTransientFile(fd);
 7613 bruce@momjian.us          986                 :              0 :         return false;
                                987                 :                :     }
 2584 rhaas@postgresql.org      988                 :CBC     7340843 :     pgstat_report_wait_end();
                                989                 :                : 
                                990                 :                :     /* Queue up a sync request for the checkpointer. */
 1297 tmunro@postgresql.or      991         [ +  + ]:        7340843 :     if (ctl->sync_handler != SYNC_HANDLER_NONE)
                                992                 :                :     {
                                993                 :                :         FileTag     tag;
                                994                 :                : 
                                995                 :         433476 :         INIT_SLRUFILETAG(tag, ctl->sync_handler, segno);
                                996         [ -  + ]:         433476 :         if (!RegisterSyncRequest(&tag, SYNC_REQUEST, false))
                                997                 :                :         {
                                998                 :                :             /* No space to enqueue sync request.  Do it synchronously. */
 1297 tmunro@postgresql.or      999                 :UBC           0 :             pgstat_report_wait_start(WAIT_EVENT_SLRU_SYNC);
                               1000         [ #  # ]:              0 :             if (pg_fsync(fd) != 0)
                               1001                 :                :             {
                               1002                 :              0 :                 pgstat_report_wait_end();
                               1003                 :              0 :                 slru_errcause = SLRU_FSYNC_FAILED;
                               1004                 :              0 :                 slru_errno = errno;
                               1005                 :              0 :                 CloseTransientFile(fd);
                               1006                 :              0 :                 return false;
                               1007                 :                :             }
 2584 rhaas@postgresql.org     1008                 :              0 :             pgstat_report_wait_end();
                               1009                 :                :         }
                               1010                 :                :     }
                               1011                 :                : 
                               1012                 :                :     /* Close file, unless part of flush request. */
 1297 tmunro@postgresql.or     1013         [ +  + ]:CBC     7340843 :     if (!fdata)
                               1014                 :                :     {
 1744 peter@eisentraut.org     1015         [ -  + ]:        7338390 :         if (CloseTransientFile(fd) != 0)
                               1016                 :                :         {
 7258 tgl@sss.pgh.pa.us        1017                 :UBC           0 :             slru_errcause = SLRU_CLOSE_FAILED;
                               1018                 :              0 :             slru_errno = errno;
                               1019                 :              0 :             return false;
                               1020                 :                :         }
                               1021                 :                :     }
                               1022                 :                : 
 7613 bruce@momjian.us         1023                 :CBC     7340843 :     return true;
                               1024                 :                : }
                               1025                 :                : 
                               1026                 :                : /*
                               1027                 :                :  * Issue the error message after failure of SlruPhysicalReadPage or
                               1028                 :                :  * SlruPhysicalWritePage.  Call this after cleaning up shared-memory state.
                               1029                 :                :  */
                               1030                 :                : static void
  137 akorotkov@postgresql     1031                 :UNC           0 : SlruReportIOError(SlruCtl ctl, int64 pageno, TransactionId xid)
                               1032                 :                : {
                               1033                 :              0 :     int64       segno = pageno / SLRU_PAGES_PER_SEGMENT;
 7613 bruce@momjian.us         1034                 :UBC           0 :     int         rpageno = pageno % SLRU_PAGES_PER_SEGMENT;
                               1035                 :              0 :     int         offset = rpageno * BLCKSZ;
                               1036                 :                :     char        path[MAXPGPATH];
                               1037                 :                : 
                               1038                 :              0 :     SlruFileName(ctl, path, segno);
                               1039                 :              0 :     errno = slru_errno;
                               1040   [ #  #  #  #  :              0 :     switch (slru_errcause)
                                           #  #  # ]
                               1041                 :                :     {
                               1042                 :              0 :         case SLRU_OPEN_FAILED:
 7575 tgl@sss.pgh.pa.us        1043         [ #  # ]:              0 :             ereport(ERROR,
                               1044                 :                :                     (errcode_for_file_access(),
                               1045                 :                :                      errmsg("could not access status of transaction %u", xid),
                               1046                 :                :                      errdetail("Could not open file \"%s\": %m.", path)));
                               1047                 :                :             break;
 7613 bruce@momjian.us         1048                 :              0 :         case SLRU_SEEK_FAILED:
 7575 tgl@sss.pgh.pa.us        1049         [ #  # ]:              0 :             ereport(ERROR,
                               1050                 :                :                     (errcode_for_file_access(),
                               1051                 :                :                      errmsg("could not access status of transaction %u", xid),
                               1052                 :                :                      errdetail("Could not seek in file \"%s\" to offset %d: %m.",
                               1053                 :                :                                path, offset)));
                               1054                 :                :             break;
 7613 bruce@momjian.us         1055                 :              0 :         case SLRU_READ_FAILED:
 1685 peter@eisentraut.org     1056         [ #  # ]:              0 :             if (errno)
                               1057         [ #  # ]:              0 :                 ereport(ERROR,
                               1058                 :                :                         (errcode_for_file_access(),
                               1059                 :                :                          errmsg("could not access status of transaction %u", xid),
                               1060                 :                :                          errdetail("Could not read from file \"%s\" at offset %d: %m.",
                               1061                 :                :                                    path, offset)));
                               1062                 :                :             else
                               1063         [ #  # ]:              0 :                 ereport(ERROR,
                               1064                 :                :                         (errmsg("could not access status of transaction %u", xid),
                               1065                 :                :                          errdetail("Could not read from file \"%s\" at offset %d: read too few bytes.", path, offset)));
                               1066                 :                :             break;
 7613 bruce@momjian.us         1067                 :              0 :         case SLRU_WRITE_FAILED:
 1685 peter@eisentraut.org     1068         [ #  # ]:              0 :             if (errno)
                               1069         [ #  # ]:              0 :                 ereport(ERROR,
                               1070                 :                :                         (errcode_for_file_access(),
                               1071                 :                :                          errmsg("could not access status of transaction %u", xid),
                               1072                 :                :                          errdetail("Could not write to file \"%s\" at offset %d: %m.",
                               1073                 :                :                                    path, offset)));
                               1074                 :                :             else
                               1075         [ #  # ]:              0 :                 ereport(ERROR,
                               1076                 :                :                         (errmsg("could not access status of transaction %u", xid),
                               1077                 :                :                          errdetail("Could not write to file \"%s\" at offset %d: wrote too few bytes.",
                               1078                 :                :                                    path, offset)));
                               1079                 :                :             break;
 7258 tgl@sss.pgh.pa.us        1080                 :              0 :         case SLRU_FSYNC_FAILED:
 1973 tmunro@postgresql.or     1081         [ #  # ]:              0 :             ereport(data_sync_elevel(ERROR),
                               1082                 :                :                     (errcode_for_file_access(),
                               1083                 :                :                      errmsg("could not access status of transaction %u", xid),
                               1084                 :                :                      errdetail("Could not fsync file \"%s\": %m.",
                               1085                 :                :                                path)));
 7258 tgl@sss.pgh.pa.us        1086                 :              0 :             break;
 7384                          1087                 :              0 :         case SLRU_CLOSE_FAILED:
                               1088         [ #  # ]:              0 :             ereport(ERROR,
                               1089                 :                :                     (errcode_for_file_access(),
                               1090                 :                :                      errmsg("could not access status of transaction %u", xid),
                               1091                 :                :                      errdetail("Could not close file \"%s\": %m.",
                               1092                 :                :                                path)));
                               1093                 :                :             break;
 7613 bruce@momjian.us         1094                 :              0 :         default:
                               1095                 :                :             /* can't get here, we trust */
 7575 tgl@sss.pgh.pa.us        1096         [ #  # ]:              0 :             elog(ERROR, "unrecognized SimpleLru error cause: %d",
                               1097                 :                :                  (int) slru_errcause);
                               1098                 :                :             break;
                               1099                 :                :     }
 7613 bruce@momjian.us         1100                 :              0 : }
                               1101                 :                : 
                               1102                 :                : /*
                               1103                 :                :  * Mark a buffer slot "most recently used".
                               1104                 :                :  */
                               1105                 :                : static inline void
   46 alvherre@alvh.no-ip.     1106                 :GNC     8128891 : SlruRecentlyUsed(SlruShared shared, int slotno)
                               1107                 :                : {
                               1108                 :        8128891 :     int         bankno = SlotGetBankNumber(slotno);
                               1109                 :        8128891 :     int         new_lru_count = shared->bank_cur_lru_count[bankno];
                               1110                 :                : 
                               1111         [ -  + ]:        8128891 :     Assert(shared->page_status[slotno] != SLRU_PAGE_EMPTY);
                               1112                 :                : 
                               1113                 :                :     /*
                               1114                 :                :      * The reason for the if-test is that there are often many consecutive
                               1115                 :                :      * accesses to the same page (particularly the latest page).  By
                               1116                 :                :      * suppressing useless increments of bank_cur_lru_count, we reduce the
                               1117                 :                :      * probability that old pages' counts will "wrap around" and make them
                               1118                 :                :      * appear recently used.
                               1119                 :                :      *
                               1120                 :                :      * We allow this code to be executed concurrently by multiple processes
                               1121                 :                :      * within SimpleLruReadPage_ReadOnly().  As long as int reads and writes
                               1122                 :                :      * are atomic, this should not cause any completely-bogus values to enter
                               1123                 :                :      * the computation.  However, it is possible for either bank_cur_lru_count
                               1124                 :                :      * or individual page_lru_count entries to be "reset" to lower values than
                               1125                 :                :      * they should have, in case a process is delayed while it executes this
                               1126                 :                :      * function.  With care in SlruSelectLRUPage(), this does little harm, and
                               1127                 :                :      * in any case the absolute worst possible consequence is a nonoptimal
                               1128                 :                :      * choice of page to evict.  The gain from allowing concurrent reads of
                               1129                 :                :      * SLRU pages seems worth it.
                               1130                 :                :      */
                               1131         [ +  + ]:        8128891 :     if (new_lru_count != shared->page_lru_count[slotno])
                               1132                 :                :     {
                               1133                 :        7341178 :         shared->bank_cur_lru_count[bankno] = ++new_lru_count;
                               1134                 :        7341178 :         shared->page_lru_count[slotno] = new_lru_count;
                               1135                 :                :     }
                               1136                 :        8128891 : }
                               1137                 :                : 
                               1138                 :                : /*
                               1139                 :                :  * Select the slot to re-use when we need a free slot for the given page.
                               1140                 :                :  *
                               1141                 :                :  * The target page number is passed not only because we need to know the
                               1142                 :                :  * correct bank to use, but also because we need to consider the possibility
                               1143                 :                :  * that some other process reads in the target page while we are doing I/O to
                               1144                 :                :  * free a slot.  Hence, check or recheck to see if any slot already holds the
                               1145                 :                :  * target page, and return that slot if so.  Thus, the returned slot is
                               1146                 :                :  * *either* a slot already holding the pageno (could be any state except
                               1147                 :                :  * EMPTY), *or* a freeable slot (state EMPTY or CLEAN).
                               1148                 :                :  *
                               1149                 :                :  * The correct bank lock must be held at entry, and will be held at exit.
                               1150                 :                :  */
                               1151                 :                : static int
  137 akorotkov@postgresql     1152                 :        7485966 : SlruSelectLRUPage(SlruCtl ctl, int64 pageno)
                               1153                 :                : {
 7258 tgl@sss.pgh.pa.us        1154                 :CBC     7485966 :     SlruShared  shared = ctl->shared;
                               1155                 :                : 
                               1156                 :                :     /* Outer loop handles restart after I/O */
                               1157                 :                :     for (;;)
 7613 bruce@momjian.us         1158                 :        7338091 :     {
                               1159                 :                :         int         cur_count;
 4326                          1160                 :       14824057 :         int         bestvalidslot = 0;  /* keep compiler quiet */
 4389 rhaas@postgresql.org     1161                 :       14824057 :         int         best_valid_delta = -1;
  137 akorotkov@postgresql     1162                 :GNC    14824057 :         int64       best_valid_page_number = 0; /* keep compiler quiet */
 2489 tgl@sss.pgh.pa.us        1163                 :CBC    14824057 :         int         bestinvalidslot = 0;    /* keep compiler quiet */
 4389 rhaas@postgresql.org     1164                 :       14824057 :         int         best_invalid_delta = -1;
  137 akorotkov@postgresql     1165                 :GNC    14824057 :         int64       best_invalid_page_number = 0;   /* keep compiler quiet */
   46 alvherre@alvh.no-ip.     1166                 :       14824057 :         int         bankno = pageno & ctl->bank_mask;
                               1167                 :       14824057 :         int         bankstart = bankno * SLRU_BANK_SIZE;
                               1168                 :       14824057 :         int         bankend = bankstart + SLRU_BANK_SIZE;
                               1169                 :                : 
   41                          1170         [ -  + ]:       14824057 :         Assert(LWLockHeldByMe(SimpleLruGetBankLock(ctl, pageno)));
                               1171                 :                : 
                               1172                 :                :         /* See if page already has a buffer assigned */
   48                          1173         [ +  + ]:      484539299 :         for (int slotno = 0; slotno < shared->num_slots; slotno++)
                               1174                 :                :         {
   46                          1175         [ +  + ]:      469860236 :             if (shared->page_status[slotno] != SLRU_PAGE_EMPTY &&
                               1176         [ +  + ]:      469793045 :                 shared->page_number[slotno] == pageno)
 7613 bruce@momjian.us         1177                 :CBC      144994 :                 return slotno;
                               1178                 :                :         }
                               1179                 :                : 
                               1180                 :                :         /*
                               1181                 :                :          * If we find any EMPTY slot, just select that one. Else choose a
                               1182                 :                :          * victim page to replace.  We normally take the least recently used
                               1183                 :                :          * valid page, but we will never take the slot containing
                               1184                 :                :          * latest_page_number, even if it appears least recently used.  We
                               1185                 :                :          * will select a slot that is already I/O busy only if there is no
                               1186                 :                :          * other choice: a read-busy slot will not be least recently used once
                               1187                 :                :          * the read finishes, and waiting for an I/O on a write-busy slot is
                               1188                 :                :          * inferior to just picking some other slot.  Testing shows the slot
                               1189                 :                :          * we pick instead will often be clean, allowing us to begin a read at
                               1190                 :                :          * once.
                               1191                 :                :          *
                               1192                 :                :          * Normally the page_lru_count values will all be different and so
                               1193                 :                :          * there will be a well-defined LRU page.  But since we allow
                               1194                 :                :          * concurrent execution of SlruRecentlyUsed() within
                               1195                 :                :          * SimpleLruReadPage_ReadOnly(), it is possible that multiple pages
                               1196                 :                :          * acquire the same lru_count values.  In that case we break ties by
                               1197                 :                :          * choosing the furthest-back page.
                               1198                 :                :          *
                               1199                 :                :          * Notice that this next line forcibly advances cur_lru_count to a
                               1200                 :                :          * value that is certainly beyond any value that will be in the
                               1201                 :                :          * page_lru_count array after the loop finishes.  This ensures that
                               1202                 :                :          * the next execution of SlruRecentlyUsed will mark the page newly
                               1203                 :                :          * used, even if it's for a page that has the current counter value.
                               1204                 :                :          * That gets us back on the path to having good data when there are
                               1205                 :                :          * multiple pages with the same lru_count.
                               1206                 :                :          */
   46 alvherre@alvh.no-ip.     1207                 :GNC    14679063 :         cur_count = (shared->bank_cur_lru_count[bankno])++;
                               1208         [ +  + ]:      249502723 :         for (int slotno = bankstart; slotno < bankend; slotno++)
                               1209                 :                :         {
                               1210                 :                :             int         this_delta;
                               1211                 :                :             int64       this_page_number;
                               1212                 :                : 
 7613 bruce@momjian.us         1213         [ +  + ]:CBC   234826358 :             if (shared->page_status[slotno] == SLRU_PAGE_EMPTY)
                               1214                 :           2698 :                 return slotno;
                               1215                 :                : 
 6704 tgl@sss.pgh.pa.us        1216                 :      234823660 :             this_delta = cur_count - shared->page_lru_count[slotno];
                               1217         [ -  + ]:      234823660 :             if (this_delta < 0)
                               1218                 :                :             {
                               1219                 :                :                 /*
                               1220                 :                :                  * Clean up in case shared updates have caused cur_count
                               1221                 :                :                  * increments to get "lost".  We back off the page counts,
                               1222                 :                :                  * rather than trying to increase cur_count, to avoid any
                               1223                 :                :                  * question of infinite loops or failure in the presence of
                               1224                 :                :                  * wrapped-around counts.
                               1225                 :                :                  */
 6704 tgl@sss.pgh.pa.us        1226                 :UBC           0 :                 shared->page_lru_count[slotno] = cur_count;
                               1227                 :              0 :                 this_delta = 0;
                               1228                 :                :             }
                               1229                 :                : 
                               1230                 :                :             /*
                               1231                 :                :              * If this page is the one most recently zeroed, don't consider it
                               1232                 :                :              * an eviction candidate. See comments in SimpleLruZeroPage for an
                               1233                 :                :              * explanation about the lack of a memory barrier here.
                               1234                 :                :              */
 6704 tgl@sss.pgh.pa.us        1235                 :CBC   234823660 :             this_page_number = shared->page_number[slotno];
   68 alvherre@alvh.no-ip.     1236         [ +  + ]:GNC   234823660 :             if (this_page_number ==
                               1237                 :      234823660 :                 pg_atomic_read_u64(&shared->latest_page_number))
 4389 rhaas@postgresql.org     1238                 :CBC         416 :                 continue;
                               1239                 :                : 
                               1240         [ +  + ]:      234823244 :             if (shared->page_status[slotno] == SLRU_PAGE_VALID)
                               1241                 :                :             {
                               1242   [ +  +  -  + ]:      234823207 :                 if (this_delta > best_valid_delta ||
 4389 rhaas@postgresql.org     1243         [ #  # ]:UBC           0 :                     (this_delta == best_valid_delta &&
                               1244                 :              0 :                      ctl->PagePrecedes(this_page_number,
                               1245                 :                :                                        best_valid_page_number)))
                               1246                 :                :                 {
 4389 rhaas@postgresql.org     1247                 :CBC    28804397 :                     bestvalidslot = slotno;
                               1248                 :       28804397 :                     best_valid_delta = this_delta;
                               1249                 :       28804397 :                     best_valid_page_number = this_page_number;
                               1250                 :                :                 }
                               1251                 :                :             }
                               1252                 :                :             else
                               1253                 :                :             {
 4389 rhaas@postgresql.org     1254   [ -  +  -  - ]:GBC          37 :                 if (this_delta > best_invalid_delta ||
 4389 rhaas@postgresql.org     1255         [ #  # ]:UBC           0 :                     (this_delta == best_invalid_delta &&
                               1256                 :              0 :                      ctl->PagePrecedes(this_page_number,
                               1257                 :                :                                        best_invalid_page_number)))
                               1258                 :                :                 {
 4389 rhaas@postgresql.org     1259                 :GBC          37 :                     bestinvalidslot = slotno;
                               1260                 :             37 :                     best_invalid_delta = this_delta;
                               1261                 :             37 :                     best_invalid_page_number = this_page_number;
                               1262                 :                :                 }
                               1263                 :                :             }
                               1264                 :                :         }
                               1265                 :                : 
                               1266                 :                :         /*
                               1267                 :                :          * If all pages (except possibly the latest one) are I/O busy, we'll
                               1268                 :                :          * have to wait for an I/O to complete and then retry.  In that
                               1269                 :                :          * unhappy case, we choose to wait for the I/O on the least recently
                               1270                 :                :          * used slot, on the assumption that it was likely initiated first of
                               1271                 :                :          * all the I/Os in progress and may therefore finish first.
                               1272                 :                :          */
 4389 rhaas@postgresql.org     1273         [ -  + ]:CBC    14676365 :         if (best_valid_delta < 0)
                               1274                 :                :         {
 4389 rhaas@postgresql.org     1275                 :UBC           0 :             SimpleLruWaitIO(ctl, bestinvalidslot);
                               1276                 :              0 :             continue;
                               1277                 :                :         }
                               1278                 :                : 
                               1279                 :                :         /*
                               1280                 :                :          * If the selected page is clean, we're set.
                               1281                 :                :          */
 4389 rhaas@postgresql.org     1282         [ +  + ]:CBC    14676365 :         if (!shared->page_dirty[bestvalidslot])
                               1283                 :        7338274 :             return bestvalidslot;
                               1284                 :                : 
                               1285                 :                :         /*
                               1286                 :                :          * Write the page.
                               1287                 :                :          */
                               1288                 :        7338091 :         SlruInternalWritePage(ctl, bestvalidslot, NULL);
                               1289                 :                : 
                               1290                 :                :         /*
                               1291                 :                :          * Now loop back and try again.  This is the easiest way of dealing
                               1292                 :                :          * with corner cases such as the victim page being re-dirtied while we
                               1293                 :                :          * wrote it.
                               1294                 :                :          */
                               1295                 :                :     }
                               1296                 :                : }
                               1297                 :                : 
                               1298                 :                : /*
                               1299                 :                :  * Write dirty pages to disk during checkpoint or database shutdown.  Flushing
                               1300                 :                :  * is deferred until the next call to ProcessSyncRequests(), though we do fsync
                               1301                 :                :  * the containing directory here to make sure that newly created directory
                               1302                 :                :  * entries are on disk.
                               1303                 :                :  */
                               1304                 :                : void
 1297 tmunro@postgresql.or     1305                 :           5789 : SimpleLruWriteAll(SlruCtl ctl, bool allow_redirtied)
                               1306                 :                : {
 7258 tgl@sss.pgh.pa.us        1307                 :           5789 :     SlruShared  shared = ctl->shared;
                               1308                 :                :     SlruWriteAllData fdata;
  137 akorotkov@postgresql     1309                 :GNC        5789 :     int64       pageno = 0;
   46 alvherre@alvh.no-ip.     1310                 :           5789 :     int         prevbank = SlotGetBankNumber(0);
                               1311                 :                :     bool        ok;
                               1312                 :                : 
                               1313                 :                :     /* update the stats counter of flushes */
 1432 tgl@sss.pgh.pa.us        1314                 :CBC        5789 :     pgstat_count_slru_flush(shared->slru_stats_idx);
                               1315                 :                : 
                               1316                 :                :     /*
                               1317                 :                :      * Find and write dirty pages
                               1318                 :                :      */
 7258                          1319                 :           5789 :     fdata.num_files = 0;
                               1320                 :                : 
   46 alvherre@alvh.no-ip.     1321                 :GNC        5789 :     LWLockAcquire(&shared->bank_locks[prevbank].lock, LW_EXCLUSIVE);
                               1322                 :                : 
   48                          1323         [ +  + ]:         148333 :     for (int slotno = 0; slotno < shared->num_slots; slotno++)
                               1324                 :                :     {
   46                          1325                 :         142544 :         int         curbank = SlotGetBankNumber(slotno);
                               1326                 :                : 
                               1327                 :                :         /*
                               1328                 :                :          * If the current bank lock is not same as the previous bank lock then
                               1329                 :                :          * release the previous lock and acquire the new lock.
                               1330                 :                :          */
                               1331         [ +  + ]:         142544 :         if (curbank != prevbank)
                               1332                 :                :         {
                               1333                 :           3120 :             LWLockRelease(&shared->bank_locks[prevbank].lock);
                               1334                 :           3120 :             LWLockAcquire(&shared->bank_locks[curbank].lock, LW_EXCLUSIVE);
                               1335                 :           3120 :             prevbank = curbank;
                               1336                 :                :         }
                               1337                 :                : 
                               1338                 :                :         /* Do nothing if slot is unused */
                               1339         [ +  + ]:         142544 :         if (shared->page_status[slotno] == SLRU_PAGE_EMPTY)
                               1340                 :         138800 :             continue;
                               1341                 :                : 
 4854 alvherre@alvh.no-ip.     1342                 :CBC        3744 :         SlruInternalWritePage(ctl, slotno, &fdata);
                               1343                 :                : 
                               1344                 :                :         /*
                               1345                 :                :          * In some places (e.g. checkpoints), we cannot assert that the slot
                               1346                 :                :          * is clean now, since another process might have re-dirtied it
                               1347                 :                :          * already.  That's okay.
                               1348                 :                :          */
 3123 andres@anarazel.de       1349   [ -  +  -  -  :           3744 :         Assert(allow_redirtied ||
                                        -  -  -  - ]
                               1350                 :                :                shared->page_status[slotno] == SLRU_PAGE_EMPTY ||
                               1351                 :                :                (shared->page_status[slotno] == SLRU_PAGE_VALID &&
                               1352                 :                :                 !shared->page_dirty[slotno]));
                               1353                 :                :     }
                               1354                 :                : 
   46 alvherre@alvh.no-ip.     1355                 :GNC        5789 :     LWLockRelease(&shared->bank_locks[prevbank].lock);
                               1356                 :                : 
                               1357                 :                :     /*
                               1358                 :                :      * Now close any files that were open
                               1359                 :                :      */
 7258 tgl@sss.pgh.pa.us        1360                 :CBC        5789 :     ok = true;
   48 alvherre@alvh.no-ip.     1361         [ +  + ]:GNC        8071 :     for (int i = 0; i < fdata.num_files; i++)
                               1362                 :                :     {
 1744 peter@eisentraut.org     1363         [ -  + ]:CBC        2282 :         if (CloseTransientFile(fdata.fd[i]) != 0)
                               1364                 :                :         {
 7258 tgl@sss.pgh.pa.us        1365                 :UBC           0 :             slru_errcause = SLRU_CLOSE_FAILED;
                               1366                 :              0 :             slru_errno = errno;
                               1367                 :              0 :             pageno = fdata.segno[i] * SLRU_PAGES_PER_SEGMENT;
                               1368                 :              0 :             ok = false;
                               1369                 :                :         }
                               1370                 :                :     }
 7258 tgl@sss.pgh.pa.us        1371         [ -  + ]:CBC        5789 :     if (!ok)
 7258 tgl@sss.pgh.pa.us        1372                 :UBC           0 :         SlruReportIOError(ctl, pageno, InvalidTransactionId);
                               1373                 :                : 
                               1374                 :                :     /* Ensure that directory entries for new files are on disk. */
 1297 tmunro@postgresql.or     1375         [ +  + ]:CBC        5789 :     if (ctl->sync_handler != SYNC_HANDLER_NONE)
 1298                          1376                 :           4634 :         fsync_fname(ctl->Dir, true);
 7613 bruce@momjian.us         1377                 :           5789 : }
                               1378                 :                : 
                               1379                 :                : /*
                               1380                 :                :  * Remove all segments before the one holding the passed page number
                               1381                 :                :  *
                               1382                 :                :  * All SLRUs prevent concurrent calls to this function, either with an LWLock
                               1383                 :                :  * or by calling it only as part of a checkpoint.  Mutual exclusion must begin
                               1384                 :                :  * before computing cutoffPage.  Mutual exclusion must end after any limit
                               1385                 :                :  * update that would permit other backends to write fresh data into the
                               1386                 :                :  * segment immediately preceding the one containing cutoffPage.  Otherwise,
                               1387                 :                :  * when the SLRU is quite full, SimpleLruTruncate() might delete that segment
                               1388                 :                :  * after it has accrued freshly-written data.
                               1389                 :                :  */
                               1390                 :                : void
  137 akorotkov@postgresql     1391                 :GNC        1092 : SimpleLruTruncate(SlruCtl ctl, int64 cutoffPage)
                               1392                 :                : {
 7258 tgl@sss.pgh.pa.us        1393                 :CBC        1092 :     SlruShared  shared = ctl->shared;
                               1394                 :                :     int         prevbank;
                               1395                 :                : 
                               1396                 :                :     /* update the stats counter of truncates */
 1432                          1397                 :           1092 :     pgstat_count_slru_truncate(shared->slru_stats_idx);
                               1398                 :                : 
                               1399                 :                :     /*
                               1400                 :                :      * Scan shared memory and remove any pages preceding the cutoff page, to
                               1401                 :                :      * ensure we won't rewrite them later.  (Since this is normally called in
                               1402                 :                :      * or just after a checkpoint, any dirty pages should have been flushed
                               1403                 :                :      * already ... we're just being extra careful here.)
                               1404                 :                :      */
  552 john.naylor@postgres     1405                 :           1127 : restart:
                               1406                 :                : 
                               1407                 :                :     /*
                               1408                 :                :      * An important safety check: the current endpoint page must not be
                               1409                 :                :      * eligible for removal.  This check is just a backstop against wraparound
                               1410                 :                :      * bugs elsewhere in SLRU handling, so we don't care if we read a slightly
                               1411                 :                :      * outdated value; therefore we don't add a memory barrier.
                               1412                 :                :      */
   68 alvherre@alvh.no-ip.     1413         [ -  + ]:GNC        1127 :     if (ctl->PagePrecedes(pg_atomic_read_u64(&shared->latest_page_number),
                               1414                 :                :                           cutoffPage))
                               1415                 :                :     {
 7575 tgl@sss.pgh.pa.us        1416         [ #  # ]:UBC           0 :         ereport(LOG,
                               1417                 :                :                 (errmsg("could not truncate directory \"%s\": apparent wraparound",
                               1418                 :                :                         ctl->Dir)));
 7613 bruce@momjian.us         1419                 :              0 :         return;
                               1420                 :                :     }
                               1421                 :                : 
   46 alvherre@alvh.no-ip.     1422                 :GNC        1127 :     prevbank = SlotGetBankNumber(0);
                               1423                 :           1127 :     LWLockAcquire(&shared->bank_locks[prevbank].lock, LW_EXCLUSIVE);
   68                          1424         [ +  + ]:          29345 :     for (int slotno = 0; slotno < shared->num_slots; slotno++)
                               1425                 :                :     {
   46                          1426                 :          28253 :         int         curbank = SlotGetBankNumber(slotno);
                               1427                 :                : 
                               1428                 :                :         /*
                               1429                 :                :          * If the current bank lock is not same as the previous bank lock then
                               1430                 :                :          * release the previous lock and acquire the new lock.
                               1431                 :                :          */
                               1432         [ +  + ]:          28253 :         if (curbank != prevbank)
                               1433                 :                :         {
                               1434                 :            656 :             LWLockRelease(&shared->bank_locks[prevbank].lock);
                               1435                 :            656 :             LWLockAcquire(&shared->bank_locks[curbank].lock, LW_EXCLUSIVE);
                               1436                 :            656 :             prevbank = curbank;
                               1437                 :                :         }
                               1438                 :                : 
 7613 bruce@momjian.us         1439         [ +  + ]:CBC       28253 :         if (shared->page_status[slotno] == SLRU_PAGE_EMPTY)
                               1440                 :          25359 :             continue;
                               1441         [ +  + ]:           2894 :         if (!ctl->PagePrecedes(shared->page_number[slotno], cutoffPage))
                               1442                 :           2690 :             continue;
                               1443                 :                : 
                               1444                 :                :         /*
                               1445                 :                :          * If page is clean, just change state to EMPTY (expected case).
                               1446                 :                :          */
 6735 tgl@sss.pgh.pa.us        1447         [ +  - ]:            204 :         if (shared->page_status[slotno] == SLRU_PAGE_VALID &&
                               1448         [ +  + ]:            204 :             !shared->page_dirty[slotno])
                               1449                 :                :         {
 7613 bruce@momjian.us         1450                 :            169 :             shared->page_status[slotno] = SLRU_PAGE_EMPTY;
                               1451                 :            169 :             continue;
                               1452                 :                :         }
                               1453                 :                : 
                               1454                 :                :         /*
                               1455                 :                :          * Hmm, we have (or may have) I/O operations acting on the page, so
                               1456                 :                :          * we've got to wait for them to finish and then start again. This is
                               1457                 :                :          * the same logic as in SlruSelectLRUPage.  (XXX if page is dirty,
                               1458                 :                :          * wouldn't it be OK to just discard it without writing it?
                               1459                 :                :          * SlruMayDeleteSegment() uses a stricter qualification, so we might
                               1460                 :                :          * not delete this page in the end; even if we don't delete it, we
                               1461                 :                :          * won't have cause to read its data again.  For now, keep the logic
                               1462                 :                :          * the same as it was.)
                               1463                 :                :          */
 6735 tgl@sss.pgh.pa.us        1464         [ +  - ]:             35 :         if (shared->page_status[slotno] == SLRU_PAGE_VALID)
 4854 alvherre@alvh.no-ip.     1465                 :             35 :             SlruInternalWritePage(ctl, slotno, NULL);
                               1466                 :                :         else
 6735 tgl@sss.pgh.pa.us        1467                 :UBC           0 :             SimpleLruWaitIO(ctl, slotno);
                               1468                 :                : 
   46 alvherre@alvh.no-ip.     1469                 :GNC          35 :         LWLockRelease(&shared->bank_locks[prevbank].lock);
 7613 bruce@momjian.us         1470                 :CBC          35 :         goto restart;
                               1471                 :                :     }
                               1472                 :                : 
   46 alvherre@alvh.no-ip.     1473                 :GNC        1092 :     LWLockRelease(&shared->bank_locks[prevbank].lock);
                               1474                 :                : 
                               1475                 :                :     /* Now we can remove the old segment(s) */
 4582 alvherre@alvh.no-ip.     1476                 :CBC        1092 :     (void) SlruScanDirectory(ctl, SlruScanDirCbDeleteCutoff, &cutoffPage);
                               1477                 :                : }
                               1478                 :                : 
                               1479                 :                : /*
                               1480                 :                :  * Delete an individual SLRU segment.
                               1481                 :                :  *
                               1482                 :                :  * NB: This does not touch the SLRU buffers themselves, callers have to ensure
                               1483                 :                :  * they either can't yet contain anything, or have already been cleaned out.
                               1484                 :                :  */
                               1485                 :                : static void
  137 akorotkov@postgresql     1486                 :GNC      109282 : SlruInternalDeleteSegment(SlruCtl ctl, int64 segno)
                               1487                 :                : {
                               1488                 :                :     char        path[MAXPGPATH];
                               1489                 :                : 
                               1490                 :                :     /* Forget any fsync requests queued for this segment. */
 1256 tmunro@postgresql.or     1491         [ +  + ]:CBC      109282 :     if (ctl->sync_handler != SYNC_HANDLER_NONE)
                               1492                 :                :     {
                               1493                 :                :         FileTag     tag;
                               1494                 :                : 
                               1495                 :          13271 :         INIT_SLRUFILETAG(tag, ctl->sync_handler, segno);
                               1496                 :          13271 :         RegisterSyncRequest(&tag, SYNC_FORGET_REQUEST, true);
                               1497                 :                :     }
                               1498                 :                : 
                               1499                 :                :     /* Unlink the file. */
                               1500                 :         109282 :     SlruFileName(ctl, path, segno);
 1152 peter@eisentraut.org     1501         [ -  + ]:         109282 :     ereport(DEBUG2, (errmsg_internal("removing file \"%s\"", path)));
 3755 alvherre@alvh.no-ip.     1502                 :         109282 :     unlink(path);
                               1503                 :         109282 : }
                               1504                 :                : 
                               1505                 :                : /*
                               1506                 :                :  * Delete an individual SLRU segment, identified by the segment number.
                               1507                 :                :  */
                               1508                 :                : void
  137 akorotkov@postgresql     1509                 :GNC           2 : SlruDeleteSegment(SlruCtl ctl, int64 segno)
                               1510                 :                : {
 3123 andres@anarazel.de       1511                 :CBC           2 :     SlruShared  shared = ctl->shared;
   46 alvherre@alvh.no-ip.     1512                 :GNC           2 :     int         prevbank = SlotGetBankNumber(0);
                               1513                 :                :     bool        did_write;
                               1514                 :                : 
                               1515                 :                :     /* Clean out any possibly existing references to the segment. */
                               1516                 :              2 :     LWLockAcquire(&shared->bank_locks[prevbank].lock, LW_EXCLUSIVE);
 3123 andres@anarazel.de       1517                 :CBC           2 : restart:
                               1518                 :              2 :     did_write = false;
   48 alvherre@alvh.no-ip.     1519         [ +  + ]:GNC          34 :     for (int slotno = 0; slotno < shared->num_slots; slotno++)
                               1520                 :                :     {
                               1521                 :                :         int         pagesegno;
   46                          1522                 :             32 :         int         curbank = SlotGetBankNumber(slotno);
                               1523                 :                : 
                               1524                 :                :         /*
                               1525                 :                :          * If the current bank lock is not same as the previous bank lock then
                               1526                 :                :          * release the previous lock and acquire the new lock.
                               1527                 :                :          */
                               1528         [ -  + ]:             32 :         if (curbank != prevbank)
                               1529                 :                :         {
   46 alvherre@alvh.no-ip.     1530                 :UNC           0 :             LWLockRelease(&shared->bank_locks[prevbank].lock);
                               1531                 :              0 :             LWLockAcquire(&shared->bank_locks[curbank].lock, LW_EXCLUSIVE);
                               1532                 :              0 :             prevbank = curbank;
                               1533                 :                :         }
                               1534                 :                : 
 3123 andres@anarazel.de       1535         [ -  + ]:CBC          32 :         if (shared->page_status[slotno] == SLRU_PAGE_EMPTY)
 3123 andres@anarazel.de       1536                 :UBC           0 :             continue;
                               1537                 :                : 
   46 alvherre@alvh.no-ip.     1538                 :GNC          32 :         pagesegno = shared->page_number[slotno] / SLRU_PAGES_PER_SEGMENT;
                               1539                 :                :         /* not the segment we're looking for */
 3123 andres@anarazel.de       1540         [ +  + ]:CBC          32 :         if (pagesegno != segno)
                               1541                 :             22 :             continue;
                               1542                 :                : 
                               1543                 :                :         /* If page is clean, just change state to EMPTY (expected case). */
                               1544         [ +  - ]:             10 :         if (shared->page_status[slotno] == SLRU_PAGE_VALID &&
                               1545         [ +  - ]:             10 :             !shared->page_dirty[slotno])
                               1546                 :                :         {
                               1547                 :             10 :             shared->page_status[slotno] = SLRU_PAGE_EMPTY;
                               1548                 :             10 :             continue;
                               1549                 :                :         }
                               1550                 :                : 
                               1551                 :                :         /* Same logic as SimpleLruTruncate() */
 3123 andres@anarazel.de       1552         [ #  # ]:UBC           0 :         if (shared->page_status[slotno] == SLRU_PAGE_VALID)
                               1553                 :              0 :             SlruInternalWritePage(ctl, slotno, NULL);
                               1554                 :                :         else
                               1555                 :              0 :             SimpleLruWaitIO(ctl, slotno);
                               1556                 :                : 
                               1557                 :              0 :         did_write = true;
                               1558                 :                :     }
                               1559                 :                : 
                               1560                 :                :     /*
                               1561                 :                :      * Be extra careful and re-check. The IO functions release the control
                               1562                 :                :      * lock, so new pages could have been read in.
                               1563                 :                :      */
 3123 andres@anarazel.de       1564         [ -  + ]:CBC           2 :     if (did_write)
 3123 andres@anarazel.de       1565                 :UBC           0 :         goto restart;
                               1566                 :                : 
 1256 tmunro@postgresql.or     1567                 :CBC           2 :     SlruInternalDeleteSegment(ctl, segno);
                               1568                 :                : 
   46 alvherre@alvh.no-ip.     1569                 :GNC           2 :     LWLockRelease(&shared->bank_locks[prevbank].lock);
 3123 andres@anarazel.de       1570                 :CBC           2 : }
                               1571                 :                : 
                               1572                 :                : /*
                               1573                 :                :  * Determine whether a segment is okay to delete.
                               1574                 :                :  *
                               1575                 :                :  * segpage is the first page of the segment, and cutoffPage is the oldest (in
                               1576                 :                :  * PagePrecedes order) page in the SLRU containing still-useful data.  Since
                               1577                 :                :  * every core PagePrecedes callback implements "wrap around", check the
                               1578                 :                :  * segment's first and last pages:
                               1579                 :                :  *
                               1580                 :                :  * first<cutoff  && last<cutoff:  yes
                               1581                 :                :  * first<cutoff  && last>=cutoff: no; cutoff falls inside this segment
                               1582                 :                :  * first>=cutoff && last<cutoff:  no; wrap point falls inside this segment
                               1583                 :                :  * first>=cutoff && last>=cutoff: no; every page of this segment is too young
                               1584                 :                :  */
                               1585                 :                : static bool
  137 akorotkov@postgresql     1586                 :GNC      836179 : SlruMayDeleteSegment(SlruCtl ctl, int64 segpage, int64 cutoffPage)
                               1587                 :                : {
                               1588                 :         836179 :     int64       seg_last_page = segpage + SLRU_PAGES_PER_SEGMENT - 1;
                               1589                 :                : 
 1184 noah@leadboat.com        1590         [ -  + ]:CBC      836179 :     Assert(segpage % SLRU_PAGES_PER_SEGMENT == 0);
                               1591                 :                : 
                               1592   [ +  +  +  + ]:         945707 :     return (ctl->PagePrecedes(segpage, cutoffPage) &&
                               1593                 :         109528 :             ctl->PagePrecedes(seg_last_page, cutoffPage));
                               1594                 :                : }
                               1595                 :                : 
                               1596                 :                : #ifdef USE_ASSERT_CHECKING
                               1597                 :                : static void
                               1598                 :          13470 : SlruPagePrecedesTestOffset(SlruCtl ctl, int per_page, uint32 offset)
                               1599                 :                : {
                               1600                 :                :     TransactionId lhs,
                               1601                 :                :                 rhs;
                               1602                 :                :     int64       newestPage,
                               1603                 :                :                 oldestPage;
                               1604                 :                :     TransactionId newestXact,
                               1605                 :                :                 oldestXact;
                               1606                 :                : 
                               1607                 :                :     /*
                               1608                 :                :      * Compare an XID pair having undefined order (see RFC 1982), a pair at
                               1609                 :                :      * "opposite ends" of the XID space.  TransactionIdPrecedes() treats each
                               1610                 :                :      * as preceding the other.  If RHS is oldestXact, LHS is the first XID we
                               1611                 :                :      * must not assign.
                               1612                 :                :      */
                               1613                 :          13470 :     lhs = per_page + offset;    /* skip first page to avoid non-normal XIDs */
                               1614                 :          13470 :     rhs = lhs + (1U << 31);
                               1615         [ -  + ]:          13470 :     Assert(TransactionIdPrecedes(lhs, rhs));
                               1616         [ -  + ]:          13470 :     Assert(TransactionIdPrecedes(rhs, lhs));
                               1617         [ -  + ]:          13470 :     Assert(!TransactionIdPrecedes(lhs - 1, rhs));
                               1618         [ -  + ]:          13470 :     Assert(TransactionIdPrecedes(rhs, lhs - 1));
                               1619         [ -  + ]:          13470 :     Assert(TransactionIdPrecedes(lhs + 1, rhs));
                               1620         [ -  + ]:          13470 :     Assert(!TransactionIdPrecedes(rhs, lhs + 1));
                               1621         [ -  + ]:          13470 :     Assert(!TransactionIdFollowsOrEquals(lhs, rhs));
                               1622         [ -  + ]:          13470 :     Assert(!TransactionIdFollowsOrEquals(rhs, lhs));
                               1623         [ -  + ]:          13470 :     Assert(!ctl->PagePrecedes(lhs / per_page, lhs / per_page));
                               1624         [ -  + ]:          13470 :     Assert(!ctl->PagePrecedes(lhs / per_page, rhs / per_page));
                               1625         [ -  + ]:          13470 :     Assert(!ctl->PagePrecedes(rhs / per_page, lhs / per_page));
                               1626         [ -  + ]:          13470 :     Assert(!ctl->PagePrecedes((lhs - per_page) / per_page, rhs / per_page));
                               1627         [ -  + ]:          13470 :     Assert(ctl->PagePrecedes(rhs / per_page, (lhs - 3 * per_page) / per_page));
                               1628         [ -  + ]:          13470 :     Assert(ctl->PagePrecedes(rhs / per_page, (lhs - 2 * per_page) / per_page));
                               1629   [ +  +  -  + ]:          13470 :     Assert(ctl->PagePrecedes(rhs / per_page, (lhs - 1 * per_page) / per_page)
                               1630                 :                :            || (1U << 31) % per_page != 0);    /* See CommitTsPagePrecedes() */
                               1631   [ +  +  -  + ]:          13470 :     Assert(ctl->PagePrecedes((lhs + 1 * per_page) / per_page, rhs / per_page)
                               1632                 :                :            || (1U << 31) % per_page != 0);
                               1633         [ -  + ]:          13470 :     Assert(ctl->PagePrecedes((lhs + 2 * per_page) / per_page, rhs / per_page));
                               1634         [ -  + ]:          13470 :     Assert(ctl->PagePrecedes((lhs + 3 * per_page) / per_page, rhs / per_page));
                               1635         [ -  + ]:          13470 :     Assert(!ctl->PagePrecedes(rhs / per_page, (lhs + per_page) / per_page));
                               1636                 :                : 
                               1637                 :                :     /*
                               1638                 :                :      * GetNewTransactionId() has assigned the last XID it can safely use, and
                               1639                 :                :      * that XID is in the *LAST* page of the second segment.  We must not
                               1640                 :                :      * delete that segment.
                               1641                 :                :      */
                               1642                 :          13470 :     newestPage = 2 * SLRU_PAGES_PER_SEGMENT - 1;
                               1643                 :          13470 :     newestXact = newestPage * per_page + offset;
                               1644         [ -  + ]:          13470 :     Assert(newestXact / per_page == newestPage);
                               1645                 :          13470 :     oldestXact = newestXact + 1;
                               1646                 :          13470 :     oldestXact -= 1U << 31;
                               1647                 :          13470 :     oldestPage = oldestXact / per_page;
                               1648         [ -  + ]:          13470 :     Assert(!SlruMayDeleteSegment(ctl,
                               1649                 :                :                                  (newestPage -
                               1650                 :                :                                   newestPage % SLRU_PAGES_PER_SEGMENT),
                               1651                 :                :                                  oldestPage));
                               1652                 :                : 
                               1653                 :                :     /*
                               1654                 :                :      * GetNewTransactionId() has assigned the last XID it can safely use, and
                               1655                 :                :      * that XID is in the *FIRST* page of the second segment.  We must not
                               1656                 :                :      * delete that segment.
                               1657                 :                :      */
                               1658                 :          13470 :     newestPage = SLRU_PAGES_PER_SEGMENT;
                               1659                 :          13470 :     newestXact = newestPage * per_page + offset;
                               1660         [ -  + ]:          13470 :     Assert(newestXact / per_page == newestPage);
                               1661                 :          13470 :     oldestXact = newestXact + 1;
                               1662                 :          13470 :     oldestXact -= 1U << 31;
                               1663                 :          13470 :     oldestPage = oldestXact / per_page;
                               1664         [ -  + ]:          13470 :     Assert(!SlruMayDeleteSegment(ctl,
                               1665                 :                :                                  (newestPage -
                               1666                 :                :                                   newestPage % SLRU_PAGES_PER_SEGMENT),
                               1667                 :                :                                  oldestPage));
                               1668                 :          13470 : }
                               1669                 :                : 
                               1670                 :                : /*
                               1671                 :                :  * Unit-test a PagePrecedes function.
                               1672                 :                :  *
                               1673                 :                :  * This assumes every uint32 >= FirstNormalTransactionId is a valid key.  It
                               1674                 :                :  * assumes each value occupies a contiguous, fixed-size region of SLRU bytes.
                               1675                 :                :  * (MultiXactMemberCtl separates flags from XIDs.  NotifyCtl has
                               1676                 :                :  * variable-length entries, no keys, and no random access.  These unit tests
                               1677                 :                :  * do not apply to them.)
                               1678                 :                :  */
                               1679                 :                : void
                               1680                 :           4490 : SlruPagePrecedesUnitTests(SlruCtl ctl, int per_page)
                               1681                 :                : {
                               1682                 :                :     /* Test first, middle and last entries of a page. */
                               1683                 :           4490 :     SlruPagePrecedesTestOffset(ctl, per_page, 0);
                               1684                 :           4490 :     SlruPagePrecedesTestOffset(ctl, per_page, per_page / 2);
                               1685                 :           4490 :     SlruPagePrecedesTestOffset(ctl, per_page, per_page - 1);
                               1686                 :           4490 : }
                               1687                 :                : #endif
                               1688                 :                : 
                               1689                 :                : /*
                               1690                 :                :  * SlruScanDirectory callback
                               1691                 :                :  *      This callback reports true if there's any segment wholly prior to the
                               1692                 :                :  *      one containing the page passed as "data".
                               1693                 :                :  */
                               1694                 :                : bool
  137 akorotkov@postgresql     1695                 :GNC      664249 : SlruScanDirCbReportPresence(SlruCtl ctl, char *filename, int64 segpage,
                               1696                 :                :                             void *data)
                               1697                 :                : {
                               1698                 :         664249 :     int64       cutoffPage = *(int64 *) data;
                               1699                 :                : 
 1184 noah@leadboat.com        1700         [ +  + ]:CBC      664249 :     if (SlruMayDeleteSegment(ctl, segpage, cutoffPage))
 4326 bruce@momjian.us         1701                 :GBC          51 :         return true;            /* found one; don't iterate any more */
                               1702                 :                : 
 4326 bruce@momjian.us         1703                 :CBC      664198 :     return false;               /* keep going */
                               1704                 :                : }
                               1705                 :                : 
                               1706                 :                : /*
                               1707                 :                :  * SlruScanDirectory callback.
                               1708                 :                :  *      This callback deletes segments prior to the one passed in as "data".
                               1709                 :                :  */
                               1710                 :                : static bool
  137 akorotkov@postgresql     1711                 :GNC      144990 : SlruScanDirCbDeleteCutoff(SlruCtl ctl, char *filename, int64 segpage,
                               1712                 :                :                           void *data)
                               1713                 :                : {
                               1714                 :         144990 :     int64       cutoffPage = *(int64 *) data;
                               1715                 :                : 
 1184 noah@leadboat.com        1716         [ +  + ]:CBC      144990 :     if (SlruMayDeleteSegment(ctl, segpage, cutoffPage))
 1256 tmunro@postgresql.or     1717                 :         109273 :         SlruInternalDeleteSegment(ctl, segpage / SLRU_PAGES_PER_SEGMENT);
                               1718                 :                : 
 4326 bruce@momjian.us         1719                 :         144990 :     return false;               /* keep going */
                               1720                 :                : }
                               1721                 :                : 
                               1722                 :                : /*
                               1723                 :                :  * SlruScanDirectory callback.
                               1724                 :                :  *      This callback deletes all segments.
                               1725                 :                :  */
                               1726                 :                : bool
  137 akorotkov@postgresql     1727                 :GNC           7 : SlruScanDirCbDeleteAll(SlruCtl ctl, char *filename, int64 segpage, void *data)
                               1728                 :                : {
 1256 tmunro@postgresql.or     1729                 :CBC           7 :     SlruInternalDeleteSegment(ctl, segpage / SLRU_PAGES_PER_SEGMENT);
                               1730                 :                : 
 4326 bruce@momjian.us         1731                 :              7 :     return false;               /* keep going */
                               1732                 :                : }
                               1733                 :                : 
                               1734                 :                : /*
                               1735                 :                :  * An internal function used by SlruScanDirectory().
                               1736                 :                :  *
                               1737                 :                :  * Returns true if a file with a name of a given length may be a correct
                               1738                 :                :  * SLRU segment.
                               1739                 :                :  */
                               1740                 :                : static inline bool
  137 akorotkov@postgresql     1741                 :GNC      817634 : SlruCorrectSegmentFilenameLength(SlruCtl ctl, size_t len)
                               1742                 :                : {
                               1743         [ +  + ]:         817634 :     if (ctl->long_segment_names)
                               1744                 :           1808 :         return (len == 15);     /* see SlruFileName() */
                               1745                 :                :     else
                               1746                 :                : 
                               1747                 :                :         /*
                               1748                 :                :          * Commit 638cf09e76d allowed 5-character lengths. Later commit
                               1749                 :                :          * 73c986adde5 allowed 6-character length.
                               1750                 :                :          *
                               1751                 :                :          * Note: There is an ongoing plan to migrate all SLRUs to 64-bit page
                               1752                 :                :          * numbers, and the corresponding 15-character file names, which may
                               1753                 :                :          * eventually deprecate the support for 4, 5, and 6-character names.
                               1754                 :                :          */
                               1755   [ +  +  +  -  :         815826 :         return (len == 4 || len == 5 || len == 6);
                                              -  + ]
                               1756                 :                : }
                               1757                 :                : 
                               1758                 :                : /*
                               1759                 :                :  * Scan the SimpleLru directory and apply a callback to each file found in it.
                               1760                 :                :  *
                               1761                 :                :  * If the callback returns true, the scan is stopped.  The last return value
                               1762                 :                :  * from the callback is returned.
                               1763                 :                :  *
                               1764                 :                :  * The callback receives the following arguments: 1. the SlruCtl struct for the
                               1765                 :                :  * slru being truncated; 2. the filename being considered; 3. the page number
                               1766                 :                :  * for the first page of that file; 4. a pointer to the opaque data given to us
                               1767                 :                :  * by the caller.
                               1768                 :                :  *
                               1769                 :                :  * Note that the ordering in which the directory is scanned is not guaranteed.
                               1770                 :                :  *
                               1771                 :                :  * Note that no locking is applied.
                               1772                 :                :  */
                               1773                 :                : bool
 4582 alvherre@alvh.no-ip.     1774                 :CBC        4194 : SlruScanDirectory(SlruCtl ctl, SlruScanCallback callback, void *data)
                               1775                 :                : {
 4576 tgl@sss.pgh.pa.us        1776                 :           4194 :     bool        retval = false;
                               1777                 :                :     DIR        *cldir;
                               1778                 :                :     struct dirent *clde;
                               1779                 :                :     int64       segno;
                               1780                 :                :     int64       segpage;
                               1781                 :                : 
 7356                          1782                 :           4194 :     cldir = AllocateDir(ctl->Dir);
 6874                          1783         [ +  + ]:         821777 :     while ((clde = ReadDir(cldir, ctl->Dir)) != NULL)
                               1784                 :                :     {
                               1785                 :                :         size_t      len;
                               1786                 :                : 
 3755 alvherre@alvh.no-ip.     1787                 :         817634 :         len = strlen(clde->d_name);
                               1788                 :                : 
  137 akorotkov@postgresql     1789         [ +  + ]:GNC      817634 :         if (SlruCorrectSegmentFilenameLength(ctl, len) &&
 3755 alvherre@alvh.no-ip.     1790         [ +  - ]:CBC      809246 :             strspn(clde->d_name, "0123456789ABCDEF") == len)
                               1791                 :                :         {
  137 akorotkov@postgresql     1792                 :GNC      809246 :             segno = strtoi64(clde->d_name, NULL, 16);
 7613 bruce@momjian.us         1793                 :CBC      809246 :             segpage = segno * SLRU_PAGES_PER_SEGMENT;
                               1794                 :                : 
 4582 alvherre@alvh.no-ip.     1795         [ +  + ]:         809246 :             elog(DEBUG2, "SlruScanDirectory invoking callback on %s/%s",
                               1796                 :                :                  ctl->Dir, clde->d_name);
                               1797                 :         809246 :             retval = callback(ctl, clde->d_name, segpage, data);
                               1798         [ +  + ]:         809246 :             if (retval)
 4582 alvherre@alvh.no-ip.     1799                 :GBC          51 :                 break;
                               1800                 :                :         }
                               1801                 :                :     }
 7356 tgl@sss.pgh.pa.us        1802                 :CBC        4194 :     FreeDir(cldir);
                               1803                 :                : 
 4582 alvherre@alvh.no-ip.     1804                 :           4194 :     return retval;
                               1805                 :                : }
                               1806                 :                : 
                               1807                 :                : /*
                               1808                 :                :  * Individual SLRUs (clog, ...) have to provide a sync.c handler function so
                               1809                 :                :  * that they can provide the correct "SlruCtl" (otherwise we don't know how to
                               1810                 :                :  * build the path), but they just forward to this common implementation that
                               1811                 :                :  * performs the fsync.
                               1812                 :                :  */
                               1813                 :                : int
 1297 tmunro@postgresql.or     1814                 :              2 : SlruSyncFileTag(SlruCtl ctl, const FileTag *ftag, char *path)
                               1815                 :                : {
                               1816                 :                :     int         fd;
                               1817                 :                :     int         save_errno;
                               1818                 :                :     int         result;
                               1819                 :                : 
                               1820                 :              2 :     SlruFileName(ctl, path, ftag->segno);
                               1821                 :                : 
                               1822                 :              2 :     fd = OpenTransientFile(path, O_RDWR | PG_BINARY);
                               1823         [ -  + ]:              2 :     if (fd < 0)
 1297 tmunro@postgresql.or     1824                 :UBC           0 :         return -1;
                               1825                 :                : 
  354 michael@paquier.xyz      1826                 :CBC           2 :     pgstat_report_wait_start(WAIT_EVENT_SLRU_FLUSH_SYNC);
 1297 tmunro@postgresql.or     1827                 :              2 :     result = pg_fsync(fd);
  354 michael@paquier.xyz      1828                 :              2 :     pgstat_report_wait_end();
 1297 tmunro@postgresql.or     1829                 :              2 :     save_errno = errno;
                               1830                 :                : 
                               1831                 :              2 :     CloseTransientFile(fd);
                               1832                 :                : 
                               1833                 :              2 :     errno = save_errno;
                               1834                 :              2 :     return result;
                               1835                 :                : }
        

Generated by: LCOV version 2.1-beta2-3-g6141622