From 39102eed00fc1121773ab1e9cb970a4ae6f759d9 Mon Sep 17 00:00:00 2001
From: Jim Jones <jim.jones@uni-muenster.de>
Date: Fri, 27 Nov 2026 09:25:50 +0100
Subject: [PATCH v4 3/3] Look up element type information once when mapping
 arrays to XML

map_sql_value_to_xml_value() converts each element of an array by
calling itself, and every such call looks up the base type and the
output function of the element type again, although they are the same
for all elements.  Do these lookups once per array instead and pass
the results down to the conversion of each element.

Add tests for arrays of domains, including domains over array types,
whose elements are still converted recursively.

Suggested-by: Tom Lane <tgl@sss.pgh.pa.us>
Author: Jim Jones <jim.jones@uni-muenster.de>
Discussion: https://postgr.es/m/7581277e-e089-417d-867f-8ffd54b9e4c6@uni-muenster.de
---
 src/backend/utils/adt/xml.c       | 61 +++++++++++++++++++++++++++----
 src/test/regress/expected/xml.out | 16 ++++++++
 src/test/regress/sql/xml.sql      |  6 +++
 3 files changed, 76 insertions(+), 7 deletions(-)

diff --git a/src/backend/utils/adt/xml.c b/src/backend/utils/adt/xml.c
index e19ab446e6d..c60cb3dc26d 100644
--- a/src/backend/utils/adt/xml.c
+++ b/src/backend/utils/adt/xml.c
@@ -184,6 +184,9 @@ static const char *map_sql_catalog_to_xmlschema_types(List *nspid_list,
 static const char *map_sql_type_to_xml_name(Oid typeoid, int typmod);
 static const char *map_sql_typecoll_to_xmlschema_types(List *tupdesc_list);
 static const char *map_sql_type_to_xmlschema_type(Oid typeoid, int typmod);
+static char *map_sql_value_to_xml_value_internal(Datum value, Oid type,
+												 bool xml_escape_strings,
+												 FmgrInfo *typoutput);
 static void SPI_sql_row_to_xmlelement(uint64 rownum, StringInfo result,
 									  char *tablename, bool nulls, bool tableforest,
 									  const char *targetns, bool top_level);
@@ -2533,7 +2536,23 @@ map_xml_name_to_sql_identifier(const char *name)
 char *
 map_sql_value_to_xml_value(Datum value, Oid type, bool xml_escape_strings)
 {
-	if (type_is_array_domain(type))
+	return map_sql_value_to_xml_value_internal(value, type,
+											   xml_escape_strings, NULL);
+}
+
+/*
+ * Workhorse for map_sql_value_to_xml_value().
+ *
+ * If typoutput is not NULL, "type" must be a non-array base type and
+ * typoutput its output function.  The array case uses this to look them
+ * up only once per array instead of for every element.
+ */
+static char *
+map_sql_value_to_xml_value_internal(Datum value, Oid type,
+									bool xml_escape_strings,
+									FmgrInfo *typoutput)
+{
+	if (typoutput == NULL && type_is_array_domain(type))
 	{
 		ArrayType  *array;
 		Oid			elmtype;
@@ -2546,6 +2565,8 @@ map_sql_value_to_xml_value(Datum value, Oid type, bool xml_escape_strings)
 		StringInfoData buf;
 		MemoryContext elemcxt;
 		MemoryContext oldcxt;
+		FmgrInfo	elmoutput;
+		FmgrInfo   *elmoutputp = NULL;
 		int			i;
 
 		array = DatumGetArrayTypeP(value);
@@ -2557,6 +2578,24 @@ map_sql_value_to_xml_value(Datum value, Oid type, bool xml_escape_strings)
 						  &elem_values, &elem_nulls,
 						  &num_elems);
 
+		/*
+		 * Look up the element type's output function once for all elements,
+		 * unless the elements are themselves arrays (a domain over an array
+		 * type), which are converted recursively.  This must be done before
+		 * switching to the temporary context below, so that whatever the
+		 * output function caches in fn_extra survives each reset.
+		 */
+		if (!type_is_array_domain(elmtype))
+		{
+			Oid			typeOut;
+			bool		isvarlena;
+
+			elmtype = getBaseType(elmtype);
+			getTypeOutputInfo(elmtype, &typeOut, &isvarlena);
+			fmgr_info(typeOut, &elmoutput);
+			elmoutputp = &elmoutput;
+		}
+
 		initStringInfo(&buf);
 
 		/* Free per-element garbage, which can be large, after each element */
@@ -2573,8 +2612,9 @@ map_sql_value_to_xml_value(Datum value, Oid type, bool xml_escape_strings)
 				continue;
 			appendStringInfoString(&buf, "<element>");
 			appendStringInfoString(&buf,
-								   map_sql_value_to_xml_value(elem_values[i],
-															  elmtype, true));
+								   map_sql_value_to_xml_value_internal(elem_values[i],
+																	   elmtype, true,
+																	   elmoutputp));
 			appendStringInfoString(&buf, "</element>");
 
 			MemoryContextReset(elemcxt);
@@ -2597,7 +2637,8 @@ map_sql_value_to_xml_value(Datum value, Oid type, bool xml_escape_strings)
 		 * Flatten domains; the special-case treatments below should apply to,
 		 * eg, domains over boolean not just boolean.
 		 */
-		type = getBaseType(type);
+		if (typoutput == NULL)
+			type = getBaseType(type);
 
 		/*
 		 * Special XSD formatting for some data types
@@ -2741,10 +2782,16 @@ map_sql_value_to_xml_value(Datum value, Oid type, bool xml_escape_strings)
 		}
 
 		/*
-		 * otherwise, just use the type's native text representation
+		 * otherwise, just use the type's native text representation, through
+		 * the output function given by the caller if there is one
 		 */
-		getTypeOutputInfo(type, &typeOut, &isvarlena);
-		str = OidOutputFunctionCall(typeOut, value);
+		if (typoutput)
+			str = OutputFunctionCall(typoutput, value);
+		else
+		{
+			getTypeOutputInfo(type, &typeOut, &isvarlena);
+			str = OidOutputFunctionCall(typeOut, value);
+		}
 
 		/* ... exactly as-is for XML, and when escaping is not wanted */
 		if (type == XMLOID || !xml_escape_strings)
diff --git a/src/test/regress/expected/xml.out b/src/test/regress/expected/xml.out
index 6998745ee03..ee149303fcd 100644
--- a/src/test/regress/expected/xml.out
+++ b/src/test/regress/expected/xml.out
@@ -196,6 +196,22 @@ SELECT xmlelement(name foo, array[1, 2, 3]);
  <foo><element>1</element><element>2</element><element>3</element></foo>
 (1 row)
 
+-- arrays of domains: over a scalar type, and over an array type
+CREATE DOMAIN xmltest_bool AS bool;
+CREATE DOMAIN xmltest_intarr AS int[];
+SELECT xmlelement(name foo, array[true, false]::xmltest_bool[]);
+                         xmlelement                         
+------------------------------------------------------------
+ <foo><element>true</element><element>false</element></foo>
+(1 row)
+
+SELECT xmlelement(name foo, array['{1,2}', '{3}']::xmltest_intarr[]);
+                                                  xmlelement                                                   
+---------------------------------------------------------------------------------------------------------------
+ <foo><element><element>1</element><element>2</element></element><element><element>3</element></element></foo>
+(1 row)
+
+DROP DOMAIN xmltest_bool, xmltest_intarr;
 SET xmlbinary TO base64;
 SELECT xmlelement(name foo, bytea 'bar');
    xmlelement    
diff --git a/src/test/regress/sql/xml.sql b/src/test/regress/sql/xml.sql
index 0581d21b03f..c1c0c9783f0 100644
--- a/src/test/regress/sql/xml.sql
+++ b/src/test/regress/sql/xml.sql
@@ -63,6 +63,12 @@ SELECT xmlelement(name foo, xml 'bar');
 SELECT xmlelement(name foo, text 'b<a/>r');
 SELECT xmlelement(name foo, xml 'b<a/>r');
 SELECT xmlelement(name foo, array[1, 2, 3]);
+-- arrays of domains: over a scalar type, and over an array type
+CREATE DOMAIN xmltest_bool AS bool;
+CREATE DOMAIN xmltest_intarr AS int[];
+SELECT xmlelement(name foo, array[true, false]::xmltest_bool[]);
+SELECT xmlelement(name foo, array['{1,2}', '{3}']::xmltest_intarr[]);
+DROP DOMAIN xmltest_bool, xmltest_intarr;
 SET xmlbinary TO base64;
 SELECT xmlelement(name foo, bytea 'bar');
 SET xmlbinary TO hex;
-- 
2.56.1

