Project: YugabyteDB (App Definition and Development / Database)
File: pg-stat-activity.md

title: View live queries with pgstatactivity linkTitle: View live queries description: Using pgstatactivity to troubleshoot issues and help to identify long running transactions. headerTitle: View live queries with pgstatactivity menu: v2.18: identifier: pg-stat-activity parent: query-tuning weight: 300 type: docs YugabyteDB supports the PostgreSQL `pgstatactivity` view to analyze live queries. This view returns analytic and diagnostic information about active YugabyteDB server processes and queries. The view returns one row per server process, and displays information related to the current status of the database connection. {{% explore-setup-single %}} At a `ysqlsh` prompt, run the following meta-command to return the fields supported by pgstatactivity: ```sql yugabyte=# \d pgstatactivity ``` The following table describes the fields and their values: | Field | Type | Description | | :- | : | :- | | `datid` | oid | Object identifier (OID) of the database to which the backend is connected. | | `datname` | name | Name of the database to which the backend is connected. | | `pid` | integer | Backend process ID. | | `usesysid` | oid | The user's OID. | | `usename` | name | The user's name. | | `application_name` | text | Name of the application connected to this backend. | | `client_addr` | inet | The client's IP address. Empty if the client is connected through a Unix socket on the server, or if this is an internal process such as autovacuum. | | `clienthostname` | text | The client's hostname, as reported by a reverse DNS lookup of clientaddr. | | `client_port` | integer | TCP port the client is using for communication with the backend server. A value of -1 indicates a Unix socket. | | `backend_start` | timestampz | Time at which the current backend process started. | | `xactstart` | timestampz | Time at which the current transaction started, or null if no transaction is active. If the current query is the process's first transaction, this field is equivalent to the `querystart` field. | | `querystart` | timestampz | Time at which the currently active query started. If the state field is not set to active, the querystart field indicates the time when the last query was started. | | `state_change` | timestampz | Time at which the previous state changed. | | `waiteventtype` | text | Type of event the backend is waiting for. | | `wait_event` | text | Name of the event being waited for. | | `state` | text | Current state of the

Valid values are active, idle, idle in transaction, idle in transaction (aborted), fastpath function call, and disabled. | | `backend_xid` | xid | This backend's top-level transaction identifier, if any. | | `backend_xmin` | xid | The current backend's xmin horizon. | | `query` | text | The last executed query. If `state` is `active`, this is the currently executing query. If `state` has a different value, this is the last executed query. By default, the query text is limited to the first 1,024 characters. Adjust the `trackactivityquery_size` parameter to change the character limit. | | `backendtype` | text | The current backend's type. Possible values are autovacuum launcher, autovacuum worker, background worker, background writer, client backend, checkpointer, startup, walreceiver, walsender, and walwriter_. | | `allocatedmembytes` | bigint | Heap memory usage in bytes of the backend process. | | `rssmembytes` | bigint | Resident Set Size of the backend process in bytes. It shows how much memory is allocated to the process and is in RAM. It does not include memory that is swapped out. | The following query returns basic information about active Yugabyte processes: ```sql yugabyte=# SELECT datname, pid, application_name, state, query FROM pgstatactivity; ``` ```output datname | pid | application_name | state | query -+-++--+- yugabyte | 10027 | ysqlsh | active | SELECT datname, pid, applicationname, state, query FROM pgstat_activity; | 10013 | | | (2 rows) ``` In this listing: `datname` is the database connected to this process. `pid` is the process ID. `application_name` is the application connected to this process. `state` is the operational condition of the process. `query` is the latest query executed for this process. Often enough, you may need to identify long-running queries, because these queries could indicate deeper problems. The pgstatactivity view can help identify these issues. In this example, you create an open transaction, identify it, and terminate it. The example uses the . Use the following query to return a row from the users table. ```sql yb_demo=# SELECT id, name, state FROM users WHERE id = 212; ``` ```output id | name | state --++- 212 | Jacinthe Rowe | CO (1 row) ``` Update the state column value of this role with a transaction. The query is deliberately missing the `END;` statement to close the transaction. ```sql yb_demo=# BEGIN TRANSACTION; UPDATE users SET state = 'IA' WHERE id = 212; ``` ```output BEGIN UPDATE 1 ``` Because the transaction never ends, it wastes resources as an open process. Check the state of the transaction by opening another `ysqlsh` instance and finding information about this idle transaction with

```sql yugabyte=# SELECT datname, pid, application_name, state, query FROM pgstatactivity; ``` ```output datname | pid | application_name | state | query -+-+++- yugabyte | 10381 | ysqlsh | active | SELECT datname, pid, applicationname, state, query FROM pgstat_activity; yb_demo | 10033 | ysqlsh | idle in transaction | UPDATE users SET state = 'IA' WHERE id = 212; | 10013 | | | (3 rows) ``` Find the idle transaction's PID. In the sample output in the previous step, it's PID 10033, in the second row. Terminate the idle transaction. Replace `<pid>` with the PID of the process to terminate. ```sql yugabyte=# SELECT pgterminatebackend(<pid>); ``` ```output pgterminatebackend t (1 row) ``` The pgterminatebackend function returns `t` on success, and `f` on failure. Query pgstatactivity again in the second terminal, and verify that the idle process has ended. ```sql yugabyte=# SELECT datname, pid, application_name, state, query FROM pgstatactivity; ``` ```output datname | pid | application_name | state | query -+-++--+- yugabyte | 10381 | ysqlsh | active | SELECT datname, pid, applicationname, state, query FROM pgstat_activity; | 10013 | | | (2 rows) ``` You can run some time-related queries to help you identify long-running transactions. These are particularly helpful when there are a lot of open connections on that node. Get a list of processes ordered by current `txn_duration`: ```sql yugabyte=# SELECT datname, pid, applicationname, state, query, now() - xactstart AS txn_duration FROM pgstatactivity ORDER BY txn_duration desc; ``` ```output datname | pid | applicationname | state | query | txnduration -+-++--+-+-- yugabyte | 17695 | ysqlsh | idle | | | 17519 | | | | yugabyte | 17540 | ysqlsh | active | SELECT datname, pid, applicationname, state, query, now() - xactstart+| 00:00:00 | | | | AS txn_duration +| | | | | FROM pgstatactivity +| | | | | ORDER BY txn_duration desc; | (3 rows) ``` Get a list of processes where the current transaction has taken more than 1 minute: ```sql yugabyte=# SELECT datname, pid, applicationname, state, query, xactstart FROM pgstatactivity WHERE now() - xact_start > '1 min'; ``` ```output datname | pid | applicationname | state | query | xactstart +-+++--+ yb_demo | 10033 | ysqlsh | idle in transaction | UPDATE users SET state = 'IA' WHERE id = 212; | 2021-05-06 15:26:28.74615-04 (1 row) ``` Refer to to track planning and execution of all the SQL statements. Refer to to track the COPY operation status. Refer to to optimize YSQL's EXPLAIN and EXPLAIN ANALYZE queries. Refer to show the query execution plan generated by YSQL.