On 7th of April 2018, Alvaro Herrera committed patch:

Support partition pruning at execution time
 
 
Existing partition pruning is only able to work at plan time, for query
quals that appear in the parsed query.  This is good but limiting, as
there can be parameters that appear later that can be usefully used to
further prune partitions.
 
This commit adds support for pruning subnodes of Append which cannot
possibly contain any matching tuples, during execution, by evaluating
Params to determine the minimum set of subnodes that can possibly match.
We support more than just simple Params in WHERE clauses. Support
additionally includes:
 
1. Parameterized Nested Loop Joins: The parameter from the outer side of the
   join can be used to determine the minimum set of inner side partitions to
   scan.
 
2. Initplans: Once an initplan has been executed we can then determine which
   partitions match the value from the initplan.
 
Partition pruning is performed in two ways.  When Params external to the plan
are found to match the partition key we attempt to prune away unneeded Append
subplans during the initialization of the executor.  This allows us to bypass
the initialization of non-matching subplans meaning they won't appear in the
EXPLAIN or EXPLAIN ANALYZE output.
 
For parameters whose value is only known during the actual execution
then the pruning of these subplans must wait.  Subplans which are
eliminated during this stage of pruning are still visible in the EXPLAIN
output.  In order to determine if pruning has actually taken place, the
EXPLAIN ANALYZE must be viewed.  If a certain Append subplan was never
executed due to the elimination of the partition then the execution
timing area will state "(never executed)".  Whereas, if, for example in
the case of parameterized nested loops, the number of loops stated in
the EXPLAIN ANALYZE output for certain subplans may appear lower than
others due to the subplan having been scanned fewer times.  This is due
to the list of matching subnodes having to be evaluated whenever a
parameter which was found to match the partition key changes.
 
This commit required some additional infrastructure that permits the
building of a data structure which is able to perform the translation of
the matching partition IDs, as returned by get_matching_partitions, into
the list index of a subpaths list, as exist in node types such as
Append, MergeAppend and ModifyTable.  This allows us to translate a list
of clauses into a Bitmapset of all the subpath indexes which must be
included to satisfy the clause list.
 
Author: David Rowley, based on an earlier effort by Beena Emerson
Reviewers: Amit Langote, Robert Haas, Amul Sul, Rajkumar Raghuwanshi,
Jesper Pedersen
Discussion: https://postgr.es/m/CAOG9ApE16ac-_VVZVvv0gePSgkg_BwYEV1NBqZFqDR2bBE0X0A@mail.gmail.com

Long commit message. Long and pretty good in explaining what is going on, so let's just see the result for ourselves.

First, sample data:

create table users (
    id             serial not null,
    username       text not null,
    password       text
)
PARTITION BY RANGE ( id );
 
CREATE TABLE users_0
    partition of users (id, primary key (id), unique (username))
    for values from (minvalue) to (10);
 
CREATE TABLE users_1
    partition of users (id, primary key (id), unique (username))
    for values from (10) to (20);
CREATE TABLE users_2
    partition of users (id, primary key (id), unique (username))
    for values from (20) to (30);
CREATE TABLE users_3
    partition of users (id, primary key (id), unique (username))
    for values from (30) to (maxvalue);
 
INSERT INTO users (username) SELECT 'u:' || i::text FROM generate_series(1,25) i;
 
CREATE TABLE x (u_id INT4);
 
INSERT INTO x (u_id) VALUES (13);
 
ANALYZE;

Pretty straightforward. Now, let's see execution plan with hard-coded id:

=$ explain analyze SELECT * FROM users WHERE id = 13;
                                               QUERY PLAN
--------------------------------------------------------------------------------------------------------
 Append  (cost=0.00..1.12 rows=1 width=41) (actual time=0.012..0.017 rows=1 loops=1)
   ->  Seq Scan on users_1  (cost=0.00..1.12 rows=1 width=41) (actual time=0.009..0.011 rows=1 loops=1)
         Filter: (id = 13)
         Rows Removed by Filter: 9
 Planning time: 0.549 ms
 Execution time: 0.040 ms
(6 rows)

As we can see only one partition was scanned. All good.

Now, let's see what happens if id is taken from subselect:

=$ explain analyze SELECT * FROM users WHERE id = (SELECT u_id FROM x);
                                               QUERY PLAN
--------------------------------------------------------------------------------------------------------
 Append  (cost=1.01..12.51 rows=4 width=48) (actual time=0.007..0.007 rows=1 loops=1)
   InitPlan 1 (returns $0)
     ->  Seq Scan on x  (cost=0.00..1.01 rows=1 width=4) (actual time=0.002..0.002 rows=1 loops=1)
   ->  Seq Scan on users_0  (cost=0.00..1.11 rows=1 width=40) (never executed)
         Filter: (id = $0)
   ->  Seq Scan on users_1  (cost=0.00..1.12 rows=1 width=41) (actual time=0.002..0.002 rows=1 loops=1)
         Filter: (id = $0)
         Rows Removed by Filter: 9
   ->  Seq Scan on users_2  (cost=0.00..1.07 rows=1 width=41) (never executed)
         Filter: (id = $0)
   ->  Index Scan using users_3_pkey on users_3  (cost=0.15..8.17 rows=1 width=68) (never executed)
         Index Cond: (id = $0)
 Planning Time: 0.104 ms
 Execution Time: 0.021 ms
(14 rows)

for comparison, in Pg 10, explain analyze for query above looked like:

                                                         QUERY PLAN
-----------------------------------------------------------------------------------------------------------------------------
 Append  (cost=1.01..12.49 rows=4 width=48) (actual time=0.025..0.058 rows=1 loops=1)
   InitPlan 1 (returns $0)
     ->  Seq Scan on x  (cost=0.00..1.01 rows=1 width=4) (actual time=0.003..0.005 rows=1 loops=1)
   ->  Seq Scan on users_0  (cost=0.00..1.11 rows=1 width=40) (actual time=0.017..0.017 rows=0 loops=1)
         Filter: (id = $0)
         Rows Removed by Filter: 9
   ->  Seq Scan on users_1  (cost=0.00..1.12 rows=1 width=41) (actual time=0.003..0.005 rows=1 loops=1)
         Filter: (id = $0)
         Rows Removed by Filter: 9
   ->  Seq Scan on users_2  (cost=0.00..1.07 rows=1 width=41) (actual time=0.003..0.003 rows=0 loops=1)
         Filter: (id = $0)
         Rows Removed by Filter: 6
   ->  Index Scan using users_3_pkey on users_3  (cost=0.15..8.17 rows=1 width=68) (actual time=0.023..0.023 rows=0 loops=1)
         Index Cond: (id = $0)
 Planning time: 0.238 ms
 Execution time: 0.090 ms
(16 rows)

The important part is that when old Pg (well, 10.3 is not so old, but it's not the git HEAD) had loops=1 on all partitions, in Pg 11 only one partition – the correct one, of course, was actually scanned. In case of all other partitions we can see “never executed", which is great sign that Pg didn't waste any cycles on work that it could skip.

Great stuff, thanks a lot to all involved.

Leave a comment