Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 234617
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T20:11:58+00:00 2026-05-11T20:11:58+00:00

I am working with a postgreSQL database that gets updated in batches. I need

  • 0

I am working with a postgreSQL database that gets updated in batches. I need to know when the last time that the database (or a table in the database)has been updated or modified, either will do.

I saw that someone on the postgeSQL forum had suggested that to use logging and query your logs for the time. This will not work for me as that I do not have control over the clients codebase.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T20:11:58+00:00Added an answer on May 11, 2026 at 8:11 pm

    You can write a trigger to run every time an insert/update is made on a particular table. The common usage is to set a “created” or “last_updated” column of the row to the current time, but you could also update the time in a central location if you don’t want to change the existing tables.

    So for example a typical way is the following one:

    CREATE FUNCTION stamp_updated() RETURNS TRIGGER LANGUAGE 'plpgsql' AS $$
    BEGIN
      NEW.last_updated := now();
      RETURN NEW;
    END
    $$;
    -- repeat for each table you need to track:
    ALTER TABLE sometable ADD COLUMN last_updated TIMESTAMP;
    CREATE TRIGGER sometable_stamp_updated
      BEFORE INSERT OR UPDATE ON sometable
      FOR EACH ROW EXECUTE PROCEDURE stamp_updated();
    

    Then to find the last update time, you need to select “MAX(last_updated)” from each table you are tracking and take the greatest of those, e.g.:

    SELECT MAX(max_last_updated) FROM (
      SELECT MAX(last_updated) AS max_last_updated FROM sometable
      UNION ALL
      SELECT MAX(last_updated) FROM someothertable
    ) updates
    

    For tables with a serial (or similarly-generated) primary key, you can try avoid the sequential scan to find the latest update time by using the primary key index, or you create indices on last_updated.

    -- get timestamp of row with highest id
    SELECT last_updated FROM sometable ORDER BY sometable_id DESC LIMIT 1
    

    Note that this can give slightly wrong results in the case of IDs not being quite sequential, but how much accuracy do you need? (Bear in mind that transactions mean that rows can become visible to you in a different order to them being created.)

    An alternative approach to avoid adding ‘updated’ columns to each table is to have a central table to store update timestamps in. For example:

    CREATE TABLE update_log(table_name text PRIMARY KEY, updated timestamp NOT NULL DEFAULT now());
    CREATE FUNCTION stamp_update_log() RETURNS TRIGGER LANGUAGE 'plpgsql' AS $$
    BEGIN
      INSERT INTO update_log(table_name) VALUES(TG_TABLE_NAME);
      RETURN NEW;
    END
    $$;
    -- Repeat for each table you need to track:
    CREATE TRIGGER sometable_stamp_update_log
     AFTER INSERT OR UPDATE ON sometable
     FOR EACH STATEMENT EXECUTE stamp_update_log();
    

    This will give you a table with a row for each table update: you can then just do:

    SELECT MAX(updated) FROM update_log
    

    To get the last update time. (You could split this out by table if you wanted). This table will of course just keep growing: either create an index on ‘updated’ (which should make getting the latest one pretty fast) or truncate it periodically if that fits with your use case, (e.g. take an exclusive lock on the table, get the latest update time, then truncate it if you need to periodically check if changes have been made).

    An alternative approach- which might be what the folks on the forum meant- is to set ‘log_statement = mod’ in the database configuration (either globally for the cluster, or on the database or user you need to track) and then all statements that modify the database will be written to the server log. You’ll then need to write something outside the database to scan the server log, filtering out tables you aren’t interested in, etc.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 108k
  • Answers 108k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The 'convert' in question is a 'lock convert' from RangeS-S… May 11, 2026 at 9:15 pm
  • Editorial Team
    Editorial Team added an answer Urls defined with ASP.NET MVC are configurable and not based… May 11, 2026 at 9:15 pm
  • Editorial Team
    Editorial Team added an answer There can certainly be fragmentation in MySQL data files or… May 11, 2026 at 9:15 pm

Related Questions

I am not sure if I am missing something grotesquely obvious or what, but
I have just started working on a project and I am using zend framework
I have been working on an application in Django. To begin with, for simplicity,
Very simple question (I hope), does there exist a tool similar to phpMyAdmin when
I am trying to implement a request to an unreliable server. The request is

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.