Received: from localhost (maia-1.hub.org [200.46.204.191]) by postgresql.org (Postfix) with ESMTP id BF9809FB63B for ; Thu, 5 Jul 2007 05:54:43 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.191]) (amavisd-maia, port 10024) with ESMTP id 44551-02 for ; Thu, 5 Jul 2007 05:54:39 -0300 (ADT) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.4 Received: from ug-out-1314.google.com (ug-out-1314.google.com [66.249.92.172]) by postgresql.org (Postfix) with ESMTP id 754CD9FB30E for ; Thu, 5 Jul 2007 05:54:39 -0300 (ADT) Received: by ug-out-1314.google.com with SMTP id a2so488850ugf for ; Thu, 05 Jul 2007 01:54:38 -0700 (PDT) DKIM-Signature: a=rsa-sha1; c=relaxed/relaxed; d=gmail.com; s=beta; h=domainkey-signature:received:received:message-id:date:from:user-agent:mime-version:to:cc:subject:references:in-reply-to:content-type:content-transfer-encoding:sender; b=UMsSZRegvsyQlGPNtc6Lzhi3vnuEgpYF6KqeVCg4Pk0/4EaCG4BWip//ehwz917RMRkThRIG1lwAOxdRhDjq+LfU0tNMOriVE/p0/mKWRL+GbxgZlja0f2K4Z6f434wNKt2cWvqAiuzgauepxWJjmq5Yk2/MucVOO3ZUq7c6GAQ= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=beta; h=received:message-id:date:from:user-agent:mime-version:to:cc:subject:references:in-reply-to:content-type:content-transfer-encoding:sender; b=fO0XlrP0gwtPPhvcaYTrquOc7H6J/4+yu7Cz59OszEW9LeW4GB/iSOP1FiOP+4I/hfbsQwblCunQZThWX3ZHtiXhmKR9ePaEfrn2iUUk6H9xNmjA0/5swgSNeuICWrD+ALFwBuN4cR09mfgdCmzK297DzJcrXLVoLwLO2GZ4pyE= Received: by 10.66.222.9 with SMTP id u9mr1818724ugg.1183625677980; Thu, 05 Jul 2007 01:54:37 -0700 (PDT) Received: from ?192.168.1.36? ( [81.105.64.157]) by mx.google.com with ESMTPS id 30sm673850ugf.2007.07.05.01.54.35 (version=SSLv3 cipher=RC4-MD5); Thu, 05 Jul 2007 01:54:36 -0700 (PDT) Message-ID: <468CB19F.6080108@enterprisedb.com> Date: Thu, 05 Jul 2007 09:53:51 +0100 From: Heikki Linnakangas User-Agent: Icedove 1.5.0.10 (X11/20070329) MIME-Version: 1.0 To: Flipper CC: pgsql-jdbc@postgresql.org Subject: Re: prepared statement: are they pre-compiled? References: <200707051023.25650.flipper@gammadue.com> In-Reply-To: <200707051023.25650.flipper@gammadue.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200707/17 X-Sequence-Number: 17191 Flipper wrote: > Hi all, > a little question about prepared statement. I know that one advantage of using > prepared statement is the fact that the statament could be "precompiled" by > the server before the binding of parameters, is it true? > But while observing the messages sent from the FE to the BE by the postgresql > driver, I noticed that the query is sent along with the bind of the > parameters, that is the when the executeQuery() method is called on the > prepared statement, both the parse and bind messages are sent. I thought the > bind message have to be sent before the bind one, but it seems to me it's > not. Anyone can please help me understand this? It depends on the prepareThreshold parameter. The default is 5, which means that the query is parsed and planned separately for each call to executeQuery, until the 5th call. After that, a generic plan is generated and used thereafter. For queries that are executed few times, planning the query each time is more efficient because the parameter values can be used in the planning, which can give you a better plan. For example, if you have a partial index on X > 10, and you run a query with WHERE X > ?, you can only use the partial index if the parameter > 10. When the query is planned using the parameter given, the planner can check its value and use the partial index when possible, but when the plan is made for use with arbitrary parameters later on, the partial index can't be used. Partial indexes are an obvious example, but there's many other cases where the parameter values can make a big difference in the plan. -- Heikki Linnakangas EnterpriseDB http://www.enterprisedb.com