58 #define AUTOPREWARM_FILE "autoprewarm.blocks"
92 static int apw_dump_now(
bool is_bgworker,
bool dump_unlogged);
115 "Sets the interval between dumps of shared buffers",
116 "If set to zero, time-based dumping is disabled.",
131 "Starts the autoprewarm worker.",
170 bool first_time =
true;
171 bool final_dump_allowed =
true;
196 (
errmsg(
"autoprewarm worker is already running under PID %d",
256 if (delay_in_ms <= 0)
278 if (final_dump_allowed)
306 (
errmsg(
"skipping prewarm because block dump file is being written by PID %d",
328 errmsg(
"could not read file \"%s\": %m",
333 if (fscanf(file,
"<<%d>>\n", &num_elements) != 1)
336 errmsg(
"could not read from file \"%s\": %m",
344 for (
i = 0;
i < num_elements;
i++)
348 if (fscanf(file,
"%u,%u,%u,%u,%u\n", &blkinfo[
i].database,
350 &forknum, &blkinfo[
i].blocknum) != 5)
352 (
errmsg(
"autoprewarm block dump file is corrupted at line %d",
379 while (
j < num_elements)
381 if (current_db != blkinfo[
j].database)
439 (
errmsg(
"autoprewarm successfully prewarmed %d of %d previously-loaded blocks",
466 (
errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
467 errmsg(
"could not map dynamic shared memory segment")));
528 if (old_blk == NULL ||
589 char transient_dump_file_path[
MAXPGPATH];
602 (
errmsg(
"could not perform block dump because dump file is being used by PID %d",
606 (
errmsg(
"skipping block dump because it is already being performed by PID %d",
637 block_info_array[num_blocks].
forknum =
651 errmsg(
"could not open file \"%s\": %m",
652 transient_dump_file_path)));
654 ret =
fprintf(file,
"<<%d>>\n", num_blocks);
657 int save_errno = errno;
660 unlink(transient_dump_file_path);
664 errmsg(
"could not write to file \"%s\": %m",
665 transient_dump_file_path)));
668 for (
i = 0;
i < num_blocks;
i++)
672 ret =
fprintf(file,
"%u,%u,%u,%u,%u\n",
673 block_info_array[
i].database,
675 block_info_array[
i].filenumber,
676 (
uint32) block_info_array[
i].forknum,
677 block_info_array[
i].blocknum);
680 int save_errno = errno;
683 unlink(transient_dump_file_path);
687 errmsg(
"could not write to file \"%s\": %m",
688 transient_dump_file_path)));
692 pfree(block_info_array);
701 int save_errno = errno;
703 unlink(transient_dump_file_path);
707 errmsg(
"could not close file \"%s\": %m",
708 transient_dump_file_path)));
729 (
errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
730 errmsg(
"autoprewarm is disabled")));
739 (
errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
740 errmsg(
"autoprewarm worker is already running under PID %d",
828 strcpy(worker.
bgw_name,
"autoprewarm leader");
829 strcpy(worker.
bgw_type,
"autoprewarm leader");
842 (
errcode(ERRCODE_INSUFFICIENT_RESOURCES),
843 errmsg(
"could not register background process"),
844 errhint(
"You may need to increase max_worker_processes.")));
849 (
errcode(ERRCODE_INSUFFICIENT_RESOURCES),
850 errmsg(
"could not start background process"),
851 errhint(
"More details may be available in the server log.")));
870 strcpy(worker.
bgw_name,
"autoprewarm worker");
871 strcpy(worker.
bgw_type,
"autoprewarm worker");
878 (
errcode(ERRCODE_INSUFFICIENT_RESOURCES),
879 errmsg(
"registering dynamic bgworker autoprewarm failed"),
880 errhint(
"Consider increasing configuration parameter \"max_worker_processes\".")));
890 #define cmp_member_elem(fld) \
892 if (a->fld < b->fld) \
894 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)
static void autoprewarm_shmem_request(void)
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)
static shmem_request_hook_type prev_shmem_request_hook
PGDLLEXPORT void autoprewarm_database_main(Datum main_arg)
static void apw_start_database_worker(void)
static void apw_load_buffers(void)
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)
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 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
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 on_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 RequestAddinShmemSpace(Size size)
void ResetLatch(Latch *latch)
int WaitLatch(Latch *latch, int wakeEvents, long timeout, uint32 wait_event_info)
#define WL_EXIT_ON_PM_DEATH
Assert(fmt[strlen(fmt) - 1] !='\n')
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()
void(* shmem_request_hook_type)(void)
shmem_request_hook_type shmem_request_hook
bool process_shared_preload_libraries_in_progress
pqsigfunc pqsignal(int signo, pqsigfunc func)
void pg_qsort(void *base, size_t nel, size_t elsize, int(*cmp)(const void *, const void *))
void BackgroundWorkerUnblockSignals(void)
void BackgroundWorkerInitializeConnectionByOid(Oid dboid, Oid useroid, uint32 flags)
void procsignal_sigusr1_handler(SIGNAL_ARGS)
static SMgrRelation RelationGetSmgr(Relation rel)
Oid RelidByRelfilenumber(Oid reltablespace, RelFileNumber relfilenumber)
void * ShmemInitStruct(const char *name, Size size, bool *foundPtr)
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)
void StartTransactionCommand(void)
void CommitTransactionCommand(void)