Movatterモバイル変換


[0]ホーム

URL:



Facebook
Postgres Pro
Facebook
Downloads
F.52. pg_wait_sampling — collecting sampling-based statistics on wait events
Prev UpAppendix F. Additional Supplied Modules and Extensions Shipped inpostgrespro-std-17-contribHome Next

F.52. pg_wait_sampling — collecting sampling-based statistics on wait events#

pg_wait_sampling is aPostgres Pro extension for collecting sampling-based statistics on wait events.

Starting from the 9.6 version,Postgres Pro Standard provides information about the current wait events for particular processes. However, to get descriptive statistics of the server activity, you have to sample wait events multiple times. Thepg_wait_sampling extension automates wait events sampling by launching a special background worker. Withpg_wait_sampling enabled, you can get the following sampling-based data:

  • Waits history — the list of wait events for recent processes, with timestamps.

  • Waits profile — the number of wait event samples for all processes over time (per wait event type).

  • Current wait events for all processes, including background workers.

Usingpg_wait_sampling, you can troubleshoot dependencies for queries that process longer than expected. You can see what a particular process is waiting for at each moment of time, and analyze wait events statistics. For the list of possible wait events, seeTable 26.4.

In combination withpg_stat_statements, this extension can also provide per query statistics.

See Also

Viewing Statistics on Wait Events

Reference

F.52.1. Installation#

Thepg_wait_sampling extension is included intoPostgres Pro Standard and requires no special prerequisites.

To enablepg_wait_sampling, do the following:

  1. Addpg_wait_sampling to theshared_preload_libraries variable in thepostgresql.conf file:

    shared_preload_libraries = 'pg_wait_sampling'

    Important

    When using thepg_wait_sampling with thepg_stat_statements extension, placepg_stat_statements beforepg_wait_sampling in theshared_preload_libraries variable. This ensures that thepg_stat_statements extension does not overwrite query IDs that are used bypg_wait_sampling.

  2. Create thepg_wait_sampling extension using the following query:

    CREATE EXTENSION pg_wait_sampling;
  3. Restart the server. Sincepg_wait_sampling requires additional shared memory and launches a background worker, you must restart the server after adding or removingpg_wait_sampling.

Once the extension is created,pg_wait_sampling starts collecting statistics on wait events for each process. Optionally, you can change the sampling frequency and configure statistics collection modes usingGUC variables.

If you want to stop collecting statistics, drop thepg_wait_sampling extension, remove thepg_wait_sampling from theshared_preload_libraries variable, and restart the server again.

F.52.2. Usage#

F.52.2.1. Viewing Statistics on Wait Events#

Whenpg_wait_sampling is enabled, wait events are sampled each 10ms by default. You can access the collected statistics through a set of functions and views. If some of your queries appear stalled or take longer than expected, you can review all the current wait events in thepg_wait_sampling_current view:

SELECT * FROM pg_wait_sampling_current;

The returned statistics covers all the active processes, including background workers. To filter the view for a single process, runpg_wait_sampling_get_current() function specifying the process ID:

SELECT * FROM pg_wait_sampling_get_current(pid);

To better understand the dynamics of the recent wait events, you can access thepg_wait_sampling_history view that provides the wait events history for the latest processes:

SELECT * FROM pg_wait_sampling_history;

The returned view displays wait samples per process, with event timestamps. Waits history is implemented as an in-memory ring buffer. By default, the history size is limited to 5000 samples. To save history for a longer period of time, you can modify thepg_wait_sampling.history_size variable, or connect a client application to periodically read waits history and dump it to a local storage.

To monitor wait events in your database over time, use thepg_wait_sampling_profile view:

SELECT * FROM pg_wait_sampling_profile;

Waits profile is stored as an in-memory hash table that accumulates samples per each process and each wait event. You can reset waits profile by calling thepg_wait_sampling_reset_profile() function. If you connect a client to your database that periodically dumps the wait events data and resets the profile, you can save and compare statistics of wait events intensity over time.

Important

Since wait sampling statistics is stored in RAM, make sure to reset the waits profile from time to time to avoid memory overflow.

