planner.c 61.4 KB
Newer Older
1 2
/*-------------------------------------------------------------------------
 *
3
 * planner.c
4
 *	  The query optimizer external interface.
5
 *
6
 * Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
B
Add:  
Bruce Momjian 已提交
7
 * Portions Copyright (c) 1994, Regents of the University of California
8 9 10
 *
 *
 * IDENTIFICATION
11
 *	  $PostgreSQL: pgsql/src/backend/optimizer/plan/planner.c,v 1.237 2008/08/03 19:10:52 tgl Exp $
12 13 14
 *
 *-------------------------------------------------------------------------
 */
M
Marc G. Fournier 已提交
15

16 17
#include "postgres.h"

18 19
#include <limits.h>

20
#include "catalog/pg_operator.h"
21
#include "executor/executor.h"
22
#include "executor/nodeAgg.h"
23
#include "miscadmin.h"
B
Bruce Momjian 已提交
24 25
#include "nodes/makefuncs.h"
#include "optimizer/clauses.h"
26 27
#include "optimizer/cost.h"
#include "optimizer/pathnode.h"
28
#include "optimizer/paths.h"
B
Bruce Momjian 已提交
29
#include "optimizer/planmain.h"
30 31
#include "optimizer/planner.h"
#include "optimizer/prep.h"
32
#include "optimizer/subselect.h"
33 34
#include "optimizer/tlist.h"
#include "optimizer/var.h"
35 36 37
#ifdef OPTIMIZER_DEBUG
#include "nodes/print.h"
#endif
B
Bruce Momjian 已提交
38
#include "parser/parse_expr.h"
39
#include "parser/parse_oper.h"
40
#include "parser/parsetree.h"
41
#include "utils/lsyscache.h"
42
#include "utils/syscache.h"
43

44

45 46 47
/* GUC parameter */
double cursor_tuple_fraction = DEFAULT_CURSOR_TUPLE_FRACTION;

48 49 50 51
/* Hook for plugins to get control in planner() */
planner_hook_type planner_hook = NULL;


52
/* Expression kind codes for preprocess_expression */
53 54 55
#define EXPRKIND_QUAL		0
#define EXPRKIND_TARGET		1
#define EXPRKIND_RTFUNC		2
56 57 58 59
#define EXPRKIND_VALUES		3
#define EXPRKIND_LIMIT		4
#define EXPRKIND_ININFO		5
#define EXPRKIND_APPINFO	6
60 61


62 63
static Node *preprocess_expression(PlannerInfo *root, Node *expr, int kind);
static void preprocess_qual_conditions(PlannerInfo *root, Node *jtnode);
64
static Plan *inheritance_planner(PlannerInfo *root);
65
static Plan *grouping_planner(PlannerInfo *root, double tuple_fraction);
66
static bool is_dummy_plan(Plan *plan);
67
static double preprocess_limit(PlannerInfo *root,
B
Bruce Momjian 已提交
68
				 double tuple_fraction,
B
Bruce Momjian 已提交
69
				 int64 *offset_est, int64 *count_est);
70
static void preprocess_groupclause(PlannerInfo *root);
71
static Oid *extract_grouping_ops(List *groupClause);
72 73
static bool grouping_is_sortable(List *groupClause);
static bool grouping_is_hashable(List *groupClause);
74 75
static bool choose_hashed_grouping(PlannerInfo *root,
					   double tuple_fraction, double limit_tuples,
76
					   Path *cheapest_path, Path *sorted_path,
77
					   double dNumGroups, AggClauseCounts *agg_counts);
78
static List *make_subplanTargetList(PlannerInfo *root, List *tlist,
79
					   AttrNumber **groupColIdx, bool *need_tlist_eval);
80
static void locate_grouping_columns(PlannerInfo *root,
B
Bruce Momjian 已提交
81 82 83
						List *tlist,
						List *sub_tlist,
						AttrNumber *groupColIdx);
84 85
static List *postprocess_setop_tlist(List *new_tlist, List *orig_tlist);

86 87 88

/*****************************************************************************
 *
89 90
 *	   Query optimizer entry point
 *
91 92 93 94 95 96 97 98
 * To support loadable plugins that monitor or modify planner behavior,
 * we provide a hook variable that lets a plugin get control before and
 * after the standard planning process.  The plugin would normally call
 * standard_planner().
 *
 * Note to plugin authors: standard_planner() scribbles on its Query input,
 * so you'd better copy that data structure if you want to plan more than once.
 *
99
 *****************************************************************************/
100
PlannedStmt *
101
planner(Query *parse, int cursorOptions, ParamListInfo boundParams)
102 103 104 105 106 107 108 109 110 111 112 113
{
	PlannedStmt *result;

	if (planner_hook)
		result = (*planner_hook) (parse, cursorOptions, boundParams);
	else
		result = standard_planner(parse, cursorOptions, boundParams);
	return result;
}

PlannedStmt *
standard_planner(Query *parse, int cursorOptions, ParamListInfo boundParams)
114
{
115
	PlannedStmt *result;
116
	PlannerGlobal *glob;
117
	double		tuple_fraction;
118 119
	PlannerInfo *root;
	Plan	   *top_plan;
120 121
	ListCell   *lp,
			   *lr;
122

123 124 125 126 127
	/* Cursor options may come from caller or from DECLARE CURSOR stmt */
	if (parse->utilityStmt &&
		IsA(parse->utilityStmt, DeclareCursorStmt))
		cursorOptions |= ((DeclareCursorStmt *) parse->utilityStmt)->options;

128
	/*
129 130 131 132
	 * Set up global state for this planner invocation.  This data is needed
	 * across all levels of sub-Query that might exist in the given command,
	 * so we keep it in a separate struct that's linked to by each per-Query
	 * PlannerInfo.
133
	 */
134
	glob = makeNode(PlannerGlobal);
135

136 137
	glob->boundParams = boundParams;
	glob->paramlist = NIL;
138 139
	glob->subplans = NIL;
	glob->subrtables = NIL;
140
	glob->rewindPlanIDs = NULL;
141
	glob->finalrtable = NIL;
142
	glob->relationOids = NIL;
143
	glob->transientPlan = false;
144

145
	/* Determine what fraction of the plan is likely to be scanned */
146
	if (cursorOptions & CURSOR_OPT_FAST_PLAN)
147 148
	{
		/*
B
Bruce Momjian 已提交
149
		 * We have no real idea how many tuples the user will ultimately FETCH
150 151 152 153 154 155 156 157 158 159 160 161
		 * from a cursor, but it is often the case that he doesn't want 'em
		 * all, or would prefer a fast-start plan anyway so that he can
		 * process some of the tuples sooner.  Use a GUC parameter to decide
		 * what fraction to optimize for.
		 */
		tuple_fraction = cursor_tuple_fraction;

		/*
		 * We document cursor_tuple_fraction as simply being a fraction,
		 * which means the edge cases 0 and 1 have to be treated specially
		 * here.  We convert 1 to 0 ("all the tuples") and 0 to a very small
		 * fraction.
162
		 */
163 164 165 166
		if (tuple_fraction >= 1.0)
			tuple_fraction = 0.0;
		else if (tuple_fraction <= 0.0)
			tuple_fraction = 1e-10;
167 168 169 170 171 172 173
	}
	else
	{
		/* Default assumption is we need all the tuples */
		tuple_fraction = 0.0;
	}

174
	/* primary planning entry point (may recurse for subqueries) */
175
	top_plan = subquery_planner(glob, parse, 1, tuple_fraction, &root);
176

177
	/*
B
Bruce Momjian 已提交
178 179
	 * If creating a plan for a scrollable cursor, make sure it can run
	 * backwards on demand.  Add a Material node at the top at need.
180
	 */
181
	if (cursorOptions & CURSOR_OPT_SCROLL)
182
	{
183 184
		if (!ExecSupportsBackwardScan(top_plan))
			top_plan = materialize_finished_plan(top_plan);
185 186
	}

187
	/* final cleanup of the plan */
188 189 190 191 192 193
	Assert(glob->finalrtable == NIL);
	top_plan = set_plan_references(glob, top_plan, root->parse->rtable);
	/* ... and the subplans (both regular subplans and initplans) */
	Assert(list_length(glob->subplans) == list_length(glob->subrtables));
	forboth(lp, glob->subplans, lr, glob->subrtables)
	{
B
Bruce Momjian 已提交
194 195
		Plan	   *subplan = (Plan *) lfirst(lp);
		List	   *subrtable = (List *) lfirst(lr);
196 197 198

		lfirst(lp) = set_plan_references(glob, subplan, subrtable);
	}
199 200 201 202 203 204

	/* build the PlannedStmt result */
	result = makeNode(PlannedStmt);

	result->commandType = parse->commandType;
	result->canSetTag = parse->canSetTag;
205
	result->transientPlan = glob->transientPlan;
206
	result->planTree = top_plan;
207
	result->rtable = glob->finalrtable;
208
	result->resultRelations = root->resultRelations;
209 210
	result->utilityStmt = parse->utilityStmt;
	result->intoClause = parse->intoClause;
211
	result->subplans = glob->subplans;
212
	result->rewindPlanIDs = glob->rewindPlanIDs;
213 214
	result->returningLists = root->returningLists;
	result->rowMarks = parse->rowMarks;
215
	result->relationOids = glob->relationOids;
216 217 218
	result->nParamExec = list_length(glob->paramlist);

	return result;
219
}
220

221

222
/*--------------------
223 224 225
 * subquery_planner
 *	  Invokes the planner on a subquery.  We recurse to here for each
 *	  sub-SELECT found in the query tree.
226
 *
227
 * glob is the global state for the current planner run.
228
 * parse is the querytree produced by the parser & rewriter.
229
 * level is the current recursion depth (1 at the top-level Query).
230
 * tuple_fraction is the fraction of tuples we expect will be retrieved.
231
 * tuple_fraction is interpreted as explained for grouping_planner, below.
232
 *
233 234
 * If subroot isn't NULL, we pass back the query's final PlannerInfo struct;
 * among other things this tells the output sort ordering of the plan.
235
 *
236
 * Basically, this routine does the stuff that should only be done once
237 238 239 240 241
 * per Query object.  It then calls grouping_planner.  At one time,
 * grouping_planner could be invoked recursively on the same Query object;
 * that's not currently true, but we keep the separation between the two
 * routines anyway, in case we need it again someday.
 *
242 243
 * subquery_planner will be called recursively to handle sub-Query nodes
 * found within the query's expressions and rangetable.
244
 *
245 246
 * Returns a query plan.
 *--------------------
247
 */
