51 #define AUTOPREWARM_FILE "autoprewarm.blocks"
85 static int apw_dump_now(
bool is_bgworker,
bool dump_unlogged);
106 "Sets the interval between dumps of shared buffers",
107 "If set to zero, time-based dumping is disabled.",
122 "Starts the autoprewarm worker.",
146 bool first_time =
true;
147 bool final_dump_allowed =
true;
178 (
errmsg(
"autoprewarm worker is already running under PID %d",
238 if (delay_in_ms <= 0)
260 if (final_dump_allowed)
288 (
errmsg(
"skipping prewarm because block dump file is being written by PID %d",
310 errmsg(
"could not read file \"%s\": %m",
315 if (fscanf(file,
"<<%d>>\n", &num_elements) != 1)
318 errmsg(
"could not read from file \"%s\": %m",
326 for (
i = 0;
i < num_elements;
i++)
330 if (fscanf(file,
"%u,%u,%u,%u,%u\n", &blkinfo[
i].database,
332 &forknum, &blkinfo[
i].blocknum) != 5)
334 (
errmsg(
"autoprewarm block dump file is corrupted at line %d",
361 while (
j < num_elements)
363 if (current_db != blkinfo[
j].database)
421 (
errmsg(
"autoprewarm successfully prewarmed %d of %d previously-loaded blocks",
448 (
errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
449 errmsg(
"could not map dynamic shared memory segment")));
510 if (old_blk == NULL ||
571 char transient_dump_file_path[
MAXPGPATH];
584 (
errmsg(
"could not perform block dump because dump file is being used by PID %d",
588 (
errmsg(
"skipping block dump because it is already being performed by PID %d",
619 block_info_array[num_blocks].
forknum =
633 errmsg(
"could not open file \"%s\": %m",
634 transient_dump_file_path)));
636 ret =
fprintf(file,
"<<%d>>\n", num_blocks);
639 int save_errno = errno;
642 unlink(transient_dump_file_path);
646 errmsg(
"could not write to file \"%s\": %m",
647 transient_dump_file_path)));
650 for (
i = 0;
i < num_blocks;
i++)
654 ret =
fprintf(file,
"%u,%u,%u,%u,%u\n",
655 block_info_array[
i].database,
657 block_info_array[
i].filenumber,
658 (
uint32) block_info_array[
i].forknum,
659 block_info_array[
i].blocknum);
662 int save_errno = errno;
665 unlink(transient_dump_file_path);
669 errmsg(
"could not write to file \"%s\": %m",
670 transient_dump_file_path)));
674 pfree(block_info_array);
683 int save_errno = errno;
685 unlink(transient_dump_file_path);
689 errmsg(
"could not close file \"%s\": %m",
690 transient_dump_file_path)));
711 (
errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
712 errmsg(
"autoprewarm is disabled")));
721 (
errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
722 errmsg(
"autoprewarm worker is already running under PID %d",
811 strcpy(worker.
bgw_name,
"autoprewarm leader");
812 strcpy(worker.
bgw_type,
"autoprewarm leader");
825 (
errcode(ERRCODE_INSUFFICIENT_RESOURCES),
826 errmsg(
"could not register background process"),
827 errhint(
"You may need to increase \"max_worker_processes\".")));
832 (
errcode(ERRCODE_INSUFFICIENT_RESOURCES),
833 errmsg(
"could not start background process"),
834 errhint(
"More details may be available in the server log.")));
853 strcpy(worker.
bgw_name,
"autoprewarm worker");
854 strcpy(worker.
bgw_type,
"autoprewarm worker");
861 (
errcode(ERRCODE_INSUFFICIENT_RESOURCES),
862 errmsg(
"registering dynamic bgworker autoprewarm failed"),
863 errhint(
"Consider increasing the configuration parameter \"%s\".",
"max_worker_processes")));
873 #define cmp_member_elem(fld) \
875 if (a->fld < b->fld) \
877 else if (a->fld > b->fld) \
static int apw_compare_blockinfo(const void *p, const void *q)
Datum autoprewarm_start_worker(PG_FUNCTION_ARGS)
static AutoPrewarmSharedState * apw_state
static void apw_detach_shmem(int code, Datum arg)
PGDLLEXPORT void autoprewarm_main(Datum main_arg)
#define cmp_member_elem(fld)
static bool apw_init_shmem(void)
static void apw_start_leader_worker(void)
PGDLLEXPORT void autoprewarm_database_main(Datum main_arg)
static void apw_start_database_worker(void)
static void apw_load_buffers(void)
static void apw_init_state(void *ptr)
Datum autoprewarm_dump_now(PG_FUNCTION_ARGS)
static int autoprewarm_interval
static int apw_dump_now(bool is_bgworker, bool dump_unlogged)
struct AutoPrewarmSharedState AutoPrewarmSharedState
PG_FUNCTION_INFO_V1(autoprewarm_start_worker)
struct BlockInfoRecord BlockInfoRecord
long TimestampDifferenceMilliseconds(TimestampTz start_time, TimestampTz stop_time)
TimestampTz GetCurrentTimestamp(void)
void RegisterBackgroundWorker(BackgroundWorker *worker)
BgwHandleStatus WaitForBackgroundWorkerStartup(BackgroundWorkerHandle *handle, pid_t *pidp)
BgwHandleStatus WaitForBackgroundWorkerShutdown(BackgroundWorkerHandle *handle)
void BackgroundWorkerUnblockSignals(void)
void BackgroundWorkerInitializeConnectionByOid(Oid dboid, Oid useroid, uint32 flags)
bool RegisterDynamicBackgroundWorker(BackgroundWorker *worker, BackgroundWorkerHandle **handle)
#define BGW_NEVER_RESTART
@ BgWorkerStart_ConsistentState
#define BGWORKER_BACKEND_DATABASE_CONNECTION
#define BGWORKER_SHMEM_ACCESS
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)
void ReleaseBuffer(Buffer buffer)
Buffer ReadBufferExtended(Relation reln, ForkNumber forkNum, BlockNumber blockNum, ReadBufferMode mode, BufferAccessStrategy strategy)
uint32 LockBufHdr(BufferDesc *desc)
static bool BufferIsValid(Buffer bufnum)
#define Assert(condition)
#define MemSet(start, val, len)
#define OidIsValid(objectId)
dsm_handle dsm_segment_handle(dsm_segment *seg)
void * dsm_segment_address(dsm_segment *seg)
void dsm_detach(dsm_segment *seg)
dsm_segment * dsm_attach(dsm_handle h)
dsm_segment * dsm_create(Size size, int flags)
#define DSM_HANDLE_INVALID
void * GetNamedDSMSegment(const char *name, size_t size, void(*init_callback)(void *ptr), bool *found)
int errmsg_internal(const char *fmt,...)
int errcode_for_file_access(void)
int errhint(const char *fmt,...)
int errcode(int sqlerrcode)
int errmsg(const char *fmt,...)
#define ereport(elevel,...)
FILE * AllocateFile(const char *name, const char *mode)
int durable_rename(const char *oldfile, const char *newfile, int elevel)
#define PG_RETURN_INT64(x)
bool have_free_buffer(void)
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)
void MarkGUCPrefixReserved(const char *className)
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)
void ProcessConfigFile(GucContext context)
void SignalHandlerForShutdownRequest(SIGNAL_ARGS)
volatile sig_atomic_t ShutdownRequestPending
volatile sig_atomic_t ConfigReloadPending
void SignalHandlerForConfigReload(SIGNAL_ARGS)
void before_shmem_exit(pg_on_exit_callback function, Datum arg)
#define PG_ENSURE_ERROR_CLEANUP(cleanup_function, arg)
#define PG_END_ENSURE_ERROR_CLEANUP(cleanup_function, arg)
void ResetLatch(Latch *latch)
int WaitLatch(Latch *latch, int wakeEvents, long timeout, uint32 wait_event_info)
#define WL_EXIT_ON_PM_DEATH
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
void LWLockRegisterTranche(int tranche_id, const char *tranche_name)
int LWLockNewTrancheId(void)
void LWLockRelease(LWLock *lock)
void LWLockInitialize(LWLock *lock, int tranche_id)
void pfree(void *pointer)
#define CHECK_FOR_INTERRUPTS()
bool process_shared_preload_libraries_in_progress
pqsigfunc pqsignal(int signo, pqsigfunc func)
#define qsort(a, b, c, d)
void procsignal_sigusr1_handler(SIGNAL_ARGS)
static SMgrRelation RelationGetSmgr(Relation rel)
Oid RelidByRelfilenumber(Oid reltablespace, RelFileNumber relfilenumber)
bool smgrexists(SMgrRelation reln, ForkNumber forknum)
void relation_close(Relation relation, LOCKMODE lockmode)
Relation try_relation_open(Oid relationId, LOCKMODE lockmode)
dsm_handle block_info_handle
char bgw_function_name[BGW_MAXLEN]
char bgw_name[BGW_MAXLEN]
char bgw_type[BGW_MAXLEN]
BgWorkerStartTime bgw_start_time
char bgw_library_name[MAXPGPATH]
#define TimestampTzPlusMilliseconds(tz, ms)
#define PG_WAIT_EXTENSION
void StartTransactionCommand(void)
void CommitTransactionCommand(void)