PostgreSQL Source Code  git master
autoprewarm.c File Reference
#include "postgres.h"
#include <unistd.h>
#include "access/relation.h"
#include "access/xact.h"
#include "catalog/pg_class.h"
#include "catalog/pg_type.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "postmaster/bgworker.h"
#include "postmaster/interrupt.h"
#include "storage/buf_internals.h"
#include "storage/dsm.h"
#include "storage/ipc.h"
#include "storage/latch.h"
#include "storage/lwlock.h"
#include "storage/proc.h"
#include "storage/procsignal.h"
#include "storage/shmem.h"
#include "storage/smgr.h"
#include "tcop/tcopprot.h"
#include "utils/acl.h"
#include "utils/datetime.h"
#include "utils/guc.h"
#include "utils/memutils.h"
#include "utils/rel.h"
#include "utils/relfilenodemap.h"
#include "utils/resowner.h"
Include dependency graph for autoprewarm.c:

Go to the source code of this file.

Data Structures

struct  BlockInfoRecord
 
struct  AutoPrewarmSharedState
 

Macros

#define AUTOPREWARM_FILE   "autoprewarm.blocks"
 
#define cmp_member_elem(fld)
 

Typedefs

typedef struct BlockInfoRecord BlockInfoRecord
 
typedef struct AutoPrewarmSharedState AutoPrewarmSharedState
 

Functions

void _PG_init (void)
 
void autoprewarm_main (Datum main_arg)
 
void autoprewarm_database_main (Datum main_arg)
 
 PG_FUNCTION_INFO_V1 (autoprewarm_start_worker)
 
 PG_FUNCTION_INFO_V1 (autoprewarm_dump_now)
 
static void apw_load_buffers (void)
 
static int apw_dump_now (bool is_bgworker, bool dump_unlogged)
 
static void apw_start_leader_worker (void)
 
static void apw_start_database_worker (void)
 
static bool apw_init_shmem (void)
 
static void apw_detach_shmem (int code, Datum arg)
 
static int apw_compare_blockinfo (const void *p, const void *q)
 
Datum autoprewarm_start_worker (PG_FUNCTION_ARGS)
 
Datum autoprewarm_dump_now (PG_FUNCTION_ARGS)
 

Variables

static AutoPrewarmSharedStateapw_state = NULL
 
static bool autoprewarm = true
 
static int autoprewarm_interval
 

Macro Definition Documentation

◆ AUTOPREWARM_FILE

#define AUTOPREWARM_FILE   "autoprewarm.blocks"

Definition at line 57 of file autoprewarm.c.

Referenced by apw_dump_now(), and apw_load_buffers().

◆ cmp_member_elem

#define cmp_member_elem (   fld)
Value:
do { \
if (a->fld < b->fld) \
return -1; \
else if (a->fld > b->fld) \
return 1; \
} while(0)

Definition at line 860 of file autoprewarm.c.

Referenced by apw_compare_blockinfo().

Typedef Documentation

◆ AutoPrewarmSharedState

◆ BlockInfoRecord

Function Documentation

◆ _PG_init()

void _PG_init ( void  )

Definition at line 110 of file autoprewarm.c.

References apw_start_leader_worker(), autoprewarm, autoprewarm_interval, DefineCustomBoolVariable(), DefineCustomIntVariable(), EmitWarningsOnPlaceholders(), GUC_UNIT_S, MAXALIGN, PGC_POSTMASTER, PGC_SIGHUP, process_shared_preload_libraries_in_progress, and RequestAddinShmemSpace().

111 {
112  DefineCustomIntVariable("pg_prewarm.autoprewarm_interval",
113  "Sets the interval between dumps of shared buffers",
114  "If set to zero, time-based dumping is disabled.",
116  300,
117  0, INT_MAX / 1000,
118  PGC_SIGHUP,
119  GUC_UNIT_S,
120  NULL,
121  NULL,
122  NULL);
123 
125  return;
126 
127  /* can't define PGC_POSTMASTER variable after startup */
128  DefineCustomBoolVariable("pg_prewarm.autoprewarm",
129  "Starts the autoprewarm worker.",
130  NULL,
131  &autoprewarm,
132  true,
134  0,
135  NULL,
136  NULL,
137  NULL);
138 
139  EmitWarningsOnPlaceholders("pg_prewarm");
140 
142 
143  /* Register autoprewarm worker, if enabled. */
144  if (autoprewarm)
146 }
void DefineCustomIntVariable(const char *name, const char *short_desc, const char *long_desc, int *valueAddr, int bootValue, int minValue, int maxValue, GucContext context, int flags, GucIntCheckHook check_hook, GucIntAssignHook assign_hook, GucShowHook show_hook)
Definition: guc.c:8920
void RequestAddinShmemSpace(Size size)
Definition: ipci.c:71
bool process_shared_preload_libraries_in_progress
Definition: miscinit.c:1596
static int autoprewarm_interval
Definition: autoprewarm.c:104
#define GUC_UNIT_S
Definition: guc.h:227
void EmitWarningsOnPlaceholders(const char *className)
Definition: guc.c:9033
static void apw_start_leader_worker(void)
Definition: autoprewarm.c:786
Definition: guc.h:72
static bool autoprewarm
Definition: autoprewarm.c:103
#define MAXALIGN(LEN)
Definition: c.h:753
void DefineCustomBoolVariable(const char *name, const char *short_desc, const char *long_desc, bool *valueAddr, bool bootValue, GucContext context, int flags, GucBoolCheckHook check_hook, GucBoolAssignHook assign_hook, GucShowHook show_hook)
Definition: guc.c:8894

◆ apw_compare_blockinfo()

static int apw_compare_blockinfo ( const void *  p,
const void *  q 
)
static

Definition at line 878 of file autoprewarm.c.

References BlockInfoRecord::blocknum, cmp_member_elem, BlockInfoRecord::database, BlockInfoRecord::filenode, BlockInfoRecord::forknum, and BlockInfoRecord::tablespace.

