2016-07-06 45 views
2

我試圖從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 

它看起來像有在統計收集意見有些奇怪的可見性規則。有沒有合理的方法呢?

回答

2

試試這個:

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