From 3feb2f98433afb8150d44a0bf8ffad047f89c20b Mon Sep 17 00:00:00 2001 From: Alexey Kondratov Date: Thu, 4 Feb 2021 00:10:01 +0300 Subject: [PATCH 01/62] Add comment about considering of reverting e6f52a75 --- collector.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/collector.c b/collector.c index f3f141c..913166d 100644 --- a/collector.c +++ b/collector.c @@ -440,6 +440,12 @@ collector_main(Datum main_arg) LockAcquire(&tag, ExclusiveLock, false, false); collector_hdr->request = NO_REQUEST; + /* + * XXX: it is very likely that this TRY/CATCH is useless. If any + * error occurs, collector's bgworker will exit cancelling the lock. + * + * TODO: thus, consider reverting commit e6f52a7547a15. + */ PG_TRY(); { if (request == HISTORY_REQUEST || request == PROFILE_REQUEST) From 394f918f5fed02c6dca97086bcbdc66631ca8ab3 Mon Sep 17 00:00:00 2001 From: Roman Zharkov Date: Thu, 13 May 2021 16:27:26 +0600 Subject: [PATCH 02/62] [refer #PGPRO-4978] Update the .gitignore file. tags: pg_wait_sampling --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index be4254b..0636f19 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ *pg_wait_sampling--1.1.sql .log Dockerfile +/log/ From 9ab0ec778b56d94666c5b65815b6019341562f3e Mon Sep 17 00:00:00 2001 From: Rustem Bapin Date: Mon, 26 Jul 2021 20:02:45 +0400 Subject: [PATCH 03/62] [refer #PGPRO-4791] Revert "Fix another issue with collector infinite loop." This reverts commit 9e56e69030afa9b5bd1a36383c1dd9097c74629a. --- collector.c | 86 ++++++++++++++++++++++------------------------------- 1 file changed, 35 insertions(+), 51 deletions(-) diff --git a/collector.c b/collector.c index 913166d..237061f 100644 --- a/collector.c +++ b/collector.c @@ -440,63 +440,47 @@ collector_main(Datum main_arg) LockAcquire(&tag, ExclusiveLock, false, false); collector_hdr->request = NO_REQUEST; - /* - * XXX: it is very likely that this TRY/CATCH is useless. If any - * error occurs, collector's bgworker will exit cancelling the lock. - * - * TODO: thus, consider reverting commit e6f52a7547a15. - */ - PG_TRY(); + if (request == HISTORY_REQUEST || request == PROFILE_REQUEST) { - if (request == HISTORY_REQUEST || request == PROFILE_REQUEST) - { - shm_mq_result mq_result; - - /* Send history or profile */ - shm_mq_set_sender(collector_mq, MyProc); - mqh = shm_mq_attach(collector_mq, NULL, NULL); - mq_result = shm_mq_wait_for_attach(mqh); - switch (mq_result) - { - case SHM_MQ_SUCCESS: - switch (request) - { - case HISTORY_REQUEST: - send_history(&observations, mqh); - break; - case PROFILE_REQUEST: - send_profile(profile_hash, mqh); - break; - default: - AssertState(false); - } - break; - case SHM_MQ_DETACHED: - ereport(WARNING, - (errmsg("pg_wait_sampling collector: " - "receiver of message queue has been " - "detached"))); - break; - default: - AssertState(false); - } - shm_mq_detach_compat(mqh, collector_mq); - } - else if (request == PROFILE_RESET) + shm_mq_result mq_result; + + /* Send history or profile */ + shm_mq_set_sender(collector_mq, MyProc); + mqh = shm_mq_attach(collector_mq, NULL, NULL); + mq_result = shm_mq_wait_for_attach(mqh); + switch (mq_result) { - /* Reset profile hash */ - hash_destroy(profile_hash); - profile_hash = make_profile_hash(); + case SHM_MQ_SUCCESS: + switch (request) + { + case HISTORY_REQUEST: + send_history(&observations, mqh); + break; + case PROFILE_REQUEST: + send_profile(profile_hash, mqh); + break; + default: + AssertState(false); + } + break; + case SHM_MQ_DETACHED: + ereport(WARNING, + (errmsg("pg_wait_sampling collector: " + "receiver of message queue have been " + "detached"))); + break; + default: + AssertState(false); } - - LockRelease(&tag, ExclusiveLock, false); + shm_mq_detach_compat(mqh, collector_mq); } - PG_CATCH(); + else if (request == PROFILE_RESET) { - LockRelease(&tag, ExclusiveLock, false); - PG_RE_THROW(); + /* Reset profile hash */ + hash_destroy(profile_hash); + profile_hash = make_profile_hash(); } - PG_END_TRY(); + LockRelease(&tag, ExclusiveLock, false); } } From a9109e6693d5c49115725ff8f2ad8c176ca5f11c Mon Sep 17 00:00:00 2001 From: Nikolay Samokhvalov Date: Tue, 28 Sep 2021 09:07:04 -0700 Subject: [PATCH 04/62] Fix the doc: profile_queries is "true" by default MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Noticed that `profile_queries` is `true` by default (and per https://github.com/postgrespro/pg_wait_sampling/blob/3feb2f98433afb8150d44a0bf8ffad047f89c20b/pg_wait_sampling.c#L193) – while README claims it's `false`. --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 0e47ad1..aa539d8 100644 --- a/README.md +++ b/README.md @@ -125,7 +125,7 @@ GUCs. | pg_wait_sampling.history_period | int4 | Period for history sampling in milliseconds | 10 | | pg_wait_sampling.profile_period | int4 | Period for profile sampling in milliseconds | 10 | | pg_wait_sampling.profile_pid | bool | Whether profile should be per pid | true | -| pg_wait_sampling.profile_queries | bool | Whether profile should be per query | false | +| pg_wait_sampling.profile_queries | bool | Whether profile should be per query | true | If `pg_wait_sampling.profile_pid` is set to false, sampling profile wouldn't be collected in per-process manner. In this case the value of pid could would From 77ba2cab55b217f0188270c0fa3c4a475b0f2bbd Mon Sep 17 00:00:00 2001 From: Julien Rouhaud Date: Sat, 23 Oct 2021 13:00:54 +0800 Subject: [PATCH 05/62] Fix compatibility with new shm_mq_send API introduced in pg15. Related upstream commit: 46846433a03dff4f2e08c8a161e54a842da360d6 --- collector.c | 12 +++++++----- compat.c | 11 +++++++++++ pg_wait_sampling.h | 3 +++ 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/collector.c b/collector.c index 237061f..2fa80c3 100644 --- a/collector.c +++ b/collector.c @@ -220,7 +220,7 @@ send_history(History *observations, shm_mq_handle *mqh) else count = observations->index; - mq_result = shm_mq_send(mqh, sizeof(count), &count, false); + mq_result = shm_mq_send_compat(mqh, sizeof(count), &count, false, true); if (mq_result == SHM_MQ_DETACHED) { ereport(WARNING, @@ -230,10 +230,11 @@ send_history(History *observations, shm_mq_handle *mqh) } for (i = 0; i < count; i++) { - mq_result = shm_mq_send(mqh, + mq_result = shm_mq_send_compat(mqh, sizeof(HistoryItem), &observations->items[i], - false); + false, + true); if (mq_result == SHM_MQ_DETACHED) { ereport(WARNING, @@ -255,7 +256,7 @@ send_profile(HTAB *profile_hash, shm_mq_handle *mqh) Size count = hash_get_num_entries(profile_hash); shm_mq_result mq_result; - mq_result = shm_mq_send(mqh, sizeof(count), &count, false); + mq_result = shm_mq_send_compat(mqh, sizeof(count), &count, false, true); if (mq_result == SHM_MQ_DETACHED) { ereport(WARNING, @@ -266,7 +267,8 @@ send_profile(HTAB *profile_hash, shm_mq_handle *mqh) hash_seq_init(&scan_status, profile_hash); while ((item = (ProfileItem *) hash_seq_search(&scan_status)) != NULL) { - mq_result = shm_mq_send(mqh, sizeof(ProfileItem), item, false); + mq_result = shm_mq_send_compat(mqh, sizeof(ProfileItem), item, false, + true); if (mq_result == SHM_MQ_DETACHED) { hash_seq_term(&scan_status); diff --git a/compat.c b/compat.c index 249a53a..7004d7e 100644 --- a/compat.c +++ b/compat.c @@ -13,6 +13,17 @@ shm_mq_detach_compat(shm_mq_handle *mqh, shm_mq *mq) #endif } +inline shm_mq_result +shm_mq_send_compat(shm_mq_handle *mqh, Size nbytes, const void *data, + bool nowait, bool force_flush) +{ +#if PG_VERSION_NUM >= 150000 + return shm_mq_send(mqh, nbytes, data, nowait, force_flush); +#else + return shm_mq_send(mqh, nbytes, data, nowait); +#endif +} + inline TupleDesc CreateTemplateTupleDescCompat(int nattrs, bool hasoid) { diff --git a/pg_wait_sampling.h b/pg_wait_sampling.h index 1001f6f..4c0ddac 100644 --- a/pg_wait_sampling.h +++ b/pg_wait_sampling.h @@ -84,6 +84,9 @@ extern void alloc_history(History *, int); extern void collector_main(Datum main_arg); extern void shm_mq_detach_compat(shm_mq_handle *mqh, shm_mq *mq); +extern shm_mq_result shm_mq_send_compat(shm_mq_handle *mqh, Size nbytes, + const void *data, bool nowait, + bool force_flush); extern TupleDesc CreateTemplateTupleDescCompat(int nattrs, bool hasoid); #endif From 277a4e5abcd94098b19999c8ad898694f0d0b8fc Mon Sep 17 00:00:00 2001 From: Julien Rouhaud Date: Fri, 25 Mar 2022 14:01:39 +0800 Subject: [PATCH 06/62] Fix shmem allocation size. MaxBackends is still 0 when _PG_init() is called, which means that we don't request enough memory in RequestAddinShmemSpace(), while the rest of the code sees (and allocate) a correct value. It's technically usually not a problem as postgres adds an extra 100kB of memory for small unaccounted memory usage, but it's better to avoid relying on it too much. Note that the value is still not guaranteed to be exact as other modules _PG_init() could later change the underlying GUCs, but there is not available API to handle that case accurately. --- pg_wait_sampling.c | 42 ++++++++++++++++++++++++++++++++++++------ 1 file changed, 36 insertions(+), 6 deletions(-) diff --git a/pg_wait_sampling.c b/pg_wait_sampling.c index a90a981..8098e3a 100644 --- a/pg_wait_sampling.c +++ b/pg_wait_sampling.c @@ -17,6 +17,10 @@ #include "miscadmin.h" #include "optimizer/planner.h" #include "pgstat.h" +#include "postmaster/autovacuum.h" +#if PG_VERSION_NUM >= 120000 +#include "replication/walsender.h" +#endif #include "storage/ipc.h" #include "storage/pg_shmem.h" #include "storage/procarray.h" @@ -66,20 +70,46 @@ static void pgws_ExecutorEnd(QueryDesc *queryDesc); /* * Calculate max processes count. - * Look at InitProcGlobal (proc.c) and TotalProcs variable in it - * if something wrong here. + * + * The value has to be in sync with ProcGlobal->allProcCount, initialized in + * InitProcGlobal() (proc.c). + * + * We calculate the value here as it won't initialized when we need need + * it during _PG_init(). + * + * Note 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. */ static int get_max_procs_count(void) { int count = 0; - /* MyProcs, including autovacuum workers and launcher */ - count += MaxBackends; + /* + * MaxBackends: bgworkers, autovacuum workers and launcher. + * This has to be in sync with the value computed in + * InitializeMaxBackends() (postinit.c) + */ + count += MaxConnections + autovacuum_max_workers + 1 + + max_worker_processes; + + /* + * Starting with pg12, wal senders aren't part of MaxConnections anymore + * and have to be accounted for. + */ +#if PG_VERSION_NUM >= 120000 + count += max_wal_senders; +#endif + /* AuxiliaryProcs */ count += NUM_AUXILIARY_PROCS; - /* Prepared xacts */ - count += max_prepared_xacts; return count; } From 452bdcb02db5c20aaee8c53231dd3abb155e659e Mon Sep 17 00:00:00 2001 From: Andrey Sokolov Date: Thu, 31 Mar 2022 16:14:21 +0300 Subject: [PATCH 07/62] Fix typo --- 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 8098e3a..d5a998e 100644 --- a/pg_wait_sampling.c +++ b/pg_wait_sampling.c @@ -74,8 +74,8 @@ static void pgws_ExecutorEnd(QueryDesc *queryDesc); * The value has to be in sync with ProcGlobal->allProcCount, initialized in * InitProcGlobal() (proc.c). * - * We calculate the value here as it won't initialized when we need need - * it during _PG_init(). + * We calculate the value here as it won't initialized when we need it during + * _PG_init(). * * Note that the value returned during _PG_init() might be different from the * value returned later if some third-party modules change one of the From f4a87c5897c519c4e52896a8b3a0ec3f06e88932 Mon Sep 17 00:00:00 2001 From: Andrey Sokolov Date: Fri, 1 Apr 2022 13:27:12 +0300 Subject: [PATCH 08/62] Fix annoying git fatal message --- Makefile | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/Makefile b/Makefile index 65a54e3..ab90e59 100644 --- a/Makefile +++ b/Makefile @@ -28,11 +28,10 @@ $(EXTENSION)--$(EXTVERSION).sql: setup.sql cat $^ > $@ # Prepare the package for PGXN submission -DISTVERSION := $(shell git tag -l | tail -n 1 | cut -d 'v' -f 2) -package: dist dist/$(EXTENSION)-$(DISTVERSION).zip +package: dist .git + $(eval DISTVERSION := $(shell git tag -l | tail -n 1 | cut -d 'v' -f 2)) + $(info Generating zip file for version $(DISTVERSION)...) + git archive --format zip --prefix=$(EXTENSION)-${DISTVERSION}/ --output dist/$(EXTENSION)-${DISTVERSION}.zip HEAD dist: mkdir -p dist - -dist/$(EXTENSION)-$(DISTVERSION).zip: - git archive --format zip --prefix=$(EXTENSION)-$(DISTVERSION)/ --output $@ HEAD From 609975ac219d069552c7e574ce4bc500b25ed010 Mon Sep 17 00:00:00 2001 From: Julien Rouhaud Date: Sat, 14 May 2022 14:54:22 +0800 Subject: [PATCH 09/62] Remove _PG_fini(). Postgres hasn't called this function for more than a decade (even before extensions were introduced), and version 15 officially removed it so let's get rid of it too. --- pg_wait_sampling.c | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/pg_wait_sampling.c b/pg_wait_sampling.c index d5a998e..f42528f 100644 --- a/pg_wait_sampling.c +++ b/pg_wait_sampling.c @@ -39,7 +39,6 @@ PG_MODULE_MAGIC; void _PG_init(void); -void _PG_fini(void); /* Global variables */ bool shmem_initialized = false; @@ -359,22 +358,14 @@ _PG_init(void) */ prev_shmem_startup_hook = shmem_startup_hook; shmem_startup_hook = pgws_shmem_startup; + prev_shmem_startup_hook = shmem_startup_hook; + shmem_startup_hook = pgws_shmem_startup; planner_hook_next = planner_hook; planner_hook = pgws_planner_hook; prev_ExecutorEnd = ExecutorEnd_hook; ExecutorEnd_hook = pgws_ExecutorEnd; } -/* - * Module unload callback - */ -void -_PG_fini(void) -{ - /* Uninstall hooks. */ - shmem_startup_hook = prev_shmem_startup_hook; -} - /* * Find PGPROC entry responsible for given pid assuming ProcArrayLock was * already taken. From 47616672ebdec64292690e2e37632255ba34531d Mon Sep 17 00:00:00 2001 From: Julien Rouhaud Date: Sat, 14 May 2022 15:47:14 +0800 Subject: [PATCH 10/62] Wait 1 second before restarting the bgworker. If for some reason the bgworker keeps crashing, this will limit the amount of traces logged when restarting. --- collector.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/collector.c b/collector.c index 2fa80c3..a49f90c 100644 --- a/collector.c +++ b/collector.c @@ -44,7 +44,7 @@ register_wait_collector(void) memset(&worker, 0, sizeof(worker)); worker.bgw_flags = BGWORKER_SHMEM_ACCESS; worker.bgw_start_time = BgWorkerStart_ConsistentState; - worker.bgw_restart_time = 0; + worker.bgw_restart_time = 1; worker.bgw_notify_pid = 0; snprintf(worker.bgw_library_name, BGW_MAXLEN, "pg_wait_sampling"); snprintf(worker.bgw_function_name, BGW_MAXLEN, CppAsString(collector_main)); From 1a8aa648fb478499fdc02285aaba2f8a390833f6 Mon Sep 17 00:00:00 2001 From: Julien Rouhaud Date: Sat, 14 May 2022 15:55:28 +0800 Subject: [PATCH 11/62] Setup SIGUSR1 procsignal_sigusr1_handler handler. This is normally automatically done when a bgworker declares the BGWORKER_BACKEND_DATABASE_CONNECTION flag, but our bgworker doesn't connect to databases, even though it calls InitPostgres, which will still initialize a new bacckend and thus participate to the ProcSignal infrastructure. This wasn't a problem until recently, but now that DROP DATABASE relies on PROCSIG_BARRIER to work on Windows (see commit 4eb2176318d0561846c1f9fb3c68bede799d640f) we need to respond to ProcSignal notifications. --- collector.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/collector.c b/collector.c index a49f90c..bbf2741 100644 --- a/collector.c +++ b/collector.c @@ -339,8 +339,16 @@ collector_main(Datum main_arg) * 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 + * 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 + * partitipate to the ProcSignal infrastructure. */ pqsignal(SIGTERM, handle_sigterm); + pqsignal(SIGUSR1, procsignal_sigusr1_handler); BackgroundWorkerUnblockSignals(); #if PG_VERSION_NUM >= 110000 @@ -379,6 +387,9 @@ collector_main(Datum main_arg) bool write_history, write_profile; + /* We need an explicit call for at least ProcSignal notifications. */ + CHECK_FOR_INTERRUPTS(); + /* Wait calculate time to next sample for history or profile */ current_ts = GetCurrentTimestamp(); From 5b50e788951caa4cec6606b2b5f39897e983ea05 Mon Sep 17 00:00:00 2001 From: Julien Rouhaud Date: Sat, 14 May 2022 15:59:36 +0800 Subject: [PATCH 12/62] Fix compatibility with pg15 new shmem_request_hook. Hook added upstream in 4f2400cb3f10aa79f99fba680c198237da28dd38. --- pg_wait_sampling.c | 75 +++++++++++++++++++++++++++++++++++----------- 1 file changed, 58 insertions(+), 17 deletions(-) diff --git a/pg_wait_sampling.c b/pg_wait_sampling.c index f42528f..f27a5de 100644 --- a/pg_wait_sampling.c +++ b/pg_wait_sampling.c @@ -58,6 +58,9 @@ shm_mq *recv_mq = NULL; shm_mq_handle *recv_mqh = NULL; LOCKTAG queueTag; +#if PG_VERSION_NUM >= 150000 +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 PlannedStmt *pgws_planner_hook(Query *parse, @@ -73,28 +76,40 @@ static void pgws_ExecutorEnd(QueryDesc *queryDesc); * The value has to be in sync with ProcGlobal->allProcCount, initialized in * InitProcGlobal() (proc.c). * - * We calculate the value here as it won't initialized when we need it during - * _PG_init(). - * - * Note 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. */ static int get_max_procs_count(void) { 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. + */ + Assert(MaxBackends > 0); + count += MaxBackends; +#else /* - * MaxBackends: bgworkers, autovacuum workers and launcher. + * 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 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. */ count += MaxConnections + autovacuum_max_workers + 1 + max_worker_processes; @@ -105,9 +120,11 @@ get_max_procs_count(void) */ #if PG_VERSION_NUM >= 120000 count += max_wal_senders; -#endif +#endif /* pg 12+ */ +#endif /* pg 15- */ + /* End of MaxBackends calculation. */ - /* AuxiliaryProcs */ + /* Add AuxiliaryProcs */ count += NUM_AUXILIARY_PROCS; return count; @@ -265,6 +282,23 @@ setup_gucs() } } +#if PG_VERSION_NUM >= 150000 +/* + * shmem_request hook: request additional shared memory resources. + * + * If you change code here, don't forget to also report the modifications in + * _PG_init() for pg14 and below. + */ +static void +pgws_shmem_request(void) +{ + if (prev_shmem_request_hook) + prev_shmem_request_hook(); + + RequestAddinShmemSpace(pgws_shmem_size()); +} +#endif + /* * Distribute shared memory. */ @@ -344,20 +378,27 @@ _PG_init(void) if (!process_shared_preload_libraries_in_progress) 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 * resources in pgws_shmem_startup(). + * + * If you change code here, don't forget to also report the modifications + * in pgsp_shmem_request() for pg15 and later. */ RequestAddinShmemSpace(pgws_shmem_size()); +#endif register_wait_collector(); /* * Install hooks. */ - prev_shmem_startup_hook = shmem_startup_hook; - shmem_startup_hook = pgws_shmem_startup; +#if PG_VERSION_NUM >= 150000 + prev_shmem_request_hook = shmem_request_hook; + 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; From df1aa39780238219bb353a147794cba32ab28a8a Mon Sep 17 00:00:00 2001 From: "i.kartyshov" Date: Thu, 2 Jun 2022 13:29:23 +0300 Subject: [PATCH 13/62] [refer #PGPRO-6612] Count max procs tags: pg_wait_sampling --- pg_wait_sampling.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/pg_wait_sampling.c b/pg_wait_sampling.c index f27a5de..8c45f10 100644 --- a/pg_wait_sampling.c +++ b/pg_wait_sampling.c @@ -114,6 +114,10 @@ get_max_procs_count(void) count += MaxConnections + autovacuum_max_workers + 1 + max_worker_processes; +#if PG_VERSION_NUM >= 140000 && defined(PGPRO_EE) + count += MaxATX; +#endif + /* * Starting with pg12, wal senders aren't part of MaxConnections anymore * and have to be accounted for. From 81c641ffdcbc7342aaba4e62cb0499ee49061416 Mon Sep 17 00:00:00 2001 From: Georgy Shelkovy Date: Wed, 27 Jul 2022 12:37:52 +0500 Subject: [PATCH 14/62] PostgreSQL 15 support. Pass extra argument to InitPostgres as per upstream commit 31ed3cf746a. --- collector.c | 7 +------ compat.c | 20 ++++++++++++++++++++ pg_wait_sampling.h | 5 +++++ 3 files changed, 26 insertions(+), 6 deletions(-) diff --git a/collector.c b/collector.c index bbf2741..9bb8cfb 100644 --- a/collector.c +++ b/collector.c @@ -350,12 +350,7 @@ collector_main(Datum main_arg) pqsignal(SIGTERM, handle_sigterm); pqsignal(SIGUSR1, procsignal_sigusr1_handler); BackgroundWorkerUnblockSignals(); - -#if PG_VERSION_NUM >= 110000 - InitPostgres(NULL, InvalidOid, NULL, InvalidOid, NULL, false); -#else - InitPostgres(NULL, InvalidOid, NULL, InvalidOid, NULL); -#endif + InitPostgresCompat(NULL, InvalidOid, NULL, InvalidOid, false, false, NULL); SetProcessingMode(NormalProcessing); /* Make pg_wait_sampling recognisable in pg_stat_activity */ diff --git a/compat.c b/compat.c index 7004d7e..f2a57ae 100644 --- a/compat.c +++ b/compat.c @@ -1,5 +1,7 @@ #include "postgres.h" + #include "access/tupdesc.h" +#include "miscadmin.h" #include "pg_wait_sampling.h" @@ -33,3 +35,21 @@ CreateTemplateTupleDescCompat(int nattrs, bool hasoid) return CreateTemplateTupleDesc(nattrs, hasoid); #endif } + +inline void +InitPostgresCompat(const char *in_dbname, Oid dboid, + const char *username, Oid useroid, + bool load_session_libraries, + bool override_allow_connections, + char *out_dbname) +{ +#if PG_VERSION_NUM >= 150000 + InitPostgres(in_dbname, dboid, username, useroid, load_session_libraries, + override_allow_connections, out_dbname); +#elif PG_VERSION_NUM >= 110000 + InitPostgres(in_dbname, dboid, username, useroid, out_dbname, + override_allow_connections); +#else + InitPostgres(in_dbname, dboid, username, useroid, out_dbname); +#endif +} diff --git a/pg_wait_sampling.h b/pg_wait_sampling.h index 4c0ddac..bb0e1d8 100644 --- a/pg_wait_sampling.h +++ b/pg_wait_sampling.h @@ -88,5 +88,10 @@ extern shm_mq_result shm_mq_send_compat(shm_mq_handle *mqh, Size nbytes, const void *data, bool nowait, bool force_flush); extern TupleDesc CreateTemplateTupleDescCompat(int nattrs, bool hasoid); +extern void InitPostgresCompat(const char *in_dbname, Oid dboid, + const char *username, Oid useroid, + bool load_session_libraries, + bool override_allow_connections, + char *out_dbname); #endif From 9dad24b502d3f8b5faff7dd06d38dcf0e34c9292 Mon Sep 17 00:00:00 2001 From: Maksim Milyutin Date: Thu, 11 Aug 2022 00:29:23 +0400 Subject: [PATCH 15/62] Add missing PGDLLEXPORT markings for collector_main function After commit 089480c07, it's necessary for background worker entry points to be marked PGDLLEXPORT, else they aren't findable by LookupBackgroundWorkerFunction(). --- pg_wait_sampling.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pg_wait_sampling.h b/pg_wait_sampling.h index bb0e1d8..0de8d7b 100644 --- a/pg_wait_sampling.h +++ b/pg_wait_sampling.h @@ -81,7 +81,7 @@ extern void init_lock_tag(LOCKTAG *tag, uint32 lock); /* collector.c */ extern void register_wait_collector(void); extern void alloc_history(History *, int); -extern void collector_main(Datum main_arg); +extern PGDLLEXPORT void collector_main(Datum main_arg); extern void shm_mq_detach_compat(shm_mq_handle *mqh, shm_mq *mq); extern shm_mq_result shm_mq_send_compat(shm_mq_handle *mqh, Size nbytes, From d36b971171eb9284dd8a659bca41e0557e4a13f4 Mon Sep 17 00:00:00 2001 From: Zharkov Roman Date: Mon, 15 Aug 2022 11:38:00 +0700 Subject: [PATCH 16/62] Simplify the Makefile to fix MSVC build. Mkvcbuild does not support variables expansion in makefiles, so the build failed on the Windows systems. --- .gitignore | 1 - Makefile | 8 +------- setup.sql => pg_wait_sampling--1.1.sql | 0 3 files changed, 1 insertion(+), 8 deletions(-) rename setup.sql => pg_wait_sampling--1.1.sql (100%) diff --git a/.gitignore b/.gitignore index 0636f19..1c13cb9 100644 --- a/.gitignore +++ b/.gitignore @@ -2,7 +2,6 @@ *.o *.so /results -*pg_wait_sampling--1.1.sql .log Dockerfile /log/ diff --git a/Makefile b/Makefile index ab90e59..e048136 100644 --- a/Makefile +++ b/Makefile @@ -4,14 +4,11 @@ MODULE_big = pg_wait_sampling OBJS = pg_wait_sampling.o collector.o compat.o EXTENSION = pg_wait_sampling -EXTVERSION = 1.1 -DATA_built = pg_wait_sampling--$(EXTVERSION).sql -DATA = pg_wait_sampling--1.0--1.1.sql +DATA = pg_wait_sampling--1.1.sql pg_wait_sampling--1.0--1.1.sql REGRESS = load queries EXTRA_REGRESS_OPTS=--temp-config=$(top_srcdir)/$(subdir)/conf.add -EXTRA_CLEAN = pg_wait_sampling--$(EXTVERSION).sql ifdef USE_PGXS PG_CONFIG = pg_config @@ -24,9 +21,6 @@ include $(top_builddir)/src/Makefile.global include $(top_srcdir)/contrib/contrib-global.mk endif -$(EXTENSION)--$(EXTVERSION).sql: setup.sql - cat $^ > $@ - # Prepare the package for PGXN submission package: dist .git $(eval DISTVERSION := $(shell git tag -l | tail -n 1 | cut -d 'v' -f 2)) diff --git a/setup.sql b/pg_wait_sampling--1.1.sql similarity index 100% rename from setup.sql rename to pg_wait_sampling--1.1.sql From 8ac2e6a86b65121d8c2d8f5cf0fd43fcf225abfe Mon Sep 17 00:00:00 2001 From: Maksim Milyutin Date: Wed, 17 Aug 2022 11:40:28 +0400 Subject: [PATCH 17/62] Move all inlined compatible wrappers into header file Previous implementation segregated compatible inline function wrappers into separate .c file that prevented inlining of these functions into caller points in other modules. In current patch all these fuctions with `static inline` specification have been moved to header file compat.h. This emulates behavior of macro expanding in case when compiler performs inlining. Resolve https://github.com/postgrespro/pg_wait_sampling/issues/45 --- Makefile | 2 +- collector.c | 1 + compat.c => compat.h | 41 +++++++++++++++++++++++++++-------------- pg_wait_sampling.c | 1 + pg_wait_sampling.h | 11 ----------- 5 files changed, 30 insertions(+), 26 deletions(-) rename compat.c => compat.h (76%) diff --git a/Makefile b/Makefile index ab90e59..9869ec9 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ # contrib/pg_wait_sampling/Makefile MODULE_big = pg_wait_sampling -OBJS = pg_wait_sampling.o collector.o compat.o +OBJS = pg_wait_sampling.o collector.o EXTENSION = pg_wait_sampling EXTVERSION = 1.1 diff --git a/collector.c b/collector.c index 9bb8cfb..2bfe534 100644 --- a/collector.c +++ b/collector.c @@ -26,6 +26,7 @@ #include "utils/resowner.h" #include "pgstat.h" +#include "compat.h" #include "pg_wait_sampling.h" static volatile sig_atomic_t shutdown_requested = false; diff --git a/compat.c b/compat.h similarity index 76% rename from compat.c rename to compat.h index f2a57ae..3f471ce 100644 --- a/compat.c +++ b/compat.h @@ -1,11 +1,32 @@ +/* + * compat.h + * Definitions for function wrappers compatible between PG versions. + * + * Copyright (c) 2015-2022, Postgres Professional + * + * IDENTIFICATION + * contrib/pg_wait_sampling/compat.h + */ +#ifndef __COMPAT_H__ +#define __COMPAT_H__ + #include "postgres.h" #include "access/tupdesc.h" #include "miscadmin.h" +#include "storage/shm_mq.h" -#include "pg_wait_sampling.h" +static inline TupleDesc +CreateTemplateTupleDescCompat(int nattrs, bool hasoid) +{ +#if PG_VERSION_NUM >= 120000 + return CreateTemplateTupleDesc(nattrs); +#else + return CreateTemplateTupleDesc(nattrs, hasoid); +#endif +} -inline void +static inline void shm_mq_detach_compat(shm_mq_handle *mqh, shm_mq *mq) { #if PG_VERSION_NUM >= 100000 @@ -15,7 +36,7 @@ shm_mq_detach_compat(shm_mq_handle *mqh, shm_mq *mq) #endif } -inline shm_mq_result +static inline shm_mq_result shm_mq_send_compat(shm_mq_handle *mqh, Size nbytes, const void *data, bool nowait, bool force_flush) { @@ -26,17 +47,7 @@ shm_mq_send_compat(shm_mq_handle *mqh, Size nbytes, const void *data, #endif } -inline TupleDesc -CreateTemplateTupleDescCompat(int nattrs, bool hasoid) -{ -#if PG_VERSION_NUM >= 120000 - return CreateTemplateTupleDesc(nattrs); -#else - return CreateTemplateTupleDesc(nattrs, hasoid); -#endif -} - -inline void +static inline void InitPostgresCompat(const char *in_dbname, Oid dboid, const char *username, Oid useroid, bool load_session_libraries, @@ -53,3 +64,5 @@ InitPostgresCompat(const char *in_dbname, Oid dboid, InitPostgres(in_dbname, dboid, username, useroid, out_dbname); #endif } + +#endif diff --git a/pg_wait_sampling.c b/pg_wait_sampling.c index 8c45f10..deda26c 100644 --- a/pg_wait_sampling.c +++ b/pg_wait_sampling.c @@ -34,6 +34,7 @@ #include "utils/memutils.h" /* TopMemoryContext. Actually for PG 9.6 only, * but there should be no harm for others. */ +#include "compat.h" #include "pg_wait_sampling.h" PG_MODULE_MAGIC; diff --git a/pg_wait_sampling.h b/pg_wait_sampling.h index 0de8d7b..a33d707 100644 --- a/pg_wait_sampling.h +++ b/pg_wait_sampling.h @@ -83,15 +83,4 @@ extern void register_wait_collector(void); extern void alloc_history(History *, int); extern PGDLLEXPORT void collector_main(Datum main_arg); -extern void shm_mq_detach_compat(shm_mq_handle *mqh, shm_mq *mq); -extern shm_mq_result shm_mq_send_compat(shm_mq_handle *mqh, Size nbytes, - const void *data, bool nowait, - bool force_flush); -extern TupleDesc CreateTemplateTupleDescCompat(int nattrs, bool hasoid); -extern void InitPostgresCompat(const char *in_dbname, Oid dboid, - const char *username, Oid useroid, - bool load_session_libraries, - bool override_allow_connections, - char *out_dbname); - #endif From 2d38a7989ae2b7c58347cb88b8d23a66b6c559e8 Mon Sep 17 00:00:00 2001 From: Sergey Shinderuk Date: Wed, 31 Aug 2022 15:39:12 +0300 Subject: [PATCH 18/62] PGPRO-6599: Avoid race when accessing the request shared variable. Consider the following sequence of events: 1. Session 1 calls pg_wait_sampling_reset_profile and sets the request shared variable to PROFILE_RESET. 2. The collector reads request and saves PROFILE_RESET to a local variable. 3. Session 2 queries pg_wait_sampling_profile, which sets request to PROFILE_REQUEST and waits for the collector in shm_mq_receive. 4. The collector continues and clears shared request, thus dropping PROFILE_REQUEST from Session 2. 5. Session 2 waits indefinitely in shm_mq_receive. A similar example with query cancellation: 1. Session 1 queries pg_wait_sampling_history and sets request to HISTORY_REQUEST. 2. Session 1 cancels the query while waiting for the collector. 3. The collector reads request and saves HISTORY_REQUEST to a local variable. 4. Session 2 queries pg_wait_sampling_profile, sets request to PROFILE_REQUEST and waits for the collector. 5. The collector continues and responds to HISTORY_REQUEST. 6. Session 2 receives history data and renders them as profile data returning invalid counts. These interleavings are avoided by acquiring the collector lock before reading request from shared memory in the collector. But we also need to hold the collector lock when we set request in receive_array in a backend. Otherwise, the following interleaving is possible: 1. Session 1 calls pg_wait_sampling_reset_profile and sets request to PROFILE_RESET. 2. Session 2 queries pg_wait_sampling_profile, acquires and releases the collector lock. 3. The collector acquires the lock, reads request and saves PROFILE_RESET to a local variable. 4. Session 2 sets request to PROFILE_REQUEST. 5. The collector clears request, and PROFILE_REQUEST is lost. 6. Session 2 waits indefinitely in shm_mq_receive. Same for the second example above. This patch, however, doesn't prevent loosing PROFILE_RESET requests: 1. Session 1 calls pg_wait_sampling_reset_profile and sets request to PROFILE_RESET. 2. Session 2 queries pg_wait_sampling_profile before the collector reads request. 3. The collector reads PROFILE_REQUEST, while PROFILE_RESET is lost. To fix this, we could make pg_wait_sampling_reset_profile wait for the collector, but we decided not to, as loosing a PROFILE_RESET isn't critical. Resolves #48. Author: Roman Zharkov Reported-By: Alexander Lakhin Reviewed-By: Maksim Milyutin, Sergey Shinderuk --- collector.c | 3 ++- pg_wait_sampling.c | 6 ++---- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/collector.c b/collector.c index 9bb8cfb..80a559a 100644 --- a/collector.c +++ b/collector.c @@ -441,11 +441,12 @@ collector_main(Datum main_arg) if (collector_hdr->request != NO_REQUEST) { LOCKTAG tag; - SHMRequest request = collector_hdr->request; + SHMRequest request; init_lock_tag(&tag, PGWS_COLLECTOR_LOCK); LockAcquire(&tag, ExclusiveLock, false, false); + request = collector_hdr->request; collector_hdr->request = NO_REQUEST; if (request == HISTORY_REQUEST || request == PROFILE_REQUEST) diff --git a/pg_wait_sampling.c b/pg_wait_sampling.c index 8c45f10..6877a12 100644 --- a/pg_wait_sampling.c +++ b/pg_wait_sampling.c @@ -594,13 +594,11 @@ receive_array(SHMRequest request, Size item_size, Size *count) init_lock_tag(&queueTag, PGWS_QUEUE_LOCK); LockAcquire(&queueTag, ExclusiveLock, false, false); - /* Ensure collector has processed previous request */ init_lock_tag(&collectorTag, PGWS_COLLECTOR_LOCK); LockAcquire(&collectorTag, ExclusiveLock, false, false); - LockRelease(&collectorTag, ExclusiveLock, false); - recv_mq = shm_mq_create(collector_mq, COLLECTOR_QUEUE_SIZE); collector_hdr->request = request; + LockRelease(&collectorTag, ExclusiveLock, false); if (!collector_hdr->latch) ereport(ERROR, (errcode(ERRCODE_INTERNAL_ERROR), @@ -770,9 +768,9 @@ pg_wait_sampling_reset_profile(PG_FUNCTION_ARGS) init_lock_tag(&tagCollector, PGWS_COLLECTOR_LOCK); LockAcquire(&tagCollector, ExclusiveLock, false, false); + collector_hdr->request = PROFILE_RESET; LockRelease(&tagCollector, ExclusiveLock, false); - collector_hdr->request = PROFILE_RESET; SetLatch(collector_hdr->latch); LockRelease(&tag, ExclusiveLock, false); From 0f1b89a0d6bc161edbd9aa216e3bbfc8034e6b63 Mon Sep 17 00:00:00 2001 From: Sergey Shinderuk Date: Thu, 1 Sep 2022 14:46:11 +0300 Subject: [PATCH 19/62] PGPRO-6601: Add prefix to global variables and functions. This is necessary to avoid name collision with other modules. Where possible, make variables and functions static. --- collector.c | 40 +++++++------- pg_wait_sampling.c | 130 ++++++++++++++++++++++----------------------- pg_wait_sampling.h | 15 +++--- 3 files changed, 90 insertions(+), 95 deletions(-) diff --git a/collector.c b/collector.c index 23cb7f0..e1e00cf 100644 --- a/collector.c +++ b/collector.c @@ -37,7 +37,7 @@ static void handle_sigterm(SIGNAL_ARGS); * Register background worker for collecting waits history. */ void -register_wait_collector(void) +pgws_register_wait_collector(void) { BackgroundWorker worker; @@ -48,7 +48,7 @@ register_wait_collector(void) worker.bgw_restart_time = 1; worker.bgw_notify_pid = 0; snprintf(worker.bgw_library_name, BGW_MAXLEN, "pg_wait_sampling"); - snprintf(worker.bgw_function_name, BGW_MAXLEN, CppAsString(collector_main)); + snprintf(worker.bgw_function_name, BGW_MAXLEN, CppAsString(pgws_collector_main)); snprintf(worker.bgw_name, BGW_MAXLEN, "pg_wait_sampling collector"); worker.bgw_main_arg = (Datum) 0; RegisterBackgroundWorker(&worker); @@ -57,7 +57,7 @@ register_wait_collector(void) /* * Allocate memory for waits history. */ -void +static void alloc_history(History *observations, int count) { observations->items = (HistoryItem *) palloc0(sizeof(HistoryItem) * count); @@ -151,7 +151,7 @@ probe_waits(History *observations, HTAB *profile_hash, TimestampTz ts = GetCurrentTimestamp(); /* Realloc waits history if needed */ - newSize = collector_hdr->historySize; + newSize = pgws_collector_hdr->historySize; if (observations->count != newSize) realloc_history(observations, newSize); @@ -173,8 +173,8 @@ probe_waits(History *observations, HTAB *profile_hash, item.pid = proc->pid; item.wait_event_info = proc->wait_event_info; - if (collector_hdr->profileQueries) - item.queryId = proc_queryids[i]; + if (pgws_collector_hdr->profileQueries) + item.queryId = pgws_proc_queryids[i]; else item.queryId = 0; @@ -292,7 +292,7 @@ make_profile_hash() hash_ctl.hash = tag_hash; hash_ctl.hcxt = TopMemoryContext; - if (collector_hdr->profileQueries) + if (pgws_collector_hdr->profileQueries) hash_ctl.keysize = offsetof(ProfileItem, count); else hash_ctl.keysize = offsetof(ProfileItem, queryId); @@ -321,7 +321,7 @@ millisecs_diff(TimestampTz tz1, TimestampTz tz2) * Main routine of wait history collector. */ void -collector_main(Datum main_arg) +pgws_collector_main(Datum main_arg) { HTAB *profile_hash = NULL; History observations; @@ -358,13 +358,13 @@ collector_main(Datum main_arg) pgstat_report_appname("pg_wait_sampling collector"); profile_hash = make_profile_hash(); - collector_hdr->latch = &MyProc->procLatch; + pgws_collector_hdr->latch = &MyProc->procLatch; CurrentResourceOwner = ResourceOwnerCreate(NULL, "pg_wait_sampling collector"); collector_context = AllocSetContextCreate(TopMemoryContext, "pg_wait_sampling context", ALLOCSET_DEFAULT_SIZES); old_context = MemoryContextSwitchTo(collector_context); - alloc_history(&observations, collector_hdr->historySize); + alloc_history(&observations, pgws_collector_hdr->historySize); MemoryContextSwitchTo(old_context); ereport(LOG, (errmsg("pg_wait_sampling collector started"))); @@ -391,8 +391,8 @@ collector_main(Datum main_arg) history_diff = millisecs_diff(history_ts, current_ts); profile_diff = millisecs_diff(profile_ts, current_ts); - history_period = collector_hdr->historyPeriod; - profile_period = collector_hdr->profilePeriod; + 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); @@ -400,7 +400,7 @@ collector_main(Datum main_arg) if (write_history || write_profile) { probe_waits(&observations, profile_hash, - write_history, write_profile, collector_hdr->profilePid); + write_history, write_profile, pgws_collector_hdr->profilePid); if (write_history) { @@ -439,24 +439,24 @@ collector_main(Datum main_arg) ResetLatch(&MyProc->procLatch); /* Handle request if any */ - if (collector_hdr->request != NO_REQUEST) + if (pgws_collector_hdr->request != NO_REQUEST) { LOCKTAG tag; SHMRequest request; - init_lock_tag(&tag, PGWS_COLLECTOR_LOCK); + pgws_init_lock_tag(&tag, PGWS_COLLECTOR_LOCK); LockAcquire(&tag, ExclusiveLock, false, false); - request = collector_hdr->request; - collector_hdr->request = NO_REQUEST; + request = pgws_collector_hdr->request; + pgws_collector_hdr->request = NO_REQUEST; if (request == HISTORY_REQUEST || request == PROFILE_REQUEST) { shm_mq_result mq_result; /* Send history or profile */ - shm_mq_set_sender(collector_mq, MyProc); - mqh = shm_mq_attach(collector_mq, NULL, NULL); + shm_mq_set_sender(pgws_collector_mq, MyProc); + mqh = shm_mq_attach(pgws_collector_mq, NULL, NULL); mq_result = shm_mq_wait_for_attach(mqh); switch (mq_result) { @@ -482,7 +482,7 @@ collector_main(Datum main_arg) default: AssertState(false); } - shm_mq_detach_compat(mqh, collector_mq); + shm_mq_detach_compat(mqh, pgws_collector_mq); } else if (request == PROFILE_RESET) { diff --git a/pg_wait_sampling.c b/pg_wait_sampling.c index 22732dd..c77f980 100644 --- a/pg_wait_sampling.c +++ b/pg_wait_sampling.c @@ -41,23 +41,21 @@ PG_MODULE_MAGIC; void _PG_init(void); -/* Global variables */ -bool shmem_initialized = false; +static bool shmem_initialized = false; /* Hooks */ static ExecutorEnd_hook_type prev_ExecutorEnd = NULL; static planner_hook_type planner_hook_next = NULL; -/* Shared memory variables */ -shm_toc *toc = NULL; -shm_mq *collector_mq = NULL; -uint64 *proc_queryids = NULL; -CollectorShmqHeader *collector_hdr = NULL; +/* Pointers to shared memory objects */ +shm_mq *pgws_collector_mq = NULL; +uint64 *pgws_proc_queryids = NULL; +CollectorShmqHeader *pgws_collector_hdr = NULL; /* Receiver (backend) local shm_mq pointers and lock */ -shm_mq *recv_mq = NULL; -shm_mq_handle *recv_mqh = NULL; -LOCKTAG queueTag; +static shm_mq *recv_mq = NULL; +static shm_mq_handle *recv_mqh = NULL; +static LOCKTAG queueTag; #if PG_VERSION_NUM >= 150000 static shmem_request_hook_type prev_shmem_request_hook = NULL; @@ -218,63 +216,63 @@ setup_gucs() if (!strcmp(name, "pg_wait_sampling.history_size")) { history_size_found = true; - var->integer.variable = &collector_hdr->historySize; - collector_hdr->historySize = 5000; + 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 = &collector_hdr->historyPeriod; - collector_hdr->historyPeriod = 10; + 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 = &collector_hdr->profilePeriod; - collector_hdr->profilePeriod = 10; + 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 = &collector_hdr->profilePid; - collector_hdr->profilePid = 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->_bool.variable = &collector_hdr->profileQueries; - collector_hdr->profileQueries = true; + var->_bool.variable = &pgws_collector_hdr->profileQueries; + pgws_collector_hdr->profileQueries = true; } } if (!history_size_found) DefineCustomIntVariable("pg_wait_sampling.history_size", "Sets size of waits history.", NULL, - &collector_hdr->historySize, 5000, 100, INT_MAX, + &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, - &collector_hdr->historyPeriod, 10, 1, INT_MAX, + &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, - &collector_hdr->profilePeriod, 10, 1, INT_MAX, + &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, - &collector_hdr->profilePid, true, + &pgws_collector_hdr->profilePid, true, PGC_SUSET, 0, shmem_bool_guc_check_hook, NULL, NULL); if (!profile_queries_found) DefineCustomBoolVariable("pg_wait_sampling.profile_queries", "Sets whether profile should be collected per query.", NULL, - &collector_hdr->profileQueries, true, + &pgws_collector_hdr->profileQueries, true, PGC_SUSET, 0, shmem_bool_guc_check_hook, NULL, NULL); if (history_size_found @@ -310,9 +308,10 @@ pgws_shmem_request(void) static void pgws_shmem_startup(void) { - bool found; - Size segsize = pgws_shmem_size(); - void *pgws; + bool found; + Size segsize = pgws_shmem_size(); + void *pgws; + shm_toc *toc; pgws = ShmemInitStruct("pg_wait_sampling", segsize, &found); @@ -320,14 +319,14 @@ pgws_shmem_startup(void) { toc = shm_toc_create(PG_WAIT_SAMPLING_MAGIC, pgws, segsize); - collector_hdr = shm_toc_allocate(toc, sizeof(CollectorShmqHeader)); - shm_toc_insert(toc, 0, collector_hdr); - collector_mq = shm_toc_allocate(toc, COLLECTOR_QUEUE_SIZE); - shm_toc_insert(toc, 1, collector_mq); - proc_queryids = shm_toc_allocate(toc, + pgws_collector_hdr = shm_toc_allocate(toc, sizeof(CollectorShmqHeader)); + shm_toc_insert(toc, 0, pgws_collector_hdr); + 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, proc_queryids); - MemSet(proc_queryids, 0, 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(); @@ -337,13 +336,13 @@ pgws_shmem_startup(void) toc = shm_toc_attach(PG_WAIT_SAMPLING_MAGIC, pgws); #if PG_VERSION_NUM >= 100000 - collector_hdr = shm_toc_lookup(toc, 0, false); - collector_mq = shm_toc_lookup(toc, 1, false); - proc_queryids = shm_toc_lookup(toc, 2, false); + pgws_collector_hdr = shm_toc_lookup(toc, 0, false); + pgws_collector_mq = shm_toc_lookup(toc, 1, false); + pgws_proc_queryids = shm_toc_lookup(toc, 2, false); #else - collector_hdr = shm_toc_lookup(toc, 0); - collector_mq = shm_toc_lookup(toc, 1); - proc_queryids = shm_toc_lookup(toc, 2); + pgws_collector_hdr = shm_toc_lookup(toc, 0); + pgws_collector_mq = shm_toc_lookup(toc, 1); + pgws_proc_queryids = shm_toc_lookup(toc, 2); #endif } @@ -356,7 +355,7 @@ pgws_shmem_startup(void) /* * Check shared memory is initialized. Report an error otherwise. */ -void +static void check_shmem(void) { if (!shmem_initialized) @@ -395,7 +394,7 @@ _PG_init(void) RequestAddinShmemSpace(pgws_shmem_size()); #endif - register_wait_collector(); + pgws_register_wait_collector(); /* * Install hooks. @@ -490,7 +489,7 @@ pg_wait_sampling_get_current(PG_FUNCTION_ARGS) item = ¶ms->items[0]; item->pid = proc->pid; item->wait_event_info = proc->wait_event_info; - item->queryId = proc_queryids[proc - ProcGlobal->allProcs]; + item->queryId = pgws_proc_queryids[proc - ProcGlobal->allProcs]; funcctx->max_calls = 1; } else @@ -508,7 +507,7 @@ pg_wait_sampling_get_current(PG_FUNCTION_ARGS) { params->items[j].pid = proc->pid; params->items[j].wait_event_info = proc->wait_event_info; - params->items[j].queryId = proc_queryids[i]; + params->items[j].queryId = pgws_proc_queryids[i]; j++; } } @@ -569,7 +568,7 @@ typedef struct } Profile; void -init_lock_tag(LOCKTAG *tag, uint32 lock) +pgws_init_lock_tag(LOCKTAG *tag, uint32 lock) { tag->locktag_field1 = PG_WAIT_SAMPLING_MAGIC; tag->locktag_field2 = lock; @@ -592,20 +591,20 @@ receive_array(SHMRequest request, Size item_size, Size *count) MemoryContext oldctx; /* Ensure nobody else trying to send request to queue */ - init_lock_tag(&queueTag, PGWS_QUEUE_LOCK); + pgws_init_lock_tag(&queueTag, PGWS_QUEUE_LOCK); LockAcquire(&queueTag, ExclusiveLock, false, false); - init_lock_tag(&collectorTag, PGWS_COLLECTOR_LOCK); + pgws_init_lock_tag(&collectorTag, PGWS_COLLECTOR_LOCK); LockAcquire(&collectorTag, ExclusiveLock, false, false); - recv_mq = shm_mq_create(collector_mq, COLLECTOR_QUEUE_SIZE); - collector_hdr->request = request; + recv_mq = shm_mq_create(pgws_collector_mq, COLLECTOR_QUEUE_SIZE); + pgws_collector_hdr->request = request; LockRelease(&collectorTag, ExclusiveLock, false); - if (!collector_hdr->latch) + if (!pgws_collector_hdr->latch) ereport(ERROR, (errcode(ERRCODE_INTERNAL_ERROR), errmsg("pg_wait_sampling collector wasn't started"))); - SetLatch(collector_hdr->latch); + SetLatch(pgws_collector_hdr->latch); shm_mq_set_receiver(recv_mq, MyProc); @@ -736,7 +735,7 @@ pg_wait_sampling_get_profile(PG_FUNCTION_ARGS) else nulls[2] = true; - if (collector_hdr->profileQueries) + if (pgws_collector_hdr->profileQueries) values[3] = Int64GetDatumFast(item->queryId); else values[3] = (Datum) 0; @@ -758,23 +757,22 @@ PG_FUNCTION_INFO_V1(pg_wait_sampling_reset_profile); Datum pg_wait_sampling_reset_profile(PG_FUNCTION_ARGS) { - LOCKTAG tag; - LOCKTAG tagCollector; + LOCKTAG collectorTag; check_shmem(); - init_lock_tag(&tag, PGWS_QUEUE_LOCK); + pgws_init_lock_tag(&queueTag, PGWS_QUEUE_LOCK); - LockAcquire(&tag, ExclusiveLock, false, false); + LockAcquire(&queueTag, ExclusiveLock, false, false); - init_lock_tag(&tagCollector, PGWS_COLLECTOR_LOCK); - LockAcquire(&tagCollector, ExclusiveLock, false, false); - collector_hdr->request = PROFILE_RESET; - LockRelease(&tagCollector, ExclusiveLock, false); + pgws_init_lock_tag(&collectorTag, PGWS_COLLECTOR_LOCK); + LockAcquire(&collectorTag, ExclusiveLock, false, false); + pgws_collector_hdr->request = PROFILE_RESET; + LockRelease(&collectorTag, ExclusiveLock, false); - SetLatch(collector_hdr->latch); + SetLatch(pgws_collector_hdr->latch); - LockRelease(&tag, ExclusiveLock, false); + LockRelease(&queueTag, ExclusiveLock, false); PG_RETURN_VOID(); } @@ -894,8 +892,8 @@ pgws_planner_hook(Query *parse, StaticAssertExpr(sizeof(parse->queryId) == sizeof(uint32), "queryId size is not uint32"); #endif - if (!proc_queryids[i]) - proc_queryids[i] = parse->queryId; + if (!pgws_proc_queryids[i]) + pgws_proc_queryids[i] = parse->queryId; } @@ -921,7 +919,7 @@ static void pgws_ExecutorEnd(QueryDesc *queryDesc) { if (MyProc) - proc_queryids[MyProc - ProcGlobal->allProcs] = UINT64CONST(0); + pgws_proc_queryids[MyProc - ProcGlobal->allProcs] = UINT64CONST(0); if (prev_ExecutorEnd) prev_ExecutorEnd(queryDesc); diff --git a/pg_wait_sampling.h b/pg_wait_sampling.h index a33d707..29425fc 100644 --- a/pg_wait_sampling.h +++ b/pg_wait_sampling.h @@ -71,16 +71,13 @@ typedef struct } CollectorShmqHeader; /* pg_wait_sampling.c */ -extern void check_shmem(void); -extern CollectorShmqHeader *collector_hdr; -extern shm_mq *collector_mq; -extern uint64 *proc_queryids; -extern void read_current_wait(PGPROC *proc, HistoryItem *item); -extern void init_lock_tag(LOCKTAG *tag, uint32 lock); +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); /* collector.c */ -extern void register_wait_collector(void); -extern void alloc_history(History *, int); -extern PGDLLEXPORT void collector_main(Datum main_arg); +extern void pgws_register_wait_collector(void); +extern PGDLLEXPORT void pgws_collector_main(Datum main_arg); #endif From 8b05241a8cfc87b3ccc633a0bd3c020cc8c76150 Mon Sep 17 00:00:00 2001 From: Maksim Milyutin Date: Wed, 25 May 2022 22:39:50 +0400 Subject: [PATCH 20/62] Add building/testing with PG14 and PG15beta3 in Travis CI. Drop builds for PG9.6. --- .travis.yml | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/.travis.yml b/.travis.yml index 69c38e0..4ea7793 100644 --- a/.travis.yml +++ b/.travis.yml @@ -17,15 +17,16 @@ script: - docker-compose run tests env: - - PG_VERSION=9.6 CHECK_CODE=clang - - PG_VERSION=9.6 CHECK_CODE=cppcheck - - PG_VERSION=9.6 CHECK_CODE=false - - PG_VERSION=10 CHECK_CODE=clang - - PG_VERSION=10 CHECK_CODE=cppcheck - - PG_VERSION=10 CHECK_CODE=false - - PG_VERSION=11 CHECK_CODE=clang - - PG_VERSION=11 CHECK_CODE=false - - PG_VERSION=12 CHECK_CODE=clang - - PG_VERSION=12 CHECK_CODE=false - - PG_VERSION=13 CHECK_CODE=clang - - PG_VERSION=13 CHECK_CODE=false + - PG_VERSION=10 CHECK_CODE=clang + - PG_VERSION=10 CHECK_CODE=cppcheck + - PG_VERSION=10 CHECK_CODE=false + - PG_VERSION=11 CHECK_CODE=clang + - PG_VERSION=11 CHECK_CODE=false + - PG_VERSION=12 CHECK_CODE=clang + - PG_VERSION=12 CHECK_CODE=false + - PG_VERSION=13 CHECK_CODE=clang + - PG_VERSION=13 CHECK_CODE=false + - PG_VERSION=14 CHECK_CODE=clang + - PG_VERSION=14 CHECK_CODE=false + - PG_VERSION=15beta3 CHECK_CODE=clang + - PG_VERSION=15beta3 CHECK_CODE=false From 57c1ecfed6413a033fc66c1c612ee1cacd1ff836 Mon Sep 17 00:00:00 2001 From: Sergey Shinderuk Date: Fri, 30 Sep 2022 12:38:01 +0300 Subject: [PATCH 21/62] Bump PGXN version to 1.1.4 --- META.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/META.json b/META.json index f26554e..0f09ab0 100644 --- a/META.json +++ b/META.json @@ -2,7 +2,7 @@ "name": "pg_wait_sampling", "abstract": "Sampling based statistics of wait events", "description": "pg_wait_sampling provides functions for detailed per backend and per query statistics about PostgreSQL wait events", - "version": "1.1.3", + "version": "1.1.4", "maintainer": [ "Alexander Korotkov ", "Ildus Kurbangaliev " @@ -21,7 +21,7 @@ "pg_wait_sampling": { "file": "pg_wait_sampling--1.1.sql", "docfile": "README.md", - "version": "1.1.3", + "version": "1.1.4", "abstract": "Sampling based statistics of wait events" } }, From 71a4935bcd4e97408f9955d812310bfac631b923 Mon Sep 17 00:00:00 2001 From: Michael Zhilin Date: Tue, 4 Oct 2022 11:46:55 +0300 Subject: [PATCH 22/62] [README] add information about pre-built packages --- README.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/README.md b/README.md index aa539d8..f4bd9bd 100644 --- a/README.md +++ b/README.md @@ -53,6 +53,12 @@ and supports PostgreSQL 9.6+. Installation ------------ +Pre-built `pg_wait_sampling` packages are provided in official PostgreSQL +repository: https://download.postgresql.org/pub/repos/ + +Manual build +------------ + `pg_wait_sampling` is PostgreSQL extension which requires PostgreSQL 9.6 or higher. Before build and install you should ensure following: From 067ba2f5ea6680c74572182b6c3159d5be7b37c2 Mon Sep 17 00:00:00 2001 From: Sergey Shinderuk Date: Wed, 12 Oct 2022 17:04:16 +0300 Subject: [PATCH 23/62] Revert "[refer #PGPRO-6612] Count max procs" We deliberately don't include max_prepared_xact and needn't include MaxATX either. We iterate over ProcGlobal->allProcCount which doesn't include them. This reverts commit df1aa39780238219bb353a147794cba32ab28a8a. --- pg_wait_sampling.c | 4 ---- 1 file changed, 4 deletions(-) diff --git a/pg_wait_sampling.c b/pg_wait_sampling.c index c77f980..783c839 100644 --- a/pg_wait_sampling.c +++ b/pg_wait_sampling.c @@ -113,10 +113,6 @@ get_max_procs_count(void) count += MaxConnections + autovacuum_max_workers + 1 + max_worker_processes; -#if PG_VERSION_NUM >= 140000 && defined(PGPRO_EE) - count += MaxATX; -#endif - /* * Starting with pg12, wal senders aren't part of MaxConnections anymore * and have to be accounted for. From f7e49c6d908e8ec8381843dcb25324503d310153 Mon Sep 17 00:00:00 2001 From: Marina Polyakova Date: Fri, 18 Nov 2022 08:13:53 +0300 Subject: [PATCH 24/62] Fix build due to new checks in PostgreSQL 16 Use UInt64GetDatum instead of Int64GetDatumFast for uint64 variables as the input data type is now checked (see the commit c8b2ef05f481ef06326d7b9f3eb14b303f215c7e in PostgreSQL 16). --- pg_wait_sampling.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pg_wait_sampling.c b/pg_wait_sampling.c index 783c839..8017a6f 100644 --- a/pg_wait_sampling.c +++ b/pg_wait_sampling.c @@ -546,7 +546,7 @@ pg_wait_sampling_get_current(PG_FUNCTION_ARGS) else nulls[2] = true; - values[3] = Int64GetDatumFast(item->queryId); + values[3] = UInt64GetDatum(item->queryId); tuple = heap_form_tuple(funcctx->tuple_desc, values, nulls); SRF_RETURN_NEXT(funcctx, HeapTupleGetDatum(tuple)); @@ -732,11 +732,11 @@ pg_wait_sampling_get_profile(PG_FUNCTION_ARGS) nulls[2] = true; if (pgws_collector_hdr->profileQueries) - values[3] = Int64GetDatumFast(item->queryId); + values[3] = UInt64GetDatum(item->queryId); else values[3] = (Datum) 0; - values[4] = Int64GetDatumFast(item->count); + values[4] = UInt64GetDatum(item->count); tuple = heap_form_tuple(funcctx->tuple_desc, values, nulls); @@ -848,7 +848,7 @@ pg_wait_sampling_get_history(PG_FUNCTION_ARGS) else nulls[3] = true; - values[4] = Int64GetDatumFast(item->queryId); + values[4] = UInt64GetDatum(item->queryId); tuple = heap_form_tuple(funcctx->tuple_desc, values, nulls); history->index++; From fb3eb361798f129921d7a5ec147bc78838e210ee Mon Sep 17 00:00:00 2001 From: Marina Polyakova Date: Mon, 21 Nov 2022 17:38:20 +0300 Subject: [PATCH 25/62] Fix build due to changes in PostgreSQL 16 See the commit 3057465acfbea2f3dd7a914a1478064022c6eecd (Replace the sorted array of GUC variables with a hash table.) in PostgreSQL 16. --- compat.h | 15 +++++++++++++++ pg_wait_sampling.c | 3 +-- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/compat.h b/compat.h index 3f471ce..32874f7 100644 --- a/compat.h +++ b/compat.h @@ -15,6 +15,7 @@ #include "access/tupdesc.h" #include "miscadmin.h" #include "storage/shm_mq.h" +#include "utils/guc_tables.h" static inline TupleDesc CreateTemplateTupleDescCompat(int nattrs, bool hasoid) @@ -65,4 +66,18 @@ 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 8017a6f..f31b869 100644 --- a/pg_wait_sampling.c +++ b/pg_wait_sampling.c @@ -198,8 +198,7 @@ setup_gucs() profile_pid_found = false, profile_queries_found = false; - guc_vars = get_guc_variables(); - numOpts = GetNumConfigOptions(); + get_guc_variables_compat(&guc_vars, &numOpts); for (i = 0; i < numOpts; i++) { From 0138dc7dad68d197ff6ab5f56c08f11b25dbeaac Mon Sep 17 00:00:00 2001 From: Marina Polyakova Date: Mon, 21 Nov 2022 17:41:45 +0300 Subject: [PATCH 26/62] Fix compiler warnings due to new checks in PostgreSQL 16 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit See the commit 0fe954c28584169938e5c0738cfaa9930ce77577 (Add -Wshadow=compatible-local to the standard compilation flags) in PostgreSQL 16. pg_wait_sampling.c: In function ‘pg_wait_sampling_get_current’: pg_wait_sampling.c:454:42: warning: declaration of ‘params’ shadows a previous local [-Wshadow=compatible-local] 454 | WaitCurrentContext *params; | ^~~~~~ pg_wait_sampling.c:446:34: note: shadowed declaration is here 446 | WaitCurrentContext *params; | ^~~~~~ --- pg_wait_sampling.c | 1 - 1 file changed, 1 deletion(-) diff --git a/pg_wait_sampling.c b/pg_wait_sampling.c index f31b869..eaa0327 100644 --- a/pg_wait_sampling.c +++ b/pg_wait_sampling.c @@ -451,7 +451,6 @@ pg_wait_sampling_get_current(PG_FUNCTION_ARGS) { MemoryContext oldcontext; TupleDesc tupdesc; - WaitCurrentContext *params; funcctx = SRF_FIRSTCALL_INIT(); From 95264289f6eec91a4ca2169fc1150c5f666020c8 Mon Sep 17 00:00:00 2001 From: Roman Zharkov Date: Wed, 23 Nov 2022 10:25:33 +0700 Subject: [PATCH 27/62] Replace AssertState() with Assert(). AssertArg and AssertState were removed in PostgreSQL by commit #b1099eca8f. --- collector.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/collector.c b/collector.c index e1e00cf..dcb9695 100644 --- a/collector.c +++ b/collector.c @@ -470,7 +470,7 @@ pgws_collector_main(Datum main_arg) send_profile(profile_hash, mqh); break; default: - AssertState(false); + Assert(false); } break; case SHM_MQ_DETACHED: @@ -480,7 +480,7 @@ pgws_collector_main(Datum main_arg) "detached"))); break; default: - AssertState(false); + Assert(false); } shm_mq_detach_compat(mqh, pgws_collector_mq); } From 9d1a922502c56cde924dcf6a800d58db6c693587 Mon Sep 17 00:00:00 2001 From: Maksim Milyutin Date: Wed, 8 Feb 2023 17:36:14 +0400 Subject: [PATCH 28/62] Add stable PG15 version on travis CI --- .travis.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 4ea7793..15670ee 100644 --- a/.travis.yml +++ b/.travis.yml @@ -28,5 +28,5 @@ env: - PG_VERSION=13 CHECK_CODE=false - PG_VERSION=14 CHECK_CODE=clang - PG_VERSION=14 CHECK_CODE=false - - PG_VERSION=15beta3 CHECK_CODE=clang - - PG_VERSION=15beta3 CHECK_CODE=false + - PG_VERSION=15 CHECK_CODE=clang + - PG_VERSION=15 CHECK_CODE=false From 054c6032830023a58d7cee3ccb4a7aebdfdcac9c Mon Sep 17 00:00:00 2001 From: Christoph Berg Date: Wed, 20 Sep 2023 13:00:14 +0200 Subject: [PATCH 29/62] Remove debian directory The Debian packaging repository has been moved to Debian infrastructure to be handled easier there. Delete the debian/ directory in the upstream repository since it's outdated already and would only become more stale over time. New location: https://salsa.debian.org/postgresql/pg-wait-sampling --- debian/changelog | 11 ----------- debian/compat | 1 - debian/control | 39 --------------------------------------- debian/control.in | 15 --------------- debian/copyright | 11 ----------- debian/pgversions | 1 - debian/rules | 27 --------------------------- debian/source/format | 1 - debian/tests/control | 3 --- debian/tests/installcheck | 5 ----- debian/watch | 3 --- 11 files changed, 117 deletions(-) delete mode 100644 debian/changelog delete mode 100644 debian/compat delete mode 100644 debian/control delete mode 100644 debian/control.in delete mode 100644 debian/copyright delete mode 100644 debian/pgversions delete mode 100755 debian/rules delete mode 100644 debian/source/format delete mode 100644 debian/tests/control delete mode 100755 debian/tests/installcheck delete mode 100644 debian/watch diff --git a/debian/changelog b/debian/changelog deleted file mode 100644 index 8f87a47..0000000 --- a/debian/changelog +++ /dev/null @@ -1,11 +0,0 @@ -pg-wait-sampling (1.1.2-1) unstable; urgency=medium - - * New upstream version compatible with PG13 - - -- Adrien Nayrat Wed, 28 Oct 2020 09:03:03 +0000 - -pg-wait-sampling (1.1.1-1) unstable; urgency=medium - - * Release 1.1.1 - - -- Adrien Nayrat Wed, 17 Jun 2020 12:26:59 +0000 diff --git a/debian/compat b/debian/compat deleted file mode 100644 index ec63514..0000000 --- a/debian/compat +++ /dev/null @@ -1 +0,0 @@ -9 diff --git a/debian/control b/debian/control deleted file mode 100644 index fd27614..0000000 --- a/debian/control +++ /dev/null @@ -1,39 +0,0 @@ -Source: pg-wait-sampling -Section: database -Priority: optional -Maintainer: Adrien Nayrat -Standards-Version: 4.5.0 -Build-Depends: debhelper (>=9~), postgresql-server-dev-all (>= 141~) -Homepage: https://github.com/postgrespro/pg_wait_sampling -Vcs-Browser: https://github.com/postgrespro/pg_wait_sampling -Vcs-Git: https://github.com/postgrespro/pg_wait_sampling.git - -Package: postgresql-9.6-pg-wait-sampling -Architecture: any -Depends: ${misc:Depends}, ${shlibs:Depends}, postgresql-9.6, -Description: pg_wait-sampling provides functions for detailed per backend - and per query statistics about PostgreSQL wait events - -Package: postgresql-10-pg-wait-sampling -Architecture: any -Depends: ${misc:Depends}, ${shlibs:Depends}, postgresql-10, -Description: pg_wait-sampling provides functions for detailed per backend - and per query statistics about PostgreSQL wait events - -Package: postgresql-11-pg-wait-sampling -Architecture: any -Depends: ${misc:Depends}, ${shlibs:Depends}, postgresql-11, -Description: pg_wait-sampling provides functions for detailed per backend - and per query statistics about PostgreSQL wait events - -Package: postgresql-12-pg-wait-sampling -Architecture: any -Depends: ${misc:Depends}, ${shlibs:Depends}, postgresql-12, -Description: pg_wait-sampling provides functions for detailed per backend - and per query statistics about PostgreSQL wait events - -Package: postgresql-13-pg-wait-sampling -Architecture: any -Depends: ${misc:Depends}, ${shlibs:Depends}, postgresql-13, -Description: pg_wait-sampling provides functions for detailed per backend - and per query statistics about PostgreSQL wait events diff --git a/debian/control.in b/debian/control.in deleted file mode 100644 index 3ed5fe0..0000000 --- a/debian/control.in +++ /dev/null @@ -1,15 +0,0 @@ -Source: pg-wait-sampling -Section: database -Priority: optional -Maintainer: Adrien Nayrat -Standards-Version: 4.5.0 -Build-Depends: debhelper (>=9~), postgresql-server-dev-all (>= 141~) -Homepage: https://github.com/postgrespro/pg_wait_sampling -Vcs-Browser: https://github.com/postgrespro/pg_wait_sampling -Vcs-Git: https://github.com/postgrespro/pg_wait_sampling.git - -Package: postgresql-PGVERSION-pg-wait-sampling -Architecture: any -Depends: ${misc:Depends}, ${shlibs:Depends}, postgresql-PGVERSION, -Description: pg_wait-sampling provides functions for detailed per backend - and per query statistics about PostgreSQL wait events diff --git a/debian/copyright b/debian/copyright deleted file mode 100644 index c68d0d1..0000000 --- a/debian/copyright +++ /dev/null @@ -1,11 +0,0 @@ -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 -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. - -IN NO EVENT SHALL POSTGRES PROFESSIONAL BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF POSTGRES PROFESSIONAL HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - -POSTGRES PROFESSIONAL SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND POSTGRES PROFESSIONAL HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. diff --git a/debian/pgversions b/debian/pgversions deleted file mode 100644 index 7e9cdc1..0000000 --- a/debian/pgversions +++ /dev/null @@ -1 +0,0 @@ -9.6+ diff --git a/debian/rules b/debian/rules deleted file mode 100755 index da410f6..0000000 --- a/debian/rules +++ /dev/null @@ -1,27 +0,0 @@ -#!/usr/bin/make -f - -PKGVER = $(shell dpkg-parsechangelog | awk -F '[:-]' '/^Version:/ { print substr($$2, 2) }') -EXCLUDE = --exclude-vcs --exclude=debian - -include /usr/share/postgresql-common/pgxs_debian_control.mk - -override_dh_auto_build: - # do nothing - -override_dh_auto_test: - # nothing to do here, upstream tests used, see debian/tests/* - -override_dh_auto_install: - # build all supported versions - +pg_buildext loop postgresql-%v-pg-wait-sampling - -override_dh_installdocs: - dh_installdocs --all README.md - -override_dh_auto_clean: - $(MAKE) clean USE_PGXS=1 PG_CONFIG=/usr/bin/pg_config - -orig: debian/control clean - cd .. && tar czf pg-wait-sampling_$(PKGVER).orig.tar.gz $(EXCLUDE) pg_wait_sampling-$(PKGVER) -%: - dh $@ diff --git a/debian/source/format b/debian/source/format deleted file mode 100644 index 163aaf8..0000000 --- a/debian/source/format +++ /dev/null @@ -1 +0,0 @@ -3.0 (quilt) diff --git a/debian/tests/control b/debian/tests/control deleted file mode 100644 index b0dc4e8..0000000 --- a/debian/tests/control +++ /dev/null @@ -1,3 +0,0 @@ -Depends: @, postgresql-server-dev-all -Tests: installcheck -Restrictions: allow-stderr diff --git a/debian/tests/installcheck b/debian/tests/installcheck deleted file mode 100755 index 432cb15..0000000 --- a/debian/tests/installcheck +++ /dev/null @@ -1,5 +0,0 @@ -#!/bin/sh - -set -eu - -pg_buildext -o "shared_preload_libraries=pg_wait_sampling" installcheck diff --git a/debian/watch b/debian/watch deleted file mode 100644 index 38c54ed..0000000 --- a/debian/watch +++ /dev/null @@ -1,3 +0,0 @@ -version=3 -opts="uversionmangle=s/_/./g" \ - https://github.com/postgrespro/pg_wait_sampling/releases .*/archive/v(.*).tar.gz From a44de6748b7e3e7dc258e070faa572e716eecd93 Mon Sep 17 00:00:00 2001 From: Georgy Shelkovy Date: Thu, 12 Oct 2023 08:51:54 +0500 Subject: [PATCH 30/62] Postgres 17 support --- compat.h | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/compat.h b/compat.h index 32874f7..5fcc814 100644 --- a/compat.h +++ b/compat.h @@ -55,7 +55,10 @@ InitPostgresCompat(const char *in_dbname, Oid dboid, bool override_allow_connections, char *out_dbname) { -#if PG_VERSION_NUM >= 150000 +#if PG_VERSION_NUM >= 170000 + InitPostgres(in_dbname, dboid, username, useroid, (load_session_libraries ? INIT_PG_LOAD_SESSION_LIBS : 0) | + (override_allow_connections ? INIT_PG_OVERRIDE_ALLOW_CONNS : 0), out_dbname); +#elif PG_VERSION_NUM >= 150000 InitPostgres(in_dbname, dboid, username, useroid, load_session_libraries, override_allow_connections, out_dbname); #elif PG_VERSION_NUM >= 110000 From e566633dc657716dc5ed584fc0774efee094e654 Mon Sep 17 00:00:00 2001 From: Sergey Shinderuk Date: Tue, 17 Oct 2023 10:29:31 +0300 Subject: [PATCH 31/62] Drop PGXN metadata, update README --- META.json | 51 --------------------------------------------------- Makefile | 9 --------- README.md | 11 ----------- 3 files changed, 71 deletions(-) delete mode 100644 META.json diff --git a/META.json b/META.json deleted file mode 100644 index 0f09ab0..0000000 --- a/META.json +++ /dev/null @@ -1,51 +0,0 @@ -{ - "name": "pg_wait_sampling", - "abstract": "Sampling based statistics of wait events", - "description": "pg_wait_sampling provides functions for detailed per backend and per query statistics about PostgreSQL wait events", - "version": "1.1.4", - "maintainer": [ - "Alexander Korotkov ", - "Ildus Kurbangaliev " - ], - "license": { - "PostgreSQL": "http://www.postgresql.org/about/licence" - }, - "prereqs": { - "runtime": { - "requires": { - "PostgreSQL": "9.6.0" - } - } - }, - "provides": { - "pg_wait_sampling": { - "file": "pg_wait_sampling--1.1.sql", - "docfile": "README.md", - "version": "1.1.4", - "abstract": "Sampling based statistics of wait events" - } - }, - "resources": { - "bugtracker": { - "web": "https://github.com/postgrespro/pg_wait_sampling/issues" - }, - "repository": { - "url": "https://github.com/postgrespro/pg_wait_sampling.git", - "web": "https://github.com/postgrespro/pg_wait_sampling", - "type": "git" - } - }, - "generated_by": "Ildus Kurbangaliev", - "meta-spec": { - "version": "1.0.0", - "url": "http://pgxn.org/meta/spec.txt" - }, - "tags": [ - "waits", - "sampling", - "background worker", - "wait events", - "waits history", - "waits profile" - ] -} diff --git a/Makefile b/Makefile index 67de107..32711a3 100644 --- a/Makefile +++ b/Makefile @@ -20,12 +20,3 @@ top_builddir = ../.. include $(top_builddir)/src/Makefile.global include $(top_srcdir)/contrib/contrib-global.mk endif - -# Prepare the package for PGXN submission -package: dist .git - $(eval DISTVERSION := $(shell git tag -l | tail -n 1 | cut -d 'v' -f 2)) - $(info Generating zip file for version $(DISTVERSION)...) - git archive --format zip --prefix=$(EXTENSION)-${DISTVERSION}/ --output dist/$(EXTENSION)-${DISTVERSION}.zip HEAD - -dist: - mkdir -p dist diff --git a/README.md b/README.md index f4bd9bd..079c9a9 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,4 @@ [![Build Status](https://travis-ci.com/postgrespro/pg_wait_sampling.svg?branch=master)](https://travis-ci.com/postgrespro/pg_wait_sampling) -[![PGXN version](https://badge.fury.io/pg/pg_wait_sampling.svg)](https://badge.fury.io/pg/pg_wait_sampling) [![GitHub license](https://img.shields.io/badge/license-PostgreSQL-blue.svg)](https://raw.githubusercontent.com/postgrespro/pg_wait_sampling/master/LICENSE) `pg_wait_sampling` – sampling based statistics of wait events @@ -159,16 +158,6 @@ your bug reports. If you're lacking of some functionality in `pg_wait_sampling` and feeling power to implement it then you're welcome to make pull requests. -Releases --------- - -New features are developed in feature-branches and then merged into [master](https://github.com/postgrespro/pg_wait_sampling/tree/master). To make a new release: - -1) Bump `PGXN` version in the `META.json`. -2) Merge [master](https://github.com/postgrespro/pg_wait_sampling/tree/master) into [stable](https://github.com/postgrespro/pg_wait_sampling/tree/stable). -3) Tag new release in the [stable](https://github.com/postgrespro/pg_wait_sampling/tree/stable) with `git tag -a v1.1.X`, where the last digit is used for indicating compatible shared library changes and bugfixes. Second digit is used to indicate extension schema change, i.e. when `ALTER EXTENSION pg_wait_sampling UPDATE;` is required. -4) Merge [stable](https://github.com/postgrespro/pg_wait_sampling/tree/stable) into [debian](https://github.com/postgrespro/pg_wait_sampling/tree/debian). This separate branch is used to independently support `Debian` packaging and @anayrat with @df7cb have an access there. - Authors ------- From bf6bc4b1091cb3256013b8d1492ac4ee21e5d3da Mon Sep 17 00:00:00 2001 From: Sergey Shinderuk Date: Tue, 17 Oct 2023 11:49:43 +0300 Subject: [PATCH 32/62] Update Travis config --- .travis.yml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.travis.yml b/.travis.yml index 15670ee..aa9e796 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,7 +2,7 @@ os: - linux sudo: required -dist: trusty +dist: jammy language: c @@ -17,10 +17,8 @@ script: - docker-compose run tests env: - - PG_VERSION=10 CHECK_CODE=clang - - PG_VERSION=10 CHECK_CODE=cppcheck - - PG_VERSION=10 CHECK_CODE=false - PG_VERSION=11 CHECK_CODE=clang + - PG_VERSION=11 CHECK_CODE=cppcheck - PG_VERSION=11 CHECK_CODE=false - PG_VERSION=12 CHECK_CODE=clang - PG_VERSION=12 CHECK_CODE=false @@ -30,3 +28,5 @@ env: - PG_VERSION=14 CHECK_CODE=false - PG_VERSION=15 CHECK_CODE=clang - PG_VERSION=15 CHECK_CODE=false + - PG_VERSION=16 CHECK_CODE=clang + - PG_VERSION=16 CHECK_CODE=false From b8037cf0885a54d08132d0e93d943b642b9cb1df Mon Sep 17 00:00:00 2001 From: Sergey Shinderuk Date: Fri, 20 Oct 2023 16:06:41 +0300 Subject: [PATCH 33/62] Build and test on Travis without Docker Install postgresql packages from the PGDG apt repo. --- .gitignore | 7 +++-- .travis.yml | 46 ++++++++++++------------------- Dockerfile.tmpl | 34 ----------------------- docker-compose.yml | 2 -- run-tests.sh | 22 +++++++++++++++ run_tests.sh | 67 ---------------------------------------------- 6 files changed, 42 insertions(+), 136 deletions(-) delete mode 100644 Dockerfile.tmpl delete mode 100644 docker-compose.yml create mode 100755 run-tests.sh delete mode 100755 run_tests.sh diff --git a/.gitignore b/.gitignore index 1c13cb9..e066fb5 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,6 @@ -.deps *.o *.so -/results -.log -Dockerfile +/.deps/ /log/ +/results/ +/tmp_check/ diff --git a/.travis.yml b/.travis.yml index aa9e796..b4296bf 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,32 +1,20 @@ -os: - - linux - -sudo: required dist: jammy - language: c - -services: - - docker - -install: - - sed -e 's/${CHECK_CODE}/'${CHECK_CODE}/g -e 's/${PG_VERSION}/'${PG_VERSION}/g Dockerfile.tmpl > Dockerfile - - docker-compose build - -script: - - docker-compose run tests - env: - - PG_VERSION=11 CHECK_CODE=clang - - PG_VERSION=11 CHECK_CODE=cppcheck - - PG_VERSION=11 CHECK_CODE=false - - PG_VERSION=12 CHECK_CODE=clang - - PG_VERSION=12 CHECK_CODE=false - - PG_VERSION=13 CHECK_CODE=clang - - PG_VERSION=13 CHECK_CODE=false - - PG_VERSION=14 CHECK_CODE=clang - - PG_VERSION=14 CHECK_CODE=false - - PG_VERSION=15 CHECK_CODE=clang - - PG_VERSION=15 CHECK_CODE=false - - PG_VERSION=16 CHECK_CODE=clang - - PG_VERSION=16 CHECK_CODE=false +- PG_MAJOR=16 +- PG_MAJOR=15 +- PG_MAJOR=14 +- PG_MAJOR=13 +- PG_MAJOR=12 +- PG_MAJOR=11 +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 +- sudo apt-get update +- sudo systemctl stop postgresql +- sudo apt-get install -y --no-install-recommends postgresql-${PG_MAJOR} postgresql-server-dev-${PG_MAJOR} +- sudo systemctl stop postgresql +script: ./run-tests.sh +after_script: +- cat regression.diffs +- cat logfile diff --git a/Dockerfile.tmpl b/Dockerfile.tmpl deleted file mode 100644 index eaf70c8..0000000 --- a/Dockerfile.tmpl +++ /dev/null @@ -1,34 +0,0 @@ -FROM postgres:${PG_VERSION}-alpine - -ENV LANG=C.UTF-8 PGDATA=/pg/data - -RUN if [ "${CHECK_CODE}" = "clang" ] ; then \ - # echo 'http://dl-3.alpinelinux.org/alpine/edge/main' > /etc/apk/repositories; \ - # Use alpine/v3.6/main instead of alpine/edge/main to fix version of clang to '8.*.*' - apk --no-cache add clang-analyzer make musl-dev gcc --repository http://dl-cdn.alpinelinux.org/alpine/v3.6/main; \ - fi - -RUN if [ "${CHECK_CODE}" = "cppcheck" ] ; then \ - apk --no-cache add cppcheck --repository http://dl-cdn.alpinelinux.org/alpine/v3.6/community; \ - fi - -RUN if [ "${CHECK_CODE}" = "false" ] ; then \ - # echo 'http://dl-3.alpinelinux.org/alpine/edge/main' > /etc/apk/repositories; \ - # Use alpine/v3.6/main instead of alpine/edge/main to fix version of clang to '8.*.*' - # Install clang as well, since LLVM is enabled in PG_VERSION >= 11 by default - apk --no-cache add curl python3 gcc make musl-dev llvm clang clang-dev \ - --repository http://dl-cdn.alpinelinux.org/alpine/v3.6/community \ - --repository http://dl-cdn.alpinelinux.org/alpine/v3.6/main; \ - fi - -RUN mkdir -p ${PGDATA} && \ - mkdir /pg/src && \ - chown postgres:postgres ${PGDATA} && \ - chmod -R a+rwx /usr/local/lib/postgresql && \ - chmod a+rwx /usr/local/share/postgresql/extension - -ADD . /pg/src -WORKDIR /pg/src -RUN chmod -R go+rwX /pg/src -USER postgres -ENTRYPOINT PGDATA=${PGDATA} CHECK_CODE=${CHECK_CODE} bash run_tests.sh diff --git a/docker-compose.yml b/docker-compose.yml deleted file mode 100644 index 471ab77..0000000 --- a/docker-compose.yml +++ /dev/null @@ -1,2 +0,0 @@ -tests: - build: . diff --git a/run-tests.sh b/run-tests.sh new file mode 100755 index 0000000..f3f1bba --- /dev/null +++ b/run-tests.sh @@ -0,0 +1,22 @@ +#!/bin/bash +set -ev + +PATH=/usr/lib/postgresql/$PG_MAJOR/bin:$PATH +export PGDATA=/var/lib/postgresql/$PG_MAJOR/test +export COPT=-Werror +export USE_PGXS=1 + +sudo chmod 1777 /var/lib/postgresql/$PG_MAJOR +sudo chmod 1777 /var/run/postgresql + +make clean +make + +sudo -E env PATH=$PATH make install + +initdb +echo "shared_preload_libraries = pg_wait_sampling" >> $PGDATA/postgresql.conf + +pg_ctl -l logfile start +make installcheck +pg_ctl stop diff --git a/run_tests.sh b/run_tests.sh deleted file mode 100755 index 3bdeb32..0000000 --- a/run_tests.sh +++ /dev/null @@ -1,67 +0,0 @@ -#!/bin/bash - -# This is a main testing script for: -# * regression tests -# * testgres-based tests -# * cmocka-based tests -# Copyright (c) 2017, Postgres Professional - -set -eux - -echo CHECK_CODE=$CHECK_CODE - -status=0 - -# perform code analysis if necessary -if [ "$CHECK_CODE" = "clang" ]; then - scan-build --status-bugs make USE_PGXS=1 || status=$? - exit $status - -elif [ "$CHECK_CODE" = "cppcheck" ]; then - cppcheck \ - --template "{file} ({line}): {severity} ({id}): {message}" \ - --enable=warning,portability,performance \ - --suppress=redundantAssignment \ - --suppress=uselessAssignmentPtrArg \ - --suppress=literalWithCharPtrCompare \ - --suppress=incorrectStringBooleanError \ - --std=c89 *.c *.h 2> cppcheck.log - - if [ -s cppcheck.log ]; then - cat cppcheck.log - status=1 # error - fi - - exit $status -fi - -# don't forget to "make clean" -make USE_PGXS=1 clean - -# initialize database -initdb - -# build extension -make USE_PGXS=1 install - -# check build -status=$? -if [ $status -ne 0 ]; then exit $status; fi - -# add pg_wait_sampling to shared_preload_libraries and restart cluster 'test' -echo "shared_preload_libraries = 'pg_wait_sampling'" >> $PGDATA/postgresql.conf -echo "port = 55435" >> $PGDATA/postgresql.conf -pg_ctl start -l /tmp/postgres.log -w - -# check startup -status=$? -if [ $status -ne 0 ]; then cat /tmp/postgres.log; fi - -# run regression tests -export PG_REGRESS_DIFF_OPTS="-w -U3" # for alpine's diff (BusyBox) -PGPORT=55435 make USE_PGXS=1 installcheck || status=$? - -# show diff if it exists -if test -f regression.diffs; then cat regression.diffs; fi - -exit $status From 92097fdb226ac2c897c76a9b3f692f6b372f92fd Mon Sep 17 00:00:00 2001 From: Sergey Shinderuk Date: Fri, 20 Oct 2023 16:54:44 +0300 Subject: [PATCH 34/62] README: mention adding to shared_preload_libraries Fixes #65 --- README.md | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 079c9a9..d8b03dc 100644 --- a/README.md +++ b/README.md @@ -73,8 +73,17 @@ Typical installation procedure may look like this: $ cd pg_wait_sampling $ make USE_PGXS=1 $ sudo make USE_PGXS=1 install + +Then add `shared_preload_libraries = pg_wait_sampling` to `postgresql.conf` and +restart the server. + +To test your installation: + $ make USE_PGXS=1 installcheck - $ psql DB -c "CREATE EXTENSION pg_wait_sampling;" + +To create the extension in the target database: + + CREATE EXTENSION pg_wait_sampling; Compilation on Windows is not supported, since the extension uses symbols from PostgreSQL that are not exported. From 22212ba7406b043653d478c00385bd376f47d4b3 Mon Sep 17 00:00:00 2001 From: "Anton A. Melnikov" Date: Wed, 8 Nov 2023 15:48:04 +0300 Subject: [PATCH 35/62] PostgreSQL 17 support. Caused by: - 4800a5dfb4c46d22b5d05f16c615bea6ff24a2bb (PostgreSQL) Refactor InitPostgres() to use bitwise option flags --- collector.c | 2 +- compat.h | 18 +++++++++++------- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/collector.c b/collector.c index dcb9695..7f5a914 100644 --- a/collector.c +++ b/collector.c @@ -351,7 +351,7 @@ pgws_collector_main(Datum main_arg) pqsignal(SIGTERM, handle_sigterm); pqsignal(SIGUSR1, procsignal_sigusr1_handler); BackgroundWorkerUnblockSignals(); - InitPostgresCompat(NULL, InvalidOid, NULL, InvalidOid, false, false, NULL); + InitPostgresCompat(NULL, InvalidOid, NULL, InvalidOid, 0, NULL); SetProcessingMode(NormalProcessing); /* Make pg_wait_sampling recognisable in pg_stat_activity */ diff --git a/compat.h b/compat.h index 5fcc814..a425c2a 100644 --- a/compat.h +++ b/compat.h @@ -48,22 +48,26 @@ shm_mq_send_compat(shm_mq_handle *mqh, Size nbytes, const void *data, #endif } +#if PG_VERSION_NUM < 170000 +#define INIT_PG_LOAD_SESSION_LIBS 0x0001 +#define INIT_PG_OVERRIDE_ALLOW_CONNS 0x0002 +#endif + static inline void InitPostgresCompat(const char *in_dbname, Oid dboid, const char *username, Oid useroid, - bool load_session_libraries, - bool override_allow_connections, + bits32 flags, char *out_dbname) { #if PG_VERSION_NUM >= 170000 - InitPostgres(in_dbname, dboid, username, useroid, (load_session_libraries ? INIT_PG_LOAD_SESSION_LIBS : 0) | - (override_allow_connections ? INIT_PG_OVERRIDE_ALLOW_CONNS : 0), out_dbname); + InitPostgres(in_dbname, dboid, username, useroid, flags, out_dbname); #elif PG_VERSION_NUM >= 150000 - InitPostgres(in_dbname, dboid, username, useroid, load_session_libraries, - override_allow_connections, out_dbname); + InitPostgres(in_dbname, dboid, username, useroid, + flags & INIT_PG_LOAD_SESSION_LIBS, + flags & INIT_PG_OVERRIDE_ALLOW_CONNS, out_dbname); #elif PG_VERSION_NUM >= 110000 InitPostgres(in_dbname, dboid, username, useroid, out_dbname, - override_allow_connections); + flags & INIT_PG_OVERRIDE_ALLOW_CONNS); #else InitPostgres(in_dbname, dboid, username, useroid, out_dbname); #endif From 174a7862eaad018d0e918a601afc1e1716bc535f Mon Sep 17 00:00:00 2001 From: Oleg Tselebrovskiy Date: Thu, 23 May 2024 13:11:45 +0700 Subject: [PATCH 36/62] Add ExecutorStart hook to track top-level queryId for prepared queries and more precisely in general --- pg_wait_sampling.c | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/pg_wait_sampling.c b/pg_wait_sampling.c index eaa0327..268b40e 100644 --- a/pg_wait_sampling.c +++ b/pg_wait_sampling.c @@ -44,6 +44,7 @@ void _PG_init(void); static bool shmem_initialized = false; /* Hooks */ +static ExecutorStart_hook_type prev_ExecutorStart = NULL; static ExecutorEnd_hook_type prev_ExecutorEnd = NULL; static planner_hook_type planner_hook_next = NULL; @@ -67,6 +68,7 @@ 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 void pgws_ExecutorEnd(QueryDesc *queryDesc); /* @@ -402,6 +404,8 @@ _PG_init(void) 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_ExecutorEnd = ExecutorEnd_hook; ExecutorEnd_hook = pgws_ExecutorEnd; } @@ -906,6 +910,38 @@ pgws_planner_hook(Query *parse, cursorOptions, boundParams); } +/* + * ExecutorStart hook: save queryId for collector + */ +static void +pgws_ExecutorStart(QueryDesc *queryDesc, int eflags) +{ + int i; + + if (MyProc) + { + i = MyProc - ProcGlobal->allProcs; +#if PG_VERSION_NUM >= 110000 + /* + * since we depend on queryId we need to check that its size + * is uint64 as we coded in pg_wait_sampling + */ + StaticAssertExpr(sizeof(queryDesc->plannedstmt->queryId) == sizeof(uint64), + "queryId size is not uint64"); +#else + StaticAssertExpr(sizeof(queryDesc->plannedstmt->queryId) == sizeof(uint32), + "queryId size is not uint32"); +#endif + if (!pgws_proc_queryids[i]) + pgws_proc_queryids[i] = queryDesc->plannedstmt->queryId; + } + + if (prev_ExecutorStart) + prev_ExecutorStart(queryDesc, eflags); + else + standard_ExecutorStart(queryDesc, eflags); +} + /* * ExecutorEnd hook: clear queryId */ From d7dbd4406b567df570647b939b274047385f45f0 Mon Sep 17 00:00:00 2001 From: Sergey Shinderuk Date: Fri, 14 Jun 2024 11:27:21 +0300 Subject: [PATCH 37/62] Fix Travis build with PostgreSQL 14 Update preinstalled postgresql-client-14 package from PGDG. This fixes clang version mismatch. --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index b4296bf..77f7266 100644 --- a/.travis.yml +++ b/.travis.yml @@ -12,7 +12,7 @@ before_script: - echo "deb https://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" | sudo tee -a /etc/apt/sources.list - sudo apt-get update - sudo systemctl stop postgresql -- sudo apt-get install -y --no-install-recommends postgresql-${PG_MAJOR} postgresql-server-dev-${PG_MAJOR} +- sudo apt-get install -y --no-install-recommends postgresql-client-${PG_MAJOR} postgresql-${PG_MAJOR} postgresql-server-dev-${PG_MAJOR} - sudo systemctl stop postgresql script: ./run-tests.sh after_script: From f3892e025ca8134392a8676e107f2f245530d6d8 Mon Sep 17 00:00:00 2001 From: Sergey Shinderuk Date: Fri, 14 Jun 2024 11:43:00 +0300 Subject: [PATCH 38/62] Build and test with PostgreSQL 17 beta in Travis CI --- .travis.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.travis.yml b/.travis.yml index 77f7266..ffbb0da 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,7 @@ dist: jammy language: c env: +- PG_MAJOR=17 BETA=1 - PG_MAJOR=16 - PG_MAJOR=15 - PG_MAJOR=14 @@ -10,6 +11,7 @@ env: 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 - 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 ed9f977cc760fea29386e8c8758eb32ea5baa19e Mon Sep 17 00:00:00 2001 From: Sergey Shinderuk Date: Fri, 14 Jun 2024 10:08:16 +0300 Subject: [PATCH 39/62] Drop support for Postgres < 12 --- .travis.yml | 1 - README.md | 8 ++++---- collector.c | 8 +------- compat.h | 24 +----------------------- pg_wait_sampling.c | 43 +++++-------------------------------------- pg_wait_sampling.h | 7 +------ 6 files changed, 12 insertions(+), 79 deletions(-) diff --git a/.travis.yml b/.travis.yml index ffbb0da..1fce04a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,7 +7,6 @@ env: - PG_MAJOR=14 - PG_MAJOR=13 - PG_MAJOR=12 -- PG_MAJOR=11 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 diff --git a/README.md b/README.md index d8b03dc..cac5be7 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ Introduction ------------ -PostgreSQL 9.6+ provides an information about current wait event of particular +PostgreSQL provides information about current wait event of particular process. However, in order to gather descriptive statistics of server behavior user have to sample current wait event multiple times. `pg_wait_sampling` is an extension for collecting sampling statistics of wait @@ -47,7 +47,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 9.6+. +and supports PostgreSQL 12+. Installation ------------ @@ -58,10 +58,10 @@ repository: https://download.postgresql.org/pub/repos/ Manual build ------------ -`pg_wait_sampling` is PostgreSQL extension which requires PostgreSQL 9.6 or +`pg_wait_sampling` is PostgreSQL extension which requires PostgreSQL 12 or higher. Before build and install you should ensure following: - * PostgreSQL version is 9.6 or higher. + * PostgreSQL version is 12 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 diff --git a/collector.c b/collector.c index dcb9695..29e8ee2 100644 --- a/collector.c +++ b/collector.c @@ -423,15 +423,9 @@ pgws_collector_main(Datum main_arg) * Wait until next sample time or request to do something through * shared memory. */ -#if PG_VERSION_NUM >= 100000 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); -#else - rc = WaitLatch(&MyProc->procLatch, WL_LATCH_SET | WL_TIMEOUT | WL_POSTMASTER_DEATH, - Min(history_period - (int)history_diff, - profile_period - (int)profile_diff)); -#endif if (rc & WL_POSTMASTER_DEATH) proc_exit(1); @@ -482,7 +476,7 @@ pgws_collector_main(Datum main_arg) default: Assert(false); } - shm_mq_detach_compat(mqh, pgws_collector_mq); + shm_mq_detach(mqh); } else if (request == PROFILE_RESET) { diff --git a/compat.h b/compat.h index 5fcc814..72a63ab 100644 --- a/compat.h +++ b/compat.h @@ -17,26 +17,6 @@ #include "storage/shm_mq.h" #include "utils/guc_tables.h" -static inline TupleDesc -CreateTemplateTupleDescCompat(int nattrs, bool hasoid) -{ -#if PG_VERSION_NUM >= 120000 - return CreateTemplateTupleDesc(nattrs); -#else - return CreateTemplateTupleDesc(nattrs, hasoid); -#endif -} - -static inline void -shm_mq_detach_compat(shm_mq_handle *mqh, shm_mq *mq) -{ -#if PG_VERSION_NUM >= 100000 - shm_mq_detach(mqh); -#else - shm_mq_detach(mq); -#endif -} - static inline shm_mq_result shm_mq_send_compat(shm_mq_handle *mqh, Size nbytes, const void *data, bool nowait, bool force_flush) @@ -61,11 +41,9 @@ InitPostgresCompat(const char *in_dbname, Oid dboid, #elif PG_VERSION_NUM >= 150000 InitPostgres(in_dbname, dboid, username, useroid, load_session_libraries, override_allow_connections, out_dbname); -#elif PG_VERSION_NUM >= 110000 +#else InitPostgres(in_dbname, dboid, username, useroid, out_dbname, override_allow_connections); -#else - InitPostgres(in_dbname, dboid, username, useroid, out_dbname); #endif } diff --git a/pg_wait_sampling.c b/pg_wait_sampling.c index 268b40e..72e2fb4 100644 --- a/pg_wait_sampling.c +++ b/pg_wait_sampling.c @@ -18,9 +18,7 @@ #include "optimizer/planner.h" #include "pgstat.h" #include "postmaster/autovacuum.h" -#if PG_VERSION_NUM >= 120000 #include "replication/walsender.h" -#endif #include "storage/ipc.h" #include "storage/pg_shmem.h" #include "storage/procarray.h" @@ -119,9 +117,7 @@ get_max_procs_count(void) * Starting with pg12, wal senders aren't part of MaxConnections anymore * and have to be accounted for. */ -#if PG_VERSION_NUM >= 120000 count += max_wal_senders; -#endif /* pg 12+ */ #endif /* pg 15- */ /* End of MaxBackends calculation. */ @@ -331,16 +327,9 @@ pgws_shmem_startup(void) else { toc = shm_toc_attach(PG_WAIT_SAMPLING_MAGIC, pgws); - -#if PG_VERSION_NUM >= 100000 pgws_collector_hdr = shm_toc_lookup(toc, 0, false); pgws_collector_mq = shm_toc_lookup(toc, 1, false); pgws_proc_queryids = shm_toc_lookup(toc, 2, false); -#else - pgws_collector_hdr = shm_toc_lookup(toc, 0); - pgws_collector_mq = shm_toc_lookup(toc, 1); - pgws_proc_queryids = shm_toc_lookup(toc, 2); -#endif } shmem_initialized = true; @@ -366,7 +355,7 @@ static void pgws_cleanup_callback(int code, Datum arg) { elog(DEBUG3, "pg_wait_sampling cleanup: detaching shm_mq and releasing queue lock"); - shm_mq_detach_compat(recv_mqh, recv_mq); + shm_mq_detach(recv_mqh); LockRelease(&queueTag, ExclusiveLock, false); } @@ -463,7 +452,7 @@ pg_wait_sampling_get_current(PG_FUNCTION_ARGS) params->ts = GetCurrentTimestamp(); funcctx->user_fctx = params; - tupdesc = CreateTemplateTupleDescCompat(4, false); + tupdesc = CreateTemplateTupleDesc(4); TupleDescInitEntry(tupdesc, (AttrNumber) 1, "pid", INT4OID, -1, 0); TupleDescInitEntry(tupdesc, (AttrNumber) 2, "type", @@ -651,7 +640,7 @@ receive_array(SHMRequest request, Size item_size, Size *count) PG_END_ENSURE_ERROR_CLEANUP(pgws_cleanup_callback, 0); /* We still have to detach and release lock during normal operation. */ - shm_mq_detach_compat(recv_mqh, recv_mq); + shm_mq_detach(recv_mqh); LockRelease(&queueTag, ExclusiveLock, false); return result; @@ -684,7 +673,7 @@ pg_wait_sampling_get_profile(PG_FUNCTION_ARGS) funcctx->max_calls = profile->count; /* Make tuple descriptor */ - tupdesc = CreateTemplateTupleDescCompat(5, false); + tupdesc = CreateTemplateTupleDesc(5); TupleDescInitEntry(tupdesc, (AttrNumber) 1, "pid", INT4OID, -1, 0); TupleDescInitEntry(tupdesc, (AttrNumber) 2, "type", @@ -801,7 +790,7 @@ pg_wait_sampling_get_history(PG_FUNCTION_ARGS) funcctx->max_calls = history->count; /* Make tuple descriptor */ - tupdesc = CreateTemplateTupleDescCompat(5, false); + tupdesc = CreateTemplateTupleDesc(5); TupleDescInitEntry(tupdesc, (AttrNumber) 1, "pid", INT4OID, -1, 0); TupleDescInitEntry(tupdesc, (AttrNumber) 2, "sample_ts", @@ -879,17 +868,6 @@ pgws_planner_hook(Query *parse, if (MyProc) { int i = MyProc - ProcGlobal->allProcs; -#if PG_VERSION_NUM >= 110000 - /* - * since we depend on queryId we need to check that its size - * is uint64 as we coded in pg_wait_sampling - */ - StaticAssertExpr(sizeof(parse->queryId) == sizeof(uint64), - "queryId size is not uint64"); -#else - StaticAssertExpr(sizeof(parse->queryId) == sizeof(uint32), - "queryId size is not uint32"); -#endif if (!pgws_proc_queryids[i]) pgws_proc_queryids[i] = parse->queryId; @@ -921,17 +899,6 @@ pgws_ExecutorStart(QueryDesc *queryDesc, int eflags) if (MyProc) { i = MyProc - ProcGlobal->allProcs; -#if PG_VERSION_NUM >= 110000 - /* - * since we depend on queryId we need to check that its size - * is uint64 as we coded in pg_wait_sampling - */ - StaticAssertExpr(sizeof(queryDesc->plannedstmt->queryId) == sizeof(uint64), - "queryId size is not uint64"); -#else - StaticAssertExpr(sizeof(queryDesc->plannedstmt->queryId) == sizeof(uint32), - "queryId size is not uint32"); -#endif if (!pgws_proc_queryids[i]) pgws_proc_queryids[i] = queryDesc->plannedstmt->queryId; } diff --git a/pg_wait_sampling.h b/pg_wait_sampling.h index 29425fc..79297e8 100644 --- a/pg_wait_sampling.h +++ b/pg_wait_sampling.h @@ -10,12 +10,7 @@ #ifndef __PG_WAIT_SAMPLING_H__ #define __PG_WAIT_SAMPLING_H__ -#include - -/* Check PostgreSQL version */ -#if PG_VERSION_NUM < 90600 - #error "You are trying to build pg_wait_sampling with PostgreSQL version lower than 9.6. Please, check you environment." -#endif +#include "postgres.h" #include "storage/proc.h" #include "storage/shm_mq.h" From 69db35f369e7fb47c668dcdbcae1be10cdaf3535 Mon Sep 17 00:00:00 2001 From: Oleg Tselebrovskiy Date: Fri, 14 Jun 2024 19:05:12 +0700 Subject: [PATCH 40/62] Remove if(MyProc) checks in hooks --- pg_wait_sampling.c | 24 +++++++----------------- 1 file changed, 7 insertions(+), 17 deletions(-) diff --git a/pg_wait_sampling.c b/pg_wait_sampling.c index 72e2fb4..7f6b31d 100644 --- a/pg_wait_sampling.c +++ b/pg_wait_sampling.c @@ -865,13 +865,9 @@ pgws_planner_hook(Query *parse, int cursorOptions, ParamListInfo boundParams) { - if (MyProc) - { - int i = MyProc - ProcGlobal->allProcs; - if (!pgws_proc_queryids[i]) - pgws_proc_queryids[i] = parse->queryId; - - } + int i = MyProc - ProcGlobal->allProcs; + if (!pgws_proc_queryids[i]) + pgws_proc_queryids[i] = parse->queryId; /* Invoke original hook if needed */ if (planner_hook_next) @@ -894,14 +890,9 @@ pgws_planner_hook(Query *parse, static void pgws_ExecutorStart(QueryDesc *queryDesc, int eflags) { - int i; - - if (MyProc) - { - i = MyProc - ProcGlobal->allProcs; - if (!pgws_proc_queryids[i]) - pgws_proc_queryids[i] = queryDesc->plannedstmt->queryId; - } + int i = MyProc - ProcGlobal->allProcs; + if (!pgws_proc_queryids[i]) + pgws_proc_queryids[i] = queryDesc->plannedstmt->queryId; if (prev_ExecutorStart) prev_ExecutorStart(queryDesc, eflags); @@ -915,8 +906,7 @@ pgws_ExecutorStart(QueryDesc *queryDesc, int eflags) static void pgws_ExecutorEnd(QueryDesc *queryDesc) { - if (MyProc) - pgws_proc_queryids[MyProc - ProcGlobal->allProcs] = UINT64CONST(0); + pgws_proc_queryids[MyProc - ProcGlobal->allProcs] = UINT64CONST(0); if (prev_ExecutorEnd) prev_ExecutorEnd(queryDesc); From 0b5c61b58acdf35d4cf25fb4a8dd7b153617d339 Mon Sep 17 00:00:00 2001 From: Oleg Tselebrovskiy Date: Fri, 14 Jun 2024 20:24:39 +0700 Subject: [PATCH 41/62] Make so subqueries do not rewrite queryId of top-level query --- pg_wait_sampling.c | 99 ++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 87 insertions(+), 12 deletions(-) diff --git a/pg_wait_sampling.c b/pg_wait_sampling.c index 7f6b31d..3950ce6 100644 --- a/pg_wait_sampling.c +++ b/pg_wait_sampling.c @@ -43,9 +43,14 @@ static bool shmem_initialized = false; /* Hooks */ 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; +/* Current nesting depth of planner/Executor calls */ +static int nesting_level = 0; + /* Pointers to shared memory objects */ shm_mq *pgws_collector_mq = NULL; uint64 *pgws_proc_queryids = NULL; @@ -67,6 +72,10 @@ static PlannedStmt *pgws_planner_hook(Query *parse, #endif int cursorOptions, ParamListInfo boundParams); static void pgws_ExecutorStart(QueryDesc *queryDesc, int eflags); +static void pgws_ExecutorRun(QueryDesc *queryDesc, + ScanDirection direction, + uint64 count, bool execute_once); +static void pgws_ExecutorFinish(QueryDesc *queryDesc); static void pgws_ExecutorEnd(QueryDesc *queryDesc); /* @@ -395,6 +404,10 @@ _PG_init(void) 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; } @@ -865,23 +878,41 @@ pgws_planner_hook(Query *parse, int cursorOptions, ParamListInfo boundParams) { + PlannedStmt *result; int i = MyProc - ProcGlobal->allProcs; - if (!pgws_proc_queryids[i]) + if (nesting_level == 0) pgws_proc_queryids[i] = parse->queryId; - /* Invoke original hook if needed */ - if (planner_hook_next) - return planner_hook_next(parse, + nesting_level++; + PG_TRY(); + { + /* Invoke original hook if needed */ + if (planner_hook_next) + result = planner_hook_next(parse, #if PG_VERSION_NUM >= 130000 - query_string, + query_string, #endif - cursorOptions, boundParams); - - return standard_planner(parse, + 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); + } + PG_CATCH(); + { + nesting_level--; + if (nesting_level == 0) + pgws_proc_queryids[i] = UINT64CONST(0); + PG_RE_THROW(); + } + PG_END_TRY(); + + return result; } /* @@ -891,7 +922,7 @@ static void pgws_ExecutorStart(QueryDesc *queryDesc, int eflags) { int i = MyProc - ProcGlobal->allProcs; - if (!pgws_proc_queryids[i]) + if (nesting_level == 0) pgws_proc_queryids[i] = queryDesc->plannedstmt->queryId; if (prev_ExecutorStart) @@ -900,13 +931,57 @@ pgws_ExecutorStart(QueryDesc *queryDesc, int eflags) standard_ExecutorStart(queryDesc, eflags); } +static void +pgws_ExecutorRun(QueryDesc *queryDesc, + ScanDirection direction, + uint64 count, bool execute_once) +{ + nesting_level++; + PG_TRY(); + { + if (prev_ExecutorRun) + prev_ExecutorRun(queryDesc, direction, count, execute_once); + else + standard_ExecutorRun(queryDesc, direction, count, execute_once); + nesting_level--; + } + PG_CATCH(); + { + nesting_level--; + PG_RE_THROW(); + } + PG_END_TRY(); +} + +static void +pgws_ExecutorFinish(QueryDesc *queryDesc) +{ + nesting_level++; + PG_TRY(); + { + if (prev_ExecutorFinish) + prev_ExecutorFinish(queryDesc); + else + standard_ExecutorFinish(queryDesc); + nesting_level--; + } + PG_CATCH(); + { + nesting_level--; + PG_RE_THROW(); + } + PG_END_TRY(); +} + /* * ExecutorEnd hook: clear queryId */ static void pgws_ExecutorEnd(QueryDesc *queryDesc) { - pgws_proc_queryids[MyProc - ProcGlobal->allProcs] = UINT64CONST(0); + int i = MyProc - ProcGlobal->allProcs; + if (nesting_level == 0) + pgws_proc_queryids[i] = UINT64CONST(0); if (prev_ExecutorEnd) prev_ExecutorEnd(queryDesc); From 12c0f7d0f9bc3ac2684c387dbe07cda971ca819f Mon Sep 17 00:00:00 2001 From: Ants Aasma Date: Mon, 17 Jun 2024 18:44:16 +0300 Subject: [PATCH 42/62] Track on CPU events too (#74) To not count dead backends as still running on the CPU we need to detect that the backend is dead. Starting from PG17 proc->pid is reset in ProcKill, before this we can check if the process latch is disowned. Not nice to be poking around in latch internals like this, but all alternatives seem to involve scanning bestatus array and correlating pids. Also makes sense to exclude ourselves as we will always be on CPU while looking at wait events. Add a GUC for controlling whether on CPU events are counted. --- README.md | 19 ++++++++++------ collector.c | 5 +---- pg_wait_sampling.c | 54 ++++++++++++++++++++++++++++++++++++++-------- pg_wait_sampling.h | 2 ++ 4 files changed, 60 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index cac5be7..9b827e0 100644 --- a/README.md +++ b/README.md @@ -133,13 +133,14 @@ in-memory hash table. The work of wait event statistics collector worker is controlled by following GUCs. -| Parameter name | Data type | Description | Default value | -| ----------------------------------- | --------- | ------------------------------------------- | ------------: | -| pg_wait_sampling.history_size | int4 | Size of history in-memory ring buffer | 5000 | -| pg_wait_sampling.history_period | int4 | Period for history sampling in milliseconds | 10 | -| pg_wait_sampling.profile_period | int4 | Period for profile sampling in milliseconds | 10 | -| pg_wait_sampling.profile_pid | bool | Whether profile should be per pid | true | -| pg_wait_sampling.profile_queries | bool | Whether profile should be per query | true | +| Parameter name | Data type | Description | Default value | +|----------------------------------| --------- |---------------------------------------------|--------------:| +| pg_wait_sampling.history_size | int4 | Size of history in-memory ring buffer | 5000 | +| pg_wait_sampling.history_period | int4 | Period for history sampling in milliseconds | 10 | +| pg_wait_sampling.profile_period | int4 | Period for profile sampling in milliseconds | 10 | +| pg_wait_sampling.profile_pid | bool | Whether profile should be per pid | true | +| pg_wait_sampling.profile_queries | bool | Whether profile should be per query | true | +| pg_wait_sampling.sample_cpu | bool | Whether on CPU backends should be sampled | true | If `pg_wait_sampling.profile_pid` is set to false, sampling profile wouldn't be collected in per-process manner. In this case the value of pid could would @@ -148,6 +149,10 @@ be always zero and corresponding row contain samples among all the processes. While `pg_wait_sampling.profile_queries` is set to false `queryid` field in views will be zero. +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. diff --git a/collector.c b/collector.c index 29e8ee2..235d95f 100644 --- a/collector.c +++ b/collector.c @@ -163,10 +163,7 @@ probe_waits(History *observations, HTAB *profile_hash, *observation; PGPROC *proc = &ProcGlobal->allProcs[i]; - if (proc->pid == 0) - continue; - - if (proc->wait_event_info == 0) + if (!pgws_should_sample_proc(proc)) continue; /* Collect next wait event sample */ diff --git a/pg_wait_sampling.c b/pg_wait_sampling.c index 3950ce6..f07b1cf 100644 --- a/pg_wait_sampling.c +++ b/pg_wait_sampling.c @@ -203,7 +203,8 @@ setup_gucs() history_period_found = false, profile_period_found = false, profile_pid_found = false, - profile_queries_found = false; + profile_queries_found = false, + sample_cpu_found = false; get_guc_variables_compat(&guc_vars, &numOpts); @@ -245,6 +246,12 @@ setup_gucs() var->_bool.variable = &pgws_collector_hdr->profileQueries; pgws_collector_hdr->profileQueries = true; } + 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) @@ -277,11 +284,18 @@ setup_gucs() &pgws_collector_hdr->profileQueries, true, PGC_SUSET, 0, shmem_bool_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) + || profile_queries_found + || sample_cpu_found) { ProcessConfigFile(PGC_SIGHUP); } @@ -438,6 +452,28 @@ search_proc(int pid) return NULL; } +/* + * Decide whether this PGPROC entry should be included in profiles and output + * views. + */ +bool +pgws_should_sample_proc(PGPROC *proc) +{ + if (proc->wait_event_info == 0 && !pgws_collector_hdr->sampleCpu) + return false; + + /* + * On PostgreSQL versions < 17 the PGPROC->pid field is not reset on + * process exit. This would lead to such processes getting counted for + * 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) + return false; + + return true; +} + typedef struct { HistoryItem *items; @@ -503,13 +539,13 @@ pg_wait_sampling_get_current(PG_FUNCTION_ARGS) { PGPROC *proc = &ProcGlobal->allProcs[i]; - if (proc != NULL && proc->pid != 0 && proc->wait_event_info) - { - params->items[j].pid = proc->pid; - params->items[j].wait_event_info = proc->wait_event_info; - params->items[j].queryId = pgws_proc_queryids[i]; - j++; - } + if (!pgws_should_sample_proc(proc)) + continue; + + params->items[j].pid = proc->pid; + params->items[j].wait_event_info = proc->wait_event_info; + params->items[j].queryId = pgws_proc_queryids[i]; + j++; } funcctx->max_calls = j; } diff --git a/pg_wait_sampling.h b/pg_wait_sampling.h index 79297e8..7c409e3 100644 --- a/pg_wait_sampling.h +++ b/pg_wait_sampling.h @@ -63,6 +63,7 @@ typedef struct int profilePeriod; bool profilePid; bool profileQueries; + bool sampleCpu; } CollectorShmqHeader; /* pg_wait_sampling.c */ @@ -70,6 +71,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); /* collector.c */ extern void pgws_register_wait_collector(void); From c02059c67a4bb3df063cf91daf27079e491d671e Mon Sep 17 00:00:00 2001 From: Sergey Shinderuk Date: Mon, 17 Jun 2024 18:57:40 +0300 Subject: [PATCH 43/62] Update Travis build status image in README --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 9b827e0..3895faf 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -[![Build Status](https://travis-ci.com/postgrespro/pg_wait_sampling.svg?branch=master)](https://travis-ci.com/postgrespro/pg_wait_sampling) +[![Build Status](https://app.travis-ci.com/postgrespro/pg_wait_sampling.svg&branch=master)](https://app.travis-ci.com/postgrespro/pg_wait_sampling) [![GitHub license](https://img.shields.io/badge/license-PostgreSQL-blue.svg)](https://raw.githubusercontent.com/postgrespro/pg_wait_sampling/master/LICENSE) `pg_wait_sampling` – sampling based statistics of wait events From 1b714f4066db36599397fa871db49259c9b44f6d Mon Sep 17 00:00:00 2001 From: Sergey Shinderuk Date: Mon, 17 Jun 2024 19:02:24 +0300 Subject: [PATCH 44/62] Fix build status image URL in README --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 3895faf..01aa116 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -[![Build Status](https://app.travis-ci.com/postgrespro/pg_wait_sampling.svg&branch=master)](https://app.travis-ci.com/postgrespro/pg_wait_sampling) +[![Build Status](https://app.travis-ci.com/postgrespro/pg_wait_sampling.svg?branch=master)](https://app.travis-ci.com/postgrespro/pg_wait_sampling) [![GitHub license](https://img.shields.io/badge/license-PostgreSQL-blue.svg)](https://raw.githubusercontent.com/postgrespro/pg_wait_sampling/master/LICENSE) `pg_wait_sampling` – sampling based statistics of wait events From 970e1c152194cea346dd886ca4f1a8f5d7b17f9f Mon Sep 17 00:00:00 2001 From: Oleg Tselebrovskiy Date: Tue, 25 Jun 2024 09:37:14 +0700 Subject: [PATCH 45/62] Add a ProcessUtility hook to track utility statements' wait events --- pg_wait_sampling.c | 84 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) diff --git a/pg_wait_sampling.c b/pg_wait_sampling.c index f07b1cf..2cd7c7d 100644 --- a/pg_wait_sampling.c +++ b/pg_wait_sampling.c @@ -25,6 +25,7 @@ #include "storage/shm_mq.h" #include "storage/shm_toc.h" #include "storage/spin.h" +#include "tcop/utility.h" #include "utils/builtins.h" #include "utils/datetime.h" #include "utils/guc_tables.h" @@ -47,6 +48,7 @@ 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 ProcessUtility_hook_type prev_ProcessUtility = NULL; /* Current nesting depth of planner/Executor calls */ static int nesting_level = 0; @@ -77,6 +79,21 @@ static void pgws_ExecutorRun(QueryDesc *queryDesc, uint64 count, bool execute_once); static void pgws_ExecutorFinish(QueryDesc *queryDesc); static void pgws_ExecutorEnd(QueryDesc *queryDesc); +static void pgws_ProcessUtility(PlannedStmt *pstmt, + const char *queryString, +#if PG_VERSION_NUM >= 140000 + bool readOnlyTree, +#endif + ProcessUtilityContext context, + ParamListInfo params, + QueryEnvironment *queryEnv, + DestReceiver *dest, +#if PG_VERSION_NUM >= 130000 + QueryCompletion *qc +#else + char *completionTag +#endif + ); /* * Calculate max processes count. @@ -424,6 +441,8 @@ _PG_init(void) ExecutorFinish_hook = pgws_ExecutorFinish; prev_ExecutorEnd = ExecutorEnd_hook; ExecutorEnd_hook = pgws_ExecutorEnd; + prev_ProcessUtility = ProcessUtility_hook; + ProcessUtility_hook = pgws_ProcessUtility; } /* @@ -1024,3 +1043,68 @@ pgws_ExecutorEnd(QueryDesc *queryDesc) else standard_ExecutorEnd(queryDesc); } + +static void +pgws_ProcessUtility(PlannedStmt *pstmt, + const char *queryString, +#if PG_VERSION_NUM >= 140000 + bool readOnlyTree, +#endif + ProcessUtilityContext context, + ParamListInfo params, + QueryEnvironment *queryEnv, + DestReceiver *dest, +#if PG_VERSION_NUM >= 130000 + QueryCompletion *qc +#else + char *completionTag +#endif + ) +{ + int i = MyProc - ProcGlobal->allProcs; + + if (nesting_level == 0) + pgws_proc_queryids[i] = pstmt->queryId; + + nesting_level++; + PG_TRY(); + { + if (prev_ProcessUtility) + prev_ProcessUtility (pstmt, queryString, +#if PG_VERSION_NUM >= 140000 + readOnlyTree, +#endif + context, params, queryEnv, + dest, +#if PG_VERSION_NUM >= 130000 + qc +#else + completionTag +#endif + ); + else + standard_ProcessUtility(pstmt, queryString, +#if PG_VERSION_NUM >= 140000 + readOnlyTree, +#endif + context, params, queryEnv, + dest, +#if PG_VERSION_NUM >= 130000 + qc +#else + completionTag +#endif + ); + nesting_level--; + if (nesting_level == 0) + pgws_proc_queryids[i] = UINT64CONST(0); + } + PG_CATCH(); + { + nesting_level--; + if (nesting_level == 0) + pgws_proc_queryids[i] = UINT64CONST(0); + PG_RE_THROW(); + } + PG_END_TRY(); +} From 7ebbf8a5279ee61069a3d89a1aee8d116f7a7525 Mon Sep 17 00:00:00 2001 From: Oleg Tselebrovskiy Date: Tue, 25 Jun 2024 17:24:39 +0700 Subject: [PATCH 46/62] Add option for tracking subqueries' wait events --- README.md | 7 ++-- pg_wait_sampling.c | 98 ++++++++++++++++++++++++++++++++++++++++------ pg_wait_sampling.h | 2 +- 3 files changed, 91 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 01aa116..c9ef3f9 100644 --- a/README.md +++ b/README.md @@ -139,15 +139,16 @@ GUCs. | pg_wait_sampling.history_period | int4 | Period for history sampling in milliseconds | 10 | | pg_wait_sampling.profile_period | int4 | Period for profile sampling in milliseconds | 10 | | pg_wait_sampling.profile_pid | bool | Whether profile should be per pid | true | -| pg_wait_sampling.profile_queries | bool | Whether profile should be per query | true | +| pg_wait_sampling.profile_queries | enum | Whether profile should be per query | top | | pg_wait_sampling.sample_cpu | bool | Whether on CPU backends should be sampled | true | If `pg_wait_sampling.profile_pid` is set to false, sampling profile wouldn't be collected in per-process manner. In this case the value of pid could would be always zero and corresponding row contain samples among all the processes. -While `pg_wait_sampling.profile_queries` is set to false `queryid` field in -views will be zero. +If `pg_wait_sampling.profile_queries` is set to `none`, `queryid` field in +views will be zero. If it is set to `top`, queryIds only of top level statements +are recorded. If it is set to `all`, queryIds of nested statements are recorded. 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 diff --git a/pg_wait_sampling.c b/pg_wait_sampling.c index 2cd7c7d..068b553 100644 --- a/pg_wait_sampling.c +++ b/pg_wait_sampling.c @@ -95,6 +95,35 @@ static void pgws_ProcessUtility(PlannedStmt *pstmt, #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 */ +} 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} +}; + +#define pgws_enabled(level) \ + ((pgws_collector_hdr->profileQueries == PGWS_PROFILE_QUERIES_ALL) || \ + (pgws_collector_hdr->profileQueries == PGWS_PROFILE_QUERIES_TOP && (level) == 0)) + /* * Calculate max processes count. * @@ -185,6 +214,14 @@ shmem_int_guc_check_hook(int *newval, void **extra, GucSource source) 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) { @@ -260,8 +297,8 @@ setup_gucs() else if (!strcmp(name, "pg_wait_sampling.profile_queries")) { profile_queries_found = true; - var->_bool.variable = &pgws_collector_hdr->profileQueries; - pgws_collector_hdr->profileQueries = 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")) { @@ -296,10 +333,10 @@ setup_gucs() PGC_SUSET, 0, shmem_bool_guc_check_hook, NULL, NULL); if (!profile_queries_found) - DefineCustomBoolVariable("pg_wait_sampling.profile_queries", + DefineCustomEnumVariable("pg_wait_sampling.profile_queries", "Sets whether profile should be collected per query.", NULL, - &pgws_collector_hdr->profileQueries, true, - PGC_SUSET, 0, shmem_bool_guc_check_hook, NULL, 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", @@ -354,6 +391,8 @@ 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, @@ -933,10 +972,15 @@ pgws_planner_hook(Query *parse, int cursorOptions, ParamListInfo boundParams) { - PlannedStmt *result; - int i = MyProc - ProcGlobal->allProcs; - if (nesting_level == 0) + PlannedStmt *result; + int i = MyProc - ProcGlobal->allProcs; + uint64 save_queryId = 0; + + if (pgws_enabled(nesting_level)) + { + save_queryId = pgws_proc_queryids[i]; pgws_proc_queryids[i] = parse->queryId; + } nesting_level++; PG_TRY(); @@ -957,12 +1001,16 @@ pgws_planner_hook(Query *parse, nesting_level--; if (nesting_level == 0) pgws_proc_queryids[i] = UINT64CONST(0); + else if (pgws_enabled(nesting_level)) + pgws_proc_queryids[i] = save_queryId; } PG_CATCH(); { nesting_level--; if (nesting_level == 0) pgws_proc_queryids[i] = UINT64CONST(0); + else if (pgws_enabled(nesting_level)) + pgws_proc_queryids[i] = save_queryId; PG_RE_THROW(); } PG_END_TRY(); @@ -977,9 +1025,8 @@ static void pgws_ExecutorStart(QueryDesc *queryDesc, int eflags) { int i = MyProc - ProcGlobal->allProcs; - if (nesting_level == 0) + if (pgws_enabled(nesting_level)) pgws_proc_queryids[i] = queryDesc->plannedstmt->queryId; - if (prev_ExecutorStart) prev_ExecutorStart(queryDesc, eflags); else @@ -991,6 +1038,9 @@ pgws_ExecutorRun(QueryDesc *queryDesc, ScanDirection direction, uint64 count, bool execute_once) { + int i = MyProc - ProcGlobal->allProcs; + uint64 save_queryId = pgws_proc_queryids[i]; + nesting_level++; PG_TRY(); { @@ -999,10 +1049,18 @@ pgws_ExecutorRun(QueryDesc *queryDesc, else standard_ExecutorRun(queryDesc, direction, count, execute_once); nesting_level--; + if (nesting_level == 0) + pgws_proc_queryids[i] = UINT64CONST(0); + else + pgws_proc_queryids[i] = save_queryId; } PG_CATCH(); { nesting_level--; + if (nesting_level == 0) + pgws_proc_queryids[i] = UINT64CONST(0); + else + pgws_proc_queryids[i] = save_queryId; PG_RE_THROW(); } PG_END_TRY(); @@ -1011,6 +1069,9 @@ pgws_ExecutorRun(QueryDesc *queryDesc, static void pgws_ExecutorFinish(QueryDesc *queryDesc) { + int i = MyProc - ProcGlobal->allProcs; + uint64 save_queryId = pgws_proc_queryids[i]; + nesting_level++; PG_TRY(); { @@ -1019,10 +1080,15 @@ pgws_ExecutorFinish(QueryDesc *queryDesc) else standard_ExecutorFinish(queryDesc); nesting_level--; + if (nesting_level == 0) + pgws_proc_queryids[i] = UINT64CONST(0); + else + pgws_proc_queryids[i] = save_queryId; } PG_CATCH(); { nesting_level--; + pgws_proc_queryids[i] = save_queryId; PG_RE_THROW(); } PG_END_TRY(); @@ -1061,10 +1127,14 @@ pgws_ProcessUtility(PlannedStmt *pstmt, #endif ) { - int i = MyProc - ProcGlobal->allProcs; + int i = MyProc - ProcGlobal->allProcs; + uint64 save_queryId = 0; - if (nesting_level == 0) + if (pgws_enabled(nesting_level)) + { + save_queryId = pgws_proc_queryids[i]; pgws_proc_queryids[i] = pstmt->queryId; + } nesting_level++; PG_TRY(); @@ -1098,12 +1168,16 @@ pgws_ProcessUtility(PlannedStmt *pstmt, nesting_level--; if (nesting_level == 0) pgws_proc_queryids[i] = UINT64CONST(0); + else if (pgws_enabled(nesting_level)) + pgws_proc_queryids[i] = save_queryId; } PG_CATCH(); { nesting_level--; if (nesting_level == 0) pgws_proc_queryids[i] = UINT64CONST(0); + else if (pgws_enabled(nesting_level)) + pgws_proc_queryids[i] = save_queryId; PG_RE_THROW(); } PG_END_TRY(); diff --git a/pg_wait_sampling.h b/pg_wait_sampling.h index 7c409e3..a8a550f 100644 --- a/pg_wait_sampling.h +++ b/pg_wait_sampling.h @@ -62,7 +62,7 @@ typedef struct int historyPeriod; int profilePeriod; bool profilePid; - bool profileQueries; + int profileQueries; bool sampleCpu; } CollectorShmqHeader; From 8eaa130c6160f02c88fd1dddd1796083a4391398 Mon Sep 17 00:00:00 2001 From: Oleg Tselebrovskiy Date: Fri, 5 Jul 2024 12:38:58 +0300 Subject: [PATCH 47/62] README fix Add a recommendation for 'pg_stat_statements' and 'pg_wait_sampling' order in shared_preload_libraries --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index c9ef3f9..47fb882 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,10 @@ The module must be loaded by adding `pg_wait_sampling` to shared memory and launches background worker. This means that a server restart is needed to add or remove the module. +When used with `pg_stat_statements` it is recommended to put `pg_stat_statements` +before `pg_wait_sampling` in `shared_preload_libraries` so queryIds of +utility statements are not rewritten by the former. + When `pg_wait_sampling` is enabled, it collects two kinds of statistics. * History of waits events. It's implemented as in-memory ring buffer where From ce5d628597e7f2b6f1911b348f5db020ddc1a891 Mon Sep 17 00:00:00 2001 From: RekGRpth Date: Wed, 11 Dec 2024 08:37:30 +0500 Subject: [PATCH 48/62] 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 49/62] 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 50/62] 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 51/62] 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 52/62] 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 53/62] 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 54/62] 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 55/62] 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 56/62] 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 57/62] 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 58/62] 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 59/62] 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 60/62] 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 61/62] 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 62/62] 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