diff --git a/src/backend/optimizer/plan/analyzejoins.c b/src/backend/optimizer/plan/analyzejoins.c index 129fc3d..2d9fe96 100644 --- a/src/backend/optimizer/plan/analyzejoins.c +++ b/src/backend/optimizer/plan/analyzejoins.c @@ -22,6 +22,8 @@ */ #include "postgres.h" +#include "nodes/nodeFuncs.h" +#include "optimizer/clauses.h" #include "optimizer/joininfo.h" #include "optimizer/pathnode.h" #include "optimizer/paths.h" @@ -147,19 +149,33 @@ join_is_removable(PlannerInfo *root, SpecialJoinInfo *sjinfo) { int innerrelid; RelOptInfo *innerrel; + Query *subquery; Relids joinrelids; List *clause_list = NIL; ListCell *l; int attroff; /* - * Currently, we only know how to remove left joins to a baserel with - * unique indexes. We can check most of these criteria pretty trivially - * to avoid doing useless extra work. But checking whether any of the - * indexes are unique would require iterating over the indexlist, so for - * now we just make sure there are indexes of some sort or other. If none - * of them are unique, join removal will still fail, just slightly later. + * Assuming none of the variables from the join are needed by the query, + * it is possible here to remove a left join providing we can determine + * that the join will never produce more than 1 row that matches the join + * condition. + * + * There are a few ways that we can do this: + * + * 1. When joining to a baserel we can check if a unique index exists + * where all of the columns of the index are seen in the join condition + * using suitable operators. + * + * 2. When joining to a subquery we can check if the subquery contains a + * GROUP BY clause, DISTINCT clause or set operators which will make it + * unique on the join condition. + * + * The code below is written with the assumption that join removal is more + * than likely not to happen, for this reason there are fast paths for both of + * the cases above to try to save on any unnecessary processing. */ + if (sjinfo->jointype != JOIN_LEFT || sjinfo->delay_upper_joins || bms_membership(sjinfo->min_righthand) != BMS_SINGLETON) @@ -168,11 +184,32 @@ join_is_removable(PlannerInfo *root, SpecialJoinInfo *sjinfo) innerrelid = bms_singleton_member(sjinfo->min_righthand); innerrel = find_base_rel(root, innerrelid); - if (innerrel->reloptkind != RELOPT_BASEREL || - innerrel->rtekind != RTE_RELATION || - innerrel->indexlist == NIL) + if (innerrel->reloptkind != RELOPT_BASEREL) return false; + if (innerrel->rtekind == RTE_RELATION) + { + /* + * If there are no indexes then there's certainly no unique indexes + * so there's no need to go any further. + */ + if (innerrel->indexlist == NIL) + return false; + } + else if (innerrel->rtekind == RTE_SUBQUERY) + { + subquery = root->simple_rte_array[innerrelid]->subquery; + + /* + * If the subquery has no qualities that support distinctness of any + * kind then we can be certain that we cannot remove the join. + */ + if (!query_supports_distinctness(subquery)) + return false; + } + else + return false; /* unsupported rtekind */ + /* Compute the relid set for the join we are considering */ joinrelids = bms_union(sjinfo->min_lefthand, sjinfo->min_righthand); @@ -276,17 +313,61 @@ join_is_removable(PlannerInfo *root, SpecialJoinInfo *sjinfo) */ /* Now examine the indexes to see if we have a matching unique index */ - if (relation_has_unique_index_for(root, innerrel, clause_list, NIL, NIL)) + if (innerrel->rtekind == RTE_RELATION && + relation_has_unique_index_for(root, innerrel, clause_list, NIL, NIL)) return true; /* + * For subqueries we should be able to remove the join if the subquery + * can't produce more than 1 record that matches the outer query on the + * join condition. + */ + if (innerrel->rtekind == RTE_SUBQUERY) + { + List *colnos = NULL; + List *opids = NULL; + + Assert(subquery == root->simple_rte_array[innerrelid]->subquery); + + /* + * Build a list of varattnos that we require the subquery to be unique + * over. We also build a list of the operators that are used with these + * vars in the join condition so that query_is_distinct_for can check + * that these operators are compatible with any GROUP BY, DISTINCT or + * UNIQUE clauses in the subquery. + */ + foreach(l, clause_list) + { + RestrictInfo *rinfo = (RestrictInfo *) lfirst(l); + Var *var; + + if (rinfo->outer_is_left) + var = (Var *) get_rightop(rinfo->clause); + else + var = (Var *) get_leftop(rinfo->clause); + + /* + * query_is_distinct_for only supports Vars, so anything that's not + * a var will mean the join cannot be removed. + */ + if (!var || !IsA(var, Var) || + var->varno != innerrelid) + return false; + + colnos = lappend_int(colnos, var->varattno); + opids = lappend_oid(opids, rinfo->hashjoinoperator); + } + + return query_is_distinct_for(subquery, colnos, opids); + } + + /* * Some day it would be nice to check for other methods of establishing * distinctness. */ return false; } - /* * Remove the target relid from the planner's data structures, having * determined that there is no need to include it in the query. diff --git a/src/backend/optimizer/util/pathnode.c b/src/backend/optimizer/util/pathnode.c index d129f8d..119fb34 100644 --- a/src/backend/optimizer/util/pathnode.c +++ b/src/backend/optimizer/util/pathnode.c @@ -38,7 +38,6 @@ typedef enum } PathCostComparison; static List *translate_sub_tlist(List *tlist, int relid); -static bool query_is_distinct_for(Query *query, List *colnos, List *opids); static Oid distinct_col_search(int colno, List *colnos, List *opids); @@ -1451,6 +1450,32 @@ translate_sub_tlist(List *tlist, int relid) } /* + * query_supports_distinctness - True if the query can be seen to be distinct + * on some set of columns. + * + * This is effectively a pre-checking function for query_is_distinct_for, + * and can be used by any code that needs to make a call to + * query_is_distinct_for, but has to perform some possibly expensive processing + * beforehand. If this function returns False then a call to + * query_is_distinct_for will also return False, though the reverse is not true + * as this would depend on the columns and operators passed to + * query_is_distinct_for. + */ +bool +query_supports_distinctness(Query *query) +{ + if (query->distinctClause != NIL || + query->groupClause != NIL || + query->hasAggs || + query->havingQual || + query->setOperations) + return true; + + return false; +} + + +/* * query_is_distinct_for - does query never return duplicates of the * specified columns? * @@ -1465,12 +1490,16 @@ translate_sub_tlist(List *tlist, int relid) * should give trustworthy answers for all operators that we might need * to deal with here.) */ -static bool +bool query_is_distinct_for(Query *query, List *colnos, List *opids) { ListCell *l; Oid opid; + /* XXX the logic used to test for distinctness here must be followed by + * query_supports_distinctness. If this function returns True, then + * query_supports_distinctness must also return True. + */ Assert(list_length(colnos) == list_length(opids)); /* diff --git a/src/include/optimizer/pathnode.h b/src/include/optimizer/pathnode.h index a0bcc82..1df6e50 100644 --- a/src/include/optimizer/pathnode.h +++ b/src/include/optimizer/pathnode.h @@ -67,6 +67,8 @@ extern ResultPath *create_result_path(List *quals); extern MaterialPath *create_material_path(RelOptInfo *rel, Path *subpath); extern UniquePath *create_unique_path(PlannerInfo *root, RelOptInfo *rel, Path *subpath, SpecialJoinInfo *sjinfo); +extern bool query_supports_distinctness(Query *query); +extern bool query_is_distinct_for(Query *query, List *colnos, List *opids); extern Path *create_subqueryscan_path(PlannerInfo *root, RelOptInfo *rel, List *pathkeys, Relids required_outer); extern Path *create_functionscan_path(PlannerInfo *root, RelOptInfo *rel, diff --git a/src/test/regress/expected/join.out b/src/test/regress/expected/join.out index c62a63f..9f7bff2 100644 --- a/src/test/regress/expected/join.out +++ b/src/test/regress/expected/join.out @@ -3131,9 +3131,11 @@ begin; CREATE TEMP TABLE a (id int PRIMARY KEY, b_id int); CREATE TEMP TABLE b (id int PRIMARY KEY, c_id int); CREATE TEMP TABLE c (id int PRIMARY KEY); +CREATE TEMP TABLE d (a INT, b INT); INSERT INTO a VALUES (0, 0), (1, NULL); INSERT INTO b VALUES (0, 0), (1, NULL); INSERT INTO c VALUES (0), (1); +INSERT INTO d VALUES (1,3),(2,2),(3,1); -- all three cases should be optimizable into a simple seqscan explain (costs off) SELECT a.* FROM a LEFT JOIN b ON a.b_id = b.id; QUERY PLAN @@ -3169,6 +3171,117 @@ select id from a where id in ( -> Seq Scan on b (5 rows) +-- check that join removal works for a left join when joining a subquery +-- that is guaranteed to be unique on the join condition by the GROUP BY clause +EXPLAIN (COSTS OFF) +SELECT a.id FROM a LEFT JOIN (SELECT b.id FROM b GROUP BY b.id) b ON a.b_id = b.id; + QUERY PLAN +--------------- + Seq Scan on a +(1 row) + +-- check that join removal works for a left join when joining a subquery +-- that is guaranteed to be unique on the join condition by the GROUP BY clause +-- which contains more than 1 column. +EXPLAIN (COSTS OFF) +SELECT a.id FROM a LEFT JOIN (SELECT b.id,b.c_id FROM b GROUP BY b.id,b.c_id) b ON a.b_id = b.id AND a.id = b.c_id; + QUERY PLAN +--------------- + Seq Scan on a +(1 row) + +-- check that join removal works for a left join when joining a subquery +-- where the join condition is a superset of the columns in the GROUP BY. +EXPLAIN (COSTS OFF) +SELECT a.id FROM a +LEFT JOIN (SELECT b.id,c_id FROM b GROUP BY b.id) b ON a.id = b.id AND b.c_id = 1; + QUERY PLAN +--------------- + Seq Scan on a +(1 row) + +-- check that join removal works for a left join when joining a subquery +-- that is guaranteed to be unique on the join condition by the DISTINCT clause +EXPLAIN (COSTS OFF) +SELECT a.id FROM a LEFT JOIN (SELECT DISTINCT a+b AS ab FROM d) d ON a.id = d.ab; + QUERY PLAN +--------------- + Seq Scan on a +(1 row) + +-- check join removal works when joining to a subquery that is guaranteed to be +-- unique on the join condition even when the subquery itself involves a join. +EXPLAIN (COSTS OFF) +SELECT a.id FROM a LEFT JOIN (SELECT b.id FROM b INNER JOIN c ON b.id = c.id GROUP BY b.id) b ON a.id + 10 = b.id; + QUERY PLAN +--------------- + Seq Scan on a +(1 row) + +-- check join removal works with a left join when joining a unique subquery which +-- contains 2 tables where the uniqueness enforced by the GROUP BY clause is a +-- subset of the join condition. +EXPLAIN (COSTS OFF) +SELECT a.id FROM a +LEFT JOIN (SELECT b.id,1 as dummy FROM b INNER JOIN c ON b.id = c.id GROUP BY b.id) b ON a.id = b.id AND b.dummy = 1; + QUERY PLAN +--------------- + Seq Scan on a +(1 row) + +-- join removal is not possible when the GROUP BY contains a column which is +-- not in the join condition. +EXPLAIN (COSTS OFF) +SELECT a.id FROM a LEFT JOIN (SELECT b.id FROM b GROUP BY b.id,b.c_id) b ON a.id = b.id; + QUERY PLAN +--------------------------------- + Hash Right Join + Hash Cond: (b.id = a.id) + -> HashAggregate + Group Key: b.id, b.c_id + -> Seq Scan on b + -> Hash + -> Seq Scan on a +(7 rows) + +-- join removal is not possible when DISTINCT clause contains a column which is +-- not in the join condition. +EXPLAIN (COSTS OFF) +SELECT a.id FROM a LEFT JOIN (SELECT DISTINCT b.id,c_id FROM b) b ON a.id = b.id; + QUERY PLAN +--------------------------------- + Hash Right Join + Hash Cond: (b.id = a.id) + -> HashAggregate + Group Key: b.id, b.c_id + -> Seq Scan on b + -> Hash + -> Seq Scan on a +(7 rows) + +-- join removal is not possible when there are set returning functions in the target list. +EXPLAIN (COSTS OFF) +SELECT a.id FROM a LEFT JOIN (SELECT id,generate_series(1,2) FROM b GROUP BY id) b ON a.id = b.id; + QUERY PLAN +---------------------------- + Hash Right Join + Hash Cond: (b.id = a.id) + -> HashAggregate + Group Key: b.id + -> Seq Scan on b + -> Hash + -> Seq Scan on a +(7 rows) + +-- check join removal works when uniqueness of the join condition is enforced by +-- a UNION +EXPLAIN (COSTS OFF) +SELECT a.* FROM A LEFT OUTER JOIN (SELECT id FROM b UNION SELECT id from b) b on a.id=b.id; + QUERY PLAN +--------------- + Seq Scan on a +(1 row) + rollback; create temp table parent (k int primary key, pd int); create temp table child (k int unique, cd int); diff --git a/src/test/regress/sql/join.sql b/src/test/regress/sql/join.sql index 1031f26..a9ec9a8 100644 --- a/src/test/regress/sql/join.sql +++ b/src/test/regress/sql/join.sql @@ -919,9 +919,11 @@ begin; CREATE TEMP TABLE a (id int PRIMARY KEY, b_id int); CREATE TEMP TABLE b (id int PRIMARY KEY, c_id int); CREATE TEMP TABLE c (id int PRIMARY KEY); +CREATE TEMP TABLE d (a INT, b INT); INSERT INTO a VALUES (0, 0), (1, NULL); INSERT INTO b VALUES (0, 0), (1, NULL); INSERT INTO c VALUES (0), (1); +INSERT INTO d VALUES (1,3),(2,2),(3,1); -- all three cases should be optimizable into a simple seqscan explain (costs off) SELECT a.* FROM a LEFT JOIN b ON a.b_id = b.id; @@ -936,6 +938,59 @@ select id from a where id in ( select b.id from b left join c on b.id = c.id ); +-- check that join removal works for a left join when joining a subquery +-- that is guaranteed to be unique on the join condition by the GROUP BY clause +EXPLAIN (COSTS OFF) +SELECT a.id FROM a LEFT JOIN (SELECT b.id FROM b GROUP BY b.id) b ON a.b_id = b.id; + +-- check that join removal works for a left join when joining a subquery +-- that is guaranteed to be unique on the join condition by the GROUP BY clause +-- which contains more than 1 column. +EXPLAIN (COSTS OFF) +SELECT a.id FROM a LEFT JOIN (SELECT b.id,b.c_id FROM b GROUP BY b.id,b.c_id) b ON a.b_id = b.id AND a.id = b.c_id; + +-- check that join removal works for a left join when joining a subquery +-- where the join condition is a superset of the columns in the GROUP BY. +EXPLAIN (COSTS OFF) +SELECT a.id FROM a +LEFT JOIN (SELECT b.id,c_id FROM b GROUP BY b.id) b ON a.id = b.id AND b.c_id = 1; + +-- check that join removal works for a left join when joining a subquery +-- that is guaranteed to be unique on the join condition by the DISTINCT clause +EXPLAIN (COSTS OFF) +SELECT a.id FROM a LEFT JOIN (SELECT DISTINCT a+b AS ab FROM d) d ON a.id = d.ab; + +-- check join removal works when joining to a subquery that is guaranteed to be +-- unique on the join condition even when the subquery itself involves a join. +EXPLAIN (COSTS OFF) +SELECT a.id FROM a LEFT JOIN (SELECT b.id FROM b INNER JOIN c ON b.id = c.id GROUP BY b.id) b ON a.id + 10 = b.id; + +-- check join removal works with a left join when joining a unique subquery which +-- contains 2 tables where the uniqueness enforced by the GROUP BY clause is a +-- subset of the join condition. +EXPLAIN (COSTS OFF) +SELECT a.id FROM a +LEFT JOIN (SELECT b.id,1 as dummy FROM b INNER JOIN c ON b.id = c.id GROUP BY b.id) b ON a.id = b.id AND b.dummy = 1; + +-- join removal is not possible when the GROUP BY contains a column which is +-- not in the join condition. +EXPLAIN (COSTS OFF) +SELECT a.id FROM a LEFT JOIN (SELECT b.id FROM b GROUP BY b.id,b.c_id) b ON a.id = b.id; + +-- join removal is not possible when DISTINCT clause contains a column which is +-- not in the join condition. +EXPLAIN (COSTS OFF) +SELECT a.id FROM a LEFT JOIN (SELECT DISTINCT b.id,c_id FROM b) b ON a.id = b.id; + +-- join removal is not possible when there are set returning functions in the target list. +EXPLAIN (COSTS OFF) +SELECT a.id FROM a LEFT JOIN (SELECT id,generate_series(1,2) FROM b GROUP BY id) b ON a.id = b.id; + +-- check join removal works when uniqueness of the join condition is enforced by +-- a UNION +EXPLAIN (COSTS OFF) +SELECT a.* FROM A LEFT OUTER JOIN (SELECT id FROM b UNION SELECT id from b) b on a.id=b.id; + rollback; create temp table parent (k int primary key, pd int);