Re: Strange sort node/explain result

From: "Gunnar \"Nick\" Bluth" <gunnar(dot)bluth(at)pro-open(dot)de>
To: David Rowley <dgrowleyml(at)gmail(dot)com>
Cc: pgsql-bugs(at)lists(dot)postgresql(dot)org, corrado(at)ficicchia(dot)net
Subject: Re: Strange sort node/explain result
Date: 2022-11-01 13:13:38
Message-ID: e7781c7f-99ba-ef25-e764-16eaf5e39de3@pro-open.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

Am 31.10.22 um 21:40 schrieb David Rowley:
> On Tue, 1 Nov 2022 at 03:20, Gunnar "Nick" Bluth
> <gunnar(dot)bluth(at)pro-open(dot)de> wrote:
>> What puzzles us is the part where the CTE "oneyear" somehow explodes
>> into a sort node of almost 10 mio (but not the same amount as the index
>> scan emits!) rows, taking ~ 0.4 seconds but only using 4x-5x kB of memory:
>>
>> -> Sort (cost=69.83..72.33 rows=1000 width=4) (actual
>> time=0.418..448.397 rows=9855001 loops=1)
>
> I think you're interpreting the EXPLAIN output wrongly. It's not that
> the Sort node emits 9855001 rows, it's that 9855001 are read from the
> Sort node.

Thanks for looking into this, David!

> The reason more rows are read from it than are produced is because
> Merge Join must perform mark and restore to "rewind" the inner side of
> the scan back for the subsequent outer tuple which has the same value.

Ok, I get it. We kind of had that suspicion.

So the sort could also say
"rows=366 loops=26926"
instead of
"rows=9855001 loops=1"

(which I myself would find reasonable...)?

< snip >

> There are only 366 rows for the Sort node to sort. 42kb seems like
> reasonable memory use for that.

Absolutely!

Thanks again,
--
Gunnar "Nick" Bluth

Eimermacherweg 106
D-48159 Münster

Mobil +49 172 8853339
Email: gunnar(dot)bluth(at)pro-open(dot)de
__________________________________________________________________________
"Ceterum censeo SystemD esse delendam" - Cato

In response to

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message David Rowley 2022-11-02 01:30:45 Re: Strange sort node/explain result
Previous Message Andres Freund 2022-11-01 05:51:32 Re: Memory leak on subquery as scalar operand