Received: from localhost (unknown [200.46.208.211]) by mail.postgresql.org (Postfix) with ESMTP id 78925633C61; Mon, 9 Mar 2009 13:51:33 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by localhost (mx1.hub.org [200.46.208.211]) (amavisd-maia, port 10024) with ESMTP id 45430-05; Mon, 9 Mar 2009 13:51:30 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from mail.mnc.ch (62-2-77-205.static.cablecom.ch [62.2.77.205]) by mail.postgresql.org (Postfix) with ESMTP id 486896339F3; Mon, 9 Mar 2009 13:51:27 -0300 (ADT) Received: from localhost (mail2.mnc.ch [127.0.0.1]) by mail.mnc.ch (Postfix) with ESMTP id 023D623079; Mon, 9 Mar 2009 16:51:24 +0000 (GMT) X-Virus-Scanned: amavisd-new at mnc.ch Received: from mail.mnc.ch ([127.0.0.1]) by localhost (mail2.mnc.ch [127.0.0.1]) (amavisd-new, port 10025) with ESMTP id wYfCVdf9Mkiw; Mon, 9 Mar 2009 16:51:23 +0000 (GMT) Received: from meuh.localdomain (62-2-77-204.static.cablecom.ch [62.2.77.204]) by mail.mnc.ch (Postfix) with ESMTP id 87B7C23020; Mon, 9 Mar 2009 16:51:23 +0000 (GMT) Received: by meuh.localdomain (Postfix, from userid 1000) id 9B41EDF23A; Mon, 9 Mar 2009 17:51:24 +0100 (CET) To: Mario Splivalo , pgsql-performance@postgresql.org cc: pgsql-jdbc@postgresql.org Subject: Re: [PERFORM] Query much slower when run from postgres function References: <49B51B60.6030600@megafon.hr> <27649.1236616294@sss.pgh.pa.us> From: Guillaume Cottenceau X-Hashcash: 1:20:090309:mario.splivalo@megafon.hr::6h5MXHN8chF3nnbs:0000000000000000000000000000000000001fJY X-Hashcash: 1:20:090309:pgsql-performance@postgresql.org::/h7X5gj9oPYJRaxU:000000000000000000000000000001Xxr X-Hashcash: 1:20:090309:pgsql-jdbc@postgresql.org::V0jUgSYRHbymh0Zj:0000000000000000000000000000000000005525 Date: Mon, 09 Mar 2009 17:51:24 +0100 In-Reply-To: <27649.1236616294@sss.pgh.pa.us> (Tom Lane's message of "Mon\, 09 Mar 2009 12\:31\:34 -0400") Message-ID: <87fxhmmygj.fsf@meuh.mnc.lan> User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.3 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0 tagged_above=0 required=5 tests=none X-Spam-Level: X-Archive-Number: 200903/28 X-Sequence-Number: 19358 Tom Lane writes: > Mario Splivalo writes: >> Now I'm confused, why is 'sql' function much slower than 'direct' SELECT? > > Usually the reason for this is that the planner chooses a different plan > when it has knowledge of the particular value you are searching for than > when it does not. Yes, and since Mario is coming from JDBC, I'll share my part on this: I also noticed some very wrong plans in JDBC because of the "optimization" in prepared statements consisting of planning once for all runs, e.g. without any parameter values to help planning. My understanding is that practically, it's difficult for the planner to opt for an index (or not) because the selectivity of a parameter value may be much different when the actual value changes. Normally, the planner "thinks" that planning is so costly that it's better to plan once for all runs, but practically for our use, this is very wrong (it may be very good for some uses, though it would be interesting to know the actual uses share). Until it's possible to specifically tell the JDBC driver (and/or PG?) to not plan once for all runs (or is there something better to think of?), or the whole thing would be more clever (off the top of my head, PG could try to replan with the first actual values - or first xx actual values - and if the plan is different, then flag that prepared statement for replanning each time if the overall time estimate is different enough), I've opted to tell the JDBC driver to use the protocol version 2, as prepared statements were not so much prepared back then (IIRC parameter interpolation is performed in driver and the whole SQL query is passed each time, parsed, and planned) using protocolVersion=2 in the JDBC URL. So far it worked very well for us. -- Guillaume Cottenceau