preptlist.c 7.48 KB
Newer Older
1 2
/*-------------------------------------------------------------------------
 *
3
 * preptlist.c
4
 *	  Routines to preprocess the parse tree target list
5
 *
6
 * This module takes care of altering the query targetlist as needed for
7
 * INSERT, UPDATE, and DELETE queries.	For INSERT and UPDATE queries,
8 9 10 11 12
 * 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.
 *
13
 *
Bruce Momjian's avatar
Bruce Momjian committed
14
 * Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
Bruce Momjian's avatar
Add:  
Bruce Momjian committed
15
 * Portions Copyright (c) 1994, Regents of the University of California
16 17
 *
 * IDENTIFICATION
18
 *	  $Header: /cvsroot/pgsql/src/backend/optimizer/prep/preptlist.c,v 1.58 2002/11/25 21:29:40 tgl Exp $
19 20 21
 *
 *-------------------------------------------------------------------------
 */
22

23 24
#include "postgres.h"

25
#include "access/heapam.h"
Bruce Momjian's avatar
Bruce Momjian committed
26 27 28 29
#include "catalog/pg_type.h"
#include "nodes/makefuncs.h"
#include "optimizer/prep.h"
#include "parser/parsetree.h"
30
#include "parser/parse_coerce.h"
31

32 33

static List *expand_targetlist(List *tlist, int command_type,
34
				  Index result_relation, List *range_table);
35

36 37

/*
38
 * preprocess_targetlist
39 40 41
 *	  Driver for preprocessing the parse tree targetlist.
 *
 *	  Returns the new targetlist.
42
 */
43
List *
44
preprocess_targetlist(List *tlist,
45 46
					  int command_type,
					  Index result_relation,
47
					  List *range_table)
