Add CHECK_FOR_INTERRUPTS() to XPath result serialisation
Thread notes
1 note#bug
hackorum-bot#bug
@global #bug
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 -
/messages/by-id/d3270e9f-4f10-4d04-8209-87e513801b05@uni-muenster.de
2-
/messages/by-id/463aec1b-f833-43ca-a892-534f8b2dd65f@uni-muenster.de
Attachments:
v1-0001-Allow-cancelling-XPath-result-serialization.patchtext/x-patch; charset=UTF-8; name=v1-0001-Allow-cancelling-XPath-result-serialization.patchDownload+2-1
v1-0001-Allow-cancelling-xpath_table.patchtext/x-patch; charset=UTF-8; name=v1-0001-Allow-cancelling-xpath_table.patchDownload+5-1
On Tue, 24 Nov 2026 at 19:34, Jim Jones <jim.jones@uni-muenster.de> wrote:
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 -
/messages/by-id/d3270e9f-4f10-4d04-8209-87e513801b05@uni-muenster.de
2-
/messages/by-id/463aec1b-f833-43ca-a892-534f8b2dd65f@uni-muenster.de
I think, if patched hunks are related to frequently-used XML features,
it's good to fix them. Otherwise it would bloat the codebase, huh?
Not much in context here, but it looks like places can be frequently hit.
Also, are there more places to cover? For example, can
escape_xml/xmlelement take long?
--
Best regards,
Kirill Reshke
Hi Kirill
On 25/11/2026 06:05, Kirill Reshke wrote:
I think, if patched hunks are related to frequently-used XML features,
it's good to fix them. Otherwise it would bloat the codebase, huh?
The overhead here is negligible. At least I was unable to measure
anything significant.
create table t (doc xml);
insert into t values (('<r>' || repeat(repeat('<a>', 250) ||
repeat('</a>', 250), 4000) || '</r>')::xml);
== patched ==
postgres=# select xpath('//a', doc) is null from t;
?column?
----------
f
(1 row)
Time: 13281,557 ms (00:13,282)
== master ==
postgres=# \timing on
Timing is on.
postgres=# select xpath('//a', doc) is null from t;
?column?
----------
f
(1 row)
Time: 13260,056 ms (00:13,260)
Not much in context here, but it looks like places can be frequently hit.
Also, are there more places to cover? For example, can
escape_xml/xmlelement take long?
I'd argue that it does not apply to escape_xml. In this case, it's a
linear pass over a single string, and in case the input is too large,
the 1GB limit kicks in:
postgres=# \timing on
Timing is on.
postgres=# select length(xmlelement(name a, repeat('&',
1000000000))::text);
ERROR: string buffer exceeds maximum allowed length (1073741823 bytes)
DETAIL: Cannot enlarge string buffer containing 1073741820 bytes by 5
more bytes.
Time: 5462,958 ms (00:05,463)
Arrays passed to xmlelement are a different story, since
map_sql_value_to_xml_value() loops over every element. While testing it,
I managed to get the backend killed (OOM) with a large array.. but I
suggest we look into it in a different thread, since it needs more than
a CHECK_FOR_INTERRUPTS().
WDYT?
Thanks for the review!
Best, Jim
On 25 Nov 2026, at 09:15, Jim Jones <jim.jones@uni-muenster.de> wrote:
On 25/11/2026 06:05, Kirill Reshke wrote:
I think, if patched hunks are related to frequently-used XML features,
it's good to fix them. Otherwise it would bloat the codebase, huh?The overhead here is negligible. At least I was unable to measure
anything significant.
Adding CFI here will have an unmeasurable impact on any realistic workload, so
I wouldn't worry about that. The patch LGTM, being able to cancel long-running
operations is important.
--
Daniel Gustafsson