Allow ordered partition scans in more cases

Поиск
Список
Период
Сортировка
От David Rowley
Тема Allow ordered partition scans in more cases
Дата
Msg-id CAApHDvojKdBR3MR59JXmaCYbyHB6Q_5qPRU+dy93En8wm+XiDA@mail.gmail.com
обсуждение исходный текст
Ответы Re: Allow ordered partition scans in more cases  (Ronan Dunklau <ronan.dunklau@aiven.io>)
Список pgsql-hackers
Over on [1], Benjamin highlighted that we don't do ordered partition
scans in some cases where we could.

Basically, what was added in 959d00e9d only works when at least one
child path has pathkeys that suit the required query pathkeys.  If the
partitions or partitioned table does not have an index matching the
partitioning columns or some subset thereof, then we'll not add an
ordered Append path.

I've attached a patch.  This is what it does:

create table lp (a int) partition by list(a);
create table lp1 partition of lp for values in(1);
create table lp2 partition of lp for values in(2);

explain (costs off) select * from lp order by a;

master;

            QUERY PLAN
----------------------------------
 Sort
   Sort Key: lp.a
   ->  Append
         ->  Seq Scan on lp1 lp_1
         ->  Seq Scan on lp2 lp_2
(5 rows)

patched:

            QUERY PLAN
----------------------------------
 Append
   ->  Sort
         Sort Key: lp_1.a
         ->  Seq Scan on lp1 lp_1
   ->  Sort
         Sort Key: lp_2.a
         ->  Seq Scan on lp2 lp_2
(7 rows)

There's still something in there that I'm not happy with which relates
to the tests I added in inherit.sql. Anyone looking at the new tests
might expect that the following query should work too:

explain (costs off) select * from range_parted order by a,b,c;

but it *appears* not to.  We do build an AppendPath for that, it's
just that the AppendPath added by the following code seems to win over
it:

/*
* If we found unparameterized paths for all children, build an unordered,
* unparameterized Append path for the rel.  (Note: this is correct even
* if we have zero or one live subpath due to constraint exclusion.)
*/
if (subpaths_valid)
add_path(rel, (Path *) create_append_path(root, rel, subpaths, NIL,
  NIL, NULL, 0, false,
  -1));

I still need to look to see if there's some small amount of data that
can be loaded into the table to help coax the planner into producing
the ordered scan for this one.  It works fine as-is for ORDER BY a,b
and ORDER BY a; so I've put tests in for that.

David

[1] https://postgr.es/m/CABTcpyuXXY1625-Mns=mPFCVSf4aouGiRVyLPiGQQ0doT0PiLQ@mail.gmail.com

Вложения

В списке pgsql-hackers по дате отправления:

Предыдущее
От: Amit Langote
Дата:
Сообщение: Re: SQL/JSON revisited
Следующее
От: Thomas Munro
Дата:
Сообщение: Re: File descriptors in exec'd subprocesses