]> granicus.if.org Git - postgresql/blob - src/backend/nodes/print.c
Rewriter and planner should use only resno, not resname, to identify
[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-2003, 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.64 2003/08/11 23:04:49 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,
452                            tle->resdom->resname ? tle->resdom->resname : "<null>");
453                 if (tle->resdom->ressortgroupref != 0)
454                         printf("(%u):\t", tle->resdom->ressortgroupref);
455                 else
456                         printf("    :\t");
457                 print_expr((Node *) tle->expr, rtable);
458                 printf("\n");
459         }
460         printf(")\n");
461 }
462
463 /*
464  * print_slot
465  *        print out the tuple with the given TupleTableSlot
466  */
467 void
468 print_slot(TupleTableSlot *slot)
469 {
470         if (!slot->val)
471         {
472                 printf("tuple is null.\n");
473                 return;
474         }
475         if (!slot->ttc_tupleDescriptor)
476         {
477                 printf("no tuple descriptor.\n");
478                 return;
479         }
480
481         debugtup(slot->val, slot->ttc_tupleDescriptor, NULL);
482 }
483
484 static char *
485 plannode_type(Plan *p)
486 {
487         switch (nodeTag(p))
488         {
489                 case T_Plan:
490                         return "PLAN";
491                 case T_Result:
492                         return "RESULT";
493                 case T_Append:
494                         return "APPEND";
495                 case T_Scan:
496                         return "SCAN";
497                 case T_SeqScan:
498                         return "SEQSCAN";
499                 case T_IndexScan:
500                         return "INDEXSCAN";
501                 case T_TidScan:
502                         return "TIDSCAN";
503                 case T_SubqueryScan:
504                         return "SUBQUERYSCAN";
505                 case T_FunctionScan:
506                         return "FUNCTIONSCAN";
507                 case T_Join:
508                         return "JOIN";
509                 case T_NestLoop:
510                         return "NESTLOOP";
511                 case T_MergeJoin:
512                         return "MERGEJOIN";
513                 case T_HashJoin:
514                         return "HASHJOIN";
515                 case T_Material:
516                         return "MATERIAL";
517                 case T_Sort:
518                         return "SORT";
519                 case T_Agg:
520                         return "AGG";
521                 case T_Unique:
522                         return "UNIQUE";
523                 case T_SetOp:
524                         return "SETOP";
525                 case T_Limit:
526                         return "LIMIT";
527                 case T_Hash:
528                         return "HASH";
529                 case T_Group:
530                         return "GROUP";
531                 default:
532                         return "UNKNOWN";
533         }
534 }
535
536 /*
537  * Recursively prints a simple text description of the plan tree
538  */
539 void
540 print_plan_recursive(Plan *p, Query *parsetree, int indentLevel, char *label)
541 {
542         int                     i;
543         char            extraInfo[NAMEDATALEN + 100];
544
545         if (!p)
546                 return;
547         for (i = 0; i < indentLevel; i++)
548                 printf(" ");
549         printf("%s%s :c=%.2f..%.2f :r=%.0f :w=%d ", label, plannode_type(p),
550                    p->startup_cost, p->total_cost,
551                    p->plan_rows, p->plan_width);
552         if (IsA(p, Scan) ||
553                 IsA(p, SeqScan))
554         {
555                 RangeTblEntry *rte;
556
557                 rte = rt_fetch(((Scan *) p)->scanrelid, parsetree->rtable);
558                 StrNCpy(extraInfo, rte->eref->aliasname, NAMEDATALEN);
559         }
560         else if (IsA(p, IndexScan))
561         {
562                 RangeTblEntry *rte;
563
564                 rte = rt_fetch(((IndexScan *) p)->scan.scanrelid, parsetree->rtable);
565                 StrNCpy(extraInfo, rte->eref->aliasname, NAMEDATALEN);
566         }
567         else if (IsA(p, FunctionScan))
568         {
569                 RangeTblEntry *rte;
570
571                 rte = rt_fetch(((FunctionScan *) p)->scan.scanrelid, parsetree->rtable);
572                 StrNCpy(extraInfo, rte->eref->aliasname, NAMEDATALEN);
573         }
574         else
575                 extraInfo[0] = '\0';
576         if (extraInfo[0] != '\0')
577                 printf(" ( %s )\n", extraInfo);
578         else
579                 printf("\n");
580         print_plan_recursive(p->lefttree, parsetree, indentLevel + 3, "l: ");
581         print_plan_recursive(p->righttree, parsetree, indentLevel + 3, "r: ");
582
583         if (IsA(p, Append))
584         {
585                 List       *lst;
586                 int                     whichplan = 0;
587                 Append     *appendplan = (Append *) p;
588
589                 foreach(lst, appendplan->appendplans)
590                 {
591                         Plan       *subnode = (Plan *) lfirst(lst);
592
593                         /*
594                          * I don't think we need to fiddle with the range table here,
595                          * bjm
596                          */
597                         print_plan_recursive(subnode, parsetree, indentLevel + 3, "a: ");
598
599                         whichplan++;
600                 }
601         }
602 }
603
604 /* print_plan
605   prints just the plan node types */
606
607 void
608 print_plan(Plan *p, Query *parsetree)
609 {
610         print_plan_recursive(p, parsetree, 0, "");
611 }