Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1n86SR-0004xY-0N for pgsql-hackers@arkaria.postgresql.org; Thu, 13 Jan 2022 20:12:28 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1n86SP-0000eo-Bn for pgsql-hackers@arkaria.postgresql.org; Thu, 13 Jan 2022 20:12:25 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1n86SO-0000ee-N9 for pgsql-hackers@lists.postgresql.org; Thu, 13 Jan 2022 20:12:25 +0000 Received: from mxout4.routing.net ([2a03:2900:1:a::9]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1n86SK-0007SJ-P0 for pgsql-hackers@postgresql.org; Thu, 13 Jan 2022 20:12:23 +0000 Received: from mxbox2.masterlogin.de (unknown [192.168.10.89]) by mxout4.routing.net (Postfix) with ESMTP id EDE42100803; Thu, 13 Jan 2022 20:12:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=mailerdienst.de; s=20200217; t=1642104736; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=N6FZ044I36gdZLrbAuGRkowssH9JkRClTxBopG9gfBw=; b=jYKi4cc3tlLvcjPrqqjfqwtbzczLW0h3evu2fvpNJqL3j+Y0fOaHLRAYYm46DncEDyijzF fVyMsZzJDqYyQiQ1n6iZgCm6Duvt8xIuz+AIVOWG1xjA2Zm/w2auvMiuYer5cCxNZ9e+pc TL9MacS/nl9lKrTr4LhRv/Obxz3rmKA= Received: from remote.index.de (i68970D96.versanet.de [104.151.13.150]) by mxbox2.masterlogin.de (Postfix) with ESMTPSA id 688E71001F8; Thu, 13 Jan 2022 20:12:15 +0000 (UTC) Received: from acedia.index-gruppe.local (192.168.54.121) by invidia.index-gruppe.local (192.168.54.140) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2308.21; Thu, 13 Jan 2022 21:12:14 +0100 Received: from acedia.index-gruppe.local ([fe80::501:b638:b4b6:57d1]) by acedia.index-gruppe.local ([fe80::501:b638:b4b6:57d1%13]) with mapi id 15.01.2308.021; Thu, 13 Jan 2022 21:12:14 +0100 From: Arne Roland To: Tomas Vondra CC: pgsql-hackers Subject: Re: PATCH: generate fractional cheapest paths in generate_orderedappend_path Thread-Topic: PATCH: generate fractional cheapest paths in generate_orderedappend_path Thread-Index: AQHXMxwFBSWPrfL6EUqWBkKfzekca6rGmsgBgDwx81n///NcAIAALGoAgAAAZwCAAAGEAIAASqAYgCOeUZuA+duYRIAARBQAgAqYO8uAAlg8AIAzrQkAgAFXTYI= Date: Thu, 13 Jan 2022 20:12:14 +0000 Message-ID: <6c4cf676bc74402696018af08bc040af@index.de> References: , In-Reply-To: Accept-Language: en-US, de-DE Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-originating-ip: [10.255.1.33] Content-Type: multipart/alternative; boundary="_000_6c4cf676bc74402696018af08bc040afindexde_" MIME-Version: 1.0 X-Mail-ID: 33e2f5b9-2290-40b5-b659-7738c9624e26 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --_000_6c4cf676bc74402696018af08bc040afindexde_ Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Hi! > From: Tomas Vondra > Subject: Re: PATCH: generate fractional cheapest paths in generate_ordere= dappend_path > > test-# SELECT * FROM fract_t x LEFT JOIN fract_t y USING (id1, id2) > ORDER BY id1 ASC, id2 ASC LIMIT 10; > QUERY PLAN > > -------------------------------------------------------------------------= ----- > Limit > -> Merge Left Join > Merge Cond: ((x.id1 =3D y.id1) AND (x.id2 =3D y.id2)) > -> Append > -> Index Only Scan using fract_t0_id1_id2_idx on > fract_t0 x_1 > -> Incremental Sort > Sort Key: x_2.id1, x_2.id2 > Presorted Key: x_2.id1 > -> Index Scan using fract_t1_pkey on fract_t1 x_2 > -> Materialize > -> Append > -> Incremental Sort > Sort Key: y_1.id1, y_1.id2 > Presorted Key: y_1.id1 > -> Index Scan using fract_t0_pkey on > fract_t0 y_1 > Index Cond: (id1 =3D id1) > Filter: (id2 =3D id2) > -> Incremental Sort > Sort Key: y_2.id1, y_2.id2 > Presorted Key: y_2.id1 > -> Index Scan using fract_t1_pkey on > fract_t1 y_2 > Index Cond: (id1 =3D id1) > Filter: (id2 =3D id2) > (23 rows) > [...] > So that seems reasonable Maybe I'm just slow, but that doesn't seem reasonable to me. It doesn't loo= k like a valid plan to me. Sure all the nodes are arranged like I'd like th= em to be. But what are the id1/id2 bound we have in the index and filter co= nditions? > [...]but there's a couple issues too: > > 1) Planning works (hence we can run explain), but execution fails > because of segfault in CheckVarSlotCompatibility - it gets NULL slot for > some reason. I haven't looked into the details, but I'd guess we need to > pass a different rel to create_incrementalsort_path, not childrel. In case my above concern is valid, maybe the executor is just as confused a= s I am. Such conditions should generate VarSlots, no? If so, where should t= hey come from? Sadly I don't have time to debug that in depth today. > 2) enable_partitionwisejoin=3Don seems to cause some confusion, because i= t > results in picking a different plan with higher cost. But that's clearly > not because of this patch. Short version: I'd neither conceptually expect costs to be lower in any cas= e, nor would that be desirable, because our estimates aren't perfect. Long version: What do you mean by confusion. The plan I get with the patch = doesn't seem to confusing to me. Generally something like that is to be exp= ected. enable_partitionwisejoin changes the way this planing works by rewri= ting the entire query effectively rule based. So we end up with a completel= y different query. I'd in particular expect slightly different startup cost= s. So if we activate this we consider completely different plans, I struggle t= o come up with a meaningful example where there is any overlap at all. Thus= it doesn't surprise me conceptually. From practical experience I'd say: If they are about the same plan, the cos= ts estimates work somewhat okish. If we change the way we compose the nodes together, we sometimes end up wit= h radical different costs for doing the same. While I suspect there are a l= ot of corner cases causing this, I've seen quite a few where this was due t= o the fact, that our planer often has insignificant statistics to know some= thing and takes a guess. This has gotten way better of recent years, but it= 's in particular for non-trivial joins still a problem in practice. > 3) I'm still a bit skeptical about the cost of this implementation - it > builds the incrementalsort path, just to throw most of the paths away. > It'd be much better to just calculate the cost, which should be much > cheaper, and only do the heavy work for the one "best" path. Maybe we should profile this to get a rough estimate, how much time we spen= d building these incremental paths. From a code perspective it's non trivia= l to me where the time is lost. > 4) One thing I did not realize before is what pathkeys we even consider > here. Imagine you have two tables: > > CREATE TABLE t1 (a int, b int, primary key (a)); > CREATE TABLE t2 (a int, b int, primary key (a)); > > and query > > SELECT * FROM t1 JOIN t2 USING (a,b); > > It seems reasonable to also consider pathkeys (a,b) because that's make > e.g. mergejoin much cheaper, right? But no, we'll not do that - we only > consider pathkeys that at least one child relation has, so in this case > it's just (a). Which means we'll never consider incremental sort for > this particular example. > > It's a bit strange, because it's enough to create index on (a,b) for > just one of the relations, and it'll suddenly consider building > incremental sort on both sides. I don't find that surprising, because the single index *might* make the inc= remental sort cheaper for the join *without* considering any external sort = order. So we would be switching up the incremental sort and the mergejoin, in case= we need to sort anyways. That would mean considering also the sort order, = that might be relevant on the outside. Sounds like an interesting idea for = a later patch. > I don't plan to pursue this further at this point, so I pushed the first > part because that's a fairly simple improvement over what we have now. > But it seems like a fairly promising area for improvements. I think 1) is pretty important, so we should sort that out sooner than late= r. Apart form that: :+1: Thank you! Regards Arne --_000_6c4cf676bc74402696018af08bc040afindexde_ Content-Type: text/html; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable
 Hi!

> From: Tomas Vondra <tomas.vondra@enterprisedb.com>
> Subject: Re: PATCH: generate fractional cheapest paths in generate_ord= eredappend_path
>  
> test-# SELECT * FROM fract_t x LEFT JOIN fract_t y USING (id1, id2) > ORDER BY id1 ASC, id2 ASC LIMIT 10;
>            = ;            &n= bsp;           QUERY PLAN=
>
> ----------------------------------------------------------------------= --------
>   Limit
>     ->  Merge Left Join
>           Merge Cond= : ((x.id1 =3D y.id1) AND (x.id2 =3D y.id2))
>           -> = ; Append
>            = ;     ->  Index Only Scan using fract_t0_id1_id= 2_idx on
>            = ;            &n= bsp;            = ;      fract_t0 x_1
>            = ;     ->  Incremental Sort
>            = ;           Sort Key: x_2= .id1, x_2.id2
>            = ;           Presorted Key= : x_2.id1
>            = ;           ->  I= ndex Scan using fract_t1_pkey on fract_t1 x_2
>           -> = ; Materialize
>            = ;     ->  Append
>            = ;           ->  I= ncremental Sort
>            = ;            &n= bsp;    Sort Key: y_1.id1, y_1.id2
>            = ;            &n= bsp;    Presorted Key: y_1.id1
>            = ;            &n= bsp;    ->  Index Scan using fract_t0_pkey on
>            = ;            &n= bsp;            = ;             f= ract_t0 y_1
>            = ;            &n= bsp;          Index Cond: (id1= =3D id1)
>            = ;            &n= bsp;          Filter: (id2 =3D= id2)
>            = ;           ->  I= ncremental Sort
>            = ;            &n= bsp;    Sort Key: y_2.id1, y_2.id2
>            = ;            &n= bsp;    Presorted Key: y_2.id1
>            = ;            &n= bsp;    ->  Index Scan using fract_t1_pkey on
>            = ;            &n= bsp;            = ;             f= ract_t1 y_2
>            = ;            &n= bsp;          Index Cond: (id1= =3D id1)
>            = ;            &n= bsp;          Filter: (id2 =3D= id2)
> (23 rows)
> [...]
> So that seems reasonable

Maybe I'm just slow, but that doesn't seem reasonable to me. It doesn't loo= k like a valid plan to me. Sure all the nodes are arranged like I'd like th= em to be. But what are the id1/id2 bound we have in the index and filter co= nditions?

> [...]but there's a couple issues too:
>
> 1) Planning works (hence we can run explain), but execution fails
> because of segfault in CheckVarSlotCompatibility - it gets NULL slot f= or
> some reason. I haven't looked into the details, but I'd guess we need = to
> pass a different rel to create_incrementalsort_path, not childrel.

