1 /*-------------------------------------------------------------------------
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
22 * contrib/pg_prewarm/autoprewarm.c
24 *-------------------------------------------------------------------------
31 #include "access/relation.h"
32 #include "access/xact.h"
33 #include "catalog/pg_class.h"
34 #include "catalog/pg_type.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
65 RelFileNumber filenumber
;
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
;
80 int prewarm_start_idx
;
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
;
103 static bool autoprewarm
= true; /* start worker? */
104 static int autoprewarm_interval
= 300; /* dump interval */
107 * Module load callback.
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
,
124 if (!process_shared_preload_libraries_in_progress
)
127 /* can't define PGC_POSTMASTER variable after startup */
128 DefineCustomBoolVariable("pg_prewarm.autoprewarm",
129 "Starts the autoprewarm worker.",
139 MarkGUCPrefixReserved("pg_prewarm");
141 /* Register autoprewarm worker, if enabled. */
143 apw_start_leader_worker();
147 * Main entry point for the leader autoprewarm process. Per-database workers
148 * have a separate entry point.
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())
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
);
185 (errmsg("autoprewarm worker is already running under PID %d",
186 (int) apw_state
->bgworker_pid
)));
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.
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
,
233 TimestampTz next_dump_time
;
236 /* Compute the next dump time. */
238 TimestampTzPlusMilliseconds(last_dump_time
,
239 autoprewarm_interval
* 1000);
241 TimestampDifferenceMilliseconds(GetCurrentTimestamp(),
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);
252 /* Sleep until the next dump time. */
253 (void) WaitLatch(MyLatch
,
254 WL_LATCH_SET
| WL_TIMEOUT
| WL_EXIT_ON_PM_DEATH
,
259 /* Reset the latch, loop. */
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.
276 apw_load_buffers(void)
281 BlockInfoRecord
*blkinfo
;
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
;
293 LWLockRelease(&apw_state
->lock
);
295 (errmsg("skipping prewarm because block dump file is being written by PID %d",
296 (int) apw_state
->pid_using_dumpfile
)));
299 LWLockRelease(&apw_state
->lock
);
302 * Open the block dump file. Exit quietly if it doesn't exist, but report
305 file
= AllocateFile(AUTOPREWARM_FILE
, "r");
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. */
316 (errcode_for_file_access(),
317 errmsg("could not read file \"%s\": %m",
321 /* First line of the file is a record count. */
322 if (fscanf(file
, "<<%d>>\n", &num_elements
) != 1)
324 (errcode_for_file_access(),
325 errmsg("could not read from file \"%s\": %m",
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
++)
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)
341 (errmsg("autoprewarm block dump file is corrupted at line %d",
343 blkinfo
[i
].forknum
= forknum
;
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.
368 while (j
< num_elements
)
370 if (current_db
!= blkinfo
[j
].database
)
373 * Combine BlockInfoRecords for global objects with those of
376 if (current_db
!= InvalidOid
)
378 current_db
= blkinfo
[j
].database
;
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
)
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())
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
)
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
;
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
)
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).
437 autoprewarm_database_main(Datum main_arg
)
440 BlockInfoRecord
*block_info
;
442 BlockNumber nblocks
= 0;
443 BlockInfoRecord
*old_blk
= NULL
;
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. */
452 seg
= dsm_attach(apw_state
->block_info_handle
);
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
465 while (pos
< apw_state
->prewarm_stop_idx
&& have_free_buffer())
467 BlockInfoRecord
*blk
= &block_info
[pos
++];
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)
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
&&
488 relation_close(rel
, AccessShareLock
);
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
)
502 StartTransactionCommand();
503 reloid
= RelidByRelfilenumber(blk
->tablespace
, blk
->filenumber
);
504 if (OidIsValid(reloid
))
505 rel
= try_relation_open(reloid
, AccessShareLock
);
508 CommitTransactionCommand();
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
);
533 /* Check whether blocknum is valid and within fork file size. */
534 if (blk
->blocknum
>= nblocks
)
536 /* Move to next forknum. */
541 /* Prewarm buffer. */
542 buf
= ReadBufferExtended(rel
, blk
->forknum
, blk
->blocknum
, RBM_NORMAL
,
544 if (BufferIsValid(buf
))
546 apw_state
->prewarmed_blocks
++;
555 /* Release lock on previous relation. */
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
567 * Returns the number of blocks dumped.
570 apw_dump_now(bool is_bgworker
, bool dump_unlogged
)
575 BlockInfoRecord
*block_info_array
;
578 char transient_dump_file_path
[MAXPGPATH
];
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
)
591 (errmsg("could not perform block dump because dump file is being used by PID %d",
592 (int) apw_state
->pid_using_dumpfile
)));
595 (errmsg("skipping block dump because it is already being performed by PID %d",
596 (int) apw_state
->pid_using_dumpfile
)));
601 (BlockInfoRecord
*) palloc(sizeof(BlockInfoRecord
) * NBuffers
);
603 for (num_blocks
= 0, i
= 0; i
< NBuffers
; i
++)
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
;
632 UnlockBufHdr(bufHdr
, buf_state
);
635 snprintf(transient_dump_file_path
, MAXPGPATH
, "%s.tmp", AUTOPREWARM_FILE
);
636 file
= AllocateFile(transient_dump_file_path
, "w");
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
);
646 int save_errno
= errno
;
649 unlink(transient_dump_file_path
);
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
);
669 int save_errno
= errno
;
672 unlink(transient_dump_file_path
);
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
687 ret
= FreeFile(file
);
690 int save_errno
= errno
;
692 unlink(transient_dump_file_path
);
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
;
704 (errmsg_internal("wrote block details for %d blocks", num_blocks
)));
709 * SQL-callable function to launch autoprewarm.
712 autoprewarm_start_worker(PG_FUNCTION_ARGS
)
718 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE
),
719 errmsg("autoprewarm is disabled")));
722 LWLockAcquire(&apw_state
->lock
, LW_EXCLUSIVE
);
723 pid
= apw_state
->bgworker_pid
;
724 LWLockRelease(&apw_state
->lock
);
726 if (pid
!= InvalidPid
)
728 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE
),
729 errmsg("autoprewarm worker is already running under PID %d",
732 apw_start_leader_worker();
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.
744 autoprewarm_dump_now(PG_FUNCTION_ARGS
)
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
);
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.
779 apw_state
= GetNamedDSMSegment("autoprewarm",
780 sizeof(AutoPrewarmSharedState
),
783 LWLockRegisterTranche(apw_state
->lock
.tranche
, "autoprewarm");
789 * Clear our PID from autoprewarm shared state.
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.
806 apw_start_leader_worker(void)
808 BackgroundWorker worker
;
809 BackgroundWorkerHandle
*handle
;
810 BgwHandleStatus status
;
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
);
827 /* must set notify PID to wait for startup */
828 worker
.bgw_notify_pid
= MyProcPid
;
830 if (!RegisterDynamicBackgroundWorker(&worker
, &handle
))
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
)
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.
848 apw_start_database_worker(void)
850 BackgroundWorker worker
;
851 BackgroundWorkerHandle
*handle
;
853 MemSet(&worker
, 0, sizeof(BackgroundWorker
));
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
))
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) \
882 if (a->fld < b->fld) \
884 else if (a->fld > b->fld) \
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.
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
);