Skip to content

Commit 89a3684

Browse files
committed
Further cleanup of indxpath logic related to IndexOptInfo.opfamily array.
We no longer need the terminating zero entry in opfamily[], so get rid of it. Also replace assorted ad-hoc looping logic with simple for and foreach constructs. This code is now noticeably more readable than it was an hour ago; credit to Robert for seeing that it could be simplified.
1 parent 99bc012 commit 89a3684

File tree

3 files changed

+28
-38
lines changed

3 files changed

+28
-38
lines changed

src/backend/optimizer/path/indxpath.c

Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1047,14 +1047,14 @@ group_clauses_by_indexkey(IndexOptInfo *index,
10471047
{
10481048
List *clausegroup_list = NIL;
10491049
bool found_outer_clause = false;
1050-
int indexcol = 0;
1050+
int indexcol;
10511051

10521052
*found_clause = false; /* default result */
10531053

10541054
if (clauses == NIL && outer_clauses == NIL)
10551055
return NIL; /* cannot succeed */
10561056

1057-
do
1057+
for (indexcol = 0; indexcol < index->ncolumns; indexcol++)
10581058
{
10591059
List *clausegroup = NIL;
10601060
ListCell *l;
@@ -1102,10 +1102,7 @@ group_clauses_by_indexkey(IndexOptInfo *index,
11021102
return NIL;
11031103

11041104
clausegroup_list = lappend(clausegroup_list, clausegroup);
1105-
1106-
indexcol++;
1107-
1108-
} while (indexcol < index->ncolumns);
1105+
}
11091106

11101107
if (!*found_clause && !found_outer_clause)
11111108
return NIL; /* no indexable clauses anywhere */
@@ -1163,8 +1160,8 @@ group_clauses_by_indexkey(IndexOptInfo *index,
11631160
*
11641161
* 'index' is the index of interest.
11651162
* 'indexcol' is a column number of 'index' (counting from 0).
1166-
* 'opfamily' is the corresponding operator family.
11671163
* 'rinfo' is the clause to be tested (as a RestrictInfo node).
1164+
* 'outer_relids' lists rels whose Vars can be considered pseudoconstant.
11681165
* 'saop_control' indicates whether ScalarArrayOpExpr clauses can be used.
11691166
*
11701167
* Returns true if the clause can be used with this index key.
@@ -1180,12 +1177,12 @@ match_clause_to_indexcol(IndexOptInfo *index,
11801177
SaOpControl saop_control)
11811178
{
11821179
Expr *clause = rinfo->clause;
1180+
Oid opfamily = index->opfamily[indexcol];
11831181
Node *leftop,
11841182
*rightop;
11851183
Relids left_relids;
11861184
Relids right_relids;
11871185
Oid expr_op;
1188-
Oid opfamily = index->opfamily[indexcol];
11891186
bool plain_op;
11901187

11911188
/*
@@ -1571,19 +1568,17 @@ matches_any_index(RestrictInfo *rinfo, RelOptInfo *rel, Relids outer_relids)
15711568
foreach(l, rel->indexlist)
15721569
{
15731570
IndexOptInfo *index = (IndexOptInfo *) lfirst(l);
1574-
int indexcol = 0;
1571+
int indexcol;
15751572

1576-
do
1573+
for (indexcol = 0; indexcol < index->ncolumns; indexcol++)
15771574
{
15781575
if (match_clause_to_indexcol(index,
15791576
indexcol,
15801577
rinfo,
15811578
outer_relids,
15821579
SAOP_ALLOW))
15831580
return true;
1584-
1585-
indexcol++;
1586-
} while (indexcol < index->ncolumns);
1581+
}
15871582
}
15881583

15891584
return false;
@@ -1605,9 +1600,9 @@ eclass_matches_any_index(EquivalenceClass *ec, EquivalenceMember *em,
16051600
foreach(l, rel->indexlist)
16061601
{
16071602
IndexOptInfo *index = (IndexOptInfo *) lfirst(l);
1608-
int indexcol = 0;
1603+
int indexcol;
16091604

1610-
do
1605+
for (indexcol = 0; indexcol < index->ncolumns; indexcol++)
16111606
{
16121607
Oid curFamily = index->opfamily[indexcol];
16131608

@@ -1625,9 +1620,7 @@ eclass_matches_any_index(EquivalenceClass *ec, EquivalenceMember *em,
16251620
list_member_oid(ec->ec_opfamilies, curFamily)) &&
16261621
match_index_to_operand((Node *) em->em_expr, indexcol, index))
16271622
return true;
1628-
1629-
indexcol++;
1630-
} while (indexcol < index->ncolumns);
1623+
}
16311624
}
16321625

16331626
return false;
@@ -2360,21 +2353,25 @@ List *
23602353
expand_indexqual_conditions(IndexOptInfo *index, List *clausegroups)
23612354
{
23622355
List *resultquals = NIL;
2363-
ListCell *clausegroup_item;
2364-
int indexcol = 0;
2356+
ListCell *lc;
2357+
int indexcol;
23652358

23662359
if (clausegroups == NIL)
23672360
return NIL;
23682361

2369-
clausegroup_item = list_head(clausegroups);
2370-
do
2362+
/* clausegroups must correspond to index columns */
2363+
Assert(list_length(clausegroups) <= index->ncolumns);
2364+
2365+
indexcol = 0;
2366+
foreach(lc, clausegroups)
23712367
{
2368+
List *clausegroup = (List *) lfirst(lc);
23722369
Oid curFamily = index->opfamily[indexcol];
2373-
ListCell *l;
2370+
ListCell *lc2;
23742371

2375-
foreach(l, (List *) lfirst(clausegroup_item))
2372+
foreach(lc2, clausegroup)
23762373
{
2377-
RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
2374+
RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc2);
23782375
Expr *clause = rinfo->clause;
23792376

23802377
/* First check for boolean cases */
@@ -2426,12 +2423,8 @@ expand_indexqual_conditions(IndexOptInfo *index, List *clausegroups)
24262423
(int) nodeTag(clause));
24272424
}
24282425

2429-
clausegroup_item = lnext(clausegroup_item);
2430-
24312426
indexcol++;
2432-
} while (clausegroup_item != NULL && indexcol < index->ncolumns);
2433-
2434-
Assert(clausegroup_item == NULL); /* else more groups than indexkeys */
2427+
}
24352428

24362429
return resultquals;
24372430
}