Referenced by apw_load_buffers().

879 {
880  const BlockInfoRecord *a = (const BlockInfoRecord *) p;
881  const BlockInfoRecord *b = (const BlockInfoRecord *) q;
882 
883  cmp_member_elem(database);
885  cmp_member_elem(filenode);
886  cmp_member_elem(forknum);
887  cmp_member_elem(blocknum);
888 
889  return 0;
890 }
#define cmp_member_elem(fld)
Definition: autoprewarm.c:860
char * tablespace
Definition: pgbench.c:189

◆ apw_detach_shmem()

static void apw_detach_shmem ( int  code,
Datum  arg 
)
static

Definition at line 772 of file autoprewarm.c.

References AutoPrewarmSharedState::bgworker_pid, InvalidPid, AutoPrewarmSharedState::lock, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), MyProcPid, and AutoPrewarmSharedState::pid_using_dumpfile.

Referenced by autoprewarm_dump_now(), and autoprewarm_main().

773 {
780 }
int MyProcPid
Definition: globals.c:40
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1811
static AutoPrewarmSharedState * apw_state
Definition: autoprewarm.c:100
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1207
#define InvalidPid
Definition: miscadmin.h:32

◆ apw_dump_now()

static int apw_dump_now ( bool  is_bgworker,
bool  dump_unlogged 
)
static

Definition at line 553 of file autoprewarm.c.

References AllocateFile(), AUTOPREWARM_FILE, BlockInfoRecord::blocknum, buftag::blockNum, BM_PERMANENT, BM_TAG_VALID, CHECK_FOR_INTERRUPTS, BlockInfoRecord::database, RelFileNode::dbNode, DEBUG1, durable_rename(), ereport, errcode_for_file_access(), errmsg(), ERROR, BlockInfoRecord::filenode, BlockInfoRecord::forknum, buftag::forkNum, fprintf, FreeFile(), GetBufferDescriptor, i, InvalidPid, AutoPrewarmSharedState::lock, LockBufHdr(), LOG, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), MAXPGPATH, MyProcPid, NBuffers, palloc(), pfree(), AutoPrewarmSharedState::pid_using_dumpfile, RelFileNode::relNode, buftag::rnode, snprintf, RelFileNode::spcNode, BlockInfoRecord::tablespace, BufferDesc::tag, and UnlockBufHdr.

Referenced by autoprewarm_dump_now(), and autoprewarm_main().

554 {
555  int num_blocks;
556  int i;
557  int ret;
558  BlockInfoRecord *block_info_array;
559  BufferDesc *bufHdr;
560  FILE *file;
561  char transient_dump_file_path[MAXPGPATH];
562  pid_t pid;
563 
569 
570  if (pid != InvalidPid)
571  {
572  if (!is_bgworker)
573  ereport(ERROR,
574  (errmsg("could not perform block dump because dump file is being used by PID %lu",
575  (unsigned long) apw_state->pid_using_dumpfile)));
576 
577  ereport(LOG,
578  (errmsg("skipping block dump because it is already being performed by PID %lu",
579  (unsigned long) apw_state->pid_using_dumpfile)));
580  return 0;
581  }
582 
583  block_info_array =
585 
586  for (num_blocks = 0, i = 0; i < NBuffers; i++)
587  {
588  uint32 buf_state;
589 
591 
592  bufHdr = GetBufferDescriptor(i);
593 
594  /* Lock each buffer header before inspecting. */
595  buf_state = LockBufHdr(bufHdr);
596 
597  /*
598  * Unlogged tables will be automatically truncated after a crash or
599  * unclean shutdown. In such cases we need not prewarm them. Dump them
600  * only if requested by caller.
601  */
602  if (buf_state & BM_TAG_VALID &&
603  ((buf_state & BM_PERMANENT) || dump_unlogged))
604  {
605  block_info_array[num_blocks].database = bufHdr->tag.rnode.dbNode;
606  block_info_array[num_blocks].tablespace = bufHdr->tag.rnode.spcNode;
607  block_info_array[num_blocks].filenode = bufHdr->tag.rnode.relNode;
608  block_info_array[num_blocks].forknum = bufHdr->tag.forkNum;
609  block_info_array[num_blocks].blocknum = bufHdr->tag.blockNum;
610  ++num_blocks;
611  }
612 
613  UnlockBufHdr(bufHdr, buf_state);
614  }
615 
616  snprintf(transient_dump_file_path, MAXPGPATH, "%s.tmp", AUTOPREWARM_FILE);
617  file = AllocateFile(transient_dump_file_path, "w");
618  if (!file)
619  ereport(ERROR,
621  errmsg("could not open file \"%s\": %m",
622  transient_dump_file_path)));
623 
624  ret = fprintf(file, "<<%d>>\n", num_blocks);
625  if (ret < 0)
626  {
627  int save_errno = errno;
628 
629  FreeFile(file);
630  unlink(transient_dump_file_path);
631  errno = save_errno;
632  ereport(ERROR,
634  errmsg("could not write to file \"%s\": %m",
635  transient_dump_file_path)));
636  }
637 
638  for (i = 0; i < num_blocks; i++)
639  {
641 
642  ret = fprintf(file, "%u,%u,%u,%u,%u\n",
643  block_info_array[i].database,
644  block_info_array[i].tablespace,
645  block_info_array[i].filenode,
646  (uint32) block_info_array[i].forknum,
647  block_info_array[i].blocknum);
648  if (ret < 0)
649  {
650  int save_errno = errno;
651 
652  FreeFile(file);
653  unlink(transient_dump_file_path);
654  errno = save_errno;
655  ereport(ERROR,
657  errmsg("could not write to file \"%s\": %m",
658  transient_dump_file_path)));
659  }
660  }
661 
662  pfree(block_info_array);
663 
664  /*
665  * Rename transient_dump_file_path to AUTOPREWARM_FILE to make things
666  * permanent.
667  */
668  ret = FreeFile(file);
669  if (ret != 0)
670  {
671  int save_errno = errno;
672 
673  unlink(transient_dump_file_path);
674  errno = save_errno;
675  ereport(ERROR,
677  errmsg("could not close file \"%s\": %m",
678  transient_dump_file_path)));
679  }
680 
681  (void) durable_rename(transient_dump_file_path, AUTOPREWARM_FILE, ERROR);
683 
684  ereport(DEBUG1,
685  (errmsg("wrote block details for %d blocks", num_blocks)));
686  return num_blocks;
687 }
#define BM_PERMANENT
Definition: buf_internals.h:66
#define DEBUG1
Definition: elog.h:25
int MyProcPid
Definition: globals.c:40
#define BM_TAG_VALID
Definition: buf_internals.h:60
ForkNumber forkNum
Definition: buf_internals.h:93
#define LOG
Definition: elog.h:26
#define fprintf
Definition: port.h:219
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1811
#define AUTOPREWARM_FILE
Definition: autoprewarm.c:57
ForkNumber forknum
Definition: autoprewarm.c:65
void pfree(void *pointer)
Definition: mcxt.c:1057
#define ERROR
Definition: elog.h:43
static AutoPrewarmSharedState * apw_state
Definition: autoprewarm.c:100
#define MAXPGPATH
char * tablespace
Definition: pgbench.c:189
int errcode_for_file_access(void)
Definition: elog.c:714
#define GetBufferDescriptor(id)
FILE * AllocateFile(const char *name, const char *mode)
Definition: fd.c:2353
unsigned int uint32
Definition: c.h:429
int durable_rename(const char *oldfile, const char *newfile, int elevel)
Definition: fd.c:686
#define ereport(elevel,...)
Definition: elog.h:155
uint32 LockBufHdr(BufferDesc *desc)
Definition: bufmgr.c:4320
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1207
BlockNumber blockNum
Definition: buf_internals.h:94
RelFileNode rnode
Definition: buf_internals.h:92
int FreeFile(FILE *file)
Definition: fd.c:2552
BufferTag tag
BlockNumber blocknum
Definition: autoprewarm.c:66
void * palloc(Size size)
Definition: mcxt.c:950
int errmsg(const char *fmt,...)
Definition: elog.c:902
#define UnlockBufHdr(desc, s)
int i
int NBuffers
Definition: globals.c:132
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:99
#define snprintf
Definition: port.h:215
#define InvalidPid
Definition: miscadmin.h:32

