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 1xEt4v-00000001KDw-0JUn for pgsql-bugs@arkaria.postgresql.org; Thu, 08 Oct 2026 18:42:53 +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 1xEt4s-0000000DYe8-3kfO for pgsql-bugs@arkaria.postgresql.org; Thu, 08 Oct 2026 18:42:50 +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.98.2) (envelope-from ) id 1xEsjs-0000000DMib-2j73 for pgsql-bugs@lists.postgresql.org; Thu, 08 Oct 2026 18:21:08 +0000 Received: from mahout.postgresql.org ([2001:4800:3e1:1::227]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1xEsjq-000000011rD-1R0h for pgsql-bugs@lists.postgresql.org; Thu, 08 Oct 2026 18:21:07 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=postgresql.org; s=20171124; h=Message-ID:Date:Reply-To:Cc:From:To:Subject: Content-Transfer-Encoding:MIME-Version:Content-Type:Sender:Content-ID: Content-Description:In-Reply-To:References; bh=z4IRDWf/CaKs78Y34JSOKE+BX/SQXmK6OBoIdgApE1k=; b=NY2mWjUvKVt3zfwJwA72VkM2ke /3Ke4FEd6mU1Y9WK2KdsWOQwNxp8/UQRtSi7EMVwMl5Si2DR//mcfovFYezwd/mDZoAx8Gb3b5cSk L8RPNm9uab8MfQ/ItJeYcM8Xa4KgqD2UAJPLNrSd7zTZIGaJftCn6u/gNZTwm3lkA/nf5x80KR73U xMLWAt+Pn0cf5yirgW6nnPm/L2Z9xCtNZ+6/J6A8RpRBbtXsq0znpBAr7g1CZMqxBz88XvsaaZgPb edBrTEw9HPrO3RuoxeWA0w8p6g7iqdJKrA8lek3HzHmaHriF24oF4E1E/7jDQMm6gij4479wwXhad mb/la4Mw==; Received: from wrigleys.postgresql.org ([2a02:16a8:dc51::60]) by mahout.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1xEsjp-000000035Ku-3RxB for pgsql-bugs@lists.postgresql.org; Thu, 08 Oct 2026 18:21:05 +0000 Received: from localhost ([127.0.0.1] helo=wrigleys.postgresql.org) by wrigleys.postgresql.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xEsjo-000000095UT-0qMo for pgsql-bugs@lists.postgresql.org; Thu, 08 Oct 2026 18:21:04 +0000 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Subject: BUG #19752: GROUP BY on a constant and a cast to a length-limited array type crashes the server while planning To: pgsql-bugs@lists.postgresql.org From: PG Bug reporting form Cc: mertkul669@gmail.com Reply-To: mertkul669@gmail.com, pgsql-bugs@lists.postgresql.org Date: Thu, 08 Oct 2026 18:20:17 +0000 Message-ID: <19752-928339cd23b58a33@postgresql.org> X-Auto-Response-Suppress: All Auto-Submitted: auto-generated List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk The following bug has been logged on the website: Bug reference: 19752 Logged by: Mert Kul Email address: mertkul669@gmail.com PostgreSQL version: 18.4 Operating system: Ubuntu Description: =20 Hi, I found a planner crash (SIGSEGV) in PostgreSQL 18 with the following query: SELECT ARRAY[]::varchar(500)[] GROUP BY CAST('true' AS boolean); Tested on 18.6 (REL_18_STABLE at 1370a7832a). The query works on 17.11. No tables or data are required, and EXPLAIN alone is enough to reproduce the crash, so the executor proper is never reached. The planner does, however, call executor expression-evaluation code while constant-folding the expression. I first hit the crash with a LEFT JOIN query, but the FROM clause and the join are not required. Reproduction (unpatched 18.6) ARRAY[]::varchar(500)[] GROUP BY CAST('true' AS boolean) crash ARRAY[]::char(1)[] GROUP BY CAST('true' AS boolean) crash ARRAY[]::varchar(500)[] without GROUP BY ok ARRAY[]::varchar[] GROUP BY ... (no typmod) ok ARRAY[]::text[] GROUP BY ... ok ARRAY['a']::varchar(500)[] GROUP BY ... (non-empty array) ok The crash needs both a constant GROUP BY expression and an array whose element type requires length coercion, such as varchar(n) or char(n). Backtrace (18.6, assert-enabled, -O0) #0 slot_getsomeattrs (slot=3D0x0, attnum=3D1) at tuptable.h:361 #1 ExecInterpExpr (...) at execExprInterp.c:664 #2 ExecInterpExprStillValid (...) at execExprInterp.c:2307 #3 ExecEvalExprSwitchContext (...) at executor.h:440 #4 evaluate_expr (expr=3D..., result_type=3D1015, result_typmod=3D504, result_collation=3D100) at clauses.c:5082 #5 eval_const_expressions_mutator (node=3D, ...) at clauses.c:3155 #6 expression_tree_mutator_impl (...) #7 eval_const_expressions_mutator (...) at clauses.c:3797 #8 expression_tree_mutator_impl (...) #9 eval_const_expressions_mutator (...) at clauses.c:3797 #10 eval_const_expressions (...) at clauses.c:2295 #11 preprocess_expression (..., kind=3D1) at planner.c:1348 #12 subquery_planner (...) at planner.c:919 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.) eval_const_expressions_mutator() then sees an ArrayCoerceExpr with a Const argument and no volatile functions in elemexpr, and folds it through evaluate_expr(). ExecInitExprRec() assumes elemexpr contains no Var, but the substitution above violates that assumption. The Var is evaluated without a tuple slot, which dereferences NULL. Why 17 is not affected In 17, check_ungrouped_columns_walker() performs the same equal() check, but a match only stops the descent; the expression tree is not rewritten. Commit 247dea89f ("Introduce an RTE for the grouping step") made a match rewrite the tree, without skipping these template fields. The ArrayCoerceExpr folding code in clauses.c is identical in 17 and 18, so the relevant change is on the parser side, in parse_agg.c. Thanks, Mert Kul