If you are not interested in the distribution of wait events between processes, you can set thepg_wait_sampling.profile_pid variable tofalse. In this case, thepid value for each process is set to zero, and wait samples for all the processes are stored together.

F.52.3. Reference#

F.52.3.1. GUC Variables#

Thepg_wait_sampling extension is configured with GUC variables. They are stored in the local memory and can be changed in thepostgresql.conf file or using theALTER SYSTEM command. For the changes to take effect, reload the server configuration. You can do it, for example, by calling thepg_reload_conf() function or executing thepg_ctlreload command.

pg_wait_sampling.history_size (int4)

Size of in-memory ring buffer for history sampling, in the number of samples.

Default: 5000

pg_wait_sampling.history_period (int4)

Time interval for history sampling, in milliseconds.

Default: 10

pg_wait_sampling.profile_period (int4)

Time interval for waits profile sampling, in milliseconds.

Default: 10

pg_wait_sampling.profile_pid (bool)

Sampling mode for waits profile. If set totrue, waits profile is collected per process. If you setpg_wait_sampling.profile_pid tofalse, waits profile is collected for all processes together. In this case, thepid value for each process is set to zero, and the corresponding row contains wait samples for all the processes.

Default:true

pg_wait_sampling.profile_queries (enum)

Sampling mode for the waits profile. Ifpg_wait_sampling.profile_queries is set tonone, thequeryid field in views will be zero. If it is set totop, query IDs of only the top level statements are recorded. If it is set toall, query IDs of nested statements are recorded. To collect the waits profile per query, ensure that thepg_stat_statements extension is configured and setpg_wait_sampling.profile_queries totop. For version 14 or higher, you can compute query IDs by configuring thecompute_query_id parameter and set thepg_wait_sampling.profile_queries value totop orall. For details, seeSection 18.9.2.

Default:top

pg_wait_sampling.sample_cpu (bool)

The sampling mode that determines whether to perform sampling of on-CPU backends. Ifpg_wait_sampling.sample_cpu is set totrue, then sampling also includes processes that are not waiting for anything. The wait event columns for such processes will have aNULL value.

Default:true

F.52.3.2. pg_wait_sampling Views#

F.52.3.2.1. pg_wait_sampling_current View#

Thepg_wait_sampling_current view provides the information about the current wait events for all processes, including background workers.

Table F.32. pg_wait_sampling_current View

Column Name Column Type Description
pidint4 Process ID
event_typetext Name of wait event type
eventtext Name of wait event
queryidint8 Query ID

F.52.3.2.2. pg_wait_sampling_history View#

Thepg_wait_sampling_history view provides the history of wait events. This data is stored as an in-memory ring buffer.

Table F.33. pg_wait_sampling_history View

Column Name Column Type Description
pidint4 Process ID
tstimestamptz Sample timestamp
event_typetext Name of wait event type
eventtext Name of wait event
queryidint8 Query ID

F.52.3.2.3. pg_wait_sampling_profile View#

Thepg_wait_sampling_profile view provides the profile of wait events. This data is stored as an in-memory hash table.

Table F.34. pg_wait_sampling_profile View

Column Name Column Type Description
pidint4 Process ID
event_typetext Name of wait event type
eventtext Name of wait event
queryidint8 Query ID
countint8 Number of samples

F.52.3.3. Functions#

pg_wait_sampling_get_current(pidint4)

Returns thepg_wait_sampling_current view with the list of current wait events. If you set thepid argument, the view is filtered for the process with thispid.

Arguments:

  • pid — Optional. The process ID for which to display the current wait events.

pg_wait_sampling_reset_profile()

Resets the waits profile and clears the memory.

F.52.4. Authors#


Prev Up Next
F.51. pg_visibility — visibility map information and utilities Home F.53. pg_walinspect — low-level WAL inspection
pdfepub
Go to Postgres Pro Standard 17
By continuing to browse this website, you agree to the use of cookies. Go toPrivacy Policy.

[8]ページ先頭

©2009-2025 Movatter.jp