PostgreSQL Source Code  git master
slru.h File Reference
#include "access/xlogdefs.h"
#include "storage/lwlock.h"
#include "storage/sync.h"
Include dependency graph for slru.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  SlruSharedData
 
struct  SlruCtlData
 

Macros

#define SLRU_PAGES_PER_SEGMENT   32
 
#define SlruPagePrecedesUnitTests(ctl, per_page)   do {} while (0)
 

Typedefs

typedef struct SlruSharedData SlruSharedData
 
typedef SlruSharedDataSlruShared
 
typedef struct SlruCtlData SlruCtlData
 
typedef SlruCtlDataSlruCtl
 
typedef bool(* SlruScanCallback) (SlruCtl ctl, char *filename, int segpage, void *data)
 

Enumerations

enum  SlruPageStatus { SLRU_PAGE_EMPTY, SLRU_PAGE_READ_IN_PROGRESS, SLRU_PAGE_VALID, SLRU_PAGE_WRITE_IN_PROGRESS }
 

Functions

Size SimpleLruShmemSize (int nslots, int nlsns)
 
void SimpleLruInit (SlruCtl ctl, const char *name, int nslots, int nlsns, LWLock *ctllock, const char *subdir, int tranche_id, SyncRequestHandler sync_handler)
 
int SimpleLruZeroPage (SlruCtl ctl, int pageno)
 
int SimpleLruReadPage (SlruCtl ctl, int pageno, bool write_ok, TransactionId xid)
 
int SimpleLruReadPage_ReadOnly (SlruCtl ctl, int pageno, TransactionId xid)
 
void SimpleLruWritePage (SlruCtl ctl, int slotno)
 
void SimpleLruWriteAll (SlruCtl ctl, bool allow_redirtied)
 
void SimpleLruTruncate (SlruCtl ctl, int cutoffPage)
 
bool SimpleLruDoesPhysicalPageExist (SlruCtl ctl, int pageno)
 
bool SlruScanDirectory (SlruCtl ctl, SlruScanCallback callback, void *data)
 
void SlruDeleteSegment (SlruCtl ctl, int segno)
 
int SlruSyncFileTag (SlruCtl ctl, const FileTag *ftag, char *path)
 
bool SlruScanDirCbReportPresence (SlruCtl ctl, char *filename, int segpage, void *data)
 
bool SlruScanDirCbDeleteAll (SlruCtl ctl, char *filename, int segpage, void *data)
 

Macro Definition Documentation

◆ SLRU_PAGES_PER_SEGMENT

◆ SlruPagePrecedesUnitTests

#define SlruPagePrecedesUnitTests (   ctl,
  per_page 
)    do {} while (0)

Typedef Documentation

◆ SlruCtl

typedef SlruCtlData* SlruCtl

Definition at line 139 of file slru.h.

◆ SlruCtlData

typedef struct SlruCtlData SlruCtlData

◆ SlruScanCallback

typedef bool(* SlruScanCallback) (SlruCtl ctl, char *filename, int segpage, void *data)

Definition at line 161 of file slru.h.

◆ SlruShared

Definition at line 104 of file slru.h.

◆ SlruSharedData

Enumeration Type Documentation

◆ SlruPageStatus

Enumerator
SLRU_PAGE_EMPTY 
SLRU_PAGE_READ_IN_PROGRESS 
SLRU_PAGE_VALID 
SLRU_PAGE_WRITE_IN_PROGRESS 

Definition at line 42 of file slru.h.

