1 /*-------------------------------------------------------------------------
4 * various print routines (used mostly for debugging)
6 * Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * $PostgreSQL: pgsql/src/backend/nodes/print.c,v 1.81 2006/08/02 01:59:45 joe Exp $
14 * AUTHOR DATE MAJOR EVENT
15 * Andrew Yu Oct 26, 1994 file creation
17 *-------------------------------------------------------------------------
22 #include "access/printtup.h"
23 #include "nodes/print.h"
24 #include "optimizer/clauses.h"
25 #include "parser/parsetree.h"
26 #include "utils/lsyscache.h"
28 static char *plannode_type(Plan *p);
32 * print contents of Node to stdout
40 s = nodeToString(obj);
41 f = format_node_dump(s);
50 * pretty-print contents of Node to stdout
58 s = nodeToString(obj);
59 f = pretty_format_node_dump(s);
68 * send pretty-printed contents of Node to postmaster log
71 elog_node_display(int lev, const char *title, void *obj, bool pretty)
76 s = nodeToString(obj);
78 f = pretty_format_node_dump(s);
80 f = format_node_dump(s);
83 (errmsg_internal("%s:", title),
89 * Format a nodeToString output for display on a terminal.
91 * The result is a palloc'd string.
93 * This version just tries to break at whitespace.
96 format_node_dump(const char *dump)
99 char line[LINELEN + 1];
105 initStringInfo(&str);
109 for (j = 0; j < LINELEN && dump[i] != '\0'; i++, j++)
115 /* ok to break at adjacent space */
120 for (k = j - 1; k > 0; k--)
125 /* back up; will reprint all after space */
131 appendStringInfo(&str, "%s\n", line);
136 appendStringInfo(&str, "%s\n", line);
143 * Format a nodeToString output for display on a terminal.
145 * The result is a palloc'd string.
147 * This version tries to indent intelligently.
150 pretty_format_node_dump(const char *dump)
155 char line[LINELEN + 1];
162 initStringInfo(&str);
163 indentLev = 0; /* logical indent level */
164 indentDist = 0; /* physical indent distance */
168 for (j = 0; j < indentDist; j++)
170 for (; j < LINELEN && dump[i] != '\0'; i++, j++)
178 /* print data before the } */
180 appendStringInfo(&str, "%s\n", line);
182 /* print the } at indentDist */
183 line[indentDist] = '}';
184 line[indentDist + 1] = '\0';
185 appendStringInfo(&str, "%s\n", line);
190 indentDist = Min(indentLev * INDENTSTOP, MAXINDENT);
193 /* j will equal indentDist on next loop iteration */
194 /* suppress whitespace just after } */
195 while (dump[i + 1] == ' ')
199 /* force line break after ), unless another ) follows */
200 if (dump[i + 1] != ')')
203 appendStringInfo(&str, "%s\n", line);
205 while (dump[i + 1] == ' ')
210 /* force line break before { */
214 appendStringInfo(&str, "%s\n", line);
218 indentDist = Min(indentLev * INDENTSTOP, MAXINDENT);
219 for (j = 0; j < indentDist; j++)
224 /* force line break before : */
228 appendStringInfo(&str, "%s\n", line);
238 appendStringInfo(&str, "%s\n", line);
241 appendStringInfo(&str, "%s\n", line);
250 * print contents of range table
253 print_rt(List *rtable)
258 printf("resno\trefname \trelid\tinFromCl\n");
259 printf("-----\t---------\t-----\t--------\n");
262 RangeTblEntry *rte = lfirst(l);
264 switch (rte->rtekind)
268 i, rte->eref->aliasname, rte->relid);
271 printf("%d\t%s\t[subquery]",
272 i, rte->eref->aliasname);
275 printf("%d\t%s\t[rangefunction]",
276 i, rte->eref->aliasname);
279 printf("%d\t%s\t[values list]",
280 i, rte->eref->aliasname);
283 printf("%d\t%s\t[join]",
284 i, rte->eref->aliasname);
287 printf("%d\t%s\t[special]",
288 i, rte->eref->aliasname);
291 printf("%d\t%s\t[unknown rtekind]",
292 i, rte->eref->aliasname);
296 (rte->inh ? "inh" : ""),
297 (rte->inFromCl ? "inFromCl" : ""));
305 * print an expression
308 print_expr(Node *expr, List *rtable)
318 Var *var = (Var *) expr;
336 Assert(var->varno > 0 &&
337 (int) var->varno <= list_length(rtable));
338 rte = rt_fetch(var->varno, rtable);
339 relname = rte->eref->aliasname;
340 attname = get_rte_attribute_name(rte, var->varattno);
344 printf("%s.%s", relname, attname);
346 else if (IsA(expr, Const))
348 Const *c = (Const *) expr;
359 getTypeOutputInfo(c->consttype,
360 &typoutput, &typIsVarlena);
362 outputstr = OidOutputFunctionCall(typoutput, c->constvalue);
363 printf("%s", outputstr);
366 else if (IsA(expr, OpExpr))
368 OpExpr *e = (OpExpr *) expr;
371 opname = get_opname(e->opno);
372 if (list_length(e->args) > 1)
374 print_expr(get_leftop((Expr *) e), rtable);
375 printf(" %s ", ((opname != NULL) ? opname : "(invalid operator)"));
376 print_expr(get_rightop((Expr *) e), rtable);
380 /* we print prefix and postfix ops the same... */
381 printf("%s ", ((opname != NULL) ? opname : "(invalid operator)"));
382 print_expr(get_leftop((Expr *) e), rtable);
385 else if (IsA(expr, FuncExpr))
387 FuncExpr *e = (FuncExpr *) expr;
391 funcname = get_func_name(e->funcid);
392 printf("%s(", ((funcname != NULL) ? funcname : "(invalid function)"));
395 print_expr(lfirst(l), rtable);
402 printf("unknown expr");
407 * pathkeys list of list of PathKeyItems
410 print_pathkeys(List *pathkeys, List *rtable)
417 List *pathkey = (List *) lfirst(i);
423 PathKeyItem *item = (PathKeyItem *) lfirst(k);
425 print_expr(item->key, rtable);
438 * print targetlist in a more legible way.
441 print_tl(List *tlist, List *rtable)
448 TargetEntry *tle = (TargetEntry *) lfirst(tl);
450 printf("\t%d %s\t", tle->resno,
451 tle->resname ? tle->resname : "<null>");
452 if (tle->ressortgroupref != 0)
453 printf("(%u):\t", tle->ressortgroupref);
456 print_expr((Node *) tle->expr, rtable);
464 * print out the tuple with the given TupleTableSlot
467 print_slot(TupleTableSlot *slot)
471 printf("tuple is null.\n");
474 if (!slot->tts_tupleDescriptor)
476 printf("no tuple descriptor.\n");
480 debugtup(slot, NULL);
484 plannode_type(Plan *p)
504 case T_BitmapIndexScan:
505 return "BITMAPINDEXSCAN";
506 case T_BitmapHeapScan:
507 return "BITMAPHEAPSCAN";
511 return "SUBQUERYSCAN";
513 return "FUNCTIONSCAN";
546 * Recursively prints a simple text description of the plan tree
549 print_plan_recursive(Plan *p, Query *parsetree, int indentLevel, char *label)
552 char extraInfo[NAMEDATALEN + 100];
556 for (i = 0; i < indentLevel; i++)
558 printf("%s%s :c=%.2f..%.2f :r=%.0f :w=%d ", label, plannode_type(p),
559 p->startup_cost, p->total_cost,
560 p->plan_rows, p->plan_width);
563 IsA(p, BitmapHeapScan))
567 rte = rt_fetch(((Scan *) p)->scanrelid, parsetree->rtable);
568 StrNCpy(extraInfo, rte->eref->aliasname, NAMEDATALEN);
570 else if (IsA(p, IndexScan))
574 rte = rt_fetch(((IndexScan *) p)->scan.scanrelid, parsetree->rtable);
575 StrNCpy(extraInfo, rte->eref->aliasname, NAMEDATALEN);
577 else if (IsA(p, FunctionScan))
581 rte = rt_fetch(((FunctionScan *) p)->scan.scanrelid, parsetree->rtable);
582 StrNCpy(extraInfo, rte->eref->aliasname, NAMEDATALEN);
584 else if (IsA(p, ValuesScan))
588 rte = rt_fetch(((ValuesScan *) p)->scan.scanrelid, parsetree->rtable);
589 StrNCpy(extraInfo, rte->eref->aliasname, NAMEDATALEN);
593 if (extraInfo[0] != '\0')
594 printf(" ( %s )\n", extraInfo);
597 print_plan_recursive(p->lefttree, parsetree, indentLevel + 3, "l: ");
598 print_plan_recursive(p->righttree, parsetree, indentLevel + 3, "r: ");
603 Append *appendplan = (Append *) p;
605 foreach(l, appendplan->appendplans)
607 Plan *subnode = (Plan *) lfirst(l);
609 print_plan_recursive(subnode, parsetree, indentLevel + 3, "a: ");
613 if (IsA(p, BitmapAnd))
616 BitmapAnd *bitmapandplan = (BitmapAnd *) p;
618 foreach(l, bitmapandplan->bitmapplans)
620 Plan *subnode = (Plan *) lfirst(l);
622 print_plan_recursive(subnode, parsetree, indentLevel + 3, "a: ");
626 if (IsA(p, BitmapOr))
629 BitmapOr *bitmaporplan = (BitmapOr *) p;
631 foreach(l, bitmaporplan->bitmapplans)
633 Plan *subnode = (Plan *) lfirst(l);
635 print_plan_recursive(subnode, parsetree, indentLevel + 3, "a: ");
643 * prints just the plan node types
646 print_plan(Plan *p, Query *parsetree)
648 print_plan_recursive(p, parsetree, 0, "");