X-Original-To: pgsql-performance-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id DB8D7D8833 for ; Thu, 6 Oct 2005 16:46:19 -0300 (ADT) 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 55714-07 for ; Thu, 6 Oct 2005 19:46:12 +0000 (GMT) Received: from tigger.fuhr.org (tigger.fuhr.org [63.214.45.158]) by svr1.postgresql.org (Postfix) with ESMTP id AEE69DA415 for ; Thu, 6 Oct 2005 16:46:09 -0300 (ADT) Received: from winnie.fuhr.org (winnie.fuhr.org [10.1.0.1]) by tigger.fuhr.org (8.13.3/8.13.3) with ESMTP id j96Jk8gg076626 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Thu, 6 Oct 2005 13:46:10 -0600 (MDT) (envelope-from mfuhr@winnie.fuhr.org) Received: from winnie.fuhr.org (localhost [127.0.0.1]) by winnie.fuhr.org (8.13.3/8.13.3) with ESMTP id j96Jk7ZW035332; Thu, 6 Oct 2005 13:46:07 -0600 (MDT) (envelope-from mfuhr@winnie.fuhr.org) Received: (from mfuhr@localhost) by winnie.fuhr.org (8.13.3/8.13.3/Submit) id j96Jk60I035331; Thu, 6 Oct 2005 13:46:06 -0600 (MDT) (envelope-from mfuhr) Date: Thu, 6 Oct 2005 13:46:06 -0600 From: Michael Fuhr To: Kelly Burkhart Cc: pgsql-performance@postgresql.org Subject: Re: functions and execution plan caching Message-ID: <20051006194606.GA17939@winnie.fuhr.org> References: <1128604674.5421.35.camel@krb06.tradebot.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <1128604674.5421.35.camel@krb06.tradebot.com> X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.006 required=5 tests=[AWL=0.006] X-Spam-Level: X-Archive-Number: 200510/127 X-Sequence-Number: 14895 On Thu, Oct 06, 2005 at 08:17:54AM -0500, Kelly Burkhart wrote: > Given: > - Processes which are connected to the database for long periods of time > (transactions are always short). > - These processes will use some functions to query data. > - Lots of data is being inserted into tables that these functions query. > - Vacuums are done frequently. > > Am I at risk of degrading performance after some time due to stale > execution plans? Yes, because plans are chosen based on the statistics that were current when the function was first called. For example, if a sequential scan made sense when you first called the function, then subsequent calls will also use a sequential scan. You can see this for yourself with a simple test: create a table, populate it with a handful of records, and call a function that issues a query that can (but won't necessarily) use an index. Then add a lot of records to the table and call the function again. You'll probably notice that the function runs slower than the same query run from outside the function, and that the function runs fast if you recreate it or call it in a new session. If you set debug_print_plan to on and client_min_messages to debug1, then you'll see the plan that the function chose (but only on the first call to the function). If you have statistics enabled, then you can query pg_stat_user_tables and pg_stat_user_indexes to see whether subsequent calls use sequential or index scans (this should be done when nobody else is querying the table so the statistics represent only what you did). You can avoid cached plans by using EXECUTE. You'll have to run tests to see whether the potential gain is worth the overhead. -- Michael Fuhr