]> granicus.if.org Git - postgresql/blob - src/backend/nodes/print.c
Error message editing in backend/bootstrap, /lib, /nodes, /port.
[postgresql] / src / backend / nodes / print.c
1 /*-------------------------------------------------------------------------
2  *
3  * print.c
4  *        various print routines (used mostly for debugging)
5  *
6  * Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  *
10  * IDENTIFICATION
11  *        $Header: /cvsroot/pgsql/src/backend/nodes/print.c,v 1.62 2003/07/22 23:30:37 tgl Exp $
12  *
13  * HISTORY
14  *        AUTHOR                        DATE                    MAJOR EVENT
15  *        Andrew Yu                     Oct 26, 1994    file creation
16  *
17  *-------------------------------------------------------------------------
18  */
19
20 #include "postgres.h"
21
22 #include "access/printtup.h"
23 #include "catalog/pg_type.h"
24 #include "lib/stringinfo.h"
25 #include "nodes/print.h"
26 #include "optimizer/clauses.h"
27 #include "parser/parsetree.h"
28 #include "utils/lsyscache.h"
29 #include "utils/syscache.h"
30
31 static char *plannode_type(Plan *p);
32
33 /*
34  * print
35  *        print contents of Node to stdout
36  */
37 void
38 print(void *obj)
39 {
40         char       *s;
41         char       *f;
42
43         s = nodeToString(obj);
44         f = format_node_dump(s);
45         pfree(s);
46         printf("%s\n", f);
47         fflush(stdout);
48         pfree(f);
49 }
50
51 /*
52  * pprint
53  *        pretty-print contents of Node to stdout
54  */
55 void
56 pprint(void *obj)
57 {
58         char       *s;
59         char       *f;
60
61         s = nodeToString(obj);
62         f = pretty_format_node_dump(s);
63         pfree(s);
64         printf("%s\n", f);
65         fflush(stdout);
66         pfree(f);
67 }
68
69 /*
70  * elog_node_display
71  *        send pretty-printed contents of Node to postmaster log
72  */
73 void
74 elog_node_display(int lev, const char *title, void *obj, bool pretty)
75 {
76         char       *s;
77         char       *f;
78
79         s = nodeToString(obj);
80         if (pretty)
81                 f = pretty_format_node_dump(s);
82         else
83                 f = format_node_dump(s);
84         pfree(s);
85         ereport(lev,
86                         (errmsg_internal("%s:", title),
87                          errdetail("%s", f)));
88         pfree(f);
89 }
90
91 /*
92  * Format a nodeToString output for display on a terminal.
93  *
94  * The result is a palloc'd string.
95  *
96  * This version just tries to break at whitespace.
97  */
98 char *
99 format_node_dump(const char *dump)
100 {
101 #define LINELEN         78
102         char            line[LINELEN + 1];
103         StringInfoData str;
104         int                     i;
105         int                     j;
106         int                     k;
107
108         initStringInfo(&str);
109         i = 0;
110         for (;;)
111         {
112                 for (j = 0; j < LINELEN && dump[i] != '\0'; i++, j++)
113                         line[j] = dump[i];
114                 if (dump[i] == '\0')
115                         break;
116                 if (dump[i] == ' ')
117                 {
118                         /* ok to break at adjacent space */
119                         i++;
120                 }
121                 else
122                 {
123                         for (k = j - 1; k > 0; k--)
124                                 if (line[k] == ' ')
125                                         break;
126                         if (k > 0)
127                         {
128                                 /* back up; will reprint all after space */
129                                 i -= (j - k - 1);
130                                 j = k;
131                         }
132                 }
133                 line[j] = '\0';
134                 appendStringInfo(&str, "%s\n", line);
135         }
136         if (j > 0)
137         {
138                 line[j] = '\0';
139                 appendStringInfo(&str, "%s\n", line);
140         }
141         return str.data;
142 #undef LINELEN
143 }
144
145 /*
146  * Format a nodeToString output for display on a terminal.
147  *
148  * The result is a palloc'd string.
149  *
150  * This version tries to indent intelligently.
151  */
152 char *
153 pretty_format_node_dump(const char *dump)
154 {
155 #define INDENTSTOP      3
156 #define MAXINDENT       60
157 #define LINELEN         78
158         char            line[LINELEN + 1];
159         StringInfoData str;
160         int                     indentLev;
161         int                     indentDist;
162         int                     i;
163         int                     j;
164
165         initStringInfo(&str);
166         indentLev = 0;                          /* logical indent level */
167         indentDist = 0;                         /* physical indent distance */
168         i = 0;
169         for (;;)
170         {
171                 for (j = 0; j < indentDist; j++)
172                         line[j] = ' ';
173                 for (; j < LINELEN && dump[i] != '\0'; i++, j++)
174                 {
175                         line[j] = dump[i];
176                         switch (line[j])
177                         {
178                                 case '}':
179                                         if (j != indentDist)
180                                         {
181                                                 /* print data before the } */
182                                                 line[j] = '\0';
183                                                 appendStringInfo(&str, "%s\n", line);
184                                         }
185                                         /* print the } at indentDist */
186                                         line[indentDist] = '}';
187                                         line[indentDist + 1] = '\0';
188                                         appendStringInfo(&str, "%s\n", line);
189                                         /* outdent */
190                                         if (indentLev > 0)
191                                         {
192                                                 indentLev--;
193                                                 indentDist = Min(indentLev * INDENTSTOP, MAXINDENT);
194                                         }
195                                         j = indentDist - 1;
196                                         /* j will equal indentDist on next loop iteration */
197                                         break;
198                                 case ')':
199                                         /* force line break after ')' */
200                                         line[j + 1] = '\0';
201                                         appendStringInfo(&str, "%s\n", line);
202                                         j = indentDist - 1;
203                                         break;
204                                 case '{':
205                                         /* force line break before { */
206                                         if (j != indentDist)
207                                         {
208                                                 line[j] = '\0';
209                                                 appendStringInfo(&str, "%s\n", line);
210                                         }
211                                         /* indent */
212                                         indentLev++;
213                                         indentDist = Min(indentLev * INDENTSTOP, MAXINDENT);
214                                         for (j = 0; j < indentDist; j++)
215                                                 line[j] = ' ';
216                                         line[j] = dump[i];
217                                         break;
218                                 case ':':
219                                         /* force line break before : */
220                                         if (j != indentDist)
221                                         {
222                                                 line[j] = '\0';
223                                                 appendStringInfo(&str, "%s\n", line);
224                                         }
225                                         j = indentDist;
226                                         line[j] = dump[i];
227                                         break;
228                         }
229                 }
230                 line[j] = '\0';
231                 if (dump[i] == '\0')
232                         break;
233                 appendStringInfo(&str, "%s\n", line);
234         }
235         if (j > 0)
236                 appendStringInfo(&str, "%s\n", line);
237         return str.data;
238 #undef INDENTSTOP
239 #undef MAXINDENT
240 #undef LINELEN
241 }
242
243 /*
244  * print_rt
245  *        print contents of range table
246  */
247 void
248 print_rt(List *rtable)
249 {
250         List       *l;
251         int                     i = 1;
252
253         printf("resno\trefname  \trelid\tinFromCl\n");
254         printf("-----\t---------\t-----\t--------\n");
255         foreach(l, rtable)
256         {
257                 RangeTblEntry *rte = lfirst(l);
258
259                 switch (rte->rtekind)
260                 {
261                         case RTE_RELATION:
262                                 printf("%d\t%s\t%u",
263                                            i, rte->eref->aliasname, rte->relid);
264                                 break;
265                         case RTE_SUBQUERY:
266                                 printf("%d\t%s\t[subquery]",
267                                            i, rte->eref->aliasname);
268                                 break;
269                         case RTE_FUNCTION:
270                                 printf("%d\t%s\t[rangefunction]",
271                                            i, rte->eref->aliasname);
272                                 break;
273                         case RTE_JOIN:
274                                 printf("%d\t%s\t[join]",
275                                            i, rte->eref->aliasname);
276                                 break;
277                         case RTE_SPECIAL:
278                                 printf("%d\t%s\t[special]",
279                                            i, rte->eref->aliasname);
280                                 break;
281                         default:
282                                 printf("%d\t%s\t[unknown rtekind]",
283                                            i, rte->eref->aliasname);
284                 }
285
286                 printf("\t%s\t%s\n",
287                            (rte->inh ? "inh" : ""),
288                            (rte->inFromCl ? "inFromCl" : ""));
289                 i++;
290         }
291 }
292
293
294 /*
295  * print_expr
296  *        print an expression
297  */
298 void
299 print_expr(Node *expr, List *rtable)
300 {
301         if (expr == NULL)
302         {
303                 printf("<>");
304                 return;
305         }
306
307         if (IsA(expr, Var))
308         {
309                 Var                *var = (Var *) expr;
310                 char       *relname,
311                                    *attname;
312
313                 switch (var->varno)
314                 {
315                         case INNER:
316                                 relname = "INNER";
317                                 attname = "?";
318                                 break;
319                         case OUTER:
320                                 relname = "OUTER";
321                                 attname = "?";
322                                 break;
323                         default:
324                                 {
325                                         RangeTblEntry *rte;
326
327                                         Assert(var->varno > 0 &&
328                                                    (int) var->varno <= length(rtable));
329                                         rte = rt_fetch(var->varno, rtable);
330                                         relname = rte->eref->aliasname;
331                                         attname = get_rte_attribute_name(rte, var->varattno);
332                                 }
333                                 break;
334                 }
335                 printf("%s.%s", relname, attname);
336         }
337         else if (IsA(expr, Const))
338         {
339                 Const      *c = (Const *) expr;
340                 HeapTuple       typeTup;
341                 Oid                     typoutput;
342                 Oid                     typelem;
343                 char       *outputstr;
344
345                 if (c->constisnull)
346                 {
347                         printf("NULL");
348                         return;
349                 }
350
351                 typeTup = SearchSysCache(TYPEOID,
352                                                                  ObjectIdGetDatum(c->consttype),
353                                                                  0, 0, 0);
354                 if (!HeapTupleIsValid(typeTup))
355                         elog(ERROR, "cache lookup failed for type %u", c->consttype);
356                 typoutput = ((Form_pg_type) GETSTRUCT(typeTup))->typoutput;
357                 typelem = ((Form_pg_type) GETSTRUCT(typeTup))->typelem;
358                 ReleaseSysCache(typeTup);
359
360                 outputstr = DatumGetCString(OidFunctionCall3(typoutput,
361                                                                                                          c->constvalue,
362                                                                                            ObjectIdGetDatum(typelem),
363                                                                                                          Int32GetDatum(-1)));
364                 printf("%s", outputstr);
365                 pfree(outputstr);
366         }
367         else if (IsA(expr, OpExpr))
368         {
369                 OpExpr     *e = (OpExpr *) expr;
370                 char       *opname;
371
372                 opname = get_opname(e->opno);
373                 if (length(e->args) > 1)
374                 {
375                         print_expr(get_leftop((Expr *) e), rtable);
376                         printf(" %s ", ((opname != NULL) ? opname : "(invalid operator)"));
377                         print_expr(get_rightop((Expr *) e), rtable);
378                 }
379                 else
380                 {
381                         /* we print prefix and postfix ops the same... */
382                         printf("%s ", ((opname != NULL) ? opname : "(invalid operator)"));
383                         print_expr(get_leftop((Expr *) e), rtable);
384                 }
385         }
386         else if (IsA(expr, FuncExpr))
387         {
388                 FuncExpr   *e = (FuncExpr *) expr;
389                 char       *funcname;
390                 List       *l;
391
392                 funcname = get_func_name(e->funcid);
393                 printf("%s(", ((funcname != NULL) ? funcname : "(invalid function)"));
394                 foreach(l, e->args)
395                 {
396                         print_expr(lfirst(l), rtable);
397                         if (lnext(l))
398                                 printf(",");
399                 }
400                 printf(")");
401         }
402         else
403                 printf("unknown expr");
404 }
405
406 /*
407  * print_pathkeys -
408  *        pathkeys list of list of PathKeyItems
409  */
410 void
411 print_pathkeys(List *pathkeys, List *rtable)
412 {
413         List       *i,
414                            *k;
415
416         printf("(");
417         foreach(i, pathkeys)
418         {
419                 List       *pathkey = lfirst(i);
420
421                 printf("(");
422                 foreach(k, pathkey)
423                 {
424                         PathKeyItem *item = lfirst(k);
425
426                         print_expr(item->key, rtable);
427                         if (lnext(k))
428                                 printf(", ");
429                 }
430                 printf(")");
431                 if (lnext(i))
432                         printf(", ");
433         }
434         printf(")\n");
435 }
436
437 /*
438  * print_tl
439  *        print targetlist in a more legible way.
440  */
441 void
442 print_tl(List *tlist, List *rtable)
443 {
444         List       *tl;
445
446         printf("(\n");
447         foreach(tl, tlist)
448         {
449                 TargetEntry *tle = lfirst(tl);
450
451                 printf("\t%d %s\t", tle->resdom->resno, tle->resdom->resname);
452                 if (tle->resdom->ressortgroupref != 0)
453                         printf("(%u):\t", tle->resdom->ressortgroupref);
454                 else
455                         printf("    :\t");
456                 print_expr((Node *) tle->expr, rtable);
457                 printf("\n");
458         }
459         printf(")\n");
460 }
461
462 /*
463  * print_slot
464  *        print out the tuple with the given TupleTableSlot
465  */
466 void
467 print_slot(TupleTableSlot *slot)
468 {
469         if (!slot->val)
470         {
471                 printf("tuple is null.\n");
472                 return;
473         }
474         if (!slot->ttc_tupleDescriptor)
475         {
476                 printf("no tuple descriptor.\n");
477                 return;
478         }
479
480         debugtup(slot->val, slot->ttc_tupleDescriptor, NULL);
481 }
482
483 static char *
484 plannode_type(Plan *p)
485 {
486         switch (nodeTag(p))
487         {
488                 case T_Plan:
489                         return "PLAN";
490                 case T_Result:
491                         return "RESULT";
492                 case T_Append:
493                         return "APPEND";
494                 case T_Scan:
495                         return "SCAN";
496                 case T_SeqScan:
497                         return "SEQSCAN";
498                 case T_IndexScan:
499                         return "INDEXSCAN";
500                 case T_TidScan:
501                         return "TIDSCAN";
502                 case T_SubqueryScan:
503                         return "SUBQUERYSCAN";
504                 case T_FunctionScan:
505                         return "FUNCTIONSCAN";
506                 case T_Join:
507                         return "JOIN";
508                 case T_NestLoop:
509                         return "NESTLOOP";
510                 case T_MergeJoin:
511                         return "MERGEJOIN";
512                 case T_HashJoin:
513                         return "HASHJOIN";
514                 case T_Material:
515                         return "MATERIAL";
516                 case T_Sort:
517                         return "SORT";
518                 case T_Agg:
519                         return "AGG";
520                 case T_Unique:
521                         return "UNIQUE";
522                 case T_SetOp:
523                         return "SETOP";
524                 case T_Limit:
525                         return "LIMIT";
526                 case T_Hash:
527                         return "HASH";
528                 case T_Group:
529                         return "GROUP";
530                 default:
531                         return "UNKNOWN";
532         }
533 }
534
535 /*
536  * Recursively prints a simple text description of the plan tree
537  */
538 void
539 print_plan_recursive(Plan *p, Query *parsetree, int indentLevel, char *label)
540 {
541         int                     i;
542         char            extraInfo[NAMEDATALEN + 100];
543
544         if (!p)
545                 return;
546         for (i = 0; i < indentLevel; i++)
547                 printf(" ");
548         printf("%s%s :c=%.2f..%.2f :r=%.0f :w=%d ", label, plannode_type(p),
549                    p->startup_cost, p->total_cost,
550                    p->plan_rows, p->plan_width);
551         if (IsA(p, Scan) ||
552                 IsA(p, SeqScan))
553         {
554                 RangeTblEntry *rte;
555
556                 rte = rt_fetch(((Scan *) p)->scanrelid, parsetree->rtable);
557                 StrNCpy(extraInfo, rte->eref->aliasname, NAMEDATALEN);
558         }
559         else if (IsA(p, IndexScan))
560         {
561                 RangeTblEntry *rte;
562
563                 rte = rt_fetch(((IndexScan *) p)->scan.scanrelid, parsetree->rtable);
564                 StrNCpy(extraInfo, rte->eref->aliasname, NAMEDATALEN);
565         }
566         else if (IsA(p, FunctionScan))
567         {
568                 RangeTblEntry *rte;
569
570                 rte = rt_fetch(((FunctionScan *) p)->scan.scanrelid, parsetree->rtable);
571                 StrNCpy(extraInfo, rte->eref->aliasname, NAMEDATALEN);
572         }
573         else
574                 extraInfo[0] = '\0';
575         if (extraInfo[0] != '\0')
576                 printf(" ( %s )\n", extraInfo);
577         else
578                 printf("\n");
579         print_plan_recursive(p->lefttree, parsetree, indentLevel + 3, "l: ");
580         print_plan_recursive(p->righttree, parsetree, indentLevel + 3, "r: ");
581
582         if (IsA(p, Append))
583         {
584                 List       *lst;
585                 int                     whichplan = 0;
586                 Append     *appendplan = (Append *) p;
587
588                 foreach(lst, appendplan->appendplans)
589                 {
590                         Plan       *subnode = (Plan *) lfirst(lst);
591
592                         /*
593                          * I don't think we need to fiddle with the range table here,
594                          * bjm
595                          */
596                         print_plan_recursive(subnode, parsetree, indentLevel + 3, "a: ");
597
598                         whichplan++;
599                 }
600         }
601 }
602
603 /* print_plan
604   prints just the plan node types */
605
606 void
607 print_plan(Plan *p, Query *parsetree)
608 {
609         print_plan_recursive(p, parsetree, 0, "");
610 }