PostgreSQL Source Code  git master
slru.h File Reference
#include "access/xlogdefs.h"
#include "storage/lwlock.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 SLRU_MAX_NAME_LENGTH   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)
 
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 SimpleLruFlush (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)
 
bool SlruScanDirCbReportPresence (SlruCtl ctl, char *filename, int segpage, void *data)
 
bool SlruScanDirCbDeleteAll (SlruCtl ctl, char *filename, int segpage, void *data)
 

Macro Definition Documentation

◆ SLRU_MAX_NAME_LENGTH

#define SLRU_MAX_NAME_LENGTH   32

Definition at line 36 of file slru.h.

Referenced by SimpleLruInit().

◆ SLRU_PAGES_PER_SEGMENT

Typedef Documentation

◆ SlruCtl

Definition at line 137 of file slru.h.

◆ SlruCtlData

◆ SlruScanCallback

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

Definition at line 153 of file slru.h.

◆ SlruShared

Definition at line 107 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 44 of file slru.h.

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

Function Documentation

◆ SimpleLruDoesPhysicalPageExist()

bool SimpleLruDoesPhysicalPageExist ( SlruCtl  ctl,
int  pageno 
)

Definition at line 590 of file slru.c.

References CloseTransientFile(), endpos, SlruFlushData::fd, MAXPGPATH, OpenTransientFile(), PG_BINARY, SlruFlushData::segno, slru_errcause, slru_errno, SLRU_OPEN_FAILED, SLRU_PAGES_PER_SEGMENT, SlruFileName, and SlruReportIOError().

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

591 {
592  int segno = pageno / SLRU_PAGES_PER_SEGMENT;
593  int rpageno = pageno % SLRU_PAGES_PER_SEGMENT;
594  int offset = rpageno * BLCKSZ;
595  char path[MAXPGPATH];
596  int fd;
597  bool result;
598  off_t endpos;
599 
600  SlruFileName(ctl, path, segno);
601 
602  fd = OpenTransientFile(path, O_RDWR | PG_BINARY);
603  if (fd < 0)
604  {
605  /* expected: file doesn't exist */
606  if (errno == ENOENT)
607  return false;
608 
609  /* report error normally */
611  slru_errno = errno;
612  SlruReportIOError(ctl, pageno, 0);
613  }
614 
615  if ((endpos = lseek(fd, 0, SEEK_END)) < 0)
616  {
618  slru_errno = errno;
619  SlruReportIOError(ctl, pageno, 0);
620  }
621 
622  result = endpos >= (off_t) (offset + BLCKSZ);
623 
624  CloseTransientFile(fd);
625  return result;
626 }
static SlruErrorCause slru_errcause
Definition: slru.c:123
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:892
#define PG_BINARY
Definition: c.h:1035
int OpenTransientFile(const char *fileName, int fileFlags)
Definition: fd.c:2392
#define MAXPGPATH
static XLogRecPtr endpos
Definition: pg_receivewal.c:46
int CloseTransientFile(int fd)
Definition: fd.c:2562
#define SlruFileName(ctl, path, seg)
Definition: slru.c:63
static int slru_errno
Definition: slru.c:124
#define SLRU_PAGES_PER_SEGMENT
Definition: slru.h:33

◆ SimpleLruFlush()

void SimpleLruFlush ( SlruCtl  ctl,
bool  allow_redirtied 
)

Definition at line 1103 of file slru.c.

References Assert, CloseTransientFile(), SlruSharedData::ControlLock, SlruCtlData::do_fsync, SlruFlushData::fd, i, InvalidTransactionId, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), SlruFlushData::num_files, SlruSharedData::num_slots, SlruSharedData::page_dirty, SlruSharedData::page_status, pg_fsync(), pgstat_report_wait_end(), pgstat_report_wait_start(), SlruFlushData::segno, SlruCtlData::shared, SLRU_CLOSE_FAILED, slru_errcause, slru_errno, SLRU_FSYNC_FAILED, SLRU_PAGE_EMPTY, SLRU_PAGE_VALID, SLRU_PAGES_PER_SEGMENT, SlruInternalWritePage(), SlruReportIOError(), and WAIT_EVENT_SLRU_FLUSH_SYNC.

