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 585 of file slru.c.

References CloseTransientFile(), endpos, 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().

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

Definition at line 1090 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(), 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(), and SlruReportIOError().

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

1091 {
1092  SlruShared shared = ctl->shared;
1093  SlruFlushData fdata;
1094  int slotno;
1095  int pageno = 0;
1096  int i;
1097  bool ok;
1098 
1099  /*
1100  * Find and write dirty pages
1101  */
1102  fdata.num_files = 0;
1103 
1105 
1106  for (slotno = 0; slotno < shared->num_slots; slotno++)
1107  {
1108  SlruInternalWritePage(ctl, slotno, &fdata);
1109 
1110  /*
1111  * In some places (e.g. checkpoints), we cannot assert that the slot
1112  * is clean now, since another process might have re-dirtied it
1113  * already. That's okay.
1114  */
1115  Assert(allow_redirtied ||
1116  shared->page_status[slotno] == SLRU_PAGE_EMPTY ||
1117  (shared->page_status[slotno] == SLRU_PAGE_VALID &&
1118  !shared->page_dirty[slotno]));
1119  }
1120 
1121  LWLockRelease(shared->ControlLock);
1122 
1123  /*
1124  * Now fsync and close any files that were open
1125  */
1126  ok = true;
1127  for (i = 0; i < fdata.num_files; i++)
1128  {
1129  if (ctl->do_fsync && pg_fsync(fdata.fd[i]))
1130  {
1132  slru_errno = errno;
1133  pageno = fdata.segno[i] * SLRU_PAGES_PER_SEGMENT;
1134  ok = false;
1135  }
1136 
1137  if (CloseTransientFile(fdata.fd[i]))
1138  {
1140  slru_errno = errno;
1141  pageno = fdata.segno[i] * SLRU_PAGES_PER_SEGMENT;
1142  ok = false;
1143  }
1144  }
1145  if (!ok)
1147 }
LWLock * ControlLock
Definition: slru.h:61
static void SlruInternalWritePage(SlruCtl ctl, int slotno, SlruFlush fdata)
Definition: slru.c:502
static SlruErrorCause slru_errcause
Definition: slru.c:122
int segno[MAX_FLUSH_BUFFERS]
Definition: slru.c:78
static void SlruReportIOError(SlruCtl ctl, int pageno, TransactionId xid)
Definition: slru.c:879
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1714
SlruPageStatus * page_status
Definition: slru.h:71
#define InvalidTransactionId
Definition: transam.h:31
bool do_fsync
Definition: slru.h:125
int CloseTransientFile(int fd)
Definition: fd.c:2254
int num_files
Definition: slru.c:76
#define Assert(condition)
Definition: c.h:671
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1110
int num_slots
Definition: slru.h:64
static int slru_errno
Definition: slru.c:123
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:77
void SimpleLruInit ( SlruCtl  ctl,
const char *  name,
int  nslots,
int  nlsns,
LWLock ctllock,
const char *  subdir,
int  tranche_id 
)

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

