1 /*-------------------------------------------------------------------------
4 * various print routines (used mostly for debugging)
6 * Portions Copyright (c) 1996-2017, 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[table function]",
284 i, rte->eref->aliasname);
287 printf("%d\t%s\t[values list]",
288 i, rte->eref->aliasname);
291 printf("%d\t%s\t[cte]",
292 i, rte->eref->aliasname);
294 case RTE_NAMEDTUPLESTORE:
295 printf("%d\t%s\t[tuplestore]",
296 i, rte->eref->aliasname);
299 printf("%d\t%s\t[unknown rtekind]",
300 i, rte->eref->aliasname);
304 (rte->inh ? "inh" : ""),
305 (rte->inFromCl ? "inFromCl" : ""));
313 * print an expression
316 print_expr(const Node *expr, const List *rtable)
326 const Var *var = (const Var *) expr;
348 Assert(var->varno > 0 &&
349 (int) var->varno <= list_length(rtable));
350 rte = rt_fetch(var->varno, rtable);
351 relname = rte->eref->aliasname;
352 attname = get_rte_attribute_name(rte, var->varattno);
356 printf("%s.%s", relname, attname);
358 else if (IsA(expr, Const))
360 const Const *c = (const Const *) expr;
371 getTypeOutputInfo(c->consttype,
372 &typoutput, &typIsVarlena);
374 outputstr = OidOutputFunctionCall(typoutput, c->constvalue);
375 printf("%s", outputstr);
378 else if (IsA(expr, OpExpr))
380 const OpExpr *e = (const OpExpr *) expr;
383 opname = get_opname(e->opno);
384 if (list_length(e->args) > 1)
386 print_expr(get_leftop((const Expr *) e), rtable);
387 printf(" %s ", ((opname != NULL) ? opname : "(invalid operator)"));
388 print_expr(get_rightop((const Expr *) e), rtable);
392 /* we print prefix and postfix ops the same... */
393 printf("%s ", ((opname != NULL) ? opname : "(invalid operator)"));
394 print_expr(get_leftop((const Expr *) e), rtable);
397 else if (IsA(expr, FuncExpr))
399 const FuncExpr *e = (const FuncExpr *) expr;
403 funcname = get_func_name(e->funcid);
404 printf("%s(", ((funcname != NULL) ? funcname : "(invalid function)"));
407 print_expr(lfirst(l), rtable);
414 printf("unknown expr");
419 * pathkeys list of PathKeys
422 print_pathkeys(const List *pathkeys, const List *rtable)
429 PathKey *pathkey = (PathKey *) lfirst(i);
430 EquivalenceClass *eclass;
434 eclass = pathkey->pk_eclass;
435 /* chase up, in case pathkey is non-canonical */
436 while (eclass->ec_merged)
437 eclass = eclass->ec_merged;
440 foreach(k, eclass->ec_members)
442 EquivalenceMember *mem = (EquivalenceMember *) lfirst(k);
448 print_expr((Node *) mem->em_expr, rtable);
459 * print targetlist in a more legible way.
462 print_tl(const List *tlist, const List *rtable)
469 TargetEntry *tle = (TargetEntry *) lfirst(tl);
471 printf("\t%d %s\t", tle->resno,
472 tle->resname ? tle->resname : "<null>");
473 if (tle->ressortgroupref != 0)
474 printf("(%u):\t", tle->ressortgroupref);
477 print_expr((Node *) tle->expr, rtable);
485 * print out the tuple with the given TupleTableSlot
488 print_slot(TupleTableSlot *slot)
492 printf("tuple is null.\n");
495 if (!slot->tts_tupleDescriptor)
497 printf("no tuple descriptor.\n");
501 debugtup(slot, NULL);