◆ apw_init_shmem()

static bool apw_init_shmem ( void  )
static

Definition at line 746 of file autoprewarm.c.

References AutoPrewarmSharedState::bgworker_pid, InvalidPid, AutoPrewarmSharedState::lock, LW_EXCLUSIVE, LWLockAcquire(), LWLockInitialize(), LWLockNewTrancheId(), LWLockRegisterTranche(), LWLockRelease(), AutoPrewarmSharedState::pid_using_dumpfile, ShmemInitStruct(), and LWLock::tranche.

Referenced by autoprewarm_database_main(), autoprewarm_dump_now(), autoprewarm_main(), and autoprewarm_start_worker().

747 {
748  bool found;
749 
750  LWLockAcquire(AddinShmemInitLock, LW_EXCLUSIVE);
751  apw_state = ShmemInitStruct("autoprewarm",
752  sizeof(AutoPrewarmSharedState),
753  &found);
754  if (!found)
755  {
756  /* First time through ... */
760  }
761  LWLockRelease(AddinShmemInitLock);
762 
763  LWLockRegisterTranche(apw_state->lock.tranche, "autoprewarm");
764 
765  return found;
766 }
int LWLockNewTrancheId(void)
Definition: lwlock.c:628
void LWLockRegisterTranche(int tranche_id, const char *tranche_name)
Definition: lwlock.c:652
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1811
static AutoPrewarmSharedState * apw_state
Definition: autoprewarm.c:100
void * ShmemInitStruct(const char *name, Size size, bool *foundPtr)
Definition: shmem.c:392
uint16 tranche
Definition: lwlock.h:33
void LWLockInitialize(LWLock *lock, int tranche_id)
Definition: lwlock.c:744
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1207
#define InvalidPid
Definition: miscadmin.h:32

◆ apw_load_buffers()

static void apw_load_buffers ( void  )
static

Definition at line 265 of file autoprewarm.c.

References AllocateFile(), apw_compare_blockinfo(), apw_start_database_worker(), Assert, AUTOPREWARM_FILE, AutoPrewarmSharedState::block_info_handle, BlockInfoRecord::blocknum, BlockInfoRecord::database, AutoPrewarmSharedState::database, dsm_create(), dsm_detach(), DSM_HANDLE_INVALID, dsm_segment_address(), dsm_segment_handle(), ereport, errcode_for_file_access(), errmsg(), ERROR, BlockInfoRecord::filenode, BlockInfoRecord::forknum, FreeFile(), have_free_buffer(), i, InvalidOid, InvalidPid, AutoPrewarmSharedState::lock, LOG, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), MyProcPid, pg_qsort(), AutoPrewarmSharedState::pid_using_dumpfile, AutoPrewarmSharedState::prewarm_start_idx, AutoPrewarmSharedState::prewarm_stop_idx, AutoPrewarmSharedState::prewarmed_blocks, and BlockInfoRecord::tablespace.

Referenced by autoprewarm_main().