43 {
44  SLRU_PAGE_EMPTY, /* buffer is not in use */
45  SLRU_PAGE_READ_IN_PROGRESS, /* page is being read in */
46  SLRU_PAGE_VALID, /* page is valid and not being written */
47  SLRU_PAGE_WRITE_IN_PROGRESS /* page is being written out */
SlruPageStatus
Definition: slru.h:42

Function Documentation

◆ SimpleLruDoesPhysicalPageExist()

bool SimpleLruDoesPhysicalPageExist ( SlruCtl  ctl,
int  pageno 
)

Definition at line 625 of file slru.c.

References CloseTransientFile(), endpos, SlruWriteAllData::fd, MAXPGPATH, OpenTransientFile(), PG_BINARY, pgstat_count_slru_page_exists(), SlruWriteAllData::segno, SlruCtlData::shared, SLRU_CLOSE_FAILED, slru_errcause, slru_errno, SLRU_OPEN_FAILED, SLRU_PAGES_PER_SEGMENT, SLRU_SEEK_FAILED, SlruSharedData::slru_stats_idx, SlruFileName, and SlruReportIOError().

Referenced by ActivateCommitTs(), find_multixact_start(), and MaybeExtendOffsetSlru().

626 {
627  int segno = pageno / SLRU_PAGES_PER_SEGMENT;
628  int rpageno = pageno % SLRU_PAGES_PER_SEGMENT;
629  int offset = rpageno * BLCKSZ;
630  char path[MAXPGPATH];
631  int fd;
632  bool result;
633  off_t endpos;
634 
635  /* update the stats counter of checked pages */
637 
638  SlruFileName(ctl, path, segno);
639 
640  fd = OpenTransientFile(path, O_RDONLY | PG_BINARY);
641  if (fd < 0)
642  {
643  /* expected: file doesn't exist */
644  if (errno == ENOENT)
645  return false;
646 
647  /* report error normally */
649  slru_errno = errno;
650  SlruReportIOError(ctl, pageno, 0);
651  }
652 
653  if ((endpos = lseek(fd, 0, SEEK_END)) < 0)
654  {
656  slru_errno = errno;
657  SlruReportIOError(ctl, pageno, 0);
658  }
659 
660  result = endpos >= (off_t) (offset + BLCKSZ);
661 
662  if (CloseTransientFile(fd) != 0)
663  {
665  slru_errno = errno;
666  return false;
667  }
668 
669  return result;
670 }
static SlruErrorCause slru_errcause
Definition: slru.c:133
static int fd(const char *x, int i)
Definition: preproc-init.c:105
static void SlruReportIOError(SlruCtl ctl, int pageno, TransactionId xid)
Definition: slru.c:930
#define PG_BINARY
Definition: c.h:1271
int OpenTransientFile(const char *fileName, int fileFlags)
Definition: fd.c:2423
#define MAXPGPATH
static XLogRecPtr endpos
Definition: pg_receivewal.c:46
int CloseTransientFile(int fd)
Definition: fd.c:2600
void pgstat_count_slru_page_exists(int slru_idx)
Definition: pgstat.c:5833
#define SlruFileName(ctl, path, seg)
Definition: slru.c:62
int slru_stats_idx
Definition: slru.h:101
static int slru_errno
Definition: slru.c:134
SlruShared shared
Definition: slru.h:112
#define SLRU_PAGES_PER_SEGMENT
Definition: slru.h:34

◆ SimpleLruInit()

void SimpleLruInit ( SlruCtl  ctl,
const char *  name,
int  nslots,
int  nlsns,
LWLock ctllock,
const char *  subdir,
int  tranche_id,
SyncRequestHandler  sync_handler 
)

Definition at line 186 of file slru.c.

References Assert, SlruSharedData::buffer_locks, BUFFERALIGN, SlruSharedData::ControlLock, SlruSharedData::cur_lru_count, SlruCtlData::Dir, SlruSharedData::group_lsn, IsUnderPostmaster, LWLockPadded::lock, SlruSharedData::lsn_groups_per_page, LWLockInitialize(), MAXALIGN, SlruSharedData::num_slots, SlruSharedData::page_buffer, SlruSharedData::page_dirty, SlruSharedData::page_lru_count, SlruSharedData::page_number, SlruSharedData::page_status, pgstat_slru_index(), SlruCtlData::shared, ShmemInitStruct(), SimpleLruShmemSize(), SLRU_PAGE_EMPTY, SlruSharedData::slru_stats_idx, strlcpy(), and SlruCtlData::sync_handler.

Referenced by AsyncShmemInit(), CLOGShmemInit(), CommitTsShmemInit(), MultiXactShmemInit(), SerialInit(), and SUBTRANSShmemInit().

189 {
190  SlruShared shared;
191  bool found;
192 
193  shared = (SlruShared) ShmemInitStruct(name,
194  SimpleLruShmemSize(nslots, nlsns),
195  &found);
196 
197  if (!IsUnderPostmaster)
198  {
199  /* Initialize locks and shared memory area */
200  char *ptr;
201  Size offset;
202  int slotno;
203 
204  Assert(!found);
205 
206  memset(shared, 0, sizeof(SlruSharedData));
207 
208  shared->ControlLock = ctllock;
209 
210  shared->num_slots = nslots;
211  shared->lsn_groups_per_page = nlsns;
212 
213  shared->cur_lru_count = 0;
214 
215  /* shared->latest_page_number will be set later */
216 
218 
219  ptr = (char *) shared;
220  offset = MAXALIGN(sizeof(SlruSharedData));
221  shared->page_buffer = (char **) (ptr + offset);
222  offset += MAXALIGN(nslots * sizeof(char *));
223  shared->page_status = (SlruPageStatus *) (ptr + offset);
224  offset += MAXALIGN(nslots * sizeof(SlruPageStatus));
225  shared->page_dirty = (bool *) (ptr + offset);
226  offset += MAXALIGN(nslots * sizeof(bool));
227  shared->page_number = (int *) (ptr + offset);
228  offset += MAXALIGN(nslots * sizeof(int));
229  shared->page_lru_count = (int *) (ptr + offset);
230  offset += MAXALIGN(nslots * sizeof(int));
231 
232  /* Initialize LWLocks */
233  shared->buffer_locks = (LWLockPadded *) (ptr + offset);
234  offset += MAXALIGN(nslots * sizeof(LWLockPadded));
235 
236  if (nlsns > 0)
237  {
238  shared->group_lsn = (XLogRecPtr *) (ptr + offset);
239  offset += MAXALIGN(nslots * nlsns * sizeof(XLogRecPtr));
240  }
241 
242  ptr += BUFFERALIGN(offset);
243  for (slotno = 0; slotno < nslots; slotno++)
244  {
245  LWLockInitialize(&shared->buffer_locks[slotno].lock,
246  tranche_id);
247 
248  shared->page_buffer[slotno] = ptr;
249  shared->page_status[slotno] = SLRU_PAGE_EMPTY;
250  shared->page_dirty[slotno] = false;
251  shared->page_lru_count[slotno] = 0;
252  ptr += BLCKSZ;
253  }
254 
255  /* Should fit to estimated shmem size */
256  Assert(ptr - (char *) shared <= SimpleLruShmemSize(nslots, nlsns));
257  }
258  else
259  Assert(found);
260 
261  /*
262  * Initialize the unshared control struct, including directory path. We
263  * assume caller set PagePrecedes.
264  */
265  ctl->shared = shared;
266  ctl->sync_handler = sync_handler;
267  strlcpy(ctl->Dir, subdir, sizeof(ctl->Dir));
268 }
LWLock * ControlLock
Definition: slru.h:55
int * page_number
Definition: slru.h:67
SlruPageStatus
Definition: slru.h:42
char ** page_buffer
Definition: slru.h:64
int cur_lru_count
Definition: slru.h:91
int lsn_groups_per_page
Definition: slru.h:80
Size SimpleLruShmemSize(int nslots, int nlsns)
Definition: slru.c:155
int pgstat_slru_index(const char *name)
Definition: pgstat.c:5766
SlruPageStatus * page_status
Definition: slru.h:65
void * ShmemInitStruct(const char *name, Size size, bool *foundPtr)
Definition: shmem.c:396
bool IsUnderPostmaster
Definition: globals.c:112
LWLockPadded * buffer_locks
Definition: slru.h:69
XLogRecPtr * group_lsn
Definition: slru.h:79
void LWLockInitialize(LWLock *lock, int tranche_id)
Definition: lwlock.c:740
SlruSharedData * SlruShared
Definition: slru.h:104
char Dir[64]
Definition: slru.h:136
LWLock lock
Definition: lwlock.h:57
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
int * page_lru_count
Definition: slru.h:68
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define Assert(condition)
Definition: c.h:804
size_t Size
Definition: c.h:540
#define MAXALIGN(LEN)
Definition: c.h:757
SyncRequestHandler sync_handler
Definition: slru.h:118
int slru_stats_idx
Definition: slru.h:101
int num_slots
Definition: slru.h:58
const char * name
Definition: encode.c:515
bool * page_dirty
Definition: slru.h:66
SlruShared shared
Definition: slru.h:112
#define BUFFERALIGN(LEN)
Definition: c.h:759

◆ SimpleLruReadPage()

int SimpleLruReadPage ( SlruCtl  ctl,
int  pageno,
bool  write_ok,
TransactionId  xid 
)

Definition at line 394 of file slru.c.

References Assert, SlruSharedData::buffer_locks, SlruSharedData::ControlLock, LWLockPadded::lock, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), SlruSharedData::page_dirty, SlruSharedData::page_number, SlruSharedData::page_status, pgstat_count_slru_page_hit(), pgstat_count_slru_page_read(), SlruCtlData::shared, SimpleLruWaitIO(), SimpleLruZeroLSNs(), SLRU_PAGE_EMPTY, SLRU_PAGE_READ_IN_PROGRESS, SLRU_PAGE_VALID, SLRU_PAGE_WRITE_IN_PROGRESS, SlruSharedData::slru_stats_idx, SlruPhysicalReadPage(), SlruRecentlyUsed, SlruReportIOError(), and SlruSelectLRUPage().