248
Plan *
249
subquery_planner(PlannerGlobal *glob, Query *parse,
250
				 Index level, double tuple_fraction,
251
				 PlannerInfo **subroot)
252
{
253
	int			num_old_subplans = list_length(glob->subplans);
254
	PlannerInfo *root;
255
	Plan	   *plan;
256
	List	   *newHaving;
257
	ListCell   *l;
258

259 260 261
	/* Create a PlannerInfo data structure for this subquery */
	root = makeNode(PlannerInfo);
	root->parse = parse;
262 263
	root->glob = glob;
	root->query_level = level;
264
	root->planner_cxt = CurrentMemoryContext;
265
	root->init_plans = NIL;
266
	root->eq_classes = NIL;
267 268
	root->in_info_list = NIL;
	root->append_rel_list = NIL;
269

270
	/*
B
Bruce Momjian 已提交
271 272
	 * Look for IN clauses at the top level of WHERE, and transform them into
	 * joins.  Note that this step only handles IN clauses originally at top
273 274
	 * level of WHERE; if we pull up any subqueries below, their INs are
	 * processed just before pulling them up.
275 276
	 */
	if (parse->hasSubLinks)
277 278
		parse->jointree->quals = pull_up_IN_clauses(root,
													parse->jointree->quals);
279

280 281 282 283 284 285 286
	/*
	 * Scan the rangetable for set-returning functions, and inline them
	 * if possible (producing subqueries that might get pulled up next).
	 * Recursion issues here are handled in the same way as for IN clauses.
	 */
	inline_set_returning_functions(root);

287 288 289 290 291
	/*
	 * Check to see if any subqueries in the rangetable can be merged into
	 * this query.
	 */
	parse->jointree = (FromExpr *)
292
		pull_up_subqueries(root, (Node *) parse->jointree, false, false);
B
Bruce Momjian 已提交
293

294
	/*
B
Bruce Momjian 已提交
295 296 297 298
	 * Detect whether any rangetable entries are RTE_JOIN kind; if not, we can
	 * avoid the expense of doing flatten_join_alias_vars().  Also check for
	 * outer joins --- if none, we can skip reduce_outer_joins() and some
	 * other processing.  This must be done after we have done
B
Bruce Momjian 已提交
299
	 * pull_up_subqueries, of course.
300 301
	 *
	 * Note: if reduce_outer_joins manages to eliminate all outer joins,
B
Bruce Momjian 已提交
302
	 * root->hasOuterJoins is not reset currently.	This is OK since its
303
	 * purpose is merely to suppress unnecessary processing in simple cases.
304
	 */
305
	root->hasJoinRTEs = false;
306
	root->hasOuterJoins = false;
307
	foreach(l, parse->rtable)
308
	{
309
		RangeTblEntry *rte = (RangeTblEntry *) lfirst(l);
310 311 312

		if (rte->rtekind == RTE_JOIN)
		{
313
			root->hasJoinRTEs = true;
314 315
			if (IS_OUTER_JOIN(rte->jointype))
			{
316
				root->hasOuterJoins = true;
317 318 319
				/* Can quit scanning once we find an outer join */
				break;
			}
320 321 322
		}
	}

323 324 325 326 327 328 329 330 331 332
	/*
	 * Expand any rangetable entries that are inheritance sets into "append
	 * relations".  This can add entries to the rangetable, but they must be
	 * plain base relations not joins, so it's OK (and marginally more
	 * efficient) to do it after checking for join RTEs.  We must do it after
	 * pulling up subqueries, else we'd fail to handle inherited tables in
	 * subqueries.
	 */
	expand_inherited_tables(root);

333 334
	/*
	 * Set hasHavingQual to remember if HAVING clause is present.  Needed
B
Bruce Momjian 已提交
335 336
	 * because preprocess_expression will reduce a constant-true condition to
	 * an empty qual list ... but "HAVING TRUE" is not a semantic no-op.
337
	 */
338
	root->hasHavingQual = (parse->havingQual != NULL);
339

340 341 342
	/* Clear this flag; might get set in distribute_qual_to_rels */
	root->hasPseudoConstantQuals = false;

343
	/*
344
	 * Do expression preprocessing on targetlist and quals.
345
	 */
346
	parse->targetList = (List *)
347
		preprocess_expression(root, (Node *) parse->targetList,
348 349
							  EXPRKIND_TARGET);

350 351 352 353
	parse->returningList = (List *)
		preprocess_expression(root, (Node *) parse->returningList,
							  EXPRKIND_TARGET);

354
	preprocess_qual_conditions(root, (Node *) parse->jointree);
355

356
	parse->havingQual = preprocess_expression(root, parse->havingQual,
357 358
											  EXPRKIND_QUAL);

359
	parse->limitOffset = preprocess_expression(root, parse->limitOffset,
360
											   EXPRKIND_LIMIT);
361
	parse->limitCount = preprocess_expression(root, parse->limitCount,
362 363
											  EXPRKIND_LIMIT);

364 365
	root->in_info_list = (List *)
		preprocess_expression(root, (Node *) root->in_info_list,
366
							  EXPRKIND_ININFO);
367 368 369
	root->append_rel_list = (List *)
		preprocess_expression(root, (Node *) root->append_rel_list,
							  EXPRKIND_APPINFO);
370

371
	/* Also need to preprocess expressions for function and values RTEs */
372
	foreach(l, parse->rtable)
373
	{
374
		RangeTblEntry *rte = (RangeTblEntry *) lfirst(l);
375 376

		if (rte->rtekind == RTE_FUNCTION)
377
			rte->funcexpr = preprocess_expression(root, rte->funcexpr,
378
												  EXPRKIND_RTFUNC);
379 380 381 382
		else if (rte->rtekind == RTE_VALUES)
			rte->values_lists = (List *)
				preprocess_expression(root, (Node *) rte->values_lists,
									  EXPRKIND_VALUES);
383 384
	}

385
	/*
B
Bruce Momjian 已提交
386 387 388
	 * In some cases we may want to transfer a HAVING clause into WHERE. We
	 * cannot do so if the HAVING clause contains aggregates (obviously) or
	 * volatile functions (since a HAVING clause is supposed to be executed
389 390 391 392
	 * only once per group).  Also, it may be that the clause is so expensive
	 * to execute that we're better off doing it only once per group, despite
	 * the loss of selectivity.  This is hard to estimate short of doing the
	 * entire planning process twice, so we use a heuristic: clauses
B
Bruce Momjian 已提交
393 394
	 * containing subplans are left in HAVING.	Otherwise, we move or copy the
	 * HAVING clause into WHERE, in hopes of eliminating tuples before
395 396
	 * aggregation instead of after.
	 *
397 398 399 400 401 402 403 404
	 * If the query has explicit grouping then we can simply move such a
	 * clause into WHERE; any group that fails the clause will not be in the
	 * output because none of its tuples will reach the grouping or
	 * aggregation stage.  Otherwise we must have a degenerate (variable-free)
	 * HAVING clause, which we put in WHERE so that query_planner() can use it
	 * in a gating Result node, but also keep in HAVING to ensure that we
	 * don't emit a bogus aggregated row. (This could be done better, but it
	 * seems not worth optimizing.)
405 406
	 *
	 * Note that both havingQual and parse->jointree->quals are in
B
Bruce Momjian 已提交
407 408
	 * implicitly-ANDed-list form at this point, even though they are declared
	 * as Node *.
409 410
	 */
	newHaving = NIL;
411
	foreach(l, (List *) parse->havingQual)
412
	{
413
		Node	   *havingclause = (Node *) lfirst(l);
414

415 416 417 418 419
		if (contain_agg_clause(havingclause) ||
			contain_volatile_functions(havingclause) ||
			contain_subplans(havingclause))
		{
			/* keep it in HAVING */
420
			newHaving = lappend(newHaving, havingclause);
421 422 423 424
		}
		else if (parse->groupClause)
		{
			/* move it to WHERE */
425 426
			parse->jointree->quals = (Node *)
				lappend((List *) parse->jointree->quals, havingclause);
427 428 429 430 431 432 433 434 435
		}
		else
		{
			/* put a copy in WHERE, keep it in HAVING */
			parse->jointree->quals = (Node *)
				lappend((List *) parse->jointree->quals,
						copyObject(havingclause));
			newHaving = lappend(newHaving, havingclause);
		}
436 437 438
	}
	parse->havingQual = (Node *) newHaving;

439
	/*
B
Bruce Momjian 已提交
440 441
	 * If we have any outer joins, try to reduce them to plain inner joins.
	 * This step is most easily done after we've done expression
B
Bruce Momjian 已提交
442
	 * preprocessing.
443
	 */
444
	if (root->hasOuterJoins)
445
		reduce_outer_joins(root);
446

447
	/*
B
Bruce Momjian 已提交
448 449
	 * Do the main planning.  If we have an inherited target relation, that
	 * needs special processing, else go straight to grouping_planner.
450
	 */
451
	if (parse->resultRelation &&
452 453
		rt_fetch(parse->resultRelation, parse->rtable)->inh)
		plan = inheritance_planner(root);
454
	else
455
		plan = grouping_planner(root, tuple_fraction);
456 457

	/*
B
Bruce Momjian 已提交
458
	 * If any subplans were generated, or if we're inside a subplan, build
B
Bruce Momjian 已提交
459 460
	 * initPlan list and extParam/allParam sets for plan nodes, and attach the
	 * initPlans to the top plan node.
461
	 */
462 463
	if (list_length(glob->subplans) != num_old_subplans ||
		root->query_level > 1)
464
		SS_finalize_plan(root, plan, true);
B
Bruce Momjian 已提交
465

466 467 468
	/* Return internal info if caller wants it */
	if (subroot)
		*subroot = root;
469

470
	return plan;
471
}
472

