pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Adrian Klaver <adrian.klaver@aklaver.com>
To: Martin Kováčik <kovacik@redbyte.eu>
Cc: Michael Lewis <mlewis@entrata.com>
Cc: PostgreSQL General <pgsql-general@lists.postgresql.org>
Subject: Re: analyze causes query planner to choose suboptimal plan for a select query in separate transaction
Date: Thu, 25 Apr 2019 15:25:43 -0700
Message-ID: <b711b66b-697e-abc2-5826-12315c48d16b@aklaver.com> (raw)
In-Reply-To: <CABp97hNieHyxXUqSOHj=SaqWjD5tD-8_gOdqTG=wpkZHiSaa5w@mail.gmail.com>
References: <CABp97hOoomefGd6HB8KOyDUdOt6+VjyNunUf_gn_gmVL2zZ2ZA@mail.gmail.com>
	<CAHOFxGrhmb5HA4AUj2M2fgzAC=Wr8CywbuzV-eDuAu-jpi+TUA@mail.gmail.com>
	<CABp97hPDdq9FN9RYjD+w1bHsk+iePFrXHQ09BNxjUz6H3Jk+EA@mail.gmail.com>
	<CAHOFxGr=CCaQxLoZGFvc+eQ2GWB3huqSk12DZTo+pF_iXNLq3w@mail.gmail.com>
	<CABp97hNswjPXqoC4nDX2_Jvc3f59BBq+VUjsm95+Yw41KRD87w@mail.gmail.com>
	<5918b32f-9461-ac2a-77c2-fb03d091e928@aklaver.com>
	<CABp97hNieHyxXUqSOHj=SaqWjD5tD-8_gOdqTG=wpkZHiSaa5w@mail.gmail.com>

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 <mailto:kovacik@redbyte.eu>, www.redbyte.eu 
> <http://redbyte.eu;
> 
> 
> On Thu, Apr 25, 2019 at 9:58 PM Adrian Klaver <adrian.klaver@aklaver.com 
> <mailto:adrian.klaver@aklaver.com>> 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 <mailto:kovacik@redbyte.eu>
>     <mailto:kovacik@redbyte.eu <mailto:kovacik@redbyte.eu>>,
>     www.redbyte.eu <http://www.redbyte.eu;
>      > <http://redbyte.eu;
>      >
>      >
>      > On Thu, Apr 25, 2019 at 9:28 PM Michael Lewis <mlewis@entrata.com
>     <mailto:mlewis@entrata.com>
>      > <mailto:mlewis@entrata.com <mailto:mlewis@entrata.com>>> wrote:
>      >
>      >
>      >
>      >     On Thu, Apr 25, 2019, 11:34 AM Martin Kováčik
>     <kovacik@redbyte.eu <mailto:kovacik@redbyte.eu>
>      >     <mailto:kovacik@redbyte.eu <mailto:kovacik@redbyte.eu>>> 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 <mailto:adrian.klaver@aklaver.com>
> 


-- 
Adrian Klaver
adrian.klaver@aklaver.com





view thread (12+ messages)  latest in thread

Message-ID: <b711b66b-697e-abc2-5826-12315c48d16b@aklaver.com>
Permalink:  ../b711b66b-697e-abc2-5826-12315c48d16b@aklaver.com/
Also on:    postgresql.org/message-id/b711b66b-697e-abc2-5826-12315c48d16b@aklaver.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: adrian.klaver@aklaver.com, kovacik@redbyte.eu, mlewis@entrata.com
  Subject: Re: analyze causes query planner to choose suboptimal plan for a select query in separate transaction
  In-Reply-To: <b711b66b-697e-abc2-5826-12315c48d16b@aklaver.com>
  References: <CABp97hOoomefGd6HB8KOyDUdOt6+VjyNunUf_gn_gmVL2zZ2ZA@mail.gmail.com> <CAHOFxGrhmb5HA4AUj2M2fgzAC=Wr8CywbuzV-eDuAu-jpi+TUA@mail.gmail.com> <CABp97hPDdq9FN9RYjD+w1bHsk+iePFrXHQ09BNxjUz6H3Jk+EA@mail.gmail.com> <CAHOFxGr=CCaQxLoZGFvc+eQ2GWB3huqSk12DZTo+pF_iXNLq3w@mail.gmail.com> <CABp97hNswjPXqoC4nDX2_Jvc3f59BBq+VUjsm95+Yw41KRD87w@mail.gmail.com> <5918b32f-9461-ac2a-77c2-fb03d091e928@aklaver.com> <CABp97hNieHyxXUqSOHj=SaqWjD5tD-8_gOdqTG=wpkZHiSaa5w@mail.gmail.com> <b711b66b-697e-abc2-5826-12315c48d16b@aklaver.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox