NEW.* and OLD.* inside trigger function don't seem to contain recently added columns

From: Janne Annala <janne(dot)annala(at)forenom(dot)com>
To: "pgsql-bugs(at)lists(dot)postgresql(dot)org" <pgsql-bugs(at)lists(dot)postgresql(dot)org>
Subject: NEW.* and OLD.* inside trigger function don't seem to contain recently added columns
Date: 2024-03-22 16:59:37
Message-ID: PA6PR08MB10705C96AC875298000EF44B38A312@PA6PR08MB10705.eurprd08.prod.outlook.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

It seems that there are cases when comparing NEW.* to OLD.* using IS DISTINCT FROM incorrectly returns false. If a new column has been added to the table during the same database connection, the new column doesn't seem to be included in NEW.* and OLD.*. To workaround the issue you can apparently start a new connection, or recreate the function.

Platform: Macbook Pro M2
OS: macOS 14.3 (23D56)
Docker image: postgres:16.2 (using all default arguments and configuration)
Docker engine: 25.0.3

Here's the minimum case to reproduce the issue:

CREATE FUNCTION x()
RETURNS trigger
LANGUAGE plpgsql
AS $function$
BEGIN
IF ROW(NEW.*) IS DISTINCT FROM ROW(OLD.*) THEN
NEW.updated = current_timestamp;
END IF;

RETURN NEW;
END;
$function$;

CREATE TABLE test (
id SERIAL PRIMARY KEY,
old_column TEXT DEFAULT NULL,
updated TIMESTAMP NOT NULL DEFAULT current_timestamp
);

CREATE TRIGGER test_before_update_change_updated_timestamp
BEFORE UPDATE ON test
FOR EACH ROW EXECUTE FUNCTION x();

INSERT INTO test(old_column) VALUES ('a');
UPDATE test SET old_column = 'b';

-- Updated timestamp was refreshed

ALTER TABLE test ADD COLUMN new_column TEXT DEFAULT NULL;

UPDATE test SET new_column = 'b';

-- Updated timestamp was not refreshed

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Melanie Plageman 2024-03-22 18:41:25 Re: relfrozenxid may disagree with row XIDs after 1ccc1e05ae
Previous Message Bruce Momjian 2024-03-22 13:36:19 Re: Regression tests fail with musl libc because libpq.so can't be loaded