473 474 475 476 477 478 479
/*
 * preprocess_expression
 *		Do subquery_planner's preprocessing work for an expression,
 *		which can be a targetlist, a WHERE clause (including JOIN/ON
 *		conditions), or a HAVING clause.
 */
static Node *
480
preprocess_expression(PlannerInfo *root, Node *expr, int kind)
481
{
482
	/*
B
Bruce Momjian 已提交
483 484 485
	 * Fall out quickly if expression is empty.  This occurs often enough to
	 * be worth checking.  Note that null->null is the correct conversion for
	 * implicit-AND result format, too.
486 487 488 489
	 */
	if (expr == NULL)
		return NULL;

490 491 492
	/*
	 * If the query has any join RTEs, replace join alias variables with
	 * base-relation variables. We must do this before sublink processing,
B
Bruce Momjian 已提交
493 494 495
	 * else sublinks expanded out from join aliases wouldn't get processed. We
	 * can skip it in VALUES lists, however, since they can't contain any Vars
	 * at all.
496
	 */
497
	if (root->hasJoinRTEs && kind != EXPRKIND_VALUES)
498
		expr = flatten_join_alias_vars(root, expr);
499

500
	/*
501
	 * Simplify constant expressions.
502
	 *
503 504 505 506
	 * Note: this also flattens nested AND and OR expressions into N-argument
	 * form.  All processing of a qual expression after this point must be
	 * careful to maintain AND/OR flatness --- that is, do not generate a tree
	 * with AND directly under AND, nor OR directly under OR.
507
	 *
508 509
	 * Because this is a relatively expensive process, we skip it when the
	 * query is trivial, such as "SELECT 2+2;" or "INSERT ... VALUES()". The
B
Bruce Momjian 已提交
510
	 * expression will only be evaluated once anyway, so no point in
511 512
	 * pre-simplifying; we can't execute it any faster than the executor can,
	 * and we will waste cycles copying the tree.  Notice however that we
B
Bruce Momjian 已提交
513 514
	 * still must do it for quals (to get AND/OR flatness); and if we are in a
	 * subquery we should not assume it will be done only once.
515
	 *
B
Bruce Momjian 已提交
516 517
	 * For VALUES lists we never do this at all, again on the grounds that we
	 * should optimize for one-time evaluation.
518
	 */
519 520 521
	if (kind != EXPRKIND_VALUES &&
		(root->parse->jointree->fromlist != NIL ||
		 kind == EXPRKIND_QUAL ||
522
		 root->query_level > 1))
523
		expr = eval_const_expressions(root, expr);
524 525 526

	/*
	 * If it's a qual or havingQual, canonicalize it.
527
	 */
528
	if (kind == EXPRKIND_QUAL)
529
	{
530
		expr = (Node *) canonicalize_qual((Expr *) expr);
531 532 533 534 535 536

#ifdef OPTIMIZER_DEBUG
		printf("After canonicalize_qual()\n");
		pprint(expr);
#endif
	}
537

538
	/* Expand SubLinks to SubPlans */
539
	if (root->parse->hasSubLinks)
540
		expr = SS_process_sublinks(root, expr, (kind == EXPRKIND_QUAL));
541

542
	/*
B
Bruce Momjian 已提交
543 544
	 * XXX do not insert anything here unless you have grokked the comments in
	 * SS_replace_correlation_vars ...
545 546
	 */

547
	/* Replace uplevel vars with Param nodes (this IS possible in VALUES) */
548 549
	if (root->query_level > 1)
		expr = SS_replace_correlation_vars(root, expr);
550

551
	/*
B
Bruce Momjian 已提交
552 553 554
	 * If it's a qual or havingQual, convert it to implicit-AND format. (We
	 * don't want to do this before eval_const_expressions, since the latter
	 * would be unable to simplify a top-level AND correctly. Also,
555
	 * SS_process_sublinks expects explicit-AND format.)
556 557 558 559
	 */
	if (kind == EXPRKIND_QUAL)
		expr = (Node *) make_ands_implicit((Expr *) expr);

560 561 562 563 564 565 566 567 568
	return expr;
}

/*
 * preprocess_qual_conditions
 *		Recursively scan the query's jointree and do subquery_planner's
 *		preprocessing work on each qual condition found therein.
 */
static void
569
preprocess_qual_conditions(PlannerInfo *root, Node *jtnode)
570 571 572 573 574 575 576 577 578 579
{
	if (jtnode == NULL)
		return;
	if (IsA(jtnode, RangeTblRef))
	{
		/* nothing to do here */
	}
	else if (IsA(jtnode, FromExpr))
	{
		FromExpr   *f = (FromExpr *) jtnode;
580
		ListCell   *l;
581

582
		foreach(l, f->fromlist)
583
			preprocess_qual_conditions(root, lfirst(l));
584

585
		f->quals = preprocess_expression(root, f->quals, EXPRKIND_QUAL);
586 587 588 589 590
	}
	else if (IsA(jtnode, JoinExpr))
	{
		JoinExpr   *j = (JoinExpr *) jtnode;

591 592
		preprocess_qual_conditions(root, j->larg);
		preprocess_qual_conditions(root, j->rarg);
593

594
		j->quals = preprocess_expression(root, j->quals, EXPRKIND_QUAL);
595 596
	}
	else
597 598
		elog(ERROR, "unrecognized node type: %d",
			 (int) nodeTag(jtnode));
599
}
600

601
/*
602 603 604 605
 * inheritance_planner
 *	  Generate a plan in the case where the result relation is an
 *	  inheritance set.
 *
606 607 608 609 610 611 612 613 614
 * We have to handle this case differently from cases where a source relation
 * is an inheritance set. Source inheritance is expanded at the bottom of the
 * plan tree (see allpaths.c), but target inheritance has to be expanded at
 * the top.  The reason is that for UPDATE, each target relation needs a
 * different targetlist matching its own column set.  Also, for both UPDATE
 * and DELETE, the executor needs the Append plan node at the top, else it
 * can't keep track of which table is the current target table.  Fortunately,
 * the UPDATE/DELETE target can never be the nullable side of an outer join,
 * so it's OK to generate the plan this way.
615 616 617 618
 *
 * Returns a query plan.
 */
static Plan *
619
inheritance_planner(PlannerInfo *root)
620
{
621
	Query	   *parse = root->parse;
622 623
	int			parentRTindex = parse->resultRelation;
	List	   *subplans = NIL;
624 625
	List	   *resultRelations = NIL;
	List	   *returningLists = NIL;
626
	List	   *rtable = NIL;
627
	List	   *tlist = NIL;
628
	PlannerInfo subroot;
629
	ListCell   *l;
630

631
	foreach(l, root->append_rel_list)
632
	{
633
		AppendRelInfo *appinfo = (AppendRelInfo *) lfirst(l);
B
Bruce Momjian 已提交
634
		Plan	   *subplan;
635

636 637 638 639
		/* append_rel_list contains all append rels; ignore others */
		if (appinfo->parent_relid != parentRTindex)
			continue;

640
		/*
B
Bruce Momjian 已提交
641 642 643
		 * Generate modified query with this rel as target.  We have to be
		 * prepared to translate varnos in in_info_list as well as in the
		 * Query proper.
644 645 646
		 */
		memcpy(&subroot, root, sizeof(PlannerInfo));
		subroot.parse = (Query *)
647 648
			adjust_appendrel_attrs((Node *) parse,
								   appinfo);
649
		subroot.in_info_list = (List *)
650 651
			adjust_appendrel_attrs((Node *) root->in_info_list,
								   appinfo);
652
		subroot.init_plans = NIL;
653
		/* There shouldn't be any OJ info to translate, as yet */
654
		Assert(subroot.oj_info_list == NIL);
655

656
		/* Generate plan */
657 658
		subplan = grouping_planner(&subroot, 0.0 /* retrieve all tuples */ );

659
		/*
B
Bruce Momjian 已提交
660 661
		 * If this child rel was excluded by constraint exclusion, exclude it
		 * from the plan.
662 663 664
		 */
		if (is_dummy_plan(subplan))
			continue;
B
Bruce Momjian 已提交
665

666 667 668 669
		/* Save rtable and tlist from first rel for use below */
		if (subplans == NIL)
		{
			rtable = subroot.parse->rtable;
670
			tlist = subplan->targetlist;
671 672 673 674
		}

		subplans = lappend(subplans, subplan);

675 676 677
		/* Make sure any initplans from this rel get into the outer list */
		root->init_plans = list_concat(root->init_plans, subroot.init_plans);

678
		/* Build target-relations list for the executor */
679 680 681 682 683
		resultRelations = lappend_int(resultRelations, appinfo->child_relid);

		/* Build list of per-relation RETURNING targetlists */
		if (parse->returningList)
		{
684
			Assert(list_length(subroot.returningLists) == 1);
685
			returningLists = list_concat(returningLists,
686
										 subroot.returningLists);
687
		}
688 689
	}

690 691
	root->resultRelations = resultRelations;
	root->returningLists = returningLists;
692

693 694 695 696 697 698 699
	/* Mark result as unordered (probably unnecessary) */
	root->query_pathkeys = NIL;

	/*
	 * If we managed to exclude every child rel, return a dummy plan
	 */
	if (subplans == NIL)
700 701 702 703
	{
		root->resultRelations = list_make1_int(parentRTindex);
		/* although dummy, it must have a valid tlist for executor */
		tlist = preprocess_targetlist(root, parse->targetList);
704 705
		return (Plan *) make_result(root,
									tlist,
706 707 708
									(Node *) list_make1(makeBoolConst(false,
																	  false)),
									NULL);
709
	}
710

711 712 713 714 715 716 717 718 719 720 721
	/*
	 * Planning might have modified the rangetable, due to changes of the
	 * Query structures inside subquery RTEs.  We have to ensure that this
	 * gets propagated back to the master copy.  But can't do this until we
	 * are done planning, because all the calls to grouping_planner need
	 * virgin sub-Queries to work from.  (We are effectively assuming that
	 * sub-Queries will get planned identically each time, or at least that
	 * the impacts on their rangetables will be the same each time.)
	 *
	 * XXX should clean this up someday
	 */
722
	parse->rtable = rtable;
723

724 725 726 727
	/* Suppress Append if there's only one surviving child rel */
	if (list_length(subplans) == 1)
		return (Plan *) linitial(subplans);

728 729 730 731 732 733 734 735
	return (Plan *) make_append(subplans, true, tlist);
}

