- Notifications
You must be signed in to change notification settings - Fork41
Sampling based statistics of wait events
License
postgrespro/pg_wait_sampling
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
PostgreSQL provides information about current wait event of particularprocess. However, in order to gather descriptive statistics of serverbehavior users have to sample current wait events multiple times.pg_wait_sampling is an extension for collecting sampling statistics of waitevents.
The module must be loaded by addingpg_wait_sampling toshared_preload_libraries in postgresql.conf, because it requires additionalshared memory and launches a background worker. This means that a server restartis needed to add or remove the module.
When used withpg_stat_statements it is recommended to putpg_stat_statementsbeforepg_wait_sampling inshared_preload_libraries so queryIds ofutility statements are not rewritten by the former.
Whenpg_wait_sampling is enabled, it collects two kinds of statistics.
- History of waits events. It's implemented as in-memory ring buffer wheresamples of each process' wait events are written with given (configurable)period. Therefore, for each running process user can see some number ofrecent samples depending on history size (configurable). Assuming there isa client who periodically reads this history and dumps it somewhere, usercan have continuous history of wait events.
- Waits profile. It's implemented as in-memory hash table where samplesare accumulated per each wait event and can be divided by process, wait event,query and other dimensions. 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 wait events over time.
In combination withpg_stat_statements this extension can also provideper query statistics.
pg_wait_sampling launches special background worker for gathering thestatistics above.
pg_wait_sampling is implemented as an extension and not available in defaultPostgreSQL installation. It is available fromgithubunder the same license asPostgreSQLand supports PostgreSQL 13+.
Pre-builtpg_wait_sampling packages are provided in official PostgreSQLrepository:https://download.postgresql.org/pub/repos/
pg_wait_sampling is PostgreSQL extension which requires PostgreSQL 13 orhigher. Before build and install you should ensure following:
- PostgreSQL version is 13 or higher.
- You have development package of PostgreSQL installed or you have builtPostgreSQL from source.
- Your PATH variable is configured so that
pg_configcommand available, orPG_CONFIG variable is set.
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 installThen addshared_preload_libraries = pg_wait_sampling topostgresql.conf andrestart the server.
To test your installation:
$ make USE_PGXS=1 installcheckTo create the extension in the target database:
CREATE EXTENSION pg_wait_sampling;Compilation on Windows is not supported, since the extension uses symbols from PostgreSQLthat are not exported.
pg_wait_sampling interacts with user by set of views and functions.
pg_wait_sampling_current view – information about current wait events forall processes 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 |
| role_id | int4 | Id of role |
| database_id | int4 | Id of database |
| leader_pid | int4 | Id of parallel query leader |
| backend_type | text | Name of backend type |
| backend_state | text | Name of backend state |
| backend_start | timestamptz | Timestamp of process start |
| client_addr | inet | Client address |
| client_hostname | text | Client hostname |
| application_name | text | Application name |
pg_wait_sampling_get_current(pid int4) returns the same table for single givenprocess.
pg_wait_sampling_history view – history of wait events obtained by sampling intoin-memory ring buffer.
| 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 |
| role_id | int4 | Id of role |
| database_id | int4 | Id of database |
| leader_pid | int4 | Id of parallel query leader |
| backend_type | text | Name of backend type |
| backend_state | text | Name of backend state |
| backend_start | timestamptz | Timestamp of process start |
| client_addr | inet | Client address |
| client_hostname | text | Client hostname |
| application_name | text | Application name |
| ts | timestamptz | Sample timestamp |
pg_wait_sampling_reset_history() function resets the history.
pg_wait_sampling_profile view – profile of wait events obtained by sampling intoin-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 |
| role_id | int4 | Id of role |
| database_id | int4 | Id of database |
| leader_pid | int4 | Id of parallel query leader |
| backend_type | text | Name of backend type |
| backend_state | text | Name of backend state |
| backend_start | timestamptz | Timestamp of process start |
| client_addr | inet | Client address |
| client_hostname | text | Client hostname |
| application_name | text | Application name |
| count | int8 | Count of samples |
pg_wait_sampling_reset_profile() function resets the profile.
The work of wait event statistics collector worker is controlled by followingGUCs.
| 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 |
| pg_wait_sampling.history_dimensions | text | Additional columns in extended history view | 'pid, event, queryid' |
| pg_wait_sampling.profile_dimensions | text | Additional columns in extended profile view | 'pid, event, queryid' |
Ifpg_wait_sampling.profile_pid is set to false, sampling profile wouldn't becollected in per-process manner. In this case the value of pid could wouldbe always zero and corresponding row contain samples among all the processes.
Ifpg_wait_sampling.profile_queries is set tonone,queryid field inviews will be zero. If it is set totop, queryIds only of top level statementsare recorded. If it is set toall, queryIds of nested statements are recorded.
Ifpg_wait_sampling.sample_cpu is set to true then processes that are notwaiting on anything are also sampled. The wait event columns for such processeswill be NULL.
pg_wait_sampling.history_dimenstions andpg_wait_sampling.profile_dimensionsdetermine what additional columns will be sampled inhistory/profile_extendedviews. Possible values areall,pid,event,query_id,role_id,database_id,leader_pid,backend_type,backend_state,backend_start,client_addr,client_hostname,application_nameand any combination of column names.event turns on and off both event and event_type columns.all cannot be used together with any other values and must be used alone.
Warning
Turning on any of the following columns:backend_type,backend_state,backend_start,client_addr,client_hostname,application_name willreduce performance compared to sampling none of those due to the need to lookinto BackendStatusTable. This is especially noticeable with PostgreSQL 13-16
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 withpg_reload_conf function)for changes to take effect.
Warning
When usingpg_reload_conf you also need to invokepg_wait_sampling_reset_history()andpg_wait_sampling_reset_profile() for correct application of new dimensions
SeePostgreSQL documentationfor list of possible wait events.
Please, notice, thatpg_wait_sampling is still under development and whileit's stable and tested, it may contain some bugs. Don't hesitate to raiseissues at github withyour bug reports.
If you're lacking of some functionality inpg_wait_sampling and feeling powerto implement it then you're welcome to make pull requests.
- Alexander Korotkova.korotkov@postgrespro.ru, Postgres Professional,Moscow, Russia
- Ildus Kurbangalievi.kurbangaliev@gmail.com, Postgres Professional,Moscow, Russia
About
Sampling based statistics of wait events
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.