How to capture last modified date of all the tables in PGSQL

151 Views Asked by At

How can we capture modified dates for all the tables in PGSQL (apart from writing data triggers on each table for inserts /deletes).Can we do it at a generic level for all the tables using event triggers. for eg: the below query captures modified dates for every function in a generic .

 `select now(), nspname, proname, command_tag, prosrc
    from pg_event_trigger_ddl_commands() e
    join pg_proc p on p.oid = e.objid
    join pg_namespace n on n.oid = pronamespace;`

Likewise ,is there any to capture last modified date logs for all tables ?

Have Tried ,

CREATE FUNCTION test_event_trigger_table_rewrite_oid()
 RETURNS event_trigger
 LANGUAGE plpgsql AS
$$
BEGIN
  RAISE NOTICE 'rewriting table % for reason %',
                pg_event_trigger_table_rewrite_oid()::regclass,
                pg_event_trigger_table_rewrite_reason();
END;
$$;

CREATE EVENT TRIGGER test_table_rewrite_oid
                  ON table_rewrite
   EXECUTE FUNCTION test_event_trigger_table_rewrite_oid();

but the above code only captures time when table DDL is changed.Want it to happen at inserts /deletes

1

There are 1 best solutions below

0
On

To capture the time when DML statements are run, use a trigger AFTER INSERT OR UPDATE OR DELETE defined FOR EACH STATEMENT.