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 626 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().

627 {
628  int segno = pageno / SLRU_PAGES_PER_SEGMENT;
629  int rpageno = pageno % SLRU_PAGES_PER_SEGMENT;
630  int offset = rpageno * BLCKSZ;
631  char path[MAXPGPATH];
632  int fd;
633  bool result;
634  off_t endpos;
635 
636  /* update the stats counter of checked pages */
638 
639  SlruFileName(ctl, path, segno);
640 
641  fd = OpenTransientFile(path, O_RDONLY | PG_BINARY);
642  if (fd < 0)
643  {
644  /* expected: file doesn't exist */
645  if (errno == ENOENT)
646  return false;
647 
648  /* report error normally */
650  slru_errno = errno;
651  SlruReportIOError(ctl, pageno, 0);
652  }
653 
654  if ((endpos = lseek(fd, 0, SEEK_END)) < 0)
655  {
657  slru_errno = errno;
658  SlruReportIOError(ctl, pageno, 0);
659  }
660 
661  result = endpos >= (off_t) (offset + BLCKSZ);
662 
663  if (CloseTransientFile(fd) != 0)
664  {
666  slru_errno = errno;
667  return false;
668  }
669 
670  return result;
671 }
static SlruErrorCause slru_errcause
Definition: slru.c:134
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:931
#define PG_BINARY
Definition: c.h:1271
int OpenTransientFile(const char *fileName, int fileFlags)
Definition: fd.c:2509
#define MAXPGPATH
static XLogRecPtr endpos
Definition: pg_receivewal.c:48
int CloseTransientFile(int fd)
Definition: fd.c:2686
void pgstat_count_slru_page_exists(int slru_idx)
Definition: pgstat.c:5960
#define SlruFileName(ctl, path, seg)
Definition: slru.c:63
int slru_stats_idx
Definition: slru.h:101
static int slru_errno
Definition: slru.c:135
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 187 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().

190 {
191  SlruShared shared;
192  bool found;
193 
194  shared = (SlruShared) ShmemInitStruct(name,
195  SimpleLruShmemSize(nslots, nlsns),
196  &found);
197 
198  if (!IsUnderPostmaster)
199  {
200  /* Initialize locks and shared memory area */
201  char *ptr;
202  Size offset;
203  int slotno;
204 
205  Assert(!found);
206 
207  memset(shared, 0, sizeof(SlruSharedData));
208 
209  shared->ControlLock = ctllock;
210 
211  shared->num_slots = nslots;
212  shared->lsn_groups_per_page = nlsns;
213 
214  shared->cur_lru_count = 0;
215 
216  /* shared->latest_page_number will be set later */
217 
219 
220  ptr = (char *) shared;
221  offset = MAXALIGN(sizeof(SlruSharedData));
222  shared->page_buffer = (char **) (ptr + offset);
223  offset += MAXALIGN(nslots * sizeof(char *));
224  shared->page_status = (SlruPageStatus *) (ptr + offset);
225  offset += MAXALIGN(nslots * sizeof(SlruPageStatus));
226  shared->page_dirty = (bool *) (ptr + offset);
227  offset += MAXALIGN(nslots * sizeof(bool));
228  shared->page_number = (int *) (ptr + offset);
229  offset += MAXALIGN(nslots * sizeof(int));
230  shared->page_lru_count = (int *) (ptr + offset);
231  offset += MAXALIGN(nslots * sizeof(int));
232 
233  /* Initialize LWLocks */
234  shared->buffer_locks = (LWLockPadded *) (ptr + offset);
235  offset += MAXALIGN(nslots * sizeof(LWLockPadded));
236 
237  if (nlsns > 0)
238  {
239  shared->group_lsn = (XLogRecPtr *) (ptr + offset);
240  offset += MAXALIGN(nslots * nlsns * sizeof(XLogRecPtr));
241  }
242 
243  ptr += BUFFERALIGN(offset);
244  for (slotno = 0; slotno < nslots; slotno++)
245  {
246  LWLockInitialize(&shared->buffer_locks[slotno].lock,
247  tranche_id);
248 
249  shared->page_buffer[slotno] = ptr;
250  shared->page_status[slotno] = SLRU_PAGE_EMPTY;
251  shared->page_dirty[slotno] = false;
252  shared->page_lru_count[slotno] = 0;
253  ptr += BLCKSZ;
254  }
255 
256  /* Should fit to estimated shmem size */
257  Assert(ptr - (char *) shared <= SimpleLruShmemSize(nslots, nlsns));
258  }
259  else
260  Assert(found);
261 
262  /*
263  * Initialize the unshared control struct, including directory path. We
264  * assume caller set PagePrecedes.
265  */
266  ctl->shared = shared;
267  ctl->sync_handler = sync_handler;
268  strlcpy(ctl->Dir, subdir, sizeof(ctl->Dir));
269 }
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:156
int pgstat_slru_index(const char *name)
Definition: pgstat.c:5891
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:736
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:561
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 395 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().

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

◆ SimpleLruReadPage_ReadOnly()

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

Definition at line 495 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().

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

◆ SimpleLruShmemSize()

Size SimpleLruShmemSize ( int  nslots,
int  nlsns 
)

Definition at line 156 of file slru.c.

References BUFFERALIGN, and MAXALIGN.

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

