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-2024, 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 "pgstat.h"
34 #include "postmaster/bgworker.h"
35 #include "postmaster/interrupt.h"
36 #include "storage/buf_internals.h"
37 #include "storage/dsm.h"
38 #include "storage/dsm_registry.h"
39 #include "storage/fd.h"
40 #include "storage/ipc.h"
41 #include "storage/latch.h"
42 #include "storage/lwlock.h"
43 #include "storage/procsignal.h"
44 #include "storage/smgr.h"
45 #include "tcop/tcopprot.h"
46 #include "utils/guc.h"
47 #include "utils/rel.h"
48 #include "utils/relfilenumbermap.h"
49 #include "utils/timestamp.h"
50 
51 #define AUTOPREWARM_FILE "autoprewarm.blocks"
52 
53 /* Metadata for each block we dump. */
54 typedef struct BlockInfoRecord
55 {
62 
63 /* Shared state information for autoprewarm bgworker. */
64 typedef struct AutoPrewarmSharedState
65 {
66  LWLock lock; /* mutual exclusion */
67  pid_t bgworker_pid; /* for main bgworker */
68  pid_t pid_using_dumpfile; /* for autoprewarm or block dump */
69 
70  /* Following items are for communication with per-database worker */
77 
78 PGDLLEXPORT void autoprewarm_main(Datum main_arg);
80 
83 
84 static void apw_load_buffers(void);
85 static int apw_dump_now(bool is_bgworker, bool dump_unlogged);
86 static void apw_start_leader_worker(void);
87 static void apw_start_database_worker(void);
88 static bool apw_init_shmem(void);
89 static void apw_detach_shmem(int code, Datum arg);
90 static int apw_compare_blockinfo(const void *p, const void *q);
91 
92 /* Pointer to shared-memory state. */
94 
95 /* GUC variables. */
96 static bool autoprewarm = true; /* start worker? */
97 static int autoprewarm_interval = 300; /* dump interval */
98 
99 /*
100  * Module load callback.
101  */
102 void
103 _PG_init(void)
104 {
105  DefineCustomIntVariable("pg_prewarm.autoprewarm_interval",
106  "Sets the interval between dumps of shared buffers",
107  "If set to zero, time-based dumping is disabled.",
109  300,
110  0, INT_MAX / 1000,
111  PGC_SIGHUP,
112  GUC_UNIT_S,
113  NULL,
114  NULL,
115  NULL);
116 
118  return;
119 
120  /* can't define PGC_POSTMASTER variable after startup */
121  DefineCustomBoolVariable("pg_prewarm.autoprewarm",
122  "Starts the autoprewarm worker.",
123  NULL,
124  &autoprewarm,
125  true,
127  0,
128  NULL,
129  NULL,
130  NULL);
131 
132  MarkGUCPrefixReserved("pg_prewarm");
133 
134  /* Register autoprewarm worker, if enabled. */
135  if (autoprewarm)
137 }
138 
139 /*
140  * Main entry point for the leader autoprewarm process. Per-database workers
141  * have a separate entry point.
142  */
143 void
145 {
146  bool first_time = true;
147  bool final_dump_allowed = true;
148  TimestampTz last_dump_time = 0;
149 
150  /* Establish signal handlers; once that's done, unblock signals. */
155 
156  /* Create (if necessary) and attach to our shared memory area. */
157  if (apw_init_shmem())
158  first_time = false;
159 
160  /*
161  * Set on-detach hook so that our PID will be cleared on exit.
162  *
163  * NB: Autoprewarm's state is stored in a DSM segment, and DSM segments
164  * are detached before calling the on_shmem_exit callbacks, so we must put
165  * apw_detach_shmem in the before_shmem_exit callback list.
166  */
168 
169  /*
170  * Store our PID in the shared memory area --- unless there's already
171  * another worker running, in which case just exit.
172  */
175  {
177  ereport(LOG,
178  (errmsg("autoprewarm worker is already running under PID %d",
179  (int) apw_state->bgworker_pid)));
180  return;
181  }
184 
185  /*
186  * Preload buffers from the dump file only if we just created the shared
187  * memory region. Otherwise, it's either already been done or shouldn't
188  * be done - e.g. because the old dump file has been overwritten since the
189  * server was started.
190  *
191  * There's not much point in performing a dump immediately after we finish
192  * preloading; so, if we do end up preloading, consider the last dump time
193  * to be equal to the current time.
194  *
195  * If apw_load_buffers() is terminated early by a shutdown request,
196  * prevent dumping out our state below the loop, because we'd effectively
197  * just truncate the saved state to however much we'd managed to preload.
198  */
199  if (first_time)
200  {
202  final_dump_allowed = !ShutdownRequestPending;
203  last_dump_time = GetCurrentTimestamp();
204  }
205 
206  /* Periodically dump buffers until terminated. */
207  while (!ShutdownRequestPending)
208  {
209  /* In case of a SIGHUP, just reload the configuration. */
211  {
212  ConfigReloadPending = false;
214  }
215 
216  if (autoprewarm_interval <= 0)
217  {
218  /* We're only dumping at shutdown, so just wait forever. */
219  (void) WaitLatch(MyLatch,
221  -1L,
223  }
224  else
225  {
226  TimestampTz next_dump_time;
227  long delay_in_ms;
228 
229  /* Compute the next dump time. */
230  next_dump_time =
231  TimestampTzPlusMilliseconds(last_dump_time,
232  autoprewarm_interval * 1000);
233  delay_in_ms =
235  next_dump_time);
236 
237  /* Perform a dump if it's time. */
238  if (delay_in_ms <= 0)
239  {
240  last_dump_time = GetCurrentTimestamp();
241  apw_dump_now(true, false);
242  continue;
243  }
244 
245  /* Sleep until the next dump time. */
246  (void) WaitLatch(MyLatch,
248  delay_in_ms,
250  }
251 
252  /* Reset the latch, loop. */
254  }
255 
256  /*
257  * Dump one last time. We assume this is probably the result of a system
258  * shutdown, although it's possible that we've merely been terminated.
259  */
260  if (final_dump_allowed)
261  apw_dump_now(true, true);
262 }
263 
264 /*
265  * Read the dump file and launch per-database workers one at a time to
266  * prewarm the buffers found there.
267  */
268 static void
270 {
271  FILE *file = NULL;
272  int num_elements,
273  i;
274  BlockInfoRecord *blkinfo;
275  dsm_segment *seg;
276 
277  /*
278  * Skip the prewarm if the dump file is in use; otherwise, prevent any
279  * other process from writing it while we're using it.
280  */
284  else
285  {
287  ereport(LOG,
288  (errmsg("skipping prewarm because block dump file is being written by PID %d",
289  (int) apw_state->pid_using_dumpfile)));
290  return;
291  }
293 
294  /*
295  * Open the block dump file. Exit quietly if it doesn't exist, but report
296  * any other error.
297  */
298  file = AllocateFile(AUTOPREWARM_FILE, "r");
299  if (!file)
300  {
301  if (errno == ENOENT)
302  {
306  return; /* No file to load. */
307  }
308  ereport(ERROR,
310  errmsg("could not read file \"%s\": %m",
311  AUTOPREWARM_FILE)));
312  }
313 
314  /* First line of the file is a record count. */
315  if (fscanf(file, "<<%d>>\n", &num_elements) != 1)
316  ereport(ERROR,
318  errmsg("could not read from file \"%s\": %m",
319  AUTOPREWARM_FILE)));
320 
321  /* Allocate a dynamic shared memory segment to store the record data. */
322  seg = dsm_create(sizeof(BlockInfoRecord) * num_elements, 0);
323  blkinfo = (BlockInfoRecord *) dsm_segment_address(seg);
324 
325  /* Read records, one per line. */
326  for (i = 0; i < num_elements; i++)
327  {
328  unsigned forknum;
329 
330  if (fscanf(file, "%u,%u,%u,%u,%u\n", &blkinfo[i].database,
331  &blkinfo[i].tablespace, &blkinfo[i].filenumber,
332  &forknum, &blkinfo[i].blocknum) != 5)
333  ereport(ERROR,
334  (errmsg("autoprewarm block dump file is corrupted at line %d",
335  i + 1)));
336  blkinfo[i].forknum = forknum;
337  }
338 
339  FreeFile(file);
340 
341  /* Sort the blocks to be loaded. */
342  qsort(blkinfo, num_elements, sizeof(BlockInfoRecord),
344 
345  /* Populate shared memory state. */
349 
350  /* Get the info position of the first block of the next database. */
351  while (apw_state->prewarm_start_idx < num_elements)
352  {
354  Oid current_db = blkinfo[j].database;
355 
356  /*
357  * Advance the prewarm_stop_idx to the first BlockInfoRecord that does
358  * not belong to this database.
359  */
360  j++;
361  while (j < num_elements)
362  {
363  if (current_db != blkinfo[j].database)
364  {
365  /*
366  * Combine BlockInfoRecords for global objects with those of
367  * the database.
368  */
369  if (current_db != InvalidOid)
370  break;
371  current_db = blkinfo[j].database;
372  }
373 
374  j++;
375  }
376 
377  /*
378  * If we reach this point with current_db == InvalidOid, then only
379  * BlockInfoRecords belonging to global objects exist. We can't
380  * prewarm without a database connection, so just bail out.
381  */
382  if (current_db == InvalidOid)
383  break;
384 
385  /* Configure stop point and database for next per-database worker. */
387  apw_state->database = current_db;
389 
390  /* If we've run out of free buffers, don't launch another worker. */
391  if (!have_free_buffer())
392  break;
393 
394  /*
395  * Likewise, don't launch if we've already been told to shut down.
396  * (The launch would fail anyway, but we might as well skip it.)
397  */
399  break;
400 
401  /*
402  * Start a per-database worker to load blocks for this database; this
403  * function will return once the per-database worker exits.
404  */
406 
407  /* Prepare for next database. */
409  }
410 
411  /* Clean up. */
412  dsm_detach(seg);
417 
418  /* Report our success, if we were able to finish. */
420  ereport(LOG,
421  (errmsg("autoprewarm successfully prewarmed %d of %d previously-loaded blocks",
422  apw_state->prewarmed_blocks, num_elements)));
423 }
424 
425 /*
426  * Prewarm all blocks for one database (and possibly also global objects, if
427  * those got grouped with this database).
428  */
429 void
431 {
432  int pos;
433  BlockInfoRecord *block_info;
434  Relation rel = NULL;
435  BlockNumber nblocks = 0;
436  BlockInfoRecord *old_blk = NULL;
437  dsm_segment *seg;
438 
439  /* Establish signal handlers; once that's done, unblock signals. */
440  pqsignal(SIGTERM, die);
442 
443  /* Connect to correct database and get block information. */
444  apw_init_shmem();
446  if (seg == NULL)
447  ereport(ERROR,
448  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
449  errmsg("could not map dynamic shared memory segment")));
451  block_info = (BlockInfoRecord *) dsm_segment_address(seg);
453 
454  /*
455  * Loop until we run out of blocks to prewarm or until we run out of free
456  * buffers.
457  */
458  while (pos < apw_state->prewarm_stop_idx && have_free_buffer())
459  {
460  BlockInfoRecord *blk = &block_info[pos++];
461  Buffer buf;
462 
464 
465  /*
466  * Quit if we've reached records for another database. If previous
467  * blocks are of some global objects, then continue pre-warming.
468  */
469  if (old_blk != NULL && old_blk->database != blk->database &&
470  old_blk->database != 0)
471  break;
472 
473  /*
474  * As soon as we encounter a block of a new relation, close the old
475  * relation. Note that rel will be NULL if try_relation_open failed
476  * previously; in that case, there is nothing to close.
477  */
478  if (old_blk != NULL && old_blk->filenumber != blk->filenumber &&
479  rel != NULL)
480  {
482  rel = NULL;
484  }
485 
486  /*
487  * Try to open each new relation, but only once, when we first
488  * encounter it. If it's been dropped, skip the associated blocks.
489  */
490  if (old_blk == NULL || old_blk->filenumber != blk->filenumber)
491  {
492  Oid reloid;
493 
494  Assert(rel == NULL);
496  reloid = RelidByRelfilenumber(blk->tablespace, blk->filenumber);
497  if (OidIsValid(reloid))
498  rel = try_relation_open(reloid, AccessShareLock);
499 
500  if (!rel)
502  }
503  if (!rel)
504  {
505  old_blk = blk;
506  continue;
507  }
508 
509  /* Once per fork, check for fork existence and size. */
510  if (old_blk == NULL ||
511  old_blk->filenumber != blk->filenumber ||
512  old_blk->forknum != blk->forknum)
513  {
514  /*
515  * smgrexists is not safe for illegal forknum, hence check whether
516  * the passed forknum is valid before using it in smgrexists.
517  */
518  if (blk->forknum > InvalidForkNumber &&
519  blk->forknum <= MAX_FORKNUM &&
520  smgrexists(RelationGetSmgr(rel), blk->forknum))
521  nblocks = RelationGetNumberOfBlocksInFork(rel, blk->forknum);
522  else
523  nblocks = 0;
524  }
525 
526  /* Check whether blocknum is valid and within fork file size. */
527  if (blk->blocknum >= nblocks)
528  {
529  /* Move to next forknum. */
530  old_blk = blk;
531  continue;
532  }
533 
534  /* Prewarm buffer. */
535  buf = ReadBufferExtended(rel, blk->forknum, blk->blocknum, RBM_NORMAL,
536  NULL);
537  if (BufferIsValid(buf))
538  {
541  }
542 
543  old_blk = blk;
544  }
545 
546  dsm_detach(seg);
547 
548  /* Release lock on previous relation. */
549  if (rel)
550  {
553  }
554 }
555 
556 /*
557  * Dump information on blocks in shared buffers. We use a text format here
558  * so that it's easy to understand and even change the file contents if
559  * necessary.
560  * Returns the number of blocks dumped.
561  */
562 static int
563 apw_dump_now(bool is_bgworker, bool dump_unlogged)
564 {
565  int num_blocks;
566  int i;
567  int ret;
568  BlockInfoRecord *block_info_array;
569  BufferDesc *bufHdr;
570  FILE *file;
571  char transient_dump_file_path[MAXPGPATH];
572  pid_t pid;
573 
579 
580  if (pid != InvalidPid)
581  {
582  if (!is_bgworker)
583  ereport(ERROR,
584  (errmsg("could not perform block dump because dump file is being used by PID %d",
585  (int) apw_state->pid_using_dumpfile)));
586 
587  ereport(LOG,
588  (errmsg("skipping block dump because it is already being performed by PID %d",
589  (int) apw_state->pid_using_dumpfile)));
590  return 0;
591  }
592 
593  block_info_array =
595 
596  for (num_blocks = 0, i = 0; i < NBuffers; i++)
597  {
598  uint32 buf_state;
599 
601 
602  bufHdr = GetBufferDescriptor(i);
603 
604  /* Lock each buffer header before inspecting. */
605  buf_state = LockBufHdr(bufHdr);
606 
607  /*
608  * Unlogged tables will be automatically truncated after a crash or
609  * unclean shutdown. In such cases we need not prewarm them. Dump them
610  * only if requested by caller.
611  */
612  if (buf_state & BM_TAG_VALID &&
613  ((buf_state & BM_PERMANENT) || dump_unlogged))
614  {
615  block_info_array[num_blocks].database = bufHdr->tag.dbOid;
616  block_info_array[num_blocks].tablespace = bufHdr->tag.spcOid;
617  block_info_array[num_blocks].filenumber =
618  BufTagGetRelNumber(&bufHdr->tag);
619  block_info_array[num_blocks].forknum =
620  BufTagGetForkNum(&bufHdr->tag);
621  block_info_array[num_blocks].blocknum = bufHdr->tag.blockNum;
622  ++num_blocks;
623  }
624 
625  UnlockBufHdr(bufHdr, buf_state);
626  }
627 
628  snprintf(transient_dump_file_path, MAXPGPATH, "%s.tmp", AUTOPREWARM_FILE);
629  file = AllocateFile(transient_dump_file_path, "w");
630  if (!file)
631  ereport(ERROR,
633  errmsg("could not open file \"%s\": %m",
634  transient_dump_file_path)));
635 
636  ret = fprintf(file, "<<%d>>\n", num_blocks);
637  if (ret < 0)
638  {
639  int save_errno = errno;
640 
641  FreeFile(file);
642  unlink(transient_dump_file_path);
643  errno = save_errno;
644  ereport(ERROR,
646  errmsg("could not write to file \"%s\": %m",
647  transient_dump_file_path)));
648  }
649 
650  for (i = 0; i < num_blocks; i++)
651  {
653 
654  ret = fprintf(file, "%u,%u,%u,%u,%u\n",
655  block_info_array[i].database,
656  block_info_array[i].tablespace,
657  block_info_array[i].filenumber,
658  (uint32) block_info_array[i].forknum,
659  block_info_array[i].blocknum);
660  if (ret < 0)
661  {
662  int save_errno = errno;
663 
664  FreeFile(file);
665  unlink(transient_dump_file_path);
666  errno = save_errno;
667  ereport(ERROR,
669  errmsg("could not write to file \"%s\": %m",
670  transient_dump_file_path)));
671  }
672  }
673 
674  pfree(block_info_array);
675 
676  /*
677  * Rename transient_dump_file_path to AUTOPREWARM_FILE to make things
678  * permanent.
679  */
680  ret = FreeFile(file);
681  if (ret != 0)
682  {
683  int save_errno = errno;
684 
685  unlink(transient_dump_file_path);
686  errno = save_errno;
687  ereport(ERROR,
689  errmsg("could not close file \"%s\": %m",
690  transient_dump_file_path)));
691  }
692 
693  (void) durable_rename(transient_dump_file_path, AUTOPREWARM_FILE, ERROR);
695 
696  ereport(DEBUG1,
697  (errmsg_internal("wrote block details for %d blocks", num_blocks)));
698  return num_blocks;
699 }
700 
701 /*
702  * SQL-callable function to launch autoprewarm.
703  */
704 Datum
706 {
707  pid_t pid;
708 
709  if (!autoprewarm)
710  ereport(ERROR,
711  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
712  errmsg("autoprewarm is disabled")));
713 
714  apw_init_shmem();
716  pid = apw_state->bgworker_pid;
718 
719  if (pid != InvalidPid)
720  ereport(ERROR,
721  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
722  errmsg("autoprewarm worker is already running under PID %d",
723  (int) pid)));
724 
726 
727  PG_RETURN_VOID();
728 }
729 
730 /*
731  * SQL-callable function to perform an immediate block dump.
732  *
733  * Note: this is declared to return int8, as insurance against some
734  * very distant day when we might make NBuffers wider than int.
735  */
736 Datum
738 {
739  int num_blocks;
740 
741  apw_init_shmem();
742 
744  {
745  num_blocks = apw_dump_now(false, true);
746  }
748 
749  PG_RETURN_INT64((int64) num_blocks);
750 }
751 
752 static void
753 apw_init_state(void *ptr)
754 {
756 
758  state->bgworker_pid = InvalidPid;
759  state->pid_using_dumpfile = InvalidPid;
760 }
761 
762 /*
763  * Allocate and initialize autoprewarm related shared memory, if not already
764  * done, and set up backend-local pointer to that state. Returns true if an
765  * existing shared memory segment was found.
766  */
767 static bool
769 {
770  bool found;
771 
772  apw_state = GetNamedDSMSegment("autoprewarm",
773  sizeof(AutoPrewarmSharedState),
775  &found);
776  LWLockRegisterTranche(apw_state->lock.tranche, "autoprewarm");
777 
778  return found;
779 }
780 
781 /*
782  * Clear our PID from autoprewarm shared state.
783  */
784 static void
786 {
793 }
794 
795 /*
796  * Start autoprewarm leader worker process.
797  */
798 static void
800 {
801  BackgroundWorker worker;
802  BackgroundWorkerHandle *handle;
803  BgwHandleStatus status;
804  pid_t pid;
805 
806  MemSet(&worker, 0, sizeof(BackgroundWorker));
809  strcpy(worker.bgw_library_name, "pg_prewarm");
810  strcpy(worker.bgw_function_name, "autoprewarm_main");
811  strcpy(worker.bgw_name, "autoprewarm leader");
812  strcpy(worker.bgw_type, "autoprewarm leader");
813 
815  {
816  RegisterBackgroundWorker(&worker);
817  return;
818  }
819 
820  /* must set notify PID to wait for startup */
821  worker.bgw_notify_pid = MyProcPid;
822 
823  if (!RegisterDynamicBackgroundWorker(&worker, &handle))
824  ereport(ERROR,
825  (errcode(ERRCODE_INSUFFICIENT_RESOURCES),
826  errmsg("could not register background process"),
827  errhint("You may need to increase \"max_worker_processes\".")));
828 
829  status = WaitForBackgroundWorkerStartup(handle, &pid);
830  if (status != BGWH_STARTED)
831  ereport(ERROR,
832  (errcode(ERRCODE_INSUFFICIENT_RESOURCES),
833  errmsg("could not start background process"),
834  errhint("More details may be available in the server log.")));
835 }
836 
837 /*
838  * Start autoprewarm per-database worker process.
839  */
840 static void
842 {
843  BackgroundWorker worker;
844  BackgroundWorkerHandle *handle;
845 
846  MemSet(&worker, 0, sizeof(BackgroundWorker));
847  worker.bgw_flags =
851  strcpy(worker.bgw_library_name, "pg_prewarm");
852  strcpy(worker.bgw_function_name, "autoprewarm_database_main");
853  strcpy(worker.bgw_name, "autoprewarm worker");
854  strcpy(worker.bgw_type, "autoprewarm worker");
855 
856  /* must set notify PID to wait for shutdown */
857  worker.bgw_notify_pid = MyProcPid;
858 
859  if (!RegisterDynamicBackgroundWorker(&worker, &handle))
860  ereport(ERROR,
861  (errcode(ERRCODE_INSUFFICIENT_RESOURCES),
862  errmsg("registering dynamic bgworker autoprewarm failed"),
863  errhint("Consider increasing the configuration parameter \"%s\".", "max_worker_processes")));
864 
865  /*
866  * Ignore return value; if it fails, postmaster has died, but we have
867  * checks for that elsewhere.
868  */
870 }
871 
872 /* Compare member elements to check whether they are not equal. */
873 #define cmp_member_elem(fld) \
874 do { \
875  if (a->fld < b->fld) \
876  return -1; \
877  else if (a->fld > b->fld) \
878  return 1; \
879 } while(0)
880 
881 /*
882  * apw_compare_blockinfo
883  *
884  * We depend on all records for a particular database being consecutive
885  * in the dump file; each per-database worker will preload blocks until
886  * it sees a block for some other database. Sorting by tablespace,
887  * filenumber, forknum, and blocknum isn't critical for correctness, but
888  * helps us get a sequential I/O pattern.
889  */
890 static int
891 apw_compare_blockinfo(const void *p, const void *q)
892 {
893  const BlockInfoRecord *a = (const BlockInfoRecord *) p;
894  const BlockInfoRecord *b = (const BlockInfoRecord *) q;
895 
896  cmp_member_elem(database);
898  cmp_member_elem(filenumber);
899  cmp_member_elem(forknum);
900  cmp_member_elem(blocknum);
901 
902  return 0;
903 }
static int apw_compare_blockinfo(const void *p, const void *q)
Definition: autoprewarm.c:891
Datum autoprewarm_start_worker(PG_FUNCTION_ARGS)
Definition: autoprewarm.c:705
static AutoPrewarmSharedState * apw_state
Definition: autoprewarm.c:93
static void apw_detach_shmem(int code, Datum arg)
Definition: autoprewarm.c:785
void _PG_init(void)
Definition: autoprewarm.c:103
PGDLLEXPORT void autoprewarm_main(Datum main_arg)
Definition: autoprewarm.c:144
#define cmp_member_elem(fld)
Definition: autoprewarm.c:873
static bool apw_init_shmem(void)
Definition: autoprewarm.c:768
static bool autoprewarm
Definition: autoprewarm.c:96
#define AUTOPREWARM_FILE
Definition: autoprewarm.c:51
static void apw_start_leader_worker(void)
Definition: autoprewarm.c:799
PGDLLEXPORT void autoprewarm_database_main(Datum main_arg)
Definition: autoprewarm.c:430
static void apw_start_database_worker(void)
Definition: autoprewarm.c:841
static void apw_load_buffers(void)
Definition: autoprewarm.c:269
static void apw_init_state(void *ptr)
Definition: autoprewarm.c:753
Datum autoprewarm_dump_now(PG_FUNCTION_ARGS)
Definition: autoprewarm.c:737
static int autoprewarm_interval
Definition: autoprewarm.c:97
static int apw_dump_now(bool is_bgworker, bool dump_unlogged)
Definition: autoprewarm.c:563
struct AutoPrewarmSharedState AutoPrewarmSharedState
PG_FUNCTION_INFO_V1(autoprewarm_start_worker)
struct BlockInfoRecord BlockInfoRecord
long TimestampDifferenceMilliseconds(TimestampTz start_time, TimestampTz stop_time)
Definition: timestamp.c:1756
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1644
void RegisterBackgroundWorker(BackgroundWorker *worker)
Definition: bgworker.c:939
BgwHandleStatus WaitForBackgroundWorkerStartup(BackgroundWorkerHandle *handle, pid_t *pidp)
Definition: bgworker.c:1212
BgwHandleStatus WaitForBackgroundWorkerShutdown(BackgroundWorkerHandle *handle)
Definition: bgworker.c:1257
void BackgroundWorkerUnblockSignals(void)
Definition: bgworker.c:926
void BackgroundWorkerInitializeConnectionByOid(Oid dboid, Oid useroid, uint32 flags)
Definition: bgworker.c:886
bool RegisterDynamicBackgroundWorker(BackgroundWorker *worker, BackgroundWorkerHandle **handle)
Definition: bgworker.c:1045
#define BGW_NEVER_RESTART
Definition: bgworker.h:85
BgwHandleStatus
Definition: bgworker.h:104
@ BGWH_STARTED
Definition: bgworker.h:105
@ BgWorkerStart_ConsistentState
Definition: bgworker.h:80
#define BGWORKER_BACKEND_DATABASE_CONNECTION
Definition: bgworker.h:60
#define BGWORKER_SHMEM_ACCESS
Definition: bgworker.h:53
uint32 BlockNumber
Definition: block.h:31
int Buffer
Definition: buf.h:23
#define BM_TAG_VALID
Definition: buf_internals.h:62
#define BM_PERMANENT
Definition: buf_internals.h:68
static ForkNumber BufTagGetForkNum(const BufferTag *tag)
static BufferDesc * GetBufferDescriptor(uint32 id)
static void UnlockBufHdr(BufferDesc *desc, uint32 buf_state)
static RelFileNumber BufTagGetRelNumber(const BufferTag *tag)
BlockNumber RelationGetNumberOfBlocksInFork(Relation relation, ForkNumber forkNum)
Definition: bufmgr.c:3923
void ReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:4924
Buffer ReadBufferExtended(Relation reln, ForkNumber forkNum, BlockNumber blockNum, ReadBufferMode mode, BufferAccessStrategy strategy)
Definition: bufmgr.c:793
uint32 LockBufHdr(BufferDesc *desc)
Definition: bufmgr.c:5761
@ RBM_NORMAL
Definition: bufmgr.h:45
static bool BufferIsValid(Buffer bufnum)
Definition: bufmgr.h:351
#define Assert(condition)
Definition: c.h:812
int64_t int64
Definition: c.h:482
#define PGDLLEXPORT
Definition: c.h:1289
uint32_t uint32
Definition: c.h:485
#define MemSet(start, val, len)
Definition: c.h:974
#define OidIsValid(objectId)
Definition: c.h:729
int64 TimestampTz
Definition: timestamp.h:39
dsm_handle dsm_segment_handle(dsm_segment *seg)
Definition: dsm.c:1123
void * dsm_segment_address(dsm_segment *seg)
Definition: dsm.c:1095
void dsm_detach(dsm_segment *seg)
Definition: dsm.c:803
dsm_segment * dsm_attach(dsm_handle h)
Definition: dsm.c:665
dsm_segment * dsm_create(Size size, int flags)
Definition: dsm.c:516
uint32 dsm_handle
Definition: dsm_impl.h:55
#define DSM_HANDLE_INVALID
Definition: dsm_impl.h:58
void * GetNamedDSMSegment(const char *name, size_t size, void(*init_callback)(void *ptr), bool *found)
Definition: dsm_registry.c:131
int errmsg_internal(const char *fmt,...)
Definition: elog.c:1157
int errcode_for_file_access(void)
Definition: elog.c:876
int errhint(const char *fmt,...)
Definition: elog.c:1317
int errcode(int sqlerrcode)
Definition: elog.c:853
int errmsg(const char *fmt,...)
Definition: elog.c:1070
#define LOG
Definition: elog.h:31
#define DEBUG1
Definition: elog.h:30
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:149
FILE * AllocateFile(const char *name, const char *mode)
Definition: fd.c:2605
int durable_rename(const char *oldfile, const char *newfile, int elevel)
Definition: fd.c:781
int FreeFile(FILE *file)
Definition: fd.c:2803
#define PG_RETURN_VOID()
Definition: fmgr.h:349
#define PG_RETURN_INT64(x)
Definition: fmgr.h:368
#define PG_FUNCTION_ARGS
Definition: fmgr.h:193
bool have_free_buffer(void)
Definition: freelist.c:175
int NBuffers
Definition: globals.c:141
int MyProcPid
Definition: globals.c:46
struct Latch * MyLatch
Definition: globals.c:62
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:5132
void MarkGUCPrefixReserved(const char *className)
Definition: guc.c:5279
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:5158
@ PGC_POSTMASTER
Definition: guc.h:70
@ PGC_SIGHUP
Definition: guc.h:71
#define GUC_UNIT_S
Definition: guc.h:236
void ProcessConfigFile(GucContext context)
void SignalHandlerForShutdownRequest(SIGNAL_ARGS)
Definition: interrupt.c:105
volatile sig_atomic_t ShutdownRequestPending
Definition: interrupt.c:28
volatile sig_atomic_t ConfigReloadPending
Definition: interrupt.c:27
void SignalHandlerForConfigReload(SIGNAL_ARGS)
Definition: interrupt.c:61
void before_shmem_exit(pg_on_exit_callback function, Datum arg)
Definition: ipc.c:337
#define PG_ENSURE_ERROR_CLEANUP(cleanup_function, arg)
Definition: ipc.h:47
#define PG_END_ENSURE_ERROR_CLEANUP(cleanup_function, arg)
Definition: ipc.h:52
int b
Definition: isn.c:69
int a
Definition: isn.c:68
int j
Definition: isn.c:73
int i
Definition: isn.c:72
void ResetLatch(Latch *latch)
Definition: latch.c:724
int WaitLatch(Latch *latch, int wakeEvents, long timeout, uint32 wait_event_info)
Definition: latch.c:517
#define WL_TIMEOUT
Definition: latch.h:130
#define WL_EXIT_ON_PM_DEATH
Definition: latch.h:132
#define WL_LATCH_SET
Definition: latch.h:127
#define AccessShareLock
Definition: lockdefs.h:36
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1168
void LWLockRegisterTranche(int tranche_id, const char *tranche_name)
Definition: lwlock.c:628
int LWLockNewTrancheId(void)
Definition: lwlock.c:603
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1781
void LWLockInitialize(LWLock *lock, int tranche_id)
Definition: lwlock.c:707
@ LW_EXCLUSIVE
Definition: lwlock.h:114
void pfree(void *pointer)
Definition: mcxt.c:1521
void * palloc(Size size)
Definition: mcxt.c:1317
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:122
#define InvalidPid
Definition: miscadmin.h:32
bool process_shared_preload_libraries_in_progress
Definition: miscinit.c:1839
void * arg
#define MAXPGPATH
#define die(msg)
static char * buf
Definition: pg_test_fsync.c:72
static char * tablespace
Definition: pgbench.c:216
pqsigfunc pqsignal(int signo, pqsigfunc func)
#define snprintf
Definition: port.h:238
#define fprintf
Definition: port.h:242
#define qsort(a, b, c, d)
Definition: port.h:447
uintptr_t Datum
Definition: postgres.h:64
#define InvalidOid
Definition: postgres_ext.h:36
unsigned int Oid
Definition: postgres_ext.h:31
void procsignal_sigusr1_handler(SIGNAL_ARGS)
Definition: procsignal.c:671
static SMgrRelation RelationGetSmgr(Relation rel)
Definition: rel.h:567
Oid RelidByRelfilenumber(Oid reltablespace, RelFileNumber relfilenumber)
Oid RelFileNumber
Definition: relpath.h:25
ForkNumber
Definition: relpath.h:56
@ InvalidForkNumber
Definition: relpath.h:57
#define MAX_FORKNUM
Definition: relpath.h:70
bool smgrexists(SMgrRelation reln, ForkNumber forknum)
Definition: smgr.c:401
void relation_close(Relation relation, LOCKMODE lockmode)
Definition: relation.c:205
Relation try_relation_open(Oid relationId, LOCKMODE lockmode)
Definition: relation.c:88
dsm_handle block_info_handle
Definition: autoprewarm.c:71
char bgw_function_name[BGW_MAXLEN]
Definition: bgworker.h:97
char bgw_name[BGW_MAXLEN]
Definition: bgworker.h:91
int bgw_restart_time
Definition: bgworker.h:95
char bgw_type[BGW_MAXLEN]
Definition: bgworker.h:92
BgWorkerStartTime bgw_start_time
Definition: bgworker.h:94
pid_t bgw_notify_pid
Definition: bgworker.h:100
char bgw_library_name[MAXPGPATH]
Definition: bgworker.h:96
ForkNumber forknum
Definition: autoprewarm.c:59
RelFileNumber filenumber
Definition: autoprewarm.c:58
BlockNumber blocknum
Definition: autoprewarm.c:60
BufferTag tag
Definition: lwlock.h:42
uint16 tranche
Definition: lwlock.h:43
BlockNumber blockNum
Definition: buf_internals.h:97
Oid spcOid
Definition: buf_internals.h:93
Oid dbOid
Definition: buf_internals.h:94
Definition: regguts.h:323
#define TimestampTzPlusMilliseconds(tz, ms)
Definition: timestamp.h:85
#define PG_WAIT_EXTENSION
Definition: wait_event.h:23
#define SIGHUP
Definition: win32_port.h:168
#define SIGUSR1
Definition: win32_port.h:180
void StartTransactionCommand(void)
Definition: xact.c:3051
void CommitTransactionCommand(void)
Definition: xact.c:3149