WIP: log query in auto-explain - Mailing list pgsql-hackers

From Andrew Dunstan
Subject WIP: log query in auto-explain
Date
Msg-id 4B09979C.1010505@dunslane.net
Whole thread Raw
Responses Re: WIP: log query in auto-explain
Re: WIP: log query in auto-explain
List pgsql-hackers
I put the attached together a while ago and neglected to send it.
Basically it includes the text of the query being explained in the
explain output.

I think it's more important to include the query text when using
auto_explain than in other explain uses, which is why I did it this way.
But I could be persuaded that we should always include it.

Thoughts? Comments?

cheers

andrew
Index: src/include/commands/explain.h
===================================================================
RCS file: /cvsroot/pgsql/src/include/commands/explain.h,v
retrieving revision 1.41
diff -c -r1.41 explain.h
*** src/include/commands/explain.h    10 Aug 2009 05:46:50 -0000    1.41
--- src/include/commands/explain.h    22 Nov 2009 19:50:13 -0000
***************
*** 54,59 ****
--- 54,63 ----

  extern void ExplainInitState(ExplainState *es);

+ extern void ExplainBeginOutput(ExplainState *es);
+
+ extern void ExplainEndOutput(ExplainState *es);
+
  extern TupleDesc ExplainResultDesc(ExplainStmt *stmt);

  extern void ExplainOneUtility(Node *utilityStmt, ExplainState *es,
***************
*** 62,67 ****
--- 66,73 ----
  extern void ExplainOnePlan(PlannedStmt *plannedstmt, ExplainState *es,
                 const char *queryString, ParamListInfo params);

+ extern void ExplainQueryText(ExplainState *es, QueryDesc *queryDesc);
+
  extern void ExplainPrintPlan(ExplainState *es, QueryDesc *queryDesc);

  extern void ExplainSeparatePlans(ExplainState *es);
Index: src/backend/commands/explain.c
===================================================================
RCS file: /cvsroot/pgsql/src/backend/commands/explain.c,v
retrieving revision 1.193
diff -c -r1.193 explain.c
*** src/backend/commands/explain.c    4 Nov 2009 22:26:04 -0000    1.193
--- src/backend/commands/explain.c    22 Nov 2009 19:50:13 -0000
***************
*** 91,98 ****
                   bool labeled, ExplainState *es);
  static void ExplainDummyGroup(const char *objtype, const char *labelname,
                                ExplainState *es);
- static void ExplainBeginOutput(ExplainState *es);
- static void ExplainEndOutput(ExplainState *es);
  static void ExplainXMLTag(const char *tagname, int flags, ExplainState *es);
  static void ExplainJSONLineEnding(ExplainState *es);
  static void escape_json(StringInfo buf, const char *str);
--- 91,96 ----
***************
*** 469,474 ****
--- 467,486 ----
  }

  /*
+  * ExplainQueryText -
+  *    add a "Query Text" node that conatins the actual text of the query
+  *
+  * The caller should have set up the options fields of *es, as well as
+  * initializing the output buffer es->str.
+  *
+  */
+ void
+ ExplainQueryText(ExplainState *es, QueryDesc *queryDesc)
+ {
+     ExplainPropertyText("Query Text", queryDesc->sourceText, es);
+ }
+
+ /*
   * report_triggers -
   *        report execution stats for a single relation's triggers
   */
***************
*** 1738,1744 ****
   * This is just enough different from processing a subgroup that we need
   * a separate pair of subroutines.
   */
! static void
  ExplainBeginOutput(ExplainState *es)
  {
      switch (es->format)
--- 1750,1756 ----
   * This is just enough different from processing a subgroup that we need
   * a separate pair of subroutines.
   */
! void
  ExplainBeginOutput(ExplainState *es)
  {
      switch (es->format)
***************
*** 1765,1771 ****
  /*
   * Emit the end-of-output boilerplate.
   */
! static void
  ExplainEndOutput(ExplainState *es)
  {
      switch (es->format)
--- 1777,1783 ----
  /*
   * Emit the end-of-output boilerplate.
   */
! void
  ExplainEndOutput(ExplainState *es)
  {
      switch (es->format)
Index: contrib/auto_explain/auto_explain.c
===================================================================
RCS file: /cvsroot/pgsql/contrib/auto_explain/auto_explain.c,v
retrieving revision 1.7
diff -c -r1.7 auto_explain.c
*** contrib/auto_explain/auto_explain.c    10 Aug 2009 05:46:49 -0000    1.7
--- contrib/auto_explain/auto_explain.c    22 Nov 2009 19:50:13 -0000
***************
*** 222,228 ****
--- 222,231 ----
              es.verbose = auto_explain_log_verbose;
              es.format = auto_explain_log_format;

+             ExplainBeginOutput(&es);
+             ExplainQueryText(&es, queryDesc);
              ExplainPrintPlan(&es, queryDesc);
+             ExplainEndOutput(&es);

              /* Remove last line break */
              if (es.str->len > 0 && es.str->data[es.str->len - 1] == '\n')
***************
*** 236,242 ****
               */
              ereport(LOG,
                      (errmsg("duration: %.3f ms  plan:\n%s",
!                             msec, es.str->data)));

              pfree(es.str->data);
          }
--- 239,246 ----
               */
              ereport(LOG,
                      (errmsg("duration: %.3f ms  plan:\n%s",
!                             msec, es.str->data),
!                      errhidestmt(true)));

              pfree(es.str->data);
          }

pgsql-hackers by date:

Previous
From: Magnus Hagander
Date:
Subject: Re: forget patch win32.mak.
Next
From: Stephan Szabo
Date:
Subject: Re: Partitioning option for COPY