Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1wRclq-002Wsc-11 for pgsql-bugs@arkaria.postgresql.org; Mon, 25 May 2026 21:23:34 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1wRcln-002AQb-2v for pgsql-bugs@arkaria.postgresql.org; Mon, 25 May 2026 21:23:32 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1wRcln-002AQT-26 for pgsql-bugs@lists.postgresql.org; Mon, 25 May 2026 21:23:32 +0000 Received: from sss.pgh.pa.us ([68.162.161.243]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1wRcll-00000000kyy-1aPU for pgsql-bugs@lists.postgresql.org; Mon, 25 May 2026 21:23:31 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.18.1/8.18.1) with ESMTP id 64PLNRhH154120; Mon, 25 May 2026 17:23:27 -0400 From: Tom Lane To: PetSerAl cc: a.prototype7@gmail.com, pgsql-bugs@lists.postgresql.org Subject: Re: BUG #19486: Regression in SQL-language functions using XML values and IS DOCUMENT In-reply-to: References: <19486-f1cbfe2bd1c9c3d9@postgresql.org> Comments: In-reply-to PetSerAl message dated "Thu, 21 May 2026 21:41:51 +0300" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <154118.1779744207.1@sss.pgh.pa.us> Date: Mon, 25 May 2026 17:23:27 -0400 Message-ID: <154119.1779744207@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk PetSerAl 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