Re: BUG #19486: Regression in SQL-language functions using XML values and IS DOCUMENT

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: PetSerAl <petseral(at)gmail(dot)com>
Cc: a(dot)prototype7(at)gmail(dot)com, pgsql-bugs(at)lists(dot)postgresql(dot)org
Subject: Re: BUG #19486: Regression in SQL-language functions using XML values and IS DOCUMENT
Date: 2026-05-25 21:23:27
Message-ID: 154119.1779744207@sss.pgh.pa.us
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

PetSerAl <petseral(at)gmail(dot)com> 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

In response to

Browse pgsql-bugs by date

  From Date Subject
Next Message Tom Lane 2026-05-25 22:17:25 Re: BUG #19492: intarray: fix variable stats leak in _int_matchsel
Previous Message Alexander Korotkov 2026-05-25 19:26:51 Re: BUG #19435: Error: "No relation entry for relid 2" Triggered by Complex Join with Self-Referencing Tables