Bug: Rule actions see wrong values for generated columns (NEW.gen reads OLD value)

From: SATYANARAYANA NARLAPURAM <satyanarlapuram(at)gmail(dot)com>
To: PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Bug: Rule actions see wrong values for generated columns (NEW.gen reads OLD value)
Date: 2026-04-13 01:59:28
Message-ID: CAHg+QDexGTmCZzx=73gXkY2ZADS6LRhpnU+-8Y_QmrdTS6yUhA@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi hackers,

NEW.<generated_coulmn> is resolved to the OLD row's value
for update or NULL for insert cases in a DO ALSO rule action for
generated columns. This bug affects both stored and virtual
generated columns. Reporting here to see if this is a known issue
with generated columns.

Repro:

CREATE TABLE t (id int PRIMARY KEY, a int,
gen int GENERATED ALWAYS AS (a * 2) VIRTUAL);
CREATE TABLE t_log (op text, old_gen int, new_gen int);

CREATE RULE t_log AS ON UPDATE TO t
DO ALSO INSERT INTO t_log VALUES ('UPD', OLD.gen, NEW.gen);

INSERT INTO t (id, a) VALUES (1, 5);
UPDATE t SET a = 100 WHERE id = 1;

postgres=# SELECT * FROM t;
id | a | gen
----+-----+-----
1 | 100 | 200
(1 row)

postgres=# SELECT * FROM t_log;
op | old_gen | new_gen
-----+---------+---------
UPD | 10 | 10
(1 row)

Thanks,
Satya

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Peter Smith 2026-04-13 02:14:59 Re: EXCEPT TABLE - Case inconsistency for describe \d and \dRp+
Previous Message jian he 2026-04-13 01:44:38 DELETE/UPDATE FOR PORTION OF with rule system is not working