Wait Events
Description
A wait event refers to the state in which a process or thread in a database cannot continue to execute temporarily because some resources are unavailable or some conditions need to be met.
A transaction or query enters the waiting state when it needs to obtain a lock, read or write data from or to a disk, wait for a response from a remote node, or access a shared memory structure. Although these waits are short, if they occur frequently or take too long each time, the overall throughput will be significantly slowed down. Traditional performance monitoring can only detect that the execution of an SQL statement is slow, but cannot locate the cause. To solve this, the wait event functionality comes into being.
GaussDB introduces a comprehensive wait event system. It not only records whether a wait event occurs, but also accurately collects statistics on the wait durations, number of wait times, and maximum, minimum, and average wait duration. In addition, it supports aggregation analysis by instance, thread, or SQL statement, greatly improving system observability and problem locating efficiency. In general, when the customer's job does not meet the latency requirements or customer expectations, the wait event functionality helps O&M personnel and developers understand the overall system and the detailed execution status of each thread, facilitating exception locating and system optimization.
Principles
The design concept of wait events is to regard wait behavior as traceable and aggregatable time segments. Wait events have nesting relationships. Therefore, the stack structure is used to automatically report the nesting relationships and analyze the cause behind. The recording and statistics of all wait events depend on the local wait event stack of a thread. Each backend thread maintains such a stack structure during running to trace the current wait link. Figure 1 shows the principle of the wait event functionality.
When a thread enters an operation that requires waiting (for example, applying for a lock, reading or writing a file, or waiting for a remote response), the system pushes the wait event to which the current operation belongs to the top of the stack. If there are other wait events in the stack, the system records information such as the start time and wait duration of the wait event at the top of the stack and adds the information to the global statistics view.
When the wait event ends and the thread exits the waiting state, the wait event at the top of the stack is popped out, and the system records the total duration of the event. If there are still remaining events in the stack, the system updates the start time of the wait event at the top of the stack to the current time, indicating that the wait event is still ongoing, thus maintaining the continuity of the wait chain.
Wait events are classified into the following types:
- STATUS: Wait for executing a key code segment. This is usually caused by time-consuming synchronization operations or resource coordination, for example, waiting for the application end to send data (STATUS/wait cmd), establishing a connection between the pooler CNs and DNs (STATUS/pooler create conn), obtaining a snapshot from the GTM, and executing SQL operators.
- IO_EVENT: Wait for I/O operations. The values of such wait events are large when a query involves scanning a large amount of data or accessing a large table. Typical scenarios include reading and writing HEAP files and writing WALs.
- LWLOCK: Wait for lock objects that protect the memory structure, which are used to protect globally shared memory structures, such as globally shared hash tables. Such wait events are usually generated in scenarios such as waiting for partition locks.
- LOCK: Wait for internal object locks of the database, such as relation locks, tuple locks, and transaction locks. The values of such wait events are large when a large number of concurrent updates occur.
Examples
- View the statistics about wait events on the current node. For details about how to use WAIT_EVENTS, see "Schema > DBE_PERF Schema > Wait Events > DBE_PERF.WAIT_EVENTS" in Reference.
-- View the statistics about wait events on the current node, including the total wait duration (total_wait_time) and average wait duration (avg_wait_time) of each type of wait event. gaussdb=# \x Expanded display is on. gaussdb=# SELECT nodename, type, event, wait, total_wait_time, avg_wait_time, last_updated FROM dbe_perf.wait_events ORDER BY total_wait_time DESC; -[ RECORD 1 ]---+--------------------------------------------- nodename | coordinator1 type | STATUS event | wait node wait | 393763 total_wait_time | 342584597958 avg_wait_time | 870027 last_updated | 2025-09-08 15:49:07.170297+08 -[ RECORD 2 ]---+--------------------------------------------- nodename | coordinator1 type | STATUS event | wait cmd wait | 351923 total_wait_time | 1438714213 avg_wait_time | 4088 last_updated | 2025-09-08 15:49:08.257995+08 -[ RECORD 3 ]---+--------------------------------------------- --More--
- View the waiting and blocking status of each thread on the current node.
For details about how to use PG_THREAD_WAIT_STATUS, see "System Catalogs and System Views > System Views > Other System Views > PG_THREAD_WAIT_STATUS" in Reference.
-- View the waiting and blocking status of each thread on the current node. The wait_status field indicates the type of the event that the current thread is waiting for. For example, if the value of wait_status is acquire lwlock, the current thread is waiting for a lightweight lock or transaction lock. gaussdb=# \x Expanded display is on. gaussdb=# SELECT node_name, db_name, thread_name, tid, query_id, sessionid, lwtid, wait_status FROM pg_thread_wait_status; -[ RECORD 1 ]--------------------------- node_name | coordinator1 db_name | thread_name | jit compile worker tid | 140601850853120 query_id | 0 sessionid | 140601850853120 lwtid | 56358 wait_status | none -[ RECORD 2 ]--------------------------- node_name | coordinator1 db_name | thread_name | PageWriter tid | 140603134310144 query_id | 0 sessionid | 140603134310144 lwtid | 56261 wait_status | none -[ RECORD 3 ]--------------------------- --More--
- View the wait events of FULL/SLOW SQL statements during execution. For details about how to use STATEMENT_HISTORY, see "System Catalogs and System Views > System Catalogs > Other System Catalogs > STATEMENT_HISTORY" in Reference.
-- View all wait events and their durations during the execution of full/slow SQL statements. gaussdb=# \x Expanded display is on. gaussdb=# SELECT query, unique_query_id, pg_catalog.statement_detail_decode(details, 'plaintext', true) FROM pg_catalog.statement_history; -[ RECORD 1 ]---+----------------------------------------------------------query | select gs_perf_do_shaking_collect(?); unique_query_id | 347666798 detail | ---------------Wait Events Area------------------ | '1' STATUS wait xact start command 139 (us) | '2' STATUS gtm get snapshot 125 (us) | '3' STATUS wait xact commit command 121 (us) | '4' IO_EVENT BufHashTableSearch 26 (us) | '5' STATUS flush data 11 (us) -[ RECORD 2 ]---+---------------------------------------------------------- --More--
Precautions
All wait event statistics are stored in the cumulative view in the memory, which is in the running state. After a node is restarted, the statistics are cleared.
What is your overall rating for this page?
Thank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot
