Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postgresql Current timestamp on Update

What is the postgres equivalent of the below mysql code

CREATE TABLE t1 (
  created TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  modified TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);
CREATE TABLE t2 (
  created TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  modified TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);

As per Alex Brasetvik answer below, it seems i should go with triggers, my problem is i have a number of tables t1, t2... with created and modified fields, is it possible to write a generalized procedure?

--update Almost ready

CREATE FUNCTION update_timestamp() RETURNS trigger AS $update_timestamp$
    BEGIN
        NEW.modified := current_timestamp;
        RETURN NEW;
    END;
$update_timestamp$ LANGUAGE plpgsql;

CREATE TRIGGER update_timestamp BEFORE INSERT OR UPDATE ON t1
    FOR EACH ROW EXECUTE PROCEDURE update_timestamp();
CREATE TRIGGER update_timestamp BEFORE INSERT OR UPDATE ON t2
    FOR EACH ROW EXECUTE PROCEDURE update_timestamp();
like image 742
Mithun Sreedharan Avatar asked Mar 02 '10 11:03

Mithun Sreedharan


People also ask

How do I get the current time in PostgreSQL?

Postgresql now() The NOW() function in Postgresql is used to get the current date and time. The return type of the NOW() function is the timestamp with the time zone. We can fetch the current date and time by using the PostgreSQL NOW() function. This function has a return type i.e. the timestamp with the time zone.

What is now () in PostgreSQL?

What is PostgreSQL Now Function? The Now() function is used to return the current date and time of the time zone (default or user-defined). Its return type is the timestamp with the time zone.

Which one of these will the current date and time 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. In this format, 'YYYY' is a 4-digit year, 'MM' is a 2-digit month, and 'DD' is a 2-digit day. The returned value is a date data type.

How do I set the current date and time in PostgreSQL?

SELECT CURRENT_TIME, CURRENT_TIMESTAMP; Let's have a look at using the CURRENT_DATE keyword within the SELECT query to check the current date in PostgreSQL. So, we have tried the below query to get the time and date for the current region, e.g. Asia. The output shows the date and time with a time zone of Asia.


4 Answers

Just make sure all tables have the same columnname:

CREATE OR REPLACE FUNCTION upd_timestamp() RETURNS TRIGGER 
LANGUAGE plpgsql
AS
$$
BEGIN
    NEW.modified = CURRENT_TIMESTAMP;
    RETURN NEW;
END;
$$;

CREATE TRIGGER t_name
  BEFORE UPDATE
  ON tablename
  FOR EACH ROW
  EXECUTE PROCEDURE upd_timestamp();
like image 184
Frank Heikens Avatar answered Oct 19 '22 14:10

Frank Heikens


Thank you for the information Mithun and Alex Brasetvik.

I'd like to add one minor tweak to the trigger. Since we mostly likely want the modified column to store the timestamp when the row was last changed, not when it was the target of an UPDATE statement, we have to compare the new and the old value of the row. We update the modified column only if these two values differ.

CREATE OR REPLACE FUNCTION update_modified_timestamp() RETURNS TRIGGER 
LANGUAGE plpgsql
AS
$$
BEGIN
    IF (NEW != OLD) THEN
        NEW.modified = CURRENT_TIMESTAMP;
        RETURN NEW;
    END IF;
    RETURN OLD;
END;
$$;

This trigger ensures that the modified column is updated only if the UPDATE operation actually changes the values stored in the row.

like image 39
Lauri Silvennoinen Avatar answered Oct 19 '22 14:10

Lauri Silvennoinen


Update it with a trigger. Documentation and examples.

like image 4
Alex Brasetvik Avatar answered Oct 19 '22 13:10

Alex Brasetvik


To further improve, the answer given by @Lauri Silvennoinen:

This trigger uses the WHEN clause as recommended by the official docs to check for changes in the row even before calling the specified function.

CREATE OR REPLACE FUNCTION update_modified_timestamp() RETURNS TRIGGER 
LANGUAGE plpgsql
AS
$$
BEGIN
    NEW.modified = CURRENT_TIMESTAMP;
    RETURN NEW;
END;
$$;

CREATE OR REPLACE TRIGGER tg_update_modified
    AFTER UPDATE ON table_name
    FOR EACH ROW
    WHEN (OLD.* IS DISTINCT FROM NEW.*)
    EXECUTE FUNCTION update_modified_timestamp();
like image 1
Bungeefan Avatar answered Oct 19 '22 12:10

Bungeefan