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

From: Richard Guo <guofenglinux(at)gmail(dot)com>
To: Dean Rasheed <dean(dot)a(dot)rasheed(at)gmail(dot)com>
Cc: Chao Li <li(dot)evan(dot)chao(at)gmail(dot)com>, SATYANARAYANA NARLAPURAM <satyanarlapuram(at)gmail(dot)com>, PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>, Peter Eisentraut <peter(at)eisentraut(dot)org>
Subject: Re: Bug: Rule actions see wrong values for generated columns (NEW.gen reads OLD value)
Date: 2026-04-20 07:29:04
Message-ID: CAMbWs49GX=-p_dD=2zQvHB4yfjwCttWSXg8Pz8xcZJJYAjcp1Q@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Mon, Apr 20, 2026 at 2:25 PM Richard Guo <guofenglinux(at)gmail(dot)com> wrote:
> Yeah, this is a better approach. The change looks good to me.
>
> A nitpick: For the comment "The generated column expressions typically
> refer to new.attribute ...", maybe we can remove "typically", as
> generation expressions always refer to columns of the same relation.

I noticed a couple of issues after a further look.

1. The ReplaceVarsFromTargetList call on "gen_cols" fails to handle
hasSubLinks. This can cause error if targetList contains SubLinks:

update t set a = (select max(a) from t);
ERROR: replace_rte_variables inserted a SubLink, but has noplace to record it

2. The same bug fixed in this patch also exists in rule quals:

create table t (a int, b int generated always as (a *2));
insert into t values (1);

create rule rule_qual as on update to t where new.b > 100
do instead nothing;

update t set a = 100;

select * from t;
a | b
-----+-----
100 | 200
(1 row)

I think we should apply the same fix to CopyAndAddInvertedQual.

Attached v3 fixes them.

- Richard

Attachment Content-Type Size
v3-0001-Fix-incorrect-NEW-references-to-generated-columns.patch application/octet-stream 15.6 KB

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Antonin Houska 2026-04-20 07:44:45 Re: Adding REPACK [concurrently]
Previous Message lakshmi 2026-04-20 07:18:48 Re: ECPG: inconsistent behavior with the document in “GET/SET DESCRIPTOR.”