Referenced by asyncQueueAddEntries(), GetMultiXactIdMembers(), RecordNewMultiXact(), SerialAdd(), SetXidCommitTsInPage(), SimpleLruReadPage_ReadOnly(), SubTransSetParent(), TransactionIdSetPageStatusInternal(), TrimCLOG(), and TrimMultiXact().

396 {
397  SlruShared shared = ctl->shared;
398 
399  /* Outer loop handles restart if we must wait for someone else's I/O */
400  for (;;)
401  {
402  int slotno;
403  bool ok;
404 
405  /* See if page already is in memory; if not, pick victim slot */
406  slotno = SlruSelectLRUPage(ctl, pageno);
407 
408  /* Did we find the page in memory? */
409  if (shared->page_number[slotno] == pageno &&
410  shared->page_status[slotno] != SLRU_PAGE_EMPTY)
411  {
412  /*
413  * If page is still being read in, we must wait for I/O. Likewise
414  * if the page is being written and the caller said that's not OK.
415  */
416  if (shared->page_status[slotno] == SLRU_PAGE_READ_IN_PROGRESS ||
417  (shared->page_status[slotno] == SLRU_PAGE_WRITE_IN_PROGRESS &&
418  !write_ok))
419  {
420  SimpleLruWaitIO(ctl, slotno);
421  /* Now we must recheck state from the top */
422  continue;
423  }
424  /* Otherwise, it's ready to use */
425  SlruRecentlyUsed(shared, slotno);
426 
427  /* update the stats counter of pages found in the SLRU */
429 
430  return slotno;
431  }
432 
433  /* We found no match; assert we selected a freeable slot */
434  Assert(shared->page_status[slotno] == SLRU_PAGE_EMPTY ||
435  (shared->page_status[slotno] == SLRU_PAGE_VALID &&
436  !shared->page_dirty[slotno]));
437 
438  /* Mark the slot read-busy */
439  shared->page_number[slotno] = pageno;
440  shared->page_status[slotno] = SLRU_PAGE_READ_IN_PROGRESS;
441  shared->page_dirty[slotno] = false;
442 
443  /* Acquire per-buffer lock (cannot deadlock, see notes at top) */
444  LWLockAcquire(&shared->buffer_locks[slotno].lock, LW_EXCLUSIVE);
445 
446  /* Release control lock while doing I/O */
447  LWLockRelease(shared->ControlLock);
448 
449  /* Do the read */
450  ok = SlruPhysicalReadPage(ctl, pageno, slotno);
451 
452  /* Set the LSNs for this newly read-in page to zero */
453  SimpleLruZeroLSNs(ctl, slotno);
454 
455  /* Re-acquire control lock and update page state */
457 
458  Assert(shared->page_number[slotno] == pageno &&
459  shared->page_status[slotno] == SLRU_PAGE_READ_IN_PROGRESS &&
460  !shared->page_dirty[slotno]);
461 
462  shared->page_status[slotno] = ok ? SLRU_PAGE_VALID : SLRU_PAGE_EMPTY;
463 
464  LWLockRelease(&shared->buffer_locks[slotno].lock);
465 
466  /* Now it's okay to ereport if we failed */
467  if (!ok)
468  SlruReportIOError(ctl, pageno, xid);
469 
470  SlruRecentlyUsed(shared, slotno);
471 
472  /* update the stats counter of pages not found in SLRU */
474 
475  return slotno;
476  }
477 }
LWLock * ControlLock
Definition: slru.h:55
int * page_number
Definition: slru.h:67
static void SimpleLruZeroLSNs(SlruCtl ctl, int slotno)
Definition: slru.c:323
static void SlruReportIOError(SlruCtl ctl, int pageno, TransactionId xid)
Definition: slru.c:930
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1816
SlruPageStatus * page_status
Definition: slru.h:65
LWLockPadded * buffer_locks
Definition: slru.h:69
void pgstat_count_slru_page_read(int slru_idx)
Definition: pgstat.c:5839
static void SimpleLruWaitIO(SlruCtl ctl, int slotno)
Definition: slru.c:340
LWLock lock
Definition: lwlock.h:57
static bool SlruPhysicalReadPage(SlruCtl ctl, int pageno, int slotno)
Definition: slru.c:683
#define Assert(condition)
Definition: c.h:804
void pgstat_count_slru_page_hit(int slru_idx)
Definition: pgstat.c:5827
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1203
int slru_stats_idx
Definition: slru.h:101
static int SlruSelectLRUPage(SlruCtl ctl, int pageno)
Definition: slru.c:1015
bool * page_dirty
Definition: slru.h:66
SlruShared shared
Definition: slru.h:112
#define SlruRecentlyUsed(shared, slotno)
Definition: slru.c:113

