PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
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

#define SLRU_MAX_NAME_LENGTH   32

Definition at line 40 of file slru.h.

Referenced by SimpleLruInit().

Typedef Documentation

Definition at line 141 of file slru.h.

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

Definition at line 157 of file slru.h.

Definition at line 111 of file slru.h.

Enumeration Type Documentation

Enumerator
SLRU_PAGE_EMPTY 
SLRU_PAGE_READ_IN_PROGRESS 
SLRU_PAGE_VALID 
SLRU_PAGE_WRITE_IN_PROGRESS 

Definition at line 48 of file slru.h.

49 {
50  SLRU_PAGE_EMPTY, /* buffer is not in use */
51  SLRU_PAGE_READ_IN_PROGRESS, /* page is being read in */
52  SLRU_PAGE_VALID, /* page is valid and not being written */
53  SLRU_PAGE_WRITE_IN_PROGRESS /* page is being written out */
SlruPageStatus
Definition: slru.h:48

Function Documentation

bool SimpleLruDoesPhysicalPageExist ( SlruCtl  ctl,
int  pageno 
)

Definition at line 586 of file slru.c.

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

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

587 {
588  int segno = pageno / SLRU_PAGES_PER_SEGMENT;
589  int rpageno = pageno % SLRU_PAGES_PER_SEGMENT;
590  int offset = rpageno * BLCKSZ;
591  char path[MAXPGPATH];
592  int fd;
593  bool result;
594  off_t endpos;
595 
596  SlruFileName(ctl, path, segno);
597 
598  fd = OpenTransientFile(path, O_RDWR | PG_BINARY, S_IRUSR | S_IWUSR);
599  if (fd < 0)
600  {
601  /* expected: file doesn't exist */
602  if (errno == ENOENT)
603  return false;
604 
605  /* report error normally */
607  slru_errno = errno;
608  SlruReportIOError(ctl, pageno, 0);
609  }
610 
611  if ((endpos = lseek(fd, 0, SEEK_END)) < 0)
612  {
614  slru_errno = errno;
615  SlruReportIOError(ctl, pageno, 0);
616  }
617 
618  result = endpos >= (off_t) (offset + BLCKSZ);
619 
620  CloseTransientFile(fd);
621  return result;
622 }
static SlruErrorCause slru_errcause
Definition: slru.c:123
return result
Definition: formatting.c:1618
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:889
#define PG_BINARY
Definition: c.h:1038
static XLogRecPtr endpos
#define MAXPGPATH
int OpenTransientFile(FileName fileName, int fileFlags, int fileMode)
Definition: fd.c:2107
int CloseTransientFile(int fd)
Definition: fd.c:2268
#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:37
void SimpleLruFlush ( SlruCtl  ctl,
bool  allow_redirtied 
)

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

1101 {
1102  SlruShared shared = ctl->shared;
1103  SlruFlushData fdata;
1104  int slotno;
1105  int pageno = 0;
1106  int i;
1107  bool ok;
1108 
1109  /*
1110  * Find and write dirty pages
1111  */
1112  fdata.num_files = 0;
1113 
1115 
1116  for (slotno = 0; slotno < shared->num_slots; slotno++)
1117  {
1118  SlruInternalWritePage(ctl, slotno, &fdata);
1119 
1120  /*
1121  * In some places (e.g. checkpoints), we cannot assert that the slot
1122  * is clean now, since another process might have re-dirtied it
1123  * already. That's okay.
1124  */
1125  Assert(allow_redirtied ||
1126  shared->page_status[slotno] == SLRU_PAGE_EMPTY ||
1127  (shared->page_status[slotno] == SLRU_PAGE_VALID &&
1128  !shared->page_dirty[slotno]));
1129  }
1130 
1131  LWLockRelease(shared->ControlLock);
1132 
1133  /*
1134  * Now fsync and close any files that were open
1135  */
1136  ok = true;
1137  for (i = 0; i < fdata.num_files; i++)
1138  {
1140  if (ctl->do_fsync && pg_fsync(fdata.fd[i]))
1141  {
1143  slru_errno = errno;
1144  pageno = fdata.segno[i] * SLRU_PAGES_PER_SEGMENT;
1145  ok = false;
1146  }
1148 
1149  if (CloseTransientFile(fdata.fd[i]))
1150  {
1152  slru_errno = errno;
1153  pageno = fdata.segno[i] * SLRU_PAGES_PER_SEGMENT;
1154  ok = false;
1155  }
1156  }
1157  if (!ok)
1159 }
LWLock * ControlLock
Definition: slru.h:61
static void SlruInternalWritePage(SlruCtl ctl, int slotno, SlruFlush fdata)
Definition: slru.c:503
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:889
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1715
SlruPageStatus * page_status
Definition: slru.h:71
#define InvalidTransactionId
Definition: transam.h:31
static void pgstat_report_wait_end(void)
Definition: pgstat.h:1205
bool do_fsync
Definition: slru.h:125
int CloseTransientFile(int fd)
Definition: fd.c:2268
int num_files
Definition: slru.c:77
#define Assert(condition)
Definition: c.h:675
static void pgstat_report_wait_start(uint32 wait_event_info)
Definition: pgstat.h:1181
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1111
int num_slots
Definition: slru.h:64
static int slru_errno
Definition: slru.c:124
bool * page_dirty
Definition: slru.h:72
int i
SlruShared shared
Definition: slru.h:119
int pg_fsync(int fd)
Definition: fd.c:333
#define SLRU_PAGES_PER_SEGMENT
Definition: slru.h:37
int fd[MAX_FLUSH_BUFFERS]
Definition: slru.c:78
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, ShmemAlloc(), 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  if (nlsns > 0)
209  {
210  shared->group_lsn = (XLogRecPtr *) (ptr + offset);
211  offset += MAXALIGN(nslots * nlsns * sizeof(XLogRecPtr));
212  }
213 
214  /* Initialize LWLocks */
215  shared->buffer_locks = (LWLockPadded *) ShmemAlloc(sizeof(LWLockPadded) * nslots);
216 
217  Assert(strlen(name) + 1 < SLRU_MAX_NAME_LENGTH);
219  shared->lwlock_tranche_id = tranche_id;
220 
221  ptr += BUFFERALIGN(offset);
222  for (slotno = 0; slotno < nslots; slotno++)
223  {
224  LWLockInitialize(&shared->buffer_locks[slotno].lock,
225  shared->lwlock_tranche_id);
226 
227  shared->page_buffer[slotno] = ptr;
228  shared->page_status[slotno] = SLRU_PAGE_EMPTY;
229  shared->page_dirty[slotno] = false;
230  shared->page_lru_count[slotno] = 0;
231  ptr += BLCKSZ;
232  }
233  }
234  else
235  Assert(found);
236 
237  /* Register SLRU tranche in the main tranches array */
239  shared->lwlock_tranche_name);
240 
241  /*
242  * Initialize the unshared control struct, including directory path. We
243  * assume caller set PagePrecedes.
244  */
245  ctl->shared = shared;
246  ctl->do_fsync = true; /* default behavior */
247  StrNCpy(ctl->Dir, subdir, sizeof(ctl->Dir));
248 }
LWLock * ControlLock
Definition: slru.h:61
int * page_number
Definition: slru.h:73
SlruPageStatus
Definition: slru.h:48
char ** page_buffer
Definition: slru.h:70
int cur_lru_count
Definition: slru.h:96
int lsn_groups_per_page
Definition: slru.h:85
void * ShmemAlloc(Size size)
Definition: shmem.c:157
Size SimpleLruShmemSize(int nslots, int nlsns)
Definition: slru.c:145
SlruPageStatus * page_status
Definition: slru.h:71
char lwlock_tranche_name[SLRU_MAX_NAME_LENGTH]
Definition: slru.h:107
void * ShmemInitStruct(const char *name, Size size, bool *foundPtr)
Definition: shmem.c:372
bool IsUnderPostmaster
Definition: globals.c:100
LWLockPadded * buffer_locks
Definition: slru.h:108
XLogRecPtr * group_lsn
Definition: slru.h:84
bool do_fsync
Definition: slru.h:125
void LWLockInitialize(LWLock *lock, int tranche_id)
Definition: lwlock.c:667
SlruSharedData * SlruShared
Definition: slru.h:111
char Dir[64]
Definition: slru.h:138
#define SLRU_MAX_NAME_LENGTH
Definition: slru.h:40
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:74
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define Assert(condition)
Definition: c.h:675
#define StrNCpy(dst, src, len)
Definition: c.h:830
size_t Size
Definition: c.h:356
#define MAXALIGN(LEN)
Definition: c.h:588
void LWLockRegisterTranche(int tranche_id, char *tranche_name)
Definition: lwlock.c:592
int num_slots
Definition: slru.h:64
const char * name
Definition: encode.c:521
bool * page_dirty
Definition: slru.h:72
SlruShared shared
Definition: slru.h:119
#define BUFFERALIGN(LEN)
Definition: c.h:590
int lwlock_tranche_id
Definition: slru.h:106
int SimpleLruReadPage ( SlruCtl  ctl,
int  pageno,
bool  write_ok,
TransactionId  xid 
)

