explain.c 7.4 KB
Newer Older
1 2 3
/*-------------------------------------------------------------------------
 *
 * explain.c--
4
 *	  Explain the query execution plan
5 6 7 8 9
 *
 * Copyright (c) 1994-5, Regents of the University of California
 *
 *
 * IDENTIFICATION
B
Bruce Momjian 已提交
10
 *	  $Header: /cvsroot/pgsql/src/backend/commands/explain.c,v 1.25 1998/10/21 16:21:20 momjian Exp $
11 12 13
 *
 *-------------------------------------------------------------------------
 */
14
#include <stdio.h>
15
#include <string.h>
16

M
Marc G. Fournier 已提交
17 18 19
#include <postgres.h>

#include <nodes/plannodes.h>
20
#include <nodes/print.h>
M
Marc G. Fournier 已提交
21 22 23
#include <tcop/tcopprot.h>
#include <lib/stringinfo.h>
#include <commands/explain.h>
24
#include <parser/parsetree.h>
25
#include <parser/parse_node.h>
M
Marc G. Fournier 已提交
26 27
#include <optimizer/planner.h>
#include <access/xact.h>
28
#include <utils/relcache.h>
B
Bruce Momjian 已提交
29
#include <rewrite/rewriteHandler.h>
30

31 32 33
typedef struct ExplainState
{
	/* options */
34
	bool		printCost;		/* print cost */
35
	bool		printNodes;		/* do nodeToString() instead */
36
	/* other states */
37
	List	   *rtable;			/* range table */
38
} ExplainState;
39

40
static char *Explain_PlanToString(Plan *plan, ExplainState *es);
B
Bruce Momjian 已提交
41 42
static void ExplainOneQuery(Query *query, bool verbose, CommandDest dest);

43 44 45

/*
 * ExplainQuery -
46
 *	  print out the execution plan for a given query
47 48 49
 *
 */
void
50
ExplainQuery(Query *query, bool verbose, CommandDest dest)
51
{
B
Bruce Momjian 已提交
52 53
	List	*rewritten;
	List	*l;
54

55 56
	if (IsAbortedTransactionBlockState())
	{
57
		char	   *tag = "*ABORT STATE*";
58

59 60 61 62 63 64 65
		EndCommand(tag, dest);

		elog(NOTICE, "(transaction aborted): %s",
			 "queries ignored until END");

		return;
	}
66

B
Bruce Momjian 已提交
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
	/* Rewrite through rule system */
	rewritten = QueryRewrite(query);

	/* In the case of an INSTEAD NOTHING, tell at least that */
	if (rewritten == NIL)
	{
		elog(NOTICE, "query rewrites to nothing");
		return;
	}

	/* Explain every plan */
	foreach(l, rewritten)
		ExplainOneQuery(lfirst(l), verbose, dest);
}

/*
 * ExplainOneQuery -
 *	  print out the execution plan for one query
 *
 */
static void
ExplainOneQuery(Query *query, bool verbose, CommandDest dest)
{
	char	   *s = NULL,
			   *s2;
	Plan	   *plan;
	ExplainState *es;
	int			len;

96 97
	/* plan the queries (XXX we've ignored rewrite!!) */
	plan = planner(query);
98

99 100 101
	/* pg_plan could have failed */
	if (plan == NULL)
		return;
B
Bruce Momjian 已提交
102

B
Bruce Momjian 已提交
103
	es = (ExplainState *) palloc(sizeof(ExplainState));
B
Bruce Momjian 已提交
104
	MemSet(es, 0, sizeof(ExplainState));
B
Bruce Momjian 已提交
105

106
	es->printCost = true;		/* default */
107

108 109
	if (verbose)
		es->printNodes = true;
B
Bruce Momjian 已提交
110

111 112 113
	es->rtable = query->rtable;

	if (es->printNodes)
114
		s = nodeToString(plan);
115

116 117 118 119 120 121 122 123 124 125
	if (es->printCost)
	{
		s2 = Explain_PlanToString(plan, es);
		if (s == NULL)
			s = s2;
		else
		{
			strcat(s, "\n\n");
			strcat(s, s2);
		}
B
Bruce Momjian 已提交
126
	}
127 128 129 130 131 132 133 134 135 136 137

	/* output the plan */
	len = strlen(s);
	elog(NOTICE, "QUERY PLAN:\n\n%.*s", ELOG_MAXLEN - 64, s);
	len -= ELOG_MAXLEN - 64;
	while (len > 0)
	{
		s += ELOG_MAXLEN - 64;
		elog(NOTICE, "%.*s", ELOG_MAXLEN - 64, s);
		len -= ELOG_MAXLEN - 64;
	}
138
	if (es->printNodes)
139
		pprint(plan);			/* display in postmaster log file */
140

B
Bruce Momjian 已提交
141
	pfree(es);
142 143 144 145 146 147 148 149
}

