PostgreSQL Source Code  git master
xlogprefetcher.h File Reference
Include dependency graph for xlogprefetcher.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Typedefs

typedef struct XLogPrefetcher XLogPrefetcher
 

Enumerations

enum  RecoveryPrefetchValue { RECOVERY_PREFETCH_OFF , RECOVERY_PREFETCH_ON , RECOVERY_PREFETCH_TRY }
 

Functions

void XLogPrefetchReconfigure (void)
 
size_t XLogPrefetchShmemSize (void)
 
void XLogPrefetchShmemInit (void)
 
void XLogPrefetchResetStats (void)
 
XLogPrefetcherXLogPrefetcherAllocate (XLogReaderState *reader)
 
void XLogPrefetcherFree (XLogPrefetcher *prefetcher)
 
XLogReaderStateXLogPrefetcherGetReader (XLogPrefetcher *prefetcher)
 
void XLogPrefetcherBeginRead (XLogPrefetcher *prefetcher, XLogRecPtr recPtr)
 
XLogRecordXLogPrefetcherReadRecord (XLogPrefetcher *prefetcher, char **errmsg)
 
void XLogPrefetcherComputeStats (XLogPrefetcher *prefetcher)
 

Variables

PGDLLIMPORT int recovery_prefetch
 

Typedef Documentation

◆ XLogPrefetcher

Definition at line 21 of file xlogprefetcher.h.

Enumeration Type Documentation

◆ RecoveryPrefetchValue

Enumerator
RECOVERY_PREFETCH_OFF 
RECOVERY_PREFETCH_ON 
RECOVERY_PREFETCH_TRY 

Definition at line 24 of file xlogprefetcher.h.