/*--------------------
 * grouping_planner
 *	  Perform planning steps related to grouping, aggregation, etc.
 *	  This primarily means adding top-level processing to the basic
 *	  query plan produced by query_planner.
736 737 738 739
 *
 * tuple_fraction is the fraction of tuples we expect will be retrieved
 *
 * tuple_fraction is interpreted as follows:
740
 *	  0: expect all tuples to be retrieved (normal case)
741 742 743 744 745
 *	  0 < tuple_fraction < 1: expect the given fraction of tuples available
 *		from the plan to be retrieved
 *	  tuple_fraction >= 1: tuple_fraction is the absolute number of tuples
 *		expected to be retrieved (ie, a LIMIT specification)
 *
746
 * Returns a query plan.  Also, root->query_pathkeys is returned as the
747
 * actual output ordering of the plan (in pathkey format).
748 749
 *--------------------
 */
750
static Plan *
751
grouping_planner(PlannerInfo *root, double tuple_fraction)
752
{
753
	Query	   *parse = root->parse;
754
	List	   *tlist = parse->targetList;
B
Bruce Momjian 已提交
755 756
	int64		offset_est = 0;
	int64		count_est = 0;
757
	double		limit_tuples = -1.0;
758 759
	Plan	   *result_plan;
	List	   *current_pathkeys;
760
	List	   *sort_pathkeys;
761
	double		dNumGroups = 0;
762

763 764
	/* Tweak caller-supplied tuple_fraction if have LIMIT/OFFSET */
	if (parse->limitCount || parse->limitOffset)
765
	{
766 767
		tuple_fraction = preprocess_limit(root, tuple_fraction,
										  &offset_est, &count_est);
B
Bruce Momjian 已提交
768

769
		/*
B
Bruce Momjian 已提交
770 771
		 * If we have a known LIMIT, and don't have an unknown OFFSET, we can
		 * estimate the effects of using a bounded sort.
772 773 774 775
		 */
		if (count_est > 0 && offset_est >= 0)
			limit_tuples = (double) count_est + (double) offset_est;
	}
776

777
	if (parse->setOperations)
B
Bruce Momjian 已提交
778
	{
B
Bruce Momjian 已提交
779
		List	   *set_sortclauses;
780

781
		/*
B
Bruce Momjian 已提交
782 783 784 785 786
		 * If there's a top-level ORDER BY, assume we have to fetch all the
		 * tuples.	This might seem too simplistic given all the hackery below
		 * to possibly avoid the sort ... but a nonzero tuple_fraction is only
		 * of use to plan_set_operations() when the setop is UNION ALL, and
		 * the result of UNION ALL is always unsorted.
787 788 789 790
		 */
		if (parse->sortClause)
			tuple_fraction = 0.0;

791
		/*
B
Bruce Momjian 已提交
792 793
		 * Construct the plan for set operations.  The result will not need
		 * any work except perhaps a top-level sort and/or LIMIT.
794
		 */
795
		result_plan = plan_set_operations(root, tuple_fraction,
796 797 798
										  &set_sortclauses);

		/*
B
Bruce Momjian 已提交
799 800 801
		 * Calculate pathkeys representing the sort order (if any) of the set
		 * operation's result.  We have to do this before overwriting the sort
		 * key information...
802
		 */
803 804
		current_pathkeys = make_pathkeys_for_sortclauses(root,
														 set_sortclauses,
B
Bruce Momjian 已提交
805
													 result_plan->targetlist,
806
														 true);
807 808

		/*
B
Bruce Momjian 已提交
809 810 811 812 813
		 * We should not need to call preprocess_targetlist, since we must be
		 * in a SELECT query node.	Instead, use the targetlist returned by
		 * plan_set_operations (since this tells whether it returned any
		 * resjunk columns!), and transfer any sort key information from the
		 * original tlist.
814 815
		 */
		Assert(parse->commandType == CMD_SELECT);
816

817 818
		tlist = postprocess_setop_tlist(result_plan->targetlist, tlist);

819
		/*
820
		 * Can't handle FOR UPDATE/SHARE here (parser should have checked
B
Bruce Momjian 已提交
821
		 * already, but let's make sure).
822 823
		 */
		if (parse->rowMarks)
824 825
			ereport(ERROR,
					(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
826
					 errmsg("SELECT FOR UPDATE/SHARE is not allowed with UNION/INTERSECT/EXCEPT")));
827

828
		/*
829
		 * Calculate pathkeys that represent result ordering requirements
830
		 */
831
		Assert(parse->distinctClause == NIL);
832 833 834 835
		sort_pathkeys = make_pathkeys_for_sortclauses(root,
													  parse->sortClause,
													  tlist,
													  true);
B
Bruce Momjian 已提交
836
	}
837
	else
838
	{
839
		/* No set operations, do regular planning */
B
Bruce Momjian 已提交
840
		List	   *sub_tlist;
841 842
		List	   *group_pathkeys;
		AttrNumber *groupColIdx = NULL;
843
		bool		need_tlist_eval = true;
844
		QualCost	tlist_cost;
845 846
		Path	   *cheapest_path;
		Path	   *sorted_path;
847
		Path	   *best_path;
848
		long		numGroups = 0;
849
		AggClauseCounts agg_counts;
850
		int			numGroupCols;
851
		bool		use_hashed_grouping = false;
852

853 854
		MemSet(&agg_counts, 0, sizeof(AggClauseCounts));

855 856 857 858 859
		/* Preprocess GROUP BY clause, if any */
		if (parse->groupClause)
			preprocess_groupclause(root);
		numGroupCols = list_length(parse->groupClause);

860
		/* Preprocess targetlist */
861
		tlist = preprocess_targetlist(root, tlist);
B
Bruce Momjian 已提交
862

863
		/*
B
Bruce Momjian 已提交
864 865
		 * Generate appropriate target list for subplan; may be different from
		 * tlist if grouping or aggregation is needed.
866
		 */
867
		sub_tlist = make_subplanTargetList(root, tlist,
B
Bruce Momjian 已提交
868
										   &groupColIdx, &need_tlist_eval);
869

870
		/*
871 872
		 * Calculate pathkeys that represent grouping/ordering requirements.
		 * Stash them in PlannerInfo so that query_planner can canonicalize
873
		 * them after EquivalenceClasses have been formed.
874 875 876 877 878
		 *
		 * Note: for the moment, DISTINCT is always implemented via sort/uniq,
		 * and we set the sort_pathkeys to be the more rigorous of the
		 * DISTINCT and ORDER BY requirements.  This should be changed
		 * someday, but DISTINCT ON is a bit of a problem ...
879
		 */
880 881 882 883 884 885 886 887 888
		if (parse->groupClause && grouping_is_sortable(parse->groupClause))
			root->group_pathkeys =
				make_pathkeys_for_sortclauses(root,
											  parse->groupClause,
											  tlist,
											  false);
		else
			root->group_pathkeys = NIL;

889 890 891 892 893 894 895 896 897 898 899 900
		if (list_length(parse->distinctClause) > list_length(parse->sortClause))
			root->sort_pathkeys =
				make_pathkeys_for_sortclauses(root,
											  parse->distinctClause,
											  tlist,
											  false);
		else
			root->sort_pathkeys =
				make_pathkeys_for_sortclauses(root,
											  parse->sortClause,
											  tlist,
											  false);
901

902 903
		/*
		 * Will need actual number of aggregates for estimating costs.
904
		 *
B
Bruce Momjian 已提交
905 906
		 * Note: we do not attempt to detect duplicate aggregates here; a
		 * somewhat-overestimated count is okay for our present purposes.
907
		 *
908 909
		 * Note: think not that we can turn off hasAggs if we find no aggs. It
		 * is possible for constant-expression simplification to remove all
B
Bruce Momjian 已提交
910 911
		 * explicit references to aggs, but we still have to follow the
		 * aggregate semantics (eg, producing only one output row).
912 913
		 */
		if (parse->hasAggs)
914 915 916 917
		{
			count_agg_clauses((Node *) tlist, &agg_counts);
			count_agg_clauses(parse->havingQual, &agg_counts);
		}
918

919 920 921
		/*
		 * Figure out whether we need a sorted result from query_planner.
		 *
922 923 924 925 926 927
		 * If we have a sortable GROUP BY clause, then we want a result sorted
		 * properly for grouping.  Otherwise, if there is an ORDER BY clause,
		 * we want to sort by the ORDER BY clause. (Note: if we have both, and
		 * ORDER BY is a superset of GROUP BY, it would be tempting to request
		 * sort by ORDER BY --- but that might just leave us failing to
		 * exploit an available sort order at all. Needs more thought...)
928
		 */
929
		if (root->group_pathkeys)
930
			root->query_pathkeys = root->group_pathkeys;
931
		else if (root->sort_pathkeys)
932
			root->query_pathkeys = root->sort_pathkeys;
933
		else
934
			root->query_pathkeys = NIL;
935

936
		/*
B
Bruce Momjian 已提交
937 938 939 940
		 * Generate the best unsorted and presorted paths for this Query (but
		 * note there may not be any presorted path).  query_planner will also
		 * estimate the number of groups in the query, and canonicalize all
		 * the pathkeys.
941
		 */
942
		query_planner(root, sub_tlist, tuple_fraction, limit_tuples,
943
					  &cheapest_path, &sorted_path, &dNumGroups);
944

945 946
		group_pathkeys = root->group_pathkeys;
		sort_pathkeys = root->sort_pathkeys;
947

948
		/*
949
		 * If grouping, decide whether to use sorted or hashed grouping.
950 951 952
		 */
		if (parse->groupClause)
		{
953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981
			bool	can_hash;
			bool	can_sort;

			/*
			 * Executor doesn't support hashed aggregation with DISTINCT
			 * aggregates.  (Doing so would imply storing *all* the input
			 * values in the hash table, which seems like a certain loser.)
			 */
			can_hash = (agg_counts.numDistinctAggs == 0 &&
						grouping_is_hashable(parse->groupClause));
			can_sort = grouping_is_sortable(parse->groupClause);
			if (can_hash && can_sort)
			{
				/* we have a meaningful choice to make ... */
				use_hashed_grouping =
					choose_hashed_grouping(root,
										   tuple_fraction, limit_tuples,
										   cheapest_path, sorted_path,
										   dNumGroups, &agg_counts);
			}
			else if (can_hash)
				use_hashed_grouping = true;
			else if (can_sort)
				use_hashed_grouping = false;
			else
				ereport(ERROR,
						(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
						 errmsg("could not implement GROUP BY"),
						 errdetail("Some of the datatypes only support hashing, while others only support sorting.")));
982 983 984

			/* Also convert # groups to long int --- but 'ware overflow! */
			numGroups = (long) Min(dNumGroups, (double) LONG_MAX);
985 986
		}

B
Bruce Momjian 已提交
987
		/*
988
		 * Select the best path.  If we are doing hashed grouping, we will
B
Bruce Momjian 已提交
989 990
		 * always read all the input tuples, so use the cheapest-total path.
		 * Otherwise, trust query_planner's decision about which to use.
991
		 */
992 993
		if (use_hashed_grouping || !sorted_path)
			best_path = cheapest_path;
994
		else
995
			best_path = sorted_path;
996

997
		/*
B
Bruce Momjian 已提交
998 999 1000 1001
		 * Check to see if it's possible to optimize MIN/MAX aggregates. If
		 * so, we will forget all the work we did so far to choose a "regular"
		 * path ... but we had to do it anyway to be able to tell which way is
		 * cheaper.
1002
		 */
1003
		result_plan = optimize_minmax_aggregates(root,
1004 1005 1006 1007 1008
												 tlist,
												 best_path);
		if (result_plan != NULL)
		{
			/*
B
Bruce Momjian 已提交
1009 1010
			 * optimize_minmax_aggregates generated the full plan, with the
			 * right tlist, and it has no sort order.
1011 1012 1013 1014
			 */
			current_pathkeys = NIL;
		}
		else
1015
		{
1016
			/*
1017 1018
			 * Normal case --- create a plan according to query_planner's
			 * results.
1019
			 */
1020 1021
			bool	need_sort_for_grouping = false;

1022
			result_plan = create_plan(root, best_path);
1023 1024
			current_pathkeys = best_path->pathkeys;

1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036
			/* Detect if we'll need an explicit sort for grouping */
			if (parse->groupClause && !use_hashed_grouping &&
				!pathkeys_contained_in(group_pathkeys, current_pathkeys))
			{
				need_sort_for_grouping = true;
				/*
				 * Always override query_planner's tlist, so that we don't
				 * sort useless data from a "physical" tlist.
				 */
				need_tlist_eval = true;
			}

1037 1038 1039 1040 1041 1042 1043 1044
			/*
			 * create_plan() returns a plan with just a "flat" tlist of
			 * required Vars.  Usually we need to insert the sub_tlist as the
			 * tlist of the top plan node.	However, we can skip that if we
			 * determined that whatever query_planner chose to return will be
			 * good enough.
			 */
			if (need_tlist_eval)
1045
			{
1046 1047 1048 1049 1050 1051 1052
				/*
				 * If the top-level plan node is one that cannot do expression
				 * evaluation, we must insert a Result node to project the
				 * desired tlist.
				 */
				if (!is_projection_capable_plan(result_plan))
				{
1053 1054 1055
					result_plan = (Plan *) make_result(root,
													   sub_tlist,
													   NULL,
1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085
													   result_plan);
				}
				else
				{
					/*
					 * Otherwise, just replace the subplan's flat tlist with
					 * the desired tlist.
					 */
					result_plan->targetlist = sub_tlist;
				}

				/*
				 * Also, account for the cost of evaluation of the sub_tlist.
				 *
				 * Up to now, we have only been dealing with "flat" tlists,
				 * containing just Vars.  So their evaluation cost is zero
				 * according to the model used by cost_qual_eval() (or if you
				 * prefer, the cost is factored into cpu_tuple_cost).  Thus we
				 * can avoid accounting for tlist cost throughout
				 * query_planner() and subroutines.  But now we've inserted a
				 * tlist that might contain actual operators, sub-selects, etc
				 * --- so we'd better account for its cost.
				 *
				 * Below this point, any tlist eval cost for added-on nodes
				 * should be accounted for as we create those nodes.
				 * Presently, of the node types we can add on, only Agg and
				 * Group project new tlists (the rest just copy their input
				 * tuples) --- so make_agg() and make_group() are responsible
				 * for computing the added cost.
				 */
1086
				cost_qual_eval(&tlist_cost, sub_tlist, root);
1087 1088 1089
				result_plan->startup_cost += tlist_cost.startup;
				result_plan->total_cost += tlist_cost.startup +
					tlist_cost.per_tuple * result_plan->plan_rows;
1090 1091 1092 1093
			}
			else
			{
				/*
1094 1095 1096
				 * Since we're using query_planner's tlist and not the one
				 * make_subplanTargetList calculated, we have to refigure any
				 * grouping-column indexes make_subplanTargetList computed.
1097
				 */
1098
				locate_grouping_columns(root, tlist, result_plan->targetlist,
1099
										groupColIdx);
1100
			}
B
Bruce Momjian 已提交
1101

1102
			/*
1103 1104
			 * Insert AGG or GROUP node if needed, plus an explicit sort step
			 * if necessary.
1105
			 *
1106
			 * HAVING clause, if any, becomes qual of the Agg or Group node.
1107
			 */
1108 1109 1110
			if (use_hashed_grouping)
			{
				/* Hashed aggregate plan --- no sort needed */
1111
				result_plan = (Plan *) make_agg(root,
1112 1113 1114 1115 1116
												tlist,
												(List *) parse->havingQual,
												AGG_HASHED,
												numGroupCols,
												groupColIdx,
1117
									extract_grouping_ops(parse->groupClause),
1118 1119 1120 1121 1122 1123 1124 1125 1126 1127
												numGroups,
												agg_counts.numAggs,
												result_plan);
				/* Hashed aggregation produces randomly-ordered results */
				current_pathkeys = NIL;
			}
			else if (parse->hasAggs)
			{
				/* Plain aggregate plan --- sort if needed */
				AggStrategy aggstrategy;
1128

1129 1130
				if (parse->groupClause)
				{
1131
					if (need_sort_for_grouping)
1132 1133
					{
						result_plan = (Plan *)
1134
							make_sort_from_groupcols(root,
1135 1136 1137 1138 1139 1140
													 parse->groupClause,
													 groupColIdx,
													 result_plan);
						current_pathkeys = group_pathkeys;
					}
					aggstrategy = AGG_SORTED;
1141

1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153
					/*
					 * The AGG node will not change the sort ordering of its
					 * groups, so current_pathkeys describes the result too.
					 */
				}
				else
				{
					aggstrategy = AGG_PLAIN;
					/* Result will be only one row anyway; no sort order */
					current_pathkeys = NIL;
				}

1154
				result_plan = (Plan *) make_agg(root,
1155 1156 1157 1158 1159
												tlist,
												(List *) parse->havingQual,
												aggstrategy,
												numGroupCols,
												groupColIdx,
1160
									extract_grouping_ops(parse->groupClause),
1161 1162 1163 1164 1165
												numGroups,
												agg_counts.numAggs,
												result_plan);
			}
			else if (parse->groupClause)
1166
			{
1167 1168 1169 1170
				/*
				 * GROUP BY without aggregation, so insert a group node (plus
				 * the appropriate sort node, if necessary).
				 *
1171 1172
				 * Add an explicit sort if we couldn't make the path come out
				 * the way the GROUP node needs it.
1173
				 */
1174
				if (need_sort_for_grouping)
1175
				{
1176
					result_plan = (Plan *)
1177
						make_sort_from_groupcols(root,
1178 1179 1180
												 parse->groupClause,
												 groupColIdx,
												 result_plan);
1181 1182
					current_pathkeys = group_pathkeys;
				}
B
Bruce Momjian 已提交
1183

1184
				result_plan = (Plan *) make_group(root,
1185 1186 1187 1188
												  tlist,
												  (List *) parse->havingQual,
												  numGroupCols,
												  groupColIdx,
1189
									extract_grouping_ops(parse->groupClause),
1190 1191 1192
												  dNumGroups,
												  result_plan);
				/* The Group node won't change sort ordering */
1193
			}
1194
			else if (root->hasHavingQual)
1195
			{
1196 1197 1198 1199 1200
				/*
				 * No aggregates, and no GROUP BY, but we have a HAVING qual.
				 * This is a degenerate case in which we are supposed to emit
				 * either 0 or 1 row depending on whether HAVING succeeds.
				 * Furthermore, there cannot be any variables in either HAVING
B
Bruce Momjian 已提交
1201 1202 1203 1204 1205 1206
				 * or the targetlist, so we actually do not need the FROM
				 * table at all!  We can just throw away the plan-so-far and
				 * generate a Result node.	This is a sufficiently unusual
				 * corner case that it's not worth contorting the structure of
				 * this routine to avoid having to generate the plan in the
				 * first place.
1207
				 */
1208 1209
				result_plan = (Plan *) make_result(root,
												   tlist,
1210 1211
												   parse->havingQual,
												   NULL);
1212
			}
1213
		}						/* end of non-minmax-aggregate case */
B
Bruce Momjian 已提交
1214
	}							/* end of if (setOperations) */
1215

1216
	/*
B
Bruce Momjian 已提交
1217 1218
	 * If we were not able to make the plan come out in the right order, add
	 * an explicit sort step.
1219
	 */
1220
	if (sort_pathkeys)
1221
	{
1222
		if (!pathkeys_contained_in(sort_pathkeys, current_pathkeys))
1223
		{
1224 1225
			result_plan = (Plan *) make_sort_from_pathkeys(root,
														   result_plan,
1226 1227
														   sort_pathkeys,
														   limit_tuples);
1228 1229
			current_pathkeys = sort_pathkeys;
		}
1230
	}
1231 1232

	/*
1233
	 * If there is a DISTINCT clause, add the UNIQUE node.
1234
	 */
1235
	if (parse->distinctClause)
1236
	{
1237
		result_plan = (Plan *) make_unique(result_plan, parse->distinctClause);
B
Bruce Momjian 已提交
1238

1239
		/*
B
Bruce Momjian 已提交
1240 1241 1242
		 * If there was grouping or aggregation, leave plan_rows as-is (ie,
		 * assume the result was already mostly unique).  If not, use the
		 * number of distinct-groups calculated by query_planner.
1243
		 */
1244
		if (!parse->groupClause && !root->hasHavingQual && !parse->hasAggs)
1245
			result_plan->plan_rows = dNumGroups;
1246
	}
1247

1248 1249 1250
	/*
	 * Finally, if there is a LIMIT/OFFSET clause, add the LIMIT node.
	 */
1251
	if (parse->limitCount || parse->limitOffset)
1252
	{
1253
		result_plan = (Plan *) make_limit(result_plan,
1254
										  parse->limitOffset,
1255 1256 1257
										  parse->limitCount,
										  offset_est,
										  count_est);
1258 1259
	}

1260 1261
	/*
	 * Deal with the RETURNING clause if any.  It's convenient to pass the
B
Bruce Momjian 已提交
1262 1263
	 * returningList through setrefs.c now rather than at top level (if we
	 * waited, handling inherited UPDATE/DELETE would be much harder).
1264 1265 1266
	 */
	if (parse->returningList)
	{
B
Bruce Momjian 已提交
1267
		List	   *rlist;
1268

1269
		Assert(parse->resultRelation);
1270 1271
		rlist = set_returning_clause_references(root->glob,
												parse->returningList,
1272 1273
												result_plan,
												parse->resultRelation);
1274
		root->returningLists = list_make1(rlist);
1275
	}
1276 1277 1278 1279 1280 1281 1282 1283
	else
		root->returningLists = NIL;

	/* Compute result-relations list if needed */
	if (parse->resultRelation)
		root->resultRelations = list_make1_int(parse->resultRelation);
	else
		root->resultRelations = NIL;
1284

1285
	/*
B
Bruce Momjian 已提交
1286 1287
	 * Return the actual output ordering in query_pathkeys for possible use by
	 * an outer query level.
1288
	 */
1289
	root->query_pathkeys = current_pathkeys;
1290

1291
	return result_plan;
1292 1293
}

