Skip to content

Commit dd9af92

Browse files
committed
Add display of sort keys to the default EXPLAIN output.
1 parent a5b3709 commit dd9af92

File tree

3 files changed

+87
-4
lines changed

3 files changed

+87
-4
lines changed

src/backend/commands/explain.c

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
66
* Portions Copyright (c) 1994-5, Regents of the University of California
77
*
8-
* $Header: /cvsroot/pgsql/src/backend/commands/explain.c,v 1.77 2002/05/12 20:10:02 tgl Exp $
8+
* $Header: /cvsroot/pgsql/src/backend/commands/explain.c,v 1.78 2002/05/18 21:38:40 tgl Exp $
99
*
1010
*/
1111

@@ -56,6 +56,8 @@ static void show_upper_qual(List *qual, const char *qlabel,
5656
const char *outer_name, int outer_varno, Plan *outer_plan,
5757
const char *inner_name, int inner_varno, Plan *inner_plan,
5858
StringInfo str, int indent, ExplainState *es);
59+
static void show_sort_keys(List *tlist, int nkeys, const char *qlabel,
60+
StringInfo str, int indent, ExplainState *es);
5961
static Node *make_ors_ands_explicit(List *orclauses);
6062
static TextOutputState *begin_text_output(CommandDest dest, char *title);
6163
static void do_text_output(TextOutputState *tstate, char *aline);
@@ -410,7 +412,7 @@ explain_outNode(StringInfo str, Plan *plan, Plan *outer_plan,
410412
}
411413
appendStringInfo(str, "\n");
412414

413-
/* quals */
415+
/* quals, sort keys, etc */
414416
switch (nodeTag(plan))
415417
{
416418
case T_IndexScan:
@@ -495,6 +497,11 @@ explain_outNode(StringInfo str, Plan *plan, Plan *outer_plan,
495497
"", 0, NULL,
496498
str, indent, es);
497499
break;
500+
case T_Sort:
501+
show_sort_keys(plan->targetlist, ((Sort *) plan)->keycount,
502+
"Sort Key",
503+
str, indent, es);
504+
break;
498505
case T_Result:
499506
show_upper_qual((List *) ((Result *) plan)->resconstantqual,
500507
"One-Time Filter",
@@ -731,6 +738,60 @@ show_upper_qual(List *qual, const char *qlabel,
731738
appendStringInfo(str, " %s: %s\n", qlabel, exprstr);
732739
}
733740

741+
/*
742+
* Show the sort keys for a Sort node.
743+
*/
744+
static void
745+
show_sort_keys(List *tlist, int nkeys, const char *qlabel,
746+
StringInfo str, int indent, ExplainState *es)
747+
{
748+
List *context;
749+
bool useprefix;
750+
int keyno;
751+
List *tl;
752+
char *exprstr;
753+
int i;
754+
755+
if (nkeys <= 0)
756+
return;
757+
758+
for (i = 0; i < indent; i++)
759+
appendStringInfo(str, " ");
760+
appendStringInfo(str, " %s: ", qlabel);
761+
762+
/*
763+
* In this routine we expect that the plan node's tlist has not been
764+
* processed by set_plan_references(), so any Vars will contain valid
765+
* varnos referencing the actual rtable.
766+
*/
767+
context = deparse_context_from_rtable(es->rtable);
768+
useprefix = length(es->rtable) > 1;
769+
770+
for (keyno = 1; keyno <= nkeys; keyno++)
771+
{
772+
/* find key expression in tlist */
773+
foreach(tl, tlist)
774+
{
775+
TargetEntry *target = (TargetEntry *) lfirst(tl);
776+
777+
if (target->resdom->reskey == keyno)
778+
{
779+
/* Deparse the expression */
780+
exprstr = deparse_expression(target->expr, context, useprefix);
781+
/* And add to str */
782+
if (keyno > 1)
783+
appendStringInfo(str, ", ");
784+
appendStringInfo(str, "%s", exprstr);
785+
break;
786+
}
787+
}
788+
if (tl == NIL)
789+
elog(ERROR, "show_sort_keys: no tlist entry for key %d", keyno);
790+
}
791+
792+
appendStringInfo(str, "\n");
793+
}
794+
734795
/*
735796
* Indexscan qual lists have an implicit OR-of-ANDs structure. Make it
736797
* explicit so deparsing works properly.

src/backend/utils/adt/ruleutils.c

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* back to source text
44
*
55
* IDENTIFICATION
6-
* $Header: /cvsroot/pgsql/src/backend/utils/adt/ruleutils.c,v 1.105 2002/05/17 01:19:18 tgl Exp $
6+
* $Header: /cvsroot/pgsql/src/backend/utils/adt/ruleutils.c,v 1.106 2002/05/18 21:38:40 tgl Exp $
77
*
88
* This software is copyrighted by Jan Wieck - Hamburg.
99
*
@@ -779,6 +779,27 @@ deparse_context_for_subplan(const char *name, List *tlist,
779779
return (Node *) rte;
780780
}
781781

782+
/*
783+
* deparse_context_from_rtable - Build deparse context given a rangetable
784+
*
785+
* This is suitable for deparsing expressions that refer to only a single
786+
* level of variables (no outer-reference Vars).
787+
*/
788+
List *
789+
deparse_context_from_rtable(List *rtable)
790+
{
791+
deparse_namespace *dpns;
792+
793+
dpns = (deparse_namespace *) palloc(sizeof(deparse_namespace));
794+
795+
dpns->rtable = rtable;
796+
dpns->outer_varno = dpns->inner_varno = 0;
797+
dpns->outer_rte = dpns->inner_rte = NULL;
798+
799+
/* Return a one-deep namespace stack */
800+
return makeList1(dpns);
801+
}
802+
782803
/* ----------
783804
* make_ruledef - reconstruct the CREATE RULE command
784805
* for a given pg_rewrite tuple

src/include/utils/builtins.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $Id: builtins.h,v 1.181 2002/05/12 20:10:05 tgl Exp $
10+
* $Id: builtins.h,v 1.182 2002/05/18 21:38:41 tgl Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -357,6 +357,7 @@ extern List *deparse_context_for_plan(int outer_varno, Node *outercontext,
357357
extern Node *deparse_context_for_rte(RangeTblEntry *rte);
358358
extern Node *deparse_context_for_subplan(const char *name, List *tlist,
359359
List *rtable);
360+
extern List *deparse_context_from_rtable(List *rtable);
360361
extern const char *quote_identifier(const char *ident);
361362
extern char *quote_qualified_identifier(const char *namespace,
362363
const char *ident);

0 commit comments

Comments
 (0)