pax_global_header00006660000000000000000000000064146462151360014522gustar00rootroot0000000000000052 comment=b79a8163d8b6d3812451f8b0126b186e63cfae81 pg_wait_sampling-1.1.6/000077500000000000000000000000001464621513600150535ustar00rootroot00000000000000pg_wait_sampling-1.1.6/.gitignore000066400000000000000000000000551464621513600170430ustar00rootroot00000000000000*.o *.so /.deps/ /log/ /results/ /tmp_check/ pg_wait_sampling-1.1.6/.travis.yml000066400000000000000000000014311464621513600171630ustar00rootroot00000000000000dist: jammy language: c env: - PG_MAJOR=17 BETA=1 - 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 - 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} - sudo systemctl stop postgresql script: ./run-tests.sh after_script: - cat regression.diffs - cat logfile pg_wait_sampling-1.1.6/LICENSE000066400000000000000000000023531464621513600160630ustar00rootroot00000000000000pg_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. pg_wait_sampling-1.1.6/Makefile000066400000000000000000000010331464621513600165100ustar00rootroot00000000000000# contrib/pg_wait_sampling/Makefile MODULE_big = pg_wait_sampling OBJS = pg_wait_sampling.o collector.o EXTENSION = pg_wait_sampling 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 ifdef USE_PGXS PG_CONFIG = pg_config PGXS := $(shell $(PG_CONFIG) --pgxs) include $(PGXS) else subdir = contrib/pg_wait_sampling top_builddir = ../.. include $(top_builddir)/src/Makefile.global include $(top_srcdir)/contrib/contrib-global.mk endif pg_wait_sampling-1.1.6/README.md000066400000000000000000000174671464621513600163510ustar00rootroot00000000000000[![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 ============================================================= Introduction ------------ 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 events. The module must be loaded by adding `pg_wait_sampling` to `shared_preload_libraries` in postgresql.conf, because it requires additional 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 samples of each process wait events are written with given (configurable) period. Therefore, for each running process user can see some number of recent samples depending on history size (configurable). Assuming there is a client who periodically read this history and dump it somewhere, user can have continuous history. * Waits profile. It's implemented as in-memory hash table where count of samples are accumulated per each process and each wait event (and each query with `pg_stat_statements`). This hash table can be reset by user request. Assuming there is a client who periodically dumps profile and resets it, user can have statistics of intensivity of wait events among time. In combination with `pg_stat_statements` this extension can also provide per query statistics. `pg_wait_sampling` launches special background worker for gathering the statistics above. Availability ------------ `pg_wait_sampling` is implemented as an extension and not available in default 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+. 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 12 or higher. Before build and install you should ensure following: * 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 set PG_CONFIG variable. Typical installation procedure may look like this: $ git clone https://github.com/postgrespro/pg_wait_sampling.git $ 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 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. Usage ----- `pg_wait_sampling` interacts with user by set of views and functions. `pg_wait_sampling_current` view – information about current wait events for all processed including background workers. | Column name | Column type | Description | | ----------- | ----------- | ----------------------- | | pid | int4 | Id of process | | event_type | text | Name of wait event type | | event | text | Name of wait event | | queryid | int8 | Id of query | `pg_wait_sampling_get_current(pid int4)` returns the same table for single given process. `pg_wait_sampling_history` view – history of wait events obtained by sampling into in-memory ring buffer. | Column name | Column type | Description | | ----------- | ----------- | ----------------------- | | pid | int4 | Id of process | | ts | timestamptz | Sample timestamp | | event_type | text | Name of wait event type | | event | text | Name of wait event | | queryid | int8 | Id of query | `pg_wait_sampling_profile` view – profile of wait events obtained by sampling into in-memory hash table. | Column name | Column type | Description | | ----------- | ----------- | ----------------------- | | pid | int4 | Id of process | | event_type | text | Name of wait event type | | event | text | Name of wait event | | queryid | int8 | Id of query | | count | text | Count of samples | `pg_wait_sampling_reset_profile()` function resets the profile. 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 | 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. 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 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. See [PostgreSQL documentation](http://www.postgresql.org/docs/devel/static/monitoring-stats.html#WAIT-EVENT-TABLE) for list of possible wait events. Contribution ------------ Please, notice, that `pg_wait_sampling` is still under development and while it's stable and tested, it may contains some bugs. Don't hesitate to raise [issues at github](https://github.com/postgrespro/pg_wait_sampling/issues) with 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. Authors ------- * Alexander Korotkov , Postgres Professional, Moscow, Russia * Ildus Kurbangaliev , Postgres Professional, Moscow, Russia pg_wait_sampling-1.1.6/collector.c000066400000000000000000000302311464621513600172040ustar00rootroot00000000000000/* * collector.c * Collector of wait event history and profile. * * Copyright (c) 2015-2016, Postgres Professional * * IDENTIFICATION * contrib/pg_wait_sampling/pg_wait_sampling.c */ #include "postgres.h" #include "catalog/pg_type.h" #if PG_VERSION_NUM >= 130000 #include "common/hashfn.h" #endif #include "funcapi.h" #include "miscadmin.h" #include "postmaster/bgworker.h" #include "storage/ipc.h" #include "storage/procarray.h" #include "storage/procsignal.h" #include "storage/shm_mq.h" #include "storage/shm_toc.h" #include "storage/spin.h" #include "utils/memutils.h" #include "utils/resowner.h" #include "pgstat.h" #include "compat.h" #include "pg_wait_sampling.h" static volatile sig_atomic_t shutdown_requested = false; static void handle_sigterm(SIGNAL_ARGS); /* * Register background worker for collecting waits history. */ void pgws_register_wait_collector(void) { BackgroundWorker worker; /* Set up background worker parameters */ memset(&worker, 0, sizeof(worker)); worker.bgw_flags = BGWORKER_SHMEM_ACCESS; worker.bgw_start_time = BgWorkerStart_ConsistentState; 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(pgws_collector_main)); snprintf(worker.bgw_name, BGW_MAXLEN, "pg_wait_sampling collector"); worker.bgw_main_arg = (Datum) 0; RegisterBackgroundWorker(&worker); } /* * Allocate memory for waits history. */ static void alloc_history(History *observations, int count) { observations->items = (HistoryItem *) palloc0(sizeof(HistoryItem) * count); observations->index = 0; observations->count = count; observations->wraparound = false; } /* * Reallocate memory for changed number of history items. */ static void realloc_history(History *observations, int count) { HistoryItem *newitems; int copyCount, i, j; /* Allocate new array for history */ newitems = (HistoryItem *) palloc0(sizeof(HistoryItem) * count); /* Copy entries from old array to the new */ if (observations->wraparound) copyCount = observations->count; else copyCount = observations->index; copyCount = Min(copyCount, count); i = 0; if (observations->wraparound) j = observations->index + 1; else j = 0; while (i < copyCount) { if (j >= observations->count) j = 0; memcpy(&newitems[i], &observations->items[j], sizeof(HistoryItem)); i++; j++; } /* Switch to new history array */ pfree(observations->items); observations->items = newitems; observations->index = copyCount; observations->count = count; observations->wraparound = false; } static void handle_sigterm(SIGNAL_ARGS) { int save_errno = errno; shutdown_requested = true; if (MyProc) SetLatch(&MyProc->procLatch); errno = save_errno; } /* * Get next item of history with rotation. */ static HistoryItem * get_next_observation(History *observations) { HistoryItem *result; if (observations->index >= observations->count) { observations->index = 0; observations->wraparound = true; } result = &observations->items[observations->index]; observations->index++; return result; } /* * Read current waits from backends and write them to history array * and/or profile hash. */ static void probe_waits(History *observations, HTAB *profile_hash, bool write_history, bool write_profile, bool profile_pid) { int i, newSize; TimestampTz ts = GetCurrentTimestamp(); /* Realloc waits history if needed */ newSize = pgws_collector_hdr->historySize; if (observations->count != newSize) realloc_history(observations, newSize); /* Iterate PGPROCs under shared lock */ LWLockAcquire(ProcArrayLock, LW_SHARED); for (i = 0; i < ProcGlobal->allProcCount; i++) { HistoryItem item, *observation; PGPROC *proc = &ProcGlobal->allProcs[i]; if (!pgws_should_sample_proc(proc)) continue; /* Collect next wait event sample */ item.pid = proc->pid; item.wait_event_info = proc->wait_event_info; if (pgws_collector_hdr->profileQueries) item.queryId = pgws_proc_queryids[i]; else item.queryId = 0; item.ts = ts; /* Write to the history if needed */ if (write_history) { observation = get_next_observation(observations); *observation = item; } /* Write to the profile if needed */ if (write_profile) { ProfileItem *profileItem; bool found; if (!profile_pid) item.pid = 0; profileItem = (ProfileItem *) hash_search(profile_hash, &item, HASH_ENTER, &found); if (found) profileItem->count++; else profileItem->count = 1; } } LWLockRelease(ProcArrayLock); } /* * Send waits history to shared memory queue. */ static void send_history(History *observations, shm_mq_handle *mqh) { Size count, i; shm_mq_result mq_result; if (observations->wraparound) count = observations->count; else count = observations->index; mq_result = shm_mq_send_compat(mqh, sizeof(count), &count, false, true); if (mq_result == SHM_MQ_DETACHED) { ereport(WARNING, (errmsg("pg_wait_sampling collector: " "receiver of message queue has been detached"))); return; } for (i = 0; i < count; i++) { mq_result = shm_mq_send_compat(mqh, sizeof(HistoryItem), &observations->items[i], false, true); if (mq_result == SHM_MQ_DETACHED) { ereport(WARNING, (errmsg("pg_wait_sampling collector: " "receiver of message queue has been detached"))); return; } } } /* * Send profile to shared memory queue. */ 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; mq_result = shm_mq_send_compat(mqh, sizeof(count), &count, false, true); if (mq_result == SHM_MQ_DETACHED) { ereport(WARNING, (errmsg("pg_wait_sampling collector: " "receiver of message queue has been detached"))); return; } hash_seq_init(&scan_status, profile_hash); while ((item = (ProfileItem *) hash_seq_search(&scan_status)) != NULL) { mq_result = shm_mq_send_compat(mqh, sizeof(ProfileItem), item, false, true); if (mq_result == SHM_MQ_DETACHED) { hash_seq_term(&scan_status); ereport(WARNING, (errmsg("pg_wait_sampling collector: " "receiver of message queue has been detached"))); return; } } } /* * Make hash table for wait profile. */ static HTAB * make_profile_hash() { HASHCTL hash_ctl; hash_ctl.hash = tag_hash; hash_ctl.hcxt = TopMemoryContext; if (pgws_collector_hdr->profileQueries) hash_ctl.keysize = offsetof(ProfileItem, count); else hash_ctl.keysize = offsetof(ProfileItem, queryId); hash_ctl.entrysize = sizeof(ProfileItem); return hash_create("Waits profile hash", 1024, &hash_ctl, HASH_FUNCTION | HASH_ELEM); } /* * Delta between two timestamps in milliseconds. */ static int64 millisecs_diff(TimestampTz tz1, TimestampTz tz2) { long secs; int microsecs; TimestampDifference(tz1, tz2, &secs, µsecs); return secs * 1000 + microsecs / 1000; } /* * Main routine of wait history collector. */ 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; /* * 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 * 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(); InitPostgresCompat(NULL, InvalidOid, NULL, InvalidOid, 0, NULL); SetProcessingMode(NormalProcessing); /* Make pg_wait_sampling recognisable in pg_stat_activity */ pgstat_report_appname("pg_wait_sampling collector"); profile_hash = make_profile_hash(); 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, pgws_collector_hdr->historySize); MemoryContextSwitchTo(old_context); ereport(LOG, (errmsg("pg_wait_sampling collector started"))); /* Start counting time for history and profile samples */ profile_ts = history_ts = GetCurrentTimestamp(); while (1) { int rc; 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(); /* 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); if (write_history || write_profile) { probe_waits(&observations, profile_hash, write_history, write_profile, pgws_collector_hdr->profilePid); if (write_history) { history_ts = current_ts; history_diff = 0; } if (write_profile) { profile_ts = current_ts; profile_diff = 0; } } /* Shutdown if requested */ if (shutdown_requested) break; /* * Wait until next sample time or request to do something through * 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); if (rc & WL_POSTMASTER_DEATH) proc_exit(1); ResetLatch(&MyProc->procLatch); /* Handle request if any */ if (pgws_collector_hdr->request != NO_REQUEST) { LOCKTAG tag; SHMRequest request; pgws_init_lock_tag(&tag, PGWS_COLLECTOR_LOCK); LockAcquire(&tag, ExclusiveLock, false, false); 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(pgws_collector_mq, MyProc); mqh = shm_mq_attach(pgws_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: Assert(false); } break; case SHM_MQ_DETACHED: ereport(WARNING, (errmsg("pg_wait_sampling collector: " "receiver of message queue have been " "detached"))); break; default: Assert(false); } shm_mq_detach(mqh); } else if (request == PROFILE_RESET) { /* Reset profile hash */ hash_destroy(profile_hash); profile_hash = make_profile_hash(); } LockRelease(&tag, ExclusiveLock, false); } } 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); } pg_wait_sampling-1.1.6/compat.h000066400000000000000000000031661464621513600165150ustar00rootroot00000000000000/* * 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 "utils/guc_tables.h" static 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 } #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, bits32 flags, char *out_dbname) { #if PG_VERSION_NUM >= 170000 InitPostgres(in_dbname, dboid, username, useroid, flags, out_dbname); #elif PG_VERSION_NUM >= 150000 InitPostgres(in_dbname, dboid, username, useroid, flags & INIT_PG_LOAD_SESSION_LIBS, flags & INIT_PG_OVERRIDE_ALLOW_CONNS, out_dbname); #else InitPostgres(in_dbname, dboid, username, useroid, out_dbname, flags & INIT_PG_OVERRIDE_ALLOW_CONNS); #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 pg_wait_sampling-1.1.6/conf.add000066400000000000000000000000561464621513600164530ustar00rootroot00000000000000shared_preload_libraries = 'pg_wait_sampling' pg_wait_sampling-1.1.6/expected/000077500000000000000000000000001464621513600166545ustar00rootroot00000000000000pg_wait_sampling-1.1.6/expected/load.out000066400000000000000000000016751464621513600203350ustar00rootroot00000000000000CREATE EXTENSION pg_wait_sampling; \d pg_wait_sampling_current View "public.pg_wait_sampling_current" Column | Type | Modifiers ------------+---------+----------- pid | integer | event_type | text | event | text | queryid | bigint | \d pg_wait_sampling_history View "public.pg_wait_sampling_history" Column | Type | Modifiers ------------+--------------------------+----------- pid | integer | ts | timestamp with time zone | event_type | text | event | text | queryid | bigint | \d pg_wait_sampling_profile View "public.pg_wait_sampling_profile" Column | Type | Modifiers ------------+---------+----------- pid | integer | event_type | text | event | text | queryid | bigint | count | bigint | DROP EXTENSION pg_wait_sampling; pg_wait_sampling-1.1.6/expected/load_1.out000066400000000000000000000026301464621513600205450ustar00rootroot00000000000000CREATE EXTENSION pg_wait_sampling; \d pg_wait_sampling_current View "public.pg_wait_sampling_current" Column | Type | Collation | Nullable | Default ------------+---------+-----------+----------+--------- pid | integer | | | event_type | text | | | event | text | | | queryid | bigint | | | \d pg_wait_sampling_history View "public.pg_wait_sampling_history" Column | Type | Collation | Nullable | Default ------------+--------------------------+-----------+----------+--------- pid | integer | | | ts | timestamp with time zone | | | event_type | text | | | event | text | | | queryid | bigint | | | \d pg_wait_sampling_profile View "public.pg_wait_sampling_profile" Column | Type | Collation | Nullable | Default ------------+---------+-----------+----------+--------- pid | integer | | | event_type | text | | | event | text | | | queryid | bigint | | | count | bigint | | | DROP EXTENSION pg_wait_sampling; pg_wait_sampling-1.1.6/expected/queries.out000066400000000000000000000016641464621513600210710ustar00rootroot00000000000000CREATE EXTENSION pg_wait_sampling; WITH t as (SELECT sum(0) FROM pg_wait_sampling_current) SELECT sum(0) FROM generate_series(1, 2), t; sum ----- 0 (1 row) WITH t as (SELECT sum(0) FROM pg_wait_sampling_history) SELECT sum(0) FROM generate_series(1, 2), t; sum ----- 0 (1 row) WITH t as (SELECT sum(0) FROM pg_wait_sampling_profile) SELECT sum(0) FROM generate_series(1, 2), t; sum ----- 0 (1 row) -- Some dummy checks just to be sure that all our functions work and return something. SELECT count(*) = 1 as test FROM pg_wait_sampling_get_current(pg_backend_pid()); test ------ t (1 row) SELECT count(*) >= 0 as test FROM pg_wait_sampling_get_profile(); test ------ t (1 row) SELECT count(*) >= 0 as test FROM pg_wait_sampling_get_history(); test ------ t (1 row) SELECT pg_wait_sampling_reset_profile(); pg_wait_sampling_reset_profile -------------------------------- (1 row) DROP EXTENSION pg_wait_sampling; pg_wait_sampling-1.1.6/pg_wait_sampling--1.0--1.1.sql000066400000000000000000000027561464621513600220550ustar00rootroot00000000000000/* contrib/pg_wait_sampling/pg_wait_sampling--1.0--1.1.sql */ DROP FUNCTION pg_wait_sampling_get_current ( pid int4, OUT pid int4, OUT event_type text, OUT event text ) CASCADE; DROP FUNCTION pg_wait_sampling_get_history ( OUT pid int4, OUT ts timestamptz, OUT event_type text, OUT event text ) CASCADE; DROP FUNCTION pg_wait_sampling_get_profile ( OUT pid int4, OUT event_type text, OUT event text, OUT count bigint ) CASCADE; CREATE FUNCTION pg_wait_sampling_get_current ( pid int4, OUT pid int4, OUT event_type text, OUT event text, OUT queryid int8 ) RETURNS SETOF record AS 'MODULE_PATHNAME' LANGUAGE C VOLATILE CALLED ON NULL INPUT; CREATE VIEW pg_wait_sampling_current AS SELECT * FROM pg_wait_sampling_get_current(NULL::integer); GRANT SELECT ON pg_wait_sampling_current TO PUBLIC; CREATE FUNCTION pg_wait_sampling_get_history ( OUT pid int4, OUT ts timestamptz, OUT event_type text, OUT event text, OUT queryid int8 ) RETURNS SETOF record AS 'MODULE_PATHNAME' LANGUAGE C VOLATILE STRICT; CREATE VIEW pg_wait_sampling_history AS SELECT * FROM pg_wait_sampling_get_history(); GRANT SELECT ON pg_wait_sampling_history TO PUBLIC; CREATE FUNCTION pg_wait_sampling_get_profile ( OUT pid int4, OUT event_type text, OUT event text, OUT queryid int8, OUT count int8 ) RETURNS SETOF record AS 'MODULE_PATHNAME' LANGUAGE C VOLATILE STRICT; CREATE VIEW pg_wait_sampling_profile AS SELECT * FROM pg_wait_sampling_get_profile(); GRANT SELECT ON pg_wait_sampling_profile TO PUBLIC; pg_wait_sampling-1.1.6/pg_wait_sampling--1.1.sql000066400000000000000000000027331464621513600214770ustar00rootroot00000000000000/* contrib/pg_wait_sampling/setup.sql */ -- complain if script is sourced in psql, rather than via CREATE EXTENSION \echo Use "CREATE EXTENSION pg_wait_sampling" to load this file. \quit CREATE FUNCTION pg_wait_sampling_get_current ( pid int4, OUT pid int4, OUT event_type text, OUT event text, OUT queryid int8 ) RETURNS SETOF record AS 'MODULE_PATHNAME' LANGUAGE C VOLATILE CALLED ON NULL INPUT; CREATE VIEW pg_wait_sampling_current AS SELECT * FROM pg_wait_sampling_get_current(NULL::integer); GRANT SELECT ON pg_wait_sampling_current TO PUBLIC; CREATE FUNCTION pg_wait_sampling_get_history ( OUT pid int4, OUT ts timestamptz, OUT event_type text, OUT event text, OUT queryid int8 ) RETURNS SETOF record AS 'MODULE_PATHNAME' LANGUAGE C VOLATILE STRICT; CREATE VIEW pg_wait_sampling_history AS SELECT * FROM pg_wait_sampling_get_history(); GRANT SELECT ON pg_wait_sampling_history TO PUBLIC; CREATE FUNCTION pg_wait_sampling_get_profile ( OUT pid int4, OUT event_type text, OUT event text, OUT queryid int8, OUT count int8 ) RETURNS SETOF record AS 'MODULE_PATHNAME' LANGUAGE C VOLATILE STRICT; CREATE VIEW pg_wait_sampling_profile AS SELECT * FROM pg_wait_sampling_get_profile(); GRANT SELECT ON pg_wait_sampling_profile TO PUBLIC; CREATE FUNCTION pg_wait_sampling_reset_profile() RETURNS void AS 'MODULE_PATHNAME' LANGUAGE C VOLATILE STRICT; -- Don't want this to be available to non-superusers. REVOKE ALL ON FUNCTION pg_wait_sampling_reset_profile() FROM PUBLIC; pg_wait_sampling-1.1.6/pg_wait_sampling.c000066400000000000000000000743521464621513600205560ustar00rootroot00000000000000/* * pg_wait_sampling.c * Track information about wait events. * * Copyright (c) 2015-2017, Postgres Professional * * IDENTIFICATION * contrib/pg_wait_sampling/pg_wait_sampling.c */ #include "postgres.h" #include "access/htup_details.h" #include "access/twophase.h" #include "catalog/pg_type.h" #include "fmgr.h" #include "funcapi.h" #include "miscadmin.h" #include "optimizer/planner.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/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" #include "utils/guc.h" #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; 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 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; /* 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 */ 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; #endif static shmem_startup_hook_type prev_shmem_startup_hook = NULL; static PGPROC * search_proc(int backendPid); static PlannedStmt *pgws_planner_hook(Query *parse, #if PG_VERSION_NUM >= 130000 const char *query_string, #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); 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 ); /*---- 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. * * The value has to be in sync with ProcGlobal->allProcCount, initialized in * InitProcGlobal() (proc.c). * */ 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 /* * 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; /* * Starting with pg12, wal senders aren't part of MaxConnections anymore * and have to be accounted for. */ count += max_wal_senders; #endif /* pg 15- */ /* End of MaxBackends calculation. */ /* Add AuxiliaryProcs */ count += NUM_AUXILIARY_PROCS; return count; } /* * Estimate amount of shared memory needed. */ static Size pgws_shmem_size(void) { shm_toc_estimator e; Size size; int nkeys; shm_toc_initialize_estimator(&e); nkeys = 3; shm_toc_estimate_chunk(&e, sizeof(CollectorShmqHeader)); shm_toc_estimate_chunk(&e, (Size) COLLECTOR_QUEUE_SIZE); shm_toc_estimate_chunk(&e, sizeof(uint64) * get_max_procs_count()); shm_toc_estimate_keys(&e, nkeys); size = shm_toc_estimate(&e); 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. * * 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. */ static void pgws_shmem_startup(void) { bool found; Size segsize = pgws_shmem_size(); void *pgws; shm_toc *toc; pgws = ShmemInitStruct("pg_wait_sampling", segsize, &found); if (!found) { toc = shm_toc_create(PG_WAIT_SAMPLING_MAGIC, pgws, segsize); 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 { 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); pgws_proc_queryids = shm_toc_lookup(toc, 2, false); } shmem_initialized = true; if (prev_shmem_startup_hook) prev_shmem_startup_hook(); } /* * Check shared memory is initialized. Report an error otherwise. */ static void check_shmem(void) { if (!shmem_initialized) { ereport(ERROR, (errcode(ERRCODE_INTERNAL_ERROR), errmsg("pg_wait_sampling shared memory wasn't initialized yet"))); } } 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(recv_mqh); LockRelease(&queueTag, ExclusiveLock, false); } /* * Module load callback */ void _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 pgws_register_wait_collector(); /* * Install hooks. */ #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; 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; } /* * Find PGPROC entry responsible for given pid assuming ProcArrayLock was * already taken. */ static PGPROC * search_proc(int pid) { int i; if (pid == 0) return MyProc; for (i = 0; i < ProcGlobal->allProcCount; i++) { PGPROC *proc = &ProcGlobal->allProcs[i]; if (proc->pid && proc->pid == pid) { return proc; } } ereport(ERROR, (errcode(ERRCODE_INTERNAL_ERROR), errmsg("backend with pid=%d not found", 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; 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; check_shmem(); if (SRF_IS_FIRSTCALL()) { MemoryContext oldcontext; TupleDesc tupdesc; funcctx = SRF_FIRSTCALL_INIT(); oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx); params = (WaitCurrentContext *)palloc0(sizeof(WaitCurrentContext)); params->ts = GetCurrentTimestamp(); funcctx->user_fctx = params; tupdesc = CreateTemplateTupleDesc(4); TupleDescInitEntry(tupdesc, (AttrNumber) 1, "pid", INT4OID, -1, 0); TupleDescInitEntry(tupdesc, (AttrNumber) 2, "type", TEXTOID, -1, 0); TupleDescInitEntry(tupdesc, (AttrNumber) 3, "event", TEXTOID, -1, 0); TupleDescInitEntry(tupdesc, (AttrNumber) 4, "queryid", INT8OID, -1, 0); funcctx->tuple_desc = BlessTupleDesc(tupdesc); LWLockAcquire(ProcArrayLock, LW_SHARED); if (!PG_ARGISNULL(0)) { HistoryItem *item; PGPROC *proc; proc = search_proc(PG_GETARG_UINT32(0)); params->items = (HistoryItem *) palloc0(sizeof(HistoryItem)); item = ¶ms->items[0]; item->pid = proc->pid; item->wait_event_info = proc->wait_event_info; item->queryId = pgws_proc_queryids[proc - ProcGlobal->allProcs]; funcctx->max_calls = 1; } else { 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]; 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; } LWLockRelease(ProcArrayLock); MemoryContextSwitchTo(oldcontext); } /* stuff done on every call of the function */ funcctx = SRF_PERCALL_SETUP(); params = (WaitCurrentContext *) funcctx->user_fctx; if (funcctx->call_cntr < funcctx->max_calls) { HeapTuple tuple; Datum values[4]; bool nulls[4]; const char *event_type, *event; HistoryItem *item; item = ¶ms->items[funcctx->call_cntr]; /* Make and return next tuple to caller */ MemSet(values, 0, sizeof(values)); MemSet(nulls, 0, sizeof(nulls)); event_type = pgstat_get_wait_event_type(item->wait_event_info); event = pgstat_get_wait_event(item->wait_event_info); values[0] = Int32GetDatum(item->pid); if (event_type) values[1] = PointerGetDatum(cstring_to_text(event_type)); else nulls[1] = true; if (event) values[2] = PointerGetDatum(cstring_to_text(event)); else nulls[2] = true; values[3] = UInt64GetDatum(item->queryId); tuple = heap_form_tuple(funcctx->tuple_desc, values, nulls); SRF_RETURN_NEXT(funcctx, HeapTupleGetDatum(tuple)); } else { SRF_RETURN_DONE(funcctx); } } typedef struct { Size count; ProfileItem *items; } Profile; void pgws_init_lock_tag(LOCKTAG *tag, uint32 lock) { tag->locktag_field1 = PG_WAIT_SAMPLING_MAGIC; tag->locktag_field2 = lock; tag->locktag_field3 = 0; tag->locktag_field4 = 0; tag->locktag_type = LOCKTAG_USERLOCK; tag->locktag_lockmethodid = USER_LOCKMETHOD; } 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; /* Ensure nobody else trying to send request to queue */ pgws_init_lock_tag(&queueTag, PGWS_QUEUE_LOCK); LockAcquire(&queueTag, ExclusiveLock, false, false); pgws_init_lock_tag(&collectorTag, PGWS_COLLECTOR_LOCK); LockAcquire(&collectorTag, ExclusiveLock, false, false); recv_mq = shm_mq_create(pgws_collector_mq, COLLECTOR_QUEUE_SIZE); pgws_collector_hdr->request = request; LockRelease(&collectorTag, ExclusiveLock, false); if (!pgws_collector_hdr->latch) ereport(ERROR, (errcode(ERRCODE_INTERNAL_ERROR), errmsg("pg_wait_sampling collector wasn't started"))); SetLatch(pgws_collector_hdr->latch); 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. * * 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(). */ oldctx = MemoryContextSwitchTo(TopMemoryContext); recv_mqh = shm_mq_attach(recv_mq, NULL, NULL); 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. * * PG_ENSURE_ERROR_CLEANUP() guaranties that cleanup callback will be * fired for both ERROR and FATAL. */ PG_ENSURE_ERROR_CLEANUP(pgws_cleanup_callback, 0); { res = shm_mq_receive(recv_mqh, &len, &data, false); if (res != SHM_MQ_SUCCESS || len != sizeof(*count)) elog(ERROR, "error reading mq"); memcpy(count, data, sizeof(*count)); result = palloc(item_size * (*count)); ptr = result; for (i = 0; i < *count; i++) { res = shm_mq_receive(recv_mqh, &len, &data, false); if (res != SHM_MQ_SUCCESS || len != item_size) elog(ERROR, "error reading mq"); memcpy(ptr, data, item_size); ptr += item_size; } } PG_END_ENSURE_ERROR_CLEANUP(pgws_cleanup_callback, 0); /* We still have to detach and release lock during normal operation. */ shm_mq_detach(recv_mqh); LockRelease(&queueTag, ExclusiveLock, false); return result; } PG_FUNCTION_INFO_V1(pg_wait_sampling_get_profile); Datum pg_wait_sampling_get_profile(PG_FUNCTION_ARGS) { Profile *profile; FuncCallContext *funcctx; check_shmem(); if (SRF_IS_FIRSTCALL()) { MemoryContext oldcontext; TupleDesc tupdesc; funcctx = SRF_FIRSTCALL_INIT(); oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx); /* Receive profile from shmq */ profile = (Profile *) palloc0(sizeof(Profile)); profile->items = (ProfileItem *) receive_array(PROFILE_REQUEST, sizeof(ProfileItem), &profile->count); funcctx->user_fctx = profile; funcctx->max_calls = profile->count; /* Make tuple descriptor */ tupdesc = CreateTemplateTupleDesc(5); TupleDescInitEntry(tupdesc, (AttrNumber) 1, "pid", INT4OID, -1, 0); TupleDescInitEntry(tupdesc, (AttrNumber) 2, "type", TEXTOID, -1, 0); TupleDescInitEntry(tupdesc, (AttrNumber) 3, "event", TEXTOID, -1, 0); TupleDescInitEntry(tupdesc, (AttrNumber) 4, "queryid", INT8OID, -1, 0); TupleDescInitEntry(tupdesc, (AttrNumber) 5, "count", INT8OID, -1, 0); funcctx->tuple_desc = BlessTupleDesc(tupdesc); MemoryContextSwitchTo(oldcontext); } /* stuff done on every call of the function */ funcctx = SRF_PERCALL_SETUP(); profile = (Profile *) funcctx->user_fctx; if (funcctx->call_cntr < funcctx->max_calls) { /* for each row */ Datum values[5]; bool nulls[5]; HeapTuple tuple; ProfileItem *item; const char *event_type, *event; item = &profile->items[funcctx->call_cntr]; MemSet(values, 0, sizeof(values)); MemSet(nulls, 0, sizeof(nulls)); /* Make and return next tuple to caller */ event_type = pgstat_get_wait_event_type(item->wait_event_info); event = pgstat_get_wait_event(item->wait_event_info); values[0] = Int32GetDatum(item->pid); if (event_type) values[1] = PointerGetDatum(cstring_to_text(event_type)); else nulls[1] = true; if (event) values[2] = PointerGetDatum(cstring_to_text(event)); else nulls[2] = true; if (pgws_collector_hdr->profileQueries) values[3] = UInt64GetDatum(item->queryId); else values[3] = (Datum) 0; values[4] = UInt64GetDatum(item->count); tuple = heap_form_tuple(funcctx->tuple_desc, values, nulls); SRF_RETURN_NEXT(funcctx, HeapTupleGetDatum(tuple)); } else { /* nothing left */ SRF_RETURN_DONE(funcctx); } } PG_FUNCTION_INFO_V1(pg_wait_sampling_reset_profile); Datum pg_wait_sampling_reset_profile(PG_FUNCTION_ARGS) { LOCKTAG collectorTag; check_shmem(); pgws_init_lock_tag(&queueTag, PGWS_QUEUE_LOCK); LockAcquire(&queueTag, ExclusiveLock, false, 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(pgws_collector_hdr->latch); LockRelease(&queueTag, ExclusiveLock, false); PG_RETURN_VOID(); } PG_FUNCTION_INFO_V1(pg_wait_sampling_get_history); Datum pg_wait_sampling_get_history(PG_FUNCTION_ARGS) { History *history; FuncCallContext *funcctx; check_shmem(); if (SRF_IS_FIRSTCALL()) { MemoryContext oldcontext; TupleDesc tupdesc; funcctx = SRF_FIRSTCALL_INIT(); oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx); /* Receive history from shmq */ history = (History *) palloc0(sizeof(History)); history->items = (HistoryItem *) receive_array(HISTORY_REQUEST, sizeof(HistoryItem), &history->count); funcctx->user_fctx = history; funcctx->max_calls = history->count; /* Make tuple descriptor */ tupdesc = CreateTemplateTupleDesc(5); TupleDescInitEntry(tupdesc, (AttrNumber) 1, "pid", INT4OID, -1, 0); TupleDescInitEntry(tupdesc, (AttrNumber) 2, "sample_ts", TIMESTAMPTZOID, -1, 0); TupleDescInitEntry(tupdesc, (AttrNumber) 3, "type", TEXTOID, -1, 0); TupleDescInitEntry(tupdesc, (AttrNumber) 4, "event", TEXTOID, -1, 0); TupleDescInitEntry(tupdesc, (AttrNumber) 5, "queryid", INT8OID, -1, 0); funcctx->tuple_desc = BlessTupleDesc(tupdesc); MemoryContextSwitchTo(oldcontext); } /* stuff done on every call of the function */ funcctx = SRF_PERCALL_SETUP(); history = (History *) funcctx->user_fctx; if (history->index < history->count) { HeapTuple tuple; HistoryItem *item; Datum values[5]; bool nulls[5]; const char *event_type, *event; item = &history->items[history->index]; /* Make and return next tuple to caller */ MemSet(values, 0, sizeof(values)); MemSet(nulls, 0, sizeof(nulls)); event_type = pgstat_get_wait_event_type(item->wait_event_info); event = pgstat_get_wait_event(item->wait_event_info); values[0] = Int32GetDatum(item->pid); values[1] = TimestampTzGetDatum(item->ts); if (event_type) values[2] = PointerGetDatum(cstring_to_text(event_type)); else nulls[2] = true; if (event) values[3] = PointerGetDatum(cstring_to_text(event)); else nulls[3] = true; values[4] = UInt64GetDatum(item->queryId); tuple = heap_form_tuple(funcctx->tuple_desc, values, nulls); history->index++; SRF_RETURN_NEXT(funcctx, HeapTupleGetDatum(tuple)); } else { /* nothing left */ SRF_RETURN_DONE(funcctx); } PG_RETURN_VOID(); } /* * planner_hook hook, save queryId for collector */ static PlannedStmt * pgws_planner_hook(Query *parse, #if PG_VERSION_NUM >= 130000 const char *query_string, #endif int cursorOptions, ParamListInfo boundParams) { 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(); { /* Invoke original hook if needed */ if (planner_hook_next) result = planner_hook_next(parse, #if PG_VERSION_NUM >= 130000 query_string, #endif cursorOptions, boundParams); else result = standard_planner(parse, #if PG_VERSION_NUM >= 130000 query_string, #endif cursorOptions, boundParams); 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(); return result; } /* * ExecutorStart hook: save queryId for collector */ static void 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); else standard_ExecutorStart(queryDesc, eflags); } static void 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(); { if (prev_ExecutorRun) prev_ExecutorRun(queryDesc, direction, count, execute_once); 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(); } static void pgws_ExecutorFinish(QueryDesc *queryDesc) { int i = MyProc - ProcGlobal->allProcs; uint64 save_queryId = pgws_proc_queryids[i]; nesting_level++; PG_TRY(); { if (prev_ExecutorFinish) prev_ExecutorFinish(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(); } /* * ExecutorEnd hook: clear queryId */ static void pgws_ExecutorEnd(QueryDesc *queryDesc) { int i = MyProc - ProcGlobal->allProcs; if (nesting_level == 0) pgws_proc_queryids[i] = UINT64CONST(0); if (prev_ExecutorEnd) prev_ExecutorEnd(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; uint64 save_queryId = 0; if (pgws_enabled(nesting_level)) { save_queryId = pgws_proc_queryids[i]; 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); 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(); } pg_wait_sampling-1.1.6/pg_wait_sampling.control000066400000000000000000000002521464621513600220000ustar00rootroot00000000000000# pg_wait_sampling extension comment = 'sampling based statistics of wait events' default_version = '1.1' module_pathname = '$libdir/pg_wait_sampling' relocatable = true pg_wait_sampling-1.1.6/pg_wait_sampling.h000066400000000000000000000030531464621513600205510ustar00rootroot00000000000000/* * pg_wait_sampling.h * Headers for pg_wait_sampling extension. * * Copyright (c) 2015-2016, Postgres Professional * * IDENTIFICATION * contrib/pg_wait_sampling/pg_wait_sampling.h */ #ifndef __PG_WAIT_SAMPLING_H__ #define __PG_WAIT_SAMPLING_H__ #include "postgres.h" #include "storage/proc.h" #include "storage/shm_mq.h" #include "utils/timestamp.h" #define PG_WAIT_SAMPLING_MAGIC 0xCA94B107 #define COLLECTOR_QUEUE_SIZE (16 * 1024) #define HISTORY_TIME_MULTIPLIER 10 #define PGWS_QUEUE_LOCK 0 #define PGWS_COLLECTOR_LOCK 1 typedef struct { uint32 pid; uint32 wait_event_info; uint64 queryId; uint64 count; } ProfileItem; typedef struct { uint32 pid; uint32 wait_event_info; uint64 queryId; TimestampTz ts; } HistoryItem; typedef struct { bool wraparound; Size index; Size count; HistoryItem *items; } History; typedef enum { NO_REQUEST, HISTORY_REQUEST, PROFILE_REQUEST, PROFILE_RESET } SHMRequest; typedef struct { Latch *latch; SHMRequest request; int historySize; int historyPeriod; int profilePeriod; bool profilePid; int profileQueries; bool sampleCpu; } CollectorShmqHeader; /* pg_wait_sampling.c */ 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); extern PGDLLEXPORT void pgws_collector_main(Datum main_arg); #endif pg_wait_sampling-1.1.6/run-tests.sh000077500000000000000000000006571464621513600173660ustar00rootroot00000000000000#!/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 pg_wait_sampling-1.1.6/sql/000077500000000000000000000000001464621513600156525ustar00rootroot00000000000000pg_wait_sampling-1.1.6/sql/load.sql000066400000000000000000000002321464621513600173070ustar00rootroot00000000000000CREATE EXTENSION pg_wait_sampling; \d pg_wait_sampling_current \d pg_wait_sampling_history \d pg_wait_sampling_profile DROP EXTENSION pg_wait_sampling; pg_wait_sampling-1.1.6/sql/queries.sql000066400000000000000000000013201464621513600200440ustar00rootroot00000000000000CREATE EXTENSION pg_wait_sampling; WITH t as (SELECT sum(0) FROM pg_wait_sampling_current) SELECT sum(0) FROM generate_series(1, 2), t; WITH t as (SELECT sum(0) FROM pg_wait_sampling_history) SELECT sum(0) FROM generate_series(1, 2), t; WITH t as (SELECT sum(0) FROM pg_wait_sampling_profile) SELECT sum(0) FROM generate_series(1, 2), t; -- Some dummy checks just to be sure that all our functions work and return something. SELECT count(*) = 1 as test FROM pg_wait_sampling_get_current(pg_backend_pid()); SELECT count(*) >= 0 as test FROM pg_wait_sampling_get_profile(); SELECT count(*) >= 0 as test FROM pg_wait_sampling_get_history(); SELECT pg_wait_sampling_reset_profile(); DROP EXTENSION pg_wait_sampling;