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
 

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

Typedef Documentation

◆ SlruCtl

typedef SlruCtlData* SlruCtl

Definition at line 134 of file slru.h.

◆ SlruCtlData

typedef struct SlruCtlData SlruCtlData

◆ SlruScanCallback

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

Definition at line 151 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:1211
int OpenTransientFile(const char *fileName, int fileFlags)
Definition: fd.c:2372
#define MAXPGPATH
static XLogRecPtr endpos
Definition: pg_receivewal.c:46
int CloseTransientFile(int fd)
Definition: fd.c:2549
void pgstat_count_slru_page_exists(int slru_idx)
Definition: pgstat.c:6778
#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:6711
SlruPageStatus * page_status
Definition: slru.h:65
void * ShmemInitStruct(const char *name, Size size, bool *foundPtr)
Definition: shmem.c:392
bool IsUnderPostmaster
Definition: globals.c:109
LWLockPadded * buffer_locks
Definition: slru.h:69
XLogRecPtr * group_lsn
Definition: slru.h:79
void LWLockInitialize(LWLock *lock, int tranche_id)
Definition: lwlock.c:745
SlruSharedData * SlruShared
Definition: slru.h:104
char Dir[64]
Definition: slru.h:131
LWLock lock
Definition: lwlock.h:78
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:745
size_t Size
Definition: c.h:473
#define MAXALIGN(LEN)
Definition: c.h:698
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:700

◆ 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:1812
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:6784
static void SimpleLruWaitIO(SlruCtl ctl, int slotno)
Definition: slru.c:340
LWLock lock
Definition: lwlock.h:78
static bool SlruPhysicalReadPage(SlruCtl ctl, int pageno, int slotno)
Definition: slru.c:683
#define Assert(condition)
Definition: c.h:745
void pgstat_count_slru_page_hit(int slru_idx)
Definition: pgstat.c:6772
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1208
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:1812
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:6772
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1208
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:473
#define MAXALIGN(LEN)
Definition: c.h:698
#define BUFFERALIGN(LEN)
Definition: c.h:700

◆ 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, SLRU_PAGES_PER_SEGMENT, 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  * The cutoff point is the start of the segment containing cutoffPage.
1235  */
1236  cutoffPage -= cutoffPage % SLRU_PAGES_PER_SEGMENT;
1237 
1238  /*
1239  * Scan shared memory and remove any pages preceding the cutoff page, to
1240  * ensure we won't rewrite them later. (Since this is normally called in
1241  * or just after a checkpoint, any dirty pages should have been flushed
1242  * already ... we're just being extra careful here.)
1243  */
1245 
1246 restart:;
1247 
1248  /*
1249  * While we are holding the lock, make an important safety check: the
1250  * planned cutoff point must be <= the current endpoint page. Otherwise we
1251  * have already wrapped around, and proceeding with the truncation would
1252  * risk removing the current segment.
1253  */
1254  if (ctl->PagePrecedes(shared->latest_page_number, cutoffPage))
1255  {
1256  LWLockRelease(shared->ControlLock);
1257  ereport(LOG,
1258  (errmsg("could not truncate directory \"%s\": apparent wraparound",
1259  ctl->Dir)));
1260  return;
1261  }
1262 
1263  for (slotno = 0; slotno < shared->num_slots; slotno++)
1264  {
1265  if (shared->page_status[slotno] == SLRU_PAGE_EMPTY)
1266  continue;
1267  if (!ctl->PagePrecedes(shared->page_number[slotno], cutoffPage))
1268  continue;
1269 
1270  /*
1271  * If page is clean, just change state to EMPTY (expected case).
1272  */
1273  if (shared->page_status[slotno] == SLRU_PAGE_VALID &&
1274  !shared->page_dirty[slotno])
1275  {
1276  shared->page_status[slotno] = SLRU_PAGE_EMPTY;
1277  continue;
1278  }
1279 
1280  /*
1281  * Hmm, we have (or may have) I/O operations acting on the page, so
1282  * we've got to wait for them to finish and then start again. This is
1283  * the same logic as in SlruSelectLRUPage. (XXX if page is dirty,
1284  * wouldn't it be OK to just discard it without writing it? For now,
1285  * keep the logic the same as it was.)
1286  */
1287  if (shared->page_status[slotno] == SLRU_PAGE_VALID)
1288  SlruInternalWritePage(ctl, slotno, NULL);
1289  else
1290  SimpleLruWaitIO(ctl, slotno);
1291  goto restart;
1292  }
1293 
1294  LWLockRelease(shared->ControlLock);
1295 
1296  /* Now we can remove the old segment(s) */
1297  (void) SlruScanDirectory(ctl, SlruScanDirCbDeleteCutoff, &cutoffPage);
1298 }
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:1812
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:1411
char Dir[64]
Definition: slru.h:131
void pgstat_count_slru_truncate(int slru_idx)
Definition: pgstat.c:6802
#define ereport(elevel,...)
Definition: elog.h:144
bool SlruScanDirectory(SlruCtl ctl, SlruScanCallback callback, void *data)
Definition: slru.c:1449
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1208
int slru_stats_idx
Definition: slru.h:101
int num_slots
Definition: slru.h:58
int errmsg(const char *fmt,...)
Definition: elog.c:824
bool * page_dirty
Definition: slru.h:66
SlruShared shared
Definition: slru.h:112
bool(* PagePrecedes)(int, int)
Definition: slru.h:125
#define SLRU_PAGES_PER_SEGMENT
Definition: slru.h:34

◆ 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:633
static SlruErrorCause slru_errcause
Definition: slru.c:133
void pgstat_count_slru_flush(int slru_idx)
Definition: pgstat.c:6796
static void SlruReportIOError(SlruCtl ctl, int pageno, TransactionId xid)
Definition: slru.c:930
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1812
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:2549
char Dir[64]
Definition: slru.h:131
#define Assert(condition)
Definition: c.h:745
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1208
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:949
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:6766
#define Assert(condition)
Definition: c.h:745
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 1321 of file slru.c.

References SlruSharedData::ControlLock, DEBUG2, SlruCtlData::Dir, ereport, errmsg(), INIT_SLRUFILETAG, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), MAXPGPATH, SlruSharedData::num_slots, SlruSharedData::page_dirty, SlruSharedData::page_number, SlruSharedData::page_status, RegisterSyncRequest(), SlruCtlData::shared, SimpleLruWaitIO(), SLRU_PAGE_EMPTY, SLRU_PAGE_VALID, SLRU_PAGES_PER_SEGMENT, SlruInternalWritePage(), snprintf, SYNC_FORGET_REQUEST, SlruCtlData::sync_handler, and SYNC_HANDLER_NONE.

Referenced by PerformMembersTruncation().

1322 {
1323  SlruShared shared = ctl->shared;
1324  int slotno;
1325  char path[MAXPGPATH];
1326  bool did_write;
1327 
1328  /* Clean out any possibly existing references to the segment. */
1330 restart:
1331  did_write = false;
1332  for (slotno = 0; slotno < shared->num_slots; slotno++)
1333  {
1334  int pagesegno = shared->page_number[slotno] / SLRU_PAGES_PER_SEGMENT;
1335 
1336  if (shared->page_status[slotno] == SLRU_PAGE_EMPTY)
1337  continue;
1338 
1339  /* not the segment we're looking for */
1340  if (pagesegno != segno)
1341  continue;
1342 
1343  /* If page is clean, just change state to EMPTY (expected case). */
1344  if (shared->page_status[slotno] == SLRU_PAGE_VALID &&
1345  !shared->page_dirty[slotno])
1346  {
1347  shared->page_status[slotno] = SLRU_PAGE_EMPTY;
1348  continue;
1349  }
1350 
1351  /* Same logic as SimpleLruTruncate() */
1352  if (shared->page_status[slotno] == SLRU_PAGE_VALID)
1353  SlruInternalWritePage(ctl, slotno, NULL);
1354  else
1355  SimpleLruWaitIO(ctl, slotno);
1356 
1357  did_write = true;
1358  }
1359 
1360  /*
1361  * Be extra careful and re-check. The IO functions release the control
1362  * lock, so new pages could have been read in.
1363  */
1364  if (did_write)
1365  goto restart;
1366 
1367  snprintf(path, MAXPGPATH, "%s/%04X", ctl->Dir, segno);
1368  ereport(DEBUG2,
1369  (errmsg("removing file \"%s\"", path)));
1370 
1371  /*
1372  * Tell the checkpointer to forget any sync requests, before we unlink the
1373  * file.
1374  */
1375  if (ctl->sync_handler != SYNC_HANDLER_NONE)
1376  {
1377  FileTag tag;
1378 
1379  INIT_SLRUFILETAG(tag, ctl->sync_handler, segno);
1381  }
1382 
1383  unlink(path);
1384 
1385  LWLockRelease(shared->ControlLock);
1386 }
LWLock * ControlLock
Definition: slru.h:55
int * page_number
Definition: slru.h:67
#define INIT_SLRUFILETAG(a, xx_handler, xx_segno)
Definition: slru.c:87
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1812
SlruPageStatus * page_status
Definition: slru.h:65
#define MAXPGPATH
static void SlruInternalWritePage(SlruCtl ctl, int slotno, SlruWriteAll fdata)
Definition: slru.c:538
#define DEBUG2
Definition: elog.h:24
static void SimpleLruWaitIO(SlruCtl ctl, int slotno)
Definition: slru.c:340
char Dir[64]
Definition: slru.h:131
bool RegisterSyncRequest(const FileTag *ftag, SyncRequestType type, bool retryOnError)
Definition: sync.c:554
#define ereport(elevel,...)
Definition: elog.h:144
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1208
SyncRequestHandler sync_handler
Definition: slru.h:118
int num_slots
Definition: slru.h:58
int errmsg(const char *fmt,...)
Definition: elog.c:824
bool * page_dirty
Definition: slru.h:66
SlruShared shared
Definition: slru.h:112
#define SLRU_PAGES_PER_SEGMENT
Definition: slru.h:34
#define snprintf
Definition: port.h:193
Definition: sync.h:50