166 {
167  SlruShared shared;
168  bool found;
169 
170  shared = (SlruShared) ShmemInitStruct(name,
171  SimpleLruShmemSize(nslots, nlsns),
172  &found);
173 
174  if (!IsUnderPostmaster)
175  {
176  /* Initialize locks and shared memory area */
177  char *ptr;
178  Size offset;
179  int slotno;
180 
181  Assert(!found);
182 
183  memset(shared, 0, sizeof(SlruSharedData));
184 
185  shared->ControlLock = ctllock;
186 
187  shared->num_slots = nslots;
188  shared->lsn_groups_per_page = nlsns;
189 
190  shared->cur_lru_count = 0;
191 
192  /* shared->latest_page_number will be set later */
193 
194  ptr = (char *) shared;
195  offset = MAXALIGN(sizeof(SlruSharedData));
196  shared->page_buffer = (char **) (ptr + offset);
197  offset += MAXALIGN(nslots * sizeof(char *));
198  shared->page_status = (SlruPageStatus *) (ptr + offset);
199  offset += MAXALIGN(nslots * sizeof(SlruPageStatus));
200  shared->page_dirty = (bool *) (ptr + offset);
201  offset += MAXALIGN(nslots * sizeof(bool));
202  shared->page_number = (int *) (ptr + offset);
203  offset += MAXALIGN(nslots * sizeof(int));
204  shared->page_lru_count = (int *) (ptr + offset);
205  offset += MAXALIGN(nslots * sizeof(int));
206 
207  if (nlsns > 0)
208  {
209  shared->group_lsn = (XLogRecPtr *) (ptr + offset);
210  offset += MAXALIGN(nslots * nlsns * sizeof(XLogRecPtr));
211  }
212 
213  /* Initialize LWLocks */
214  shared->buffer_locks = (LWLockPadded *) ShmemAlloc(sizeof(LWLockPadded) * nslots);
215 
216  Assert(strlen(name) + 1 < SLRU_MAX_NAME_LENGTH);
218  shared->lwlock_tranche_id = tranche_id;
219 
220  ptr += BUFFERALIGN(offset);
221  for (slotno = 0; slotno < nslots; slotno++)
222  {
223  LWLockInitialize(&shared->buffer_locks[slotno].lock,
224  shared->lwlock_tranche_id);
225 
226  shared->page_buffer[slotno] = ptr;
227  shared->page_status[slotno] = SLRU_PAGE_EMPTY;
228  shared->page_dirty[slotno] = false;
229  shared->page_lru_count[slotno] = 0;
230  ptr += BLCKSZ;
231  }
232  }
233  else
234  Assert(found);
235 
236  /* Register SLRU tranche in the main tranches array */
238  shared->lwlock_tranche_name);
239 
240  /*
241  * Initialize the unshared control struct, including directory path. We
242  * assume caller set PagePrecedes.
243  */
244  ctl->shared = shared;
245  ctl->do_fsync = true; /* default behavior */
246  StrNCpy(ctl->Dir, subdir, sizeof(ctl->Dir));
247 }
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:144
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:666
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:671
#define StrNCpy(dst, src, len)
Definition: c.h:826
size_t Size
Definition: c.h:353
#define MAXALIGN(LEN)
Definition: c.h:584
void LWLockRegisterTranche(int tranche_id, char *tranche_name)
Definition: lwlock.c:591
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:586
int lwlock_tranche_id
Definition: slru.h:106
int SimpleLruReadPage ( SlruCtl  ctl,
int  pageno,
bool  write_ok,
TransactionId  xid 
)

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

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

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

463 {
464  SlruShared shared = ctl->shared;
465  int slotno;
466 
467  /* Try to find the page while holding only shared lock */
469 
470  /* See if page is already in a buffer */
471  for (slotno = 0; slotno < shared->num_slots; slotno++)
472  {
473  if (shared->page_number[slotno] == pageno &&
474  shared->page_status[slotno] != SLRU_PAGE_EMPTY &&
475  shared->page_status[slotno] != SLRU_PAGE_READ_IN_PROGRESS)
476  {
477  /* See comments for SlruRecentlyUsed macro */
478  SlruRecentlyUsed(shared, slotno);
479  return slotno;
480  }
481  }
482 
483  /* No luck, so switch to normal exclusive lock and do regular read */
484  LWLockRelease(shared->ControlLock);
486 
487  return SimpleLruReadPage(ctl, pageno, true, xid);
488 }
LWLock * ControlLock
Definition: slru.h:61
int * page_number
Definition: slru.h:73
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1714
SlruPageStatus * page_status
Definition: slru.h:71
int SimpleLruReadPage(SlruCtl ctl, int pageno, bool write_ok, TransactionId xid)
Definition: slru.c:370
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1110
int num_slots
Definition: slru.h:64
SlruShared shared
Definition: slru.h:119
#define SlruRecentlyUsed(shared, slotno)
Definition: slru.c:102
Size SimpleLruShmemSize ( int  nslots,
int  nlsns 
)

Definition at line 144 of file slru.c.

References BUFFERALIGN, and MAXALIGN.

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

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

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

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

References NULL, and SlruInternalWritePage().

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

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

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

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

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

