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 1okXni-0007E9-0F for pgsql-hackers@arkaria.postgresql.org; Mon, 17 Oct 2022 21:37:34 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1okXnf-0003Pp-ST for pgsql-hackers@arkaria.postgresql.org; Mon, 17 Oct 2022 21:37:31 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1okXnf-0003Pf-Hq for pgsql-hackers@lists.postgresql.org; Mon, 17 Oct 2022 21:37:31 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1okXnY-00049A-N8 for pgsql-hackers@lists.postgresql.org; Mon, 17 Oct 2022 21:37:30 +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 29HLbNrq2503889 for ; Mon, 17 Oct 2022 17:37:23 -0400 From: Tom Lane To: pgsql-hackers@lists.postgresql.org Subject: havingQual vs hasHavingQual buglets MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----- =_aaaaaaaaaa0" Content-ID: <2503859.1666042612.0@sss.pgh.pa.us> Date: Mon, 17 Oct 2022 17:37:23 -0400 Message-ID: <2503888.1666042643@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk ------- =_aaaaaaaaaa0 Content-Type: text/plain; charset="us-ascii" Content-ID: <2503859.1666042612.1@sss.pgh.pa.us> I came across a couple of places in the planner that are checking for nonempty havingQual; but since these bits run after const-simplification of the HAVING clause, that produces the wrong answer for a constant-true HAVING clause (which'll be folded to empty). Correct code is to check root->hasHavingQual instead. These mistakes only affect cost estimates, and they're sufficiently corner cases that it'd be hard even to devise a reliable test case showing a different plan choice. So I'm not very excited about this, and am thinking of committing only to HEAD. regards, tom lane ------- =_aaaaaaaaaa0 Content-Type: text/x-diff; name="fix-hasHavingQual-oversights.patch"; charset="us-ascii" Content-ID: <2503859.1666042612.2@sss.pgh.pa.us> Content-Description: fix-hasHavingQual-oversights.patch Content-Transfer-Encoding: quoted-printable diff --git a/contrib/postgres_fdw/postgres_fdw.c b/contrib/postgres_fdw/po= stgres_fdw.c index d98709e5e8..8d7500abfb 100644 --- a/contrib/postgres_fdw/postgres_fdw.c +++ b/contrib/postgres_fdw/postgres_fdw.c @@ -3357,7 +3357,7 @@ estimate_path_cost_size(PlannerInfo *root, * Get the retrieved_rows and rows estimates. If there are HAVING * quals, account for their selectivity. */ - if (root->parse->havingQual) + if (root->hasHavingQual) { /* Factor in the selectivity of the remotely-checked quals */ retrieved_rows =3D @@ -3405,7 +3405,7 @@ estimate_path_cost_size(PlannerInfo *root, run_cost +=3D cpu_tuple_cost * numGroups; = /* Account for the eval cost of HAVING quals, if any */ - if (root->parse->havingQual) + if (root->hasHavingQual) { QualCost remote_cost; = diff --git a/src/backend/optimizer/path/allpaths.c b/src/backend/optimizer= /path/allpaths.c index 8fc28007f5..4ddaed31a4 100644 --- a/src/backend/optimizer/path/allpaths.c +++ b/src/backend/optimizer/path/allpaths.c @@ -2575,7 +2575,7 @@ set_subquery_pathlist(PlannerInfo *root, RelOptInfo = *rel, if (parse->hasAggs || parse->groupClause || parse->groupingSets || - parse->havingQual || + root->hasHavingQual || parse->distinctClause || parse->sortClause || has_multiple_baserels(root)) ------- =_aaaaaaaaaa0--