1996-07-09 08:22:35 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
1999-02-14 00:22:53 +01:00
|
|
|
* preptlist.c
|
1997-09-07 07:04:48 +02:00
|
|
|
* Routines to preprocess the parse tree target list
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
1999-10-31 01:06:32 +02:00
|
|
|
* This module takes care of altering the query targetlist as needed for
|
2000-04-12 19:17:23 +02:00
|
|
|
* INSERT, UPDATE, and DELETE queries. For INSERT and UPDATE queries,
|
1999-10-31 01:06:32 +02:00
|
|
|
* 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
|
|
|
|
* executor relies on this to find the tuple to be replaced/deleted.
|
|
|
|
*
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
2004-12-31 23:04:05 +01:00
|
|
|
* Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
|
2000-01-26 06:58:53 +01:00
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
2005-04-06 18:34:07 +02:00
|
|
|
* $PostgreSQL: pgsql/src/backend/optimizer/prep/preptlist.c,v 1.74 2005/04/06 16:34:06 tgl Exp $
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
1999-10-31 01:06:32 +02:00
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
#include "postgres.h"
|
|
|
|
|
1999-10-31 01:06:32 +02:00
|
|
|
#include "access/heapam.h"
|
1999-07-16 07:00:38 +02:00
|
|
|
#include "catalog/pg_type.h"
|
|
|
|
#include "nodes/makefuncs.h"
|
|
|
|
#include "optimizer/prep.h"
|
2005-03-18 00:45:09 +01:00
|
|
|
#include "optimizer/subselect.h"
|
|
|
|
#include "parser/analyze.h"
|
1999-07-16 07:00:38 +02:00
|
|
|
#include "parser/parsetree.h"
|
2002-09-01 00:10:48 +02:00
|
|
|
#include "parser/parse_coerce.h"
|
1996-07-09 08:22:35 +02:00
|
|
|
|
1999-10-31 01:06:32 +02:00
|
|
|
|
|
|
|
static List *expand_targetlist(List *tlist, int command_type,
|
2000-04-12 19:17:23 +02:00
|
|
|
Index result_relation, List *range_table);
|
1996-07-09 08:22:35 +02:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
/*
|
1999-02-14 00:22:53 +01:00
|
|
|
* preprocess_targetlist
|
1997-09-07 07:04:48 +02:00
|
|
|
* Driver for preprocessing the parse tree targetlist.
|
|
|
|
*
|
|
|
|
* Returns the new targetlist.
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
1998-02-26 05:46:47 +01:00
|
|
|
List *
|
2005-03-18 00:45:09 +01:00
|
|
|
preprocess_targetlist(Query *parse, List *tlist)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2005-03-18 00:45:09 +01:00
|
|
|
int result_relation = parse->resultRelation;
|
|
|
|
List *range_table = parse->rtable;
|
|
|
|
CmdType command_type = parse->commandType;
|
|
|
|
|
2000-10-05 21:11:39 +02:00
|
|
|
/*
|
2001-03-22 05:01:46 +01:00
|
|
|
* Sanity check: if there is a result relation, it'd better be a real
|
|
|
|
* relation not a subquery. Else parser or rewriter messed up.
|
2000-10-05 21:11:39 +02:00
|
|
|
*/
|
|
|
|
if (result_relation)
|
|
|
|
{
|
|
|
|
RangeTblEntry *rte = rt_fetch(result_relation, range_table);
|
|
|
|
|
|
|
|
if (rte->subquery != NULL || rte->relid == InvalidOid)
|
2003-07-25 02:01:09 +02:00
|
|
|
elog(ERROR, "subquery cannot be result relation");
|
2000-10-05 21:11:39 +02:00
|
|
|
}
|
2000-04-12 19:17:23 +02:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
/*
|
|
|
|
* for heap_formtuple to work, the targetlist must match the exact
|
1999-10-31 01:06:32 +02:00
|
|
|
* order of the attributes. We also need to fill in any missing
|
|
|
|
* attributes. -ay 10/94
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
1999-10-31 01:06:32 +02:00
|
|
|
if (command_type == CMD_INSERT || command_type == CMD_UPDATE)
|
|
|
|
tlist = expand_targetlist(tlist, command_type,
|
|
|
|
result_relation, range_table);
|
|
|
|
|
|
|
|
/*
|
2000-04-12 19:17:23 +02:00
|
|
|
* 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.
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
|
|
|
if (command_type == CMD_UPDATE || command_type == CMD_DELETE)
|
|
|
|
{
|
2005-04-06 18:34:07 +02:00
|
|
|
TargetEntry *tle;
|
1997-09-08 04:41:22 +02:00
|
|
|
Var *var;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1999-10-31 01:06:32 +02:00
|
|
|
var = makeVar(result_relation, SelfItemPointerAttributeNumber,
|
|
|
|
TIDOID, -1, 0);
|
|
|
|
|
2005-04-06 18:34:07 +02:00
|
|
|
tle = makeTargetEntry((Expr *) var,
|
|
|
|
list_length(tlist) + 1,
|
|
|
|
pstrdup("ctid"),
|
|
|
|
true);
|
|
|
|
|
2000-04-12 19:17:23 +02:00
|
|
|
/*
|
|
|
|
* For an UPDATE, expand_targetlist already created a fresh tlist.
|
1999-10-31 01:06:32 +02:00
|
|
|
* For DELETE, better do a listCopy so that we don't destructively
|
|
|
|
* modify the original tlist (is this really necessary?).
|
|
|
|
*/
|
|
|
|
if (command_type == CMD_DELETE)
|
2004-05-31 01:40:41 +02:00
|
|
|
tlist = list_copy(tlist);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2005-04-06 18:34:07 +02:00
|
|
|
tlist = lappend(tlist, tle);
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
|
|
|
|
2005-03-18 00:45:09 +01:00
|
|
|
/*
|
|
|
|
* Add TID targets for rels selected FOR UPDATE. The executor
|
|
|
|
* uses the TID to know which rows to lock, much as for UPDATE or
|
|
|
|
* DELETE.
|
|
|
|
*/
|
|
|
|
if (parse->rowMarks)
|
|
|
|
{
|
|
|
|
ListCell *l;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We've got trouble if the FOR UPDATE appears inside
|
|
|
|
* grouping, since grouping renders a reference to individual
|
|
|
|
* tuple CTIDs invalid. This is also checked at parse time,
|
|
|
|
* but that's insufficient because of rule substitution, query
|
|
|
|
* pullup, etc.
|
|
|
|
*/
|
|
|
|
CheckSelectForUpdate(parse);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Currently the executor only supports FOR UPDATE at top
|
|
|
|
* level
|
|
|
|
*/
|
|
|
|
if (PlannerQueryLevel > 1)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
|
|
|
|
errmsg("SELECT FOR UPDATE is not allowed in subqueries")));
|
|
|
|
|
|
|
|
foreach(l, parse->rowMarks)
|
|
|
|
{
|
|
|
|
Index rti = lfirst_int(l);
|
|
|
|
Var *var;
|
2005-04-06 18:34:07 +02:00
|
|
|
char *resname;
|
|
|
|
TargetEntry *tle;
|
2005-03-18 00:45:09 +01:00
|
|
|
|
|
|
|
var = makeVar(rti,
|
|
|
|
SelfItemPointerAttributeNumber,
|
|
|
|
TIDOID,
|
|
|
|
-1,
|
|
|
|
0);
|
|
|
|
|
2005-04-06 18:34:07 +02:00
|
|
|
resname = (char *) palloc(32);
|
|
|
|
snprintf(resname, 32, "ctid%u", rti);
|
|
|
|
|
|
|
|
tle = makeTargetEntry((Expr *) var,
|
|
|
|
list_length(tlist) + 1,
|
|
|
|
resname,
|
|
|
|
true);
|
|
|
|
|
|
|
|
tlist = lappend(tlist, tle);
|
2005-03-18 00:45:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-10-31 01:06:32 +02:00
|
|
|
return tlist;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*****************************************************************************
|
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* TARGETLIST EXPANSION
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
*****************************************************************************/
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
/*
|
1999-02-14 00:22:53 +01:00
|
|
|
* expand_targetlist
|
1997-09-07 07:04:48 +02:00
|
|
|
* Given a target list as generated by the parser and a result relation,
|
2002-04-05 07:47:05 +02:00
|
|
|
* add targetlist entries for any missing attributes, and ensure the
|
|
|
|
* non-junk attributes appear in proper field order.
|
|
|
|
*
|
|
|
|
* NOTE: if you are tempted to put more processing here, consider whether
|
|
|
|
* it shouldn't go in the rewriter's rewriteTargetList() instead.
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
1997-09-08 04:41:22 +02:00
|
|
|
static List *
|
1999-10-31 01:06:32 +02:00
|
|
|
expand_targetlist(List *tlist, int command_type,
|
|
|
|
Index result_relation, List *range_table)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1999-10-31 01:06:32 +02:00
|
|
|
List *new_tlist = NIL;
|
2004-05-26 06:41:50 +02:00
|
|
|
ListCell *tlist_item;
|
1999-10-31 01:06:32 +02:00
|
|
|
Relation rel;
|
|
|
|
int attrno,
|
2002-04-05 07:47:05 +02:00
|
|
|
numattrs;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2004-05-26 06:41:50 +02:00
|
|
|
tlist_item = list_head(tlist);
|
|
|
|
|
1999-10-31 01:06:32 +02:00
|
|
|
/*
|
2002-04-05 07:47:05 +02:00
|
|
|
* The rewriter should have already ensured that the TLEs are in
|
2002-09-04 22:31:48 +02:00
|
|
|
* correct order; but we have to insert TLEs for any missing
|
|
|
|
* attributes.
|
2000-07-22 08:19:04 +02:00
|
|
|
*
|
1999-10-31 01:06:32 +02:00
|
|
|
* Scan the tuple description in the relation's relcache entry to make
|
|
|
|
* sure we have all the user attributes in the right order.
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
1999-10-31 01:06:32 +02:00
|
|
|
rel = heap_open(getrelid(result_relation, range_table), AccessShareLock);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1999-10-31 01:06:32 +02:00
|
|
|
numattrs = RelationGetNumberOfAttributes(rel);
|
|
|
|
|
|
|
|
for (attrno = 1; attrno <= numattrs; attrno++)
|
|
|
|
{
|
2000-04-12 19:17:23 +02:00
|
|
|
Form_pg_attribute att_tup = rel->rd_att->attrs[attrno - 1];
|
|
|
|
TargetEntry *new_tle = NULL;
|
1999-08-21 05:49:17 +02:00
|
|
|
|
2004-05-26 06:41:50 +02:00
|
|
|
if (tlist_item != NULL)
|
1999-05-12 17:02:39 +02:00
|
|
|
{
|
2004-05-26 06:41:50 +02:00
|
|
|
TargetEntry *old_tle = (TargetEntry *) lfirst(tlist_item);
|
1999-05-12 17:02:39 +02:00
|
|
|
|
2005-04-06 18:34:07 +02:00
|
|
|
if (!old_tle->resjunk && old_tle->resno == attrno)
|
1999-05-12 17:02:39 +02:00
|
|
|
{
|
2002-04-05 07:47:05 +02:00
|
|
|
new_tle = old_tle;
|
2004-05-26 06:41:50 +02:00
|
|
|
tlist_item = lnext(tlist_item);
|
1999-05-12 17:02:39 +02:00
|
|
|
}
|
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
|
1999-10-31 01:06:32 +02:00
|
|
|
if (new_tle == NULL)
|
1999-05-29 03:48:06 +02:00
|
|
|
{
|
1999-10-31 01:06:32 +02:00
|
|
|
/*
|
|
|
|
* Didn't find a matching tlist entry, so make one.
|
|
|
|
*
|
2002-09-04 22:31:48 +02:00
|
|
|
* For INSERT, generate a NULL constant. (We assume the rewriter
|
|
|
|
* would have inserted any available default value.) Also, if
|
|
|
|
* the column isn't dropped, apply any domain constraints that
|
|
|
|
* might exist --- this is to catch domain NOT NULL.
|
1999-10-31 01:06:32 +02:00
|
|
|
*
|
2000-04-12 19:17:23 +02:00
|
|
|
* For UPDATE, generate a Var reference to the existing value of
|
2002-09-04 22:31:48 +02:00
|
|
|
* the attribute, so that it gets copied to the new tuple. But
|
|
|
|
* generate a NULL for dropped columns (we want to drop any
|
2002-08-02 20:15:10 +02:00
|
|
|
* old values).
|
2003-05-12 02:17:03 +02:00
|
|
|
*
|
|
|
|
* When generating a NULL constant for a dropped column, we label
|
|
|
|
* it INT4 (any other guaranteed-to-exist datatype would do as
|
2003-08-04 02:43:34 +02:00
|
|
|
* well). We can't label it with the dropped column's
|
|
|
|
* datatype since that might not exist anymore. It does not
|
|
|
|
* really matter what we claim the type is, since NULL is NULL
|
|
|
|
* --- its representation is datatype-independent. This could
|
|
|
|
* perhaps confuse code comparing the finished plan to the
|
|
|
|
* target relation, however.
|
1999-10-31 01:06:32 +02:00
|
|
|
*/
|
|
|
|
Oid atttype = att_tup->atttypid;
|
|
|
|
int32 atttypmod = att_tup->atttypmod;
|
2001-11-02 21:23:02 +01:00
|
|
|
Node *new_expr;
|
1996-07-09 08:22:35 +02:00
|
|
|
|
1999-10-31 01:06:32 +02:00
|
|
|
switch (command_type)
|
|
|
|
{
|
|
|
|
case CMD_INSERT:
|
2002-09-01 00:10:48 +02:00
|
|
|
if (!att_tup->attisdropped)
|
2003-05-12 02:17:03 +02:00
|
|
|
{
|
|
|
|
new_expr = (Node *) makeConst(atttype,
|
|
|
|
att_tup->attlen,
|
|
|
|
(Datum) 0,
|
2003-08-04 02:43:34 +02:00
|
|
|
true, /* isnull */
|
2003-05-12 02:17:03 +02:00
|
|
|
att_tup->attbyval);
|
2003-02-03 22:15:45 +01:00
|
|
|
new_expr = coerce_to_domain(new_expr,
|
|
|
|
InvalidOid,
|
|
|
|
atttype,
|
2004-06-16 03:27:00 +02:00
|
|
|
COERCE_IMPLICIT_CAST,
|
2004-11-06 18:46:38 +01:00
|
|
|
false,
|
2004-06-16 03:27:00 +02:00
|
|
|
false);
|
2003-05-12 02:17:03 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Insert NULL for dropped column */
|
|
|
|
new_expr = (Node *) makeConst(INT4OID,
|
|
|
|
sizeof(int32),
|
|
|
|
(Datum) 0,
|
2003-08-04 02:43:34 +02:00
|
|
|
true, /* isnull */
|
|
|
|
true /* byval */ );
|
2003-05-12 02:17:03 +02:00
|
|
|
}
|
2001-11-02 21:23:02 +01:00
|
|
|
break;
|
1999-10-31 01:06:32 +02:00
|
|
|
case CMD_UPDATE:
|
2003-05-12 02:17:03 +02:00
|
|
|
if (!att_tup->attisdropped)
|
|
|
|
{
|
2002-08-02 20:15:10 +02:00
|
|
|
new_expr = (Node *) makeVar(result_relation,
|
|
|
|
attrno,
|
|
|
|
atttype,
|
|
|
|
atttypmod,
|
|
|
|
0);
|
2003-05-12 02:17:03 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Insert NULL for dropped column */
|
|
|
|
new_expr = (Node *) makeConst(INT4OID,
|
|
|
|
sizeof(int32),
|
|
|
|
(Datum) 0,
|
2003-08-04 02:43:34 +02:00
|
|
|
true, /* isnull */
|
|
|
|
true /* byval */ );
|
2003-05-12 02:17:03 +02:00
|
|
|
}
|
2001-11-02 21:23:02 +01:00
|
|
|
break;
|
1999-10-31 01:06:32 +02:00
|
|
|
default:
|
2003-07-25 02:01:09 +02:00
|
|
|
elog(ERROR, "unrecognized command_type: %d",
|
|
|
|
(int) command_type);
|
2001-11-05 18:46:40 +01:00
|
|
|
new_expr = NULL; /* keep compiler quiet */
|
1999-10-31 01:06:32 +02:00
|
|
|
break;
|
|
|
|
}
|
2001-11-02 21:23:02 +01:00
|
|
|
|
2005-04-06 18:34:07 +02:00
|
|
|
new_tle = makeTargetEntry((Expr *) new_expr,
|
|
|
|
attrno,
|
2002-09-04 22:31:48 +02:00
|
|
|
pstrdup(NameStr(att_tup->attname)),
|
2005-04-06 18:34:07 +02:00
|
|
|
false);
|
1999-10-31 01:06:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
new_tlist = lappend(new_tlist, new_tle);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2002-04-05 07:47:05 +02:00
|
|
|
* The remaining tlist entries should be resjunk; append them all to
|
|
|
|
* the end of the new tlist, making sure they have resnos higher than
|
|
|
|
* the last real attribute. (Note: although the rewriter already did
|
|
|
|
* such renumbering, we have to do it again here in case we are doing
|
2002-08-02 20:15:10 +02:00
|
|
|
* an UPDATE in a table with dropped columns, or an inheritance child
|
|
|
|
* table with extra columns.)
|
1999-10-31 01:06:32 +02:00
|
|
|
*/
|
2004-05-26 06:41:50 +02:00
|
|
|
while (tlist_item)
|
1999-10-31 01:06:32 +02:00
|
|
|
{
|
2004-05-26 06:41:50 +02:00
|
|
|
TargetEntry *old_tle = (TargetEntry *) lfirst(tlist_item);
|
1999-10-31 01:06:32 +02:00
|
|
|
|
2005-04-06 18:34:07 +02:00
|
|
|
if (!old_tle->resjunk)
|
2003-07-25 02:01:09 +02:00
|
|
|
elog(ERROR, "targetlist is not sorted correctly");
|
2002-04-05 07:47:05 +02:00
|
|
|
/* Get the resno right, but don't copy unnecessarily */
|
2005-04-06 18:34:07 +02:00
|
|
|
if (old_tle->resno != attrno)
|
1999-10-31 01:06:32 +02:00
|
|
|
{
|
2005-04-06 18:34:07 +02:00
|
|
|
old_tle = flatCopyTargetEntry(old_tle);
|
|
|
|
old_tle->resno = attrno;
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
2002-04-05 07:47:05 +02:00
|
|
|
new_tlist = lappend(new_tlist, old_tle);
|
|
|
|
attrno++;
|
2004-05-26 06:41:50 +02:00
|
|
|
tlist_item = lnext(tlist_item);
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
|
|
|
|
1999-10-31 01:06:32 +02:00
|
|
|
heap_close(rel, AccessShareLock);
|
|
|
|
|
|
|
|
return new_tlist;
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|