48
{
49
	/*
50 51
	 * Sanity check: if there is a result relation, it'd better be a real
	 * relation not a subquery.  Else parser or rewriter messed up.
52 53 54 55 56 57 58 59
	 */
	if (result_relation)
	{
		RangeTblEntry *rte = rt_fetch(result_relation, range_table);

		if (rte->subquery != NULL || rte->relid == InvalidOid)
			elog(ERROR, "preprocess_targetlist: subquery cannot be result relation");
	}
60

61 62
	/*
	 * for heap_formtuple to work, the targetlist must match the exact
63 64
	 * order of the attributes. We also need to fill in any missing
	 * attributes.							-ay 10/94
65
	 */
66 67 68 69 70
	if (command_type == CMD_INSERT || command_type == CMD_UPDATE)
		tlist = expand_targetlist(tlist, command_type,
								  result_relation, range_table);

	/*
71 72 73 74 75
	 * 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.
76 77 78
	 */
	if (command_type == CMD_UPDATE || command_type == CMD_DELETE)
	{
79 80
		Resdom	   *resdom;
		Var		   *var;
81

82
		resdom = makeResdom(length(tlist) + 1,
83
							TIDOID,
84
							-1,
85
							pstrdup("ctid"),
Bruce Momjian's avatar
Bruce Momjian committed
86
							true);
87

88 89 90
		var = makeVar(result_relation, SelfItemPointerAttributeNumber,
					  TIDOID, -1, 0);

91 92
		/*
		 * For an UPDATE, expand_targetlist already created a fresh tlist.
93 94 95 96 97
		 * 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)
			tlist = listCopy(tlist);
98

99
		tlist = lappend(tlist, makeTargetEntry(resdom, (Node *) var));
100 101
	}

102
	return tlist;
103 104 105 106
}

/*****************************************************************************
 *
107
 *		TARGETLIST EXPANSION
108 109 110
 *
 *****************************************************************************/

111
/*
112
 * expand_targetlist
113
 *	  Given a target list as generated by the parser and a result relation,
114 115 116 117 118
 *	  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.
119
 */
120
static List *
121 122
expand_targetlist(List *tlist, int command_type,
				  Index result_relation, List *range_table)
123
{
124 125 126
	List	   *new_tlist = NIL;
	Relation	rel;
	int			attrno,
127
				numattrs;
128

129
	/*
130
	 * The rewriter should have already ensured that the TLEs are in
Bruce Momjian's avatar
Bruce Momjian committed
131 132
	 * correct order; but we have to insert TLEs for any missing
	 * attributes.
133
	 *
134 135
	 * Scan the tuple description in the relation's relcache entry to make
	 * sure we have all the user attributes in the right order.
136
	 */
137
	rel = heap_open(getrelid(result_relation, range_table), AccessShareLock);
138

139 140 141 142
	numattrs = RelationGetNumberOfAttributes(rel);

	for (attrno = 1; attrno <= numattrs; attrno++)
	{
143 144
		Form_pg_attribute att_tup = rel->rd_att->attrs[attrno - 1];
		TargetEntry *new_tle = NULL;
145

146
		if (tlist != NIL)
147
		{
148
			TargetEntry *old_tle = (TargetEntry *) lfirst(tlist);
149
			Resdom	   *resdom = old_tle->resdom;
150

151
			if (!resdom->resjunk && resdom->resno == attrno)
152
			{
153 154 155 156
				Assert(strcmp(resdom->resname,
							  NameStr(att_tup->attname)) == 0);
				new_tle = old_tle;
				tlist = lnext(tlist);
157 158
			}
		}
159

160
		if (new_tle == NULL)
161
		{
162 163 164
			/*
			 * Didn't find a matching tlist entry, so make one.
			 *
Bruce Momjian's avatar
Bruce Momjian committed
165 166 167 168
			 * 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.
169
			 *
170
			 * For UPDATE, generate a Var reference to the existing value of
Bruce Momjian's avatar
Bruce Momjian committed
171 172
			 * the attribute, so that it gets copied to the new tuple. But
			 * generate a NULL for dropped columns (we want to drop any
173
			 * old values).
174 175 176
			 */
			Oid			atttype = att_tup->atttypid;
			int32		atttypmod = att_tup->atttypmod;
177
			Node	   *new_expr;
178

179 180 181
			switch (command_type)
			{
				case CMD_INSERT:
182 183 184
					new_expr = (Node *) makeConst(atttype,
												  att_tup->attlen,
												  (Datum) 0,
Bruce Momjian's avatar
Bruce Momjian committed
185
												  true, /* isnull */
186
												  att_tup->attbyval);
187
					if (!att_tup->attisdropped)
188 189 190
						new_expr = coerce_type_constraints(new_expr,
														   atttype,
														   COERCE_IMPLICIT_CAST);
191
					break;
192
				case CMD_UPDATE:
193 194 195 196 197 198
					/* Insert NULLs for dropped columns */
					if (att_tup->attisdropped)
						new_expr = (Node *) makeConst(atttype,
													  att_tup->attlen,
													  (Datum) 0,
													  true,		/* isnull */
199
													  att_tup->attbyval);
200 201 202 203 204 205
					else
						new_expr = (Node *) makeVar(result_relation,
													attrno,
													atttype,
													atttypmod,
													0);
206
					break;
207 208
				default:
					elog(ERROR, "expand_targetlist: unexpected command_type");
209
					new_expr = NULL;	/* keep compiler quiet */
210 211
					break;
			}
212 213 214 215

			new_tle = makeTargetEntry(makeResdom(attrno,
												 atttype,
												 atttypmod,
Bruce Momjian's avatar
Bruce Momjian committed
216
									  pstrdup(NameStr(att_tup->attname)),
217 218
												 false),
									  new_expr);
219 220 221 222 223 224
		}

		new_tlist = lappend(new_tlist, new_tle);
	}

	/*
225 226 227 228
	 * 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
229 230
	 * an UPDATE in a table with dropped columns, or an inheritance child
	 * table with extra columns.)
231
	 */
232
	while (tlist)
233
	{
234 235
		TargetEntry *old_tle = (TargetEntry *) lfirst(tlist);
		Resdom	   *resdom = old_tle->resdom;
236

237 238 239 240
		if (!resdom->resjunk)
			elog(ERROR, "expand_targetlist: targetlist is not sorted correctly");
		/* Get the resno right, but don't copy unnecessarily */
		if (resdom->resno != attrno)
241
		{
242 243 244
			resdom = (Resdom *) copyObject((Node *) resdom);
			resdom->resno = attrno;
			old_tle = makeTargetEntry(resdom, old_tle->expr);
245
		}
246 247 248
		new_tlist = lappend(new_tlist, old_tle);
		attrno++;
		tlist = lnext(tlist);
249 250
	}

251 252 253
	heap_close(rel, AccessShareLock);

	return new_tlist;
254
}