Preserve CurrentMemoryContext across notify and sinval interrupts.
[pgsql.git] / contrib / pg_prewarm / autoprewarm.c
blobd061731706ac02390c9620e6138fa38b4821d013
1 /*-------------------------------------------------------------------------
3 * autoprewarm.c
4 * Periodically dump information about the blocks present in
5 * shared_buffers, and reload them on server restart.
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.
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.
19 * Copyright (c) 2016-2024, PostgreSQL Global Development Group
21 * IDENTIFICATION
22 * contrib/pg_prewarm/autoprewarm.c
24 *-------------------------------------------------------------------------
27 #include "postgres.h"
29 #include <unistd.h>
31 #include "access/relation.h"
32 #include "access/xact.h"
33 #include "catalog/pg_class.h"
34 #include "catalog/pg_type.h"
35 #include "pgstat.h"
36 #include "postmaster/bgworker.h"
37 #include "postmaster/interrupt.h"
38 #include "storage/buf_internals.h"
39 #include "storage/dsm.h"
40 #include "storage/dsm_registry.h"
41 #include "storage/fd.h"
42 #include "storage/ipc.h"
43 #include "storage/latch.h"
44 #include "storage/lwlock.h"
45 #include "storage/proc.h"
46 #include "storage/procsignal.h"
47 #include "storage/shmem.h"
48 #include "storage/smgr.h"
49 #include "tcop/tcopprot.h"
50 #include "utils/acl.h"
51 #include "utils/datetime.h"
52 #include "utils/guc.h"
53 #include "utils/memutils.h"
54 #include "utils/rel.h"
55 #include "utils/relfilenumbermap.h"
56 #include "utils/resowner.h"
58 #define AUTOPREWARM_FILE "autoprewarm.blocks"
60 /* Metadata for each block we dump. */
61 typedef struct BlockInfoRecord
63 Oid database;
64 Oid tablespace;
65 RelFileNumber filenumber;
66 ForkNumber forknum;
67 BlockNumber blocknum;
68 } BlockInfoRecord;
70 /* Shared state information for autoprewarm bgworker. */
71 typedef struct AutoPrewarmSharedState
73 LWLock lock; /* mutual exclusion */
74 pid_t bgworker_pid; /* for main bgworker */
75 pid_t pid_using_dumpfile; /* for autoprewarm or block dump */
77 /* Following items are for communication with per-database worker */
78 dsm_handle block_info_handle;
79 Oid database;
80 int prewarm_start_idx;
81 int prewarm_stop_idx;
82 int prewarmed_blocks;
83 } AutoPrewarmSharedState;
85 PGDLLEXPORT void autoprewarm_main(Datum main_arg);
86 PGDLLEXPORT void autoprewarm_database_main(Datum main_arg);
88 PG_FUNCTION_INFO_V1(autoprewarm_start_worker);
89 PG_FUNCTION_INFO_V1(autoprewarm_dump_now);
91 static void apw_load_buffers(void);
92 static int apw_dump_now(bool is_bgworker, bool dump_unlogged);
93 static void apw_start_leader_worker(void);
94 static void apw_start_database_worker(void);
95 static bool apw_init_shmem(void);
96 static void apw_detach_shmem(int code, Datum arg);
97 static int apw_compare_blockinfo(const void *p, const void *q);
99 /* Pointer to shared-memory state. */
100 static AutoPrewarmSharedState *apw_state = NULL;
102 /* GUC variables. */
103 static bool autoprewarm = true; /* start worker? */
104 static int autoprewarm_interval = 300; /* dump interval */
107 * Module load callback.
109 void
110 _PG_init(void)
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.",
115 &autoprewarm_interval,
116 300,
117 0, INT_MAX / 1000,
118 PGC_SIGHUP,
119 GUC_UNIT_S,
120 NULL,
121 NULL,
122 NULL);
124 if (!process_shared_preload_libraries_in_progress)
125 return;
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,
133 PGC_POSTMASTER,
135 NULL,
136 NULL,
137 NULL);
139 MarkGUCPrefixReserved("pg_prewarm");
141 /* Register autoprewarm worker, if enabled. */
142 if (autoprewarm)
143 apw_start_leader_worker();
147 * Main entry point for the leader autoprewarm process. Per-database workers
148 * have a separate entry point.
150 void
151 autoprewarm_main(Datum main_arg)
153 bool first_time = true;
154 bool final_dump_allowed = true;
155 TimestampTz last_dump_time = 0;
157 /* Establish signal handlers; once that's done, unblock signals. */
158 pqsignal(SIGTERM, SignalHandlerForShutdownRequest);
159 pqsignal(SIGHUP, SignalHandlerForConfigReload);
160 pqsignal(SIGUSR1, procsignal_sigusr1_handler);
161 BackgroundWorkerUnblockSignals();
163 /* Create (if necessary) and attach to our shared memory area. */
164 if (apw_init_shmem())
165 first_time = false;
168 * Set on-detach hook so that our PID will be cleared on exit.
170 * NB: Autoprewarm's state is stored in a DSM segment, and DSM segments
171 * are detached before calling the on_shmem_exit callbacks, so we must put
172 * apw_detach_shmem in the before_shmem_exit callback list.
174 before_shmem_exit(apw_detach_shmem, 0);
177 * Store our PID in the shared memory area --- unless there's already
178 * another worker running, in which case just exit.
180 LWLockAcquire(&apw_state->lock, LW_EXCLUSIVE);
181 if (apw_state->bgworker_pid != InvalidPid)
183 LWLockRelease(&apw_state->lock);
184 ereport(LOG,
185 (errmsg("autoprewarm worker is already running under PID %d",
186 (int) apw_state->bgworker_pid)));
187 return;
189 apw_state->bgworker_pid = MyProcPid;
190 LWLockRelease(&apw_state->lock);
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.
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.
202 * If apw_load_buffers() is terminated early by a shutdown request,
203 * prevent dumping out our state below the loop, because we'd effectively
204 * just truncate the saved state to however much we'd managed to preload.
206 if (first_time)
208 apw_load_buffers();
209 final_dump_allowed = !ShutdownRequestPending;
210 last_dump_time = GetCurrentTimestamp();
213 /* Periodically dump buffers until terminated. */
214 while (!ShutdownRequestPending)
216 /* In case of a SIGHUP, just reload the configuration. */
217 if (ConfigReloadPending)
219 ConfigReloadPending = false;
220 ProcessConfigFile(PGC_SIGHUP);
223 if (autoprewarm_interval <= 0)
225 /* We're only dumping at shutdown, so just wait forever. */
226 (void) WaitLatch(MyLatch,
227 WL_LATCH_SET | WL_EXIT_ON_PM_DEATH,
228 -1L,
229 PG_WAIT_EXTENSION);
231 else
233 TimestampTz next_dump_time;
234 long delay_in_ms;
236 /* Compute the next dump time. */
237 next_dump_time =
238 TimestampTzPlusMilliseconds(last_dump_time,
239 autoprewarm_interval * 1000);
240 delay_in_ms =
241 TimestampDifferenceMilliseconds(GetCurrentTimestamp(),
242 next_dump_time);
244 /* Perform a dump if it's time. */
245 if (delay_in_ms <= 0)
247 last_dump_time = GetCurrentTimestamp();
248 apw_dump_now(true, false);
249 continue;
252 /* Sleep until the next dump time. */
253 (void) WaitLatch(MyLatch,
254 WL_LATCH_SET | WL_TIMEOUT | WL_EXIT_ON_PM_DEATH,
255 delay_in_ms,
256 PG_WAIT_EXTENSION);
259 /* Reset the latch, loop. */
260 ResetLatch(MyLatch);
264 * Dump one last time. We assume this is probably the result of a system
265 * shutdown, although it's possible that we've merely been terminated.
267 if (final_dump_allowed)
268 apw_dump_now(true, true);
272 * Read the dump file and launch per-database workers one at a time to
273 * prewarm the buffers found there.
275 static void
276 apw_load_buffers(void)
278 FILE *file = NULL;
279 int num_elements,
281 BlockInfoRecord *blkinfo;
282 dsm_segment *seg;
285 * Skip the prewarm if the dump file is in use; otherwise, prevent any
286 * other process from writing it while we're using it.
288 LWLockAcquire(&apw_state->lock, LW_EXCLUSIVE);
289 if (apw_state->pid_using_dumpfile == InvalidPid)
290 apw_state->pid_using_dumpfile = MyProcPid;
291 else
293 LWLockRelease(&apw_state->lock);
294 ereport(LOG,
295 (errmsg("skipping prewarm because block dump file is being written by PID %d",
296 (int) apw_state->pid_using_dumpfile)));
297 return;
299 LWLockRelease(&apw_state->lock);
302 * Open the block dump file. Exit quietly if it doesn't exist, but report
303 * any other error.
305 file = AllocateFile(AUTOPREWARM_FILE, "r");
306 if (!file)
308 if (errno == ENOENT)
310 LWLockAcquire(&apw_state->lock, LW_EXCLUSIVE);
311 apw_state->pid_using_dumpfile = InvalidPid;
312 LWLockRelease(&apw_state->lock);
313 return; /* No file to load. */
315 ereport(ERROR,
316 (errcode_for_file_access(),
317 errmsg("could not read file \"%s\": %m",
318 AUTOPREWARM_FILE)));
321 /* First line of the file is a record count. */
322 if (fscanf(file, "<<%d>>\n", &num_elements) != 1)
323 ereport(ERROR,
324 (errcode_for_file_access(),
325 errmsg("could not read from file \"%s\": %m",
326 AUTOPREWARM_FILE)));
328 /* Allocate a dynamic shared memory segment to store the record data. */
329 seg = dsm_create(sizeof(BlockInfoRecord) * num_elements, 0);
330 blkinfo = (BlockInfoRecord *) dsm_segment_address(seg);
332 /* Read records, one per line. */
333 for (i = 0; i < num_elements; i++)
335 unsigned forknum;
337 if (fscanf(file, "%u,%u,%u,%u,%u\n", &blkinfo[i].database,
338 &blkinfo[i].tablespace, &blkinfo[i].filenumber,
339 &forknum, &blkinfo[i].blocknum) != 5)
340 ereport(ERROR,
341 (errmsg("autoprewarm block dump file is corrupted at line %d",
342 i + 1)));
343 blkinfo[i].forknum = forknum;
346 FreeFile(file);
348 /* Sort the blocks to be loaded. */
349 qsort(blkinfo, num_elements, sizeof(BlockInfoRecord),
350 apw_compare_blockinfo);
352 /* Populate shared memory state. */
353 apw_state->block_info_handle = dsm_segment_handle(seg);
354 apw_state->prewarm_start_idx = apw_state->prewarm_stop_idx = 0;
355 apw_state->prewarmed_blocks = 0;
357 /* Get the info position of the first block of the next database. */
358 while (apw_state->prewarm_start_idx < num_elements)
360 int j = apw_state->prewarm_start_idx;
361 Oid current_db = blkinfo[j].database;
364 * Advance the prewarm_stop_idx to the first BlockInfoRecord that does
365 * not belong to this database.
367 j++;
368 while (j < num_elements)
370 if (current_db != blkinfo[j].database)
373 * Combine BlockInfoRecords for global objects with those of
374 * the database.
376 if (current_db != InvalidOid)
377 break;
378 current_db = blkinfo[j].database;
381 j++;
385 * If we reach this point with current_db == InvalidOid, then only
386 * BlockInfoRecords belonging to global objects exist. We can't
387 * prewarm without a database connection, so just bail out.
389 if (current_db == InvalidOid)
390 break;
392 /* Configure stop point and database for next per-database worker. */
393 apw_state->prewarm_stop_idx = j;
394 apw_state->database = current_db;
395 Assert(apw_state->prewarm_start_idx < apw_state->prewarm_stop_idx);
397 /* If we've run out of free buffers, don't launch another worker. */
398 if (!have_free_buffer())
399 break;
402 * Likewise, don't launch if we've already been told to shut down.
403 * (The launch would fail anyway, but we might as well skip it.)
405 if (ShutdownRequestPending)
406 break;
409 * Start a per-database worker to load blocks for this database; this
410 * function will return once the per-database worker exits.
412 apw_start_database_worker();
414 /* Prepare for next database. */
415 apw_state->prewarm_start_idx = apw_state->prewarm_stop_idx;
418 /* Clean up. */
419 dsm_detach(seg);
420 LWLockAcquire(&apw_state->lock, LW_EXCLUSIVE);
421 apw_state->block_info_handle = DSM_HANDLE_INVALID;
422 apw_state->pid_using_dumpfile = InvalidPid;
423 LWLockRelease(&apw_state->lock);
425 /* Report our success, if we were able to finish. */
426 if (!ShutdownRequestPending)
427 ereport(LOG,
428 (errmsg("autoprewarm successfully prewarmed %d of %d previously-loaded blocks",
429 apw_state->prewarmed_blocks, num_elements)));
433 * Prewarm all blocks for one database (and possibly also global objects, if
434 * those got grouped with this database).
436 void
437 autoprewarm_database_main(Datum main_arg)
439 int pos;
440 BlockInfoRecord *block_info;
441 Relation rel = NULL;
442 BlockNumber nblocks = 0;
443 BlockInfoRecord *old_blk = NULL;
444 dsm_segment *seg;
446 /* Establish signal handlers; once that's done, unblock signals. */
447 pqsignal(SIGTERM, die);
448 BackgroundWorkerUnblockSignals();
450 /* Connect to correct database and get block information. */
451 apw_init_shmem();
452 seg = dsm_attach(apw_state->block_info_handle);
453 if (seg == NULL)
454 ereport(ERROR,
455 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
456 errmsg("could not map dynamic shared memory segment")));
457 BackgroundWorkerInitializeConnectionByOid(apw_state->database, InvalidOid, 0);
458 block_info = (BlockInfoRecord *) dsm_segment_address(seg);
459 pos = apw_state->prewarm_start_idx;
462 * Loop until we run out of blocks to prewarm or until we run out of free
463 * buffers.
465 while (pos < apw_state->prewarm_stop_idx && have_free_buffer())
467 BlockInfoRecord *blk = &block_info[pos++];
468 Buffer buf;
470 CHECK_FOR_INTERRUPTS();
473 * Quit if we've reached records for another database. If previous
474 * blocks are of some global objects, then continue pre-warming.
476 if (old_blk != NULL && old_blk->database != blk->database &&
477 old_blk->database != 0)
478 break;
481 * As soon as we encounter a block of a new relation, close the old
482 * relation. Note that rel will be NULL if try_relation_open failed
483 * previously; in that case, there is nothing to close.
485 if (old_blk != NULL && old_blk->filenumber != blk->filenumber &&
486 rel != NULL)
488 relation_close(rel, AccessShareLock);
489 rel = NULL;
490 CommitTransactionCommand();
494 * Try to open each new relation, but only once, when we first
495 * encounter it. If it's been dropped, skip the associated blocks.
497 if (old_blk == NULL || old_blk->filenumber != blk->filenumber)
499 Oid reloid;
501 Assert(rel == NULL);
502 StartTransactionCommand();
503 reloid = RelidByRelfilenumber(blk->tablespace, blk->filenumber);
504 if (OidIsValid(reloid))
505 rel = try_relation_open(reloid, AccessShareLock);
507 if (!rel)
508 CommitTransactionCommand();
510 if (!rel)
512 old_blk = blk;
513 continue;
516 /* Once per fork, check for fork existence and size. */
517 if (old_blk == NULL ||
518 old_blk->filenumber != blk->filenumber ||
519 old_blk->forknum != blk->forknum)
522 * smgrexists is not safe for illegal forknum, hence check whether
523 * the passed forknum is valid before using it in smgrexists.
525 if (blk->forknum > InvalidForkNumber &&
526 blk->forknum <= MAX_FORKNUM &&
527 smgrexists(RelationGetSmgr(rel), blk->forknum))
528 nblocks = RelationGetNumberOfBlocksInFork(rel, blk->forknum);
529 else
530 nblocks = 0;
533 /* Check whether blocknum is valid and within fork file size. */
534 if (blk->blocknum >= nblocks)
536 /* Move to next forknum. */
537 old_blk = blk;
538 continue;
541 /* Prewarm buffer. */
542 buf = ReadBufferExtended(rel, blk->forknum, blk->blocknum, RBM_NORMAL,
543 NULL);
544 if (BufferIsValid(buf))
546 apw_state->prewarmed_blocks++;
547 ReleaseBuffer(buf);
550 old_blk = blk;
553 dsm_detach(seg);
555 /* Release lock on previous relation. */
556 if (rel)
558 relation_close(rel, AccessShareLock);
559 CommitTransactionCommand();
564 * Dump information on blocks in shared buffers. We use a text format here
565 * so that it's easy to understand and even change the file contents if
566 * necessary.
567 * Returns the number of blocks dumped.
569 static int
570 apw_dump_now(bool is_bgworker, bool dump_unlogged)
572 int num_blocks;
573 int i;
574 int ret;
575 BlockInfoRecord *block_info_array;
576 BufferDesc *bufHdr;
577 FILE *file;
578 char transient_dump_file_path[MAXPGPATH];
579 pid_t pid;
581 LWLockAcquire(&apw_state->lock, LW_EXCLUSIVE);
582 pid = apw_state->pid_using_dumpfile;
583 if (apw_state->pid_using_dumpfile == InvalidPid)
584 apw_state->pid_using_dumpfile = MyProcPid;
585 LWLockRelease(&apw_state->lock);
587 if (pid != InvalidPid)
589 if (!is_bgworker)
590 ereport(ERROR,
591 (errmsg("could not perform block dump because dump file is being used by PID %d",
592 (int) apw_state->pid_using_dumpfile)));
594 ereport(LOG,
595 (errmsg("skipping block dump because it is already being performed by PID %d",
596 (int) apw_state->pid_using_dumpfile)));
597 return 0;
600 block_info_array =
601 (BlockInfoRecord *) palloc(sizeof(BlockInfoRecord) * NBuffers);
603 for (num_blocks = 0, i = 0; i < NBuffers; i++)
605 uint32 buf_state;
607 CHECK_FOR_INTERRUPTS();
609 bufHdr = GetBufferDescriptor(i);
611 /* Lock each buffer header before inspecting. */
612 buf_state = LockBufHdr(bufHdr);
615 * Unlogged tables will be automatically truncated after a crash or
616 * unclean shutdown. In such cases we need not prewarm them. Dump them
617 * only if requested by caller.
619 if (buf_state & BM_TAG_VALID &&
620 ((buf_state & BM_PERMANENT) || dump_unlogged))
622 block_info_array[num_blocks].database = bufHdr->tag.dbOid;
623 block_info_array[num_blocks].tablespace = bufHdr->tag.spcOid;
624 block_info_array[num_blocks].filenumber =
625 BufTagGetRelNumber(&bufHdr->tag);
626 block_info_array[num_blocks].forknum =
627 BufTagGetForkNum(&bufHdr->tag);
628 block_info_array[num_blocks].blocknum = bufHdr->tag.blockNum;
629 ++num_blocks;
632 UnlockBufHdr(bufHdr, buf_state);
635 snprintf(transient_dump_file_path, MAXPGPATH, "%s.tmp", AUTOPREWARM_FILE);
636 file = AllocateFile(transient_dump_file_path, "w");
637 if (!file)
638 ereport(ERROR,
639 (errcode_for_file_access(),
640 errmsg("could not open file \"%s\": %m",
641 transient_dump_file_path)));
643 ret = fprintf(file, "<<%d>>\n", num_blocks);
644 if (ret < 0)
646 int save_errno = errno;
648 FreeFile(file);
649 unlink(transient_dump_file_path);
650 errno = save_errno;
651 ereport(ERROR,
652 (errcode_for_file_access(),
653 errmsg("could not write to file \"%s\": %m",
654 transient_dump_file_path)));
657 for (i = 0; i < num_blocks; i++)
659 CHECK_FOR_INTERRUPTS();
661 ret = fprintf(file, "%u,%u,%u,%u,%u\n",
662 block_info_array[i].database,
663 block_info_array[i].tablespace,
664 block_info_array[i].filenumber,
665 (uint32) block_info_array[i].forknum,
666 block_info_array[i].blocknum);
667 if (ret < 0)
669 int save_errno = errno;
671 FreeFile(file);
672 unlink(transient_dump_file_path);
673 errno = save_errno;
674 ereport(ERROR,
675 (errcode_for_file_access(),
676 errmsg("could not write to file \"%s\": %m",
677 transient_dump_file_path)));
681 pfree(block_info_array);
684 * Rename transient_dump_file_path to AUTOPREWARM_FILE to make things
685 * permanent.
687 ret = FreeFile(file);
688 if (ret != 0)
690 int save_errno = errno;
692 unlink(transient_dump_file_path);
693 errno = save_errno;
694 ereport(ERROR,
695 (errcode_for_file_access(),
696 errmsg("could not close file \"%s\": %m",
697 transient_dump_file_path)));
700 (void) durable_rename(transient_dump_file_path, AUTOPREWARM_FILE, ERROR);
701 apw_state->pid_using_dumpfile = InvalidPid;
703 ereport(DEBUG1,
704 (errmsg_internal("wrote block details for %d blocks", num_blocks)));
705 return num_blocks;
709 * SQL-callable function to launch autoprewarm.
711 Datum
712 autoprewarm_start_worker(PG_FUNCTION_ARGS)
714 pid_t pid;
716 if (!autoprewarm)
717 ereport(ERROR,
718 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
719 errmsg("autoprewarm is disabled")));
721 apw_init_shmem();
722 LWLockAcquire(&apw_state->lock, LW_EXCLUSIVE);
723 pid = apw_state->bgworker_pid;
724 LWLockRelease(&apw_state->lock);
726 if (pid != InvalidPid)
727 ereport(ERROR,
728 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
729 errmsg("autoprewarm worker is already running under PID %d",
730 (int) pid)));
732 apw_start_leader_worker();
734 PG_RETURN_VOID();
738 * SQL-callable function to perform an immediate block dump.
740 * Note: this is declared to return int8, as insurance against some
741 * very distant day when we might make NBuffers wider than int.
743 Datum
744 autoprewarm_dump_now(PG_FUNCTION_ARGS)
746 int num_blocks;
748 apw_init_shmem();
750 PG_ENSURE_ERROR_CLEANUP(apw_detach_shmem, 0);
752 num_blocks = apw_dump_now(false, true);
754 PG_END_ENSURE_ERROR_CLEANUP(apw_detach_shmem, 0);
756 PG_RETURN_INT64((int64) num_blocks);
759 static void
760 apw_init_state(void *ptr)
762 AutoPrewarmSharedState *state = (AutoPrewarmSharedState *) ptr;
764 LWLockInitialize(&state->lock, LWLockNewTrancheId());
765 state->bgworker_pid = InvalidPid;
766 state->pid_using_dumpfile = InvalidPid;
770 * Allocate and initialize autoprewarm related shared memory, if not already
771 * done, and set up backend-local pointer to that state. Returns true if an
772 * existing shared memory segment was found.
774 static bool
775 apw_init_shmem(void)
777 bool found;
779 apw_state = GetNamedDSMSegment("autoprewarm",
780 sizeof(AutoPrewarmSharedState),
781 apw_init_state,
782 &found);
783 LWLockRegisterTranche(apw_state->lock.tranche, "autoprewarm");
785 return found;
789 * Clear our PID from autoprewarm shared state.
791 static void
792 apw_detach_shmem(int code, Datum arg)
794 LWLockAcquire(&apw_state->lock, LW_EXCLUSIVE);
795 if (apw_state->pid_using_dumpfile == MyProcPid)
796 apw_state->pid_using_dumpfile = InvalidPid;
797 if (apw_state->bgworker_pid == MyProcPid)
798 apw_state->bgworker_pid = InvalidPid;
799 LWLockRelease(&apw_state->lock);
803 * Start autoprewarm leader worker process.
805 static void
806 apw_start_leader_worker(void)
808 BackgroundWorker worker;
809 BackgroundWorkerHandle *handle;
810 BgwHandleStatus status;
811 pid_t pid;
813 MemSet(&worker, 0, sizeof(BackgroundWorker));
814 worker.bgw_flags = BGWORKER_SHMEM_ACCESS;
815 worker.bgw_start_time = BgWorkerStart_ConsistentState;
816 strcpy(worker.bgw_library_name, "pg_prewarm");
817 strcpy(worker.bgw_function_name, "autoprewarm_main");
818 strcpy(worker.bgw_name, "autoprewarm leader");
819 strcpy(worker.bgw_type, "autoprewarm leader");
821 if (process_shared_preload_libraries_in_progress)
823 RegisterBackgroundWorker(&worker);
824 return;
827 /* must set notify PID to wait for startup */
828 worker.bgw_notify_pid = MyProcPid;
830 if (!RegisterDynamicBackgroundWorker(&worker, &handle))
831 ereport(ERROR,
832 (errcode(ERRCODE_INSUFFICIENT_RESOURCES),
833 errmsg("could not register background process"),
834 errhint("You may need to increase \"max_worker_processes\".")));
836 status = WaitForBackgroundWorkerStartup(handle, &pid);
837 if (status != BGWH_STARTED)
838 ereport(ERROR,
839 (errcode(ERRCODE_INSUFFICIENT_RESOURCES),
840 errmsg("could not start background process"),
841 errhint("More details may be available in the server log.")));
845 * Start autoprewarm per-database worker process.
847 static void
848 apw_start_database_worker(void)
850 BackgroundWorker worker;
851 BackgroundWorkerHandle *handle;
853 MemSet(&worker, 0, sizeof(BackgroundWorker));
854 worker.bgw_flags =
855 BGWORKER_SHMEM_ACCESS | BGWORKER_BACKEND_DATABASE_CONNECTION;
856 worker.bgw_start_time = BgWorkerStart_ConsistentState;
857 worker.bgw_restart_time = BGW_NEVER_RESTART;
858 strcpy(worker.bgw_library_name, "pg_prewarm");
859 strcpy(worker.bgw_function_name, "autoprewarm_database_main");
860 strcpy(worker.bgw_name, "autoprewarm worker");
861 strcpy(worker.bgw_type, "autoprewarm worker");
863 /* must set notify PID to wait for shutdown */
864 worker.bgw_notify_pid = MyProcPid;
866 if (!RegisterDynamicBackgroundWorker(&worker, &handle))
867 ereport(ERROR,
868 (errcode(ERRCODE_INSUFFICIENT_RESOURCES),
869 errmsg("registering dynamic bgworker autoprewarm failed"),
870 errhint("Consider increasing the configuration parameter \"%s\".", "max_worker_processes")));
873 * Ignore return value; if it fails, postmaster has died, but we have
874 * checks for that elsewhere.
876 WaitForBackgroundWorkerShutdown(handle);
879 /* Compare member elements to check whether they are not equal. */
880 #define cmp_member_elem(fld) \
881 do { \
882 if (a->fld < b->fld) \
883 return -1; \
884 else if (a->fld > b->fld) \
885 return 1; \
886 } while(0)
889 * apw_compare_blockinfo
891 * We depend on all records for a particular database being consecutive
892 * in the dump file; each per-database worker will preload blocks until
893 * it sees a block for some other database. Sorting by tablespace,
894 * filenumber, forknum, and blocknum isn't critical for correctness, but
895 * helps us get a sequential I/O pattern.
897 static int
898 apw_compare_blockinfo(const void *p, const void *q)
900 const BlockInfoRecord *a = (const BlockInfoRecord *) p;
901 const BlockInfoRecord *b = (const BlockInfoRecord *) q;
903 cmp_member_elem(database);
904 cmp_member_elem(tablespace);
905 cmp_member_elem(filenumber);
906 cmp_member_elem(forknum);
907 cmp_member_elem(blocknum);
909 return 0;