Received: from maia.hub.org (maia-3.hub.org [200.46.204.243]) by mail.postgresql.org (Postfix) with ESMTP id DB3AF1337BCC for ; Wed, 13 Apr 2011 13:25:52 -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 02481-04-4 for ; Wed, 13 Apr 2011 16:25:17 +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 510391337BF2 for ; Wed, 13 Apr 2011 13:24: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 p3DGO6dq006362; Wed, 13 Apr 2011 12:24:06 -0400 (EDT) To: =?iso-8859-1?Q?V=E1clav_Ovs=EDk?= cc: pgsql-performance@postgresql.org Subject: Re: poor execution plan because column dependence In-reply-to: <20110413082139.GB24050@bobek.localdomain> References: <20110412232343.GA15966@bobek.localdomain> <20568.1302655935@sss.pgh.pa.us> <20110413082139.GB24050@bobek.localdomain> Comments: In-reply-to =?iso-8859-1?Q?V=E1clav_Ovs=EDk?= message dated "Wed, 13 Apr 2011 10:21:39 +0200" Date: Wed, 13 Apr 2011 12:24:06 -0400 Message-ID: <6361.1302711846@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: 201104/187 X-Sequence-Number: 43249 =?iso-8859-1?Q?V=E1clav_Ovs=EDk?= writes: > On Tue, Apr 12, 2011 at 08:52:15PM -0400, Tom Lane wrote: >> ... If you can change it, try replacing main.EffectiveId = main.id >> with the underlying function, eg if they're integers use >> int4eq(main.EffectiveId, main.id). This will bypass the overoptimistic >> estimator for the "=" operator and get you a default selectivity >> estimate of (IIRC) 0.3333. Which is still off, but only by 3x not 200x, >> and that should be close enough to get a decent plan. > Great idea! > Interesting the original index tickets5 is still used for > int4eq(main.effectiveid, main.id), no need to build a different. Well, no, it won't be. This hack is entirely dependent on the fact that the optimizer mostly works with operator expressions, and is blind to the fact that the underlying functions are really the same thing. (Which is something I'd like to see fixed someday, but in the meantime it gives you an escape hatch.) If you use the int4eq() construct in a context where you'd like to see it transformed into an index qual, it won't be. For this particular case that doesn't matter because there's no use in using an index for that clause anyway. But you'll need to be very careful that your changes in the query generator don't result in using int4eq() in any contexts other than the "main.EffectiveId=main.id" check. regards, tom lane