LCOV - code coverage report
Current view: top level - src/backend/access/transam - slru.c (source / functions) Hit Total Coverage
Test: PostgreSQL 13beta1 Lines: 285 457 62.4 %
Date: 2020-05-25 05:06:35 Functions: 19 22 86.4 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*-------------------------------------------------------------------------
       2             :  *
       3             :  * slru.c
       4             :  *      Simple LRU buffering for transaction status logfiles
       5             :  *
       6             :  * We use a simple least-recently-used scheme to manage a pool of page
       7             :  * buffers.  Under ordinary circumstances we expect that write
       8             :  * traffic will occur mostly to the latest page (and to the just-prior
       9             :  * page, soon after a page transition).  Read traffic will probably touch
      10             :  * a larger span of pages, but in any case a fairly small number of page
      11             :  * buffers should be sufficient.  So, we just search the buffers using plain
      12             :  * linear search; there's no need for a hashtable or anything fancy.
      13             :  * The management algorithm is straight LRU except that we will never swap
      14             :  * out the latest page (since we know it's going to be hit again eventually).
      15             :  *
      16             :  * We use a control LWLock to protect the shared data structures, plus
      17             :  * per-buffer LWLocks that synchronize I/O for each buffer.  The control lock
      18             :  * must be held to examine or modify any shared state.  A process that is
      19             :  * reading in or writing out a page buffer does not hold the control lock,
      20             :  * only the per-buffer lock for the buffer it is working on.
      21             :  *
      22             :  * "Holding the control lock" means exclusive lock in all cases except for
      23             :  * SimpleLruReadPage_ReadOnly(); see comments for SlruRecentlyUsed() for
      24             :  * the implications of that.
      25             :  *
      26             :  * When initiating I/O on a buffer, we acquire the per-buffer lock exclusively
      27             :  * before releasing the control lock.  The per-buffer lock is released after
      28             :  * completing the I/O, re-acquiring the control lock, and updating the shared
      29             :  * state.  (Deadlock is not possible here, because we never try to initiate
      30             :  * I/O when someone else is already doing I/O on the same buffer.)
      31             :  * To wait for I/O to complete, release the control lock, acquire the
      32             :  * per-buffer lock in shared mode, immediately release the per-buffer lock,
      33             :  * reacquire the control lock, and then recheck state (since arbitrary things
      34             :  * could have happened while we didn't have the lock).
      35             :  *
      36             :  * As with the regular buffer manager, it is possible for another process
      37             :  * to re-dirty a page that is currently being written out.  This is handled
      38             :  * by re-setting the page's page_dirty flag.
      39             :  *
      40             :  *
      41             :  * Portions Copyright (c) 1996-2020, PostgreSQL Global Development Group
      42             :  * Portions Copyright (c) 1994, Regents of the University of California
      43             :  *
      44             :  * src/backend/access/transam/slru.c
      45             :  *
      46             :  *-------------------------------------------------------------------------
      47             :  */
      48             : #include "postgres.h"
      49             : 
      50             : #include <fcntl.h>
      51             : #include <sys/stat.h>
      52             : #include <unistd.h>
      53             : 
      54             : #include "access/slru.h"
      55             : #include "access/transam.h"
      56             : #include "access/xlog.h"
      57             : #include "miscadmin.h"
      58             : #include "pgstat.h"
      59             : #include "storage/fd.h"
      60             : #include "storage/shmem.h"
      61             : 
      62             : #define SlruFileName(ctl, path, seg) \
      63             :     snprintf(path, MAXPGPATH, "%s/%04X", (ctl)->Dir, seg)
      64             : 
      65             : /*
      66             :  * During SimpleLruFlush(), we will usually not need to write/fsync more
      67             :  * than one or two physical files, but we may need to write several pages
      68             :  * per file.  We can consolidate the I/O requests by leaving files open
      69             :  * until control returns to SimpleLruFlush().  This data structure remembers
      70             :  * which files are open.
      71             :  */
      72             : #define MAX_FLUSH_BUFFERS   16
      73             : 
      74             : typedef struct SlruFlushData
      75             : {
      76             :     int         num_files;      /* # files actually open */
      77             :     int         fd[MAX_FLUSH_BUFFERS];  /* their FD's */
      78             :     int         segno[MAX_FLUSH_BUFFERS];   /* their log seg#s */
      79             : } SlruFlushData;
      80             : 
      81             : typedef struct SlruFlushData *SlruFlush;
      82             : 
      83             : /*
      84             :  * Macro to mark a buffer slot "most recently used".  Note multiple evaluation
      85             :  * of arguments!
      86             :  *
      87             :  * The reason for the if-test is that there are often many consecutive
      88             :  * accesses to the same page (particularly the latest page).  By suppressing
      89             :  * useless increments of cur_lru_count, we reduce the probability that old
      90             :  * pages' counts will "wrap around" and make them appear recently used.
      91             :  *
      92             :  * We allow this code to be executed concurrently by multiple processes within
      93             :  * SimpleLruReadPage_ReadOnly().  As long as int reads and writes are atomic,
      94             :  * this should not cause any completely-bogus values to enter the computation.
      95             :  * However, it is possible for either cur_lru_count or individual
      96             :  * page_lru_count entries to be "reset" to lower values than they should have,
      97             :  * in case a process is delayed while it executes this macro.  With care in
      98             :  * SlruSelectLRUPage(), this does little harm, and in any case the absolute
      99             :  * worst possible consequence is a nonoptimal choice of page to evict.  The
     100             :  * gain from allowing concurrent reads of SLRU pages seems worth it.
     101             :  */
     102             : #define SlruRecentlyUsed(shared, slotno)    \
     103             :     do { \
     104             :         int     new_lru_count = (shared)->cur_lru_count; \
     105             :         if (new_lru_count != (shared)->page_lru_count[slotno]) { \
     106             :             (shared)->cur_lru_count = ++new_lru_count; \
     107             :             (shared)->page_lru_count[slotno] = new_lru_count; \
     108             :         } \
     109             :     } while (0)
     110             : 
     111             : /* Saved info for SlruReportIOError */
     112             : typedef enum
     113             : {
     114             :     SLRU_OPEN_FAILED,
     115             :     SLRU_SEEK_FAILED,
     116             :     SLRU_READ_FAILED,
     117             :     SLRU_WRITE_FAILED,
     118             :     SLRU_FSYNC_FAILED,
     119             :     SLRU_CLOSE_FAILED
     120             : } SlruErrorCause;
     121             : 
     122             : static SlruErrorCause slru_errcause;
     123             : static int  slru_errno;
     124             : 
     125             : 
     126             : static void SimpleLruZeroLSNs(SlruCtl ctl, int slotno);
     127             : static void SimpleLruWaitIO(SlruCtl ctl, int slotno);
     128             : static void SlruInternalWritePage(SlruCtl ctl, int slotno, SlruFlush fdata);
     129             : static bool SlruPhysicalReadPage(SlruCtl ctl, int pageno, int slotno);
     130             : static bool SlruPhysicalWritePage(SlruCtl ctl, int pageno, int slotno,
     131             :                                   SlruFlush fdata);
     132             : static void SlruReportIOError(SlruCtl ctl, int pageno, TransactionId xid);
     133             : static int  SlruSelectLRUPage(SlruCtl ctl, int pageno);
     134             : 
     135             : static bool SlruScanDirCbDeleteCutoff(SlruCtl ctl, char *filename,
     136             :                                       int segpage, void *data);
     137             : static void SlruInternalDeleteSegment(SlruCtl ctl, char *filename);
     138             : 
     139             : /*
     140             :  * Initialization of shared memory
     141             :  */
     142             : 
     143             : Size
     144       30408 : SimpleLruShmemSize(int nslots, int nlsns)
     145             : {
     146             :     Size        sz;
     147             : 
     148             :     /* we assume nslots isn't so large as to risk overflow */
     149       30408 :     sz = MAXALIGN(sizeof(SlruSharedData));
     150       30408 :     sz += MAXALIGN(nslots * sizeof(char *));    /* page_buffer[] */
     151       30408 :     sz += MAXALIGN(nslots * sizeof(SlruPageStatus));    /* page_status[] */
     152       30408 :     sz += MAXALIGN(nslots * sizeof(bool));  /* page_dirty[] */
     153       30408 :     sz += MAXALIGN(nslots * sizeof(int));   /* page_number[] */
     154       30408 :     sz += MAXALIGN(nslots * sizeof(int));   /* page_lru_count[] */
     155       30408 :     sz += MAXALIGN(nslots * sizeof(LWLockPadded));  /* buffer_locks[] */
     156             : 
     157       30408 :     if (nlsns > 0)
     158        4344 :         sz += MAXALIGN(nslots * nlsns * sizeof(XLogRecPtr));    /* group_lsn[] */
     159             : 
     160       30408 :     return BUFFERALIGN(sz) + BLCKSZ * nslots;
     161             : }
     162             : 
     163             : /*
     164             :  * Initialize, or attach to, a simple LRU cache in shared memory.
     165             :  *
     166             :  * ctl: address of local (unshared) control structure.
     167             :  * name: name of SLRU.  (This is user-visible, pick with care!)
     168             :  * nslots: number of page slots to use.
     169             :  * nlsns: number of LSN groups per page (set to zero if not relevant).
     170             :  * ctllock: LWLock to use to control access to the shared control structure.
     171             :  * subdir: PGDATA-relative subdirectory that will contain the files.
     172             :  * tranche_id: LWLock tranche ID to use for the SLRU's per-buffer LWLocks.
     173             :  */
     174             : void
     175       15190 : SimpleLruInit(SlruCtl ctl, const char *name, int nslots, int nlsns,
     176             :               LWLock *ctllock, const char *subdir, int tranche_id)
     177             : {
     178             :     SlruShared  shared;
     179             :     bool        found;
     180             : 
     181       15190 :     shared = (SlruShared) ShmemInitStruct(name,
     182             :                                           SimpleLruShmemSize(nslots, nlsns),
     183             :                                           &found);
     184             : 
     185       15190 :     if (!IsUnderPostmaster)
     186             :     {
     187             :         /* Initialize locks and shared memory area */
     188             :         char       *ptr;
     189             :         Size        offset;
     190             :         int         slotno;
     191             : 
     192             :         Assert(!found);
     193             : 
     194       15190 :         memset(shared, 0, sizeof(SlruSharedData));
     195             : 
     196       15190 :         shared->ControlLock = ctllock;
     197             : 
     198       15190 :         shared->num_slots = nslots;
     199       15190 :         shared->lsn_groups_per_page = nlsns;
     200             : 
     201       15190 :         shared->cur_lru_count = 0;
     202             : 
     203             :         /* shared->latest_page_number will be set later */
     204             : 
     205       15190 :         shared->slru_stats_idx = pgstat_slru_index(name);
     206             : 
     207       15190 :         ptr = (char *) shared;
     208       15190 :         offset = MAXALIGN(sizeof(SlruSharedData));
     209       15190 :         shared->page_buffer = (char **) (ptr + offset);
     210       15190 :         offset += MAXALIGN(nslots * sizeof(char *));
     211       15190 :         shared->page_status = (SlruPageStatus *) (ptr + offset);
     212       15190 :         offset += MAXALIGN(nslots * sizeof(SlruPageStatus));
     213       15190 :         shared->page_dirty = (bool *) (ptr + offset);
     214       15190 :         offset += MAXALIGN(nslots * sizeof(bool));
     215       15190 :         shared->page_number = (int *) (ptr + offset);
     216       15190 :         offset += MAXALIGN(nslots * sizeof(int));
     217       15190 :         shared->page_lru_count = (int *) (ptr + offset);
     218       15190 :         offset += MAXALIGN(nslots * sizeof(int));
     219             : 
     220             :         /* Initialize LWLocks */
     221       15190 :         shared->buffer_locks = (LWLockPadded *) (ptr + offset);
     222       15190 :         offset += MAXALIGN(nslots * sizeof(LWLockPadded));
     223             : 
     224       15190 :         if (nlsns > 0)
     225             :         {
     226        2170 :             shared->group_lsn = (XLogRecPtr *) (ptr + offset);
     227        2170 :             offset += MAXALIGN(nslots * nlsns * sizeof(XLogRecPtr));
     228             :         }
     229             : 
     230       15190 :         ptr += BUFFERALIGN(offset);
     231      264150 :         for (slotno = 0; slotno < nslots; slotno++)
     232             :         {
     233      248960 :             LWLockInitialize(&shared->buffer_locks[slotno].lock,
     234             :                              tranche_id);
     235             : 
     236      248960 :             shared->page_buffer[slotno] = ptr;
     237      248960 :             shared->page_status[slotno] = SLRU_PAGE_EMPTY;
     238      248960 :             shared->page_dirty[slotno] = false;
     239      248960 :             shared->page_lru_count[slotno] = 0;
     240      248960 :             ptr += BLCKSZ;
     241             :         }
     242             : 
     243             :         /* Should fit to estimated shmem size */
     244             :         Assert(ptr - (char *) shared <= SimpleLruShmemSize(nslots, nlsns));
     245             :     }
     246             :     else
     247             :         Assert(found);
     248             : 
     249             :     /*
     250             :      * Initialize the unshared control struct, including directory path. We
     251             :      * assume caller set PagePrecedes.
     252             :      */
     253       15190 :     ctl->shared = shared;
     254       15190 :     ctl->do_fsync = true;        /* default behavior */
     255       15190 :     StrNCpy(ctl->Dir, subdir, sizeof(ctl->Dir));
     256       15190 : }
     257             : 
     258             : /*
     259             :  * Initialize (or reinitialize) a page to zeroes.
     260             :  *
     261             :  * The page is not actually written, just set up in shared memory.
     262             :  * The slot number of the new page is returned.
     263             :  *
     264             :  * Control lock must be held at entry, and will be held at exit.
     265             :  */
     266             : int
     267        3628 : SimpleLruZeroPage(SlruCtl ctl, int pageno)
     268             : {
     269        3628 :     SlruShared  shared = ctl->shared;
     270             :     int         slotno;
     271             : 
     272             :     /* Find a suitable buffer slot for the page */
     273        3628 :     slotno = SlruSelectLRUPage(ctl, pageno);
     274             :     Assert(shared->page_status[slotno] == SLRU_PAGE_EMPTY ||
     275             :            (shared->page_status[slotno] == SLRU_PAGE_VALID &&
     276             :             !shared->page_dirty[slotno]) ||
     277             :            shared->page_number[slotno] == pageno);
     278             : 
     279             :     /* Mark the slot as containing this page */
     280        3628 :     shared->page_number[slotno] = pageno;
     281        3628 :     shared->page_status[slotno] = SLRU_PAGE_VALID;
     282        3628 :     shared->page_dirty[slotno] = true;
     283        3628 :     SlruRecentlyUsed(shared, slotno);
     284             : 
     285             :     /* Set the buffer to zeroes */
     286        3628 :     MemSet(shared->page_buffer[slotno], 0, BLCKSZ);
     287             : 
     288             :     /* Set the LSNs for this new page to zero */
     289        3628 :     SimpleLruZeroLSNs(ctl, slotno);
     290             : 
     291             :     /* Assume this page is now the latest active page */
     292        3628 :     shared->latest_page_number = pageno;
     293             : 
     294             :     /* update the stats counter of zeroed pages */
     295        3628 :     pgstat_count_slru_page_zeroed(shared->slru_stats_idx);
     296             : 
     297        3628 :     return slotno;
     298             : }
     299             : 
     300             : /*
     301             :  * Zero all the LSNs we store for this slru page.
     302             :  *
     303             :  * This should be called each time we create a new page, and each time we read
     304             :  * in a page from disk into an existing buffer.  (Such an old page cannot
     305             :  * have any interesting LSNs, since we'd have flushed them before writing
     306             :  * the page in the first place.)
     307             :  *
     308             :  * This assumes that InvalidXLogRecPtr is bitwise-all-0.
     309             :  */
     310             : static void
     311        5670 : SimpleLruZeroLSNs(SlruCtl ctl, int slotno)
     312             : {
     313        5670 :     SlruShared  shared = ctl->shared;
     314             : 
     315        5670 :     if (shared->lsn_groups_per_page > 0)
     316        1742 :         MemSet(&shared->group_lsn[slotno * shared->lsn_groups_per_page], 0,
     317             :                shared->lsn_groups_per_page * sizeof(XLogRecPtr));
     318        5670 : }
     319             : 
     320             : /*
     321             :  * Wait for any active I/O on a page slot to finish.  (This does not
     322             :  * guarantee that new I/O hasn't been started before we return, though.
     323             :  * In fact the slot might not even contain the same page anymore.)
     324             :  *
     325             :  * Control lock must be held at entry, and will be held at exit.
     326             :  */
     327             : static void
     328           0 : SimpleLruWaitIO(SlruCtl ctl, int slotno)
     329             : {
     330           0 :     SlruShared  shared = ctl->shared;
     331             : 
     332             :     /* See notes at top of file */
     333           0 :     LWLockRelease(shared->ControlLock);
     334           0 :     LWLockAcquire(&shared->buffer_locks[slotno].lock, LW_SHARED);
     335           0 :     LWLockRelease(&shared->buffer_locks[slotno].lock);
     336           0 :     LWLockAcquire(shared->ControlLock, LW_EXCLUSIVE);
     337             : 
     338             :     /*
     339             :      * If the slot is still in an io-in-progress state, then either someone
     340             :      * already started a new I/O on the slot, or a previous I/O failed and
     341             :      * neglected to reset the page state.  That shouldn't happen, really, but
     342             :      * it seems worth a few extra cycles to check and recover from it. We can
     343             :      * cheaply test for failure by seeing if the buffer lock is still held (we
     344             :      * assume that transaction abort would release the lock).
     345             :      */
     346           0 :     if (shared->page_status[slotno] == SLRU_PAGE_READ_IN_PROGRESS ||
     347           0 :         shared->page_status[slotno] == SLRU_PAGE_WRITE_IN_PROGRESS)
     348             :     {
     349           0 :         if (LWLockConditionalAcquire(&shared->buffer_locks[slotno].lock, LW_SHARED))
     350             :         {
     351             :             /* indeed, the I/O must have failed */
     352           0 :             if (shared->page_status[slotno] == SLRU_PAGE_READ_IN_PROGRESS)
     353           0 :                 shared->page_status[slotno] = SLRU_PAGE_EMPTY;
     354             :             else                /* write_in_progress */
     355             :             {
     356           0 :                 shared->page_status[slotno] = SLRU_PAGE_VALID;
     357           0 :                 shared->page_dirty[slotno] = true;
     358             :             }
     359           0 :             LWLockRelease(&shared->buffer_locks[slotno].lock);
     360             :         }
     361             :     }
     362           0 : }
     363             : 
     364             : /*
     365             :  * Find a page in a shared buffer, reading it in if necessary.
     366             :  * The page number must correspond to an already-initialized page.
     367             :  *
     368             :  * If write_ok is true then it is OK to return a page that is in
     369             :  * WRITE_IN_PROGRESS state; it is the caller's responsibility to be sure
     370             :  * that modification of the page is safe.  If write_ok is false then we
     371             :  * will not return the page until it is not undergoing active I/O.
     372             :  *
     373             :  * The passed-in xid is used only for error reporting, and may be
     374             :  * InvalidTransactionId if no specific xid is associated with the action.
     375             :  *
     376             :  * Return value is the shared-buffer slot number now holding the page.
     377             :  * The buffer's LRU access info is updated.
     378             :  *
     379             :  * Control lock must be held at entry, and will be held at exit.
     380             :  */
     381             : int
     382      290476 : SimpleLruReadPage(SlruCtl ctl, int pageno, bool write_ok,
     383             :                   TransactionId xid)
     384             : {
     385      290476 :     SlruShared  shared = ctl->shared;
     386             : 
     387             :     /* Outer loop handles restart if we must wait for someone else's I/O */
     388             :     for (;;)
     389           0 :     {
     390             :         int         slotno;
     391             :         bool        ok;
     392             : 
     393             :         /* See if page already is in memory; if not, pick victim slot */
     394      290476 :         slotno = SlruSelectLRUPage(ctl, pageno);
     395             : 
     396             :         /* Did we find the page in memory? */
     397      290476 :         if (shared->page_number[slotno] == pageno &&
     398      290476 :             shared->page_status[slotno] != SLRU_PAGE_EMPTY)
     399             :         {
     400             :             /*
     401             :              * If page is still being read in, we must wait for I/O.  Likewise
     402             :              * if the page is being written and the caller said that's not OK.
     403             :              */
     404      288434 :             if (shared->page_status[slotno] == SLRU_PAGE_READ_IN_PROGRESS ||
     405      288434 :                 (shared->page_status[slotno] == SLRU_PAGE_WRITE_IN_PROGRESS &&
     406           0 :                  !write_ok))
     407             :             {
     408           0 :                 SimpleLruWaitIO(ctl, slotno);
     409             :                 /* Now we must recheck state from the top */
     410           0 :                 continue;
     411             :             }
     412             :             /* Otherwise, it's ready to use */
     413      288434 :             SlruRecentlyUsed(shared, slotno);
     414             : 
     415             :             /* update the stats counter of pages found in the SLRU */
     416      288434 :             pgstat_count_slru_page_hit(shared->slru_stats_idx);
     417             : 
     418      288434 :             return slotno;
     419             :         }
     420             : 
     421             :         /* We found no match; assert we selected a freeable slot */
     422             :         Assert(shared->page_status[slotno] == SLRU_PAGE_EMPTY ||
     423             :                (shared->page_status[slotno] == SLRU_PAGE_VALID &&
     424             :                 !shared->page_dirty[slotno]));
     425             : 
     426             :         /* Mark the slot read-busy */
     427        2042 :         shared->page_number[slotno] = pageno;
     428        2042 :         shared->page_status[slotno] = SLRU_PAGE_READ_IN_PROGRESS;
     429        2042 :         shared->page_dirty[slotno] = false;
     430             : 
     431             :         /* Acquire per-buffer lock (cannot deadlock, see notes at top) */
     432        2042 :         LWLockAcquire(&shared->buffer_locks[slotno].lock, LW_EXCLUSIVE);
     433             : 
     434             :         /* Release control lock while doing I/O */
     435        2042 :         LWLockRelease(shared->ControlLock);
     436             : 
     437             :         /* Do the read */
     438        2042 :         ok = SlruPhysicalReadPage(ctl, pageno, slotno);
     439             : 
     440             :         /* Set the LSNs for this newly read-in page to zero */
     441        2042 :         SimpleLruZeroLSNs(ctl, slotno);
     442             : 
     443             :         /* Re-acquire control lock and update page state */
     444        2042 :         LWLockAcquire(shared->ControlLock, LW_EXCLUSIVE);
     445             : 
     446             :         Assert(shared->page_number[slotno] == pageno &&
     447             :                shared->page_status[slotno] == SLRU_PAGE_READ_IN_PROGRESS &&
     448             :                !shared->page_dirty[slotno]);
     449             : 
     450        2042 :         shared->page_status[slotno] = ok ? SLRU_PAGE_VALID : SLRU_PAGE_EMPTY;
     451             : 
     452        2042 :         LWLockRelease(&shared->buffer_locks[slotno].lock);
     453             : 
     454             :         /* Now it's okay to ereport if we failed */
     455        2042 :         if (!ok)
     456           0 :             SlruReportIOError(ctl, pageno, xid);
     457             : 
     458        2042 :         SlruRecentlyUsed(shared, slotno);
     459             : 
     460             :         /* update the stats counter of pages not found in SLRU */
     461        2042 :         pgstat_count_slru_page_read(shared->slru_stats_idx);
     462             : 
     463        2042 :         return slotno;
     464             :     }
     465             : }
     466             : 
     467             : /*
     468             :  * Find a page in a shared buffer, reading it in if necessary.
     469             :  * The page number must correspond to an already-initialized page.
     470             :  * The caller must intend only read-only access to the page.
     471             :  *
     472             :  * The passed-in xid is used only for error reporting, and may be
     473             :  * InvalidTransactionId if no specific xid is associated with the action.
     474             :  *
     475             :  * Return value is the shared-buffer slot number now holding the page.
     476             :  * The buffer's LRU access info is updated.
     477             :  *
     478             :  * Control lock must NOT be held at entry, but will be held at exit.
     479             :  * It is unspecified whether the lock will be shared or exclusive.
     480             :  */
     481             : int
     482     1186378 : SimpleLruReadPage_ReadOnly(SlruCtl ctl, int pageno, TransactionId xid)
     483             : {
     484     1186378 :     SlruShared  shared = ctl->shared;
     485             :     int         slotno;
     486             : 
     487             :     /* Try to find the page while holding only shared lock */
     488     1186378 :     LWLockAcquire(shared->ControlLock, LW_SHARED);
     489             : 
     490             :     /* See if page is already in a buffer */
     491     1187154 :     for (slotno = 0; slotno < shared->num_slots; slotno++)
     492             :     {
     493     1187100 :         if (shared->page_number[slotno] == pageno &&
     494     1186540 :             shared->page_status[slotno] != SLRU_PAGE_EMPTY &&
     495     1186324 :             shared->page_status[slotno] != SLRU_PAGE_READ_IN_PROGRESS)
     496             :         {
     497             :             /* See comments for SlruRecentlyUsed macro */
     498     1186324 :             SlruRecentlyUsed(shared, slotno);
     499             : 
     500             :             /* update the stats counter of pages found in the SLRU */
     501     1186324 :             pgstat_count_slru_page_hit(shared->slru_stats_idx);
     502             : 
     503     1186324 :             return slotno;
     504             :         }
     505             :     }
     506             : 
     507             :     /* No luck, so switch to normal exclusive lock and do regular read */
     508          54 :     LWLockRelease(shared->ControlLock);
     509          54 :     LWLockAcquire(shared->ControlLock, LW_EXCLUSIVE);
     510             : 
     511          54 :     return SimpleLruReadPage(ctl, pageno, true, xid);
     512             : }
     513             : 
     514             : /*
     515             :  * Write a page from a shared buffer, if necessary.
     516             :  * Does nothing if the specified slot is not dirty.
     517             :  *
     518             :  * NOTE: only one write attempt is made here.  Hence, it is possible that
     519             :  * the page is still dirty at exit (if someone else re-dirtied it during
     520             :  * the write).  However, we *do* attempt a fresh write even if the page
     521             :  * is already being written; this is for checkpoints.
     522             :  *
     523             :  * Control lock must be held at entry, and will be held at exit.
     524             :  */
     525             : static void
     526      431238 : SlruInternalWritePage(SlruCtl ctl, int slotno, SlruFlush fdata)
     527             : {
     528      431238 :     SlruShared  shared = ctl->shared;
     529      431238 :     int         pageno = shared->page_number[slotno];
     530             :     bool        ok;
     531             : 
     532             :     /* If a write is in progress, wait for it to finish */
     533      431238 :     while (shared->page_status[slotno] == SLRU_PAGE_WRITE_IN_PROGRESS &&
     534           0 :            shared->page_number[slotno] == pageno)
     535             :     {
     536           0 :         SimpleLruWaitIO(ctl, slotno);
     537             :     }
     538             : 
     539             :     /*
     540             :      * Do nothing if page is not dirty, or if buffer no longer contains the
     541             :      * same page we were called for.
     542             :      */
     543      431238 :     if (!shared->page_dirty[slotno] ||
     544        5984 :         shared->page_status[slotno] != SLRU_PAGE_VALID ||
     545        5984 :         shared->page_number[slotno] != pageno)
     546      425254 :         return;
     547             : 
     548             :     /*
     549             :      * Mark the slot write-busy, and clear the dirtybit.  After this point, a
     550             :      * transaction status update on this page will mark it dirty again.
     551             :      */
     552        5984 :     shared->page_status[slotno] = SLRU_PAGE_WRITE_IN_PROGRESS;
     553        5984 :     shared->page_dirty[slotno] = false;
     554             : 
     555             :     /* Acquire per-buffer lock (cannot deadlock, see notes at top) */
     556        5984 :     LWLockAcquire(&shared->buffer_locks[slotno].lock, LW_EXCLUSIVE);
     557             : 
     558             :     /* Release control lock while doing I/O */
     559        5984 :     LWLockRelease(shared->ControlLock);
     560             : 
     561             :     /* Do the write */
     562        5984 :     ok = SlruPhysicalWritePage(ctl, pageno, slotno, fdata);
     563             : 
     564             :     /* If we failed, and we're in a flush, better close the files */
     565        5984 :     if (!ok && fdata)
     566             :     {
     567             :         int         i;
     568             : 
     569           0 :         for (i = 0; i < fdata->num_files; i++)
     570           0 :             CloseTransientFile(fdata->fd[i]);
     571             :     }
     572             : 
     573             :     /* Re-acquire control lock and update page state */
     574        5984 :     LWLockAcquire(shared->ControlLock, LW_EXCLUSIVE);
     575             : 
     576             :     Assert(shared->page_number[slotno] == pageno &&
     577             :            shared->page_status[slotno] == SLRU_PAGE_WRITE_IN_PROGRESS);
     578             : 
     579             :     /* If we failed to write, mark the page dirty again */
     580        5984 :     if (!ok)
     581           0 :         shared->page_dirty[slotno] = true;
     582             : 
     583        5984 :     shared->page_status[slotno] = SLRU_PAGE_VALID;
     584             : 
     585        5984 :     LWLockRelease(&shared->buffer_locks[slotno].lock);
     586             : 
     587             :     /* Now it's okay to ereport if we failed */
     588        5984 :     if (!ok)
     589           0 :         SlruReportIOError(ctl, pageno, InvalidTransactionId);
     590             : }
     591             : 
     592             : /*
     593             :  * Wrapper of SlruInternalWritePage, for external callers.
     594             :  * fdata is always passed a NULL here.
     595             :  */
     596             : void
     597        1446 : SimpleLruWritePage(SlruCtl ctl, int slotno)
     598             : {
     599        1446 :     SlruInternalWritePage(ctl, slotno, NULL);
     600        1446 : }
     601             : 
     602             : /*
     603             :  * Return whether the given page exists on disk.
     604             :  *
     605             :  * A false return means that either the file does not exist, or that it's not
     606             :  * large enough to contain the given page.
     607             :  */
     608             : bool
     609          50 : SimpleLruDoesPhysicalPageExist(SlruCtl ctl, int pageno)
     610             : {
     611          50 :     int         segno = pageno / SLRU_PAGES_PER_SEGMENT;
     612          50 :     int         rpageno = pageno % SLRU_PAGES_PER_SEGMENT;
     613          50 :     int         offset = rpageno * BLCKSZ;
     614             :     char        path[MAXPGPATH];
     615             :     int         fd;
     616             :     bool        result;
     617             :     off_t       endpos;
     618             : 
     619             :     /* update the stats counter of checked pages */
     620          50 :     pgstat_count_slru_page_exists(ctl->shared->slru_stats_idx);
     621             : 
     622          50 :     SlruFileName(ctl, path, segno);
     623             : 
     624          50 :     fd = OpenTransientFile(path, O_RDONLY | PG_BINARY);
     625          50 :     if (fd < 0)
     626             :     {
     627             :         /* expected: file doesn't exist */
     628          14 :         if (errno == ENOENT)
     629          14 :             return false;
     630             : 
     631             :         /* report error normally */
     632           0 :         slru_errcause = SLRU_OPEN_FAILED;
     633           0 :         slru_errno = errno;
     634           0 :         SlruReportIOError(ctl, pageno, 0);
     635             :     }
     636             : 
     637          36 :     if ((endpos = lseek(fd, 0, SEEK_END)) < 0)
     638             :     {
     639           0 :         slru_errcause = SLRU_SEEK_FAILED;
     640           0 :         slru_errno = errno;
     641           0 :         SlruReportIOError(ctl, pageno, 0);
     642             :     }
     643             : 
     644          36 :     result = endpos >= (off_t) (offset + BLCKSZ);
     645             : 
     646          36 :     if (CloseTransientFile(fd) != 0)
     647             :     {
     648           0 :         slru_errcause = SLRU_CLOSE_FAILED;
     649           0 :         slru_errno = errno;
     650           0 :         return false;
     651             :     }
     652             : 
     653          36 :     return result;
     654             : }
     655             : 
     656             : /*
     657             :  * Physical read of a (previously existing) page into a buffer slot
     658             :  *
     659             :  * On failure, we cannot just ereport(ERROR) since caller has put state in
     660             :  * shared memory that must be undone.  So, we return false and save enough
     661             :  * info in static variables to let SlruReportIOError make the report.
     662             :  *
     663             :  * For now, assume it's not worth keeping a file pointer open across
     664             :  * read/write operations.  We could cache one virtual file pointer ...
     665             :  */
     666             : static bool
     667        2042 : SlruPhysicalReadPage(SlruCtl ctl, int pageno, int slotno)
     668             : {
     669        2042 :     SlruShared  shared = ctl->shared;
     670        2042 :     int         segno = pageno / SLRU_PAGES_PER_SEGMENT;
     671        2042 :     int         rpageno = pageno % SLRU_PAGES_PER_SEGMENT;
     672        2042 :     int         offset = rpageno * BLCKSZ;
     673             :     char        path[MAXPGPATH];
     674             :     int         fd;
     675             : 
     676        2042 :     SlruFileName(ctl, path, segno);
     677             : 
     678             :     /*
     679             :      * In a crash-and-restart situation, it's possible for us to receive
     680             :      * commands to set the commit status of transactions whose bits are in
     681             :      * already-truncated segments of the commit log (see notes in
     682             :      * SlruPhysicalWritePage).  Hence, if we are InRecovery, allow the case
     683             :      * where the file doesn't exist, and return zeroes instead.
     684             :      */
     685        2042 :     fd = OpenTransientFile(path, O_RDONLY | PG_BINARY);
     686        2042 :     if (fd < 0)
     687             :     {
     688           0 :         if (errno != ENOENT || !InRecovery)
     689             :         {
     690           0 :             slru_errcause = SLRU_OPEN_FAILED;
     691           0 :             slru_errno = errno;
     692           0 :             return false;
     693             :         }
     694             : 
     695           0 :         ereport(LOG,
     696             :                 (errmsg("file \"%s\" doesn't exist, reading as zeroes",
     697             :                         path)));
     698           0 :         MemSet(shared->page_buffer[slotno], 0, BLCKSZ);
     699           0 :         return true;
     700             :     }
     701             : 
     702        2042 :     if (lseek(fd, (off_t) offset, SEEK_SET) < 0)
     703             :     {
     704           0 :         slru_errcause = SLRU_SEEK_FAILED;
     705           0 :         slru_errno = errno;
     706           0 :         CloseTransientFile(fd);
     707           0 :         return false;
     708             :     }
     709             : 
     710        2042 :     errno = 0;
     711        2042 :     pgstat_report_wait_start(WAIT_EVENT_SLRU_READ);
     712        2042 :     if (read(fd, shared->page_buffer[slotno], BLCKSZ) != BLCKSZ)
     713             :     {
     714           0 :         pgstat_report_wait_end();
     715           0 :         slru_errcause = SLRU_READ_FAILED;
     716           0 :         slru_errno = errno;
     717           0 :         CloseTransientFile(fd);
     718           0 :         return false;
     719             :     }
     720        2042 :     pgstat_report_wait_end();
     721             : 
     722        2042 :     if (CloseTransientFile(fd) != 0)
     723             :     {
     724           0 :         slru_errcause = SLRU_CLOSE_FAILED;
     725           0 :         slru_errno = errno;
     726           0 :         return false;
     727             :     }
     728             : 
     729        2042 :     return true;
     730             : }
     731             : 
     732             : /*
     733             :  * Physical write of a page from a buffer slot
     734             :  *
     735             :  * On failure, we cannot just ereport(ERROR) since caller has put state in
     736             :  * shared memory that must be undone.  So, we return false and save enough
     737             :  * info in static variables to let SlruReportIOError make the report.
     738             :  *
     739             :  * For now, assume it's not worth keeping a file pointer open across
     740             :  * independent read/write operations.  We do batch operations during
     741             :  * SimpleLruFlush, though.
     742             :  *
     743             :  * fdata is NULL for a standalone write, pointer to open-file info during
     744             :  * SimpleLruFlush.
     745             :  */
     746             : static bool
     747        5984 : SlruPhysicalWritePage(SlruCtl ctl, int pageno, int slotno, SlruFlush fdata)
     748             : {
     749        5984 :     SlruShared  shared = ctl->shared;
     750        5984 :     int         segno = pageno / SLRU_PAGES_PER_SEGMENT;
     751        5984 :     int         rpageno = pageno % SLRU_PAGES_PER_SEGMENT;
     752        5984 :     int         offset = rpageno * BLCKSZ;
     753             :     char        path[MAXPGPATH];
     754        5984 :     int         fd = -1;
     755             : 
     756             :     /* update the stats counter of written pages */
     757        5984 :     pgstat_count_slru_page_written(shared->slru_stats_idx);
     758             : 
     759             :     /*
     760             :      * Honor the write-WAL-before-data rule, if appropriate, so that we do not
     761             :      * write out data before associated WAL records.  This is the same action
     762             :      * performed during FlushBuffer() in the main buffer manager.
     763             :      */
     764        5984 :     if (shared->group_lsn != NULL)
     765             :     {
     766             :         /*
     767             :          * We must determine the largest async-commit LSN for the page. This
     768             :          * is a bit tedious, but since this entire function is a slow path
     769             :          * anyway, it seems better to do this here than to maintain a per-page
     770             :          * LSN variable (which'd need an extra comparison in the
     771             :          * transaction-commit path).
     772             :          */
     773             :         XLogRecPtr  max_lsn;
     774             :         int         lsnindex,
     775             :                     lsnoff;
     776             : 
     777        2552 :         lsnindex = slotno * shared->lsn_groups_per_page;
     778        2552 :         max_lsn = shared->group_lsn[lsnindex++];
     779     2613248 :         for (lsnoff = 1; lsnoff < shared->lsn_groups_per_page; lsnoff++)
     780             :         {
     781     2610696 :             XLogRecPtr  this_lsn = shared->group_lsn[lsnindex++];
     782             : 
     783     2610696 :             if (max_lsn < this_lsn)
     784        1602 :                 max_lsn = this_lsn;
     785             :         }
     786             : 
     787        2552 :         if (!XLogRecPtrIsInvalid(max_lsn))
     788             :         {
     789             :             /*
     790             :              * As noted above, elog(ERROR) is not acceptable here, so if
     791             :              * XLogFlush were to fail, we must PANIC.  This isn't much of a
     792             :              * restriction because XLogFlush is just about all critical
     793             :              * section anyway, but let's make sure.
     794             :              */
     795         146 :             START_CRIT_SECTION();
     796         146 :             XLogFlush(max_lsn);
     797         146 :             END_CRIT_SECTION();
     798             :         }
     799             :     }
     800             : 
     801             :     /*
     802             :      * During a Flush, we may already have the desired file open.
     803             :      */
     804        5984 :     if (fdata)
     805             :     {
     806             :         int         i;
     807             : 
     808        4538 :         for (i = 0; i < fdata->num_files; i++)
     809             :         {
     810          32 :             if (fdata->segno[i] == segno)
     811             :             {
     812          32 :                 fd = fdata->fd[i];
     813          32 :                 break;
     814             :             }
     815             :         }
     816             :     }
     817             : 
     818        5984 :     if (fd < 0)
     819             :     {
     820             :         /*
     821             :          * If the file doesn't already exist, we should create it.  It is
     822             :          * possible for this to need to happen when writing a page that's not
     823             :          * first in its segment; we assume the OS can cope with that. (Note:
     824             :          * it might seem that it'd be okay to create files only when
     825             :          * SimpleLruZeroPage is called for the first page of a segment.
     826             :          * However, if after a crash and restart the REDO logic elects to
     827             :          * replay the log from a checkpoint before the latest one, then it's
     828             :          * possible that we will get commands to set transaction status of
     829             :          * transactions that have already been truncated from the commit log.
     830             :          * Easiest way to deal with that is to accept references to
     831             :          * nonexistent files here and in SlruPhysicalReadPage.)
     832             :          *
     833             :          * Note: it is possible for more than one backend to be executing this
     834             :          * code simultaneously for different pages of the same file. Hence,
     835             :          * don't use O_EXCL or O_TRUNC or anything like that.
     836             :          */
     837        5952 :         SlruFileName(ctl, path, segno);
     838        5952 :         fd = OpenTransientFile(path, O_RDWR | O_CREAT | PG_BINARY);
     839        5952 :         if (fd < 0)
     840             :         {
     841           0 :             slru_errcause = SLRU_OPEN_FAILED;
     842           0 :             slru_errno = errno;
     843           0 :             return false;
     844             :         }
     845             : 
     846        5952 :         if (fdata)
     847             :         {
     848        4506 :             if (fdata->num_files < MAX_FLUSH_BUFFERS)
     849             :             {
     850        4506 :                 fdata->fd[fdata->num_files] = fd;
     851        4506 :                 fdata->segno[fdata->num_files] = segno;
     852        4506 :                 fdata->num_files++;
     853             :             }
     854             :             else
     855             :             {
     856             :                 /*
     857             :                  * In the unlikely event that we exceed MAX_FLUSH_BUFFERS,
     858             :                  * fall back to treating it as a standalone write.
     859             :                  */
     860           0 :                 fdata = NULL;
     861             :             }
     862             :         }
     863             :     }
     864             : 
     865        5984 :     if (lseek(fd, (off_t) offset, SEEK_SET) < 0)
     866             :     {
     867           0 :         slru_errcause = SLRU_SEEK_FAILED;
     868           0 :         slru_errno = errno;
     869           0 :         if (!fdata)
     870           0 :             CloseTransientFile(fd);
     871           0 :         return false;
     872             :     }
     873             : 
     874        5984 :     errno = 0;
     875        5984 :     pgstat_report_wait_start(WAIT_EVENT_SLRU_WRITE);
     876        5984 :     if (write(fd, shared->page_buffer[slotno], BLCKSZ) != BLCKSZ)
     877             :     {
     878           0 :         pgstat_report_wait_end();
     879             :         /* if write didn't set errno, assume problem is no disk space */
     880           0 :         if (errno == 0)
     881           0 :             errno = ENOSPC;
     882           0 :         slru_errcause = SLRU_WRITE_FAILED;
     883           0 :         slru_errno = errno;
     884           0 :         if (!fdata)
     885           0 :             CloseTransientFile(fd);
     886           0 :         return false;
     887             :     }
     888        5984 :     pgstat_report_wait_end();
     889             : 
     890             :     /*
     891             :      * If not part of Flush, need to fsync now.  We assume this happens
     892             :      * infrequently enough that it's not a performance issue.
     893             :      */
     894        5984 :     if (!fdata)
     895             :     {
     896        1446 :         pgstat_report_wait_start(WAIT_EVENT_SLRU_SYNC);
     897        1446 :         if (ctl->do_fsync && pg_fsync(fd) != 0)
     898             :         {
     899           0 :             pgstat_report_wait_end();
     900           0 :             slru_errcause = SLRU_FSYNC_FAILED;
     901           0 :             slru_errno = errno;
     902           0 :             CloseTransientFile(fd);
     903           0 :             return false;
     904             :         }
     905        1446 :         pgstat_report_wait_end();
     906             : 
     907        1446 :         if (CloseTransientFile(fd) != 0)
     908             :         {
     909           0 :             slru_errcause = SLRU_CLOSE_FAILED;
     910           0 :             slru_errno = errno;
     911           0 :             return false;
     912             :         }
     913             :     }
     914             : 
     915        5984 :     return true;
     916             : }
     917             : 
     918             : /*
     919             :  * Issue the error message after failure of SlruPhysicalReadPage or
     920             :  * SlruPhysicalWritePage.  Call this after cleaning up shared-memory state.
     921             :  */
     922             : static void
     923           0 : SlruReportIOError(SlruCtl ctl, int pageno, TransactionId xid)
     924             : {
     925           0 :     int         segno = pageno / SLRU_PAGES_PER_SEGMENT;
     926           0 :     int         rpageno = pageno % SLRU_PAGES_PER_SEGMENT;
     927           0 :     int         offset = rpageno * BLCKSZ;
     928             :     char        path[MAXPGPATH];
     929             : 
     930           0 :     SlruFileName(ctl, path, segno);
     931           0 :     errno = slru_errno;
     932           0 :     switch (slru_errcause)
     933             :     {
     934           0 :         case SLRU_OPEN_FAILED:
     935           0 :             ereport(ERROR,
     936             :                     (errcode_for_file_access(),
     937             :                      errmsg("could not access status of transaction %u", xid),
     938             :                      errdetail("Could not open file \"%s\": %m.", path)));
     939             :             break;
     940           0 :         case SLRU_SEEK_FAILED:
     941           0 :             ereport(ERROR,
     942             :                     (errcode_for_file_access(),
     943             :                      errmsg("could not access status of transaction %u", xid),
     944             :                      errdetail("Could not seek in file \"%s\" to offset %u: %m.",
     945             :                                path, offset)));
     946             :             break;
     947           0 :         case SLRU_READ_FAILED:
     948           0 :             if (errno)
     949           0 :                 ereport(ERROR,
     950             :                         (errcode_for_file_access(),
     951             :                          errmsg("could not access status of transaction %u", xid),
     952             :                          errdetail("Could not read from file \"%s\" at offset %u: %m.",
     953             :                                    path, offset)));
     954             :             else
     955           0 :                 ereport(ERROR,
     956             :                         (errmsg("could not access status of transaction %u", xid),
     957             :                          errdetail("Could not read from file \"%s\" at offset %u: read too few bytes.", path, offset)));
     958             :             break;
     959           0 :         case SLRU_WRITE_FAILED:
     960           0 :             if (errno)
     961           0 :                 ereport(ERROR,
     962             :                         (errcode_for_file_access(),
     963             :                          errmsg("could not access status of transaction %u", xid),
     964             :                          errdetail("Could not write to file \"%s\" at offset %u: %m.",
     965             :                                    path, offset)));
     966             :             else
     967           0 :                 ereport(ERROR,
     968             :                         (errmsg("could not access status of transaction %u", xid),
     969             :                          errdetail("Could not write to file \"%s\" at offset %u: wrote too few bytes.",
     970             :                                    path, offset)));
     971             :             break;
     972           0 :         case SLRU_FSYNC_FAILED:
     973           0 :             ereport(data_sync_elevel(ERROR),
     974             :                     (errcode_for_file_access(),
     975             :                      errmsg("could not access status of transaction %u", xid),
     976             :                      errdetail("Could not fsync file \"%s\": %m.",
     977             :                                path)));
     978           0 :             break;
     979           0 :         case SLRU_CLOSE_FAILED:
     980           0 :             ereport(ERROR,
     981             :                     (errcode_for_file_access(),
     982             :                      errmsg("could not access status of transaction %u", xid),
     983             :                      errdetail("Could not close file \"%s\": %m.",
     984             :                                path)));
     985             :             break;
     986           0 :         default:
     987             :             /* can't get here, we trust */
     988           0 :             elog(ERROR, "unrecognized SimpleLru error cause: %d",
     989             :                  (int) slru_errcause);
     990             :             break;
     991             :     }
     992           0 : }
     993             : 
     994             : /*
     995             :  * Select the slot to re-use when we need a free slot.
     996             :  *
     997             :  * The target page number is passed because we need to consider the
     998             :  * possibility that some other process reads in the target page while
     999             :  * we are doing I/O to free a slot.  Hence, check or recheck to see if
    1000             :  * any slot already holds the target page, and return that slot if so.
    1001             :  * Thus, the returned slot is *either* a slot already holding the pageno
    1002             :  * (could be any state except EMPTY), *or* a freeable slot (state EMPTY
    1003             :  * or CLEAN).
    1004             :  *
    1005             :  * Control lock must be held at entry, and will be held at exit.
    1006             :  */
    1007             : static int
    1008      294104 : SlruSelectLRUPage(SlruCtl ctl, int pageno)
    1009             : {
    1010      294104 :     SlruShared  shared = ctl->shared;
    1011             : 
    1012             :     /* Outer loop handles restart after I/O */
    1013             :     for (;;)
    1014           0 :     {
    1015             :         int         slotno;
    1016             :         int         cur_count;
    1017      294104 :         int         bestvalidslot = 0;  /* keep compiler quiet */
    1018      294104 :         int         best_valid_delta = -1;
    1019      294104 :         int         best_valid_page_number = 0; /* keep compiler quiet */
    1020      294104 :         int         bestinvalidslot = 0;    /* keep compiler quiet */
    1021      294104 :         int         best_invalid_delta = -1;
    1022      294104 :         int         best_invalid_page_number = 0;   /* keep compiler quiet */
    1023             : 
    1024             :         /* See if page already has a buffer assigned */
    1025      394176 :         for (slotno = 0; slotno < shared->num_slots; slotno++)
    1026             :         {
    1027      389592 :             if (shared->page_number[slotno] == pageno &&
    1028      386912 :                 shared->page_status[slotno] != SLRU_PAGE_EMPTY)
    1029      289520 :                 return slotno;
    1030             :         }
    1031             : 
    1032             :         /*
    1033             :          * If we find any EMPTY slot, just select that one. Else choose a
    1034             :          * victim page to replace.  We normally take the least recently used
    1035             :          * valid page, but we will never take the slot containing
    1036             :          * latest_page_number, even if it appears least recently used.  We
    1037             :          * will select a slot that is already I/O busy only if there is no
    1038             :          * other choice: a read-busy slot will not be least recently used once
    1039             :          * the read finishes, and waiting for an I/O on a write-busy slot is
    1040             :          * inferior to just picking some other slot.  Testing shows the slot
    1041             :          * we pick instead will often be clean, allowing us to begin a read at
    1042             :          * once.
    1043             :          *
    1044             :          * Normally the page_lru_count values will all be different and so
    1045             :          * there will be a well-defined LRU page.  But since we allow
    1046             :          * concurrent execution of SlruRecentlyUsed() within
    1047             :          * SimpleLruReadPage_ReadOnly(), it is possible that multiple pages
    1048             :          * acquire the same lru_count values.  In that case we break ties by
    1049             :          * choosing the furthest-back page.
    1050             :          *
    1051             :          * Notice that this next line forcibly advances cur_lru_count to a
    1052             :          * value that is certainly beyond any value that will be in the
    1053             :          * page_lru_count array after the loop finishes.  This ensures that
    1054             :          * the next execution of SlruRecentlyUsed will mark the page newly
    1055             :          * used, even if it's for a page that has the current counter value.
    1056             :          * That gets us back on the path to having good data when there are
    1057             :          * multiple pages with the same lru_count.
    1058             :          */
    1059        4584 :         cur_count = (shared->cur_lru_count)++;
    1060        4720 :         for (slotno = 0; slotno < shared->num_slots; slotno++)
    1061             :         {
    1062             :             int         this_delta;
    1063             :             int         this_page_number;
    1064             : 
    1065        4720 :             if (shared->page_status[slotno] == SLRU_PAGE_EMPTY)
    1066        4584 :                 return slotno;
    1067         136 :             this_delta = cur_count - shared->page_lru_count[slotno];
    1068         136 :             if (this_delta < 0)
    1069             :             {
    1070             :                 /*
    1071             :                  * Clean up in case shared updates have caused cur_count
    1072             :                  * increments to get "lost".  We back off the page counts,
    1073             :                  * rather than trying to increase cur_count, to avoid any
    1074             :                  * question of infinite loops or failure in the presence of
    1075             :                  * wrapped-around counts.
    1076             :                  */
    1077           0 :                 shared->page_lru_count[slotno] = cur_count;
    1078           0 :                 this_delta = 0;
    1079             :             }
    1080         136 :             this_page_number = shared->page_number[slotno];
    1081         136 :             if (this_page_number == shared->latest_page_number)
    1082          44 :                 continue;
    1083          92 :             if (shared->page_status[slotno] == SLRU_PAGE_VALID)
    1084             :             {
    1085          92 :                 if (this_delta > best_valid_delta ||
    1086           0 :                     (this_delta == best_valid_delta &&
    1087           0 :                      ctl->PagePrecedes(this_page_number,
    1088             :                                        best_valid_page_number)))
    1089             :                 {
    1090          30 :                     bestvalidslot = slotno;
    1091          30 :                     best_valid_delta = this_delta;
    1092          30 :                     best_valid_page_number = this_page_number;
    1093             :                 }
    1094             :             }
    1095             :             else
    1096             :             {
    1097           0 :                 if (this_delta > best_invalid_delta ||
    1098           0 :                     (this_delta == best_invalid_delta &&
    1099           0 :                      ctl->PagePrecedes(this_page_number,
    1100             :                                        best_invalid_page_number)))
    1101             :                 {
    1102           0 :                     bestinvalidslot = slotno;
    1103           0 :                     best_invalid_delta = this_delta;
    1104           0 :                     best_invalid_page_number = this_page_number;
    1105             :                 }
    1106             :             }
    1107             :         }
    1108             : 
    1109             :         /*
    1110             :          * If all pages (except possibly the latest one) are I/O busy, we'll
    1111             :          * have to wait for an I/O to complete and then retry.  In that
    1112             :          * unhappy case, we choose to wait for the I/O on the least recently
    1113             :          * used slot, on the assumption that it was likely initiated first of
    1114             :          * all the I/Os in progress and may therefore finish first.
    1115             :          */
    1116           0 :         if (best_valid_delta < 0)
    1117             :         {
    1118           0 :             SimpleLruWaitIO(ctl, bestinvalidslot);
    1119           0 :             continue;
    1120             :         }
    1121             : 
    1122             :         /*
    1123             :          * If the selected page is clean, we're set.
    1124             :          */
    1125           0 :         if (!shared->page_dirty[bestvalidslot])
    1126           0 :             return bestvalidslot;
    1127             : 
    1128             :         /*
    1129             :          * Write the page.
    1130             :          */
    1131           0 :         SlruInternalWritePage(ctl, bestvalidslot, NULL);
    1132             : 
    1133             :         /*
    1134             :          * Now loop back and try again.  This is the easiest way of dealing
    1135             :          * with corner cases such as the victim page being re-dirtied while we
    1136             :          * wrote it.
    1137             :          */
    1138             :     }
    1139             : }
    1140             : 
    1141             : /*
    1142             :  * Flush dirty pages to disk during checkpoint or database shutdown
    1143             :  */
    1144             : void
    1145       21510 : SimpleLruFlush(SlruCtl ctl, bool allow_redirtied)
    1146             : {
    1147       21510 :     SlruShared  shared = ctl->shared;
    1148             :     SlruFlushData fdata;
    1149             :     int         slotno;
    1150       21510 :     int         pageno = 0;
    1151             :     int         i;
    1152             :     bool        ok;
    1153             : 
    1154             :     /* update the stats counter of flushes */
    1155       21510 :     pgstat_count_slru_flush(shared->slru_stats_idx);
    1156             : 
    1157             :     /*
    1158             :      * Find and write dirty pages
    1159             :      */
    1160       21510 :     fdata.num_files = 0;
    1161             : 
    1162       21510 :     LWLockAcquire(shared->ControlLock, LW_EXCLUSIVE);
    1163             : 
    1164      451302 :     for (slotno = 0; slotno < shared->num_slots; slotno++)
    1165             :     {
    1166      429792 :         SlruInternalWritePage(ctl, slotno, &fdata);
    1167             : 
    1168             :         /*
    1169             :          * In some places (e.g. checkpoints), we cannot assert that the slot
    1170             :          * is clean now, since another process might have re-dirtied it
    1171             :          * already.  That's okay.
    1172             :          */
    1173             :         Assert(allow_redirtied ||
    1174             :                shared->page_status[slotno] == SLRU_PAGE_EMPTY ||
    1175             :                (shared->page_status[slotno] == SLRU_PAGE_VALID &&
    1176             :                 !shared->page_dirty[slotno]));
    1177             :     }
    1178             : 
    1179       21510 :     LWLockRelease(shared->ControlLock);
    1180             : 
    1181             :     /*
    1182             :      * Now fsync and close any files that were open
    1183             :      */
    1184       21510 :     ok = true;
    1185       26016 :     for (i = 0; i < fdata.num_files; i++)
    1186             :     {
    1187        4506 :         pgstat_report_wait_start(WAIT_EVENT_SLRU_FLUSH_SYNC);
    1188        4506 :         if (ctl->do_fsync && pg_fsync(fdata.fd[i]) != 0)
    1189             :         {
    1190           0 :             slru_errcause = SLRU_FSYNC_FAILED;
    1191           0 :             slru_errno = errno;
    1192           0 :             pageno = fdata.segno[i] * SLRU_PAGES_PER_SEGMENT;
    1193           0 :             ok = false;
    1194             :         }
    1195        4506 :         pgstat_report_wait_end();
    1196             : 
    1197        4506 :         if (CloseTransientFile(fdata.fd[i]) != 0)
    1198             :         {
    1199           0 :             slru_errcause = SLRU_CLOSE_FAILED;
    1200           0 :             slru_errno = errno;
    1201           0 :             pageno = fdata.segno[i] * SLRU_PAGES_PER_SEGMENT;
    1202           0 :             ok = false;
    1203             :         }
    1204             :     }
    1205       21510 :     if (!ok)
    1206           0 :         SlruReportIOError(ctl, pageno, InvalidTransactionId);
    1207       21510 : }
    1208             : 
    1209             : /*
    1210             :  * Remove all segments before the one holding the passed page number
    1211             :  */
    1212             : void
    1213        3058 : SimpleLruTruncate(SlruCtl ctl, int cutoffPage)
    1214             : {
    1215        3058 :     SlruShared  shared = ctl->shared;
    1216             :     int         slotno;
    1217             : 
    1218             :     /* update the stats counter of truncates */
    1219        3058 :     pgstat_count_slru_truncate(shared->slru_stats_idx);
    1220             : 
    1221             :     /*
    1222             :      * The cutoff point is the start of the segment containing cutoffPage.
    1223             :      */
    1224        3058 :     cutoffPage -= cutoffPage % SLRU_PAGES_PER_SEGMENT;
    1225             : 
    1226             :     /*
    1227             :      * Scan shared memory and remove any pages preceding the cutoff page, to
    1228             :      * ensure we won't rewrite them later.  (Since this is normally called in
    1229             :      * or just after a checkpoint, any dirty pages should have been flushed
    1230             :      * already ... we're just being extra careful here.)
    1231             :      */
    1232        3058 :     LWLockAcquire(shared->ControlLock, LW_EXCLUSIVE);
    1233             : 
    1234        3058 : restart:;
    1235             : 
    1236             :     /*
    1237             :      * While we are holding the lock, make an important safety check: the
    1238             :      * planned cutoff point must be <= the current endpoint page. Otherwise we
    1239             :      * have already wrapped around, and proceeding with the truncation would
    1240             :      * risk removing the current segment.
    1241             :      */
    1242        3058 :     if (ctl->PagePrecedes(shared->latest_page_number, cutoffPage))
    1243             :     {
    1244           0 :         LWLockRelease(shared->ControlLock);
    1245           0 :         ereport(LOG,
    1246             :                 (errmsg("could not truncate directory \"%s\": apparent wraparound",
    1247             :                         ctl->Dir)));
    1248           0 :         return;
    1249             :     }
    1250             : 
    1251      100914 :     for (slotno = 0; slotno < shared->num_slots; slotno++)
    1252             :     {
    1253       97856 :         if (shared->page_status[slotno] == SLRU_PAGE_EMPTY)
    1254       94710 :             continue;
    1255        3146 :         if (!ctl->PagePrecedes(shared->page_number[slotno], cutoffPage))
    1256        3146 :             continue;
    1257             : 
    1258             :         /*
    1259             :          * If page is clean, just change state to EMPTY (expected case).
    1260             :          */
    1261           0 :         if (shared->page_status[slotno] == SLRU_PAGE_VALID &&
    1262           0 :             !shared->page_dirty[slotno])
    1263             :         {
    1264           0 :             shared->page_status[slotno] = SLRU_PAGE_EMPTY;
    1265           0 :             continue;
    1266             :         }
    1267             : 
    1268             :         /*
    1269             :          * Hmm, we have (or may have) I/O operations acting on the page, so
    1270             :          * we've got to wait for them to finish and then start again. This is
    1271             :          * the same logic as in SlruSelectLRUPage.  (XXX if page is dirty,
    1272             :          * wouldn't it be OK to just discard it without writing it?  For now,
    1273             :          * keep the logic the same as it was.)
    1274             :          */
    1275           0 :         if (shared->page_status[slotno] == SLRU_PAGE_VALID)
    1276           0 :             SlruInternalWritePage(ctl, slotno, NULL);
    1277             :         else
    1278           0 :             SimpleLruWaitIO(ctl, slotno);
    1279           0 :         goto restart;
    1280             :     }
    1281             : 
    1282        3058 :     LWLockRelease(shared->ControlLock);
    1283             : 
    1284             :     /* Now we can remove the old segment(s) */
    1285        3058 :     (void) SlruScanDirectory(ctl, SlruScanDirCbDeleteCutoff, &cutoffPage);
    1286             : }
    1287             : 
    1288             : /*
    1289             :  * Delete an individual SLRU segment, identified by the filename.
    1290             :  *
    1291             :  * NB: This does not touch the SLRU buffers themselves, callers have to ensure
    1292             :  * they either can't yet contain anything, or have already been cleaned out.
    1293             :  */
    1294             : static void
    1295          10 : SlruInternalDeleteSegment(SlruCtl ctl, char *filename)
    1296             : {
    1297             :     char        path[MAXPGPATH];
    1298             : 
    1299          10 :     snprintf(path, MAXPGPATH, "%s/%s", ctl->Dir, filename);
    1300          10 :     ereport(DEBUG2,
    1301             :             (errmsg("removing file \"%s\"", path)));
    1302          10 :     unlink(path);
    1303          10 : }
    1304             : 
    1305             : /*
    1306             :  * Delete an individual SLRU segment, identified by the segment number.
    1307             :  */
    1308             : void
    1309           0 : SlruDeleteSegment(SlruCtl ctl, int segno)
    1310             : {
    1311           0 :     SlruShared  shared = ctl->shared;
    1312             :     int         slotno;
    1313             :     char        path[MAXPGPATH];
    1314             :     bool        did_write;
    1315             : 
    1316             :     /* Clean out any possibly existing references to the segment. */
    1317           0 :     LWLockAcquire(shared->ControlLock, LW_EXCLUSIVE);
    1318           0 : restart:
    1319           0 :     did_write = false;
    1320           0 :     for (slotno = 0; slotno < shared->num_slots; slotno++)
    1321             :     {
    1322           0 :         int         pagesegno = shared->page_number[slotno] / SLRU_PAGES_PER_SEGMENT;
    1323             : 
    1324           0 :         if (shared->page_status[slotno] == SLRU_PAGE_EMPTY)
    1325           0 :             continue;
    1326             : 
    1327             :         /* not the segment we're looking for */
    1328           0 :         if (pagesegno != segno)
    1329           0 :             continue;
    1330             : 
    1331             :         /* If page is clean, just change state to EMPTY (expected case). */
    1332           0 :         if (shared->page_status[slotno] == SLRU_PAGE_VALID &&
    1333           0 :             !shared->page_dirty[slotno])
    1334             :         {
    1335           0 :             shared->page_status[slotno] = SLRU_PAGE_EMPTY;
    1336           0 :             continue;
    1337             :         }
    1338             : 
    1339             :         /* Same logic as SimpleLruTruncate() */
    1340           0 :         if (shared->page_status[slotno] == SLRU_PAGE_VALID)
    1341           0 :             SlruInternalWritePage(ctl, slotno, NULL);
    1342             :         else
    1343           0 :             SimpleLruWaitIO(ctl, slotno);
    1344             : 
    1345           0 :         did_write = true;
    1346             :     }
    1347             : 
    1348             :     /*
    1349             :      * Be extra careful and re-check. The IO functions release the control
    1350             :      * lock, so new pages could have been read in.
    1351             :      */
    1352           0 :     if (did_write)
    1353           0 :         goto restart;
    1354             : 
    1355           0 :     snprintf(path, MAXPGPATH, "%s/%04X", ctl->Dir, segno);
    1356           0 :     ereport(DEBUG2,
    1357             :             (errmsg("removing file \"%s\"", path)));
    1358           0 :     unlink(path);
    1359             : 
    1360           0 :     LWLockRelease(shared->ControlLock);
    1361           0 : }
    1362             : 
    1363             : /*
    1364             :  * SlruScanDirectory callback
    1365             :  *      This callback reports true if there's any segment prior to the one
    1366             :  *      containing the page passed as "data".
    1367             :  */
    1368             : bool
    1369         782 : SlruScanDirCbReportPresence(SlruCtl ctl, char *filename, int segpage, void *data)
    1370             : {
    1371         782 :     int         cutoffPage = *(int *) data;
    1372             : 
    1373         782 :     cutoffPage -= cutoffPage % SLRU_PAGES_PER_SEGMENT;
    1374             : 
    1375         782 :     if (ctl->PagePrecedes(segpage, cutoffPage))
    1376           0 :         return true;            /* found one; don't iterate any more */
    1377             : 
    1378         782 :     return false;               /* keep going */
    1379             : }
    1380             : 
    1381             : /*
    1382             :  * SlruScanDirectory callback.
    1383             :  *      This callback deletes segments prior to the one passed in as "data".
    1384             :  */
    1385             : static bool
    1386        3058 : SlruScanDirCbDeleteCutoff(SlruCtl ctl, char *filename, int segpage, void *data)
    1387             : {
    1388        3058 :     int         cutoffPage = *(int *) data;
    1389             : 
    1390        3058 :     if (ctl->PagePrecedes(segpage, cutoffPage))
    1391           0 :         SlruInternalDeleteSegment(ctl, filename);
    1392             : 
    1393        3058 :     return false;               /* keep going */
    1394             : }
    1395             : 
    1396             : /*
    1397             :  * SlruScanDirectory callback.
    1398             :  *      This callback deletes all segments.
    1399             :  */
    1400             : bool
    1401          10 : SlruScanDirCbDeleteAll(SlruCtl ctl, char *filename, int segpage, void *data)
    1402             : {
    1403          10 :     SlruInternalDeleteSegment(ctl, filename);
    1404             : 
    1405          10 :     return false;               /* keep going */
    1406             : }
    1407             : 
    1408             : /*
    1409             :  * Scan the SimpleLru directory and apply a callback to each file found in it.
    1410             :  *
    1411             :  * If the callback returns true, the scan is stopped.  The last return value
    1412             :  * from the callback is returned.
    1413             :  *
    1414             :  * The callback receives the following arguments: 1. the SlruCtl struct for the
    1415             :  * slru being truncated; 2. the filename being considered; 3. the page number
    1416             :  * for the first page of that file; 4. a pointer to the opaque data given to us
    1417             :  * by the caller.
    1418             :  *
    1419             :  * Note that the ordering in which the directory is scanned is not guaranteed.
    1420             :  *
    1421             :  * Note that no locking is applied.
    1422             :  */
    1423             : bool
    1424        8128 : SlruScanDirectory(SlruCtl ctl, SlruScanCallback callback, void *data)
    1425             : {
    1426        8128 :     bool        retval = false;
    1427             :     DIR        *cldir;
    1428             :     struct dirent *clde;
    1429             :     int         segno;
    1430             :     int         segpage;
    1431             : 
    1432        8128 :     cldir = AllocateDir(ctl->Dir);
    1433       28234 :     while ((clde = ReadDir(cldir, ctl->Dir)) != NULL)
    1434             :     {
    1435             :         size_t      len;
    1436             : 
    1437       20106 :         len = strlen(clde->d_name);
    1438             : 
    1439       20106 :         if ((len == 4 || len == 5 || len == 6) &&
    1440        3850 :             strspn(clde->d_name, "0123456789ABCDEF") == len)
    1441             :         {
    1442        3850 :             segno = (int) strtol(clde->d_name, NULL, 16);
    1443        3850 :             segpage = segno * SLRU_PAGES_PER_SEGMENT;
    1444             : 
    1445        3850 :             elog(DEBUG2, "SlruScanDirectory invoking callback on %s/%s",
    1446             :                  ctl->Dir, clde->d_name);
    1447        3850 :             retval = callback(ctl, clde->d_name, segpage, data);
    1448        3850 :             if (retval)
    1449           0 :                 break;
    1450             :         }
    1451             :     }
    1452        8128 :     FreeDir(cldir);
    1453             : 
    1454        8128 :     return retval;
    1455             : }

Generated by: LCOV version 1.13