1247 {
1248  SlruShared shared = ctl->shared;
1249  int slotno;
1250  char path[MAXPGPATH];
1251  bool did_write;
1252 
1253  /* Clean out any possibly existing references to the segment. */
1255 restart:
1256  did_write = false;
1257  for (slotno = 0; slotno < shared->num_slots; slotno++)
1258  {
1259  int pagesegno = shared->page_number[slotno] / SLRU_PAGES_PER_SEGMENT;
1260 
1261  if (shared->page_status[slotno] == SLRU_PAGE_EMPTY)
1262  continue;
1263 
1264  /* not the segment we're looking for */
1265  if (pagesegno != segno)
1266  continue;
1267 
1268  /* If page is clean, just change state to EMPTY (expected case). */
1269  if (shared->page_status[slotno] == SLRU_PAGE_VALID &&
1270  !shared->page_dirty[slotno])
1271  {
1272  shared->page_status[slotno] = SLRU_PAGE_EMPTY;
1273  continue;
1274  }
1275 
1276  /* Same logic as SimpleLruTruncate() */
1277  if (shared->page_status[slotno] == SLRU_PAGE_VALID)
1278  SlruInternalWritePage(ctl, slotno, NULL);
1279  else
1280  SimpleLruWaitIO(ctl, slotno);
1281 
1282  did_write = true;
1283  }
1284 
1285  /*
1286  * Be extra careful and re-check. The IO functions release the control
1287  * lock, so new pages could have been read in.
1288  */
1289  if (did_write)
1290  goto restart;
1291 
1292  snprintf(path, MAXPGPATH, "%s/%04X", ctl->Dir, segno);
1293  ereport(DEBUG2,
1294  (errmsg("removing file \"%s\"", path)));
1295  unlink(path);
1296 
1297  LWLockRelease(shared->ControlLock);
1298 }
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:502
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1714
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:316
char Dir[64]
Definition: slru.h:138
#define NULL
Definition: c.h:226
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1110
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 1338 of file slru.c.

References SlruInternalDeleteSegment().

Referenced by AsyncShmemInit(), and DeactivateCommitTs().

1339 {
1341 
1342  return false; /* keep going */
1343 }
static void SlruInternalDeleteSegment(SlruCtl ctl, char *filename)
Definition: slru.c:1232
static char * filename
Definition: pg_dumpall.c:84
bool SlruScanDirCbReportPresence ( SlruCtl  ctl,
char *  filename,
int  segpage,
void *  data 
)

Definition at line 1306 of file slru.c.

References SlruCtlData::PagePrecedes, and SLRU_PAGES_PER_SEGMENT.

Referenced by TruncateCLOG(), and TruncateCommitTs().

1307 {
1308  int cutoffPage = *(int *) data;
1309 
1310  cutoffPage -= cutoffPage % SLRU_PAGES_PER_SEGMENT;
1311 
1312  if (ctl->PagePrecedes(segpage, cutoffPage))
1313  return true; /* found one; don't iterate any more */
1314 
1315  return false; /* keep going */
1316 }
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 1361 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().

1362 {
1363  bool retval = false;
1364  DIR *cldir;
1365  struct dirent *clde;
1366  int segno;
1367  int segpage;
1368 
1369  cldir = AllocateDir(ctl->Dir);
1370  while ((clde = ReadDir(cldir, ctl->Dir)) != NULL)
1371  {
1372  size_t len;
1373 
1374  len = strlen(clde->d_name);
1375 
1376  if ((len == 4 || len == 5 || len == 6) &&
1377  strspn(clde->d_name, "0123456789ABCDEF") == len)
1378  {
1379  segno = (int) strtol(clde->d_name, NULL, 16);
1380  segpage = segno * SLRU_PAGES_PER_SEGMENT;
1381 
1382  elog(DEBUG2, "SlruScanDirectory invoking callback on %s/%s",
1383  ctl->Dir, clde->d_name);
1384  retval = callback(ctl, clde->d_name, segpage, data);
1385  if (retval)
1386  break;
1387  }
1388  }
1389  FreeDir(cldir);
1390 
1391  return retval;
1392 }
Definition: dirent.h:9
Definition: dirent.c:25
static void callback(struct sockaddr *addr, struct sockaddr *mask, void *unused)
Definition: test_ifaddrs.c:49
#define DEBUG2
Definition: elog.h:24
DIR * AllocateDir(const char *dirname)
Definition: fd.c:2284
char Dir[64]
Definition: slru.h:138
#define NULL
Definition: c.h:226
struct dirent * ReadDir(DIR *dir, const char *dirname)
Definition: fd.c:2350
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:2393