Definition at line 371 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(), TransactionIdSetPageStatus(), TrimCLOG(), and TrimMultiXact().

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

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

464 {
465  SlruShared shared = ctl->shared;
466  int slotno;
467 
468  /* Try to find the page while holding only shared lock */
470 
471  /* See if page is already in a buffer */
472  for (slotno = 0; slotno < shared->num_slots; slotno++)
473  {
474  if (shared->page_number[slotno] == pageno &&
475  shared->page_status[slotno] != SLRU_PAGE_EMPTY &&
476  shared->page_status[slotno] != SLRU_PAGE_READ_IN_PROGRESS)
477  {
478  /* See comments for SlruRecentlyUsed macro */
479  SlruRecentlyUsed(shared, slotno);
480  return slotno;
481  }
482  }
483 
484  /* No luck, so switch to normal exclusive lock and do regular read */
485  LWLockRelease(shared->ControlLock);
487 
488  return SimpleLruReadPage(ctl, pageno, true, xid);
489 }
LWLock * ControlLock
Definition: slru.h:61
int * page_number
Definition: slru.h:73
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1715
SlruPageStatus * page_status
Definition: slru.h:71
int SimpleLruReadPage(SlruCtl ctl, int pageno, bool write_ok, TransactionId xid)
Definition: slru.c:371
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1111
int num_slots
Definition: slru.h:64
SlruShared shared
Definition: slru.h:119
#define SlruRecentlyUsed(shared, slotno)
Definition: slru.c:103
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:48
uint64 XLogRecPtr
Definition: xlogdefs.h:21
size_t Size
Definition: c.h:356
#define MAXALIGN(LEN)
Definition: c.h:588
#define BUFFERALIGN(LEN)
Definition: c.h:590
void SimpleLruTruncate ( SlruCtl  ctl,
int  cutoffPage 
)

