public inbox for [email protected]  
help / color / mirror / Atom feed
From: Tom Lane <[email protected]>
To: PetSerAl <[email protected]>
Cc: [email protected]
Cc: [email protected]
Subject: Re: BUG #19486: Regression in SQL-language functions using XML values and IS DOCUMENT
Date: Mon, 25 May 2026 17:23:27 -0400
Message-ID: <[email protected]> (raw)
In-Reply-To: <CAKygsHTkCSHzR82G5ePAJ3wQ7BP4Z=qaUymLEZYo8Pvo4+dYSw@mail.gmail.com>
References: <[email protected]>
	<CAKygsHTkCSHzR82G5ePAJ3wQ7BP4Z=qaUymLEZYo8Pvo4+dYSw@mail.gmail.com>

PetSerAl <[email protected]> writes:
>>   CREATE OR REPLACE FUNCTION xml_to_text_no_inline(pXml xml) RETURNS text
>>   LANGUAGE sql
>>   IMMUTABLE
>>   SET search_path = pg_catalog
>>   AS $$
>>   SELECT CASE WHEN pXml IS DOCUMENT
>>               THEN (xpath('/*/text()', pXml))[1]::text
>>               ELSE pXml::text
>>          END;
>>   $$;

> There is bug in that function. Expectation, that `xpath('/*/text()',
> pXml)` will be evaluate only after successful `pXml IS DOCUMENT`
> check, is not supported by documentation.

Yeah, CASE is not strong enough to prevent constant-folding in this
context.  You could try something like

    create or replace function xml_to_text(pXml xml) returns text
    as $$
    select
      coalesce(
        (xpath('/*/text()',
               case when pXml is document then pXml else null end))[1],
        pXml
      )::text;
    $$ language sql immutable;

This works because xpath() is strict so it won't try to do anything
with a NULL input, just return NULL; and then the COALESCE() serves
the purpose of injecting pXml when that happens.

			regards, tom lane






reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: [email protected]
  Cc: [email protected], [email protected], [email protected], [email protected]
  Subject: Re: BUG #19486: Regression in SQL-language functions using XML values and IS DOCUMENT
  In-Reply-To: <[email protected]>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox