explain.c 4.64 KB
Newer Older
1 2 3 4 5 6 7 8 9
/*-------------------------------------------------------------------------
 *
 * explain.c--
 *    Explain the query execution plan
 *
 * Copyright (c) 1994-5, Regents of the University of California
 *
 *
 * IDENTIFICATION
10
 *    $Header: /cvsroot/pgsql/src/backend/commands/explain.c,v 1.10 1997/08/18 20:52:17 momjian Exp $
11 12 13
 *
 *-------------------------------------------------------------------------
 */
14
#include <stdio.h>
15
#include <string.h>
16

17 18 19 20 21 22 23 24 25 26
#include <postgres.h>

#include <parser/catalog_utils.h>
#include <parser/parse_query.h>	    /* for MakeTimeRange() */
#include <nodes/plannodes.h>
#include <tcop/tcopprot.h>
#include <lib/stringinfo.h>
#include <commands/explain.h>
#include <optimizer/planner.h>
#include <access/xact.h>
27 28 29

typedef struct ExplainState {
    /* options */
Bruce Momjian's avatar
Bruce Momjian committed
30 31
    bool		printCost;	/* print cost */
    bool		printNodes;	/* do nodeToString() instead */
32 33 34 35 36 37 38 39 40 41 42 43
    /* other states */
    List 	*rtable;	/* range table */
} ExplainState;

static char *Explain_PlanToString(Plan *plan, ExplainState *es);

/*
 * ExplainQuery -
 *    print out the execution plan for a given query
 *
 */
void
44
ExplainQuery(Query *query, bool verbose, CommandDest dest)
45
{
46
    char *s = NULL, *s2;
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
    Plan *plan;
    ExplainState *es;
    int len;

    if (IsAbortedTransactionBlockState()) {
	char *tag = "*ABORT STATE*";
	EndCommand(tag, dest);
		
	elog(NOTICE, "(transaction aborted): %s",
	     "queries ignored until END");
		
	return;
    }

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

    /* pg_plan could have failed */
    if (plan == NULL)
	return;

    es = (ExplainState*)malloc(sizeof(ExplainState));
    memset(es, 0, sizeof(ExplainState));

71
    es->printCost = true;	/* default */
Bruce Momjian's avatar
Bruce Momjian committed
72

73 74
    if (verbose)
	es->printNodes = true;
Bruce Momjian's avatar
Bruce Momjian committed
75

76 77
    es->rtable = query->rtable;

78
    if (es->printNodes)
79
	s = nodeToString(plan);
80 81 82 83 84 85 86 87 88

    if (es->printCost) {
	s2 = Explain_PlanToString(plan, es);
	if (s == NULL)
	    s = s2;
	else {
	    strcat(s, "\n\n");
	    strcat(s, s2);
	}
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
    }

    /* 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;
    }
    free(es);
}

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

/*
 * explain_outNode -
 *    converts a Node into ascii string and append it to 'str'
 */
static void
explain_outNode(StringInfo str, Plan *plan, int indent, ExplainState *es)
{
    char *pname;
    char buf[1000];
    int i;
    
    if (plan==NULL) {
	appendStringInfo(str, "\n");
	return;
    }

    switch(nodeTag(plan)) {
    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;
151 152 153
    case T_Group:
	pname = "Group";
	break;
154 155 156 157 158 159 160 161 162 163 164 165 166
    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:
167
	pname = "";
168 169 170 171 172 173 174 175 176 177 178 179
	break;
    }

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

    appendStringInfo(str, pname);
    switch(nodeTag(plan)) {
    case T_SeqScan:
    case T_IndexScan:
	if (((Scan*)plan)->scanrelid > 0) {
	    RangeTblEntry *rte = nth(((Scan*)plan)->scanrelid-1, es->rtable);
180
	    sprintf(buf, " on %s", rte->refname);
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 214 215 216 217 218 219 220 221 222 223 224 225 226 227
	    appendStringInfo(str, buf);
	}
	break;
    default:
	break;
    }
    if (es->printCost) {
	sprintf(buf, "  (cost=%.2f size=%d width=%d)",
		plan->cost, plan->plan_size, plan->plan_width);
	appendStringInfo(str, buf);
    }
    appendStringInfo(str, "\n");

    /* lefttree */
    if (outerPlan(plan)) {
	for(i=0; i < indent; i++)
	    appendStringInfo(str, "  ");
	appendStringInfo(str, "  -> ");
	explain_outNode(str, outerPlan(plan), indent+1, es);
    }

    /* righttree */
    if (innerPlan(plan)) {
	for(i=0; i < indent; i++)
	    appendStringInfo(str, "  ");
	appendStringInfo(str, "  -> ");
	explain_outNode(str, innerPlan(plan), indent+1, es);
    }
    return;
}

static char *
Explain_PlanToString(Plan *plan, ExplainState *es)
{
    StringInfo str;
    char *s;
    
    if (plan==NULL)
	return "";
    Assert(plan!=NULL);
    str = makeStringInfo();
    explain_outNode(str, plan, 0, es);
    s = str->data;
    pfree(str);

    return s;
}