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 1v04zQ-00AOn3-L3 for pgsql-bugs@arkaria.postgresql.org; Sat, 20 Sep 2025 21:19:28 +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 1v04zO-00DtLB-Ij for pgsql-bugs@arkaria.postgresql.org; Sat, 20 Sep 2025 21:19:26 +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 1v04zO-00DtL3-A5 for pgsql-bugs@lists.postgresql.org; Sat, 20 Sep 2025 21:19:26 +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.96) (envelope-from ) id 1v04zL-001UoS-2z for pgsql-bugs@lists.postgresql.org; Sat, 20 Sep 2025 21:19:25 +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 58KLJMDM2510593; Sat, 20 Sep 2025 17:19:22 -0400 From: Tom Lane To: jinhui.lai@qq.com cc: pgsql-bugs@lists.postgresql.org Subject: Re: BUG #19059: PostgreSQL fails to evaluate the cheaper expression first, leading to 45X performance degradation In-reply-to: <19059-c751eed385314309@postgresql.org> References: <19059-c751eed385314309@postgresql.org> Comments: In-reply-to PG Bug reporting form message dated "Sat, 20 Sep 2025 16:08:11 -0000" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <2510591.1758403162.1@sss.pgh.pa.us> Date: Sat, 20 Sep 2025 17:19:22 -0400 Message-ID: <2510592.1758403162@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk PG Bug reporting form writes: > However, the optimizer fails to reorder the expressions in the WHERE clause > for the second query. You can observe this from the second row in the plan: > "Filter: (EXISTS(SubPlan 1) OR (c0 > 0))" > A more optimal strategy would be for PG to use its cost model to reorder > expressions, prioritizing the evaluation of less expensive operations first. We do do that at the top AND level (cf. order_qual_clauses()) but we have not bothered for OR clauses. The whole exercise is pretty questionable really, considering how weak our cost model for expressions is. We could easily end up pessimizing a clause that the user had put into carefully-selected order. While we've not gotten a huge amount of push-back about the top-level re-ordering, there's been some complaints. So I'm not eager to propagate the idea further down. regards, tom lane