PostgreSQL 从文件时间戳获悉一些信息(如数据库创建时间)

1 minute read

背景

PG的一些元数据没有时间字段,例如对象的创建时间、DDL的修改时间。

如果要获得这个时间信息,我们可以

1、通过事件触发器,将DDL事件记录到某个元数据表中,来获得。

2、或者打开DDL审计日志,从审计日志中获得。

3、与事件触发器类似,不过是通过PG的HOOK来实现的,在执行DDL时,通过代码中对应的HOOK来跟踪记录DDL的时间。

还有没有其他方法呢?

人民群众的智慧是无限的,我收集了一个方法,来自PGer(智宏):

通过查看文件的创建时间,获得一些信息

例如数据库的创建时间。

在创建数据库时,会在“表空间/数据库”或“pg_tblspc/表空间/版本/数据库”目录中新增一个PG_VERSION文件,新增后就不会变化。所以通过查看这个文件的创建时间,我们可以知道数据库的创建时间。

stat PG_VERSION

  File: ‘PG_VERSION’  
  Size: 3               Blocks: 8          IO Block: 4096   regular file  
Device: fd11h/64785d    Inode: 1315077     Links: 1  
Access: (0600/-rw-------)  Uid: ( 1000/  digoal)   Gid: ( 1000/  digoal)  
Access: 2018-04-07 17:07:02.431482742 +0800  
Modify: 2018-04-07 17:07:02.431482742 +0800  
Change: 2018-04-07 17:07:02.431482742 +0800  
 Birth: -  

目录例子:

/data01/pg/pg_root4000/pg_tblspc/28704/PG_11_201804061/28705  
  
/data01/pg/pg_root4000/base/13220  

使用SQL查询所有数据库的创建时间,取modification。

select   
datname,  
(pg_stat_file(format('%s/%s/PG_VERSION',  
case   
  when spcname='pg_default' then 'base'   
  else 'pg_tblspc/'||t2.oid||'/PG_11_201804061/'    
end,  
t1.oid))).*    
from   
pg_database t1,    
pg_tablespace t2   
where t1.dattablespace=t2.oid;   
  
  datname  | size |         access         |      modification      |         change         | creation | isdir   
-----------+------+------------------------+------------------------+------------------------+----------+-------  
 postgres  |    3 | 2018-04-07 17:07:05+08 | 2018-04-07 17:07:05+08 | 2018-04-07 17:07:05+08 |          | f  
 test      |    3 | 2018-04-13 16:30:08+08 | 2018-04-13 16:30:08+08 | 2018-04-13 16:30:08+08 |          | f  
 template1 |    3 | 2018-04-07 17:07:02+08 | 2018-04-07 17:07:02+08 | 2018-04-07 17:07:02+08 |          | f  
 template0 |    3 | 2018-04-07 17:07:05+08 | 2018-04-07 17:07:05+08 | 2018-04-07 17:07:05+08 |          | f  
(4 rows)  

背景知识

Linux offers three timestamps for files:

time of last access of contents (atime),
time of last modification of contents (mtime),
and time of last modification of the inode (metadata, ctime). 当文件大小发生变化时,会修改INODE导致ctime变化

The directory’s mtime corresponds to the last file creation or deletion that happened, though.

The stat command may output this - (dash).
I guess it depends on the filesystem you are using.
stat calls it the “Birth time”. On my ext4 fs it is empty, though.

%w Time of file birth, human-readable; - if unknown  
  
%W Time of file birth, seconds since Epoch; 0 if unknown  
  
stat foo.txt  
  File: `foo.txt'  
  Size: 239             Blocks: 8          IO Block: 4096   regular file  
Device: 900h/2304d      Inode: 121037111   Links: 1  
Access: (0644/-rw-r--r--)  Uid: ( 1000/  adrian)   Gid: (  100/   users)  
Access: 2011-10-26 13:57:15.000000000 -0600  
Modify: 2011-10-26 13:57:15.000000000 -0600  
Change: 2011-10-26 13:57:15.000000000 -0600  
 Birth: -  

但是,它对表的创建时间是无法准确获得的,原因如下:

1、access time,mount atime=off 的话,不记录atime

2、注意表在rewrite(例如truncate, 导致rewrite的alter)后,会产生新的filenode,所以即使access time不变,也不完全准确。

3、touch文件,会改变access time

参考

https://unix.stackexchange.com/questions/24441/get-file-created-creation-time

《PostgreSQL 事件触发器应用 - DDL审计记录 + 异步通知(notify)》

《use event trigger function record user who alter table’s SQL》

《PostgreSQL 事件触发器 - DDL审计 , DDL逻辑复制 , 打造DDL统一管理入》

《PostgreSQL 事件触发器 - PostgreSQL 9.3 Event Trigger》

Flag Counter

digoal’s 大量PostgreSQL文章入口