Allow ordered partition scans in more cases

From: David Rowley <dgrowleyml(at)gmail(dot)com>
To: PostgreSQL Developers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Allow ordered partition scans in more cases
Date: 2023-02-21 03:14:02
Message-ID: CAApHDvojKdBR3MR59JXmaCYbyHB6Q_5qPRU+dy93En8wm+XiDA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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

Attachment Content-Type Size
allow_more_ordered_partition_scans.patch text/plain 6.2 KB

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Thomas Munro 2023-02-21 03:59:59 Re: File descriptors in exec'd subprocesses
Previous Message Amit Langote 2023-02-21 03:09:15 Re: SQL/JSON revisited