1 /*-------------------------------------------------------------------------
4 * various print routines (used mostly for debugging)
6 * Portions Copyright (c) 1996-2012, 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 "lib/stringinfo.h"
24 #include "nodes/print.h"
25 #include "optimizer/clauses.h"
26 #include "parser/parsetree.h"
27 #include "utils/lsyscache.h"
32 * print contents of Node to stdout
35 print(const void *obj)
40 s = nodeToString(obj);
41 f = format_node_dump(s);
50 * pretty-print contents of Node to stdout
53 pprint(const void *obj)
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, const 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),
84 errdetail_internal("%s", f)));
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(const List *rtable)
258 printf("resno\trefname \trelid\tinFromCl\n");
259 printf("-----\t---------\t-----\t--------\n");
262 RangeTblEntry *rte = lfirst(l);
264 switch (rte->rtekind)
267 printf("%d\t%s\t%u\t%c",
268 i, rte->eref->aliasname, rte->relid, rte->relkind);
271 printf("%d\t%s\t[subquery]",
272 i, rte->eref->aliasname);
275 printf("%d\t%s\t[join]",
276 i, rte->eref->aliasname);
279 printf("%d\t%s\t[rangefunction]",
280 i, rte->eref->aliasname);
283 printf("%d\t%s\t[values list]",
284 i, rte->eref->aliasname);
287 printf("%d\t%s\t[cte]",
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(const Node *expr, const List *rtable)
318 const Var *var = (const Var *) expr;
340 Assert(var->varno > 0 &&
341 (int) var->varno <= list_length(rtable));
342 rte = rt_fetch(var->varno, rtable);
343 relname = rte->eref->aliasname;
344 attname = get_rte_attribute_name(rte, var->varattno);
348 printf("%s.%s", relname, attname);
350 else if (IsA(expr, Const))
352 const Const *c = (const Const *) expr;
363 getTypeOutputInfo(c->consttype,
364 &typoutput, &typIsVarlena);
366 outputstr = OidOutputFunctionCall(typoutput, c->constvalue);
367 printf("%s", outputstr);
370 else if (IsA(expr, OpExpr))
372 const OpExpr *e = (const OpExpr *) expr;
375 opname = get_opname(e->opno);
376 if (list_length(e->args) > 1)
378 print_expr(get_leftop((const Expr *) e), rtable);
379 printf(" %s ", ((opname != NULL) ? opname : "(invalid operator)"));
380 print_expr(get_rightop((const Expr *) e), rtable);
384 /* we print prefix and postfix ops the same... */
385 printf("%s ", ((opname != NULL) ? opname : "(invalid operator)"));
386 print_expr(get_leftop((const Expr *) e), rtable);
389 else if (IsA(expr, FuncExpr))
391 const FuncExpr *e = (const FuncExpr *) expr;
395 funcname = get_func_name(e->funcid);
396 printf("%s(", ((funcname != NULL) ? funcname : "(invalid function)"));
399 print_expr(lfirst(l), rtable);
406 printf("unknown expr");
411 * pathkeys list of PathKeys
414 print_pathkeys(const List *pathkeys, const List *rtable)
421 PathKey *pathkey = (PathKey *) lfirst(i);
422 EquivalenceClass *eclass;
426 eclass = pathkey->pk_eclass;
427 /* chase up, in case pathkey is non-canonical */
428 while (eclass->ec_merged)
429 eclass = eclass->ec_merged;
432 foreach(k, eclass->ec_members)
434 EquivalenceMember *mem = (EquivalenceMember *) lfirst(k);
440 print_expr((Node *) mem->em_expr, rtable);
451 * print targetlist in a more legible way.
454 print_tl(const List *tlist, const List *rtable)
461 TargetEntry *tle = (TargetEntry *) lfirst(tl);
463 printf("\t%d %s\t", tle->resno,
464 tle->resname ? tle->resname : "<null>");
465 if (tle->ressortgroupref != 0)
466 printf("(%u):\t", tle->ressortgroupref);
469 print_expr((Node *) tle->expr, rtable);
477 * print out the tuple with the given TupleTableSlot
480 print_slot(TupleTableSlot *slot)
484 printf("tuple is null.\n");
487 if (!slot->tts_tupleDescriptor)
489 printf("no tuple descriptor.\n");
493 debugtup(slot, NULL);