◆ SimpleLruReadPage_ReadOnly()

int SimpleLruReadPage_ReadOnly ( SlruCtl  ctl,
int  pageno,
TransactionId  xid 
)

Definition at line 494 of file slru.c.

References SlruSharedData::ControlLock, LW_EXCLUSIVE, LW_SHARED, LWLockAcquire(), LWLockRelease(), SlruSharedData::num_slots, SlruSharedData::page_number, SlruSharedData::page_status, pgstat_count_slru_page_hit(), SlruCtlData::shared, SimpleLruReadPage(), SLRU_PAGE_EMPTY, SLRU_PAGE_READ_IN_PROGRESS, SlruSharedData::slru_stats_idx, and SlruRecentlyUsed.

Referenced by asyncQueueReadAllNotifications(), find_multixact_start(), SerialGetMinConflictCommitSeqNo(), SubTransGetParent(), TransactionIdGetCommitTsData(), and TransactionIdGetStatus().

495 {
496  SlruShared shared = ctl->shared;
497  int slotno;
498 
499  /* Try to find the page while holding only shared lock */
501 
502  /* See if page is already in a buffer */
503  for (slotno = 0; slotno < shared->num_slots; slotno++)
504  {
505  if (shared->page_number[slotno] == pageno &&
506  shared->page_status[slotno] != SLRU_PAGE_EMPTY &&
507  shared->page_status[slotno] != SLRU_PAGE_READ_IN_PROGRESS)
508  {
509  /* See comments for SlruRecentlyUsed macro */
510  SlruRecentlyUsed(shared, slotno);
511 
512  /* update the stats counter of pages found in the SLRU */
514 
515  return slotno;
516  }
517  }
518 
519  /* No luck, so switch to normal exclusive lock and do regular read */
520  LWLockRelease(shared->ControlLock);
522 
523  return SimpleLruReadPage(ctl, pageno, true, xid);
524 }
LWLock * ControlLock
Definition: slru.h:55
int * page_number
Definition: slru.h:67
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1816
SlruPageStatus * page_status
Definition: slru.h:65
int SimpleLruReadPage(SlruCtl ctl, int pageno, bool write_ok, TransactionId xid)
Definition: slru.c:394
void pgstat_count_slru_page_hit(int slru_idx)
Definition: pgstat.c:5827
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1203
int slru_stats_idx
Definition: slru.h:101
int num_slots
Definition: slru.h:58
SlruShared shared
Definition: slru.h:112
#define SlruRecentlyUsed(shared, slotno)
Definition: slru.c:113

◆ SimpleLruShmemSize()

Size SimpleLruShmemSize ( int  nslots,
int  nlsns 
)

Definition at line 155 of file slru.c.

References BUFFERALIGN, and MAXALIGN.

Referenced by AsyncShmemSize(), CLOGShmemSize(), CommitTsShmemSize(), MultiXactShmemSize(), PredicateLockShmemSize(), SimpleLruInit(), and SUBTRANSShmemSize().