In case my above concern is valid, maybe the executor is just as confused a= s I am. Such conditions should generate VarSlots, no? If so, where should they come from?

Sadly I don't have time to debug that in depth today.

> 2) enable_partitionwisejoin=3Don seems to cause some confusion, becaus= e it
> results in picking a different plan with higher cost. But that's clear= ly
> not because of this patch.

Short version: I'd neither conceptually expect costs to be lower= in any case, nor would that be desirable, because our estimates aren't per= fect.

Long version: What do you mean by confusion. The plan I get with the patch = doesn't seem to confusing to me. Generally something like that is to be exp= ected. enable_partitionwisejoin changes the way this planing works by rewri= ting the entire query effectively rule based. So we end up with a completely different query. I'd in particu= lar expect slightly different startup costs.
So if we activate this we consider completely different plans, I struggle t= o come up with a meaningful example where there is any overlap at all. Thus= it doesn't surprise me conceptually.
From practical experience I'd say: If they are about the same plan, the cos= ts estimates work somewhat okish.
If we change the way we compose the nodes together, we sometimes end up wit= h radical different costs for doing the same. While I suspect there are a l= ot of corner cases causing this, I've seen quite a few where this was due t= o the fact, that our planer often has insignificant statistics to know something and takes a guess. This has= gotten way better of recent years, but it's in particular for non-trivial = joins still a problem in practice.

