Postgresql

上一期間訪問的表

  • July 11, 2016

我想檢查在給定期間哪些表已更新,例如按每個表的訪問時間降序排列。

我怎樣才能為 PostgreSQL 獲得它?

您可以使用 獲取有關表的最後更改的一些資訊xmin,例如:

select max(xmin::text::bigint) from t;

但是您需要注意許多警告,包括模數和環繞以及凍結的 xids

試驗台:

set role dba;
create role stack;
grant stack to dba;
create schema authorization stack;
set role stack;
--
create or replace function f(p_schema in text, p_table in text) 
                 returns integer language plpgsql immutable as $$
declare
 n integer;
begin
 execute 'select max(xmin::text::bigint) from '||p_schema||'.'||p_table into n;
 return n;
end;$$;
--
create table foo as select generate_series(1, 100) as id;
create table bar as select generate_series(1, 100) as id;
create table baz as select generate_series(1, 100) as id;
--

方法:

select table_name, f(table_schema, table_name)
from information_schema.tables
where table_schema='stack'
order by 2 desc;
/*
table_name |   f
------------+--------
baz        | 784657
bar        | 784656
foo        | 784655
*/
--
update foo set id=id+1 where id=100;
--
select table_name, f(table_schema, table_name)
from information_schema.tables
where table_schema='stack'
order by 2 desc;
/*
table_name |   f
------------+--------
foo        | 784658
baz        | 784657
bar        | 784656
*/

清理:

drop schema stack cascade;
set role dba;
drop role stack;

引用自:https://dba.stackexchange.com/questions/12374