Back to Yugabyte Db

View live queries with pg_stat_activity

docs/content/v2024.2/explore/observability/pg-stat-activity.md

2026.1.0.0-b2510.0 KB
Original Source

YugabyteDB supports the PostgreSQL pg_stat_activity 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 %}}

Supported fields

At a ysqlsh prompt, run the following meta-command to return the fields supported by pg_stat_activity:

sql
yugabyte=# \d pg_stat_activity

The following table describes the fields and their values:

FieldTypeDescription
datidoidObject identifier (OID) of the database to which the backend is connected.
datnamenameName of the database to which the backend is connected.
pidintegerBackend process ID.
usesysidoidThe user's OID.
usenamenameThe user's name.
application_nametextName of the application connected to this backend.
client_addrinetThe 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.
client_hostnametextThe client's hostname, as reported by a reverse DNS lookup of client_addr.
client_portintegerTCP port the client is using for communication with the backend server. A value of -1 indicates a Unix socket.
backend_starttimestampzTime at which the current backend process started.
xact_starttimestampzTime 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 query_start field.
query_starttimestampzTime at which the currently active query started. If the state field is not set to active, the query_start field indicates the time when the last query was started.
state_changetimestampzTime at which the previous state changed.
wait_event_typetextType of event the backend is waiting for.
wait_eventtextName of the event being waited for.
statetextCurrent state of the backend. Valid values are active, idle, idle in transaction, idle in transaction (aborted), fastpath function call, and disabled.
backend_xidxidThis backend's top-level transaction identifier, if any.
backend_xminxidThe current backend's xmin horizon.
querytextThe 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 track_activity_query_size parameter to change the character limit.
backend_typetextThe current backend's type. Possible values are autovacuum launcher, autovacuum worker, background worker, background writer, client backend, checkpointer, startup, walreceiver, walsender, and walwriter.
allocated_mem_bytesbigintHeap memory usage in bytes of the backend process.
rss_mem_bytesbigintResident 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.

Examples

Get basic information

The following query returns basic information about active Yugabyte processes:

sql
yugabyte=# SELECT datname, pid, application_name, state, query
    FROM pg_stat_activity;
output
 datname  |  pid  | application_name | state  |                                   query
----------+-------+------------------+--------+----------------------------------------------------------------------------
 yugabyte | 10027 | ysqlsh           | active | SELECT datname, pid, application_name, state, query FROM pg_stat_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.

Identify and terminate an open transaction

Often enough, you may need to identify long-running queries, because these queries could indicate deeper problems. The pg_stat_activity view can help identify these issues. In this example, you create an open transaction, identify it, and terminate it. The example uses the Retail Analytics sample dataset.

Create an open transaction

  1. 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)
    
  2. 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
    

Find the open transaction

Because the transaction never ends, it wastes resources as an open process.

  1. Check the state of the transaction by opening another ysqlsh instance and finding information about this idle transaction with pg_stat_activity.

    sql
    yugabyte=# SELECT datname, pid, application_name, state, query
        FROM pg_stat_activity;
    
    output
     datname  |  pid  | application_name |        state        |                                   query
    ----------+-------+------------------+---------------------+----------------------------------------------------------------------------
     yugabyte | 10381 | ysqlsh           | active              | SELECT datname, pid, application_name, state, query FROM pg_stat_activity;
     yb_demo  | 10033 | ysqlsh           | idle in transaction | UPDATE users SET state = 'IA' WHERE id = 212;
              | 10013 |                  |                     |
    (3 rows)
    
  2. Find the idle transaction's PID. In the sample output in the previous step, it's PID 10033, in the second row.

Terminate the open transaction

  1. Terminate the idle transaction. Replace <pid> with the PID of the process to terminate.

    sql
    yugabyte=# SELECT pg_terminate_backend(<pid>);
    
    output
     pg_terminate_backend
    ---------------------------
     t
    (1 row)
    
  2. The pg_terminate_backend function returns t on success, and f on failure. Query pg_stat_activity again in the second terminal, and verify that the idle process has ended.

    sql
    yugabyte=# SELECT datname, pid, application_name, state, query
        FROM pg_stat_activity;
    
    output
     datname  |  pid  | application_name | state  |                                   query
    ----------+-------+------------------+--------+----------------------------------------------------------------------------
     yugabyte | 10381 | ysqlsh           | active | SELECT datname, pid, application_name, state, query FROM pg_stat_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, application_name, state, query, now() - xact_start
    AS txn_duration
    FROM pg_stat_activity
    ORDER BY txn_duration desc;
output
 datname  |  pid  | application_name | state  |                                  query                                  | txn_duration
----------+-------+------------------+--------+-------------------------------------------------------------------------+--------------
 yugabyte | 17695 | ysqlsh           | idle   |                                                                         |
          | 17519 |                  |        |                                                                         |
 yugabyte | 17540 | ysqlsh           | active | SELECT datname, pid, application_name, state, query, now() - xact_start+| 00:00:00
          |       |                  |        |     AS txn_duration                                                    +|
          |       |                  |        |     FROM pg_stat_activity                                              +|
          |       |                  |        |     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, application_name, state, query, xact_start
    FROM pg_stat_activity
    WHERE now() - xact_start > '1 min';
output
 datname |  pid  | application_name |        state        |                     query                     |          xact_start
---------+-------+------------------+---------------------+-----------------------------------------------+------------------------------
 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)

Learn more