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 1kXBNW-00061S-PK for pgsql-performance@arkaria.postgresql.org; Mon, 26 Oct 2020 22:54:14 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kXBNV-0000xn-1z for pgsql-performance@arkaria.postgresql.org; Mon, 26 Oct 2020 22:54:13 +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 1kXBNU-0000xO-R6 for pgsql-performance@lists.postgresql.org; Mon, 26 Oct 2020 22:54:12 +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 1kXBNS-0005Zf-NL for pgsql-performance@lists.postgresql.org; Mon, 26 Oct 2020 22:54:12 +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 09QMs1oO807005; Mon, 26 Oct 2020 18:54:01 -0400 From: Tom Lane To: David Rowley cc: "Ehrenreich, Sigrid" , "pgsql-performance@lists.postgresql.org" Subject: Re: Postgres Optimizer ignores information about foreign key relationship, severly misestimating number of returned rows in join In-reply-to: References: Comments: In-reply-to David Rowley message dated "Tue, 27 Oct 2020 11:25:45 +1300" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <807003.1603752841.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Mon, 26 Oct 2020 18:54:01 -0400 Message-ID: <807004.1603752841@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk David Rowley writes: > On Tue, 27 Oct 2020 at 06:54, Ehrenreich, Sigrid = wrote: >> -> Hash Join (cost=3D226.27..423.82 rows=3D115 width=3D0) (actual tim= e=3D3.150..7.511 rows=3D3344 loops=3D1) <=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= With the FK, the estimation should be 3344, but it is 115 rows > I'd have expected this to find the foreign key and have the join > selectivity of 1.0, but I see it does not due to the fact that one of > the EquivalenceClass has a constant due to the fact.low_card =3D 1 qual. Right. > I feel like we could probably do better there and perhaps somehow > count ECs with ec_has_const as matched, but there seems to be some > assumptions later in get_foreign_key_join_selectivity() where we > determine the selectivity based on the base rel's tuple count. We'd > need to account for how many rows remainder after filtering the ECs > with ec_has_const =3D=3D true, else we'd be doing the wrong thing. That > needs more thought than I have time for right now. Yeah, I'm fooling with a patch for that now. The basic problem is that the selectivity of the x =3D constant clauses has already been factored into the sizes of both join input relations, so we're double-counting it if we just apply the existing FK-based selectivity estimate. I think though that we can recover the selectivity associated with that qual on the FK side (or should it be the PK side?) and cancel it out of the FK selectivity. regards, tom lane