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

Merge Resdom nodes into TargetEntry nodes to simplify code and save a

few palloc's.  I also chose to eliminate the restype and restypmod fields
entirely, since they are redundant with information stored in the node's
contained expression; re-examining the expression at need seems simpler
and more reliable than trying to keep restype/restypmod up to date.

initdb forced due to change in contents of stored rules.
This commit is contained in:
Tom Lane
2005-04-06 16:34:07 +00:00
parent 0f3748a28c
commit ad161bcc8a
43 changed files with 537 additions and 799 deletions

View File

@@ -16,7 +16,7 @@
*
*
* IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/optimizer/prep/prepjointree.c,v 1.25 2004/12/31 22:00:20 pgsql Exp $
* $PostgreSQL: pgsql/src/backend/optimizer/prep/prepjointree.c,v 1.26 2005/04/06 16:34:06 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@@ -458,7 +458,7 @@ has_nullable_targetlist(Query *subquery)
TargetEntry *tle = (TargetEntry *) lfirst(l);
/* ignore resjunk columns */
if (tle->resdom->resjunk)
if (tle->resjunk)
continue;
/* Must contain a Var of current level */

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.73 2005/03/17 23:44:52 neilc Exp $
* $PostgreSQL: pgsql/src/backend/optimizer/prep/preptlist.c,v 1.74 2005/04/06 16:34:06 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@@ -79,18 +79,17 @@ preprocess_targetlist(Query *parse, List *tlist)
*/
if (command_type == CMD_UPDATE || command_type == CMD_DELETE)
{
Resdom *resdom;
TargetEntry *tle;
Var *var;
resdom = makeResdom(list_length(tlist) + 1,
TIDOID,
-1,
pstrdup("ctid"),
true);
var = makeVar(result_relation, SelfItemPointerAttributeNumber,
TIDOID, -1, 0);
tle = makeTargetEntry((Expr *) var,
list_length(tlist) + 1,
pstrdup("ctid"),
true);
/*
* For an UPDATE, expand_targetlist already created a fresh tlist.
* For DELETE, better do a listCopy so that we don't destructively
@@ -99,7 +98,7 @@ preprocess_targetlist(Query *parse, List *tlist)
if (command_type == CMD_DELETE)
tlist = list_copy(tlist);
tlist = lappend(tlist, makeTargetEntry(resdom, (Expr *) var));
tlist = lappend(tlist, tle);
}
/*
@@ -132,18 +131,9 @@ preprocess_targetlist(Query *parse, List *tlist)
foreach(l, parse->rowMarks)
{
Index rti = lfirst_int(l);
char *resname;
Resdom *resdom;
Var *var;
TargetEntry *ctid;
resname = (char *) palloc(32);
snprintf(resname, 32, "ctid%u", rti);
resdom = makeResdom(list_length(tlist) + 1,
TIDOID,
-1,
resname,
true);
char *resname;
TargetEntry *tle;
var = makeVar(rti,
SelfItemPointerAttributeNumber,
@@ -151,8 +141,15 @@ preprocess_targetlist(Query *parse, List *tlist)
-1,
0);
ctid = makeTargetEntry(resdom, (Expr *) var);
tlist = lappend(tlist, ctid);
resname = (char *) palloc(32);
snprintf(resname, 32, "ctid%u", rti);
tle = makeTargetEntry((Expr *) var,
list_length(tlist) + 1,
resname,
true);
tlist = lappend(tlist, tle);
}
}
@@ -206,9 +203,8 @@ expand_targetlist(List *tlist, int command_type,
if (tlist_item != NULL)
{
TargetEntry *old_tle = (TargetEntry *) lfirst(tlist_item);
Resdom *resdom = old_tle->resdom;
if (!resdom->resjunk && resdom->resno == attrno)
if (!old_tle->resjunk && old_tle->resno == attrno)
{
new_tle = old_tle;
tlist_item = lnext(tlist_item);
@@ -268,9 +264,6 @@ expand_targetlist(List *tlist, int command_type,
(Datum) 0,
true, /* isnull */
true /* byval */ );
/* label resdom with INT4, too */
atttype = INT4OID;
atttypmod = -1;
}
break;
case CMD_UPDATE:
@@ -290,9 +283,6 @@ expand_targetlist(List *tlist, int command_type,
(Datum) 0,
true, /* isnull */
true /* byval */ );
/* label resdom with INT4, too */
atttype = INT4OID;
atttypmod = -1;
}
break;
default:
@@ -302,12 +292,10 @@ expand_targetlist(List *tlist, int command_type,
break;
}
new_tle = makeTargetEntry(makeResdom(attrno,
atttype,
atttypmod,
new_tle = makeTargetEntry((Expr *) new_expr,
attrno,
pstrdup(NameStr(att_tup->attname)),
false),
(Expr *) new_expr);
false);
}
new_tlist = lappend(new_tlist, new_tle);
@@ -324,16 +312,14 @@ expand_targetlist(List *tlist, int command_type,
while (tlist_item)
{
TargetEntry *old_tle = (TargetEntry *) lfirst(tlist_item);
Resdom *resdom = old_tle->resdom;
if (!resdom->resjunk)
if (!old_tle->resjunk)
elog(ERROR, "targetlist is not sorted correctly");
/* Get the resno right, but don't copy unnecessarily */
if (resdom->resno != attrno)
if (old_tle->resno != attrno)
{
resdom = (Resdom *) copyObject((Node *) resdom);
resdom->resno = attrno;
old_tle = makeTargetEntry(resdom, old_tle->expr);
old_tle = flatCopyTargetEntry(old_tle);
old_tle->resno = attrno;
}
new_tlist = lappend(new_tlist, old_tle);
attrno++;

View File

@@ -14,7 +14,7 @@
*
*
* IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/optimizer/prep/prepunion.c,v 1.119 2004/12/31 22:00:20 pgsql Exp $
* $PostgreSQL: pgsql/src/backend/optimizer/prep/prepunion.c,v 1.120 2005/04/06 16:34:06 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@@ -32,6 +32,7 @@
#include "optimizer/tlist.h"
#include "parser/parse_clause.h"
#include "parser/parse_coerce.h"
#include "parser/parse_expr.h"
#include "parser/parsetree.h"
#include "utils/lsyscache.h"
@@ -429,7 +430,7 @@ generate_setop_tlist(List *colTypes, int flag,
ListCell *i,
*j,
*k;
Resdom *resdom;
TargetEntry *tle;
Node *expr;
j = list_head(input_tlist);
@@ -439,12 +440,11 @@ generate_setop_tlist(List *colTypes, int flag,
Oid colType = lfirst_oid(i);
TargetEntry *inputtle = (TargetEntry *) lfirst(j);
TargetEntry *reftle = (TargetEntry *) lfirst(k);
int32 colTypmod;
Assert(inputtle->resdom->resno == resno);
Assert(reftle->resdom->resno == resno);
Assert(!inputtle->resdom->resjunk);
Assert(!reftle->resdom->resjunk);
Assert(inputtle->resno == resno);
Assert(reftle->resno == resno);
Assert(!inputtle->resjunk);
Assert(!reftle->resjunk);
/*
* Generate columns referencing input columns and having
@@ -463,29 +463,23 @@ generate_setop_tlist(List *colTypes, int flag,
expr = (Node *) inputtle->expr;
else
expr = (Node *) makeVar(0,
inputtle->resdom->resno,
inputtle->resdom->restype,
inputtle->resdom->restypmod,
inputtle->resno,
exprType((Node *) inputtle->expr),
exprTypmod((Node *) inputtle->expr),
0);
if (inputtle->resdom->restype == colType)
{
/* no coercion needed, and believe the input typmod */
colTypmod = inputtle->resdom->restypmod;
}
else
if (exprType(expr) != colType)
{
expr = coerce_to_common_type(NULL, /* no UNKNOWNs here */
expr,
colType,
"UNION/INTERSECT/EXCEPT");
colTypmod = -1;
}
resdom = makeResdom((AttrNumber) resno++,
colType,
colTypmod,
pstrdup(reftle->resdom->resname),
false);
tlist = lappend(tlist, makeTargetEntry(resdom, (Expr *) expr));
tle = makeTargetEntry((Expr *) expr,
(AttrNumber) resno++,
pstrdup(reftle->resname),
false);
tlist = lappend(tlist, tle);
j = lnext(j);
k = lnext(k);
}
@@ -493,18 +487,17 @@ generate_setop_tlist(List *colTypes, int flag,
if (flag >= 0)
{
/* Add a resjunk flag column */
resdom = makeResdom((AttrNumber) resno++,
INT4OID,
-1,
pstrdup("flag"),
true);
/* flag value is the given constant */
expr = (Node *) makeConst(INT4OID,
sizeof(int4),
Int32GetDatum(flag),
false,
true);
tlist = lappend(tlist, makeTargetEntry(resdom, (Expr *) expr));
tle = makeTargetEntry((Expr *) expr,
(AttrNumber) resno++,
pstrdup("flag"),
true);
tlist = lappend(tlist, tle);
}
return tlist;
@@ -531,7 +524,7 @@ generate_append_tlist(List *colTypes, bool flag,
ListCell *curColType;
ListCell *ref_tl_item;
int colindex;
Resdom *resdom;
TargetEntry *tle;
Node *expr;
ListCell *planl;
int32 *colTypmods;
@@ -555,15 +548,17 @@ generate_append_tlist(List *colTypes, bool flag,
{
TargetEntry *subtle = (TargetEntry *) lfirst(subtlist);
if (subtle->resdom->resjunk)
if (subtle->resjunk)
continue;
Assert(curColType != NULL);
if (subtle->resdom->restype == lfirst_oid(curColType))
if (exprType((Node *) subtle->expr) == lfirst_oid(curColType))
{
/* If first subplan, copy the typmod; else compare */
int32 subtypmod = exprTypmod((Node *) subtle->expr);
if (planl == list_head(input_plans))
colTypmods[colindex] = subtle->resdom->restypmod;
else if (subtle->resdom->restypmod != colTypmods[colindex])
colTypmods[colindex] = subtypmod;
else if (subtypmod != colTypmods[colindex])
colTypmods[colindex] = -1;
}
else
@@ -587,36 +582,34 @@ generate_append_tlist(List *colTypes, bool flag,
int32 colTypmod = colTypmods[colindex++];
TargetEntry *reftle = (TargetEntry *) lfirst(ref_tl_item);
Assert(reftle->resdom->resno == resno);
Assert(!reftle->resdom->resjunk);
Assert(reftle->resno == resno);
Assert(!reftle->resjunk);
expr = (Node *) makeVar(0,
resno,
colType,
colTypmod,
0);
resdom = makeResdom((AttrNumber) resno++,
colType,
colTypmod,
pstrdup(reftle->resdom->resname),
false);
tlist = lappend(tlist, makeTargetEntry(resdom, (Expr *) expr));
tle = makeTargetEntry((Expr *) expr,
(AttrNumber) resno++,
pstrdup(reftle->resname),
false);
tlist = lappend(tlist, tle);
}
if (flag)
{
/* Add a resjunk flag column */
resdom = makeResdom((AttrNumber) resno++,
INT4OID,
-1,
pstrdup("flag"),
true);
/* flag value is shown as copied up from subplan */
expr = (Node *) makeVar(0,
resdom->resno,
resno,
INT4OID,
-1,
0);
tlist = lappend(tlist, makeTargetEntry(resdom, (Expr *) expr));
tle = makeTargetEntry((Expr *) expr,
(AttrNumber) resno++,
pstrdup("flag"),
true);
tlist = lappend(tlist, tle);
}
pfree(colTypmods);
@@ -640,7 +633,7 @@ tlist_same_datatypes(List *tlist, List *colTypes, bool junkOK)
{
TargetEntry *tle = (TargetEntry *) lfirst(l);
if (tle->resdom->resjunk)
if (tle->resjunk)
{
if (!junkOK)
return false;
@@ -649,7 +642,7 @@ tlist_same_datatypes(List *tlist, List *colTypes, bool junkOK)
{
if (curColType == NULL)
return false;
if (tle->resdom->restype != lfirst_oid(curColType))
if (exprType((Node *) tle->expr) != lfirst_oid(curColType))
return false;
curColType = lnext(curColType);
}
@@ -1105,8 +1098,7 @@ adjust_relid_set(Relids relids, Index oldrelid, Index newrelid)
*
* The given tlist has already been through expression_tree_mutator;
* therefore the TargetEntry nodes are fresh copies that it's okay to
* scribble on. But the Resdom nodes have not been copied; make new ones
* if we need to change them!
* scribble on.
*
* Note that this is not needed for INSERT because INSERT isn't inheritable.
*/
@@ -1124,18 +1116,15 @@ adjust_inherited_tlist(List *tlist,
foreach(tl, tlist)
{
TargetEntry *tle = (TargetEntry *) lfirst(tl);
Resdom *resdom = tle->resdom;
if (resdom->resjunk)
if (tle->resjunk)
continue; /* ignore junk items */
attrno = translate_inherited_attnum(resdom->resno, context);
attrno = translate_inherited_attnum(tle->resno, context);
if (resdom->resno != attrno)
if (tle->resno != attrno)
{
resdom = (Resdom *) copyObject((Node *) resdom);
resdom->resno = attrno;
tle->resdom = resdom;
tle->resno = attrno;
changed_it = true;
}
}
@@ -1157,14 +1146,13 @@ adjust_inherited_tlist(List *tlist,
foreach(tl, tlist)
{
TargetEntry *tle = (TargetEntry *) lfirst(tl);
Resdom *resdom = tle->resdom;
if (resdom->resjunk)
if (tle->resjunk)
continue; /* ignore junk items */
if (resdom->resno == attrno)
if (tle->resno == attrno)
new_tlist = lappend(new_tlist, tle);
else if (resdom->resno > attrno)
else if (tle->resno > attrno)
more = true;
}
}
@@ -1172,17 +1160,11 @@ adjust_inherited_tlist(List *tlist,
foreach(tl, tlist)
{
TargetEntry *tle = (TargetEntry *) lfirst(tl);
Resdom *resdom = tle->resdom;
if (!resdom->resjunk)
if (!tle->resjunk)
continue; /* here, ignore non-junk items */
if (resdom->resno != attrno)
{
resdom = (Resdom *) copyObject((Node *) resdom);
resdom->resno = attrno;
tle->resdom = resdom;
}
tle->resno = attrno;
new_tlist = lappend(new_tlist, tle);
attrno++;
}