PostgreSQL pg_stat_activity 统计数据库当前活动会话信息

PostgreSQL About 3,143 words

字段含义

字段名称 类型 描述
datid oid 数据库的对象ID。
datname name 数据库的名称。
pid integer 与会话关联的后台进程ID。
leader_pid integer 并行查询的领导者进程ID。
usesysid oid 用户的对象ID。
usename name 用户名。
application_name text 客户端连接的应用程序名称。
client_addr inet 客户端的IP地址。如果是本地连接,则为NULL
client_hostname text 客户端的主机名。如果没有解析主机名,则为NULL
client_port integer 客户端的TCP端口。如果是本地连接,则为-1
backend_start timestamp with time zone 该会话开始的时间。
xact_start timestamp with time zone 当前事务开始的时间。如果没有正在进行的事务,则为NULL
query_start timestamp with time zone 当前正在执行的查询开始的时间。
state_change timestamp with time zone 上次会话状态改变的时间。
wait_event_type text 正在等待的事件类型,如果不等待则为NULL
wait_event text 正在等待的具体事件,如果不等待则为NULL
state text 会话的当前状态。可能的值包括active, idle, idle in transaction, idle in transaction (aborted), fastpath function call, disabled
backend_xid xid 后台进程的当前事务ID。
backend_xmin xid 后台进程的最早未提交事务ID。
query text 当前正在执行的查询。如果为空闲或在事务中空闲,则显示最近执行的语句。
backend_type text 后台进程的类型,例如autovacuum launcherlogical replication launcher等。

查询正在执行的 SQL

可以查看query字段。

select * from pg_stat_activity;

输出

postgres=# select * from pg_stat_activity ;
-[ RECORD 1 ]----+------------------------------------------------
datid            | 13759
datname          | postgres
pid              | 50417
leader_pid       | 
usesysid         | 10
usename          | postgres
application_name | psql
client_addr      | 
client_hostname  | 
client_port      | -1
backend_start    | 2025-02-10 07:42:35.082126+00
xact_start       | 
query_start      | 2025-02-12 02:30:55.549706+00
state_change     | 2025-02-12 02:30:55.558081+00
wait_event_type  | Client
wait_event       | ClientRead
state            | idle
backend_xid      | 
backend_xmin     | 
query_id         | 
query            | select * from pg_stat_activity;
backend_type     | client backend

统计客户端连接情况

  • open:打开状态的连接
  • active:激活状态的连接
  • idle:空闲状态的连接
  • idle in transaction:事务中空闲状态的连接
select  datname,
        count(*) as open,
        count(*) filter (where state = 'active') as active,
        count(*) filter (where state = 'idle') as idle,
        count(*) filter (where state = 'idle in transaction') as idle_in_transaction
from pg_stat_activity
where backend_type='client backend'

输出

postgres=# select  datname,
postgres-#         count(*) as open,
postgres-#         count(*) filter (where state = 'active') as active,
postgres-#         count(*) filter (where state = 'idle') as idle,
postgres-#         count(*) filter (where state = 'idle in transaction') as idle_in_transaction
postgres-# from pg_stat_activity
postgres-# where backend_type='client backend'
postgres-# group by rollup(1);
 datname  | open | active | idle | idle_in_transaction 
----------+------+--------+------+---------------------
 testdb1  |   10 |      0 |   10 |                   0
 testdb2  |   20 |      0 |   20 |                   0
 postgres |    3 |      1 |    2 |                   0
          |   33 |      1 |   32 |                   0
(4 rows)

断开指定连接

使用pg_terminate_backend函数可以断开连接。

SELECT pg_terminate_backend(pid)
FROM pg_stat_activity
WHERE pid = specific_pid;

官方文档

https://www.postgresql.org/docs/17/monitoring-stats.html#MONITORING-PG-STAT-ACTIVITY-VIEW

Views: 336 · Posted: 2025-02-14

————        END        ————

Give me a Star, Thanks:)

https://github.com/fendoudebb/LiteNote

扫描下方二维码关注公众号和小程序↓↓↓

扫描下方二维码关注公众号和小程序↓↓↓


Today On History
Browsing Refresh