Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1aIy8P-0003jg-Bl for pgsql-jdbc@arkaria.postgresql.org; Tue, 12 Jan 2016 12:33:13 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84) (envelope-from ) id 1aIy8N-0004WJ-Sy for pgsql-jdbc@arkaria.postgresql.org; Tue, 12 Jan 2016 12:33:11 +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 1aIy8M-0004VX-Kr for pgsql-jdbc@postgresql.org; Tue, 12 Jan 2016 12:33:10 +0000 Received: from mx1.redhat.com ([209.132.183.28]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84) (envelope-from ) id 1aIy8J-0006zk-1U for pgsql-jdbc@postgresql.org; Tue, 12 Jan 2016 12:33:08 +0000 Received: from int-mx13.intmail.prod.int.phx2.redhat.com (int-mx13.intmail.prod.int.phx2.redhat.com [10.5.11.26]) by mx1.redhat.com (Postfix) with ESMTPS id B9353C0A516E for ; Tue, 12 Jan 2016 12:33:04 +0000 (UTC) Received: from [10.36.116.72] (ovpn-116-72.ams2.redhat.com [10.36.116.72]) by int-mx13.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id u0CCX3Me013270 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO) for ; Tue, 12 Jan 2016 07:33:04 -0500 Subject: Re: Re: 9.4-1207 behaves differently with server side prepared statements compared to 9.2-1102 To: pgsql-jdbc@postgresql.org References: From: Jeremy Whiting Message-ID: <5694F27D.2080303@redhat.com> Date: Tue, 12 Jan 2016 12:33:01 +0000 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.4.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Scanned-By: MIMEDefang 2.68 on 10.5.11.26 X-Pg-Spam-Score: -6.9 (------) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-jdbc Precedence: bulk Sender: pgsql-jdbc-owner@postgresql.org On 12/01/16 11:26, Vladimir Sitnikov wrote: >> My guess is that the plan that is generated using the prepared statement only > works for some input values, but not for all (a problem that Oracle has suffered > from for ages as well). > > I think the problem is as follows: > 1) During initial runs in server-prepared mode PG still uses exact > values to plan the query Most types in the driver map directly but date time parameters are special. There may be others treated as a special case. I think you'll find initial executions (Bind) use the paramater type org.postgresql.core.Oid.UNSPECIFIED for the date time. https://github.com/pgjdbc/pgjdbc/blob/master/pgjdbc/src/main/java/org/postgresql/jdbc/PgPreparedStatement.java#L1426 > 2) It turns out that for certain conditions PG properly understands > that certain conditions are bad. > I bet $1 = '180', $2 = '1' returns lots of rows, thus backend chooses > to start with $3 = '7728'. At the threshold a ParameterDescribe is issued by the front end. The back end returns the actual type used to store the parameter. The front end stores the updated parameter type information. > 3) Later backend creates generic plan. That one does not include > knowledge of exact $1 and &2 values. Thus backend estimates the > cardinality differently. Subsequent Bind messages use the replaced parameter type. The back end switches to a new plan using the updated type information. I could be wrong on this as the codebase has changed dramatically in recent weeks. Jeremy > That results a plan flip. > > Note: in Oracle (9-10) bind peeking worked in another way. > There, a plan built for the first bind values was reused for all the > subsequent executions. > That makes more sense for me since that gives at least some stability > (it avoids sudden plan flips). > > I'll try to file a case to hackers list to check what they say on the plan flip. > > PS. The sad thing is JDBC does have room for "SQL-injection-safe, > non-server-prepared query". In other words, plain java.sql.Statement > does not have "setXXX" methods, and PreparedStatement has no > user-provided API to convey "please, do not use server-prepared > statement as the plan is very data-dependent". > > PPS. I do not think the plan is data-dependent in the particular case. > I bet the same plan works for all the values that particular > application uses. It is just PG's fault that plan flip happens. > > Vladimir > > -- Jeremy Whiting Senior Software Engineer, JBoss Performance Team Red Hat ------------------------------------------------------------ Registered Address: RED HAT UK LIMITED, 64 Baker Street, 4th Floor, Paddington. London. United Kingdom W1U 7DF Registered in UK and Wales under Company Registration No. 3798903 Directors: Michael Cunningham (US), Charles Peters (US), Matt Parson (US) and Michael O'Neill(Ireland) -- Sent via pgsql-jdbc mailing list (pgsql-jdbc@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-jdbc