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 1vvIld-00Cjle-2a for pgsql-general@arkaria.postgresql.org; Wed, 25 Feb 2026 17:33:45 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1vvIlc-007pvq-2J for pgsql-general@arkaria.postgresql.org; Wed, 25 Feb 2026 17:33:44 +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 1vvIlc-007pvb-1H for pgsql-general@lists.postgresql.org; Wed, 25 Feb 2026 17:33:44 +0000 Received: from sss.pgh.pa.us ([68.162.161.243]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1vvIlZ-000000019YA-1NTg for pgsql-general@lists.postgresql.org; Wed, 25 Feb 2026 17:33:43 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 61PHXdcU295609; Wed, 25 Feb 2026 12:33:39 -0500 From: Tom Lane To: "David G. Johnston" cc: Adrian Klaver , PALAYRET Jacques , pgsql-general@lists.postgresql.org Subject: Re: PostgreSQL query with FULL OUTER JOIN and subquery using column of GROUP BY expression ERROR: subquery uses ungrouped column from outer query In-reply-to: References: <970600119.344834366.1772018068349.JavaMail.zimbra@meteo.fr> <52acd108-2949-4762-80c2-c761662fde73@aklaver.com> Comments: In-reply-to "David G. Johnston" message dated "Wed, 25 Feb 2026 09:20:18 -0700" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <295607.1772040819.1@sss.pgh.pa.us> Date: Wed, 25 Feb 2026 12:33:39 -0500 Message-ID: <295608.1772040819@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk "David G. Johnston" writes: > My first impression is that this is a bug. Especially since the query > apparently executes in both left-join and right-join modes. Well, it's a longstanding deficiency anyway. The problem is that the full-join-using merged column "id" is represented as "COALESCE(t2.id, t3.id)" not as a single Var. That should be okay, because we can handle grouping by an expression, but substitute_grouped_columns (and check_ungrouped_columns before it) doesn't handle the case in subqueries: * NOTE: we recognize grouping expressions in the main query, but only * grouping Vars in subqueries. For example, this will be rejected, * although it could be allowed: * SELECT * (SELECT x FROM bar where y = (foo.a + foo.b)) * FROM foo * GROUP BY a + b; * The difficulty is the need to account for different sublevels_up. * This appears to require a whole custom version of equal(), which is * way more pain than the feature seems worth. This commentary dates to 2003 (and it's from a patch that replaced an older implementation with the same limitation; it doesn't look to me like the case ever worked). AFAIR, the number of complaints we've gotten about this limitation in the past 30 years could be counted without running out of thumbs. Still, it is annoying. I wonder if there's a way to do it without either a large amount of new code or exponential time spent trying useless subexpression matches... regards, tom lane