preptlist.c 10.8 KB
Newer Older
1 2
/*-------------------------------------------------------------------------
 *
3
 * preptlist.c
4
 *	  Routines to preprocess the parse tree target list
5
 *
T
Tom Lane 已提交
6 7 8 9 10 11 12 13
 * For INSERT and UPDATE queries, the targetlist must contain an entry for
 * each attribute of the target relation in the correct order.  For all query
 * types, we may need to add junk tlist entries for Vars used in the RETURNING
 * list and row ID information needed for EvalPlanQual checking.
 *
 * NOTE: the rewriter's rewriteTargetListIU and rewriteTargetListUD
 * routines also do preprocessing of the targetlist.  The division of labor
 * between here and there is a bit arbitrary and historical.
14
 *
15
 *
16
 * Portions Copyright (c) 1996-2010, PostgreSQL Global Development Group
B
Add:  
Bruce Momjian 已提交
17
 * Portions Copyright (c) 1994, Regents of the University of California
18 19
 *
 * IDENTIFICATION
20
 *	  src/backend/optimizer/prep/preptlist.c
21 22 23
 *
 *-------------------------------------------------------------------------
 */
24

25 26
#include "postgres.h"

27
#include "access/heapam.h"
28
#include "access/sysattr.h"
B
Bruce Momjian 已提交
29 30 31
#include "catalog/pg_type.h"
#include "nodes/makefuncs.h"
#include "optimizer/prep.h"
32
#include "optimizer/subselect.h"
33 34
#include "optimizer/tlist.h"
#include "optimizer/var.h"
B
Bruce Momjian 已提交
35
#include "parser/parsetree.h"
36
#include "parser/parse_coerce.h"
37
#include "utils/rel.h"
38

39 40

static List *expand_targetlist(List *tlist, int command_type,
41
				  Index result_relation, List *range_table);
42

43 44

/*
45
 * preprocess_targetlist
46 47 48
 *	  Driver for preprocessing the parse tree targetlist.
 *
 *	  Returns the new targetlist.
49
 */
