How can I check all the current SQL queries being executed by the PostgreSQL?

本文介绍如何使用PostgreSQL内置的pg_stat_activity视图来查看当前正在执行的所有SQL查询,包括查询用户、进程ID、客户端地址及等待状态。此外,还提供了按状态分组查询的方法,以及如何设置日志记录所有超过2秒的查询。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

https://access.redhat.com/solutions/1248183

 SOLUTION IN PROGRESS - 已更新 2014年十一月5日00:35 - 

English 

环境

  • Red Hat Enterprise Linux 5
  • Red Hat Enterprise Linux 6
  • PostgreSQL 8.4 or higher

问题

  • How can I check all the current SQL queries being executed by the PostgreSQL?

决议

  • To show all current queries:

Raw

  SELECT datname,procpid,current_query FROM pg_stat_activity;

  SELECT datname,usename,procpid,client_addr,waiting,query_start,current_query FROM pg_stat_activity ;

  • To group the queries by status:

Raw

SELECT count(*) as counter, usename, current_query FROM pg_stat_activity GROUP BY usename,current_query ORDER BY counter DESC;
  • To save to a log all the queries that takes more the 2 seconds, you can use:

Raw

 # vim /var/lib/pgsql/data/postgresql.conf
    log_min_duration_statement = 2000
 # service postgresql restart
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值