Referenced by CheckPointCLOG(), CheckPointCommitTs(), CheckPointMultiXact(), CheckPointPredicate(), CheckPointSUBTRANS(), find_multixact_start(), ShutdownCLOG(), ShutdownCommitTs(), ShutdownMultiXact(), and ShutdownSUBTRANS().

1104 {
1105  SlruShared shared = ctl->shared;
1106  SlruFlushData fdata;
1107  int slotno;
1108  int pageno = 0;
1109  int i;
1110  bool ok;
1111 
1112  /*
1113  * Find and write dirty pages
1114  */
1115  fdata.num_files = 0;
1116 
1118 
1119  for (slotno = 0; slotno < shared->num_slots; slotno++)
1120  {
1121  SlruInternalWritePage(ctl, slotno, &fdata);
1122 
1123  /*
1124  * In some places (e.g. checkpoints), we cannot assert that the slot
1125  * is clean now, since another process might have re-dirtied it
1126  * already. That's okay.
1127  */
1128  Assert(allow_redirtied ||
1129  shared->page_status[slotno] == SLRU_PAGE_EMPTY ||
1130  (shared->page_status[slotno] == SLRU_PAGE_VALID &&
1131  !shared->page_dirty[slotno]));
1132  }
1133 
1134  LWLockRelease(shared->ControlLock);
1135 
1136  /*
1137  * Now fsync and close any files that were open
1138  */
1139  ok = true;
1140  for (i = 0; i < fdata.num_files; i++)
1141  {
1143  if (ctl->do_fsync && pg_fsync(fdata.fd[i]))
1144  {
1146  slru_errno = errno;
1147  pageno = fdata.segno[i] * SLRU_PAGES_PER_SEGMENT;
1148  ok = false;
1149  }
1151 
1152  if (CloseTransientFile(fdata.fd[i]))
1153  {
1155  slru_errno = errno;
1156  pageno = fdata.segno[i] * SLRU_PAGES_PER_SEGMENT;
1157  ok = false;
1158  }
1159  }
1160  if (!ok)
1162 }
LWLock * ControlLock
Definition: slru.h:57
static void SlruInternalWritePage(SlruCtl ctl, int slotno, SlruFlush fdata)
Definition: slru.c:507
static SlruErrorCause slru_errcause
Definition: slru.c:123
int segno[MAX_FLUSH_BUFFERS]
Definition: slru.c:79
static void SlruReportIOError(SlruCtl ctl, int pageno, TransactionId xid)
Definition: slru.c:892
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1722
SlruPageStatus * page_status
Definition: slru.h:67
#define InvalidTransactionId
Definition: transam.h:31
static void pgstat_report_wait_end(void)
Definition: pgstat.h:1244
bool do_fsync
Definition: slru.h:121
int CloseTransientFile(int fd)
Definition: fd.c:2562
int num_files
Definition: slru.c:77
#define Assert(condition)
Definition: c.h:680
static void pgstat_report_wait_start(uint32 wait_event_info)
Definition: pgstat.h:1220
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1118
int num_slots
Definition: slru.h:60
static int slru_errno
Definition: slru.c:124
bool * page_dirty
Definition: slru.h:68
int i
SlruShared shared
Definition: slru.h:115
int pg_fsync(int fd)
Definition: fd.c:347
#define SLRU_PAGES_PER_SEGMENT
Definition: slru.h:33
int fd[MAX_FLUSH_BUFFERS]
Definition: slru.c:78

◆ SimpleLruInit()

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

Definition at line 165 of file slru.c.

References Assert, SlruSharedData::buffer_locks, BUFFERALIGN, SlruSharedData::ControlLock, SlruSharedData::cur_lru_count, SlruCtlData::Dir, SlruCtlData::do_fsync, SlruSharedData::group_lsn, IsUnderPostmaster, LWLockPadded::lock, SlruSharedData::lsn_groups_per_page, SlruSharedData::lwlock_tranche_id, SlruSharedData::lwlock_tranche_name, LWLockInitialize(), LWLockRegisterTranche(), MAXALIGN, SlruSharedData::num_slots, SlruSharedData::page_buffer, SlruSharedData::page_dirty, SlruSharedData::page_lru_count, SlruSharedData::page_number, SlruSharedData::page_status, SlruCtlData::shared, ShmemInitStruct(), SimpleLruShmemSize(), SLRU_MAX_NAME_LENGTH, SLRU_PAGE_EMPTY, strlcpy(), and StrNCpy.

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

