Received: from localhost (maia-2.hub.org [200.46.204.187]) by postgresql.org (Postfix) with ESMTP id C8C569FB56F; Tue, 6 Mar 2007 19:07:29 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.187]) (amavisd-new, port 10024) with ESMTP id 61995-07; Tue, 6 Mar 2007 19:07:22 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey-1.7.4 X-Greylist: from auto-whitelisted by SQLgrey-1.7.4 Received: from mail01.enterprisedb.com (mail01.enterprisedb.com [63.246.7.168]) by postgresql.org (Postfix) with ESMTP id 4F1999FB24E; Tue, 6 Mar 2007 19:07:26 -0400 (AST) thread-index: AcdgRDs4tMhYaO/oQdKvcaxVsh6NGQ== Received: from [192.168.0.22] ([81.6.226.224]) by mail01.enterprisedb.com with Microsoft SMTPSVC(6.0.3790.1830); Tue, 6 Mar 2007 18:07:36 -0500 Content-Class: urn:content-classes:message Importance: normal X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.2826 Subject: Re: Plan invalidation vs. unnamed prepared statements From: "Simon Riggs" To: "Tom Lane" Cc: , In-Reply-To: <6080.1173201777@sss.pgh.pa.us> References: <6080.1173201777@sss.pgh.pa.us> Content-Type: text/plain; charset="iso-8859-1" Date: Tue, 06 Mar 2007 23:04:21 +0000 Message-ID: <1173222262.3760.2171.camel@silverbirch.site> MIME-Version: 1.0 X-Mailer: Evolution 2.6.0 Content-Transfer-Encoding: 7bit X-OriginalArrivalTime: 06 Mar 2007 23:07:36.0359 (UTC) FILETIME=[3AC83370:01C76044] X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.063 tagged_above=0 required=5 tests=AWL, BAYES_50 X-Spam-Level: X-Archive-Number: 200703/442 X-Sequence-Number: 99926 On Tue, 2007-03-06 at 12:22 -0500, Tom Lane wrote: > A. Just accept the extra overhead, thereby preserving the current > behavior of unnamed statements, and gaining the benefit that plan > invalidation will work correctly in the few cases where an unnamed > statement's plan lasts long enough to need replanning. With connection pooling, multiple sessions will execute each statement. If we check the cache each time this does seem more expensive for each individual session, but we should gain synergy from other similar sessions. Taken across multiple sessions, A will be a win because it will reduce planning overhead by ~99%. > C. Don't store the unnamed statement in the plan cache. To make sure > it's not used anymore when the plan might be stale, don't analyze or > plan at Parse-message time, but postpone *all* that work until Bind; > and always discard the plan after Execute. We could still do "raw > parsing" at Parse time, since that's independent of database state, > but all but the most trivial syntactic errors would now occur at Bind > not Parse time, as well as the majority of the time expenditure. ISTM there will be some cases where the current behaviour will not be maintained if we implement A exactly. One thing I've not seen mentioned is the effect of constants on various plans. The current system plans at Bind time so it can make longer term decisions based upon the values of initial parameters. So I'd say we need to check the cache at Parse time, but if we do need to plan, continue to do this at Bind time (and so don't write to plan cache until that point). That might mean we end up giving some of our benefit away if multiple sessions all concurrently plan a previously unplanned query. That does seem less likely and in any case much better than taking a step backwards in query planning of parameterised queries. Also, some of those plans are only currently possible with actual constants, specifically predicate proving for partial indexes and constraint exclusion. Parameter to constant folding may change the plan completely and make it non-reusable anyhow. How would we cope with that type of prepared query with plan inval? -- Simon Riggs EnterpriseDB http://www.enterprisedb.com