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 1vdq6q-005Tei-0R for pgsql-hackers@arkaria.postgresql.org; Thu, 08 Jan 2026 13:31:29 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1vdq5p-002Adn-0J for pgsql-hackers@arkaria.postgresql.org; Thu, 08 Jan 2026 13:30:25 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1vdq5o-002Add-27 for pgsql-hackers@lists.postgresql.org; Thu, 08 Jan 2026 13:30:25 +0000 Received: from mail-oa1-x29.google.com ([2001:4860:4864:20::29]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1vdq5n-004tRP-2t for pgsql-hackers@lists.postgresql.org; Thu, 08 Jan 2026 13:30:24 +0000 Received: by mail-oa1-x29.google.com with SMTP id 586e51a60fabf-3e7f68df436so1432507fac.1 for ; Thu, 08 Jan 2026 05:30:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1767879023; x=1768483823; 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=OwBRbbJ0Q0XnfWg2qsAi7fTAEVOma/fO6JG1Cwct0WM=; b=BnlOm1HvBv0EgCL/ykjOcuyxM4PwwFFsHDfTmE/yPtxvrSDxtm1/mp2+p0iW3eA4pX sZeXrbkJSQp+iwAONsvmhiJO8sx1/pOHZHvj6bhM0hTpaKWlHLHaa7KV3MdgdYGmdU/K VlrG0Av8kb89EoKX54Ns5ZkB5GUX84fGGkvM//QaP9O1wJcv0Pjo3AXu9MFq9Feg9AUP Wj98Z0wPKfSSxa2T+a1I+5iiIbdhl0otP0zfmF238MZd8EJGDfneZvBFZ1NVoiIoFzBp Q8oaZORy1WFjqBvoDnYUJb4YMrLBKIiC/w5HiOnsZ4HGhvKJiS9V+dHmugurBhpiQ8Fo jiyA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1767879023; x=1768483823; 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=OwBRbbJ0Q0XnfWg2qsAi7fTAEVOma/fO6JG1Cwct0WM=; b=EWuFDInwczuw2ycPEyYRxIcgC9hC6SMRj01ff7nV4N+RmNdhJVfB+CtXKnSl8F+9/c fp0O4SWS8zDEQmn5vvaBA+zmMYKsy0fTPgAs45HlDl/MBBoVwqzv7ByoLyGz4p7ORhSZ ejxwgR0j0yL0AlN2Zpy53kGPvUWPI4JiKMZyIS2FvB+c/vBnXDRqNeUJRZOfgfqNKJ9F kWb6nxVsiQBFT/TfYVMK3mjqUaf5UsIoL6s79pac0JvHunl0t50CM9hLk4540OoLvbuN tGn/eVBhVkhcqJLt/sUJem++lbQxOjFIypxaIsydh85V0pxR9HmT5T6gJeTljcsYaGJ3 4MtA== X-Forwarded-Encrypted: i=1; AJvYcCUn6L7m+2II+9ZNsJg0ADUGJ5vcVN8lnXRrvQs9T7Vc78+oczzU2xkqOAuzSHkO8GfvXUGHFmnjTP3LjXi+@lists.postgresql.org X-Gm-Message-State: AOJu0YwcwP7h0lz614wWzwa9WkXf+xIWurW9bUaHAXCM2mxjFzQeKPTI LNFu1l9RIfChUK9s83RV9gzRUcLVT/+wXtOIMuaUKK9g4mydCt/Y2RPOmiIL+OLr87e6vdmxBsN d4OcwNxcJAN84Cr3/7Rtzcvdmgst8RSI= X-Gm-Gg: AY/fxX6S1uSzmlsHagrrwknyv5rKBVk0sMPgTEDtKf18ZDuZmUGLouUoUFaJDIH3mNF wkRhL64g/ghDvG3r6Ox4mdigZmDggNKk3fKhyHaESQks+2KSc5Sedc9p4R46gzw7ubkyEQZix5c GHTCzFeG8A12eLZ24Sq/EITEDd2n05WrQ0VghFTIwlKi5MLVAKXSLgYntpFPBR6bQ5ztedrkbR2 tQVXMqmrkAQcp6okg7u08ghpbc6Gp4ilMtRRNmGe5bSjgRv9BHHH5q0RPKxG2MlFh4dyhnT X-Google-Smtp-Source: AGHT+IGJAYEbXVhPkRnVBLwlN5rIT6IJ2EBagNLHX1QpwOso5iHpkdUUnlhN7khOgM85ne1O34UoHhQXdQLC8+ub0PE= X-Received: by 2002:a05:6871:5807:b0:3ec:4067:c684 with SMTP id 586e51a60fabf-3ffbed0d6d1mr3077378fac.3.1767879022701; Thu, 08 Jan 2026 05:30:22 -0800 (PST) MIME-Version: 1.0 References: <7900964C-F99E-481E-BEE5-4338774CEB9F@gmail.com> <1607957.1767725532@sss.pgh.pa.us> <1654153.1767753474@sss.pgh.pa.us> <1752324.1767805242@sss.pgh.pa.us> <1892152.1767844982@sss.pgh.pa.us> In-Reply-To: <1892152.1767844982@sss.pgh.pa.us> From: Richard Guo Date: Thu, 8 Jan 2026 22:30:11 +0900 X-Gm-Features: AQt7F2qFWDNuv8n6Bgbe3sc7xhvcR0LTa6y_Ndx2Hv_EzFfZBqSJrSEq0WG5j2M Message-ID: Subject: Re: Fwd: pg18 bug? SELECT query doesn't work To: Tom Lane Cc: Eric Ridge , Pg Hackers 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 Thu, Jan 8, 2026 at 1:03=E2=80=AFPM Tom Lane wrote: > [ itch... ] That statement is false in general, because subquery > pullup within the subquery can replace a sub-subquery's output Vars > with expressions. It might be okay for this purpose, as I think we'd > not pull up if the sub-subquery's output expressions are volatile or > SRFs. These assumptions had better be well commented though. Ah, I see. Once the sub-subqueries are flattened, the join alias entries in the subquery can become arbitrary expressions rather than simple Vars. I suspect we have only avoided issues with join aliases in subquery_is_pushdown_safe() for all these years by sheer luck: since we don't pull up subqueries that output set-returning or volatile functions, those 'arbitrary expressions' are unlikely to include them. How about we add a comment to check_output_expressions() along the below lines? /* * We need to expand grouping Vars to their underlying expressions (the * grouping clauses) because the grouping expressions themselves might be * volatile or set-returning. However, we do not need to recurse deeper * into the arguments of those expressions. If an argument references a * lower-level subquery output, we can rely on the fact that subqueries * containing volatile or set-returning functions in their targetlists are * never pulled up. * * We do not need to expand join alias Vars. The underlying expression of * a join alias Var does not itself introduce volatility or set-returning * behavior. As with grouping Vars, we rely on the pull-up restrictions to * guarantee that any referenced inputs from lower levels are free of such * functions. */ > The larger point here is that this behavior is all recursive, > and we can happily end with an expression that's been pulled up > several levels; we'd better make sure the right checks happen. > So I'm a little bit distressed that planner.c's invocations of > flatten_group_exprs are not at all analogous to its usage of > flatten_join_alias_vars. The latter pattern has a couple of > decades of usage to lend credence to the assumption that it's > correct. flatten_group_exprs, um, not so much. It may be > fine, given the fact that grouping Vars can appear within > much less of the query than join aliases. But in view of the > present bug, I'm feeling nervous. I checked the invocations of flatten_join_alias_vars and flatten_group_exprs in the planner to understand why they are not being used analogously. 1. planner.c:1041 Here, we call flatten_join_alias_vars on the subquery because the subquery may contain join aliases from the outer query level; since these won't be expanded during the subquery's own planning, we must expand them now. A query illustrating this scenario is: select * from tenk1 t1 full join tenk1 t2 using (unique1) join lateral (select unique1 offset 0) on true; (BTW, the test cases added in da3df9987 for this logic are no longer valid. These queries still function correctly even if this code is removed. I think this is something we should fix.) However, I don't think an analogous call to flatten_group_exprs is necessary here. Subqueries should not contain grouping-Vars from the outer query, since FROM clause is processed before the GROUP BY step. While it is true that a subquery could reference the output of another subquery that happens to be a grouping-Var, that would be handled when expanding grouping-Vars for that specific subquery. 2. prepjointree.c:1473 Here, we call flatten_join_alias_vars on the subquery's targetlist during subquery flattening because once the the subquery's subqueries are flattened, join alias entries may become arbitrary expressions rather than simple Vars. Again, I don't see a need for an analogous flatten_group_exprs call here. Any subquery containing grouping-Vars must involve grouping, and we don't flatten such subqueries to begin with. 3. planner.c:1309 Here, flatten_join_alias_vars is called within preprocess_expression for various expressions. The analogous call to flatten_group_exprs occurs at planner.c:1118. I believe we only need to call flatten_group_exprs on the targetList and havingQual, as these are the only places where grouping-Vars can appear. Based on the above, I suspect whether we should expect the invocations of flatten_group_exprs to be analogous to those of flatten_join_alias_vars. - Richard