266 {
267  FILE *file = NULL;
268  int num_elements,
269  i;
270  BlockInfoRecord *blkinfo;
271  dsm_segment *seg;
272 
273  /*
274  * Skip the prewarm if the dump file is in use; otherwise, prevent any
275  * other process from writing it while we're using it.
276  */
280  else
281  {
283  ereport(LOG,
284  (errmsg("skipping prewarm because block dump file is being written by PID %lu",
285  (unsigned long) apw_state->pid_using_dumpfile)));
286  return;
287  }
289 
290  /*
291  * Open the block dump file. Exit quietly if it doesn't exist, but report
292  * any other error.
293  */
294  file = AllocateFile(AUTOPREWARM_FILE, "r");
295  if (!file)
296  {
297  if (errno == ENOENT)
298  {
302  return; /* No file to load. */
303  }
304  ereport(ERROR,
306  errmsg("could not read file \"%s\": %m",
307  AUTOPREWARM_FILE)));
308  }
309 
310  /* First line of the file is a record count. */
311  if (fscanf(file, "<<%d>>\n", &num_elements) != 1)
312  ereport(ERROR,
314  errmsg("could not read from file \"%s\": %m",
315  AUTOPREWARM_FILE)));
316 
317  /* Allocate a dynamic shared memory segment to store the record data. */
318  seg = dsm_create(sizeof(BlockInfoRecord) * num_elements, 0);
319  blkinfo = (BlockInfoRecord *) dsm_segment_address(seg);
320 
321  /* Read records, one per line. */
322  for (i = 0; i < num_elements; i++)
323  {
324  unsigned forknum;
325 
326  if (fscanf(file, "%u,%u,%u,%u,%u\n", &blkinfo[i].database,
327  &blkinfo[i].tablespace, &blkinfo[i].filenode,
328  &forknum, &blkinfo[i].blocknum) != 5)
329  ereport(ERROR,
330  (errmsg("autoprewarm block dump file is corrupted at line %d",
331  i + 1)));
332  blkinfo[i].forknum = forknum;
333  }
334 
335  FreeFile(file);
336 
337  /* Sort the blocks to be loaded. */
338  pg_qsort(blkinfo, num_elements, sizeof(BlockInfoRecord),
340 
341  /* Populate shared memory state. */
345 
346  /* Get the info position of the first block of the next database. */
347  while (apw_state->prewarm_start_idx < num_elements)
348  {
349  int j = apw_state->prewarm_start_idx;
350  Oid current_db = blkinfo[j].database;
351 
352  /*
353  * Advance the prewarm_stop_idx to the first BlockInfoRecord that does
354  * not belong to this database.
355  */
356  j++;
357  while (j < num_elements)
358  {
359  if (current_db != blkinfo[j].database)
360  {
361  /*
362  * Combine BlockInfoRecords for global objects with those of
363  * the database.
364  */
365  if (current_db != InvalidOid)
366  break;
367  current_db = blkinfo[j].database;
368  }
369 
370  j++;
371  }
372 
373  /*
374  * If we reach this point with current_db == InvalidOid, then only
375  * BlockInfoRecords belonging to global objects exist. We can't
376  * prewarm without a database connection, so just bail out.
377  */
378  if (current_db == InvalidOid)
379  break;
380 
381  /* Configure stop point and database for next per-database worker. */
383  apw_state->database = current_db;
385 
386  /* If we've run out of free buffers, don't launch another worker. */
387  if (!have_free_buffer())
388  break;
389 
390  /*
391  * Start a per-database worker to load blocks for this database; this
392  * function will return once the per-database worker exits.
393  */
395 
396  /* Prepare for next database. */
398  }
399 
400  /* Clean up. */
401  dsm_detach(seg);
406 
407  /* Report our success. */
408  ereport(LOG,
409  (errmsg("autoprewarm successfully prewarmed %d of %d previously-loaded blocks",
410  apw_state->prewarmed_blocks, num_elements)));
411 }
int MyProcPid
Definition: globals.c:40
dsm_handle dsm_segment_handle(dsm_segment *seg)
Definition: dsm.c:1082
#define LOG
Definition: elog.h:26
unsigned int Oid
Definition: postgres_ext.h:31
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1811
#define DSM_HANDLE_INVALID
Definition: dsm.h:23
#define AUTOPREWARM_FILE
Definition: autoprewarm.c:57
ForkNumber forknum
Definition: autoprewarm.c:65
#define ERROR
Definition: elog.h:43
static AutoPrewarmSharedState * apw_state
Definition: autoprewarm.c:100
static void apw_start_database_worker(void)
Definition: autoprewarm.c:828
char * tablespace
Definition: pgbench.c:189
int errcode_for_file_access(void)
Definition: elog.c:714
dsm_handle block_info_handle
Definition: autoprewarm.c:77
FILE * AllocateFile(const char *name, const char *mode)
Definition: fd.c:2353
dsm_segment * dsm_create(Size size, int flags)
Definition: dsm.c:487
bool have_free_buffer(void)
Definition: freelist.c:180
#define InvalidOid
Definition: postgres_ext.h:36
#define ereport(elevel,...)
Definition: elog.h:155
void * dsm_segment_address(dsm_segment *seg)
Definition: dsm.c:1054
#define Assert(condition)
Definition: c.h:800
void pg_qsort(void *base, size_t nel, size_t elsize, int(*cmp)(const void *, const void *))
Definition: qsort.c:113
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1207
int FreeFile(FILE *file)
Definition: fd.c:2552
void dsm_detach(dsm_segment *seg)
Definition: dsm.c:769
int errmsg(const char *fmt,...)
Definition: elog.c:902
int i
static int apw_compare_blockinfo(const void *p, const void *q)
Definition: autoprewarm.c:878
#define InvalidPid
Definition: miscadmin.h:32

◆ apw_start_database_worker()

static void apw_start_database_worker ( void  )
static

Definition at line 828 of file autoprewarm.c.

References BackgroundWorker::bgw_flags, BackgroundWorker::bgw_function_name, BackgroundWorker::bgw_library_name, BackgroundWorker::bgw_name, BGW_NEVER_RESTART, BackgroundWorker::bgw_notify_pid, BackgroundWorker::bgw_restart_time, BackgroundWorker::bgw_start_time, BackgroundWorker::bgw_type, BGWORKER_BACKEND_DATABASE_CONNECTION, BGWORKER_SHMEM_ACCESS, BgWorkerStart_ConsistentState, ereport, errcode(), errhint(), errmsg(), ERROR, MemSet, MyProcPid, RegisterDynamicBackgroundWorker(), and WaitForBackgroundWorkerShutdown().

