From ce5d628597e7f2b6f1911b348f5db020ddc1a891 Mon Sep 17 00:00:00 2001 From: RekGRpth Date: Wed, 11 Dec 2024 08:37:30 +0500 Subject: [PATCH 01/17] pg18 --- pg_wait_sampling.c | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/pg_wait_sampling.c b/pg_wait_sampling.c index 068b553..7a09283 100644 --- a/pg_wait_sampling.c +++ b/pg_wait_sampling.c @@ -76,7 +76,11 @@ static PlannedStmt *pgws_planner_hook(Query *parse, static void pgws_ExecutorStart(QueryDesc *queryDesc, int eflags); static void pgws_ExecutorRun(QueryDesc *queryDesc, ScanDirection direction, - uint64 count, bool execute_once); + uint64 count +#if PG_VERSION_NUM >= 100000 && PG_VERSION_NUM < 180000 + , bool execute_once +#endif + ); static void pgws_ExecutorFinish(QueryDesc *queryDesc); static void pgws_ExecutorEnd(QueryDesc *queryDesc); static void pgws_ProcessUtility(PlannedStmt *pstmt, @@ -1036,7 +1040,11 @@ pgws_ExecutorStart(QueryDesc *queryDesc, int eflags) static void pgws_ExecutorRun(QueryDesc *queryDesc, ScanDirection direction, - uint64 count, bool execute_once) + uint64 count +#if PG_VERSION_NUM >= 100000 && PG_VERSION_NUM < 180000 + , bool execute_once +#endif + ) { int i = MyProc - ProcGlobal->allProcs; uint64 save_queryId = pgws_proc_queryids[i]; @@ -1045,9 +1053,17 @@ pgws_ExecutorRun(QueryDesc *queryDesc, PG_TRY(); { if (prev_ExecutorRun) +#if PG_VERSION_NUM >= 100000 && PG_VERSION_NUM < 180000 prev_ExecutorRun(queryDesc, direction, count, execute_once); +#else + prev_ExecutorRun(queryDesc, direction, count); +#endif else +#if PG_VERSION_NUM >= 100000 && PG_VERSION_NUM < 180000 standard_ExecutorRun(queryDesc, direction, count, execute_once); +#else + standard_ExecutorRun(queryDesc, direction, count); +#endif nesting_level--; if (nesting_level == 0) pgws_proc_queryids[i] = UINT64CONST(0); From c3229585e74f90a61f9f2767d8e2d06e8d84bc53 Mon Sep 17 00:00:00 2001 From: Sergey Shinderuk Date: Thu, 12 Dec 2024 23:37:32 +0300 Subject: [PATCH 02/17] Build with PostgreSQL 18devel in Travis --- .travis.yml | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/.travis.yml b/.travis.yml index 1fce04a..2c63eff 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,16 +1,30 @@ dist: jammy language: c env: -- PG_MAJOR=17 BETA=1 +- PG_MAJOR=18 SNAPSHOT=1 +- PG_MAJOR=17 - PG_MAJOR=16 - PG_MAJOR=15 - PG_MAJOR=14 - PG_MAJOR=13 -- PG_MAJOR=12 before_script: - curl https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add - -- echo "deb https://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" | sudo tee -a /etc/apt/sources.list -- if [ -n "${BETA}" ]; then echo "deb https://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main ${PG_MAJOR}" | sudo tee -a /etc/apt/sources.list; fi +- | + if [ -n "${SNAPSHOT}" ]; then + echo "deb https://apt.postgresql.org/pub/repos/apt/ $(lsb_release -cs)-pgdg-snapshot main ${PG_MAJOR}" + elif [ -n "${BETA}" ]; then + echo "deb https://apt.postgresql.org/pub/repos/apt/ $(lsb_release -cs)-pgdg main ${PG_MAJOR}" + else + echo "deb https://apt.postgresql.org/pub/repos/apt/ $(lsb_release -cs)-pgdg main" + fi | sudo tee /etc/apt/sources.list.d/postgresql.list +- | + if [ -n "${SNAPSHOT}" ]; then + { + echo "Package: *" + echo "Pin: origin apt.postgresql.org" + echo "Pin-Priority: 600" + } | sudo tee /etc/apt/preferences.d/pgdg.pref + fi - sudo apt-get update - sudo systemctl stop postgresql - sudo apt-get install -y --no-install-recommends postgresql-client-${PG_MAJOR} postgresql-${PG_MAJOR} postgresql-server-dev-${PG_MAJOR} From 36f59d2ad3eed4db882a7f04b586826b77c49e03 Mon Sep 17 00:00:00 2001 From: Zharkov Roman Date: Tue, 21 Jan 2025 16:19:53 +0300 Subject: [PATCH 03/17] Add meson.build file to support building from the contrib source tree. --- meson.build | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 meson.build diff --git a/meson.build b/meson.build new file mode 100644 index 0000000..c3c3dc9 --- /dev/null +++ b/meson.build @@ -0,0 +1,41 @@ +# Copyright (c) 2025, Postgres Professional + +# Does not support the PGXS infrastructure at this time. Please, compile as part +# of the contrib source tree. + +pg_wait_sampling_sources = files( + 'collector.c', + 'pg_wait_sampling.c', +) + +if host_system == 'windows' + pg_wait_sampling_sources += rc_lib_gen.process(win32ver_rc, extra_args: [ + '--NAME', 'pg_wait_sampling', + '--FILEDESC', 'pg_wait_sampling - provides information about the current wait events for particular processes.',]) +endif + +pg_wait_sampling = shared_module('pg_wait_sampling', + pg_wait_sampling_sources, + kwargs: contrib_mod_args, +) +contrib_targets += pg_wait_sampling + +install_data( + 'pg_wait_sampling.control', + 'pg_wait_sampling--1.0--1.1.sql', + 'pg_wait_sampling--1.1.sql', + kwargs: contrib_data_args, +) + +tests += { + 'name': 'pg_wait_sampling', + 'sd': meson.current_source_dir(), + 'bd': meson.current_build_dir(), + 'regress': { + 'sql': [ + 'load', + 'queries', + ], + 'regress_args': ['--temp-config', files('conf.add')], + }, +} From 8f6f7e6a885e23f17dd53faf6dfec9829be33a3d Mon Sep 17 00:00:00 2001 From: Oleg Tselebrovskiy Date: Tue, 28 Jan 2025 14:31:30 +0700 Subject: [PATCH 04/17] Store GUC variables in local process memory to avoid IPC shenanigans For more context and reasoning see issue #85 --- README.md | 6 +- collector.c | 30 +++--- compat.h | 14 --- pg_wait_sampling.c | 248 ++++++++++++++++----------------------------- pg_wait_sampling.h | 14 +-- 5 files changed, 118 insertions(+), 194 deletions(-) diff --git a/README.md b/README.md index 47fb882..5c123e0 100644 --- a/README.md +++ b/README.md @@ -158,9 +158,9 @@ If `pg_wait_sampling.sample_cpu` is set to true then processes that are not waiting on anything are also sampled. The wait event columns for such processes will be NULL. -These GUCs are allowed to be changed by superuser. Also, they are placed into -shared memory. Thus, they could be changed from any backend and affects worker -runtime. +Values of these GUC variables can be changed only in config file or with ALTER SYSTEM. +Then you need to reload server's configuration (such as with pg_reload_conf function) +for changes to take effect. See [PostgreSQL documentation](http://www.postgresql.org/docs/devel/static/monitoring-stats.html#WAIT-EVENT-TABLE) diff --git a/collector.c b/collector.c index a59083f..731ea58 100644 --- a/collector.c +++ b/collector.c @@ -16,6 +16,7 @@ #include "funcapi.h" #include "miscadmin.h" #include "postmaster/bgworker.h" +#include "postmaster/interrupt.h" #include "storage/ipc.h" #include "storage/procarray.h" #include "storage/procsignal.h" @@ -151,7 +152,7 @@ probe_waits(History *observations, HTAB *profile_hash, TimestampTz ts = GetCurrentTimestamp(); /* Realloc waits history if needed */ - newSize = pgws_collector_hdr->historySize; + newSize = pgws_historySize; if (observations->count != newSize) realloc_history(observations, newSize); @@ -170,7 +171,7 @@ probe_waits(History *observations, HTAB *profile_hash, item.pid = proc->pid; item.wait_event_info = proc->wait_event_info; - if (pgws_collector_hdr->profileQueries) + if (pgws_profileQueries) item.queryId = pgws_proc_queryids[i]; else item.queryId = 0; @@ -289,7 +290,7 @@ make_profile_hash() hash_ctl.hash = tag_hash; hash_ctl.hcxt = TopMemoryContext; - if (pgws_collector_hdr->profileQueries) + if (pgws_profileQueries) hash_ctl.keysize = offsetof(ProfileItem, count); else hash_ctl.keysize = offsetof(ProfileItem, queryId); @@ -346,6 +347,7 @@ pgws_collector_main(Datum main_arg) * partitipate to the ProcSignal infrastructure. */ pqsignal(SIGTERM, handle_sigterm); + pqsignal(SIGHUP, SignalHandlerForConfigReload); pqsignal(SIGUSR1, procsignal_sigusr1_handler); BackgroundWorkerUnblockSignals(); InitPostgresCompat(NULL, InvalidOid, NULL, InvalidOid, 0, NULL); @@ -361,7 +363,7 @@ pgws_collector_main(Datum main_arg) collector_context = AllocSetContextCreate(TopMemoryContext, "pg_wait_sampling context", ALLOCSET_DEFAULT_SIZES); old_context = MemoryContextSwitchTo(collector_context); - alloc_history(&observations, pgws_collector_hdr->historySize); + alloc_history(&observations, pgws_historySize); MemoryContextSwitchTo(old_context); ereport(LOG, (errmsg("pg_wait_sampling collector started"))); @@ -375,29 +377,31 @@ pgws_collector_main(Datum main_arg) shm_mq_handle *mqh; int64 history_diff, profile_diff; - int history_period, - profile_period; bool write_history, write_profile; /* We need an explicit call for at least ProcSignal notifications. */ CHECK_FOR_INTERRUPTS(); + if (ConfigReloadPending) + { + ConfigReloadPending = false; + ProcessConfigFile(PGC_SIGHUP); + } + /* Wait calculate time to next sample for history or profile */ current_ts = GetCurrentTimestamp(); history_diff = millisecs_diff(history_ts, current_ts); profile_diff = millisecs_diff(profile_ts, current_ts); - history_period = pgws_collector_hdr->historyPeriod; - profile_period = pgws_collector_hdr->profilePeriod; - write_history = (history_diff >= (int64)history_period); - write_profile = (profile_diff >= (int64)profile_period); + write_history = (history_diff >= (int64)pgws_historyPeriod); + write_profile = (profile_diff >= (int64)pgws_profilePeriod); if (write_history || write_profile) { probe_waits(&observations, profile_hash, - write_history, write_profile, pgws_collector_hdr->profilePid); + write_history, write_profile, pgws_profilePid); if (write_history) { @@ -421,8 +425,8 @@ pgws_collector_main(Datum main_arg) * shared memory. */ rc = WaitLatch(&MyProc->procLatch, WL_LATCH_SET | WL_TIMEOUT | WL_POSTMASTER_DEATH, - Min(history_period - (int)history_diff, - profile_period - (int)profile_diff), PG_WAIT_EXTENSION); + Min(pgws_historyPeriod - (int)history_diff, + pgws_historyPeriod - (int)profile_diff), PG_WAIT_EXTENSION); if (rc & WL_POSTMASTER_DEATH) proc_exit(1); diff --git a/compat.h b/compat.h index e515765..5371ae2 100644 --- a/compat.h +++ b/compat.h @@ -51,18 +51,4 @@ InitPostgresCompat(const char *in_dbname, Oid dboid, #endif } -static inline void -get_guc_variables_compat(struct config_generic ***vars, int *num_vars) -{ - Assert(vars != NULL); - Assert(num_vars != NULL); - -#if PG_VERSION_NUM >= 160000 - *vars = get_guc_variables(num_vars); -#else - *vars = get_guc_variables(); - *num_vars = GetNumConfigOptions(); -#endif -} - #endif diff --git a/pg_wait_sampling.c b/pg_wait_sampling.c index 7a09283..9e5df34 100644 --- a/pg_wait_sampling.c +++ b/pg_wait_sampling.c @@ -124,9 +124,17 @@ static const struct config_enum_entry pgws_profile_queries_options[] = {NULL, 0, false} }; +/* GUC variables */ +int pgws_historySize = 5000; +int pgws_historyPeriod = 10; +int pgws_profilePeriod = 10; +bool pgws_profilePid = true; +int pgws_profileQueries = PGWS_PROFILE_QUERIES_TOP; +bool pgws_sampleCpu = true; + #define pgws_enabled(level) \ - ((pgws_collector_hdr->profileQueries == PGWS_PROFILE_QUERIES_ALL) || \ - (pgws_collector_hdr->profileQueries == PGWS_PROFILE_QUERIES_TOP && (level) == 0)) + ((pgws_profileQueries == PGWS_PROFILE_QUERIES_ALL) || \ + (pgws_profileQueries == PGWS_PROFILE_QUERIES_TOP && (level) == 0)) /* * Calculate max processes count. @@ -210,155 +218,6 @@ pgws_shmem_size(void) return size; } -static bool -shmem_int_guc_check_hook(int *newval, void **extra, GucSource source) -{ - if (UsedShmemSegAddr == NULL) - return false; - return true; -} - -static bool -shmem_enum_guc_check_hook(int *newval, void **extra, GucSource source) -{ - if (UsedShmemSegAddr == NULL) - return false; - return true; -} - -static bool -shmem_bool_guc_check_hook(bool *newval, void **extra, GucSource source) -{ - if (UsedShmemSegAddr == NULL) - return false; - return true; -} - -/* - * This union allows us to mix the numerous different types of structs - * that we are organizing. - */ -typedef union -{ - struct config_generic generic; - struct config_bool _bool; - struct config_real real; - struct config_int integer; - struct config_string string; - struct config_enum _enum; -} mixedStruct; - -/* - * Setup new GUCs or modify existsing. - */ -static void -setup_gucs() -{ - struct config_generic **guc_vars; - int numOpts, - i; - bool history_size_found = false, - history_period_found = false, - profile_period_found = false, - profile_pid_found = false, - profile_queries_found = false, - sample_cpu_found = false; - - get_guc_variables_compat(&guc_vars, &numOpts); - - for (i = 0; i < numOpts; i++) - { - mixedStruct *var = (mixedStruct *) guc_vars[i]; - const char *name = var->generic.name; - - if (var->generic.flags & GUC_CUSTOM_PLACEHOLDER) - continue; - - if (!strcmp(name, "pg_wait_sampling.history_size")) - { - history_size_found = true; - var->integer.variable = &pgws_collector_hdr->historySize; - pgws_collector_hdr->historySize = 5000; - } - else if (!strcmp(name, "pg_wait_sampling.history_period")) - { - history_period_found = true; - var->integer.variable = &pgws_collector_hdr->historyPeriod; - pgws_collector_hdr->historyPeriod = 10; - } - else if (!strcmp(name, "pg_wait_sampling.profile_period")) - { - profile_period_found = true; - var->integer.variable = &pgws_collector_hdr->profilePeriod; - pgws_collector_hdr->profilePeriod = 10; - } - else if (!strcmp(name, "pg_wait_sampling.profile_pid")) - { - profile_pid_found = true; - var->_bool.variable = &pgws_collector_hdr->profilePid; - pgws_collector_hdr->profilePid = true; - } - else if (!strcmp(name, "pg_wait_sampling.profile_queries")) - { - profile_queries_found = true; - var->_enum.variable = &pgws_collector_hdr->profileQueries; - pgws_collector_hdr->profileQueries = PGWS_PROFILE_QUERIES_TOP; - } - else if (!strcmp(name, "pg_wait_sampling.sample_cpu")) - { - sample_cpu_found = true; - var->_bool.variable = &pgws_collector_hdr->sampleCpu; - pgws_collector_hdr->sampleCpu = true; - } - } - - if (!history_size_found) - DefineCustomIntVariable("pg_wait_sampling.history_size", - "Sets size of waits history.", NULL, - &pgws_collector_hdr->historySize, 5000, 100, INT_MAX, - PGC_SUSET, 0, shmem_int_guc_check_hook, NULL, NULL); - - if (!history_period_found) - DefineCustomIntVariable("pg_wait_sampling.history_period", - "Sets period of waits history sampling.", NULL, - &pgws_collector_hdr->historyPeriod, 10, 1, INT_MAX, - PGC_SUSET, 0, shmem_int_guc_check_hook, NULL, NULL); - - if (!profile_period_found) - DefineCustomIntVariable("pg_wait_sampling.profile_period", - "Sets period of waits profile sampling.", NULL, - &pgws_collector_hdr->profilePeriod, 10, 1, INT_MAX, - PGC_SUSET, 0, shmem_int_guc_check_hook, NULL, NULL); - - if (!profile_pid_found) - DefineCustomBoolVariable("pg_wait_sampling.profile_pid", - "Sets whether profile should be collected per pid.", NULL, - &pgws_collector_hdr->profilePid, true, - PGC_SUSET, 0, shmem_bool_guc_check_hook, NULL, NULL); - - if (!profile_queries_found) - DefineCustomEnumVariable("pg_wait_sampling.profile_queries", - "Sets whether profile should be collected per query.", NULL, - &pgws_collector_hdr->profileQueries, PGWS_PROFILE_QUERIES_TOP, pgws_profile_queries_options, - PGC_SUSET, 0, shmem_enum_guc_check_hook, NULL, NULL); - - if (!sample_cpu_found) - DefineCustomBoolVariable("pg_wait_sampling.sample_cpu", - "Sets whether not waiting backends should be sampled.", NULL, - &pgws_collector_hdr->sampleCpu, true, - PGC_SUSET, 0, shmem_bool_guc_check_hook, NULL, NULL); - - if (history_size_found - || history_period_found - || profile_period_found - || profile_pid_found - || profile_queries_found - || sample_cpu_found) - { - ProcessConfigFile(PGC_SIGHUP); - } -} - #if PG_VERSION_NUM >= 150000 /* * shmem_request hook: request additional shared memory resources. @@ -395,17 +254,12 @@ pgws_shmem_startup(void) pgws_collector_hdr = shm_toc_allocate(toc, sizeof(CollectorShmqHeader)); shm_toc_insert(toc, 0, pgws_collector_hdr); - /* needed to please check_GUC_init */ - pgws_collector_hdr->profileQueries = PGWS_PROFILE_QUERIES_TOP; pgws_collector_mq = shm_toc_allocate(toc, COLLECTOR_QUEUE_SIZE); shm_toc_insert(toc, 1, pgws_collector_mq); pgws_proc_queryids = shm_toc_allocate(toc, sizeof(uint64) * get_max_procs_count()); shm_toc_insert(toc, 2, pgws_proc_queryids); MemSet(pgws_proc_queryids, 0, sizeof(uint64) * get_max_procs_count()); - - /* Initialize GUC variables in shared memory */ - setup_gucs(); } else { @@ -486,6 +340,84 @@ _PG_init(void) ExecutorEnd_hook = pgws_ExecutorEnd; prev_ProcessUtility = ProcessUtility_hook; ProcessUtility_hook = pgws_ProcessUtility; + + /* Define GUC variables */ + DefineCustomIntVariable("pg_wait_sampling.history_size", + "Sets size of waits history.", + NULL, + &pgws_historySize, + 5000, + 100, + INT_MAX, + PGC_SIGHUP, + 0, + NULL, + NULL, + NULL); + + DefineCustomIntVariable("pg_wait_sampling.history_period", + "Sets period of waits history sampling.", + NULL, + &pgws_historyPeriod, + 10, + 1, + INT_MAX, + PGC_SIGHUP, + GUC_UNIT_MS, + NULL, + NULL, + NULL); + + DefineCustomIntVariable("pg_wait_sampling.profile_period", + "Sets period of waits profile sampling.", + NULL, + &pgws_profilePeriod, + 10, + 1, + INT_MAX, + PGC_SIGHUP, + GUC_UNIT_MS, + NULL, + NULL, + NULL); + + DefineCustomBoolVariable("pg_wait_sampling.profile_pid", + "Sets whether profile should be collected per pid.", + NULL, + &pgws_profilePid, + true, + PGC_SIGHUP, + 0, + NULL, + NULL, + NULL); + + DefineCustomEnumVariable("pg_wait_sampling.profile_queries", + "Sets whether profile should be collected per query.", + NULL, + &pgws_profileQueries, + PGWS_PROFILE_QUERIES_TOP, + pgws_profile_queries_options, + PGC_SIGHUP, + 0, + NULL, + NULL, + NULL); + + DefineCustomBoolVariable("pg_wait_sampling.sample_cpu", + "Sets whether not waiting backends should be sampled.", + NULL, + &pgws_sampleCpu, + true, + PGC_SIGHUP, + 0, + NULL, + NULL, + NULL); + +#if PG_VERSION_NUM >= 150000 + MarkGUCPrefixReserved("pg_wait_sampling"); +#endif } /* @@ -521,7 +453,7 @@ search_proc(int pid) bool pgws_should_sample_proc(PGPROC *proc) { - if (proc->wait_event_info == 0 && !pgws_collector_hdr->sampleCpu) + if (proc->wait_event_info == 0 && !pgws_sampleCpu) return false; /* @@ -833,7 +765,7 @@ pg_wait_sampling_get_profile(PG_FUNCTION_ARGS) else nulls[2] = true; - if (pgws_collector_hdr->profileQueries) + if (pgws_profileQueries) values[3] = UInt64GetDatum(item->queryId); else values[3] = (Datum) 0; diff --git a/pg_wait_sampling.h b/pg_wait_sampling.h index a8a550f..e9733e7 100644 --- a/pg_wait_sampling.h +++ b/pg_wait_sampling.h @@ -58,14 +58,16 @@ typedef struct { Latch *latch; SHMRequest request; - int historySize; - int historyPeriod; - int profilePeriod; - bool profilePid; - int profileQueries; - bool sampleCpu; } CollectorShmqHeader; +/* GUC variables */ +extern int pgws_historySize; +extern int pgws_historyPeriod; +extern int pgws_profilePeriod; +extern bool pgws_profilePid; +extern int pgws_profileQueries; +extern bool pgws_sampleCpu; + /* pg_wait_sampling.c */ extern CollectorShmqHeader *pgws_collector_hdr; extern shm_mq *pgws_collector_mq; From 4826caa6f6d76a86daf04ea4a83463019bac7cc2 Mon Sep 17 00:00:00 2001 From: Oleg Tselebrovskiy Date: Wed, 5 Feb 2025 10:03:49 +0700 Subject: [PATCH 05/17] Save pid and wait_event_info from PGPROC to avoid race condition When using a pointer to PGPROC structure it can be changed somewhere else and cause a race condition, which leads to an incorrect entry in pg_wait_sampling. Saving pid and wait_event_info at the start guarantees consistent data Reviewed and amended by Sergey Shinderuk --- collector.c | 6 +----- pg_wait_sampling.c | 16 ++++++++++++---- pg_wait_sampling.h | 6 +++--- 3 files changed, 16 insertions(+), 12 deletions(-) diff --git a/collector.c b/collector.c index 731ea58..cd2762d 100644 --- a/collector.c +++ b/collector.c @@ -164,13 +164,9 @@ probe_waits(History *observations, HTAB *profile_hash, *observation; PGPROC *proc = &ProcGlobal->allProcs[i]; - if (!pgws_should_sample_proc(proc)) + if (!pgws_should_sample_proc(proc, &item.pid, &item.wait_event_info)) continue; - /* Collect next wait event sample */ - item.pid = proc->pid; - item.wait_event_info = proc->wait_event_info; - if (pgws_profileQueries) item.queryId = pgws_proc_queryids[i]; else diff --git a/pg_wait_sampling.c b/pg_wait_sampling.c index 9e5df34..153d875 100644 --- a/pg_wait_sampling.c +++ b/pg_wait_sampling.c @@ -451,9 +451,15 @@ search_proc(int pid) * views. */ bool -pgws_should_sample_proc(PGPROC *proc) +pgws_should_sample_proc(PGPROC *proc, int *pid_p, uint32 *wait_event_info_p) { - if (proc->wait_event_info == 0 && !pgws_sampleCpu) + int pid = proc->pid; + uint32 wait_event_info = proc->wait_event_info; + + *pid_p = pid; + *wait_event_info_p = wait_event_info; + + if (wait_event_info == 0 && !pgws_sampleCpu) return false; /* @@ -462,7 +468,7 @@ pgws_should_sample_proc(PGPROC *proc) * null wait events. So instead we make use of DisownLatch() resetting * owner_pid during ProcKill(). */ - if (proc->pid == 0 || proc->procLatch.owner_pid == 0 || proc->pid == MyProcPid) + if (pid == 0 || proc->procLatch.owner_pid == 0 || pid == MyProcPid) return false; return true; @@ -533,7 +539,9 @@ pg_wait_sampling_get_current(PG_FUNCTION_ARGS) { PGPROC *proc = &ProcGlobal->allProcs[i]; - if (!pgws_should_sample_proc(proc)) + if (!pgws_should_sample_proc(proc, + ¶ms->items[j].pid, + ¶ms->items[j].wait_event_info)) continue; params->items[j].pid = proc->pid; diff --git a/pg_wait_sampling.h b/pg_wait_sampling.h index e9733e7..141269a 100644 --- a/pg_wait_sampling.h +++ b/pg_wait_sampling.h @@ -24,7 +24,7 @@ typedef struct { - uint32 pid; + int pid; uint32 wait_event_info; uint64 queryId; uint64 count; @@ -32,7 +32,7 @@ typedef struct typedef struct { - uint32 pid; + int pid; uint32 wait_event_info; uint64 queryId; TimestampTz ts; @@ -73,7 +73,7 @@ extern CollectorShmqHeader *pgws_collector_hdr; extern shm_mq *pgws_collector_mq; extern uint64 *pgws_proc_queryids; extern void pgws_init_lock_tag(LOCKTAG *tag, uint32 lock); -extern bool pgws_should_sample_proc(PGPROC *proc); +extern bool pgws_should_sample_proc(PGPROC *proc, int *pid_p, uint32 *wait_event_info_p); /* collector.c */ extern void pgws_register_wait_collector(void); From e3b1e15ed4a3abbe304a9ebf367789445fe66d28 Mon Sep 17 00:00:00 2001 From: Oleg Tselebrovskiy Date: Fri, 7 Feb 2025 15:57:59 +0700 Subject: [PATCH 06/17] Fix README to mention support of pg13+ --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 5c123e0..bbdbd20 100644 --- a/README.md +++ b/README.md @@ -51,7 +51,7 @@ PostgreSQL installation. It is available from [github](https://github.com/postgrespro/pg_wait_sampling) under the same license as [PostgreSQL](http://www.postgresql.org/about/licence/) -and supports PostgreSQL 12+. +and supports PostgreSQL 13+. Installation ------------ @@ -62,10 +62,10 @@ repository: https://download.postgresql.org/pub/repos/ Manual build ------------ -`pg_wait_sampling` is PostgreSQL extension which requires PostgreSQL 12 or +`pg_wait_sampling` is PostgreSQL extension which requires PostgreSQL 13 or higher. Before build and install you should ensure following: - * PostgreSQL version is 12 or higher. + * PostgreSQL version is 13 or higher. * You have development package of PostgreSQL installed or you built PostgreSQL from source. * Your PATH variable is configured so that `pg_config` command available, or From 753940f2f786367613a0f7eec025c09b8b402528 Mon Sep 17 00:00:00 2001 From: Sergey Shinderuk Date: Thu, 20 Feb 2025 16:28:02 +0300 Subject: [PATCH 07/17] Clean includes with include-what-you-use --- collector.c | 23 ++++++++++++----------- compat.h | 4 ---- pg_wait_sampling.c | 30 ++++++++++++++++-------------- pg_wait_sampling.h | 7 +++---- 4 files changed, 31 insertions(+), 33 deletions(-) diff --git a/collector.c b/collector.c index cd2762d..c221007 100644 --- a/collector.c +++ b/collector.c @@ -9,26 +9,27 @@ */ #include "postgres.h" -#include "catalog/pg_type.h" -#if PG_VERSION_NUM >= 130000 +#include + #include "common/hashfn.h" -#endif -#include "funcapi.h" +#include "compat.h" #include "miscadmin.h" +#include "pg_wait_sampling.h" +#include "pgstat.h" #include "postmaster/bgworker.h" #include "postmaster/interrupt.h" #include "storage/ipc.h" -#include "storage/procarray.h" +#include "storage/latch.h" +#include "storage/lock.h" +#include "storage/lwlock.h" +#include "storage/proc.h" #include "storage/procsignal.h" #include "storage/shm_mq.h" -#include "storage/shm_toc.h" -#include "storage/spin.h" +#include "utils/guc.h" +#include "utils/hsearch.h" #include "utils/memutils.h" #include "utils/resowner.h" -#include "pgstat.h" - -#include "compat.h" -#include "pg_wait_sampling.h" +#include "utils/timestamp.h" static volatile sig_atomic_t shutdown_requested = false; diff --git a/compat.h b/compat.h index 5371ae2..9194d97 100644 --- a/compat.h +++ b/compat.h @@ -10,12 +10,8 @@ #ifndef __COMPAT_H__ #define __COMPAT_H__ -#include "postgres.h" - -#include "access/tupdesc.h" #include "miscadmin.h" #include "storage/shm_mq.h" -#include "utils/guc_tables.h" static inline shm_mq_result shm_mq_send_compat(shm_mq_handle *mqh, Size nbytes, const void *data, diff --git a/pg_wait_sampling.c b/pg_wait_sampling.c index 153d875..46e09a9 100644 --- a/pg_wait_sampling.c +++ b/pg_wait_sampling.c @@ -9,32 +9,34 @@ */ #include "postgres.h" +#include + #include "access/htup_details.h" -#include "access/twophase.h" -#include "catalog/pg_type.h" -#include "fmgr.h" +#include "catalog/pg_type_d.h" +#include "executor/executor.h" #include "funcapi.h" #include "miscadmin.h" #include "optimizer/planner.h" +#include "pg_wait_sampling.h" #include "pgstat.h" -#include "postmaster/autovacuum.h" -#include "replication/walsender.h" #include "storage/ipc.h" -#include "storage/pg_shmem.h" -#include "storage/procarray.h" +#include "storage/latch.h" +#include "storage/lock.h" +#include "storage/lwlock.h" +#include "storage/proc.h" #include "storage/shm_mq.h" #include "storage/shm_toc.h" -#include "storage/spin.h" +#include "storage/shmem.h" #include "tcop/utility.h" #include "utils/builtins.h" -#include "utils/datetime.h" -#include "utils/guc_tables.h" #include "utils/guc.h" -#include "utils/memutils.h" /* TopMemoryContext. Actually for PG 9.6 only, - * but there should be no harm for others. */ +#include "utils/memutils.h" +#include "utils/timestamp.h" -#include "compat.h" -#include "pg_wait_sampling.h" +#if PG_VERSION_NUM < 150000 +#include "postmaster/autovacuum.h" +#include "replication/walsender.h" +#endif PG_MODULE_MAGIC; diff --git a/pg_wait_sampling.h b/pg_wait_sampling.h index 141269a..b75df66 100644 --- a/pg_wait_sampling.h +++ b/pg_wait_sampling.h @@ -10,11 +10,10 @@ #ifndef __PG_WAIT_SAMPLING_H__ #define __PG_WAIT_SAMPLING_H__ -#include "postgres.h" - -#include "storage/proc.h" +#include "datatype/timestamp.h" +#include "storage/latch.h" +#include "storage/lock.h" #include "storage/shm_mq.h" -#include "utils/timestamp.h" #define PG_WAIT_SAMPLING_MAGIC 0xCA94B107 #define COLLECTOR_QUEUE_SIZE (16 * 1024) From 8e0e23ec3e489bfc1af8a5ac24632a9d22a14119 Mon Sep 17 00:00:00 2001 From: Sergey Shinderuk Date: Thu, 20 Feb 2025 16:36:15 +0300 Subject: [PATCH 08/17] Update copyright year --- LICENSE | 4 ++-- collector.c | 2 +- compat.h | 2 +- pg_wait_sampling.c | 2 +- pg_wait_sampling.h | 2 +- 5 files changed, 6 insertions(+), 6 deletions(-) diff --git a/LICENSE b/LICENSE index f4b38be..46c4b8f 100644 --- a/LICENSE +++ b/LICENSE @@ -1,7 +1,7 @@ pg_wait_sampling is released under the PostgreSQL License, a liberal Open Source license, similar to the BSD or MIT licenses. -Copyright (c) 2015-2017, Postgres Professional -Portions Copyright (c) 1996-2017, PostgreSQL Global Development Group +Copyright (c) 2015-2025, Postgres Professional +Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group Portions Copyright (c) 1994, The Regents of the University of California Permission to use, copy, modify, and distribute this software and its documentation for any purpose, without fee, and without a written agreement is hereby granted, provided that the above copyright notice and this paragraph and the following two paragraphs appear in all copies. diff --git a/collector.c b/collector.c index c221007..d7c4d9e 100644 --- a/collector.c +++ b/collector.c @@ -2,7 +2,7 @@ * collector.c * Collector of wait event history and profile. * - * Copyright (c) 2015-2016, Postgres Professional + * Copyright (c) 2015-2025, Postgres Professional * * IDENTIFICATION * contrib/pg_wait_sampling/pg_wait_sampling.c diff --git a/compat.h b/compat.h index 9194d97..32aef49 100644 --- a/compat.h +++ b/compat.h @@ -2,7 +2,7 @@ * compat.h * Definitions for function wrappers compatible between PG versions. * - * Copyright (c) 2015-2022, Postgres Professional + * Copyright (c) 2015-2025, Postgres Professional * * IDENTIFICATION * contrib/pg_wait_sampling/compat.h diff --git a/pg_wait_sampling.c b/pg_wait_sampling.c index 46e09a9..590cd20 100644 --- a/pg_wait_sampling.c +++ b/pg_wait_sampling.c @@ -2,7 +2,7 @@ * pg_wait_sampling.c * Track information about wait events. * - * Copyright (c) 2015-2017, Postgres Professional + * Copyright (c) 2015-2025, Postgres Professional * * IDENTIFICATION * contrib/pg_wait_sampling/pg_wait_sampling.c diff --git a/pg_wait_sampling.h b/pg_wait_sampling.h index b75df66..d0e6962 100644 --- a/pg_wait_sampling.h +++ b/pg_wait_sampling.h @@ -2,7 +2,7 @@ * pg_wait_sampling.h * Headers for pg_wait_sampling extension. * - * Copyright (c) 2015-2016, Postgres Professional + * Copyright (c) 2015-2025, Postgres Professional * * IDENTIFICATION * contrib/pg_wait_sampling/pg_wait_sampling.h From aa8e1bb9538e99a28a8b940f1f3aacac3309d59a Mon Sep 17 00:00:00 2001 From: Sergey Shinderuk Date: Thu, 20 Feb 2025 16:50:53 +0300 Subject: [PATCH 09/17] pgindent --- collector.c | 89 +++++++------- pg_wait_sampling.c | 281 +++++++++++++++++++++++---------------------- pg_wait_sampling.h | 40 +++---- 3 files changed, 208 insertions(+), 202 deletions(-) diff --git a/collector.c b/collector.c index d7c4d9e..d1d4755 100644 --- a/collector.c +++ b/collector.c @@ -74,10 +74,10 @@ alloc_history(History *observations, int count) static void realloc_history(History *observations, int count) { - HistoryItem *newitems; - int copyCount, - i, - j; + HistoryItem *newitems; + int copyCount, + i, + j; /* Allocate new array for history */ newitems = (HistoryItem *) palloc0(sizeof(HistoryItem) * count); @@ -115,7 +115,8 @@ realloc_history(History *observations, int count) static void handle_sigterm(SIGNAL_ARGS) { - int save_errno = errno; + int save_errno = errno; + shutdown_requested = true; if (MyProc) SetLatch(&MyProc->procLatch); @@ -150,7 +151,7 @@ probe_waits(History *observations, HTAB *profile_hash, { int i, newSize; - TimestampTz ts = GetCurrentTimestamp(); + TimestampTz ts = GetCurrentTimestamp(); /* Realloc waits history if needed */ newSize = pgws_historySize; @@ -161,9 +162,9 @@ probe_waits(History *observations, HTAB *profile_hash, LWLockAcquire(ProcArrayLock, LW_SHARED); for (i = 0; i < ProcGlobal->allProcCount; i++) { - HistoryItem item, - *observation; - PGPROC *proc = &ProcGlobal->allProcs[i]; + HistoryItem item, + *observation; + PGPROC *proc = &ProcGlobal->allProcs[i]; if (!pgws_should_sample_proc(proc, &item.pid, &item.wait_event_info)) continue; @@ -185,8 +186,8 @@ probe_waits(History *observations, HTAB *profile_hash, /* Write to the profile if needed */ if (write_profile) { - ProfileItem *profileItem; - bool found; + ProfileItem *profileItem; + bool found; if (!profile_pid) item.pid = 0; @@ -207,9 +208,9 @@ probe_waits(History *observations, HTAB *profile_hash, static void send_history(History *observations, shm_mq_handle *mqh) { - Size count, - i; - shm_mq_result mq_result; + Size count, + i; + shm_mq_result mq_result; if (observations->wraparound) count = observations->count; @@ -227,10 +228,10 @@ send_history(History *observations, shm_mq_handle *mqh) for (i = 0; i < count; i++) { mq_result = shm_mq_send_compat(mqh, - sizeof(HistoryItem), - &observations->items[i], - false, - true); + sizeof(HistoryItem), + &observations->items[i], + false, + true); if (mq_result == SHM_MQ_DETACHED) { ereport(WARNING, @@ -247,10 +248,10 @@ send_history(History *observations, shm_mq_handle *mqh) static void send_profile(HTAB *profile_hash, shm_mq_handle *mqh) { - HASH_SEQ_STATUS scan_status; - ProfileItem *item; - Size count = hash_get_num_entries(profile_hash); - shm_mq_result mq_result; + HASH_SEQ_STATUS scan_status; + ProfileItem *item; + Size count = hash_get_num_entries(profile_hash); + shm_mq_result mq_result; mq_result = shm_mq_send_compat(mqh, sizeof(count), &count, false, true); if (mq_result == SHM_MQ_DETACHED) @@ -282,7 +283,7 @@ send_profile(HTAB *profile_hash, shm_mq_handle *mqh) static HTAB * make_profile_hash() { - HASHCTL hash_ctl; + HASHCTL hash_ctl; hash_ctl.hash = tag_hash; hash_ctl.hcxt = TopMemoryContext; @@ -303,8 +304,8 @@ make_profile_hash() static int64 millisecs_diff(TimestampTz tz1, TimestampTz tz2) { - long secs; - int microsecs; + long secs; + int microsecs; TimestampDifference(tz1, tz2, &secs, µsecs); @@ -318,13 +319,13 @@ millisecs_diff(TimestampTz tz1, TimestampTz tz2) void pgws_collector_main(Datum main_arg) { - HTAB *profile_hash = NULL; - History observations; - MemoryContext old_context, - collector_context; - TimestampTz current_ts, - history_ts, - profile_ts; + HTAB *profile_hash = NULL; + History observations; + MemoryContext old_context, + collector_context; + TimestampTz current_ts, + history_ts, + profile_ts; /* * Establish signal handlers. @@ -358,7 +359,7 @@ pgws_collector_main(Datum main_arg) CurrentResourceOwner = ResourceOwnerCreate(NULL, "pg_wait_sampling collector"); collector_context = AllocSetContextCreate(TopMemoryContext, - "pg_wait_sampling context", ALLOCSET_DEFAULT_SIZES); + "pg_wait_sampling context", ALLOCSET_DEFAULT_SIZES); old_context = MemoryContextSwitchTo(collector_context); alloc_history(&observations, pgws_historySize); MemoryContextSwitchTo(old_context); @@ -370,12 +371,12 @@ pgws_collector_main(Datum main_arg) while (1) { - int rc; - shm_mq_handle *mqh; - int64 history_diff, - profile_diff; - bool write_history, - write_profile; + int rc; + shm_mq_handle *mqh; + int64 history_diff, + profile_diff; + bool write_history, + write_profile; /* We need an explicit call for at least ProcSignal notifications. */ CHECK_FOR_INTERRUPTS(); @@ -392,8 +393,8 @@ pgws_collector_main(Datum main_arg) history_diff = millisecs_diff(history_ts, current_ts); profile_diff = millisecs_diff(profile_ts, current_ts); - write_history = (history_diff >= (int64)pgws_historyPeriod); - write_profile = (profile_diff >= (int64)pgws_profilePeriod); + write_history = (history_diff >= (int64) pgws_historyPeriod); + write_profile = (profile_diff >= (int64) pgws_profilePeriod); if (write_history || write_profile) { @@ -422,8 +423,8 @@ pgws_collector_main(Datum main_arg) * shared memory. */ rc = WaitLatch(&MyProc->procLatch, WL_LATCH_SET | WL_TIMEOUT | WL_POSTMASTER_DEATH, - Min(pgws_historyPeriod - (int)history_diff, - pgws_historyPeriod - (int)profile_diff), PG_WAIT_EXTENSION); + Min(pgws_historyPeriod - (int) history_diff, + pgws_historyPeriod - (int) profile_diff), PG_WAIT_EXTENSION); if (rc & WL_POSTMASTER_DEATH) proc_exit(1); @@ -444,7 +445,7 @@ pgws_collector_main(Datum main_arg) if (request == HISTORY_REQUEST || request == PROFILE_REQUEST) { - shm_mq_result mq_result; + shm_mq_result mq_result; /* Send history or profile */ shm_mq_set_sender(pgws_collector_mq, MyProc); diff --git a/pg_wait_sampling.c b/pg_wait_sampling.c index 590cd20..d6bb99a 100644 --- a/pg_wait_sampling.c +++ b/pg_wait_sampling.c @@ -45,20 +45,20 @@ void _PG_init(void); static bool shmem_initialized = false; /* Hooks */ -static ExecutorStart_hook_type prev_ExecutorStart = NULL; -static ExecutorRun_hook_type prev_ExecutorRun = NULL; +static ExecutorStart_hook_type prev_ExecutorStart = NULL; +static ExecutorRun_hook_type prev_ExecutorRun = NULL; static ExecutorFinish_hook_type prev_ExecutorFinish = NULL; -static ExecutorEnd_hook_type prev_ExecutorEnd = NULL; -static planner_hook_type planner_hook_next = NULL; +static ExecutorEnd_hook_type prev_ExecutorEnd = NULL; +static planner_hook_type planner_hook_next = NULL; static ProcessUtility_hook_type prev_ProcessUtility = NULL; /* Current nesting depth of planner/Executor calls */ -static int nesting_level = 0; +static int nesting_level = 0; /* Pointers to shared memory objects */ -shm_mq *pgws_collector_mq = NULL; -uint64 *pgws_proc_queryids = NULL; -CollectorShmqHeader *pgws_collector_hdr = NULL; +shm_mq *pgws_collector_mq = NULL; +uint64 *pgws_proc_queryids = NULL; +CollectorShmqHeader *pgws_collector_hdr = NULL; /* Receiver (backend) local shm_mq pointers and lock */ static shm_mq *recv_mq = NULL; @@ -69,20 +69,20 @@ static LOCKTAG queueTag; static shmem_request_hook_type prev_shmem_request_hook = NULL; #endif static shmem_startup_hook_type prev_shmem_startup_hook = NULL; -static PGPROC * search_proc(int backendPid); +static PGPROC *search_proc(int backendPid); static PlannedStmt *pgws_planner_hook(Query *parse, #if PG_VERSION_NUM >= 130000 - const char *query_string, + const char *query_string, #endif - int cursorOptions, ParamListInfo boundParams); + int cursorOptions, ParamListInfo boundParams); static void pgws_ExecutorStart(QueryDesc *queryDesc, int eflags); static void pgws_ExecutorRun(QueryDesc *queryDesc, ScanDirection direction, uint64 count #if PG_VERSION_NUM >= 100000 && PG_VERSION_NUM < 180000 - , bool execute_once + ,bool execute_once #endif - ); +); static void pgws_ExecutorFinish(QueryDesc *queryDesc); static void pgws_ExecutorEnd(QueryDesc *queryDesc); static void pgws_ProcessUtility(PlannedStmt *pstmt, @@ -99,40 +99,40 @@ static void pgws_ProcessUtility(PlannedStmt *pstmt, #else char *completionTag #endif - ); +); /*---- GUC variables ----*/ typedef enum { - PGWS_PROFILE_QUERIES_NONE, /* profile no statements */ - PGWS_PROFILE_QUERIES_TOP, /* only top level statements */ - PGWS_PROFILE_QUERIES_ALL /* all statements, including nested ones */ + PGWS_PROFILE_QUERIES_NONE, /* profile no statements */ + PGWS_PROFILE_QUERIES_TOP, /* only top level statements */ + PGWS_PROFILE_QUERIES_ALL /* all statements, including nested ones */ } PGWSTrackLevel; static const struct config_enum_entry pgws_profile_queries_options[] = { - {"none", PGWS_PROFILE_QUERIES_NONE, false}, - {"off", PGWS_PROFILE_QUERIES_NONE, false}, - {"no", PGWS_PROFILE_QUERIES_NONE, false}, - {"false", PGWS_PROFILE_QUERIES_NONE, false}, - {"0", PGWS_PROFILE_QUERIES_NONE, false}, - {"top", PGWS_PROFILE_QUERIES_TOP, false}, - {"on", PGWS_PROFILE_QUERIES_TOP, false}, - {"yes", PGWS_PROFILE_QUERIES_TOP, false}, - {"true", PGWS_PROFILE_QUERIES_TOP, false}, - {"1", PGWS_PROFILE_QUERIES_TOP, false}, - {"all", PGWS_PROFILE_QUERIES_ALL, false}, - {NULL, 0, false} + {"none", PGWS_PROFILE_QUERIES_NONE, false}, + {"off", PGWS_PROFILE_QUERIES_NONE, false}, + {"no", PGWS_PROFILE_QUERIES_NONE, false}, + {"false", PGWS_PROFILE_QUERIES_NONE, false}, + {"0", PGWS_PROFILE_QUERIES_NONE, false}, + {"top", PGWS_PROFILE_QUERIES_TOP, false}, + {"on", PGWS_PROFILE_QUERIES_TOP, false}, + {"yes", PGWS_PROFILE_QUERIES_TOP, false}, + {"true", PGWS_PROFILE_QUERIES_TOP, false}, + {"1", PGWS_PROFILE_QUERIES_TOP, false}, + {"all", PGWS_PROFILE_QUERIES_ALL, false}, + {NULL, 0, false} }; /* GUC variables */ -int pgws_historySize = 5000; -int pgws_historyPeriod = 10; -int pgws_profilePeriod = 10; -bool pgws_profilePid = true; -int pgws_profileQueries = PGWS_PROFILE_QUERIES_TOP; -bool pgws_sampleCpu = true; +int pgws_historySize = 5000; +int pgws_historyPeriod = 10; +int pgws_profilePeriod = 10; +bool pgws_profilePid = true; +int pgws_profileQueries = PGWS_PROFILE_QUERIES_TOP; +bool pgws_sampleCpu = true; #define pgws_enabled(level) \ ((pgws_profileQueries == PGWS_PROFILE_QUERIES_ALL) || \ @@ -148,10 +148,11 @@ bool pgws_sampleCpu = true; static int get_max_procs_count(void) { - int count = 0; + int count = 0; /* First, add the maximum number of backends (MaxBackends). */ #if PG_VERSION_NUM >= 150000 + /* * On pg15+, we can directly access the MaxBackends variable, as it will * have already been initialized in shmem_request_hook. @@ -159,35 +160,35 @@ get_max_procs_count(void) Assert(MaxBackends > 0); count += MaxBackends; #else + /* - * On older versions, we need to compute MaxBackends: bgworkers, autovacuum - * workers and launcher. - * This has to be in sync with the value computed in - * InitializeMaxBackends() (postinit.c) + * On older versions, we need to compute MaxBackends: bgworkers, + * autovacuum workers and launcher. This has to be in sync with the value + * computed in InitializeMaxBackends() (postinit.c) * - * Note that we need to calculate the value as it won't initialized when we - * need it during _PG_init(). + * Note that we need to calculate the value as it won't initialized when + * we need it during _PG_init(). * * Note also that the value returned during _PG_init() might be different * from the value returned later if some third-party modules change one of * the underlying GUC. This isn't ideal but can't lead to a crash, as the * value returned during _PG_init() is only used to ask for additional * shmem with RequestAddinShmemSpace(), and postgres has an extra 100kB of - * shmem to compensate some small unaccounted usage. So if the value later - * changes, we will allocate and initialize the new (and correct) memory - * size, which will either work thanks for the extra 100kB of shmem, of - * fail (and prevent postgres startup) due to an out of shared memory - * error. + * shmem to compensate some small unaccounted usage. So if the value + * later changes, we will allocate and initialize the new (and correct) + * memory size, which will either work thanks for the extra 100kB of + * shmem, of fail (and prevent postgres startup) due to an out of shared + * memory error. */ count += MaxConnections + autovacuum_max_workers + 1 - + max_worker_processes; + + max_worker_processes; /* * Starting with pg12, wal senders aren't part of MaxConnections anymore * and have to be accounted for. */ count += max_wal_senders; -#endif /* pg 15- */ +#endif /* pg 15- */ /* End of MaxBackends calculation. */ /* Add AuxiliaryProcs */ @@ -202,9 +203,9 @@ get_max_procs_count(void) static Size pgws_shmem_size(void) { - shm_toc_estimator e; - Size size; - int nkeys; + shm_toc_estimator e; + Size size; + int nkeys; shm_toc_initialize_estimator(&e); @@ -246,7 +247,7 @@ pgws_shmem_startup(void) bool found; Size segsize = pgws_shmem_size(); void *pgws; - shm_toc *toc; + shm_toc *toc; pgws = ShmemInitStruct("pg_wait_sampling", segsize, &found); @@ -259,7 +260,7 @@ pgws_shmem_startup(void) pgws_collector_mq = shm_toc_allocate(toc, COLLECTOR_QUEUE_SIZE); shm_toc_insert(toc, 1, pgws_collector_mq); pgws_proc_queryids = shm_toc_allocate(toc, - sizeof(uint64) * get_max_procs_count()); + sizeof(uint64) * get_max_procs_count()); shm_toc_insert(toc, 2, pgws_proc_queryids); MemSet(pgws_proc_queryids, 0, sizeof(uint64) * get_max_procs_count()); } @@ -308,6 +309,7 @@ _PG_init(void) return; #if PG_VERSION_NUM < 150000 + /* * Request additional shared resources. (These are no-ops if we're not in * the postmaster process.) We'll allocate or attach to the shared @@ -326,22 +328,22 @@ _PG_init(void) */ #if PG_VERSION_NUM >= 150000 prev_shmem_request_hook = shmem_request_hook; - shmem_request_hook = pgws_shmem_request; + shmem_request_hook = pgws_shmem_request; #endif prev_shmem_startup_hook = shmem_startup_hook; - shmem_startup_hook = pgws_shmem_startup; - planner_hook_next = planner_hook; - planner_hook = pgws_planner_hook; - prev_ExecutorStart = ExecutorStart_hook; - ExecutorStart_hook = pgws_ExecutorStart; - prev_ExecutorRun = ExecutorRun_hook; - ExecutorRun_hook = pgws_ExecutorRun; - prev_ExecutorFinish = ExecutorFinish_hook; - ExecutorFinish_hook = pgws_ExecutorFinish; - prev_ExecutorEnd = ExecutorEnd_hook; - ExecutorEnd_hook = pgws_ExecutorEnd; - prev_ProcessUtility = ProcessUtility_hook; - ProcessUtility_hook = pgws_ProcessUtility; + shmem_startup_hook = pgws_shmem_startup; + planner_hook_next = planner_hook; + planner_hook = pgws_planner_hook; + prev_ExecutorStart = ExecutorStart_hook; + ExecutorStart_hook = pgws_ExecutorStart; + prev_ExecutorRun = ExecutorRun_hook; + ExecutorRun_hook = pgws_ExecutorRun; + prev_ExecutorFinish = ExecutorFinish_hook; + ExecutorFinish_hook = pgws_ExecutorFinish; + prev_ExecutorEnd = ExecutorEnd_hook; + ExecutorEnd_hook = pgws_ExecutorEnd; + prev_ProcessUtility = ProcessUtility_hook; + ProcessUtility_hook = pgws_ProcessUtility; /* Define GUC variables */ DefineCustomIntVariable("pg_wait_sampling.history_size", @@ -429,14 +431,15 @@ _PG_init(void) static PGPROC * search_proc(int pid) { - int i; + int i; if (pid == 0) return MyProc; for (i = 0; i < ProcGlobal->allProcCount; i++) { - PGPROC *proc = &ProcGlobal->allProcs[i]; + PGPROC *proc = &ProcGlobal->allProcs[i]; + if (proc->pid && proc->pid == pid) { return proc; @@ -478,28 +481,28 @@ pgws_should_sample_proc(PGPROC *proc, int *pid_p, uint32 *wait_event_info_p) typedef struct { - HistoryItem *items; - TimestampTz ts; + HistoryItem *items; + TimestampTz ts; } WaitCurrentContext; PG_FUNCTION_INFO_V1(pg_wait_sampling_get_current); Datum pg_wait_sampling_get_current(PG_FUNCTION_ARGS) { - FuncCallContext *funcctx; - WaitCurrentContext *params; + FuncCallContext *funcctx; + WaitCurrentContext *params; check_shmem(); if (SRF_IS_FIRSTCALL()) { - MemoryContext oldcontext; - TupleDesc tupdesc; + MemoryContext oldcontext; + TupleDesc tupdesc; funcctx = SRF_FIRSTCALL_INIT(); oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx); - params = (WaitCurrentContext *)palloc0(sizeof(WaitCurrentContext)); + params = (WaitCurrentContext *) palloc0(sizeof(WaitCurrentContext)); params->ts = GetCurrentTimestamp(); funcctx->user_fctx = params; @@ -519,8 +522,8 @@ pg_wait_sampling_get_current(PG_FUNCTION_ARGS) if (!PG_ARGISNULL(0)) { - HistoryItem *item; - PGPROC *proc; + HistoryItem *item; + PGPROC *proc; proc = search_proc(PG_GETARG_UINT32(0)); params->items = (HistoryItem *) palloc0(sizeof(HistoryItem)); @@ -532,14 +535,14 @@ pg_wait_sampling_get_current(PG_FUNCTION_ARGS) } else { - int procCount = ProcGlobal->allProcCount, - i, - j = 0; + int procCount = ProcGlobal->allProcCount, + i, + j = 0; params->items = (HistoryItem *) palloc0(sizeof(HistoryItem) * procCount); for (i = 0; i < procCount; i++) { - PGPROC *proc = &ProcGlobal->allProcs[i]; + PGPROC *proc = &ProcGlobal->allProcs[i]; if (!pgws_should_sample_proc(proc, ¶ms->items[j].pid, @@ -603,8 +606,8 @@ pg_wait_sampling_get_current(PG_FUNCTION_ARGS) typedef struct { - Size count; - ProfileItem *items; + Size count; + ProfileItem *items; } Profile; void @@ -621,14 +624,14 @@ pgws_init_lock_tag(LOCKTAG *tag, uint32 lock) static void * receive_array(SHMRequest request, Size item_size, Size *count) { - LOCKTAG collectorTag; - shm_mq_result res; - Size len, - i; - void *data; - Pointer result, - ptr; - MemoryContext oldctx; + LOCKTAG collectorTag; + shm_mq_result res; + Size len, + i; + void *data; + Pointer result, + ptr; + MemoryContext oldctx; /* Ensure nobody else trying to send request to queue */ pgws_init_lock_tag(&queueTag, PGWS_QUEUE_LOCK); @@ -649,9 +652,9 @@ receive_array(SHMRequest request, Size item_size, Size *count) shm_mq_set_receiver(recv_mq, MyProc); /* - * We switch to TopMemoryContext, so that recv_mqh is allocated there - * and is guaranteed to survive until before_shmem_exit callbacks are - * fired. Anyway, shm_mq_detach() will free handler on its own. + * We switch to TopMemoryContext, so that recv_mqh is allocated there and + * is guaranteed to survive until before_shmem_exit callbacks are fired. + * Anyway, shm_mq_detach() will free handler on its own. * * NB: we do not pass `seg` to shm_mq_attach(), so it won't set its own * callback, i.e. we do not interfere here with shm_mq_detach_callback(). @@ -661,10 +664,10 @@ receive_array(SHMRequest request, Size item_size, Size *count) MemoryContextSwitchTo(oldctx); /* - * Now we surely attached to the shm_mq and got collector's attention. - * If anything went wrong (e.g. Ctrl+C received from the client) we have - * to cleanup some things, i.e. detach from the shm_mq, so collector was - * able to continue responding to other requests. + * Now we surely attached to the shm_mq and got collector's attention. If + * anything went wrong (e.g. Ctrl+C received from the client) we have to + * cleanup some things, i.e. detach from the shm_mq, so collector was able + * to continue responding to other requests. * * PG_ENSURE_ERROR_CLEANUP() guaranties that cleanup callback will be * fired for both ERROR and FATAL. @@ -704,15 +707,15 @@ PG_FUNCTION_INFO_V1(pg_wait_sampling_get_profile); Datum pg_wait_sampling_get_profile(PG_FUNCTION_ARGS) { - Profile *profile; - FuncCallContext *funcctx; + Profile *profile; + FuncCallContext *funcctx; check_shmem(); if (SRF_IS_FIRSTCALL()) { - MemoryContext oldcontext; - TupleDesc tupdesc; + MemoryContext oldcontext; + TupleDesc tupdesc; funcctx = SRF_FIRSTCALL_INIT(); oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx); @@ -720,7 +723,7 @@ pg_wait_sampling_get_profile(PG_FUNCTION_ARGS) /* Receive profile from shmq */ profile = (Profile *) palloc0(sizeof(Profile)); profile->items = (ProfileItem *) receive_array(PROFILE_REQUEST, - sizeof(ProfileItem), &profile->count); + sizeof(ProfileItem), &profile->count); funcctx->user_fctx = profile; funcctx->max_calls = profile->count; @@ -821,15 +824,15 @@ PG_FUNCTION_INFO_V1(pg_wait_sampling_get_history); Datum pg_wait_sampling_get_history(PG_FUNCTION_ARGS) { - History *history; - FuncCallContext *funcctx; + History *history; + FuncCallContext *funcctx; check_shmem(); if (SRF_IS_FIRSTCALL()) { - MemoryContext oldcontext; - TupleDesc tupdesc; + MemoryContext oldcontext; + TupleDesc tupdesc; funcctx = SRF_FIRSTCALL_INIT(); oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx); @@ -837,7 +840,7 @@ pg_wait_sampling_get_history(PG_FUNCTION_ARGS) /* Receive history from shmq */ history = (History *) palloc0(sizeof(History)); history->items = (HistoryItem *) receive_array(HISTORY_REQUEST, - sizeof(HistoryItem), &history->count); + sizeof(HistoryItem), &history->count); funcctx->user_fctx = history; funcctx->max_calls = history->count; @@ -918,9 +921,9 @@ pgws_planner_hook(Query *parse, int cursorOptions, ParamListInfo boundParams) { - PlannedStmt *result; - int i = MyProc - ProcGlobal->allProcs; - uint64 save_queryId = 0; + PlannedStmt *result; + int i = MyProc - ProcGlobal->allProcs; + uint64 save_queryId = 0; if (pgws_enabled(nesting_level)) { @@ -935,15 +938,15 @@ pgws_planner_hook(Query *parse, if (planner_hook_next) result = planner_hook_next(parse, #if PG_VERSION_NUM >= 130000 - query_string, + query_string, #endif - cursorOptions, boundParams); + cursorOptions, boundParams); else result = standard_planner(parse, #if PG_VERSION_NUM >= 130000 - query_string, + query_string, #endif - cursorOptions, boundParams); + cursorOptions, boundParams); nesting_level--; if (nesting_level == 0) pgws_proc_queryids[i] = UINT64CONST(0); @@ -970,7 +973,8 @@ pgws_planner_hook(Query *parse, static void pgws_ExecutorStart(QueryDesc *queryDesc, int eflags) { - int i = MyProc - ProcGlobal->allProcs; + int i = MyProc - ProcGlobal->allProcs; + if (pgws_enabled(nesting_level)) pgws_proc_queryids[i] = queryDesc->plannedstmt->queryId; if (prev_ExecutorStart) @@ -981,15 +985,15 @@ pgws_ExecutorStart(QueryDesc *queryDesc, int eflags) static void pgws_ExecutorRun(QueryDesc *queryDesc, - ScanDirection direction, - uint64 count + ScanDirection direction, + uint64 count #if PG_VERSION_NUM >= 100000 && PG_VERSION_NUM < 180000 - , bool execute_once + ,bool execute_once #endif - ) +) { - int i = MyProc - ProcGlobal->allProcs; - uint64 save_queryId = pgws_proc_queryids[i]; + int i = MyProc - ProcGlobal->allProcs; + uint64 save_queryId = pgws_proc_queryids[i]; nesting_level++; PG_TRY(); @@ -1027,8 +1031,8 @@ pgws_ExecutorRun(QueryDesc *queryDesc, static void pgws_ExecutorFinish(QueryDesc *queryDesc) { - int i = MyProc - ProcGlobal->allProcs; - uint64 save_queryId = pgws_proc_queryids[i]; + int i = MyProc - ProcGlobal->allProcs; + uint64 save_queryId = pgws_proc_queryids[i]; nesting_level++; PG_TRY(); @@ -1058,7 +1062,8 @@ pgws_ExecutorFinish(QueryDesc *queryDesc) static void pgws_ExecutorEnd(QueryDesc *queryDesc) { - int i = MyProc - ProcGlobal->allProcs; + int i = MyProc - ProcGlobal->allProcs; + if (nesting_level == 0) pgws_proc_queryids[i] = UINT64CONST(0); @@ -1083,10 +1088,10 @@ pgws_ProcessUtility(PlannedStmt *pstmt, #else char *completionTag #endif - ) +) { - int i = MyProc - ProcGlobal->allProcs; - uint64 save_queryId = 0; + int i = MyProc - ProcGlobal->allProcs; + uint64 save_queryId = 0; if (pgws_enabled(nesting_level)) { @@ -1098,18 +1103,18 @@ pgws_ProcessUtility(PlannedStmt *pstmt, PG_TRY(); { if (prev_ProcessUtility) - prev_ProcessUtility (pstmt, queryString, + prev_ProcessUtility(pstmt, queryString, #if PG_VERSION_NUM >= 140000 - readOnlyTree, + readOnlyTree, #endif - context, params, queryEnv, - dest, + context, params, queryEnv, + dest, #if PG_VERSION_NUM >= 130000 - qc + qc #else - completionTag + completionTag #endif - ); + ); else standard_ProcessUtility(pstmt, queryString, #if PG_VERSION_NUM >= 140000 @@ -1122,7 +1127,7 @@ pgws_ProcessUtility(PlannedStmt *pstmt, #else completionTag #endif - ); + ); nesting_level--; if (nesting_level == 0) pgws_proc_queryids[i] = UINT64CONST(0); diff --git a/pg_wait_sampling.h b/pg_wait_sampling.h index d0e6962..dab773c 100644 --- a/pg_wait_sampling.h +++ b/pg_wait_sampling.h @@ -23,26 +23,26 @@ typedef struct { - int pid; - uint32 wait_event_info; - uint64 queryId; - uint64 count; + int pid; + uint32 wait_event_info; + uint64 queryId; + uint64 count; } ProfileItem; typedef struct { - int pid; - uint32 wait_event_info; - uint64 queryId; - TimestampTz ts; + int pid; + uint32 wait_event_info; + uint64 queryId; + TimestampTz ts; } HistoryItem; typedef struct { - bool wraparound; - Size index; - Size count; - HistoryItem *items; + bool wraparound; + Size index; + Size count; + HistoryItem *items; } History; typedef enum @@ -55,22 +55,22 @@ typedef enum typedef struct { - Latch *latch; - SHMRequest request; + Latch *latch; + SHMRequest request; } CollectorShmqHeader; /* GUC variables */ -extern int pgws_historySize; -extern int pgws_historyPeriod; -extern int pgws_profilePeriod; +extern int pgws_historySize; +extern int pgws_historyPeriod; +extern int pgws_profilePeriod; extern bool pgws_profilePid; -extern int pgws_profileQueries; +extern int pgws_profileQueries; extern bool pgws_sampleCpu; /* pg_wait_sampling.c */ extern CollectorShmqHeader *pgws_collector_hdr; -extern shm_mq *pgws_collector_mq; -extern uint64 *pgws_proc_queryids; +extern shm_mq *pgws_collector_mq; +extern uint64 *pgws_proc_queryids; extern void pgws_init_lock_tag(LOCKTAG *tag, uint32 lock); extern bool pgws_should_sample_proc(PGPROC *proc, int *pid_p, uint32 *wait_event_info_p); From 2cc26e6557a64425cc493f8682ec15b4f13debd0 Mon Sep 17 00:00:00 2001 From: Sergey Shinderuk Date: Thu, 20 Feb 2025 16:55:54 +0300 Subject: [PATCH 10/17] Remove irrelevant and outdated copy-paste --- collector.c | 17 ++--------------- 1 file changed, 2 insertions(+), 15 deletions(-) diff --git a/collector.c b/collector.c index d1d4755..e0aa07d 100644 --- a/collector.c +++ b/collector.c @@ -330,15 +330,8 @@ pgws_collector_main(Datum main_arg) /* * Establish signal handlers. * - * We want CHECK_FOR_INTERRUPTS() to kill off this worker process just as - * it would a normal user backend. To make that happen, we establish a - * signal handler that is a stripped-down version of die(). We don't have - * any equivalent of the backend's command-read loop, where interrupts can - * be processed immediately, so make sure ImmediateInterruptOK is turned - * off. - * - * We also want to respond to the ProcSignal notifications. This is done - * in the upstream provided procsignal_sigusr1_handler, which is + * We want to respond to the ProcSignal notifications. This is done in + * the upstream provided procsignal_sigusr1_handler, which is * automatically used if a bgworker connects to a database. But since our * worker doesn't connect to any database even though it calls * InitPostgres, which will still initializze a new backend and thus @@ -489,12 +482,6 @@ pgws_collector_main(Datum main_arg) MemoryContextReset(collector_context); - /* - * We're done. Explicitly detach the shared memory segment so that we - * don't get a resource leak warning at commit time. This will fire any - * on_dsm_detach callbacks we've registered, as well. Once that's done, - * we can go ahead and exit. - */ ereport(LOG, (errmsg("pg_wait_sampling collector shutting down"))); proc_exit(0); } From 09463b6c041b0af7fec9cd01821e81567eb54d95 Mon Sep 17 00:00:00 2001 From: Sergey Shinderuk Date: Thu, 20 Feb 2025 17:28:33 +0300 Subject: [PATCH 11/17] Add some comments Author: Oleg Tselebrovskiy --- collector.c | 5 ++++- pg_wait_sampling.c | 10 ++++++++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/collector.c b/collector.c index e0aa07d..1923a53 100644 --- a/collector.c +++ b/collector.c @@ -131,6 +131,7 @@ get_next_observation(History *observations) { HistoryItem *result; + /* Check for wraparound */ if (observations->index >= observations->count) { observations->index = 0; @@ -217,6 +218,7 @@ send_history(History *observations, shm_mq_handle *mqh) else count = observations->index; + /* Send array size first since receive_array expects this */ mq_result = shm_mq_send_compat(mqh, sizeof(count), &count, false, true); if (mq_result == SHM_MQ_DETACHED) { @@ -253,6 +255,7 @@ send_profile(HTAB *profile_hash, shm_mq_handle *mqh) Size count = hash_get_num_entries(profile_hash); shm_mq_result mq_result; + /* Send array size first since receive_array expects this */ mq_result = shm_mq_send_compat(mqh, sizeof(count), &count, false, true); if (mq_result == SHM_MQ_DETACHED) { @@ -380,7 +383,7 @@ pgws_collector_main(Datum main_arg) ProcessConfigFile(PGC_SIGHUP); } - /* Wait calculate time to next sample for history or profile */ + /* Calculate time to next sample for history or profile */ current_ts = GetCurrentTimestamp(); history_diff = millisecs_diff(history_ts, current_ts); diff --git a/pg_wait_sampling.c b/pg_wait_sampling.c index d6bb99a..a0cc582 100644 --- a/pg_wait_sampling.c +++ b/pg_wait_sampling.c @@ -44,7 +44,7 @@ void _PG_init(void); static bool shmem_initialized = false; -/* Hooks */ +/* Hooks variables */ static ExecutorStart_hook_type prev_ExecutorStart = NULL; static ExecutorRun_hook_type prev_ExecutorRun = NULL; static ExecutorFinish_hook_type prev_ExecutorFinish = NULL; @@ -65,6 +65,7 @@ static shm_mq *recv_mq = NULL; static shm_mq_handle *recv_mqh = NULL; static LOCKTAG queueTag; +/* Hook functions */ #if PG_VERSION_NUM >= 150000 static shmem_request_hook_type prev_shmem_request_hook = NULL; #endif @@ -126,7 +127,6 @@ static const struct config_enum_entry pgws_profile_queries_options[] = {NULL, 0, false} }; -/* GUC variables */ int pgws_historySize = 5000; int pgws_historyPeriod = 10; int pgws_profilePeriod = 10; @@ -253,6 +253,7 @@ pgws_shmem_startup(void) if (!found) { + /* Create shared objects */ toc = shm_toc_create(PG_WAIT_SAMPLING_MAGIC, pgws, segsize); pgws_collector_hdr = shm_toc_allocate(toc, sizeof(CollectorShmqHeader)); @@ -266,6 +267,7 @@ pgws_shmem_startup(void) } else { + /* Attach to existing shared objects */ toc = shm_toc_attach(PG_WAIT_SAMPLING_MAGIC, pgws); pgws_collector_hdr = shm_toc_lookup(toc, 0, false); pgws_collector_mq = shm_toc_lookup(toc, 1, false); @@ -522,6 +524,7 @@ pg_wait_sampling_get_current(PG_FUNCTION_ARGS) if (!PG_ARGISNULL(0)) { + /* pg_wait_sampling_get_current(pid int4) function */ HistoryItem *item; PGPROC *proc; @@ -535,6 +538,7 @@ pg_wait_sampling_get_current(PG_FUNCTION_ARGS) } else { + /* pg_wait_sampling_current view */ int procCount = ProcGlobal->allProcCount, i, j = 0; @@ -600,6 +604,7 @@ pg_wait_sampling_get_current(PG_FUNCTION_ARGS) } else { + /* nothing left */ SRF_RETURN_DONE(funcctx); } } @@ -621,6 +626,7 @@ pgws_init_lock_tag(LOCKTAG *tag, uint32 lock) tag->locktag_lockmethodid = USER_LOCKMETHOD; } +/* Get array (history or profile data) from shared memory */ static void * receive_array(SHMRequest request, Size item_size, Size *count) { From 957075d8f8dd2a208a6597d9dafe080458eeb617 Mon Sep 17 00:00:00 2001 From: Sergey Shinderuk Date: Thu, 20 Feb 2025 17:04:45 +0300 Subject: [PATCH 12/17] Pass HASH_BLOBS to hash_create() instead of expicit tag_hash As per hash_create docs: > Note: It is deprecated for callers of hash_create() to explicitly specify > string_hash, tag_hash, uint32_hash, or oid_hash. Just set HASH_STRINGS or > HASH_BLOBS. Use HASH_FUNCTION only when you want something other than > one of these. Also there is no need to explicitly specify TopMemoryContext as it is used by default. --- collector.c | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/collector.c b/collector.c index 1923a53..721299f 100644 --- a/collector.c +++ b/collector.c @@ -11,7 +11,6 @@ #include -#include "common/hashfn.h" #include "compat.h" #include "miscadmin.h" #include "pg_wait_sampling.h" @@ -288,9 +287,6 @@ make_profile_hash() { HASHCTL hash_ctl; - hash_ctl.hash = tag_hash; - hash_ctl.hcxt = TopMemoryContext; - if (pgws_profileQueries) hash_ctl.keysize = offsetof(ProfileItem, count); else @@ -298,7 +294,7 @@ make_profile_hash() hash_ctl.entrysize = sizeof(ProfileItem); return hash_create("Waits profile hash", 1024, &hash_ctl, - HASH_FUNCTION | HASH_ELEM); + HASH_ELEM | HASH_BLOBS); } /* From dd524f2dde44b3907aa457777192c19d3dba9097 Mon Sep 17 00:00:00 2001 From: Georgy Shelkovy Date: Fri, 21 Feb 2025 09:08:18 +0500 Subject: [PATCH 13/17] Fix compatibility with pg18 Upstream commit postgres/postgres@525392d changed return type of ExecutorStart_hook API from void to bool. --- pg_wait_sampling.c | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/pg_wait_sampling.c b/pg_wait_sampling.c index 153d875..0a12003 100644 --- a/pg_wait_sampling.c +++ b/pg_wait_sampling.c @@ -73,7 +73,13 @@ static PlannedStmt *pgws_planner_hook(Query *parse, const char *query_string, #endif int cursorOptions, ParamListInfo boundParams); -static void pgws_ExecutorStart(QueryDesc *queryDesc, int eflags); +static +#if PG_VERSION_NUM >= 180000 +bool +#else +void +#endif +pgws_ExecutorStart(QueryDesc *queryDesc, int eflags); static void pgws_ExecutorRun(QueryDesc *queryDesc, ScanDirection direction, uint64 count @@ -965,16 +971,21 @@ pgws_planner_hook(Query *parse, /* * ExecutorStart hook: save queryId for collector */ -static void +static +#if PG_VERSION_NUM >= 180000 +bool +#else +void +#endif pgws_ExecutorStart(QueryDesc *queryDesc, int eflags) { int i = MyProc - ProcGlobal->allProcs; if (pgws_enabled(nesting_level)) pgws_proc_queryids[i] = queryDesc->plannedstmt->queryId; if (prev_ExecutorStart) - prev_ExecutorStart(queryDesc, eflags); + return prev_ExecutorStart(queryDesc, eflags); else - standard_ExecutorStart(queryDesc, eflags); + return standard_ExecutorStart(queryDesc, eflags); } static void From 3c1046c83c0a45e9fd43d7e805e21a916c25ee30 Mon Sep 17 00:00:00 2001 From: Oleg Tselebrovskiy Date: Mon, 24 Feb 2025 14:38:08 +0700 Subject: [PATCH 14/17] Remove units from profile_period and history_period --- pg_wait_sampling.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pg_wait_sampling.c b/pg_wait_sampling.c index 29f487e..a35fb94 100644 --- a/pg_wait_sampling.c +++ b/pg_wait_sampling.c @@ -375,7 +375,7 @@ _PG_init(void) 1, INT_MAX, PGC_SIGHUP, - GUC_UNIT_MS, + 0, NULL, NULL, NULL); @@ -388,7 +388,7 @@ _PG_init(void) 1, INT_MAX, PGC_SIGHUP, - GUC_UNIT_MS, + 0, NULL, NULL, NULL); From 24b2d144cff961c514579b998de59112099fe3c0 Mon Sep 17 00:00:00 2001 From: Oleg Tselebrovskiy Date: Thu, 10 Apr 2025 16:55:29 +0700 Subject: [PATCH 15/17] Fix return of pgws_ExecutorStart since some systems gave warnings when returning from void functions --- pg_wait_sampling.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/pg_wait_sampling.c b/pg_wait_sampling.c index a35fb94..81c37ea 100644 --- a/pg_wait_sampling.c +++ b/pg_wait_sampling.c @@ -995,9 +995,17 @@ pgws_ExecutorStart(QueryDesc *queryDesc, int eflags) if (pgws_enabled(nesting_level)) pgws_proc_queryids[i] = queryDesc->plannedstmt->queryId; if (prev_ExecutorStart) +#if PG_VERSION_NUM >= 180000 return prev_ExecutorStart(queryDesc, eflags); +#else + prev_ExecutorStart(queryDesc, eflags); +#endif else +#if PG_VERSION_NUM >= 180000 return standard_ExecutorStart(queryDesc, eflags); +#else + standard_ExecutorStart(queryDesc, eflags); +#endif } static void From 9d713ce514e5b96b13fd6aa1980b6db11ec9ed45 Mon Sep 17 00:00:00 2001 From: Oleg Tselebrovskiy Date: Wed, 28 May 2025 12:35:29 +0700 Subject: [PATCH 16/17] Revert "Fix return of pgws_ExecutorStart since some systems gave warnings when returning from void functions" This reverts commit 24b2d144cff961c514579b998de59112099fe3c0. --- pg_wait_sampling.c | 8 -------- 1 file changed, 8 deletions(-) diff --git a/pg_wait_sampling.c b/pg_wait_sampling.c index 81c37ea..a35fb94 100644 --- a/pg_wait_sampling.c +++ b/pg_wait_sampling.c @@ -995,17 +995,9 @@ pgws_ExecutorStart(QueryDesc *queryDesc, int eflags) if (pgws_enabled(nesting_level)) pgws_proc_queryids[i] = queryDesc->plannedstmt->queryId; if (prev_ExecutorStart) -#if PG_VERSION_NUM >= 180000 return prev_ExecutorStart(queryDesc, eflags); -#else - prev_ExecutorStart(queryDesc, eflags); -#endif else -#if PG_VERSION_NUM >= 180000 return standard_ExecutorStart(queryDesc, eflags); -#else - standard_ExecutorStart(queryDesc, eflags); -#endif } static void From fafeda026efb260cdcd4e31e21e6166a41521fb9 Mon Sep 17 00:00:00 2001 From: Oleg Tselebrovskiy Date: Wed, 28 May 2025 12:38:45 +0700 Subject: [PATCH 17/17] Revert "Fix compatibility with pg18" This reverts commit dd524f2dde44b3907aa457777192c19d3dba9097. --- pg_wait_sampling.c | 19 ++++--------------- 1 file changed, 4 insertions(+), 15 deletions(-) diff --git a/pg_wait_sampling.c b/pg_wait_sampling.c index a35fb94..f5bd6e0 100644 --- a/pg_wait_sampling.c +++ b/pg_wait_sampling.c @@ -76,13 +76,7 @@ static PlannedStmt *pgws_planner_hook(Query *parse, const char *query_string, #endif int cursorOptions, ParamListInfo boundParams); -static -#if PG_VERSION_NUM >= 180000 -bool -#else -void -#endif -pgws_ExecutorStart(QueryDesc *queryDesc, int eflags); +static void pgws_ExecutorStart(QueryDesc *queryDesc, int eflags); static void pgws_ExecutorRun(QueryDesc *queryDesc, ScanDirection direction, uint64 count @@ -982,12 +976,7 @@ pgws_planner_hook(Query *parse, /* * ExecutorStart hook: save queryId for collector */ -static -#if PG_VERSION_NUM >= 180000 -bool -#else -void -#endif +static void pgws_ExecutorStart(QueryDesc *queryDesc, int eflags) { int i = MyProc - ProcGlobal->allProcs; @@ -995,9 +984,9 @@ pgws_ExecutorStart(QueryDesc *queryDesc, int eflags) if (pgws_enabled(nesting_level)) pgws_proc_queryids[i] = queryDesc->plannedstmt->queryId; if (prev_ExecutorStart) - return prev_ExecutorStart(queryDesc, eflags); + prev_ExecutorStart(queryDesc, eflags); else - return standard_ExecutorStart(queryDesc, eflags); + standard_ExecutorStart(queryDesc, eflags); } static void