Hi Andy, On Fri, Oct 2, 2020 at 1:04 AM Andy Fan <zhihui.fan1...@gmail.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. > 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]. -- Amit Langote EDB: http://www.enterprisedb.com [1] https://www.postgresql.org/message-id/CA%2BTgmoZHYoAL4HYwnGO25B8CxCB%2BvNMdf%2B7rbUzYykR4sU9yUA%40mail.gmail.com