Referenced by apw_load_buffers().

829 {
830  BackgroundWorker worker;
831  BackgroundWorkerHandle *handle;
832 
833  MemSet(&worker, 0, sizeof(BackgroundWorker));
834  worker.bgw_flags =
838  strcpy(worker.bgw_library_name, "pg_prewarm");
839  strcpy(worker.bgw_function_name, "autoprewarm_database_main");
840  strcpy(worker.bgw_name, "autoprewarm worker");
841  strcpy(worker.bgw_type, "autoprewarm worker");
842 
843  /* must set notify PID to wait for shutdown */
844  worker.bgw_notify_pid = MyProcPid;
845 
846  if (!RegisterDynamicBackgroundWorker(&worker, &handle))
847  ereport(ERROR,
848  (errcode(ERRCODE_INSUFFICIENT_RESOURCES),
849  errmsg("registering dynamic bgworker autoprewarm failed"),
850  errhint("Consider increasing configuration parameter \"max_worker_processes\".")));
851 
852  /*
853  * Ignore return value; if it fails, postmaster has died, but we have
854  * checks for that elsewhere.
855  */
857 }
int MyProcPid
Definition: globals.c:40
int errhint(const char *fmt,...)
Definition: elog.c:1149
int bgw_restart_time
Definition: bgworker.h:94
int errcode(int sqlerrcode)
Definition: elog.c:691
#define MemSet(start, val, len)
Definition: c.h:1004
#define BGWORKER_SHMEM_ACCESS
Definition: bgworker.h:52
char bgw_function_name[BGW_MAXLEN]
Definition: bgworker.h:96
#define ERROR
Definition: elog.h:43
BgwHandleStatus WaitForBackgroundWorkerShutdown(BackgroundWorkerHandle *handle)
Definition: bgworker.c:1124
#define BGW_NEVER_RESTART
Definition: bgworker.h:84
#define ereport(elevel,...)
Definition: elog.h:155
char bgw_name[BGW_MAXLEN]
Definition: bgworker.h:90
#define BGWORKER_BACKEND_DATABASE_CONNECTION
Definition: bgworker.h:59
BgWorkerStartTime bgw_start_time
Definition: bgworker.h:93
bool RegisterDynamicBackgroundWorker(BackgroundWorker *worker, BackgroundWorkerHandle **handle)
Definition: bgworker.c:918
char bgw_type[BGW_MAXLEN]
Definition: bgworker.h:91
int errmsg(const char *fmt,...)
Definition: elog.c:902
pid_t bgw_notify_pid
Definition: bgworker.h:99
char bgw_library_name[BGW_MAXLEN]
Definition: bgworker.h:95

◆ apw_start_leader_worker()

static void apw_start_leader_worker ( void  )
static

Definition at line 786 of file autoprewarm.c.

References BackgroundWorker::bgw_flags, BackgroundWorker::bgw_function_name, BackgroundWorker::bgw_library_name, BackgroundWorker::bgw_name, BackgroundWorker::bgw_notify_pid, BackgroundWorker::bgw_start_time, BackgroundWorker::bgw_type, BGWH_STARTED, BGWORKER_SHMEM_ACCESS, BgWorkerStart_ConsistentState, ereport, errcode(), errhint(), errmsg(), ERROR, MemSet, MyProcPid, process_shared_preload_libraries_in_progress, RegisterBackgroundWorker(), RegisterDynamicBackgroundWorker(), status(), and WaitForBackgroundWorkerStartup().

Referenced by _PG_init(), and autoprewarm_start_worker().

787 {
788  BackgroundWorker worker;
789  BackgroundWorkerHandle *handle;
791  pid_t pid;
792 
793  MemSet(&worker, 0, sizeof(BackgroundWorker));
796  strcpy(worker.bgw_library_name, "pg_prewarm");
797  strcpy(worker.bgw_function_name, "autoprewarm_main");
798  strcpy(worker.bgw_name, "autoprewarm leader");
799  strcpy(worker.bgw_type, "autoprewarm leader");
800 
802  {
803  RegisterBackgroundWorker(&worker);
804  return;
805  }
806 
807  /* must set notify PID to wait for startup */
808  worker.bgw_notify_pid = MyProcPid;
809 
810  if (!RegisterDynamicBackgroundWorker(&worker, &handle))
811  ereport(ERROR,
812  (errcode(ERRCODE_INSUFFICIENT_RESOURCES),
813  errmsg("could not register background process"),
814  errhint("You may need to increase max_worker_processes.")));
815 
816  status = WaitForBackgroundWorkerStartup(handle, &pid);
817  if (status != BGWH_STARTED)
818  ereport(ERROR,
819  (errcode(ERRCODE_INSUFFICIENT_RESOURCES),
820  errmsg("could not start background process"),
821  errhint("More details may be available in the server log.")));
822 }
BgwHandleStatus WaitForBackgroundWorkerStartup(BackgroundWorkerHandle *handle, pid_t *pidp)
Definition: bgworker.c:1082
void RegisterBackgroundWorker(BackgroundWorker *worker)
Definition: bgworker.c:834
int MyProcPid
Definition: globals.c:40
int errhint(const char *fmt,...)
Definition: elog.c:1149
bool process_shared_preload_libraries_in_progress
Definition: miscinit.c:1596
int errcode(int sqlerrcode)
Definition: elog.c:691
#define MemSet(start, val, len)
Definition: c.h:1004
#define BGWORKER_SHMEM_ACCESS
Definition: bgworker.h:52
char bgw_function_name[BGW_MAXLEN]
Definition: bgworker.h:96
#define ERROR
Definition: elog.h:43
BgwHandleStatus
Definition: bgworker.h:102
#define ereport(elevel,...)
Definition: elog.h:155
char bgw_name[BGW_MAXLEN]
Definition: bgworker.h:90
BgWorkerStartTime bgw_start_time
Definition: bgworker.h:93
bool RegisterDynamicBackgroundWorker(BackgroundWorker *worker, BackgroundWorkerHandle **handle)
Definition: bgworker.c:918
char bgw_type[BGW_MAXLEN]
Definition: bgworker.h:91
int errmsg(const char *fmt,...)
Definition: elog.c:902
pid_t bgw_notify_pid
Definition: bgworker.h:99
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:227
char bgw_library_name[BGW_MAXLEN]
Definition: bgworker.h:95

