X-Original-To: pgsql-jdbc-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 873CE329D3B for ; Tue, 21 Sep 2004 11:35:46 +0100 (BST) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 54039-04 for ; Tue, 21 Sep 2004 10:35:35 +0000 (GMT) Received: from yass.opencloud.co.nz (yass.opencloud.co.nz [203.79.85.162]) by svr1.postgresql.org (Postfix) with ESMTP id 3EC04329DBE for ; Tue, 21 Sep 2004 11:35:35 +0100 (BST) Received: from 203-79-104-60.cable.paradise.net.nz ([203.79.104.60] helo=opencloud.com) by yass.opencloud.co.nz with asmtp (Exim 3.35 #1 (Debian)) id 1C9hzT-0003MK-00; Tue, 21 Sep 2004 22:35:19 +1200 Message-ID: <415003E4.9020603@opencloud.com> Date: Tue, 21 Sep 2004 22:35:16 +1200 From: Oliver Jowett User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.6b) Gecko/20031205 Thunderbird/0.4 X-Accept-Language: en-us, en MIME-Version: 1.0 To: Markus Schaber Cc: pgsql-jdbc Subject: Re: raising the default prepareTheshold References: <414CA8A0.7070707@opencloud.com> <20040921100948.052b6ead@kingfisher.intern.logi-track.com> In-Reply-To: <20040921100948.052b6ead@kingfisher.intern.logi-track.com> Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.0 tagged_above=0.0 required=5.0 tests= X-Spam-Level: X-Archive-Number: 200409/138 X-Sequence-Number: 11028 Markus Schaber wrote: > Hi, Oliver, > > On Sun, 19 Sep 2004 09:29:04 +1200 > Oliver Jowett wrote: > > >>However, with a 8.0 backend, if the planner benefits from knowing the >>particular parameter values in use for a particular query, then using an >>unnamed statement may be faster due to using an improved plan. Under 7.4 >>there will be no difference; both named and unnamed statements will use >>the generic plan. > > > Quoting from http://developer.postgresql.org/beta-history.txt: > > * Plan prepared queries only when first executed so constants can be > used for statistics (Oliver Jowett) > Prepared statements plan queries once and execute them many times. > While prepared queries avoid the overhead of re-planning on each > use, the quality of the plan suffers from not knowing the exact > parameters to be used in the query. In this release, planning of > unnamed prepared statements is delayed until the first execution, > and the actual parameter values of that execution are used as > optimization hints. Yes, this is the end result of the patch I submitted. > So it seems that your issue is addressed in 8.0 - as long as the Beta of > the Readme is correct :-) The readme is right (although the first sentence is a bit general). However, it's not a perfect solution. If you use named statements then the query is planned only once, when the Parse message is received. This plan cannot take the actual parameter values being used into account. In contrast, the unnamed statement is planned when the first Bind is received, and uses the actual parameter values for cost estimation. So using a named statement might produce a worse plan than using the unnamed statement each time. By default, the JDBC driver uses the unnamed statement for each query execution, which should produce the same plans as if it just did direct parameter substitution into the query string. If the prepareThreshold is reached for a particular PreparedStatement, the JDBC driver starts (re)using a named statement for that query, which is not *always* a win. It does avoids re-parsing and re-planning the query each time, but each execution may run slower if the plan becomes worse. -O