From 5efc08f9a643fbb713bacd53b462aaafd63b8c1f Mon Sep 17 00:00:00 2001
From: Maxime Schoemans <maxime.schoemans@enterprisedb.com>
Date: Tue, 16 Sep 2025 18:19:58 +0200
Subject: [PATCH v2 1/2] Check that index can return in
 get_actual_variable_range()

Follow-up to 9ef1851685b and ee1ae8b99f9. The min and/or max variables
are retrieved from the index using the index-only-scan machinery, so
first check that we can use it by looking at index->canreturn.
---
 src/backend/utils/adt/selfuncs.c | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/src/backend/utils/adt/selfuncs.c b/src/backend/utils/adt/selfuncs.c
index e5e066a5537..eff4372aa90 100644
--- a/src/backend/utils/adt/selfuncs.c
+++ b/src/backend/utils/adt/selfuncs.c
@@ -6574,6 +6574,13 @@ get_actual_variable_range(PlannerInfo *root, VariableStatData *vardata,
 		if (index->hypothetical)
 			continue;
 
+		/*
+		 * get_actual_variable_endpoint uses the index-only-scan machinery,
+		 * so ignore indexes that can't use it on their first column.
+		 */
+		if (!index->canreturn[0])
+			continue;
+
 		/*
 		 * The first index column must match the desired variable, sortop, and
 		 * collation --- but we can use a descending-order index.
-- 
2.39.5 (Apple Git-154)