◆ autoprewarm_database_main()

void autoprewarm_database_main ( Datum  main_arg)

Definition at line 418 of file autoprewarm.c.

References AccessShareLock, apw_init_shmem(), Assert, BackgroundWorkerInitializeConnectionByOid(), BackgroundWorkerUnblockSignals(), AutoPrewarmSharedState::block_info_handle, BlockInfoRecord::blocknum, buf, BufferIsValid, CHECK_FOR_INTERRUPTS, CommitTransactionCommand(), BlockInfoRecord::database, AutoPrewarmSharedState::database, die, dsm_attach(), dsm_detach(), dsm_segment_address(), ereport, errcode(), errmsg(), ERROR, BlockInfoRecord::filenode, BlockInfoRecord::forknum, have_free_buffer(), InvalidForkNumber, InvalidOid, MAX_FORKNUM, OidIsValid, pqsignal(), AutoPrewarmSharedState::prewarm_start_idx, AutoPrewarmSharedState::prewarmed_blocks, RBM_NORMAL, RelationData::rd_smgr, ReadBufferExtended(), relation_close(), RelationGetNumberOfBlocksInFork(), RelationOpenSmgr, ReleaseBuffer(), RelidByRelfilenode(), smgrexists(), StartTransactionCommand(), BlockInfoRecord::tablespace, and try_relation_open().

419 {
420  int pos;
421  BlockInfoRecord *block_info;
422  Relation rel = NULL;
423  BlockNumber nblocks = 0;
424  BlockInfoRecord *old_blk = NULL;
425  dsm_segment *seg;
426 
427  /* Establish signal handlers; once that's done, unblock signals. */
428  pqsignal(SIGTERM, die);
430 
431  /* Connect to correct database and get block information. */
432  apw_init_shmem();
434  if (seg == NULL)
435  ereport(ERROR,
436  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
437  errmsg("could not map dynamic shared memory segment")));
439  block_info = (BlockInfoRecord *) dsm_segment_address(seg);
441 
442  /*
443  * Loop until we run out of blocks to prewarm or until we run out of free
444  * buffers.
445  */
446  while (pos < apw_state->prewarm_stop_idx && have_free_buffer())
447  {
448  BlockInfoRecord *blk = &block_info[pos++];
449  Buffer buf;
450 
452 
453  /*
454  * Quit if we've reached records for another database. If previous
455  * blocks are of some global objects, then continue pre-warming.
456  */
457  if (old_blk != NULL && old_blk->database != blk->database &&
458  old_blk->database != 0)
459  break;
460 
461  /*
462  * As soon as we encounter a block of a new relation, close the old
463  * relation. Note that rel will be NULL if try_relation_open failed
464  * previously; in that case, there is nothing to close.
465  */
466  if (old_blk != NULL && old_blk->filenode != blk->filenode &&
467  rel != NULL)
468  {
470  rel = NULL;
472  }
473 
474  /*
475  * Try to open each new relation, but only once, when we first
476  * encounter it. If it's been dropped, skip the associated blocks.
477  */
478  if (old_blk == NULL || old_blk->filenode != blk->filenode)
479  {
480  Oid reloid;
481 
482  Assert(rel == NULL);
484  reloid = RelidByRelfilenode(blk->tablespace, blk->filenode);
485  if (OidIsValid(reloid))
486  rel = try_relation_open(reloid, AccessShareLock);
487 
488  if (!rel)
490  }
491  if (!rel)
492  {
493  old_blk = blk;
494  continue;
495  }
496 
497  /* Once per fork, check for fork existence and size. */
498  if (old_blk == NULL ||
499  old_blk->filenode != blk->filenode ||
500  old_blk->forknum != blk->forknum)
501  {
502  RelationOpenSmgr(rel);
503 
504  /*
505  * smgrexists is not safe for illegal forknum, hence check whether
506  * the passed forknum is valid before using it in smgrexists.
507  */
508  if (blk->forknum > InvalidForkNumber &&
509  blk->forknum <= MAX_FORKNUM &&
510  smgrexists(rel->rd_smgr, blk->forknum))
511  nblocks = RelationGetNumberOfBlocksInFork(rel, blk->forknum);
512  else
513  nblocks = 0;
514  }
515 
516  /* Check whether blocknum is valid and within fork file size. */
517  if (blk->blocknum >= nblocks)
518  {
519  /* Move to next forknum. */
520  old_blk = blk;
521  continue;
522  }
523 
524  /* Prewarm buffer. */
525  buf = ReadBufferExtended(rel, blk->forknum, blk->blocknum, RBM_NORMAL,
526  NULL);
527  if (BufferIsValid(buf))
528  {
530  ReleaseBuffer(buf);
531  }
532 
533  old_blk = blk;
534  }
535 
536  dsm_detach(seg);
537 
538  /* Release lock on previous relation. */
539  if (rel)
540  {
543  }
544 }
struct SMgrRelationData * rd_smgr
Definition: rel.h:57
void CommitTransactionCommand(void)
Definition: xact.c:2948
Buffer ReadBufferExtended(Relation reln, ForkNumber forkNum, BlockNumber blockNum, ReadBufferMode mode, BufferAccessStrategy strategy)
Definition: bufmgr.c:654
dsm_segment * dsm_attach(dsm_handle h)
Definition: dsm.c:631
Oid RelidByRelfilenode(Oid reltablespace, Oid relfilenode)
#define AccessShareLock
Definition: lockdefs.h:36
int errcode(int sqlerrcode)
Definition: elog.c:691
uint32 BlockNumber
Definition: block.h:31
void ReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3513
bool smgrexists(SMgrRelation reln, ForkNumber forknum)
Definition: smgr.c:248
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:706
Relation try_relation_open(Oid relationId, LOCKMODE lockmode)
Definition: relation.c:89
#define RelationOpenSmgr(relation)
Definition: rel.h:514
ForkNumber forknum
Definition: autoprewarm.c:65
#define ERROR
Definition: elog.h:43
static AutoPrewarmSharedState * apw_state
Definition: autoprewarm.c:100
static char * buf
Definition: pg_test_fsync.c:68
dsm_handle block_info_handle
Definition: autoprewarm.c:77
static bool apw_init_shmem(void)
Definition: autoprewarm.c:746
bool have_free_buffer(void)
Definition: freelist.c:180
void BackgroundWorkerInitializeConnectionByOid(Oid dboid, Oid useroid, uint32 flags)
Definition: postmaster.c:5718
#define InvalidOid
Definition: postgres_ext.h:36
#define ereport(elevel,...)
Definition: elog.h:155
pqsigfunc pqsignal(int signum, pqsigfunc handler)
Definition: signal.c:170
BlockNumber RelationGetNumberOfBlocksInFork(Relation relation, ForkNumber forkNum)
Definition: bufmgr.c:2850
void * dsm_segment_address(dsm_segment *seg)
Definition: dsm.c:1054
void relation_close(Relation relation, LOCKMODE lockmode)
Definition: relation.c:206
#define Assert(condition)
Definition: c.h:800
void StartTransactionCommand(void)
Definition: xact.c:2847
#define MAX_FORKNUM
Definition: relpath.h:55
#define BufferIsValid(bufnum)
Definition: bufmgr.h:123
BlockNumber blocknum
Definition: autoprewarm.c:66
void dsm_detach(dsm_segment *seg)
Definition: dsm.c:769
int errmsg(const char *fmt,...)
Definition: elog.c:902
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:99
int Buffer
Definition: buf.h:23
#define die(msg)
Definition: pg_test_fsync.c:97
void BackgroundWorkerUnblockSignals(void)
Definition: postmaster.c:5747

