Received: from maia.hub.org (maia-3.hub.org [200.46.204.243]) by mail.postgresql.org (Postfix) with ESMTP id 04A9AB5D95B for ; Tue, 14 Jun 2011 13:29:15 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.243]) (amavisd-maia, port 10024) with ESMTP id 98587-06 for ; Tue, 14 Jun 2011 16:29:08 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by mail.postgresql.org (Postfix) with ESMTP id 7CE4BB5D80B for ; Tue, 14 Jun 2011 13:29:08 -0300 (ADT) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.2/8.14.2) with ESMTP id p5EGT5pZ000678; Tue, 14 Jun 2011 12:29:05 -0400 (EDT) To: Svetlin Manavski cc: pgsql-performance@postgresql.org Subject: Re: need to repeat the same condition on joined tables in order to choose the proper plan In-reply-to: References: Comments: In-reply-to Svetlin Manavski message dated "Tue, 14 Jun 2011 14:55:26 +0100" Date: Tue, 14 Jun 2011 12:29:05 -0400 Message-ID: <676.1308068945@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.91 tagged_above=-10 required=5 tests=BAYES_00=-1.9, T_RP_MATCHES_RCVD=-0.01 X-Spam-Level: X-Archive-Number: 201106/133 X-Sequence-Number: 44066 Svetlin Manavski writes: > I am really surprised to see that the planner needs me to explicitly specify > the same condition twice like this: > SD.detectorid = SS.detectorid > and SD.sessionid = SS.id > and SD.detectorid = 1 > and SD.sessionid >= 1001000000000::INT8 and SD.sessionid <= > 2001000000000::INT8 > and SS.detectorid = 1 > and SS.id >= 1001000000000::INT8 and SS.id <= 2001000000000::INT8 The planner does infer implied equalities, eg, given A = B and B = C it will figure out that A = C. What you are asking is for it to derive inequalities, eg infer A < C from A = B and B < C. That would be considerably more work for considerably less reward, since the sort of situation where this is helpful doesn't come up very often. On balance I don't believe it's a good thing for us to do: I think it would make PG slower on average because on most queries it would just waste time looking for this sort of situation. (In this example, the SS.detectorid = 1 clause is in fact unnecessary, since the planner will infer it from SD.detectorid = SS.detectorid and SD.detectorid = 1. But it won't infer the range conditions on SS.id from the range conditions on SD.sessionid or vice versa.) regards, tom lane