1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305
/*
 * Detect whether a plan node is a "dummy" plan created when a relation
 * is deemed not to need scanning due to constraint exclusion.
 *
 * Currently, such dummy plans are Result nodes with constant FALSE
 * filter quals.
 */
static bool
is_dummy_plan(Plan *plan)
{
	if (IsA(plan, Result))
	{
B
Bruce Momjian 已提交
1306
		List	   *rcqual = (List *) ((Result *) plan)->resconstantqual;
1307 1308 1309

		if (list_length(rcqual) == 1)
		{
B
Bruce Momjian 已提交
1310
			Const	   *constqual = (Const *) linitial(rcqual);
1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322

			if (constqual && IsA(constqual, Const))
			{
				if (!constqual->constisnull &&
					!DatumGetBool(constqual->constvalue))
					return true;
			}
		}
	}
	return false;
}

1323
/*
1324
 * preprocess_limit - do pre-estimation for LIMIT and/or OFFSET clauses
1325
 *
1326
 * We try to estimate the values of the LIMIT/OFFSET clauses, and pass the
B
Bruce Momjian 已提交
1327
 * results back in *count_est and *offset_est.	These variables are set to
1328 1329 1330 1331 1332 1333 1334 1335
 * 0 if the corresponding clause is not present, and -1 if it's present
 * but we couldn't estimate the value for it.  (The "0" convention is OK
 * for OFFSET but a little bit bogus for LIMIT: effectively we estimate
 * LIMIT 0 as though it were LIMIT 1.  But this is in line with the planner's
 * usual practice of never estimating less than one row.)  These values will
 * be passed to make_limit, which see if you change this code.
 *
 * The return value is the suitably adjusted tuple_fraction to use for
B
Bruce Momjian 已提交
1336
 * planning the query.	This adjustment is not overridable, since it reflects
1337 1338
 * plan actions that grouping_planner() will certainly take, not assumptions
 * about context.
1339 1340
 */