src/backend/optimizer/util/plancat.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -192,13 +192,13 @@ get_relation_info(PlannerInfo *root, Oid relationObjectId, bool inhparent,
192192

193193
/*
194194
* Allocate per-column info arrays. To save a few palloc cycles
195-
* we allocate all the Oid-type arrays in one request. Note that
196-
* the opfamily array needs an extra, terminating zero at the end.
197-
* We pre-zero the ordering info in case the index is unordered.
195+
* we allocate all the Oid-type arrays in one request. We must
196+
* pre-zero the sortop and nulls_first arrays in case the index is
197+
* unordered.
198198
*/
199199
info->indexkeys = (int *) palloc(sizeof(int) * ncolumns);
200-
info->opfamily = (Oid *) palloc0(sizeof(Oid) * (4 * ncolumns + 1));
201-
info->opcintype = info->opfamily + (ncolumns + 1);
200+
info->opfamily = (Oid *) palloc0(sizeof(Oid) * (4 * ncolumns));
201+
info->opcintype = info->opfamily + ncolumns;
202202
info->fwdsortop = info->opcintype + ncolumns;
203203
info->revsortop = info->fwdsortop + ncolumns;
204204
info->nulls_first = (bool *) palloc0(sizeof(bool) * ncolumns);

src/include/nodes/relation.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -427,9 +427,6 @@ typedef struct RelOptInfo
427427
*
428428
* opfamily[], indexkeys[], opcintype[], fwdsortop[], revsortop[],
429429
* and nulls_first[] each have ncolumns entries.
430-
* Note: for historical reasons, the opfamily array has an extra entry
431-
* that is always zero. Some code scans until it sees a zero entry,
432-
* rather than looking at ncolumns.
433430
*
434431
* Zeroes in the indexkeys[] array indicate index columns that are
435432
* expressions; there is one element in indexprs for each such column.

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