Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Latest table modified time in PostgreSQL

Tags:

sql

postgresql

I need to get the latest modified time of a table, so came across

select relfilenode from pg_class where relname = 'test';

which gives me the relfilenode id, this seems to be a directory name in

L:\Databases\PostgresSQL\data\base\inodenumber

For which I later extract the latest modified time.

Is this the right way to do this or are there any better methods to do the same

like image 926
Akash Avatar asked Oct 05 '12 16:10

Akash


People also ask

How do you find when was the table last modified in Postgres?

You can do it via checking last modification time of table's file. In postgresql,every table correspond one or more os files,like this: select relfilenode from pg_class where relname = 'test'; the relfilenode is the file name of table "test".

How do I get the latest date in PostgreSQL?

The PostgreSQL CURRENT_DATE function returns the current date (the system date on the machine running PostgreSQL) as a value in the 'YYYY-MM-DD' format.

What is XMIN in PostgreSQL?

xmin. The identity (transaction ID) of the inserting transaction for this row version. (A row version is an individual state of a row; each update of a row creates a new row version for the same logical row.) cmin. The command identifier (starting at zero) within the inserting transaction.

What is a PostgreSQL function that returns the current time?

The PostgreSQL CURRENT_TIME function returns the current time and the current time zone.


1 Answers

Testing the mtime of the table's relfilenode won't work well. As Eelke noted VACUUM among other operations will modify the timestamp. Hint bit setting will modify the table too, causing it to appear to be "modified" by a SELECT. Additionally, sometimes a table has more than one fork to its on-disk relation (1GB chunks), and you'd have to check all of them to find the most recent.

If you want to keep a last modified time for a table, add an AFTER INSERT OR UPDATE OR DELETE OR TRUNCATE ... FOR EACH STATEMENT trigger that updates a timestamp row in a table you use for tracking modification times.

The downside of the trigger is that it'll contest a single row lock on the table, so it'll serialize all your transactions. It'll also greatly increase the chance of getting deadlocks. What you really want is probably something nontransactional that doesn't have to roll back when the transaction does, where if multiple transactions update a counter the highest value wins. There's nothing like that built in, though it might not be too hard as a C extension.

A slightly more sophisticated option would be to create a trigger that uses dblink to do an update of the last-updated counter. That'll avoid most of the contention problems but it'll actually make deadlocking worse because PostgreSQL's deadlock detection won't be able to "see" the fact that the two sessions are deadlocked via an intermediary. You'd need a way to SELECT ... FOR UPDATE with a timeout to make it reliable without aborting transactions too often.

In any case, a trigger won't catch DDL, though. DDL triggers ("Event triggers") are coming in Pg 9.3.

See also:

  • How do I find the last time that a PostgreSQL database has been updated?
  • How to get 'last modified time' of the table in postgres?
like image 176
Craig Ringer Avatar answered Nov 15 '22 05:11

Craig Ringer