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 1wQ8LT-001JKY-1i for pgsql-bugs@arkaria.postgresql.org; Thu, 21 May 2026 18:42:11 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1wQ8LR-00Arpc-1l for pgsql-bugs@arkaria.postgresql.org; Thu, 21 May 2026 18:42:10 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1wQ8LR-00ArpU-0y for pgsql-bugs@lists.postgresql.org; Thu, 21 May 2026 18:42:10 +0000 Received: from mail-oa1-x2c.google.com ([2001:4860:4864:20::2c]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1wQ8LQ-00000000lxK-007i for pgsql-bugs@lists.postgresql.org; Thu, 21 May 2026 18:42:09 +0000 Received: by mail-oa1-x2c.google.com with SMTP id 586e51a60fabf-43496e6a964so4791165fac.2 for ; Thu, 21 May 2026 11:42:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1779388921; cv=none; d=google.com; s=arc-20240605; b=MMD5wDTl8lgnRFnzS4/D5plr9aJYPp+Jbn9ZDtlUZNB551nBZQbpz4VtDO3nSNhO9F t5dlv9Ebueu3RhA4Z8fuLTKS7Gk+edlc3KTudVYOxdR8n26uNEktT5PmZ/iXMsVPo+QR eCgs+jk4EympSNQmCK7qXTax4wZQQBOJD89k4QtOilmTmDTmyKfGrdRND0WgbZ+IXz/G 2KovM32JsTIaZdntfgAmwj5aRTFR0cR/eFyZVGefJIGx2bvV2SBxoSqjZeKgpE23/a2D RiLVv0kU+DhqyKmZz0VmgnAVJNYA3d2bNKnZ2IVXODS8kkz0Z6z8VlP6IxPfZ4SaLvti blTw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=to:subject:message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=2fSIGl6OSW387xz7buxd2Zc/GadbyTHWsOoJZH6+wH8=; fh=OLV6OgT7oivS5Yoa1/nbgUzIzG2CxYYMBiL9ddYgY9M=; b=brDRZQlFMdiWLba+YxpE85mMuX4Njllf0G9NeJeGtcxRKjFBjedanSDcmAaSrlMKOr f0OGX83JFTkWy9a6tFzdPiOtpeW795Dw7VW4lIoeUlDHr0Y9pvxAxAP/zCTeFOachpfZ zW97ndc2nd65ZBDkaUFT0R9RE5yserxN8BkWpGBosZN7aubY6X/98Ns6h7w8veZlGJgM YJFz5scdFkm1l/EcyiPZOBNRlShd6Gxy9V6xn+tVOH21CfojEkk5rjbZlMmVMt5Scn1k DbBxHtxsWkDZlwVAtl/XkexiMjIee0iVWH7+H1L4OGwhI2W8QeBrrWE6o4ZoFqLfp/9I hmLw==; darn=lists.postgresql.org ARC-Authentication-Results: i=1; mx.google.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1779388921; x=1779993721; darn=lists.postgresql.org; h=to:subject:message-id:date:from:in-reply-to:references:mime-version :from:to:cc:subject:date:message-id:reply-to; bh=2fSIGl6OSW387xz7buxd2Zc/GadbyTHWsOoJZH6+wH8=; b=c+N/k5l5tV/MfRnz9WgtvRMMqCIoC/n0UyY63wrJokLgh9WNw6XeDA7ZItdnzpadt0 TC6PwEZMpDlJbxBghsNb4JgHW+j+LCIdJZ+DdIu4r6vToRJgpLx0sZR2xJeDX+yJAL/x aCix1GaAaND1K207QMTVXIJePqvN+zTdUVPFc3oGJFjzsqV3ECA2fmupc395M35oLAcM ElINz7XiwqibzMp8WhUup06mE92B6ysHM7mFU4a0GfLL1SWe1UcTA/fDMpmkCUNC3Gn3 jaK7eZrw1J8wuhxF84PsCL0nIIht9kNuze/vyU7umcFIzgeKcXMLh3LsUdQjs4BtkrQ9 Hvkw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1779388921; x=1779993721; h=to:subject:message-id:date:from:in-reply-to:references:mime-version :x-gm-gg:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=2fSIGl6OSW387xz7buxd2Zc/GadbyTHWsOoJZH6+wH8=; b=XZ4yI1OA/aBh7HAoQ+0CcpDxK0rkol7xBkyl9ZpTtk/VmbjkMX/rM4t+WL+fhvy/Mx aLARGF/rKdUohV4XOymOujaTRxSSCI4KLe5FF+x8epRKXZT0SYkaW6zkAFRGWS/8hnK5 ZtpnZV5gFUXU5jemVz30gMLXwD2v/7wICckt2Lz+0naN2moQMkzpXjpIhNztmk/RFUkr nDXL58q83ZQPiM7/9uTd/kfE+zYWhjRsrJxlRQ8RWaB1XDmAWBS7Is4wb81y3aJREVNi OjOom5Z4LTCNEeLHS8qbJUSiS12/0DO2WoAPOlLpNvl46L5GYYWktWxlKSuYZxGUuwvL un3Q== X-Forwarded-Encrypted: i=1; AFNElJ84OFkaXwW+75E2palcCqFVC8c0kXarx4XGFNe3DQ2Pnjo4yg2CSioXdN+GiXo0BEEO8TCFOaQBUHI8@lists.postgresql.org X-Gm-Message-State: AOJu0YzhxfvImShPI35mDg5wZavhZbvjqlAcjX3Gkkl0UAPVwFeiDE8e CCfzFiJiLAy0SrZTX5tTpvZV+bl1vYn213q4Up79RzyMRbhl2r1LnQmMCsyS4zrHX7JuRbhZPNT 7jIA6amEOjUaoWz+SAoMnCWZYsfHNUDM= X-Gm-Gg: Acq92OE7LBBVA4TokiZmXhtXTIyTwyFQzZMBzNF/U4pklrG3AXyKseoHPDc3vDFq9zb J256EivKqufjSgZRNP/jKSMSYQ/8h3LxFS/TM9E0oUwrLM8DUnQBinrQUH0U5sBAkRExgBUrniP tXepr9PIbpVM95ARxLtck9F29rGcUGmIPIhb5UJv5EvT+WMu/Ql5ekbxR65vMh9GIFW+qPRQuJa kQxjYFnMcn/pTQUaDV7fD3AL9k/aaPJa1J98ayHqOvv2Cer3F+3/y526OksKIZqUESgWfgy2JNQ 0gFUJ/VJ X-Received: by 2002:a05:6870:a48f:b0:36e:8381:db00 with SMTP id 586e51a60fabf-43b5aa4536fmr166562fac.9.1779388921209; Thu, 21 May 2026 11:42:01 -0700 (PDT) MIME-Version: 1.0 References: <19486-f1cbfe2bd1c9c3d9@postgresql.org> In-Reply-To: <19486-f1cbfe2bd1c9c3d9@postgresql.org> From: PetSerAl Date: Thu, 21 May 2026 21:41:51 +0300 X-Gm-Features: AVHnY4I2vS1j-_qkysZeatifSHYvUA0aEnMUcefFnFrkccEimvih7qZmmZK5aiU Message-ID: Subject: Re: BUG #19486: Regression in SQL-language functions using XML values and IS DOCUMENT To: a.prototype7@gmail.com, pgsql-bugs@lists.postgresql.org Content-Type: text/plain; charset="UTF-8" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk > 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. https://www.postgresql.org/docs/current/sql-expressions.html#SYNTAX-EXPRESS-EVAL The order of evaluation of subexpressions is not defined. In particular, the inputs of an operator or function are not necessarily evaluated left-to-right or in any other fixed order. ... When it is essential to force evaluation order, a `CASE` construct (see Section 9.18) can be used. ... `CASE` is not a cure-all for such issues, however. One limitation of the technique illustrated above is that it does not prevent early evaluation of constant subexpressions. As described in Section 36.7, functions and operators marked `IMMUTABLE` can be evaluated when the query is planned rather than when it is executed. Thus for example ``` SELECT CASE WHEN x > 0 THEN x ELSE 1/0 END FROM tab; ``` is likely to result in a division-by-zero failure due to the planner trying to simplify the constant subexpression, even if every row in the table has `x > 0` so that the `ELSE` arm would never be entered at run time. While that particular example might seem silly, related cases that don't obviously involve constants can occur in queries executed within functions, since the values of function arguments and local variables can be inserted into queries as constants for planning purposes. Within PL/pgSQL functions, for example, using an `IF`-`THEN`-`ELSE` statement to protect a risky computation is much safer than just nesting it in a `CASE` expression.