156 {
157  Size sz;
158 
159  /* we assume nslots isn't so large as to risk overflow */
160  sz = MAXALIGN(sizeof(SlruSharedData));
161  sz += MAXALIGN(nslots * sizeof(char *)); /* page_buffer[] */
162  sz += MAXALIGN(nslots * sizeof(SlruPageStatus)); /* page_status[] */
163  sz += MAXALIGN(nslots * sizeof(bool)); /* page_dirty[] */
164  sz += MAXALIGN(nslots * sizeof(int)); /* page_number[] */
165  sz += MAXALIGN(nslots * sizeof(int)); /* page_lru_count[] */
166  sz += MAXALIGN(nslots * sizeof(LWLockPadded)); /* buffer_locks[] */
167 
168  if (nlsns > 0)
169  sz += MAXALIGN(nslots * nlsns * sizeof(XLogRecPtr)); /* group_lsn[] */
170 
171  return BUFFERALIGN(sz) + BLCKSZ * nslots;
172 }
SlruPageStatus
Definition: slru.h:42
uint64 XLogRecPtr
Definition: xlogdefs.h:21
size_t Size
Definition: c.h:540
#define MAXALIGN(LEN)
Definition: c.h:757
#define BUFFERALIGN(LEN)
Definition: c.h:759

◆ SimpleLruTruncate()

void SimpleLruTruncate ( SlruCtl  ctl,
int  cutoffPage 
)

Definition at line 1225 of file slru.c.

References SlruSharedData::ControlLock, SlruCtlData::Dir, ereport, errmsg(), SlruSharedData::latest_page_number, LOG, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), SlruSharedData::num_slots, SlruSharedData::page_dirty, SlruSharedData::page_number, SlruSharedData::page_status, SlruCtlData::PagePrecedes, pgstat_count_slru_truncate(), SlruCtlData::shared, SimpleLruWaitIO(), SLRU_PAGE_EMPTY, SLRU_PAGE_VALID, SlruSharedData::slru_stats_idx, SlruInternalWritePage(), SlruScanDirCbDeleteCutoff(), and SlruScanDirectory().

Referenced by asyncQueueAdvanceTail(), CheckPointPredicate(), clog_redo(), commit_ts_redo(), PerformOffsetsTruncation(), TruncateCLOG(), TruncateCommitTs(), and TruncateSUBTRANS().

1226 {
1227  SlruShared shared = ctl->shared;
1228  int slotno;
1229 
1230  /* update the stats counter of truncates */
1232 
1233  /*
1234  * Scan shared memory and remove any pages preceding the cutoff page, to
1235  * ensure we won't rewrite them later. (Since this is normally called in
1236  * or just after a checkpoint, any dirty pages should have been flushed
1237  * already ... we're just being extra careful here.)
1238  */
1240 
1241 restart:;
1242 
1243  /*
1244  * While we are holding the lock, make an important safety check: the
1245  * current endpoint page must not be eligible for removal.
1246  */
1247  if (ctl->PagePrecedes(shared->latest_page_number, cutoffPage))
1248  {
1249  LWLockRelease(shared->ControlLock);
1250  ereport(LOG,
1251  (errmsg("could not truncate directory \"%s\": apparent wraparound",
1252  ctl->Dir)));
1253  return;
1254  }
1255 
1256  for (slotno = 0; slotno < shared->num_slots; slotno++)
1257  {
1258  if (shared->page_status[slotno] == SLRU_PAGE_EMPTY)
1259  continue;
1260  if (!ctl->PagePrecedes(shared->page_number[slotno], cutoffPage))
1261  continue;
1262 
1263  /*
1264  * If page is clean, just change state to EMPTY (expected case).
1265  */
1266  if (shared->page_status[slotno] == SLRU_PAGE_VALID &&
1267  !shared->page_dirty[slotno])
1268  {
1269  shared->page_status[slotno] = SLRU_PAGE_EMPTY;
1270  continue;
1271  }
1272 
1273  /*
1274  * Hmm, we have (or may have) I/O operations acting on the page, so
1275  * we've got to wait for them to finish and then start again. This is
1276  * the same logic as in SlruSelectLRUPage. (XXX if page is dirty,
1277  * wouldn't it be OK to just discard it without writing it?
1278  * SlruMayDeleteSegment() uses a stricter qualification, so we might
1279  * not delete this page in the end; even if we don't delete it, we
1280  * won't have cause to read its data again. For now, keep the logic
1281  * the same as it was.)
1282  */
1283  if (shared->page_status[slotno] == SLRU_PAGE_VALID)
1284  SlruInternalWritePage(ctl, slotno, NULL);
1285  else
1286  SimpleLruWaitIO(ctl, slotno);
1287  goto restart;
1288  }
1289 
1290  LWLockRelease(shared->ControlLock);
1291 
1292  /* Now we can remove the old segment(s) */
1293  (void) SlruScanDirectory(ctl, SlruScanDirCbDeleteCutoff, &cutoffPage);
1294 }
LWLock * ControlLock
Definition: slru.h:55
int * page_number
Definition: slru.h:67
int latest_page_number
Definition: slru.h:98
#define LOG
Definition: elog.h:26
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1816
SlruPageStatus * page_status
Definition: slru.h:65
static void SlruInternalWritePage(SlruCtl ctl, int slotno, SlruWriteAll fdata)
Definition: slru.c:538
static void SimpleLruWaitIO(SlruCtl ctl, int slotno)
Definition: slru.c:340
static bool SlruScanDirCbDeleteCutoff(SlruCtl ctl, char *filename, int segpage, void *data)
Definition: slru.c:1514
char Dir[64]
Definition: slru.h:136
void pgstat_count_slru_truncate(int slru_idx)
Definition: pgstat.c:5857
#define ereport(elevel,...)
Definition: elog.h:157
bool SlruScanDirectory(SlruCtl ctl, SlruScanCallback callback, void *data)
Definition: slru.c:1552
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1203
int slru_stats_idx
Definition: slru.h:101
int num_slots
Definition: slru.h:58
int errmsg(const char *fmt,...)
Definition: elog.c:909
bool * page_dirty
Definition: slru.h:66
SlruShared shared
Definition: slru.h:112
bool(* PagePrecedes)(int, int)
Definition: slru.h:130

◆ SimpleLruWriteAll()

void SimpleLruWriteAll ( SlruCtl  ctl,
bool  allow_redirtied 
)

Definition at line 1155 of file slru.c.

References Assert, CloseTransientFile(), SlruSharedData::ControlLock, SlruCtlData::Dir, SlruWriteAllData::fd, fsync_fname(), i, InvalidTransactionId, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), SlruWriteAllData::num_files, SlruSharedData::num_slots, SlruSharedData::page_dirty, SlruSharedData::page_status, pgstat_count_slru_flush(), SlruWriteAllData::segno, SlruCtlData::shared, SLRU_CLOSE_FAILED, slru_errcause, slru_errno, SLRU_PAGE_EMPTY, SLRU_PAGE_VALID, SLRU_PAGES_PER_SEGMENT, SlruSharedData::slru_stats_idx, SlruInternalWritePage(), SlruReportIOError(), SlruCtlData::sync_handler, and SYNC_HANDLER_NONE.

Referenced by CheckPointCLOG(), CheckPointCommitTs(), CheckPointMultiXact(), CheckPointPredicate(), CheckPointSUBTRANS(), and find_multixact_start().

1156 {
1157  SlruShared shared = ctl->shared;
1158  SlruWriteAllData fdata;
1159  int slotno;
1160  int pageno = 0;
1161  int i;
1162  bool ok;
1163 
1164  /* update the stats counter of flushes */
1166 
1167  /*
1168  * Find and write dirty pages
1169  */
1170  fdata.num_files = 0;
1171 
1173 
1174  for (slotno = 0; slotno < shared->num_slots; slotno++)
1175  {
1176  SlruInternalWritePage(ctl, slotno, &fdata);
1177 
1178  /*
1179  * In some places (e.g. checkpoints), we cannot assert that the slot
1180  * is clean now, since another process might have re-dirtied it
1181  * already. That's okay.
1182  */
1183  Assert(allow_redirtied ||
1184  shared->page_status[slotno] == SLRU_PAGE_EMPTY ||
1185  (shared->page_status[slotno] == SLRU_PAGE_VALID &&
1186  !shared->page_dirty[slotno]));
1187  }
1188 
1189  LWLockRelease(shared->ControlLock);
1190 
1191  /*
1192  * Now close any files that were open
1193  */
1194  ok = true;
1195  for (i = 0; i < fdata.num_files; i++)
1196  {
1197  if (CloseTransientFile(fdata.fd[i]) != 0)
1198  {
1200  slru_errno = errno;
1201  pageno = fdata.segno[i] * SLRU_PAGES_PER_SEGMENT;
1202  ok = false;
1203  }
1204  }
1205  if (!ok)
1207 
1208  /* Ensure that directory entries for new files are on disk. */
1209  if (ctl->sync_handler != SYNC_HANDLER_NONE)
1210  fsync_fname(ctl->Dir, true);
1211 }
LWLock * ControlLock
Definition: slru.h:55
void fsync_fname(const char *fname, bool isdir)
Definition: fd.c:666
static SlruErrorCause slru_errcause
Definition: slru.c:133
void pgstat_count_slru_flush(int slru_idx)
Definition: pgstat.c:5851
static void SlruReportIOError(SlruCtl ctl, int pageno, TransactionId xid)
Definition: slru.c:930
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1816
int segno[MAX_WRITEALL_BUFFERS]
Definition: slru.c:77
int fd[MAX_WRITEALL_BUFFERS]
Definition: slru.c:76
SlruPageStatus * page_status
Definition: slru.h:65
int num_files
Definition: slru.c:75
static void SlruInternalWritePage(SlruCtl ctl, int slotno, SlruWriteAll fdata)
Definition: slru.c:538
#define InvalidTransactionId
Definition: transam.h:31
int CloseTransientFile(int fd)
Definition: fd.c:2600
char Dir[64]
Definition: slru.h:136
#define Assert(condition)
Definition: c.h:804
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1203
SyncRequestHandler sync_handler
Definition: slru.h:118
int slru_stats_idx
Definition: slru.h:101
int num_slots
Definition: slru.h:58
static int slru_errno
Definition: slru.c:134
bool * page_dirty
Definition: slru.h:66
int i
SlruShared shared
Definition: slru.h:112
#define SLRU_PAGES_PER_SEGMENT
Definition: slru.h:34

◆ SimpleLruWritePage()

void SimpleLruWritePage ( SlruCtl  ctl,
int  slotno 
)

Definition at line 613 of file slru.c.

References SlruInternalWritePage().

Referenced by ActivateCommitTs(), BootStrapCLOG(), BootStrapMultiXact(), BootStrapSUBTRANS(), clog_redo(), commit_ts_redo(), MaybeExtendOffsetSlru(), and multixact_redo().

614 {
615  SlruInternalWritePage(ctl, slotno, NULL);
616 }
static void SlruInternalWritePage(SlruCtl ctl, int slotno, SlruWriteAll fdata)
Definition: slru.c:538

◆ SimpleLruZeroPage()

int SimpleLruZeroPage ( SlruCtl  ctl,
int  pageno 
)

Definition at line 279 of file slru.c.