50
List *
51
preprocess_targetlist(PlannerInfo *root, List *tlist)
52
{
B
Bruce Momjian 已提交
53 54 55 56
	Query	   *parse = root->parse;
	int			result_relation = parse->resultRelation;
	List	   *range_table = parse->rtable;
	CmdType		command_type = parse->commandType;
57
	ListCell   *lc;
58

59
	/*
B
Bruce Momjian 已提交
60 61
	 * Sanity check: if there is a result relation, it'd better be a real
	 * relation not a subquery.  Else parser or rewriter messed up.
62 63 64 65 66 67
	 */
	if (result_relation)
	{
		RangeTblEntry *rte = rt_fetch(result_relation, range_table);

		if (rte->subquery != NULL || rte->relid == InvalidOid)
68
			elog(ERROR, "subquery cannot be result relation");
69
	}
70

71
	/*
72
	 * for heap_form_tuple to work, the targetlist must match the exact order
73 74
	 * of the attributes. We also need to fill in any missing attributes. -ay
	 * 10/94
75
	 */
76 77 78 79
	if (command_type == CMD_INSERT || command_type == CMD_UPDATE)
		tlist = expand_targetlist(tlist, command_type,
								  result_relation, range_table);

80
	/*
B
Bruce Momjian 已提交
81 82 83 84
	 * Add necessary junk columns for rowmarked rels.  These values are needed
	 * for locking of rels selected FOR UPDATE/SHARE, and to do EvalPlanQual
	 * rechecking.	While we are at it, store these junk attnos in the
	 * PlanRowMark list so that we don't have to redetermine them at runtime.
85
	 */
86
	foreach(lc, root->rowMarks)
87
	{
88 89 90 91
		PlanRowMark *rc = (PlanRowMark *) lfirst(lc);
		Var		   *var;
		char		resname[32];
		TargetEntry *tle;
92

93 94
		/* child rels should just use the same junk attrs as their parents */
		if (rc->rti != rc->prti)
95
		{
96 97 98 99 100 101 102 103 104
			PlanRowMark *prc = get_plan_rowmark(root->rowMarks, rc->prti);

			/* parent should have appeared earlier in list */
			if (prc == NULL || prc->toidAttNo == InvalidAttrNumber)
				elog(ERROR, "parent PlanRowMark not processed yet");
			rc->ctidAttNo = prc->ctidAttNo;
			rc->toidAttNo = prc->toidAttNo;
			continue;
		}
105

106 107 108
		if (rc->markType != ROW_MARK_COPY)
		{
			/* It's a regular table, so fetch its TID */
109
			var = makeVar(rc->rti,
110 111 112 113
						  SelfItemPointerAttributeNumber,
						  TIDOID,
						  -1,
						  0);
114
			snprintf(resname, sizeof(resname), "ctid%u", rc->rti);
115 116
			tle = makeTargetEntry((Expr *) var,
								  list_length(tlist) + 1,
117
								  pstrdup(resname),
118 119
								  true);
			tlist = lappend(tlist, tle);
120
			rc->ctidAttNo = tle->resno;
121 122 123 124 125 126 127 128 129

			/* if parent of inheritance tree, need the tableoid too */
			if (rc->isParent)
			{
				var = makeVar(rc->rti,
							  TableOidAttributeNumber,
							  OIDOID,
							  -1,
							  0);
130
				snprintf(resname, sizeof(resname), "tableoid%u", rc->rti);
131 132
				tle = makeTargetEntry((Expr *) var,
									  list_length(tlist) + 1,
133
									  pstrdup(resname),
134 135
									  true);
				tlist = lappend(tlist, tle);
136
				rc->toidAttNo = tle->resno;
137
			}
138
		}
139 140 141
		else
		{
			/* Not a table, so we need the whole row as a junk var */
142 143 144
			var = makeWholeRowVar(rt_fetch(rc->rti, range_table),
								  rc->rti,
								  0);
145 146 147 148 149 150 151 152
			snprintf(resname, sizeof(resname), "wholerow%u", rc->rti);
			tle = makeTargetEntry((Expr *) var,
								  list_length(tlist) + 1,
								  pstrdup(resname),
								  true);
			tlist = lappend(tlist, tle);
			rc->wholeAttNo = tle->resno;
		}
153 154
	}

155 156 157
	/*
	 * If the query has a RETURNING list, add resjunk entries for any Vars
	 * used in RETURNING that belong to other relations.  We need to do this
B
Bruce Momjian 已提交
158 159 160
	 * to make these Vars available for the RETURNING calculation.	Vars that
	 * belong to the result rel don't need to be added, because they will be
	 * made to refer to the actual heap tuple.
161 162 163
	 */
	if (parse->returningList && list_length(parse->rtable) > 1)
	{
B
Bruce Momjian 已提交
164
		List	   *vars;
165 166
		ListCell   *l;

167 168
		vars = pull_var_clause((Node *) parse->returningList,
							   PVC_INCLUDE_PLACEHOLDERS);
169 170 171 172 173
		foreach(l, vars)
		{
			Var		   *var = (Var *) lfirst(l);
			TargetEntry *tle;

174 175
			if (IsA(var, Var) &&
				var->varno == result_relation)
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
				continue;		/* don't need it */

			if (tlist_member((Node *) var, tlist))
				continue;		/* already got it */

			tle = makeTargetEntry((Expr *) var,
								  list_length(tlist) + 1,
								  NULL,
								  true);

			tlist = lappend(tlist, tle);
		}
		list_free(vars);
	}

191
	return tlist;
192 193 194 195
}

/*****************************************************************************
 *
196
 *		TARGETLIST EXPANSION
197 198 199
 *
 *****************************************************************************/

200
/*
201
 * expand_targetlist
202
 *	  Given a target list as generated by the parser and a result relation,
203 204
 *	  add targetlist entries for any missing attributes, and ensure the
 *	  non-junk attributes appear in proper field order.
205
 */
206
static List *
207 208
expand_targetlist(List *tlist, int command_type,
				  Index result_relation, List *range_table)
