postgresql - 如何获取事务中的当前查询

标签 postgresql postgresql-9.3

我尝试从 pg_stats_activity 获取当前查询,但没有按预期工作。

在事务之外一切正常:

pagetest=# select query from pg_stat_activity where pid = pg_backend_pid() and 1 is not null;
                                       query                                        
------------------------------------------------------------------------------------
 select query from pg_stat_activity where pid = pg_backend_pid() and 1 is not null;
(1 row)

pagetest=# select query from pg_stat_activity where pid = pg_backend_pid() and 2 is not null;
                                       query                                        
------------------------------------------------------------------------------------
 select query from pg_stat_activity where pid = pg_backend_pid() and 2 is not null;
(1 row)

pagetest=# select query from pg_stat_activity where pid = pg_backend_pid() and 3 is not null;
                                       query                                        
------------------------------------------------------------------------------------
 select query from pg_stat_activity where pid = pg_backend_pid() and 3 is not null;
(1 row)

但是在交易中我得到了错误的结果:

pagetest=# begin;
BEGIN
pagetest=# select query from pg_stat_activity where pid = pg_backend_pid() and 1 is not null;
                                       query                                        
------------------------------------------------------------------------------------
 select query from pg_stat_activity where pid = pg_backend_pid() and 1 is not null;
(1 row)

pagetest=# select query from pg_stat_activity where pid = pg_backend_pid() and 2 is not null;
                                       query                                        
------------------------------------------------------------------------------------
 select query from pg_stat_activity where pid = pg_backend_pid() and 1 is not null;
(1 row)

pagetest=# select query from pg_stat_activity where pid = pg_backend_pid() and 3 is not null;
                                       query                                        
------------------------------------------------------------------------------------
 select query from pg_stat_activity where pid = pg_backend_pid() and 1 is not null;
(1 row)

pagetest=# rollback
pagetest-# ;
ROLLBACK

看起来统计收集器 View 上有一些奇怪的可见性规则。有什么合理的方法可以解决这个问题吗?

最佳答案

试试这个:

  BEGIN;
    select 1,current_query();
    select 2,current_query();
   ROLLBACK;

关于postgresql - 如何获取事务中的当前查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38221822/

相关文章:

python - Django 和 postgres - 在模型字段中将数据存储为 json 的缺点

macos - PG::ConnectionBad 升级到 Yosemite 和 postgresql 9.4.4

postgresql - 刷新物化 View : Concurrency, 事务行为

postgresql - 附加模块 citext 已安装,但未找到类型 "citext"?

python-2.7 - python通过psycopg2 unicode字符插入postgres

PostgreSQL 模式设计器

sql - Postgresql 递归返回意外结果

python - 有没有办法让 psycopg2 将点作为 python 元组返回?

PostgreSQL 回合(v 数字,s int)

ruby-on-rails - 不读取database.yml中的ENV变量(Rails 4.2.0、RubyMine 7、Postgres、Ruby 2.2.0、DotEnv)