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 "lib/stringinfo.h"
24 #include "nodes/print.h"
25 #include "optimizer/clauses.h"
26 #include "parser/parsetree.h"
27 #include "utils/lsyscache.h"
28 #include "utils/rel.h"
33 * print contents of Node to stdout
41 s = nodeToString(obj);
42 f = format_node_dump(s);
51 * pretty-print contents of Node to stdout
59 s = nodeToString(obj);
60 f = pretty_format_node_dump(s);
69 * send pretty-printed contents of Node to postmaster log
72 elog_node_display(int lev, const char *title, void *obj, bool pretty)
77 s = nodeToString(obj);
79 f = pretty_format_node_dump(s);
81 f = format_node_dump(s);
84 (errmsg_internal("%s:", title),
90 * Format a nodeToString output for display on a terminal.
92 * The result is a palloc'd string.
94 * This version just tries to break at whitespace.
97 format_node_dump(const char *dump)
100 char line[LINELEN + 1];
106 initStringInfo(&str);
110 for (j = 0; j < LINELEN && dump[i] != '\0'; i++, j++)
116 /* ok to break at adjacent space */
121 for (k = j - 1; k > 0; k--)
126 /* back up; will reprint all after space */
132 appendStringInfo(&str, "%s\n", line);
137 appendStringInfo(&str, "%s\n", line);
144 * Format a nodeToString output for display on a terminal.
146 * The result is a palloc'd string.
148 * This version tries to indent intelligently.
151 pretty_format_node_dump(const char *dump)
156 char line[LINELEN + 1];
163 initStringInfo(&str);
164 indentLev = 0; /* logical indent level */
165 indentDist = 0; /* physical indent distance */
169 for (j = 0; j < indentDist; j++)
171 for (; j < LINELEN && dump[i] != '\0'; i++, j++)
179 /* print data before the } */
181 appendStringInfo(&str, "%s\n", line);
183 /* print the } at indentDist */
184 line[indentDist] = '}';
185 line[indentDist + 1] = '\0';
186 appendStringInfo(&str, "%s\n", line);
191 indentDist = Min(indentLev * INDENTSTOP, MAXINDENT);
194 /* j will equal indentDist on next loop iteration */
195 /* suppress whitespace just after } */
196 while (dump[i + 1] == ' ')
200 /* force line break after ), unless another ) follows */
201 if (dump[i + 1] != ')')
204 appendStringInfo(&str, "%s\n", line);
206 while (dump[i + 1] == ' ')
211 /* force line break before { */
215 appendStringInfo(&str, "%s\n", line);
219 indentDist = Min(indentLev * INDENTSTOP, MAXINDENT);
220 for (j = 0; j < indentDist; j++)
225 /* force line break before : */
229 appendStringInfo(&str, "%s\n", line);
239 appendStringInfo(&str, "%s\n", line);
242 appendStringInfo(&str, "%s\n", line);
251 * print contents of range table
254 print_rt(List *rtable)
259 printf("resno\trefname \trelid\tinFromCl\n");
260 printf("-----\t---------\t-----\t--------\n");
263 RangeTblEntry *rte = lfirst(l);
265 switch (rte->rtekind)
269 i, rte->eref->aliasname, rte->relid);
272 printf("%d\t%s\t[subquery]",
273 i, rte->eref->aliasname);
276 printf("%d\t%s\t[join]",
277 i, rte->eref->aliasname);
280 printf("%d\t%s\t[special]",
281 i, rte->eref->aliasname);
284 printf("%d\t%s\t[rangefunction]",
285 i, rte->eref->aliasname);
288 printf("%d\t%s\t[values list]",
289 i, rte->eref->aliasname);
292 printf("%d\t%s\t[cte]",
293 i, rte->eref->aliasname);
296 printf("%d\t%s\t[unknown rtekind]",
297 i, rte->eref->aliasname);
301 (rte->inh ? "inh" : ""),
302 (rte->inFromCl ? "inFromCl" : ""));
310 * print an expression
313 print_expr(Node *expr, List *rtable)
323 Var *var = (Var *) expr;
341 Assert(var->varno > 0 &&
342 (int) var->varno <= list_length(rtable));
343 rte = rt_fetch(var->varno, rtable);
344 relname = rte->eref->aliasname;
345 attname = get_rte_attribute_name(rte, var->varattno);
349 printf("%s.%s", relname, attname);
351 else if (IsA(expr, Const))
353 Const *c = (Const *) expr;
364 getTypeOutputInfo(c->consttype,
365 &typoutput, &typIsVarlena);
367 outputstr = OidOutputFunctionCall(typoutput, c->constvalue);
368 printf("%s", outputstr);
371 else if (IsA(expr, OpExpr))
373 OpExpr *e = (OpExpr *) expr;
376 opname = get_opname(e->opno);
377 if (list_length(e->args) > 1)
379 print_expr(get_leftop((Expr *) e), rtable);
380 printf(" %s ", ((opname != NULL) ? opname : "(invalid operator)"));
381 print_expr(get_rightop((Expr *) e), rtable);
385 /* we print prefix and postfix ops the same... */
386 printf("%s ", ((opname != NULL) ? opname : "(invalid operator)"));
387 print_expr(get_leftop((Expr *) e), rtable);
390 else if (IsA(expr, FuncExpr))
392 FuncExpr *e = (FuncExpr *) expr;
396 funcname = get_func_name(e->funcid);
397 printf("%s(", ((funcname != NULL) ? funcname : "(invalid function)"));
400 print_expr(lfirst(l), rtable);
407 printf("unknown expr");
412 * pathkeys list of PathKeys
415 print_pathkeys(List *pathkeys, List *rtable)
422 PathKey *pathkey = (PathKey *) lfirst(i);
423 EquivalenceClass *eclass;
427 eclass = pathkey->pk_eclass;
428 /* chase up, in case pathkey is non-canonical */
429 while (eclass->ec_merged)
430 eclass = eclass->ec_merged;
433 foreach(k, eclass->ec_members)
435 EquivalenceMember *mem = (EquivalenceMember *) lfirst(k);
441 print_expr((Node *) mem->em_expr, rtable);
452 * print targetlist in a more legible way.
455 print_tl(List *tlist, List *rtable)
462 TargetEntry *tle = (TargetEntry *) lfirst(tl);
464 printf("\t%d %s\t", tle->resno,
465 tle->resname ? tle->resname : "<null>");
466 if (tle->ressortgroupref != 0)
467 printf("(%u):\t", tle->ressortgroupref);
470 print_expr((Node *) tle->expr, rtable);
478 * print out the tuple with the given TupleTableSlot
481 print_slot(TupleTableSlot *slot)
485 printf("tuple is null.\n");
488 if (!slot->tts_tupleDescriptor)
490 printf("no tuple descriptor.\n");
494 debugtup(slot, NULL);