From cd7438a7e05492bf257f74d4458af00db561acac Mon Sep 17 00:00:00 2001 From: Richard Guo Date: Mon, 16 Nov 2026 11:52:53 +0900 Subject: [PATCH v5 1/3] Detect unique inner sides of joins when the inner side is a join The planner marks a join as inner_unique when each outer row can match at most one inner row, which lets the executor stop scanning the inner side after the first match. Until now this could only be proven when the inner side was a single base relation. This commit introduces UniqueKeys to make such proofs possible for join relations. A UniqueKey is a set of EquivalenceClasses that no two rows of a base relation agree on. For a plain table they are derived from its unique, immediately enforced, non-partial indexes, the first time they are needed. UniqueKeys are kept for base relations only. A join relation would typically have many of them, most of which would never be used. And since they would be derived from the join's input relations, which of them are found would depend on the order in which the joins are performed. Instead, whether a join relation is unique for a set of join clauses is worked out when the question comes up, from the UniqueKeys of its base relations. For example, in SELECT * FROM a LEFT JOIN (b JOIN c ON b.x = c.id) ON a.x = b.id; with unique indexes on b.id and c.id, the join of b and c is now detected as unique for a: the clause fixes b.id, which leaves one row of b, which fixes b.x and hence c.id, which leaves one row of c. For now this handles only join relations that consist of plain tables and involve no outer joins, semijoins or antijoins. UniqueKeys say nothing about rows containing NULLs in the key columns, which is good enough here because the join clauses in question are mergejoinable, and such clauses are assumed to be strict. Since join relations can now have entries in their unique_for_rels and non_unique_for_rels caches, allocate those entries in the same memory context as the relation they belong to, rather than always in the planner_cxt. Otherwise the entries of join relations built by GEQO would outlive the relations. --- src/backend/optimizer/README | 58 +++ src/backend/optimizer/path/Makefile | 3 +- src/backend/optimizer/path/equivclass.c | 7 +- src/backend/optimizer/path/meson.build | 1 + src/backend/optimizer/path/uniquekeys.c | 413 ++++++++++++++++++++++ src/backend/optimizer/plan/analyzejoins.c | 23 +- src/backend/optimizer/util/relnode.c | 4 + src/include/nodes/pathnodes.h | 50 ++- src/include/optimizer/paths.h | 9 + src/test/regress/expected/uniquekeys.out | 158 +++++++++ src/test/regress/parallel_schedule | 2 +- src/test/regress/sql/uniquekeys.sql | 53 +++ src/tools/pgindent/typedefs.list | 1 + 13 files changed, 771 insertions(+), 11 deletions(-) create mode 100644 src/backend/optimizer/path/uniquekeys.c create mode 100644 src/test/regress/expected/uniquekeys.out create mode 100644 src/test/regress/sql/uniquekeys.sql diff --git a/src/backend/optimizer/README b/src/backend/optimizer/README index 73d0b7536ac..3b247de7572 100644 --- a/src/backend/optimizer/README +++ b/src/backend/optimizer/README @@ -1022,6 +1022,64 @@ ugly ad-hoc code to recognize that in limited contexts, but discarding constant ECs from pathkeys makes it happen cleanly and automatically. +UniqueKeys +---------- + +The UniqueKeys data structure represents what is known about the uniqueness +of the rows of a base relation. Each UniqueKey is a set of +EquivalenceClasses, and it says that the relation cannot have two rows with +equal values in all those ECs. A unique index on (a, b) thus gives a +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. + +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. + +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 +they would be derived from the join's input relations, which of them are +found would depend on the order in which the joins are performed. Instead, +whether a join relation is unique for a given set of join clauses is worked +out when the question comes up (see joinrel_is_distinct_for), as follows. +A join emits each combination of rows of its base relations at most once, +so it suffices to show that a row on the other side of the clauses leaves +only one possible row for each base relation. Start with the ECs that the +clauses equate to the other side; their values are fixed. Then repeat +these steps until nothing changes: + + If a base relation has a UniqueKey all of whose ECs are fixed, it + has only one possible row. + + If a base relation has only one possible row, every EC that has a + member belonging to that relation is fixed. + +The join relation is unique if every base relation ends up with only one +possible row. For example, given + + SELECT ... FROM a, b, c WHERE a.x = b.id AND b.y = c.id AND c.z = a.w + +with unique indexes on b.id and c.id, the join of b and c is unique for the +clause a.x = b.id: the EC {a.x, b.id} is fixed, which leaves one row of b, +which fixes the EC {b.y, c.id}, which leaves one row of c. It is not +unique for the clause c.z = a.w, since that fixes only the EC {c.z, a.w}, +and neither relation has a UniqueKey within that. + +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. + + Order of processing for EquivalenceClasses and PathKeys ------------------------------------------------------- diff --git a/src/backend/optimizer/path/Makefile b/src/backend/optimizer/path/Makefile index 1e199ff66f7..7b9820c25f3 100644 --- a/src/backend/optimizer/path/Makefile +++ b/src/backend/optimizer/path/Makefile @@ -21,6 +21,7 @@ OBJS = \ joinpath.o \ joinrels.o \ pathkeys.o \ - tidpath.o + tidpath.o \ + uniquekeys.o include $(top_srcdir)/src/backend/common.mk diff --git a/src/backend/optimizer/path/equivclass.c b/src/backend/optimizer/path/equivclass.c index 439cb651c01..315dbea2675 100644 --- a/src/backend/optimizer/path/equivclass.c +++ b/src/backend/optimizer/path/equivclass.c @@ -864,6 +864,7 @@ get_eclass_for_sort_expr(PlannerInfo *root, } } + newec->ec_index = list_length(root->eq_classes); root->eq_classes = lappend(root->eq_classes, newec); /* @@ -872,7 +873,6 @@ get_eclass_for_sort_expr(PlannerInfo *root, */ if (root->ec_merging_done) { - int ec_index = list_length(root->eq_classes) - 1; int i = -1; while ((i = bms_next_member(newec->ec_relids, i)) > 0) @@ -892,7 +892,7 @@ get_eclass_for_sort_expr(PlannerInfo *root, Assert(rel->reloptkind == RELOPT_BASEREL); rel->eclass_indexes = bms_add_member(rel->eclass_indexes, - ec_index); + newec->ec_index); } } @@ -1214,6 +1214,9 @@ generate_base_implied_equalities(PlannerInfo *root) Assert(ec->ec_merged == NULL); /* else shouldn't be in list */ Assert(!ec->ec_broken); /* not yet anyway... */ + /* The EC's position in the list is final now, so remember it */ + ec->ec_index = ec_index; + /* * Generate implied equalities that are restriction clauses. * Single-member ECs won't generate any deductions, either here or at diff --git a/src/backend/optimizer/path/meson.build b/src/backend/optimizer/path/meson.build index 98f3ebd5192..7d480656ce6 100644 --- a/src/backend/optimizer/path/meson.build +++ b/src/backend/optimizer/path/meson.build @@ -10,4 +10,5 @@ backend_sources += files( 'joinrels.c', 'pathkeys.c', 'tidpath.c', + 'uniquekeys.c', ) diff --git a/src/backend/optimizer/path/uniquekeys.c b/src/backend/optimizer/path/uniquekeys.c new file mode 100644 index 00000000000..55ef7acc1c3 --- /dev/null +++ b/src/backend/optimizer/path/uniquekeys.c @@ -0,0 +1,413 @@ +/*------------------------------------------------------------------------- + * + * uniquekeys.c + * Utilities for proving relations distinct using UniqueKeys + * + * A UniqueKey is a set of EquivalenceClasses that no two rows of a base + * relation agree on. Plain relations get them from their unique indexes. + * Join relations have no UniqueKeys of their own: whether a join relation is + * distinct is worked out when needed, from the UniqueKeys of the base + * relations it consists of. + * + * See src/backend/optimizer/README for more information. + * + * + * Copyright (c) 2026, PostgreSQL Global Development Group + * + * IDENTIFICATION + * src/backend/optimizer/path/uniquekeys.c + * + *------------------------------------------------------------------------- + */ +#include "postgres.h" + +#include "optimizer/pathnode.h" +#include "optimizer/paths.h" +#include "utils/lsyscache.h" + +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 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); +static Bitmapset *get_own_eclass_indexes(PlannerInfo *root, RelOptInfo *rel); + + +/* + * joinrel_supports_distinctness + * Could the join relation possibly be proven distinct? + * + * This is a pre-checking function for joinrel_is_distinct_for(), in the same + * way as rel_supports_distinctness() is for rel_is_distinct_for(). + */ +bool +joinrel_supports_distinctness(PlannerInfo *root, RelOptInfo *joinrel) +{ + Assert(IS_JOIN_REL(joinrel)); + + /* A grouped join rel's rows are not combinations of base rel rows */ + if (IS_GROUPED_REL(joinrel)) + return false; + + /* + * We don't handle join rels that include outer joins. A left join pushed + * down per outer-join identity 3 is missed here, which is harmless since + * its clauses are in no EC. + */ + if (bms_overlap(joinrel->relids, root->outer_join_rels)) + return false; + + build_base_rel_uniquekeys(root); + + /* Every base rel in the join must have a UniqueKey */ + return !bms_overlap(joinrel->relids, root->no_uniquekey_relids); +} + +/* + * joinrel_is_distinct_for + * Does the join relation return only distinct rows according to + * clause_list? + * + * clause_list is as for rel_is_distinct_for(): each clause is a mergejoinable + * equality with an expression of this relation on one side, as identified by + * 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; + + foreach_node(RestrictInfo, rinfo, clause_list) + { + EquivalenceClass *ec; + + /* caller identified the side belonging to this relation for us */ + ec = rinfo->outer_is_left ? rinfo->right_ec : rinfo->left_ec; + if (ec == NULL) + continue; + + /* the cached EC might be non-canonical, so chase up to the top */ + while (ec->ec_merged) + ec = ec->ec_merged; + + known_ecs = bms_add_member(known_ecs, ec->ec_index); + } + + /* There are no outer join relids, so all the relids are base relations */ + remaining = bms_copy(joinrel->relids); + + do + { + int relid = -1; + + found = false; + + while ((relid = bms_next_member(remaining, relid)) >= 0) + { + RelOptInfo *rel = find_base_rel(root, relid); + + if (!rel_has_uniquekey_in(root, rel, known_ecs)) + continue; + + remaining = bms_del_member(remaining, relid); + known_ecs = bms_add_members(known_ecs, + get_own_eclass_indexes(root, rel)); + found = true; + } + } while (found && remaining != NULL); + + return remaining == NULL; +} + +/* + * build_base_rel_uniquekeys + * Build the UniqueKeys of all base relations, if not already done. + * + * We do this the first time UniqueKeys are needed, so that queries that + * never need them don't pay for it. Also set root->no_uniquekey_relids. + */ +static void +build_base_rel_uniquekeys(PlannerInfo *root) +{ + MemoryContext oldcontext; + Relids no_uniquekey_relids = NULL; + int rti; + + if (root->uniquekeys_built) + return; + + /* UniqueKeys refer to ECs by index, so the EC list must be final */ + Assert(root->ec_merging_done); + + /* Be sure to keep the results in the planner_cxt even if within GEQO */ + oldcontext = MemoryContextSwitchTo(root->planner_cxt); + + for (rti = 1; rti < root->simple_rel_array_size; rti++) + { + RelOptInfo *rel = root->simple_rel_array[rti]; + + /* there may be empty slots corresponding to non-baserel RTEs */ + if (rel == NULL) + continue; + + /* Currently we only know about plain relations */ + if (rel->reloptkind == RELOPT_BASEREL && + rel->rtekind == RTE_RELATION) + build_plain_rel_uniquekeys(root, rel); + + if (rel->uniquekeys == NIL) + no_uniquekey_relids = bms_add_member(no_uniquekey_relids, rti); + } + + /* + * We don't handle semijoins and antijoins. Rather than checking for them + * in each join relation, treat their right-hand relations as having no + * UniqueKeys. + */ + foreach_node(SpecialJoinInfo, sjinfo, root->join_info_list) + { + if (sjinfo->jointype == JOIN_SEMI || sjinfo->jointype == JOIN_ANTI) + no_uniquekey_relids = bms_add_members(no_uniquekey_relids, + sjinfo->syn_righthand); + } + + root->no_uniquekey_relids = no_uniquekey_relids; + root->uniquekeys_built = true; + + MemoryContextSwitchTo(oldcontext); +} + +/* + * build_plain_rel_uniquekeys + * Build UniqueKeys for a plain relation from its unique indexes. + * + * An index column that is in no EC cannot be matched by any clause, so an + * index having such a column yields no UniqueKey. + */ +static void +build_plain_rel_uniquekeys(PlannerInfo *root, RelOptInfo *rel) +{ + /* Short-circuit if no EC mentions this relation */ + if (rel->eclass_indexes == NULL) + return; + + foreach_node(IndexOptInfo, ind, rel->indexlist) + { + Bitmapset *key_ecs = NULL; + int c; + + /* + * If the index is not unique, or not immediately enforced, or if it's + * a partial index, it's useless here. + */ + if (!ind->unique || !ind->immediate || ind->indpred != NIL) + continue; + + for (c = 0; c < ind->nkeycolumns; c++) + { + TargetEntry *tle = list_nth_node(TargetEntry, ind->indextlist, c); + EquivalenceClass *ec; + Oid eq_op; + int ec_index; + + /* + * An EC's opfamily list comes from its equality operator, which + * can belong to more opfamilies than just the index's. So look + * up the index column's equality operator to compare with. + */ + eq_op = get_opfamily_member_for_cmptype(ind->opfamily[c], + ind->opcintype[c], + ind->opcintype[c], + COMPARE_EQ); + if (!OidIsValid(eq_op)) + break; + + ec_index = find_ec_index(root, rel, tle->expr, + get_mergejoin_opfamilies(eq_op), + ind->indexcollations[c]); + if (ec_index < 0) + break; + + /* + * A column that is equal to a constant has the same value in all + * the relation's rows, so the remaining columns are unique + * without it. + */ + ec = list_nth_node(EquivalenceClass, root->eq_classes, ec_index); + if (ec->ec_has_const) + continue; + + key_ecs = bms_add_member(key_ecs, ec_index); + } + + /* Add the UniqueKey if we matched all the index columns */ + if (c == ind->nkeycolumns) + add_uniquekey(rel, key_ecs); + } +} + +/* + * add_uniquekey + * 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. + */ +static void +add_uniquekey(RelOptInfo *rel, Bitmapset *eclass_indexes) +{ + UniqueKey *ukey; + ListCell *lc; + + foreach(lc, rel->uniquekeys) + { + 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)) + return; + + /* Remove any existing key that the new one makes redundant */ + if (bms_is_subset(eclass_indexes, other->eclass_indexes)) + rel->uniquekeys = foreach_delete_current(rel->uniquekeys, lc); + } + + ukey = makeNode(UniqueKey); + ukey->eclass_indexes = eclass_indexes; + rel->uniquekeys = lappend(rel->uniquekeys, ukey); +} + +/* + * find_ec_index + * Find the EC that has the given expression of the given base relation + * as a member, and that uses the given opfamilies and collation. + * + * Returns the EC's index in root->eq_classes, or -1 if there is none. + */ +static int +find_ec_index(PlannerInfo *root, RelOptInfo *rel, Expr *expr, + List *opfamilies, Oid collation) +{ + int i = -1; + + while ((i = bms_next_member(rel->eclass_indexes, i)) >= 0) + { + EquivalenceClass *ec = list_nth_node(EquivalenceClass, + root->eq_classes, i); + + /* The members of a broken EC are not known to be equal */ + if (ec->ec_broken || ec->ec_has_volatile) + continue; + + if (ec->ec_collation != collation || + !equal(ec->ec_opfamilies, opfamilies)) + continue; + + if (find_ec_member_matching_expr(ec, expr, NULL)) + return i; + } + + return -1; +} + +/* + * rel_has_uniquekey_in + * Does the relation have a UniqueKey that is a subset of the given set + * of ECs? + */ +static bool +rel_has_uniquekey_in(PlannerInfo *root, RelOptInfo *rel, + Bitmapset *eclass_indexes) +{ + foreach_node(UniqueKey, ukey, rel->uniquekeys) + { + bool broken = false; + int i = -1; + + if (!bms_is_subset(ukey->eclass_indexes, eclass_indexes)) + continue; + + /* An EC can be found to be broken after the key was built */ + while ((i = bms_next_member(ukey->eclass_indexes, i)) >= 0) + { + EquivalenceClass *ec = list_nth_node(EquivalenceClass, + root->eq_classes, i); + + if (ec->ec_broken) + { + broken = true; + break; + } + } + + if (!broken) + return true; + } + + return false; +} + +/* + * get_own_eclass_indexes + * Get the indexes of the ECs that have a member belonging to the given + * base relation alone. + * + * This is not the same as rel->eclass_indexes, which also includes ECs whose + * members only use the relation together with other relations. + * + * The result is cached in the RelOptInfo. An empty result is not cached, + * but that is cheap to recompute. + */ +static Bitmapset * +get_own_eclass_indexes(PlannerInfo *root, RelOptInfo *rel) +{ + MemoryContext oldcontext; + Bitmapset *result = NULL; + int i = -1; + + if (rel->own_eclass_indexes != NULL) + return rel->own_eclass_indexes; + + /* Be sure to keep the result in the planner_cxt even if within GEQO */ + oldcontext = MemoryContextSwitchTo(root->planner_cxt); + + while ((i = bms_next_member(rel->eclass_indexes, i)) >= 0) + { + EquivalenceClass *ec = list_nth_node(EquivalenceClass, + root->eq_classes, i); + + foreach_node(EquivalenceMember, em, ec->ec_members) + { + if (bms_equal(em->em_relids, rel->relids)) + { + result = bms_add_member(result, i); + break; + } + } + } + + MemoryContextSwitchTo(oldcontext); + + rel->own_eclass_indexes = result; + + return result; +} diff --git a/src/backend/optimizer/plan/analyzejoins.c b/src/backend/optimizer/plan/analyzejoins.c index 25a863efbde..2eda06ec7ce 100644 --- a/src/backend/optimizer/plan/analyzejoins.c +++ b/src/backend/optimizer/plan/analyzejoins.c @@ -40,6 +40,7 @@ #include "parser/parse_relation.h" #include "rewrite/rewriteManip.h" #include "utils/lsyscache.h" +#include "utils/memutils.h" /* * Utility structure. A sorting procedure is needed to simplify the search @@ -601,7 +602,10 @@ reduce_semijoin_in_jointree(Node *jtnode, Relids syn_righthand) static bool rel_supports_distinctness(PlannerInfo *root, RelOptInfo *rel) { - /* We only know about baserels ... */ + /* For a join relation, the proof has to come from UniqueKeys */ + if (IS_JOIN_REL(rel)) + return joinrel_supports_distinctness(root, rel); + /* Otherwise we only know about baserels ... */ if (rel->reloptkind != RELOPT_BASEREL) return false; if (rel->rtekind == RTE_RELATION) @@ -662,6 +666,15 @@ static bool rel_is_distinct_for(PlannerInfo *root, RelOptInfo *rel, List *clause_list, List **extra_clauses) { + /* + * For a join relation, see if UniqueKeys can prove it. We can't supply + * extra_clauses that way, but only self-join checks ask for those, and + * they are only made on baserels. + */ + if (IS_JOIN_REL(rel)) + return extra_clauses == NULL && + joinrel_is_distinct_for(root, rel, clause_list); + /* * We could skip a couple of tests here if we assume all callers checked * rel_supports_distinctness first, but it doesn't seem worth taking any @@ -1104,7 +1117,9 @@ innerrel_is_unique_ext(PlannerInfo *root, { /* * Cache the positive result for future probes, being sure to keep it - * in the planner_cxt even if we are working in GEQO. + * in the same context as the innerrel. For a baserel that is the + * planner_cxt even if we are working in GEQO, while a joinrel built + * within GEQO is short-lived, and so should its cache entries be. * * Note: one might consider trying to isolate the minimal subset of * the outerrels that proved the innerrel unique. But it's not worth @@ -1112,7 +1127,7 @@ innerrel_is_unique_ext(PlannerInfo *root, * and so we'll see the minimally sufficient outerrels before any * supersets of them anyway. */ - old_context = MemoryContextSwitchTo(root->planner_cxt); + old_context = MemoryContextSwitchTo(GetMemoryChunkContext(innerrel)); uniqueRelInfo = makeNode(UniqueRelInfo); uniqueRelInfo->outerrelids = bms_copy(outerrelids); uniqueRelInfo->self_join = self_join; @@ -1143,7 +1158,7 @@ innerrel_is_unique_ext(PlannerInfo *root, */ if (force_cache || root->assumeReplanning) { - old_context = MemoryContextSwitchTo(root->planner_cxt); + old_context = MemoryContextSwitchTo(GetMemoryChunkContext(innerrel)); innerrel->non_unique_for_rels = lappend(innerrel->non_unique_for_rels, bms_copy(outerrelids)); diff --git a/src/backend/optimizer/util/relnode.c b/src/backend/optimizer/util/relnode.c index 90a687c32da..850098ff4bc 100644 --- a/src/backend/optimizer/util/relnode.c +++ b/src/backend/optimizer/util/relnode.c @@ -292,6 +292,8 @@ build_simple_rel(PlannerInfo *root, int relid, RelOptInfo *parent) rel->fdw_private = NULL; rel->unique_for_rels = NIL; rel->non_unique_for_rels = NIL; + rel->uniquekeys = NIL; + rel->own_eclass_indexes = NULL; rel->unique_rel = NULL; rel->unique_pathkeys = NIL; rel->unique_groupclause = NIL; @@ -913,6 +915,8 @@ build_join_rel(PlannerInfo *root, joinrel->fdw_private = NULL; joinrel->unique_for_rels = NIL; joinrel->non_unique_for_rels = NIL; + joinrel->uniquekeys = NIL; + joinrel->own_eclass_indexes = NULL; joinrel->unique_rel = NULL; joinrel->unique_pathkeys = NIL; joinrel->unique_groupclause = NIL; diff --git a/src/include/nodes/pathnodes.h b/src/include/nodes/pathnodes.h index 2ba1dbfb67f..a672e6b5eab 100644 --- a/src/include/nodes/pathnodes.h +++ b/src/include/nodes/pathnodes.h @@ -438,6 +438,12 @@ struct PlannerInfo /* set true once ECs are canonical */ bool ec_merging_done; + /* set true once the base relations' UniqueKeys have been built */ + bool uniquekeys_built; + + /* base relations that keep a join relation from being proven distinct */ + Relids no_uniquekey_relids; + /* list of "canonical" PathKeys */ List *canon_pathkeys; @@ -868,9 +874,7 @@ typedef struct PartitionSchemeData *PartitionScheme; * Two fields are used to cache knowledge acquired during the join search * about whether this rel is provably unique when being joined to given other * relation(s), ie, it can have at most one row matching any given row from - * that join relation. Currently we only attempt such proofs, and thus only - * populate these fields, for base rels; but someday they might be used for - * join rels too: + * that join relation. These are used for both base rels and join rels: * * unique_for_rels - list of UniqueRelInfo, each one being a set of other * rels for which this one has been proven unique @@ -878,6 +882,15 @@ typedef struct PartitionSchemeData *PartitionScheme; * other rels for which we have tried and failed to prove * this one unique * + * Two fields hold the information that such proofs for join rels are based + * on (see uniquekeys.c). They are only used for base rels: + * + * uniquekeys - list of UniqueKeys, each one being a set of + * EquivalenceClasses that no two rows of this rel agree on + * (filled when first needed) + * own_eclass_indexes - EquivalenceClasses that have a member belonging + * to this rel alone (filled when first needed) + * * Three fields are used to cache information about unique-ification of this * relation. This is used to support semijoins where the relation appears on * the RHS: the relation is first unique-ified, and then a regular join is @@ -1128,6 +1141,14 @@ typedef struct RelOptInfo /* known not unique for these set(s) */ List *non_unique_for_rels; + /* + * information for proving join relations unique + */ + /* sets of ECs that no two rows of this rel agree on */ + List *uniquekeys; + /* indexes in root->eq_classes of ECs with a member of this rel alone */ + Bitmapset *own_eclass_indexes; + /* * information about unique-ification of this relation */ @@ -1674,6 +1695,8 @@ typedef struct EquivalenceClass bool ec_has_const; /* any pseudoconstants in ec_members? */ bool ec_has_volatile; /* the (sole) member is a volatile expr */ bool ec_broken; /* failed to generate needed clauses? */ + int ec_index; /* index in root->eq_classes, valid once EC + * merging is done */ Index ec_sortref; /* originating sortclause label, or 0 */ Index ec_min_security; /* minimum security_level in ec_sources */ Index ec_max_security; /* maximum security_level in ec_sources */ @@ -3856,6 +3879,27 @@ typedef struct AggTransInfo bool initValueIsNull; } AggTransInfo; +/* + * UniqueKey + * + * A UniqueKey represents the knowledge that no two rows of a base relation + * have equal values in all the EquivalenceClasses of a certain set. Each EC + * 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. + */ +typedef struct UniqueKey +{ + pg_node_attr(no_copy_equal, no_read, no_query_jumble) + + NodeTag type; + + /* indexes in PlannerInfo's eq_classes list of the ECs in the key */ + Bitmapset *eclass_indexes; +} UniqueKey; + /* * UniqueRelInfo caches a fact that a relation is unique when being joined * to other relation(s). diff --git a/src/include/optimizer/paths.h b/src/include/optimizer/paths.h index 64f87da1674..78685679c75 100644 --- a/src/include/optimizer/paths.h +++ b/src/include/optimizer/paths.h @@ -211,6 +211,15 @@ extern bool is_redundant_derived_clause(RestrictInfo *rinfo, List *clauselist); extern bool is_redundant_with_indexclauses(RestrictInfo *rinfo, List *indexclauses); +/* + * uniquekeys.c + * utilities for proving relations distinct using UniqueKeys + */ +extern bool joinrel_supports_distinctness(PlannerInfo *root, + RelOptInfo *joinrel); +extern bool joinrel_is_distinct_for(PlannerInfo *root, RelOptInfo *joinrel, + List *clause_list); + /* * pathkeys.c * utilities for matching and building path keys diff --git a/src/test/regress/expected/uniquekeys.out b/src/test/regress/expected/uniquekeys.out new file mode 100644 index 00000000000..00ab2b1904c --- /dev/null +++ b/src/test/regress/expected/uniquekeys.out @@ -0,0 +1,158 @@ +-- +-- UNIQUEKEYS +-- +create table uk_a (id int primary key, x int, y int); +create table uk_b (id int primary key, x int, y int); +create table uk_c (id int primary key, x int, y int); +create table uk_d (a int, b int, x int, primary key (a, b)); +-- +-- Test proving a join relation unique from the UniqueKeys of its base +-- relations. We use a left join to make the join relation of interest the +-- inner side, and look at whether that join is marked as "Inner Unique". +-- +set enable_hashjoin = off; +set enable_mergejoin = off; +-- the clause leaves one row of uk_b, which leaves one row of uk_c +explain (verbose, costs off) +select * from uk_a a left join + (uk_b b join uk_c c on b.x = c.id) on a.x = b.id; + QUERY PLAN +---------------------------------------------------------- + Nested Loop Left Join + Output: a.id, a.x, a.y, b.id, b.x, b.y, c.id, c.x, c.y + Inner Unique: true + -> Seq Scan on public.uk_a a + Output: a.id, a.x, a.y + -> Nested Loop + Output: b.id, b.x, b.y, c.id, c.x, c.y + Inner Unique: true + -> Index Scan using uk_b_pkey on public.uk_b b + Output: b.id, b.x, b.y + Index Cond: (b.id = a.x) + -> Index Scan using uk_c_pkey on public.uk_c c + Output: c.id, c.x, c.y + Index Cond: (c.id = b.x) +(14 rows) + +-- the clause leaves one row of uk_c, but that doesn't limit uk_b +explain (verbose, costs off) +select * from uk_a a left join + (uk_b b join uk_c c on b.x = c.id) on a.x = c.id; + QUERY PLAN +--------------------------------------------------------------- + Nested Loop Left Join + Output: a.id, a.x, a.y, b.id, b.x, b.y, c.id, c.x, c.y + Join Filter: (a.x = c.id) + -> Seq Scan on public.uk_a a + Output: a.id, a.x, a.y + -> Materialize + Output: b.id, b.x, b.y, c.id, c.x, c.y + -> Nested Loop + Output: b.id, b.x, b.y, c.id, c.x, c.y + Inner Unique: true + -> Seq Scan on public.uk_b b + Output: b.id, b.x, b.y + -> Index Scan using uk_c_pkey on public.uk_c c + Output: c.id, c.x, c.y + Index Cond: (c.id = b.x) +(15 rows) + +-- each relation has a clause of its own +explain (verbose, costs off) +select * from uk_a a left join + (uk_b b cross join uk_c c) on a.x = b.id and a.y = c.id; + QUERY PLAN +---------------------------------------------------------- + Nested Loop Left Join + Output: a.id, a.x, a.y, b.id, b.x, b.y, c.id, c.x, c.y + Inner Unique: true + -> Seq Scan on public.uk_a a + Output: a.id, a.x, a.y + -> Nested Loop + Output: b.id, b.x, b.y, c.id, c.x, c.y + -> Index Scan using uk_b_pkey on public.uk_b b + Output: b.id, b.x, b.y + Index Cond: (b.id = a.x) + -> Index Scan using uk_c_pkey on public.uk_c c + Output: c.id, c.x, c.y + Index Cond: (c.id = a.y) +(13 rows) + +-- but a clause for only one of them is not enough +explain (verbose, costs off) +select * from uk_a a left join + (uk_b b cross join uk_c c) on a.x = b.id; + QUERY PLAN +---------------------------------------------------------- + Nested Loop Left Join + Output: a.id, a.x, a.y, b.id, b.x, b.y, c.id, c.x, c.y + -> Seq Scan on public.uk_a a + Output: a.id, a.x, a.y + -> Nested Loop + Output: b.id, b.x, b.y, c.id, c.x, c.y + -> Index Scan using uk_b_pkey on public.uk_b b + Output: b.id, b.x, b.y + Index Cond: (b.id = a.x) + -> Seq Scan on public.uk_c c + Output: c.id, c.x, c.y +(11 rows) + +-- a key column that is equal to a constant is not needed +explain (verbose, costs off) +select * from uk_a a left join + (uk_d d join uk_c c on d.x = c.id) on a.x = d.b and d.a = 1; + QUERY PLAN +--------------------------------------------------------------- + Nested Loop Left Join + Output: a.id, a.x, a.y, d.a, d.b, d.x, c.id, c.x, c.y + Inner Unique: true + Join Filter: (a.x = d.b) + -> Seq Scan on public.uk_a a + Output: a.id, a.x, a.y + -> Materialize + Output: d.a, d.b, d.x, c.id, c.x, c.y + -> Nested Loop + Output: d.a, d.b, d.x, c.id, c.x, c.y + Inner Unique: true + -> Bitmap Heap Scan on public.uk_d d + Output: d.a, d.b, d.x + Recheck Cond: (d.a = 1) + -> Bitmap Index Scan on uk_d_pkey + Index Cond: (d.a = 1) + -> Index Scan using uk_c_pkey on public.uk_c c + Output: c.id, c.x, c.y + Index Cond: (c.id = d.x) +(19 rows) + +-- a chain of three relations, the last one with a two-column key +explain (verbose, costs off) +select * from uk_a a left join + (uk_b b join uk_c c on b.x = c.id join uk_d d on c.x = d.a and c.y = d.b) + on a.x = b.id; + QUERY PLAN +------------------------------------------------------------------------- + Nested Loop Left Join + Output: a.id, a.x, a.y, b.id, b.x, b.y, c.id, c.x, c.y, d.a, d.b, d.x + Inner Unique: true + -> Seq Scan on public.uk_a a + Output: a.id, a.x, a.y + -> Nested Loop + Output: b.id, b.x, b.y, c.id, c.x, c.y, d.a, d.b, d.x + Inner Unique: true + -> Nested Loop + Output: b.id, b.x, b.y, c.id, c.x, c.y + Inner Unique: true + -> Index Scan using uk_b_pkey on public.uk_b b + Output: b.id, b.x, b.y + Index Cond: (b.id = a.x) + -> Index Scan using uk_c_pkey on public.uk_c c + Output: c.id, c.x, c.y + Index Cond: (c.id = b.x) + -> Index Scan using uk_d_pkey on public.uk_d d + Output: d.a, d.b, d.x + Index Cond: ((d.a = c.x) AND (d.b = c.y)) +(20 rows) + +reset enable_hashjoin; +reset enable_mergejoin; +drop table uk_a, uk_b, uk_c, uk_d; diff --git a/src/test/regress/parallel_schedule b/src/test/regress/parallel_schedule index bb13a7b6b8f..773e106f755 100644 --- a/src/test/regress/parallel_schedule +++ b/src/test/regress/parallel_schedule @@ -81,7 +81,7 @@ test: create_table_like alter_generic alter_operator misc async dbsize merge mis # collate.linux.utf8 and collate.icu.utf8 tests cannot be run in parallel with each other # psql depends on create_am # amutils depends on geometry, create_index_spgist, hash_index, brin -test: rules psql psql_crosstab psql_pipeline amutils stats_ext collate.linux.utf8 collate.windows.win1252 +test: rules psql psql_crosstab psql_pipeline amutils stats_ext collate.linux.utf8 collate.windows.win1252 uniquekeys # ---------- # Run these alone so they don't run out of parallel workers diff --git a/src/test/regress/sql/uniquekeys.sql b/src/test/regress/sql/uniquekeys.sql new file mode 100644 index 00000000000..0b211dffed3 --- /dev/null +++ b/src/test/regress/sql/uniquekeys.sql @@ -0,0 +1,53 @@ +-- +-- UNIQUEKEYS +-- + +create table uk_a (id int primary key, x int, y int); +create table uk_b (id int primary key, x int, y int); +create table uk_c (id int primary key, x int, y int); +create table uk_d (a int, b int, x int, primary key (a, b)); + +-- +-- Test proving a join relation unique from the UniqueKeys of its base +-- relations. We use a left join to make the join relation of interest the +-- inner side, and look at whether that join is marked as "Inner Unique". +-- + +set enable_hashjoin = off; +set enable_mergejoin = off; + +-- the clause leaves one row of uk_b, which leaves one row of uk_c +explain (verbose, costs off) +select * from uk_a a left join + (uk_b b join uk_c c on b.x = c.id) on a.x = b.id; + +-- the clause leaves one row of uk_c, but that doesn't limit uk_b +explain (verbose, costs off) +select * from uk_a a left join + (uk_b b join uk_c c on b.x = c.id) on a.x = c.id; + +-- each relation has a clause of its own +explain (verbose, costs off) +select * from uk_a a left join + (uk_b b cross join uk_c c) on a.x = b.id and a.y = c.id; + +-- but a clause for only one of them is not enough +explain (verbose, costs off) +select * from uk_a a left join + (uk_b b cross join uk_c c) on a.x = b.id; + +-- a key column that is equal to a constant is not needed +explain (verbose, costs off) +select * from uk_a a left join + (uk_d d join uk_c c on d.x = c.id) on a.x = d.b and d.a = 1; + +-- a chain of three relations, the last one with a two-column key +explain (verbose, costs off) +select * from uk_a a left join + (uk_b b join uk_c c on b.x = c.id join uk_d d on c.x = d.a and c.y = d.b) + on a.x = b.id; + +reset enable_hashjoin; +reset enable_mergejoin; + +drop table uk_a, uk_b, uk_c, uk_d; diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list index f10eb469266..18128a91779 100644 --- a/src/tools/pgindent/typedefs.list +++ b/src/tools/pgindent/typedefs.list @@ -3317,6 +3317,7 @@ UVersionInfo UnicodeNormalizationForm UnicodeNormalizationQC Unique +UniqueKey UniquePath UniqueRelInfo UniqueState -- 2.37.1 (Apple Git-137.1)