您的位置:首页 > 其它

pg确定一张表最后被使用的时间

2018-01-02 09:47 183 查看
create or replace function table_file_access_info(
IN schemaname text,IN tablename text,OUT last_access timestamp with time zone,
OUT last_change timestamp with time zone
) LANGUAGE plpgsql AS $func$
DECLARE
tabledir text;
filenode text;
BEGIN
SELECT regexp_replace(current_setting('data_directory') || '/' || pg_relation_filepath(c.oid), pg_relation_filenode(c.oid) || '$',''),pg_relation_filenode(c.oid)
INTO tabledir,filenode
from pg_class c
join pg_namespace ns
on c.relnamespace=ns.oid
and c.relname=tablename
and ns.nspname=schemaname;
raise notice 'tabledir:% - filenode : %',tabledir,filenode;
select max((pg_stat_file(tabledir || filename)).access),
max((pg_stat_file(tabledir || filename)).modification)
INTO last_access, last_change
from pg_ls_dir(tabledir) as filename
where filename ~ ('^' || filenode || '([.]?[0-9]+)?$');
END;
$func$;

调用该函数:
select  table_file_access_info('public','test');   --大概时间

输出示例:

postgres=# select  table_file_access_info('public','test');
NOTICE:  tabledir:/home/postgres/datalm/base/13241/ - filenode : 16392
table_file_access_info
-----------------------------------------------------
("2017-03-23 17:26:02+08","2017-03-23 17:35:16+08")
(1 row)

postgres=#

by  丹心明月
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