PostgreSQL Source Code  git master
autoprewarm.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * autoprewarm.c
4  * Periodically dump information about the blocks present in
5  * shared_buffers, and reload them on server restart.
6  *
7  * Due to locking considerations, we can't actually begin prewarming
8  * until the server reaches a consistent state. We need the catalogs
9  * to be consistent so that we can figure out which relation to lock,
10  * and we need to lock the relations so that we don't try to prewarm
11  * pages from a relation that is in the process of being dropped.
12  *
13  * While prewarming, autoprewarm will use two workers. There's a
14  * leader worker that reads and sorts the list of blocks to be
15  * prewarmed and then launches a per-database worker for each
16  * relevant database in turn. The former keeps running after the
17  * initial prewarm is complete to update the dump file periodically.
18  *
19  * Copyright (c) 2016-2020, PostgreSQL Global Development Group
20  *
21  * IDENTIFICATION
22  * contrib/pg_prewarm/autoprewarm.c
23  *
24  *-------------------------------------------------------------------------
25  */
26 
27 #include "postgres.h"
28 
29 #include <unistd.h>
30 
31 #include "access/relation.h"
32 #include "access/xact.h"
33 #include "catalog/pg_class.h"
34 #include "catalog/pg_type.h"
35 #include "miscadmin.h"
36 #include "pgstat.h"
37 #include "postmaster/bgworker.h"
38 #include "storage/buf_internals.h"
39 #include "storage/dsm.h"
40 #include "storage/ipc.h"
41 #include "storage/latch.h"
42 #include "storage/lwlock.h"
43 #include "storage/proc.h"
44 #include "storage/procsignal.h"
45 #include "storage/shmem.h"
46 #include "storage/smgr.h"
47 #include "tcop/tcopprot.h"
48 #include "utils/acl.h"
49 #include "utils/datetime.h"
50 #include "utils/guc.h"
51 #include "utils/memutils.h"
52 #include "utils/rel.h"
53 #include "utils/relfilenodemap.h"
54 #include "utils/resowner.h"
55 
56 #define AUTOPREWARM_FILE "autoprewarm.blocks"
57 
58 /* Metadata for each block we dump. */
59 typedef struct BlockInfoRecord
60 {
67 
68 /* Shared state information for autoprewarm bgworker. */
69 typedef struct AutoPrewarmSharedState
70 {
71  LWLock lock; /* mutual exclusion */
72  pid_t bgworker_pid; /* for main bgworker */
73  pid_t pid_using_dumpfile; /* for autoprewarm or block dump */
74 
75  /* Following items are for communication with per-database worker */
82 
83 void _PG_init(void);
84 void autoprewarm_main(Datum main_arg);
85 void autoprewarm_database_main(Datum main_arg);
86 
89 
90 static void apw_load_buffers(void);
91 static int apw_dump_now(bool is_bgworker, bool dump_unlogged);
92 static void apw_start_leader_worker(void);
93 static void apw_start_database_worker(void);
94 static bool apw_init_shmem(void);
95 static void apw_detach_shmem(int code, Datum arg);
96 static int apw_compare_blockinfo(const void *p, const void *q);
97 static void apw_sigterm_handler(SIGNAL_ARGS);
98 static void apw_sighup_handler(SIGNAL_ARGS);
99 
100 /* Flags set by signal handlers */
101 static volatile sig_atomic_t got_sigterm = false;
102 static volatile sig_atomic_t got_sighup = false;
103 
104 /* Pointer to shared-memory state. */
106 
107 /* GUC variables. */
108 static bool autoprewarm = true; /* start worker? */
109 static int autoprewarm_interval; /* dump interval */
110 
111 /*
112  * Module load callback.
113  */
114 void
115 _PG_init(void)
116 {
117  DefineCustomIntVariable("pg_prewarm.autoprewarm_interval",
118  "Sets the interval between dumps of shared buffers",
119  "If set to zero, time-based dumping is disabled.",
121  300,
122  0, INT_MAX / 1000,
123  PGC_SIGHUP,
124  GUC_UNIT_S,
125  NULL,
126  NULL,
127  NULL);
128 
130  return;
131 
132  /* can't define PGC_POSTMASTER variable after startup */
133  DefineCustomBoolVariable("pg_prewarm.autoprewarm",
134  "Starts the autoprewarm worker.",
135  NULL,
136  &autoprewarm,
137  true,
139  0,
140  NULL,
141  NULL,
142  NULL);
143 
144  EmitWarningsOnPlaceholders("pg_prewarm");
145 
147 
148  /* Register autoprewarm worker, if enabled. */
149  if (autoprewarm)
151 }
152 
153 /*
154  * Main entry point for the leader autoprewarm process. Per-database workers
155  * have a separate entry point.
156  */
157 void
159 {
160  bool first_time = true;
161  TimestampTz last_dump_time = 0;
162 
163  /* Establish signal handlers; once that's done, unblock signals. */
164  pqsignal(SIGTERM, apw_sigterm_handler);
168 
169  /* Create (if necessary) and attach to our shared memory area. */
170  if (apw_init_shmem())
171  first_time = false;
172 
173  /* Set on-detach hook so that our PID will be cleared on exit. */
175 
176  /*
177  * Store our PID in the shared memory area --- unless there's already
178  * another worker running, in which case just exit.
179  */
180  LWLockAcquire(&apw_state->lock, LW_EXCLUSIVE);
181  if (apw_state->bgworker_pid != InvalidPid)
182  {
183  LWLockRelease(&apw_state->lock);
184  ereport(LOG,
185  (errmsg("autoprewarm worker is already running under PID %lu",
186  (unsigned long) apw_state->bgworker_pid)));
187  return;
188  }
189  apw_state->bgworker_pid = MyProcPid;
190  LWLockRelease(&apw_state->lock);
191 
192  /*
193  * Preload buffers from the dump file only if we just created the shared
194  * memory region. Otherwise, it's either already been done or shouldn't
195  * be done - e.g. because the old dump file has been overwritten since the
196  * server was started.
197  *
198  * There's not much point in performing a dump immediately after we finish
199  * preloading; so, if we do end up preloading, consider the last dump time
200  * to be equal to the current time.
201  */
202  if (first_time)
203  {
205  last_dump_time = GetCurrentTimestamp();
206  }
207 
208  /* Periodically dump buffers until terminated. */
209  while (!got_sigterm)
210  {
211  /* In case of a SIGHUP, just reload the configuration. */
212  if (got_sighup)
213  {
214  got_sighup = false;
216  }
217 
218  if (autoprewarm_interval <= 0)
219  {
220  /* We're only dumping at shutdown, so just wait forever. */
221  (void) WaitLatch(&MyProc->procLatch,
223  -1L,
225  }
226  else
227  {
228  long delay_in_ms = 0;
229  TimestampTz next_dump_time = 0;
230  long secs = 0;
231  int usecs = 0;
232 
233  /* Compute the next dump time. */
234  next_dump_time =
235  TimestampTzPlusMilliseconds(last_dump_time,
236  autoprewarm_interval * 1000);
237  TimestampDifference(GetCurrentTimestamp(), next_dump_time,
238  &secs, &usecs);
239  delay_in_ms = secs + (usecs / 1000);
240 
241  /* Perform a dump if it's time. */
242  if (delay_in_ms <= 0)
243  {
244  last_dump_time = GetCurrentTimestamp();
245  apw_dump_now(true, false);
246  continue;
247  }
248 
249  /* Sleep until the next dump time. */
250  (void) WaitLatch(&MyProc->procLatch,
252  delay_in_ms,
254  }
255 
256  /* Reset the latch, loop. */
258  }
259 
260  /*
261  * Dump one last time. We assume this is probably the result of a system
262  * shutdown, although it's possible that we've merely been terminated.
263  */
264  apw_dump_now(true, true);
265 }
266 
267 /*
268  * Read the dump file and launch per-database workers one at a time to
269  * prewarm the buffers found there.
270  */
271 static void
273 {
274  FILE *file = NULL;
275  int num_elements,
276  i;
277  BlockInfoRecord *blkinfo;
278  dsm_segment *seg;
279 
280  /*
281  * Skip the prewarm if the dump file is in use; otherwise, prevent any
282  * other process from writing it while we're using it.
283  */
284  LWLockAcquire(&apw_state->lock, LW_EXCLUSIVE);
285  if (apw_state->pid_using_dumpfile == InvalidPid)
286  apw_state->pid_using_dumpfile = MyProcPid;
287  else
288  {
289  LWLockRelease(&apw_state->lock);
290  ereport(LOG,
291  (errmsg("skipping prewarm because block dump file is being written by PID %lu",
292  (unsigned long) apw_state->pid_using_dumpfile)));
293  return;
294  }
295  LWLockRelease(&apw_state->lock);
296 
297  /*
298  * Open the block dump file. Exit quietly if it doesn't exist, but report
299  * any other error.
300  */
301  file = AllocateFile(AUTOPREWARM_FILE, "r");
302  if (!file)
303  {
304  if (errno == ENOENT)
305  {
306  LWLockAcquire(&apw_state->lock, LW_EXCLUSIVE);
307  apw_state->pid_using_dumpfile = InvalidPid;
308  LWLockRelease(&apw_state->lock);
309  return; /* No file to load. */
310  }
311  ereport(ERROR,
313  errmsg("could not read file \"%s\": %m",
314  AUTOPREWARM_FILE)));
315  }
316 
317  /* First line of the file is a record count. */
318  if (fscanf(file, "<<%d>>\n", &num_elements) != 1)
319  ereport(ERROR,
321  errmsg("could not read from file \"%s\": %m",
322  AUTOPREWARM_FILE)));
323 
324  /* Allocate a dynamic shared memory segment to store the record data. */
325  seg = dsm_create(sizeof(BlockInfoRecord) * num_elements, 0);
326  blkinfo = (BlockInfoRecord *) dsm_segment_address(seg);
327 
328  /* Read records, one per line. */
329  for (i = 0; i < num_elements; i++)
330  {
331  unsigned forknum;
332 
333  if (fscanf(file, "%u,%u,%u,%u,%u\n", &blkinfo[i].database,
334  &blkinfo[i].tablespace, &blkinfo[i].filenode,
335  &forknum, &blkinfo[i].blocknum) != 5)
336  ereport(ERROR,
337  (errmsg("autoprewarm block dump file is corrupted at line %d",
338  i + 1)));
339  blkinfo[i].forknum = forknum;
340  }
341 
342  FreeFile(file);
343 
344  /* Sort the blocks to be loaded. */
345  pg_qsort(blkinfo, num_elements, sizeof(BlockInfoRecord),
347 
348  /* Populate shared memory state. */
349  apw_state->block_info_handle = dsm_segment_handle(seg);
350  apw_state->prewarm_start_idx = apw_state->prewarm_stop_idx = 0;
351  apw_state->prewarmed_blocks = 0;
352 
353  /* Get the info position of the first block of the next database. */
354  while (apw_state->prewarm_start_idx < num_elements)
355  {
356  int j = apw_state->prewarm_start_idx;
357  Oid current_db = blkinfo[j].database;
358 
359  /*
360  * Advance the prewarm_stop_idx to the first BlockInfoRecord that does
361  * not belong to this database.
362  */
363  j++;
364  while (j < num_elements)
365  {
366  if (current_db != blkinfo[j].database)
367  {
368  /*
369  * Combine BlockInfoRecords for global objects with those of
370  * the database.
371  */
372  if (current_db != InvalidOid)
373  break;
374  current_db = blkinfo[j].database;
375  }
376 
377  j++;
378  }
379 
380  /*
381  * If we reach this point with current_db == InvalidOid, then only
382  * BlockInfoRecords belonging to global objects exist. We can't
383  * prewarm without a database connection, so just bail out.
384  */
385  if (current_db == InvalidOid)
386  break;
387 
388  /* Configure stop point and database for next per-database worker. */
389  apw_state->prewarm_stop_idx = j;
390  apw_state->database = current_db;
391  Assert(apw_state->prewarm_start_idx < apw_state->prewarm_stop_idx);
392 
393  /* If we've run out of free buffers, don't launch another worker. */
394  if (!have_free_buffer())
395  break;
396 
397  /*
398  * Start a per-database worker to load blocks for this database; this
399  * function will return once the per-database worker exits.
400  */
402 
403  /* Prepare for next database. */
404  apw_state->prewarm_start_idx = apw_state->prewarm_stop_idx;
405  }
406 
407  /* Clean up. */
408  dsm_detach(seg);
409  LWLockAcquire(&apw_state->lock, LW_EXCLUSIVE);
411  apw_state->pid_using_dumpfile = InvalidPid;
412  LWLockRelease(&apw_state->lock);
413 
414  /* Report our success. */
415  ereport(LOG,
416  (errmsg("autoprewarm successfully prewarmed %d of %d previously-loaded blocks",
417  apw_state->prewarmed_blocks, num_elements)));
418 }
419 
420 /*
421  * Prewarm all blocks for one database (and possibly also global objects, if
422  * those got grouped with this database).
423  */
424 void
426 {
427  int pos;
428  BlockInfoRecord *block_info;
429  Relation rel = NULL;
430  BlockNumber nblocks = 0;
431  BlockInfoRecord *old_blk = NULL;
432  dsm_segment *seg;
433 
434  /* Establish signal handlers; once that's done, unblock signals. */
435  pqsignal(SIGTERM, die);
437 
438  /* Connect to correct database and get block information. */
439  apw_init_shmem();
440  seg = dsm_attach(apw_state->block_info_handle);
441  if (seg == NULL)
442  ereport(ERROR,
443  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
444  errmsg("could not map dynamic shared memory segment")));
446  block_info = (BlockInfoRecord *) dsm_segment_address(seg);
447  pos = apw_state->prewarm_start_idx;
448 
449  /*
450  * Loop until we run out of blocks to prewarm or until we run out of free
451  * buffers.
452  */
453  while (pos < apw_state->prewarm_stop_idx && have_free_buffer())
454  {
455  BlockInfoRecord *blk = &block_info[pos++];
456  Buffer buf;
457 
459 
460  /*
461  * Quit if we've reached records for another database. If previous
462  * blocks are of some global objects, then continue pre-warming.
463  */
464  if (old_blk != NULL && old_blk->database != blk->database &&
465  old_blk->database != 0)
466  break;
467 
468  /*
469  * As soon as we encounter a block of a new relation, close the old
470  * relation. Note that rel will be NULL if try_relation_open failed
471  * previously; in that case, there is nothing to close.
472  */
473  if (old_blk != NULL && old_blk->filenode != blk->filenode &&
474  rel != NULL)
475  {
477  rel = NULL;
479  }
480 
481  /*
482  * Try to open each new relation, but only once, when we first
483  * encounter it. If it's been dropped, skip the associated blocks.
484  */
485  if (old_blk == NULL || old_blk->filenode != blk->filenode)
486  {
487  Oid reloid;
488 
489  Assert(rel == NULL);
491  reloid = RelidByRelfilenode(blk->tablespace, blk->filenode);
492  if (OidIsValid(reloid))
493  rel = try_relation_open(reloid, AccessShareLock);
494 
495  if (!rel)
497  }
498  if (!rel)
499  {
500  old_blk = blk;
501  continue;
502  }
503 
504  /* Once per fork, check for fork existence and size. */
505  if (old_blk == NULL ||
506  old_blk->filenode != blk->filenode ||
507  old_blk->forknum != blk->forknum)
508  {
509  RelationOpenSmgr(rel);
510 
511  /*
512  * smgrexists is not safe for illegal forknum, hence check whether
513  * the passed forknum is valid before using it in smgrexists.
514  */
515  if (blk->forknum > InvalidForkNumber &&
516  blk->forknum <= MAX_FORKNUM &&
517  smgrexists(rel->rd_smgr, blk->forknum))
518  nblocks = RelationGetNumberOfBlocksInFork(rel, blk->forknum);
519  else
520  nblocks = 0;
521  }
522 
523  /* Check whether blocknum is valid and within fork file size. */
524  if (blk->blocknum >= nblocks)
525  {
526  /* Move to next forknum. */
527  old_blk = blk;
528  continue;
529  }
530 
531  /* Prewarm buffer. */
532  buf = ReadBufferExtended(rel, blk->forknum, blk->blocknum, RBM_NORMAL,
533  NULL);
534  if (BufferIsValid(buf))
535  {
536  apw_state->prewarmed_blocks++;
537  ReleaseBuffer(buf);
538  }
539 
540  old_blk = blk;
541  }
542 
543  dsm_detach(seg);
544 
545  /* Release lock on previous relation. */
546  if (rel)
547  {
550  }
551 }
552 
553 /*
554  * Dump information on blocks in shared buffers. We use a text format here
555  * so that it's easy to understand and even change the file contents if
556  * necessary.
557  * Returns the number of blocks dumped.
558  */
559 static int
560 apw_dump_now(bool is_bgworker, bool dump_unlogged)
561 {
562  int num_blocks;
563  int i;
564  int ret;
565  BlockInfoRecord *block_info_array;
566  BufferDesc *bufHdr;
567  FILE *file;
568  char transient_dump_file_path[MAXPGPATH];
569  pid_t pid;
570 
571  LWLockAcquire(&apw_state->lock, LW_EXCLUSIVE);
572  pid = apw_state->pid_using_dumpfile;
573  if (apw_state->pid_using_dumpfile == InvalidPid)
574  apw_state->pid_using_dumpfile = MyProcPid;
575  LWLockRelease(&apw_state->lock);
576 
577  if (pid != InvalidPid)
578  {
579  if (!is_bgworker)
580  ereport(ERROR,
581  (errmsg("could not perform block dump because dump file is being used by PID %lu",
582  (unsigned long) apw_state->pid_using_dumpfile)));
583 
584  ereport(LOG,
585  (errmsg("skipping block dump because it is already being performed by PID %lu",
586  (unsigned long) apw_state->pid_using_dumpfile)));
587  return 0;
588  }
589 
590  block_info_array =
592 
593  for (num_blocks = 0, i = 0; i < NBuffers; i++)
594  {
595  uint32 buf_state;
596 
598 
599  bufHdr = GetBufferDescriptor(i);
600 
601  /* Lock each buffer header before inspecting. */
602  buf_state = LockBufHdr(bufHdr);
603 
604  /*
605  * Unlogged tables will be automatically truncated after a crash or
606  * unclean shutdown. In such cases we need not prewarm them. Dump them
607  * only if requested by caller.
608  */
609  if (buf_state & BM_TAG_VALID &&
610  ((buf_state & BM_PERMANENT) || dump_unlogged))
611  {
612  block_info_array[num_blocks].database = bufHdr->tag.rnode.dbNode;
613  block_info_array[num_blocks].tablespace = bufHdr->tag.rnode.spcNode;
614  block_info_array[num_blocks].filenode = bufHdr->tag.rnode.relNode;
615  block_info_array[num_blocks].forknum = bufHdr->tag.forkNum;
616  block_info_array[num_blocks].blocknum = bufHdr->tag.blockNum;
617  ++num_blocks;
618  }
619 
620  UnlockBufHdr(bufHdr, buf_state);
621  }
622 
623  snprintf(transient_dump_file_path, MAXPGPATH, "%s.tmp", AUTOPREWARM_FILE);
624  file = AllocateFile(transient_dump_file_path, "w");
625  if (!file)
626  ereport(ERROR,
628  errmsg("could not open file \"%s\": %m",
629  transient_dump_file_path)));
630 
631  ret = fprintf(file, "<<%d>>\n", num_blocks);
632  if (ret < 0)
633  {
634  int save_errno = errno;
635 
636  FreeFile(file);
637  unlink(transient_dump_file_path);
638  errno = save_errno;
639  ereport(ERROR,
641  errmsg("could not write to file \"%s\": %m",
642  transient_dump_file_path)));
643  }
644 
645  for (i = 0; i < num_blocks; i++)
646  {
648 
649  ret = fprintf(file, "%u,%u,%u,%u,%u\n",
650  block_info_array[i].database,
651  block_info_array[i].tablespace,
652  block_info_array[i].filenode,
653  (uint32) block_info_array[i].forknum,
654  block_info_array[i].blocknum);
655  if (ret < 0)
656  {
657  int save_errno = errno;
658 
659  FreeFile(file);
660  unlink(transient_dump_file_path);
661  errno = save_errno;
662  ereport(ERROR,
664  errmsg("could not write to file \"%s\": %m",
665  transient_dump_file_path)));
666  }
667  }
668 
669  pfree(block_info_array);
670 
671  /*
672  * Rename transient_dump_file_path to AUTOPREWARM_FILE to make things
673  * permanent.
674  */
675  ret = FreeFile(file);
676  if (ret != 0)
677  {
678  int save_errno = errno;
679 
680  unlink(transient_dump_file_path);
681  errno = save_errno;
682  ereport(ERROR,
684  errmsg("could not close file \"%s\": %m",
685  transient_dump_file_path)));
686  }
687 
688  (void) durable_rename(transient_dump_file_path, AUTOPREWARM_FILE, ERROR);
689  apw_state->pid_using_dumpfile = InvalidPid;
690 
691  ereport(DEBUG1,
692  (errmsg("wrote block details for %d blocks", num_blocks)));
693  return num_blocks;
694 }
695 
696 /*
697  * SQL-callable function to launch autoprewarm.
698  */
699 Datum
701 {
702  pid_t pid;
703 
704  if (!autoprewarm)
705  ereport(ERROR,
706  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
707  errmsg("autoprewarm is disabled")));
708 
709  apw_init_shmem();
710  LWLockAcquire(&apw_state->lock, LW_EXCLUSIVE);
711  pid = apw_state->bgworker_pid;
712  LWLockRelease(&apw_state->lock);
713 
714  if (pid != InvalidPid)
715  ereport(ERROR,
716  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
717  errmsg("autoprewarm worker is already running under PID %lu",
718  (unsigned long) pid)));
719 
721 
722  PG_RETURN_VOID();
723 }
724 
725 /*
726  * SQL-callable function to perform an immediate block dump.
727  *
728  * Note: this is declared to return int8, as insurance against some
729  * very distant day when we might make NBuffers wider than int.
730  */
731 Datum
733 {
734  int num_blocks;
735 
736  apw_init_shmem();
737 
739  {
740  num_blocks = apw_dump_now(false, true);
741  }
743 
744  PG_RETURN_INT64((int64) num_blocks);
745 }
746 
747 /*
748  * Allocate and initialize autoprewarm related shared memory, if not already
749  * done, and set up backend-local pointer to that state. Returns true if an
750  * existing shared memory segment was found.
751  */
752 static bool
754 {
755  bool found;
756 
757  LWLockAcquire(AddinShmemInitLock, LW_EXCLUSIVE);
758  apw_state = ShmemInitStruct("autoprewarm",
759  sizeof(AutoPrewarmSharedState),
760  &found);
761  if (!found)
762  {
763  /* First time through ... */
764  LWLockInitialize(&apw_state->lock, LWLockNewTrancheId());
765  apw_state->bgworker_pid = InvalidPid;
766  apw_state->pid_using_dumpfile = InvalidPid;
767  }
768  LWLockRelease(AddinShmemInitLock);
769 
770  LWLockRegisterTranche(apw_state->lock.tranche, "autoprewarm");
771 
772  return found;
773 }
774 
775 /*
776  * Clear our PID from autoprewarm shared state.
777  */
778 static void
780 {
781  LWLockAcquire(&apw_state->lock, LW_EXCLUSIVE);
782  if (apw_state->pid_using_dumpfile == MyProcPid)
783  apw_state->pid_using_dumpfile = InvalidPid;
784  if (apw_state->bgworker_pid == MyProcPid)
785  apw_state->bgworker_pid = InvalidPid;
786  LWLockRelease(&apw_state->lock);
787 }
788 
789 /*
790  * Start autoprewarm leader worker process.
791  */
792 static void
794 {
795  BackgroundWorker worker;
796  BackgroundWorkerHandle *handle;
798  pid_t pid;
799 
800  MemSet(&worker, 0, sizeof(BackgroundWorker));
803  strcpy(worker.bgw_library_name, "pg_prewarm");
804  strcpy(worker.bgw_function_name, "autoprewarm_main");
805  strcpy(worker.bgw_name, "autoprewarm leader");
806  strcpy(worker.bgw_type, "autoprewarm leader");
807 
809  {
810  RegisterBackgroundWorker(&worker);
811  return;
812  }
813 
814  /* must set notify PID to wait for startup */
815  worker.bgw_notify_pid = MyProcPid;
816 
817  if (!RegisterDynamicBackgroundWorker(&worker, &handle))
818  ereport(ERROR,
819  (errcode(ERRCODE_INSUFFICIENT_RESOURCES),
820  errmsg("could not register background process"),
821  errhint("You may need to increase max_worker_processes.")));
822 
823  status = WaitForBackgroundWorkerStartup(handle, &pid);
824  if (status != BGWH_STARTED)
825  ereport(ERROR,
826  (errcode(ERRCODE_INSUFFICIENT_RESOURCES),
827  errmsg("could not start background process"),
828  errhint("More details may be available in the server log.")));
829 }
830 
831 /*
832  * Start autoprewarm per-database worker process.
833  */
834 static void
836 {
837  BackgroundWorker worker;
838  BackgroundWorkerHandle *handle;
839 
840  MemSet(&worker, 0, sizeof(BackgroundWorker));
841  worker.bgw_flags =
845  strcpy(worker.bgw_library_name, "pg_prewarm");
846  strcpy(worker.bgw_function_name, "autoprewarm_database_main");
847  strcpy(worker.bgw_name, "autoprewarm worker");
848  strcpy(worker.bgw_type, "autoprewarm worker");
849 
850  /* must set notify PID to wait for shutdown */
851  worker.bgw_notify_pid = MyProcPid;
852 
853  if (!RegisterDynamicBackgroundWorker(&worker, &handle))
854  ereport(ERROR,
855  (errcode(ERRCODE_INSUFFICIENT_RESOURCES),
856  errmsg("registering dynamic bgworker autoprewarm failed"),
857  errhint("Consider increasing configuration parameter \"max_worker_processes\".")));
858 
859  /*
860  * Ignore return value; if it fails, postmaster has died, but we have
861  * checks for that elsewhere.
862  */
864 }
865 
866 /* Compare member elements to check whether they are not equal. */
867 #define cmp_member_elem(fld) \
868 do { \
869  if (a->fld < b->fld) \
870  return -1; \
871  else if (a->fld > b->fld) \
872  return 1; \
873 } while(0)
874 
875 /*
876  * apw_compare_blockinfo
877  *
878  * We depend on all records for a particular database being consecutive
879  * in the dump file; each per-database worker will preload blocks until
880  * it sees a block for some other database. Sorting by tablespace,
881  * filenode, forknum, and blocknum isn't critical for correctness, but
882  * helps us get a sequential I/O pattern.
883  */
884 static int
885 apw_compare_blockinfo(const void *p, const void *q)
886 {
887  const BlockInfoRecord *a = (const BlockInfoRecord *) p;
888  const BlockInfoRecord *b = (const BlockInfoRecord *) q;
889 
895 
896  return 0;
897 }
898 
899 /*
900  * Signal handler for SIGTERM
901  */
902 static void
904 {
905  int save_errno = errno;
906 
907  got_sigterm = true;
908 
909  if (MyProc)
911 
912  errno = save_errno;
913 }
914 
915 /*
916  * Signal handler for SIGHUP
917  */
918 static void
920 {
921  int save_errno = errno;
922 
923  got_sighup = true;
924 
925  if (MyProc)
927 
928  errno = save_errno;
929 }
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:8854
static void apw_sigterm_handler(SIGNAL_ARGS)
Definition: autoprewarm.c:903
BgwHandleStatus WaitForBackgroundWorkerStartup(BackgroundWorkerHandle *handle, pid_t *pidp)
Definition: bgworker.c:1082
Definition: lwlock.h:31
void _PG_init(void)
Definition: autoprewarm.c:115
void RegisterBackgroundWorker(BackgroundWorker *worker)
Definition: bgworker.c:834
#define BM_PERMANENT
Definition: buf_internals.h:66
void RequestAddinShmemSpace(Size size)
Definition: ipci.c:71
#define DEBUG1
Definition: elog.h:25
int MyProcPid
Definition: globals.c:40
int errhint(const char *fmt,...)
Definition: elog.c:1068
#define BM_TAG_VALID
Definition: buf_internals.h:60
int LWLockNewTrancheId(void)
Definition: lwlock.c:629
void autoprewarm_main(Datum main_arg)
Definition: autoprewarm.c:158
static volatile sig_atomic_t got_sigterm
Definition: autoprewarm.c:101
ForkNumber forkNum
Definition: buf_internals.h:93
#define WL_TIMEOUT
Definition: latch.h:127
void ProcessConfigFile(GucContext context)
uint32 dsm_handle
Definition: dsm_impl.h:55
bool process_shared_preload_libraries_in_progress
Definition: miscinit.c:1596
#define SIGUSR1
Definition: win32_port.h:171
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1578
#define PG_RETURN_INT64(x)
Definition: fmgr.h:366
PGPROC * MyProc
Definition: proc.c:67
int64 TimestampTz
Definition: timestamp.h:39
struct SMgrRelationData * rd_smgr
Definition: rel.h:57
static int autoprewarm_interval
Definition: autoprewarm.c:109
void CommitTransactionCommand(void)
Definition: xact.c:2947
Buffer ReadBufferExtended(Relation reln, ForkNumber forkNum, BlockNumber blockNum, ReadBufferMode mode, BufferAccessStrategy strategy)
Definition: bufmgr.c:653
dsm_segment * dsm_attach(dsm_handle h)
Definition: dsm.c:631
Oid RelidByRelfilenode(Oid reltablespace, Oid relfilenode)
#define AccessShareLock
Definition: lockdefs.h:36
#define cmp_member_elem(fld)
Definition: autoprewarm.c:867
dsm_handle dsm_segment_handle(dsm_segment *seg)
Definition: dsm.c:1082
int bgw_restart_time
Definition: bgworker.h:94
int errcode(int sqlerrcode)
Definition: elog.c:610
#define MemSet(start, val, len)
Definition: c.h:950
uint32 BlockNumber
Definition: block.h:31
void ReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3511
bool smgrexists(SMgrRelation reln, ForkNumber forknum)
Definition: smgr.c:248
#define LOG
Definition: elog.h:26
unsigned int Oid
Definition: postgres_ext.h:31
void SetLatch(Latch *latch)
Definition: latch.c:505
#define BGWORKER_SHMEM_ACCESS
Definition: bgworker.h:52
#define fprintf
Definition: port.h:219
#define OidIsValid(objectId)
Definition: c.h:652
static int apw_dump_now(bool is_bgworker, bool dump_unlogged)
Definition: autoprewarm.c:560
char bgw_function_name[BGW_MAXLEN]
Definition: bgworker.h:96
Relation try_relation_open(Oid relationId, LOCKMODE lockmode)
Definition: relation.c:89
void ResetLatch(Latch *latch)
Definition: latch.c:588
Latch procLatch
Definition: proc.h:121
int WaitLatch(Latch *latch, int wakeEvents, long timeout, uint32 wait_event_info)
Definition: latch.c:390
void LWLockRegisterTranche(int tranche_id, const char *tranche_name)
Definition: lwlock.c:653
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1812
#define RelationOpenSmgr(relation)
Definition: rel.h:513
#define DSM_HANDLE_INVALID
Definition: dsm.h:23
#define AUTOPREWARM_FILE
Definition: autoprewarm.c:56
#define PG_ENSURE_ERROR_CLEANUP(cleanup_function, arg)
Definition: ipc.h:47
ForkNumber forknum
Definition: autoprewarm.c:64
void pfree(void *pointer)
Definition: mcxt.c:1057
#define ERROR
Definition: elog.h:43
BgwHandleStatus WaitForBackgroundWorkerShutdown(BackgroundWorkerHandle *handle)
Definition: bgworker.c:1124
#define GUC_UNIT_S
Definition: guc.h:227
struct AutoPrewarmSharedState AutoPrewarmSharedState
static AutoPrewarmSharedState * apw_state
Definition: autoprewarm.c:105
void * ShmemInitStruct(const char *name, Size size, bool *foundPtr)
Definition: shmem.c:392
#define MAXPGPATH
void EmitWarningsOnPlaceholders(const char *className)
Definition: guc.c:8967
static void apw_start_database_worker(void)
Definition: autoprewarm.c:835
static void apw_start_leader_worker(void)
Definition: autoprewarm.c:793
void on_shmem_exit(pg_on_exit_callback function, Datum arg)
Definition: ipc.c:361
static char * buf
Definition: pg_test_fsync.c:68
int errcode_for_file_access(void)
Definition: elog.c:633
#define SIGHUP
Definition: win32_port.h:159
#define GetBufferDescriptor(id)
dsm_handle block_info_handle
Definition: autoprewarm.c:76
FILE * AllocateFile(const char *name, const char *mode)
Definition: fd.c:2322
#define BGW_NEVER_RESTART
Definition: bgworker.h:84
unsigned int uint32
Definition: c.h:375
uint16 tranche
Definition: lwlock.h:33
static bool apw_init_shmem(void)
Definition: autoprewarm.c:753
BgwHandleStatus
Definition: bgworker.h:102
int durable_rename(const char *oldfile, const char *newfile, int elevel)
Definition: fd.c:659
ForkNumber
Definition: relpath.h:40
PG_FUNCTION_INFO_V1(autoprewarm_start_worker)
Definition: guc.h:72
void LWLockInitialize(LWLock *lock, int tranche_id)
Definition: lwlock.c:745
uintptr_t Datum
Definition: postgres.h:367
dsm_segment * dsm_create(Size size, int flags)
Definition: dsm.c:487
#define PG_WAIT_EXTENSION
Definition: pgstat.h:854
struct BlockInfoRecord BlockInfoRecord
bool have_free_buffer(void)
Definition: freelist.c:180
void BackgroundWorkerInitializeConnectionByOid(Oid dboid, Oid useroid, uint32 flags)
Definition: postmaster.c:5759
#define InvalidOid
Definition: postgres_ext.h:36
#define TimestampTzPlusMilliseconds(tz, ms)
Definition: timestamp.h:56
#define ereport(elevel,...)
Definition: elog.h:144
static void apw_sighup_handler(SIGNAL_ARGS)
Definition: autoprewarm.c:919
pqsigfunc pqsignal(int signum, pqsigfunc handler)
Definition: signal.c:170
#define PG_RETURN_VOID()
Definition: fmgr.h:348
BlockNumber RelationGetNumberOfBlocksInFork(Relation relation, ForkNumber forkNum)
Definition: bufmgr.c:2848
static bool autoprewarm
Definition: autoprewarm.c:108
#define SIGNAL_ARGS
Definition: c.h:1275
uint32 LockBufHdr(BufferDesc *desc)
Definition: bufmgr.c:4318
void * dsm_segment_address(dsm_segment *seg)
Definition: dsm.c:1054
void relation_close(Relation relation, LOCKMODE lockmode)
Definition: relation.c:206
char bgw_name[BGW_MAXLEN]
Definition: bgworker.h:90
#define Assert(condition)
Definition: c.h:746
#define BGWORKER_BACKEND_DATABASE_CONNECTION
Definition: bgworker.h:59
void StartTransactionCommand(void)
Definition: xact.c:2846
BgWorkerStartTime bgw_start_time
Definition: bgworker.h:93
void pg_qsort(void *base, size_t nel, size_t elsize, int(*cmp)(const void *, const void *))
Definition: qsort.c:113
bool RegisterDynamicBackgroundWorker(BackgroundWorker *worker, BackgroundWorkerHandle **handle)
Definition: bgworker.c:918
#define MAX_FORKNUM
Definition: relpath.h:55
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1208
#define MAXALIGN(LEN)
Definition: c.h:699
BlockNumber blockNum
Definition: buf_internals.h:94
#define BufferIsValid(bufnum)
Definition: bufmgr.h:123
RelFileNode rnode
Definition: buf_internals.h:92
#define PG_END_ENSURE_ERROR_CLEANUP(cleanup_function, arg)
Definition: ipc.h:52
static volatile sig_atomic_t got_sighup
Definition: autoprewarm.c:102
int FreeFile(FILE *file)
Definition: fd.c:2521
BufferTag tag
char bgw_type[BGW_MAXLEN]
Definition: bgworker.h:91
BlockNumber blocknum
Definition: autoprewarm.c:65
void dsm_detach(dsm_segment *seg)
Definition: dsm.c:769
void * palloc(Size size)
Definition: mcxt.c:950
int errmsg(const char *fmt,...)
Definition: elog.c:821
#define UnlockBufHdr(desc, s)
Datum autoprewarm_dump_now(PG_FUNCTION_ARGS)
Definition: autoprewarm.c:732
pid_t bgw_notify_pid
Definition: bgworker.h:99
int i
static void apw_detach_shmem(int code, Datum arg)
Definition: autoprewarm.c:779
static void apw_load_buffers(void)
Definition: autoprewarm.c:272
int NBuffers
Definition: globals.c:132
void * arg
#define PG_FUNCTION_ARGS
Definition: fmgr.h:193
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:99
Datum autoprewarm_start_worker(PG_FUNCTION_ARGS)
Definition: autoprewarm.c:700
void procsignal_sigusr1_handler(SIGNAL_ARGS)
Definition: procsignal.c:551
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:227
void TimestampDifference(TimestampTz start_time, TimestampTz stop_time, long *secs, int *microsecs)
Definition: timestamp.c:1652
char bgw_library_name[BGW_MAXLEN]
Definition: bgworker.h:95
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:8828
#define snprintf
Definition: port.h:215
int Buffer
Definition: buf.h:23
#define WL_LATCH_SET
Definition: latch.h:124
#define die(msg)
Definition: pg_test_fsync.c:97
static int apw_compare_blockinfo(const void *p, const void *q)
Definition: autoprewarm.c:885
void autoprewarm_database_main(Datum main_arg)
Definition: autoprewarm.c:425
#define WL_EXIT_ON_PM_DEATH
Definition: latch.h:129
#define InvalidPid
Definition: miscadmin.h:32
void BackgroundWorkerUnblockSignals(void)
Definition: postmaster.c:5788