◆ SlruScanDirCbDeleteAll()

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

Definition at line 1426 of file slru.c.

References SlruInternalDeleteSegment().

Referenced by AsyncShmemInit(), and DeactivateCommitTs().

1427 {
1429 
1430  return false; /* keep going */
1431 }
static void SlruInternalDeleteSegment(SlruCtl ctl, char *filename)
Definition: slru.c:1307
static char * filename
Definition: pg_dumpall.c:91

◆ SlruScanDirCbReportPresence()

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

Definition at line 1394 of file slru.c.

References SlruCtlData::PagePrecedes, and SLRU_PAGES_PER_SEGMENT.

Referenced by TruncateCLOG(), and TruncateCommitTs().

1395 {
1396  int cutoffPage = *(int *) data;
1397 
1398  cutoffPage -= cutoffPage % SLRU_PAGES_PER_SEGMENT;
1399 
1400  if (ctl->PagePrecedes(segpage, cutoffPage))
1401  return true; /* found one; don't iterate any more */
1402 
1403  return false; /* keep going */
1404 }
bool(* PagePrecedes)(int, int)
Definition: slru.h:125
#define SLRU_PAGES_PER_SEGMENT
Definition: slru.h:34

◆ SlruScanDirectory()

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

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

1450 {
1451  bool retval = false;
1452  DIR *cldir;
1453  struct dirent *clde;
1454  int segno;
1455  int segpage;
1456 
1457  cldir = AllocateDir(ctl->Dir);
1458  while ((clde = ReadDir(cldir, ctl->Dir)) != NULL)
1459  {
1460  size_t len;
1461 
1462  len = strlen(clde->d_name);
1463 
1464  if ((len == 4 || len == 5 || len == 6) &&
1465  strspn(clde->d_name, "0123456789ABCDEF") == len)
1466  {
1467  segno = (int) strtol(clde->d_name, NULL, 16);
1468  segpage = segno * SLRU_PAGES_PER_SEGMENT;
1469 
1470  elog(DEBUG2, "SlruScanDirectory invoking callback on %s/%s",
1471  ctl->Dir, clde->d_name);
1472  retval = callback(ctl, clde->d_name, segpage, data);
1473  if (retval)
1474  break;
1475  }
1476  }
1477  FreeDir(cldir);
1478 
1479  return retval;
1480 }
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:2583
char Dir[64]
Definition: slru.h:131
struct dirent * ReadDir(DIR *dir, const char *dirname)
Definition: fd.c:2649
#define elog(elevel,...)
Definition: elog.h:214
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:2701

◆ SlruSyncFileTag()

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

Definition at line 1489 of file slru.c.

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

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

1490 {
1491  int fd;
1492  int save_errno;
1493  int result;
1494 
1495  SlruFileName(ctl, path, ftag->segno);
1496 
1497  fd = OpenTransientFile(path, O_RDWR | PG_BINARY);
1498  if (fd < 0)
1499  return -1;
1500 
1501  result = pg_fsync(fd);
1502  save_errno = errno;
1503 
1504  CloseTransientFile(fd);
1505 
1506  errno = save_errno;
1507  return result;
1508 }
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:1211
int OpenTransientFile(const char *fileName, int fileFlags)
Definition: fd.c:2372
int CloseTransientFile(int fd)
Definition: fd.c:2549
#define SlruFileName(ctl, path, seg)
Definition: slru.c:62
int pg_fsync(int fd)
Definition: fd.c:346