您的位置:首页 > 其它

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

2017-03-28 09:34 102 查看
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,filenodefrom pg_class cjoin pg_namespace nson c.relnamespace=ns.oidand c.relname=tablenameand 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_changefrom pg_ls_dir(tabledir) as filenamewhere 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=#
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