HiveBrain v1.2.0
Get Started
← Back to all entries
patternsqlMinor

Get last modified date of table in postgresql

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
postgresqllastdatemodifiedgettable

Problem

I want know the last modified date of table in postgresql. In SQL Server can get using

SELECT modify_date FROM sys.objects


How to get same thing in Postgres? I am using Postgres 9.4

Solution

There is a way with Postgres 9.5 or later. Turn on track_commit_timestamp in postgresql.conf and restart the DB cluster. Starts logging commit timestamps.

Then you can get the timestamp of the latest modification (latest commit) for a given table using the function 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;


NULLS LAST is necessary while there still may be rows without recorded commit timestamp.

Related:

  • PostgreSQL sort by datetime asc, null first?



  • Row with latest timestamp



  • Is there way to get transaction commit timestamp in Postgres?



For Postgres 9.4 or older, see:

  • How to find out when data was inserted to Postgres?

Code Snippets

SELECT pg_xact_commit_timestamp(t.xmin) AS modified_ts
FROM   my_table t
ORDER  BY modified_ts DESC NULLS LAST
LIMIT  1;

Context

StackExchange Database Administrators Q#199290, answer score: 8

Revisions (0)

No revisions yet.