1998-12-14 09:11:17 +01:00
|
|
|
/*
|
1999-02-14 00:22:53 +01:00
|
|
|
* explain.c
|
1997-09-07 07:04:48 +02:00
|
|
|
* Explain the query execution plan
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
2000-01-26 06:58:53 +01:00
|
|
|
* Portions Copyright (c) 1996-2000, PostgreSQL, Inc
|
|
|
|
* Portions Copyright (c) 1994-5, Regents of the University of California
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
2000-06-19 00:44:35 +02:00
|
|
|
* $Header: /cvsroot/pgsql/src/backend/commands/explain.c,v 1.57 2000/06/18 22:43:58 tgl Exp $
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
*/
|
1996-10-23 09:42:13 +02:00
|
|
|
|
1999-07-16 01:04:24 +02:00
|
|
|
#include "postgres.h"
|
1996-11-06 09:21:43 +01:00
|
|
|
|
1999-07-16 01:04:24 +02:00
|
|
|
#include "commands/explain.h"
|
1999-07-16 07:00:38 +02:00
|
|
|
#include "lib/stringinfo.h"
|
|
|
|
#include "nodes/print.h"
|
1999-07-16 01:04:24 +02:00
|
|
|
#include "optimizer/planner.h"
|
1999-07-16 07:00:38 +02:00
|
|
|
#include "parser/parsetree.h"
|
1999-07-16 01:04:24 +02:00
|
|
|
#include "rewrite/rewriteHandler.h"
|
1999-07-16 07:00:38 +02:00
|
|
|
#include "utils/relcache.h"
|
1996-07-09 08:22:35 +02:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
typedef struct ExplainState
|
|
|
|
{
|
|
|
|
/* options */
|
1997-09-08 04:41:22 +02:00
|
|
|
bool printCost; /* print cost */
|
1998-08-04 17:00:28 +02:00
|
|
|
bool printNodes; /* do nodeToString() instead */
|
1997-09-07 07:04:48 +02:00
|
|
|
/* other states */
|
1997-09-08 04:41:22 +02:00
|
|
|
List *rtable; /* range table */
|
1997-09-08 23:56:23 +02:00
|
|
|
} ExplainState;
|
1996-07-09 08:22:35 +02:00
|
|
|
|
1997-09-08 23:56:23 +02:00
|
|
|
static char *Explain_PlanToString(Plan *plan, ExplainState *es);
|
1998-10-21 18:21:29 +02:00
|
|
|
static void ExplainOneQuery(Query *query, bool verbose, CommandDest dest);
|
|
|
|
|
1999-08-31 03:28:37 +02:00
|
|
|
/* Convert a null string pointer into "<>" */
|
|
|
|
#define stringStringInfo(s) (((s) == NULL) ? "<>" : (s))
|
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* ExplainQuery -
|
1997-09-07 07:04:48 +02:00
|
|
|
* print out the execution plan for a given query
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
void
|
1997-09-08 23:56:23 +02:00
|
|
|
ExplainQuery(Query *query, bool verbose, CommandDest dest)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1999-05-25 18:15:34 +02:00
|
|
|
List *rewritten;
|
|
|
|
List *l;
|
1996-07-09 08:22:35 +02:00
|
|
|
|
1999-05-10 01:31:47 +02:00
|
|
|
/* rewriter and planner may not work in aborted state? */
|
1997-09-07 07:04:48 +02:00
|
|
|
if (IsAbortedTransactionBlockState())
|
|
|
|
{
|
|
|
|
elog(NOTICE, "(transaction aborted): %s",
|
|
|
|
"queries ignored until END");
|
1999-05-10 01:31:47 +02:00
|
|
|
return;
|
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1999-05-10 01:31:47 +02:00
|
|
|
/* rewriter and planner will not cope with utility statements */
|
|
|
|
if (query->commandType == CMD_UTILITY)
|
|
|
|
{
|
|
|
|
elog(NOTICE, "Utility statements have no plan structure");
|
1997-09-07 07:04:48 +02:00
|
|
|
return;
|
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
|
1998-10-21 18:21:29 +02:00
|
|
|
/* Rewrite through rule system */
|
|
|
|
rewritten = QueryRewrite(query);
|
|
|
|
|
|
|
|
/* In the case of an INSTEAD NOTHING, tell at least that */
|
|
|
|
if (rewritten == NIL)
|
|
|
|
{
|
1999-05-10 01:31:47 +02:00
|
|
|
elog(NOTICE, "Query rewrites to nothing");
|
1998-10-21 18:21:29 +02:00
|
|
|
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)
|
|
|
|
{
|
1998-11-08 20:38:34 +01:00
|
|
|
char *s;
|
1998-10-21 18:21:29 +02:00
|
|
|
Plan *plan;
|
|
|
|
ExplainState *es;
|
|
|
|
|
1999-05-10 01:31:47 +02:00
|
|
|
/* plan the query */
|
1997-09-07 07:04:48 +02:00
|
|
|
plan = planner(query);
|
1996-07-09 08:22:35 +02:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
/* pg_plan could have failed */
|
|
|
|
if (plan == NULL)
|
|
|
|
return;
|
1996-12-29 20:31:16 +01:00
|
|
|
|
1997-12-29 06:13:57 +01:00
|
|
|
es = (ExplainState *) palloc(sizeof(ExplainState));
|
1997-09-18 22:22:58 +02:00
|
|
|
MemSet(es, 0, sizeof(ExplainState));
|
1996-12-29 20:31:16 +01:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
es->printCost = true; /* default */
|
1996-07-09 08:22:35 +02:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
if (verbose)
|
|
|
|
es->printNodes = true;
|
1996-12-29 01:53:20 +01:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
es->rtable = query->rtable;
|
|
|
|
|
|
|
|
if (es->printNodes)
|
1998-11-08 20:38:34 +01:00
|
|
|
{
|
1998-08-04 17:00:28 +02:00
|
|
|
s = nodeToString(plan);
|
1998-11-08 20:38:34 +01:00
|
|
|
if (s)
|
|
|
|
{
|
1999-09-11 21:06:42 +02:00
|
|
|
elog(NOTICE, "QUERY DUMP:\n\n%s", s);
|
1998-11-08 20:38:34 +01:00
|
|
|
pfree(s);
|
|
|
|
}
|
|
|
|
}
|
1998-09-01 06:40:42 +02:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
if (es->printCost)
|
|
|
|
{
|
1998-11-08 20:38:34 +01:00
|
|
|
s = Explain_PlanToString(plan, es);
|
|
|
|
if (s)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
1999-09-11 21:06:42 +02:00
|
|
|
elog(NOTICE, "QUERY PLAN:\n\n%s", s);
|
1998-11-08 20:38:34 +01:00
|
|
|
pfree(s);
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
1996-12-29 01:53:20 +01:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1998-08-04 18:44:31 +02:00
|
|
|
if (es->printNodes)
|
1998-09-01 06:40:42 +02:00
|
|
|
pprint(plan); /* display in postmaster log file */
|
1998-08-04 18:44:31 +02:00
|
|
|
|
1997-12-29 06:13:57 +01:00
|
|
|
pfree(es);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*****************************************************************************
|
|
|
|
*
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* explain_outNode -
|
1997-09-07 07:04:48 +02:00
|
|
|
* converts a Node into ascii string and append it to 'str'
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
|
|
|
static void
|
1997-09-08 23:56:23 +02:00
|
|
|
explain_outNode(StringInfo str, Plan *plan, int indent, ExplainState *es)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1999-05-25 18:15:34 +02:00
|
|
|
List *l;
|
1998-04-27 18:57:09 +02:00
|
|
|
Relation relation;
|
1999-05-25 18:15:34 +02:00
|
|
|
char *pname;
|
|
|
|
int i;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
if (plan == NULL)
|
|
|
|
{
|
|
|
|
appendStringInfo(str, "\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (nodeTag(plan))
|
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
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;
|
1999-08-17 01:47:23 +02:00
|
|
|
case T_Material:
|
|
|
|
pname = "Materialize";
|
|
|
|
break;
|
1997-09-08 04:41:22 +02:00
|
|
|
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;
|
1999-11-23 21:07:06 +01:00
|
|
|
case T_TidScan:
|
|
|
|
pname = "Tid Scan";
|
|
|
|
break;
|
1997-09-08 04:41:22 +02:00
|
|
|
default:
|
1999-05-10 01:31:47 +02:00
|
|
|
pname = "???";
|
1997-09-08 04:41:22 +02:00
|
|
|
break;
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
appendStringInfo(str, pname);
|
|
|
|
switch (nodeTag(plan))
|
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
case T_IndexScan:
|
2000-04-12 19:17:23 +02:00
|
|
|
if (ScanDirectionIsBackward(((IndexScan *) plan)->indxorderdir))
|
1999-08-09 08:20:27 +02:00
|
|
|
appendStringInfo(str, " Backward");
|
1998-04-27 18:57:09 +02:00
|
|
|
appendStringInfo(str, " using ");
|
1998-11-22 11:48:45 +01:00
|
|
|
i = 0;
|
1999-05-25 18:15:34 +02:00
|
|
|
foreach(l, ((IndexScan *) plan)->indxid)
|
1998-11-22 11:48:45 +01:00
|
|
|
{
|
1999-09-18 21:08:25 +02:00
|
|
|
relation = RelationIdGetRelation(lfirsti(l));
|
|
|
|
Assert(relation);
|
2000-02-15 21:49:31 +01:00
|
|
|
appendStringInfo(str, "%s%s",
|
|
|
|
(++i > 1) ? ", " : "",
|
2000-04-12 19:17:23 +02:00
|
|
|
stringStringInfo(RelationGetRelationName(relation)));
|
1999-09-18 21:08:25 +02:00
|
|
|
/* drop relcache refcount from RelationIdGetRelation */
|
|
|
|
RelationDecrementReferenceCount(relation);
|
1998-11-22 11:48:45 +01:00
|
|
|
}
|
2000-02-15 21:49:31 +01:00
|
|
|
/* FALL THRU */
|
1998-04-27 18:57:09 +02:00
|
|
|
case T_SeqScan:
|
2000-02-15 21:49:31 +01:00
|
|
|
case T_TidScan:
|
1997-09-08 04:41:22 +02:00
|
|
|
if (((Scan *) plan)->scanrelid > 0)
|
|
|
|
{
|
|
|
|
RangeTblEntry *rte = nth(((Scan *) plan)->scanrelid - 1, es->rtable);
|
|
|
|
|
2000-02-15 21:49:31 +01:00
|
|
|
appendStringInfo(str, " on %s",
|
|
|
|
stringStringInfo(rte->relname));
|
2000-03-15 00:06:59 +01:00
|
|
|
if (rte->ref != NULL)
|
|
|
|
{
|
|
|
|
if ((strcmp(rte->ref->relname, rte->relname) != 0)
|
|
|
|
|| (length(rte->ref->attrs) > 0))
|
|
|
|
{
|
|
|
|
appendStringInfo(str, " %s",
|
2000-04-12 19:17:23 +02:00
|
|
|
stringStringInfo(rte->ref->relname));
|
2000-03-15 00:06:59 +01:00
|
|
|
|
|
|
|
if (length(rte->ref->attrs) > 0)
|
|
|
|
{
|
2000-04-12 19:17:23 +02:00
|
|
|
List *c;
|
|
|
|
int firstEntry = true;
|
2000-03-15 00:06:59 +01:00
|
|
|
|
|
|
|
appendStringInfo(str, " (");
|
2000-04-12 19:17:23 +02:00
|
|
|
foreach(c, rte->ref->attrs)
|
2000-03-15 00:06:59 +01:00
|
|
|
{
|
2000-04-12 19:17:23 +02:00
|
|
|
if (!firstEntry)
|
2000-03-15 00:06:59 +01:00
|
|
|
{
|
|
|
|
appendStringInfo(str, ", ");
|
|
|
|
firstEntry = false;
|
|
|
|
}
|
|
|
|
appendStringInfo(str, "%s", strVal(lfirst(c)));
|
|
|
|
}
|
|
|
|
appendStringInfo(str, ")");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
1999-11-23 21:07:06 +01:00
|
|
|
}
|
|
|
|
break;
|
1997-09-08 04:41:22 +02:00
|
|
|
default:
|
|
|
|
break;
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
|
|
|
if (es->printCost)
|
|
|
|
{
|
2000-02-15 21:49:31 +01:00
|
|
|
appendStringInfo(str, " (cost=%.2f..%.2f rows=%.0f width=%d)",
|
|
|
|
plan->startup_cost, plan->total_cost,
|
|
|
|
plan->plan_rows, plan->plan_width);
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
appendStringInfo(str, "\n");
|
1998-02-26 05:46:47 +01:00
|
|
|
|
1998-02-13 04:21:30 +01:00
|
|
|
/* initPlan-s */
|
|
|
|
if (plan->initPlan)
|
|
|
|
{
|
1998-02-26 05:46:47 +01:00
|
|
|
List *saved_rtable = es->rtable;
|
|
|
|
List *lst;
|
|
|
|
|
1999-05-25 18:15:34 +02:00
|
|
|
for (i = 0; i < indent; i++)
|
1998-02-13 04:21:30 +01:00
|
|
|
appendStringInfo(str, " ");
|
|
|
|
appendStringInfo(str, " InitPlan\n");
|
1998-02-26 05:46:47 +01:00
|
|
|
foreach(lst, plan->initPlan)
|
1998-02-13 04:21:30 +01:00
|
|
|
{
|
1998-02-26 05:46:47 +01:00
|
|
|
es->rtable = ((SubPlan *) lfirst(lst))->rtable;
|
1998-02-13 04:21:30 +01:00
|
|
|
for (i = 0; i < indent; i++)
|
|
|
|
appendStringInfo(str, " ");
|
|
|
|
appendStringInfo(str, " -> ");
|
1998-10-21 18:21:29 +02:00
|
|
|
explain_outNode(str, ((SubPlan *) lfirst(lst))->plan, indent + 2, es);
|
1998-02-13 04:21:30 +01:00
|
|
|
}
|
|
|
|
es->rtable = saved_rtable;
|
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
/* lefttree */
|
|
|
|
if (outerPlan(plan))
|
|
|
|
{
|
|
|
|
for (i = 0; i < indent; i++)
|
|
|
|
appendStringInfo(str, " ");
|
1998-02-13 04:21:30 +01:00
|
|
|
appendStringInfo(str, " -> ");
|
|
|
|
explain_outNode(str, outerPlan(plan), indent + 3, es);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
/* righttree */
|
|
|
|
if (innerPlan(plan))
|
|
|
|
{
|
|
|
|
for (i = 0; i < indent; i++)
|
|
|
|
appendStringInfo(str, " ");
|
1998-02-13 04:21:30 +01:00
|
|
|
appendStringInfo(str, " -> ");
|
|
|
|
explain_outNode(str, innerPlan(plan), indent + 3, es);
|
|
|
|
}
|
1998-02-26 05:46:47 +01:00
|
|
|
|
1998-02-13 04:21:30 +01:00
|
|
|
/* subPlan-s */
|
|
|
|
if (plan->subPlan)
|
|
|
|
{
|
1998-02-26 05:46:47 +01:00
|
|
|
List *saved_rtable = es->rtable;
|
|
|
|
List *lst;
|
|
|
|
|
1998-02-13 04:21:30 +01:00
|
|
|
for (i = 0; i < indent; i++)
|
|
|
|
appendStringInfo(str, " ");
|
|
|
|
appendStringInfo(str, " SubPlan\n");
|
1998-02-26 05:46:47 +01:00
|
|
|
foreach(lst, plan->subPlan)
|
1998-02-13 04:21:30 +01:00
|
|
|
{
|
1998-02-26 05:46:47 +01:00
|
|
|
es->rtable = ((SubPlan *) lfirst(lst))->rtable;
|
1998-02-13 04:21:30 +01:00
|
|
|
for (i = 0; i < indent; i++)
|
|
|
|
appendStringInfo(str, " ");
|
|
|
|
appendStringInfo(str, " -> ");
|
1998-02-26 05:46:47 +01:00
|
|
|
explain_outNode(str, ((SubPlan *) lfirst(lst))->plan, indent + 4, es);
|
1998-02-13 04:21:30 +01:00
|
|
|
}
|
|
|
|
es->rtable = saved_rtable;
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
1998-07-15 16:54:39 +02:00
|
|
|
|
|
|
|
if (nodeTag(plan) == T_Append)
|
|
|
|
{
|
|
|
|
List *saved_rtable = es->rtable;
|
|
|
|
List *lst;
|
|
|
|
int whichplan = 0;
|
1998-09-01 06:40:42 +02:00
|
|
|
Append *appendplan = (Append *) plan;
|
1998-07-15 16:54:39 +02:00
|
|
|
|
|
|
|
foreach(lst, appendplan->appendplans)
|
|
|
|
{
|
1998-09-01 06:40:42 +02:00
|
|
|
Plan *subnode = (Plan *) lfirst(lst);
|
1998-07-15 16:54:39 +02:00
|
|
|
|
|
|
|
if (appendplan->inheritrelid > 0)
|
|
|
|
{
|
1999-07-17 21:01:21 +02:00
|
|
|
RangeTblEntry *rtentry;
|
1998-07-15 16:54:39 +02:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
1997-09-08 04:41:22 +02:00
|
|
|
static char *
|
1997-09-08 23:56:23 +02:00
|
|
|
Explain_PlanToString(Plan *plan, ExplainState *es)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1999-05-25 18:15:34 +02:00
|
|
|
StringInfoData str;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1999-04-25 05:19:27 +02:00
|
|
|
/* see stringinfo.h for an explanation of this maneuver */
|
|
|
|
initStringInfo(&str);
|
|
|
|
if (plan != NULL)
|
|
|
|
explain_outNode(&str, plan, 0, es);
|
|
|
|
return str.data;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|