static double
1341
preprocess_limit(PlannerInfo *root, double tuple_fraction,
B
Bruce Momjian 已提交
1342
				 int64 *offset_est, int64 *count_est)
1343 1344
{
	Query	   *parse = root->parse;
1345 1346
	Node	   *est;
	double		limit_fraction;
1347

1348 1349
	/* Should not be called unless LIMIT or OFFSET */
	Assert(parse->limitCount || parse->limitOffset);
1350 1351

	/*
1352 1353
	 * Try to obtain the clause values.  We use estimate_expression_value
	 * primarily because it can sometimes do something useful with Params.
1354
	 */
1355
	if (parse->limitCount)
1356
	{
1357
		est = estimate_expression_value(root, parse->limitCount);
1358
		if (est && IsA(est, Const))
1359
		{
1360
			if (((Const *) est)->constisnull)
1361
			{
1362
				/* NULL indicates LIMIT ALL, ie, no limit */
B
Bruce Momjian 已提交
1363
				*count_est = 0; /* treat as not present */
1364 1365 1366
			}
			else
			{
B
Bruce Momjian 已提交
1367
				*count_est = DatumGetInt64(((Const *) est)->constvalue);
1368 1369
				if (*count_est <= 0)
					*count_est = 1;		/* force to at least 1 */
1370 1371
			}
		}
1372 1373
		else
			*count_est = -1;	/* can't estimate */
1374 1375
	}
	else
1376 1377 1378
		*count_est = 0;			/* not present */

	if (parse->limitOffset)
1379
	{
1380
		est = estimate_expression_value(root, parse->limitOffset);
1381 1382 1383 1384 1385
		if (est && IsA(est, Const))
		{
			if (((Const *) est)->constisnull)
			{
				/* Treat NULL as no offset; the executor will too */
B
Bruce Momjian 已提交
1386
				*offset_est = 0;	/* treat as not present */
1387 1388 1389
			}
			else
			{
B
Bruce Momjian 已提交
1390
				*offset_est = DatumGetInt64(((Const *) est)->constvalue);
1391 1392 1393 1394 1395 1396
				if (*offset_est < 0)
					*offset_est = 0;	/* less than 0 is same as 0 */
			}
		}
		else
			*offset_est = -1;	/* can't estimate */
1397
	}
1398 1399
	else
		*offset_est = 0;		/* not present */
1400

1401
	if (*count_est != 0)
1402
	{
1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418
		/*
		 * A LIMIT clause limits the absolute number of tuples returned.
		 * However, if it's not a constant LIMIT then we have to guess; for
		 * lack of a better idea, assume 10% of the plan's result is wanted.
		 */
		if (*count_est < 0 || *offset_est < 0)
		{
			/* LIMIT or OFFSET is an expression ... punt ... */
			limit_fraction = 0.10;
		}
		else
		{
			/* LIMIT (plus OFFSET, if any) is max number of tuples needed */
			limit_fraction = (double) *count_est + (double) *offset_est;
		}

1419 1420
		/*
		 * If we have absolute limits from both caller and LIMIT, use the
1421 1422 1423 1424
		 * smaller value; likewise if they are both fractional.  If one is
		 * fractional and the other absolute, we can't easily determine which
		 * is smaller, but we use the heuristic that the absolute will usually
		 * be smaller.
1425 1426 1427 1428 1429 1430 1431 1432 1433 1434
		 */
		if (tuple_fraction >= 1.0)
		{
			if (limit_fraction >= 1.0)
			{
				/* both absolute */
				tuple_fraction = Min(tuple_fraction, limit_fraction);
			}
			else
			{
1435
				/* caller absolute, limit fractional; use caller's value */
1436 1437 1438 1439 1440 1441
			}
		}
		else if (tuple_fraction > 0.0)
		{
			if (limit_fraction >= 1.0)
			{
1442 1443
				/* caller fractional, limit absolute; use limit */
				tuple_fraction = limit_fraction;
1444 1445 1446 1447
			}
			else
			{
				/* both fractional */
1448
				tuple_fraction = Min(tuple_fraction, limit_fraction);
1449 1450 1451 1452 1453 1454 1455 1456
			}
		}
		else
		{
			/* no info from caller, just use limit */
			tuple_fraction = limit_fraction;
		}
	}
1457 1458 1459
	else if (*offset_est != 0 && tuple_fraction > 0.0)
	{
		/*
B
Bruce Momjian 已提交
1460 1461 1462 1463 1464
		 * We have an OFFSET but no LIMIT.	This acts entirely differently
		 * from the LIMIT case: here, we need to increase rather than decrease
		 * the caller's tuple_fraction, because the OFFSET acts to cause more
		 * tuples to be fetched instead of fewer.  This only matters if we got
		 * a tuple_fraction > 0, however.
1465 1466 1467 1468 1469 1470 1471 1472 1473 1474
		 *
		 * As above, use 10% if OFFSET is present but unestimatable.
		 */
		if (*offset_est < 0)
			limit_fraction = 0.10;
		else
			limit_fraction = (double) *offset_est;

		/*
		 * If we have absolute counts from both caller and OFFSET, add them
B
Bruce Momjian 已提交
1475 1476 1477
		 * together; likewise if they are both fractional.	If one is
		 * fractional and the other absolute, we want to take the larger, and
		 * we heuristically assume that's the fractional one.
1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502
		 */
		if (tuple_fraction >= 1.0)
		{
			if (limit_fraction >= 1.0)
			{
				/* both absolute, so add them together */
				tuple_fraction += limit_fraction;
			}
			else
			{
				/* caller absolute, limit fractional; use limit */
				tuple_fraction = limit_fraction;
			}
		}
		else
		{
			if (limit_fraction >= 1.0)
			{
				/* caller fractional, limit absolute; use caller's value */
			}
			else
			{
				/* both fractional, so add them together */
				tuple_fraction += limit_fraction;
				if (tuple_fraction >= 1.0)
B
Bruce Momjian 已提交
1503
					tuple_fraction = 0.0;		/* assume fetch all */
1504 1505 1506
			}
		}
	}
1507 1508 1509 1510

	return tuple_fraction;
}

