vee*_*sur 5 postgresql postgresql-9.4
我想知道 postgresql 中表的最后修改日期。在 SQL Server 中可以得到使用
SELECT modify_date FROM sys.objects
Run Code Online (Sandbox Code Playgroud)
如何在 Postgres 中得到同样的东西?我正在使用 Postgres 9.4
Postgres 9.5 或更高版本有一种方法。打开track_commit_timestamp
并postgresql.conf
重新启动数据库集群。开始记录提交时间戳。
然后,您可以使用以下函数获取给定表的最新修改(最新提交)的时间戳pg_xact_commit_timestamp()
:
SELECT pg_xact_commit_timestamp(t.xmin) AS modified_ts
FROM my_table t
ORDER BY modified_ts DESC NULLS LAST
LIMIT 1;
Run Code Online (Sandbox Code Playgroud)
NULLS LAST
当仍然可能存在没有记录提交时间戳的行时,这是必要的。
有关的:
对于Postgres 9.4或更早版本,请参阅:
如果文件系统存储文件修改时间(mtime),我相信你可以使用
SELECT pg_relation_filepath('schema.table');
Run Code Online (Sandbox Code Playgroud)
找到表堆的路径。从那里您可以查找文件修改时间(mtime)。这有很多缺点,
害羞的是,您可能需要在要更新/插入的行或表上编写自己的 mtime 功能(通过创建具有自己的行的元表)。
CREATE TABLE foo (
id int,
mtime timestamp with time zone DEFAULT now()
);
INSERT INTO foo(id) VALUES (1);
Run Code Online (Sandbox Code Playgroud)
或者,对于元表
CREATE TABLE foo (
id serial,
fqn_table text,
mtime timestamp with time zone DEFAULT now()
);
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
12227 次 |
最近记录: |