Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1b9yVv-0006XQ-KN for pgsql-hackers@arkaria.postgresql.org; Mon, 06 Jun 2016 17:40:35 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1b9yVu-0003Q7-Hp for pgsql-hackers@arkaria.postgresql.org; Mon, 06 Jun 2016 17:40:34 +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 1b9yVt-0003Q0-PO for pgsql-hackers@postgresql.org; Mon, 06 Jun 2016 17:40:33 +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 1b9yVm-0006QY-Sn for pgsql-hackers@postgresql.org; Mon, 06 Jun 2016 17:40: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 u56HeJ0P026524; Mon, 6 Jun 2016 13:40:19 -0400 From: Tom Lane To: Tomas Vondra cc: Robert Haas , Noah Misch , Simon Riggs , David Rowley , Julien Rouhaud , 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: <57238E64.8010702@dalibo.com> <22731.1462037712@sss.pgh.pa.us> <8344835e-18af-9d40-aed7-bd2261be9162@2ndquadrant.com> <16807.1462388069@sss.pgh.pa.us> <20160529053601.GE496776@tornado.leadboat.com> <20160602012954.GA619629@tornado.leadboat.com> <28109.1465064127@sss.pgh.pa.us> <039fca3d-890a-d09a-e0df-e045187eb69e@2ndquadrant.com> <23385.1465229755@sss.pgh.pa.us> Comments: In-reply-to Tomas Vondra message dated "Mon, 06 Jun 2016 19:10:39 +0200" Date: Mon, 06 Jun 2016 13:40:19 -0400 Message-ID: <26523.1465234819@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 Tomas Vondra writes: > On 06/06/2016 06:15 PM, Tom Lane wrote: >> This checks that you found a joinclause mentioning foreignrel. But >> foreignrel need have nothing to do with the foreign key; it could be any >> table in the query. > I don't follow. How could it have 'nothing to do with the foreign key'? Precisely that: clauselist_join_selectivity iterates over every table in the join as a potential foreignrel, and you explicitly refuse to check that that table has anything to do with the foreign key's referenced side. Here's an example: drop table if exists t1, t2, t3; create table t1(f1 int, f2 int, primary key(f1,f2)); insert into t1 select x,x from generate_series(1,100000) x; create table t2 (f1 int, f2 int, foreign key(f1,f2) references t1); insert into t2 select (x+10)/10,(x+10)/10 from generate_series(1,100000) x; create table t3(f1 int, f2 int); insert into t3 select (x+10)/10,(x+10)/10 from generate_series(1,100000) x; analyze t1; analyze t2; analyze t3; explain select * from t1 join t2 on t1.f1=t2.f1 and t1.f2=t2.f2; explain select * from t3 join t2 on t3.f1=t2.f1 and t3.f2=t2.f2; 9.5 estimates the first query as producing 1 row, the second as producing 100 rows. Both of those estimates suck, of course, but it's what you'd expect from treating the joinclauses as uncorrelated. HEAD estimates them both at 100000 rows, which is correct for the first query but a pure flight of fancy for the second query. Tracing through this shows that it's accepting t2's FK as a reason to make the estimate, even though t1 doesn't even appear in that query! If we made the modifications previously discussed to throw away FKs that don't connect two tables mentioned in the query, this particular example would stop failing. But it would only take a three-table query involving t1, t2, and t3 to bring the bug back to life, whether or not the join conditions actually match the FK. 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