Skip to content

Commit bf9ee29

Browse files
author
Richard Guo
committed
Simplify relation_has_unique_index_for()
Now that the only call to relation_has_unique_index_for() that supplied an exprlist and oprlist has been removed, the loop handling those lists is effectively dead code. This patch removes that loop and simplifies the function accordingly. Author: Richard Guo <guofenglinux@gmail.com> Discussion: https://postgr.es/m/CAMbWs4-EBnaRvEs7frTLbsXiweSTUXifsteF-d3rvv01FKO86w@mail.gmail.com
1 parent 24225ad commit bf9ee29

File tree

3 files changed

+17
-78
lines changed

3 files changed

+17
-78
lines changed

src/backend/optimizer/path/indxpath.c

Lines changed: 14 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -4142,47 +4142,26 @@ ec_member_matches_indexcol(PlannerInfo *root, RelOptInfo *rel,
41424142
* a set of equality conditions, because the conditions constrain all
41434143
* columns of some unique index.
41444144
*
4145-
* The conditions can be represented in either or both of two ways:
4146-
* 1. A list of RestrictInfo nodes, where the caller has already determined
4147-
* that each condition is a mergejoinable equality with an expression in
4148-
* this relation on one side, and an expression not involving this relation
4149-
* on the other. The transient outer_is_left flag is used to identify which
4150-
* side we should look at: left side if outer_is_left is false, right side
4151-
* if it is true.
4152-
* 2. A list of expressions in this relation, and a corresponding list of
4153-
* equality operators. The caller must have already checked that the operators
4154-
* represent equality. (Note: the operators could be cross-type; the
4155-
* expressions should correspond to their RHS inputs.)
4145+
* The conditions are provided as a list of RestrictInfo nodes, where the
4146+
* caller has already determined that each condition is a mergejoinable
4147+
* equality with an expression in this relation on one side, and an
4148+
* expression not involving this relation on the other. The transient
4149+
* outer_is_left flag is used to identify which side we should look at:
4150+
* left side if outer_is_left is false, right side if it is true.
41564151
*
41574152
* The caller need only supply equality conditions arising from joins;
41584153
* this routine automatically adds in any usable baserestrictinfo clauses.
41594154
* (Note that the passed-in restrictlist will be destructively modified!)
4155+
*
4156+
* If extra_clauses isn't NULL, return baserestrictinfo clauses which were used
4157+
* to derive uniqueness.
41604158
*/
41614159
bool
41624160
relation_has_unique_index_for(PlannerInfo *root, RelOptInfo *rel,
4163-
List *restrictlist,
4164-
List *exprlist, List *oprlist)
4165-
{
4166-
return relation_has_unique_index_ext(root, rel, restrictlist,
4167-
exprlist, oprlist, NULL);
4168-
}
4169-
4170-
/*
4171-
* relation_has_unique_index_ext
4172-
* Same as relation_has_unique_index_for(), but supports extra_clauses
4173-
* parameter. If extra_clauses isn't NULL, return baserestrictinfo clauses
4174-
* which were used to derive uniqueness.
4175-
*/
4176-
bool
4177-
relation_has_unique_index_ext(PlannerInfo *root, RelOptInfo *rel,
4178-
List *restrictlist,
4179-
List *exprlist, List *oprlist,
4180-
List **extra_clauses)
4161+
List *restrictlist, List **extra_clauses)
41814162
{
41824163
ListCell *ic;
41834164

4184-
Assert(list_length(exprlist) == list_length(oprlist));
4185-
41864165
/* Short-circuit if no indexes... */
41874166
if (rel->indexlist == NIL)
41884167
return false;
@@ -4225,7 +4204,7 @@ relation_has_unique_index_ext(PlannerInfo *root, RelOptInfo *rel,
42254204
}
42264205

42274206
/* Short-circuit the easy case */
4228-
if (restrictlist == NIL && exprlist == NIL)
4207+
if (restrictlist == NIL)
42294208
return false;
42304209

42314210
/* Examine each index of the relation ... */
@@ -4247,14 +4226,12 @@ relation_has_unique_index_ext(PlannerInfo *root, RelOptInfo *rel,
42474226
continue;
42484227

42494228
/*
4250-
* Try to find each index column in the lists of conditions. This is
4229+
* Try to find each index column in the list of conditions. This is
42514230
* O(N^2) or worse, but we expect all the lists to be short.
42524231
*/
42534232
for (c = 0; c < ind->nkeycolumns; c++)
42544233
{
4255-
bool matched = false;
42564234
ListCell *lc;
4257-
ListCell *lc2;
42584235

42594236
foreach(lc, restrictlist)
42604237
{
@@ -4284,8 +4261,6 @@ relation_has_unique_index_ext(PlannerInfo *root, RelOptInfo *rel,
42844261

42854262
if (match_index_to_operand(rexpr, c, ind))
42864263
{
4287-
matched = true; /* column is unique */
4288-
42894264
if (bms_membership(rinfo->clause_relids) == BMS_SINGLETON)
42904265
{
42914266
MemoryContext oldMemCtx =
@@ -4303,43 +4278,11 @@ relation_has_unique_index_ext(PlannerInfo *root, RelOptInfo *rel,
43034278
MemoryContextSwitchTo(oldMemCtx);
43044279
}
43054280

4306-
break;
4281+
break; /* found a match; column is unique */
43074282
}
43084283
}
43094284

4310-
if (matched)
4311-
continue;
4312-
4313-
forboth(lc, exprlist, lc2, oprlist)
4314-
{
4315-
Node *expr = (Node *) lfirst(lc);
4316-
Oid opr = lfirst_oid(lc2);
4317-
4318-
/* See if the expression matches the index key */
4319-
if (!match_index_to_operand(expr, c, ind))
4320-
continue;
4321-
4322-
/*
4323-
* The equality operator must be a member of the index
4324-
* opfamily, else it is not asserting the right kind of
4325-
* equality behavior for this index. We assume the caller
4326-
* determined it is an equality operator, so we don't need to
4327-
* check any more tightly than this.
4328-
*/
4329-
if (!op_in_opfamily(opr, ind->opfamily[c]))
4330-
continue;
4331-
4332-
/*
4333-
* XXX at some point we may need to check collations here too.
4334-
* For the moment we assume all collations reduce to the same
4335-
* notion of equality.
4336-
*/
4337-
4338-
matched = true; /* column is unique */
4339-
break;
4340-
}
4341-
4342-
if (!matched)
4285+
if (lc == NULL)
43434286
break; /* no match; this index doesn't help us */
43444287
}
43454288

src/backend/optimizer/plan/analyzejoins.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -990,11 +990,10 @@ rel_is_distinct_for(PlannerInfo *root, RelOptInfo *rel, List *clause_list,
990990
{
991991
/*
992992
* Examine the indexes to see if we have a matching unique index.
993-
* relation_has_unique_index_ext automatically adds any usable
993+
* relation_has_unique_index_for automatically adds any usable
994994
* restriction clauses for the rel, so we needn't do that here.
995995
*/
996-
if (relation_has_unique_index_ext(root, rel, clause_list, NIL, NIL,
997-
extra_clauses))
996+
if (relation_has_unique_index_for(root, rel, clause_list, extra_clauses))
998997
return true;
999998
}
1000999
else if (rel->rtekind == RTE_SUBQUERY)

src/include/optimizer/paths.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,7 @@ extern void generate_partitionwise_join_paths(PlannerInfo *root,
7171
extern void create_index_paths(PlannerInfo *root, RelOptInfo *rel);
7272
extern bool relation_has_unique_index_for(PlannerInfo *root, RelOptInfo *rel,
7373
List *restrictlist,
74-
List *exprlist, List *oprlist);
75-
extern bool relation_has_unique_index_ext(PlannerInfo *root, RelOptInfo *rel,
76-
List *restrictlist, List *exprlist,
77-
List *oprlist, List **extra_clauses);
74+
List **extra_clauses);
7875
extern bool indexcol_is_bool_constant_for_query(PlannerInfo *root,
7976
IndexOptInfo *index,
8077
int indexcol);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy