mirror of
https://github.com/postgres/postgres.git
synced 2025-07-15 19:21:59 +03:00
Ye-old pgindent run. Same 4-space tabs.
This commit is contained in:
@ -8,7 +8,7 @@
|
||||
*
|
||||
*
|
||||
* IDENTIFICATION
|
||||
* $Header: /cvsroot/pgsql/src/backend/optimizer/prep/prepqual.c,v 1.23 2000/02/27 19:45:44 tgl Exp $
|
||||
* $Header: /cvsroot/pgsql/src/backend/optimizer/prep/prepqual.c,v 1.24 2000/04/12 17:15:23 momjian Exp $
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
@ -33,7 +33,7 @@ static Expr *and_normalize(List *andlist);
|
||||
static Expr *qual_cleanup(Expr *qual);
|
||||
static List *remove_duplicates(List *list);
|
||||
static void count_bool_nodes(Expr *qual, double *nodes,
|
||||
double *cnfnodes, double *dnfnodes);
|
||||
double *cnfnodes, double *dnfnodes);
|
||||
|
||||
/*****************************************************************************
|
||||
*
|
||||
@ -71,7 +71,7 @@ static void count_bool_nodes(Expr *qual, double *nodes,
|
||||
*
|
||||
* If 'removeAndFlag' is true then it removes explicit AND at the top level,
|
||||
* producing a list of implicitly-ANDed conditions. Otherwise, a regular
|
||||
* boolean expression is returned. Since most callers pass 'true', we
|
||||
* boolean expression is returned. Since most callers pass 'true', we
|
||||
* prefer to declare the result as List *, not Expr *.
|
||||
*
|
||||
* XXX This code could be much smarter, at the cost of also being slower,
|
||||
@ -95,12 +95,14 @@ canonicalize_qual(Expr *qual, bool removeAndFlag)
|
||||
if (qual == NULL)
|
||||
return NIL;
|
||||
|
||||
/* Flatten AND and OR groups throughout the tree.
|
||||
* This improvement is always worthwhile, so do it unconditionally.
|
||||
/*
|
||||
* Flatten AND and OR groups throughout the tree. This improvement is
|
||||
* always worthwhile, so do it unconditionally.
|
||||
*/
|
||||
qual = flatten_andors(qual);
|
||||
|
||||
/* Push down NOTs. We do this only in the top-level boolean
|
||||
/*
|
||||
* Push down NOTs. We do this only in the top-level boolean
|
||||
* expression, without examining arguments of operators/functions.
|
||||
* Even so, it might not be a win if we are unable to find negators
|
||||
* for all the operators involved; perhaps we should compare before-
|
||||
@ -109,21 +111,24 @@ canonicalize_qual(Expr *qual, bool removeAndFlag)
|
||||
newqual = find_nots(qual);
|
||||
|
||||
/*
|
||||
* Choose whether to convert to CNF, or DNF, or leave well enough alone.
|
||||
* Choose whether to convert to CNF, or DNF, or leave well enough
|
||||
* alone.
|
||||
*
|
||||
* We make an approximate estimate of the number of bottom-level nodes
|
||||
* that will appear in the CNF and DNF forms of the query.
|
||||
*/
|
||||
count_bool_nodes(newqual, &nodes, &cnfnodes, &dnfnodes);
|
||||
|
||||
/*
|
||||
* First heuristic is to forget about *both* normal forms if there are
|
||||
* a huge number of terms in the qual clause. This would only happen
|
||||
* with machine-generated queries, presumably; and most likely such
|
||||
* a query is already in either CNF or DNF.
|
||||
* with machine-generated queries, presumably; and most likely such a
|
||||
* query is already in either CNF or DNF.
|
||||
*/
|
||||
cnfok = dnfok = true;
|
||||
if (nodes >= 500.0)
|
||||
cnfok = dnfok = false;
|
||||
|
||||
/*
|
||||
* Second heuristic is to forget about either CNF or DNF if it shows
|
||||
* unreasonable growth compared to the original form of the qual,
|
||||
@ -134,15 +139,17 @@ canonicalize_qual(Expr *qual, bool removeAndFlag)
|
||||
cnfok = false;
|
||||
if (dnfnodes >= 4.0 * nodes)
|
||||
dnfok = false;
|
||||
|
||||
/*
|
||||
* Third heuristic is to prefer DNF if top level is already an OR,
|
||||
* and only one relation is mentioned, and DNF is no larger than
|
||||
* the CNF representation. (Pretty shaky; can we improve on this?)
|
||||
* Third heuristic is to prefer DNF if top level is already an OR, and
|
||||
* only one relation is mentioned, and DNF is no larger than the CNF
|
||||
* representation. (Pretty shaky; can we improve on this?)
|
||||
*/
|
||||
if (cnfok && dnfok && dnfnodes <= cnfnodes &&
|
||||
or_clause((Node *) newqual) &&
|
||||
NumRelids((Node *) newqual) == 1)
|
||||
cnfok = false;
|
||||
|
||||
/*
|
||||
* Otherwise, we prefer CNF.
|
||||
*
|
||||
@ -150,20 +157,26 @@ canonicalize_qual(Expr *qual, bool removeAndFlag)
|
||||
*/
|
||||
if (cnfok)
|
||||
{
|
||||
/* Normalize into conjunctive normal form, and clean up the result. */
|
||||
|
||||
/*
|
||||
* Normalize into conjunctive normal form, and clean up the
|
||||
* result.
|
||||
*/
|
||||
newqual = qual_cleanup(find_ors(newqual));
|
||||
}
|
||||
else if (dnfok)
|
||||
{
|
||||
/* Normalize into disjunctive normal form, and clean up the result. */
|
||||
|
||||
/*
|
||||
* Normalize into disjunctive normal form, and clean up the
|
||||
* result.
|
||||
*/
|
||||
newqual = qual_cleanup(find_ands(newqual));
|
||||
}
|
||||
|
||||
/* Convert to implicit-AND list if requested */
|
||||
if (removeAndFlag)
|
||||
{
|
||||
newqual = (Expr *) make_ands_implicit(newqual);
|
||||
}
|
||||
|
||||
return (List *) newqual;
|
||||
}
|
||||
@ -177,7 +190,7 @@ canonicalize_qual(Expr *qual, bool removeAndFlag)
|
||||
*
|
||||
* If 'removeAndFlag' is true then it removes explicit AND at the top level,
|
||||
* producing a list of implicitly-ANDed conditions. Otherwise, a regular
|
||||
* boolean expression is returned. Since most callers pass 'true', we
|
||||
* boolean expression is returned. Since most callers pass 'true', we
|
||||
* prefer to declare the result as List *, not Expr *.
|
||||
*/
|
||||
List *
|
||||
@ -188,11 +201,14 @@ cnfify(Expr *qual, bool removeAndFlag)
|
||||
if (qual == NULL)
|
||||
return NIL;
|
||||
|
||||
/* Flatten AND and OR groups throughout the tree.
|
||||
* This improvement is always worthwhile.
|
||||
/*
|
||||
* Flatten AND and OR groups throughout the tree. This improvement is
|
||||
* always worthwhile.
|
||||
*/
|
||||
newqual = flatten_andors(qual);
|
||||
/* Push down NOTs. We do this only in the top-level boolean
|
||||
|
||||
/*
|
||||
* Push down NOTs. We do this only in the top-level boolean
|
||||
* expression, without examining arguments of operators/functions.
|
||||
*/
|
||||
newqual = find_nots(newqual);
|
||||
@ -202,9 +218,7 @@ cnfify(Expr *qual, bool removeAndFlag)
|
||||
newqual = qual_cleanup(newqual);
|
||||
|
||||
if (removeAndFlag)
|
||||
{
|
||||
newqual = (Expr *) make_ands_implicit(newqual);
|
||||
}
|
||||
|
||||
return (List *) newqual;
|
||||
}
|
||||
@ -227,11 +241,14 @@ dnfify(Expr *qual)
|
||||
if (qual == NULL)
|
||||
return NULL;
|
||||
|
||||
/* Flatten AND and OR groups throughout the tree.
|
||||
* This improvement is always worthwhile.
|
||||
/*
|
||||
* Flatten AND and OR groups throughout the tree. This improvement is
|
||||
* always worthwhile.
|
||||
*/
|
||||
newqual = flatten_andors(qual);
|
||||
/* Push down NOTs. We do this only in the top-level boolean
|
||||
|
||||
/*
|
||||
* Push down NOTs. We do this only in the top-level boolean
|
||||
* expression, without examining arguments of operators/functions.
|
||||
*/
|
||||
newqual = find_nots(newqual);
|
||||
@ -280,13 +297,13 @@ flatten_andors(Expr *qual)
|
||||
|
||||
foreach(arg, qual->args)
|
||||
{
|
||||
Expr *subexpr = flatten_andors((Expr *) lfirst(arg));
|
||||
Expr *subexpr = flatten_andors((Expr *) lfirst(arg));
|
||||
|
||||
/*
|
||||
* Note: we can destructively nconc the subexpression's arglist
|
||||
* because we know the recursive invocation of flatten_andors
|
||||
* will have built a new arglist not shared with any other expr.
|
||||
* Otherwise we'd need a listCopy here.
|
||||
* Note: we can destructively nconc the subexpression's
|
||||
* arglist because we know the recursive invocation of
|
||||
* flatten_andors will have built a new arglist not shared
|
||||
* with any other expr. Otherwise we'd need a listCopy here.
|
||||
*/
|
||||
if (and_clause((Node *) subexpr))
|
||||
out_list = nconc(out_list, subexpr->args);
|
||||
@ -302,13 +319,13 @@ flatten_andors(Expr *qual)
|
||||
|
||||
foreach(arg, qual->args)
|
||||
{
|
||||
Expr *subexpr = flatten_andors((Expr *) lfirst(arg));
|
||||
Expr *subexpr = flatten_andors((Expr *) lfirst(arg));
|
||||
|
||||
/*
|
||||
* Note: we can destructively nconc the subexpression's arglist
|
||||
* because we know the recursive invocation of flatten_andors
|
||||
* will have built a new arglist not shared with any other expr.
|
||||
* Otherwise we'd need a listCopy here.
|
||||
* Note: we can destructively nconc the subexpression's
|
||||
* arglist because we know the recursive invocation of
|
||||
* flatten_andors will have built a new arglist not shared
|
||||
* with any other expr. Otherwise we'd need a listCopy here.
|
||||
*/
|
||||
if (or_clause((Node *) subexpr))
|
||||
out_list = nconc(out_list, subexpr->args);
|
||||
@ -354,13 +371,13 @@ pull_ors(List *orlist)
|
||||
|
||||
foreach(arg, orlist)
|
||||
{
|
||||
Expr *subexpr = (Expr *) lfirst(arg);
|
||||
Expr *subexpr = (Expr *) lfirst(arg);
|
||||
|
||||
/*
|
||||
* Note: we can destructively nconc the subexpression's arglist
|
||||
* because we know the recursive invocation of pull_ors
|
||||
* will have built a new arglist not shared with any other expr.
|
||||
* Otherwise we'd need a listCopy here.
|
||||
* because we know the recursive invocation of pull_ors will have
|
||||
* built a new arglist not shared with any other expr. Otherwise
|
||||
* we'd need a listCopy here.
|
||||
*/
|
||||
if (or_clause((Node *) subexpr))
|
||||
out_list = nconc(out_list, pull_ors(subexpr->args));
|
||||
@ -385,13 +402,13 @@ pull_ands(List *andlist)
|
||||
|
||||
foreach(arg, andlist)
|
||||
{
|
||||
Expr *subexpr = (Expr *) lfirst(arg);
|
||||
Expr *subexpr = (Expr *) lfirst(arg);
|
||||
|
||||
/*
|
||||
* Note: we can destructively nconc the subexpression's arglist
|
||||
* because we know the recursive invocation of pull_ands
|
||||
* will have built a new arglist not shared with any other expr.
|
||||
* Otherwise we'd need a listCopy here.
|
||||
* because we know the recursive invocation of pull_ands will have
|
||||
* built a new arglist not shared with any other expr. Otherwise
|
||||
* we'd need a listCopy here.
|
||||
*/
|
||||
if (and_clause((Node *) subexpr))
|
||||
out_list = nconc(out_list, pull_ands(subexpr->args));
|
||||
@ -407,7 +424,7 @@ pull_ands(List *andlist)
|
||||
* For 'NOT' clauses, apply push_not() to try to push down the 'NOT'.
|
||||
* For all other clause types, simply recurse.
|
||||
*
|
||||
* Returns the modified qualification. AND/OR flatness is preserved.
|
||||
* Returns the modified qualification. AND/OR flatness is preserved.
|
||||
*/
|
||||
static Expr *
|
||||
find_nots(Expr *qual)
|
||||
@ -468,7 +485,8 @@ static Expr *
|
||||
push_nots(Expr *qual)
|
||||
{
|
||||
if (qual == NULL)
|
||||
return make_notclause(qual); /* XXX is this right? Or possible? */
|
||||
return make_notclause(qual); /* XXX is this right? Or
|
||||
* possible? */
|
||||
|
||||
/*
|
||||
* Negate an operator clause if possible: ("NOT" (< A B)) => (> A B)
|
||||
@ -486,6 +504,7 @@ push_nots(Expr *qual)
|
||||
InvalidOid,
|
||||
oper->opresulttype,
|
||||
0, NULL);
|
||||
|
||||
return make_opclause(op, get_leftop(qual), get_rightop(qual));
|
||||
}
|
||||
else
|
||||
@ -496,7 +515,7 @@ push_nots(Expr *qual)
|
||||
/*--------------------
|
||||
* Apply DeMorgan's Laws:
|
||||
* ("NOT" ("AND" A B)) => ("OR" ("NOT" A) ("NOT" B))
|
||||
* ("NOT" ("OR" A B)) => ("AND" ("NOT" A) ("NOT" B))
|
||||
* ("NOT" ("OR" A B)) => ("AND" ("NOT" A) ("NOT" B))
|
||||
* i.e., swap AND for OR and negate all the subclauses.
|
||||
*--------------------
|
||||
*/
|
||||
@ -518,6 +537,7 @@ push_nots(Expr *qual)
|
||||
}
|
||||
else if (not_clause((Node *) qual))
|
||||
{
|
||||
|
||||
/*
|
||||
* Another 'not' cancels this 'not', so eliminate the 'not' and
|
||||
* stop negating this branch. But search the subexpression for
|
||||
@ -527,6 +547,7 @@ push_nots(Expr *qual)
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
/*
|
||||
* We don't know how to negate anything else, place a 'not' at
|
||||
* this level.
|
||||
@ -544,7 +565,7 @@ push_nots(Expr *qual)
|
||||
* Note that 'or' clauses will always be turned into 'and' clauses
|
||||
* if they contain any 'and' subclauses.
|
||||
*
|
||||
* Returns the modified qualification. AND/OR flatness is preserved.
|
||||
* Returns the modified qualification. AND/OR flatness is preserved.
|
||||
*/
|
||||
static Expr *
|
||||
find_ors(Expr *qual)
|
||||
@ -601,17 +622,17 @@ or_normalize(List *orlist)
|
||||
return lfirst(orlist); /* single-expression OR (can this happen?) */
|
||||
|
||||
/*
|
||||
* If we have a choice of AND clauses, pick the one with the
|
||||
* most subclauses. Because we initialized num_subclauses = 1,
|
||||
* any AND clauses with only one arg will be ignored as useless.
|
||||
* If we have a choice of AND clauses, pick the one with the most
|
||||
* subclauses. Because we initialized num_subclauses = 1, any AND
|
||||
* clauses with only one arg will be ignored as useless.
|
||||
*/
|
||||
foreach(temp, orlist)
|
||||
{
|
||||
Expr *clause = lfirst(temp);
|
||||
Expr *clause = lfirst(temp);
|
||||
|
||||
if (and_clause((Node *) clause))
|
||||
{
|
||||
int nclauses = length(clause->args);
|
||||
int nclauses = length(clause->args);
|
||||
|
||||
if (nclauses > num_subclauses)
|
||||
{
|
||||
@ -622,12 +643,13 @@ or_normalize(List *orlist)
|
||||
}
|
||||
|
||||
/* if there's no suitable AND clause, we can't transform the OR */
|
||||
if (! distributable)
|
||||
if (!distributable)
|
||||
return make_orclause(orlist);
|
||||
|
||||
/* Caution: lremove destructively modifies the input orlist.
|
||||
* This should be OK, since or_normalize is only called with
|
||||
* freshly constructed lists that are not referenced elsewhere.
|
||||
/*
|
||||
* Caution: lremove destructively modifies the input orlist. This
|
||||
* should be OK, since or_normalize is only called with freshly
|
||||
* constructed lists that are not referenced elsewhere.
|
||||
*/
|
||||
orlist = lremove(distributable, orlist);
|
||||
|
||||
@ -635,11 +657,12 @@ or_normalize(List *orlist)
|
||||
{
|
||||
Expr *andclause = lfirst(temp);
|
||||
|
||||
/* pull_ors is needed here in case andclause has a top-level OR.
|
||||
* Then we recursively apply or_normalize, since there might
|
||||
* be an AND subclause in the resulting OR-list.
|
||||
* Note: we rely on pull_ors to build a fresh list,
|
||||
* and not damage the given orlist.
|
||||
/*
|
||||
* pull_ors is needed here in case andclause has a top-level OR.
|
||||
* Then we recursively apply or_normalize, since there might be an
|
||||
* AND subclause in the resulting OR-list. Note: we rely on
|
||||
* pull_ors to build a fresh list, and not damage the given
|
||||
* orlist.
|
||||
*/
|
||||
andclause = or_normalize(pull_ors(lcons(andclause, orlist)));
|
||||
andclauses = lappend(andclauses, andclause);
|
||||
@ -658,7 +681,7 @@ or_normalize(List *orlist)
|
||||
* Note that 'and' clauses will always be turned into 'or' clauses
|
||||
* if they contain any 'or' subclauses.
|
||||
*
|
||||
* Returns the modified qualification. AND/OR flatness is preserved.
|
||||
* Returns the modified qualification. AND/OR flatness is preserved.
|
||||
*/
|
||||
static Expr *
|
||||
find_ands(Expr *qual)
|
||||
@ -712,20 +735,21 @@ and_normalize(List *andlist)
|
||||
if (andlist == NIL)
|
||||
return NULL; /* probably can't happen */
|
||||
if (lnext(andlist) == NIL)
|
||||
return lfirst(andlist); /* single-expression AND (can this happen?) */
|
||||
return lfirst(andlist); /* single-expression AND (can this
|
||||
* happen?) */
|
||||
|
||||
/*
|
||||
* If we have a choice of OR clauses, pick the one with the
|
||||
* most subclauses. Because we initialized num_subclauses = 1,
|
||||
* any OR clauses with only one arg will be ignored as useless.
|
||||
* If we have a choice of OR clauses, pick the one with the most
|
||||
* subclauses. Because we initialized num_subclauses = 1, any OR
|
||||
* clauses with only one arg will be ignored as useless.
|
||||
*/
|
||||
foreach(temp, andlist)
|
||||
{
|
||||
Expr *clause = lfirst(temp);
|
||||
Expr *clause = lfirst(temp);
|
||||
|
||||
if (or_clause((Node *) clause))
|
||||
{
|
||||
int nclauses = length(clause->args);
|
||||
int nclauses = length(clause->args);
|
||||
|
||||
if (nclauses > num_subclauses)
|
||||
{
|
||||
@ -736,12 +760,13 @@ and_normalize(List *andlist)
|
||||
}
|
||||
|
||||
/* if there's no suitable OR clause, we can't transform the AND */
|
||||
if (! distributable)
|
||||
if (!distributable)
|
||||
return make_andclause(andlist);
|
||||
|
||||
/* Caution: lremove destructively modifies the input andlist.
|
||||
* This should be OK, since and_normalize is only called with
|
||||
* freshly constructed lists that are not referenced elsewhere.
|
||||
/*
|
||||
* Caution: lremove destructively modifies the input andlist. This
|
||||
* should be OK, since and_normalize is only called with freshly
|
||||
* constructed lists that are not referenced elsewhere.
|
||||
*/
|
||||
andlist = lremove(distributable, andlist);
|
||||
|
||||
@ -749,11 +774,12 @@ and_normalize(List *andlist)
|
||||
{
|
||||
Expr *orclause = lfirst(temp);
|
||||
|
||||
/* pull_ands is needed here in case orclause has a top-level AND.
|
||||
* Then we recursively apply and_normalize, since there might
|
||||
* be an OR subclause in the resulting AND-list.
|
||||
* Note: we rely on pull_ands to build a fresh list,
|
||||
* and not damage the given andlist.
|
||||
/*
|
||||
* pull_ands is needed here in case orclause has a top-level AND.
|
||||
* Then we recursively apply and_normalize, since there might be
|
||||
* an OR subclause in the resulting AND-list. Note: we rely on
|
||||
* pull_ands to build a fresh list, and not damage the given
|
||||
* andlist.
|
||||
*/
|
||||
orclause = and_normalize(pull_ands(lcons(orclause, andlist)));
|
||||
orclauses = lappend(orclauses, orclause);
|
||||
@ -767,7 +793,7 @@ and_normalize(List *andlist)
|
||||
* qual_cleanup
|
||||
* Fix up a qualification by removing duplicate entries (which could be
|
||||
* created during normalization, if identical subexpressions from different
|
||||
* parts of the tree are brought together). Also, check for AND and OR
|
||||
* parts of the tree are brought together). Also, check for AND and OR
|
||||
* clauses with only one remaining subexpression, and simplify.
|
||||
*
|
||||
* Returns the modified qualification.
|
||||
@ -828,7 +854,7 @@ remove_duplicates(List *list)
|
||||
|
||||
foreach(i, list)
|
||||
{
|
||||
if (! member(lfirst(i), result))
|
||||
if (!member(lfirst(i), result))
|
||||
result = lappend(result, lfirst(i));
|
||||
}
|
||||
return result;
|
||||
@ -855,7 +881,9 @@ count_bool_nodes(Expr *qual,
|
||||
double *dnfnodes)
|
||||
{
|
||||
List *temp;
|
||||
double subnodes, subcnfnodes, subdnfnodes;
|
||||
double subnodes,
|
||||
subcnfnodes,
|
||||
subdnfnodes;
|
||||
|
||||
if (and_clause((Node *) qual))
|
||||
{
|
||||
@ -864,13 +892,15 @@ count_bool_nodes(Expr *qual,
|
||||
|
||||
foreach(temp, qual->args)
|
||||
{
|
||||
count_bool_nodes(lfirst(temp),
|
||||
count_bool_nodes(lfirst(temp),
|
||||
&subnodes, &subcnfnodes, &subdnfnodes);
|
||||
*nodes += subnodes;
|
||||
*cnfnodes += subcnfnodes;
|
||||
*dnfnodes *= subdnfnodes;
|
||||
}
|
||||
/* we could get dnfnodes < cnfnodes here, if all the sub-nodes are
|
||||
|
||||
/*
|
||||
* we could get dnfnodes < cnfnodes here, if all the sub-nodes are
|
||||
* simple ones with count 1. Make sure dnfnodes isn't too small.
|
||||
*/
|
||||
if (*dnfnodes < *cnfnodes)
|
||||
@ -883,13 +913,15 @@ count_bool_nodes(Expr *qual,
|
||||
|
||||
foreach(temp, qual->args)
|
||||
{
|
||||
count_bool_nodes(lfirst(temp),
|
||||
count_bool_nodes(lfirst(temp),
|
||||
&subnodes, &subcnfnodes, &subdnfnodes);
|
||||
*nodes += subnodes;
|
||||
*cnfnodes *= subcnfnodes;
|
||||
*dnfnodes += subdnfnodes;
|
||||
}
|
||||
/* we could get cnfnodes < dnfnodes here, if all the sub-nodes are
|
||||
|
||||
/*
|
||||
* we could get cnfnodes < dnfnodes here, if all the sub-nodes are
|
||||
* simple ones with count 1. Make sure cnfnodes isn't too small.
|
||||
*/
|
||||
if (*cnfnodes < *dnfnodes)
|
||||
|
@ -4,7 +4,7 @@
|
||||
* Routines to preprocess the parse tree target list
|
||||
*
|
||||
* This module takes care of altering the query targetlist as needed for
|
||||
* INSERT, UPDATE, and DELETE queries. For INSERT and UPDATE queries,
|
||||
* INSERT, UPDATE, and DELETE queries. For INSERT and UPDATE queries,
|
||||
* the targetlist must contain an entry for each attribute of the target
|
||||
* relation in the correct order. For both UPDATE and DELETE queries,
|
||||
* we need a junk targetlist entry holding the CTID attribute --- the
|
||||
@ -15,7 +15,7 @@
|
||||
* Portions Copyright (c) 1994, Regents of the University of California
|
||||
*
|
||||
* IDENTIFICATION
|
||||
* $Header: /cvsroot/pgsql/src/backend/optimizer/prep/preptlist.c,v 1.35 2000/03/09 05:00:24 inoue Exp $
|
||||
* $Header: /cvsroot/pgsql/src/backend/optimizer/prep/preptlist.c,v 1.36 2000/04/12 17:15:23 momjian Exp $
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
@ -31,7 +31,7 @@
|
||||
|
||||
|
||||
static List *expand_targetlist(List *tlist, int command_type,
|
||||
Index result_relation, List *range_table);
|
||||
Index result_relation, List *range_table);
|
||||
|
||||
|
||||
/*
|
||||
@ -46,6 +46,7 @@ preprocess_targetlist(List *tlist,
|
||||
Index result_relation,
|
||||
List *range_table)
|
||||
{
|
||||
|
||||
/*
|
||||
* for heap_formtuple to work, the targetlist must match the exact
|
||||
* order of the attributes. We also need to fill in any missing
|
||||
@ -56,11 +57,11 @@ preprocess_targetlist(List *tlist,
|
||||
result_relation, range_table);
|
||||
|
||||
/*
|
||||
* for "update" and "delete" queries, add ctid of the result
|
||||
* relation into the target list so that the ctid will propagate
|
||||
* through execution and ExecutePlan() will be able to identify
|
||||
* the right tuple to replace or delete. This extra field is
|
||||
* marked "junk" so that it is not stored back into the tuple.
|
||||
* for "update" and "delete" queries, add ctid of the result relation
|
||||
* into the target list so that the ctid will propagate through
|
||||
* execution and ExecutePlan() will be able to identify the right
|
||||
* tuple to replace or delete. This extra field is marked "junk" so
|
||||
* that it is not stored back into the tuple.
|
||||
*/
|
||||
if (command_type == CMD_UPDATE || command_type == CMD_DELETE)
|
||||
{
|
||||
@ -78,7 +79,8 @@ preprocess_targetlist(List *tlist,
|
||||
var = makeVar(result_relation, SelfItemPointerAttributeNumber,
|
||||
TIDOID, -1, 0);
|
||||
|
||||
/* For an UPDATE, expand_targetlist already created a fresh tlist.
|
||||
/*
|
||||
* For an UPDATE, expand_targetlist already created a fresh tlist.
|
||||
* For DELETE, better do a listCopy so that we don't destructively
|
||||
* modify the original tlist (is this really necessary?).
|
||||
*/
|
||||
@ -117,11 +119,11 @@ expand_targetlist(List *tlist, int command_type,
|
||||
List *temp;
|
||||
|
||||
/*
|
||||
* Keep a map of which tlist items we have transferred to new list.
|
||||
* +1 here keeps palloc from complaining if old_tlist_len=0.
|
||||
* Keep a map of which tlist items we have transferred to new list. +1
|
||||
* here keeps palloc from complaining if old_tlist_len=0.
|
||||
*/
|
||||
tlistentry_used = (bool *) palloc((old_tlist_len+1) * sizeof(bool));
|
||||
memset(tlistentry_used, 0, (old_tlist_len+1) * sizeof(bool));
|
||||
tlistentry_used = (bool *) palloc((old_tlist_len + 1) * sizeof(bool));
|
||||
memset(tlistentry_used, 0, (old_tlist_len + 1) * sizeof(bool));
|
||||
|
||||
/*
|
||||
* Scan the tuple description in the relation's relcache entry to make
|
||||
@ -133,9 +135,9 @@ expand_targetlist(List *tlist, int command_type,
|
||||
|
||||
for (attrno = 1; attrno <= numattrs; attrno++)
|
||||
{
|
||||
Form_pg_attribute att_tup = rel->rd_att->attrs[attrno-1];
|
||||
char *attrname = NameStr(att_tup->attname);
|
||||
TargetEntry *new_tle = NULL;
|
||||
Form_pg_attribute att_tup = rel->rd_att->attrs[attrno - 1];
|
||||
char *attrname = NameStr(att_tup->attname);
|
||||
TargetEntry *new_tle = NULL;
|
||||
|
||||
/*
|
||||
* We match targetlist entries to attributes using the resname.
|
||||
@ -143,22 +145,22 @@ expand_targetlist(List *tlist, int command_type,
|
||||
old_tlist_index = 0;
|
||||
foreach(temp, tlist)
|
||||
{
|
||||
TargetEntry *old_tle = (TargetEntry *) lfirst(temp);
|
||||
Resdom *resdom = old_tle->resdom;
|
||||
TargetEntry *old_tle = (TargetEntry *) lfirst(temp);
|
||||
Resdom *resdom = old_tle->resdom;
|
||||
|
||||
if (! tlistentry_used[old_tlist_index] &&
|
||||
if (!tlistentry_used[old_tlist_index] &&
|
||||
strcmp(resdom->resname, attrname) == 0 &&
|
||||
! resdom->resjunk)
|
||||
!resdom->resjunk)
|
||||
{
|
||||
|
||||
/*
|
||||
* We can recycle the old TLE+resdom if right resno; else
|
||||
* make a new one to avoid modifying the old tlist structure.
|
||||
* (Is preserving old tlist actually necessary?)
|
||||
* make a new one to avoid modifying the old tlist
|
||||
* structure. (Is preserving old tlist actually
|
||||
* necessary?)
|
||||
*/
|
||||
if (resdom->resno == attrno)
|
||||
{
|
||||
new_tle = old_tle;
|
||||
}
|
||||
else
|
||||
{
|
||||
resdom = (Resdom *) copyObject((Node *) resdom);
|
||||
@ -173,14 +175,15 @@ expand_targetlist(List *tlist, int command_type,
|
||||
|
||||
if (new_tle == NULL)
|
||||
{
|
||||
|
||||
/*
|
||||
* Didn't find a matching tlist entry, so make one.
|
||||
*
|
||||
* For INSERT, generate a constant of the default value for
|
||||
* the attribute type, or NULL if no default value.
|
||||
* For INSERT, generate a constant of the default value for the
|
||||
* attribute type, or NULL if no default value.
|
||||
*
|
||||
* For UPDATE, generate a Var reference to the existing value
|
||||
* of the attribute, so that it gets copied to the new tuple.
|
||||
* For UPDATE, generate a Var reference to the existing value of
|
||||
* the attribute, so that it gets copied to the new tuple.
|
||||
*/
|
||||
Oid atttype = att_tup->atttypid;
|
||||
int32 atttypmod = att_tup->atttypmod;
|
||||
@ -188,92 +191,96 @@ expand_targetlist(List *tlist, int command_type,
|
||||
switch (command_type)
|
||||
{
|
||||
case CMD_INSERT:
|
||||
{
|
||||
{
|
||||
#ifdef _DROP_COLUMN_HACK__
|
||||
Datum typedefault;
|
||||
Datum typedefault;
|
||||
|
||||
#else
|
||||
Datum typedefault = get_typdefault(atttype);
|
||||
#endif /* _DROP_COLUMN_HACK__ */
|
||||
int typlen;
|
||||
Const *temp_const;
|
||||
Datum typedefault = get_typdefault(atttype);
|
||||
|
||||
#endif /* _DROP_COLUMN_HACK__ */
|
||||
int typlen;
|
||||
Const *temp_const;
|
||||
|
||||
#ifdef _DROP_COLUMN_HACK__
|
||||
if (COLUMN_IS_DROPPED(att_tup))
|
||||
typedefault = PointerGetDatum(NULL);
|
||||
else
|
||||
typedefault = get_typdefault(atttype);
|
||||
#endif /* _DROP_COLUMN_HACK__ */
|
||||
if (typedefault == PointerGetDatum(NULL))
|
||||
typlen = 0;
|
||||
else
|
||||
{
|
||||
/*
|
||||
* Since this is an append or replace, the size of
|
||||
* any set attribute is the size of the OID used to
|
||||
* represent it.
|
||||
*/
|
||||
if (att_tup->attisset)
|
||||
typlen = get_typlen(OIDOID);
|
||||
if (COLUMN_IS_DROPPED(att_tup))
|
||||
typedefault = PointerGetDatum(NULL);
|
||||
else
|
||||
typlen = get_typlen(atttype);
|
||||
typedefault = get_typdefault(atttype);
|
||||
#endif /* _DROP_COLUMN_HACK__ */
|
||||
if (typedefault == PointerGetDatum(NULL))
|
||||
typlen = 0;
|
||||
else
|
||||
{
|
||||
|
||||
/*
|
||||
* Since this is an append or replace, the
|
||||
* size of any set attribute is the size of
|
||||
* the OID used to represent it.
|
||||
*/
|
||||
if (att_tup->attisset)
|
||||
typlen = get_typlen(OIDOID);
|
||||
else
|
||||
typlen = get_typlen(atttype);
|
||||
}
|
||||
|
||||
temp_const = makeConst(atttype,
|
||||
typlen,
|
||||
typedefault,
|
||||
(typedefault == PointerGetDatum(NULL)),
|
||||
false,
|
||||
false, /* not a set */
|
||||
false);
|
||||
|
||||
new_tle = makeTargetEntry(makeResdom(attrno,
|
||||
atttype,
|
||||
-1,
|
||||
pstrdup(attrname),
|
||||
0,
|
||||
(Oid) 0,
|
||||
false),
|
||||
(Node *) temp_const);
|
||||
break;
|
||||
}
|
||||
|
||||
temp_const = makeConst(atttype,
|
||||
typlen,
|
||||
typedefault,
|
||||
(typedefault == PointerGetDatum(NULL)),
|
||||
false,
|
||||
false, /* not a set */
|
||||
false);
|
||||
|
||||
new_tle = makeTargetEntry(makeResdom(attrno,
|
||||
atttype,
|
||||
-1,
|
||||
pstrdup(attrname),
|
||||
0,
|
||||
(Oid) 0,
|
||||
false),
|
||||
(Node *) temp_const);
|
||||
break;
|
||||
}
|
||||
case CMD_UPDATE:
|
||||
{
|
||||
Var *temp_var;
|
||||
|
||||
#ifdef _DROP_COLUMN_HACK__
|
||||
Node *temp_node = (Node *) NULL;
|
||||
if (COLUMN_IS_DROPPED(att_tup))
|
||||
{
|
||||
temp_node = (Node *)makeConst(atttype, 0,
|
||||
PointerGetDatum(NULL),
|
||||
true,
|
||||
false,
|
||||
false, /* not a set */
|
||||
false);
|
||||
}
|
||||
else
|
||||
#endif /* _DROP_COLUMN_HACK__ */
|
||||
temp_var = makeVar(result_relation, attrno, atttype,
|
||||
atttypmod, 0);
|
||||
#ifdef _DROP_COLUMN_HACK__
|
||||
if (!temp_node)
|
||||
temp_node = (Node *) temp_var;
|
||||
#endif /* _DROP_COLUMN_HACK__ */
|
||||
Var *temp_var;
|
||||
|
||||
new_tle = makeTargetEntry(makeResdom(attrno,
|
||||
atttype,
|
||||
atttypmod,
|
||||
pstrdup(attrname),
|
||||
0,
|
||||
(Oid) 0,
|
||||
false),
|
||||
#ifdef _DROP_COLUMN_HACK__
|
||||
temp_node);
|
||||
Node *temp_node = (Node *) NULL;
|
||||
|
||||
if (COLUMN_IS_DROPPED(att_tup))
|
||||
{
|
||||
temp_node = (Node *) makeConst(atttype, 0,
|
||||
PointerGetDatum(NULL),
|
||||
true,
|
||||
false,
|
||||
false, /* not a set */
|
||||
false);
|
||||
}
|
||||
else
|
||||
#endif /* _DROP_COLUMN_HACK__ */
|
||||
temp_var = makeVar(result_relation, attrno, atttype,
|
||||
atttypmod, 0);
|
||||
#ifdef _DROP_COLUMN_HACK__
|
||||
if (!temp_node)
|
||||
temp_node = (Node *) temp_var;
|
||||
#endif /* _DROP_COLUMN_HACK__ */
|
||||
|
||||
new_tle = makeTargetEntry(makeResdom(attrno,
|
||||
atttype,
|
||||
atttypmod,
|
||||
pstrdup(attrname),
|
||||
0,
|
||||
(Oid) 0,
|
||||
false),
|
||||
#ifdef _DROP_COLUMN_HACK__
|
||||
temp_node);
|
||||
#else
|
||||
(Node *) temp_var);
|
||||
#endif /* _DROP_COLUMN_HACK__ */
|
||||
break;
|
||||
}
|
||||
(Node *) temp_var);
|
||||
#endif /* _DROP_COLUMN_HACK__ */
|
||||
break;
|
||||
}
|
||||
default:
|
||||
elog(ERROR, "expand_targetlist: unexpected command_type");
|
||||
break;
|
||||
@ -285,18 +292,19 @@ expand_targetlist(List *tlist, int command_type,
|
||||
|
||||
/*
|
||||
* Copy all unprocessed tlist entries to the end of the new tlist,
|
||||
* making sure they are marked resjunk = true. Typical junk entries
|
||||
* include ORDER BY or GROUP BY expressions (are these actually possible
|
||||
* in an INSERT or UPDATE?), system attribute references, etc.
|
||||
* making sure they are marked resjunk = true. Typical junk entries
|
||||
* include ORDER BY or GROUP BY expressions (are these actually
|
||||
* possible in an INSERT or UPDATE?), system attribute references,
|
||||
* etc.
|
||||
*/
|
||||
old_tlist_index = 0;
|
||||
foreach(temp, tlist)
|
||||
{
|
||||
TargetEntry *old_tle = (TargetEntry *) lfirst(temp);
|
||||
TargetEntry *old_tle = (TargetEntry *) lfirst(temp);
|
||||
|
||||
if (! tlistentry_used[old_tlist_index])
|
||||
if (!tlistentry_used[old_tlist_index])
|
||||
{
|
||||
Resdom *resdom;
|
||||
Resdom *resdom;
|
||||
|
||||
resdom = (Resdom *) copyObject((Node *) old_tle->resdom);
|
||||
resdom->resno = attrno++;
|
||||
|
@ -8,7 +8,7 @@
|
||||
*
|
||||
*
|
||||
* IDENTIFICATION
|
||||
* $Header: /cvsroot/pgsql/src/backend/optimizer/prep/prepunion.c,v 1.47 2000/03/21 05:12:06 tgl Exp $
|
||||
* $Header: /cvsroot/pgsql/src/backend/optimizer/prep/prepunion.c,v 1.48 2000/04/12 17:15:23 momjian Exp $
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
@ -26,7 +26,8 @@
|
||||
#include "parser/parsetree.h"
|
||||
#include "utils/lsyscache.h"
|
||||
|
||||
typedef struct {
|
||||
typedef struct
|
||||
{
|
||||
Index rt_index;
|
||||
int sublevels_up;
|
||||
Oid old_relid;
|
||||
@ -40,11 +41,11 @@ static RangeTblEntry *new_rangetable_entry(Oid new_relid,
|
||||
RangeTblEntry *old_entry);
|
||||
static void fix_parsetree_attnums(Index rt_index, Oid old_relid,
|
||||
Oid new_relid, Query *parsetree);
|
||||
static bool fix_parsetree_attnums_walker (Node *node,
|
||||
fix_parsetree_attnums_context *context);
|
||||
static bool fix_parsetree_attnums_walker(Node *node,
|
||||
fix_parsetree_attnums_context *context);
|
||||
static Append *make_append(List *appendplans, List *unionrtables,
|
||||
Index rt_index,
|
||||
List *inheritrtable, List *tlist);
|
||||
Index rt_index,
|
||||
List *inheritrtable, List *tlist);
|
||||
|
||||
|
||||
/*
|
||||
@ -122,11 +123,11 @@ plan_union_queries(Query *parse)
|
||||
/* Is this a simple one */
|
||||
if (!union_all_found ||
|
||||
!union_found ||
|
||||
/* A trailing UNION negates the effect of earlier UNION ALLs */
|
||||
/* A trailing UNION negates the effect of earlier UNION ALLs */
|
||||
!last_union_all_flag)
|
||||
{
|
||||
List *hold_unionClause = parse->unionClause;
|
||||
double tuple_fraction = -1.0; /* default processing */
|
||||
double tuple_fraction = -1.0; /* default processing */
|
||||
|
||||
/* we will do sorting later, so don't do it now */
|
||||
if (!union_all_found ||
|
||||
@ -134,6 +135,7 @@ plan_union_queries(Query *parse)
|
||||
{
|
||||
parse->sortClause = NIL;
|
||||
parse->distinctClause = NIL;
|
||||
|
||||
/*
|
||||
* force lower-level planning to assume that all tuples will
|
||||
* be retrieved, even if it sees a LIMIT in the query node.
|
||||
@ -149,8 +151,9 @@ plan_union_queries(Query *parse)
|
||||
{
|
||||
Query *union_query = lfirst(ulist);
|
||||
|
||||
/* use subquery_planner here because the union'd queries
|
||||
* have not been preprocessed yet. My goodness this is messy...
|
||||
/*
|
||||
* use subquery_planner here because the union'd queries have
|
||||
* not been preprocessed yet. My goodness this is messy...
|
||||
*/
|
||||
union_plans = lappend(union_plans,
|
||||
subquery_planner(union_query,
|
||||
@ -178,8 +181,8 @@ plan_union_queries(Query *parse)
|
||||
* Recursion, but UNION only. The last one is a UNION, so it will
|
||||
* not come here in recursion.
|
||||
*
|
||||
* XXX is it OK to pass default -1 to union_planner in this path,
|
||||
* or should we force a tuple_fraction value?
|
||||
* XXX is it OK to pass default -1 to union_planner in this path, or
|
||||
* should we force a tuple_fraction value?
|
||||
*/
|
||||
union_plans = lcons(union_planner(parse, -1.0), NIL);
|
||||
union_rts = lcons(parse->rtable, NIL);
|
||||
@ -189,11 +192,12 @@ plan_union_queries(Query *parse)
|
||||
{
|
||||
Query *union_all_query = lfirst(ulist);
|
||||
|
||||
/* use subquery_planner here because the union'd queries
|
||||
* have not been preprocessed yet. My goodness this is messy...
|
||||
/*
|
||||
* use subquery_planner here because the union'd queries have
|
||||
* not been preprocessed yet. My goodness this is messy...
|
||||
*/
|
||||
union_plans = lappend(union_plans,
|
||||
subquery_planner(union_all_query, -1.0));
|
||||
subquery_planner(union_all_query, -1.0));
|
||||
union_rts = lappend(union_rts, union_all_query->rtable);
|
||||
}
|
||||
}
|
||||
@ -201,9 +205,11 @@ plan_union_queries(Query *parse)
|
||||
/* We have already split UNION and UNION ALL and we made it consistent */
|
||||
if (!last_union_all_flag)
|
||||
{
|
||||
/* Need SELECT DISTINCT behavior to implement UNION.
|
||||
* Put back the held sortClause, add any missing columns to the
|
||||
* sort clause, and set distinctClause properly.
|
||||
|
||||
/*
|
||||
* Need SELECT DISTINCT behavior to implement UNION. Put back the
|
||||
* held sortClause, add any missing columns to the sort clause,
|
||||
* and set distinctClause properly.
|
||||
*/
|
||||
List *slitem;
|
||||
|
||||
@ -215,7 +221,7 @@ plan_union_queries(Query *parse)
|
||||
SortClause *scl = (SortClause *) lfirst(slitem);
|
||||
TargetEntry *tle = get_sortgroupclause_tle(scl, parse->targetList);
|
||||
|
||||
if (! tle->resdom->resjunk)
|
||||
if (!tle->resdom->resjunk)
|
||||
parse->distinctClause = lappend(parse->distinctClause,
|
||||
copyObject(scl));
|
||||
}
|
||||
@ -226,9 +232,10 @@ plan_union_queries(Query *parse)
|
||||
parse->distinctClause = NIL;
|
||||
}
|
||||
|
||||
/* Make sure we don't try to apply the first query's grouping stuff
|
||||
* to the Append node, either. Basically we don't want union_planner
|
||||
* to do anything when we return control, except add the top sort/unique
|
||||
/*
|
||||
* Make sure we don't try to apply the first query's grouping stuff to
|
||||
* the Append node, either. Basically we don't want union_planner to
|
||||
* do anything when we return control, except add the top sort/unique
|
||||
* nodes for DISTINCT processing if this wasn't UNION ALL, or the top
|
||||
* sort node if it was UNION ALL with a user-provided sort clause.
|
||||
*/
|
||||
@ -259,13 +266,13 @@ plan_union_queries(Query *parse)
|
||||
*
|
||||
* If grouping, aggregation, or sorting is specified in the parent plan,
|
||||
* the subplans should not do any of those steps --- we must do those
|
||||
* operations just once above the APPEND node. The given tlist has been
|
||||
* operations just once above the APPEND node. The given tlist has been
|
||||
* modified appropriately to remove group/aggregate expressions, but the
|
||||
* Query node still has the relevant fields set. We remove them in the
|
||||
* copies used for subplans (see plan_inherit_query).
|
||||
*
|
||||
* NOTE: this can be invoked recursively if more than one inheritance wildcard
|
||||
* is present. At each level of recursion, the first wildcard remaining in
|
||||
* is present. At each level of recursion, the first wildcard remaining in
|
||||
* the rangetable is expanded.
|
||||
*/
|
||||
Append *
|
||||
@ -282,8 +289,8 @@ plan_inherit_queries(Query *parse, List *tlist, Index rt_index)
|
||||
|
||||
/*
|
||||
* Remove the flag for this relation, since we're about to handle it.
|
||||
* XXX destructive change to parent parse tree, but necessary to prevent
|
||||
* infinite recursion.
|
||||
* XXX destructive change to parent parse tree, but necessary to
|
||||
* prevent infinite recursion.
|
||||
*/
|
||||
rt_entry->inh = false;
|
||||
|
||||
@ -313,42 +320,44 @@ plan_inherit_query(Relids relids,
|
||||
List *union_plans = NIL;
|
||||
List *union_rtentries = NIL;
|
||||
List *save_tlist = root->targetList;
|
||||
double tuple_fraction;
|
||||
double tuple_fraction;
|
||||
List *i;
|
||||
|
||||
/*
|
||||
* Avoid making copies of the root's tlist, which we aren't going to
|
||||
* use anyway (we are going to make copies of the passed tlist, instead).
|
||||
* use anyway (we are going to make copies of the passed tlist,
|
||||
* instead).
|
||||
*/
|
||||
root->targetList = NIL;
|
||||
|
||||
/*
|
||||
* If we are going to need sorting or grouping at the top level,
|
||||
* force lower-level planners to assume that all tuples will be
|
||||
* retrieved.
|
||||
* If we are going to need sorting or grouping at the top level, force
|
||||
* lower-level planners to assume that all tuples will be retrieved.
|
||||
*/
|
||||
if (root->distinctClause || root->sortClause ||
|
||||
root->groupClause || root->hasAggs)
|
||||
tuple_fraction = 0.0; /* will need all tuples from each subplan */
|
||||
tuple_fraction = 0.0; /* will need all tuples from each subplan */
|
||||
else
|
||||
tuple_fraction = -1.0; /* default behavior is OK (I think) */
|
||||
tuple_fraction = -1.0; /* default behavior is OK (I think) */
|
||||
|
||||
foreach(i, relids)
|
||||
{
|
||||
int relid = lfirsti(i);
|
||||
|
||||
/*
|
||||
* Make a modifiable copy of the original query,
|
||||
* and replace the target rangetable entry with a new one
|
||||
* identifying this child table.
|
||||
* Make a modifiable copy of the original query, and replace the
|
||||
* target rangetable entry with a new one identifying this child
|
||||
* table.
|
||||
*/
|
||||
Query *new_root = copyObject(root);
|
||||
RangeTblEntry *new_rt_entry = new_rangetable_entry(relid,
|
||||
rt_entry);
|
||||
|
||||
rt_store(rt_index, new_root->rtable, new_rt_entry);
|
||||
|
||||
/*
|
||||
* Insert (a modifiable copy of) the desired simplified tlist
|
||||
* into the subquery
|
||||
* Insert (a modifiable copy of) the desired simplified tlist into
|
||||
* the subquery
|
||||
*/
|
||||
new_root->targetList = copyObject(tlist);
|
||||
|
||||
@ -360,14 +369,15 @@ plan_inherit_query(Relids relids,
|
||||
new_root->sortClause = NIL;
|
||||
new_root->groupClause = NIL;
|
||||
new_root->havingQual = NULL;
|
||||
new_root->hasAggs = false; /* shouldn't be any left ... */
|
||||
new_root->hasAggs = false; /* shouldn't be any left ... */
|
||||
|
||||
/*
|
||||
* Update attribute numbers in case child has different ordering
|
||||
* of columns than parent (as can happen after ALTER TABLE).
|
||||
*
|
||||
* XXX This is a crock, and it doesn't really work. It'd be better
|
||||
* to fix ALTER TABLE to preserve consistency of attribute numbering.
|
||||
* to fix ALTER TABLE to preserve consistency of attribute
|
||||
* numbering.
|
||||
*/
|
||||
fix_parsetree_attnums(rt_index,
|
||||
rt_entry->relid,
|
||||
@ -397,23 +407,24 @@ find_all_inheritors(Oid parentrel)
|
||||
List *unexamined_relids = lconsi(parentrel, NIL);
|
||||
|
||||
/*
|
||||
* While the queue of unexamined relids is nonempty, remove the
|
||||
* first element, mark it examined, and find its direct descendants.
|
||||
* NB: cannot use foreach(), since we modify the queue inside loop.
|
||||
* While the queue of unexamined relids is nonempty, remove the first
|
||||
* element, mark it examined, and find its direct descendants. NB:
|
||||
* cannot use foreach(), since we modify the queue inside loop.
|
||||
*/
|
||||
while (unexamined_relids != NIL)
|
||||
{
|
||||
Oid currentrel = lfirsti(unexamined_relids);
|
||||
List *currentchildren;
|
||||
Oid currentrel = lfirsti(unexamined_relids);
|
||||
List *currentchildren;
|
||||
|
||||
unexamined_relids = lnext(unexamined_relids);
|
||||
examined_relids = lappendi(examined_relids, currentrel);
|
||||
currentchildren = find_inheritance_children(currentrel);
|
||||
|
||||
/*
|
||||
* Add to the queue only those children not already seen.
|
||||
* This could probably be simplified to a plain nconc,
|
||||
* because our inheritance relationships should always be a
|
||||
* strict tree, no? Should never find any matches, ISTM...
|
||||
* Add to the queue only those children not already seen. This
|
||||
* could probably be simplified to a plain nconc, because our
|
||||
* inheritance relationships should always be a strict tree, no?
|
||||
* Should never find any matches, ISTM...
|
||||
*/
|
||||
currentchildren = set_differencei(currentchildren, examined_relids);
|
||||
unexamined_relids = LispUnioni(unexamined_relids, currentchildren);
|
||||
@ -477,7 +488,7 @@ new_rangetable_entry(Oid new_relid, RangeTblEntry *old_entry)
|
||||
* 'old_relid' in 'parsetree' with the attribute numbers from
|
||||
* 'new_relid'.
|
||||
*
|
||||
* The parsetree is MODIFIED IN PLACE. This is OK only because
|
||||
* The parsetree is MODIFIED IN PLACE. This is OK only because
|
||||
* plan_inherit_query made a copy of the tree for us to hack upon.
|
||||
*/
|
||||
static void
|
||||
@ -495,6 +506,7 @@ fix_parsetree_attnums(Index rt_index,
|
||||
context.old_relid = old_relid;
|
||||
context.new_relid = new_relid;
|
||||
context.sublevels_up = 0;
|
||||
|
||||
/*
|
||||
* We must scan both the targetlist and qual, but we know the
|
||||
* havingQual is empty, so we can ignore it.
|
||||
@ -504,7 +516,7 @@ fix_parsetree_attnums(Index rt_index,
|
||||
}
|
||||
|
||||
/*
|
||||
* Adjust varnos for child tables. This routine makes it possible for
|
||||
* Adjust varnos for child tables. This routine makes it possible for
|
||||
* child tables to have different column positions for the "same" attribute
|
||||
* as a parent, which helps ALTER TABLE ADD COLUMN. Unfortunately this isn't
|
||||
* nearly enough to make it work transparently; there are other places where
|
||||
@ -513,8 +525,8 @@ fix_parsetree_attnums(Index rt_index,
|
||||
* ALTER TABLE...
|
||||
*/
|
||||
static bool
|
||||
fix_parsetree_attnums_walker (Node *node,
|
||||
fix_parsetree_attnums_context *context)
|
||||
fix_parsetree_attnums_walker(Node *node,
|
||||
fix_parsetree_attnums_context *context)
|
||||
{
|
||||
if (node == NULL)
|
||||
return false;
|
||||
@ -534,9 +546,10 @@ fix_parsetree_attnums_walker (Node *node,
|
||||
}
|
||||
if (IsA(node, SubLink))
|
||||
{
|
||||
|
||||
/*
|
||||
* Standard expression_tree_walker will not recurse into subselect,
|
||||
* but here we must do so.
|
||||
* Standard expression_tree_walker will not recurse into
|
||||
* subselect, but here we must do so.
|
||||
*/
|
||||
SubLink *sub = (SubLink *) node;
|
||||
|
||||
@ -588,9 +601,9 @@ make_append(List *appendplans,
|
||||
node->plan.plan_width = 0;
|
||||
foreach(subnode, appendplans)
|
||||
{
|
||||
Plan *subplan = (Plan *) lfirst(subnode);
|
||||
Plan *subplan = (Plan *) lfirst(subnode);
|
||||
|
||||
if (subnode == appendplans) /* first node? */
|
||||
if (subnode == appendplans) /* first node? */
|
||||
node->plan.startup_cost = subplan->startup_cost;
|
||||
node->plan.total_cost += subplan->total_cost;
|
||||
node->plan.plan_rows += subplan->plan_rows;
|
||||
|
Reference in New Issue
Block a user