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 1hJmoV-0001aY-BO for pgsql-general@arkaria.postgresql.org; Thu, 25 Apr 2019 22:25:56 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hJmoT-0004Bw-Sw for pgsql-general@arkaria.postgresql.org; Thu, 25 Apr 2019 22:25:53 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hJmoS-0003uX-Ky for pgsql-general@lists.postgresql.org; Thu, 25 Apr 2019 22:25:53 +0000 Received: from wout3-smtp.messagingengine.com ([64.147.123.19]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hJmoO-000479-C6 for pgsql-general@lists.postgresql.org; Thu, 25 Apr 2019 22:25:52 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.west.internal (Postfix) with ESMTP id E8AC367A; Thu, 25 Apr 2019 18:25:45 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute6.internal (MEProxy); Thu, 25 Apr 2019 18:25:46 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= subject:to:cc:references:from:message-id:date:mime-version :in-reply-to:content-type:content-transfer-encoding; s=fm2; bh=A oJMnt74SkirbMfONF09izGjDhcNWIzDn+0qR2D57wM=; b=G3J2ozMfJ9qYxiJQ3 dLeClTJlCVOKMKHvgxn46+zxK7K08hqmxk0hvhi7OOPT/ItreZQim93ryvMa5K0P rO9KPZmUXZP3P0cy/43FaljpRCn8oMEcUfg/vD/Sv5bSNIlt0fSvYuo4Sc4jJYsR gQVWGJFc23FAY0qXv1lOROnJ5hV1yxvN81gSt2I3vDP9PMMa4sZRgPnq+/0EjZBF Clb2jJgvU3s/SbKgrG3mqO5JQhdT9FUO6uwIzCSJxX4XE0NrTjwFT5rdn7k3gD+z +vxdKPZYZAXCfRxw2feCux+oeuTjyDvjyWoHwjG4DlhaT/xDcaG32Lj1pWewgqHc FC9eQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm2; bh=AoJMnt74SkirbMfONF09izGjDhcNWIzDn+0qR2D57 wM=; b=m7me1TEdqoXwLlJB1rwslbIfZUakU1w89Hew9DsYL5BquRUwHCTcCoNTg Z9ONljW4oDyn4OSvkYX3UK3EYDDWx97NDn/XkvhcjgWG1DNfTy91j2r+JuYWgeom YdG8yhH2fB5aS1RmH5HKeTaE/Tq3IwWqZ0Us5yWAnam+QwvSg3+OvB/dOYimmjed 7gR7eob3ZHmhPhTxTUMd9FmOgzKYnrRK7ExpvVPV80dxqhygWUW0vGYWzHzYdkwE fvEOFaO1pUiwDskZijKsUsfYqCW+IDjDQ8nR4QyvuPwBa9T+nOA4NGM4wV327UAc 2cio2USMt+6PRyYXL3bO1f+/id5Ww== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduuddrheehgddutdcutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenuc fjughrpefuvfhfhffkffgfgggjtgfgsehtkeertddtfeejnecuhfhrohhmpeetughrihgr nhcumfhlrghvvghruceorggurhhirghnrdhklhgrvhgvrhesrghklhgrvhgvrhdrtghomh eqnecuffhomhgrihhnpehrvggusgihthgvrdgvuhdpphhoshhtghhrvghsqhhlrdhorhhg necukfhppeejfedrvdefledruddujedrvdefkeenucfrrghrrghmpehmrghilhhfrhhomh eprggurhhirghnrdhklhgrvhgvrhesrghklhgrvhgvrhdrtghomhenucevlhhushhtvghr ufhiiigvpedt X-ME-Proxy: Received: from ranger.suse (c-73-239-117-238.hsd1.wa.comcast.net [73.239.117.238]) by mail.messagingengine.com (Postfix) with ESMTPA id 35B06E432B; Thu, 25 Apr 2019 18:25:44 -0400 (EDT) Subject: Re: analyze causes query planner to choose suboptimal plan for a select query in separate transaction To: =?UTF-8?Q?Martin_Kov=c3=a1=c4=8dik?= Cc: Michael Lewis , PostgreSQL General References: <5918b32f-9461-ac2a-77c2-fb03d091e928@aklaver.com> From: Adrian Klaver Message-ID: Date: Thu, 25 Apr 2019 15:25:43 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.6.1 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 4/25/19 1:28 PM, Martin Kováčik wrote: > I'm not sure if I understand what you mean. My initial thought was that > stats are fixed per transaction, i.e. analyze from comitted transaction > doesn't interfere with another running transaction. Maybe I was confused > by this because analyze can be run inside a transaction, so my > assumption was it is isolated like other statements. To make it clearer I would take a look at: https://www.postgresql.org/docs/11/sql-analyze.html https://www.postgresql.org/docs/11/catalog-pg-statistic.html The gist is currently there is one entry(with exception noted in second link) per column in each table. This is the most recent information available subject to the conditions Tom pointed out in his post. To make it MVCC aware like you suggest would mean tracking the state of all transactions currently open and recording that information, so each transaction could find its stats. This means the planning problem could then move to the stats table as it would need to be analyzed itself to work efficiently. This would probably also need to be MVCC aware to be relevant, which would add to the overhead. I could see this turn into a 'hall of mirrors' problem quickly. > *Martin Kováčik* > /CEO/ > *redByte*, s.r.o. > +421 904 236 791 > kovacik@redbyte.eu , www.redbyte.eu > > > > On Thu, Apr 25, 2019 at 9:58 PM Adrian Klaver > wrote: > > On 4/25/19 12:47 PM, Martin Kováčik wrote: > > As my example shows you don't have to import a lot of rows - 1000 is > > enough to make a difference - it all depends on the query. When a > > cartesian product is involved only a few records is enough. > > I think that stats should be MVCC versioned otherwise the planner is > > using wrong statistics and chooses wrong plans. > > Then you are looking at moving the choke point to looking up the > correct > stats across possibly hundreds/thousands of transactions in flight. > > > *Martin Kováčik* > > /CEO/ > > *redByte*, s.r.o. > > +421 904 236 791 > > kovacik@redbyte.eu > >, > www.redbyte.eu > > > > > > > > On Thu, Apr 25, 2019 at 9:28 PM Michael Lewis > > >> wrote: > > > > > > > >     On Thu, Apr 25, 2019, 11:34 AM Martin Kováčik > > >     >> wrote: > > > >         Turning off autovacuum for the tests is a valid option and I > >         will definitely do this as a workaround. Each test pretty > much > >         starts with empty schema and data for it is generated > during the > >         run and rolled back at the end. I have a lot of tests and > at the > >         moment it is not feasible to modify them. > > > >         The real workload for the application is different, but there > >         are some cases, when we import data from remote web > service in a > >         transaction do some work with it and then we do a commit. If > >         there is an autovacuum during this process I assume there > will > >         be similar problem regarding planner statistics. > > > > > >     Unless you are importing a huge amount of data relative to > what is > >     already there, it seems likely to be significantly less impactful > >     than adding data to a completely empty table. The stats on a > table > >     with 0 rows and then 5000 rows is going to be night and day, > while > >     the difference between stats on 100,000 rows and 105,000 is > not as > >     impactful. Musing here. I expect others will chime in. > > > >     Stats are not versioned with MVCC so it would expected that a > commit > >     in another transaction that is updating stats would influence the > >     query plan for another transaction that is active. > > > > > -- > Adrian Klaver > adrian.klaver@aklaver.com > -- Adrian Klaver adrian.klaver@aklaver.com