|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* pathkeys.c
|
|
|
|
* Utilities for matching and building path keys
|
|
|
|
*
|
|
|
|
* See src/backend/optimizer/README for a great deal of information about
|
|
|
|
* the nature and use of path keys.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* Portions Copyright (c) 1996-2021, PostgreSQL Global Development Group
|
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
|
|
|
* src/backend/optimizer/path/pathkeys.c
|
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
#include "postgres.h"
|
|
|
|
|
|
|
|
#include "access/stratnum.h"
|
Use Append rather than MergeAppend for scanning ordered partitions.
If we need ordered output from a scan of a partitioned table, but
the ordering matches the partition ordering, then we don't need to
use a MergeAppend to combine the pre-ordered per-partition scan
results: a plain Append will produce the same results. This
both saves useless comparison work inside the MergeAppend proper,
and allows us to start returning tuples after istarting up just
the first child node not all of them.
However, all is not peaches and cream, because if some of the
child nodes have high startup costs then there will be big
discontinuities in the tuples-returned-versus-elapsed-time curve.
The planner's cost model cannot handle that (yet, anyway).
If we model the Append's startup cost as being just the first
child's startup cost, we may drastically underestimate the cost
of fetching slightly more tuples than are available from the first
child. Since we've had bad experiences with over-optimistic choices
of "fast start" plans for ORDER BY LIMIT queries, that seems scary.
As a klugy workaround, set the startup cost estimate for an ordered
Append to be the sum of its children's startup costs (as MergeAppend
would). This doesn't really describe reality, but it's less likely
to cause a bad plan choice than an underestimated startup cost would.
In practice, the cases where we really care about this optimization
will have child plans that are IndexScans with zero startup cost,
so that the overly conservative estimate is still just zero.
David Rowley, reviewed by Julien Rouhaud and Antonin Houska
Discussion: https://postgr.es/m/CAKJS1f-hAqhPLRk_RaSFTgYxd=Tz5hA7kQ2h4-DhJufQk8TGuw@mail.gmail.com
7 years ago
|
|
|
#include "catalog/pg_opfamily.h"
|
|
|
|
#include "nodes/makefuncs.h"
|
|
|
|
#include "nodes/nodeFuncs.h"
|
|
|
|
#include "nodes/plannodes.h"
|
|
|
|
#include "optimizer/optimizer.h"
|
|
|
|
#include "optimizer/pathnode.h"
|
|
|
|
#include "optimizer/paths.h"
|
Use Append rather than MergeAppend for scanning ordered partitions.
If we need ordered output from a scan of a partitioned table, but
the ordering matches the partition ordering, then we don't need to
use a MergeAppend to combine the pre-ordered per-partition scan
results: a plain Append will produce the same results. This
both saves useless comparison work inside the MergeAppend proper,
and allows us to start returning tuples after istarting up just
the first child node not all of them.
However, all is not peaches and cream, because if some of the
child nodes have high startup costs then there will be big
discontinuities in the tuples-returned-versus-elapsed-time curve.
The planner's cost model cannot handle that (yet, anyway).
If we model the Append's startup cost as being just the first
child's startup cost, we may drastically underestimate the cost
of fetching slightly more tuples than are available from the first
child. Since we've had bad experiences with over-optimistic choices
of "fast start" plans for ORDER BY LIMIT queries, that seems scary.
As a klugy workaround, set the startup cost estimate for an ordered
Append to be the sum of its children's startup costs (as MergeAppend
would). This doesn't really describe reality, but it's less likely
to cause a bad plan choice than an underestimated startup cost would.
In practice, the cases where we really care about this optimization
will have child plans that are IndexScans with zero startup cost,
so that the overly conservative estimate is still just zero.
David Rowley, reviewed by Julien Rouhaud and Antonin Houska
Discussion: https://postgr.es/m/CAKJS1f-hAqhPLRk_RaSFTgYxd=Tz5hA7kQ2h4-DhJufQk8TGuw@mail.gmail.com
7 years ago
|
|
|
#include "partitioning/partbounds.h"
|
|
|
|
#include "utils/lsyscache.h"
|
|
|
|
|
|
|
|
|
|
|
|
static bool pathkey_is_redundant(PathKey *new_pathkey, List *pathkeys);
|
Use Append rather than MergeAppend for scanning ordered partitions.
If we need ordered output from a scan of a partitioned table, but
the ordering matches the partition ordering, then we don't need to
use a MergeAppend to combine the pre-ordered per-partition scan
results: a plain Append will produce the same results. This
both saves useless comparison work inside the MergeAppend proper,
and allows us to start returning tuples after istarting up just
the first child node not all of them.
However, all is not peaches and cream, because if some of the
child nodes have high startup costs then there will be big
discontinuities in the tuples-returned-versus-elapsed-time curve.
The planner's cost model cannot handle that (yet, anyway).
If we model the Append's startup cost as being just the first
child's startup cost, we may drastically underestimate the cost
of fetching slightly more tuples than are available from the first
child. Since we've had bad experiences with over-optimistic choices
of "fast start" plans for ORDER BY LIMIT queries, that seems scary.
As a klugy workaround, set the startup cost estimate for an ordered
Append to be the sum of its children's startup costs (as MergeAppend
would). This doesn't really describe reality, but it's less likely
to cause a bad plan choice than an underestimated startup cost would.
In practice, the cases where we really care about this optimization
will have child plans that are IndexScans with zero startup cost,
so that the overly conservative estimate is still just zero.
David Rowley, reviewed by Julien Rouhaud and Antonin Houska
Discussion: https://postgr.es/m/CAKJS1f-hAqhPLRk_RaSFTgYxd=Tz5hA7kQ2h4-DhJufQk8TGuw@mail.gmail.com
7 years ago
|
|
|
static bool matches_boolean_partition_clause(RestrictInfo *rinfo,
|
|
|
|
RelOptInfo *partrel,
|
|
|
|
int partkeycol);
|
Repair issues with faulty generation of merge-append plans.
create_merge_append_plan failed to honor the CP_EXACT_TLIST flag:
it would generate the expected targetlist but then it felt free to
add resjunk sort targets to it. This demonstrably leads to assertion
failures in v11 and HEAD, and it's probably just accidental that we
don't see the same in older branches. I've not looked into whether
there would be any real-world consequences in non-assert builds.
In HEAD, create_append_plan has sprouted the same problem, so fix
that too (although we do not have any test cases that seem able to
reach that bug). This is an oversight in commit 3fc6e2d7f which
invented the CP_EXACT_TLIST flag, so back-patch to 9.6 where that
came in.
convert_subquery_pathkeys would create pathkeys for subquery output
values if they match any EquivalenceClass known in the outer query
and are available in the subquery's syntactic targetlist. However,
the second part of that condition is wrong, because such values might
not appear in the subquery relation's reltarget list, which would
mean that they couldn't be accessed above the level of the subquery
scan. We must check that they appear in the reltarget list, instead.
This can lead to dropping knowledge about the subquery's sort
ordering, but I believe it's okay, because any sort key that the
outer query actually has any interest in would appear in the
reltarget list.
This second issue is of very long standing, but right now there's no
evidence that it causes observable problems before 9.6, so I refrained
from back-patching further than that. We can revisit that choice if
somebody finds a way to make it cause problems in older branches.
(Developing useful test cases for these issues is really problematic;
fixing convert_subquery_pathkeys removes the only known way to exhibit
the create_merge_append_plan bug, and neither of the test cases added
by this patch causes a problem in all branches, even when considering
the issues separately.)
The second issue explains bug #15795 from Suresh Kumar R ("could not
find pathkey item to sort" with nested DISTINCT queries). I stumbled
across the first issue while investigating that.
Discussion: https://postgr.es/m/15795-fadb56c8e44ee73c@postgresql.org
6 years ago
|
|
|
static Var *find_var_for_subquery_tle(RelOptInfo *rel, TargetEntry *tle);
|
|
|
|
static bool right_merge_direction(PlannerInfo *root, PathKey *pathkey);
|
|
|
|
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* PATHKEY CONSTRUCTION AND REDUNDANCY TESTING
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* make_canonical_pathkey
|
|
|
|
* Given the parameters for a PathKey, find any pre-existing matching
|
|
|
|
* pathkey in the query's list of "canonical" pathkeys. Make a new
|
|
|
|
* entry if there's not one already.
|
Teach planner about some cases where a restriction clause can be
propagated inside an outer join. In particular, given
LEFT JOIN ON (A = B) WHERE A = constant, we cannot conclude that
B = constant at the top level (B might be null instead), but we
can nonetheless put a restriction B = constant into the quals for
B's relation, since no inner-side rows not meeting that condition
can contribute to the final result. Similarly, given
FULL JOIN USING (J) WHERE J = constant, we can't directly conclude
that either input J variable = constant, but it's OK to push such
quals into each input rel. Per recent gripe from Kim Bisgaard.
Along the way, remove 'valid_everywhere' flag from RestrictInfo,
as on closer analysis it was not being used for anything, and was
defined backwards anyway.
21 years ago
|
|
|
*
|
|
|
|
* Note that this function must not be used until after we have completed
|
Speed up finding EquivalenceClasses for a given set of rels
Previously in order to determine which ECs a relation had members in, we
had to loop over all ECs stored in PlannerInfo's eq_classes and check if
ec_relids mentioned the relation. For the most part, this was fine, as
generally, unless queries were fairly complex, the overhead of performing
the lookup would have not been that significant. However, when queries
contained large numbers of joins and ECs, the overhead to find the set of
classes matching a given set of relations could become a significant
portion of the overall planning effort.
Here we allow a much more efficient method to access the ECs which match a
given relation or set of relations. A new Bitmapset field in RelOptInfo
now exists to store the indexes into PlannerInfo's eq_classes list which
each relation is mentioned in. This allows very fast lookups to find all
ECs belonging to a single relation. When we need to lookup ECs belonging
to a given pair of relations, we can simply bitwise-AND the Bitmapsets from
each relation and use the result to perform the lookup.
We also take the opportunity to write a new implementation of
generate_join_implied_equalities which makes use of the new indexes.
generate_join_implied_equalities_for_ecs must remain as is as it can be
given a custom list of ECs, which we can't easily determine the indexes of.
This was originally intended to fix the performance penalty of looking up
foreign keys matching a join condition which was introduced by 100340e2d.
However, we're speeding up much more than just that here.
Author: David Rowley, Tom Lane
Reviewed-by: Tom Lane, Tomas Vondra
Discussion: https://postgr.es/m/6970.1545327857@sss.pgh.pa.us
6 years ago
|
|
|
* merging EquivalenceClasses.
|
|
|
|
*/
|
|
|
|
PathKey *
|
|
|
|
make_canonical_pathkey(PlannerInfo *root,
|
|
|
|
EquivalenceClass *eclass, Oid opfamily,
|
|
|
|
int strategy, bool nulls_first)
|
|
|
|
{
|
|
|
|
PathKey *pk;
|
|
|
|
ListCell *lc;
|
|
|
|
MemoryContext oldcontext;
|
|
|
|
|
Speed up finding EquivalenceClasses for a given set of rels
Previously in order to determine which ECs a relation had members in, we
had to loop over all ECs stored in PlannerInfo's eq_classes and check if
ec_relids mentioned the relation. For the most part, this was fine, as
generally, unless queries were fairly complex, the overhead of performing
the lookup would have not been that significant. However, when queries
contained large numbers of joins and ECs, the overhead to find the set of
classes matching a given set of relations could become a significant
portion of the overall planning effort.
Here we allow a much more efficient method to access the ECs which match a
given relation or set of relations. A new Bitmapset field in RelOptInfo
now exists to store the indexes into PlannerInfo's eq_classes list which
each relation is mentioned in. This allows very fast lookups to find all
ECs belonging to a single relation. When we need to lookup ECs belonging
to a given pair of relations, we can simply bitwise-AND the Bitmapsets from
each relation and use the result to perform the lookup.
We also take the opportunity to write a new implementation of
generate_join_implied_equalities which makes use of the new indexes.
generate_join_implied_equalities_for_ecs must remain as is as it can be
given a custom list of ECs, which we can't easily determine the indexes of.
This was originally intended to fix the performance penalty of looking up
foreign keys matching a join condition which was introduced by 100340e2d.
However, we're speeding up much more than just that here.
Author: David Rowley, Tom Lane
Reviewed-by: Tom Lane, Tomas Vondra
Discussion: https://postgr.es/m/6970.1545327857@sss.pgh.pa.us
6 years ago
|
|
|
/* Can't make canonical pathkeys if the set of ECs might still change */
|
|
|
|
if (!root->ec_merging_done)
|
|
|
|
elog(ERROR, "too soon to build canonical pathkeys");
|
|
|
|
|
|
|
|
/* The passed eclass might be non-canonical, so chase up to the top */
|
|
|
|
while (eclass->ec_merged)
|
|
|
|
eclass = eclass->ec_merged;
|
|
|
|
|
|
|
|
foreach(lc, root->canon_pathkeys)
|
|
|
|
{
|
|
|
|
pk = (PathKey *) lfirst(lc);
|
|
|
|
if (eclass == pk->pk_eclass &&
|
|
|
|
opfamily == pk->pk_opfamily &&
|
|
|
|
strategy == pk->pk_strategy &&
|
|
|
|
nulls_first == pk->pk_nulls_first)
|
|
|
|
return pk;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Be sure canonical pathkeys are allocated in the main planning context.
|
|
|
|
* Not an issue in normal planning, but it is for GEQO.
|
|
|
|
*/
|
|
|
|
oldcontext = MemoryContextSwitchTo(root->planner_cxt);
|
|
|
|
|
Postpone creation of pathkeys lists to fix bug #8049.
This patch gets rid of the concept of, and infrastructure for,
non-canonical PathKeys; we now only ever create canonical pathkey lists.
The need for non-canonical pathkeys came from the desire to have
grouping_planner initialize query_pathkeys and related pathkey lists before
calling query_planner. However, since query_planner didn't actually *do*
anything with those lists before they'd been made canonical, we can get rid
of the whole mess by just not creating the lists at all until the point
where we formerly canonicalized them.
There are several ways in which we could implement that without making
query_planner itself deal with grouping/sorting features (which are
supposed to be the province of grouping_planner). I chose to add a
callback function to query_planner's API; other alternatives would have
required adding more fields to PlannerInfo, which while not bad in itself
would create an ABI break for planner-related plugins in the 9.2 release
series. This still breaks ABI for anything that calls query_planner
directly, but it seems somewhat unlikely that there are any such plugins.
I had originally conceived of this change as merely a step on the way to
fixing bug #8049 from Teun Hoogendoorn; but it turns out that this fixes
that bug all by itself, as per the added regression test. The reason is
that now get_eclass_for_sort_expr is adding the ORDER BY expression at the
end of EquivalenceClass creation not the start, and so anything that is in
a multi-member EquivalenceClass has already been created with correct
em_nullable_relids. I am suspicious that there are related scenarios in
which we still need to teach get_eclass_for_sort_expr to compute correct
nullable_relids, but am not eager to risk destabilizing either 9.2 or 9.3
to fix bugs that are only hypothetical. So for the moment, do this and
stop here.
Back-patch to 9.2 but not to earlier branches, since they don't exhibit
this bug for lack of join-clause-movement logic that depends on
em_nullable_relids being correct. (We might have to revisit that choice
if any related bugs turn up.) In 9.2, don't change the signature of
make_pathkeys_for_sortclauses nor remove canonicalize_pathkeys, so as
not to risk more plugin breakage than we have to.
13 years ago
|
|
|
pk = makeNode(PathKey);
|
|
|
|
pk->pk_eclass = eclass;
|
|
|
|
pk->pk_opfamily = opfamily;
|
|
|
|
pk->pk_strategy = strategy;
|
|
|
|
pk->pk_nulls_first = nulls_first;
|
|
|
|
|
|
|
|
root->canon_pathkeys = lappend(root->canon_pathkeys, pk);
|
|
|
|
|
|
|
|
MemoryContextSwitchTo(oldcontext);
|
|
|
|
|
|
|
|
return pk;
|
|
|
|
}
|
|
|
|
|
Teach planner about some cases where a restriction clause can be
propagated inside an outer join. In particular, given
LEFT JOIN ON (A = B) WHERE A = constant, we cannot conclude that
B = constant at the top level (B might be null instead), but we
can nonetheless put a restriction B = constant into the quals for
B's relation, since no inner-side rows not meeting that condition
can contribute to the final result. Similarly, given
FULL JOIN USING (J) WHERE J = constant, we can't directly conclude
that either input J variable = constant, but it's OK to push such
quals into each input rel. Per recent gripe from Kim Bisgaard.
Along the way, remove 'valid_everywhere' flag from RestrictInfo,
as on closer analysis it was not being used for anything, and was
defined backwards anyway.
21 years ago
|
|
|
/*
|
|
|
|
* pathkey_is_redundant
|
|
|
|
* Is a pathkey redundant with one already in the given list?
|
Teach planner about some cases where a restriction clause can be
propagated inside an outer join. In particular, given
LEFT JOIN ON (A = B) WHERE A = constant, we cannot conclude that
B = constant at the top level (B might be null instead), but we
can nonetheless put a restriction B = constant into the quals for
B's relation, since no inner-side rows not meeting that condition
can contribute to the final result. Similarly, given
FULL JOIN USING (J) WHERE J = constant, we can't directly conclude
that either input J variable = constant, but it's OK to push such
quals into each input rel. Per recent gripe from Kim Bisgaard.
Along the way, remove 'valid_everywhere' flag from RestrictInfo,
as on closer analysis it was not being used for anything, and was
defined backwards anyway.
21 years ago
|
|
|
*
|
Postpone creation of pathkeys lists to fix bug #8049.
This patch gets rid of the concept of, and infrastructure for,
non-canonical PathKeys; we now only ever create canonical pathkey lists.
The need for non-canonical pathkeys came from the desire to have
grouping_planner initialize query_pathkeys and related pathkey lists before
calling query_planner. However, since query_planner didn't actually *do*
anything with those lists before they'd been made canonical, we can get rid
of the whole mess by just not creating the lists at all until the point
where we formerly canonicalized them.
There are several ways in which we could implement that without making
query_planner itself deal with grouping/sorting features (which are
supposed to be the province of grouping_planner). I chose to add a
callback function to query_planner's API; other alternatives would have
required adding more fields to PlannerInfo, which while not bad in itself
would create an ABI break for planner-related plugins in the 9.2 release
series. This still breaks ABI for anything that calls query_planner
directly, but it seems somewhat unlikely that there are any such plugins.
I had originally conceived of this change as merely a step on the way to
fixing bug #8049 from Teun Hoogendoorn; but it turns out that this fixes
that bug all by itself, as per the added regression test. The reason is
that now get_eclass_for_sort_expr is adding the ORDER BY expression at the
end of EquivalenceClass creation not the start, and so anything that is in
a multi-member EquivalenceClass has already been created with correct
em_nullable_relids. I am suspicious that there are related scenarios in
which we still need to teach get_eclass_for_sort_expr to compute correct
nullable_relids, but am not eager to risk destabilizing either 9.2 or 9.3
to fix bugs that are only hypothetical. So for the moment, do this and
stop here.
Back-patch to 9.2 but not to earlier branches, since they don't exhibit
this bug for lack of join-clause-movement logic that depends on
em_nullable_relids being correct. (We might have to revisit that choice
if any related bugs turn up.) In 9.2, don't change the signature of
make_pathkeys_for_sortclauses nor remove canonicalize_pathkeys, so as
not to risk more plugin breakage than we have to.
13 years ago
|
|
|
* We detect two cases:
|
Teach planner about some cases where a restriction clause can be
propagated inside an outer join. In particular, given
LEFT JOIN ON (A = B) WHERE A = constant, we cannot conclude that
B = constant at the top level (B might be null instead), but we
can nonetheless put a restriction B = constant into the quals for
B's relation, since no inner-side rows not meeting that condition
can contribute to the final result. Similarly, given
FULL JOIN USING (J) WHERE J = constant, we can't directly conclude
that either input J variable = constant, but it's OK to push such
quals into each input rel. Per recent gripe from Kim Bisgaard.
Along the way, remove 'valid_everywhere' flag from RestrictInfo,
as on closer analysis it was not being used for anything, and was
defined backwards anyway.
21 years ago
|
|
|
*
|
|
|
|
* 1. If the new pathkey's equivalence class contains a constant, and isn't
|
|
|
|
* below an outer join, then we can disregard it as a sort key. An example:
|
|
|
|
* SELECT ... WHERE x = 42 ORDER BY x, y;
|
|
|
|
* We may as well just sort by y. Note that because of opfamily matching,
|
|
|
|
* this is semantically correct: we know that the equality constraint is one
|
|
|
|
* that actually binds the variable to a single value in the terms of any
|
|
|
|
* ordering operator that might go with the eclass. This rule not only lets
|
|
|
|
* us simplify (or even skip) explicit sorts, but also allows matching index
|
|
|
|
* sort orders to a query when there are don't-care index columns.
|
Teach planner about some cases where a restriction clause can be
propagated inside an outer join. In particular, given
LEFT JOIN ON (A = B) WHERE A = constant, we cannot conclude that
B = constant at the top level (B might be null instead), but we
can nonetheless put a restriction B = constant into the quals for
B's relation, since no inner-side rows not meeting that condition
can contribute to the final result. Similarly, given
FULL JOIN USING (J) WHERE J = constant, we can't directly conclude
that either input J variable = constant, but it's OK to push such
quals into each input rel. Per recent gripe from Kim Bisgaard.
Along the way, remove 'valid_everywhere' flag from RestrictInfo,
as on closer analysis it was not being used for anything, and was
defined backwards anyway.
21 years ago
|
|
|
*
|
|
|
|
* 2. If the new pathkey's equivalence class is the same as that of any
|
|
|
|
* existing member of the pathkey list, then it is redundant. Some examples:
|
|
|
|
* SELECT ... ORDER BY x, x;
|
|
|
|
* SELECT ... ORDER BY x, x DESC;
|
|
|
|
* SELECT ... WHERE x = y ORDER BY x, y;
|
|
|
|
* In all these cases the second sort key cannot distinguish values that are
|
|
|
|
* considered equal by the first, and so there's no point in using it.
|
|
|
|
* Note in particular that we need not compare opfamily (all the opfamilies
|
|
|
|
* of the EC have the same notion of equality) nor sort direction.
|
|
|
|
*
|
Postpone creation of pathkeys lists to fix bug #8049.
This patch gets rid of the concept of, and infrastructure for,
non-canonical PathKeys; we now only ever create canonical pathkey lists.
The need for non-canonical pathkeys came from the desire to have
grouping_planner initialize query_pathkeys and related pathkey lists before
calling query_planner. However, since query_planner didn't actually *do*
anything with those lists before they'd been made canonical, we can get rid
of the whole mess by just not creating the lists at all until the point
where we formerly canonicalized them.
There are several ways in which we could implement that without making
query_planner itself deal with grouping/sorting features (which are
supposed to be the province of grouping_planner). I chose to add a
callback function to query_planner's API; other alternatives would have
required adding more fields to PlannerInfo, which while not bad in itself
would create an ABI break for planner-related plugins in the 9.2 release
series. This still breaks ABI for anything that calls query_planner
directly, but it seems somewhat unlikely that there are any such plugins.
I had originally conceived of this change as merely a step on the way to
fixing bug #8049 from Teun Hoogendoorn; but it turns out that this fixes
that bug all by itself, as per the added regression test. The reason is
that now get_eclass_for_sort_expr is adding the ORDER BY expression at the
end of EquivalenceClass creation not the start, and so anything that is in
a multi-member EquivalenceClass has already been created with correct
em_nullable_relids. I am suspicious that there are related scenarios in
which we still need to teach get_eclass_for_sort_expr to compute correct
nullable_relids, but am not eager to risk destabilizing either 9.2 or 9.3
to fix bugs that are only hypothetical. So for the moment, do this and
stop here.
Back-patch to 9.2 but not to earlier branches, since they don't exhibit
this bug for lack of join-clause-movement logic that depends on
em_nullable_relids being correct. (We might have to revisit that choice
if any related bugs turn up.) In 9.2, don't change the signature of
make_pathkeys_for_sortclauses nor remove canonicalize_pathkeys, so as
not to risk more plugin breakage than we have to.
13 years ago
|
|
|
* Both the given pathkey and the list members must be canonical for this
|
|
|
|
* to work properly, but that's okay since we no longer ever construct any
|
|
|
|
* non-canonical pathkeys. (Note: the notion of a pathkey *list* being
|
Postpone creation of pathkeys lists to fix bug #8049.
This patch gets rid of the concept of, and infrastructure for,
non-canonical PathKeys; we now only ever create canonical pathkey lists.
The need for non-canonical pathkeys came from the desire to have
grouping_planner initialize query_pathkeys and related pathkey lists before
calling query_planner. However, since query_planner didn't actually *do*
anything with those lists before they'd been made canonical, we can get rid
of the whole mess by just not creating the lists at all until the point
where we formerly canonicalized them.
There are several ways in which we could implement that without making
query_planner itself deal with grouping/sorting features (which are
supposed to be the province of grouping_planner). I chose to add a
callback function to query_planner's API; other alternatives would have
required adding more fields to PlannerInfo, which while not bad in itself
would create an ABI break for planner-related plugins in the 9.2 release
series. This still breaks ABI for anything that calls query_planner
directly, but it seems somewhat unlikely that there are any such plugins.
I had originally conceived of this change as merely a step on the way to
fixing bug #8049 from Teun Hoogendoorn; but it turns out that this fixes
that bug all by itself, as per the added regression test. The reason is
that now get_eclass_for_sort_expr is adding the ORDER BY expression at the
end of EquivalenceClass creation not the start, and so anything that is in
a multi-member EquivalenceClass has already been created with correct
em_nullable_relids. I am suspicious that there are related scenarios in
which we still need to teach get_eclass_for_sort_expr to compute correct
nullable_relids, but am not eager to risk destabilizing either 9.2 or 9.3
to fix bugs that are only hypothetical. So for the moment, do this and
stop here.
Back-patch to 9.2 but not to earlier branches, since they don't exhibit
this bug for lack of join-clause-movement logic that depends on
em_nullable_relids being correct. (We might have to revisit that choice
if any related bugs turn up.) In 9.2, don't change the signature of
make_pathkeys_for_sortclauses nor remove canonicalize_pathkeys, so as
not to risk more plugin breakage than we have to.
13 years ago
|
|
|
* canonical includes the additional requirement of no redundant entries,
|
|
|
|
* which is exactly what we are checking for here.)
|
|
|
|
*
|
|
|
|
* Because the equivclass.c machinery forms only one copy of any EC per query,
|
|
|
|
* pointer comparison is enough to decide whether canonical ECs are the same.
|
|
|
|
*/
|
|
|
|
static bool
|
|
|
|
pathkey_is_redundant(PathKey *new_pathkey, List *pathkeys)
|
|
|
|
{
|
|
|
|
EquivalenceClass *new_ec = new_pathkey->pk_eclass;
|
|
|
|
ListCell *lc;
|
|
|
|
|
|
|
|
/* Check for EC containing a constant --- unconditionally redundant */
|
Fix some planner issues found while investigating Kevin Grittner's report
of poorer planning in 8.3 than 8.2:
1. After pushing a constant across an outer join --- ie, given
"a LEFT JOIN b ON (a.x = b.y) WHERE a.x = 42", we can deduce that b.y is
sort of equal to 42, in the sense that we needn't fetch any b rows where
it isn't 42 --- loop to see if any additional deductions can be made.
Previous releases did that by recursing, but I had mistakenly thought that
this was no longer necessary given the EquivalenceClass machinery.
2. Allow pushing constants across outer join conditions even if the
condition is outerjoin_delayed due to a lower outer join. This is safe
as long as the condition is strict and we re-test it at the upper join.
3. Keep the outer-join clause even if we successfully push a constant
across it. This is *necessary* in the outerjoin_delayed case, but
even in the simple case, it seems better to do this to ensure that the
join search order heuristics will consider the join as reasonable to
make. Mark such a clause as having selectivity 1.0, though, since it's
not going to eliminate very many rows after application of the constant
condition.
4. Tweak have_relevant_eclass_joinclause to report that two relations
are joinable when they have vars that are equated to the same constant.
We won't actually generate any joinclause from such an EquivalenceClass,
but again it seems that in such a case it's a good idea to consider
the join as worth costing out.
5. Fix a bug in select_mergejoin_clauses that was exposed by these
changes: we have to reject candidate mergejoin clauses if either side was
equated to a constant, because we can't construct a canonical pathkey list
for such a clause. This is an implementation restriction that might be
worth fixing someday, but it doesn't seem critical to get it done for 8.3.
18 years ago
|
|
|
if (EC_MUST_BE_REDUNDANT(new_ec))
|
|
|
|
return true;
|
Teach planner about some cases where a restriction clause can be
propagated inside an outer join. In particular, given
LEFT JOIN ON (A = B) WHERE A = constant, we cannot conclude that
B = constant at the top level (B might be null instead), but we
can nonetheless put a restriction B = constant into the quals for
B's relation, since no inner-side rows not meeting that condition
can contribute to the final result. Similarly, given
FULL JOIN USING (J) WHERE J = constant, we can't directly conclude
that either input J variable = constant, but it's OK to push such
quals into each input rel. Per recent gripe from Kim Bisgaard.
Along the way, remove 'valid_everywhere' flag from RestrictInfo,
as on closer analysis it was not being used for anything, and was
defined backwards anyway.
21 years ago
|
|
|
|
|
|
|
/* If same EC already used in list, then redundant */
|
|
|
|
foreach(lc, pathkeys)
|
Teach planner about some cases where a restriction clause can be
propagated inside an outer join. In particular, given
LEFT JOIN ON (A = B) WHERE A = constant, we cannot conclude that
B = constant at the top level (B might be null instead), but we
can nonetheless put a restriction B = constant into the quals for
B's relation, since no inner-side rows not meeting that condition
can contribute to the final result. Similarly, given
FULL JOIN USING (J) WHERE J = constant, we can't directly conclude
that either input J variable = constant, but it's OK to push such
quals into each input rel. Per recent gripe from Kim Bisgaard.
Along the way, remove 'valid_everywhere' flag from RestrictInfo,
as on closer analysis it was not being used for anything, and was
defined backwards anyway.
21 years ago
|
|
|
{
|
|
|
|
PathKey *old_pathkey = (PathKey *) lfirst(lc);
|
Teach planner about some cases where a restriction clause can be
propagated inside an outer join. In particular, given
LEFT JOIN ON (A = B) WHERE A = constant, we cannot conclude that
B = constant at the top level (B might be null instead), but we
can nonetheless put a restriction B = constant into the quals for
B's relation, since no inner-side rows not meeting that condition
can contribute to the final result. Similarly, given
FULL JOIN USING (J) WHERE J = constant, we can't directly conclude
that either input J variable = constant, but it's OK to push such
quals into each input rel. Per recent gripe from Kim Bisgaard.
Along the way, remove 'valid_everywhere' flag from RestrictInfo,
as on closer analysis it was not being used for anything, and was
defined backwards anyway.
21 years ago
|
|
|
|
|
|
|
if (new_ec == old_pathkey->pk_eclass)
|
|
|
|
return true;
|
Teach planner about some cases where a restriction clause can be
propagated inside an outer join. In particular, given
LEFT JOIN ON (A = B) WHERE A = constant, we cannot conclude that
B = constant at the top level (B might be null instead), but we
can nonetheless put a restriction B = constant into the quals for
B's relation, since no inner-side rows not meeting that condition
can contribute to the final result. Similarly, given
FULL JOIN USING (J) WHERE J = constant, we can't directly conclude
that either input J variable = constant, but it's OK to push such
quals into each input rel. Per recent gripe from Kim Bisgaard.
Along the way, remove 'valid_everywhere' flag from RestrictInfo,
as on closer analysis it was not being used for anything, and was
defined backwards anyway.
21 years ago
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* make_pathkey_from_sortinfo
|
|
|
|
* Given an expression and sort-order information, create a PathKey.
|
Postpone creation of pathkeys lists to fix bug #8049.
This patch gets rid of the concept of, and infrastructure for,
non-canonical PathKeys; we now only ever create canonical pathkey lists.
The need for non-canonical pathkeys came from the desire to have
grouping_planner initialize query_pathkeys and related pathkey lists before
calling query_planner. However, since query_planner didn't actually *do*
anything with those lists before they'd been made canonical, we can get rid
of the whole mess by just not creating the lists at all until the point
where we formerly canonicalized them.
There are several ways in which we could implement that without making
query_planner itself deal with grouping/sorting features (which are
supposed to be the province of grouping_planner). I chose to add a
callback function to query_planner's API; other alternatives would have
required adding more fields to PlannerInfo, which while not bad in itself
would create an ABI break for planner-related plugins in the 9.2 release
series. This still breaks ABI for anything that calls query_planner
directly, but it seems somewhat unlikely that there are any such plugins.
I had originally conceived of this change as merely a step on the way to
fixing bug #8049 from Teun Hoogendoorn; but it turns out that this fixes
that bug all by itself, as per the added regression test. The reason is
that now get_eclass_for_sort_expr is adding the ORDER BY expression at the
end of EquivalenceClass creation not the start, and so anything that is in
a multi-member EquivalenceClass has already been created with correct
em_nullable_relids. I am suspicious that there are related scenarios in
which we still need to teach get_eclass_for_sort_expr to compute correct
nullable_relids, but am not eager to risk destabilizing either 9.2 or 9.3
to fix bugs that are only hypothetical. So for the moment, do this and
stop here.
Back-patch to 9.2 but not to earlier branches, since they don't exhibit
this bug for lack of join-clause-movement logic that depends on
em_nullable_relids being correct. (We might have to revisit that choice
if any related bugs turn up.) In 9.2, don't change the signature of
make_pathkeys_for_sortclauses nor remove canonicalize_pathkeys, so as
not to risk more plugin breakage than we have to.
13 years ago
|
|
|
* The result is always a "canonical" PathKey, but it might be redundant.
|
|
|
|
*
|
Compute correct em_nullable_relids in get_eclass_for_sort_expr().
Bug #8591 from Claudio Freire demonstrates that get_eclass_for_sort_expr
must be able to compute valid em_nullable_relids for any new equivalence
class members it creates. I'd worried about this in the commit message
for db9f0e1d9a4a0842c814a464cdc9758c3f20b96c, but claimed that it wasn't a
problem because multi-member ECs should already exist when it runs. That
is transparently wrong, though, because this function is also called by
initialize_mergeclause_eclasses, which runs during deconstruct_jointree.
The example given in the bug report (which the new regression test item
is based upon) fails because the COALESCE() expression is first seen by
initialize_mergeclause_eclasses rather than process_equivalence.
Fixing this requires passing the appropriate nullable_relids set to
get_eclass_for_sort_expr, and it requires new code to compute that set
for top-level expressions such as ORDER BY, GROUP BY, etc. We store
the top-level nullable_relids in a new field in PlannerInfo to avoid
computing it many times. In the back branches, I've added the new
field at the end of the struct to minimize ABI breakage for planner
plugins. There doesn't seem to be a good alternative to changing
get_eclass_for_sort_expr's API signature, though. There probably aren't
any third-party extensions calling that function directly; moreover,
if there are, they probably need to think about what to pass for
nullable_relids anyway.
Back-patch to 9.2, like the previous patch in this area.
12 years ago
|
|
|
* expr is the expression, and nullable_relids is the set of base relids
|
|
|
|
* that are potentially nullable below it.
|
|
|
|
*
|
|
|
|
* If the PathKey is being generated from a SortGroupClause, sortref should be
|
|
|
|
* the SortGroupClause's SortGroupRef; otherwise zero.
|
|
|
|
*
|
Revisit handling of UNION ALL subqueries with non-Var output columns.
In commit 57664ed25e5dea117158a2e663c29e60b3546e1c I tried to fix a bug
reported by Teodor Sigaev by making non-simple-Var output columns distinct
(by wrapping their expressions with dummy PlaceHolderVar nodes). This did
not work too well. Commit b28ffd0fcc583c1811e5295279e7d4366c3cae6c fixed
some ensuing problems with matching to child indexes, but per a recent
report from Claus Stadler, constraint exclusion of UNION ALL subqueries was
still broken, because constant-simplification didn't handle the injected
PlaceHolderVars well either. On reflection, the original patch was quite
misguided: there is no reason to expect that EquivalenceClass child members
will be distinct. So instead of trying to make them so, we should ensure
that we can cope with the situation when they're not.
Accordingly, this patch reverts the code changes in the above-mentioned
commits (though the regression test cases they added stay). Instead, I've
added assorted defenses to make sure that duplicate EC child members don't
cause any problems. Teodor's original problem ("MergeAppend child's
targetlist doesn't match MergeAppend") is addressed more directly by
revising prepare_sort_from_pathkeys to let the parent MergeAppend's sort
list guide creation of each child's sort list.
In passing, get rid of add_sort_column; as far as I can tell, testing for
duplicate sort keys at this stage is dead code. Certainly it doesn't
trigger often enough to be worth expending cycles on in ordinary queries.
And keeping the test would've greatly complicated the new logic in
prepare_sort_from_pathkeys, because comparing pathkey list entries against
a previous output array requires that we not skip any entries in the list.
Back-patch to 9.1, like the previous patches. The only known issue in
this area that wasn't caused by the ill-advised previous patches was the
MergeAppend planning failure, which of course is not relevant before 9.1.
It's possible that we need some of the new defenses against duplicate child
EC entries in older branches, but until there's some clear evidence of that
I'm going to refrain from back-patching further.
14 years ago
|
|
|
* If rel is not NULL, it identifies a specific relation we're considering
|
|
|
|
* a path for, and indicates that child EC members for that relation can be
|
|
|
|
* considered. Otherwise child members are ignored. (See the comments for
|
Revisit handling of UNION ALL subqueries with non-Var output columns.
In commit 57664ed25e5dea117158a2e663c29e60b3546e1c I tried to fix a bug
reported by Teodor Sigaev by making non-simple-Var output columns distinct
(by wrapping their expressions with dummy PlaceHolderVar nodes). This did
not work too well. Commit b28ffd0fcc583c1811e5295279e7d4366c3cae6c fixed
some ensuing problems with matching to child indexes, but per a recent
report from Claus Stadler, constraint exclusion of UNION ALL subqueries was
still broken, because constant-simplification didn't handle the injected
PlaceHolderVars well either. On reflection, the original patch was quite
misguided: there is no reason to expect that EquivalenceClass child members
will be distinct. So instead of trying to make them so, we should ensure
that we can cope with the situation when they're not.
Accordingly, this patch reverts the code changes in the above-mentioned
commits (though the regression test cases they added stay). Instead, I've
added assorted defenses to make sure that duplicate EC child members don't
cause any problems. Teodor's original problem ("MergeAppend child's
targetlist doesn't match MergeAppend") is addressed more directly by
revising prepare_sort_from_pathkeys to let the parent MergeAppend's sort
list guide creation of each child's sort list.
In passing, get rid of add_sort_column; as far as I can tell, testing for
duplicate sort keys at this stage is dead code. Certainly it doesn't
trigger often enough to be worth expending cycles on in ordinary queries.
And keeping the test would've greatly complicated the new logic in
prepare_sort_from_pathkeys, because comparing pathkey list entries against
a previous output array requires that we not skip any entries in the list.
Back-patch to 9.1, like the previous patches. The only known issue in
this area that wasn't caused by the ill-advised previous patches was the
MergeAppend planning failure, which of course is not relevant before 9.1.
It's possible that we need some of the new defenses against duplicate child
EC entries in older branches, but until there's some clear evidence of that
I'm going to refrain from back-patching further.
14 years ago
|
|
|
* get_eclass_for_sort_expr.)
|
|
|
|
*
|
|
|
|
* create_it is true if we should create any missing EquivalenceClass
|
|
|
|
* needed to represent the sort key. If it's false, we return NULL if the
|
|
|
|
* sort key isn't already present in any EquivalenceClass.
|
|
|
|
*/
|
|
|
|
static PathKey *
|
|
|
|
make_pathkey_from_sortinfo(PlannerInfo *root,
|
|
|
|
Expr *expr,
|
Compute correct em_nullable_relids in get_eclass_for_sort_expr().
Bug #8591 from Claudio Freire demonstrates that get_eclass_for_sort_expr
must be able to compute valid em_nullable_relids for any new equivalence
class members it creates. I'd worried about this in the commit message
for db9f0e1d9a4a0842c814a464cdc9758c3f20b96c, but claimed that it wasn't a
problem because multi-member ECs should already exist when it runs. That
is transparently wrong, though, because this function is also called by
initialize_mergeclause_eclasses, which runs during deconstruct_jointree.
The example given in the bug report (which the new regression test item
is based upon) fails because the COALESCE() expression is first seen by
initialize_mergeclause_eclasses rather than process_equivalence.
Fixing this requires passing the appropriate nullable_relids set to
get_eclass_for_sort_expr, and it requires new code to compute that set
for top-level expressions such as ORDER BY, GROUP BY, etc. We store
the top-level nullable_relids in a new field in PlannerInfo to avoid
computing it many times. In the back branches, I've added the new
field at the end of the struct to minimize ABI breakage for planner
plugins. There doesn't seem to be a good alternative to changing
get_eclass_for_sort_expr's API signature, though. There probably aren't
any third-party extensions calling that function directly; moreover,
if there are, they probably need to think about what to pass for
nullable_relids anyway.
Back-patch to 9.2, like the previous patch in this area.
12 years ago
|
|
|
Relids nullable_relids,
|
|
|
|
Oid opfamily,
|
|
|
|
Oid opcintype,
|
|
|
|
Oid collation,
|
|
|
|
bool reverse_sort,
|
|
|
|
bool nulls_first,
|
|
|
|
Index sortref,
|
Revisit handling of UNION ALL subqueries with non-Var output columns.
In commit 57664ed25e5dea117158a2e663c29e60b3546e1c I tried to fix a bug
reported by Teodor Sigaev by making non-simple-Var output columns distinct
(by wrapping their expressions with dummy PlaceHolderVar nodes). This did
not work too well. Commit b28ffd0fcc583c1811e5295279e7d4366c3cae6c fixed
some ensuing problems with matching to child indexes, but per a recent
report from Claus Stadler, constraint exclusion of UNION ALL subqueries was
still broken, because constant-simplification didn't handle the injected
PlaceHolderVars well either. On reflection, the original patch was quite
misguided: there is no reason to expect that EquivalenceClass child members
will be distinct. So instead of trying to make them so, we should ensure
that we can cope with the situation when they're not.
Accordingly, this patch reverts the code changes in the above-mentioned
commits (though the regression test cases they added stay). Instead, I've
added assorted defenses to make sure that duplicate EC child members don't
cause any problems. Teodor's original problem ("MergeAppend child's
targetlist doesn't match MergeAppend") is addressed more directly by
revising prepare_sort_from_pathkeys to let the parent MergeAppend's sort
list guide creation of each child's sort list.
In passing, get rid of add_sort_column; as far as I can tell, testing for
duplicate sort keys at this stage is dead code. Certainly it doesn't
trigger often enough to be worth expending cycles on in ordinary queries.
And keeping the test would've greatly complicated the new logic in
prepare_sort_from_pathkeys, because comparing pathkey list entries against
a previous output array requires that we not skip any entries in the list.
Back-patch to 9.1, like the previous patches. The only known issue in
this area that wasn't caused by the ill-advised previous patches was the
MergeAppend planning failure, which of course is not relevant before 9.1.
It's possible that we need some of the new defenses against duplicate child
EC entries in older branches, but until there's some clear evidence of that
I'm going to refrain from back-patching further.
14 years ago
|
|
|
Relids rel,
|
Postpone creation of pathkeys lists to fix bug #8049.
This patch gets rid of the concept of, and infrastructure for,
non-canonical PathKeys; we now only ever create canonical pathkey lists.
The need for non-canonical pathkeys came from the desire to have
grouping_planner initialize query_pathkeys and related pathkey lists before
calling query_planner. However, since query_planner didn't actually *do*
anything with those lists before they'd been made canonical, we can get rid
of the whole mess by just not creating the lists at all until the point
where we formerly canonicalized them.
There are several ways in which we could implement that without making
query_planner itself deal with grouping/sorting features (which are
supposed to be the province of grouping_planner). I chose to add a
callback function to query_planner's API; other alternatives would have
required adding more fields to PlannerInfo, which while not bad in itself
would create an ABI break for planner-related plugins in the 9.2 release
series. This still breaks ABI for anything that calls query_planner
directly, but it seems somewhat unlikely that there are any such plugins.
I had originally conceived of this change as merely a step on the way to
fixing bug #8049 from Teun Hoogendoorn; but it turns out that this fixes
that bug all by itself, as per the added regression test. The reason is
that now get_eclass_for_sort_expr is adding the ORDER BY expression at the
end of EquivalenceClass creation not the start, and so anything that is in
a multi-member EquivalenceClass has already been created with correct
em_nullable_relids. I am suspicious that there are related scenarios in
which we still need to teach get_eclass_for_sort_expr to compute correct
nullable_relids, but am not eager to risk destabilizing either 9.2 or 9.3
to fix bugs that are only hypothetical. So for the moment, do this and
stop here.
Back-patch to 9.2 but not to earlier branches, since they don't exhibit
this bug for lack of join-clause-movement logic that depends on
em_nullable_relids being correct. (We might have to revisit that choice
if any related bugs turn up.) In 9.2, don't change the signature of
make_pathkeys_for_sortclauses nor remove canonicalize_pathkeys, so as
not to risk more plugin breakage than we have to.
13 years ago
|
|
|
bool create_it)
|
|
|
|
{
|
|
|
|
int16 strategy;
|
|
|
|
Oid equality_op;
|
|
|
|
List *opfamilies;
|
|
|
|
EquivalenceClass *eclass;
|
|
|
|
|
|
|
|
strategy = reverse_sort ? BTGreaterStrategyNumber : BTLessStrategyNumber;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* EquivalenceClasses need to contain opfamily lists based on the family
|
|
|
|
* membership of mergejoinable equality operators, which could belong to
|
|
|
|
* more than one opfamily. So we have to look up the opfamily's equality
|
|
|
|
* operator and get its membership.
|
|
|
|
*/
|
|
|
|
equality_op = get_opfamily_member(opfamily,
|
|
|
|
opcintype,
|
|
|
|
opcintype,
|
|
|
|
BTEqualStrategyNumber);
|
Phase 2 of pgindent updates.
Change pg_bsd_indent to follow upstream rules for placement of comments
to the right of code, and remove pgindent hack that caused comments
following #endif to not obey the general rule.
Commit e3860ffa4dd0dad0dd9eea4be9cc1412373a8c89 wasn't actually using
the published version of pg_bsd_indent, but a hacked-up version that
tried to minimize the amount of movement of comments to the right of
code. The situation of interest is where such a comment has to be
moved to the right of its default placement at column 33 because there's
code there. BSD indent has always moved right in units of tab stops
in such cases --- but in the previous incarnation, indent was working
in 8-space tab stops, while now it knows we use 4-space tabs. So the
net result is that in about half the cases, such comments are placed
one tab stop left of before. This is better all around: it leaves
more room on the line for comment text, and it means that in such
cases the comment uniformly starts at the next 4-space tab stop after
the code, rather than sometimes one and sometimes two tabs after.
Also, ensure that comments following #endif are indented the same
as comments following other preprocessor commands such as #else.
That inconsistency turns out to have been self-inflicted damage
from a poorly-thought-through post-indent "fixup" in pgindent.
This patch is much less interesting than the first round of indent
changes, but also bulkier, so I thought it best to separate the effects.
Discussion: https://postgr.es/m/E1dAmxK-0006EE-1r@gemulon.postgresql.org
Discussion: https://postgr.es/m/30527.1495162840@sss.pgh.pa.us
8 years ago
|
|
|
if (!OidIsValid(equality_op)) /* shouldn't happen */
|
|
|
|
elog(ERROR, "missing operator %d(%u,%u) in opfamily %u",
|
|
|
|
BTEqualStrategyNumber, opcintype, opcintype, opfamily);
|
|
|
|
opfamilies = get_mergejoin_opfamilies(equality_op);
|
|
|
|
if (!opfamilies) /* certainly should find some */
|
|
|
|
elog(ERROR, "could not find opfamilies for equality operator %u",
|
|
|
|
equality_op);
|
|
|
|
|
|
|
|
/* Now find or (optionally) create a matching EquivalenceClass */
|
Compute correct em_nullable_relids in get_eclass_for_sort_expr().
Bug #8591 from Claudio Freire demonstrates that get_eclass_for_sort_expr
must be able to compute valid em_nullable_relids for any new equivalence
class members it creates. I'd worried about this in the commit message
for db9f0e1d9a4a0842c814a464cdc9758c3f20b96c, but claimed that it wasn't a
problem because multi-member ECs should already exist when it runs. That
is transparently wrong, though, because this function is also called by
initialize_mergeclause_eclasses, which runs during deconstruct_jointree.
The example given in the bug report (which the new regression test item
is based upon) fails because the COALESCE() expression is first seen by
initialize_mergeclause_eclasses rather than process_equivalence.
Fixing this requires passing the appropriate nullable_relids set to
get_eclass_for_sort_expr, and it requires new code to compute that set
for top-level expressions such as ORDER BY, GROUP BY, etc. We store
the top-level nullable_relids in a new field in PlannerInfo to avoid
computing it many times. In the back branches, I've added the new
field at the end of the struct to minimize ABI breakage for planner
plugins. There doesn't seem to be a good alternative to changing
get_eclass_for_sort_expr's API signature, though. There probably aren't
any third-party extensions calling that function directly; moreover,
if there are, they probably need to think about what to pass for
nullable_relids anyway.
Back-patch to 9.2, like the previous patch in this area.
12 years ago
|
|
|
eclass = get_eclass_for_sort_expr(root, expr, nullable_relids,
|
|
|
|
opfamilies, opcintype, collation,
|
Revisit handling of UNION ALL subqueries with non-Var output columns.
In commit 57664ed25e5dea117158a2e663c29e60b3546e1c I tried to fix a bug
reported by Teodor Sigaev by making non-simple-Var output columns distinct
(by wrapping their expressions with dummy PlaceHolderVar nodes). This did
not work too well. Commit b28ffd0fcc583c1811e5295279e7d4366c3cae6c fixed
some ensuing problems with matching to child indexes, but per a recent
report from Claus Stadler, constraint exclusion of UNION ALL subqueries was
still broken, because constant-simplification didn't handle the injected
PlaceHolderVars well either. On reflection, the original patch was quite
misguided: there is no reason to expect that EquivalenceClass child members
will be distinct. So instead of trying to make them so, we should ensure
that we can cope with the situation when they're not.
Accordingly, this patch reverts the code changes in the above-mentioned
commits (though the regression test cases they added stay). Instead, I've
added assorted defenses to make sure that duplicate EC child members don't
cause any problems. Teodor's original problem ("MergeAppend child's
targetlist doesn't match MergeAppend") is addressed more directly by
revising prepare_sort_from_pathkeys to let the parent MergeAppend's sort
list guide creation of each child's sort list.
In passing, get rid of add_sort_column; as far as I can tell, testing for
duplicate sort keys at this stage is dead code. Certainly it doesn't
trigger often enough to be worth expending cycles on in ordinary queries.
And keeping the test would've greatly complicated the new logic in
prepare_sort_from_pathkeys, because comparing pathkey list entries against
a previous output array requires that we not skip any entries in the list.
Back-patch to 9.1, like the previous patches. The only known issue in
this area that wasn't caused by the ill-advised previous patches was the
MergeAppend planning failure, which of course is not relevant before 9.1.
It's possible that we need some of the new defenses against duplicate child
EC entries in older branches, but until there's some clear evidence of that
I'm going to refrain from back-patching further.
14 years ago
|
|
|
sortref, rel, create_it);
|
|
|
|
|
|
|
|
/* Fail if no EC and !create_it */
|
|
|
|
if (!eclass)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* And finally we can find or create a PathKey node */
|
Postpone creation of pathkeys lists to fix bug #8049.
This patch gets rid of the concept of, and infrastructure for,
non-canonical PathKeys; we now only ever create canonical pathkey lists.
The need for non-canonical pathkeys came from the desire to have
grouping_planner initialize query_pathkeys and related pathkey lists before
calling query_planner. However, since query_planner didn't actually *do*
anything with those lists before they'd been made canonical, we can get rid
of the whole mess by just not creating the lists at all until the point
where we formerly canonicalized them.
There are several ways in which we could implement that without making
query_planner itself deal with grouping/sorting features (which are
supposed to be the province of grouping_planner). I chose to add a
callback function to query_planner's API; other alternatives would have
required adding more fields to PlannerInfo, which while not bad in itself
would create an ABI break for planner-related plugins in the 9.2 release
series. This still breaks ABI for anything that calls query_planner
directly, but it seems somewhat unlikely that there are any such plugins.
I had originally conceived of this change as merely a step on the way to
fixing bug #8049 from Teun Hoogendoorn; but it turns out that this fixes
that bug all by itself, as per the added regression test. The reason is
that now get_eclass_for_sort_expr is adding the ORDER BY expression at the
end of EquivalenceClass creation not the start, and so anything that is in
a multi-member EquivalenceClass has already been created with correct
em_nullable_relids. I am suspicious that there are related scenarios in
which we still need to teach get_eclass_for_sort_expr to compute correct
nullable_relids, but am not eager to risk destabilizing either 9.2 or 9.3
to fix bugs that are only hypothetical. So for the moment, do this and
stop here.
Back-patch to 9.2 but not to earlier branches, since they don't exhibit
this bug for lack of join-clause-movement logic that depends on
em_nullable_relids being correct. (We might have to revisit that choice
if any related bugs turn up.) In 9.2, don't change the signature of
make_pathkeys_for_sortclauses nor remove canonicalize_pathkeys, so as
not to risk more plugin breakage than we have to.
13 years ago
|
|
|
return make_canonical_pathkey(root, eclass, opfamily,
|
|
|
|
strategy, nulls_first);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* make_pathkey_from_sortop
|
|
|
|
* Like make_pathkey_from_sortinfo, but work from a sort operator.
|
|
|
|
*
|
|
|
|
* This should eventually go away, but we need to restructure SortGroupClause
|
|
|
|
* first.
|
|
|
|
*/
|
|
|
|
static PathKey *
|
|
|
|
make_pathkey_from_sortop(PlannerInfo *root,
|
|
|
|
Expr *expr,
|
Compute correct em_nullable_relids in get_eclass_for_sort_expr().
Bug #8591 from Claudio Freire demonstrates that get_eclass_for_sort_expr
must be able to compute valid em_nullable_relids for any new equivalence
class members it creates. I'd worried about this in the commit message
for db9f0e1d9a4a0842c814a464cdc9758c3f20b96c, but claimed that it wasn't a
problem because multi-member ECs should already exist when it runs. That
is transparently wrong, though, because this function is also called by
initialize_mergeclause_eclasses, which runs during deconstruct_jointree.
The example given in the bug report (which the new regression test item
is based upon) fails because the COALESCE() expression is first seen by
initialize_mergeclause_eclasses rather than process_equivalence.
Fixing this requires passing the appropriate nullable_relids set to
get_eclass_for_sort_expr, and it requires new code to compute that set
for top-level expressions such as ORDER BY, GROUP BY, etc. We store
the top-level nullable_relids in a new field in PlannerInfo to avoid
computing it many times. In the back branches, I've added the new
field at the end of the struct to minimize ABI breakage for planner
plugins. There doesn't seem to be a good alternative to changing
get_eclass_for_sort_expr's API signature, though. There probably aren't
any third-party extensions calling that function directly; moreover,
if there are, they probably need to think about what to pass for
nullable_relids anyway.
Back-patch to 9.2, like the previous patch in this area.
12 years ago
|
|
|
Relids nullable_relids,
|
|
|
|
Oid ordering_op,
|
|
|
|
bool nulls_first,
|
|
|
|
Index sortref,
|
Postpone creation of pathkeys lists to fix bug #8049.
This patch gets rid of the concept of, and infrastructure for,
non-canonical PathKeys; we now only ever create canonical pathkey lists.
The need for non-canonical pathkeys came from the desire to have
grouping_planner initialize query_pathkeys and related pathkey lists before
calling query_planner. However, since query_planner didn't actually *do*
anything with those lists before they'd been made canonical, we can get rid
of the whole mess by just not creating the lists at all until the point
where we formerly canonicalized them.
There are several ways in which we could implement that without making
query_planner itself deal with grouping/sorting features (which are
supposed to be the province of grouping_planner). I chose to add a
callback function to query_planner's API; other alternatives would have
required adding more fields to PlannerInfo, which while not bad in itself
would create an ABI break for planner-related plugins in the 9.2 release
series. This still breaks ABI for anything that calls query_planner
directly, but it seems somewhat unlikely that there are any such plugins.
I had originally conceived of this change as merely a step on the way to
fixing bug #8049 from Teun Hoogendoorn; but it turns out that this fixes
that bug all by itself, as per the added regression test. The reason is
that now get_eclass_for_sort_expr is adding the ORDER BY expression at the
end of EquivalenceClass creation not the start, and so anything that is in
a multi-member EquivalenceClass has already been created with correct
em_nullable_relids. I am suspicious that there are related scenarios in
which we still need to teach get_eclass_for_sort_expr to compute correct
nullable_relids, but am not eager to risk destabilizing either 9.2 or 9.3
to fix bugs that are only hypothetical. So for the moment, do this and
stop here.
Back-patch to 9.2 but not to earlier branches, since they don't exhibit
this bug for lack of join-clause-movement logic that depends on
em_nullable_relids being correct. (We might have to revisit that choice
if any related bugs turn up.) In 9.2, don't change the signature of
make_pathkeys_for_sortclauses nor remove canonicalize_pathkeys, so as
not to risk more plugin breakage than we have to.
13 years ago
|
|
|
bool create_it)
|
|
|
|
{
|
|
|
|
Oid opfamily,
|
|
|
|
opcintype,
|
|
|
|
collation;
|
|
|
|
int16 strategy;
|
|
|
|
|
|
|
|
/* Find the operator in pg_amop --- failure shouldn't happen */
|
|
|
|
if (!get_ordering_op_properties(ordering_op,
|
|
|
|
&opfamily, &opcintype, &strategy))
|
|
|
|
elog(ERROR, "operator %u is not a valid ordering operator",
|
|
|
|
ordering_op);
|
|
|
|
|
|
|
|
/* Because SortGroupClause doesn't carry collation, consult the expr */
|
|
|
|
collation = exprCollation((Node *) expr);
|
|
|
|
|
|
|
|
return make_pathkey_from_sortinfo(root,
|
|
|
|
expr,
|
Compute correct em_nullable_relids in get_eclass_for_sort_expr().
Bug #8591 from Claudio Freire demonstrates that get_eclass_for_sort_expr
must be able to compute valid em_nullable_relids for any new equivalence
class members it creates. I'd worried about this in the commit message
for db9f0e1d9a4a0842c814a464cdc9758c3f20b96c, but claimed that it wasn't a
problem because multi-member ECs should already exist when it runs. That
is transparently wrong, though, because this function is also called by
initialize_mergeclause_eclasses, which runs during deconstruct_jointree.
The example given in the bug report (which the new regression test item
is based upon) fails because the COALESCE() expression is first seen by
initialize_mergeclause_eclasses rather than process_equivalence.
Fixing this requires passing the appropriate nullable_relids set to
get_eclass_for_sort_expr, and it requires new code to compute that set
for top-level expressions such as ORDER BY, GROUP BY, etc. We store
the top-level nullable_relids in a new field in PlannerInfo to avoid
computing it many times. In the back branches, I've added the new
field at the end of the struct to minimize ABI breakage for planner
plugins. There doesn't seem to be a good alternative to changing
get_eclass_for_sort_expr's API signature, though. There probably aren't
any third-party extensions calling that function directly; moreover,
if there are, they probably need to think about what to pass for
nullable_relids anyway.
Back-patch to 9.2, like the previous patch in this area.
12 years ago
|
|
|
nullable_relids,
|
|
|
|
opfamily,
|
|
|
|
opcintype,
|
|
|
|
collation,
|
|
|
|
(strategy == BTGreaterStrategyNumber),
|
|
|
|
nulls_first,
|
|
|
|
sortref,
|
Revisit handling of UNION ALL subqueries with non-Var output columns.
In commit 57664ed25e5dea117158a2e663c29e60b3546e1c I tried to fix a bug
reported by Teodor Sigaev by making non-simple-Var output columns distinct
(by wrapping their expressions with dummy PlaceHolderVar nodes). This did
not work too well. Commit b28ffd0fcc583c1811e5295279e7d4366c3cae6c fixed
some ensuing problems with matching to child indexes, but per a recent
report from Claus Stadler, constraint exclusion of UNION ALL subqueries was
still broken, because constant-simplification didn't handle the injected
PlaceHolderVars well either. On reflection, the original patch was quite
misguided: there is no reason to expect that EquivalenceClass child members
will be distinct. So instead of trying to make them so, we should ensure
that we can cope with the situation when they're not.
Accordingly, this patch reverts the code changes in the above-mentioned
commits (though the regression test cases they added stay). Instead, I've
added assorted defenses to make sure that duplicate EC child members don't
cause any problems. Teodor's original problem ("MergeAppend child's
targetlist doesn't match MergeAppend") is addressed more directly by
revising prepare_sort_from_pathkeys to let the parent MergeAppend's sort
list guide creation of each child's sort list.
In passing, get rid of add_sort_column; as far as I can tell, testing for
duplicate sort keys at this stage is dead code. Certainly it doesn't
trigger often enough to be worth expending cycles on in ordinary queries.
And keeping the test would've greatly complicated the new logic in
prepare_sort_from_pathkeys, because comparing pathkey list entries against
a previous output array requires that we not skip any entries in the list.
Back-patch to 9.1, like the previous patches. The only known issue in
this area that wasn't caused by the ill-advised previous patches was the
MergeAppend planning failure, which of course is not relevant before 9.1.
It's possible that we need some of the new defenses against duplicate child
EC entries in older branches, but until there's some clear evidence of that
I'm going to refrain from back-patching further.
14 years ago
|
|
|
NULL,
|
Postpone creation of pathkeys lists to fix bug #8049.
This patch gets rid of the concept of, and infrastructure for,
non-canonical PathKeys; we now only ever create canonical pathkey lists.
The need for non-canonical pathkeys came from the desire to have
grouping_planner initialize query_pathkeys and related pathkey lists before
calling query_planner. However, since query_planner didn't actually *do*
anything with those lists before they'd been made canonical, we can get rid
of the whole mess by just not creating the lists at all until the point
where we formerly canonicalized them.
There are several ways in which we could implement that without making
query_planner itself deal with grouping/sorting features (which are
supposed to be the province of grouping_planner). I chose to add a
callback function to query_planner's API; other alternatives would have
required adding more fields to PlannerInfo, which while not bad in itself
would create an ABI break for planner-related plugins in the 9.2 release
series. This still breaks ABI for anything that calls query_planner
directly, but it seems somewhat unlikely that there are any such plugins.
I had originally conceived of this change as merely a step on the way to
fixing bug #8049 from Teun Hoogendoorn; but it turns out that this fixes
that bug all by itself, as per the added regression test. The reason is
that now get_eclass_for_sort_expr is adding the ORDER BY expression at the
end of EquivalenceClass creation not the start, and so anything that is in
a multi-member EquivalenceClass has already been created with correct
em_nullable_relids. I am suspicious that there are related scenarios in
which we still need to teach get_eclass_for_sort_expr to compute correct
nullable_relids, but am not eager to risk destabilizing either 9.2 or 9.3
to fix bugs that are only hypothetical. So for the moment, do this and
stop here.
Back-patch to 9.2 but not to earlier branches, since they don't exhibit
this bug for lack of join-clause-movement logic that depends on
em_nullable_relids being correct. (We might have to revisit that choice
if any related bugs turn up.) In 9.2, don't change the signature of
make_pathkeys_for_sortclauses nor remove canonicalize_pathkeys, so as
not to risk more plugin breakage than we have to.
13 years ago
|
|
|
create_it);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* PATHKEY COMPARISONS
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* compare_pathkeys
|
|
|
|
* Compare two pathkeys to see if they are equivalent, and if not whether
|
|
|
|
* one is "better" than the other.
|
|
|
|
*
|
Postpone creation of pathkeys lists to fix bug #8049.
This patch gets rid of the concept of, and infrastructure for,
non-canonical PathKeys; we now only ever create canonical pathkey lists.
The need for non-canonical pathkeys came from the desire to have
grouping_planner initialize query_pathkeys and related pathkey lists before
calling query_planner. However, since query_planner didn't actually *do*
anything with those lists before they'd been made canonical, we can get rid
of the whole mess by just not creating the lists at all until the point
where we formerly canonicalized them.
There are several ways in which we could implement that without making
query_planner itself deal with grouping/sorting features (which are
supposed to be the province of grouping_planner). I chose to add a
callback function to query_planner's API; other alternatives would have
required adding more fields to PlannerInfo, which while not bad in itself
would create an ABI break for planner-related plugins in the 9.2 release
series. This still breaks ABI for anything that calls query_planner
directly, but it seems somewhat unlikely that there are any such plugins.
I had originally conceived of this change as merely a step on the way to
fixing bug #8049 from Teun Hoogendoorn; but it turns out that this fixes
that bug all by itself, as per the added regression test. The reason is
that now get_eclass_for_sort_expr is adding the ORDER BY expression at the
end of EquivalenceClass creation not the start, and so anything that is in
a multi-member EquivalenceClass has already been created with correct
em_nullable_relids. I am suspicious that there are related scenarios in
which we still need to teach get_eclass_for_sort_expr to compute correct
nullable_relids, but am not eager to risk destabilizing either 9.2 or 9.3
to fix bugs that are only hypothetical. So for the moment, do this and
stop here.
Back-patch to 9.2 but not to earlier branches, since they don't exhibit
this bug for lack of join-clause-movement logic that depends on
em_nullable_relids being correct. (We might have to revisit that choice
if any related bugs turn up.) In 9.2, don't change the signature of
make_pathkeys_for_sortclauses nor remove canonicalize_pathkeys, so as
not to risk more plugin breakage than we have to.
13 years ago
|
|
|
* We assume the pathkeys are canonical, and so they can be checked for
|
|
|
|
* equality by simple pointer comparison.
|
|
|
|
*/
|
|
|
|
PathKeysComparison
|
|
|
|
compare_pathkeys(List *keys1, List *keys2)
|
|
|
|
{
|
|
|
|
ListCell *key1,
|
|
|
|
*key2;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Fall out quickly if we are passed two identical lists. This mostly
|
|
|
|
* catches the case where both are NIL, but that's common enough to
|
|
|
|
* warrant the test.
|
|
|
|
*/
|
|
|
|
if (keys1 == keys2)
|
|
|
|
return PATHKEYS_EQUAL;
|
|
|
|
|
|
|
|
forboth(key1, keys1, key2, keys2)
|
|
|
|
{
|
|
|
|
PathKey *pathkey1 = (PathKey *) lfirst(key1);
|
|
|
|
PathKey *pathkey2 = (PathKey *) lfirst(key2);
|
|
|
|
|
|
|
|
if (pathkey1 != pathkey2)
|
|
|
|
return PATHKEYS_DIFFERENT; /* no need to keep looking */
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If we reached the end of only one list, the other is longer and
|
|
|
|
* therefore not a subset.
|
|
|
|
*/
|
|
|
|
if (key1 != NULL)
|
|
|
|
return PATHKEYS_BETTER1; /* key1 is longer */
|
|
|
|
if (key2 != NULL)
|
|
|
|
return PATHKEYS_BETTER2; /* key2 is longer */
|
|
|
|
return PATHKEYS_EQUAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* pathkeys_contained_in
|
|
|
|
* Common special case of compare_pathkeys: we just want to know
|
|
|
|
* if keys2 are at least as well sorted as keys1.
|
|
|
|
*/
|
|
|
|
bool
|
|
|
|
pathkeys_contained_in(List *keys1, List *keys2)
|
|
|
|
{
|
|
|
|
switch (compare_pathkeys(keys1, keys2))
|
|
|
|
{
|
|
|
|
case PATHKEYS_EQUAL:
|
|
|
|
case PATHKEYS_BETTER2:
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
Implement Incremental Sort
Incremental Sort is an optimized variant of multikey sort for cases when
the input is already sorted by a prefix of the requested sort keys. For
example when the relation is already sorted by (key1, key2) and we need
to sort it by (key1, key2, key3) we can simply split the input rows into
groups having equal values in (key1, key2), and only sort/compare the
remaining column key3.
This has a number of benefits:
- Reduced memory consumption, because only a single group (determined by
values in the sorted prefix) needs to be kept in memory. This may also
eliminate the need to spill to disk.
- Lower startup cost, because Incremental Sort produce results after each
prefix group, which is beneficial for plans where startup cost matters
(like for example queries with LIMIT clause).
We consider both Sort and Incremental Sort, and decide based on costing.
The implemented algorithm operates in two different modes:
- Fetching a minimum number of tuples without check of equality on the
prefix keys, and sorting on all columns when safe.
- Fetching all tuples for a single prefix group and then sorting by
comparing only the remaining (non-prefix) keys.
We always start in the first mode, and employ a heuristic to switch into
the second mode if we believe it's beneficial - the goal is to minimize
the number of unnecessary comparions while keeping memory consumption
below work_mem.
This is a very old patch series. The idea was originally proposed by
Alexander Korotkov back in 2013, and then revived in 2017. In 2018 the
patch was taken over by James Coleman, who wrote and rewrote most of the
current code.
There were many reviewers/contributors since 2013 - I've done my best to
pick the most active ones, and listed them in this commit message.
Author: James Coleman, Alexander Korotkov
Reviewed-by: Tomas Vondra, Andreas Karlsson, Marti Raudsepp, Peter Geoghegan, Robert Haas, Thomas Munro, Antonin Houska, Andres Freund, Alexander Kuzmenkov
Discussion: https://postgr.es/m/CAPpHfdscOX5an71nHd8WSUH6GNOCf=V7wgDaTXdDd9=goN-gfA@mail.gmail.com
Discussion: https://postgr.es/m/CAPpHfds1waRZ=NOmueYq0sx1ZSCnt+5QJvizT8ndT2=etZEeAQ@mail.gmail.com
6 years ago
|
|
|
/*
|
|
|
|
* pathkeys_count_contained_in
|
|
|
|
* Same as pathkeys_contained_in, but also sets length of longest
|
|
|
|
* common prefix of keys1 and keys2.
|
|
|
|
*/
|
|
|
|
bool
|
|
|
|
pathkeys_count_contained_in(List *keys1, List *keys2, int *n_common)
|
|
|
|
{
|
|
|
|
int n = 0;
|
|
|
|
ListCell *key1,
|
|
|
|
*key2;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* See if we can avoiding looping through both lists. This optimization
|
|
|
|
* gains us several percent in planning time in a worst-case test.
|
|
|
|
*/
|
|
|
|
if (keys1 == keys2)
|
|
|
|
{
|
|
|
|
*n_common = list_length(keys1);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if (keys1 == NIL)
|
|
|
|
{
|
|
|
|
*n_common = 0;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if (keys2 == NIL)
|
|
|
|
{
|
|
|
|
*n_common = 0;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If both lists are non-empty, iterate through both to find out how many
|
|
|
|
* items are shared.
|
|
|
|
*/
|
|
|
|
forboth(key1, keys1, key2, keys2)
|
|
|
|
{
|
|
|
|
PathKey *pathkey1 = (PathKey *) lfirst(key1);
|
|
|
|
PathKey *pathkey2 = (PathKey *) lfirst(key2);
|
|
|
|
|
|
|
|
if (pathkey1 != pathkey2)
|
|
|
|
{
|
|
|
|
*n_common = n;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
n++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If we ended with a null value, then we've processed the whole list. */
|
|
|
|
*n_common = n;
|
|
|
|
return (key1 == NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* get_cheapest_path_for_pathkeys
|
|
|
|
* Find the cheapest path (according to the specified criterion) that
|
|
|
|
* satisfies the given pathkeys and parameterization.
|
|
|
|
* Return NULL if no such path.
|
|
|
|
*
|
|
|
|
* 'paths' is a list of possible paths that all generate the same relation
|
Postpone creation of pathkeys lists to fix bug #8049.
This patch gets rid of the concept of, and infrastructure for,
non-canonical PathKeys; we now only ever create canonical pathkey lists.
The need for non-canonical pathkeys came from the desire to have
grouping_planner initialize query_pathkeys and related pathkey lists before
calling query_planner. However, since query_planner didn't actually *do*
anything with those lists before they'd been made canonical, we can get rid
of the whole mess by just not creating the lists at all until the point
where we formerly canonicalized them.
There are several ways in which we could implement that without making
query_planner itself deal with grouping/sorting features (which are
supposed to be the province of grouping_planner). I chose to add a
callback function to query_planner's API; other alternatives would have
required adding more fields to PlannerInfo, which while not bad in itself
would create an ABI break for planner-related plugins in the 9.2 release
series. This still breaks ABI for anything that calls query_planner
directly, but it seems somewhat unlikely that there are any such plugins.
I had originally conceived of this change as merely a step on the way to
fixing bug #8049 from Teun Hoogendoorn; but it turns out that this fixes
that bug all by itself, as per the added regression test. The reason is
that now get_eclass_for_sort_expr is adding the ORDER BY expression at the
end of EquivalenceClass creation not the start, and so anything that is in
a multi-member EquivalenceClass has already been created with correct
em_nullable_relids. I am suspicious that there are related scenarios in
which we still need to teach get_eclass_for_sort_expr to compute correct
nullable_relids, but am not eager to risk destabilizing either 9.2 or 9.3
to fix bugs that are only hypothetical. So for the moment, do this and
stop here.
Back-patch to 9.2 but not to earlier branches, since they don't exhibit
this bug for lack of join-clause-movement logic that depends on
em_nullable_relids being correct. (We might have to revisit that choice
if any related bugs turn up.) In 9.2, don't change the signature of
make_pathkeys_for_sortclauses nor remove canonicalize_pathkeys, so as
not to risk more plugin breakage than we have to.
13 years ago
|
|
|
* 'pathkeys' represents a required ordering (in canonical form!)
|
|
|
|
* 'required_outer' denotes allowable outer relations for parameterized paths
|
|
|
|
* 'cost_criterion' is STARTUP_COST or TOTAL_COST
|
|
|
|
* 'require_parallel_safe' causes us to consider only parallel-safe paths
|
|
|
|
*/
|
|
|
|
Path *
|
|
|
|
get_cheapest_path_for_pathkeys(List *paths, List *pathkeys,
|
|
|
|
Relids required_outer,
|
|
|
|
CostSelector cost_criterion,
|
|
|
|
bool require_parallel_safe)
|
|
|
|
{
|
|
|
|
Path *matched_path = NULL;
|
|
|
|
ListCell *l;
|
|
|
|
|
|
|
|
foreach(l, paths)
|
|
|
|
{
|
|
|
|
Path *path = (Path *) lfirst(l);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Since cost comparison is a lot cheaper than pathkey comparison, do
|
|
|
|
* that first. (XXX is that still true?)
|
|
|
|
*/
|
|
|
|
if (matched_path != NULL &&
|
|
|
|
compare_path_costs(matched_path, path, cost_criterion) <= 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (require_parallel_safe && !path->parallel_safe)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (pathkeys_contained_in(pathkeys, path->pathkeys) &&
|
Revise parameterized-path mechanism to fix assorted issues.
This patch adjusts the treatment of parameterized paths so that all paths
with the same parameterization (same set of required outer rels) for the
same relation will have the same rowcount estimate. We cache the rowcount
estimates to ensure that property, and hopefully save a few cycles too.
Doing this makes it practical for add_path_precheck to operate without
a rowcount estimate: it need only assume that paths with different
parameterizations never dominate each other, which is close enough to
true anyway for coarse filtering, because normally a more-parameterized
path should yield fewer rows thanks to having more join clauses to apply.
In add_path, we do the full nine yards of comparing rowcount estimates
along with everything else, so that we can discard parameterized paths that
don't actually have an advantage. This fixes some issues I'd found with
add_path rejecting parameterized paths on the grounds that they were more
expensive than not-parameterized ones, even though they yielded many fewer
rows and hence would be cheaper once subsequent joining was considered.
To make the same-rowcounts assumption valid, we have to require that any
parameterized path enforce *all* join clauses that could be obtained from
the particular set of outer rels, even if not all of them are useful for
indexing. This is required at both base scans and joins. It's a good
thing anyway since the net impact is that join quals are checked at the
lowest practical level in the join tree. Hence, discard the original
rather ad-hoc mechanism for choosing parameterization joinquals, and build
a better one that has a more principled rule for when clauses can be moved.
The original rule was actually buggy anyway for lack of knowledge about
which relations are part of an outer join's outer side; getting this right
requires adding an outer_relids field to RestrictInfo.
14 years ago
|
|
|
bms_is_subset(PATH_REQ_OUTER(path), required_outer))
|
|
|
|
matched_path = path;
|
|
|
|
}
|
|
|
|
return matched_path;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* get_cheapest_fractional_path_for_pathkeys
|
|
|
|
* Find the cheapest path (for retrieving a specified fraction of all
|
|
|
|
* the tuples) that satisfies the given pathkeys and parameterization.
|
|
|
|
* Return NULL if no such path.
|
|
|
|
*
|
|
|
|
* See compare_fractional_path_costs() for the interpretation of the fraction
|
|
|
|
* parameter.
|
|
|
|
*
|
|
|
|
* 'paths' is a list of possible paths that all generate the same relation
|
Postpone creation of pathkeys lists to fix bug #8049.
This patch gets rid of the concept of, and infrastructure for,
non-canonical PathKeys; we now only ever create canonical pathkey lists.
The need for non-canonical pathkeys came from the desire to have
grouping_planner initialize query_pathkeys and related pathkey lists before
calling query_planner. However, since query_planner didn't actually *do*
anything with those lists before they'd been made canonical, we can get rid
of the whole mess by just not creating the lists at all until the point
where we formerly canonicalized them.
There are several ways in which we could implement that without making
query_planner itself deal with grouping/sorting features (which are
supposed to be the province of grouping_planner). I chose to add a
callback function to query_planner's API; other alternatives would have
required adding more fields to PlannerInfo, which while not bad in itself
would create an ABI break for planner-related plugins in the 9.2 release
series. This still breaks ABI for anything that calls query_planner
directly, but it seems somewhat unlikely that there are any such plugins.
I had originally conceived of this change as merely a step on the way to
fixing bug #8049 from Teun Hoogendoorn; but it turns out that this fixes
that bug all by itself, as per the added regression test. The reason is
that now get_eclass_for_sort_expr is adding the ORDER BY expression at the
end of EquivalenceClass creation not the start, and so anything that is in
a multi-member EquivalenceClass has already been created with correct
em_nullable_relids. I am suspicious that there are related scenarios in
which we still need to teach get_eclass_for_sort_expr to compute correct
nullable_relids, but am not eager to risk destabilizing either 9.2 or 9.3
to fix bugs that are only hypothetical. So for the moment, do this and
stop here.
Back-patch to 9.2 but not to earlier branches, since they don't exhibit
this bug for lack of join-clause-movement logic that depends on
em_nullable_relids being correct. (We might have to revisit that choice
if any related bugs turn up.) In 9.2, don't change the signature of
make_pathkeys_for_sortclauses nor remove canonicalize_pathkeys, so as
not to risk more plugin breakage than we have to.
13 years ago
|
|
|
* 'pathkeys' represents a required ordering (in canonical form!)
|
|
|
|
* 'required_outer' denotes allowable outer relations for parameterized paths
|
|
|
|
* 'fraction' is the fraction of the total tuples expected to be retrieved
|
|
|
|
*/
|
|
|
|
Path *
|
|
|
|
get_cheapest_fractional_path_for_pathkeys(List *paths,
|
|
|
|
List *pathkeys,
|
|
|
|
Relids required_outer,
|
|
|
|
double fraction)
|
|
|
|
{
|
|
|
|
Path *matched_path = NULL;
|
|
|
|
ListCell *l;
|
|
|
|
|
|
|
|
foreach(l, paths)
|
|
|
|
{
|
|
|
|
Path *path = (Path *) lfirst(l);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Since cost comparison is a lot cheaper than pathkey comparison, do
|
|
|
|
* that first. (XXX is that still true?)
|
|
|
|
*/
|
|
|
|
if (matched_path != NULL &&
|
|
|
|
compare_fractional_path_costs(matched_path, path, fraction) <= 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (pathkeys_contained_in(pathkeys, path->pathkeys) &&
|
Revise parameterized-path mechanism to fix assorted issues.
This patch adjusts the treatment of parameterized paths so that all paths
with the same parameterization (same set of required outer rels) for the
same relation will have the same rowcount estimate. We cache the rowcount
estimates to ensure that property, and hopefully save a few cycles too.
Doing this makes it practical for add_path_precheck to operate without
a rowcount estimate: it need only assume that paths with different
parameterizations never dominate each other, which is close enough to
true anyway for coarse filtering, because normally a more-parameterized
path should yield fewer rows thanks to having more join clauses to apply.
In add_path, we do the full nine yards of comparing rowcount estimates
along with everything else, so that we can discard parameterized paths that
don't actually have an advantage. This fixes some issues I'd found with
add_path rejecting parameterized paths on the grounds that they were more
expensive than not-parameterized ones, even though they yielded many fewer
rows and hence would be cheaper once subsequent joining was considered.
To make the same-rowcounts assumption valid, we have to require that any
parameterized path enforce *all* join clauses that could be obtained from
the particular set of outer rels, even if not all of them are useful for
indexing. This is required at both base scans and joins. It's a good
thing anyway since the net impact is that join quals are checked at the
lowest practical level in the join tree. Hence, discard the original
rather ad-hoc mechanism for choosing parameterization joinquals, and build
a better one that has a more principled rule for when clauses can be moved.
The original rule was actually buggy anyway for lack of knowledge about
which relations are part of an outer join's outer side; getting this right
requires adding an outer_relids field to RestrictInfo.
14 years ago
|
|
|
bms_is_subset(PATH_REQ_OUTER(path), required_outer))
|
|
|
|
matched_path = path;
|
|
|
|
}
|
|
|
|
return matched_path;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* get_cheapest_parallel_safe_total_inner
|
|
|
|
* Find the unparameterized parallel-safe path with the least total cost.
|
|
|
|
*/
|
|
|
|
Path *
|
|
|
|
get_cheapest_parallel_safe_total_inner(List *paths)
|
|
|
|
{
|
|
|
|
ListCell *l;
|
|
|
|
|
|
|
|
foreach(l, paths)
|
|
|
|
{
|
|
|
|
Path *innerpath = (Path *) lfirst(l);
|
|
|
|
|
|
|
|
if (innerpath->parallel_safe &&
|
|
|
|
bms_is_empty(PATH_REQ_OUTER(innerpath)))
|
|
|
|
return innerpath;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* NEW PATHKEY FORMATION
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* build_index_pathkeys
|
|
|
|
* Build a pathkeys list that describes the ordering induced by an index
|
|
|
|
* scan using the given index. (Note that an unordered index doesn't
|
|
|
|
* induce any ordering, so we return NIL.)
|
|
|
|
*
|
|
|
|
* If 'scandir' is BackwardScanDirection, build pathkeys representing a
|
|
|
|
* backwards scan of the index.
|
|
|
|
*
|
|
|
|
* We iterate only key columns of covering indexes, since non-key columns
|
|
|
|
* don't influence index ordering. The result is canonical, meaning that
|
|
|
|
* redundant pathkeys are removed; it may therefore have fewer entries than
|
|
|
|
* there are key columns in the index.
|
|
|
|
*
|
|
|
|
* Another reason for stopping early is that we may be able to tell that
|
|
|
|
* an index column's sort order is uninteresting for this query. However,
|
|
|
|
* that test is just based on the existence of an EquivalenceClass and not
|
|
|
|
* on position in pathkey lists, so it's not complete. Caller should call
|
|
|
|
* truncate_useless_pathkeys() to possibly remove more pathkeys.
|
|
|
|
*/
|
|
|
|
List *
|
|
|
|
build_index_pathkeys(PlannerInfo *root,
|
|
|
|
IndexOptInfo *index,
|
|
|
|
ScanDirection scandir)
|
|
|
|
{
|
|
|
|
List *retval = NIL;
|
|
|
|
ListCell *lc;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (index->sortopfamily == NULL)
|
|
|
|
return NIL; /* non-orderable index */
|
|
|
|
|
|
|
|
i = 0;
|
|
|
|
foreach(lc, index->indextlist)
|
|
|
|
{
|
|
|
|
TargetEntry *indextle = (TargetEntry *) lfirst(lc);
|
|
|
|
Expr *indexkey;
|
|
|
|
bool reverse_sort;
|
|
|
|
bool nulls_first;
|
|
|
|
PathKey *cpathkey;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* INCLUDE columns are stored in index unordered, so they don't
|
|
|
|
* support ordered index scan.
|
|
|
|
*/
|
|
|
|
if (i >= index->nkeycolumns)
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* We assume we don't need to make a copy of the tlist item */
|
|
|
|
indexkey = indextle->expr;
|
|
|
|
|
|
|
|
if (ScanDirectionIsBackward(scandir))
|
|
|
|
{
|
|
|
|
reverse_sort = !index->reverse_sort[i];
|
|
|
|
nulls_first = !index->nulls_first[i];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
reverse_sort = index->reverse_sort[i];
|
|
|
|
nulls_first = index->nulls_first[i];
|
|
|
|
}
|
|
|
|
|
Compute correct em_nullable_relids in get_eclass_for_sort_expr().
Bug #8591 from Claudio Freire demonstrates that get_eclass_for_sort_expr
must be able to compute valid em_nullable_relids for any new equivalence
class members it creates. I'd worried about this in the commit message
for db9f0e1d9a4a0842c814a464cdc9758c3f20b96c, but claimed that it wasn't a
problem because multi-member ECs should already exist when it runs. That
is transparently wrong, though, because this function is also called by
initialize_mergeclause_eclasses, which runs during deconstruct_jointree.
The example given in the bug report (which the new regression test item
is based upon) fails because the COALESCE() expression is first seen by
initialize_mergeclause_eclasses rather than process_equivalence.
Fixing this requires passing the appropriate nullable_relids set to
get_eclass_for_sort_expr, and it requires new code to compute that set
for top-level expressions such as ORDER BY, GROUP BY, etc. We store
the top-level nullable_relids in a new field in PlannerInfo to avoid
computing it many times. In the back branches, I've added the new
field at the end of the struct to minimize ABI breakage for planner
plugins. There doesn't seem to be a good alternative to changing
get_eclass_for_sort_expr's API signature, though. There probably aren't
any third-party extensions calling that function directly; moreover,
if there are, they probably need to think about what to pass for
nullable_relids anyway.
Back-patch to 9.2, like the previous patch in this area.
12 years ago
|
|
|
/*
|
|
|
|
* OK, try to make a canonical pathkey for this sort key. Note we're
|
|
|
|
* underneath any outer joins, so nullable_relids should be NULL.
|
|
|
|
*/
|
|
|
|
cpathkey = make_pathkey_from_sortinfo(root,
|
|
|
|
indexkey,
|
Compute correct em_nullable_relids in get_eclass_for_sort_expr().
Bug #8591 from Claudio Freire demonstrates that get_eclass_for_sort_expr
must be able to compute valid em_nullable_relids for any new equivalence
class members it creates. I'd worried about this in the commit message
for db9f0e1d9a4a0842c814a464cdc9758c3f20b96c, but claimed that it wasn't a
problem because multi-member ECs should already exist when it runs. That
is transparently wrong, though, because this function is also called by
initialize_mergeclause_eclasses, which runs during deconstruct_jointree.
The example given in the bug report (which the new regression test item
is based upon) fails because the COALESCE() expression is first seen by
initialize_mergeclause_eclasses rather than process_equivalence.
Fixing this requires passing the appropriate nullable_relids set to
get_eclass_for_sort_expr, and it requires new code to compute that set
for top-level expressions such as ORDER BY, GROUP BY, etc. We store
the top-level nullable_relids in a new field in PlannerInfo to avoid
computing it many times. In the back branches, I've added the new
field at the end of the struct to minimize ABI breakage for planner
plugins. There doesn't seem to be a good alternative to changing
get_eclass_for_sort_expr's API signature, though. There probably aren't
any third-party extensions calling that function directly; moreover,
if there are, they probably need to think about what to pass for
nullable_relids anyway.
Back-patch to 9.2, like the previous patch in this area.
12 years ago
|
|
|
NULL,
|
|
|
|
index->sortopfamily[i],
|
|
|
|
index->opcintype[i],
|
|
|
|
index->indexcollations[i],
|
|
|
|
reverse_sort,
|
|
|
|
nulls_first,
|
|
|
|
0,
|
Revisit handling of UNION ALL subqueries with non-Var output columns.
In commit 57664ed25e5dea117158a2e663c29e60b3546e1c I tried to fix a bug
reported by Teodor Sigaev by making non-simple-Var output columns distinct
(by wrapping their expressions with dummy PlaceHolderVar nodes). This did
not work too well. Commit b28ffd0fcc583c1811e5295279e7d4366c3cae6c fixed
some ensuing problems with matching to child indexes, but per a recent
report from Claus Stadler, constraint exclusion of UNION ALL subqueries was
still broken, because constant-simplification didn't handle the injected
PlaceHolderVars well either. On reflection, the original patch was quite
misguided: there is no reason to expect that EquivalenceClass child members
will be distinct. So instead of trying to make them so, we should ensure
that we can cope with the situation when they're not.
Accordingly, this patch reverts the code changes in the above-mentioned
commits (though the regression test cases they added stay). Instead, I've
added assorted defenses to make sure that duplicate EC child members don't
cause any problems. Teodor's original problem ("MergeAppend child's
targetlist doesn't match MergeAppend") is addressed more directly by
revising prepare_sort_from_pathkeys to let the parent MergeAppend's sort
list guide creation of each child's sort list.
In passing, get rid of add_sort_column; as far as I can tell, testing for
duplicate sort keys at this stage is dead code. Certainly it doesn't
trigger often enough to be worth expending cycles on in ordinary queries.
And keeping the test would've greatly complicated the new logic in
prepare_sort_from_pathkeys, because comparing pathkey list entries against
a previous output array requires that we not skip any entries in the list.
Back-patch to 9.1, like the previous patches. The only known issue in
this area that wasn't caused by the ill-advised previous patches was the
MergeAppend planning failure, which of course is not relevant before 9.1.
It's possible that we need some of the new defenses against duplicate child
EC entries in older branches, but until there's some clear evidence of that
I'm going to refrain from back-patching further.
14 years ago
|
|
|
index->rel->relids,
|
Postpone creation of pathkeys lists to fix bug #8049.
This patch gets rid of the concept of, and infrastructure for,
non-canonical PathKeys; we now only ever create canonical pathkey lists.
The need for non-canonical pathkeys came from the desire to have
grouping_planner initialize query_pathkeys and related pathkey lists before
calling query_planner. However, since query_planner didn't actually *do*
anything with those lists before they'd been made canonical, we can get rid
of the whole mess by just not creating the lists at all until the point
where we formerly canonicalized them.
There are several ways in which we could implement that without making
query_planner itself deal with grouping/sorting features (which are
supposed to be the province of grouping_planner). I chose to add a
callback function to query_planner's API; other alternatives would have
required adding more fields to PlannerInfo, which while not bad in itself
would create an ABI break for planner-related plugins in the 9.2 release
series. This still breaks ABI for anything that calls query_planner
directly, but it seems somewhat unlikely that there are any such plugins.
I had originally conceived of this change as merely a step on the way to
fixing bug #8049 from Teun Hoogendoorn; but it turns out that this fixes
that bug all by itself, as per the added regression test. The reason is
that now get_eclass_for_sort_expr is adding the ORDER BY expression at the
end of EquivalenceClass creation not the start, and so anything that is in
a multi-member EquivalenceClass has already been created with correct
em_nullable_relids. I am suspicious that there are related scenarios in
which we still need to teach get_eclass_for_sort_expr to compute correct
nullable_relids, but am not eager to risk destabilizing either 9.2 or 9.3
to fix bugs that are only hypothetical. So for the moment, do this and
stop here.
Back-patch to 9.2 but not to earlier branches, since they don't exhibit
this bug for lack of join-clause-movement logic that depends on
em_nullable_relids being correct. (We might have to revisit that choice
if any related bugs turn up.) In 9.2, don't change the signature of
make_pathkeys_for_sortclauses nor remove canonicalize_pathkeys, so as
not to risk more plugin breakage than we have to.
13 years ago
|
|
|
false);
|
|
|
|
|
|
|
|
if (cpathkey)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* We found the sort key in an EquivalenceClass, so it's relevant
|
|
|
|
* for this query. Add it to list, unless it's redundant.
|
|
|
|
*/
|
|
|
|
if (!pathkey_is_redundant(cpathkey, retval))
|
|
|
|
retval = lappend(retval, cpathkey);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Boolean index keys might be redundant even if they do not
|
|
|
|
* appear in an EquivalenceClass, because of our special treatment
|
|
|
|
* of boolean equality conditions --- see the comment for
|
|
|
|
* indexcol_is_bool_constant_for_query(). If that applies, we can
|
|
|
|
* continue to examine lower-order index columns. Otherwise, the
|
|
|
|
* sort key is not an interesting sort order for this query, so we
|
|
|
|
* should stop considering index columns; any lower-order sort
|
|
|
|
* keys won't be useful either.
|
|
|
|
*/
|
Fix pull_varnos' miscomputation of relids set for a PlaceHolderVar.
Previously, pull_varnos() took the relids of a PlaceHolderVar as being
equal to the relids in its contents, but that fails to account for the
possibility that we have to postpone evaluation of the PHV due to outer
joins. This could result in a malformed plan. The known cases end up
triggering the "failed to assign all NestLoopParams to plan nodes"
sanity check in createplan.c, but other symptoms may be possible.
The right value to use is the join level we actually intend to evaluate
the PHV at. We can get that from the ph_eval_at field of the associated
PlaceHolderInfo. However, there are some places that call pull_varnos()
before the PlaceHolderInfos have been created; in that case, fall back
to the conservative assumption that the PHV will be evaluated at its
syntactic level. (In principle this might result in missing some legal
optimization, but I'm not aware of any cases where it's an issue in
practice.) Things are also a bit ticklish for calls occurring during
deconstruct_jointree(), but AFAICS the ph_eval_at fields should have
reached their final values by the time we need them.
The main problem in making this work is that pull_varnos() has no
way to get at the PlaceHolderInfos. We can fix that easily, if a
bit tediously, in HEAD by passing it the planner "root" pointer.
In the back branches that'd cause an unacceptable API/ABI break for
extensions, so leave the existing entry points alone and add new ones
with the additional parameter. (If an old entry point is called and
encounters a PHV, it'll fall back to using the syntactic level,
again possibly missing some valid optimization.)
Back-patch to v12. The computation is surely also wrong before that,
but it appears that we cannot reach a bad plan thanks to join order
restrictions imposed on the subquery that the PlaceHolderVar came from.
The error only became reachable when commit 4be058fe9 allowed trivial
subqueries to be collapsed out completely, eliminating their join order
restrictions.
Per report from Stephan Springl.
Discussion: https://postgr.es/m/171041.1610849523@sss.pgh.pa.us
5 years ago
|
|
|
if (!indexcol_is_bool_constant_for_query(root, index, i))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
Use Append rather than MergeAppend for scanning ordered partitions.
If we need ordered output from a scan of a partitioned table, but
the ordering matches the partition ordering, then we don't need to
use a MergeAppend to combine the pre-ordered per-partition scan
results: a plain Append will produce the same results. This
both saves useless comparison work inside the MergeAppend proper,
and allows us to start returning tuples after istarting up just
the first child node not all of them.
However, all is not peaches and cream, because if some of the
child nodes have high startup costs then there will be big
discontinuities in the tuples-returned-versus-elapsed-time curve.
The planner's cost model cannot handle that (yet, anyway).
If we model the Append's startup cost as being just the first
child's startup cost, we may drastically underestimate the cost
of fetching slightly more tuples than are available from the first
child. Since we've had bad experiences with over-optimistic choices
of "fast start" plans for ORDER BY LIMIT queries, that seems scary.
As a klugy workaround, set the startup cost estimate for an ordered
Append to be the sum of its children's startup costs (as MergeAppend
would). This doesn't really describe reality, but it's less likely
to cause a bad plan choice than an underestimated startup cost would.
In practice, the cases where we really care about this optimization
will have child plans that are IndexScans with zero startup cost,
so that the overly conservative estimate is still just zero.
David Rowley, reviewed by Julien Rouhaud and Antonin Houska
Discussion: https://postgr.es/m/CAKJS1f-hAqhPLRk_RaSFTgYxd=Tz5hA7kQ2h4-DhJufQk8TGuw@mail.gmail.com
7 years ago
|
|
|
/*
|
|
|
|
* partkey_is_bool_constant_for_query
|
|
|
|
*
|
|
|
|
* If a partition key column is constrained to have a constant value by the
|
|
|
|
* query's WHERE conditions, then it's irrelevant for sort-order
|
|
|
|
* considerations. Usually that means we have a restriction clause
|
|
|
|
* WHERE partkeycol = constant, which gets turned into an EquivalenceClass
|
|
|
|
* containing a constant, which is recognized as redundant by
|
|
|
|
* build_partition_pathkeys(). But if the partition key column is a
|
|
|
|
* boolean variable (or expression), then we are not going to see such a
|
|
|
|
* WHERE clause, because expression preprocessing will have simplified it
|
|
|
|
* to "WHERE partkeycol" or "WHERE NOT partkeycol". So we are not going
|
|
|
|
* to have a matching EquivalenceClass (unless the query also contains
|
|
|
|
* "ORDER BY partkeycol"). To allow such cases to work the same as they would
|
|
|
|
* for non-boolean values, this function is provided to detect whether the
|
|
|
|
* specified partition key column matches a boolean restriction clause.
|
|
|
|
*/
|
|
|
|
static bool
|
|
|
|
partkey_is_bool_constant_for_query(RelOptInfo *partrel, int partkeycol)
|
|
|
|
{
|
|
|
|
PartitionScheme partscheme = partrel->part_scheme;
|
|
|
|
ListCell *lc;
|
|
|
|
|
|
|
|
/* If the partkey isn't boolean, we can't possibly get a match */
|
|
|
|
if (!IsBooleanOpfamily(partscheme->partopfamily[partkeycol]))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* Check each restriction clause for the partitioned rel */
|
|
|
|
foreach(lc, partrel->baserestrictinfo)
|
|
|
|
{
|
|
|
|
RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
|
|
|
|
|
|
|
|
/* Ignore pseudoconstant quals, they won't match */
|
|
|
|
if (rinfo->pseudoconstant)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* See if we can match the clause's expression to the partkey column */
|
|
|
|
if (matches_boolean_partition_clause(rinfo, partrel, partkeycol))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* matches_boolean_partition_clause
|
|
|
|
* Determine if the boolean clause described by rinfo matches
|
|
|
|
* partrel's partkeycol-th partition key column.
|
|
|
|
*
|
|
|
|
* "Matches" can be either an exact match (equivalent to partkey = true),
|
|
|
|
* or a NOT above an exact match (equivalent to partkey = false).
|
|
|
|
*/
|
|
|
|
static bool
|
|
|
|
matches_boolean_partition_clause(RestrictInfo *rinfo,
|
|
|
|
RelOptInfo *partrel, int partkeycol)
|
|
|
|
{
|
|
|
|
Node *clause = (Node *) rinfo->clause;
|
|
|
|
Node *partexpr = (Node *) linitial(partrel->partexprs[partkeycol]);
|
|
|
|
|
|
|
|
/* Direct match? */
|
|
|
|
if (equal(partexpr, clause))
|
|
|
|
return true;
|
|
|
|
/* NOT clause? */
|
|
|
|
else if (is_notclause(clause))
|
|
|
|
{
|
|
|
|
Node *arg = (Node *) get_notclausearg((Expr *) clause);
|
|
|
|
|
|
|
|
if (equal(partexpr, arg))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* build_partition_pathkeys
|
|
|
|
* Build a pathkeys list that describes the ordering induced by the
|
|
|
|
* partitions of partrel, under either forward or backward scan
|
|
|
|
* as per scandir.
|
|
|
|
*
|
|
|
|
* Caller must have checked that the partitions are properly ordered,
|
|
|
|
* as detected by partitions_are_ordered().
|
|
|
|
*
|
|
|
|
* Sets *partialkeys to true if pathkeys were only built for a prefix of the
|
|
|
|
* partition key, or false if the pathkeys include all columns of the
|
|
|
|
* partition key.
|
|
|
|
*/
|
|
|
|
List *
|
|
|
|
build_partition_pathkeys(PlannerInfo *root, RelOptInfo *partrel,
|
|
|
|
ScanDirection scandir, bool *partialkeys)
|
|
|
|
{
|
|
|
|
List *retval = NIL;
|
|
|
|
PartitionScheme partscheme = partrel->part_scheme;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
Assert(partscheme != NULL);
|
|
|
|
Assert(partitions_are_ordered(partrel->boundinfo, partrel->live_parts));
|
Use Append rather than MergeAppend for scanning ordered partitions.
If we need ordered output from a scan of a partitioned table, but
the ordering matches the partition ordering, then we don't need to
use a MergeAppend to combine the pre-ordered per-partition scan
results: a plain Append will produce the same results. This
both saves useless comparison work inside the MergeAppend proper,
and allows us to start returning tuples after istarting up just
the first child node not all of them.
However, all is not peaches and cream, because if some of the
child nodes have high startup costs then there will be big
discontinuities in the tuples-returned-versus-elapsed-time curve.
The planner's cost model cannot handle that (yet, anyway).
If we model the Append's startup cost as being just the first
child's startup cost, we may drastically underestimate the cost
of fetching slightly more tuples than are available from the first
child. Since we've had bad experiences with over-optimistic choices
of "fast start" plans for ORDER BY LIMIT queries, that seems scary.
As a klugy workaround, set the startup cost estimate for an ordered
Append to be the sum of its children's startup costs (as MergeAppend
would). This doesn't really describe reality, but it's less likely
to cause a bad plan choice than an underestimated startup cost would.
In practice, the cases where we really care about this optimization
will have child plans that are IndexScans with zero startup cost,
so that the overly conservative estimate is still just zero.
David Rowley, reviewed by Julien Rouhaud and Antonin Houska
Discussion: https://postgr.es/m/CAKJS1f-hAqhPLRk_RaSFTgYxd=Tz5hA7kQ2h4-DhJufQk8TGuw@mail.gmail.com
7 years ago
|
|
|
/* For now, we can only cope with baserels */
|
|
|
|
Assert(IS_SIMPLE_REL(partrel));
|
|
|
|
|
|
|
|
for (i = 0; i < partscheme->partnatts; i++)
|
|
|
|
{
|
|
|
|
PathKey *cpathkey;
|
|
|
|
Expr *keyCol = (Expr *) linitial(partrel->partexprs[i]);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Try to make a canonical pathkey for this partkey.
|
|
|
|
*
|
|
|
|
* We're considering a baserel scan, so nullable_relids should be
|
|
|
|
* NULL. Also, we assume the PartitionDesc lists any NULL partition
|
|
|
|
* last, so we treat the scan like a NULLS LAST index: we have
|
|
|
|
* nulls_first for backwards scan only.
|
|
|
|
*/
|
|
|
|
cpathkey = make_pathkey_from_sortinfo(root,
|
|
|
|
keyCol,
|
|
|
|
NULL,
|
|
|
|
partscheme->partopfamily[i],
|
|
|
|
partscheme->partopcintype[i],
|
|
|
|
partscheme->partcollation[i],
|
|
|
|
ScanDirectionIsBackward(scandir),
|
|
|
|
ScanDirectionIsBackward(scandir),
|
|
|
|
0,
|
|
|
|
partrel->relids,
|
|
|
|
false);
|
|
|
|
|
|
|
|
|
|
|
|
if (cpathkey)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* We found the sort key in an EquivalenceClass, so it's relevant
|
|
|
|
* for this query. Add it to list, unless it's redundant.
|
|
|
|
*/
|
|
|
|
if (!pathkey_is_redundant(cpathkey, retval))
|
|
|
|
retval = lappend(retval, cpathkey);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Boolean partition keys might be redundant even if they do not
|
|
|
|
* appear in an EquivalenceClass, because of our special treatment
|
|
|
|
* of boolean equality conditions --- see the comment for
|
|
|
|
* partkey_is_bool_constant_for_query(). If that applies, we can
|
|
|
|
* continue to examine lower-order partition keys. Otherwise, the
|
|
|
|
* sort key is not an interesting sort order for this query, so we
|
|
|
|
* should stop considering partition columns; any lower-order sort
|
|
|
|
* keys won't be useful either.
|
|
|
|
*/
|
|
|
|
if (!partkey_is_bool_constant_for_query(partrel, i))
|
|
|
|
{
|
|
|
|
*partialkeys = true;
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*partialkeys = false;
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
Support multi-argument UNNEST(), and TABLE() syntax for multiple functions.
This patch adds the ability to write TABLE( function1(), function2(), ...)
as a single FROM-clause entry. The result is the concatenation of the
first row from each function, followed by the second row from each
function, etc; with NULLs inserted if any function produces fewer rows than
others. This is believed to be a much more useful behavior than what
Postgres currently does with multiple SRFs in a SELECT list.
This syntax also provides a reasonable way to combine use of column
definition lists with WITH ORDINALITY: put the column definition list
inside TABLE(), where it's clear that it doesn't control the ordinality
column as well.
Also implement SQL-compliant multiple-argument UNNEST(), by turning
UNNEST(a,b,c) into TABLE(unnest(a), unnest(b), unnest(c)).
The SQL standard specifies TABLE() with only a single function, not
multiple functions, and it seems to require an implicit UNNEST() which is
not what this patch does. There may be something wrong with that reading
of the spec, though, because if it's right then the spec's TABLE() is just
a pointless alternative spelling of UNNEST(). After further review of
that, we might choose to adopt a different syntax for what this patch does,
but in any case this functionality seems clearly worthwhile.
Andrew Gierth, reviewed by Zoltán Böszörményi and Heikki Linnakangas, and
significantly revised by me
12 years ago
|
|
|
/*
|
|
|
|
* build_expression_pathkey
|
|
|
|
* Build a pathkeys list that describes an ordering by a single expression
|
|
|
|
* using the given sort operator.
|
|
|
|
*
|
|
|
|
* expr, nullable_relids, and rel are as for make_pathkey_from_sortinfo.
|
|
|
|
* We induce the other arguments assuming default sort order for the operator.
|
|
|
|
*
|
|
|
|
* Similarly to make_pathkey_from_sortinfo, the result is NIL if create_it
|
|
|
|
* is false and the expression isn't already in some EquivalenceClass.
|
|
|
|
*/
|
|
|
|
List *
|
|
|
|
build_expression_pathkey(PlannerInfo *root,
|
|
|
|
Expr *expr,
|
|
|
|
Relids nullable_relids,
|
|
|
|
Oid opno,
|
|
|
|
Relids rel,
|
|
|
|
bool create_it)
|
|
|
|
{
|
|
|
|
List *pathkeys;
|
|
|
|
Oid opfamily,
|
|
|
|
opcintype;
|
|
|
|
int16 strategy;
|
|
|
|
PathKey *cpathkey;
|
|
|
|
|
|
|
|
/* Find the operator in pg_amop --- failure shouldn't happen */
|
|
|
|
if (!get_ordering_op_properties(opno,
|
|
|
|
&opfamily, &opcintype, &strategy))
|
|
|
|
elog(ERROR, "operator %u is not a valid ordering operator",
|
|
|
|
opno);
|
|
|
|
|
|
|
|
cpathkey = make_pathkey_from_sortinfo(root,
|
|
|
|
expr,
|
|
|
|
nullable_relids,
|
|
|
|
opfamily,
|
|
|
|
opcintype,
|
|
|
|
exprCollation((Node *) expr),
|
Phase 3 of pgindent updates.
Don't move parenthesized lines to the left, even if that means they
flow past the right margin.
By default, BSD indent lines up statement continuation lines that are
within parentheses so that they start just to the right of the preceding
left parenthesis. However, traditionally, if that resulted in the
continuation line extending to the right of the desired right margin,
then indent would push it left just far enough to not overrun the margin,
if it could do so without making the continuation line start to the left of
the current statement indent. That makes for a weird mix of indentations
unless one has been completely rigid about never violating the 80-column
limit.
This behavior has been pretty universally panned by Postgres developers.
Hence, disable it with indent's new -lpl switch, so that parenthesized
lines are always lined up with the preceding left paren.
This patch is much less interesting than the first round of indent
changes, but also bulkier, so I thought it best to separate the effects.
Discussion: https://postgr.es/m/E1dAmxK-0006EE-1r@gemulon.postgresql.org
Discussion: https://postgr.es/m/30527.1495162840@sss.pgh.pa.us
8 years ago
|
|
|
(strategy == BTGreaterStrategyNumber),
|
|
|
|
(strategy == BTGreaterStrategyNumber),
|
Support multi-argument UNNEST(), and TABLE() syntax for multiple functions.
This patch adds the ability to write TABLE( function1(), function2(), ...)
as a single FROM-clause entry. The result is the concatenation of the
first row from each function, followed by the second row from each
function, etc; with NULLs inserted if any function produces fewer rows than
others. This is believed to be a much more useful behavior than what
Postgres currently does with multiple SRFs in a SELECT list.
This syntax also provides a reasonable way to combine use of column
definition lists with WITH ORDINALITY: put the column definition list
inside TABLE(), where it's clear that it doesn't control the ordinality
column as well.
Also implement SQL-compliant multiple-argument UNNEST(), by turning
UNNEST(a,b,c) into TABLE(unnest(a), unnest(b), unnest(c)).
The SQL standard specifies TABLE() with only a single function, not
multiple functions, and it seems to require an implicit UNNEST() which is
not what this patch does. There may be something wrong with that reading
of the spec, though, because if it's right then the spec's TABLE() is just
a pointless alternative spelling of UNNEST(). After further review of
that, we might choose to adopt a different syntax for what this patch does,
but in any case this functionality seems clearly worthwhile.
Andrew Gierth, reviewed by Zoltán Böszörményi and Heikki Linnakangas, and
significantly revised by me
12 years ago
|
|
|
0,
|
|
|
|
rel,
|
|
|
|
create_it);
|
|
|
|
|
|
|
|
if (cpathkey)
|
|
|
|
pathkeys = list_make1(cpathkey);
|
|
|
|
else
|
|
|
|
pathkeys = NIL;
|
|
|
|
|
|
|
|
return pathkeys;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* convert_subquery_pathkeys
|
|
|
|
* Build a pathkeys list that describes the ordering of a subquery's
|
|
|
|
* result, in the terms of the outer query. This is essentially a
|
|
|
|
* task of conversion.
|
|
|
|
*
|
|
|
|
* 'rel': outer query's RelOptInfo for the subquery relation.
|
|
|
|
* 'subquery_pathkeys': the subquery's output pathkeys, in its terms.
|
Make the upper part of the planner work by generating and comparing Paths.
I've been saying we needed to do this for more than five years, and here it
finally is. This patch removes the ever-growing tangle of spaghetti logic
that grouping_planner() used to use to try to identify the best plan for
post-scan/join query steps. Now, there is (nearly) independent
consideration of each execution step, and entirely separate construction of
Paths to represent each of the possible ways to do that step. We choose
the best Path or set of Paths using the same add_path() logic that's been
used inside query_planner() for years.
In addition, this patch removes the old restriction that subquery_planner()
could return only a single Plan. It now returns a RelOptInfo containing a
set of Paths, just as query_planner() does, and the parent query level can
use each of those Paths as the basis of a SubqueryScanPath at its level.
This allows finding some optimizations that we missed before, wherein a
subquery was capable of returning presorted data and thereby avoiding a
sort in the parent level, making the overall cost cheaper even though
delivering sorted output was not the cheapest plan for the subquery in
isolation. (A couple of regression test outputs change in consequence of
that. However, there is very little change in visible planner behavior
overall, because the point of this patch is not to get immediate planning
benefits but to create the infrastructure for future improvements.)
There is a great deal left to do here. This patch unblocks a lot of
planner work that was basically impractical in the old code structure,
such as allowing FDWs to implement remote aggregation, or rewriting
plan_set_operations() to allow consideration of multiple implementation
orders for set operations. (The latter will likely require a full
rewrite of plan_set_operations(); what I've done here is only to fix it
to return Paths not Plans.) I have also left unfinished some localized
refactoring in createplan.c and planner.c, because it was not necessary
to get this patch to a working state.
Thanks to Robert Haas, David Rowley, and Amit Kapila for review.
10 years ago
|
|
|
* 'subquery_tlist': the subquery's output targetlist, in its terms.
|
|
|
|
*
|
Repair issues with faulty generation of merge-append plans.
create_merge_append_plan failed to honor the CP_EXACT_TLIST flag:
it would generate the expected targetlist but then it felt free to
add resjunk sort targets to it. This demonstrably leads to assertion
failures in v11 and HEAD, and it's probably just accidental that we
don't see the same in older branches. I've not looked into whether
there would be any real-world consequences in non-assert builds.
In HEAD, create_append_plan has sprouted the same problem, so fix
that too (although we do not have any test cases that seem able to
reach that bug). This is an oversight in commit 3fc6e2d7f which
invented the CP_EXACT_TLIST flag, so back-patch to 9.6 where that
came in.
convert_subquery_pathkeys would create pathkeys for subquery output
values if they match any EquivalenceClass known in the outer query
and are available in the subquery's syntactic targetlist. However,
the second part of that condition is wrong, because such values might
not appear in the subquery relation's reltarget list, which would
mean that they couldn't be accessed above the level of the subquery
scan. We must check that they appear in the reltarget list, instead.
This can lead to dropping knowledge about the subquery's sort
ordering, but I believe it's okay, because any sort key that the
outer query actually has any interest in would appear in the
reltarget list.
This second issue is of very long standing, but right now there's no
evidence that it causes observable problems before 9.6, so I refrained
from back-patching further than that. We can revisit that choice if
somebody finds a way to make it cause problems in older branches.
(Developing useful test cases for these issues is really problematic;
fixing convert_subquery_pathkeys removes the only known way to exhibit
the create_merge_append_plan bug, and neither of the test cases added
by this patch causes a problem in all branches, even when considering
the issues separately.)
The second issue explains bug #15795 from Suresh Kumar R ("could not
find pathkey item to sort" with nested DISTINCT queries). I stumbled
across the first issue while investigating that.
Discussion: https://postgr.es/m/15795-fadb56c8e44ee73c@postgresql.org
6 years ago
|
|
|
* We intentionally don't do truncate_useless_pathkeys() here, because there
|
|
|
|
* are situations where seeing the raw ordering of the subquery is helpful.
|
|
|
|
* For example, if it returns ORDER BY x DESC, that may prompt us to
|
|
|
|
* construct a mergejoin using DESC order rather than ASC order; but the
|
|
|
|
* right_merge_direction heuristic would have us throw the knowledge away.
|
|
|
|
*/
|
|
|
|
List *
|
|
|
|
convert_subquery_pathkeys(PlannerInfo *root, RelOptInfo *rel,
|
Make the upper part of the planner work by generating and comparing Paths.
I've been saying we needed to do this for more than five years, and here it
finally is. This patch removes the ever-growing tangle of spaghetti logic
that grouping_planner() used to use to try to identify the best plan for
post-scan/join query steps. Now, there is (nearly) independent
consideration of each execution step, and entirely separate construction of
Paths to represent each of the possible ways to do that step. We choose
the best Path or set of Paths using the same add_path() logic that's been
used inside query_planner() for years.
In addition, this patch removes the old restriction that subquery_planner()
could return only a single Plan. It now returns a RelOptInfo containing a
set of Paths, just as query_planner() does, and the parent query level can
use each of those Paths as the basis of a SubqueryScanPath at its level.
This allows finding some optimizations that we missed before, wherein a
subquery was capable of returning presorted data and thereby avoiding a
sort in the parent level, making the overall cost cheaper even though
delivering sorted output was not the cheapest plan for the subquery in
isolation. (A couple of regression test outputs change in consequence of
that. However, there is very little change in visible planner behavior
overall, because the point of this patch is not to get immediate planning
benefits but to create the infrastructure for future improvements.)
There is a great deal left to do here. This patch unblocks a lot of
planner work that was basically impractical in the old code structure,
such as allowing FDWs to implement remote aggregation, or rewriting
plan_set_operations() to allow consideration of multiple implementation
orders for set operations. (The latter will likely require a full
rewrite of plan_set_operations(); what I've done here is only to fix it
to return Paths not Plans.) I have also left unfinished some localized
refactoring in createplan.c and planner.c, because it was not necessary
to get this patch to a working state.
Thanks to Robert Haas, David Rowley, and Amit Kapila for review.
10 years ago
|
|
|
List *subquery_pathkeys,
|
|
|
|
List *subquery_tlist)
|
|
|
|
{
|
|
|
|
List *retval = NIL;
|
|
|
|
int retvallen = 0;
|
|
|
|
int outer_query_keys = list_length(root->query_pathkeys);
|
|
|
|
ListCell *i;
|
|
|
|
|
|
|
|
foreach(i, subquery_pathkeys)
|
|
|
|
{
|
|
|
|
PathKey *sub_pathkey = (PathKey *) lfirst(i);
|
|
|
|
EquivalenceClass *sub_eclass = sub_pathkey->pk_eclass;
|
|
|
|
PathKey *best_pathkey = NULL;
|
|
|
|
|
|
|
|
if (sub_eclass->ec_has_volatile)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* If the sub_pathkey's EquivalenceClass is volatile, then it must
|
|
|
|
* have come from an ORDER BY clause, and we have to match it to
|
|
|
|
* that same targetlist entry.
|
|
|
|
*/
|
|
|
|
TargetEntry *tle;
|
Repair issues with faulty generation of merge-append plans.
create_merge_append_plan failed to honor the CP_EXACT_TLIST flag:
it would generate the expected targetlist but then it felt free to
add resjunk sort targets to it. This demonstrably leads to assertion
failures in v11 and HEAD, and it's probably just accidental that we
don't see the same in older branches. I've not looked into whether
there would be any real-world consequences in non-assert builds.
In HEAD, create_append_plan has sprouted the same problem, so fix
that too (although we do not have any test cases that seem able to
reach that bug). This is an oversight in commit 3fc6e2d7f which
invented the CP_EXACT_TLIST flag, so back-patch to 9.6 where that
came in.
convert_subquery_pathkeys would create pathkeys for subquery output
values if they match any EquivalenceClass known in the outer query
and are available in the subquery's syntactic targetlist. However,
the second part of that condition is wrong, because such values might
not appear in the subquery relation's reltarget list, which would
mean that they couldn't be accessed above the level of the subquery
scan. We must check that they appear in the reltarget list, instead.
This can lead to dropping knowledge about the subquery's sort
ordering, but I believe it's okay, because any sort key that the
outer query actually has any interest in would appear in the
reltarget list.
This second issue is of very long standing, but right now there's no
evidence that it causes observable problems before 9.6, so I refrained
from back-patching further than that. We can revisit that choice if
somebody finds a way to make it cause problems in older branches.
(Developing useful test cases for these issues is really problematic;
fixing convert_subquery_pathkeys removes the only known way to exhibit
the create_merge_append_plan bug, and neither of the test cases added
by this patch causes a problem in all branches, even when considering
the issues separately.)
The second issue explains bug #15795 from Suresh Kumar R ("could not
find pathkey item to sort" with nested DISTINCT queries). I stumbled
across the first issue while investigating that.
Discussion: https://postgr.es/m/15795-fadb56c8e44ee73c@postgresql.org
6 years ago
|
|
|
Var *outer_var;
|
|
|
|
|
|
|
|
if (sub_eclass->ec_sortref == 0) /* can't happen */
|
|
|
|
elog(ERROR, "volatile EquivalenceClass has no sortref");
|
Make the upper part of the planner work by generating and comparing Paths.
I've been saying we needed to do this for more than five years, and here it
finally is. This patch removes the ever-growing tangle of spaghetti logic
that grouping_planner() used to use to try to identify the best plan for
post-scan/join query steps. Now, there is (nearly) independent
consideration of each execution step, and entirely separate construction of
Paths to represent each of the possible ways to do that step. We choose
the best Path or set of Paths using the same add_path() logic that's been
used inside query_planner() for years.
In addition, this patch removes the old restriction that subquery_planner()
could return only a single Plan. It now returns a RelOptInfo containing a
set of Paths, just as query_planner() does, and the parent query level can
use each of those Paths as the basis of a SubqueryScanPath at its level.
This allows finding some optimizations that we missed before, wherein a
subquery was capable of returning presorted data and thereby avoiding a
sort in the parent level, making the overall cost cheaper even though
delivering sorted output was not the cheapest plan for the subquery in
isolation. (A couple of regression test outputs change in consequence of
that. However, there is very little change in visible planner behavior
overall, because the point of this patch is not to get immediate planning
benefits but to create the infrastructure for future improvements.)
There is a great deal left to do here. This patch unblocks a lot of
planner work that was basically impractical in the old code structure,
such as allowing FDWs to implement remote aggregation, or rewriting
plan_set_operations() to allow consideration of multiple implementation
orders for set operations. (The latter will likely require a full
rewrite of plan_set_operations(); what I've done here is only to fix it
to return Paths not Plans.) I have also left unfinished some localized
refactoring in createplan.c and planner.c, because it was not necessary
to get this patch to a working state.
Thanks to Robert Haas, David Rowley, and Amit Kapila for review.
10 years ago
|
|
|
tle = get_sortgroupref_tle(sub_eclass->ec_sortref, subquery_tlist);
|
|
|
|
Assert(tle);
|
Repair issues with faulty generation of merge-append plans.
create_merge_append_plan failed to honor the CP_EXACT_TLIST flag:
it would generate the expected targetlist but then it felt free to
add resjunk sort targets to it. This demonstrably leads to assertion
failures in v11 and HEAD, and it's probably just accidental that we
don't see the same in older branches. I've not looked into whether
there would be any real-world consequences in non-assert builds.
In HEAD, create_append_plan has sprouted the same problem, so fix
that too (although we do not have any test cases that seem able to
reach that bug). This is an oversight in commit 3fc6e2d7f which
invented the CP_EXACT_TLIST flag, so back-patch to 9.6 where that
came in.
convert_subquery_pathkeys would create pathkeys for subquery output
values if they match any EquivalenceClass known in the outer query
and are available in the subquery's syntactic targetlist. However,
the second part of that condition is wrong, because such values might
not appear in the subquery relation's reltarget list, which would
mean that they couldn't be accessed above the level of the subquery
scan. We must check that they appear in the reltarget list, instead.
This can lead to dropping knowledge about the subquery's sort
ordering, but I believe it's okay, because any sort key that the
outer query actually has any interest in would appear in the
reltarget list.
This second issue is of very long standing, but right now there's no
evidence that it causes observable problems before 9.6, so I refrained
from back-patching further than that. We can revisit that choice if
somebody finds a way to make it cause problems in older branches.
(Developing useful test cases for these issues is really problematic;
fixing convert_subquery_pathkeys removes the only known way to exhibit
the create_merge_append_plan bug, and neither of the test cases added
by this patch causes a problem in all branches, even when considering
the issues separately.)
The second issue explains bug #15795 from Suresh Kumar R ("could not
find pathkey item to sort" with nested DISTINCT queries). I stumbled
across the first issue while investigating that.
Discussion: https://postgr.es/m/15795-fadb56c8e44ee73c@postgresql.org
6 years ago
|
|
|
/* Is TLE actually available to the outer query? */
|
|
|
|
outer_var = find_var_for_subquery_tle(rel, tle);
|
|
|
|
if (outer_var)
|
|
|
|
{
|
|
|
|
/* We can represent this sub_pathkey */
|
|
|
|
EquivalenceMember *sub_member;
|
|
|
|
EquivalenceClass *outer_ec;
|
|
|
|
|
|
|
|
Assert(list_length(sub_eclass->ec_members) == 1);
|
|
|
|
sub_member = (EquivalenceMember *) linitial(sub_eclass->ec_members);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Note: it might look funny to be setting sortref = 0 for a
|
|
|
|
* reference to a volatile sub_eclass. However, the
|
|
|
|
* expression is *not* volatile in the outer query: it's just
|
|
|
|
* a Var referencing whatever the subquery emitted. (IOW, the
|
|
|
|
* outer query isn't going to re-execute the volatile
|
Compute correct em_nullable_relids in get_eclass_for_sort_expr().
Bug #8591 from Claudio Freire demonstrates that get_eclass_for_sort_expr
must be able to compute valid em_nullable_relids for any new equivalence
class members it creates. I'd worried about this in the commit message
for db9f0e1d9a4a0842c814a464cdc9758c3f20b96c, but claimed that it wasn't a
problem because multi-member ECs should already exist when it runs. That
is transparently wrong, though, because this function is also called by
initialize_mergeclause_eclasses, which runs during deconstruct_jointree.
The example given in the bug report (which the new regression test item
is based upon) fails because the COALESCE() expression is first seen by
initialize_mergeclause_eclasses rather than process_equivalence.
Fixing this requires passing the appropriate nullable_relids set to
get_eclass_for_sort_expr, and it requires new code to compute that set
for top-level expressions such as ORDER BY, GROUP BY, etc. We store
the top-level nullable_relids in a new field in PlannerInfo to avoid
computing it many times. In the back branches, I've added the new
field at the end of the struct to minimize ABI breakage for planner
plugins. There doesn't seem to be a good alternative to changing
get_eclass_for_sort_expr's API signature, though. There probably aren't
any third-party extensions calling that function directly; moreover,
if there are, they probably need to think about what to pass for
nullable_relids anyway.
Back-patch to 9.2, like the previous patch in this area.
12 years ago
|
|
|
* expression itself.) So this is okay. Likewise, it's
|
|
|
|
* correct to pass nullable_relids = NULL, because we're
|
|
|
|
* underneath any outer joins appearing in the outer query.
|
|
|
|
*/
|
|
|
|
outer_ec =
|
|
|
|
get_eclass_for_sort_expr(root,
|
Repair issues with faulty generation of merge-append plans.
create_merge_append_plan failed to honor the CP_EXACT_TLIST flag:
it would generate the expected targetlist but then it felt free to
add resjunk sort targets to it. This demonstrably leads to assertion
failures in v11 and HEAD, and it's probably just accidental that we
don't see the same in older branches. I've not looked into whether
there would be any real-world consequences in non-assert builds.
In HEAD, create_append_plan has sprouted the same problem, so fix
that too (although we do not have any test cases that seem able to
reach that bug). This is an oversight in commit 3fc6e2d7f which
invented the CP_EXACT_TLIST flag, so back-patch to 9.6 where that
came in.
convert_subquery_pathkeys would create pathkeys for subquery output
values if they match any EquivalenceClass known in the outer query
and are available in the subquery's syntactic targetlist. However,
the second part of that condition is wrong, because such values might
not appear in the subquery relation's reltarget list, which would
mean that they couldn't be accessed above the level of the subquery
scan. We must check that they appear in the reltarget list, instead.
This can lead to dropping knowledge about the subquery's sort
ordering, but I believe it's okay, because any sort key that the
outer query actually has any interest in would appear in the
reltarget list.
This second issue is of very long standing, but right now there's no
evidence that it causes observable problems before 9.6, so I refrained
from back-patching further than that. We can revisit that choice if
somebody finds a way to make it cause problems in older branches.
(Developing useful test cases for these issues is really problematic;
fixing convert_subquery_pathkeys removes the only known way to exhibit
the create_merge_append_plan bug, and neither of the test cases added
by this patch causes a problem in all branches, even when considering
the issues separately.)
The second issue explains bug #15795 from Suresh Kumar R ("could not
find pathkey item to sort" with nested DISTINCT queries). I stumbled
across the first issue while investigating that.
Discussion: https://postgr.es/m/15795-fadb56c8e44ee73c@postgresql.org
6 years ago
|
|
|
(Expr *) outer_var,
|
Compute correct em_nullable_relids in get_eclass_for_sort_expr().
Bug #8591 from Claudio Freire demonstrates that get_eclass_for_sort_expr
must be able to compute valid em_nullable_relids for any new equivalence
class members it creates. I'd worried about this in the commit message
for db9f0e1d9a4a0842c814a464cdc9758c3f20b96c, but claimed that it wasn't a
problem because multi-member ECs should already exist when it runs. That
is transparently wrong, though, because this function is also called by
initialize_mergeclause_eclasses, which runs during deconstruct_jointree.
The example given in the bug report (which the new regression test item
is based upon) fails because the COALESCE() expression is first seen by
initialize_mergeclause_eclasses rather than process_equivalence.
Fixing this requires passing the appropriate nullable_relids set to
get_eclass_for_sort_expr, and it requires new code to compute that set
for top-level expressions such as ORDER BY, GROUP BY, etc. We store
the top-level nullable_relids in a new field in PlannerInfo to avoid
computing it many times. In the back branches, I've added the new
field at the end of the struct to minimize ABI breakage for planner
plugins. There doesn't seem to be a good alternative to changing
get_eclass_for_sort_expr's API signature, though. There probably aren't
any third-party extensions calling that function directly; moreover,
if there are, they probably need to think about what to pass for
nullable_relids anyway.
Back-patch to 9.2, like the previous patch in this area.
12 years ago
|
|
|
NULL,
|
|
|
|
sub_eclass->ec_opfamilies,
|
|
|
|
sub_member->em_datatype,
|
|
|
|
sub_eclass->ec_collation,
|
|
|
|
0,
|
Revisit handling of UNION ALL subqueries with non-Var output columns.
In commit 57664ed25e5dea117158a2e663c29e60b3546e1c I tried to fix a bug
reported by Teodor Sigaev by making non-simple-Var output columns distinct
(by wrapping their expressions with dummy PlaceHolderVar nodes). This did
not work too well. Commit b28ffd0fcc583c1811e5295279e7d4366c3cae6c fixed
some ensuing problems with matching to child indexes, but per a recent
report from Claus Stadler, constraint exclusion of UNION ALL subqueries was
still broken, because constant-simplification didn't handle the injected
PlaceHolderVars well either. On reflection, the original patch was quite
misguided: there is no reason to expect that EquivalenceClass child members
will be distinct. So instead of trying to make them so, we should ensure
that we can cope with the situation when they're not.
Accordingly, this patch reverts the code changes in the above-mentioned
commits (though the regression test cases they added stay). Instead, I've
added assorted defenses to make sure that duplicate EC child members don't
cause any problems. Teodor's original problem ("MergeAppend child's
targetlist doesn't match MergeAppend") is addressed more directly by
revising prepare_sort_from_pathkeys to let the parent MergeAppend's sort
list guide creation of each child's sort list.
In passing, get rid of add_sort_column; as far as I can tell, testing for
duplicate sort keys at this stage is dead code. Certainly it doesn't
trigger often enough to be worth expending cycles on in ordinary queries.
And keeping the test would've greatly complicated the new logic in
prepare_sort_from_pathkeys, because comparing pathkey list entries against
a previous output array requires that we not skip any entries in the list.
Back-patch to 9.1, like the previous patches. The only known issue in
this area that wasn't caused by the ill-advised previous patches was the
MergeAppend planning failure, which of course is not relevant before 9.1.
It's possible that we need some of the new defenses against duplicate child
EC entries in older branches, but until there's some clear evidence of that
I'm going to refrain from back-patching further.
14 years ago
|
|
|
rel->relids,
|
|
|
|
false);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If we don't find a matching EC, sub-pathkey isn't
|
|
|
|
* interesting to the outer query
|
|
|
|
*/
|
|
|
|
if (outer_ec)
|
|
|
|
best_pathkey =
|
|
|
|
make_canonical_pathkey(root,
|
|
|
|
outer_ec,
|
|
|
|
sub_pathkey->pk_opfamily,
|
|
|
|
sub_pathkey->pk_strategy,
|
|
|
|
sub_pathkey->pk_nulls_first);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Otherwise, the sub_pathkey's EquivalenceClass could contain
|
|
|
|
* multiple elements (representing knowledge that multiple items
|
|
|
|
* are effectively equal). Each element might match none, one, or
|
|
|
|
* more of the output columns that are visible to the outer query.
|
|
|
|
* This means we may have multiple possible representations of the
|
|
|
|
* sub_pathkey in the context of the outer query. Ideally we
|
|
|
|
* would generate them all and put them all into an EC of the
|
|
|
|
* outer query, thereby propagating equality knowledge up to the
|
|
|
|
* outer query. Right now we cannot do so, because the outer
|
|
|
|
* query's EquivalenceClasses are already frozen when this is
|
|
|
|
* called. Instead we prefer the one that has the highest "score"
|
|
|
|
* (number of EC peers, plus one if it matches the outer
|
|
|
|
* query_pathkeys). This is the most likely to be useful in the
|
|
|
|
* outer query.
|
|
|
|
*/
|
|
|
|
int best_score = -1;
|
|
|
|
ListCell *j;
|
|
|
|
|
|
|
|
foreach(j, sub_eclass->ec_members)
|
|
|
|
{
|
|
|
|
EquivalenceMember *sub_member = (EquivalenceMember *) lfirst(j);
|
|
|
|
Expr *sub_expr = sub_member->em_expr;
|
|
|
|
Oid sub_expr_type = sub_member->em_datatype;
|
|
|
|
Oid sub_expr_coll = sub_eclass->ec_collation;
|
|
|
|
ListCell *k;
|
|
|
|
|
Revisit handling of UNION ALL subqueries with non-Var output columns.
In commit 57664ed25e5dea117158a2e663c29e60b3546e1c I tried to fix a bug
reported by Teodor Sigaev by making non-simple-Var output columns distinct
(by wrapping their expressions with dummy PlaceHolderVar nodes). This did
not work too well. Commit b28ffd0fcc583c1811e5295279e7d4366c3cae6c fixed
some ensuing problems with matching to child indexes, but per a recent
report from Claus Stadler, constraint exclusion of UNION ALL subqueries was
still broken, because constant-simplification didn't handle the injected
PlaceHolderVars well either. On reflection, the original patch was quite
misguided: there is no reason to expect that EquivalenceClass child members
will be distinct. So instead of trying to make them so, we should ensure
that we can cope with the situation when they're not.
Accordingly, this patch reverts the code changes in the above-mentioned
commits (though the regression test cases they added stay). Instead, I've
added assorted defenses to make sure that duplicate EC child members don't
cause any problems. Teodor's original problem ("MergeAppend child's
targetlist doesn't match MergeAppend") is addressed more directly by
revising prepare_sort_from_pathkeys to let the parent MergeAppend's sort
list guide creation of each child's sort list.
In passing, get rid of add_sort_column; as far as I can tell, testing for
duplicate sort keys at this stage is dead code. Certainly it doesn't
trigger often enough to be worth expending cycles on in ordinary queries.
And keeping the test would've greatly complicated the new logic in
prepare_sort_from_pathkeys, because comparing pathkey list entries against
a previous output array requires that we not skip any entries in the list.
Back-patch to 9.1, like the previous patches. The only known issue in
this area that wasn't caused by the ill-advised previous patches was the
MergeAppend planning failure, which of course is not relevant before 9.1.
It's possible that we need some of the new defenses against duplicate child
EC entries in older branches, but until there's some clear evidence of that
I'm going to refrain from back-patching further.
14 years ago
|
|
|
if (sub_member->em_is_child)
|
|
|
|
continue; /* ignore children here */
|
|
|
|
|
Make the upper part of the planner work by generating and comparing Paths.
I've been saying we needed to do this for more than five years, and here it
finally is. This patch removes the ever-growing tangle of spaghetti logic
that grouping_planner() used to use to try to identify the best plan for
post-scan/join query steps. Now, there is (nearly) independent
consideration of each execution step, and entirely separate construction of
Paths to represent each of the possible ways to do that step. We choose
the best Path or set of Paths using the same add_path() logic that's been
used inside query_planner() for years.
In addition, this patch removes the old restriction that subquery_planner()
could return only a single Plan. It now returns a RelOptInfo containing a
set of Paths, just as query_planner() does, and the parent query level can
use each of those Paths as the basis of a SubqueryScanPath at its level.
This allows finding some optimizations that we missed before, wherein a
subquery was capable of returning presorted data and thereby avoiding a
sort in the parent level, making the overall cost cheaper even though
delivering sorted output was not the cheapest plan for the subquery in
isolation. (A couple of regression test outputs change in consequence of
that. However, there is very little change in visible planner behavior
overall, because the point of this patch is not to get immediate planning
benefits but to create the infrastructure for future improvements.)
There is a great deal left to do here. This patch unblocks a lot of
planner work that was basically impractical in the old code structure,
such as allowing FDWs to implement remote aggregation, or rewriting
plan_set_operations() to allow consideration of multiple implementation
orders for set operations. (The latter will likely require a full
rewrite of plan_set_operations(); what I've done here is only to fix it
to return Paths not Plans.) I have also left unfinished some localized
refactoring in createplan.c and planner.c, because it was not necessary
to get this patch to a working state.
Thanks to Robert Haas, David Rowley, and Amit Kapila for review.
10 years ago
|
|
|
foreach(k, subquery_tlist)
|
|
|
|
{
|
|
|
|
TargetEntry *tle = (TargetEntry *) lfirst(k);
|
Repair issues with faulty generation of merge-append plans.
create_merge_append_plan failed to honor the CP_EXACT_TLIST flag:
it would generate the expected targetlist but then it felt free to
add resjunk sort targets to it. This demonstrably leads to assertion
failures in v11 and HEAD, and it's probably just accidental that we
don't see the same in older branches. I've not looked into whether
there would be any real-world consequences in non-assert builds.
In HEAD, create_append_plan has sprouted the same problem, so fix
that too (although we do not have any test cases that seem able to
reach that bug). This is an oversight in commit 3fc6e2d7f which
invented the CP_EXACT_TLIST flag, so back-patch to 9.6 where that
came in.
convert_subquery_pathkeys would create pathkeys for subquery output
values if they match any EquivalenceClass known in the outer query
and are available in the subquery's syntactic targetlist. However,
the second part of that condition is wrong, because such values might
not appear in the subquery relation's reltarget list, which would
mean that they couldn't be accessed above the level of the subquery
scan. We must check that they appear in the reltarget list, instead.
This can lead to dropping knowledge about the subquery's sort
ordering, but I believe it's okay, because any sort key that the
outer query actually has any interest in would appear in the
reltarget list.
This second issue is of very long standing, but right now there's no
evidence that it causes observable problems before 9.6, so I refrained
from back-patching further than that. We can revisit that choice if
somebody finds a way to make it cause problems in older branches.
(Developing useful test cases for these issues is really problematic;
fixing convert_subquery_pathkeys removes the only known way to exhibit
the create_merge_append_plan bug, and neither of the test cases added
by this patch causes a problem in all branches, even when considering
the issues separately.)
The second issue explains bug #15795 from Suresh Kumar R ("could not
find pathkey item to sort" with nested DISTINCT queries). I stumbled
across the first issue while investigating that.
Discussion: https://postgr.es/m/15795-fadb56c8e44ee73c@postgresql.org
6 years ago
|
|
|
Var *outer_var;
|
|
|
|
Expr *tle_expr;
|
|
|
|
EquivalenceClass *outer_ec;
|
|
|
|
PathKey *outer_pk;
|
|
|
|
int score;
|
|
|
|
|
Repair issues with faulty generation of merge-append plans.
create_merge_append_plan failed to honor the CP_EXACT_TLIST flag:
it would generate the expected targetlist but then it felt free to
add resjunk sort targets to it. This demonstrably leads to assertion
failures in v11 and HEAD, and it's probably just accidental that we
don't see the same in older branches. I've not looked into whether
there would be any real-world consequences in non-assert builds.
In HEAD, create_append_plan has sprouted the same problem, so fix
that too (although we do not have any test cases that seem able to
reach that bug). This is an oversight in commit 3fc6e2d7f which
invented the CP_EXACT_TLIST flag, so back-patch to 9.6 where that
came in.
convert_subquery_pathkeys would create pathkeys for subquery output
values if they match any EquivalenceClass known in the outer query
and are available in the subquery's syntactic targetlist. However,
the second part of that condition is wrong, because such values might
not appear in the subquery relation's reltarget list, which would
mean that they couldn't be accessed above the level of the subquery
scan. We must check that they appear in the reltarget list, instead.
This can lead to dropping knowledge about the subquery's sort
ordering, but I believe it's okay, because any sort key that the
outer query actually has any interest in would appear in the
reltarget list.
This second issue is of very long standing, but right now there's no
evidence that it causes observable problems before 9.6, so I refrained
from back-patching further than that. We can revisit that choice if
somebody finds a way to make it cause problems in older branches.
(Developing useful test cases for these issues is really problematic;
fixing convert_subquery_pathkeys removes the only known way to exhibit
the create_merge_append_plan bug, and neither of the test cases added
by this patch causes a problem in all branches, even when considering
the issues separately.)
The second issue explains bug #15795 from Suresh Kumar R ("could not
find pathkey item to sort" with nested DISTINCT queries). I stumbled
across the first issue while investigating that.
Discussion: https://postgr.es/m/15795-fadb56c8e44ee73c@postgresql.org
6 years ago
|
|
|
/* Is TLE actually available to the outer query? */
|
|
|
|
outer_var = find_var_for_subquery_tle(rel, tle);
|
|
|
|
if (!outer_var)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The targetlist entry is considered to match if it
|
|
|
|
* matches after sort-key canonicalization. That is
|
|
|
|
* needed since the sub_expr has been through the same
|
|
|
|
* process.
|
|
|
|
*/
|
|
|
|
tle_expr = canonicalize_ec_expression(tle->expr,
|
|
|
|
sub_expr_type,
|
|
|
|
sub_expr_coll);
|
|
|
|
if (!equal(tle_expr, sub_expr))
|
|
|
|
continue;
|
|
|
|
|
Repair issues with faulty generation of merge-append plans.
create_merge_append_plan failed to honor the CP_EXACT_TLIST flag:
it would generate the expected targetlist but then it felt free to
add resjunk sort targets to it. This demonstrably leads to assertion
failures in v11 and HEAD, and it's probably just accidental that we
don't see the same in older branches. I've not looked into whether
there would be any real-world consequences in non-assert builds.
In HEAD, create_append_plan has sprouted the same problem, so fix
that too (although we do not have any test cases that seem able to
reach that bug). This is an oversight in commit 3fc6e2d7f which
invented the CP_EXACT_TLIST flag, so back-patch to 9.6 where that
came in.
convert_subquery_pathkeys would create pathkeys for subquery output
values if they match any EquivalenceClass known in the outer query
and are available in the subquery's syntactic targetlist. However,
the second part of that condition is wrong, because such values might
not appear in the subquery relation's reltarget list, which would
mean that they couldn't be accessed above the level of the subquery
scan. We must check that they appear in the reltarget list, instead.
This can lead to dropping knowledge about the subquery's sort
ordering, but I believe it's okay, because any sort key that the
outer query actually has any interest in would appear in the
reltarget list.
This second issue is of very long standing, but right now there's no
evidence that it causes observable problems before 9.6, so I refrained
from back-patching further than that. We can revisit that choice if
somebody finds a way to make it cause problems in older branches.
(Developing useful test cases for these issues is really problematic;
fixing convert_subquery_pathkeys removes the only known way to exhibit
the create_merge_append_plan bug, and neither of the test cases added
by this patch causes a problem in all branches, even when considering
the issues separately.)
The second issue explains bug #15795 from Suresh Kumar R ("could not
find pathkey item to sort" with nested DISTINCT queries). I stumbled
across the first issue while investigating that.
Discussion: https://postgr.es/m/15795-fadb56c8e44ee73c@postgresql.org
6 years ago
|
|
|
/* See if we have a matching EC for the TLE */
|
|
|
|
outer_ec = get_eclass_for_sort_expr(root,
|
Repair issues with faulty generation of merge-append plans.
create_merge_append_plan failed to honor the CP_EXACT_TLIST flag:
it would generate the expected targetlist but then it felt free to
add resjunk sort targets to it. This demonstrably leads to assertion
failures in v11 and HEAD, and it's probably just accidental that we
don't see the same in older branches. I've not looked into whether
there would be any real-world consequences in non-assert builds.
In HEAD, create_append_plan has sprouted the same problem, so fix
that too (although we do not have any test cases that seem able to
reach that bug). This is an oversight in commit 3fc6e2d7f which
invented the CP_EXACT_TLIST flag, so back-patch to 9.6 where that
came in.
convert_subquery_pathkeys would create pathkeys for subquery output
values if they match any EquivalenceClass known in the outer query
and are available in the subquery's syntactic targetlist. However,
the second part of that condition is wrong, because such values might
not appear in the subquery relation's reltarget list, which would
mean that they couldn't be accessed above the level of the subquery
scan. We must check that they appear in the reltarget list, instead.
This can lead to dropping knowledge about the subquery's sort
ordering, but I believe it's okay, because any sort key that the
outer query actually has any interest in would appear in the
reltarget list.
This second issue is of very long standing, but right now there's no
evidence that it causes observable problems before 9.6, so I refrained
from back-patching further than that. We can revisit that choice if
somebody finds a way to make it cause problems in older branches.
(Developing useful test cases for these issues is really problematic;
fixing convert_subquery_pathkeys removes the only known way to exhibit
the create_merge_append_plan bug, and neither of the test cases added
by this patch causes a problem in all branches, even when considering
the issues separately.)
The second issue explains bug #15795 from Suresh Kumar R ("could not
find pathkey item to sort" with nested DISTINCT queries). I stumbled
across the first issue while investigating that.
Discussion: https://postgr.es/m/15795-fadb56c8e44ee73c@postgresql.org
6 years ago
|
|
|
(Expr *) outer_var,
|
Compute correct em_nullable_relids in get_eclass_for_sort_expr().
Bug #8591 from Claudio Freire demonstrates that get_eclass_for_sort_expr
must be able to compute valid em_nullable_relids for any new equivalence
class members it creates. I'd worried about this in the commit message
for db9f0e1d9a4a0842c814a464cdc9758c3f20b96c, but claimed that it wasn't a
problem because multi-member ECs should already exist when it runs. That
is transparently wrong, though, because this function is also called by
initialize_mergeclause_eclasses, which runs during deconstruct_jointree.
The example given in the bug report (which the new regression test item
is based upon) fails because the COALESCE() expression is first seen by
initialize_mergeclause_eclasses rather than process_equivalence.
Fixing this requires passing the appropriate nullable_relids set to
get_eclass_for_sort_expr, and it requires new code to compute that set
for top-level expressions such as ORDER BY, GROUP BY, etc. We store
the top-level nullable_relids in a new field in PlannerInfo to avoid
computing it many times. In the back branches, I've added the new
field at the end of the struct to minimize ABI breakage for planner
plugins. There doesn't seem to be a good alternative to changing
get_eclass_for_sort_expr's API signature, though. There probably aren't
any third-party extensions calling that function directly; moreover,
if there are, they probably need to think about what to pass for
nullable_relids anyway.
Back-patch to 9.2, like the previous patch in this area.
12 years ago
|
|
|
NULL,
|
Phase 3 of pgindent updates.
Don't move parenthesized lines to the left, even if that means they
flow past the right margin.
By default, BSD indent lines up statement continuation lines that are
within parentheses so that they start just to the right of the preceding
left parenthesis. However, traditionally, if that resulted in the
continuation line extending to the right of the desired right margin,
then indent would push it left just far enough to not overrun the margin,
if it could do so without making the continuation line start to the left of
the current statement indent. That makes for a weird mix of indentations
unless one has been completely rigid about never violating the 80-column
limit.
This behavior has been pretty universally panned by Postgres developers.
Hence, disable it with indent's new -lpl switch, so that parenthesized
lines are always lined up with the preceding left paren.
This patch is much less interesting than the first round of indent
changes, but also bulkier, so I thought it best to separate the effects.
Discussion: https://postgr.es/m/E1dAmxK-0006EE-1r@gemulon.postgresql.org
Discussion: https://postgr.es/m/30527.1495162840@sss.pgh.pa.us
8 years ago
|
|
|
sub_eclass->ec_opfamilies,
|
|
|
|
sub_expr_type,
|
|
|
|
sub_expr_coll,
|
|
|
|
0,
|
Revisit handling of UNION ALL subqueries with non-Var output columns.
In commit 57664ed25e5dea117158a2e663c29e60b3546e1c I tried to fix a bug
reported by Teodor Sigaev by making non-simple-Var output columns distinct
(by wrapping their expressions with dummy PlaceHolderVar nodes). This did
not work too well. Commit b28ffd0fcc583c1811e5295279e7d4366c3cae6c fixed
some ensuing problems with matching to child indexes, but per a recent
report from Claus Stadler, constraint exclusion of UNION ALL subqueries was
still broken, because constant-simplification didn't handle the injected
PlaceHolderVars well either. On reflection, the original patch was quite
misguided: there is no reason to expect that EquivalenceClass child members
will be distinct. So instead of trying to make them so, we should ensure
that we can cope with the situation when they're not.
Accordingly, this patch reverts the code changes in the above-mentioned
commits (though the regression test cases they added stay). Instead, I've
added assorted defenses to make sure that duplicate EC child members don't
cause any problems. Teodor's original problem ("MergeAppend child's
targetlist doesn't match MergeAppend") is addressed more directly by
revising prepare_sort_from_pathkeys to let the parent MergeAppend's sort
list guide creation of each child's sort list.
In passing, get rid of add_sort_column; as far as I can tell, testing for
duplicate sort keys at this stage is dead code. Certainly it doesn't
trigger often enough to be worth expending cycles on in ordinary queries.
And keeping the test would've greatly complicated the new logic in
prepare_sort_from_pathkeys, because comparing pathkey list entries against
a previous output array requires that we not skip any entries in the list.
Back-patch to 9.1, like the previous patches. The only known issue in
this area that wasn't caused by the ill-advised previous patches was the
MergeAppend planning failure, which of course is not relevant before 9.1.
It's possible that we need some of the new defenses against duplicate child
EC entries in older branches, but until there's some clear evidence of that
I'm going to refrain from back-patching further.
14 years ago
|
|
|
rel->relids,
|
|
|
|
false);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If we don't find a matching EC, this sub-pathkey isn't
|
|
|
|
* interesting to the outer query
|
|
|
|
*/
|
|
|
|
if (!outer_ec)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
outer_pk = make_canonical_pathkey(root,
|
|
|
|
outer_ec,
|
Phase 3 of pgindent updates.
Don't move parenthesized lines to the left, even if that means they
flow past the right margin.
By default, BSD indent lines up statement continuation lines that are
within parentheses so that they start just to the right of the preceding
left parenthesis. However, traditionally, if that resulted in the
continuation line extending to the right of the desired right margin,
then indent would push it left just far enough to not overrun the margin,
if it could do so without making the continuation line start to the left of
the current statement indent. That makes for a weird mix of indentations
unless one has been completely rigid about never violating the 80-column
limit.
This behavior has been pretty universally panned by Postgres developers.
Hence, disable it with indent's new -lpl switch, so that parenthesized
lines are always lined up with the preceding left paren.
This patch is much less interesting than the first round of indent
changes, but also bulkier, so I thought it best to separate the effects.
Discussion: https://postgr.es/m/E1dAmxK-0006EE-1r@gemulon.postgresql.org
Discussion: https://postgr.es/m/30527.1495162840@sss.pgh.pa.us
8 years ago
|
|
|
sub_pathkey->pk_opfamily,
|
|
|
|
sub_pathkey->pk_strategy,
|
|
|
|
sub_pathkey->pk_nulls_first);
|
|
|
|
/* score = # of equivalence peers */
|
|
|
|
score = list_length(outer_ec->ec_members) - 1;
|
|
|
|
/* +1 if it matches the proper query_pathkeys item */
|
|
|
|
if (retvallen < outer_query_keys &&
|
|
|
|
list_nth(root->query_pathkeys, retvallen) == outer_pk)
|
|
|
|
score++;
|
|
|
|
if (score > best_score)
|
|
|
|
{
|
|
|
|
best_pathkey = outer_pk;
|
|
|
|
best_score = score;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If we couldn't find a representation of this sub_pathkey, we're
|
|
|
|
* done (we can't use the ones to its right, either).
|
|
|
|
*/
|
|
|
|
if (!best_pathkey)
|
|
|
|
break;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Eliminate redundant ordering info; could happen if outer query
|
|
|
|
* equivalences subquery keys...
|
|
|
|
*/
|
|
|
|
if (!pathkey_is_redundant(best_pathkey, retval))
|
|
|
|
{
|
|
|
|
retval = lappend(retval, best_pathkey);
|
|
|
|
retvallen++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
Repair issues with faulty generation of merge-append plans.
create_merge_append_plan failed to honor the CP_EXACT_TLIST flag:
it would generate the expected targetlist but then it felt free to
add resjunk sort targets to it. This demonstrably leads to assertion
failures in v11 and HEAD, and it's probably just accidental that we
don't see the same in older branches. I've not looked into whether
there would be any real-world consequences in non-assert builds.
In HEAD, create_append_plan has sprouted the same problem, so fix
that too (although we do not have any test cases that seem able to
reach that bug). This is an oversight in commit 3fc6e2d7f which
invented the CP_EXACT_TLIST flag, so back-patch to 9.6 where that
came in.
convert_subquery_pathkeys would create pathkeys for subquery output
values if they match any EquivalenceClass known in the outer query
and are available in the subquery's syntactic targetlist. However,
the second part of that condition is wrong, because such values might
not appear in the subquery relation's reltarget list, which would
mean that they couldn't be accessed above the level of the subquery
scan. We must check that they appear in the reltarget list, instead.
This can lead to dropping knowledge about the subquery's sort
ordering, but I believe it's okay, because any sort key that the
outer query actually has any interest in would appear in the
reltarget list.
This second issue is of very long standing, but right now there's no
evidence that it causes observable problems before 9.6, so I refrained
from back-patching further than that. We can revisit that choice if
somebody finds a way to make it cause problems in older branches.
(Developing useful test cases for these issues is really problematic;
fixing convert_subquery_pathkeys removes the only known way to exhibit
the create_merge_append_plan bug, and neither of the test cases added
by this patch causes a problem in all branches, even when considering
the issues separately.)
The second issue explains bug #15795 from Suresh Kumar R ("could not
find pathkey item to sort" with nested DISTINCT queries). I stumbled
across the first issue while investigating that.
Discussion: https://postgr.es/m/15795-fadb56c8e44ee73c@postgresql.org
6 years ago
|
|
|
/*
|
|
|
|
* find_var_for_subquery_tle
|
|
|
|
*
|
|
|
|
* If the given subquery tlist entry is due to be emitted by the subquery's
|
|
|
|
* scan node, return a Var for it, else return NULL.
|
|
|
|
*
|
|
|
|
* We need this to ensure that we don't return pathkeys describing values
|
|
|
|
* that are unavailable above the level of the subquery scan.
|
|
|
|
*/
|
|
|
|
static Var *
|
|
|
|
find_var_for_subquery_tle(RelOptInfo *rel, TargetEntry *tle)
|
|
|
|
{
|
|
|
|
ListCell *lc;
|
|
|
|
|
|
|
|
/* If the TLE is resjunk, it's certainly not visible to the outer query */
|
|
|
|
if (tle->resjunk)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* Search the rel's targetlist to see what it will return */
|
|
|
|
foreach(lc, rel->reltarget->exprs)
|
|
|
|
{
|
|
|
|
Var *var = (Var *) lfirst(lc);
|
|
|
|
|
|
|
|
/* Ignore placeholders */
|
|
|
|
if (!IsA(var, Var))
|
|
|
|
continue;
|
|
|
|
Assert(var->varno == rel->relid);
|
|
|
|
|
|
|
|
/* If we find a Var referencing this TLE, we're good */
|
|
|
|
if (var->varattno == tle->resno)
|
|
|
|
return copyObject(var); /* Make a copy for safety */
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* build_join_pathkeys
|
|
|
|
* Build the path keys for a join relation constructed by mergejoin or
|
|
|
|
* nestloop join. This is normally the same as the outer path's keys.
|
|
|
|
*
|
|
|
|
* EXCEPTION: in a FULL or RIGHT join, we cannot treat the result as
|
|
|
|
* having the outer path's path keys, because null lefthand rows may be
|
|
|
|
* inserted at random points. It must be treated as unsorted.
|
|
|
|
*
|
|
|
|
* We truncate away any pathkeys that are uninteresting for higher joins.
|
|
|
|
*
|
|
|
|
* 'joinrel' is the join relation that paths are being formed for
|
|
|
|
* 'jointype' is the join type (inner, left, full, etc)
|
|
|
|
* 'outer_pathkeys' is the list of the current outer path's path keys
|
|
|
|
*
|
|
|
|
* Returns the list of new path keys.
|
|
|
|
*/
|
|
|
|
List *
|
|
|
|
build_join_pathkeys(PlannerInfo *root,
|
|
|
|
RelOptInfo *joinrel,
|
|
|
|
JoinType jointype,
|
|
|
|
List *outer_pathkeys)
|
|
|
|
{
|
|
|
|
if (jointype == JOIN_FULL || jointype == JOIN_RIGHT)
|
|
|
|
return NIL;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This used to be quite a complex bit of code, but now that all pathkey
|
|
|
|
* sublists start out life canonicalized, we don't have to do a darn thing
|
|
|
|
* here!
|
|
|
|
*
|
|
|
|
* We do, however, need to truncate the pathkeys list, since it may
|
|
|
|
* contain pathkeys that were useful for forming this joinrel but are
|
|
|
|
* uninteresting to higher levels.
|
|
|
|
*/
|
|
|
|
return truncate_useless_pathkeys(root, joinrel, outer_pathkeys);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* PATHKEYS AND SORT CLAUSES
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* make_pathkeys_for_sortclauses
|
|
|
|
* Generate a pathkeys list that represents the sort order specified
|
|
|
|
* by a list of SortGroupClauses
|
|
|
|
*
|
Postpone creation of pathkeys lists to fix bug #8049.
This patch gets rid of the concept of, and infrastructure for,
non-canonical PathKeys; we now only ever create canonical pathkey lists.
The need for non-canonical pathkeys came from the desire to have
grouping_planner initialize query_pathkeys and related pathkey lists before
calling query_planner. However, since query_planner didn't actually *do*
anything with those lists before they'd been made canonical, we can get rid
of the whole mess by just not creating the lists at all until the point
where we formerly canonicalized them.
There are several ways in which we could implement that without making
query_planner itself deal with grouping/sorting features (which are
supposed to be the province of grouping_planner). I chose to add a
callback function to query_planner's API; other alternatives would have
required adding more fields to PlannerInfo, which while not bad in itself
would create an ABI break for planner-related plugins in the 9.2 release
series. This still breaks ABI for anything that calls query_planner
directly, but it seems somewhat unlikely that there are any such plugins.
I had originally conceived of this change as merely a step on the way to
fixing bug #8049 from Teun Hoogendoorn; but it turns out that this fixes
that bug all by itself, as per the added regression test. The reason is
that now get_eclass_for_sort_expr is adding the ORDER BY expression at the
end of EquivalenceClass creation not the start, and so anything that is in
a multi-member EquivalenceClass has already been created with correct
em_nullable_relids. I am suspicious that there are related scenarios in
which we still need to teach get_eclass_for_sort_expr to compute correct
nullable_relids, but am not eager to risk destabilizing either 9.2 or 9.3
to fix bugs that are only hypothetical. So for the moment, do this and
stop here.
Back-patch to 9.2 but not to earlier branches, since they don't exhibit
this bug for lack of join-clause-movement logic that depends on
em_nullable_relids being correct. (We might have to revisit that choice
if any related bugs turn up.) In 9.2, don't change the signature of
make_pathkeys_for_sortclauses nor remove canonicalize_pathkeys, so as
not to risk more plugin breakage than we have to.
13 years ago
|
|
|
* The resulting PathKeys are always in canonical form. (Actually, there
|
|
|
|
* is no longer any code anywhere that creates non-canonical PathKeys.)
|
|
|
|
*
|
Compute correct em_nullable_relids in get_eclass_for_sort_expr().
Bug #8591 from Claudio Freire demonstrates that get_eclass_for_sort_expr
must be able to compute valid em_nullable_relids for any new equivalence
class members it creates. I'd worried about this in the commit message
for db9f0e1d9a4a0842c814a464cdc9758c3f20b96c, but claimed that it wasn't a
problem because multi-member ECs should already exist when it runs. That
is transparently wrong, though, because this function is also called by
initialize_mergeclause_eclasses, which runs during deconstruct_jointree.
The example given in the bug report (which the new regression test item
is based upon) fails because the COALESCE() expression is first seen by
initialize_mergeclause_eclasses rather than process_equivalence.
Fixing this requires passing the appropriate nullable_relids set to
get_eclass_for_sort_expr, and it requires new code to compute that set
for top-level expressions such as ORDER BY, GROUP BY, etc. We store
the top-level nullable_relids in a new field in PlannerInfo to avoid
computing it many times. In the back branches, I've added the new
field at the end of the struct to minimize ABI breakage for planner
plugins. There doesn't seem to be a good alternative to changing
get_eclass_for_sort_expr's API signature, though. There probably aren't
any third-party extensions calling that function directly; moreover,
if there are, they probably need to think about what to pass for
nullable_relids anyway.
Back-patch to 9.2, like the previous patch in this area.
12 years ago
|
|
|
* We assume that root->nullable_baserels is the set of base relids that could
|
|
|
|
* have gone to NULL below the SortGroupClause expressions. This is okay if
|
|
|
|
* the expressions came from the query's top level (ORDER BY, DISTINCT, etc)
|
|
|
|
* and if this function is only invoked after deconstruct_jointree. In the
|
|
|
|
* future we might have to make callers pass in the appropriate
|
|
|
|
* nullable-relids set, but for now it seems unnecessary.
|
|
|
|
*
|
|
|
|
* 'sortclauses' is a list of SortGroupClause nodes
|
|
|
|
* 'tlist' is the targetlist to find the referenced tlist entries in
|
|
|
|
*/
|
|
|
|
List *
|
|
|
|
make_pathkeys_for_sortclauses(PlannerInfo *root,
|
|
|
|
List *sortclauses,
|
Postpone creation of pathkeys lists to fix bug #8049.
This patch gets rid of the concept of, and infrastructure for,
non-canonical PathKeys; we now only ever create canonical pathkey lists.
The need for non-canonical pathkeys came from the desire to have
grouping_planner initialize query_pathkeys and related pathkey lists before
calling query_planner. However, since query_planner didn't actually *do*
anything with those lists before they'd been made canonical, we can get rid
of the whole mess by just not creating the lists at all until the point
where we formerly canonicalized them.
There are several ways in which we could implement that without making
query_planner itself deal with grouping/sorting features (which are
supposed to be the province of grouping_planner). I chose to add a
callback function to query_planner's API; other alternatives would have
required adding more fields to PlannerInfo, which while not bad in itself
would create an ABI break for planner-related plugins in the 9.2 release
series. This still breaks ABI for anything that calls query_planner
directly, but it seems somewhat unlikely that there are any such plugins.
I had originally conceived of this change as merely a step on the way to
fixing bug #8049 from Teun Hoogendoorn; but it turns out that this fixes
that bug all by itself, as per the added regression test. The reason is
that now get_eclass_for_sort_expr is adding the ORDER BY expression at the
end of EquivalenceClass creation not the start, and so anything that is in
a multi-member EquivalenceClass has already been created with correct
em_nullable_relids. I am suspicious that there are related scenarios in
which we still need to teach get_eclass_for_sort_expr to compute correct
nullable_relids, but am not eager to risk destabilizing either 9.2 or 9.3
to fix bugs that are only hypothetical. So for the moment, do this and
stop here.
Back-patch to 9.2 but not to earlier branches, since they don't exhibit
this bug for lack of join-clause-movement logic that depends on
em_nullable_relids being correct. (We might have to revisit that choice
if any related bugs turn up.) In 9.2, don't change the signature of
make_pathkeys_for_sortclauses nor remove canonicalize_pathkeys, so as
not to risk more plugin breakage than we have to.
13 years ago
|
|
|
List *tlist)
|
|
|
|
{
|
|
|
|
List *pathkeys = NIL;
|
|
|
|
ListCell *l;
|
|
|
|
|
|
|
|
foreach(l, sortclauses)
|
|
|
|
{
|
|
|
|
SortGroupClause *sortcl = (SortGroupClause *) lfirst(l);
|
|
|
|
Expr *sortkey;
|
|
|
|
PathKey *pathkey;
|
|
|
|
|
|
|
|
sortkey = (Expr *) get_sortgroupclause_expr(sortcl, tlist);
|
|
|
|
Assert(OidIsValid(sortcl->sortop));
|
|
|
|
pathkey = make_pathkey_from_sortop(root,
|
|
|
|
sortkey,
|
Compute correct em_nullable_relids in get_eclass_for_sort_expr().
Bug #8591 from Claudio Freire demonstrates that get_eclass_for_sort_expr
must be able to compute valid em_nullable_relids for any new equivalence
class members it creates. I'd worried about this in the commit message
for db9f0e1d9a4a0842c814a464cdc9758c3f20b96c, but claimed that it wasn't a
problem because multi-member ECs should already exist when it runs. That
is transparently wrong, though, because this function is also called by
initialize_mergeclause_eclasses, which runs during deconstruct_jointree.
The example given in the bug report (which the new regression test item
is based upon) fails because the COALESCE() expression is first seen by
initialize_mergeclause_eclasses rather than process_equivalence.
Fixing this requires passing the appropriate nullable_relids set to
get_eclass_for_sort_expr, and it requires new code to compute that set
for top-level expressions such as ORDER BY, GROUP BY, etc. We store
the top-level nullable_relids in a new field in PlannerInfo to avoid
computing it many times. In the back branches, I've added the new
field at the end of the struct to minimize ABI breakage for planner
plugins. There doesn't seem to be a good alternative to changing
get_eclass_for_sort_expr's API signature, though. There probably aren't
any third-party extensions calling that function directly; moreover,
if there are, they probably need to think about what to pass for
nullable_relids anyway.
Back-patch to 9.2, like the previous patch in this area.
12 years ago
|
|
|
root->nullable_baserels,
|
|
|
|
sortcl->sortop,
|
|
|
|
sortcl->nulls_first,
|
|
|
|
sortcl->tleSortGroupRef,
|
Postpone creation of pathkeys lists to fix bug #8049.
This patch gets rid of the concept of, and infrastructure for,
non-canonical PathKeys; we now only ever create canonical pathkey lists.
The need for non-canonical pathkeys came from the desire to have
grouping_planner initialize query_pathkeys and related pathkey lists before
calling query_planner. However, since query_planner didn't actually *do*
anything with those lists before they'd been made canonical, we can get rid
of the whole mess by just not creating the lists at all until the point
where we formerly canonicalized them.
There are several ways in which we could implement that without making
query_planner itself deal with grouping/sorting features (which are
supposed to be the province of grouping_planner). I chose to add a
callback function to query_planner's API; other alternatives would have
required adding more fields to PlannerInfo, which while not bad in itself
would create an ABI break for planner-related plugins in the 9.2 release
series. This still breaks ABI for anything that calls query_planner
directly, but it seems somewhat unlikely that there are any such plugins.
I had originally conceived of this change as merely a step on the way to
fixing bug #8049 from Teun Hoogendoorn; but it turns out that this fixes
that bug all by itself, as per the added regression test. The reason is
that now get_eclass_for_sort_expr is adding the ORDER BY expression at the
end of EquivalenceClass creation not the start, and so anything that is in
a multi-member EquivalenceClass has already been created with correct
em_nullable_relids. I am suspicious that there are related scenarios in
which we still need to teach get_eclass_for_sort_expr to compute correct
nullable_relids, but am not eager to risk destabilizing either 9.2 or 9.3
to fix bugs that are only hypothetical. So for the moment, do this and
stop here.
Back-patch to 9.2 but not to earlier branches, since they don't exhibit
this bug for lack of join-clause-movement logic that depends on
em_nullable_relids being correct. (We might have to revisit that choice
if any related bugs turn up.) In 9.2, don't change the signature of
make_pathkeys_for_sortclauses nor remove canonicalize_pathkeys, so as
not to risk more plugin breakage than we have to.
13 years ago
|
|
|
true);
|
|
|
|
|
|
|
|
/* Canonical form eliminates redundant ordering keys */
|
Postpone creation of pathkeys lists to fix bug #8049.
This patch gets rid of the concept of, and infrastructure for,
non-canonical PathKeys; we now only ever create canonical pathkey lists.
The need for non-canonical pathkeys came from the desire to have
grouping_planner initialize query_pathkeys and related pathkey lists before
calling query_planner. However, since query_planner didn't actually *do*
anything with those lists before they'd been made canonical, we can get rid
of the whole mess by just not creating the lists at all until the point
where we formerly canonicalized them.
There are several ways in which we could implement that without making
query_planner itself deal with grouping/sorting features (which are
supposed to be the province of grouping_planner). I chose to add a
callback function to query_planner's API; other alternatives would have
required adding more fields to PlannerInfo, which while not bad in itself
would create an ABI break for planner-related plugins in the 9.2 release
series. This still breaks ABI for anything that calls query_planner
directly, but it seems somewhat unlikely that there are any such plugins.
I had originally conceived of this change as merely a step on the way to
fixing bug #8049 from Teun Hoogendoorn; but it turns out that this fixes
that bug all by itself, as per the added regression test. The reason is
that now get_eclass_for_sort_expr is adding the ORDER BY expression at the
end of EquivalenceClass creation not the start, and so anything that is in
a multi-member EquivalenceClass has already been created with correct
em_nullable_relids. I am suspicious that there are related scenarios in
which we still need to teach get_eclass_for_sort_expr to compute correct
nullable_relids, but am not eager to risk destabilizing either 9.2 or 9.3
to fix bugs that are only hypothetical. So for the moment, do this and
stop here.
Back-patch to 9.2 but not to earlier branches, since they don't exhibit
this bug for lack of join-clause-movement logic that depends on
em_nullable_relids being correct. (We might have to revisit that choice
if any related bugs turn up.) In 9.2, don't change the signature of
make_pathkeys_for_sortclauses nor remove canonicalize_pathkeys, so as
not to risk more plugin breakage than we have to.
13 years ago
|
|
|
if (!pathkey_is_redundant(pathkey, pathkeys))
|
|
|
|
pathkeys = lappend(pathkeys, pathkey);
|
|
|
|
}
|
|
|
|
return pathkeys;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* PATHKEYS AND MERGECLAUSES
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* initialize_mergeclause_eclasses
|
|
|
|
* Set the EquivalenceClass links in a mergeclause restrictinfo.
|
|
|
|
*
|
|
|
|
* RestrictInfo contains fields in which we may cache pointers to
|
|
|
|
* EquivalenceClasses for the left and right inputs of the mergeclause.
|
|
|
|
* (If the mergeclause is a true equivalence clause these will be the
|
|
|
|
* same EquivalenceClass, otherwise not.) If the mergeclause is either
|
|
|
|
* used to generate an EquivalenceClass, or derived from an EquivalenceClass,
|
|
|
|
* then it's easy to set up the left_ec and right_ec members --- otherwise,
|
|
|
|
* this function should be called to set them up. We will generate new
|
|
|
|
* EquivalenceClauses if necessary to represent the mergeclause's left and
|
|
|
|
* right sides.
|
|
|
|
*
|
|
|
|
* Note this is called before EC merging is complete, so the links won't
|
|
|
|
* necessarily point to canonical ECs. Before they are actually used for
|
|
|
|
* anything, update_mergeclause_eclasses must be called to ensure that
|
|
|
|
* they've been updated to point to canonical ECs.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
initialize_mergeclause_eclasses(PlannerInfo *root, RestrictInfo *restrictinfo)
|
|
|
|
{
|
|
|
|
Expr *clause = restrictinfo->clause;
|
|
|
|
Oid lefttype,
|
|
|
|
righttype;
|
|
|
|
|
|
|
|
/* Should be a mergeclause ... */
|
|
|
|
Assert(restrictinfo->mergeopfamilies != NIL);
|
|
|
|
/* ... with links not yet set */
|
|
|
|
Assert(restrictinfo->left_ec == NULL);
|
|
|
|
Assert(restrictinfo->right_ec == NULL);
|
|
|
|
|
|
|
|
/* Need the declared input types of the operator */
|
|
|
|
op_input_types(((OpExpr *) clause)->opno, &lefttype, &righttype);
|
|
|
|
|
|
|
|
/* Find or create a matching EquivalenceClass for each side */
|
|
|
|
restrictinfo->left_ec =
|
|
|
|
get_eclass_for_sort_expr(root,
|
|
|
|
(Expr *) get_leftop(clause),
|
Compute correct em_nullable_relids in get_eclass_for_sort_expr().
Bug #8591 from Claudio Freire demonstrates that get_eclass_for_sort_expr
must be able to compute valid em_nullable_relids for any new equivalence
class members it creates. I'd worried about this in the commit message
for db9f0e1d9a4a0842c814a464cdc9758c3f20b96c, but claimed that it wasn't a
problem because multi-member ECs should already exist when it runs. That
is transparently wrong, though, because this function is also called by
initialize_mergeclause_eclasses, which runs during deconstruct_jointree.
The example given in the bug report (which the new regression test item
is based upon) fails because the COALESCE() expression is first seen by
initialize_mergeclause_eclasses rather than process_equivalence.
Fixing this requires passing the appropriate nullable_relids set to
get_eclass_for_sort_expr, and it requires new code to compute that set
for top-level expressions such as ORDER BY, GROUP BY, etc. We store
the top-level nullable_relids in a new field in PlannerInfo to avoid
computing it many times. In the back branches, I've added the new
field at the end of the struct to minimize ABI breakage for planner
plugins. There doesn't seem to be a good alternative to changing
get_eclass_for_sort_expr's API signature, though. There probably aren't
any third-party extensions calling that function directly; moreover,
if there are, they probably need to think about what to pass for
nullable_relids anyway.
Back-patch to 9.2, like the previous patch in this area.
12 years ago
|
|
|
restrictinfo->nullable_relids,
|
|
|
|
restrictinfo->mergeopfamilies,
|
|
|
|
lefttype,
|
|
|
|
((OpExpr *) clause)->inputcollid,
|
|
|
|
0,
|
Revisit handling of UNION ALL subqueries with non-Var output columns.
In commit 57664ed25e5dea117158a2e663c29e60b3546e1c I tried to fix a bug
reported by Teodor Sigaev by making non-simple-Var output columns distinct
(by wrapping their expressions with dummy PlaceHolderVar nodes). This did
not work too well. Commit b28ffd0fcc583c1811e5295279e7d4366c3cae6c fixed
some ensuing problems with matching to child indexes, but per a recent
report from Claus Stadler, constraint exclusion of UNION ALL subqueries was
still broken, because constant-simplification didn't handle the injected
PlaceHolderVars well either. On reflection, the original patch was quite
misguided: there is no reason to expect that EquivalenceClass child members
will be distinct. So instead of trying to make them so, we should ensure
that we can cope with the situation when they're not.
Accordingly, this patch reverts the code changes in the above-mentioned
commits (though the regression test cases they added stay). Instead, I've
added assorted defenses to make sure that duplicate EC child members don't
cause any problems. Teodor's original problem ("MergeAppend child's
targetlist doesn't match MergeAppend") is addressed more directly by
revising prepare_sort_from_pathkeys to let the parent MergeAppend's sort
list guide creation of each child's sort list.
In passing, get rid of add_sort_column; as far as I can tell, testing for
duplicate sort keys at this stage is dead code. Certainly it doesn't
trigger often enough to be worth expending cycles on in ordinary queries.
And keeping the test would've greatly complicated the new logic in
prepare_sort_from_pathkeys, because comparing pathkey list entries against
a previous output array requires that we not skip any entries in the list.
Back-patch to 9.1, like the previous patches. The only known issue in
this area that wasn't caused by the ill-advised previous patches was the
MergeAppend planning failure, which of course is not relevant before 9.1.
It's possible that we need some of the new defenses against duplicate child
EC entries in older branches, but until there's some clear evidence of that
I'm going to refrain from back-patching further.
14 years ago
|
|
|
NULL,
|
|
|
|
true);
|
|
|
|
restrictinfo->right_ec =
|
|
|
|
get_eclass_for_sort_expr(root,
|
|
|
|
(Expr *) get_rightop(clause),
|
Compute correct em_nullable_relids in get_eclass_for_sort_expr().
Bug #8591 from Claudio Freire demonstrates that get_eclass_for_sort_expr
must be able to compute valid em_nullable_relids for any new equivalence
class members it creates. I'd worried about this in the commit message
for db9f0e1d9a4a0842c814a464cdc9758c3f20b96c, but claimed that it wasn't a
problem because multi-member ECs should already exist when it runs. That
is transparently wrong, though, because this function is also called by
initialize_mergeclause_eclasses, which runs during deconstruct_jointree.
The example given in the bug report (which the new regression test item
is based upon) fails because the COALESCE() expression is first seen by
initialize_mergeclause_eclasses rather than process_equivalence.
Fixing this requires passing the appropriate nullable_relids set to
get_eclass_for_sort_expr, and it requires new code to compute that set
for top-level expressions such as ORDER BY, GROUP BY, etc. We store
the top-level nullable_relids in a new field in PlannerInfo to avoid
computing it many times. In the back branches, I've added the new
field at the end of the struct to minimize ABI breakage for planner
plugins. There doesn't seem to be a good alternative to changing
get_eclass_for_sort_expr's API signature, though. There probably aren't
any third-party extensions calling that function directly; moreover,
if there are, they probably need to think about what to pass for
nullable_relids anyway.
Back-patch to 9.2, like the previous patch in this area.
12 years ago
|
|
|
restrictinfo->nullable_relids,
|
|
|
|
restrictinfo->mergeopfamilies,
|
|
|
|
righttype,
|
|
|
|
((OpExpr *) clause)->inputcollid,
|
|
|
|
0,
|
Revisit handling of UNION ALL subqueries with non-Var output columns.
In commit 57664ed25e5dea117158a2e663c29e60b3546e1c I tried to fix a bug
reported by Teodor Sigaev by making non-simple-Var output columns distinct
(by wrapping their expressions with dummy PlaceHolderVar nodes). This did
not work too well. Commit b28ffd0fcc583c1811e5295279e7d4366c3cae6c fixed
some ensuing problems with matching to child indexes, but per a recent
report from Claus Stadler, constraint exclusion of UNION ALL subqueries was
still broken, because constant-simplification didn't handle the injected
PlaceHolderVars well either. On reflection, the original patch was quite
misguided: there is no reason to expect that EquivalenceClass child members
will be distinct. So instead of trying to make them so, we should ensure
that we can cope with the situation when they're not.
Accordingly, this patch reverts the code changes in the above-mentioned
commits (though the regression test cases they added stay). Instead, I've
added assorted defenses to make sure that duplicate EC child members don't
cause any problems. Teodor's original problem ("MergeAppend child's
targetlist doesn't match MergeAppend") is addressed more directly by
revising prepare_sort_from_pathkeys to let the parent MergeAppend's sort
list guide creation of each child's sort list.
In passing, get rid of add_sort_column; as far as I can tell, testing for
duplicate sort keys at this stage is dead code. Certainly it doesn't
trigger often enough to be worth expending cycles on in ordinary queries.
And keeping the test would've greatly complicated the new logic in
prepare_sort_from_pathkeys, because comparing pathkey list entries against
a previous output array requires that we not skip any entries in the list.
Back-patch to 9.1, like the previous patches. The only known issue in
this area that wasn't caused by the ill-advised previous patches was the
MergeAppend planning failure, which of course is not relevant before 9.1.
It's possible that we need some of the new defenses against duplicate child
EC entries in older branches, but until there's some clear evidence of that
I'm going to refrain from back-patching further.
14 years ago
|
|
|
NULL,
|
|
|
|
true);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* update_mergeclause_eclasses
|
|
|
|
* Make the cached EquivalenceClass links valid in a mergeclause
|
|
|
|
* restrictinfo.
|
|
|
|
*
|
|
|
|
* These pointers should have been set by process_equivalence or
|
|
|
|
* initialize_mergeclause_eclasses, but they might have been set to
|
|
|
|
* non-canonical ECs that got merged later. Chase up to the canonical
|
|
|
|
* merged parent if so.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
update_mergeclause_eclasses(PlannerInfo *root, RestrictInfo *restrictinfo)
|
|
|
|
{
|
|
|
|
/* Should be a merge clause ... */
|
|
|
|
Assert(restrictinfo->mergeopfamilies != NIL);
|
|
|
|
/* ... with pointers already set */
|
|
|
|
Assert(restrictinfo->left_ec != NULL);
|
|
|
|
Assert(restrictinfo->right_ec != NULL);
|
|
|
|
|
|
|
|
/* Chase up to the top as needed */
|
|
|
|
while (restrictinfo->left_ec->ec_merged)
|
|
|
|
restrictinfo->left_ec = restrictinfo->left_ec->ec_merged;
|
|
|
|
while (restrictinfo->right_ec->ec_merged)
|
|
|
|
restrictinfo->right_ec = restrictinfo->right_ec->ec_merged;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
Fix planner failures with overlapping mergejoin clauses in an outer join.
Given overlapping or partially redundant join clauses, for example
t1 JOIN t2 ON t1.a = t2.x AND t1.b = t2.x
the planner's EquivalenceClass machinery will ordinarily refactor the
clauses as "t1.a = t1.b AND t1.a = t2.x", so that join processing doesn't
see multiple references to the same EquivalenceClass in a list of join
equality clauses. However, if the join is outer, it's incorrect to derive
a restriction clause on the outer side from the join conditions, so the
clause refactoring does not happen and we end up with overlapping join
conditions. The code that attempted to deal with such cases had several
subtle bugs, which could result in "left and right pathkeys do not match in
mergejoin" or "outer pathkeys do not match mergeclauses" planner errors,
if the selected join plan type was a mergejoin. (It does not appear that
any actually incorrect plan could have been emitted.)
The core of the problem really was failure to recognize that the outer and
inner relations' pathkeys have different relationships to the mergeclause
list. A join's mergeclause list is constructed by reference to the outer
pathkeys, so it will always be ordered the same as the outer pathkeys, but
this cannot be presumed true for the inner pathkeys. If the inner sides of
the mergeclauses contain multiple references to the same EquivalenceClass
({t2.x} in the above example) then a simplistic rendering of the required
inner sort order is like "ORDER BY t2.x, t2.x", but the pathkey machinery
recognizes that the second sort column is redundant and throws it away.
The mergejoin planning code failed to account for that behavior properly.
One error was to try to generate cut-down versions of the mergeclause list
from cut-down versions of the inner pathkeys in the same way as the initial
construction of the mergeclause list from the outer pathkeys was done; this
could lead to choosing a mergeclause list that fails to match the outer
pathkeys. The other problem was that the pathkey cross-checking code in
create_mergejoin_plan treated the inner and outer pathkey lists
identically, whereas actually the expectations for them must be different.
That led to false "pathkeys do not match" failures in some cases, and in
principle could have led to failure to detect bogus plans in other cases,
though there is no indication that such bogus plans could be generated.
Reported by Alexander Kuzmenkov, who also reviewed this patch. This has
been broken for years (back to around 8.3 according to my testing), so
back-patch to all supported branches.
Discussion: https://postgr.es/m/5dad9160-4632-0e47-e120-8e2082000c01@postgrespro.ru
8 years ago
|
|
|
* find_mergeclauses_for_outer_pathkeys
|
|
|
|
* This routine attempts to find a list of mergeclauses that can be
|
|
|
|
* used with a specified ordering for the join's outer relation.
|
|
|
|
* If successful, it returns a list of mergeclauses.
|
|
|
|
*
|
Fix planner failures with overlapping mergejoin clauses in an outer join.
Given overlapping or partially redundant join clauses, for example
t1 JOIN t2 ON t1.a = t2.x AND t1.b = t2.x
the planner's EquivalenceClass machinery will ordinarily refactor the
clauses as "t1.a = t1.b AND t1.a = t2.x", so that join processing doesn't
see multiple references to the same EquivalenceClass in a list of join
equality clauses. However, if the join is outer, it's incorrect to derive
a restriction clause on the outer side from the join conditions, so the
clause refactoring does not happen and we end up with overlapping join
conditions. The code that attempted to deal with such cases had several
subtle bugs, which could result in "left and right pathkeys do not match in
mergejoin" or "outer pathkeys do not match mergeclauses" planner errors,
if the selected join plan type was a mergejoin. (It does not appear that
any actually incorrect plan could have been emitted.)
The core of the problem really was failure to recognize that the outer and
inner relations' pathkeys have different relationships to the mergeclause
list. A join's mergeclause list is constructed by reference to the outer
pathkeys, so it will always be ordered the same as the outer pathkeys, but
this cannot be presumed true for the inner pathkeys. If the inner sides of
the mergeclauses contain multiple references to the same EquivalenceClass
({t2.x} in the above example) then a simplistic rendering of the required
inner sort order is like "ORDER BY t2.x, t2.x", but the pathkey machinery
recognizes that the second sort column is redundant and throws it away.
The mergejoin planning code failed to account for that behavior properly.
One error was to try to generate cut-down versions of the mergeclause list
from cut-down versions of the inner pathkeys in the same way as the initial
construction of the mergeclause list from the outer pathkeys was done; this
could lead to choosing a mergeclause list that fails to match the outer
pathkeys. The other problem was that the pathkey cross-checking code in
create_mergejoin_plan treated the inner and outer pathkey lists
identically, whereas actually the expectations for them must be different.
That led to false "pathkeys do not match" failures in some cases, and in
principle could have led to failure to detect bogus plans in other cases,
though there is no indication that such bogus plans could be generated.
Reported by Alexander Kuzmenkov, who also reviewed this patch. This has
been broken for years (back to around 8.3 according to my testing), so
back-patch to all supported branches.
Discussion: https://postgr.es/m/5dad9160-4632-0e47-e120-8e2082000c01@postgrespro.ru
8 years ago
|
|
|
* 'pathkeys' is a pathkeys list showing the ordering of an outer-rel path.
|
|
|
|
* 'restrictinfos' is a list of mergejoinable restriction clauses for the
|
Fix planner failures with overlapping mergejoin clauses in an outer join.
Given overlapping or partially redundant join clauses, for example
t1 JOIN t2 ON t1.a = t2.x AND t1.b = t2.x
the planner's EquivalenceClass machinery will ordinarily refactor the
clauses as "t1.a = t1.b AND t1.a = t2.x", so that join processing doesn't
see multiple references to the same EquivalenceClass in a list of join
equality clauses. However, if the join is outer, it's incorrect to derive
a restriction clause on the outer side from the join conditions, so the
clause refactoring does not happen and we end up with overlapping join
conditions. The code that attempted to deal with such cases had several
subtle bugs, which could result in "left and right pathkeys do not match in
mergejoin" or "outer pathkeys do not match mergeclauses" planner errors,
if the selected join plan type was a mergejoin. (It does not appear that
any actually incorrect plan could have been emitted.)
The core of the problem really was failure to recognize that the outer and
inner relations' pathkeys have different relationships to the mergeclause
list. A join's mergeclause list is constructed by reference to the outer
pathkeys, so it will always be ordered the same as the outer pathkeys, but
this cannot be presumed true for the inner pathkeys. If the inner sides of
the mergeclauses contain multiple references to the same EquivalenceClass
({t2.x} in the above example) then a simplistic rendering of the required
inner sort order is like "ORDER BY t2.x, t2.x", but the pathkey machinery
recognizes that the second sort column is redundant and throws it away.
The mergejoin planning code failed to account for that behavior properly.
One error was to try to generate cut-down versions of the mergeclause list
from cut-down versions of the inner pathkeys in the same way as the initial
construction of the mergeclause list from the outer pathkeys was done; this
could lead to choosing a mergeclause list that fails to match the outer
pathkeys. The other problem was that the pathkey cross-checking code in
create_mergejoin_plan treated the inner and outer pathkey lists
identically, whereas actually the expectations for them must be different.
That led to false "pathkeys do not match" failures in some cases, and in
principle could have led to failure to detect bogus plans in other cases,
though there is no indication that such bogus plans could be generated.
Reported by Alexander Kuzmenkov, who also reviewed this patch. This has
been broken for years (back to around 8.3 according to my testing), so
back-patch to all supported branches.
Discussion: https://postgr.es/m/5dad9160-4632-0e47-e120-8e2082000c01@postgrespro.ru
8 years ago
|
|
|
* join relation being formed, in no particular order.
|
|
|
|
*
|
|
|
|
* The restrictinfos must be marked (via outer_is_left) to show which side
|
|
|
|
* of each clause is associated with the current outer path. (See
|
|
|
|
* select_mergejoin_clauses())
|
|
|
|
*
|
|
|
|
* The result is NIL if no merge can be done, else a maximal list of
|
|
|
|
* usable mergeclauses (represented as a list of their restrictinfo nodes).
|
Fix planner failures with overlapping mergejoin clauses in an outer join.
Given overlapping or partially redundant join clauses, for example
t1 JOIN t2 ON t1.a = t2.x AND t1.b = t2.x
the planner's EquivalenceClass machinery will ordinarily refactor the
clauses as "t1.a = t1.b AND t1.a = t2.x", so that join processing doesn't
see multiple references to the same EquivalenceClass in a list of join
equality clauses. However, if the join is outer, it's incorrect to derive
a restriction clause on the outer side from the join conditions, so the
clause refactoring does not happen and we end up with overlapping join
conditions. The code that attempted to deal with such cases had several
subtle bugs, which could result in "left and right pathkeys do not match in
mergejoin" or "outer pathkeys do not match mergeclauses" planner errors,
if the selected join plan type was a mergejoin. (It does not appear that
any actually incorrect plan could have been emitted.)
The core of the problem really was failure to recognize that the outer and
inner relations' pathkeys have different relationships to the mergeclause
list. A join's mergeclause list is constructed by reference to the outer
pathkeys, so it will always be ordered the same as the outer pathkeys, but
this cannot be presumed true for the inner pathkeys. If the inner sides of
the mergeclauses contain multiple references to the same EquivalenceClass
({t2.x} in the above example) then a simplistic rendering of the required
inner sort order is like "ORDER BY t2.x, t2.x", but the pathkey machinery
recognizes that the second sort column is redundant and throws it away.
The mergejoin planning code failed to account for that behavior properly.
One error was to try to generate cut-down versions of the mergeclause list
from cut-down versions of the inner pathkeys in the same way as the initial
construction of the mergeclause list from the outer pathkeys was done; this
could lead to choosing a mergeclause list that fails to match the outer
pathkeys. The other problem was that the pathkey cross-checking code in
create_mergejoin_plan treated the inner and outer pathkey lists
identically, whereas actually the expectations for them must be different.
That led to false "pathkeys do not match" failures in some cases, and in
principle could have led to failure to detect bogus plans in other cases,
though there is no indication that such bogus plans could be generated.
Reported by Alexander Kuzmenkov, who also reviewed this patch. This has
been broken for years (back to around 8.3 according to my testing), so
back-patch to all supported branches.
Discussion: https://postgr.es/m/5dad9160-4632-0e47-e120-8e2082000c01@postgrespro.ru
8 years ago
|
|
|
* The list is ordered to match the pathkeys, as required for execution.
|
|
|
|
*/
|
|
|
|
List *
|
Fix planner failures with overlapping mergejoin clauses in an outer join.
Given overlapping or partially redundant join clauses, for example
t1 JOIN t2 ON t1.a = t2.x AND t1.b = t2.x
the planner's EquivalenceClass machinery will ordinarily refactor the
clauses as "t1.a = t1.b AND t1.a = t2.x", so that join processing doesn't
see multiple references to the same EquivalenceClass in a list of join
equality clauses. However, if the join is outer, it's incorrect to derive
a restriction clause on the outer side from the join conditions, so the
clause refactoring does not happen and we end up with overlapping join
conditions. The code that attempted to deal with such cases had several
subtle bugs, which could result in "left and right pathkeys do not match in
mergejoin" or "outer pathkeys do not match mergeclauses" planner errors,
if the selected join plan type was a mergejoin. (It does not appear that
any actually incorrect plan could have been emitted.)
The core of the problem really was failure to recognize that the outer and
inner relations' pathkeys have different relationships to the mergeclause
list. A join's mergeclause list is constructed by reference to the outer
pathkeys, so it will always be ordered the same as the outer pathkeys, but
this cannot be presumed true for the inner pathkeys. If the inner sides of
the mergeclauses contain multiple references to the same EquivalenceClass
({t2.x} in the above example) then a simplistic rendering of the required
inner sort order is like "ORDER BY t2.x, t2.x", but the pathkey machinery
recognizes that the second sort column is redundant and throws it away.
The mergejoin planning code failed to account for that behavior properly.
One error was to try to generate cut-down versions of the mergeclause list
from cut-down versions of the inner pathkeys in the same way as the initial
construction of the mergeclause list from the outer pathkeys was done; this
could lead to choosing a mergeclause list that fails to match the outer
pathkeys. The other problem was that the pathkey cross-checking code in
create_mergejoin_plan treated the inner and outer pathkey lists
identically, whereas actually the expectations for them must be different.
That led to false "pathkeys do not match" failures in some cases, and in
principle could have led to failure to detect bogus plans in other cases,
though there is no indication that such bogus plans could be generated.
Reported by Alexander Kuzmenkov, who also reviewed this patch. This has
been broken for years (back to around 8.3 according to my testing), so
back-patch to all supported branches.
Discussion: https://postgr.es/m/5dad9160-4632-0e47-e120-8e2082000c01@postgrespro.ru
8 years ago
|
|
|
find_mergeclauses_for_outer_pathkeys(PlannerInfo *root,
|
|
|
|
List *pathkeys,
|
|
|
|
List *restrictinfos)
|
|
|
|
{
|
|
|
|
List *mergeclauses = NIL;
|
|
|
|
ListCell *i;
|
|
|
|
|
|
|
|
/* make sure we have eclasses cached in the clauses */
|
|
|
|
foreach(i, restrictinfos)
|
|
|
|
{
|
|
|
|
RestrictInfo *rinfo = (RestrictInfo *) lfirst(i);
|
|
|
|
|
|
|
|
update_mergeclause_eclasses(root, rinfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach(i, pathkeys)
|
|
|
|
{
|
|
|
|
PathKey *pathkey = (PathKey *) lfirst(i);
|
|
|
|
EquivalenceClass *pathkey_ec = pathkey->pk_eclass;
|
|
|
|
List *matched_restrictinfos = NIL;
|
|
|
|
ListCell *j;
|
|
|
|
|
|
|
|
/*----------
|
|
|
|
* A mergejoin clause matches a pathkey if it has the same EC.
|
|
|
|
* If there are multiple matching clauses, take them all. In plain
|
|
|
|
* inner-join scenarios we expect only one match, because
|
|
|
|
* equivalence-class processing will have removed any redundant
|
|
|
|
* mergeclauses. However, in outer-join scenarios there might be
|
|
|
|
* multiple matches. An example is
|
|
|
|
*
|
|
|
|
* select * from a full join b
|
|
|
|
* on a.v1 = b.v1 and a.v2 = b.v2 and a.v1 = b.v2;
|
|
|
|
*
|
|
|
|
* Given the pathkeys ({a.v1}, {a.v2}) it is okay to return all three
|
|
|
|
* clauses (in the order a.v1=b.v1, a.v1=b.v2, a.v2=b.v2) and indeed
|
|
|
|
* we *must* do so or we will be unable to form a valid plan.
|
|
|
|
*
|
|
|
|
* We expect that the given pathkeys list is canonical, which means
|
|
|
|
* no two members have the same EC, so it's not possible for this
|
|
|
|
* code to enter the same mergeclause into the result list twice.
|
|
|
|
*
|
|
|
|
* It's possible that multiple matching clauses might have different
|
|
|
|
* ECs on the other side, in which case the order we put them into our
|
Fix planner failures with overlapping mergejoin clauses in an outer join.
Given overlapping or partially redundant join clauses, for example
t1 JOIN t2 ON t1.a = t2.x AND t1.b = t2.x
the planner's EquivalenceClass machinery will ordinarily refactor the
clauses as "t1.a = t1.b AND t1.a = t2.x", so that join processing doesn't
see multiple references to the same EquivalenceClass in a list of join
equality clauses. However, if the join is outer, it's incorrect to derive
a restriction clause on the outer side from the join conditions, so the
clause refactoring does not happen and we end up with overlapping join
conditions. The code that attempted to deal with such cases had several
subtle bugs, which could result in "left and right pathkeys do not match in
mergejoin" or "outer pathkeys do not match mergeclauses" planner errors,
if the selected join plan type was a mergejoin. (It does not appear that
any actually incorrect plan could have been emitted.)
The core of the problem really was failure to recognize that the outer and
inner relations' pathkeys have different relationships to the mergeclause
list. A join's mergeclause list is constructed by reference to the outer
pathkeys, so it will always be ordered the same as the outer pathkeys, but
this cannot be presumed true for the inner pathkeys. If the inner sides of
the mergeclauses contain multiple references to the same EquivalenceClass
({t2.x} in the above example) then a simplistic rendering of the required
inner sort order is like "ORDER BY t2.x, t2.x", but the pathkey machinery
recognizes that the second sort column is redundant and throws it away.
The mergejoin planning code failed to account for that behavior properly.
One error was to try to generate cut-down versions of the mergeclause list
from cut-down versions of the inner pathkeys in the same way as the initial
construction of the mergeclause list from the outer pathkeys was done; this
could lead to choosing a mergeclause list that fails to match the outer
pathkeys. The other problem was that the pathkey cross-checking code in
create_mergejoin_plan treated the inner and outer pathkey lists
identically, whereas actually the expectations for them must be different.
That led to false "pathkeys do not match" failures in some cases, and in
principle could have led to failure to detect bogus plans in other cases,
though there is no indication that such bogus plans could be generated.
Reported by Alexander Kuzmenkov, who also reviewed this patch. This has
been broken for years (back to around 8.3 according to my testing), so
back-patch to all supported branches.
Discussion: https://postgr.es/m/5dad9160-4632-0e47-e120-8e2082000c01@postgrespro.ru
8 years ago
|
|
|
* result makes a difference in the pathkeys required for the inner
|
|
|
|
* input rel. However this routine hasn't got any info about which
|
|
|
|
* order would be best, so we don't worry about that.
|
|
|
|
*
|
|
|
|
* It's also possible that the selected mergejoin clauses produce
|
Fix planner failures with overlapping mergejoin clauses in an outer join.
Given overlapping or partially redundant join clauses, for example
t1 JOIN t2 ON t1.a = t2.x AND t1.b = t2.x
the planner's EquivalenceClass machinery will ordinarily refactor the
clauses as "t1.a = t1.b AND t1.a = t2.x", so that join processing doesn't
see multiple references to the same EquivalenceClass in a list of join
equality clauses. However, if the join is outer, it's incorrect to derive
a restriction clause on the outer side from the join conditions, so the
clause refactoring does not happen and we end up with overlapping join
conditions. The code that attempted to deal with such cases had several
subtle bugs, which could result in "left and right pathkeys do not match in
mergejoin" or "outer pathkeys do not match mergeclauses" planner errors,
if the selected join plan type was a mergejoin. (It does not appear that
any actually incorrect plan could have been emitted.)
The core of the problem really was failure to recognize that the outer and
inner relations' pathkeys have different relationships to the mergeclause
list. A join's mergeclause list is constructed by reference to the outer
pathkeys, so it will always be ordered the same as the outer pathkeys, but
this cannot be presumed true for the inner pathkeys. If the inner sides of
the mergeclauses contain multiple references to the same EquivalenceClass
({t2.x} in the above example) then a simplistic rendering of the required
inner sort order is like "ORDER BY t2.x, t2.x", but the pathkey machinery
recognizes that the second sort column is redundant and throws it away.
The mergejoin planning code failed to account for that behavior properly.
One error was to try to generate cut-down versions of the mergeclause list
from cut-down versions of the inner pathkeys in the same way as the initial
construction of the mergeclause list from the outer pathkeys was done; this
could lead to choosing a mergeclause list that fails to match the outer
pathkeys. The other problem was that the pathkey cross-checking code in
create_mergejoin_plan treated the inner and outer pathkey lists
identically, whereas actually the expectations for them must be different.
That led to false "pathkeys do not match" failures in some cases, and in
principle could have led to failure to detect bogus plans in other cases,
though there is no indication that such bogus plans could be generated.
Reported by Alexander Kuzmenkov, who also reviewed this patch. This has
been broken for years (back to around 8.3 according to my testing), so
back-patch to all supported branches.
Discussion: https://postgr.es/m/5dad9160-4632-0e47-e120-8e2082000c01@postgrespro.ru
8 years ago
|
|
|
* a noncanonical ordering of pathkeys for the inner side, ie, we
|
|
|
|
* might select clauses that reference b.v1, b.v2, b.v1 in that
|
|
|
|
* order. This is not harmful in itself, though it suggests that
|
Fix planner failures with overlapping mergejoin clauses in an outer join.
Given overlapping or partially redundant join clauses, for example
t1 JOIN t2 ON t1.a = t2.x AND t1.b = t2.x
the planner's EquivalenceClass machinery will ordinarily refactor the
clauses as "t1.a = t1.b AND t1.a = t2.x", so that join processing doesn't
see multiple references to the same EquivalenceClass in a list of join
equality clauses. However, if the join is outer, it's incorrect to derive
a restriction clause on the outer side from the join conditions, so the
clause refactoring does not happen and we end up with overlapping join
conditions. The code that attempted to deal with such cases had several
subtle bugs, which could result in "left and right pathkeys do not match in
mergejoin" or "outer pathkeys do not match mergeclauses" planner errors,
if the selected join plan type was a mergejoin. (It does not appear that
any actually incorrect plan could have been emitted.)
The core of the problem really was failure to recognize that the outer and
inner relations' pathkeys have different relationships to the mergeclause
list. A join's mergeclause list is constructed by reference to the outer
pathkeys, so it will always be ordered the same as the outer pathkeys, but
this cannot be presumed true for the inner pathkeys. If the inner sides of
the mergeclauses contain multiple references to the same EquivalenceClass
({t2.x} in the above example) then a simplistic rendering of the required
inner sort order is like "ORDER BY t2.x, t2.x", but the pathkey machinery
recognizes that the second sort column is redundant and throws it away.
The mergejoin planning code failed to account for that behavior properly.
One error was to try to generate cut-down versions of the mergeclause list
from cut-down versions of the inner pathkeys in the same way as the initial
construction of the mergeclause list from the outer pathkeys was done; this
could lead to choosing a mergeclause list that fails to match the outer
pathkeys. The other problem was that the pathkey cross-checking code in
create_mergejoin_plan treated the inner and outer pathkey lists
identically, whereas actually the expectations for them must be different.
That led to false "pathkeys do not match" failures in some cases, and in
principle could have led to failure to detect bogus plans in other cases,
though there is no indication that such bogus plans could be generated.
Reported by Alexander Kuzmenkov, who also reviewed this patch. This has
been broken for years (back to around 8.3 according to my testing), so
back-patch to all supported branches.
Discussion: https://postgr.es/m/5dad9160-4632-0e47-e120-8e2082000c01@postgrespro.ru
8 years ago
|
|
|
* the clauses are partially redundant. Since the alternative is
|
|
|
|
* to omit mergejoin clauses and thereby possibly fail to generate a
|
|
|
|
* plan altogether, we live with it. make_inner_pathkeys_for_merge()
|
|
|
|
* has to delete duplicates when it constructs the inner pathkeys
|
|
|
|
* list, and we also have to deal with such cases specially in
|
|
|
|
* create_mergejoin_plan().
|
|
|
|
*----------
|
|
|
|
*/
|
|
|
|
foreach(j, restrictinfos)
|
|
|
|
{
|
|
|
|
RestrictInfo *rinfo = (RestrictInfo *) lfirst(j);
|
|
|
|
EquivalenceClass *clause_ec;
|
|
|
|
|
Fix planner failures with overlapping mergejoin clauses in an outer join.
Given overlapping or partially redundant join clauses, for example
t1 JOIN t2 ON t1.a = t2.x AND t1.b = t2.x
the planner's EquivalenceClass machinery will ordinarily refactor the
clauses as "t1.a = t1.b AND t1.a = t2.x", so that join processing doesn't
see multiple references to the same EquivalenceClass in a list of join
equality clauses. However, if the join is outer, it's incorrect to derive
a restriction clause on the outer side from the join conditions, so the
clause refactoring does not happen and we end up with overlapping join
conditions. The code that attempted to deal with such cases had several
subtle bugs, which could result in "left and right pathkeys do not match in
mergejoin" or "outer pathkeys do not match mergeclauses" planner errors,
if the selected join plan type was a mergejoin. (It does not appear that
any actually incorrect plan could have been emitted.)
The core of the problem really was failure to recognize that the outer and
inner relations' pathkeys have different relationships to the mergeclause
list. A join's mergeclause list is constructed by reference to the outer
pathkeys, so it will always be ordered the same as the outer pathkeys, but
this cannot be presumed true for the inner pathkeys. If the inner sides of
the mergeclauses contain multiple references to the same EquivalenceClass
({t2.x} in the above example) then a simplistic rendering of the required
inner sort order is like "ORDER BY t2.x, t2.x", but the pathkey machinery
recognizes that the second sort column is redundant and throws it away.
The mergejoin planning code failed to account for that behavior properly.
One error was to try to generate cut-down versions of the mergeclause list
from cut-down versions of the inner pathkeys in the same way as the initial
construction of the mergeclause list from the outer pathkeys was done; this
could lead to choosing a mergeclause list that fails to match the outer
pathkeys. The other problem was that the pathkey cross-checking code in
create_mergejoin_plan treated the inner and outer pathkey lists
identically, whereas actually the expectations for them must be different.
That led to false "pathkeys do not match" failures in some cases, and in
principle could have led to failure to detect bogus plans in other cases,
though there is no indication that such bogus plans could be generated.
Reported by Alexander Kuzmenkov, who also reviewed this patch. This has
been broken for years (back to around 8.3 according to my testing), so
back-patch to all supported branches.
Discussion: https://postgr.es/m/5dad9160-4632-0e47-e120-8e2082000c01@postgrespro.ru
8 years ago
|
|
|
clause_ec = rinfo->outer_is_left ?
|
|
|
|
rinfo->left_ec : rinfo->right_ec;
|
|
|
|
if (clause_ec == pathkey_ec)
|
|
|
|
matched_restrictinfos = lappend(matched_restrictinfos, rinfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If we didn't find a mergeclause, we're done --- any additional
|
|
|
|
* sort-key positions in the pathkeys are useless. (But we can still
|
|
|
|
* mergejoin if we found at least one mergeclause.)
|
|
|
|
*/
|
|
|
|
if (matched_restrictinfos == NIL)
|
|
|
|
break;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If we did find usable mergeclause(s) for this sort-key position,
|
|
|
|
* add them to result list.
|
|
|
|
*/
|
|
|
|
mergeclauses = list_concat(mergeclauses, matched_restrictinfos);
|
|
|
|
}
|
|
|
|
|
|
|
|
return mergeclauses;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* select_outer_pathkeys_for_merge
|
|
|
|
* Builds a pathkey list representing a possible sort ordering
|
|
|
|
* that can be used with the given mergeclauses.
|
|
|
|
*
|
|
|
|
* 'mergeclauses' is a list of RestrictInfos for mergejoin clauses
|
|
|
|
* that will be used in a merge join.
|
|
|
|
* 'joinrel' is the join relation we are trying to construct.
|
|
|
|
*
|
|
|
|
* The restrictinfos must be marked (via outer_is_left) to show which side
|
|
|
|
* of each clause is associated with the current outer path. (See
|
|
|
|
* select_mergejoin_clauses())
|
|
|
|
*
|
|
|
|
* Returns a pathkeys list that can be applied to the outer relation.
|
|
|
|
*
|
|
|
|
* Since we assume here that a sort is required, there is no particular use
|
|
|
|
* in matching any available ordering of the outerrel. (joinpath.c has an
|
|
|
|
* entirely separate code path for considering sort-free mergejoins.) Rather,
|
|
|
|
* it's interesting to try to match the requested query_pathkeys so that a
|
|
|
|
* second output sort may be avoided; and failing that, we try to list "more
|
|
|
|
* popular" keys (those with the most unmatched EquivalenceClass peers)
|
|
|
|
* earlier, in hopes of making the resulting ordering useful for as many
|
|
|
|
* higher-level mergejoins as possible.
|
|
|
|
*/
|
|
|
|
List *
|
|
|
|
select_outer_pathkeys_for_merge(PlannerInfo *root,
|
|
|
|
List *mergeclauses,
|
|
|
|
RelOptInfo *joinrel)
|
|
|
|
{
|
|
|
|
List *pathkeys = NIL;
|
|
|
|
int nClauses = list_length(mergeclauses);
|
|
|
|
EquivalenceClass **ecs;
|
|
|
|
int *scores;
|
|
|
|
int necs;
|
|
|
|
ListCell *lc;
|
|
|
|
int j;
|
|
|
|
|
|
|
|
/* Might have no mergeclauses */
|
|
|
|
if (nClauses == 0)
|
|
|
|
return NIL;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Make arrays of the ECs used by the mergeclauses (dropping any
|
|
|
|
* duplicates) and their "popularity" scores.
|
|
|
|
*/
|
|
|
|
ecs = (EquivalenceClass **) palloc(nClauses * sizeof(EquivalenceClass *));
|
|
|
|
scores = (int *) palloc(nClauses * sizeof(int));
|
|
|
|
necs = 0;
|
|
|
|
|
|
|
|
foreach(lc, mergeclauses)
|
|
|
|
{
|
|
|
|
RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
|
|
|
|
EquivalenceClass *oeclass;
|
|
|
|
int score;
|
|
|
|
ListCell *lc2;
|
|
|
|
|
|
|
|
/* get the outer eclass */
|
|
|
|
update_mergeclause_eclasses(root, rinfo);
|
|
|
|
|
|
|
|
if (rinfo->outer_is_left)
|
|
|
|
oeclass = rinfo->left_ec;
|
|
|
|
else
|
|
|
|
oeclass = rinfo->right_ec;
|
|
|
|
|
|
|
|
/* reject duplicates */
|
|
|
|
for (j = 0; j < necs; j++)
|
|
|
|
{
|
|
|
|
if (ecs[j] == oeclass)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (j < necs)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* compute score */
|
|
|
|
score = 0;
|
|
|
|
foreach(lc2, oeclass->ec_members)
|
|
|
|
{
|
|
|
|
EquivalenceMember *em = (EquivalenceMember *) lfirst(lc2);
|
|
|
|
|
|
|
|
/* Potential future join partner? */
|
|
|
|
if (!em->em_is_const && !em->em_is_child &&
|
|
|
|
!bms_overlap(em->em_relids, joinrel->relids))
|
|
|
|
score++;
|
|
|
|
}
|
|
|
|
|
|
|
|
ecs[necs] = oeclass;
|
|
|
|
scores[necs] = score;
|
|
|
|
necs++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Find out if we have all the ECs mentioned in query_pathkeys; if so we
|
|
|
|
* can generate a sort order that's also useful for final output. There is
|
|
|
|
* no percentage in a partial match, though, so we have to have 'em all.
|
|
|
|
*/
|
|
|
|
if (root->query_pathkeys)
|
|
|
|
{
|
|
|
|
foreach(lc, root->query_pathkeys)
|
|
|
|
{
|
|
|
|
PathKey *query_pathkey = (PathKey *) lfirst(lc);
|
|
|
|
EquivalenceClass *query_ec = query_pathkey->pk_eclass;
|
|
|
|
|
|
|
|
for (j = 0; j < necs; j++)
|
|
|
|
{
|
|
|
|
if (ecs[j] == query_ec)
|
|
|
|
break; /* found match */
|
|
|
|
}
|
|
|
|
if (j >= necs)
|
|
|
|
break; /* didn't find match */
|
|
|
|
}
|
|
|
|
/* if we got to the end of the list, we have them all */
|
|
|
|
if (lc == NULL)
|
|
|
|
{
|
|
|
|
/* copy query_pathkeys as starting point for our output */
|
|
|
|
pathkeys = list_copy(root->query_pathkeys);
|
|
|
|
/* mark their ECs as already-emitted */
|
|
|
|
foreach(lc, root->query_pathkeys)
|
|
|
|
{
|
|
|
|
PathKey *query_pathkey = (PathKey *) lfirst(lc);
|
|
|
|
EquivalenceClass *query_ec = query_pathkey->pk_eclass;
|
|
|
|
|
|
|
|
for (j = 0; j < necs; j++)
|
|
|
|
{
|
|
|
|
if (ecs[j] == query_ec)
|
|
|
|
{
|
|
|
|
scores[j] = -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Add remaining ECs to the list in popularity order, using a default sort
|
|
|
|
* ordering. (We could use qsort() here, but the list length is usually
|
|
|
|
* so small it's not worth it.)
|
|
|
|
*/
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
int best_j;
|
|
|
|
int best_score;
|
|
|
|
EquivalenceClass *ec;
|
|
|
|
PathKey *pathkey;
|
|
|
|
|
|
|
|
best_j = 0;
|
|
|
|
best_score = scores[0];
|
|
|
|
for (j = 1; j < necs; j++)
|
|
|
|
{
|
|
|
|
if (scores[j] > best_score)
|
|
|
|
{
|
|
|
|
best_j = j;
|
|
|
|
best_score = scores[j];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (best_score < 0)
|
|
|
|
break; /* all done */
|
|
|
|
ec = ecs[best_j];
|
|
|
|
scores[best_j] = -1;
|
|
|
|
pathkey = make_canonical_pathkey(root,
|
|
|
|
ec,
|
|
|
|
linitial_oid(ec->ec_opfamilies),
|
|
|
|
BTLessStrategyNumber,
|
|
|
|
false);
|
|
|
|
/* can't be redundant because no duplicate ECs */
|
|
|
|
Assert(!pathkey_is_redundant(pathkey, pathkeys));
|
|
|
|
pathkeys = lappend(pathkeys, pathkey);
|
|
|
|
}
|
|
|
|
|
|
|
|
pfree(ecs);
|
|
|
|
pfree(scores);
|
|
|
|
|
|
|
|
return pathkeys;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* make_inner_pathkeys_for_merge
|
|
|
|
* Builds a pathkey list representing the explicit sort order that
|
|
|
|
* must be applied to an inner path to make it usable with the
|
|
|
|
* given mergeclauses.
|
|
|
|
*
|
Fix planner failures with overlapping mergejoin clauses in an outer join.
Given overlapping or partially redundant join clauses, for example
t1 JOIN t2 ON t1.a = t2.x AND t1.b = t2.x
the planner's EquivalenceClass machinery will ordinarily refactor the
clauses as "t1.a = t1.b AND t1.a = t2.x", so that join processing doesn't
see multiple references to the same EquivalenceClass in a list of join
equality clauses. However, if the join is outer, it's incorrect to derive
a restriction clause on the outer side from the join conditions, so the
clause refactoring does not happen and we end up with overlapping join
conditions. The code that attempted to deal with such cases had several
subtle bugs, which could result in "left and right pathkeys do not match in
mergejoin" or "outer pathkeys do not match mergeclauses" planner errors,
if the selected join plan type was a mergejoin. (It does not appear that
any actually incorrect plan could have been emitted.)
The core of the problem really was failure to recognize that the outer and
inner relations' pathkeys have different relationships to the mergeclause
list. A join's mergeclause list is constructed by reference to the outer
pathkeys, so it will always be ordered the same as the outer pathkeys, but
this cannot be presumed true for the inner pathkeys. If the inner sides of
the mergeclauses contain multiple references to the same EquivalenceClass
({t2.x} in the above example) then a simplistic rendering of the required
inner sort order is like "ORDER BY t2.x, t2.x", but the pathkey machinery
recognizes that the second sort column is redundant and throws it away.
The mergejoin planning code failed to account for that behavior properly.
One error was to try to generate cut-down versions of the mergeclause list
from cut-down versions of the inner pathkeys in the same way as the initial
construction of the mergeclause list from the outer pathkeys was done; this
could lead to choosing a mergeclause list that fails to match the outer
pathkeys. The other problem was that the pathkey cross-checking code in
create_mergejoin_plan treated the inner and outer pathkey lists
identically, whereas actually the expectations for them must be different.
That led to false "pathkeys do not match" failures in some cases, and in
principle could have led to failure to detect bogus plans in other cases,
though there is no indication that such bogus plans could be generated.
Reported by Alexander Kuzmenkov, who also reviewed this patch. This has
been broken for years (back to around 8.3 according to my testing), so
back-patch to all supported branches.
Discussion: https://postgr.es/m/5dad9160-4632-0e47-e120-8e2082000c01@postgrespro.ru
8 years ago
|
|
|
* 'mergeclauses' is a list of RestrictInfos for the mergejoin clauses
|
|
|
|
* that will be used in a merge join, in order.
|
|
|
|
* 'outer_pathkeys' are the already-known canonical pathkeys for the outer
|
|
|
|
* side of the join.
|
|
|
|
*
|
|
|
|
* The restrictinfos must be marked (via outer_is_left) to show which side
|
|
|
|
* of each clause is associated with the current outer path. (See
|
|
|
|
* select_mergejoin_clauses())
|
|
|
|
*
|
|
|
|
* Returns a pathkeys list that can be applied to the inner relation.
|
|
|
|
*
|
|
|
|
* Note that it is not this routine's job to decide whether sorting is
|
|
|
|
* actually needed for a particular input path. Assume a sort is necessary;
|
|
|
|
* just make the keys, eh?
|
|
|
|
*/
|
|
|
|
List *
|
|
|
|
make_inner_pathkeys_for_merge(PlannerInfo *root,
|
|
|
|
List *mergeclauses,
|
|
|
|
List *outer_pathkeys)
|
|
|
|
{
|
|
|
|
List *pathkeys = NIL;
|
|
|
|
EquivalenceClass *lastoeclass;
|
|
|
|
PathKey *opathkey;
|
|
|
|
ListCell *lc;
|
|
|
|
ListCell *lop;
|
|
|
|
|
|
|
|
lastoeclass = NULL;
|
|
|
|
opathkey = NULL;
|
|
|
|
lop = list_head(outer_pathkeys);
|
|
|
|
|
|
|
|
foreach(lc, mergeclauses)
|
|
|
|
{
|
|
|
|
RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
|
|
|
|
EquivalenceClass *oeclass;
|
|
|
|
EquivalenceClass *ieclass;
|
|
|
|
PathKey *pathkey;
|
|
|
|
|
|
|
|
update_mergeclause_eclasses(root, rinfo);
|
|
|
|
|
|
|
|
if (rinfo->outer_is_left)
|
|
|
|
{
|
|
|
|
oeclass = rinfo->left_ec;
|
|
|
|
ieclass = rinfo->right_ec;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
oeclass = rinfo->right_ec;
|
|
|
|
ieclass = rinfo->left_ec;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* outer eclass should match current or next pathkeys */
|
|
|
|
/* we check this carefully for debugging reasons */
|
|
|
|
if (oeclass != lastoeclass)
|
|
|
|
{
|
|
|
|
if (!lop)
|
|
|
|
elog(ERROR, "too few pathkeys for mergeclauses");
|
|
|
|
opathkey = (PathKey *) lfirst(lop);
|
Represent Lists as expansible arrays, not chains of cons-cells.
Originally, Postgres Lists were a more or less exact reimplementation of
Lisp lists, which consist of chains of separately-allocated cons cells,
each having a value and a next-cell link. We'd hacked that once before
(commit d0b4399d8) to add a separate List header, but the data was still
in cons cells. That makes some operations -- notably list_nth() -- O(N),
and it's bulky because of the next-cell pointers and per-cell palloc
overhead, and it's very cache-unfriendly if the cons cells end up
scattered around rather than being adjacent.
In this rewrite, we still have List headers, but the data is in a
resizable array of values, with no next-cell links. Now we need at
most two palloc's per List, and often only one, since we can allocate
some values in the same palloc call as the List header. (Of course,
extending an existing List may require repalloc's to enlarge the array.
But this involves just O(log N) allocations not O(N).)
Of course this is not without downsides. The key difficulty is that
addition or deletion of a list entry may now cause other entries to
move, which it did not before.
For example, that breaks foreach() and sister macros, which historically
used a pointer to the current cons-cell as loop state. We can repair
those macros transparently by making their actual loop state be an
integer list index; the exposed "ListCell *" pointer is no longer state
carried across loop iterations, but is just a derived value. (In
practice, modern compilers can optimize things back to having just one
loop state value, at least for simple cases with inline loop bodies.)
In principle, this is a semantics change for cases where the loop body
inserts or deletes list entries ahead of the current loop index; but
I found no such cases in the Postgres code.
The change is not at all transparent for code that doesn't use foreach()
but chases lists "by hand" using lnext(). The largest share of such
code in the backend is in loops that were maintaining "prev" and "next"
variables in addition to the current-cell pointer, in order to delete
list cells efficiently using list_delete_cell(). However, we no longer
need a previous-cell pointer to delete a list cell efficiently. Keeping
a next-cell pointer doesn't work, as explained above, but we can improve
matters by changing such code to use a regular foreach() loop and then
using the new macro foreach_delete_current() to delete the current cell.
(This macro knows how to update the associated foreach loop's state so
that no cells will be missed in the traversal.)
There remains a nontrivial risk of code assuming that a ListCell *
pointer will remain good over an operation that could now move the list
contents. To help catch such errors, list.c can be compiled with a new
define symbol DEBUG_LIST_MEMORY_USAGE that forcibly moves list contents
whenever that could possibly happen. This makes list operations
significantly more expensive so it's not normally turned on (though it
is on by default if USE_VALGRIND is on).
There are two notable API differences from the previous code:
* lnext() now requires the List's header pointer in addition to the
current cell's address.
* list_delete_cell() no longer requires a previous-cell argument.
These changes are somewhat unfortunate, but on the other hand code using
either function needs inspection to see if it is assuming anything
it shouldn't, so it's not all bad.
Programmers should be aware of these significant performance changes:
* list_nth() and related functions are now O(1); so there's no
major access-speed difference between a list and an array.
* Inserting or deleting a list element now takes time proportional to
the distance to the end of the list, due to moving the array elements.
(However, it typically *doesn't* require palloc or pfree, so except in
long lists it's probably still faster than before.) Notably, lcons()
used to be about the same cost as lappend(), but that's no longer true
if the list is long. Code that uses lcons() and list_delete_first()
to maintain a stack might usefully be rewritten to push and pop at the
end of the list rather than the beginning.
* There are now list_insert_nth...() and list_delete_nth...() functions
that add or remove a list cell identified by index. These have the
data-movement penalty explained above, but there's no search penalty.
* list_concat() and variants now copy the second list's data into
storage belonging to the first list, so there is no longer any
sharing of cells between the input lists. The second argument is
now declared "const List *" to reflect that it isn't changed.
This patch just does the minimum needed to get the new implementation
in place and fix bugs exposed by the regression tests. As suggested
by the foregoing, there's a fair amount of followup work remaining to
do.
Also, the ENABLE_LIST_COMPAT macros are finally removed in this
commit. Code using those should have been gone a dozen years ago.
Patch by me; thanks to David Rowley, Jesper Pedersen, and others
for review.
Discussion: https://postgr.es/m/11587.1550975080@sss.pgh.pa.us
6 years ago
|
|
|
lop = lnext(outer_pathkeys, lop);
|
|
|
|
lastoeclass = opathkey->pk_eclass;
|
|
|
|
if (oeclass != lastoeclass)
|
|
|
|
elog(ERROR, "outer pathkeys do not match mergeclause");
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Often, we'll have same EC on both sides, in which case the outer
|
|
|
|
* pathkey is also canonical for the inner side, and we can skip a
|
|
|
|
* useless search.
|
|
|
|
*/
|
|
|
|
if (ieclass == oeclass)
|
|
|
|
pathkey = opathkey;
|
|
|
|
else
|
|
|
|
pathkey = make_canonical_pathkey(root,
|
|
|
|
ieclass,
|
|
|
|
opathkey->pk_opfamily,
|
|
|
|
opathkey->pk_strategy,
|
|
|
|
opathkey->pk_nulls_first);
|
|
|
|
|
|
|
|
/*
|
Fix planner failures with overlapping mergejoin clauses in an outer join.
Given overlapping or partially redundant join clauses, for example
t1 JOIN t2 ON t1.a = t2.x AND t1.b = t2.x
the planner's EquivalenceClass machinery will ordinarily refactor the
clauses as "t1.a = t1.b AND t1.a = t2.x", so that join processing doesn't
see multiple references to the same EquivalenceClass in a list of join
equality clauses. However, if the join is outer, it's incorrect to derive
a restriction clause on the outer side from the join conditions, so the
clause refactoring does not happen and we end up with overlapping join
conditions. The code that attempted to deal with such cases had several
subtle bugs, which could result in "left and right pathkeys do not match in
mergejoin" or "outer pathkeys do not match mergeclauses" planner errors,
if the selected join plan type was a mergejoin. (It does not appear that
any actually incorrect plan could have been emitted.)
The core of the problem really was failure to recognize that the outer and
inner relations' pathkeys have different relationships to the mergeclause
list. A join's mergeclause list is constructed by reference to the outer
pathkeys, so it will always be ordered the same as the outer pathkeys, but
this cannot be presumed true for the inner pathkeys. If the inner sides of
the mergeclauses contain multiple references to the same EquivalenceClass
({t2.x} in the above example) then a simplistic rendering of the required
inner sort order is like "ORDER BY t2.x, t2.x", but the pathkey machinery
recognizes that the second sort column is redundant and throws it away.
The mergejoin planning code failed to account for that behavior properly.
One error was to try to generate cut-down versions of the mergeclause list
from cut-down versions of the inner pathkeys in the same way as the initial
construction of the mergeclause list from the outer pathkeys was done; this
could lead to choosing a mergeclause list that fails to match the outer
pathkeys. The other problem was that the pathkey cross-checking code in
create_mergejoin_plan treated the inner and outer pathkey lists
identically, whereas actually the expectations for them must be different.
That led to false "pathkeys do not match" failures in some cases, and in
principle could have led to failure to detect bogus plans in other cases,
though there is no indication that such bogus plans could be generated.
Reported by Alexander Kuzmenkov, who also reviewed this patch. This has
been broken for years (back to around 8.3 according to my testing), so
back-patch to all supported branches.
Discussion: https://postgr.es/m/5dad9160-4632-0e47-e120-8e2082000c01@postgrespro.ru
8 years ago
|
|
|
* Don't generate redundant pathkeys (which can happen if multiple
|
|
|
|
* mergeclauses refer to the same EC). Because we do this, the output
|
|
|
|
* pathkey list isn't necessarily ordered like the mergeclauses, which
|
|
|
|
* complicates life for create_mergejoin_plan(). But if we didn't,
|
|
|
|
* we'd have a noncanonical sort key list, which would be bad; for one
|
|
|
|
* reason, it certainly wouldn't match any available sort order for
|
|
|
|
* the input relation.
|
|
|
|
*/
|
|
|
|
if (!pathkey_is_redundant(pathkey, pathkeys))
|
|
|
|
pathkeys = lappend(pathkeys, pathkey);
|
|
|
|
}
|
|
|
|
|
|
|
|
return pathkeys;
|
|
|
|
}
|
|
|
|
|
Fix planner failures with overlapping mergejoin clauses in an outer join.
Given overlapping or partially redundant join clauses, for example
t1 JOIN t2 ON t1.a = t2.x AND t1.b = t2.x
the planner's EquivalenceClass machinery will ordinarily refactor the
clauses as "t1.a = t1.b AND t1.a = t2.x", so that join processing doesn't
see multiple references to the same EquivalenceClass in a list of join
equality clauses. However, if the join is outer, it's incorrect to derive
a restriction clause on the outer side from the join conditions, so the
clause refactoring does not happen and we end up with overlapping join
conditions. The code that attempted to deal with such cases had several
subtle bugs, which could result in "left and right pathkeys do not match in
mergejoin" or "outer pathkeys do not match mergeclauses" planner errors,
if the selected join plan type was a mergejoin. (It does not appear that
any actually incorrect plan could have been emitted.)
The core of the problem really was failure to recognize that the outer and
inner relations' pathkeys have different relationships to the mergeclause
list. A join's mergeclause list is constructed by reference to the outer
pathkeys, so it will always be ordered the same as the outer pathkeys, but
this cannot be presumed true for the inner pathkeys. If the inner sides of
the mergeclauses contain multiple references to the same EquivalenceClass
({t2.x} in the above example) then a simplistic rendering of the required
inner sort order is like "ORDER BY t2.x, t2.x", but the pathkey machinery
recognizes that the second sort column is redundant and throws it away.
The mergejoin planning code failed to account for that behavior properly.
One error was to try to generate cut-down versions of the mergeclause list
from cut-down versions of the inner pathkeys in the same way as the initial
construction of the mergeclause list from the outer pathkeys was done; this
could lead to choosing a mergeclause list that fails to match the outer
pathkeys. The other problem was that the pathkey cross-checking code in
create_mergejoin_plan treated the inner and outer pathkey lists
identically, whereas actually the expectations for them must be different.
That led to false "pathkeys do not match" failures in some cases, and in
principle could have led to failure to detect bogus plans in other cases,
though there is no indication that such bogus plans could be generated.
Reported by Alexander Kuzmenkov, who also reviewed this patch. This has
been broken for years (back to around 8.3 according to my testing), so
back-patch to all supported branches.
Discussion: https://postgr.es/m/5dad9160-4632-0e47-e120-8e2082000c01@postgrespro.ru
8 years ago
|
|
|
/*
|
|
|
|
* trim_mergeclauses_for_inner_pathkeys
|
|
|
|
* This routine trims a list of mergeclauses to include just those that
|
|
|
|
* work with a specified ordering for the join's inner relation.
|
|
|
|
*
|
|
|
|
* 'mergeclauses' is a list of RestrictInfos for mergejoin clauses for the
|
|
|
|
* join relation being formed, in an order known to work for the
|
|
|
|
* currently-considered sort ordering of the join's outer rel.
|
|
|
|
* 'pathkeys' is a pathkeys list showing the ordering of an inner-rel path;
|
|
|
|
* it should be equal to, or a truncation of, the result of
|
|
|
|
* make_inner_pathkeys_for_merge for these mergeclauses.
|
|
|
|
*
|
|
|
|
* What we return will be a prefix of the given mergeclauses list.
|
|
|
|
*
|
|
|
|
* We need this logic because make_inner_pathkeys_for_merge's result isn't
|
|
|
|
* necessarily in the same order as the mergeclauses. That means that if we
|
|
|
|
* consider an inner-rel pathkey list that is a truncation of that result,
|
|
|
|
* we might need to drop mergeclauses even though they match a surviving inner
|
|
|
|
* pathkey. This happens when they are to the right of a mergeclause that
|
|
|
|
* matches a removed inner pathkey.
|
|
|
|
*
|
|
|
|
* The mergeclauses must be marked (via outer_is_left) to show which side
|
|
|
|
* of each clause is associated with the current outer path. (See
|
|
|
|
* select_mergejoin_clauses())
|
|
|
|
*/
|
|
|
|
List *
|
|
|
|
trim_mergeclauses_for_inner_pathkeys(PlannerInfo *root,
|
|
|
|
List *mergeclauses,
|
|
|
|
List *pathkeys)
|
|
|
|
{
|
|
|
|
List *new_mergeclauses = NIL;
|
|
|
|
PathKey *pathkey;
|
|
|
|
EquivalenceClass *pathkey_ec;
|
|
|
|
bool matched_pathkey;
|
|
|
|
ListCell *lip;
|
|
|
|
ListCell *i;
|
|
|
|
|
|
|
|
/* No pathkeys => no mergeclauses (though we don't expect this case) */
|
|
|
|
if (pathkeys == NIL)
|
|
|
|
return NIL;
|
|
|
|
/* Initialize to consider first pathkey */
|
|
|
|
lip = list_head(pathkeys);
|
|
|
|
pathkey = (PathKey *) lfirst(lip);
|
|
|
|
pathkey_ec = pathkey->pk_eclass;
|
Represent Lists as expansible arrays, not chains of cons-cells.
Originally, Postgres Lists were a more or less exact reimplementation of
Lisp lists, which consist of chains of separately-allocated cons cells,
each having a value and a next-cell link. We'd hacked that once before
(commit d0b4399d8) to add a separate List header, but the data was still
in cons cells. That makes some operations -- notably list_nth() -- O(N),
and it's bulky because of the next-cell pointers and per-cell palloc
overhead, and it's very cache-unfriendly if the cons cells end up
scattered around rather than being adjacent.
In this rewrite, we still have List headers, but the data is in a
resizable array of values, with no next-cell links. Now we need at
most two palloc's per List, and often only one, since we can allocate
some values in the same palloc call as the List header. (Of course,
extending an existing List may require repalloc's to enlarge the array.
But this involves just O(log N) allocations not O(N).)
Of course this is not without downsides. The key difficulty is that
addition or deletion of a list entry may now cause other entries to
move, which it did not before.
For example, that breaks foreach() and sister macros, which historically
used a pointer to the current cons-cell as loop state. We can repair
those macros transparently by making their actual loop state be an
integer list index; the exposed "ListCell *" pointer is no longer state
carried across loop iterations, but is just a derived value. (In
practice, modern compilers can optimize things back to having just one
loop state value, at least for simple cases with inline loop bodies.)
In principle, this is a semantics change for cases where the loop body
inserts or deletes list entries ahead of the current loop index; but
I found no such cases in the Postgres code.
The change is not at all transparent for code that doesn't use foreach()
but chases lists "by hand" using lnext(). The largest share of such
code in the backend is in loops that were maintaining "prev" and "next"
variables in addition to the current-cell pointer, in order to delete
list cells efficiently using list_delete_cell(). However, we no longer
need a previous-cell pointer to delete a list cell efficiently. Keeping
a next-cell pointer doesn't work, as explained above, but we can improve
matters by changing such code to use a regular foreach() loop and then
using the new macro foreach_delete_current() to delete the current cell.
(This macro knows how to update the associated foreach loop's state so
that no cells will be missed in the traversal.)
There remains a nontrivial risk of code assuming that a ListCell *
pointer will remain good over an operation that could now move the list
contents. To help catch such errors, list.c can be compiled with a new
define symbol DEBUG_LIST_MEMORY_USAGE that forcibly moves list contents
whenever that could possibly happen. This makes list operations
significantly more expensive so it's not normally turned on (though it
is on by default if USE_VALGRIND is on).
There are two notable API differences from the previous code:
* lnext() now requires the List's header pointer in addition to the
current cell's address.
* list_delete_cell() no longer requires a previous-cell argument.
These changes are somewhat unfortunate, but on the other hand code using
either function needs inspection to see if it is assuming anything
it shouldn't, so it's not all bad.
Programmers should be aware of these significant performance changes:
* list_nth() and related functions are now O(1); so there's no
major access-speed difference between a list and an array.
* Inserting or deleting a list element now takes time proportional to
the distance to the end of the list, due to moving the array elements.
(However, it typically *doesn't* require palloc or pfree, so except in
long lists it's probably still faster than before.) Notably, lcons()
used to be about the same cost as lappend(), but that's no longer true
if the list is long. Code that uses lcons() and list_delete_first()
to maintain a stack might usefully be rewritten to push and pop at the
end of the list rather than the beginning.
* There are now list_insert_nth...() and list_delete_nth...() functions
that add or remove a list cell identified by index. These have the
data-movement penalty explained above, but there's no search penalty.
* list_concat() and variants now copy the second list's data into
storage belonging to the first list, so there is no longer any
sharing of cells between the input lists. The second argument is
now declared "const List *" to reflect that it isn't changed.
This patch just does the minimum needed to get the new implementation
in place and fix bugs exposed by the regression tests. As suggested
by the foregoing, there's a fair amount of followup work remaining to
do.
Also, the ENABLE_LIST_COMPAT macros are finally removed in this
commit. Code using those should have been gone a dozen years ago.
Patch by me; thanks to David Rowley, Jesper Pedersen, and others
for review.
Discussion: https://postgr.es/m/11587.1550975080@sss.pgh.pa.us
6 years ago
|
|
|
lip = lnext(pathkeys, lip);
|
Fix planner failures with overlapping mergejoin clauses in an outer join.
Given overlapping or partially redundant join clauses, for example
t1 JOIN t2 ON t1.a = t2.x AND t1.b = t2.x
the planner's EquivalenceClass machinery will ordinarily refactor the
clauses as "t1.a = t1.b AND t1.a = t2.x", so that join processing doesn't
see multiple references to the same EquivalenceClass in a list of join
equality clauses. However, if the join is outer, it's incorrect to derive
a restriction clause on the outer side from the join conditions, so the
clause refactoring does not happen and we end up with overlapping join
conditions. The code that attempted to deal with such cases had several
subtle bugs, which could result in "left and right pathkeys do not match in
mergejoin" or "outer pathkeys do not match mergeclauses" planner errors,
if the selected join plan type was a mergejoin. (It does not appear that
any actually incorrect plan could have been emitted.)
The core of the problem really was failure to recognize that the outer and
inner relations' pathkeys have different relationships to the mergeclause
list. A join's mergeclause list is constructed by reference to the outer
pathkeys, so it will always be ordered the same as the outer pathkeys, but
this cannot be presumed true for the inner pathkeys. If the inner sides of
the mergeclauses contain multiple references to the same EquivalenceClass
({t2.x} in the above example) then a simplistic rendering of the required
inner sort order is like "ORDER BY t2.x, t2.x", but the pathkey machinery
recognizes that the second sort column is redundant and throws it away.
The mergejoin planning code failed to account for that behavior properly.
One error was to try to generate cut-down versions of the mergeclause list
from cut-down versions of the inner pathkeys in the same way as the initial
construction of the mergeclause list from the outer pathkeys was done; this
could lead to choosing a mergeclause list that fails to match the outer
pathkeys. The other problem was that the pathkey cross-checking code in
create_mergejoin_plan treated the inner and outer pathkey lists
identically, whereas actually the expectations for them must be different.
That led to false "pathkeys do not match" failures in some cases, and in
principle could have led to failure to detect bogus plans in other cases,
though there is no indication that such bogus plans could be generated.
Reported by Alexander Kuzmenkov, who also reviewed this patch. This has
been broken for years (back to around 8.3 according to my testing), so
back-patch to all supported branches.
Discussion: https://postgr.es/m/5dad9160-4632-0e47-e120-8e2082000c01@postgrespro.ru
8 years ago
|
|
|
matched_pathkey = false;
|
|
|
|
|
|
|
|
/* Scan mergeclauses to see how many we can use */
|
|
|
|
foreach(i, mergeclauses)
|
|
|
|
{
|
|
|
|
RestrictInfo *rinfo = (RestrictInfo *) lfirst(i);
|
|
|
|
EquivalenceClass *clause_ec;
|
|
|
|
|
|
|
|
/* Assume we needn't do update_mergeclause_eclasses again here */
|
|
|
|
|
|
|
|
/* Check clause's inner-rel EC against current pathkey */
|
|
|
|
clause_ec = rinfo->outer_is_left ?
|
|
|
|
rinfo->right_ec : rinfo->left_ec;
|
|
|
|
|
|
|
|
/* If we don't have a match, attempt to advance to next pathkey */
|
|
|
|
if (clause_ec != pathkey_ec)
|
|
|
|
{
|
|
|
|
/* If we had no clauses matching this inner pathkey, must stop */
|
|
|
|
if (!matched_pathkey)
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Advance to next inner pathkey, if any */
|
|
|
|
if (lip == NULL)
|
|
|
|
break;
|
|
|
|
pathkey = (PathKey *) lfirst(lip);
|
|
|
|
pathkey_ec = pathkey->pk_eclass;
|
Represent Lists as expansible arrays, not chains of cons-cells.
Originally, Postgres Lists were a more or less exact reimplementation of
Lisp lists, which consist of chains of separately-allocated cons cells,
each having a value and a next-cell link. We'd hacked that once before
(commit d0b4399d8) to add a separate List header, but the data was still
in cons cells. That makes some operations -- notably list_nth() -- O(N),
and it's bulky because of the next-cell pointers and per-cell palloc
overhead, and it's very cache-unfriendly if the cons cells end up
scattered around rather than being adjacent.
In this rewrite, we still have List headers, but the data is in a
resizable array of values, with no next-cell links. Now we need at
most two palloc's per List, and often only one, since we can allocate
some values in the same palloc call as the List header. (Of course,
extending an existing List may require repalloc's to enlarge the array.
But this involves just O(log N) allocations not O(N).)
Of course this is not without downsides. The key difficulty is that
addition or deletion of a list entry may now cause other entries to
move, which it did not before.
For example, that breaks foreach() and sister macros, which historically
used a pointer to the current cons-cell as loop state. We can repair
those macros transparently by making their actual loop state be an
integer list index; the exposed "ListCell *" pointer is no longer state
carried across loop iterations, but is just a derived value. (In
practice, modern compilers can optimize things back to having just one
loop state value, at least for simple cases with inline loop bodies.)
In principle, this is a semantics change for cases where the loop body
inserts or deletes list entries ahead of the current loop index; but
I found no such cases in the Postgres code.
The change is not at all transparent for code that doesn't use foreach()
but chases lists "by hand" using lnext(). The largest share of such
code in the backend is in loops that were maintaining "prev" and "next"
variables in addition to the current-cell pointer, in order to delete
list cells efficiently using list_delete_cell(). However, we no longer
need a previous-cell pointer to delete a list cell efficiently. Keeping
a next-cell pointer doesn't work, as explained above, but we can improve
matters by changing such code to use a regular foreach() loop and then
using the new macro foreach_delete_current() to delete the current cell.
(This macro knows how to update the associated foreach loop's state so
that no cells will be missed in the traversal.)
There remains a nontrivial risk of code assuming that a ListCell *
pointer will remain good over an operation that could now move the list
contents. To help catch such errors, list.c can be compiled with a new
define symbol DEBUG_LIST_MEMORY_USAGE that forcibly moves list contents
whenever that could possibly happen. This makes list operations
significantly more expensive so it's not normally turned on (though it
is on by default if USE_VALGRIND is on).
There are two notable API differences from the previous code:
* lnext() now requires the List's header pointer in addition to the
current cell's address.
* list_delete_cell() no longer requires a previous-cell argument.
These changes are somewhat unfortunate, but on the other hand code using
either function needs inspection to see if it is assuming anything
it shouldn't, so it's not all bad.
Programmers should be aware of these significant performance changes:
* list_nth() and related functions are now O(1); so there's no
major access-speed difference between a list and an array.
* Inserting or deleting a list element now takes time proportional to
the distance to the end of the list, due to moving the array elements.
(However, it typically *doesn't* require palloc or pfree, so except in
long lists it's probably still faster than before.) Notably, lcons()
used to be about the same cost as lappend(), but that's no longer true
if the list is long. Code that uses lcons() and list_delete_first()
to maintain a stack might usefully be rewritten to push and pop at the
end of the list rather than the beginning.
* There are now list_insert_nth...() and list_delete_nth...() functions
that add or remove a list cell identified by index. These have the
data-movement penalty explained above, but there's no search penalty.
* list_concat() and variants now copy the second list's data into
storage belonging to the first list, so there is no longer any
sharing of cells between the input lists. The second argument is
now declared "const List *" to reflect that it isn't changed.
This patch just does the minimum needed to get the new implementation
in place and fix bugs exposed by the regression tests. As suggested
by the foregoing, there's a fair amount of followup work remaining to
do.
Also, the ENABLE_LIST_COMPAT macros are finally removed in this
commit. Code using those should have been gone a dozen years ago.
Patch by me; thanks to David Rowley, Jesper Pedersen, and others
for review.
Discussion: https://postgr.es/m/11587.1550975080@sss.pgh.pa.us
6 years ago
|
|
|
lip = lnext(pathkeys, lip);
|
Fix planner failures with overlapping mergejoin clauses in an outer join.
Given overlapping or partially redundant join clauses, for example
t1 JOIN t2 ON t1.a = t2.x AND t1.b = t2.x
the planner's EquivalenceClass machinery will ordinarily refactor the
clauses as "t1.a = t1.b AND t1.a = t2.x", so that join processing doesn't
see multiple references to the same EquivalenceClass in a list of join
equality clauses. However, if the join is outer, it's incorrect to derive
a restriction clause on the outer side from the join conditions, so the
clause refactoring does not happen and we end up with overlapping join
conditions. The code that attempted to deal with such cases had several
subtle bugs, which could result in "left and right pathkeys do not match in
mergejoin" or "outer pathkeys do not match mergeclauses" planner errors,
if the selected join plan type was a mergejoin. (It does not appear that
any actually incorrect plan could have been emitted.)
The core of the problem really was failure to recognize that the outer and
inner relations' pathkeys have different relationships to the mergeclause
list. A join's mergeclause list is constructed by reference to the outer
pathkeys, so it will always be ordered the same as the outer pathkeys, but
this cannot be presumed true for the inner pathkeys. If the inner sides of
the mergeclauses contain multiple references to the same EquivalenceClass
({t2.x} in the above example) then a simplistic rendering of the required
inner sort order is like "ORDER BY t2.x, t2.x", but the pathkey machinery
recognizes that the second sort column is redundant and throws it away.
The mergejoin planning code failed to account for that behavior properly.
One error was to try to generate cut-down versions of the mergeclause list
from cut-down versions of the inner pathkeys in the same way as the initial
construction of the mergeclause list from the outer pathkeys was done; this
could lead to choosing a mergeclause list that fails to match the outer
pathkeys. The other problem was that the pathkey cross-checking code in
create_mergejoin_plan treated the inner and outer pathkey lists
identically, whereas actually the expectations for them must be different.
That led to false "pathkeys do not match" failures in some cases, and in
principle could have led to failure to detect bogus plans in other cases,
though there is no indication that such bogus plans could be generated.
Reported by Alexander Kuzmenkov, who also reviewed this patch. This has
been broken for years (back to around 8.3 according to my testing), so
back-patch to all supported branches.
Discussion: https://postgr.es/m/5dad9160-4632-0e47-e120-8e2082000c01@postgrespro.ru
8 years ago
|
|
|
matched_pathkey = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If mergeclause matches current inner pathkey, we can use it */
|
|
|
|
if (clause_ec == pathkey_ec)
|
|
|
|
{
|
|
|
|
new_mergeclauses = lappend(new_mergeclauses, rinfo);
|
|
|
|
matched_pathkey = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Else, no hope of adding any more mergeclauses */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return new_mergeclauses;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* PATHKEY USEFULNESS CHECKS
|
|
|
|
*
|
|
|
|
* We only want to remember as many of the pathkeys of a path as have some
|
|
|
|
* potential use, either for subsequent mergejoins or for meeting the query's
|
|
|
|
* requested output ordering. This ensures that add_path() won't consider
|
|
|
|
* a path to have a usefully different ordering unless it really is useful.
|
|
|
|
* These routines check for usefulness of given pathkeys.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* pathkeys_useful_for_merging
|
|
|
|
* Count the number of pathkeys that may be useful for mergejoins
|
|
|
|
* above the given relation.
|
|
|
|
*
|
|
|
|
* We consider a pathkey potentially useful if it corresponds to the merge
|
|
|
|
* ordering of either side of any joinclause for the rel. This might be
|
|
|
|
* overoptimistic, since joinclauses that require different other relations
|
|
|
|
* might never be usable at the same time, but trying to be exact is likely
|
|
|
|
* to be more trouble than it's worth.
|
|
|
|
*
|
|
|
|
* To avoid doubling the number of mergejoin paths considered, we would like
|
|
|
|
* to consider only one of the two scan directions (ASC or DESC) as useful
|
|
|
|
* for merging for any given target column. The choice is arbitrary unless
|
|
|
|
* one of the directions happens to match an ORDER BY key, in which case
|
|
|
|
* that direction should be preferred, in hopes of avoiding a final sort step.
|
|
|
|
* right_merge_direction() implements this heuristic.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
pathkeys_useful_for_merging(PlannerInfo *root, RelOptInfo *rel, List *pathkeys)
|
|
|
|
{
|
|
|
|
int useful = 0;
|
|
|
|
ListCell *i;
|
|
|
|
|
|
|
|
foreach(i, pathkeys)
|
|
|
|
{
|
|
|
|
PathKey *pathkey = (PathKey *) lfirst(i);
|
|
|
|
bool matched = false;
|
|
|
|
ListCell *j;
|
|
|
|
|
|
|
|
/* If "wrong" direction, not useful for merging */
|
|
|
|
if (!right_merge_direction(root, pathkey))
|
|
|
|
break;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* First look into the EquivalenceClass of the pathkey, to see if
|
|
|
|
* there are any members not yet joined to the rel. If so, it's
|
|
|
|
* surely possible to generate a mergejoin clause using them.
|
|
|
|
*/
|
|
|
|
if (rel->has_eclass_joins &&
|
Fix eclass_useful_for_merging to give valid results for appendrel children.
Formerly, this function would always return "true" for an appendrel child
relation, because it would think that the appendrel parent was a potential
join target for the child. In principle that should only lead to some
inefficiency in planning, but fuzz testing by Andreas Seltenreich disclosed
that it could lead to "could not find pathkey item to sort" planner errors
in odd corner cases. Specifically, we would think that all columns of a
child table's multicolumn index were interesting pathkeys, causing us to
generate a MergeAppend path that sorts by all the columns. However, if any
of those columns weren't actually used above the level of the appendrel,
they would not get added to that rel's targetlist, which would result in
being unable to resolve the MergeAppend's sort keys against its targetlist
during createplan.c.
Backpatch to 9.3. In older versions, columns of an appendrel get added
to its targetlist even if they're not mentioned above the scan level,
so that the failure doesn't occur. It might be worth back-patching this
fix to older versions anyway, but I'll refrain for the moment.
10 years ago
|
|
|
eclass_useful_for_merging(root, pathkey->pk_eclass, rel))
|
|
|
|
matched = true;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Otherwise search the rel's joininfo list, which contains
|
|
|
|
* non-EquivalenceClass-derivable join clauses that might
|
|
|
|
* nonetheless be mergejoinable.
|
|
|
|
*/
|
|
|
|
foreach(j, rel->joininfo)
|
|
|
|
{
|
|
|
|
RestrictInfo *restrictinfo = (RestrictInfo *) lfirst(j);
|
|
|
|
|
|
|
|
if (restrictinfo->mergeopfamilies == NIL)
|
|
|
|
continue;
|
|
|
|
update_mergeclause_eclasses(root, restrictinfo);
|
|
|
|
|
|
|
|
if (pathkey->pk_eclass == restrictinfo->left_ec ||
|
|
|
|
pathkey->pk_eclass == restrictinfo->right_ec)
|
|
|
|
{
|
|
|
|
matched = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If we didn't find a mergeclause, we're done --- any additional
|
|
|
|
* sort-key positions in the pathkeys are useless. (But we can still
|
|
|
|
* mergejoin if we found at least one mergeclause.)
|
|
|
|
*/
|
|
|
|
if (matched)
|
|
|
|
useful++;
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return useful;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* right_merge_direction
|
|
|
|
* Check whether the pathkey embodies the preferred sort direction
|
|
|
|
* for merging its target column.
|
|
|
|
*/
|
|
|
|
static bool
|
|
|
|
right_merge_direction(PlannerInfo *root, PathKey *pathkey)
|
|
|
|
{
|
|
|
|
ListCell *l;
|
|
|
|
|
|
|
|
foreach(l, root->query_pathkeys)
|
|
|
|
{
|
|
|
|
PathKey *query_pathkey = (PathKey *) lfirst(l);
|
|
|
|
|
|
|
|
if (pathkey->pk_eclass == query_pathkey->pk_eclass &&
|
|
|
|
pathkey->pk_opfamily == query_pathkey->pk_opfamily)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Found a matching query sort column. Prefer this pathkey's
|
|
|
|
* direction iff it matches. Note that we ignore pk_nulls_first,
|
|
|
|
* which means that a sort might be needed anyway ... but we still
|
|
|
|
* want to prefer only one of the two possible directions, and we
|
|
|
|
* might as well use this one.
|
|
|
|
*/
|
|
|
|
return (pathkey->pk_strategy == query_pathkey->pk_strategy);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If no matching ORDER BY request, prefer the ASC direction */
|
|
|
|
return (pathkey->pk_strategy == BTLessStrategyNumber);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* pathkeys_useful_for_ordering
|
|
|
|
* Count the number of pathkeys that are useful for meeting the
|
|
|
|
* query's requested output ordering.
|
|
|
|
*
|
Implement Incremental Sort
Incremental Sort is an optimized variant of multikey sort for cases when
the input is already sorted by a prefix of the requested sort keys. For
example when the relation is already sorted by (key1, key2) and we need
to sort it by (key1, key2, key3) we can simply split the input rows into
groups having equal values in (key1, key2), and only sort/compare the
remaining column key3.
This has a number of benefits:
- Reduced memory consumption, because only a single group (determined by
values in the sorted prefix) needs to be kept in memory. This may also
eliminate the need to spill to disk.
- Lower startup cost, because Incremental Sort produce results after each
prefix group, which is beneficial for plans where startup cost matters
(like for example queries with LIMIT clause).
We consider both Sort and Incremental Sort, and decide based on costing.
The implemented algorithm operates in two different modes:
- Fetching a minimum number of tuples without check of equality on the
prefix keys, and sorting on all columns when safe.
- Fetching all tuples for a single prefix group and then sorting by
comparing only the remaining (non-prefix) keys.
We always start in the first mode, and employ a heuristic to switch into
the second mode if we believe it's beneficial - the goal is to minimize
the number of unnecessary comparions while keeping memory consumption
below work_mem.
This is a very old patch series. The idea was originally proposed by
Alexander Korotkov back in 2013, and then revived in 2017. In 2018 the
patch was taken over by James Coleman, who wrote and rewrote most of the
current code.
There were many reviewers/contributors since 2013 - I've done my best to
pick the most active ones, and listed them in this commit message.
Author: James Coleman, Alexander Korotkov
Reviewed-by: Tomas Vondra, Andreas Karlsson, Marti Raudsepp, Peter Geoghegan, Robert Haas, Thomas Munro, Antonin Houska, Andres Freund, Alexander Kuzmenkov
Discussion: https://postgr.es/m/CAPpHfdscOX5an71nHd8WSUH6GNOCf=V7wgDaTXdDd9=goN-gfA@mail.gmail.com
Discussion: https://postgr.es/m/CAPpHfds1waRZ=NOmueYq0sx1ZSCnt+5QJvizT8ndT2=etZEeAQ@mail.gmail.com
6 years ago
|
|
|
* Because we the have the possibility of incremental sort, a prefix list of
|
|
|
|
* keys is potentially useful for improving the performance of the requested
|
|
|
|
* ordering. Thus we return 0, if no valuable keys are found, or the number
|
|
|
|
* of leading keys shared by the list and the requested ordering..
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
pathkeys_useful_for_ordering(PlannerInfo *root, List *pathkeys)
|
|
|
|
{
|
Implement Incremental Sort
Incremental Sort is an optimized variant of multikey sort for cases when
the input is already sorted by a prefix of the requested sort keys. For
example when the relation is already sorted by (key1, key2) and we need
to sort it by (key1, key2, key3) we can simply split the input rows into
groups having equal values in (key1, key2), and only sort/compare the
remaining column key3.
This has a number of benefits:
- Reduced memory consumption, because only a single group (determined by
values in the sorted prefix) needs to be kept in memory. This may also
eliminate the need to spill to disk.
- Lower startup cost, because Incremental Sort produce results after each
prefix group, which is beneficial for plans where startup cost matters
(like for example queries with LIMIT clause).
We consider both Sort and Incremental Sort, and decide based on costing.
The implemented algorithm operates in two different modes:
- Fetching a minimum number of tuples without check of equality on the
prefix keys, and sorting on all columns when safe.
- Fetching all tuples for a single prefix group and then sorting by
comparing only the remaining (non-prefix) keys.
We always start in the first mode, and employ a heuristic to switch into
the second mode if we believe it's beneficial - the goal is to minimize
the number of unnecessary comparions while keeping memory consumption
below work_mem.
This is a very old patch series. The idea was originally proposed by
Alexander Korotkov back in 2013, and then revived in 2017. In 2018 the
patch was taken over by James Coleman, who wrote and rewrote most of the
current code.
There were many reviewers/contributors since 2013 - I've done my best to
pick the most active ones, and listed them in this commit message.
Author: James Coleman, Alexander Korotkov
Reviewed-by: Tomas Vondra, Andreas Karlsson, Marti Raudsepp, Peter Geoghegan, Robert Haas, Thomas Munro, Antonin Houska, Andres Freund, Alexander Kuzmenkov
Discussion: https://postgr.es/m/CAPpHfdscOX5an71nHd8WSUH6GNOCf=V7wgDaTXdDd9=goN-gfA@mail.gmail.com
Discussion: https://postgr.es/m/CAPpHfds1waRZ=NOmueYq0sx1ZSCnt+5QJvizT8ndT2=etZEeAQ@mail.gmail.com
6 years ago
|
|
|
int n_common_pathkeys;
|
|
|
|
|
|
|
|
if (root->query_pathkeys == NIL)
|
|
|
|
return 0; /* no special ordering requested */
|
|
|
|
|
|
|
|
if (pathkeys == NIL)
|
|
|
|
return 0; /* unordered path */
|
|
|
|
|
Implement Incremental Sort
Incremental Sort is an optimized variant of multikey sort for cases when
the input is already sorted by a prefix of the requested sort keys. For
example when the relation is already sorted by (key1, key2) and we need
to sort it by (key1, key2, key3) we can simply split the input rows into
groups having equal values in (key1, key2), and only sort/compare the
remaining column key3.
This has a number of benefits:
- Reduced memory consumption, because only a single group (determined by
values in the sorted prefix) needs to be kept in memory. This may also
eliminate the need to spill to disk.
- Lower startup cost, because Incremental Sort produce results after each
prefix group, which is beneficial for plans where startup cost matters
(like for example queries with LIMIT clause).
We consider both Sort and Incremental Sort, and decide based on costing.
The implemented algorithm operates in two different modes:
- Fetching a minimum number of tuples without check of equality on the
prefix keys, and sorting on all columns when safe.
- Fetching all tuples for a single prefix group and then sorting by
comparing only the remaining (non-prefix) keys.
We always start in the first mode, and employ a heuristic to switch into
the second mode if we believe it's beneficial - the goal is to minimize
the number of unnecessary comparions while keeping memory consumption
below work_mem.
This is a very old patch series. The idea was originally proposed by
Alexander Korotkov back in 2013, and then revived in 2017. In 2018 the
patch was taken over by James Coleman, who wrote and rewrote most of the
current code.
There were many reviewers/contributors since 2013 - I've done my best to
pick the most active ones, and listed them in this commit message.
Author: James Coleman, Alexander Korotkov
Reviewed-by: Tomas Vondra, Andreas Karlsson, Marti Raudsepp, Peter Geoghegan, Robert Haas, Thomas Munro, Antonin Houska, Andres Freund, Alexander Kuzmenkov
Discussion: https://postgr.es/m/CAPpHfdscOX5an71nHd8WSUH6GNOCf=V7wgDaTXdDd9=goN-gfA@mail.gmail.com
Discussion: https://postgr.es/m/CAPpHfds1waRZ=NOmueYq0sx1ZSCnt+5QJvizT8ndT2=etZEeAQ@mail.gmail.com
6 years ago
|
|
|
(void) pathkeys_count_contained_in(root->query_pathkeys, pathkeys,
|
|
|
|
&n_common_pathkeys);
|
|
|
|
|
Implement Incremental Sort
Incremental Sort is an optimized variant of multikey sort for cases when
the input is already sorted by a prefix of the requested sort keys. For
example when the relation is already sorted by (key1, key2) and we need
to sort it by (key1, key2, key3) we can simply split the input rows into
groups having equal values in (key1, key2), and only sort/compare the
remaining column key3.
This has a number of benefits:
- Reduced memory consumption, because only a single group (determined by
values in the sorted prefix) needs to be kept in memory. This may also
eliminate the need to spill to disk.
- Lower startup cost, because Incremental Sort produce results after each
prefix group, which is beneficial for plans where startup cost matters
(like for example queries with LIMIT clause).
We consider both Sort and Incremental Sort, and decide based on costing.
The implemented algorithm operates in two different modes:
- Fetching a minimum number of tuples without check of equality on the
prefix keys, and sorting on all columns when safe.
- Fetching all tuples for a single prefix group and then sorting by
comparing only the remaining (non-prefix) keys.
We always start in the first mode, and employ a heuristic to switch into
the second mode if we believe it's beneficial - the goal is to minimize
the number of unnecessary comparions while keeping memory consumption
below work_mem.
This is a very old patch series. The idea was originally proposed by
Alexander Korotkov back in 2013, and then revived in 2017. In 2018 the
patch was taken over by James Coleman, who wrote and rewrote most of the
current code.
There were many reviewers/contributors since 2013 - I've done my best to
pick the most active ones, and listed them in this commit message.
Author: James Coleman, Alexander Korotkov
Reviewed-by: Tomas Vondra, Andreas Karlsson, Marti Raudsepp, Peter Geoghegan, Robert Haas, Thomas Munro, Antonin Houska, Andres Freund, Alexander Kuzmenkov
Discussion: https://postgr.es/m/CAPpHfdscOX5an71nHd8WSUH6GNOCf=V7wgDaTXdDd9=goN-gfA@mail.gmail.com
Discussion: https://postgr.es/m/CAPpHfds1waRZ=NOmueYq0sx1ZSCnt+5QJvizT8ndT2=etZEeAQ@mail.gmail.com
6 years ago
|
|
|
return n_common_pathkeys;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* truncate_useless_pathkeys
|
|
|
|
* Shorten the given pathkey list to just the useful pathkeys.
|
|
|
|
*/
|
|
|
|
List *
|
|
|
|
truncate_useless_pathkeys(PlannerInfo *root,
|
|
|
|
RelOptInfo *rel,
|
|
|
|
List *pathkeys)
|
|
|
|
{
|
|
|
|
int nuseful;
|
|
|
|
int nuseful2;
|
|
|
|
|
|
|
|
nuseful = pathkeys_useful_for_merging(root, rel, pathkeys);
|
|
|
|
nuseful2 = pathkeys_useful_for_ordering(root, pathkeys);
|
|
|
|
if (nuseful2 > nuseful)
|
|
|
|
nuseful = nuseful2;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Note: not safe to modify input list destructively, but we can avoid
|
|
|
|
* copying the list if we're not actually going to change it
|
|
|
|
*/
|
|
|
|
if (nuseful == 0)
|
|
|
|
return NIL;
|
|
|
|
else if (nuseful == list_length(pathkeys))
|
|
|
|
return pathkeys;
|
|
|
|
else
|
|
|
|
return list_truncate(list_copy(pathkeys), nuseful);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* has_useful_pathkeys
|
|
|
|
* Detect whether the specified rel could have any pathkeys that are
|
|
|
|
* useful according to truncate_useless_pathkeys().
|
|
|
|
*
|
|
|
|
* This is a cheap test that lets us skip building pathkeys at all in very
|
|
|
|
* simple queries. It's OK to err in the direction of returning "true" when
|
|
|
|
* there really aren't any usable pathkeys, but erring in the other direction
|
|
|
|
* is bad --- so keep this in sync with the routines above!
|
|
|
|
*
|
|
|
|
* We could make the test more complex, for example checking to see if any of
|
|
|
|
* the joinclauses are really mergejoinable, but that likely wouldn't win
|
|
|
|
* often enough to repay the extra cycles. Queries with neither a join nor
|
|
|
|
* a sort are reasonably common, though, so this much work seems worthwhile.
|
|
|
|
*/
|
|
|
|
bool
|
|
|
|
has_useful_pathkeys(PlannerInfo *root, RelOptInfo *rel)
|
|
|
|
{
|
|
|
|
if (rel->joininfo != NIL || rel->has_eclass_joins)
|
|
|
|
return true; /* might be able to use pathkeys for merging */
|
|
|
|
if (root->query_pathkeys != NIL)
|
|
|
|
return true; /* might be able to use them for ordering */
|
|
|
|
return false; /* definitely useless */
|
|
|
|
}
|