◆ autoprewarm_dump_now()

Datum autoprewarm_dump_now ( PG_FUNCTION_ARGS  )

Definition at line 725 of file autoprewarm.c.

References apw_detach_shmem(), apw_dump_now(), apw_init_shmem(), PG_END_ENSURE_ERROR_CLEANUP, PG_ENSURE_ERROR_CLEANUP, and PG_RETURN_INT64.

726 {
727  int num_blocks;
728 
729  apw_init_shmem();
730 
732  {
733  num_blocks = apw_dump_now(false, true);
734  }
736 
737  PG_RETURN_INT64((int64) num_blocks);
738 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:368
static int apw_dump_now(bool is_bgworker, bool dump_unlogged)
Definition: autoprewarm.c:553
#define PG_ENSURE_ERROR_CLEANUP(cleanup_function, arg)
Definition: ipc.h:47
static bool apw_init_shmem(void)
Definition: autoprewarm.c:746
#define PG_END_ENSURE_ERROR_CLEANUP(cleanup_function, arg)
Definition: ipc.h:52
static void apw_detach_shmem(int code, Datum arg)
Definition: autoprewarm.c:772

◆ autoprewarm_main()

void autoprewarm_main ( Datum  main_arg)

Definition at line 153 of file autoprewarm.c.

References apw_detach_shmem(), apw_dump_now(), apw_init_shmem(), apw_load_buffers(), autoprewarm_interval, BackgroundWorkerUnblockSignals(), AutoPrewarmSharedState::bgworker_pid, ConfigReloadPending, ereport, errmsg(), GetCurrentTimestamp(), InvalidPid, AutoPrewarmSharedState::lock, LOG, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), MyLatch, MyProcPid, on_shmem_exit(), PG_WAIT_EXTENSION, PGC_SIGHUP, pqsignal(), ProcessConfigFile(), procsignal_sigusr1_handler(), ResetLatch(), ShutdownRequestPending, SIGHUP, SignalHandlerForConfigReload(), SignalHandlerForShutdownRequest(), SIGUSR1, TimestampDifferenceMilliseconds(), TimestampTzPlusMilliseconds, WaitLatch(), WL_EXIT_ON_PM_DEATH, WL_LATCH_SET, and WL_TIMEOUT.

