1 /*-------------------------------------------------------------------------
4 * various print routines (used mostly for debugging)
6 * Portions Copyright (c) 1996-2001, 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.48 2001/10/18 16:11:41 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 "nodes/print.h"
25 #include "optimizer/clauses.h"
26 #include "parser/parsetree.h"
27 #include "utils/lsyscache.h"
28 #include "utils/syscache.h"
30 static char *plannode_type(Plan *p);
34 * print contents of Node to stdout
41 s = nodeToString(obj);
47 * pretty print hack extraordinaire. -ay 10/94
58 s = nodeToString(obj);
64 for (j = 0; j < indentLev * 3; j++)
66 for (; j < 75 && s[i] != '\0'; i++, j++)
72 if (j != indentLev * 3)
76 line[indentLev * 3] = '\0';
77 printf("%s}\n", line);
82 printf("%s}\n", line);
85 j = indentLev * 3 - 1; /* print the line before :
91 j = indentLev * 3 - 1;
95 /* !!! FALLS THROUGH */
100 printf("%s\n", line);
101 /* print the line before : and resets */
102 for (j = 0; j < indentLev * 3; j++)
112 printf("%s\n", line);
115 printf("%s\n", line);
122 * print contents of range table
125 print_rt(List *rtable)
130 printf("resno\trelname(refname)\trelid\tinFromCl\n");
131 printf("-----\t----------------\t-----\t--------\n");
134 RangeTblEntry *rte = lfirst(l);
137 printf("%d\t%s (%s)\t%u",
138 i, rte->relname, rte->eref->relname, rte->relid);
140 printf("%d\t[subquery] (%s)\t",
141 i, rte->eref->relname);
143 (rte->inh ? "inh" : ""),
144 (rte->inFromCl ? "inFromCl" : ""));
152 * print an expression
155 print_expr(Node *expr, List *rtable)
165 Var *var = (Var *) expr;
183 Assert(var->varno > 0 &&
184 (int) var->varno <= length(rtable));
185 rte = rt_fetch(var->varno, rtable);
186 relname = rte->eref->relname;
187 attname = get_rte_attribute_name(rte, var->varattno);
191 printf("%s.%s", relname, attname);
193 else if (IsA(expr, Const))
195 Const *c = (Const *) expr;
207 typeTup = SearchSysCache(TYPEOID,
208 ObjectIdGetDatum(c->consttype),
210 if (!HeapTupleIsValid(typeTup))
211 elog(ERROR, "Cache lookup for type %u failed", c->consttype);
212 typoutput = ((Form_pg_type) GETSTRUCT(typeTup))->typoutput;
213 typelem = ((Form_pg_type) GETSTRUCT(typeTup))->typelem;
214 ReleaseSysCache(typeTup);
216 outputstr = DatumGetCString(OidFunctionCall3(typoutput,
218 ObjectIdGetDatum(typelem),
220 printf("%s", outputstr);
223 else if (IsA(expr, Expr))
225 Expr *e = (Expr *) expr;
227 if (is_opclause(expr))
231 print_expr((Node *) get_leftop(e), rtable);
232 opname = get_opname(((Oper *) e->oper)->opno);
233 printf(" %s ", ((opname != NULL) ? opname : "(invalid operator)"));
234 print_expr((Node *) get_rightop(e), rtable);
240 printf("not an expr");
245 * pathkeys list of list of PathKeyItems
248 print_pathkeys(List *pathkeys, List *rtable)
256 List *pathkey = lfirst(i);
261 PathKeyItem *item = lfirst(k);
263 print_expr(item->key, rtable);
276 * print targetlist in a more legible way.
279 print_tl(List *tlist, List *rtable)
286 TargetEntry *tle = lfirst(tl);
288 printf("\t%d %s\t", tle->resdom->resno, tle->resdom->resname);
289 if (tle->resdom->reskey != 0)
290 printf("(%d):\t", tle->resdom->reskey);
293 print_expr(tle->expr, rtable);
301 * print out the tuple with the given TupleTableSlot
304 print_slot(TupleTableSlot *slot)
308 printf("tuple is null.\n");
311 if (!slot->ttc_tupleDescriptor)
313 printf("no tuple descriptor.\n");
317 debugtup(slot->val, slot->ttc_tupleDescriptor, NULL);
321 plannode_type(Plan *p)
340 return "SUBQUERYSCAN";
371 prints the ascii description of the plan nodes
372 does this recursively by doing a depth-first traversal of the
373 plan tree. for SeqScan and IndexScan, the name of the table is also
378 print_plan_recursive(Plan *p, Query *parsetree, int indentLevel, char *label)
385 for (i = 0; i < indentLevel; i++)
387 printf("%s%s :c=%.2f..%.2f :r=%.0f :w=%d ", label, plannode_type(p),
388 p->startup_cost, p->total_cost,
389 p->plan_rows, p->plan_width);
390 if (IsA(p, Scan) ||IsA(p, SeqScan))
394 rte = rt_fetch(((Scan *) p)->scanrelid, parsetree->rtable);
395 StrNCpy(extraInfo, rte->relname, NAMEDATALEN);
397 else if (IsA(p, IndexScan))
401 rte = rt_fetch(((IndexScan *) p)->scan.scanrelid, parsetree->rtable);
402 StrNCpy(extraInfo, rte->relname, NAMEDATALEN);
406 if (extraInfo[0] != '\0')
407 printf(" ( %s )\n", extraInfo);
410 print_plan_recursive(p->lefttree, parsetree, indentLevel + 3, "l: ");
411 print_plan_recursive(p->righttree, parsetree, indentLevel + 3, "r: ");
417 Append *appendplan = (Append *) p;
419 foreach(lst, appendplan->appendplans)
421 Plan *subnode = (Plan *) lfirst(lst);
424 * I don't think we need to fiddle with the range table here,
427 print_plan_recursive(subnode, parsetree, indentLevel + 3, "a: ");
435 prints just the plan node types */
438 print_plan(Plan *p, Query *parsetree)
440 print_plan_recursive(p, parsetree, 0, "");