25 {
RecoveryPrefetchValue
@ RECOVERY_PREFETCH_OFF
@ RECOVERY_PREFETCH_ON
@ RECOVERY_PREFETCH_TRY

Function Documentation

◆ XLogPrefetcherAllocate()

XLogPrefetcher* XLogPrefetcherAllocate ( XLogReaderState reader)

Definition at line 364 of file xlogprefetcher.c.

365 {
366  XLogPrefetcher *prefetcher;
367  static HASHCTL hash_table_ctl = {
368  .keysize = sizeof(RelFileNode),
369  .entrysize = sizeof(XLogPrefetcherFilter)
370  };
371 
372  prefetcher = palloc0(sizeof(XLogPrefetcher));
373 
374  prefetcher->reader = reader;
375  prefetcher->filter_table = hash_create("XLogPrefetcherFilterTable", 1024,
376  &hash_table_ctl,
378  dlist_init(&prefetcher->filter_queue);
379 
382  SharedStats->io_depth = 0;
383 
384  /* First usage will cause streaming_read to be allocated. */
386 
387  return prefetcher;
388 }
HTAB * hash_create(const char *tabname, long nelem, const HASHCTL *info, int flags)
Definition: dynahash.c:349
#define HASH_ELEM
Definition: hsearch.h:95
#define HASH_BLOBS
Definition: hsearch.h:97
static void dlist_init(dlist_head *head)
Definition: ilist.h:278
void * palloc0(Size size)
Definition: mcxt.c:1099
struct RelFileNode RelFileNode
Size keysize
Definition: hsearch.h:75
dlist_head filter_queue
XLogReaderState * reader
static int XLogPrefetchReconfigureCount
static XLogPrefetchStats * SharedStats

References XLogPrefetchStats::block_distance, dlist_init(), XLogPrefetcher::filter_queue, XLogPrefetcher::filter_table, HASH_BLOBS, hash_create(), HASH_ELEM, XLogPrefetchStats::io_depth, HASHCTL::keysize, palloc0(), XLogPrefetcher::reader, XLogPrefetcher::reconfigure_count, SharedStats, XLogPrefetchStats::wal_distance, and XLogPrefetchReconfigureCount.

Referenced by InitWalRecovery().

◆ XLogPrefetcherBeginRead()

void XLogPrefetcherBeginRead ( XLogPrefetcher prefetcher,
XLogRecPtr  recPtr 
)

Definition at line 964 of file xlogprefetcher.c.

965 {
966  /* This will forget about any in-flight IO. */
967  prefetcher->reconfigure_count--;
968 
969  /* Book-keeping to avoid readahead on first read. */
970  prefetcher->begin_ptr = recPtr;
971 
972  prefetcher->no_readahead_until = 0;
973 
974  /* This will forget about any queued up records in the decoder. */
975  XLogBeginRead(prefetcher->reader, recPtr);
976 }
XLogRecPtr no_readahead_until
XLogRecPtr begin_ptr
void XLogBeginRead(XLogReaderState *state, XLogRecPtr RecPtr)
Definition: xlogreader.c:264

References XLogPrefetcher::begin_ptr, XLogPrefetcher::no_readahead_until, XLogPrefetcher::reader, XLogPrefetcher::reconfigure_count, and XLogBeginRead().

Referenced by FinishWalRecovery(), InitWalRecovery(), PerformWalRecovery(), and ReadCheckpointRecord().

◆ XLogPrefetcherComputeStats()

void XLogPrefetcherComputeStats ( XLogPrefetcher prefetcher)

Definition at line 414 of file xlogprefetcher.c.

415 {
416  uint32 io_depth;
417  uint32 completed;
418  int64 wal_distance;
419 
420 
421  /* How far ahead of replay are we now? */
422  if (prefetcher->reader->decode_queue_tail)
423  {
424  wal_distance =
425  prefetcher->reader->decode_queue_tail->lsn -
426  prefetcher->reader->decode_queue_head->lsn;
427  }
428  else
429  {
430  wal_distance = 0;
431  }
432 
433  /* How many IOs are currently in flight and completed? */
434  io_depth = lrq_inflight(prefetcher->streaming_read);
435  completed = lrq_completed(prefetcher->streaming_read);
436 
437  /* Update the instantaneous stats visible in pg_stat_recovery_prefetch. */
438  SharedStats->io_depth = io_depth;
439  SharedStats->block_distance = io_depth + completed;
440  SharedStats->wal_distance = wal_distance;
441 
442  prefetcher->next_stats_shm_lsn =
444 }
unsigned int uint32
Definition: c.h:441
XLogRecPtr lsn
Definition: xlogreader.h:164
LsnReadQueue * streaming_read
XLogRecPtr next_stats_shm_lsn
DecodedXLogRecord * decode_queue_head
Definition: xlogreader.h:260
XLogRecPtr ReadRecPtr
Definition: xlogreader.h:206
DecodedXLogRecord * decode_queue_tail
Definition: xlogreader.h:261
static uint32 lrq_completed(LsnReadQueue *lrq)
static uint32 lrq_inflight(LsnReadQueue *lrq)
#define XLOGPREFETCHER_STATS_DISTANCE

References XLogPrefetchStats::block_distance, XLogReaderState::decode_queue_head, XLogReaderState::decode_queue_tail, XLogPrefetchStats::io_depth, lrq_completed(), lrq_inflight(), DecodedXLogRecord::lsn, XLogPrefetcher::next_stats_shm_lsn, XLogPrefetcher::reader, XLogReaderState::ReadRecPtr, SharedStats, XLogPrefetcher::streaming_read, XLogPrefetchStats::wal_distance, and XLOGPREFETCHER_STATS_DISTANCE.

Referenced by ShutdownWalRecovery(), WaitForWALToBecomeAvailable(), and XLogPrefetcherReadRecord().

◆ XLogPrefetcherFree()

void XLogPrefetcherFree ( XLogPrefetcher prefetcher)

Definition at line 394 of file xlogprefetcher.c.

395 {
396  lrq_free(prefetcher->streaming_read);
397  hash_destroy(prefetcher->filter_table);
398  pfree(prefetcher);
399 }
void hash_destroy(HTAB *hashp)
Definition: dynahash.c:862
void pfree(void *pointer)
Definition: mcxt.c:1175
static void lrq_free(LsnReadQueue *lrq)

References XLogPrefetcher::filter_table, hash_destroy(), lrq_free(), pfree(), and XLogPrefetcher::streaming_read.

Referenced by ShutdownWalRecovery().

◆ XLogPrefetcherGetReader()

XLogReaderState* XLogPrefetcherGetReader ( XLogPrefetcher prefetcher)

Definition at line 405 of file xlogprefetcher.c.

406 {
407  return prefetcher->reader;
408 }

References XLogPrefetcher::reader.

Referenced by ReadRecord().

◆ XLogPrefetcherReadRecord()

XLogRecord* XLogPrefetcherReadRecord ( XLogPrefetcher prefetcher,
char **  errmsg 
)

Definition at line 983 of file xlogprefetcher.c.

984 {
985  DecodedXLogRecord *record;
986 
987  /*
988  * See if it's time to reset the prefetching machinery, because a relevant
989  * GUC was changed.
990  */
992  {
993  uint32 max_distance;
994  uint32 max_inflight;
995 
996  if (prefetcher->streaming_read)
997  lrq_free(prefetcher->streaming_read);
998 
1000  {
1001  max_inflight = Max(maintenance_io_concurrency, 2);
1002  max_distance = max_inflight * XLOGPREFETCHER_DISTANCE_MULTIPLIER;
1003  }
1004  else
1005  {
1006  max_inflight = 1;
1007  max_distance = 1;
1008  }
1009 
1010  prefetcher->streaming_read = lrq_alloc(max_distance,
1011  max_inflight,
1012  (uintptr_t) prefetcher,
1014 
1016  }
1017 
1018  /*
1019  * Release last returned record, if there is one. We need to do this so
1020  * that we can check for empty decode queue accurately.
1021  */
1022  XLogReleasePreviousRecord(prefetcher->reader);
1023 
1024  /* If there's nothing queued yet, then start prefetching. */
1025  if (!XLogReaderHasQueuedRecordOrError(prefetcher->reader))
1026  lrq_prefetch(prefetcher->streaming_read);
1027 
1028  /* Read the next record. */
1029  record = XLogNextRecord(prefetcher->reader, errmsg);
1030  if (!record)
1031  return NULL;
1032 
1033  /*
1034  * The record we just got is the "current" one, for the benefit of the
1035  * XLogRecXXX() macros.
1036  */
1037  Assert(record == prefetcher->reader->record);
1038 
1039  /*
1040  * Can we drop any prefetch filters yet, given the record we're about to
1041  * return? This assumes that any records with earlier LSNs have been
1042  * replayed, so if we were waiting for a relation to be created or
1043  * extended, it is now OK to access blocks in the covered range.
1044  */
1045  XLogPrefetcherCompleteFilters(prefetcher, record->lsn);
1046 
1047  /*
1048  * See if it's time to compute some statistics, because enough WAL has
1049  * been processed.
1050  */
1051  if (unlikely(record->lsn >= prefetcher->next_stats_shm_lsn))
1052  XLogPrefetcherComputeStats(prefetcher);
1053 
1054  /*
1055  * The caller is about to replay this record, so we can now report that
1056  * all IO initiated because of early WAL must be finished. This may
1057  * trigger more readahead.
1058  */
1059  lrq_complete_lsn(prefetcher->streaming_read, record->lsn);
1060 
1061  Assert(record == prefetcher->reader->record);
1062 
1063  return &record->header;
1064 }
int maintenance_io_concurrency
Definition: bufmgr.c:152
#define Max(x, y)
Definition: c.h:980
#define unlikely(x)
Definition: c.h:273
int errmsg(const char *fmt,...)
Definition: elog.c:904
Assert(fmt[strlen(fmt) - 1] !='\n')
XLogRecord header
Definition: xlogreader.h:166
DecodedXLogRecord * record
Definition: xlogreader.h:236
void XLogPrefetcherComputeStats(XLogPrefetcher *prefetcher)
#define RecoveryPrefetchEnabled()
static LsnReadQueue * lrq_alloc(uint32 max_distance, uint32 max_inflight, uintptr_t lrq_private, LsnReadQueueNextFun next)
static void XLogPrefetcherCompleteFilters(XLogPrefetcher *prefetcher, XLogRecPtr replaying_lsn)
static void lrq_prefetch(LsnReadQueue *lrq)
static LsnReadQueueNextStatus XLogPrefetcherNextBlock(uintptr_t pgsr_private, XLogRecPtr *lsn)
static void lrq_complete_lsn(LsnReadQueue *lrq, XLogRecPtr lsn)
#define XLOGPREFETCHER_DISTANCE_MULTIPLIER
DecodedXLogRecord * XLogNextRecord(XLogReaderState *state, char **errormsg)
Definition: xlogreader.c:354
void XLogReleasePreviousRecord(XLogReaderState *state)
Definition: xlogreader.c:282
static bool XLogReaderHasQueuedRecordOrError(XLogReaderState *state)
Definition: xlogreader.h:325

References Assert(), errmsg(), DecodedXLogRecord::header, lrq_alloc(), lrq_complete_lsn(), lrq_free(), lrq_prefetch(), DecodedXLogRecord::lsn, maintenance_io_concurrency, Max, XLogPrefetcher::next_stats_shm_lsn, XLogPrefetcher::reader, XLogPrefetcher::reconfigure_count, XLogReaderState::record, RecoveryPrefetchEnabled, XLogPrefetcher::streaming_read, unlikely, XLogNextRecord(), XLOGPREFETCHER_DISTANCE_MULTIPLIER, XLogPrefetcherCompleteFilters(), XLogPrefetcherComputeStats(), XLogPrefetcherNextBlock(), XLogPrefetchReconfigureCount, XLogReaderHasQueuedRecordOrError(), and XLogReleasePreviousRecord().

Referenced by ReadRecord().

◆ XLogPrefetchReconfigure()

void XLogPrefetchReconfigure ( void  )

◆ XLogPrefetchResetStats()

void XLogPrefetchResetStats ( void  )

Definition at line 305 of file xlogprefetcher.c.

306 {
314 }
static void pg_atomic_write_u64(volatile pg_atomic_uint64 *ptr, uint64 val)
Definition: atomics.h:438
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1574
pg_atomic_uint64 skip_fpw
pg_atomic_uint64 skip_init
pg_atomic_uint64 reset_time
pg_atomic_uint64 hit
pg_atomic_uint64 prefetch
pg_atomic_uint64 skip_rep
pg_atomic_uint64 skip_new

References GetCurrentTimestamp(), XLogPrefetchStats::hit, pg_atomic_write_u64(), XLogPrefetchStats::prefetch, XLogPrefetchStats::reset_time, SharedStats, XLogPrefetchStats::skip_fpw, XLogPrefetchStats::skip_init, XLogPrefetchStats::skip_new, and XLogPrefetchStats::skip_rep.

Referenced by pg_stat_reset_shared().

◆ XLogPrefetchShmemInit()

void XLogPrefetchShmemInit ( void  )

Definition at line 317 of file xlogprefetcher.c.

318 {
319  bool found;
320 
322  ShmemInitStruct("XLogPrefetchStats",
323  sizeof(XLogPrefetchStats),
324  &found);
325 
326  if (!found)
327  {
335  }
336 }
static void pg_atomic_init_u64(volatile pg_atomic_uint64 *ptr, uint64 val)
Definition: atomics.h:415
void * ShmemInitStruct(const char *name, Size size, bool *foundPtr)
Definition: shmem.c:396

References GetCurrentTimestamp(), XLogPrefetchStats::hit, pg_atomic_init_u64(), XLogPrefetchStats::prefetch, XLogPrefetchStats::reset_time, SharedStats, ShmemInitStruct(), XLogPrefetchStats::skip_fpw, XLogPrefetchStats::skip_init, XLogPrefetchStats::skip_new, and XLogPrefetchStats::skip_rep.

Referenced by CreateSharedMemoryAndSemaphores().

◆ XLogPrefetchShmemSize()

size_t XLogPrefetchShmemSize ( void  )

Definition at line 296 of file xlogprefetcher.c.

297 {
298  return sizeof(XLogPrefetchStats);
299 }
struct XLogPrefetchStats XLogPrefetchStats

Referenced by CalculateShmemSize().

Variable Documentation

◆ recovery_prefetch

PGDLLIMPORT int recovery_prefetch
extern

Definition at line 72 of file xlogprefetcher.c.

Referenced by assign_recovery_prefetch().