1 /*-------------------------------------------------------------------------
4 * various print routines (used mostly for debugging)
6 * Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * $Header: /cvsroot/pgsql/src/backend/nodes/print.c,v 1.62 2003/07/22 23:30:37 tgl Exp $
14 * AUTHOR DATE MAJOR EVENT
15 * Andrew Yu Oct 26, 1994 file creation
17 *-------------------------------------------------------------------------
22 #include "access/printtup.h"
23 #include "catalog/pg_type.h"
24 #include "lib/stringinfo.h"
25 #include "nodes/print.h"
26 #include "optimizer/clauses.h"
27 #include "parser/parsetree.h"
28 #include "utils/lsyscache.h"
29 #include "utils/syscache.h"
31 static char *plannode_type(Plan *p);
35 * print contents of Node to stdout
43 s = nodeToString(obj);
44 f = format_node_dump(s);
53 * pretty-print contents of Node to stdout
61 s = nodeToString(obj);
62 f = pretty_format_node_dump(s);
71 * send pretty-printed contents of Node to postmaster log
74 elog_node_display(int lev, const char *title, void *obj, bool pretty)
79 s = nodeToString(obj);
81 f = pretty_format_node_dump(s);
83 f = format_node_dump(s);
86 (errmsg_internal("%s:", title),
92 * Format a nodeToString output for display on a terminal.
94 * The result is a palloc'd string.
96 * This version just tries to break at whitespace.
99 format_node_dump(const char *dump)
102 char line[LINELEN + 1];
108 initStringInfo(&str);
112 for (j = 0; j < LINELEN && dump[i] != '\0'; i++, j++)
118 /* ok to break at adjacent space */
123 for (k = j - 1; k > 0; k--)
128 /* back up; will reprint all after space */
134 appendStringInfo(&str, "%s\n", line);
139 appendStringInfo(&str, "%s\n", line);
146 * Format a nodeToString output for display on a terminal.
148 * The result is a palloc'd string.
150 * This version tries to indent intelligently.
153 pretty_format_node_dump(const char *dump)
158 char line[LINELEN + 1];
165 initStringInfo(&str);
166 indentLev = 0; /* logical indent level */
167 indentDist = 0; /* physical indent distance */
171 for (j = 0; j < indentDist; j++)
173 for (; j < LINELEN && dump[i] != '\0'; i++, j++)
181 /* print data before the } */
183 appendStringInfo(&str, "%s\n", line);
185 /* print the } at indentDist */
186 line[indentDist] = '}';
187 line[indentDist + 1] = '\0';
188 appendStringInfo(&str, "%s\n", line);
193 indentDist = Min(indentLev * INDENTSTOP, MAXINDENT);
196 /* j will equal indentDist on next loop iteration */
199 /* force line break after ')' */
201 appendStringInfo(&str, "%s\n", line);
205 /* force line break before { */
209 appendStringInfo(&str, "%s\n", line);
213 indentDist = Min(indentLev * INDENTSTOP, MAXINDENT);
214 for (j = 0; j < indentDist; j++)
219 /* force line break before : */
223 appendStringInfo(&str, "%s\n", line);
233 appendStringInfo(&str, "%s\n", line);
236 appendStringInfo(&str, "%s\n", line);
245 * print contents of range table
248 print_rt(List *rtable)
253 printf("resno\trefname \trelid\tinFromCl\n");
254 printf("-----\t---------\t-----\t--------\n");
257 RangeTblEntry *rte = lfirst(l);
259 switch (rte->rtekind)
263 i, rte->eref->aliasname, rte->relid);
266 printf("%d\t%s\t[subquery]",
267 i, rte->eref->aliasname);
270 printf("%d\t%s\t[rangefunction]",
271 i, rte->eref->aliasname);
274 printf("%d\t%s\t[join]",
275 i, rte->eref->aliasname);
278 printf("%d\t%s\t[special]",
279 i, rte->eref->aliasname);
282 printf("%d\t%s\t[unknown rtekind]",
283 i, rte->eref->aliasname);
287 (rte->inh ? "inh" : ""),
288 (rte->inFromCl ? "inFromCl" : ""));
296 * print an expression
299 print_expr(Node *expr, List *rtable)
309 Var *var = (Var *) expr;
327 Assert(var->varno > 0 &&
328 (int) var->varno <= length(rtable));
329 rte = rt_fetch(var->varno, rtable);
330 relname = rte->eref->aliasname;
331 attname = get_rte_attribute_name(rte, var->varattno);
335 printf("%s.%s", relname, attname);
337 else if (IsA(expr, Const))
339 Const *c = (Const *) expr;
351 typeTup = SearchSysCache(TYPEOID,
352 ObjectIdGetDatum(c->consttype),
354 if (!HeapTupleIsValid(typeTup))
355 elog(ERROR, "cache lookup failed for type %u", c->consttype);
356 typoutput = ((Form_pg_type) GETSTRUCT(typeTup))->typoutput;
357 typelem = ((Form_pg_type) GETSTRUCT(typeTup))->typelem;
358 ReleaseSysCache(typeTup);
360 outputstr = DatumGetCString(OidFunctionCall3(typoutput,
362 ObjectIdGetDatum(typelem),
364 printf("%s", outputstr);
367 else if (IsA(expr, OpExpr))
369 OpExpr *e = (OpExpr *) expr;
372 opname = get_opname(e->opno);
373 if (length(e->args) > 1)
375 print_expr(get_leftop((Expr *) e), rtable);
376 printf(" %s ", ((opname != NULL) ? opname : "(invalid operator)"));
377 print_expr(get_rightop((Expr *) e), rtable);
381 /* we print prefix and postfix ops the same... */
382 printf("%s ", ((opname != NULL) ? opname : "(invalid operator)"));
383 print_expr(get_leftop((Expr *) e), rtable);
386 else if (IsA(expr, FuncExpr))
388 FuncExpr *e = (FuncExpr *) expr;
392 funcname = get_func_name(e->funcid);
393 printf("%s(", ((funcname != NULL) ? funcname : "(invalid function)"));
396 print_expr(lfirst(l), rtable);
403 printf("unknown expr");
408 * pathkeys list of list of PathKeyItems
411 print_pathkeys(List *pathkeys, List *rtable)
419 List *pathkey = lfirst(i);
424 PathKeyItem *item = lfirst(k);
426 print_expr(item->key, rtable);
439 * print targetlist in a more legible way.
442 print_tl(List *tlist, List *rtable)
449 TargetEntry *tle = lfirst(tl);
451 printf("\t%d %s\t", tle->resdom->resno, tle->resdom->resname);
452 if (tle->resdom->ressortgroupref != 0)
453 printf("(%u):\t", tle->resdom->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->ttc_tupleDescriptor)
476 printf("no tuple descriptor.\n");
480 debugtup(slot->val, slot->ttc_tupleDescriptor, NULL);
484 plannode_type(Plan *p)
503 return "SUBQUERYSCAN";
505 return "FUNCTIONSCAN";
536 * Recursively prints a simple text description of the plan tree
539 print_plan_recursive(Plan *p, Query *parsetree, int indentLevel, char *label)
542 char extraInfo[NAMEDATALEN + 100];
546 for (i = 0; i < indentLevel; i++)
548 printf("%s%s :c=%.2f..%.2f :r=%.0f :w=%d ", label, plannode_type(p),
549 p->startup_cost, p->total_cost,
550 p->plan_rows, p->plan_width);
556 rte = rt_fetch(((Scan *) p)->scanrelid, parsetree->rtable);
557 StrNCpy(extraInfo, rte->eref->aliasname, NAMEDATALEN);
559 else if (IsA(p, IndexScan))
563 rte = rt_fetch(((IndexScan *) p)->scan.scanrelid, parsetree->rtable);
564 StrNCpy(extraInfo, rte->eref->aliasname, NAMEDATALEN);
566 else if (IsA(p, FunctionScan))
570 rte = rt_fetch(((FunctionScan *) p)->scan.scanrelid, parsetree->rtable);
571 StrNCpy(extraInfo, rte->eref->aliasname, NAMEDATALEN);
575 if (extraInfo[0] != '\0')
576 printf(" ( %s )\n", extraInfo);
579 print_plan_recursive(p->lefttree, parsetree, indentLevel + 3, "l: ");
580 print_plan_recursive(p->righttree, parsetree, indentLevel + 3, "r: ");
586 Append *appendplan = (Append *) p;
588 foreach(lst, appendplan->appendplans)
590 Plan *subnode = (Plan *) lfirst(lst);
593 * I don't think we need to fiddle with the range table here,
596 print_plan_recursive(subnode, parsetree, indentLevel + 3, "a: ");
604 prints just the plan node types */
607 print_plan(Plan *p, Query *parsetree)
609 print_plan_recursive(p, parsetree, 0, "");