1
0
mirror of https://github.com/postgres/postgres.git synced 2025-07-18 17:42:25 +03:00

Improve the heuristic for ordering child paths of a parallel append.

Commit ab7271677 introduced code that attempts to order the child
scans of a Parallel Append node in a way that will minimize execution
time, based on total cost and startup cost.  However, it failed to
think hard about what to do when estimated costs are exactly equal;
a case that's particularly likely to occur when comparing on startup
cost.  In such a case the ordering of the child paths would be left
to the whims of qsort, an algorithm that isn't even stable.

We can improve matters by applying the rule used elsewhere in the
planner: if total costs are equal, sort on startup cost, and
vice versa.  When both cost estimates are exactly equal, rather
than letting qsort do something unpredictable, sort based on the
child paths' relids, which should typically result in sorting in
inheritance order.  (The latter provision requires inventing a
qsort-style comparator for bitmapsets, but maybe we'll have use
for that for other reasons in future.)

This results in a few plan changes in the select_parallel test,
but those all look more reasonable than before, when the actual
underlying cost numbers are taken into account.

Discussion: https://postgr.es/m/4944.1515446989@sss.pgh.pa.us
This commit is contained in:
Tom Lane
2018-01-09 13:07:52 -05:00
parent 80259d4dbf
commit 624e440a47
4 changed files with 73 additions and 22 deletions

View File

@ -172,6 +172,50 @@ bms_equal(const Bitmapset *a, const Bitmapset *b)
return true;
}
/*
* bms_compare - qsort-style comparator for bitmapsets
*
* This guarantees to report values as equal iff bms_equal would say they are
* equal. Otherwise, the highest-numbered bit that is set in one value but
* not the other determines the result. (This rule means that, for example,
* {6} is greater than {5}, which seems plausible.)
*/
int
bms_compare(const Bitmapset *a, const Bitmapset *b)
{
int shortlen;
int i;
/* Handle cases where either input is NULL */
if (a == NULL)
return bms_is_empty(b) ? 0 : -1;
else if (b == NULL)
return bms_is_empty(a) ? 0 : +1;
/* Handle cases where one input is longer than the other */
shortlen = Min(a->nwords, b->nwords);
for (i = shortlen; i < a->nwords; i++)
{
if (a->words[i] != 0)
return +1;
}
for (i = shortlen; i < b->nwords; i++)
{
if (b->words[i] != 0)
return -1;
}
/* Process words in common */
i = shortlen;
while (--i >= 0)
{
bitmapword aw = a->words[i];
bitmapword bw = b->words[i];
if (aw != bw)
return (aw > bw) ? +1 : -1;
}
return 0;
}
/*
* bms_make_singleton - build a bitmapset containing a single member
*/
@ -838,7 +882,7 @@ bms_add_range(Bitmapset *a, int lower, int upper)
if (lwordnum == uwordnum)
{
a->words[lwordnum] |= ~(bitmapword) (((bitmapword) 1 << lbitnum) - 1)
& (~(bitmapword) 0) >> ushiftbits;
& (~(bitmapword) 0) >> ushiftbits;
}
else
{