References Assert, SlruSharedData::latest_page_number, MemSet, SlruSharedData::page_buffer, SlruSharedData::page_dirty, SlruSharedData::page_number, SlruSharedData::page_status, pgstat_count_slru_page_zeroed(), SlruCtlData::shared, SimpleLruZeroLSNs(), SLRU_PAGE_EMPTY, SLRU_PAGE_VALID, SlruSharedData::slru_stats_idx, SlruRecentlyUsed, and SlruSelectLRUPage().

Referenced by asyncQueueAddEntries(), SerialAdd(), ZeroCLOGPage(), ZeroCommitTsPage(), ZeroMultiXactMemberPage(), ZeroMultiXactOffsetPage(), and ZeroSUBTRANSPage().

280 {
281  SlruShared shared = ctl->shared;
282  int slotno;
283 
284  /* Find a suitable buffer slot for the page */
285  slotno = SlruSelectLRUPage(ctl, pageno);
286  Assert(shared->page_status[slotno] == SLRU_PAGE_EMPTY ||
287  (shared->page_status[slotno] == SLRU_PAGE_VALID &&
288  !shared->page_dirty[slotno]) ||
289  shared->page_number[slotno] == pageno);
290 
291  /* Mark the slot as containing this page */
292  shared->page_number[slotno] = pageno;
293  shared->page_status[slotno] = SLRU_PAGE_VALID;
294  shared->page_dirty[slotno] = true;
295  SlruRecentlyUsed(shared, slotno);
296 
297  /* Set the buffer to zeroes */
298  MemSet(shared->page_buffer[slotno], 0, BLCKSZ);
299 
300  /* Set the LSNs for this new page to zero */
301  SimpleLruZeroLSNs(ctl, slotno);
302 
303  /* Assume this page is now the latest active page */
304  shared->latest_page_number = pageno;
305 
306  /* update the stats counter of zeroed pages */
308 
309  return slotno;
310 }
int * page_number
Definition: slru.h:67
int latest_page_number
Definition: slru.h:98
char ** page_buffer
Definition: slru.h:64
#define MemSet(start, val, len)
Definition: c.h:1008
static void SimpleLruZeroLSNs(SlruCtl ctl, int slotno)
Definition: slru.c:323
SlruPageStatus * page_status
Definition: slru.h:65
void pgstat_count_slru_page_zeroed(int slru_idx)
Definition: pgstat.c:5821
#define Assert(condition)
Definition: c.h:804
int slru_stats_idx
Definition: slru.h:101
static int SlruSelectLRUPage(SlruCtl ctl, int pageno)
Definition: slru.c:1015
bool * page_dirty
Definition: slru.h:66
SlruShared shared
Definition: slru.h:112
#define SlruRecentlyUsed(shared, slotno)
Definition: slru.c:113

◆ SlruDeleteSegment()

void SlruDeleteSegment ( SlruCtl  ctl,
int  segno 
)

Definition at line 1326 of file slru.c.

References SlruSharedData::ControlLock, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), SlruSharedData::num_slots, SlruSharedData::page_dirty, SlruSharedData::page_number, SlruSharedData::page_status, SlruCtlData::shared, SimpleLruWaitIO(), SLRU_PAGE_EMPTY, SLRU_PAGE_VALID, SLRU_PAGES_PER_SEGMENT, SlruInternalDeleteSegment(), and SlruInternalWritePage().

Referenced by PerformMembersTruncation().

1327 {
1328  SlruShared shared = ctl->shared;
1329  int slotno;
1330  bool did_write;
1331 
1332  /* Clean out any possibly existing references to the segment. */
1334 restart:
1335  did_write = false;
1336  for (slotno = 0; slotno < shared->num_slots; slotno++)
1337  {
1338  int pagesegno = shared->page_number[slotno] / SLRU_PAGES_PER_SEGMENT;
1339 
1340  if (shared->page_status[slotno] == SLRU_PAGE_EMPTY)
1341  continue;
1342 
1343  /* not the segment we're looking for */
1344  if (pagesegno != segno)
1345  continue;
1346 
1347  /* If page is clean, just change state to EMPTY (expected case). */
1348  if (shared->page_status[slotno] == SLRU_PAGE_VALID &&
1349  !shared->page_dirty[slotno])
1350  {
1351  shared->page_status[slotno] = SLRU_PAGE_EMPTY;
1352  continue;
1353  }
1354 
1355  /* Same logic as SimpleLruTruncate() */
1356  if (shared->page_status[slotno] == SLRU_PAGE_VALID)
1357  SlruInternalWritePage(ctl, slotno, NULL);
1358  else
1359  SimpleLruWaitIO(ctl, slotno);
1360 
1361  did_write = true;
1362  }
1363 
1364  /*
1365  * Be extra careful and re-check. The IO functions release the control
1366  * lock, so new pages could have been read in.
1367  */
1368  if (did_write)
1369  goto restart;
1370 
1371  SlruInternalDeleteSegment(ctl, segno);
1372 
1373  LWLockRelease(shared->ControlLock);
1374 }
LWLock * ControlLock
Definition: slru.h:55
int * page_number
Definition: slru.h:67
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1816
SlruPageStatus * page_status
Definition: slru.h:65
static void SlruInternalWritePage(SlruCtl ctl, int slotno, SlruWriteAll fdata)
Definition: slru.c:538
static void SimpleLruWaitIO(SlruCtl ctl, int slotno)
Definition: slru.c:340
static void SlruInternalDeleteSegment(SlruCtl ctl, int segno)
Definition: slru.c:1303
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1203
int num_slots
Definition: slru.h:58
bool * page_dirty
Definition: slru.h:66
SlruShared shared
Definition: slru.h:112
#define SLRU_PAGES_PER_SEGMENT
Definition: slru.h:34