167 {
168  SlruShared shared;
169  bool found;
170 
171  shared = (SlruShared) ShmemInitStruct(name,
172  SimpleLruShmemSize(nslots, nlsns),
173  &found);
174 
175  if (!IsUnderPostmaster)
176  {
177  /* Initialize locks and shared memory area */
178  char *ptr;
179  Size offset;
180  int slotno;
181 
182  Assert(!found);
183 
184  memset(shared, 0, sizeof(SlruSharedData));
185 
186  shared->ControlLock = ctllock;
187 
188  shared->num_slots = nslots;
189  shared->lsn_groups_per_page = nlsns;
190 
191  shared->cur_lru_count = 0;
192 
193  /* shared->latest_page_number will be set later */
194 
195  ptr = (char *) shared;
196  offset = MAXALIGN(sizeof(SlruSharedData));
197  shared->page_buffer = (char **) (ptr + offset);
198  offset += MAXALIGN(nslots * sizeof(char *));
199  shared->page_status = (SlruPageStatus *) (ptr + offset);
200  offset += MAXALIGN(nslots * sizeof(SlruPageStatus));
201  shared->page_dirty = (bool *) (ptr + offset);
202  offset += MAXALIGN(nslots * sizeof(bool));
203  shared->page_number = (int *) (ptr + offset);
204  offset += MAXALIGN(nslots * sizeof(int));
205  shared->page_lru_count = (int *) (ptr + offset);
206  offset += MAXALIGN(nslots * sizeof(int));
207 
208  /* Initialize LWLocks */
209  shared->buffer_locks = (LWLockPadded *) (ptr + offset);
210  offset += MAXALIGN(nslots * sizeof(LWLockPadded));
211 
212  if (nlsns > 0)
213  {
214  shared->group_lsn = (XLogRecPtr *) (ptr + offset);
215  offset += MAXALIGN(nslots * nlsns * sizeof(XLogRecPtr));
216  }
217 
218  Assert(strlen(name) + 1 < SLRU_MAX_NAME_LENGTH);
220  shared->lwlock_tranche_id = tranche_id;
221 
222  ptr += BUFFERALIGN(offset);
223  for (slotno = 0; slotno < nslots; slotno++)
224  {
225  LWLockInitialize(&shared->buffer_locks[slotno].lock,
226  shared->lwlock_tranche_id);
227 
228  shared->page_buffer[slotno] = ptr;
229  shared->page_status[slotno] = SLRU_PAGE_EMPTY;
230  shared->page_dirty[slotno] = false;
231  shared->page_lru_count[slotno] = 0;
232  ptr += BLCKSZ;
233  }
234 
235  /* Should fit to estimated shmem size */
236  Assert(ptr - (char *) shared <= SimpleLruShmemSize(nslots, nlsns));
237  }
238  else
239  Assert(found);
240 
241  /* Register SLRU tranche in the main tranches array */
243  shared->lwlock_tranche_name);
244 
245  /*
246  * Initialize the unshared control struct, including directory path. We
247  * assume caller set PagePrecedes.
248  */
249  ctl->shared = shared;
250  ctl->do_fsync = true; /* default behavior */
251  StrNCpy(ctl->Dir, subdir, sizeof(ctl->Dir));
252 }
LWLock * ControlLock
Definition: slru.h:57
int * page_number
Definition: slru.h:69
SlruPageStatus
Definition: slru.h:44
char ** page_buffer
Definition: slru.h:66
int cur_lru_count
Definition: slru.h:92
int lsn_groups_per_page
Definition: slru.h:81
Size SimpleLruShmemSize(int nslots, int nlsns)
Definition: slru.c:145
void LWLockRegisterTranche(int tranche_id, const char *tranche_name)
Definition: lwlock.c:599
SlruPageStatus * page_status
Definition: slru.h:67
char lwlock_tranche_name[SLRU_MAX_NAME_LENGTH]
Definition: slru.h:103
void * ShmemInitStruct(const char *name, Size size, bool *foundPtr)
Definition: shmem.c:372
bool IsUnderPostmaster
Definition: globals.c:101
LWLockPadded * buffer_locks
Definition: slru.h:104
XLogRecPtr * group_lsn
Definition: slru.h:80
bool do_fsync
Definition: slru.h:121
void LWLockInitialize(LWLock *lock, int tranche_id)
Definition: lwlock.c:674
SlruSharedData * SlruShared
Definition: slru.h:107
char Dir[64]
Definition: slru.h:134
#define SLRU_MAX_NAME_LENGTH
Definition: slru.h:36
LWLock lock
Definition: lwlock.h:79
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
int * page_lru_count
Definition: slru.h:70
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define Assert(condition)
Definition: c.h:680
#define StrNCpy(dst, src, len)
Definition: c.h:836
size_t Size
Definition: c.h:414
#define MAXALIGN(LEN)
Definition: c.h:633
int num_slots
Definition: slru.h:60
const char * name
Definition: encode.c:521
bool * page_dirty
Definition: slru.h:68
SlruShared shared
Definition: slru.h:115
#define BUFFERALIGN(LEN)
Definition: c.h:635
int lwlock_tranche_id
Definition: slru.h:102

◆ SimpleLruReadPage()

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

Definition at line 375 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, SlruCtlData::shared, SimpleLruWaitIO(), SimpleLruZeroLSNs(), SLRU_PAGE_EMPTY, SLRU_PAGE_READ_IN_PROGRESS, SLRU_PAGE_VALID, SLRU_PAGE_WRITE_IN_PROGRESS, SlruPhysicalReadPage(), SlruRecentlyUsed, SlruReportIOError(), and SlruSelectLRUPage().

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

377 {
378  SlruShared shared = ctl->shared;
379 
380  /* Outer loop handles restart if we must wait for someone else's I/O */
381  for (;;)
382  {
383  int slotno;
384  bool ok;
385 
386  /* See if page already is in memory; if not, pick victim slot */
387  slotno = SlruSelectLRUPage(ctl, pageno);
388 
389  /* Did we find the page in memory? */
390  if (shared->page_number[slotno] == pageno &&
391  shared->page_status[slotno] != SLRU_PAGE_EMPTY)
392  {
393  /*
394  * If page is still being read in, we must wait for I/O. Likewise
395  * if the page is being written and the caller said that's not OK.
396  */
397  if (shared->page_status[slotno] == SLRU_PAGE_READ_IN_PROGRESS ||
398  (shared->page_status[slotno] == SLRU_PAGE_WRITE_IN_PROGRESS &&
399  !write_ok))
400  {
401  SimpleLruWaitIO(ctl, slotno);
402  /* Now we must recheck state from the top */
403  continue;
404  }
405  /* Otherwise, it's ready to use */
406  SlruRecentlyUsed(shared, slotno);
407  return slotno;
408  }
409 
410  /* We found no match; assert we selected a freeable slot */
411  Assert(shared->page_status[slotno] == SLRU_PAGE_EMPTY ||
412  (shared->page_status[slotno] == SLRU_PAGE_VALID &&
413  !shared->page_dirty[slotno]));
414 
415  /* Mark the slot read-busy */
416  shared->page_number[slotno] = pageno;
417  shared->page_status[slotno] = SLRU_PAGE_READ_IN_PROGRESS;
418  shared->page_dirty[slotno] = false;
419 
420  /* Acquire per-buffer lock (cannot deadlock, see notes at top) */
421  LWLockAcquire(&shared->buffer_locks[slotno].lock, LW_EXCLUSIVE);
422 
423  /* Release control lock while doing I/O */
424  LWLockRelease(shared->ControlLock);
425 
426  /* Do the read */
427  ok = SlruPhysicalReadPage(ctl, pageno, slotno);
428 
429  /* Set the LSNs for this newly read-in page to zero */
430  SimpleLruZeroLSNs(ctl, slotno);
431 
432  /* Re-acquire control lock and update page state */
434 
435  Assert(shared->page_number[slotno] == pageno &&
436  shared->page_status[slotno] == SLRU_PAGE_READ_IN_PROGRESS &&
437  !shared->page_dirty[slotno]);
438 
439  shared->page_status[slotno] = ok ? SLRU_PAGE_VALID : SLRU_PAGE_EMPTY;
440 
441  LWLockRelease(&shared->buffer_locks[slotno].lock);
442 
443  /* Now it's okay to ereport if we failed */
444  if (!ok)
445  SlruReportIOError(ctl, pageno, xid);
446 
447  SlruRecentlyUsed(shared, slotno);
448  return slotno;
449  }
450 }
LWLock * ControlLock
Definition: slru.h:57
int * page_number
Definition: slru.h:69
static void SimpleLruZeroLSNs(SlruCtl ctl, int slotno)
Definition: slru.c:304
static void SlruReportIOError(SlruCtl ctl, int pageno, TransactionId xid)
Definition: slru.c:892
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1722
SlruPageStatus * page_status
Definition: slru.h:67
LWLockPadded * buffer_locks
Definition: slru.h:104
static void SimpleLruWaitIO(SlruCtl ctl, int slotno)
Definition: slru.c:321
LWLock lock
Definition: lwlock.h:79
static bool SlruPhysicalReadPage(SlruCtl ctl, int pageno, int slotno)
Definition: slru.c:639
#define Assert(condition)
Definition: c.h:680
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1118
static int SlruSelectLRUPage(SlruCtl ctl, int pageno)
Definition: slru.c:966
bool * page_dirty
Definition: slru.h:68
SlruShared shared
Definition: slru.h:115
#define SlruRecentlyUsed(shared, slotno)
Definition: slru.c:103

