From 71a96d4539b228314773938324193bf144950ebd Mon Sep 17 00:00:00 2001 From: Richard Guo Date: Mon, 16 Nov 2026 15:06:07 +0900 Subject: [PATCH v5 2/3] Skip DISTINCT and GROUP BY steps when the input is already distinct If the result of the scan/join phase cannot contain two rows that are equal on the DISTINCT columns, the DISTINCT step has nothing to do. The same goes for a GROUP BY step when every group is certain to consist of a single row and there are no aggregates, HAVING clause or grouping sets to evaluate. Until now the planner always added the step. This commit uses UniqueKeys to detect such cases. The procedure that proves a join relation unique for a set of join clauses is reused, starting from the EquivalenceClasses of the DISTINCT or GROUP BY columns. For example, in SELECT DISTINCT a.id, a.y FROM a JOIN b ON a.x = b.id; with primary keys on a.id and b.id, no DISTINCT step is needed. DISTINCT and GROUP BY consider NULLs equal to each other, whereas a unique index normally allows any number of rows with NULLs in its columns. So a UniqueKey is now marked null_aware if it also holds with NULLs considered equal, which is the case if the index is NULLS NOT DISTINCT, or if each column in the UniqueKey has a NOT NULL constraint or is forced to be non-NULL by a strict restriction clause. A UniqueKey that is not null_aware can still be used if join clauses keep its columns from being NULL. In SELECT DISTINCT a.id, b.y FROM a JOIN b ON a.x = b.u; with a unique index on the nullable column b.u, every row of b that the join emits has a non-NULL b.u. We accept such a UniqueKey if each of its EquivalenceClasses has two members that can be computed within the join relation. As before, only plain tables and joins of them that involve no outer joins, semijoins or antijoins are handled. The step is also kept for DISTINCT ON, and when the targetlist contains set-returning functions. Some existing tests for the removal of redundant GROUP BY columns would now lose their grouping step altogether. Add an aggregate to them so that they continue to test what they were meant to. --- src/backend/optimizer/README | 34 +++- src/backend/optimizer/path/uniquekeys.c | 238 +++++++++++++++++++++-- src/backend/optimizer/plan/planner.c | 107 +++++++++- src/include/nodes/pathnodes.h | 11 +- src/include/optimizer/paths.h | 2 + src/test/regress/expected/aggregates.out | 24 +-- src/test/regress/expected/join.out | 27 ++- src/test/regress/expected/uniquekeys.out | 179 ++++++++++++++++- src/test/regress/sql/aggregates.sql | 24 +-- src/test/regress/sql/join.sql | 6 +- src/test/regress/sql/uniquekeys.sql | 68 ++++++- 11 files changed, 640 insertions(+), 80 deletions(-) diff --git a/src/backend/optimizer/README b/src/backend/optimizer/README index 3b247de7572..d2c8a17974c 100644 --- a/src/backend/optimizer/README +++ b/src/backend/optimizer/README @@ -1033,18 +1033,23 @@ UniqueKey consisting of the ECs that a and b belong to. As with PathKeys, an EC that contains a constant is left out. That can leave an empty UniqueKey, which means that the relation has at most one row. -A UniqueKey says nothing about rows in which the value of one of its ECs is -NULL, because a unique index allows any number of such rows. That is good -enough for matching mergejoinable join clauses, which are assumed to be -strict. +By default, a UniqueKey says nothing about rows in which the value of one +of its ECs is NULL, because a unique index allows any number of such rows. +That is good enough for matching mergejoinable join clauses, which are +assumed to be strict. DISTINCT and GROUP BY, however, consider NULLs equal +to each other. A UniqueKey that also holds on those terms is marked +null_aware; that is the case if the index is NULLS NOT DISTINCT, or if each +column in the UniqueKey has a NOT NULL constraint or is forced to be +non-NULL by a strict restriction clause. The UniqueKeys of base relations are built the first time they are needed, so that a query that never needs them does not pay for them. UniqueKeys are used to detect unique inner sides of joins when the inner -side is itself a join, as described in the following. For now only plain -tables get UniqueKeys, and only joins that involve no outer joins, -semijoins or antijoins are handled. +side is itself a join, and to skip DISTINCT and GROUP BY steps whose input +is already distinct. The following describes each of these in turn. For +now only plain tables get UniqueKeys, and only joins that involve no outer +joins, semijoins or antijoins are handled. We do not keep UniqueKeys for join relations. A join relation would typically have many of them, most of which would never be used. And since @@ -1079,6 +1084,21 @@ The second step relies on all members of an EC that are computable within the join relation being equal in its output. That is why a broken EC is never used. +The same procedure tells whether the result of the scan/join phase is +already distinct on the DISTINCT or GROUP BY columns: start with the ECs of +those columns, and use only UniqueKeys marked null_aware. If so, a +DISTINCT step is skipped, and so is a grouping step that has no aggregates +or HAVING clause to evaluate. + +A UniqueKey that is not marked null_aware can still be used there if join +clauses keep its columns from being NULL. In + + SELECT DISTINCT a.id, b.y FROM a JOIN b ON a.x = b.u + +with a unique index on the nullable column b.u, every row of b that the +join emits has a non-NULL b.u, so the index is as good as one on a NOT NULL +column. + Order of processing for EquivalenceClasses and PathKeys ------------------------------------------------------- diff --git a/src/backend/optimizer/path/uniquekeys.c b/src/backend/optimizer/path/uniquekeys.c index 55ef7acc1c3..4ef18c1b82a 100644 --- a/src/backend/optimizer/path/uniquekeys.c +++ b/src/backend/optimizer/path/uniquekeys.c @@ -9,6 +9,12 @@ * distinct is worked out when needed, from the UniqueKeys of the base * relations it consists of. * + * There are two kinds of question. Mergejoinable join clauses are assumed + * to be strict, so when matching them we need not care about rows that have + * NULLs in the columns of interest. DISTINCT and GROUP BY, on the other + * hand, consider NULLs equal to each other, so for them only UniqueKeys that + * hold on those terms can be used. + * * See src/backend/optimizer/README for more information. * * @@ -21,17 +27,27 @@ */ #include "postgres.h" +#include "access/sysattr.h" +#include "nodes/multibitmapset.h" +#include "optimizer/clauses.h" #include "optimizer/pathnode.h" #include "optimizer/paths.h" #include "utils/lsyscache.h" +static bool joinrel_is_distinct_for_ecs(PlannerInfo *root, RelOptInfo *joinrel, + Bitmapset *known_ecs, + bool nulls_equal); +static Bitmapset *get_joined_ecs(PlannerInfo *root, RelOptInfo *joinrel); static void build_base_rel_uniquekeys(PlannerInfo *root); static void build_plain_rel_uniquekeys(PlannerInfo *root, RelOptInfo *rel); -static void add_uniquekey(RelOptInfo *rel, Bitmapset *eclass_indexes); +static void add_uniquekey(RelOptInfo *rel, Bitmapset *eclass_indexes, + bool null_aware); static int find_ec_index(PlannerInfo *root, RelOptInfo *rel, Expr *expr, List *opfamilies, Oid collation); static bool rel_has_uniquekey_in(PlannerInfo *root, RelOptInfo *rel, - Bitmapset *eclass_indexes); + Bitmapset *eclass_indexes, + Bitmapset *joined_ecs, + bool nulls_equal); static Bitmapset *get_own_eclass_indexes(PlannerInfo *root, RelOptInfo *rel); @@ -75,23 +91,12 @@ joinrel_supports_distinctness(PlannerInfo *root, RelOptInfo *joinrel) * its outer_is_left flag, and an expression not involving this relation on * the other. Return true if no two rows emitted by the join relation could * possibly join to the same row of the other relation. - * - * A join emits each combination of its base relations' rows at most once. - * So the join is distinct if a row of the other relation leaves only one - * possible row for each of its base relations. We work that out by tracking - * the set of ECs whose values are fixed, starting with those that the clauses - * equate to the other relation. A base relation with a UniqueKey within that - * set can have only one row, and so it fixes the value of every EC that has a - * member belonging to it. We repeat this until all base relations are - * accounted for or we can make no more progress. */ bool joinrel_is_distinct_for(PlannerInfo *root, RelOptInfo *joinrel, List *clause_list) { Bitmapset *known_ecs = NULL; - Relids remaining; - bool found; if (!joinrel_supports_distinctness(root, joinrel)) return false; @@ -112,6 +117,88 @@ joinrel_is_distinct_for(PlannerInfo *root, RelOptInfo *joinrel, known_ecs = bms_add_member(known_ecs, ec->ec_index); } + /* The clauses are mergejoinable, hence strict, so NULLs don't matter */ + return joinrel_is_distinct_for_ecs(root, joinrel, known_ecs, false); +} + +/* + * rel_is_distinct_for_pathkeys + * Does the relation return only distinct rows according to the given + * pathkeys? + * + * Return true if no two rows emitted by the relation could possibly have + * equal values in all the pathkeys' ECs, with NULLs considered equal to each + * other. The sort ordering of the pathkeys is of no interest. + * + * An empty list means all rows are considered equal, so the relation is then + * distinct only if it has at most one row. + */ +bool +rel_is_distinct_for_pathkeys(PlannerInfo *root, RelOptInfo *rel, + List *pathkeys) +{ + Bitmapset *known_ecs = NULL; + + if (IS_JOIN_REL(rel)) + { + if (!joinrel_supports_distinctness(root, rel)) + return false; + } + else if (rel->reloptkind == RELOPT_BASEREL) + { + /* a base rel without UniqueKeys just fails the lookup below */ + build_base_rel_uniquekeys(root); + } + else + return false; /* we know nothing about other relations */ + + foreach_node(PathKey, pathkey, pathkeys) + { + known_ecs = bms_add_member(known_ecs, pathkey->pk_eclass->ec_index); + } + + if (IS_JOIN_REL(rel)) + return joinrel_is_distinct_for_ecs(root, rel, known_ecs, true); + + return rel_has_uniquekey_in(root, rel, known_ecs, NULL, true); +} + +/* + * joinrel_is_distinct_for_ecs + * Does the join relation return only distinct rows according to the + * given set of ECs? + * + * Return true if no two rows emitted by the join relation could possibly + * have equal values in all the ECs of known_ecs, which is a set of indexes + * into root->eq_classes. If nulls_equal is true, NULLs are considered equal + * to each other; otherwise rows having a NULL in any of the ECs are ignored. + * + * A join emits each combination of its base relations' rows at most once. + * So the join is distinct if fixing the values of the known ECs leaves only + * one possible row for each of its base relations. A base relation with a + * UniqueKey within the set of known ECs can have only one row, and so it + * fixes the value of every EC that has a member belonging to it; those ECs + * are added to the set. We repeat this until all base relations are + * accounted for or we can make no more progress. + * + * The caller must have checked joinrel_supports_distinctness(). + */ +static bool +joinrel_is_distinct_for_ecs(PlannerInfo *root, RelOptInfo *joinrel, + Bitmapset *known_ecs, bool nulls_equal) +{ + Bitmapset *joined_ecs = NULL; + Relids remaining; + bool found; + + /* + * When NULLs are considered equal, a UniqueKey that is not null_aware can + * still be used if join clauses keep its ECs from being NULL; see + * rel_has_uniquekey_in(). So find those ECs. + */ + if (nulls_equal) + joined_ecs = get_joined_ecs(root, joinrel); + /* There are no outer join relids, so all the relids are base relations */ remaining = bms_copy(joinrel->relids); @@ -125,7 +212,8 @@ joinrel_is_distinct_for(PlannerInfo *root, RelOptInfo *joinrel, { RelOptInfo *rel = find_base_rel(root, relid); - if (!rel_has_uniquekey_in(root, rel, known_ecs)) + if (!rel_has_uniquekey_in(root, rel, known_ecs, joined_ecs, + nulls_equal)) continue; remaining = bms_del_member(remaining, relid); @@ -138,6 +226,66 @@ joinrel_is_distinct_for(PlannerInfo *root, RelOptInfo *joinrel, return remaining == NULL; } +/* + * get_joined_ecs + * Get the indexes of the ECs that are known to be non-NULL in the join + * relation, because a join clause within it equates two of their + * members. + * + * The members of an EC are equated using mergejoinable operators, which we + * assume to be strict (as nodeMergejoin.c does). So an EC with two members + * that can be computed within the join relation cannot be NULL there. + * + * This is only of interest for UniqueKeys that are not null_aware, so to + * save work we look at just the ECs in those. + */ +static Bitmapset * +get_joined_ecs(PlannerInfo *root, RelOptInfo *joinrel) +{ + Bitmapset *result = NULL; + int relid = -1; + + while ((relid = bms_next_member(joinrel->relids, relid)) >= 0) + { + RelOptInfo *rel = find_base_rel(root, relid); + + foreach_node(UniqueKey, ukey, rel->uniquekeys) + { + int i = -1; + + if (ukey->null_aware) + continue; + + while ((i = bms_next_member(ukey->eclass_indexes, i)) >= 0) + { + EquivalenceClass *ec; + int nmembers = 0; + + if (bms_is_member(i, result)) + continue; + + /* The members of a broken EC are not known to be equal */ + ec = list_nth_node(EquivalenceClass, root->eq_classes, i); + if (ec->ec_broken) + continue; + + foreach_node(EquivalenceMember, em, ec->ec_members) + { + if (!em->em_is_const && + bms_is_subset(em->em_relids, joinrel->relids) && + ++nmembers >= 2) + { + result = bms_add_member(result, i); + break; + } + } + } + } + } + + return result; +} + /* * build_base_rel_uniquekeys * Build the UniqueKeys of all base relations, if not already done. @@ -206,6 +354,9 @@ build_base_rel_uniquekeys(PlannerInfo *root) static void build_plain_rel_uniquekeys(PlannerInfo *root, RelOptInfo *rel) { + List *nonnullable_vars = NIL; + bool nonnullable_vars_valid = false; + /* Short-circuit if no EC mentions this relation */ if (rel->eclass_indexes == NULL) return; @@ -213,6 +364,7 @@ build_plain_rel_uniquekeys(PlannerInfo *root, RelOptInfo *rel) foreach_node(IndexOptInfo, ind, rel->indexlist) { Bitmapset *key_ecs = NULL; + bool null_aware = true; int c; /* @@ -257,11 +409,43 @@ build_plain_rel_uniquekeys(PlannerInfo *root, RelOptInfo *rel) continue; key_ecs = bms_add_member(key_ecs, ec_index); + + /* + * Unless the index is NULLS NOT DISTINCT, it allows duplicates + * among rows that have a NULL in this column. So the UniqueKey + * is not null_aware unless the column cannot be NULL, due to a + * NOT NULL constraint or a strict restriction clause. + */ + if (null_aware && !ind->nullsnotdistinct) + { + int attno = ind->indexkeys[c]; + + if (attno == 0) + null_aware = false; + else if (!bms_is_member(attno, rel->notnullattnums)) + { + if (!nonnullable_vars_valid) + { + foreach_node(RestrictInfo, rinfo, rel->baserestrictinfo) + { + nonnullable_vars = + mbms_add_members(nonnullable_vars, + find_nonnullable_vars((Node *) rinfo->clause)); + } + nonnullable_vars_valid = true; + } + + if (!mbms_is_member(rel->relid, + attno - FirstLowInvalidHeapAttributeNumber, + nonnullable_vars)) + null_aware = false; + } + } } /* Add the UniqueKey if we matched all the index columns */ if (c == ind->nkeycolumns) - add_uniquekey(rel, key_ecs); + add_uniquekey(rel, key_ecs, null_aware); } } @@ -270,10 +454,12 @@ build_plain_rel_uniquekeys(PlannerInfo *root, RelOptInfo *rel) * Add a UniqueKey for the given set of ECs to the relation, unless it * is redundant. * - * A UniqueKey is redundant if another one of the relation is a subset of it. + * A UniqueKey is redundant if another one of the relation is a subset of it + * and can be used wherever it can. */ static void -add_uniquekey(RelOptInfo *rel, Bitmapset *eclass_indexes) +add_uniquekey(RelOptInfo *rel, Bitmapset *eclass_indexes, + bool null_aware) { UniqueKey *ukey; ListCell *lc; @@ -283,16 +469,19 @@ add_uniquekey(RelOptInfo *rel, Bitmapset *eclass_indexes) UniqueKey *other = lfirst_node(UniqueKey, lc); /* If an existing key makes the new one redundant, we're done */ - if (bms_is_subset(other->eclass_indexes, eclass_indexes)) + if (bms_is_subset(other->eclass_indexes, eclass_indexes) && + (other->null_aware || !null_aware)) return; /* Remove any existing key that the new one makes redundant */ - if (bms_is_subset(eclass_indexes, other->eclass_indexes)) + if (bms_is_subset(eclass_indexes, other->eclass_indexes) && + (null_aware || !other->null_aware)) rel->uniquekeys = foreach_delete_current(rel->uniquekeys, lc); } ukey = makeNode(UniqueKey); ukey->eclass_indexes = eclass_indexes; + ukey->null_aware = null_aware; rel->uniquekeys = lappend(rel->uniquekeys, ukey); } @@ -333,16 +522,25 @@ find_ec_index(PlannerInfo *root, RelOptInfo *rel, Expr *expr, * rel_has_uniquekey_in * Does the relation have a UniqueKey that is a subset of the given set * of ECs? + * + * If nulls_equal is true, the values of the ECs can be NULL, so only + * UniqueKeys that are null_aware are of use, or ones whose ECs are all in + * joined_ecs and so cannot be NULL (see get_joined_ecs). */ static bool rel_has_uniquekey_in(PlannerInfo *root, RelOptInfo *rel, - Bitmapset *eclass_indexes) + Bitmapset *eclass_indexes, Bitmapset *joined_ecs, + bool nulls_equal) { foreach_node(UniqueKey, ukey, rel->uniquekeys) { bool broken = false; int i = -1; + if (nulls_equal && !ukey->null_aware && + !bms_is_subset(ukey->eclass_indexes, joined_ecs)) + continue; + if (!bms_is_subset(ukey->eclass_indexes, eclass_indexes)) continue; diff --git a/src/backend/optimizer/plan/planner.c b/src/backend/optimizer/plan/planner.c index 233b13f4cdf..68c3a29c524 100644 --- a/src/backend/optimizer/plan/planner.c +++ b/src/backend/optimizer/plan/planner.c @@ -184,6 +184,12 @@ static RelOptInfo *create_grouping_paths(PlannerInfo *root, bool target_parallel_safe, grouping_sets_data *gd); static bool is_degenerate_grouping(PlannerInfo *root); +static bool is_redundant_grouping(PlannerInfo *root, RelOptInfo *input_rel); +static bool is_redundant_distinct(PlannerInfo *root, RelOptInfo *input_rel); +static void create_redundant_step_paths(PlannerInfo *root, + RelOptInfo *input_rel, + RelOptInfo *output_rel, + PathTarget *target); static void create_degenerate_grouping_paths(PlannerInfo *root, RelOptInfo *input_rel, RelOptInfo *grouped_rel); @@ -4107,11 +4113,13 @@ create_grouping_paths(PlannerInfo *root, target_parallel_safe, parse->havingQual); /* - * Create either paths for a degenerate grouping or paths for ordinary - * grouping, as appropriate. + * Create either paths for a degenerate grouping, paths for a redundant + * grouping, or paths for ordinary grouping, as appropriate. */ if (is_degenerate_grouping(root)) create_degenerate_grouping_paths(root, input_rel, grouped_rel); + else if (is_redundant_grouping(root, input_rel)) + create_redundant_step_paths(root, input_rel, grouped_rel, target); else { int flags = 0; @@ -4262,6 +4270,85 @@ is_degenerate_grouping(PlannerInfo *root) !parse->hasAggs && parse->groupClause == NIL; } +/* + * is_redundant_grouping + * + * A redundant grouping is one in which every group is certain to consist of + * a single input row, and there is nothing to compute or filter per group. + * The input rows can then be returned as they are. + */ +static bool +is_redundant_grouping(PlannerInfo *root, RelOptInfo *input_rel) +{ + Query *parse = root->parse; + List *pathkeys; + + if (parse->groupClause == NIL || parse->groupingSets || + parse->hasAggs || parse->havingQual) + return false; + + /* SRFs in the grouping columns multiply the rows of the input paths */ + if (parse->hasTargetSRFs) + return false; + + /* + * If the grouping columns are not sortable, group_pathkeys is empty and + * we can only succeed if the input has at most one row; that's fine. + */ + pathkeys = list_copy_head(root->group_pathkeys, + root->num_groupby_pathkeys); + + return rel_is_distinct_for_pathkeys(root, input_rel, pathkeys); +} + +/* + * is_redundant_distinct + * + * A redundant DISTINCT is one whose input rows are certain to be distinct + * already. + */ +static bool +is_redundant_distinct(PlannerInfo *root, RelOptInfo *input_rel) +{ + Query *parse = root->parse; + + if (parse->hasDistinctOn) + return false; + + /* SRFs in the targetlist multiply the rows of the input paths */ + if (parse->hasTargetSRFs) + return false; + + /* As above, it's fine if distinct_pathkeys is empty */ + return rel_is_distinct_for_pathkeys(root, input_rel, + root->distinct_pathkeys); +} + +/* + * create_redundant_step_paths + * + * Build paths for an upper rel whose processing step has been found to be + * redundant, by just projecting the input rel's paths to the given target. + */ +static void +create_redundant_step_paths(PlannerInfo *root, RelOptInfo *input_rel, + RelOptInfo *output_rel, PathTarget *target) +{ + foreach_ptr(Path, path, input_rel->pathlist) + { + /* + * create_projection_path insists that a ProjectionPath it is given + * belong to the same rel, so look through one that belongs to the + * input rel. + */ + if (IsA(path, ProjectionPath)) + path = ((ProjectionPath *) path)->subpath; + + add_path(output_rel, (Path *) + create_projection_path(root, output_rel, path, target)); + } +} + /* * create_degenerate_grouping_paths * @@ -5113,11 +5200,19 @@ create_distinct_paths(PlannerInfo *root, RelOptInfo *input_rel, distinct_rel->useridiscurrent = input_rel->useridiscurrent; distinct_rel->fdwroutine = input_rel->fdwroutine; - /* build distinct paths based on input_rel's pathlist */ - create_final_distinct_paths(root, input_rel, distinct_rel); + if (is_redundant_distinct(root, input_rel)) + { + /* the input rows are distinct already, so just pass them on */ + create_redundant_step_paths(root, input_rel, distinct_rel, target); + } + else + { + /* build distinct paths based on input_rel's pathlist */ + create_final_distinct_paths(root, input_rel, distinct_rel); - /* now build distinct paths based on input_rel's partial_pathlist */ - create_partial_distinct_paths(root, input_rel, distinct_rel, target); + /* now build distinct paths based on input_rel's partial_pathlist */ + create_partial_distinct_paths(root, input_rel, distinct_rel, target); + } /* Give a helpful error if we failed to create any paths */ if (distinct_rel->pathlist == NIL) diff --git a/src/include/nodes/pathnodes.h b/src/include/nodes/pathnodes.h index a672e6b5eab..d76fe6a09ee 100644 --- a/src/include/nodes/pathnodes.h +++ b/src/include/nodes/pathnodes.h @@ -3887,8 +3887,12 @@ typedef struct AggTransInfo * in the set has a member belonging to the relation. An empty set means that * the relation has at most one row. * - * Rows containing NULLs are not covered: the relation can have any number of - * rows in which the value of some EC in the set is NULL. + * By default, rows containing NULLs are not covered: the relation can have + * any number of rows in which the value of some EC in the set is NULL. If + * null_aware is true, the UniqueKey also holds with NULLs considered + * equal to each other, either because the underlying index is NULLS NOT + * DISTINCT or because none of the columns can be NULL in the relation's + * output. */ typedef struct UniqueKey { @@ -3898,6 +3902,9 @@ typedef struct UniqueKey /* indexes in PlannerInfo's eq_classes list of the ECs in the key */ Bitmapset *eclass_indexes; + + /* does the key also hold with NULLs considered equal to each other? */ + bool null_aware; } UniqueKey; /* diff --git a/src/include/optimizer/paths.h b/src/include/optimizer/paths.h index 78685679c75..a62c17bf538 100644 --- a/src/include/optimizer/paths.h +++ b/src/include/optimizer/paths.h @@ -219,6 +219,8 @@ extern bool joinrel_supports_distinctness(PlannerInfo *root, RelOptInfo *joinrel); extern bool joinrel_is_distinct_for(PlannerInfo *root, RelOptInfo *joinrel, List *clause_list); +extern bool rel_is_distinct_for_pathkeys(PlannerInfo *root, RelOptInfo *rel, + List *pathkeys); /* * pathkeys.c diff --git a/src/test/regress/expected/aggregates.out b/src/test/regress/expected/aggregates.out index 275263c293f..b9ffdf854b1 100644 --- a/src/test/regress/expected/aggregates.out +++ b/src/test/regress/expected/aggregates.out @@ -1624,7 +1624,7 @@ create temp table t1 (a int, b int, c int, d int, primary key (a, b)); create temp table t2 (x int, y int, z int, primary key (x, y)); create temp table t3 (a int, b int, c int, primary key(a, b) deferrable); -- Non-primary-key columns can be removed from GROUP BY -explain (costs off) select * from t1 group by a,b,c,d; +explain (costs off) select count(*) from t1 group by a,b,c,d; QUERY PLAN ---------------------- HashAggregate @@ -1642,7 +1642,7 @@ explain (costs off) select a,c from t1 group by a,c,d; (3 rows) -- Test removal across multiple relations -explain (costs off) select * +explain (costs off) select count(*) from t1 inner join t2 on t1.a = t2.x and t1.b = t2.y group by t1.a,t1.b,t1.c,t1.d,t2.x,t2.y,t2.z; QUERY PLAN @@ -1657,7 +1657,7 @@ group by t1.a,t1.b,t1.c,t1.d,t2.x,t2.y,t2.z; (7 rows) -- Test case where t1 can be optimized but not t2 -explain (costs off) select t1.*,t2.x,t2.z +explain (costs off) select t1.*,t2.x,t2.z,count(*) from t1 inner join t2 on t1.a = t2.x and t1.b = t2.y group by t1.a,t1.b,t1.c,t1.d,t2.x,t2.z; QUERY PLAN @@ -1682,7 +1682,7 @@ explain (costs off) select * from t3 group by a,b,c; create temp table t1c () inherits (t1); -- Ensure we don't remove any columns when t1 has a child table -explain (costs off) select * from t1 group by a,b,c,d; +explain (costs off) select count(*) from t1 group by a,b,c,d; QUERY PLAN ------------------------------------- HashAggregate @@ -1693,7 +1693,7 @@ explain (costs off) select * from t1 group by a,b,c,d; (5 rows) -- Okay to remove columns if we're only querying the parent. -explain (costs off) select * from only t1 group by a,b,c,d; +explain (costs off) select count(*) from only t1 group by a,b,c,d; QUERY PLAN ---------------------- HashAggregate @@ -1711,7 +1711,7 @@ create temp table p_t1 ( create temp table p_t1_1 partition of p_t1 for values in(1); create temp table p_t1_2 partition of p_t1 for values in(2); -- Ensure we can remove non-PK columns for partitioned tables. -explain (costs off) select * from p_t1 group by a,b,c,d; +explain (costs off) select count(*) from p_t1 group by a,b,c,d; QUERY PLAN -------------------------------- HashAggregate @@ -1724,7 +1724,7 @@ explain (costs off) select * from p_t1 group by a,b,c,d; create unique index t2_z_uidx on t2(z); -- Ensure we don't remove any columns from the GROUP BY for a unique -- index on a NULLable column. -explain (costs off) select y,z from t2 group by y,z; +explain (costs off) select y,z,count(*) from t2 group by y,z; QUERY PLAN ---------------------- HashAggregate @@ -1734,7 +1734,7 @@ explain (costs off) select y,z from t2 group by y,z; -- Make the column NOT NULL and ensure we remove the redundant column alter table t2 alter column z set not null; -explain (costs off) select y,z from t2 group by y,z; +explain (costs off) select y,z,count(*) from t2 group by y,z; QUERY PLAN ---------------------- HashAggregate @@ -1745,7 +1745,7 @@ explain (costs off) select y,z from t2 group by y,z; -- When there are multiple supporting unique indexes and the GROUP BY contains -- columns to cover all of those, ensure we pick the index with the least -- number of columns so that we can remove more columns from the GROUP BY. -explain (costs off) select x,y,z from t2 group by x,y,z; +explain (costs off) select x,y,z,count(*) from t2 group by x,y,z; QUERY PLAN ---------------------- HashAggregate @@ -1755,7 +1755,7 @@ explain (costs off) select x,y,z from t2 group by x,y,z; -- As above but try ordering the columns differently to ensure we get the -- same result. -explain (costs off) select x,y,z from t2 group by z,x,y; +explain (costs off) select x,y,z,count(*) from t2 group by z,x,y; QUERY PLAN ---------------------- HashAggregate @@ -1766,7 +1766,7 @@ explain (costs off) select x,y,z from t2 group by z,x,y; -- Ensure we don't use a partial index as proof of functional dependency drop index t2_z_uidx; create index t2_z_uidx on t2 (z) where z > 0; -explain (costs off) select y,z from t2 group by y,z; +explain (costs off) select y,z,count(*) from t2 group by y,z; QUERY PLAN ---------------------- HashAggregate @@ -1780,7 +1780,7 @@ explain (costs off) select y,z from t2 group by y,z; drop index t2_z_uidx; alter table t2 alter column z drop not null; create unique index t2_z_uidx on t2(z) nulls not distinct; -explain (costs off) select y,z from t2 group by y,z; +explain (costs off) select y,z,count(*) from t2 group by y,z; QUERY PLAN ---------------------- HashAggregate diff --git a/src/test/regress/expected/join.out b/src/test/regress/expected/join.out index 526430a5ea0..7e6f921bfd7 100644 --- a/src/test/regress/expected/join.out +++ b/src/test/regress/expected/join.out @@ -8681,23 +8681,21 @@ select d.* from d left join (select distinct * from b) s (1 row) -- join removal is not possible when the GROUP BY contains a column that is --- not in the join condition. (Note: as of 9.6, we notice that b.id is a --- primary key and so drop b.c_id from the GROUP BY of the resulting plan; --- but this happens too late for join removal in the outer plan level.) +-- not in the join condition. (Note: we notice that b.id is a primary key and +-- so skip the grouping step altogether in the resulting plan; but this happens +-- too late for join removal in the outer plan level.) explain (costs off) select d.* from d left join (select * from b group by b.id, b.c_id) s on d.a = s.id; - QUERY PLAN ------------------------------------------- + QUERY PLAN +------------------------------------ Merge Right Join Merge Cond: (b.id = d.a) - -> Group - Group Key: b.id - -> Index Scan using b_pkey on b + -> Index Scan using b_pkey on b -> Sort Sort Key: d.a -> Seq Scan on d -(8 rows) +(6 rows) -- join removal is not possible when the GROUP BY contains non-empty grouping -- sets or multiple empty grouping sets @@ -8752,18 +8750,15 @@ select d.* from d left join (select 1 as x from b group by grouping sets((), gro explain (costs off) select d.* from d left join (select distinct * from b) s on d.a = s.id; - QUERY PLAN --------------------------------------- + QUERY PLAN +------------------------------------ Merge Right Join Merge Cond: (b.id = d.a) - -> Unique - -> Sort - Sort Key: b.id, b.c_id - -> Seq Scan on b + -> Index Scan using b_pkey on b -> Sort Sort Key: d.a -> Seq Scan on d -(9 rows) +(6 rows) -- join removal is not possible when the subquery has DISTINCT ON and a -- set-returning function that is not a DISTINCT ON column diff --git a/src/test/regress/expected/uniquekeys.out b/src/test/regress/expected/uniquekeys.out index 00ab2b1904c..317f54a1f75 100644 --- a/src/test/regress/expected/uniquekeys.out +++ b/src/test/regress/expected/uniquekeys.out @@ -155,4 +155,181 @@ select * from uk_a a left join reset enable_hashjoin; reset enable_mergejoin; -drop table uk_a, uk_b, uk_c, uk_d; +-- +-- Test skipping DISTINCT and GROUP BY steps when the input is already +-- distinct +-- +create table uk_n (id int primary key, u int unique, v int, w int); +create unique index on uk_n (v) nulls not distinct; +-- a single relation with its key among the DISTINCT columns +explain (costs off) +select distinct id, x from uk_a; + QUERY PLAN +------------------ + Seq Scan on uk_a +(1 row) + +-- but not without the key +explain (costs off) +select distinct x, y from uk_a; + QUERY PLAN +------------------------ + HashAggregate + Group Key: x, y + -> Seq Scan on uk_a +(3 rows) + +-- a key column that is equal to a constant is not needed +explain (costs off) +select distinct b, x from uk_d where a = 1; + QUERY PLAN +-------------------------------------- + Bitmap Heap Scan on uk_d + Recheck Cond: (a = 1) + -> Bitmap Index Scan on uk_d_pkey + Index Cond: (a = 1) +(4 rows) + +-- a join needs the keys of all its relations +explain (costs off) +select distinct a.id, b.id from uk_a a join uk_b b on a.x = b.x; + QUERY PLAN +-------------------------------- + Merge Join + Merge Cond: (a.x = b.x) + -> Sort + Sort Key: a.x + -> Seq Scan on uk_a a + -> Sort + Sort Key: b.x + -> Seq Scan on uk_b b +(8 rows) + +explain (costs off) +select distinct a.id, b.x from uk_a a join uk_b b on a.x = b.x; + QUERY PLAN +-------------------------------------- + HashAggregate + Group Key: a.id, b.x + -> Merge Join + Merge Cond: (a.x = b.x) + -> Sort + Sort Key: a.x + -> Seq Scan on uk_a a + -> Sort + Sort Key: b.x + -> Seq Scan on uk_b b +(10 rows) + +-- unless the join clauses supply them +explain (costs off) +select distinct a.id, a.y from uk_a a join uk_b b on a.x = b.id; + QUERY PLAN +-------------------------------- + Hash Join + Hash Cond: (a.x = b.id) + -> Seq Scan on uk_a a + -> Hash + -> Seq Scan on uk_b b +(5 rows) + +-- a unique index on a nullable column allows duplicate NULLs +explain (costs off) +select distinct u from uk_n; + QUERY PLAN +------------------------ + HashAggregate + Group Key: u + -> Seq Scan on uk_n +(3 rows) + +-- unless a strict restriction clause filters them out +explain (costs off) +select distinct u from uk_n where u > 0; + QUERY PLAN +--------------------------------------- + Bitmap Heap Scan on uk_n + Recheck Cond: (u > 0) + -> Bitmap Index Scan on uk_n_u_key + Index Cond: (u > 0) +(4 rows) + +-- or the index is NULLS NOT DISTINCT +explain (costs off) +select distinct v from uk_n; + QUERY PLAN +------------------ + Seq Scan on uk_n +(1 row) + +-- a column that a join clause equates to something cannot be NULL +explain (costs off) +select distinct a.id, n.w from uk_a a join uk_n n on a.x = n.u; + QUERY PLAN +-------------------------------- + Hash Join + Hash Cond: (a.x = n.u) + -> Seq Scan on uk_a a + -> Hash + -> Seq Scan on uk_n n +(5 rows) + +-- but that takes a join clause on each column of the key +explain (costs off) +select distinct a.id, n.u from uk_a a join uk_n n on a.x = n.w; + QUERY PLAN +-------------------------------------- + HashAggregate + Group Key: a.id, n.u + -> Merge Join + Merge Cond: (n.w = a.x) + -> Sort + Sort Key: n.w + -> Seq Scan on uk_n n + -> Sort + Sort Key: a.x + -> Seq Scan on uk_a a +(10 rows) + +-- GROUP BY without aggregates is handled likewise +explain (costs off) +select a.id, a.y from uk_a a join uk_b b on a.x = b.id group by a.id, a.y; + QUERY PLAN +-------------------------------- + Hash Join + Hash Cond: (a.x = b.id) + -> Seq Scan on uk_a a + -> Hash + -> Seq Scan on uk_b b +(5 rows) + +-- but aggregates need the grouping step +explain (costs off) +select id, count(*) from uk_a group by id; + QUERY PLAN +------------------------ + HashAggregate + Group Key: id + -> Seq Scan on uk_a +(3 rows) + +-- as does DISTINCT ON, and set-returning functions in the targetlist +explain (costs off) +select distinct on (id) id, x from uk_a; + QUERY PLAN +------------------------------------------ + Unique + -> Index Scan using uk_a_pkey on uk_a +(2 rows) + +explain (costs off) +select distinct id, generate_series(1, 2) from uk_a; + QUERY PLAN +---------------------------------------- + HashAggregate + Group Key: id, generate_series(1, 2) + -> ProjectSet + -> Seq Scan on uk_a +(4 rows) + +drop table uk_a, uk_b, uk_c, uk_d, uk_n; diff --git a/src/test/regress/sql/aggregates.sql b/src/test/regress/sql/aggregates.sql index 2bbb7668dd4..32ba7479345 100644 --- a/src/test/regress/sql/aggregates.sql +++ b/src/test/regress/sql/aggregates.sql @@ -563,18 +563,18 @@ create temp table t2 (x int, y int, z int, primary key (x, y)); create temp table t3 (a int, b int, c int, primary key(a, b) deferrable); -- Non-primary-key columns can be removed from GROUP BY -explain (costs off) select * from t1 group by a,b,c,d; +explain (costs off) select count(*) from t1 group by a,b,c,d; -- No removal can happen if the complete PK is not present in GROUP BY explain (costs off) select a,c from t1 group by a,c,d; -- Test removal across multiple relations -explain (costs off) select * +explain (costs off) select count(*) from t1 inner join t2 on t1.a = t2.x and t1.b = t2.y group by t1.a,t1.b,t1.c,t1.d,t2.x,t2.y,t2.z; -- Test case where t1 can be optimized but not t2 -explain (costs off) select t1.*,t2.x,t2.z +explain (costs off) select t1.*,t2.x,t2.z,count(*) from t1 inner join t2 on t1.a = t2.x and t1.b = t2.y group by t1.a,t1.b,t1.c,t1.d,t2.x,t2.z; @@ -584,10 +584,10 @@ explain (costs off) select * from t3 group by a,b,c; create temp table t1c () inherits (t1); -- Ensure we don't remove any columns when t1 has a child table -explain (costs off) select * from t1 group by a,b,c,d; +explain (costs off) select count(*) from t1 group by a,b,c,d; -- Okay to remove columns if we're only querying the parent. -explain (costs off) select * from only t1 group by a,b,c,d; +explain (costs off) select count(*) from only t1 group by a,b,c,d; create temp table p_t1 ( a int, @@ -600,31 +600,31 @@ create temp table p_t1_1 partition of p_t1 for values in(1); create temp table p_t1_2 partition of p_t1 for values in(2); -- Ensure we can remove non-PK columns for partitioned tables. -explain (costs off) select * from p_t1 group by a,b,c,d; +explain (costs off) select count(*) from p_t1 group by a,b,c,d; create unique index t2_z_uidx on t2(z); -- Ensure we don't remove any columns from the GROUP BY for a unique -- index on a NULLable column. -explain (costs off) select y,z from t2 group by y,z; +explain (costs off) select y,z,count(*) from t2 group by y,z; -- Make the column NOT NULL and ensure we remove the redundant column alter table t2 alter column z set not null; -explain (costs off) select y,z from t2 group by y,z; +explain (costs off) select y,z,count(*) from t2 group by y,z; -- When there are multiple supporting unique indexes and the GROUP BY contains -- columns to cover all of those, ensure we pick the index with the least -- number of columns so that we can remove more columns from the GROUP BY. -explain (costs off) select x,y,z from t2 group by x,y,z; +explain (costs off) select x,y,z,count(*) from t2 group by x,y,z; -- As above but try ordering the columns differently to ensure we get the -- same result. -explain (costs off) select x,y,z from t2 group by z,x,y; +explain (costs off) select x,y,z,count(*) from t2 group by z,x,y; -- Ensure we don't use a partial index as proof of functional dependency drop index t2_z_uidx; create index t2_z_uidx on t2 (z) where z > 0; -explain (costs off) select y,z from t2 group by y,z; +explain (costs off) select y,z,count(*) from t2 group by y,z; -- A unique index defined as NULLS NOT DISTINCT does not need a supporting NOT -- NULL constraint on the indexed columns. Ensure the redundant columns are @@ -632,7 +632,7 @@ explain (costs off) select y,z from t2 group by y,z; drop index t2_z_uidx; alter table t2 alter column z drop not null; create unique index t2_z_uidx on t2(z) nulls not distinct; -explain (costs off) select y,z from t2 group by y,z; +explain (costs off) select y,z,count(*) from t2 group by y,z; -- But not if the table is nullable by an outer join, as null-extended rows -- would be conflated with the row having a NULL in the indexed column. diff --git a/src/test/regress/sql/join.sql b/src/test/regress/sql/join.sql index 27b44016717..c518c91b4e3 100644 --- a/src/test/regress/sql/join.sql +++ b/src/test/regress/sql/join.sql @@ -3198,9 +3198,9 @@ select d.* from d left join (select distinct * from b) s on d.a = s.id and d.b = s.c_id; -- join removal is not possible when the GROUP BY contains a column that is --- not in the join condition. (Note: as of 9.6, we notice that b.id is a --- primary key and so drop b.c_id from the GROUP BY of the resulting plan; --- but this happens too late for join removal in the outer plan level.) +-- not in the join condition. (Note: we notice that b.id is a primary key and +-- so skip the grouping step altogether in the resulting plan; but this happens +-- too late for join removal in the outer plan level.) explain (costs off) select d.* from d left join (select * from b group by b.id, b.c_id) s on d.a = s.id; diff --git a/src/test/regress/sql/uniquekeys.sql b/src/test/regress/sql/uniquekeys.sql index 0b211dffed3..5c0c04b16fb 100644 --- a/src/test/regress/sql/uniquekeys.sql +++ b/src/test/regress/sql/uniquekeys.sql @@ -50,4 +50,70 @@ select * from uk_a a left join reset enable_hashjoin; reset enable_mergejoin; -drop table uk_a, uk_b, uk_c, uk_d; +-- +-- Test skipping DISTINCT and GROUP BY steps when the input is already +-- distinct +-- + +create table uk_n (id int primary key, u int unique, v int, w int); +create unique index on uk_n (v) nulls not distinct; + +-- a single relation with its key among the DISTINCT columns +explain (costs off) +select distinct id, x from uk_a; + +-- but not without the key +explain (costs off) +select distinct x, y from uk_a; + +-- a key column that is equal to a constant is not needed +explain (costs off) +select distinct b, x from uk_d where a = 1; + +-- a join needs the keys of all its relations +explain (costs off) +select distinct a.id, b.id from uk_a a join uk_b b on a.x = b.x; + +explain (costs off) +select distinct a.id, b.x from uk_a a join uk_b b on a.x = b.x; + +-- unless the join clauses supply them +explain (costs off) +select distinct a.id, a.y from uk_a a join uk_b b on a.x = b.id; + +-- a unique index on a nullable column allows duplicate NULLs +explain (costs off) +select distinct u from uk_n; + +-- unless a strict restriction clause filters them out +explain (costs off) +select distinct u from uk_n where u > 0; + +-- or the index is NULLS NOT DISTINCT +explain (costs off) +select distinct v from uk_n; + +-- a column that a join clause equates to something cannot be NULL +explain (costs off) +select distinct a.id, n.w from uk_a a join uk_n n on a.x = n.u; + +-- but that takes a join clause on each column of the key +explain (costs off) +select distinct a.id, n.u from uk_a a join uk_n n on a.x = n.w; + +-- GROUP BY without aggregates is handled likewise +explain (costs off) +select a.id, a.y from uk_a a join uk_b b on a.x = b.id group by a.id, a.y; + +-- but aggregates need the grouping step +explain (costs off) +select id, count(*) from uk_a group by id; + +-- as does DISTINCT ON, and set-returning functions in the targetlist +explain (costs off) +select distinct on (id) id, x from uk_a; + +explain (costs off) +select distinct id, generate_series(1, 2) from uk_a; + +drop table uk_a, uk_b, uk_c, uk_d, uk_n; -- 2.37.1 (Apple Git-137.1)