public inbox for [email protected]  
help / color / mirror / Atom feed
From: Richard Guo <[email protected]>
To: Dean Rasheed <[email protected]>
Cc: Chao Li <[email protected]>
Cc: SATYANARAYANA NARLAPURAM <[email protected]>
Cc: PostgreSQL Hackers <[email protected]>
Cc: Peter Eisentraut <[email protected]>
Subject: Re: Bug: Rule actions see wrong values for generated columns (NEW.gen reads OLD value)
Date: Mon, 20 Apr 2026 14:25:43 +0900
Message-ID: <CAMbWs48s9EVTz__xq9s0nAGeh-Z918t4VoGD+pxCXOq0mnc_7Q@mail.gmail.com> (raw)
In-Reply-To: <CAEZATCUacsV=ffircCgh+uYj7FBh4vy5mFKCAR7g=EkXuJZBng@mail.gmail.com>
References: <CAHg+QDexGTmCZzx=73gXkY2ZADS6LRhpnU+-8Y_QmrdTS6yUhA@mail.gmail.com>
	<CAMbWs4-SJ6B26ciFu_K_2M1ObDA=GirV87N1BKeAtSRmQGATUA@mail.gmail.com>
	<[email protected]>
	<CAMbWs48=QrLy8gZprTfCmEtzoaUku54JXvAcnoRSo+OC8vUpwA@mail.gmail.com>
	<CAEZATCWYqyJJJcMNEj-LEvCSwog2H_HS61hTROZLKYuaiy5YTg@mail.gmail.com>
	<CAMbWs49DcHqOtd_BzuFcr9Va+tD-2fNs8bGsbH_Zy2bXUTNM1g@mail.gmail.com>
	<[email protected]>
	<CAEZATCUacsV=ffircCgh+uYj7FBh4vy5mFKCAR7g=EkXuJZBng@mail.gmail.com>

On Fri, Apr 17, 2026 at 7:35 PM Dean Rasheed <[email protected]> wrote:
> I don't quite buy the argument that the rule action is typically
> small. We have no idea how big it might be. Note that, at that point
> in the code, sub_action is the combination of both the original query
> and the rule action.
>
> I do accept though that rules are not widely used, and that it's not
> worth optimising too much, if it means a lot of extra complexity.
> However, IMO, it is slightly simpler and neater to put the expanded
> generated columns in the replacement list used by
> ReplaceVarsFromTargetList() on sub_action.

Fair point.

> In the attached v2 patch, I've done that by refactoring
> expand_generated_columns_internal(), renaming it to
> get_generated_columns(), and making it just return the list of
> generated column expressions, rather than doing the rewrite -- I never
> particularly liked the separation of concerns between
> expand_generated_columns_internal() and
> expand_generated_columns_in_expr(), especially after the rest of the
> code expanding virtual generated columns was moved out of the
> rewriter, so that expand_generated_columns_in_expr() became the only
> caller of expand_generated_columns_internal(). Doing this simplifies
> the function, since it's no longer necessary to pass it node, rte, and
> result_relation.
>
> With that change, all rewriteRuleAction() needs to do is get the
> generated columns, rewrite any new.attribute references in them, and
> then use that list plus the original target list as the replacement
> list when rewriting sub_action.

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.

- Richard





view thread (16+ messages)  latest in thread

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: [email protected]
  Cc: [email protected], [email protected], [email protected], [email protected], [email protected], [email protected]
  Subject: Re: Bug: Rule actions see wrong values for generated columns (NEW.gen reads OLD value)
  In-Reply-To: <CAMbWs48s9EVTz__xq9s0nAGeh-Z918t4VoGD+pxCXOq0mnc_7Q@mail.gmail.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox