From cd35e9d7468e8f86dd5a7d928707f4ba8cdae44d Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Wed, 19 Nov 2008 01:10:24 +0000 Subject: Some infrastructure changes for the upcoming auto-explain contrib module: * Refactor explain.c slightly to export a convenient-to-use subroutine for printing EXPLAIN results. * Provide hooks for plugins to get control at ExecutorStart and ExecutorEnd as well as ExecutorRun. * Add some minimal support for tracking the total runtime of ExecutorRun. This code won't actually do anything unless a plugin prods it to. * Change the API of the DefineCustomXXXVariable functions to allow nonzero "flags" to be specified for a custom GUC variable. While at it, also make the "bootstrap" default value for custom GUCs be explicitly specified as a parameter to these functions. This is to eliminate confusion over where the default comes from, as has been expressed in the past by some users of the custom-variable facility. * Refactor GUC code a bit to ensure that a custom variable gets initialized to something valid (like its default value) even if the placeholder value was invalid. --- src/backend/commands/explain.c | 46 +++++++++++++++++++++++++++++------------- 1 file changed, 32 insertions(+), 14 deletions(-) (limited to 'src/backend/commands/explain.c') diff --git a/src/backend/commands/explain.c b/src/backend/commands/explain.c index 300dc5f3fa4..e5f1b313076 100644 --- a/src/backend/commands/explain.c +++ b/src/backend/commands/explain.c @@ -7,7 +7,7 @@ * Portions Copyright (c) 1994-5, Regents of the University of California * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/commands/explain.c,v 1.180 2008/10/06 20:29:38 tgl Exp $ + * $PostgreSQL: pgsql/src/backend/commands/explain.c,v 1.181 2008/11/19 01:10:23 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -224,7 +224,6 @@ ExplainOnePlan(PlannedStmt *plannedstmt, ParamListInfo params, QueryDesc *queryDesc; instr_time starttime; double totaltime = 0; - ExplainState *es; StringInfoData buf; int eflags; @@ -265,17 +264,9 @@ ExplainOnePlan(PlannedStmt *plannedstmt, ParamListInfo params, totaltime += elapsed_time(&starttime); } - es = (ExplainState *) palloc0(sizeof(ExplainState)); - - es->printTList = stmt->verbose; - es->printAnalyze = stmt->analyze; - es->pstmt = queryDesc->plannedstmt; - es->rtable = queryDesc->plannedstmt->rtable; - + /* Create textual dump of plan tree */ initStringInfo(&buf); - explain_outNode(&buf, - queryDesc->plannedstmt->planTree, queryDesc->planstate, - NULL, 0, es); + ExplainPrintPlan(&buf, queryDesc, stmt->analyze, stmt->verbose); /* * If we ran the command, run any AFTER triggers it queued. (Note this @@ -290,7 +281,7 @@ ExplainOnePlan(PlannedStmt *plannedstmt, ParamListInfo params, } /* Print info about runtime of triggers */ - if (es->printAnalyze) + if (stmt->analyze) { ResultRelInfo *rInfo; bool show_relname; @@ -335,7 +326,34 @@ ExplainOnePlan(PlannedStmt *plannedstmt, ParamListInfo params, do_text_output_multiline(tstate, buf.data); pfree(buf.data); - pfree(es); +} + +/* + * ExplainPrintPlan - + * convert a QueryDesc's plan tree to text and append it to 'str' + * + * 'analyze' means to include runtime instrumentation results + * 'verbose' means a verbose printout (currently, it shows targetlists) + * + * NB: will not work on utility statements + */ +void +ExplainPrintPlan(StringInfo str, QueryDesc *queryDesc, + bool analyze, bool verbose) +{ + ExplainState es; + + Assert(queryDesc->plannedstmt != NULL); + + memset(&es, 0, sizeof(es)); + es.printTList = verbose; + es.printAnalyze = analyze; + es.pstmt = queryDesc->plannedstmt; + es.rtable = queryDesc->plannedstmt->rtable; + + explain_outNode(str, + queryDesc->plannedstmt->planTree, queryDesc->planstate, + NULL, 0, &es); } /* -- cgit v1.2.3