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 1wEh92-004Ilm-2g for pgsql-hackers@arkaria.postgresql.org; Mon, 20 Apr 2026 05:26:05 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1wEh90-000j6w-1k for pgsql-hackers@arkaria.postgresql.org; Mon, 20 Apr 2026 05:26:02 +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 1wEh90-000j6o-0b for pgsql-hackers@lists.postgresql.org; Mon, 20 Apr 2026 05:26:02 +0000 Received: from mail-oa1-x2f.google.com ([2001:4860:4864:20::2f]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1wEh8y-0000000253R-0O8o for pgsql-hackers@lists.postgresql.org; Mon, 20 Apr 2026 05:26:01 +0000 Received: by mail-oa1-x2f.google.com with SMTP id 586e51a60fabf-408778a8ec4so1799242fac.0 for ; Sun, 19 Apr 2026 22:25:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1776662758; cv=none; d=google.com; s=arc-20240605; b=NMDyDPE0FZIOpIA/GjBD9v8OBuUNt5wOyx+4K6As6B4Ny6VV4WFEwk7c1oRtK/1A3d k9MwGW0i5faeBpX7jPX0fjFy3Lp7sn255DVdsyoytwYV3qU6RxPCmQpRE1oBkqdvmaUT CHzJNJFx6tL4Ekg61m8WvFmFmTeRhEcOJ9+KfEn0h7gZGnswBSc4O5JhaCah0S5430bx yvkLAgxUeZBQ2aNxmMdFHJnMfwELWcz2jYoxowo8RsqiBCf8CYbpriO+kLiYqoOK3Iep +THTmxQfHRCWRJkmZpeofEXNmXc6qeKh5c+9ZC0KIGZbWEM085TuUiU9xH81rxIfEDkl V0IQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=gqph6UFvuFxO9Ue/1mU+Xax98rbiVl+vCzsy16GufEY=; fh=kh4wHU3WsR+ZggICG6IisQkCfQM+JIw55PZUSC2WjIg=; b=BweCD2IbT3+jRLPzJSSxOPoYoYAOzlRXYTnSmsSy+Mvcz/mNcRBvAJ8LbmD+2ySGHu 2Uu/1H6J3BzMT0fUmIAbMfvz0254c1Yi66V4LmJnzFTYNqzBrHjq3VgVrnNSuTK/cCy7 9plYYKp5aChsuwwicm4eizoAvLuwEhOMKj2LIbQShOvF4fYDr35Ttg38l8tgFpI0rdL3 krcSeaC1rr8RI4vPuZ+nxb/mASvqzrU0ElrYmz7ZRWe94KP9N7kqmRCSPo176wl4oN3C qqIir7ri1GJdrddHtP+Ki5jIZBRkvp7DL9z4t2HbKXvGwp99lQjmG6fsCMOwqcMVl/WJ JYog==; 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=1776662758; x=1777267558; darn=lists.postgresql.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=gqph6UFvuFxO9Ue/1mU+Xax98rbiVl+vCzsy16GufEY=; b=S0sbp56DfKYkeUheugT8FvFYwmMCgWj0CiplqaARfJe/CSnZWSST9R0USBfrXt5YTv YXaLjgsi0V5gSkAKM+ui1TDp15iKBY+yOYLSVntbCZE8U/shE8DbXPQkPRwtpNXaLv+K zQxWRbQ75ex4h7aOzwJaNKJor1u5kXCAw2OA7g5ck33057mRkjn8SI5oiiVbLqSIDE0v 8sVpldYdEqke/Vg9ctcMxHkWhP+lLO7m0edezJFF3K9VBAE44uvUn0vztxcL05/hf2Ve 1B//KqVSTAHSTYa+jCvAQdb+aelv/TQ3n/BEgJ+u83TmK+8AvIamA7v7UmoFVT4cjs3r ukpQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1776662758; x=1777267558; h=content-transfer-encoding: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=gqph6UFvuFxO9Ue/1mU+Xax98rbiVl+vCzsy16GufEY=; b=f+WtnAXsCGc8VlNHh8wGHVQrgBQmKmB8zTqXeVgbJ8CoQTHaLs8cZnWXsNvRwRNAPo 5ErhWNZCQK8pQjLCtzuhqks0B6mK8E9d64al/8QfXtw74vFjeN+GsiOTMumYAYNr111a b8OZEvC48szt8Y9V7AjOeax7IvOEqTR2YOtFFf13y9S7HTJ0Qs6kJvcCBy2w4gSlnZaH c/jW7JwlOHqU3zPtfQSXCAkH/TJtqk89wWPsz+h8JWY28qwmH02FbPn/fifaTxJV+fus GquIUAFZit4yWWp4RKuGM5/WWJrBX2GO4Rqzq/m4Goe883TNZOmr+xpNKT2p4qSFTQxa byPA== X-Forwarded-Encrypted: i=1; AFNElJ/rLfBwsm0J7G5y2Xc+ixM2XHvK9xMBzbyhpbthxW1vu1W7NYwJzIqfdjZBNTi6vamv8zTdExlsl3l1WPwL@lists.postgresql.org X-Gm-Message-State: AOJu0Yzy475ddhswUPd15Ww/9hYPkW+7bFIhlOr3ufcjXU4LVo/kvVAE zEOVos31GvtHiZmq19sllVGj2AfPsAtYLqwHg3Xv81dIrGSFEEg/dmo+1cmk+jl7BLr/v4O4swP 0myBrniyp52FYlWorXEXV8aP+N0vSP98= X-Gm-Gg: AeBDies7NKuFGGsxfdvrFCfSuGvksrh9Kp9CqPi0tIR6+Kk8947t+djWe2spYx3tUJ0 lHpemgen9VoethVhwemTVFLVqE/7rRBLthqC8T4gp7wY1mDJgNliylHQHQu3xJj89BMbGLAntMF cO+dZwjU25iXZYulNHsG2YvObBsiEV2sq/g3wTslPOUhvsgnDqzMe1RZWeysSPFd9aotoxV4Kl7 Otw5mKqXeOZO4kTSj9KVfJrXLZGiLkv9qHjzpOl+wgYa0+xiRJO4CwU7IIf1jvMmcFHgaLl1G79 JurtdNg1aNz/7wz2GFch X-Received: by 2002:a05:6870:970f:b0:422:f2ba:adea with SMTP id 586e51a60fabf-42adeda78afmr6945943fac.31.1776662757896; Sun, 19 Apr 2026 22:25:57 -0700 (PDT) MIME-Version: 1.0 References: <22B4A33A-99F3-46F5-BE0C-426A9E1D9ABA@gmail.com> <8D1CD3EB-BF72-4C73-AF24-D88581AC01BE@gmail.com> In-Reply-To: From: Richard Guo Date: Mon, 20 Apr 2026 14:25:43 +0900 X-Gm-Features: AQROBzCbX1tDSQmrcSgerLDH8UGO1QmRZy8hlOuOIInRHu8XMNERq3WVPh3L5Fw Message-ID: Subject: Re: Bug: Rule actions see wrong values for generated columns (NEW.gen reads OLD value) To: Dean Rasheed Cc: Chao Li , SATYANARAYANA NARLAPURAM , PostgreSQL Hackers , Peter Eisentraut Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Fri, Apr 17, 2026 at 7:35=E2=80=AFPM Dean Rasheed 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