157 {
158  Size sz;
159 
160  /* we assume nslots isn't so large as to risk overflow */
161  sz = MAXALIGN(sizeof(SlruSharedData));
162  sz += MAXALIGN(nslots * sizeof(char *)); /* page_buffer[] */
163  sz += MAXALIGN(nslots * sizeof(SlruPageStatus)); /* page_status[] */
164  sz += MAXALIGN(nslots * sizeof(bool)); /* page_dirty[] */
165  sz += MAXALIGN(nslots * sizeof(int)); /* page_number[] */
166  sz += MAXALIGN(nslots * sizeof(int)); /* page_lru_count[] */
167  sz += MAXALIGN(nslots * sizeof(LWLockPadded)); /* buffer_locks[] */
168 
169  if (nlsns > 0)
170  sz += MAXALIGN(nslots * nlsns * sizeof(XLogRecPtr)); /* group_lsn[] */
171 
172  return BUFFERALIGN(sz) + BLCKSZ * nslots;
173 }
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 1226 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().

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

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

References SlruInternalWritePage().

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

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

◆ SimpleLruZeroPage()

int SimpleLruZeroPage ( SlruCtl  ctl,
int  pageno 
)

Definition at line 280 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().

281 {
282  SlruShared shared = ctl->shared;
283  int slotno;
284 
285  /* Find a suitable buffer slot for the page */
286  slotno = SlruSelectLRUPage(ctl, pageno);
287  Assert(shared->page_status[slotno] == SLRU_PAGE_EMPTY ||
288  (shared->page_status[slotno] == SLRU_PAGE_VALID &&
289  !shared->page_dirty[slotno]) ||
290  shared->page_number[slotno] == pageno);
291 
292  /* Mark the slot as containing this page */
293  shared->page_number[slotno] = pageno;
294  shared->page_status[slotno] = SLRU_PAGE_VALID;
295  shared->page_dirty[slotno] = true;
296  SlruRecentlyUsed(shared, slotno);
297 
298  /* Set the buffer to zeroes */
299  MemSet(shared->page_buffer[slotno], 0, BLCKSZ);
300 
301  /* Set the LSNs for this new page to zero */
302  SimpleLruZeroLSNs(ctl, slotno);
303 
304  /* Assume this page is now the latest active page */
305  shared->latest_page_number = pageno;
306 
307  /* update the stats counter of zeroed pages */
309 
310  return slotno;
311 }
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:324
SlruPageStatus * page_status
Definition: slru.h:65
void pgstat_count_slru_page_zeroed(int slru_idx)
Definition: pgstat.c:5948
#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:1016
bool * page_dirty
Definition: slru.h:66
SlruShared shared
Definition: slru.h:112
#define SlruRecentlyUsed(shared, slotno)
Definition: slru.c:114

◆ SlruDeleteSegment()

void SlruDeleteSegment ( SlruCtl  ctl,
int  segno 
)

Definition at line 1327 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().

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

References SLRU_PAGES_PER_SEGMENT, and SlruInternalDeleteSegment().

Referenced by AsyncShmemInit(), and DeactivateCommitTs().

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

◆ SlruScanDirCbReportPresence()

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

Definition at line 1500 of file slru.c.

References SlruMayDeleteSegment().

Referenced by TruncateCLOG(), and TruncateCommitTs().

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

◆ SlruScanDirectory()

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

Definition at line 1553 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().

1554 {
1555  bool retval = false;
1556  DIR *cldir;
1557  struct dirent *clde;
1558  int segno;
1559  int segpage;
1560 
1561  cldir = AllocateDir(ctl->Dir);
1562  while ((clde = ReadDir(cldir, ctl->Dir)) != NULL)
1563  {
1564  size_t len;
1565 
1566  len = strlen(clde->d_name);
1567 
1568  if ((len == 4 || len == 5 || len == 6) &&
1569  strspn(clde->d_name, "0123456789ABCDEF") == len)
1570  {
1571  segno = (int) strtol(clde->d_name, NULL, 16);
1572  segpage = segno * SLRU_PAGES_PER_SEGMENT;
1573 
1574  elog(DEBUG2, "SlruScanDirectory invoking callback on %s/%s",
1575  ctl->Dir, clde->d_name);
1576  retval = callback(ctl, clde->d_name, segpage, data);
1577  if (retval)
1578  break;
1579  }
1580  }
1581  FreeDir(cldir);
1582 
1583  return retval;
1584 }
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:2720
char Dir[64]
Definition: slru.h:136
struct dirent * ReadDir(DIR *dir, const char *dirname)
Definition: fd.c:2786
#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:2838

◆ SlruSyncFileTag()

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

Definition at line 1593 of file slru.c.

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

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

1594 {
1595  int fd;
1596  int save_errno;
1597  int result;
1598 
1599  SlruFileName(ctl, path, ftag->segno);
1600 
1601  fd = OpenTransientFile(path, O_RDWR | PG_BINARY);
1602  if (fd < 0)
1603  return -1;
1604 
1605  result = pg_fsync(fd);
1606  save_errno = errno;
1607 
1608  CloseTransientFile(fd);
1609 
1610  errno = save_errno;
1611  return result;
1612 }
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:2509
int CloseTransientFile(int fd)
Definition: fd.c:2686
#define SlruFileName(ctl, path, seg)
Definition: slru.c:63
int pg_fsync(int fd)
Definition: fd.c:357