1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523

/*
 * preprocess_groupclause - do preparatory work on GROUP BY clause
 *
 * The idea here is to adjust the ordering of the GROUP BY elements
 * (which in itself is semantically insignificant) to match ORDER BY,
 * thereby allowing a single sort operation to both implement the ORDER BY
 * requirement and set up for a Unique step that implements GROUP BY.
 *
 * In principle it might be interesting to consider other orderings of the
 * GROUP BY elements, which could match the sort ordering of other
 * possible plans (eg an indexscan) and thereby reduce cost.  We don't
 * bother with that, though.  Hashed grouping will frequently win anyway.
1524 1525 1526
 *
 * Note: we need no comparable processing of the distinctClause because
 * the parser already enforced that that matches ORDER BY.
1527 1528 1529 1530 1531 1532 1533 1534 1535 1536
 */
static void
preprocess_groupclause(PlannerInfo *root)
{
	Query	   *parse = root->parse;
	List	   *new_groupclause;
	bool		partial_match;
	ListCell   *sl;
	ListCell   *gl;

1537
	/* If no ORDER BY, nothing useful to do here */
1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577
	if (parse->sortClause == NIL)
		return;

	/*
	 * Scan the ORDER BY clause and construct a list of matching GROUP BY
	 * items, but only as far as we can make a matching prefix.
	 *
	 * This code assumes that the sortClause contains no duplicate items.
	 */
	new_groupclause = NIL;
	foreach(sl, parse->sortClause)
	{
		SortGroupClause *sc = (SortGroupClause *) lfirst(sl);

		foreach(gl, parse->groupClause)
		{
			SortGroupClause *gc = (SortGroupClause *) lfirst(gl);

			if (equal(gc, sc))
			{
				new_groupclause = lappend(new_groupclause, gc);
				break;
			}
		}
		if (gl == NULL)
			break;				/* no match, so stop scanning */
	}

	/* Did we match all of the ORDER BY list, or just some of it? */
	partial_match = (sl != NULL);

	/* If no match at all, no point in reordering GROUP BY */
	if (new_groupclause == NIL)
		return;

	/*
	 * Add any remaining GROUP BY items to the new list, but only if we
	 * were able to make a complete match.  In other words, we only
	 * rearrange the GROUP BY list if the result is that one list is a
	 * prefix of the other --- otherwise there's no possibility of a
1578 1579
	 * common sort.  Also, give up if there are any non-sortable GROUP BY
	 * items, since then there's no hope anyway.
1580 1581 1582 1583 1584 1585 1586 1587 1588
	 */
	foreach(gl, parse->groupClause)
	{
		SortGroupClause *gc = (SortGroupClause *) lfirst(gl);

		if (list_member_ptr(new_groupclause, gc))
			continue;			/* it matched an ORDER BY item */
		if (partial_match)
			return;				/* give up, no common sort possible */
1589 1590
		if (!OidIsValid(gc->sortop))
			return;				/* give up, GROUP BY can't be sorted */
1591 1592 1593 1594 1595 1596 1597 1598
		new_groupclause = lappend(new_groupclause, gc);
	}

	/* Success --- install the rearranged GROUP BY list */
	Assert(list_length(parse->groupClause) == list_length(new_groupclause));
	parse->groupClause = new_groupclause;
}

1599 1600
/*
 * extract_grouping_ops - make an array of the equality operator OIDs
1601
 *		for a SortGroupClause list
1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614
 */
static Oid *
extract_grouping_ops(List *groupClause)
{
	int			numCols = list_length(groupClause);
	int			colno = 0;
	Oid		   *groupOperators;
	ListCell   *glitem;

	groupOperators = (Oid *) palloc(sizeof(Oid) * numCols);

	foreach(glitem, groupClause)
	{
1615
		SortGroupClause *groupcl = (SortGroupClause *) lfirst(glitem);
1616

1617 1618
		groupOperators[colno] = groupcl->eqop;
		Assert(OidIsValid(groupOperators[colno]));
1619 1620 1621 1622 1623 1624
		colno++;
	}

	return groupOperators;
}

1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667
/*
 * grouping_is_sortable - is it possible to implement grouping list by sorting?
 *
 * This is easy since the parser will have included a sortop if one exists.
 */
static bool
grouping_is_sortable(List *groupClause)
{
	ListCell   *glitem;

	foreach(glitem, groupClause)
	{
		SortGroupClause *groupcl = (SortGroupClause *) lfirst(glitem);

		if (!OidIsValid(groupcl->sortop))
			return false;
	}
	return true;
}

/*
 * grouping_is_hashable - is it possible to implement grouping list by hashing?
 *
 * We assume hashing is OK if the equality operators are marked oprcanhash.
 * (If there isn't actually a supporting hash function, the executor will
 * complain at runtime; but this is a misdeclaration of the operator, not
 * a system bug.)
 */
static bool
grouping_is_hashable(List *groupClause)
{
	ListCell   *glitem;

	foreach(glitem, groupClause)
	{
		SortGroupClause *groupcl = (SortGroupClause *) lfirst(glitem);

		if (!op_hashjoinable(groupcl->eqop))
			return false;
	}
	return true;
}

1668 1669
/*
 * choose_hashed_grouping - should we use hashed grouping?
1670 1671
 *
 * Note: this is only applied when both alternatives are actually feasible.
1672 1673
 */
static bool
1674 1675
choose_hashed_grouping(PlannerInfo *root,
					   double tuple_fraction, double limit_tuples,
1676
					   Path *cheapest_path, Path *sorted_path,
1677
					   double dNumGroups, AggClauseCounts *agg_counts)
1678
{
1679
	int			numGroupCols = list_length(root->parse->groupClause);
1680 1681 1682 1683 1684 1685 1686
	double		cheapest_path_rows;
	int			cheapest_path_width;
	Size		hashentrysize;
	List	   *current_pathkeys;
	Path		hashed_p;
	Path		sorted_p;

1687
	/* Prefer sorting when enable_hashagg is off */
1688 1689 1690 1691 1692 1693 1694
	if (!enable_hashagg)
		return false;

	/*
	 * Don't do it if it doesn't look like the hashtable will fit into
	 * work_mem.
	 *
B
Bruce Momjian 已提交
1695 1696
	 * Beware here of the possibility that cheapest_path->parent is NULL. This
	 * could happen if user does something silly like SELECT 'foo' GROUP BY 1;
1697 1698 1699 1700 1701 1702 1703 1704
	 */
	if (cheapest_path->parent)
	{
		cheapest_path_rows = cheapest_path->parent->rows;
		cheapest_path_width = cheapest_path->parent->width;
	}
	else
	{
B
Bruce Momjian 已提交
1705 1706
		cheapest_path_rows = 1; /* assume non-set result */
		cheapest_path_width = 100;		/* arbitrary */
1707 1708 1709
	}

	/* Estimate per-hash-entry space at tuple width... */
1710
	hashentrysize = MAXALIGN(cheapest_path_width) + MAXALIGN(sizeof(MinimalTupleData));
1711 1712 1713 1714 1715 1716 1717 1718 1719
	/* plus space for pass-by-ref transition values... */
	hashentrysize += agg_counts->transitionSpace;
	/* plus the per-hash-entry overhead */
	hashentrysize += hash_agg_entry_size(agg_counts->numAggs);

	if (hashentrysize * dNumGroups > work_mem * 1024L)
		return false;

	/*
B
Bruce Momjian 已提交
1720 1721 1722 1723
	 * See if the estimated cost is no more than doing it the other way. While
	 * avoiding the need for sorted input is usually a win, the fact that the
	 * output won't be sorted may be a loss; so we need to do an actual cost
	 * comparison.
1724
	 *
1725 1726 1727 1728 1729 1730
	 * We need to consider cheapest_path + hashagg [+ final sort] versus
	 * either cheapest_path [+ sort] + group or agg [+ final sort] or
	 * presorted_path + group or agg [+ final sort] where brackets indicate a
	 * step that may not be needed. We assume query_planner() will have
	 * returned a presorted path only if it's a winner compared to
	 * cheapest_path for this purpose.
1731
	 *
1732 1733
	 * These path variables are dummies that just hold cost fields; we don't
	 * make actual Paths for these steps.
1734
	 */
1735
	cost_agg(&hashed_p, root, AGG_HASHED, agg_counts->numAggs,
1736 1737 1738 1739
			 numGroupCols, dNumGroups,
			 cheapest_path->startup_cost, cheapest_path->total_cost,
			 cheapest_path_rows);
	/* Result of hashed agg is always unsorted */
1740 1741
	if (root->sort_pathkeys)
		cost_sort(&hashed_p, root, root->sort_pathkeys, hashed_p.total_cost,
1742
				  dNumGroups, cheapest_path_width, limit_tuples);
1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755

	if (sorted_path)
	{
		sorted_p.startup_cost = sorted_path->startup_cost;
		sorted_p.total_cost = sorted_path->total_cost;
		current_pathkeys = sorted_path->pathkeys;
	}
	else
	{
		sorted_p.startup_cost = cheapest_path->startup_cost;
		sorted_p.total_cost = cheapest_path->total_cost;
		current_pathkeys = cheapest_path->pathkeys;
	}
1756
	if (!pathkeys_contained_in(root->group_pathkeys, current_pathkeys))
1757
	{
1758
		cost_sort(&sorted_p, root, root->group_pathkeys, sorted_p.total_cost,
1759
				  cheapest_path_rows, cheapest_path_width, -1.0);
1760
		current_pathkeys = root->group_pathkeys;
1761 1762
	}

1763 1764
	if (root->parse->hasAggs)
		cost_agg(&sorted_p, root, AGG_SORTED, agg_counts->numAggs,
1765 1766 1767 1768
				 numGroupCols, dNumGroups,
				 sorted_p.startup_cost, sorted_p.total_cost,
				 cheapest_path_rows);
	else
1769
		cost_group(&sorted_p, root, numGroupCols, dNumGroups,
1770 1771 1772
				   sorted_p.startup_cost, sorted_p.total_cost,
				   cheapest_path_rows);
	/* The Agg or Group node will preserve ordering */
1773 1774 1775
	if (root->sort_pathkeys &&
		!pathkeys_contained_in(root->sort_pathkeys, current_pathkeys))
		cost_sort(&sorted_p, root, root->sort_pathkeys, sorted_p.total_cost,
1776
				  dNumGroups, cheapest_path_width, limit_tuples);
1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793

	/*
	 * Now make the decision using the top-level tuple fraction.  First we
	 * have to convert an absolute count (LIMIT) into fractional form.
	 */
	if (tuple_fraction >= 1.0)
		tuple_fraction /= dNumGroups;

	if (compare_fractional_path_costs(&hashed_p, &sorted_p,
									  tuple_fraction) < 0)
	{
		/* Hashed is cheaper, so use it */
		return true;
	}
	return false;
}

