1 /*-------------------------------------------------------------------------
4 * various print routines (used mostly for debugging)
6 * Portions Copyright (c) 1996-2011, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * src/backend/nodes/print.c
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"
31 * print contents of Node to stdout
34 print(const void *obj)
39 s = nodeToString(obj);
40 f = format_node_dump(s);
49 * pretty-print contents of Node to stdout
52 pprint(const void *obj)
57 s = nodeToString(obj);
58 f = pretty_format_node_dump(s);
67 * send pretty-printed contents of Node to postmaster log
70 elog_node_display(int lev, const char *title, const void *obj, bool pretty)
75 s = nodeToString(obj);
77 f = pretty_format_node_dump(s);
79 f = format_node_dump(s);
82 (errmsg_internal("%s:", title),
83 errdetail_internal("%s", f)));
88 * Format a nodeToString output for display on a terminal.
90 * The result is a palloc'd string.
92 * This version just tries to break at whitespace.
95 format_node_dump(const char *dump)
98 char line[LINELEN + 1];
104 initStringInfo(&str);
108 for (j = 0; j < LINELEN && dump[i] != '\0'; i++, j++)
114 /* ok to break at adjacent space */
119 for (k = j - 1; k > 0; k--)
124 /* back up; will reprint all after space */
130 appendStringInfo(&str, "%s\n", line);
135 appendStringInfo(&str, "%s\n", line);
142 * Format a nodeToString output for display on a terminal.
144 * The result is a palloc'd string.
146 * This version tries to indent intelligently.
149 pretty_format_node_dump(const char *dump)
154 char line[LINELEN + 1];
161 initStringInfo(&str);
162 indentLev = 0; /* logical indent level */
163 indentDist = 0; /* physical indent distance */
167 for (j = 0; j < indentDist; j++)
169 for (; j < LINELEN && dump[i] != '\0'; i++, j++)
177 /* print data before the } */
179 appendStringInfo(&str, "%s\n", line);
181 /* print the } at indentDist */
182 line[indentDist] = '}';
183 line[indentDist + 1] = '\0';
184 appendStringInfo(&str, "%s\n", line);
189 indentDist = Min(indentLev * INDENTSTOP, MAXINDENT);
192 /* j will equal indentDist on next loop iteration */
193 /* suppress whitespace just after } */
194 while (dump[i + 1] == ' ')
198 /* force line break after ), unless another ) follows */
199 if (dump[i + 1] != ')')
202 appendStringInfo(&str, "%s\n", line);
204 while (dump[i + 1] == ' ')
209 /* force line break before { */
213 appendStringInfo(&str, "%s\n", line);
217 indentDist = Min(indentLev * INDENTSTOP, MAXINDENT);
218 for (j = 0; j < indentDist; j++)
223 /* force line break before : */
227 appendStringInfo(&str, "%s\n", line);
237 appendStringInfo(&str, "%s\n", line);
240 appendStringInfo(&str, "%s\n", line);
249 * print contents of range table
252 print_rt(const List *rtable)
257 printf("resno\trefname \trelid\tinFromCl\n");
258 printf("-----\t---------\t-----\t--------\n");
261 RangeTblEntry *rte = lfirst(l);
263 switch (rte->rtekind)
266 printf("%d\t%s\t%u\t%c",
267 i, rte->eref->aliasname, rte->relid, rte->relkind);
270 printf("%d\t%s\t[subquery]",
271 i, rte->eref->aliasname);
274 printf("%d\t%s\t[join]",
275 i, rte->eref->aliasname);
278 printf("%d\t%s\t[rangefunction]",
279 i, rte->eref->aliasname);
282 printf("%d\t%s\t[values list]",
283 i, rte->eref->aliasname);
286 printf("%d\t%s\t[cte]",
287 i, rte->eref->aliasname);
290 printf("%d\t%s\t[unknown rtekind]",
291 i, rte->eref->aliasname);
295 (rte->inh ? "inh" : ""),
296 (rte->inFromCl ? "inFromCl" : ""));
304 * print an expression
307 print_expr(const Node *expr, const List *rtable)
317 const Var *var = (const Var *) expr;
339 Assert(var->varno > 0 &&
340 (int) var->varno <= list_length(rtable));
341 rte = rt_fetch(var->varno, rtable);
342 relname = rte->eref->aliasname;
343 attname = get_rte_attribute_name(rte, var->varattno);
347 printf("%s.%s", relname, attname);
349 else if (IsA(expr, Const))
351 const Const *c = (const Const *) expr;
362 getTypeOutputInfo(c->consttype,
363 &typoutput, &typIsVarlena);
365 outputstr = OidOutputFunctionCall(typoutput, c->constvalue);
366 printf("%s", outputstr);
369 else if (IsA(expr, OpExpr))
371 const OpExpr *e = (const OpExpr *) expr;
374 opname = get_opname(e->opno);
375 if (list_length(e->args) > 1)
377 print_expr(get_leftop((const Expr *) e), rtable);
378 printf(" %s ", ((opname != NULL) ? opname : "(invalid operator)"));
379 print_expr(get_rightop((const Expr *) e), rtable);
383 /* we print prefix and postfix ops the same... */
384 printf("%s ", ((opname != NULL) ? opname : "(invalid operator)"));
385 print_expr(get_leftop((const Expr *) e), rtable);
388 else if (IsA(expr, FuncExpr))
390 const FuncExpr *e = (const FuncExpr *) expr;
394 funcname = get_func_name(e->funcid);
395 printf("%s(", ((funcname != NULL) ? funcname : "(invalid function)"));
398 print_expr(lfirst(l), rtable);
405 printf("unknown expr");
410 * pathkeys list of PathKeys
413 print_pathkeys(const List *pathkeys, const List *rtable)
420 PathKey *pathkey = (PathKey *) lfirst(i);
421 EquivalenceClass *eclass;
425 eclass = pathkey->pk_eclass;
426 /* chase up, in case pathkey is non-canonical */
427 while (eclass->ec_merged)
428 eclass = eclass->ec_merged;
431 foreach(k, eclass->ec_members)
433 EquivalenceMember *mem = (EquivalenceMember *) lfirst(k);
439 print_expr((Node *) mem->em_expr, rtable);
450 * print targetlist in a more legible way.
453 print_tl(const List *tlist, const List *rtable)
460 TargetEntry *tle = (TargetEntry *) lfirst(tl);
462 printf("\t%d %s\t", tle->resno,
463 tle->resname ? tle->resname : "<null>");
464 if (tle->ressortgroupref != 0)
465 printf("(%u):\t", tle->ressortgroupref);
468 print_expr((Node *) tle->expr, rtable);
476 * print out the tuple with the given TupleTableSlot
479 print_slot(TupleTableSlot *slot)
483 printf("tuple is null.\n");
486 if (!slot->tts_tupleDescriptor)
488 printf("no tuple descriptor.\n");
492 debugtup(slot, NULL);