0001-pg_plan_advice-Avoid-assertion-failure-with-partitio.ncfbot

application/octet-stream

Filename: 0001-pg_plan_advice-Avoid-assertion-failure-with-partitio.ncfbot
Type: application/octet-stream
Part: 0
Message: Re: pg_plan_advice
From c941d57f7b73c64164229f173d97ff1f409fbb60 Mon Sep 17 00:00:00 2001
From: Robert Haas <rhaas@postgresql.org>
Date: Fri, 27 Mar 2026 08:43:14 -0400
Subject: [PATCH] pg_plan_advice: Avoid assertion failure with partitionwise
 aggregate.

An Append node that is part of a partitionwise aggregate has no
apprelids. If such a node was elided, the previous coding would
attempt to call unique_nonjoin_rtekind() on a NULL pointer, which
leads to an assertion failure. Insert a NULL check to prevent that.

Reported-by: Alexander Lakhin <exclusion@gmail.com>
Discussion: http://postgr.es/m/0afba1ce-c946-4131-972d-191d9a1c097c@gmail.com
---
 contrib/pg_plan_advice/pgpa_scan.c | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/contrib/pg_plan_advice/pgpa_scan.c b/contrib/pg_plan_advice/pgpa_scan.c
index 5f210f2b725..c79db6fb1b3 100644
--- a/contrib/pg_plan_advice/pgpa_scan.c
+++ b/contrib/pg_plan_advice/pgpa_scan.c
@@ -68,8 +68,15 @@ pgpa_build_scan(pgpa_plan_walker_context *walker, Plan *plan,
 		 * Note that the PGPA_SCAN_PARTITIONWISE case also includes
 		 * partitionwise joins; this module considers those to be a form of
 		 * scan, since they lack internal structure that we can decompose.
+		 *
+		 * Note also that it's possible for relids to be NULL here,
+		 * if the elided Append node is part of a partitionwise aggregate.
+		 * In that case, it doesn't matter what strategy we choose, but we
+		 * do need to avoid calling unique_nonjoin_rtekind(), which would fail
+		 * an assertion.
 		 */
 		if ((nodetype == T_Append || nodetype == T_MergeAppend) &&
+			relids != NULL &&
 			unique_nonjoin_rtekind(relids,
 								   walker->pstmt->rtable) == RTE_RELATION)
 			strategy = PGPA_SCAN_PARTITIONWISE;
-- 
2.51.0