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

From Tom Lane
Subject Re: BUG #19486: Regression in SQL-language functions using XML values and IS DOCUMENT
Date
Msg-id 154119.1779744207@sss.pgh.pa.us
Whole thread
In response to Re: BUG #19486: Regression in SQL-language functions using XML values and IS DOCUMENT  (PetSerAl <petseral@gmail.com>)
List pgsql-bugs
PetSerAl <petseral@gmail.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



pgsql-bugs by date:

Previous
From: Srinath Reddy Sadipiralla
Date:
Subject: Re: BUG #19491: Segmentation fault triggered by IS NULL
Next
From: Ayush Tiwari
Date:
Subject: Re: BUG #19490: Streaming standby on 16.14 stops applying WAL on MultiXactOffsetSLRU when primary is 16.8