1
0
mirror of https://github.com/postgres/postgres.git synced 2025-11-07 19:06:32 +03:00

Reimplement the linked list data structure used throughout the backend.

In the past, we used a 'Lispy' linked list implementation: a "list" was
merely a pointer to the head node of the list. The problem with that
design is that it makes lappend() and length() linear time. This patch
fixes that problem (and others) by maintaining a count of the list
length and a pointer to the tail node along with each head node pointer.
A "list" is now a pointer to a structure containing some meta-data
about the list; the head and tail pointers in that structure refer
to ListCell structures that maintain the actual linked list of nodes.

The function names of the list API have also been changed to, I hope,
be more logically consistent. By default, the old function names are
still available; they will be disabled-by-default once the rest of
the tree has been updated to use the new API names.
This commit is contained in:
Neil Conway
2004-05-26 04:41:50 +00:00
parent 18d0d10563
commit d0b4399d81
125 changed files with 3639 additions and 3038 deletions

View File

@@ -16,7 +16,7 @@
*
*
* IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/optimizer/prep/prepjointree.c,v 1.17 2004/05/10 22:44:45 tgl Exp $
* $PostgreSQL: pgsql/src/backend/optimizer/prep/prepjointree.c,v 1.18 2004/05/26 04:41:26 neilc Exp $
*
*-------------------------------------------------------------------------
*/
@@ -97,11 +97,11 @@ pull_up_IN_clauses(Query *parse, Node *node)
if (and_clause(node))
{
List *newclauses = NIL;
List *oldclauses;
ListCell *l;
foreach(oldclauses, ((BoolExpr *) node)->args)
foreach(l, ((BoolExpr *) node)->args)
{
Node *oldclause = lfirst(oldclauses);
Node *oldclause = (Node *) lfirst(l);
newclauses = lappend(newclauses,
pull_up_IN_clauses(parse,
@@ -162,7 +162,7 @@ pull_up_subqueries(Query *parse, Node *jtnode, bool below_outer_join)
{
int rtoffset;
List *subtlist;
List *rt;
ListCell *rt;
/*
* Need a modifiable copy of the subquery to hack on. Even if
@@ -314,7 +314,7 @@ pull_up_subqueries(Query *parse, Node *jtnode, bool below_outer_join)
else if (IsA(jtnode, FromExpr))
{
FromExpr *f = (FromExpr *) jtnode;
List *l;
ListCell *l;
foreach(l, f->fromlist)
lfirst(l) = pull_up_subqueries(parse, lfirst(l),
@@ -447,7 +447,7 @@ is_simple_subquery(Query *subquery)
static bool
has_nullable_targetlist(Query *subquery)
{
List *l;
ListCell *l;
foreach(l, subquery->targetList)
{
@@ -488,7 +488,7 @@ resolvenew_in_jointree(Node *jtnode, int varno,
else if (IsA(jtnode, FromExpr))
{
FromExpr *f = (FromExpr *) jtnode;
List *l;
ListCell *l;
foreach(l, f->fromlist)
resolvenew_in_jointree(lfirst(l), varno, rte, subtlist);
@@ -588,7 +588,7 @@ reduce_outer_joins_pass1(Node *jtnode)
else if (IsA(jtnode, FromExpr))
{
FromExpr *f = (FromExpr *) jtnode;
List *l;
ListCell *l;
foreach(l, f->fromlist)
{
@@ -653,8 +653,8 @@ reduce_outer_joins_pass2(Node *jtnode,
else if (IsA(jtnode, FromExpr))
{
FromExpr *f = (FromExpr *) jtnode;
List *l;
List *s;
ListCell *l;
ListCell *s;
Relids pass_nonnullable;
/* Scan quals to see if we can add any nonnullability constraints */
@@ -662,15 +662,14 @@ reduce_outer_joins_pass2(Node *jtnode,
pass_nonnullable = bms_add_members(pass_nonnullable,
nonnullable_rels);
/* And recurse --- but only into interesting subtrees */
s = state->sub_states;
foreach(l, f->fromlist)
Assert(length(f->fromlist) == length(state->sub_states));
forboth(l, f->fromlist, s, state->sub_states)
{
reduce_outer_joins_state *sub_state = lfirst(s);
if (sub_state->contains_outer)
reduce_outer_joins_pass2(lfirst(l), sub_state, parse,
pass_nonnullable);
s = lnext(s);
}
bms_free(pass_nonnullable);
}
@@ -679,7 +678,7 @@ reduce_outer_joins_pass2(Node *jtnode,
JoinExpr *j = (JoinExpr *) jtnode;
int rtindex = j->rtindex;
JoinType jointype = j->jointype;
reduce_outer_joins_state *left_state = lfirst(state->sub_states);
reduce_outer_joins_state *left_state = linitial(state->sub_states);
reduce_outer_joins_state *right_state = lsecond(state->sub_states);
/* Can we simplify this join? */
@@ -798,7 +797,7 @@ find_nonnullable_rels(Node *node, bool top_level)
}
else if (IsA(node, List))
{
List *l;
ListCell *l;
foreach(l, (List *) node)
{
@@ -898,7 +897,7 @@ simplify_jointree(Query *parse, Node *jtnode)
{
FromExpr *f = (FromExpr *) jtnode;
List *newlist = NIL;
List *l;
ListCell *l;
foreach(l, f->fromlist)
{
@@ -918,7 +917,16 @@ simplify_jointree(Query *parse, Node *jtnode)
*/
FromExpr *subf = (FromExpr *) child;
int childlen = length(subf->fromlist);
int myothers = length(newlist) + length(lnext(l));
int myothers;
ListCell *l2;
/*
* XXX: This is a quick hack, not sure of the proper
* fix.
*/
myothers = length(newlist);
for_each_cell(l2, lnext(l))
myothers++;
if (childlen <= 1 ||
(childlen + myothers) <= from_collapse_limit)
@@ -1022,7 +1030,7 @@ simplify_jointree(Query *parse, Node *jtnode)
static void
fix_in_clause_relids(List *in_info_list, int varno, Relids subrelids)
{
List *l;
ListCell *l;
foreach(l, in_info_list)
{
@@ -1060,7 +1068,7 @@ get_relids_in_jointree(Node *jtnode)
else if (IsA(jtnode, FromExpr))
{
FromExpr *f = (FromExpr *) jtnode;
List *l;
ListCell *l;
foreach(l, f->fromlist)
{
@@ -1119,7 +1127,7 @@ find_jointree_node_for_rel(Node *jtnode, int relid)
else if (IsA(jtnode, FromExpr))
{
FromExpr *f = (FromExpr *) jtnode;
List *l;
ListCell *l;
foreach(l, f->fromlist)
{

View File

@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/optimizer/prep/prepqual.c,v 1.41 2003/12/30 21:49:19 tgl Exp $
* $PostgreSQL: pgsql/src/backend/optimizer/prep/prepqual.c,v 1.42 2004/05/26 04:41:26 neilc Exp $
*
*-------------------------------------------------------------------------
*/
@@ -144,7 +144,7 @@ flatten_andors_mutator(Node *node, void *context)
static void
flatten_andors_and_walker(FastList *out_list, List *andlist)
{
List *arg;
ListCell *arg;
foreach(arg, andlist)
{
@@ -160,7 +160,7 @@ flatten_andors_and_walker(FastList *out_list, List *andlist)
static void
flatten_andors_or_walker(FastList *out_list, List *orlist)
{
List *arg;
ListCell *arg;
foreach(arg, orlist)
{
@@ -193,7 +193,7 @@ pull_ands(List *andlist)
static void
pull_ands_walker(FastList *out_list, List *andlist)
{
List *arg;
ListCell *arg;
foreach(arg, andlist)
{
@@ -226,7 +226,7 @@ pull_ors(List *orlist)
static void
pull_ors_walker(FastList *out_list, List *orlist)
{
List *arg;
ListCell *arg;
foreach(arg, orlist)
{
@@ -258,7 +258,7 @@ find_nots(Expr *qual)
if (and_clause((Node *) qual))
{
FastList t_list;
List *temp;
ListCell *temp;
FastListInit(&t_list);
foreach(temp, ((BoolExpr *) qual)->args)
@@ -268,7 +268,7 @@ find_nots(Expr *qual)
else if (or_clause((Node *) qual))
{
FastList t_list;
List *temp;
ListCell *temp;
FastListInit(&t_list);
foreach(temp, ((BoolExpr *) qual)->args)
@@ -324,7 +324,7 @@ push_nots(Expr *qual)
*--------------------
*/
FastList t_list;
List *temp;
ListCell *temp;
FastListInit(&t_list);
foreach(temp, ((BoolExpr *) qual)->args)
@@ -334,7 +334,7 @@ push_nots(Expr *qual)
else if (or_clause((Node *) qual))
{
FastList t_list;
List *temp;
ListCell *temp;
FastListInit(&t_list);
foreach(temp, ((BoolExpr *) qual)->args)
@@ -398,7 +398,7 @@ find_duplicate_ors(Expr *qual)
if (or_clause((Node *) qual))
{
List *orlist = NIL;
List *temp;
ListCell *temp;
/* Recurse */
foreach(temp, ((BoolExpr *) qual)->args)
@@ -412,7 +412,7 @@ find_duplicate_ors(Expr *qual)
else if (and_clause((Node *) qual))
{
List *andlist = NIL;
List *temp;
ListCell *temp;
/* Recurse */
foreach(temp, ((BoolExpr *) qual)->args)
@@ -441,13 +441,12 @@ process_duplicate_ors(List *orlist)
int num_subclauses = 0;
List *winners;
List *neworlist;
List *temp;
List *temp2;
ListCell *temp;
if (orlist == NIL)
return NULL; /* probably can't happen */
if (lnext(orlist) == NIL)
return lfirst(orlist); /* single-expression OR (can this happen?) */
if (length(orlist) == 1) /* single-expression OR (can this happen?) */
return linitial(orlist);
/*
* Choose the shortest AND clause as the reference list --- obviously,
@@ -457,7 +456,7 @@ process_duplicate_ors(List *orlist)
*/
foreach(temp, orlist)
{
Expr *clause = lfirst(temp);
Expr *clause = (Expr *) lfirst(temp);
if (and_clause((Node *) clause))
{
@@ -489,12 +488,13 @@ process_duplicate_ors(List *orlist)
winners = NIL;
foreach(temp, reference)
{
Expr *refclause = lfirst(temp);
Expr *refclause = (Expr *) lfirst(temp);
bool win = true;
ListCell *temp2;
foreach(temp2, orlist)
{
Expr *clause = lfirst(temp2);
Expr *clause = (Expr *) lfirst(temp2);
if (and_clause((Node *) clause))
{
@@ -537,7 +537,7 @@ process_duplicate_ors(List *orlist)
neworlist = NIL;
foreach(temp, orlist)
{
Expr *clause = lfirst(temp);
Expr *clause = (Expr *) lfirst(temp);
if (and_clause((Node *) clause))
{
@@ -546,8 +546,8 @@ process_duplicate_ors(List *orlist)
subclauses = set_difference(subclauses, winners);
if (subclauses != NIL)
{
if (lnext(subclauses) == NIL)
neworlist = lappend(neworlist, lfirst(subclauses));
if (length(subclauses) == 1)
neworlist = lappend(neworlist, linitial(subclauses));
else
neworlist = lappend(neworlist, make_andclause(subclauses));
}
@@ -577,8 +577,8 @@ process_duplicate_ors(List *orlist)
*/
if (neworlist != NIL)
{
if (lnext(neworlist) == NIL)
winners = lappend(winners, lfirst(neworlist));
if (length(neworlist) == 1)
winners = lappend(winners, linitial(neworlist));
else
winners = lappend(winners, make_orclause(pull_ors(neworlist)));
}
@@ -587,8 +587,8 @@ process_duplicate_ors(List *orlist)
* And return the constructed AND clause, again being wary of a single
* element and AND/OR flatness.
*/
if (lnext(winners) == NIL)
return (Expr *) lfirst(winners);
if (length(winners) == 1)
return (Expr *) linitial(winners);
else
return make_andclause(pull_ands(winners));
}

View File

@@ -15,7 +15,7 @@
* Portions Copyright (c) 1994, Regents of the University of California
*
* IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/optimizer/prep/preptlist.c,v 1.66 2003/11/29 19:51:51 pgsql Exp $
* $PostgreSQL: pgsql/src/backend/optimizer/prep/preptlist.c,v 1.67 2004/05/26 04:41:26 neilc Exp $
*
*-------------------------------------------------------------------------
*/
@@ -122,10 +122,13 @@ expand_targetlist(List *tlist, int command_type,
Index result_relation, List *range_table)
{
List *new_tlist = NIL;
ListCell *tlist_item;
Relation rel;
int attrno,
numattrs;
tlist_item = list_head(tlist);
/*
* The rewriter should have already ensured that the TLEs are in
* correct order; but we have to insert TLEs for any missing
@@ -143,15 +146,15 @@ expand_targetlist(List *tlist, int command_type,
Form_pg_attribute att_tup = rel->rd_att->attrs[attrno - 1];
TargetEntry *new_tle = NULL;
if (tlist != NIL)
if (tlist_item != NULL)
{
TargetEntry *old_tle = (TargetEntry *) lfirst(tlist);
TargetEntry *old_tle = (TargetEntry *) lfirst(tlist_item);
Resdom *resdom = old_tle->resdom;
if (!resdom->resjunk && resdom->resno == attrno)
{
new_tle = old_tle;
tlist = lnext(tlist);
tlist_item = lnext(tlist_item);
}
}
@@ -259,9 +262,9 @@ expand_targetlist(List *tlist, int command_type,
* an UPDATE in a table with dropped columns, or an inheritance child
* table with extra columns.)
*/
while (tlist)
while (tlist_item)
{
TargetEntry *old_tle = (TargetEntry *) lfirst(tlist);
TargetEntry *old_tle = (TargetEntry *) lfirst(tlist_item);
Resdom *resdom = old_tle->resdom;
if (!resdom->resjunk)
@@ -275,7 +278,7 @@ expand_targetlist(List *tlist, int command_type,
}
new_tlist = lappend(new_tlist, old_tle);
attrno++;
tlist = lnext(tlist);
tlist_item = lnext(tlist_item);
}
heap_close(rel, AccessShareLock);

View File

@@ -14,7 +14,7 @@
*
*
* IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/optimizer/prep/prepunion.c,v 1.110 2004/05/11 22:43:55 tgl Exp $
* $PostgreSQL: pgsql/src/backend/optimizer/prep/prepunion.c,v 1.111 2004/05/26 04:41:26 neilc Exp $
*
*-------------------------------------------------------------------------
*/
@@ -420,15 +420,19 @@ generate_setop_tlist(List *colTypes, int flag,
{
List *tlist = NIL;
int resno = 1;
List *i;
ListCell *i,
*j,
*k;
Resdom *resdom;
Node *expr;
j = list_head(input_tlist);
k = list_head(refnames_tlist);
foreach(i, colTypes)
{
Oid colType = lfirsto(i);
TargetEntry *inputtle = (TargetEntry *) lfirst(input_tlist);
TargetEntry *reftle = (TargetEntry *) lfirst(refnames_tlist);
TargetEntry *inputtle = (TargetEntry *) lfirst(j);
TargetEntry *reftle = (TargetEntry *) lfirst(k);
int32 colTypmod;
Assert(inputtle->resdom->resno == resno);
@@ -476,8 +480,8 @@ generate_setop_tlist(List *colTypes, int flag,
pstrdup(reftle->resdom->resname),
false);
tlist = lappend(tlist, makeTargetEntry(resdom, (Expr *) expr));
input_tlist = lnext(input_tlist);
refnames_tlist = lnext(refnames_tlist);
j = lnext(j);
k = lnext(k);
}
if (flag >= 0)
@@ -518,11 +522,12 @@ generate_append_tlist(List *colTypes, bool flag,
{
List *tlist = NIL;
int resno = 1;
List *curColType;
ListCell *curColType;
ListCell *ref_tl_item;
int colindex;
Resdom *resdom;
Node *expr;
List *planl;
ListCell *planl;
int32 *colTypmods;
/*
@@ -536,9 +541,9 @@ generate_append_tlist(List *colTypes, bool flag,
foreach(planl, input_plans)
{
Plan *subplan = (Plan *) lfirst(planl);
List *subtlist;
ListCell *subtlist;
curColType = colTypes;
curColType = list_head(colTypes);
colindex = 0;
foreach(subtlist, subplan->targetlist)
{
@@ -546,11 +551,11 @@ generate_append_tlist(List *colTypes, bool flag,
if (subtle->resdom->resjunk)
continue;
Assert(curColType != NIL);
if (subtle->resdom->restype == lfirsto(curColType))
Assert(curColType != NULL);
if (subtle->resdom->restype == lfirst_oid(curColType))
{
/* If first subplan, copy the typmod; else compare */
if (planl == input_plans)
if (planl == list_head(input_plans))
colTypmods[colindex] = subtle->resdom->restypmod;
else if (subtle->resdom->restypmod != colTypmods[colindex])
colTypmods[colindex] = -1;
@@ -563,18 +568,18 @@ generate_append_tlist(List *colTypes, bool flag,
curColType = lnext(curColType);
colindex++;
}
Assert(curColType == NIL);
Assert(curColType == NULL);
}
/*
* Now we can build the tlist for the Append.
*/
colindex = 0;
foreach(curColType, colTypes)
forboth(curColType, colTypes, ref_tl_item, refnames_tlist)
{
Oid colType = lfirsto(curColType);
int32 colTypmod = colTypmods[colindex++];
TargetEntry *reftle = (TargetEntry *) lfirst(refnames_tlist);
TargetEntry *reftle = (TargetEntry *) lfirst(ref_tl_item);
Assert(reftle->resdom->resno == resno);
Assert(!reftle->resdom->resjunk);
@@ -589,7 +594,6 @@ generate_append_tlist(List *colTypes, bool flag,
pstrdup(reftle->resdom->resname),
false);
tlist = lappend(tlist, makeTargetEntry(resdom, (Expr *) expr));
refnames_tlist = lnext(refnames_tlist);
}
if (flag)
@@ -623,11 +627,12 @@ generate_append_tlist(List *colTypes, bool flag,
static bool
tlist_same_datatypes(List *tlist, List *colTypes, bool junkOK)
{
List *i;
ListCell *l;
ListCell *curColType = list_head(colTypes);
foreach(i, tlist)
foreach(l, tlist)
{
TargetEntry *tle = (TargetEntry *) lfirst(i);
TargetEntry *tle = (TargetEntry *) lfirst(l);
if (tle->resdom->resjunk)
{
@@ -636,14 +641,14 @@ tlist_same_datatypes(List *tlist, List *colTypes, bool junkOK)
}
else
{
if (colTypes == NIL)
if (curColType == NULL)
return false;
if (tle->resdom->restype != lfirsto(colTypes))
if (tle->resdom->restype != lfirst_oid(curColType))
return false;
colTypes = lnext(colTypes);
curColType = lnext(curColType);
}
}
if (colTypes != NIL)
if (curColType != NULL)
return false;
return true;
}
@@ -667,10 +672,10 @@ find_all_inheritors(Oid parentrel)
*/
while (unexamined_relids != NIL)
{
Oid currentrel = lfirsto(unexamined_relids);
Oid currentrel = linitial_oid(unexamined_relids);
List *currentchildren;
unexamined_relids = lnext(unexamined_relids);
unexamined_relids = list_delete_first(unexamined_relids);
examined_relids = lappendo(examined_relids, currentrel);
currentchildren = find_inheritance_children(currentrel);
@@ -719,7 +724,7 @@ expand_inherited_rtentry(Query *parse, Index rti, bool dup_parent)
Oid parentOID;
List *inhOIDs;
List *inhRTIs;
List *l;
ListCell *l;
/* Does RT entry allow inheritance? */
if (!rte->inh)
@@ -739,7 +744,7 @@ expand_inherited_rtentry(Query *parse, Index rti, bool dup_parent)
* case. This could happen despite above has_subclass() check, if
* table once had a child but no longer does.
*/
if (lnext(inhOIDs) == NIL)
if (length(inhOIDs) < 2)
return NIL;
/* OK, it's an inheritance set; expand it */
if (dup_parent)
@@ -1020,7 +1025,7 @@ static List *
adjust_inherited_tlist(List *tlist, Oid old_relid, Oid new_relid)
{
bool changed_it = false;
List *tl;
ListCell *tl;
List *new_tlist;
bool more;
int attrno;