1794 1795
/*---------------
 * make_subplanTargetList
1796
 *	  Generate appropriate target list when grouping is required.
1797
 *
1798 1799
 * When grouping_planner inserts Aggregate, Group, or Result plan nodes
 * above the result of query_planner, we typically want to pass a different
1800
 * target list to query_planner than the outer plan nodes should have.
1801
 * This routine generates the correct target list for the subplan.
1802 1803 1804 1805
 *
 * The initial target list passed from the parser already contains entries
 * for all ORDER BY and GROUP BY expressions, but it will not have entries
 * for variables used only in HAVING clauses; so we need to add those
1806 1807 1808 1809
 * variables to the subplan target list.  Also, we flatten all expressions
 * except GROUP BY items into their component variables; the other expressions
 * will be computed by the inserted nodes rather than by the subplan.
 * For example, given a query like
1810 1811
 *		SELECT a+b,SUM(c+d) FROM table GROUP BY a+b;
 * we want to pass this targetlist to the subplan:
1812
 *		a,b,c,d,a+b
1813
 * where the a+b target will be used by the Sort/Group steps, and the
1814
 * other targets will be used for computing the final results.	(In the
1815
 * above example we could theoretically suppress the a and b targets and
1816 1817 1818
 * pass down only c,d,a+b, but it's not really worth the trouble to
 * eliminate simple var references from the subplan.  We will avoid doing
 * the extra computation to recompute a+b at the outer level; see
1819
 * fix_upper_expr() in setrefs.c.)
1820
 *
1821 1822 1823 1824 1825
 * If we are grouping or aggregating, *and* there are no non-Var grouping
 * expressions, then the returned tlist is effectively dummy; we do not
 * need to force it to be evaluated, because all the Vars it contains
 * should be present in the output of query_planner anyway.
 *
1826
 * 'tlist' is the query's target list.
1827
 * 'groupColIdx' receives an array of column numbers for the GROUP BY
1828 1829 1830
 *			expressions (if there are any) in the subplan's target list.
 * 'need_tlist_eval' is set true if we really need to evaluate the
 *			result tlist.
1831
 *
1832
 * The result is the targetlist to be passed to the subplan.
1833 1834 1835
 *---------------
 */
static List *
1836
make_subplanTargetList(PlannerInfo *root,
1837
					   List *tlist,
1838 1839
					   AttrNumber **groupColIdx,
					   bool *need_tlist_eval)
1840
{
1841
	Query	   *parse = root->parse;
1842
	List	   *sub_tlist;
1843
	List	   *extravars;
1844 1845 1846 1847
	int			numCols;

	*groupColIdx = NULL;

B
Bruce Momjian 已提交
1848
	/*
1849
	 * If we're not grouping or aggregating, there's nothing to do here;
1850 1851
	 * query_planner should receive the unmodified target list.
	 */
1852
	if (!parse->hasAggs && !parse->groupClause && !root->hasHavingQual)
1853 1854
	{
		*need_tlist_eval = true;
1855
		return tlist;
1856
	}
1857

B
Bruce Momjian 已提交
1858
	/*
1859
	 * Otherwise, start with a "flattened" tlist (having just the vars
B
Bruce Momjian 已提交
1860 1861
	 * mentioned in the targetlist and HAVING qual --- but not upper- level
	 * Vars; they will be replaced by Params later on).
1862
	 */
1863
	sub_tlist = flatten_tlist(tlist);
1864
	extravars = pull_var_clause(parse->havingQual, false);
1865
	sub_tlist = add_to_flat_tlist(sub_tlist, extravars);
1866
	list_free(extravars);
1867
	*need_tlist_eval = false;	/* only eval if not flat tlist */
1868 1869

	/*
1870
	 * If grouping, create sub_tlist entries for all GROUP BY expressions
B
Bruce Momjian 已提交
1871 1872
	 * (GROUP BY items that are simple Vars should be in the list already),
	 * and make an array showing where the group columns are in the sub_tlist.
1873
	 */
1874
	numCols = list_length(parse->groupClause);
1875
	if (numCols > 0)
1876 1877
	{
		int			keyno = 0;
1878
		AttrNumber *grpColIdx;
1879
		ListCell   *gl;
1880 1881 1882

		grpColIdx = (AttrNumber *) palloc(sizeof(AttrNumber) * numCols);
		*groupColIdx = grpColIdx;
1883 1884 1885

		foreach(gl, parse->groupClause)
		{
1886
			SortGroupClause *grpcl = (SortGroupClause *) lfirst(gl);
B
Bruce Momjian 已提交
1887 1888 1889
			Node	   *groupexpr = get_sortgroupclause_expr(grpcl, tlist);
			TargetEntry *te = NULL;
			ListCell   *sl;
1890

1891 1892
			/* Find or make a matching sub_tlist entry */
			foreach(sl, sub_tlist)
1893
			{
1894 1895 1896
				te = (TargetEntry *) lfirst(sl);
				if (equal(groupexpr, te->expr))
					break;
1897
			}
1898
			if (!sl)
1899
			{
1900 1901 1902 1903
				te = makeTargetEntry((Expr *) groupexpr,
									 list_length(sub_tlist) + 1,
									 NULL,
									 false);
1904
				sub_tlist = lappend(sub_tlist, te);
B
Bruce Momjian 已提交
1905
				*need_tlist_eval = true;		/* it's not flat anymore */
1906 1907
			}

1908
			/* and save its resno */
1909
			grpColIdx[keyno++] = te->resno;
1910 1911 1912 1913 1914 1915
		}
	}

	return sub_tlist;
}

1916 1917 1918 1919 1920
/*
 * locate_grouping_columns
 *		Locate grouping columns in the tlist chosen by query_planner.
 *
 * This is only needed if we don't use the sub_tlist chosen by
B
Bruce Momjian 已提交
1921
 * make_subplanTargetList.	We have to forget the column indexes found
1922 1923 1924
 * by that routine and re-locate the grouping vars in the real sub_tlist.
 */
static void
1925
locate_grouping_columns(PlannerInfo *root,
1926 1927 1928 1929 1930
						List *tlist,
						List *sub_tlist,
						AttrNumber *groupColIdx)
{
	int			keyno = 0;
1931
	ListCell   *gl;
1932 1933 1934 1935

	/*
	 * No work unless grouping.
	 */
1936
	if (!root->parse->groupClause)
1937 1938 1939 1940 1941 1942
	{
		Assert(groupColIdx == NULL);
		return;
	}
	Assert(groupColIdx != NULL);

1943
	foreach(gl, root->parse->groupClause)
1944
	{
1945
		SortGroupClause *grpcl = (SortGroupClause *) lfirst(gl);
B
Bruce Momjian 已提交
1946 1947 1948
		Node	   *groupexpr = get_sortgroupclause_expr(grpcl, tlist);
		TargetEntry *te = NULL;
		ListCell   *sl;
1949 1950 1951 1952 1953 1954 1955 1956

		foreach(sl, sub_tlist)
		{
			te = (TargetEntry *) lfirst(sl);
			if (equal(groupexpr, te->expr))
				break;
		}
		if (!sl)
1957
			elog(ERROR, "failed to locate grouping columns");
1958

1959
		groupColIdx[keyno++] = te->resno;
1960 1961 1962
	}
}

1963 1964 1965 1966 1967 1968 1969
/*
 * postprocess_setop_tlist
 *	  Fix up targetlist returned by plan_set_operations().
 *
 * We need to transpose sort key info from the orig_tlist into new_tlist.
 * NOTE: this would not be good enough if we supported resjunk sort keys
 * for results of set operations --- then, we'd need to project a whole
1970
 * new tlist to evaluate the resjunk columns.  For now, just ereport if we
1971 1972 1973 1974 1975
 * find any resjunk columns in orig_tlist.
 */
static List *
postprocess_setop_tlist(List *new_tlist, List *orig_tlist)
{
1976 1977
	ListCell   *l;
	ListCell   *orig_tlist_item = list_head(orig_tlist);
1978 1979 1980 1981 1982 1983 1984

	foreach(l, new_tlist)
	{
		TargetEntry *new_tle = (TargetEntry *) lfirst(l);
		TargetEntry *orig_tle;

		/* ignore resjunk columns in setop result */
1985
		if (new_tle->resjunk)
1986 1987
			continue;

1988 1989 1990
		Assert(orig_tlist_item != NULL);
		orig_tle = (TargetEntry *) lfirst(orig_tlist_item);
		orig_tlist_item = lnext(orig_tlist_item);
B
Bruce Momjian 已提交
1991
		if (orig_tle->resjunk)	/* should not happen */
1992
			elog(ERROR, "resjunk output columns are not implemented");
1993 1994
		Assert(new_tle->resno == orig_tle->resno);
		new_tle->ressortgroupref = orig_tle->ressortgroupref;
1995
	}
1996
	if (orig_tlist_item != NULL)
1997
		elog(ERROR, "resjunk output columns are not implemented");
1998 1999
	return new_tlist;
}