Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jhHc4-0008PR-9l for pgsql-general@arkaria.postgresql.org; Fri, 05 Jun 2020 19:02:44 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1jhHc0-00036J-NT for pgsql-general@arkaria.postgresql.org; Fri, 05 Jun 2020 19:02:40 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jhHc0-00036B-F6 for pgsql-general@lists.postgresql.org; Fri, 05 Jun 2020 19:02:40 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jhHby-0005a9-3x for pgsql-general@postgresql.org; Fri, 05 Jun 2020 19:02:40 +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 055J2YuB1537862; Fri, 5 Jun 2020 15:02:34 -0400 From: Tom Lane To: Paul van der Linden cc: David Rowley , pgsql-general Subject: Re: Possible improvement In-reply-to: References: <1470860.1591367497@sss.pgh.pa.us> Comments: In-reply-to Paul van der Linden message dated "Fri, 05 Jun 2020 20:39:17 +0200" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <1537860.1591383754.1@sss.pgh.pa.us> Date: Fri, 05 Jun 2020 15:02:34 -0400 Message-ID: <1537861.1591383754@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Paul van der Linden writes: > For the case where it isn't known if the case expression itself is indexed, > technically that should be added as a decision-node in the query planner. That'd be fairly hard to do, if we're regarding this as an expression simplification step, since expression simplification is run long before any consideration is given to indexes. (Even if we were willing to contemplate reversing that ordering, it'd be hard to do, because we need the simplified expressions to compare to index expressions --- else we'd get fooled by irrelevant discrepancies that simplification is supposed to remove.) The alternative is to try to wire this into index path generation instead of treating it as a general-purpose expression simplification ... but that likewise seems pretty undesirable. If you've got a case like this, you'd like it to be simplified whether it ends up as an indexqual or not. So, as I said, I'm inclined to dismiss David's complaint as an impracticable requirement. The other issues I raised are far more significant. BTW, speaking of correctness, this seems like a pretty dire counterexample: SELECT ... FROM (SELECT CASE WHEN x = 0 THEN 'zero' WHEN 1/x > 100 THEN 'tiny' ELSE 'whatever' END AS class, ... ) ss WHERE ss.class = 'tiny'; Naive application of this transformation would convert the WHERE to WHERE 1/x > 100 creating divide-by-zero failures where there should be none. I'm not sure how we get around that; in general the planner has little clue which operations can throw what errors. regards, tom lane