209
{
210
	List	   *new_tlist = NIL;
211
	ListCell   *tlist_item;
212 213
	Relation	rel;
	int			attrno,
214
				numattrs;
215

216 217
	tlist_item = list_head(tlist);

218
	/*
B
Bruce Momjian 已提交
219 220
	 * The rewriter should have already ensured that the TLEs are in correct
	 * order; but we have to insert TLEs for any missing attributes.
221
	 *
222 223 224 225
	 * Scan the tuple description in the relation's relcache entry to make
	 * sure we have all the user attributes in the right order.  We assume
	 * that the rewriter already acquired at least AccessShareLock on the
	 * relation, so we need no lock here.
226
	 */
227
	rel = heap_open(getrelid(result_relation, range_table), NoLock);
228

229 230 231 232
	numattrs = RelationGetNumberOfAttributes(rel);

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

236
		if (tlist_item != NULL)
237
		{
238
			TargetEntry *old_tle = (TargetEntry *) lfirst(tlist_item);
239

240
			if (!old_tle->resjunk && old_tle->resno == attrno)
241
			{
242
				new_tle = old_tle;
243
				tlist_item = lnext(tlist_item);
244 245
			}
		}
246

247
		if (new_tle == NULL)
248
		{
249 250 251
			/*
			 * Didn't find a matching tlist entry, so make one.
			 *
B
Bruce Momjian 已提交
252
			 * For INSERT, generate a NULL constant.  (We assume the rewriter
B
Bruce Momjian 已提交
253 254 255
			 * 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.
256
			 *
257 258
			 * For UPDATE, generate a Var reference to the existing value of
			 * the attribute, so that it gets copied to the new tuple. But
B
Bruce Momjian 已提交
259 260
			 * generate a NULL for dropped columns (we want to drop any old
			 * values).
261
			 *
262 263 264 265 266 267 268 269
			 * When generating a NULL constant for a dropped column, we label
			 * it INT4 (any other guaranteed-to-exist datatype would do as
			 * 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.
270 271 272
			 */
			Oid			atttype = att_tup->atttypid;
			int32		atttypmod = att_tup->atttypmod;
273
			Node	   *new_expr;
274

275 276 277
			switch (command_type)
			{
				case CMD_INSERT:
278
					if (!att_tup->attisdropped)
279 280
					{
						new_expr = (Node *) makeConst(atttype,
281
													  -1,
282 283
													  att_tup->attlen,
													  (Datum) 0,
B
Bruce Momjian 已提交
284
													  true,		/* isnull */
285
													  att_tup->attbyval);
286
						new_expr = coerce_to_domain(new_expr,
287
													InvalidOid, -1,
288
													atttype,
289
													COERCE_IMPLICIT_CAST,
290
													-1,
291
													false,
292
													false);
293 294 295 296 297
					}
					else
					{
						/* Insert NULL for dropped column */
						new_expr = (Node *) makeConst(INT4OID,
298
													  -1,
299 300
													  sizeof(int32),
													  (Datum) 0,
B
Bruce Momjian 已提交
301 302
													  true,		/* isnull */
													  true /* byval */ );
303
					}
304
					break;
305
				case CMD_UPDATE:
306 307
					if (!att_tup->attisdropped)
					{
308 309 310 311 312
						new_expr = (Node *) makeVar(result_relation,
													attrno,
													atttype,
													atttypmod,
													0);
313 314 315 316 317
					}
					else
					{
						/* Insert NULL for dropped column */
						new_expr = (Node *) makeConst(INT4OID,
318
													  -1,
319 320
													  sizeof(int32),
													  (Datum) 0,
B
Bruce Momjian 已提交
321 322
													  true,		/* isnull */
													  true /* byval */ );
323
					}
324
					break;
325
				default:
326 327
					elog(ERROR, "unrecognized command_type: %d",
						 (int) command_type);
328
					new_expr = NULL;	/* keep compiler quiet */
329 330
					break;
			}
331

332 333
			new_tle = makeTargetEntry((Expr *) new_expr,
									  attrno,
B
Bruce Momjian 已提交
334
									  pstrdup(NameStr(att_tup->attname)),
335
									  false);
336 337 338 339 340 341
		}

		new_tlist = lappend(new_tlist, new_tle);
	}

	/*
B
Bruce Momjian 已提交
342 343 344 345 346 347
	 * 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 an UPDATE
	 * in a table with dropped columns, or an inheritance child table with
	 * extra columns.)
348
	 */
349
	while (tlist_item)
350
	{
351
		TargetEntry *old_tle = (TargetEntry *) lfirst(tlist_item);
352

353
		if (!old_tle->resjunk)
354
			elog(ERROR, "targetlist is not sorted correctly");
355
		/* Get the resno right, but don't copy unnecessarily */
356
		if (old_tle->resno != attrno)
357
		{
358 359
			old_tle = flatCopyTargetEntry(old_tle);
			old_tle->resno = attrno;
360
		}
361 362
		new_tlist = lappend(new_tlist, old_tle);
		attrno++;
363
		tlist_item = lnext(tlist_item);
364 365
	}

366
	heap_close(rel, NoLock);
367 368

	return new_tlist;
369
}
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390


/*
 * Locate PlanRowMark for given RT index, or return NULL if none
 *
 * This probably ought to be elsewhere, but there's no very good place
 */
PlanRowMark *
get_plan_rowmark(List *rowmarks, Index rtindex)
{
	ListCell   *l;

	foreach(l, rowmarks)
	{
		PlanRowMark *rc = (PlanRowMark *) lfirst(l);

		if (rc->rti == rtindex)
			return rc;
	}
	return NULL;
}