154 {
155  bool first_time = true;
156  TimestampTz last_dump_time = 0;
157 
158  /* Establish signal handlers; once that's done, unblock signals. */
163 
164  /* Create (if necessary) and attach to our shared memory area. */
165  if (apw_init_shmem())
166  first_time = false;
167 
168  /* Set on-detach hook so that our PID will be cleared on exit. */
170 
171  /*
172  * Store our PID in the shared memory area --- unless there's already
173  * another worker running, in which case just exit.
174  */
177  {
179  ereport(LOG,
180  (errmsg("autoprewarm worker is already running under PID %lu",
181  (unsigned long) apw_state->bgworker_pid)));
182  return;
183  }
186 
187  /*
188  * Preload buffers from the dump file only if we just created the shared
189  * memory region. Otherwise, it's either already been done or shouldn't
190  * be done - e.g. because the old dump file has been overwritten since the
191  * server was started.
192  *
193  * There's not much point in performing a dump immediately after we finish
194  * preloading; so, if we do end up preloading, consider the last dump time
195  * to be equal to the current time.
196  */
197  if (first_time)
198  {
200  last_dump_time = GetCurrentTimestamp();
201  }
202 
203  /* Periodically dump buffers until terminated. */
204  while (!ShutdownRequestPending)
205  {
206  /* In case of a SIGHUP, just reload the configuration. */
208  {
209  ConfigReloadPending = false;
211  }
212 
213  if (autoprewarm_interval <= 0)
214  {
215  /* We're only dumping at shutdown, so just wait forever. */
216  (void) WaitLatch(MyLatch,
218  -1L,
220  }
221  else
222  {
223  TimestampTz next_dump_time;
224  long delay_in_ms;
225 
226  /* Compute the next dump time. */
227  next_dump_time =
228  TimestampTzPlusMilliseconds(last_dump_time,
229  autoprewarm_interval * 1000);
230  delay_in_ms =
232  next_dump_time);
233 
234  /* Perform a dump if it's time. */
235  if (delay_in_ms <= 0)
236  {
237  last_dump_time = GetCurrentTimestamp();
238  apw_dump_now(true, false);
239  continue;
240  }
241 
242  /* Sleep until the next dump time. */
243  (void) WaitLatch(MyLatch,
245  delay_in_ms,
247  }
248 
249  /* Reset the latch, loop. */
251  }
252 
253  /*
254  * Dump one last time. We assume this is probably the result of a system
255  * shutdown, although it's possible that we've merely been terminated.
256  */
257  apw_dump_now(true, true);
258 }
int MyProcPid
Definition: globals.c:40
#define WL_TIMEOUT
Definition: latch.h:127
void ProcessConfigFile(GucContext context)
#define SIGUSR1
Definition: win32_port.h:171
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1578
int64 TimestampTz
Definition: timestamp.h:39
void SignalHandlerForConfigReload(SIGNAL_ARGS)
Definition: interrupt.c:56
static int autoprewarm_interval
Definition: autoprewarm.c:104
#define LOG
Definition: elog.h:26
static int apw_dump_now(bool is_bgworker, bool dump_unlogged)
Definition: autoprewarm.c:553
void ResetLatch(Latch *latch)
Definition: latch.c:588
int WaitLatch(Latch *latch, int wakeEvents, long timeout, uint32 wait_event_info)
Definition: latch.c:390
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1811
static AutoPrewarmSharedState * apw_state
Definition: autoprewarm.c:100
void on_shmem_exit(pg_on_exit_callback function, Datum arg)
Definition: ipc.c:361
#define SIGHUP
Definition: win32_port.h:159
static bool apw_init_shmem(void)
Definition: autoprewarm.c:746
volatile sig_atomic_t ShutdownRequestPending
Definition: interrupt.c:27
Definition: guc.h:72
void SignalHandlerForShutdownRequest(SIGNAL_ARGS)
Definition: interrupt.c:104
#define PG_WAIT_EXTENSION
Definition: pgstat.h:866
#define TimestampTzPlusMilliseconds(tz, ms)
Definition: timestamp.h:56
#define ereport(elevel,...)
Definition: elog.h:155
pqsigfunc pqsignal(int signum, pqsigfunc handler)
Definition: signal.c:170
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1207
int errmsg(const char *fmt,...)
Definition: elog.c:902
volatile sig_atomic_t ConfigReloadPending
Definition: interrupt.c:26
static void apw_detach_shmem(int code, Datum arg)
Definition: autoprewarm.c:772
static void apw_load_buffers(void)
Definition: autoprewarm.c:265
struct Latch * MyLatch
Definition: globals.c:54
void procsignal_sigusr1_handler(SIGNAL_ARGS)
Definition: procsignal.c:551
#define WL_LATCH_SET
Definition: latch.h:124
long TimestampDifferenceMilliseconds(TimestampTz start_time, TimestampTz stop_time)
Definition: timestamp.c:1691
#define WL_EXIT_ON_PM_DEATH
Definition: latch.h:129
#define InvalidPid
Definition: miscadmin.h:32
void BackgroundWorkerUnblockSignals(void)
Definition: postmaster.c:5747

◆ autoprewarm_start_worker()

Datum autoprewarm_start_worker ( PG_FUNCTION_ARGS  )

Definition at line 693 of file autoprewarm.c.

References apw_init_shmem(), apw_start_leader_worker(), autoprewarm, AutoPrewarmSharedState::bgworker_pid, ereport, errcode(), errmsg(), ERROR, InvalidPid, AutoPrewarmSharedState::lock, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), and PG_RETURN_VOID.

694 {
695  pid_t pid;
696 
697  if (!autoprewarm)
698  ereport(ERROR,
699  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
700  errmsg("autoprewarm is disabled")));
701 
702  apw_init_shmem();
704  pid = apw_state->bgworker_pid;
706 
707  if (pid != InvalidPid)
708  ereport(ERROR,
709  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
710  errmsg("autoprewarm worker is already running under PID %lu",
711  (unsigned long) pid)));
712 
714 
715  PG_RETURN_VOID();
716 }
int errcode(int sqlerrcode)
Definition: elog.c:691
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1811
#define ERROR
Definition: elog.h:43
static AutoPrewarmSharedState * apw_state
Definition: autoprewarm.c:100
static void apw_start_leader_worker(void)
Definition: autoprewarm.c:786
static bool apw_init_shmem(void)
Definition: autoprewarm.c:746
#define ereport(elevel,...)
Definition: elog.h:155
#define PG_RETURN_VOID()
Definition: fmgr.h:349
static bool autoprewarm
Definition: autoprewarm.c:103
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1207
int errmsg(const char *fmt,...)
Definition: elog.c:902
#define InvalidPid
Definition: miscadmin.h:32

◆ PG_FUNCTION_INFO_V1() [1/2]

PG_FUNCTION_INFO_V1 ( autoprewarm_start_worker  )

◆ PG_FUNCTION_INFO_V1() [2/2]

PG_FUNCTION_INFO_V1 ( autoprewarm_dump_now  )

Variable Documentation

◆ apw_state

AutoPrewarmSharedState* apw_state = NULL
static

Definition at line 100 of file autoprewarm.c.

◆ autoprewarm

bool autoprewarm = true
static

Definition at line 103 of file autoprewarm.c.

Referenced by _PG_init(), and autoprewarm_start_worker().

◆ autoprewarm_interval

int autoprewarm_interval
static

Definition at line 104 of file autoprewarm.c.

Referenced by _PG_init(), and autoprewarm_main().