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 1wCBcG-001kCJ-1m for pgsql-hackers@arkaria.postgresql.org; Mon, 13 Apr 2026 07:21:52 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1wCBcD-005EkT-1M for pgsql-hackers@arkaria.postgresql.org; Mon, 13 Apr 2026 07:21:50 +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 1wCBcD-005EkL-0O for pgsql-hackers@lists.postgresql.org; Mon, 13 Apr 2026 07:21:50 +0000 Received: from mail-pl1-x62b.google.com ([2607:f8b0:4864:20::62b]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1wCBcB-00000000nOR-2T9g for pgsql-hackers@lists.postgresql.org; Mon, 13 Apr 2026 07:21:49 +0000 Received: by mail-pl1-x62b.google.com with SMTP id d9443c01a7336-2a9296b3926so21009445ad.1 for ; Mon, 13 Apr 2026 00:21:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1776064905; x=1776669705; darn=lists.postgresql.org; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=19K9k8V14fuYl5tnU/xTcYiKAKGT0V2Ol+dFYMIGLfo=; b=sqE/xc8U6egVhQ8Ce4RnbvcrRg+DsQEgnwReqSit5sTSZ5JaQddEqd14PrRzaktIR5 KoaKXEUKbb2wZKIRl933kJwNOAbI8ZPCZnl9EtPfhMx/E2T8Bp5zgdwkz3e7fV6A6SYY hADc1QCLHBV3dYGo82Y4arkRznkFe+nkxiQF0HXaPsFS0UVkBRmeEiK50KuPwb8/1ESS ESP5vcmfOnYNEyIWCHd22Ev6YIUFDs+9q33Y/45CcUyO3YimVE+LTFAMBFQXgT/IrcwD pyxopEfuE7IleyJ+9DR2L7F9rzb3f3+0gYMOWZ1TLiQLwHGOIV1dbwyDMIoyzem9AXsg 3BSA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1776064905; x=1776669705; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:x-gm-gg:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=19K9k8V14fuYl5tnU/xTcYiKAKGT0V2Ol+dFYMIGLfo=; b=jMX84LwCP/DczovKyjoVi4SguLCFJqQom0MzlTsjL12QVTg4uJKYdDYmUx/Ou9LlKB h1EG48AhZuU/hbcjudx3szBp3hVZUwTMt15frXCONn5V7J8si0FrainLqabfHz3gEDhz qQj9TYdV3oJhRn+GsuWXR8rT992C8jqxWcx7CofiOYxbPFvWZuVbd52KlDRK25uTK92E kCQjO04uGzQj2ttFovsP/6y/9Q4aX9mbXlJhLZuYCE9nPzQSL6nD/hOcZoNG1D5b5xky GLqC00JjyC7TRcbCATdbqtGONjImmCErxdm+MhYs0hkLfswVesTXomha+Mnm3tTcmiFh VynA== X-Forwarded-Encrypted: i=1; AFNElJ9CknrNDKLiYVou3z9HQXURPUtwK0Ub41SplfqFB8y5Ih3ylaYJofUE8rnfvzfQX+JHAVAWhT4gVxtyjui3@lists.postgresql.org X-Gm-Message-State: AOJu0Yzlh0mt0hIOFpy8OkGKmMTaKoLxEAGtjRpME81OWSrzFzn3FHZ9 EfCR03UqgBh8szu58dedztDzfY9hOZv7Bg7o3jWORURJ2NOBvnY9OwPb/rOBDuzhVFc= X-Gm-Gg: AeBDietDT3hMEPY7guqvLOOCjF3tuQvI4cN5HrlPd/JBf9FO0MLoBGL/lOBzlDz5X5A Nc3to9EkUbTMqQEdp4WHbFXHEYqVl6nVPPR8vPleg9+ez+WPYr/2FjrxjRyQieIkeE/EyHIACni E9jtk25taCjalGNCOs9mBJ+utWJUurH7EcYqcUQt1faXHDm5JoJIl7bepVsaZPpVzhil7uTV5dl FfV+Xvd4Iaxyyzmlhsu9ks3Y6fWA71vaXYKzn7GjBHvvojy2srF0OCWaEp83o01h2PLVwyodZQ/ g1umekF/EniaCZHPXN9uJLYl9+YkjT3vuiZN6Ah+fPTOC0GA0xRMW0eQPs/j4roS0Y2X/MRj7kf 7M7HLwKsaJ7Bd995ZvgOP9L4nAYmpZTniZLOxAnVYosoRiHCReSyecrfpS4vfOCeYApmwMWzCVn o4a5uXis6GSV2GqY7AVwGDvLUEyhPK5DQ= X-Received: by 2002:a17:902:cec6:b0:2b0:6b98:59ec with SMTP id d9443c01a7336-2b2d5a5d84bmr136773585ad.34.1776064905051; Mon, 13 Apr 2026 00:21:45 -0700 (PDT) Received: from smtpclient.apple ([45.32.121.103]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2b2d4f0b284sm106966515ad.52.2026.04.13.00.21.42 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 13 Apr 2026 00:21:44 -0700 (PDT) Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3864.400.21\)) Subject: Re: Bug: Rule actions see wrong values for generated columns (NEW.gen reads OLD value) From: Chao Li In-Reply-To: Date: Mon, 13 Apr 2026 15:21:05 +0800 Cc: SATYANARAYANA NARLAPURAM , PostgreSQL Hackers , Peter Eisentraut Content-Transfer-Encoding: quoted-printable Message-Id: <22B4A33A-99F3-46F5-BE0C-426A9E1D9ABA@gmail.com> References: To: Richard Guo X-Mailer: Apple Mail (2.3864.400.21) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk > On Apr 13, 2026, at 10:35, Richard Guo wrote: >=20 > 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. >=20 > 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. >=20 > cc-ing Peter. >=20 > - Richard >=20 Hi Richard and Satya, I reproduced the bug following Satya=E2=80=99s procedure and spent some = time 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 = missing 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 = columns 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 generated 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. Best regards, -- Chao Li (Evan) HighGo Software Co., Ltd. https://www.highgo.com/