Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1wCBiC-001kHq-2x for pgsql-hackers@arkaria.postgresql.org; Mon, 13 Apr 2026 07:28:01 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1wCBiB-005Inc-0n for pgsql-hackers@arkaria.postgresql.org; Mon, 13 Apr 2026 07:28:00 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1wCBiA-005InT-2z for pgsql-hackers@lists.postgresql.org; Mon, 13 Apr 2026 07:27:59 +0000 Received: from mail-vs1-xe29.google.com ([2607:f8b0:4864:20::e29]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1wCBi9-00000000nRb-3NtU for pgsql-hackers@lists.postgresql.org; Mon, 13 Apr 2026 07:27:59 +0000 Received: by mail-vs1-xe29.google.com with SMTP id ada2fe7eead31-607f1fac33cso1326375137.3 for ; Mon, 13 Apr 2026 00:27:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1776065276; cv=none; d=google.com; s=arc-20240605; b=PoNtcQtSPrqwen0GQvzCoQ4e7vAchTrOmbe2Bs7ToDMyu2UdpPdQQrCWlSdpm6B/4S W8myx2jhohVDpQEOV9dB+EWepegzFg9GiFimXs6E+uV2uGFx8dE8RU2vQm28yD9aZT3Q 4skTUZGdQKx6EeTGjOXq+26IMedQwVdIsXTX62jaBan1R9R7bXgSXLfLllZ/4hTl1VfD /k0zMleCt5X8OI1AcVM0TnXqjf2KCVkDZQC0tLvOgZrjRW00NMK+7hc5eD2zuT8jFBFS p3gJifVHbD2tH4SUxz1h3f+7sOU1ahB57KoZ5EM0KdjmPzYEH+On7Pep3wc8Ixh13KtM 6x0Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:dkim-signature; bh=hFgT2EV6L+ar+tVE6p1URMEzzHyVlP1XVhBUplXNJiQ=; fh=v8HlIhLMpZEEae6ssCw6CwE/X3s7JxaTKg5CMJv+22s=; b=Uxqah/Ca765V4VLn1SUuHUf0Qc7Nh4DgqzhsjE/H9DEJlarGaFxJ+cbD4zJKBLekzK RJnD5FLu5hL1vwKM9/lwZK/P4K8bvLrsIrFsGAxtsKCjkb6KTJmzyfLrOnzZV0+GBh6K stVNXzQHbUzI0r10zLRZutuqRKphQravu2ZiJqfICZuHpUyjKW5h9/iU0vrFt6o1dwld d8gOhc/jbiBmybCvkYraUkkIodwVrX6J87hxA7iaVZ/hFKcsLcC6ekKjNIL1diT1AVIo V2eQC9B8UI0EGnZUlGVePd/Nd7eLsC5/Cf7xIRezlbgMJRuN6gIlaWVxjdl+flpZ2H1v Mtjw==; darn=lists.postgresql.org ARC-Authentication-Results: i=1; mx.google.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1776065276; x=1776670076; darn=lists.postgresql.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=hFgT2EV6L+ar+tVE6p1URMEzzHyVlP1XVhBUplXNJiQ=; b=Ww26vARBS1cr8D/5jdaG+MoBCm4iN+/V8G1REpOpD37PxgYKD3+jcmNC7n+2V63Jos Tn9mTm4XZxwYrTq2umOA4fXoOPo4PeK13dsIAjRFfjCPF5TnX4cdl9GOu4F1qb3Rx0bM 6Gyhhbo1PUBaR2VmHbX4doickP8lYcLZ1BMpGjKQ62EuViIgGOYQ+ums5Jc+w2NzZq7d 0A4/l+PSbuDHplhc8Qr0RJNL/lu6KoSWuocShd8WREV7EYOJm9cqb0sjfJM72XlymMHv cZrxjwrfEv40MZFJ14pClDK91+kLyRdGi0PuRrL6wWdljMxI3ZX9zbj+sq2AmxKbtvy2 UqWQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1776065276; x=1776670076; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=hFgT2EV6L+ar+tVE6p1URMEzzHyVlP1XVhBUplXNJiQ=; b=OCYSFDbYc1B/BKpOWPXS78GRz04PboHjbpSctpCwQCAfsoGQBIOp7F7CJrkQK5p30T 13+nYPjmrA7ReCgI3QRW7flgdiFqjC8TPqPpLVh2OEdgqdO7N7cWvPlAiZYgcMND8HuX ljvYSbbnzNdBcIkklHwHrzw7Qk4j6oyWE9MMLBMgkGjXn8iIk0v6eFLnYhQVtiOV8Xwu fa81PxEE8hUzHZvWXsf3dbbw6dhVUUxKd94NCxqo7AGT5kTqbAMwsSJklStFUFYeEMOU +FG+3ErQxgAjmXX4hDrPMuVAzBKPgIwR6AxLf+iiMOsMmk6jDYFYbGs81uKsIWtQAEFH wsng== X-Forwarded-Encrypted: i=1; AFNElJ+3ed2apGTkad+U+rBgoy3qXqWwUaOX93GjtyUF0Lw1OmeWwbkUm2tsFwoYYpp04vUNtSCu2tQwPk7FiTFQ@lists.postgresql.org X-Gm-Message-State: AOJu0Yw+DbBzym7xAHC+r9FV1Pp9QGfUCtwlA1fwebTa+6iFCD5qYpaz oeuuzbijZSgnuxWJ12lrM6MCjkSBNQHuwg5l76ZkU1oSE5lRud6Oqp1q5WQ6cgKmBAM/6bqYKU5 hWwwnjqHaD3xYc97ZxMvEK0G7YtGeN6o= X-Gm-Gg: AeBDietqgOOX1cTMw/LNk9sFLvCO6VblBf6LkEFq+QrUS8R9MIDIJd4FcPSDglXqWRL gn5eqCg6nE7i72/J1JF4RpbSPYz3ThnHw+vXqE8DoA75GQfb5JXY/SREhKJ4qGrM7ahQFzHXZRJ xv73ULmALmkOyXbhZIPoW1an8LrsgfeZXBtWJGrYqc8D7o7/61xxtS5nAhdpfUGKlpyDr73hCNd i8SusA5hFe59v5XNJlkWZpiVwVZnSpUzQ8wptPdVH2o1m7dy2PF1furF4mp5dU2m3NugxOzo3/Q tTzDU3c= X-Received: by 2002:a05:6102:c02:b0:605:5756:a515 with SMTP id ada2fe7eead31-60a0062cd7fmr4274317137.16.1776065275649; Mon, 13 Apr 2026 00:27:55 -0700 (PDT) MIME-Version: 1.0 References: <22B4A33A-99F3-46F5-BE0C-426A9E1D9ABA@gmail.com> In-Reply-To: <22B4A33A-99F3-46F5-BE0C-426A9E1D9ABA@gmail.com> From: SATYANARAYANA NARLAPURAM Date: Mon, 13 Apr 2026 00:27:44 -0700 X-Gm-Features: AQROBzDqK1lWhJq8LvtPig7g6dvJnBpN1rtw5QwVte5bd2UpRH7gpfXUPt7XDpQ Message-ID: Subject: Re: Bug: Rule actions see wrong values for generated columns (NEW.gen reads OLD value) To: Chao Li Cc: Richard Guo , PostgreSQL Hackers , Peter Eisentraut Content-Type: multipart/alternative; boundary="000000000000da625c064f526bb5" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --000000000000da625c064f526bb5 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Hi Chao, On Mon, Apr 13, 2026 at 12:21=E2=80=AFAM Chao Li w= rote: > > > > On Apr 13, 2026, at 10:35, Richard Guo wrote: > > > > On Mon, Apr 13, 2026 at 10:59=E2=80=AFAM SATYANARAYANA NARLAPURAM > > wrote: > >> NEW. 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. > > > > I didn't find related item in open items. This does not seem to be a > > known issue. I think we should fix it anyway. > > > > cc-ing Peter. > > > > - Richard > > > > Hi Richard and Satya, > > I reproduced the bug following Satya=E2=80=99s procedure and spent some t= ime > debugging it. > > I think the issue is that rewriteTargetListIU() removes generated columns > from the target list, as described by this comment: > ``` > if (att_tup->attgenerated) > { > /* > * virtual generated column stores a null value; stored > generated > * column will be fixed in executor > */ > new_tle =3D NULL; > } > ``` > > Later, when the rule action is rewritten, ReplaceVarsFromTargetList() > cannot find a target list entry for NEW.gen. For UPDATE rules, the missin= g > NEW column is handled with REPLACEVARS_CHANGE_VARNO, so it falls back to > referencing the original target relation row, which gives the old value. > > One possible fix is to build a new target list that adds generated column= s > back when there are rules to fire. I tried the solution locally with some > quick and dirty code and it seems to fix both stored and virtual generate= d > columns for me. > > Do either of you plan to propose a patch for this? If so, please go ahead > and I can review it. Otherwise, I can propose a patch in a couple of days= . I have a patch with me, let me post it shortly. Thanks, Satya --000000000000da625c064f526bb5 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Hi Chao,

On Mon, Apr 13, 2026 a= t 12:21=E2=80=AFAM Chao Li <li= .evan.chao@gmail.com> wrote:


> On Apr 13, 2026, at 10:35, Richard Guo <guofenglinux@gmail.com> wrote:
>
> On Mon, Apr 13, 2026 at 10:59=E2=80=AFAM SATYANARAYANA NARLAPURAM
> <sat= yanarlapuram@gmail.com> wrote:
>> NEW.<generated_coulmn> is resolved to the OLD row's valu= e
>> for update or NULL for insert cases in a DO ALSO rule action for >> generated columns.=C2=A0 This bug affects both stored and virtual<= br> >> generated columns. Reporting here to see if this is a known issue<= br> >> with generated columns.
>
> I didn't find related item in open items.=C2=A0 This does not seem= to be a
> known issue.=C2=A0 I think we should fix it anyway.
>
> cc-ing Peter.
>
> - Richard
>

Hi Richard and Satya,

I reproduced the bug following Satya=E2=80=99s procedure and spent some tim= e debugging it.

I think the issue is that rewriteTargetListIU() removes generated columns f= rom the target list, as described by this comment:
```
=C2=A0 =C2=A0 =C2=A0 =C2=A0 if (att_tup->attgenerated)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 {
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 /*
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0* virtual generated column = stores a null value; stored generated
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0* column will be fixed in e= xecutor
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0*/
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 new_tle =3D NULL;
=C2=A0 =C2=A0 =C2=A0 =C2=A0 }
```

Later, when the rule action is rewritten, ReplaceVarsFromTargetList() canno= t find a target list entry for NEW.gen. For UPDATE rules, the missing NEW c= olumn is handled with REPLACEVARS_CHANGE_VARNO, so it falls back to referen= cing the original target relation row, which gives the old value.

One possible fix is to build a new target list that adds generated columns = back when there are rules to fire. I tried the solution locally with some q= uick and dirty code and it seems to fix both stored and virtual generated c= olumns for me.

Do either of you plan to propose a patch for this? If so, please go ahead a= nd I can review it. Otherwise, I can propose a patch in a couple of days.

I have a patch wit= h me, let me post it shortly.

Thanks,
Satya
--000000000000da625c064f526bb5--