Received: from maia.hub.org (maia-2.hub.org [200.46.204.251]) by mail.postgresql.org (Postfix) with ESMTP id 22B5BD22D0B for ; Sun, 13 Nov 2011 12:17:32 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.251]) (amavisd-maia, port 10024) with ESMTP id 00671-08 for ; Sun, 13 Nov 2011 16:17:26 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.8.0-rc2 Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by mail.postgresql.org (Postfix) with ESMTP id C8654D22D0A for ; Sun, 13 Nov 2011 12:17:25 -0400 (AST) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.2/8.14.2) with ESMTP id pADGHM02006470; Sun, 13 Nov 2011 11:17:22 -0500 (EST) To: "Strange, John W" cc: "pgsql-performance@postgresql.org" Subject: Re: Trying to understand Stats/Query planner issue In-reply-to: References: Comments: In-reply-to "Strange, John W" message dated "Tue, 08 Nov 2011 17:33:09 -0500" Date: Sun, 13 Nov 2011 11:17:22 -0500 Message-ID: <6469.1321201042@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-3.101 tagged_above=-10 required=5 tests=BAYES_00=-1.9, RP_MATCHES_RCVD=-1.201 X-Spam-Level: X-Archive-Number: 201111/154 X-Sequence-Number: 45665 "Strange, John W" writes: > I have a question on how the analyzer works in this type of scenario. > We calculate some results and COPY INTO some partitioned tables, which we use some selects to aggregate the data back out into reports. Everyone once in a while the aggregation step picks a bad plan due to stats on the tables that were just populated. Updating the stats and rerunning the query seems to solve the problem, this only happens if we enable nested loop query plans. Well, even if auto-analyze launches instantly after you commit the insertions (which it won't), it's going to take time to scan the table and then commit the updates to pg_statistic. So there is always going to be some window where queries will get planned with obsolete information. If you're inserting enough data to materially change the statistics of a table, and you need to query that table right away, doing a manual ANALYZE rather than waiting for auto-analyze is recommended. > The other option is just to analyze each table involved in the query after the insert, but that seems a bit counterproductive. Why would you think that? This type of scenario is exactly why ANALYZE isn't deprecated as a user command. regards, tom lane