From 4f7408d97dde29520cc33bab8a39b976592dcad5 Mon Sep 17 00:00:00 2001
From: Corey Huinker <corey.huinker@gmail.com>
Date: Fri, 23 Jan 2026 00:41:06 -0500
Subject: [PATCH v30 4/6] Avoid multirange step-down in statatt_get_type() when
 operators are not needed.

This highlights a situation which does not happen in attribute stats:
the multirange value appears as a part of a tuple from parsed array
input.

The fact that we need to do this is a sign that statatt_get_type()
is not the best place to handle per-type exceptions for multiranges and
tsvectors.
---
 src/backend/statistics/extended_stats_funcs.c | 42 +++++++++----------
 src/backend/statistics/stat_utils.c           | 13 ++++++
 2 files changed, 32 insertions(+), 23 deletions(-)

diff --git a/src/backend/statistics/extended_stats_funcs.c b/src/backend/statistics/extended_stats_funcs.c
index f590fab3dbf..66a2439ea9c 100644
--- a/src/backend/statistics/extended_stats_funcs.c
+++ b/src/backend/statistics/extended_stats_funcs.c
@@ -624,15 +624,15 @@ extended_statistics_update(FunctionCallInfo fcinfo)
 		 * The leading stxkeys are attribute numbers up through numattnums.
 		 * These keys must be in ascending AttNumber order, but we do not rely
 		 * on that.
+		 *
+		 * XXX:
+		 * We are using a special case of statatt_get_type, which is probably
+		 * a sign that some of what statatt_get_type does should be moved
+		 * out of there and back into attribute_stats.
 		 */
 		for (int i = 0; i < numattnums; i++)
 		{
 			AttrNumber	attnum = stxform->stxkeys.values[i];
-
-			Oid			lt_opr;
-			Oid			eq_opr;
-			char		typetype;
-
 			/*
 			 * Fetch attribute entries the same way as what is done for
 			 * attribute statistics.
@@ -641,10 +641,10 @@ extended_statistics_update(FunctionCallInfo fcinfo)
 							 attnum,
 							 &atttypids[i],
 							 &atttypmods[i],
-							 &typetype,
+							 NULL,
 							 &atttypcolls[i],
-							 &lt_opr,
-							 &eq_opr);
+							 NULL,
+							 NULL);
 		}
 
 		/*
@@ -662,21 +662,6 @@ extended_statistics_update(FunctionCallInfo fcinfo)
 			atttypids[i] = exprType(expr);
 			atttypmods[i] = exprTypmod(expr);
 			atttypcolls[i] = exprCollation(expr);
-
-			/*
-			 * If it's a multirange, step down to the range type, as is done
-			 * by multirange_typanalyze().
-			 */
-			if (type_is_multirange(atttypids[i]))
-				atttypids[i] = get_multirange_range(atttypids[i]);
-
-			/*
-			 * Special case: collation for tsvector is DEFAULT_COLLATION_OID.
-			 * See compute_tsvector_stats().
-			 */
-			if (atttypids[i] == TSVECTOROID)
-				atttypcolls[i] = DEFAULT_COLLATION_OID;
-
 		}
 	}
 
@@ -1039,6 +1024,17 @@ import_expressions(Relation pgsd, int numexprs,
 		const int	most_common_elems_freqs_idx = offset + MOST_COMMON_ELEM_FREQS_ELEM;
 		const int	elem_count_histogram_idx = offset + ELEM_COUNT_HISTOGRAM_ELEM;
 
+		/*
+		 * XXX:
+		 *
+		 * This is doing something that we relied on happening in
+		 * statatt_get_type, but is currently disabled.
+		 *
+		 * The multirange step-down may also need to happen here too.
+		 */
+		if (typid == TSVECTOROID)
+			stacoll = DEFAULT_COLLATION_OID;
+
 		/* This finds the right operators even if atttypid is a domain */
 		typcache = lookup_type_cache(typid, TYPECACHE_LT_OPR | TYPECACHE_EQ_OPR);
 
diff --git a/src/backend/statistics/stat_utils.c b/src/backend/statistics/stat_utils.c
index 9c680f1cb37..b819236c319 100644
--- a/src/backend/statistics/stat_utils.c
+++ b/src/backend/statistics/stat_utils.c
@@ -434,6 +434,11 @@ stats_fill_fcinfo_from_arg_pairs(FunctionCallInfo pairs_fcinfo,
  * This information, retrieved from pg_attribute and pg_type with some
  * specific handling for index expressions, is a prerequisite to calling
  * any of the other statatt_*() functions.
+ *
+ * XXX:
+ * If any of atttyptype, eq_opr, or lt_opt are NULL, then do not attempt
+ * to derive that information. This has the secondary effect of not stepping
+ * down multiranges to their base range type.
  */
 void
 statatt_get_type(Oid reloid, AttrNumber attnum,
@@ -491,6 +496,14 @@ statatt_get_type(Oid reloid, AttrNumber attnum,
 	}
 	ReleaseSysCache(atup);
 
+	if (atttyptype == NULL
+		|| eq_opr == NULL
+		|| lt_opr == NULL)
+	{
+		relation_close(rel, NoLock);
+		return;
+	}
+
 	/*
 	 * If it's a multirange, step down to the range type, as is done by
 	 * multirange_typanalyze().
-- 
2.52.0

