zoukankan      html  css  js  c++  java
  • postgresql获取表最后更新时间(通过表磁盘存储文件时间)

    一、创建获取表更新时间的函数

    --获取表记录更新时间(通过表磁盘存储文件时间)
    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$;

    二、调用函数获取时间

    备注:本方法相对于通过触发器实现方法有一定的时间延迟,通常在2到5分钟左右的时间,对实时性要求高的地方,不推荐使用该方法

  • 相关阅读:
    时间序列的小波分析
    粒子群算法优化BP生物能神经网络
    day:3.9基础复习
    计算机网络通信基础
    面向对象的补充
    python中的类和对象
    R语言基础
    函数(2)
    python开发第四篇:函数(1)
    Python开发【第三篇】:Python基本数据类型
  • 原文地址:https://www.cnblogs.com/mxly/p/9268922.html
Copyright © 2011-2022 走看看