LCOV - differential code coverage report
Current view: top level - src/backend/access/transam - slru.c (source / functions) Coverage Total Hit UNC UIC UBC GBC GIC GNC CBC DUB DCB
Current: bed3ffbf9d952be6c7d739d068cdce44c046dfb7 vs 574581b50ac9c63dd9e4abebb731a3b67e5b50f6 Lines: 81.9 % 625 512 15 98 15 135 362 10 107
Current Date: 2026-05-05 10:23:31 +0900 Functions: 97.1 % 34 33 1 29 4 1 26
Baseline: lcov-20260505-025707-baseline Branches: 55.3 % 447 247 19 15 166 10 1 53 183 19 45
Baseline Date: 2026-05-05 10:27:06 +0900 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(7,30] days: 92.3 % 130 120 10 119 1
(30,360] days: 80.0 % 20 16 4 16
(360..) days: 79.2 % 475 376 1 98 15 361
Function coverage date bins:
(7,30] days: 96.7 % 30 29 1 29
(360..) days: 100.0 % 4 4 4
Branch coverage date bins:
(7,30] days: 73.5 % 68 50 18 50
(30,360] days: 75.0 % 4 3 1 3
(360..) days: 51.7 % 375 194 15 166 10 1 183

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

Generated by: LCOV version 2.5.0-beta