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.94.2) (envelope-from ) id 1uZCQz-0005yJ-4u for pgsql-bugs@arkaria.postgresql.org; Tue, 08 Jul 2025 17:48:49 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1uZCQw-00AVre-PX for pgsql-bugs@arkaria.postgresql.org; Tue, 08 Jul 2025 17:48:47 +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.94.2) (envelope-from ) id 1uZCQw-00AVrP-HR for pgsql-bugs@lists.postgresql.org; Tue, 08 Jul 2025 17:48:47 +0000 Received: from mail-oo1-xc33.google.com ([2607:f8b0:4864:20::c33]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1uZCQv-006G6Q-1b for pgsql-bugs@lists.postgresql.org; Tue, 08 Jul 2025 17:48:46 +0000 Received: by mail-oo1-xc33.google.com with SMTP id 006d021491bc7-611a7c617a7so1476523eaf.1 for ; Tue, 08 Jul 2025 10:48:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=j-davis-com.20230601.gappssmtp.com; s=20230601; t=1751996924; x=1752601724; darn=lists.postgresql.org; h=mime-version:user-agent:content-transfer-encoding:references :in-reply-to:date:cc:to:from:subject:message-id:from:to:cc:subject :date:message-id:reply-to; bh=dxH1zKv1A825VnrX7RVL33lKY9JudK2iNDRZ8b9EBtc=; b=gb7+uos96QxnvDDkIE3jmV5eWPPy33v+BYYPCcLgAb4YBWBbZvqJtTLiIP2HidxrEw 17aSJUNwSTcvLLSVn/pR8MiXcR55yWvgok+WPxd+6WbIxPtpzvetzDS81N1t8aGYLPwg CF0cJ3DBb8IJOpzFgnyrXrTs9dR++0pMrAWf/eqNwgtCym6Y7a8e6nfigSEXZz0hhB6B m3mbVcxoxEdbGK9JQbAAQhVWKrIkD7DOFAHusyU7nxOvAWTPwOIaDh/u/aLG7WRF7Cn5 WzwpMI0oVeoNy5TEiiGRCvjT2lvC6mlasXrz9ErdxZ53HbCiUNpbS3LD2jYF+XWDlXqH Ud1g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1751996924; x=1752601724; h=mime-version:user-agent:content-transfer-encoding:references :in-reply-to:date:cc:to:from:subject:message-id:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=dxH1zKv1A825VnrX7RVL33lKY9JudK2iNDRZ8b9EBtc=; b=CUcGwXy7PeAIHbV1Ck4Kx5SI0FXHB96MYL4rtd1l5PLk1xIyfjsUSO7IzFSDOrAn5Q Je2FbaulEs4/9L0trBWKuHBK8VkKmbG/sX13UEG2CcjQquTObA5yZaHq9ZOYyZ7yx+iX h4O72GLQznryMxK+5zH7F3ozaB7QZX7q99nyG+4kH6N2rK2x89zkbt0/sPy2HVHQxpRJ URAdD0LrsMKVbHK7RXlkRazIGgCA2ncUZmCku9TQyqNVOkasONFFZ0uxM+CUkw1wDn+t YRywbbY/eaMxRRDs6qQk3n9xab+oB3SLAOb+XDb6wNZ3zd4qVXoLKdT+5gF6NHYNUczr lOjg== X-Gm-Message-State: AOJu0Yy6I8cVl/9TT1cW7a0IIiT6xNQuGsFzbesiOB5F2UJg9xgj7ABg odJwUpiLXk7/BqlW/jH+3UJS2/gRXmS13AV3X3o8X/MCaCyxupyWrsRZLKjd+Yezrv/F6KIjbDg NMI0= X-Gm-Gg: ASbGncusGAu8OyrEemwRcQMvNzBUKPdcGhhXEW5tQk3nSUGUNLoSix25FZvKqhB9nhr bU75arAFrhABcRZFpMd2QEnqe2N/AQQ51OrKZ8vr1i5hDBLBen8BGJW8PPOTprUUEce4lKZtODS mDzaCr8IY/O9+A5M7oYuKpuMMy9/uTuGAV0rnO9mapURN4nhr+U0d7PcOIb8soHLMReNVoP5Ohq tZrkZ+vJ4syLfSOa6U7AWAqZ7VzwwzxWbBDaaNTYyOzlpfs0X4l2oEKdaIsqyScnlWFsGsYfISR 1gvTYum3hCY8u5MfTEyvAoDjT+cgr6+mCsbpthXGH+xu3wENV4B3KrT8qOqUZJD9sWdQj4YGrgt 87BkCfPKibAKBadOrWETNl8whmHlR0vd4 X-Google-Smtp-Source: AGHT+IEjH7a0sjJOczows9D+1sVFg9xolEuoI226NAkpG7sMEvbEo9+O2S291Z3dNS3rXJGXcVNvDg== X-Received: by 2002:a05:6820:a10e:b0:613:c0fa:ccd9 with SMTP id 006d021491bc7-613c0facd4amr2613465eaf.0.1751996924452; Tue, 08 Jul 2025 10:48:44 -0700 (PDT) Received: from jeff-ws-bridge.lan (c-76-102-242-158.hsd1.ca.comcast.net. [76.102.242.158]) by smtp.gmail.com with ESMTPSA id 006d021491bc7-6138e4ad424sm1691000eaf.2.2025.07.08.10.48.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Jul 2025 10:48:44 -0700 (PDT) Message-ID: <5ecbdb70a74b1101aba33cd6c03638c0244759c8.camel@j-davis.com> Subject: Re: BUG #18965: Issue with Short-Circuit Evaluation in Boolean Expressions From: Jeff Davis To: Tom Lane , brandystodd@gmail.com Cc: pgsql-bugs@lists.postgresql.org Date: Tue, 08 Jul 2025 10:48:43 -0700 In-Reply-To: <2145178.1750444630@sss.pgh.pa.us> References: <18965-89456ea7e640a053@postgresql.org> <2145178.1750444630@sss.pgh.pa.us> Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable User-Agent: Evolution 3.52.3-0ubuntu1 MIME-Version: 1.0 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Fri, 2025-06-20 at 14:37 -0400, Tom Lane wrote: > I think the fundamental problem you're having is that you marked > this function IMMUTABLE, which gives the planner license to > pre-evaluate it.=C2=A0 It had better be VOLATILE to discourage advance > evaluation. >=20 > https://www.postgresql.org/docs/current/xfunc-volatility.html What you say is true in the narrow sense that VOLATILE prevents planner-time evaluation. But it doesn't generalize into a mathematical rule about how to mark a function: one might conclude (falsely) that=C2=A0IMMUTABLE functions must not be able to throw exceptions, but clearly they can. For instance, int4div(): EXPLAIN SELECT * FROM a WHERE (1/1 =3D 0); QUERY PLAN =20 ------------------------------------------ Result (cost=3D0.00..0.00 rows=3D0 width=3D0) One-Time Filter: false (2 rows) EXPLAIN SELECT * FROM a WHERE (1/0 =3D 0); ERROR: division by zero If you try to fix that by marking int4div as VOLATILE, you'd have to do the same for int4pl (which can overflow), and any function that can run out of memory. The way I see it, the problem is not the function marking, nor is it the evaluation order. It's that there's an expression in the query that's impossible to evaluate. Todd, you mentioned that you started from a more complex scenario, can you give some more details about how such an expression ended up in the original query, and what you'd like to happen? Regards, Jeff Davis