我尝试从 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/