Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1ZoYEt-0001mF-2u for pgsql-performance@arkaria.postgresql.org; Tue, 20 Oct 2015 14:50:11 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84) (envelope-from ) id 1ZoYEs-0006aV-LU for pgsql-performance@arkaria.postgresql.org; Tue, 20 Oct 2015 14:50:10 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84) (envelope-from ) id 1ZoYDE-00041k-9l for pgsql-performance@postgresql.org; Tue, 20 Oct 2015 14:48:28 +0000 Received: from mail.socialserve.com ([66.129.73.133]) by makus.postgresql.org with esmtp (Exim 4.84) (envelope-from ) id 1ZoYDB-0000yO-8j for pgsql-performance@postgresql.org; Tue, 20 Oct 2015 14:48:26 +0000 Received: from jrogers.socialserve.com (jrogers-vpn.socialserve.com [10.0.8.30]) (using TLSv1.2 with cipher DHE-RSA-AES128-SHA (128/128 bits)) (No client certificate requested) (Authenticated sender: jrogers) by mail.socialserve.com (Postfix) with ESMTPSA id 5212D64118; Tue, 20 Oct 2015 10:48:23 -0400 (EDT) Subject: Re: SELECT slows down on sixth execution To: Pavel Stehule , Thomas Kellerer References: <561E068F.1040702@socialserve.com> <561E749D.4090301@socialserve.com> <5621AF0D.5080203@socialserve.com> Cc: "pgsql-performance@postgresql.org" From: Jonathan Rogers X-Enigmail-Draft-Status: N1110 Message-ID: <56265436.4050806@socialserve.com> Date: Tue, 20 Oct 2015 10:48:22 -0400 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.3.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Pg-Spam-Score: -1.9 (-) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-performance Precedence: bulk Sender: pgsql-performance-owner@postgresql.org On 10/20/2015 03:45 AM, Pavel Stehule wrote: > > > 2015-10-20 8:55 GMT+02:00 Thomas Kellerer >: > > Jonathan Rogers schrieb am 17.10.2015 um 04:14: > >>> Yes, I have been looking at both plans and can see where they > diverge. > >>> How could I go about figuring out why Postgres fails to see the > large > >>> difference in plan execution time? I use exactly the same parameters > >>> every time I execute the prepared statement, so how would > Postgres come > >>> to think that those are not the norm? > >> > >> PostgreSQL does not consider the actual query execution time, it only > >> compares its estimates for there general and the custom plan. > >> Also, it does not keep track of the parameter values you supply, > >> only of the average custom plan query cost estimate. > > > > OK, that makes more sense then. It's somewhat tedious for the > purpose of > > testing to execute a prepared statement six times to see the plan > which > > needs to be optimized. Unfortunately, there doesn't seem to be any way > > to force use of a generic plan in SQL based on Pavel Stehule's reply. > > > If you are using JDBC the threshold can be changed: > > https://jdbc.postgresql.org/documentation/94/server-prepare.html > > https://jdbc.postgresql.org/documentation/publicapi/org/postgresql/PGStatement.html#setPrepareThreshold%28int%29 > > As I don't think JDBC is using anything "exotic" I would be > surprised if this > can't be changed with other programming environments also. > > > This is some different - you can switch between server side prepared > statements and client side prepared statements in JDBC. It doesn't > change the behave of server side prepared statements in Postgres. I am using psycopg2 with a layer on top which can automatically PREPARE statements, so I guess that implements something similar to the JDBC interface. I did solve my problem by turning off the automatic preparation. -- Jonathan Rogers Socialserve.com by Emphasys Software jrogers@emphasys-software.com -- Sent via pgsql-performance mailing list (pgsql-performance@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-performance