Hi

While looking into other XML cancellation issues[1,2], I noticed that
xpath() and xmltable columns of type xml serialise every node of the
result node set without checking for interrupts. Each node is serialised
with its whple subtree, so for deeply nested documents this can take a
very long time, and during that time the backend ignores query cancel,
statement_timeout and pg_terminate_backend().

Example (with a relatively small xml doc):

create table t (doc xml);
insert into t values (('<r>' || repeat(repeat('<a>', 250) ||
repeat('</a>', 250), 4000) || '</r>')::xml);
set statement_timeout to '2s';

== master ==

postgres=# \timing on
Timing is on.
postgres=# select xpath('//a', doc) from t;
^CCancel request sent
^CCancel request sent
ERROR:  canceling statement due to statement timeout
Time: 18206,080 ms (00:18,206)

== patched ==

postgres=# \timing on
Timing is on.
postgres=# select xpath('//a', doc) from t;
ERROR:  canceling statement due to statement timeout
Time: 2092,112 ms (00:02,092)

0001 adds CHECK_FOR_INTERRUPTS() to the serialisation loops in xpath and
xmltable.

xpath_table() in xml2 has a similar issue. Since xml2 is deprecated, I
am not sure it's worth fixing. In case it is, 0002 adds
CHECK_FOR_INTERRUPTS() calls to its loops.

Thoughts?

Best, Jim

1 -
https://www.postgresql.org/message-id/flat/[email protected]
2-
https://www.postgresql.org/message-id/flat/463aec1b-f833-43ca-a892-534f8b2dd65f%40uni-muenster.de
From 0daa9f199003b9d20cd775c13646a11c212ccc89 Mon Sep 17 00:00:00 2001
From: Jim Jones <[email protected]>
Date: Tue, 24 Nov 2026 13:55:35 +0100
Subject: [PATCH v1] Allow cancelling XPath result serialization

xpath() and XMLTABLE columns of type xml serialize every node of the
result node set without checking for interrupts.  Each node is
serialized with its whole subtree, so this can take a long time for
deeply nested documents, during which the backend ignores query cancel,
statement_timeout and pg_terminate_backend().

Add CHECK_FOR_INTERRUPTS() to both loops.  They run in PostgreSQL code,
outside any libxml2 callback, and the existing PG_TRY blocks already
free the libxml2 objects on error.
---
 src/backend/utils/adt/xml.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/src/backend/utils/adt/xml.c b/src/backend/utils/adt/xml.c
index 44a3a21d135..88a2e358a15 100644
--- a/src/backend/utils/adt/xml.c
+++ b/src/backend/utils/adt/xml.c
@@ -4354,6 +4354,7 @@ xml_xpathobjtoxmlarray(xmlXPathObjectPtr xpathobj,
 
 					for (i = 0; i < result; i++)
 					{
+						CHECK_FOR_INTERRUPTS();
 						datum = PointerGetDatum(xml_xmlnodetoxmltype(xpathobj->nodesetval->nodeTab[i],
 																	 xmlerrcxt));
 						(void) accumArrayResult(astate, datum, false,
@@ -5079,6 +5080,7 @@ XmlTableGetValue(TableFuncScanState *state, int colnum,
 					initStringInfo(&str);
 					for (int i = 0; i < count; i++)
 					{
+						CHECK_FOR_INTERRUPTS();
 						textstr =
 							xml_xmlnodetoxmltype(xpathobj->nodesetval->nodeTab[i],
 												 xtCxt->xmlerrcxt);
-- 
2.56.1

From 31ba80d8fafa6746184b74adb2248b463b689293 Mon Sep 17 00:00:00 2001
From: Jim Jones <[email protected]>
Date: Tue, 24 Nov 2026 14:23:11 +0100
Subject: [PATCH v1] Allow cancelling xpath_table()

xpath_table() in contrib/xml2 loops over every row returned by its
query and, for each document, re-evaluates all XPath expressions once
per output row, without checking for interrupts.  A document with many
matching nodes can therefore keep the backend busy for a long time,
during which it ignores query cancel, statement_timeout and
pg_terminate_backend().

Add CHECK_FOR_INTERRUPTS() to both loops.  They run in PostgreSQL code,
outside any libxml2 callback, and the existing PG_TRY block already
frees the libxml2 objects on error.
---
 contrib/xml2/xpath.c | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/contrib/xml2/xpath.c b/contrib/xml2/xpath.c
index 4cb924541e3..cd4c0f6c04e 100644
--- a/contrib/xml2/xpath.c
+++ b/contrib/xml2/xpath.c
@@ -11,6 +11,7 @@
 #include "fmgr.h"
 #include "funcapi.h"
 #include "lib/stringinfo.h"
+#include "miscadmin.h"
 #include "utils/builtins.h"
 #include "utils/tuplestore.h"
 #include "utils/xml.h"
@@ -772,6 +773,8 @@ xpath_table(PG_FUNCTION_ARGS)
 			char	   *xmldoc;
 			HeapTuple	ret_tuple;
 
+			CHECK_FOR_INTERRUPTS();
+
 			/* Extract the row data as C Strings */
 			spi_tuple = tuptable->vals[i];
 			pkey = SPI_getvalue(spi_tuple, spi_tupdesc, 1);
@@ -810,6 +813,8 @@ xpath_table(PG_FUNCTION_ARGS)
 
 				do
 				{
+					CHECK_FOR_INTERRUPTS();
+
 					/* Now evaluate the set of xpaths. */
 					had_values = false;
 					for (j = 0; j < numpaths; j++)
-- 
2.56.1

Reply via email to