On 2 April 2018 at 17:18, Amit Langote <langote_amit...@lab.ntt.co.jp> wrote: > On 2018/03/31 0:55, David Rowley wrote: >> explain (analyze, costs off, summary off, timing off) execute q1 (1,2,2,0); >> QUERY PLAN >> -------------------------------- >> Result (actual rows=0 loops=1) >> One-Time Filter: false >> (2 rows) > > Hmm. It is the newly added inversion step that's causing this. When > creating a generic plan (that is when the planning happens via > BuildCachedPlan called with boundParams set to NULL), the presence of > Params will cause an inversion step's source step to produce > scan-all-partitions sort of result, which the inversion step dutifully > inverts to a scan-no-partitions result. > > I have tried to attack that problem by handling the > no-values-to-prune-with case using a side-channel to propagate the > scan-all-partitions result through possibly multiple steps. That is, a > base pruning step will set datum_offsets in a PruneStepResult only if > pruning is carried out by actually comparing values with the partition > bounds. If no values were provided (like in the generic plan case), it > will set a scan_all_nonnull flag instead and return without setting > datum_offsets. Combine steps perform their combining duty only if > datum_offset contains a valid value, that is, if scan_all_nonnulls is not set.
I'm afraid this is still not correct :-( The following code is not doing the right thing: + case COMBINE_UNION: + foreach(lc1, cstep->source_stepids) + { + int step_id = lfirst_int(lc1); + PruneStepResult *step_result; + + /* + * step_results[step_id] must contain a valid result, + * which is confirmed by the fact that cstep's step_id is + * greater than step_id and the fact that results of the + * individual steps are evaluated in sequence of their + * step_ids. + */ + if (step_id >= cstep->step.step_id) + elog(ERROR, "invalid pruning combine step argument"); + step_result = step_results[step_id]; + Assert(step_result != NULL); + + result->scan_all_nonnull = step_result->scan_all_nonnull; The last line there is not properly performing a union, it just sets the result_scan_all_nonnull to whatever the last step's value was. At the very least it should be |= but I don't really like this new code. Why did you move away from just storing the matching partitions in a Bitmapset? If you want to store all non-null partitions, then why not just set the bits for all non-null partitions? That would cut down on bugs like this since the combining of step results would just be simple unions or intersects. Also, the following code could be made a bit nicer + result = (PruneStepResult *) palloc0(sizeof(PruneStepResult)); + + switch (context->strategy) + { + case PARTITION_STRATEGY_HASH: + result->bound_offsets = get_matching_hash_bound(context, + opstep->opstrategy, + values, nvalues, + partsupfunc, + opstep->nullkeys, + &result->scan_all_nonnull); Why not allocate the PruneStepResult inside the get_matching_*_bound, that way you wouldn't need all those out parameters to set the bool fields. -- David Rowley http://www.2ndQuadrant.com/ PostgreSQL Development, 24x7 Support, Training & Services