Re: Add CHECK_FOR_INTERRUPTS() to XPath result serialisation

From: Kirill Reshke <reshkekirill(at)gmail(dot)com>
To: Jim Jones <jim(dot)jones(at)uni-muenster(dot)de>
Cc: PostgreSQL Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Add CHECK_FOR_INTERRUPTS() to XPath result serialisation
Date: 2026-11-25 05:05:42
Message-ID: CALdSSPhtuUagGrq0iMaYarBysho0_gfA1cFJZ_eUUs5zfBpyuw@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Tue, 24 Nov 2026 at 19:34, Jim Jones <jim(dot)jones(at)uni-muenster(dot)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 -
> https://www.postgresql.org/message-id/flat/d3270e9f-4f10-4d04-8209-87e513801b05(at)uni-muenster(dot)de
> 2-
> https://www.postgresql.org/message-id/flat/463aec1b-f833-43ca-a892-534f8b2dd65f%40uni-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

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Hayato Kuroda (Fujitsu) 2026-11-25 05:07:49 RE: Fix LogStandbySnapshot() race during logical decoding activation
Previous Message Ayush Tiwari 2026-11-25 04:54:02 Re: Small cleanup of leftover pre-1.1.0 OpenSSL code