ITPub博客

首页 > 数据库 > PostgreSQL > PG: Estimate last modified or read time of a table

PG: Estimate last modified or read time of a table

原创 PostgreSQL 作者:zchbaby2000 时间:2023-02-13 09:35:15 0 删除 编辑
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;
    -- find latest access and modification times over all segments
    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
      -- only use files matching <basefilename>[.segmentnumber]
WHERE filename ~ ('^' || filenode || '([.]?[0-9]+)?$');
END;
$func$;
select * from table_file_access_info('public','a');


来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/725820/viewspace-2934915/ ,如需转载,请注明出处,否则将追究法律责任。

下一篇: PG: Utility queries
请登录后发表评论 登录
全部评论
喜欢各种数据库相关的开发,架构设计,运维和性能优化
  • 博文量
    260
  • 访问量
    122446