◆ SlruScanDirCbDeleteAll()

bool SlruScanDirCbDeleteAll ( SlruCtl  ctl,
char *  filename,
int  segpage,
void *  data 
)

Definition at line 1529 of file slru.c.

References SLRU_PAGES_PER_SEGMENT, and SlruInternalDeleteSegment().

Referenced by AsyncShmemInit(), and DeactivateCommitTs().

1530 {
1532 
1533  return false; /* keep going */
1534 }
static void SlruInternalDeleteSegment(SlruCtl ctl, int segno)
Definition: slru.c:1303
#define SLRU_PAGES_PER_SEGMENT
Definition: slru.h:34

◆ SlruScanDirCbReportPresence()

bool SlruScanDirCbReportPresence ( SlruCtl  ctl,
char *  filename,
int  segpage,
void *  data 
)

Definition at line 1499 of file slru.c.

References SlruMayDeleteSegment().

Referenced by TruncateCLOG(), and TruncateCommitTs().

1500 {
1501  int cutoffPage = *(int *) data;
1502 
1503  if (SlruMayDeleteSegment(ctl, segpage, cutoffPage))
1504  return true; /* found one; don't iterate any more */
1505 
1506  return false; /* keep going */
1507 }
static bool SlruMayDeleteSegment(SlruCtl ctl, int segpage, int cutoffPage)
Definition: slru.c:1390

◆ SlruScanDirectory()

bool SlruScanDirectory ( SlruCtl  ctl,
SlruScanCallback  callback,
void *  data 
)

Definition at line 1552 of file slru.c.

References AllocateDir(), callback(), dirent::d_name, DEBUG2, SlruCtlData::Dir, elog, FreeDir(), ReadDir(), SlruWriteAllData::segno, and SLRU_PAGES_PER_SEGMENT.

Referenced by AsyncShmemInit(), DeactivateCommitTs(), SimpleLruTruncate(), TruncateCLOG(), TruncateCommitTs(), and TruncateMultiXact().

1553 {
1554  bool retval = false;
1555  DIR *cldir;
1556  struct dirent *clde;
1557  int segno;
1558  int segpage;
1559 
1560  cldir = AllocateDir(ctl->Dir);
1561  while ((clde = ReadDir(cldir, ctl->Dir)) != NULL)
1562  {
1563  size_t len;
1564 
1565  len = strlen(clde->d_name);
1566 
1567  if ((len == 4 || len == 5 || len == 6) &&
1568  strspn(clde->d_name, "0123456789ABCDEF") == len)
1569  {
1570  segno = (int) strtol(clde->d_name, NULL, 16);
1571  segpage = segno * SLRU_PAGES_PER_SEGMENT;
1572 
1573  elog(DEBUG2, "SlruScanDirectory invoking callback on %s/%s",
1574  ctl->Dir, clde->d_name);
1575  retval = callback(ctl, clde->d_name, segpage, data);
1576  if (retval)
1577  break;
1578  }
1579  }
1580  FreeDir(cldir);
1581 
1582  return retval;
1583 }
Definition: dirent.h:9
Definition: dirent.c:25
static void callback(struct sockaddr *addr, struct sockaddr *mask, void *unused)
Definition: test_ifaddrs.c:48
#define DEBUG2
Definition: elog.h:24
DIR * AllocateDir(const char *dirname)
Definition: fd.c:2634
char Dir[64]
Definition: slru.h:136
struct dirent * ReadDir(DIR *dir, const char *dirname)
Definition: fd.c:2700
#define elog(elevel,...)
Definition: elog.h:232
char d_name[MAX_PATH]
Definition: dirent.h:15
#define SLRU_PAGES_PER_SEGMENT
Definition: slru.h:34
int FreeDir(DIR *dir)
Definition: fd.c:2752

◆ SlruSyncFileTag()

int SlruSyncFileTag ( SlruCtl  ctl,
const FileTag ftag,
char *  path 
)

Definition at line 1592 of file slru.c.

References CloseTransientFile(), SlruWriteAllData::fd, OpenTransientFile(), PG_BINARY, pg_fsync(), FileTag::segno, and SlruFileName.

Referenced by clogsyncfiletag(), committssyncfiletag(), multixactmemberssyncfiletag(), and multixactoffsetssyncfiletag().

1593 {
1594  int fd;
1595  int save_errno;
1596  int result;
1597 
1598  SlruFileName(ctl, path, ftag->segno);
1599 
1600  fd = OpenTransientFile(path, O_RDWR | PG_BINARY);
1601  if (fd < 0)
1602  return -1;
1603 
1604  result = pg_fsync(fd);
1605  save_errno = errno;
1606 
1607  CloseTransientFile(fd);
1608 
1609  errno = save_errno;
1610  return result;
1611 }
uint32 segno
Definition: sync.h:55
static int fd(const char *x, int i)
Definition: preproc-init.c:105
#define PG_BINARY
Definition: c.h:1271
int OpenTransientFile(const char *fileName, int fileFlags)
Definition: fd.c:2423
int CloseTransientFile(int fd)
Definition: fd.c:2600
#define SlruFileName(ctl, path, seg)
Definition: slru.c:62
int pg_fsync(int fd)
Definition: fd.c:352