-- Default case
\timing on

DROP TABLE IF EXISTS test;

CREATE TABLE test (
    id INT,
    create_time TIMESTAMPTZ DEFAULT now(),
    update_time TIMESTAMPTZ DEFAULT now());

INSERT INTO test SELECT generate_series(1, 1000000);

UPDATE test SET id=0;


-- Implement using triggers
\timing on

DROP TABLE IF EXISTS test;

CREATE TABLE test (
    id INT,
    create_time TIMESTAMPTZ DEFAULT now(),
    update_time TIMESTAMPTZ DEFAULT now());

INSERT INTO test SELECT generate_series(1, 1000000);

CREATE OR REPLACE FUNCTION test_trigger_function()
RETURNS TRIGGER AS '
BEGIN
  NEW.update_time := now();
  RETURN NEW;
END' LANGUAGE 'plpgsql';

CREATE TRIGGER test_trigger
BEFORE UPDATE ON test
FOR EACH ROW
EXECUTE PROCEDURE test_trigger_function();

UPDATE test SET id=0;


-- Implement using on_update_set_default
\timing on

DROP TABLE IF EXISTS test;

CREATE TABLE test (
    id INT,
    create_time TIMESTAMPTZ DEFAULT now(),
    update_time TIMESTAMPTZ DEFAULT now());

INSERT INTO test SELECT generate_series(1, 1000000);

ALTER TABLE test ALTER COLUMN update_time SET(on_update_set_default=true);

SELECT attoptions FROM pg_attribute WHERE attname='update_time';

UPDATE test SET id=0;