◆ SimpleLruReadPage_ReadOnly()

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

Definition at line 467 of file slru.c.

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

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

468 {
469  SlruShared shared = ctl->shared;
470  int slotno;
471 
472  /* Try to find the page while holding only shared lock */
474 
475  /* See if page is already in a buffer */
476  for (slotno = 0; slotno < shared->num_slots; slotno++)
477  {
478  if (shared->page_number[slotno] == pageno &&
479  shared->page_status[slotno] != SLRU_PAGE_EMPTY &&
480  shared->page_status[slotno] != SLRU_PAGE_READ_IN_PROGRESS)
481  {
482  /* See comments for SlruRecentlyUsed macro */
483  SlruRecentlyUsed(shared, slotno);
484  return slotno;
485  }
486  }
487 
488  /* No luck, so switch to normal exclusive lock and do regular read */
489  LWLockRelease(shared->ControlLock);
491 
492  return SimpleLruReadPage(ctl, pageno, true, xid);
493 }
LWLock * ControlLock
Definition: slru.h:57
int * page_number
Definition: slru.h:69
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1722
SlruPageStatus * page_status
Definition: slru.h:67
int SimpleLruReadPage(SlruCtl ctl, int pageno, bool write_ok, TransactionId xid)
Definition: slru.c:375
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1118
int num_slots
Definition: slru.h:60
SlruShared shared
Definition: slru.h:115
#define SlruRecentlyUsed(shared, slotno)
Definition: slru.c:103

◆ SimpleLruShmemSize()

Size SimpleLruShmemSize ( int  nslots,
int  nlsns 
)

Definition at line 145 of file slru.c.

References BUFFERALIGN, and MAXALIGN.

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

146 {
147  Size sz;
148 
149  /* we assume nslots isn't so large as to risk overflow */
150  sz = MAXALIGN(sizeof(SlruSharedData));
151  sz += MAXALIGN(nslots * sizeof(char *)); /* page_buffer[] */
152  sz += MAXALIGN(nslots * sizeof(SlruPageStatus)); /* page_status[] */
153  sz += MAXALIGN(nslots * sizeof(bool)); /* page_dirty[] */
154  sz += MAXALIGN(nslots * sizeof(int)); /* page_number[] */
155  sz += MAXALIGN(nslots * sizeof(int)); /* page_lru_count[] */
156  sz += MAXALIGN(nslots * sizeof(LWLockPadded)); /* buffer_locks[] */
157 
158  if (nlsns > 0)
159  sz += MAXALIGN(nslots * nlsns * sizeof(XLogRecPtr)); /* group_lsn[] */
160 
161  return BUFFERALIGN(sz) + BLCKSZ * nslots;
162 }
SlruPageStatus
Definition: slru.h:44
uint64 XLogRecPtr
Definition: xlogdefs.h:21
size_t Size
Definition: c.h:414
#define MAXALIGN(LEN)
Definition: c.h:633
#define BUFFERALIGN(LEN)
Definition: c.h:635

