Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hJkJo-0002ZT-R0 for pgsql-general@arkaria.postgresql.org; Thu, 25 Apr 2019 19:46:05 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hJkJn-0006ZW-8a for pgsql-general@arkaria.postgresql.org; Thu, 25 Apr 2019 19:46:03 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hJkJm-0006Xz-RU for pgsql-general@lists.postgresql.org; Thu, 25 Apr 2019 19:46:02 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hJkJk-0003Nu-Aw for pgsql-general@lists.postgresql.org; Thu, 25 Apr 2019 19:46:01 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id x3PJjwpn028702; Thu, 25 Apr 2019 15:45:58 -0400 From: Tom Lane To: =?UTF-8?B?TWFydGluIEtvdsOhxI1paw==?= cc: pgsql-general@lists.postgresql.org Subject: Re: analyze causes query planner to choose suboptimal plan for a select query in separate transaction In-reply-to: References: Comments: In-reply-to =?UTF-8?B?TWFydGluIEtvdsOhxI1paw==?= message dated "Thu, 25 Apr 2019 16:37:46 +0200" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <28700.1556221558.1@sss.pgh.pa.us> Date: Thu, 25 Apr 2019 15:45:58 -0400 Message-ID: <28701.1556221558@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk =?UTF-8?B?TWFydGluIEtvdsOhxI1paw==?= writes: > To illustrate my situation let's consider my tests look like this: > BEGIN; > -- A: insert data for the test > -- B: i'll refer to this point later > -- C: select(s) > ROLLBACK; > Everything is fine, until autovacuum (analyze) runs when the test is at > point B. After that the query planner at point C chooses wrong plan and the > query takes a long time to complete, blocking one CPU core for a long time. > It seems like the planner statistics inside running transaction are > affected by analyze task running outside of the transaction. Yup, they are. However, you're already at risk of a pretty bad plan for a case like this, since (by assumption) the stats before you did the insert at step A are radically different from what they should be after the insert. The standard recommendation, when you need the results of a data change to be understood by the planner immediately, is to do your own ANALYZE: BEGIN; -- A: insert data for the test ANALYZE test_table; -- C: select(s) ROLLBACK; This should protect step C against seeing any irrelevant stats, because (a) once your transaction has done an ANALYZE, autovacuum shouldn't think the stats are out of date, and (b) even if it does, your transaction is now holding ShareUpdateExclusiveLock on the table so auto-ANALYZE can't get that lock to do a fresh ANALYZE. Another thing you could do is not even allow the test table to exist outside your transaction: BEGIN; CREATE TABLE test_table (...); -- A: insert data for the test ANALYZE test_table; -- this is now somewhat optional -- C: select(s) ROLLBACK; Then there's nothing for auto-ANALYZE to get its hands on. If you're satisfied with the planner's default behavior in the absence of any stats, you could omit the post-insertion ANALYZE in this case. But I'm not sure that that would represent a test that has much to do with production situations. regards, tom lane