pg.ddx.io  pgsql-hackers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tomas Vondra <tomas.vondra@2ndquadrant.com>
To: James Coleman <jtc331@gmail.com>
Cc: Richard Guo <guofenglinux@gmail.com>
Cc: Justin Pryzby <pryzby@telsasoft.com>
Cc: pgsql-hackers <pgsql-hackers@postgresql.org>
Subject: Re: sqlsmith crash incremental sort
Date: Fri, 17 Apr 2020 01:13:00 +0200
Message-ID: <20200416231300.5mkhjzs6ki4yntto@development> (raw)
In-Reply-To: <20200416184416.vcmmy22zfmov54kl@development>
References: <20200411202639.2o7quia37zd6ypw7@development>
	<20200411214639.GK2228@telsasoft.com>
	<20200411224445.x4whiaeo67glpqen@development>
	<20200413000943.bejni6axj7v4q5rn@development>
	<CAAaqYe8Qt9ASuiphdjQxnMs6J6M2KSg4c=pRi5d=nQMxiSsjKw@mail.gmail.com>
	<20200415144712.wyvpgvsujzzcf2qg@development>
	<CAMbWs49FSJXAH0LVHBF1nUASGSHLP0xSVr8o0CHExyR9wtEJ+g@mail.gmail.com>
	<CAMbWs49DPHZgbH-7aqOQKhe3oR+LbEzk1je4z6RM6r_Dn4KrSQ@mail.gmail.com>
	<CAAaqYe_3xkgfcw9ZYOPcUCWUbod1yPKGm9wAY3XkJm7b2H+7jA@mail.gmail.com>
	<20200416184416.vcmmy22zfmov54kl@development>

On Thu, Apr 16, 2020 at 08:44:16PM +0200, Tomas Vondra wrote:
>On Thu, Apr 16, 2020 at 12:04:03PM -0400, James Coleman wrote:
>>On Thu, Apr 16, 2020 at 8:22 AM Richard Guo <guofenglinux@gmail.com> wrote:
>>>
>>>
>>>On Thu, Apr 16, 2020 at 6:35 PM Richard Guo <guofenglinux@gmail.com> wrote:
>>>>
>>>>
>>>>On Wed, Apr 15, 2020 at 10:47 PM Tomas Vondra <tomas.vondra@2ndquadrant.com> wrote:
>>>>>
>>>>>
>>>>>Well, yeah. The problem is the Unique simply compares the columns in the
>>>>>order it sees them, and it does not match the column order desired by
>>>>>incremental sort. But we don't push down this information at all :-(
>>>>
>>>>
>>>>This is a nice optimization better to have. Since the 'Sort and Unique'
>>>>would unique-ify the result of a UNION by sorting on all columns, why
>>>>not we adjust the sort order trying to match parse->sortClause so that
>>>>we can avoid the final sort node?
>>>>
>>>>Doing that we can transform plan from:
>>>>
>>>># explain (costs off) select * from foo union select * from foo order by 1,3;
>>>>                  QUERY PLAN
>>>>-----------------------------------------------
>>>> Incremental Sort
>>>>   Sort Key: foo.a, foo.c
>>>>   Presorted Key: foo.a
>>>>   ->  Unique
>>>>         ->  Sort
>>>>               Sort Key: foo.a, foo.b, foo.c
>>>>               ->  Append
>>>>                     ->  Seq Scan on foo
>>>>                     ->  Seq Scan on foo foo_1
>>>>(9 rows)
>>>>
>>>>To:
>>>>
>>>># explain (costs off) select * from foo union select * from foo order by 1,3;
>>>>               QUERY PLAN
>>>>-----------------------------------------
>>>> Unique
>>>>   ->  Sort
>>>>         Sort Key: foo.a, foo.c, foo.b
>>>>         ->  Append
>>>>               ->  Seq Scan on foo
>>>>               ->  Seq Scan on foo foo_1
>>>>(6 rows)
>>>>
>>>
>>>Attached is what I'm thinking about this optimization. Does it make any
>>>sense?
>>
>>Shouldn't this go one either a new thread or on the thread for the
>>patch Tomas was referencing (by Teodor I believe)?
>>
>
>FWIW the optimization I had in mind is this:
>
>  https://commitfest.postgresql.org/21/1651/
>
>I now realize that was about GROUP BY, but it's not all that different
>and the concerns will / should be fairly similar, I think.
>
>IMO simply tweaking the sort keys to match the upper parts of the plan
>is probably way too simplistic, I'm afraid. For example, if the Unique
>significantly reduces cardinality, then the cost of the additional sort
>is much less important. It may be much better to optimize the "large"
>sort of the whole data set, either by reordering the columns as proposed
>by Teodor in his patch (by number of distinct values and/or cost of the
>comparison function function).
>
>Furthermore, this is one of the places that is not using incremental
>sort yet - I can easily imagine doing something like this:
>
>
>   Sort
>     -> Unique
>        -> Incremenal Sort
>	   -> ...
>
>could be a massive win. So I think we can't just rejigger the sort keys
>abitrarily, we should / need to consider those alternatives.
>
>>Or are you saying you believe this patch guarantees we never see this
>>problem in incremental sort costing?
>>
>
>Yeah, that's not entirely close to me. But maybe it shows us where we to
>get the unprocessed target list?
>

I think at the very least this needs to apply the same change also to
generate_nonunion_paths, because otherwise this fails because of the
same issue:

   set enable_hashagg = off;
   explain select * from foo except select * from foo order by 1, 3;

I'm still of the opinion that this is really an optimization and
behavior change, and I feel rather uneasy about pushing it post feature
freeze without appropriate review. I also think we really ought to
consider how would this work with the other optimizations I outlined
elsewhere in this thread (comparison costs, ...).


regards

-- 
Tomas Vondra                  http://www.2ndQuadrant.com
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services





view thread (25+ messages)  latest in thread

Message-ID: <20200416231300.5mkhjzs6ki4yntto@development>
Permalink:  ../20200416231300.5mkhjzs6ki4yntto@development/
Also on:    postgresql.org/message-id/20200416231300.5mkhjzs6ki4yntto@development

 · 

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-hackers@postgresql.org
  Cc: tomas.vondra@2ndquadrant.com, jtc331@gmail.com, guofenglinux@gmail.com, pryzby@telsasoft.com
  Subject: Re: sqlsmith crash incremental sort
  In-Reply-To: <20200416231300.5mkhjzs6ki4yntto@development>
  References: <20200411202639.2o7quia37zd6ypw7@development> <20200411214639.GK2228@telsasoft.com> <20200411224445.x4whiaeo67glpqen@development> <20200413000943.bejni6axj7v4q5rn@development> <CAAaqYe8Qt9ASuiphdjQxnMs6J6M2KSg4c=pRi5d=nQMxiSsjKw@mail.gmail.com> <20200415144712.wyvpgvsujzzcf2qg@development> <CAMbWs49FSJXAH0LVHBF1nUASGSHLP0xSVr8o0CHExyR9wtEJ+g@mail.gmail.com> <CAMbWs49DPHZgbH-7aqOQKhe3oR+LbEzk1je4z6RM6r_Dn4KrSQ@mail.gmail.com> <CAAaqYe_3xkgfcw9ZYOPcUCWUbod1yPKGm9wAY3XkJm7b2H+7jA@mail.gmail.com> <20200416184416.vcmmy22zfmov54kl@development> <20200416231300.5mkhjzs6ki4yntto@development>

* 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