From: Tomas Vondra <tomas.vondra@2ndquadrant.com>
To: Richard Guo <guofenglinux@gmail.com>
Cc: Justin Pryzby <pryzby@telsasoft.com>
Cc: James Coleman <jtc331@gmail.com>
Cc: PostgreSQL-development <pgsql-hackers@postgresql.org>
Subject: Re: sqlsmith crash incremental sort
Date: Thu, 16 Apr 2020 14:51:01 +0200
Message-ID: <20200416125101.bkvsy5yhrjcb2ydz@development> (raw)
In-Reply-To: <CAMbWs4_EG9c1szADZNZ_Z0hg_fXrKTCzuur=iipypk2CvXPmaQ@mail.gmail.com>
References: <20200411191952.GA30754@telsasoft.com>
<20200411202639.2o7quia37zd6ypw7@development>
<20200411214639.GK2228@telsasoft.com>
<20200411224445.x4whiaeo67glpqen@development>
<20200413000943.bejni6axj7v4q5rn@development>
<CAMbWs4_EG9c1szADZNZ_Z0hg_fXrKTCzuur=iipypk2CvXPmaQ@mail.gmail.com>
On Thu, Apr 16, 2020 at 04:44:10PM +0800, Richard Guo wrote:
>On Mon, Apr 13, 2020 at 8:09 AM Tomas Vondra <tomas.vondra@2ndquadrant.com>
>wrote:
>
>>
>> I've been messing with this the whole day, without much progress :-(
>>
>> I'm 99.9999% sure it's the same issue described by the quoted comment,
>> because the plan looks like this:
>>
>> Nested Loop Left Join
>> -> Sample Scan on pg_namespace
>> Sampling: system ('7.2'::real)
>> -> Incremental Sort
>> Sort Key: ...
>> Presorted Key: ...
>> -> Unique
>> -> Sort
>> Sort Key: ...
>> -> Append
>> -> Nested Loop
>> ...
>> -> Nested Loop
>> ...
>>
>> so yeah, the plan does have set operations, and generate_append_tlist
>> does generate Vars with varno == 0, causing this issue.
>>
>
>After some digging I believe here is what happened.
>
>1. For the UNION query, we build an upper rel of UPPERREL_SETOP and
>generate Append path for it. Since Append doesn't actually evaluate its
>targetlist, we generate 'varno 0' Vars for its targetlist. (setrefs.c
>would just replace them with OUTER_VAR when adjusting the final plan so
>this usually does not cause problems.)
>
>2. To remove duplicates for UNION, we use hash/sort to unique-ify the
>result. If sort is chosen, we add Sort path and then Unique path above
>Append path, with pathkeys made from Append's targetlist.
>
>3. Also the Append's targetlist would be built into
>root->processed_tlist and with that we calculate root->sort_pathkeys.
>
>4. When handling ORDER BY clause, we figure out the pathkeys of
>Unique->Sort->Append path share some same prefix with
>root->sort_pathkeys and thus incremental sort would be considered.
>
>5. When calculating cost for incremental sort, estimate_num_groups does
>not cope with 'varno 0' Vars extracted from root->sort_pathkeys.
>
Right.
>
>With this scenario, here is a simple recipe:
>
>create table foo(a int, b int, c int);
>set enable_hashagg to off;
>explain select * from foo union select * from foo order by 1,3;
>
Yep, that's a much simpler query / plan. Thanks.
regards
--
Tomas Vondra http://www.2ndQuadrant.com
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services
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, guofenglinux@gmail.com, pryzby@telsasoft.com, jtc331@gmail.com
Subject: Re: sqlsmith crash incremental sort
In-Reply-To: <20200416125101.bkvsy5yhrjcb2ydz@development>
References: <20200411191952.GA30754@telsasoft.com> <20200411202639.2o7quia37zd6ypw7@development> <20200411214639.GK2228@telsasoft.com> <20200411224445.x4whiaeo67glpqen@development> <20200413000943.bejni6axj7v4q5rn@development> <CAMbWs4_EG9c1szADZNZ_Z0hg_fXrKTCzuur=iipypk2CvXPmaQ@mail.gmail.com> <20200416125101.bkvsy5yhrjcb2ydz@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