Re: Improve choose_custom_plan for initial partition prune case

From: Andy Fan <zhihui(dot)fan1213(at)gmail(dot)com>
To: Amit Langote <amitlangote09(at)gmail(dot)com>
Cc: PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: Improve choose_custom_plan for initial partition prune case
Date: 2020-10-03 02:05:59
Message-ID: CAKU4AWrkx8-eZ3maTrsWvhYXro3brgYGR9qr3uaXtv66gCEU=w@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi Amit:

Very glad to see your comment!

On Fri, Oct 2, 2020 at 4:21 PM Amit Langote <amitlangote09(at)gmail(dot)com> wrote:

> Hi Andy,
>
> On Fri, Oct 2, 2020 at 1:04 AM Andy Fan <zhihui(dot)fan1213(at)gmail(dot)com> wrote:
> >
> > Given the plan example:
> >
> > CREATE TABLE measurement (
> > city_id int not null,
> > logdate date not null,
> > peaktemp int,
> > unitsales int
> > ) PARTITION BY RANGE (logdate);
> >
> > CREATE TABLE measurement_y2006m02 PARTITION OF measurement
> > FOR VALUES FROM ('2006-02-01') TO ('2006-03-01');
> >
> > CREATE TABLE measurement_y2006m03 PARTITION OF measurement
> > FOR VALUES FROM ('2006-03-01') TO ('2006-04-01');
> >
> > prepare s as select * from measurement where logdate = $1;
> > execute s('2006-02-01').
> >
> > The generic plan will probably not be chosen because it doesn't reduce
> the cost
> > which can be reduced at initial_prune while the custom plan reduces such
> cost
> > at planning time. which makes the cost comparison not fair.
>
> I agree that there is something to be done here. Actually, I think we
> should try to find a solution that will allow us to consider not just
> "initial" pruning, but also "execution-time" pruning. The latter
> will allow a nested loop join whose inner side scans a partitioned
> table using a parameterized scan on the partition key to be favored
> over other join plans, because that parameterized scan can use
> execution-time pruning which can make the inner scan very cheap.
>
>
This looks like to resolve another important issue of partition prune, which
may happen at planning time totally (no generic plan or custom plan
involved).
for example between choosing a Nest Loop plan which can use
some run-time partition prune and hash join which can't. I "repeat" your
idea
just to make sure I understand you correctly.

> > I'm thinking if we can
> > get an estimated cost reduction of initial_prunne for generic plan based
> on the
> > partition pruned at plan time from custom plan and then reducing
> > such costs from the generic plan. I just went through the related code
> but
> > didn't write anything now. I'd like to see if this is a correct
> direction to go.
>
> That's an interesting idea, that is, to try to do this totally outside
> the planner. When I was thinking about this a little while ago, I was
> trying to find a way to adjust the cost of the plan in the planner
> itself by looking at the runtime pruning info in the nodes that
> support it, that is, Append, MergeAppend. Actually, such an approach
> had also come up in the original run-time pruning discussion [1].
>
>
Thank you for your comments. Looks like your approach can be helpful
for the both cases, and I did think a bit for that as well, However, that
looks
complex (for me) AND I am prefer to guess how many partitions can be
pruned with real data even it is the real data in the past (I assume that
will not cause too much difference in practice).

I'm not sure if I should treat Robert's comments as an opposed idea[1] ,
but I think there are some little differences. I'd like to implement my
idea
soon, and I'm glad to see any opposed idea at any time, of course the
sooner
the better:)

[1]
https://www.postgresql.org/message-id/CA%2BTgmoZv8sd9cKyYtHwmd_13%2BBAjkVKo%3DECe7G98tBK5Ejwatw%40mail.gmail.com

--
Best Regards
Andy Fan

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Amit Langote 2020-10-03 02:42:21 Re: a misbehavior of partition row movement (?)
Previous Message Tomas Vondra 2020-10-03 00:49:59 Re: Parallel copy