> 3) I'm still a bit skeptical about the cost of this implementation - i= t
> builds the incrementalsort path, just to throw most of the paths away.=
> It'd be much better to just calculate the cost, which should be much > cheaper, and only do the heavy work for the one "best" path.=

Maybe we should profile this to get a rough estimate, how much time we spen= d building these incremental paths. From a code perspective it's non trivia= l to me where the time is lost.

> 4) One thing I did not realize before is what pathkeys we even conside= r
> here. Imagine you have two tables:
>
>     CREATE TABLE t1 (a int, b int, primary key (a)= );
>     CREATE TABLE t2 (a int, b int, primary key (a)= );
>
> and query
>
>     SELECT * FROM t1 JOIN t2 USING (a,b);
>
> It seems reasonable to also consider pathkeys (a,b) because that's mak= e
> e.g. mergejoin much cheaper, right? But no, we'll not do that - we onl= y
> consider pathkeys that at least one child relation has, so in this cas= e
> it's just (a). Which means we'll never consider incremental sort for > this particular example.
>
> It's a bit strange, because it's enough to create index on (a,b) for > just one of the relations, and it'll suddenly consider building
> incremental sort on both sides.

I don't find that surprising, because the single index *might* make the inc= remental sort cheaper for the join *without* considering any external sort = order.
So we would be switching up the incremental sort and the mergejoin, in case= we need to sort anyways. That would mean considering also the sort order, = that might be relevant on the outside. Sounds like an interesting idea for = a later patch.

> I don't plan to pursue this further at this point, so I pushed the fir= st
> part because that's a fairly simple improvement over what we have now.=
> But it seems like a fairly promising area for improvements.

I think 1) is pretty important, so we should sort that out sooner than late= r. Apart form that: :+1:
Thank you!

Regards
Arne

--_000_6c4cf676bc74402696018af08bc040afindexde_--