Definition at line 1165 of file slru.c.

References SlruSharedData::ControlLock, SlruCtlData::Dir, ereport, errmsg(), SlruSharedData::latest_page_number, LOG, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), NULL, 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().

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

Definition at line 574 of file slru.c.

References NULL, and SlruInternalWritePage().

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

575 {
576  SlruInternalWritePage(ctl, slotno, NULL);
577 }
static void SlruInternalWritePage(SlruCtl ctl, int slotno, SlruFlush fdata)
Definition: slru.c:503
#define NULL
Definition: c.h:229
int SimpleLruZeroPage ( SlruCtl  ctl,
int  pageno 
)

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

260 {
261  SlruShared shared = ctl->shared;
262  int slotno;
263 
264  /* Find a suitable buffer slot for the page */
265  slotno = SlruSelectLRUPage(ctl, pageno);
266  Assert(shared->page_status[slotno] == SLRU_PAGE_EMPTY ||
267  (shared->page_status[slotno] == SLRU_PAGE_VALID &&
268  !shared->page_dirty[slotno]) ||
269  shared->page_number[slotno] == pageno);
270 
271  /* Mark the slot as containing this page */
272  shared->page_number[slotno] = pageno;
273  shared->page_status[slotno] = SLRU_PAGE_VALID;
274  shared->page_dirty[slotno] = true;
275  SlruRecentlyUsed(shared, slotno);
276 
277  /* Set the buffer to zeroes */
278  MemSet(shared->page_buffer[slotno], 0, BLCKSZ);
279 
280  /* Set the LSNs for this new page to zero */
281  SimpleLruZeroLSNs(ctl, slotno);
282 
283  /* Assume this page is now the latest active page */
284  shared->latest_page_number = pageno;
285 
286  return slotno;
287 }
int * page_number
Definition: slru.h:73
int latest_page_number
Definition: slru.h:103
char ** page_buffer
Definition: slru.h:70
#define MemSet(start, val, len)
Definition: c.h:857
static void SimpleLruZeroLSNs(SlruCtl ctl, int slotno)
Definition: slru.c:300
SlruPageStatus * page_status
Definition: slru.h:71
#define Assert(condition)
Definition: c.h:675
static int SlruSelectLRUPage(SlruCtl ctl, int pageno)
Definition: slru.c:963
bool * page_dirty
Definition: slru.h:72
SlruShared shared
Definition: slru.h:119
#define SlruRecentlyUsed(shared, slotno)
Definition: slru.c:103
void SlruDeleteSegment ( SlruCtl  ctl,
int  segno 
)

Definition at line 1258 of file slru.c.

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

Referenced by PerformMembersTruncation().

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

Definition at line 1350 of file slru.c.

References SlruInternalDeleteSegment().

Referenced by AsyncShmemInit(), and DeactivateCommitTs().

1351 {
1353 
1354  return false; /* keep going */
1355 }
static void SlruInternalDeleteSegment(SlruCtl ctl, char *filename)
Definition: slru.c:1244
static char * filename
Definition: pg_dumpall.c:87
bool SlruScanDirCbReportPresence ( SlruCtl  ctl,
char *  filename,
int  segpage,
void *  data 
)

Definition at line 1318 of file slru.c.

References SlruCtlData::PagePrecedes, and SLRU_PAGES_PER_SEGMENT.

Referenced by TruncateCLOG(), and TruncateCommitTs().

1319 {
1320  int cutoffPage = *(int *) data;
1321 
1322  cutoffPage -= cutoffPage % SLRU_PAGES_PER_SEGMENT;
1323 
1324  if (ctl->PagePrecedes(segpage, cutoffPage))
1325  return true; /* found one; don't iterate any more */
1326 
1327  return false; /* keep going */
1328 }
bool(* PagePrecedes)(int, int)
Definition: slru.h:132
#define SLRU_PAGES_PER_SEGMENT
Definition: slru.h:37
bool SlruScanDirectory ( SlruCtl  ctl,
SlruScanCallback  callback,
void *  data 
)

Definition at line 1373 of file slru.c.

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

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

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