Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2794429
  • 博文数量: 423
  • 博客积分: 7770
  • 博客等级: 少将
  • 技术积分: 4766
  • 用 户 组: 普通用户
  • 注册时间: 2006-11-09 11:58
个人简介

Oracle/DB2/Postgresql/Mysql/Hadoop/Greenplum/Postgres-xl/Mongodb

文章分类

全部博文(423)

文章存档

2019年(3)

2018年(6)

2017年(27)

2016年(23)

2015年(30)

2014年(16)

2013年(31)

2012年(73)

2011年(45)

2010年(14)

2009年(30)

2008年(30)

2007年(63)

2006年(32)

分类:

2009-11-12 18:27:30

Viewing current PostgreSQL queries

by last modified 2007-12-11 13:57

An important tool for debugging PostgreSQL performance issues is the ability to view all the currently executing queries. Thankfully this is easy to do. Read on for how.

Configuration

Storing of query strings is usually disabled in PostgreSQL by default.  To enable it, use this line in your postgresql.conf

stats_command_string = true

This setting can be changed on a running database without restarting or effecting open connections by telling the PostgreSQL parent process, postmaster, to reload its config.  Send it a SIGHUP or use the safer pg_ctl command with the reload option.  Example:

pg_ctl reload

Queries

When stats_command_string is enabled the pg_stat_activity table holds all currently active query strings.  The simplest query will show all current query strings along with which database they refer to and the process ID (PID) of the process serving that query.

SELECT datname,procpid,current_query FROM pg_stat_activity

Example:

database1=# SELECT datname,procpid,current_query FROM pg_stat_activity ORDER BY procpid ;
    datname    | procpid | current_query
---------------+---------+---------------
 mydatabaseabc |    2587 |
 anotherdb     |   15726 | SELECT * FROM users WHERE id=123 ; 
 mydatabaseabc |   15851 |
(3 rows)

Each row of pg_stat_activity represents one PostgreSQL process (PostgreSQL uses one server process per connection).

Any processes that are not currently performing any queries will show as the current_query.

Note that queries from all databases within the server will be shown.  If the user querying pg_stat_activity does not have privileges to access a database then it will not show the current_query.

The query_start column can also be used to show when the query started executing.

Another of my favourite queries is to show a top-like view of current queries, grouped by how many of the same query are running at that instant and the usernames belonging to each connection.

SELECT count(*) as cnt, usename, current_query FROM pg_stat_activity GROUP BY usename,current_query ORDER BY cnt DESC;

Example:

database1=# SELECT count(*) as cnt, usename, current_query FROM pg_stat_activity GROUP BY usename,current_query ORDER BY cnt DESC;
cnt |    usename    | current_query
-----+---------------+---------------
   7 | freddykrueger |
   3 | freddykrueger | SELECT name FROM users WHERE id=50;
   1 | postgres      |
(3 rows)

Update

With newer versions of PostgreSQL we get some more details. Here's an example I've used on PostgreSQL 8.2:

SELECT datname,usename,procpid,client_addr,waiting,query_start,current_query FROM pg_stat_activity ;
阅读(2551) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~