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.98.2) (envelope-from ) id 1xEtrK-00000001KaG-1IyT for pgsql-bugs@arkaria.postgresql.org; Thu, 08 Oct 2026 19:32:54 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xEtqK-0000000Doic-18E6 for pgsql-bugs@arkaria.postgresql.org; Thu, 08 Oct 2026 19:31:52 +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.98.2) (envelope-from ) id 1xEtqK-0000000DoiT-082l for pgsql-bugs@lists.postgresql.org; Thu, 08 Oct 2026 19:31:52 +0000 Received: from sss.pgh.pa.us ([68.162.161.243]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1xEtqH-000000016q8-3jhQ for pgsql-bugs@lists.postgresql.org; Thu, 08 Oct 2026 19:31:51 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.18.1/8.18.1) with ESMTP id 698JVmxM2530850; Thu, 8 Oct 2026 15:31:48 -0400 From: Tom Lane To: mertkul669@gmail.com cc: pgsql-bugs@lists.postgresql.org, Richard Guo Subject: Re: BUG #19752: GROUP BY on a constant and a cast to a length-limited array type crashes the server while planning In-reply-to: <19752-928339cd23b58a33@postgresql.org> References: <19752-928339cd23b58a33@postgresql.org> Comments: In-reply-to PG Bug reporting form message dated "Thu, 08 Oct 2026 18:20:17 -0000" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <2530848.1791487908.1@sss.pgh.pa.us> Date: Thu, 08 Oct 2026 15:31:48 -0400 Message-ID: <2530849.1791487908@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk PG Bug reporting form writes: > I found a planner crash (SIGSEGV) in PostgreSQL 18 with the following > query: > SELECT ARRAY[]::varchar(500)[] > GROUP BY CAST('true' AS boolean); > Cause > For ARRAY[]::varchar(500)[], the parser builds an ArrayCoerceExpr whose > elemexpr is a per-element template: > varchar(CaseTestExpr, 500, true) > where the final "true" is the internal isExplicit flag. > substitute_grouped_columns_mutator() in parse_agg.c replaces any > subexpression that equal()s a GROUP BY expression with a Var referencing > the RTE_GROUP RTE. It also descends into elemexpr. The GROUP BY > expression is Const(bool, true), and the internal isExplicit flag is also > Const(bool, true), so the flag is wrongly replaced by a Var. (Dumping > elemexpr at the point of failure on 18.4 shows the third argument is a > VAR rather than a CONST.) Yeah, that's nasty, and what's nastier is that even non-crash cases might result in seriously wrong answers. We shouldn't be replacing constant subexpressions by group Vars ever. I'm inclined to think that GROUP BY items should not be candidates for this search-and-replace unless they contain at least one level-zero Var. But maybe that's too simplistic? regards, tom lane