◆ SimpleLruTruncate()

void SimpleLruTruncate ( SlruCtl  ctl,
int  cutoffPage 
)

Definition at line 1168 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, SlruCtlData::shared, SimpleLruWaitIO(), SLRU_PAGE_EMPTY, SLRU_PAGE_VALID, SLRU_PAGES_PER_SEGMENT, SlruInternalWritePage(), SlruScanDirCbDeleteCutoff(), and SlruScanDirectory().

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

1169 {
1170  SlruShared shared = ctl->shared;
1171  int slotno;
1172 
1173  /*
1174  * The cutoff point is the start of the segment containing cutoffPage.
1175  */
1176  cutoffPage -= cutoffPage % SLRU_PAGES_PER_SEGMENT;
1177 
1178  /*
1179  * Scan shared memory and remove any pages preceding the cutoff page, to
1180  * ensure we won't rewrite them later. (Since this is normally called in
1181  * or just after a checkpoint, any dirty pages should have been flushed
1182  * already ... we're just being extra careful here.)
1183  */
1185 
1186 restart:;
1187 
1188  /*
1189  * While we are holding the lock, make an important safety check: the
1190  * planned cutoff point must be <= the current endpoint page. Otherwise we
1191  * have already wrapped around, and proceeding with the truncation would
1192  * risk removing the current segment.
1193  */
1194  if (ctl->PagePrecedes(shared->latest_page_number, cutoffPage))
1195  {
1196  LWLockRelease(shared->ControlLock);
1197  ereport(LOG,
1198  (errmsg("could not truncate directory \"%s\": apparent wraparound",
1199  ctl->Dir)));
1200  return;
1201  }
1202 
1203  for (slotno = 0; slotno < shared->num_slots; slotno++)
1204  {
1205  if (shared->page_status[slotno] == SLRU_PAGE_EMPTY)
1206  continue;
1207  if (!ctl->PagePrecedes(shared->page_number[slotno], cutoffPage))
1208  continue;
1209 
1210  /*
1211  * If page is clean, just change state to EMPTY (expected case).
1212  */
1213  if (shared->page_status[slotno] == SLRU_PAGE_VALID &&
1214  !shared->page_dirty[slotno])
1215  {
1216  shared->page_status[slotno] = SLRU_PAGE_EMPTY;
1217  continue;
1218  }
1219 
1220  /*
1221  * Hmm, we have (or may have) I/O operations acting on the page, so
1222  * we've got to wait for them to finish and then start again. This is
1223  * the same logic as in SlruSelectLRUPage. (XXX if page is dirty,
1224  * wouldn't it be OK to just discard it without writing it? For now,
1225  * keep the logic the same as it was.)
1226  */
1227  if (shared->page_status[slotno] == SLRU_PAGE_VALID)
1228  SlruInternalWritePage(ctl, slotno, NULL);
1229  else
1230  SimpleLruWaitIO(ctl, slotno);
1231  goto restart;
1232  }
1233 
1234  LWLockRelease(shared->ControlLock);
1235 
1236  /* Now we can remove the old segment(s) */
1237  (void) SlruScanDirectory(ctl, SlruScanDirCbDeleteCutoff, &cutoffPage);
1238 }
LWLock * ControlLock
Definition: slru.h:57
int * page_number
Definition: slru.h:69
static void SlruInternalWritePage(SlruCtl ctl, int slotno, SlruFlush fdata)
Definition: slru.c:507
int latest_page_number
Definition: slru.h:99
#define LOG
Definition: elog.h:26
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1722
SlruPageStatus * page_status
Definition: slru.h:67
#define ereport(elevel, rest)
Definition: elog.h:122
static void SimpleLruWaitIO(SlruCtl ctl, int slotno)
Definition: slru.c:321
static bool SlruScanDirCbDeleteCutoff(SlruCtl ctl, char *filename, int segpage, void *data)
Definition: slru.c:1338
char Dir[64]
Definition: slru.h:134
bool SlruScanDirectory(SlruCtl ctl, SlruScanCallback callback, void *data)
Definition: slru.c:1376
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1118
int num_slots
Definition: slru.h:60
int errmsg(const char *fmt,...)
Definition: elog.c:797
bool * page_dirty
Definition: slru.h:68
SlruShared shared
Definition: slru.h:115
bool(* PagePrecedes)(int, int)
Definition: slru.h:128
#define SLRU_PAGES_PER_SEGMENT
Definition: slru.h:33

◆ SimpleLruWritePage()

void SimpleLruWritePage ( SlruCtl  ctl,
int  slotno 
)

Definition at line 578 of file slru.c.

References SlruInternalWritePage().

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

579 {
580  SlruInternalWritePage(ctl, slotno, NULL);
581 }
static void SlruInternalWritePage(SlruCtl ctl, int slotno, SlruFlush fdata)
Definition: slru.c:507

◆ SimpleLruZeroPage()

int SimpleLruZeroPage ( SlruCtl  ctl,
int  pageno 
)

Definition at line 263 of file slru.c.

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

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

264 {
265  SlruShared shared = ctl->shared;
266  int slotno;
267 
268  /* Find a suitable buffer slot for the page */
269  slotno = SlruSelectLRUPage(ctl, pageno);
270  Assert(shared->page_status[slotno] == SLRU_PAGE_EMPTY ||
271  (shared->page_status[slotno] == SLRU_PAGE_VALID &&
272  !shared->page_dirty[slotno]) ||
273  shared->page_number[slotno] == pageno);
274 
275  /* Mark the slot as containing this page */
276  shared->page_number[slotno] = pageno;
277  shared->page_status[slotno] = SLRU_PAGE_VALID;
278  shared->page_dirty[slotno] = true;
279  SlruRecentlyUsed(shared, slotno);
280 
281  /* Set the buffer to zeroes */
282  MemSet(shared->page_buffer[slotno], 0, BLCKSZ);
283 
284  /* Set the LSNs for this new page to zero */
285  SimpleLruZeroLSNs(ctl, slotno);
286 
287  /* Assume this page is now the latest active page */
288  shared->latest_page_number = pageno;
289 
290  return slotno;
291 }
int * page_number
Definition: slru.h:69
int latest_page_number
Definition: slru.h:99
char ** page_buffer
Definition: slru.h:66
#define MemSet(start, val, len)
Definition: c.h:863
static void SimpleLruZeroLSNs(SlruCtl ctl, int slotno)
Definition: slru.c:304
SlruPageStatus * page_status
Definition: slru.h:67
#define Assert(condition)
Definition: c.h:680
static int SlruSelectLRUPage(SlruCtl ctl, int pageno)
Definition: slru.c:966
bool * page_dirty
Definition: slru.h:68
SlruShared shared
Definition: slru.h:115
#define SlruRecentlyUsed(shared, slotno)
Definition: slru.c:103

◆ SlruDeleteSegment()

void SlruDeleteSegment ( SlruCtl  ctl,
int  segno 
)

Definition at line 1261 of file slru.c.

References SlruSharedData::ControlLock, DEBUG2, SlruCtlData::Dir, ereport, errmsg(), LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), MAXPGPATH, 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, SlruInternalWritePage(), and snprintf().

Referenced by PerformMembersTruncation().

