Skip to content

Instantly share code, notes, and snippets.

@ianthrive
Created May 13, 2014 15:57
Show Gist options
  • Save ianthrive/60965dca49d9f0208822 to your computer and use it in GitHub Desktop.
Save ianthrive/60965dca49d9f0208822 to your computer and use it in GitHub Desktop.
A PostgreSQL trigger function to updated a timestamp column whenever a row is updated.
CREATE OR REPLACE FUNCTION trigger_updated RETURNS TRIGGER LANGUAGE plpgsql AS $$
BEGIN
NEW.updated = NOW();
RETURN NEW;
END;
$$
CREATE TRIGGER updated BEFORE UPDATE ON payment
OR EACH ROW WHEN (OLD.* IS DISTINCT FROM NEW.*)
EXECUTE PROCEDURE trigger_updated();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment