Hi Jakub & Daniel
Thanks for the reviews.
On 25/11/2026 14:12, Daniel Gustafsson wrote:
>> On 25 Nov 2026, at 14:01, Jakub Wartak <[email protected]> wrote:
>> +1 to fixing this. Patch LGMT, maybe just two tiny things:
>>
>> - perhaps move MemoryContext oldcxt to the loop
Done in v2-0001.
>> - "map_sql_value_to_xml_value temporary cxt" is a bit lengthy blob, maybe
>> "elements of XML array" would suffice there (or maybe check further wihtin
>> pg_backend_memory_contexts for better examples)
I kept the context label like this for now. My rationale is that it
makes it a bit easier to identify the context without looking into the
code.. let me know if there is a standard here.
> +1. Patch looks good on a first glance, expanding to the *_to_xml cases seems
> like a good idea.
Thanks.
0002 attached applies the same approach to the *to_xml functions.
== patched ==
psql (20devel)
Type "help" for help.
postgres=# select length(query_to_xml('SELECT g,g,g,g,g FROM
generate_series(1, 10000000) g', false, false, '')::text);
length
-----------
984444557
(1 row)
postgres=# create table t as select g a, g b, g c, g d, g e FROM
generate_series(1, 10000000) g;
SELECT 10000000
postgres=# SELECT length(table_to_xml('t', false, false, '')::text);
length
-----------
984444549
(1 row)
Best, JimFrom 71dadf32d6d97f86c7e6e6be90d20e7bdae1d51c Mon Sep 17 00:00:00 2001
From: Jim Jones <[email protected]>
Date: Wed, 25 Nov 2026 14:19:35 +0100
Subject: [PATCH v2 1/2] Fix memory growth when mapping arrays to XML
map_sql_value_to_xml_value() converts each array element in the
caller's memory context, so the intermediate data produced for every
element accumulates until the end of the statement. For large arrays
this can use far more memory than the result itself. The loop also
does not check for interrupts.
Convert each element in a temporary memory context that is reset after
every iteration, and add CHECK_FOR_INTERRUPTS() to the loop.
Author: Jim Jones <[email protected]>
Reviewed-by: Jakub Wartak <[email protected]>
Reviewed-by: Daniel Gustafsson <[email protected]>
---
src/backend/utils/adt/xml.c | 25 ++++++++++++++++++++++---
1 file changed, 22 insertions(+), 3 deletions(-)
diff --git a/src/backend/utils/adt/xml.c b/src/backend/utils/adt/xml.c
index 44a3a21d135..3fa050987fa 100644
--- a/src/backend/utils/adt/xml.c
+++ b/src/backend/utils/adt/xml.c
@@ -99,6 +99,7 @@
#include "utils/date.h"
#include "utils/datetime.h"
#include "utils/lsyscache.h"
+#include "utils/memutils.h"
#include "utils/rel.h"
#include "utils/syscache.h"
#include "utils/xml.h"
@@ -2543,6 +2544,7 @@ map_sql_value_to_xml_value(Datum value, Oid type, bool xml_escape_strings)
Datum *elem_values;
bool *elem_nulls;
StringInfoData buf;
+ MemoryContext elemcxt;
int i;
array = DatumGetArrayTypeP(value);
@@ -2556,17 +2558,34 @@ map_sql_value_to_xml_value(Datum value, Oid type, bool xml_escape_strings)
initStringInfo(&buf);
+ /* Free per-element garbage, which can be large, after each element */
+ elemcxt = AllocSetContextCreate(CurrentMemoryContext,
+ "map_sql_value_to_xml_value temporary cxt",
+ ALLOCSET_DEFAULT_SIZES);
+
for (i = 0; i < num_elems; i++)
{
+ char *elemstr;
+ MemoryContext oldcxt;
+
+ CHECK_FOR_INTERRUPTS();
+
if (elem_nulls[i])
continue;
+
+ oldcxt = MemoryContextSwitchTo(elemcxt);
+ elemstr = map_sql_value_to_xml_value(elem_values[i],
+ elmtype, true);
+ MemoryContextSwitchTo(oldcxt);
+
appendStringInfoString(&buf, "<element>");
- appendStringInfoString(&buf,
- map_sql_value_to_xml_value(elem_values[i],
- elmtype, true));
+ appendStringInfoString(&buf, elemstr);
appendStringInfoString(&buf, "</element>");
+
+ MemoryContextReset(elemcxt);
}
+ MemoryContextDelete(elemcxt);
pfree(elem_values);
pfree(elem_nulls);
--
2.56.1
From c3df92e4435861eb24a3ea2d11861f3f44b5bba4 Mon Sep 17 00:00:00 2001
From: Jim Jones <[email protected]>
Date: Wed, 25 Nov 2026 14:07:56 +0100
Subject: [PATCH v2 2/2] Fix memory growth in the *_to_xml() functions
query_to_xml(), table_to_xml(), cursor_to_xml() and the functions built
on them convert every row of the query result in the same memory
context, so the intermediate data produced for each row accumulates
until all rows have been processed. For large results this can use far
more memory than the result itself. The row loop also does not check
for interrupts.
Convert each row in a temporary memory context that is reset after
every iteration, and add CHECK_FOR_INTERRUPTS() to the loop.
---
src/backend/utils/adt/xml.c | 35 +++++++++++++++++++++++++++++++++++
1 file changed, 35 insertions(+)
diff --git a/src/backend/utils/adt/xml.c b/src/backend/utils/adt/xml.c
index 3fa050987fa..8687e3c2aed 100644
--- a/src/backend/utils/adt/xml.c
+++ b/src/backend/utils/adt/xml.c
@@ -2994,6 +2994,7 @@ cursor_to_xml(PG_FUNCTION_ARGS)
StringInfoData result;
Portal portal;
+ MemoryContext rowcxt;
uint64 i;
initStringInfo(&result);
@@ -3012,9 +3013,26 @@ cursor_to_xml(PG_FUNCTION_ARGS)
errmsg("cursor \"%s\" does not exist", name)));
SPI_cursor_fetch(portal, true, count);
+
+ rowcxt = AllocSetContextCreate(CurrentMemoryContext,
+ "cursor_to_xml temporary cxt",
+ ALLOCSET_DEFAULT_SIZES);
+
for (i = 0; i < SPI_processed; i++)
+ {
+ MemoryContext oldcxt;
+
+ CHECK_FOR_INTERRUPTS();
+
+ oldcxt = MemoryContextSwitchTo(rowcxt);
SPI_sql_row_to_xmlelement(i, &result, NULL, nulls,
tableforest, targetns, true);
+ MemoryContextSwitchTo(oldcxt);
+
+ MemoryContextReset(rowcxt);
+ }
+
+ MemoryContextDelete(rowcxt);
SPI_finish();
@@ -3078,6 +3096,7 @@ query_to_xml_internal(const char *query, char *tablename,
{
StringInfo result;
char *xmltn;
+ MemoryContext rowcxt;
uint64 i;
if (tablename)
@@ -3103,9 +3122,25 @@ query_to_xml_internal(const char *query, char *tablename,
if (xmlschema)
appendStringInfo(result, "%s\n\n", xmlschema);
+ rowcxt = AllocSetContextCreate(CurrentMemoryContext,
+ "query_to_xml_internal temporary cxt",
+ ALLOCSET_DEFAULT_SIZES);
+
for (i = 0; i < SPI_processed; i++)
+ {
+ MemoryContext oldcxt;
+
+ CHECK_FOR_INTERRUPTS();
+
+ oldcxt = MemoryContextSwitchTo(rowcxt);
SPI_sql_row_to_xmlelement(i, result, tablename, nulls,
tableforest, targetns, top_level);
+ MemoryContextSwitchTo(oldcxt);
+
+ MemoryContextReset(rowcxt);
+ }
+
+ MemoryContextDelete(rowcxt);
if (!tableforest)
xmldata_root_element_end(result, xmltn);
--
2.56.1