PostgreSQL系统表 pg_stat_activity
来源:互联网 发布:七上生物行知天下答案 编辑:程序博客网 时间:2024/05/18 03:51
postgres=# \d+ pg_stat_activity View "pg_catalog.pg_stat_activity" Column | Type | Modifiers | Storage | Description ------------------+--------------------------+-----------+----------+------------- datid | oid | | plain | datname | name | | plain | pid | integer | | plain | usesysid | oid | | plain | usename | name | | plain | application_name | text | | extended | client_addr | inet | | main | client_hostname | text | | extended | client_port | integer | | plain | backend_start | timestamp with time zone | | plain | xact_start | timestamp with time zone | | plain | query_start | timestamp with time zone | | plain | state_change | timestamp with time zone | | plain | waiting | boolean | | plain | state | text | | extended | query | text | | extended | View definition: SELECT s.datid, d.datname, s.pid, s.usesysid, u.rolname AS usename, s.application_name, s.client_addr, s.client_hostname, s.client_port, s.backend_start, s.xact_start, s.query_start, s.state_change, s.waiting, s.state, s.query FROM pg_database d, pg_stat_get_activity(NULL::integer) s(datid, pid, usesysid, application_name, state, query, waiting, xact_start, query_start, backend_start, state_change, client_addr, client_hostname, client_port), pg_authid u WHERE s.datid = d.oid AND s.usesysid = u.oid;
可以看出,pg_stat_activity是一个系统视图。
1. 官方解释:
One row per server process, showing information related to the current activity of that process, such as state and current query.
每一行都表示一个系统进程,显示与当前会话的活动进程的一些信息,比如当前回话的状态和查询等。
2. 详细信息。
Column Type Description
active: The backend isexecuting(实行) a query.
idle: The backend is waiting for a new client command.
idle in transaction: The backend is in atransaction(交易), but is not currentlyexecuting(实行) a query.
idle in transaction (aborted): This state is similar toidle in transaction, except one of thestatements(声明) in the transaction caused an error.
fastpath function call: The backend is executing a fast-path function.
disabled: This state is reported iftrack_activities is disabled in this backend.
3. 举例说明(讲解waiting,state,query的意义)
> active:表示当前用户正在执行查询等操作。
postgres=# select * from pg_stat_activity ; datid | datname | pid | usesysid | usename | application_name | client_addr | client_hostname | client_port | backend_start | xact_start | query_start | state_change | waiting | state | query -------+----------+------+----------+----------+------------------+-------------+-----------------+-------------+-------------------------------+-------------------------------+-------------------------------+-------------------------------+---------+--------+---------------------------------- 12860 | postgres | 2534 | 10 | postgres | psql | | | -1 | 2015-03-24 08:15:42.099642+08 | 2015-03-24 09:15:41.756177+08 | 2015-03-24 09:15:41.756177+08 | 2015-03-24 09:15:41.756181+08 | f | active | select * from pg_stat_activity ;(1 row)当前用户waiting=f,stat=active,query=select * from pg_stat_activity.
> idle:表示当前用户空闲。
用另一个客户端再登入一个用户先。
postgres=# select * from pg_stat_activity ; datid | datname | pid | usesysid | usename | application_name | client_addr | client_hostname | client_port | backend_start | xact_start | query_start | state_change | waiting | state | query -------+----------+------+----------+----------+------------------+-------------+-----------------+-------------+-------------------------------+-------------------------------+-------------------------------+-------------------------------+---------+--------+---------------------------------- 12860 | postgres | 2534 | 10 | postgres | psql | | | -1 | 2015-03-24 08:15:42.099642+08 | 2015-03-24 09:19:55.516083+08 | 2015-03-24 09:19:55.516083+08 | 2015-03-24 09:19:55.516087+08 | f | active | select * from pg_stat_activity ; 12860 | postgres | 5084 | 10 | postgres | psql | | | -1 | 2015-03-24 09:19:39.404898+08 | | | 2015-03-24 09:19:39.407193+08 | f | idle | (2 rows)可以看到,第二个postgres 的stat=idle,表示的是空闲状态,等待命令的输入。
> idle in transaction:表示当前用户在事务中。
postgres=# begin ;BEGINpostgres=# select * from book; id | name | tag ----+------+---------- 1 | java | aa,bb,cc 2 | C++ | dd,ee(2 rows)另一个客户端启动,查询:
postgres=# select * from pg_stat_activity ; datid | datname | pid | usesysid | usename | application_name | client_addr | client_hostname | client_port | backend_start | xact_start | query_start | state_change | waiting | state | query -------+----------+------+----------+----------+------------------+-------------+-----------------+-------------+-------------------------------+-------------------------------+-------------------------------+-------------------------------+---------+---------------------+---------------------------------- 12860 | postgres | 5500 | 10 | postgres | psql | | | -1 | 2015-03-24 10:22:21.344358+08 | 2015-03-24 10:23:44.615123+08 | 2015-03-24 10:23:49.750407+08 | 2015-03-24 10:23:49.76975+08 | f | idle in transaction | select * from book; 12860 | postgres | 5510 | 10 | postgres | psql | | | -1 | 2015-03-24 10:23:53.79722+08 | 2015-03-24 10:24:02.64689+08 | 2015-03-24 10:24:02.64689+08 | 2015-03-24 10:24:02.646894+08 | f | active | select * from pg_stat_activity ;(2 rows)可以看到,一个postgres用户的stat=idle in transcation,表示在事务中。
> idle in transaction (aborted): 表示当前用户在事务中,但是已经发生错误。
postgres=# begin ;BEGINpostgres=# select * from book; id | name | tag ----+------+---------- 1 | java | aa,bb,cc 2 | C++ | dd,ee(2 rows)postgres=# s;ERROR: syntax error at or near "s"LINE 1: s; ^另一个客户端启动,查询:
postgres=# select * from pg_stat_activity ; datid | datname | pid | usesysid | usename | application_name | client_addr | client_hostname | client_port | backend_start | xact_start | query_start | state_change | waiting | state | query -------+----------+------+----------+----------+------------------+-------------+-----------------+-------------+-------------------------------+-------------------------------+-------------------------------+-------------------------------+---------+-------------------------------+---------------------------------- 12860 | postgres | 5500 | 10 | postgres | psql | | | -1 | 2015-03-24 10:22:21.344358+08 | | 2015-03-24 10:28:55.763925+08 | 2015-03-24 10:28:55.764049+08 | f | idle in transaction (aborted) | s; 12860 | postgres | 5510 | 10 | postgres | psql | | | -1 | 2015-03-24 10:23:53.79722+08 | 2015-03-24 10:28:58.587982+08 | 2015-03-24 10:28:58.587982+08 | 2015-03-24 10:28:58.588028+08 | f | active | select * from pg_stat_activity ;(2 rows)可以看到,一个postgres用户的stat=idle in transcation(aborted),表示在事务中发生错误了。
一个进程等待的例子:
一个客户端在事务中进行增加字段:
postgres=# begin ;BEGINpostgres=# alter table book add column addr character varying;ALTER TABLEpostgres=#注意还没有提交;
另一个客户端来查询这个表的数据:
postgres=# select * from book;发现查询语句等在那里,是因为在进行增加字段操作的时候,会有表锁,锁没有释放之前其他进程无法访问该表。
查看进程状态:
postgres=# select * from pg_stat_activity ; datid | datname | pid | usesysid | usename | application_name | client_addr | client_hostname | client_port | backend_start | xact_start | query_start | state_change | waiting | state | query -------+----------+------+----------+----------+------------------+-------------+-----------------+-------------+-------------------------------+-------------------------------+-------------------------------+-------------------------------+---------+---------------------+------------------------------------------------------ 12860 | postgres | 5500 | 10 | postgres | psql | | | -1 | 2015-03-24 10:22:21.344358+08 | 2015-03-24 10:55:29.238829+08 | 2015-03-24 10:55:34.670946+08 | 2015-03-24 10:55:34.816089+08 | f | idle in transaction | alter table book add column addr character varying; 12860 | postgres | 5510 | 10 | postgres | psql | | | -1 | 2015-03-24 10:23:53.79722+08 | 2015-03-24 10:55:46.919415+08 | 2015-03-24 10:55:46.919415+08 | 2015-03-24 10:55:46.919419+08 | t | active | select * from book; 12860 | postgres | 5764 | 10 | postgres | psql | | | -1 | 2015-03-24 11:02:37.09896+08 | 2015-03-24 11:02:52.233883+08 | 2015-03-24 11:02:52.233883+08 | 2015-03-24 11:02:52.233886+08 | f | active | select * from pg_stat_activity ;(3 rows)可以看到第二个进程的waiting=t。
- PostgreSQL系统表 pg_stat_activity
- PostgreSQL系统表 pg_stat_activity
- POSTGRESQL 数据库 系统表
- postgresql的系统表
- PostgreSQL系统表 pg_stats
- PostgreSQL 系统表pg_enum
- postgresql系统表
- PostgreSQL系统表 pg_stats
- PostgreSQL 系统表pg_enum
- Postgresql 相关系统表查询
- PostgreSQL 的系统表对象
- PostgreSQL学习手册(系统表)
- PostgreSQL学习手册(系统表)
- PostgreSQL系统表和视图
- postgresql常用的系统表
- Postgresql数据库的系统表初探
- postgresql相关系统表查询(转载)
- postgresql 查看系统表的信息
- Sicily 4874. POGODAK
- LeetCode_4Sum
- 八进制数的表达方法!八进制数在转义符中的使用!
- Sicily 4839. 铺地毯
- eclipse不显示代码提示的解决方案
- PostgreSQL系统表 pg_stat_activity
- Sicily 4868. 数字反转
- Sicily 2386. Jollo
- 产品开关量输出 PNP NPN
- spring mvc中redirect/forward:url与拦截器的关系
- boa cgi html CGIC
- 数据结构复习-图、查找、排序
- VC++6.0 MFC窗口中移动的圆
- android LinearLayout 适配