Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1b9Jwn-00075l-FO for pgsql-hackers@arkaria.postgresql.org; Sat, 04 Jun 2016 22:21:37 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1b9Jwm-0001YB-G0 for pgsql-hackers@arkaria.postgresql.org; Sat, 04 Jun 2016 22:21:36 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1b9Jwk-0001XB-2j for pgsql-hackers@postgresql.org; Sat, 04 Jun 2016 22:21:34 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1b9Jwd-0005Sz-0H for pgsql-hackers@postgresql.org; Sat, 04 Jun 2016 22:21:32 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id u54MLKsG004252; Sat, 4 Jun 2016 18:21:20 -0400 From: Tom Lane To: David Rowley cc: Tomas Vondra , Julien Rouhaud , Simon Riggs , Michael Paquier , Stefan Huehner , PostgreSQL mailing lists Subject: Re: pg9.6 segfault using simple query (related to use fk for join estimates) In-reply-to: References: <20160429102531.GA13701@huehner.biz> <572384D7.1020602@dalibo.com> <17639.1461945908@sss.pgh.pa.us> <57238E64.8010702@dalibo.com> <22731.1462037712@sss.pgh.pa.us> <8344835e-18af-9d40-aed7-bd2261be9162@2ndquadrant.com> <16807.1462388069@sss.pgh.pa.us> Comments: In-reply-to David Rowley message dated "Thu, 05 May 2016 16:04:58 +1200" Date: Sat, 04 Jun 2016 18:21:20 -0400 Message-ID: <4250.1465078880@sss.pgh.pa.us> X-Pg-Spam-Score: -3.3 (---) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-hackers Precedence: bulk Sender: pgsql-hackers-owner@postgresql.org David Rowley writes: > I think your wires are crossed to what this patch actually does. A > unique index could only prove that no more than 1 rows exists. This > goes to prove that exactly 1 exists, then will reduce that estimate by > any other join conditions which were not matched to a foreign key. BTW, I thought some more about this, and I believe the patch is a few bricks shy of a load in this respect. An FK constraint will enforce that a referencing row matches exactly one referenced row only if all the referencing columns are marked NOT NULL. If any nulls are allowed, then we are back to the unique-index situation, ie we can only conclude that there is at most one matching row. I do not think this means that we must dial the patch back to only considering FKs that have NOT NULL on all their columns. If we could estimate the fraction of referencing rows that have any nulls, we could still arrive at a selectivity estimate that's better than we get when disregarding the FK altogether: instead of 1/num_referenced_rows it'd be fraction-of-referencing-rows-without-nulls/num_referenced_rows, since the rows containing nulls are guaranteed to have 0 matches rather than 1. However, since the statistics we have at hand only tell us the fraction of nulls in each column separately, making a fraction-with-any-nulls estimate for a multi-column FK is going to be pretty spongy. regards, tom lane -- Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-hackers