Received: from maia.hub.org (maia-5.hub.org [200.46.204.29]) by mail.postgresql.org (Postfix) with ESMTP id BFB82632C8B for ; Tue, 25 May 2010 16:02:02 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.29]) (amavisd-maia, port 10024) with ESMTP id 75811-08 for ; Tue, 25 May 2010 19:01:54 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from mail-vw0-f46.google.com (mail-vw0-f46.google.com [209.85.212.46]) by mail.postgresql.org (Postfix) with ESMTP id B987B63217C for ; Tue, 25 May 2010 16:01:54 -0300 (ADT) Received: by vws9 with SMTP id 9so3621639vws.19 for ; Tue, 25 May 2010 12:01:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:message-id:date:from:to:cc :subject:references:mime-version:content-type:content-disposition :in-reply-to:user-agent; bh=rSEp8yV3THpprZPmJ3/VmZTdxRtFTlV8725+us1zvGY=; b=eeU1bI+C21Uvk6VWxzxeB694321PXhTOQnGEBKS7y3PJusg7GBOlWicsel39Wv66wH IWDXYJsBZa4Z7SwjaWgTmROE8cWpaR+NfNraPjFoXkR6DpJejyx7tNBaI6iaCu+h1q3T yumzoFcUBg6SVUOPUskpJe66eMkV1+BTXtI0g= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=message-id:date:from:to:cc:subject:references:mime-version :content-type:content-disposition:in-reply-to:user-agent; b=a5IXT3O1PE15qDZM22iIXQQr95dhmunID26a0ry1uUtYt8n62lzYteIq1+9X4fo/SS Rxb/r5D+E3qThIBCkkaOfI4tIyT24aRUXcVp7/7uj2Sw97pdsGLPzgz1JcuXGr6uiviZ 3xi4yiqTt7tmXYgFalUow60SLQRbsqJZG6SVY= Received: by 10.220.107.5 with SMTP id z5mr5276024vco.243.1274814113712; Tue, 25 May 2010 12:01:53 -0700 (PDT) Received: from eddie (c-67-182-224-31.hsd1.ut.comcast.net [67.182.224.31]) by mx.google.com with ESMTPS id b22sm25158234vcp.20.2010.05.25.12.01.50 (version=TLSv1/SSLv3 cipher=RC4-MD5); Tue, 25 May 2010 12:01:52 -0700 (PDT) Message-ID: <4bfc1ea0.966bdc0a.4ba3.ffffb1d1@mx.google.com> Date: Tue, 25 May 2010 13:01:49 -0600 From: Joshua Tolley To: Scott Carey Cc: Matthew Wakeling , "pgsql-performance@postgresql.org" Subject: Re: prepared query performs much worse than regular query References: <0E6DA13D-D053-4CB8-A301-DC3607BBF146@richyen.com> <5AC1718A-C11D-4B6A-9559-BA717E7D5C47@richrelevance.com> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="jCrbxBqMcLqd4mOl" Content-Disposition: inline In-Reply-To: <5AC1718A-C11D-4B6A-9559-BA717E7D5C47@richrelevance.com> User-Agent: Mutt/1.5.18 (2008-05-17) X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.9 tagged_above=-10 required=5 tests=BAYES_00=-1.9, RCVD_IN_DNSWL_NONE=-0.0001 X-Spam-Level: X-Archive-Number: 201005/185 X-Sequence-Number: 38769 --jCrbxBqMcLqd4mOl Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Tue, May 25, 2010 at 11:27:08AM -0700, Scott Carey wrote: > On May 21, 2010, at 8:26 PM, Matthew Wakeling wrote: > > On Fri, 21 May 2010, Richard Yen wrote: > >> Any ideas why the query planner chooses a different query plan when us= ing prepared statements? > >=20 > > This is a FAQ. Preparing a statement makes Postgres create a plan, with= out=20 > > knowing the values that you will plug in, so it will not be as optimal = as=20 > > if the values were available. The whole idea is to avoid the planning c= ost=20 > > each time the query is executed, but if your data is unusual it can=20 > > result in worse plans. > >=20 > Maybe the planner could note a prepared query parameter is on a high skew > column and build a handful of plans to choose from, or just partially > re-plan on the skewed column with each execution. Or make it easier for a > user to have a prepared statement that re-plans the query each time. Even > just a per connection parameter "SET prepared.query.cacheplan =3D FALSE" There was talk in this year's developers' meeting of doing this replanning you've suggested. ("Re(?)plan parameterized plans with actual parameter values" on http://wiki.postgresql.org/wiki/PgCon_2010_Developer_Meeting, specificall). This wouldn't show up until at least 9.1, but it's something people are thinking about. -- Joshua Tolley / eggyknap End Point Corporation http://www.endpoint.com --jCrbxBqMcLqd4mOl Content-Type: application/pgp-signature; name="signature.asc" Content-Description: Digital signature Content-Disposition: inline -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.9 (GNU/Linux) iEYEARECAAYFAkv8Hp0ACgkQRiRfCGf1UMPXJACgmKo9zwmlKYvwxBrDgBtSWJE2 MxcAoI+IXMtx1qDYEvrvBVTWRb+mISqV =tRjL -----END PGP SIGNATURE----- --jCrbxBqMcLqd4mOl--