Using EDB Wait States
When EDB Wait States is registered as one of the shared preload libraries, it probes each of the running sessions at regular intervals. For every session, it collects information such as:
- The database to which it's connected.
- The logged-in user of the session.
- The query running in that session.
- The wait events on which it's waiting.
This information is saved in a set of files in a user-configurable path and directory folder given by the edb_wait_states.directory
parameter to add to the postgresql.conf
file. The path must be a full, absolute path, not a relative path.
Exploring data with the interface
Each of the functions in the EDB Wait States interface has common input and output parameters. Those parameters are:
Parameter(s) | Input or output | Description |
---|---|---|
start_ts and end_ts | Input | Together these specify the time interval and the data to read. If you specify only start_ts , the data starting from start_ts is output. If you specify only end_ts , data up to end_ts is output. If you don't specify either, all the data is output. |
query_id | Output | Identifies a normalized query. It's internal hash code computed from the query. |
session_id | Output | Identifies a session. |
ref_start_ts and ref_end_ts | Output | The timestamps of a file containing a particular data point. A data point might be a wait event sample record, a query record, or a session record. |
wait_time | Output | The amount of time in seconds spent waiting for some wait events. |
cpu_time | Output | The amount of time in seconds spent working on the CPU. For this given duration, the query wasn't waiting on any wait event. |
db_time | Output | The sum of the wait_time and the cpu_time . The db_time , wait_time , and the cpu_time don't provide an exact time. They provide an approximate time computed based on number of occurrences and the sampling interval. |
The following examples use a scenario where three queries are executed simultaneously on four different sessions connected to different databases using different users. Those three queries are:
edb_wait_states_data
Use this function to read the data collected by the BGW:
You can use this function to find out the following:
The queries running in the given duration (defined by
start_ts
andend_ts
) in all the sessions, and the wait events, if any, they were waiting on. For example:The progress of a session within a given duration, that is, the queries run in a session (
session_id = 100000
) and the wait events the queries waited on. For example:The duration for which the samples are available. For example:
Parameters
In addition to the common parameters described previously, each row of the output gives the following:
dbname
— The session's database.
username
— The session's logged-in user.
query
— The query running in the session.
query_start_time
— The time when the query started.
sample_time
— The time when wait event data was collected.
wait_event_type
— The type of wait event the session (backend) is waiting on.
wait_event
— The wait event the session (backend) is waiting on.
Example
This example shows sample output from the edb_wait_states_data()
function:
edb_wait_states_directory_size
This function gives the size of the $PGDATA/edb_wait_states
directory.
The function returns the total size of all the files in the edb_wait_states
directory in bytes. Optionally specify the start_ts
and end_ts
timestamps to get the file size of all the files in the specified interval.
Note
This function calculates and gives the size of all the files with prefix ews_*
only. It ignores any other file added to the edb_wait_states
directory manually.
Example
This example shows the sample output from the edb_wait_states_directory_size()
function:
edb_wait_states_queries
This function gives information about the queries sampled by the BGW. For example:
A new queries file is created periodically. Multiple query files can be generated corresponding to specific intervals.
This function returns all the queries in query files that overlap with the given time interval. A query gives all the queries in query files that contained queries sampled between start_ts
and end_ts
:
In other words, the function can output queries that didn't run in the given interval. To do that, use edb_wait_states_data()
.
Parameters
In addition to the common parameters described previously, each row of the output gives the following:
query
— Normalized query text.
Example
This example shows sample output from the edb_wait_states_queries()
function:
edb_wait_states_sessions
This function gives information about the sessions sampled by the BGW:
You can use this function to identify the databases that were connected and the users that started those sessions. For example:
Similar to edb_wait_states_queries()
, this function outputs all the sessions logged in session files that contain sessions sampled in the given interval. It doesn't necessarily output only the sessions sampled in the given interval. To identify that, use edb_wait_states_data()
.
Parameters
In addition to the common parameters described previously, each row of the output gives the following:
dbname
— The database to which the session is connected.
username
— Login user of the session.
application_name
— Name of the application connection to the session.
client_hostname
— Host name of the client machine.
session_start_ts
— Actual timestamp when the session started.
Example
This example shows sample output from the edb_wait_states_sessions()
function:
edb_wait_states_samples
This function gives information about wait events sampled by the BGW:
Usually, you don't need to call this function directly.
Parameters
In addition to the common parameters described previously, each row of the output gives the following:
query_start_time
— The time when the query started in this session.
sample_time
— The time when wait event data was collected.
wait_event_type
— The type of wait event on which the session is waiting.
wait_event
— The wait event on which the session (backend) is waiting.
sampling_interval
— The time interval at which the sample is taken.
Example
This example shows sample output from the edb_wait_states_samples()
function:
edb_wait_states_purge
The function deletes all the sampled data files (queries, sessions, and wait-event samples) that were created after start_ts
and aged (rotated) before end_ts
:
Usually you don't need to run this function. The backend usually purges those according to the retention age. However, if that doesn't happen for some reason, you can use this function.
To find out how long the samples were retained, use edb_wait_states_data()
.
Example
This code shows the $PGDATA/edb_wait_states
directory before running edb_wait_states_purge()
:
This code shows the $PGDATA/edb_wait_states
directory after running edb_wait_states_purge()
:
Deactivating
To disable EDB Wait States from collecting more data, enter:
To reenable EDB Wait States, set edb_wait_states.enable_collection
to ON
.
You can check whether the EDB Wait States is collecting data using a SHOW
command. In the following example, data collection is enabled in EDB Wait States:
edb_wait_states_system_info
The function outputs the hostname, CPU information, and memory information for the server machine.
Parameters
Each row of the output gives the following information:
host_name
— The hostname of the server machine.
cpu_info
— CPU information about the server machine.
mem_info
— Memory information about the server machine.
Example
This example shows sample output from the edb_wait_states_system_info()
function:
edb_wait_states_wait_events
The function outputs aggregated wait event information.
Parameters
In addition to the common parameters described previously, each row of the output gives the following information:
waitevent