/*****************************************************************************
 *
 *****************************************************************************/

/*
 * explain_outNode -
150
 *	  converts a Node into ascii string and append it to 'str'
151 152
 */
static void
153
explain_outNode(StringInfo str, Plan *plan, int indent, ExplainState *es)
154
{
155
	List	   *l;
156
	Relation	relation;
157 158 159
	char	   *pname;
	char		buf[1000];
	int			i;
160 161 162 163 164 165 166 167 168

	if (plan == NULL)
	{
		appendStringInfo(str, "\n");
		return;
	}

	switch (nodeTag(plan))
	{
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
		case T_Result:
			pname = "Result";
			break;
		case T_Append:
			pname = "Append";
			break;
		case T_NestLoop:
			pname = "Nested Loop";
			break;
		case T_MergeJoin:
			pname = "Merge Join";
			break;
		case T_HashJoin:
			pname = "Hash Join";
			break;
		case T_SeqScan:
			pname = "Seq Scan";
			break;
		case T_IndexScan:
			pname = "Index Scan";
			break;
		case T_Temp:
			pname = "Temp Scan";
			break;
		case T_Sort:
			pname = "Sort";
			break;
		case T_Group:
			pname = "Group";
			break;
		case T_Agg:
			pname = "Aggregate";
			break;
		case T_Unique:
			pname = "Unique";
			break;
		case T_Hash:
			pname = "Hash";
			break;
		case T_Tee:
			pname = "Tee";
			break;
		default:
			pname = "";
			break;
214 215
	}

V
Vadim B. Mikheev 已提交
216
#if 0
217 218
	for (i = 0; i < indent; i++)
		appendStringInfo(str, "  ");
V
Vadim B. Mikheev 已提交
219
#endif
220 221 222 223

	appendStringInfo(str, pname);
	switch (nodeTag(plan))
	{
224
		case T_IndexScan:
225 226 227 228 229
			appendStringInfo(str, " using ");
			l = ((IndexScan *) plan)->indxid;
			relation = RelationIdCacheGetRelation((int) lfirst(l));
			appendStringInfo(str, (RelationGetRelationName(relation))->data);
		case T_SeqScan:
230 231 232 233
			if (((Scan *) plan)->scanrelid > 0)
			{
				RangeTblEntry *rte = nth(((Scan *) plan)->scanrelid - 1, es->rtable);

B
Bruce Momjian 已提交
234 235 236 237 238 239 240
				appendStringInfo(str, " on ");
				if (strcmp(rte->refname, rte->relname) != 0)
				{
					sprintf(buf, "%s ", rte->relname);
					appendStringInfo(str, buf);
				}
				appendStringInfo(str, rte->refname);
241 242 243 244
			}
			break;
		default:
			break;
245 246 247 248 249 250 251
	}
	if (es->printCost)
	{
		sprintf(buf, "  (cost=%.2f size=%d width=%d)",
				plan->cost, plan->plan_size, plan->plan_width);
		appendStringInfo(str, buf);
	}
252
	appendStringInfo(str, "\n");
253

V
Vadim B. Mikheev 已提交
254 255 256
	/* initPlan-s */
	if (plan->initPlan)
	{
257 258 259
		List	   *saved_rtable = es->rtable;
		List	   *lst;

V
Vadim B. Mikheev 已提交
260 261 262
		for (i = 0; i < indent; i++)
			appendStringInfo(str, "  ");
		appendStringInfo(str, "  InitPlan\n");
263
		foreach(lst, plan->initPlan)
V
Vadim B. Mikheev 已提交
264
		{
265
			es->rtable = ((SubPlan *) lfirst(lst))->rtable;
V
Vadim B. Mikheev 已提交
266 267 268
			for (i = 0; i < indent; i++)
				appendStringInfo(str, "  ");
			appendStringInfo(str, "    ->  ");
B
Bruce Momjian 已提交
269
			explain_outNode(str, ((SubPlan *) lfirst(lst))->plan, indent + 2, es);
V
Vadim B. Mikheev 已提交
270 271 272
		}
		es->rtable = saved_rtable;
	}
273 274 275 276 277 278

	/* lefttree */
	if (outerPlan(plan))
	{
		for (i = 0; i < indent; i++)
			appendStringInfo(str, "  ");
V
Vadim B. Mikheev 已提交
279 280
		appendStringInfo(str, "  ->  ");
		explain_outNode(str, outerPlan(plan), indent + 3, es);
281
	}
282 283 284 285 286 287

	/* righttree */
	if (innerPlan(plan))
	{
		for (i = 0; i < indent; i++)
			appendStringInfo(str, "  ");
V
Vadim B. Mikheev 已提交
288 289 290
		appendStringInfo(str, "  ->  ");
		explain_outNode(str, innerPlan(plan), indent + 3, es);
	}
291

V
Vadim B. Mikheev 已提交
292 293 294
	/* subPlan-s */
	if (plan->subPlan)
	{
295 296 297
		List	   *saved_rtable = es->rtable;
		List	   *lst;

V
Vadim B. Mikheev 已提交
298 299 300
		for (i = 0; i < indent; i++)
			appendStringInfo(str, "  ");
		appendStringInfo(str, "  SubPlan\n");
301
		foreach(lst, plan->subPlan)
V
Vadim B. Mikheev 已提交
302
		{
303
			es->rtable = ((SubPlan *) lfirst(lst))->rtable;
V
Vadim B. Mikheev 已提交
304 305 306
			for (i = 0; i < indent; i++)
				appendStringInfo(str, "  ");
			appendStringInfo(str, "    ->  ");
307
			explain_outNode(str, ((SubPlan *) lfirst(lst))->plan, indent + 4, es);
V
Vadim B. Mikheev 已提交
308 309
		}
		es->rtable = saved_rtable;
310
	}
311 312 313 314 315 316

	if (nodeTag(plan) == T_Append)
	{
		List	   *saved_rtable = es->rtable;
		List	   *lst;
		int			whichplan = 0;
317
		Append	   *appendplan = (Append *) plan;
318 319 320

		foreach(lst, appendplan->appendplans)
		{
321
			Plan	   *subnode = (Plan *) lfirst(lst);
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344

			if (appendplan->inheritrelid > 0)
			{
				ResTarget  *rtentry;

				es->rtable = appendplan->inheritrtable;
				rtentry = nth(whichplan, appendplan->inheritrtable);
				Assert(rtentry != NULL);
				rt_store(appendplan->inheritrelid, es->rtable, rtentry);
			}
			else
				es->rtable = nth(whichplan, appendplan->unionrtables);

			for (i = 0; i < indent; i++)
				appendStringInfo(str, "  ");
			appendStringInfo(str, "    ->  ");

			explain_outNode(str, subnode, indent + 4, es);

			whichplan++;
		}
		es->rtable = saved_rtable;
	}
345
	return;
346 347
}

348
static char *
349
Explain_PlanToString(Plan *plan, ExplainState *es)
350
{
351 352
	StringInfo	str;
	char	   *s;
353 354 355 356 357 358 359 360 361 362

	if (plan == NULL)
		return "";
	Assert(plan != NULL);
	str = makeStringInfo();
	explain_outNode(str, plan, 0, es);
	s = str->data;
	pfree(str);

	return s;
363
}