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 1rmjx7-009a0J-0B for pgsql-bugs@arkaria.postgresql.org; Wed, 20 Mar 2024 00:37:09 +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 1rmjx4-001LOS-8h for pgsql-bugs@arkaria.postgresql.org; Wed, 20 Mar 2024 00:37:06 +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.94.2) (envelope-from ) id 1rmjx4-001LOK-0U for pgsql-bugs@lists.postgresql.org; Wed, 20 Mar 2024 00:37:06 +0000 Received: from sss.pgh.pa.us ([68.162.161.243]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rmjx1-005SNm-GI for pgsql-bugs@lists.postgresql.org; Wed, 20 Mar 2024 00:37:05 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 42K0b1YA4085751; Tue, 19 Mar 2024 20:37:01 -0400 From: Tom Lane To: Sawyer Knoblich cc: pgsql-bugs@lists.postgresql.org Subject: Re: BUG #18399: Query plan optimization results in runtime error when hoisting cast from inside subquery In-reply-to: References: <18399-17778030a1971177@postgresql.org> <3877106.1710863246@sss.pgh.pa.us> Comments: In-reply-to Sawyer Knoblich message dated "Tue, 19 Mar 2024 16:11:58 -0700" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <4085749.1710895021.1@sss.pgh.pa.us> Date: Tue, 19 Mar 2024 20:37:01 -0400 Message-ID: <4085750.1710895021@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Sawyer Knoblich writes: > To make sure I'm understanding correctly, are you suggesting that this is > not a bug? The mention of "the cast to integer is presumed to be > side-effect-free, which isn't true if you're working with data where it > could throw errors" to me heavily implies that this is a faulty assumption > on behalf of the optimizer about what behaviors certain parts of the query > may have, but I didn't get the impression that this is being considered an > optimizer bug and is instead something that just needs to be worked around > or avoided (although if I'm not interpreting it right then definitely > please correct me). It is not an optimizer bug: the optimizer is doing what the function marking entitles it to. You could argue that it's a marking bug and no function/operator that can throw an error should be marked stable or immutable, because throwing an error is a kind of side-effect. But that would be a pretty useless answer, as it would nearly destroy our ability to optimize queries at all, since there's not all that many functions that can be guaranteed not to throw errors. In short, this state of affairs is an engineering compromise. It's not perfect, but it's not easy to see how to do better either. regards, tom lane