1262 {
1263  SlruShared shared = ctl->shared;
1264  int slotno;
1265  char path[MAXPGPATH];
1266  bool did_write;
1267 
1268  /* Clean out any possibly existing references to the segment. */
1270 restart:
1271  did_write = false;
1272  for (slotno = 0; slotno < shared->num_slots; slotno++)
1273  {
1274  int pagesegno = shared->page_number[slotno] / SLRU_PAGES_PER_SEGMENT;
1275 
1276  if (shared->page_status[slotno] == SLRU_PAGE_EMPTY)
1277  continue;
1278 
1279  /* not the segment we're looking for */
1280  if (pagesegno != segno)
1281  continue;
1282 
1283  /* If page is clean, just change state to EMPTY (expected case). */
1284  if (shared->page_status[slotno] == SLRU_PAGE_VALID &&
1285  !shared->page_dirty[slotno])
1286  {
1287  shared->page_status[slotno] = SLRU_PAGE_EMPTY;
1288  continue;
1289  }
1290 
1291  /* Same logic as SimpleLruTruncate() */
1292  if (shared->page_status[slotno] == SLRU_PAGE_VALID)
1293  SlruInternalWritePage(ctl, slotno, NULL);
1294  else
1295  SimpleLruWaitIO(ctl, slotno);
1296 
1297  did_write = true;
1298  }
1299 
1300  /*
1301  * Be extra careful and re-check. The IO functions release the control
1302  * lock, so new pages could have been read in.
1303  */
1304  if (did_write)
1305  goto restart;
1306 
1307  snprintf(path, MAXPGPATH, "%s/%04X", ctl->Dir, segno);
1308  ereport(DEBUG2,
1309  (errmsg("removing file \"%s\"", path)));
1310  unlink(path);
1311 
1312  LWLockRelease(shared->ControlLock);
1313 }
LWLock * ControlLock
Definition: slru.h:57
int * page_number
Definition: slru.h:69
static void SlruInternalWritePage(SlruCtl ctl, int slotno, SlruFlush fdata)
Definition: slru.c:507
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1722
SlruPageStatus * page_status
Definition: slru.h:67
#define MAXPGPATH
#define DEBUG2
Definition: elog.h:24
#define ereport(elevel, rest)
Definition: elog.h:122
static void SimpleLruWaitIO(SlruCtl ctl, int slotno)
Definition: slru.c:321
char Dir[64]
Definition: slru.h:134
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1118
int num_slots
Definition: slru.h:60
int errmsg(const char *fmt,...)
Definition: elog.c:797
bool * page_dirty
Definition: slru.h:68
SlruShared shared
Definition: slru.h:115
#define SLRU_PAGES_PER_SEGMENT
Definition: slru.h:33

◆ SlruScanDirCbDeleteAll()

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

Definition at line 1353 of file slru.c.

References SlruInternalDeleteSegment().

Referenced by AsyncShmemInit(), and DeactivateCommitTs().

1354 {
1356 
1357  return false; /* keep going */
1358 }
static void SlruInternalDeleteSegment(SlruCtl ctl, char *filename)
Definition: slru.c:1247
static char * filename
Definition: pg_dumpall.c:90

◆ SlruScanDirCbReportPresence()

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

Definition at line 1321 of file slru.c.

References SlruCtlData::PagePrecedes, and SLRU_PAGES_PER_SEGMENT.

Referenced by TruncateCLOG(), and TruncateCommitTs().

1322 {
1323  int cutoffPage = *(int *) data;
1324 
1325  cutoffPage -= cutoffPage % SLRU_PAGES_PER_SEGMENT;
1326 
1327  if (ctl->PagePrecedes(segpage, cutoffPage))
1328  return true; /* found one; don't iterate any more */
1329 
1330  return false; /* keep going */
1331 }
bool(* PagePrecedes)(int, int)
Definition: slru.h:128
#define SLRU_PAGES_PER_SEGMENT
Definition: slru.h:33

◆ SlruScanDirectory()

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

Definition at line 1376 of file slru.c.

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

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

1377 {
1378  bool retval = false;
1379  DIR *cldir;
1380  struct dirent *clde;
1381  int segno;
1382  int segpage;
1383 
1384  cldir = AllocateDir(ctl->Dir);
1385  while ((clde = ReadDir(cldir, ctl->Dir)) != NULL)
1386  {
1387  size_t len;
1388 
1389  len = strlen(clde->d_name);
1390 
1391  if ((len == 4 || len == 5 || len == 6) &&
1392  strspn(clde->d_name, "0123456789ABCDEF") == len)
1393  {
1394  segno = (int) strtol(clde->d_name, NULL, 16);
1395  segpage = segno * SLRU_PAGES_PER_SEGMENT;
1396 
1397  elog(DEBUG2, "SlruScanDirectory invoking callback on %s/%s",
1398  ctl->Dir, clde->d_name);
1399  retval = callback(ctl, clde->d_name, segpage, data);
1400  if (retval)
1401  break;
1402  }
1403  }
1404  FreeDir(cldir);
1405 
1406  return retval;
1407 }
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:2596
char Dir[64]
Definition: slru.h:134
struct dirent * ReadDir(DIR *dir, const char *dirname)
Definition: fd.c:2662
char d_name[MAX_PATH]
Definition: dirent.h:14
#define elog
Definition: elog.h:219
#define SLRU_PAGES_PER_SEGMENT
Definition: slru.h:33
int FreeDir(DIR *dir)
Definition: fd.c:2714