]> granicus.if.org Git - postgresql/blob - src/backend/nodes/print.c
Replace errdetail("%s", ...) with errdetail_internal("%s", ...).
[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-2011, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  *
10  * IDENTIFICATION
11  *        src/backend/nodes/print.c
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 "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"
29
30
31 /*
32  * print
33  *        print contents of Node to stdout
34  */
35 void
36 print(void *obj)
37 {
38         char       *s;
39         char       *f;
40
41         s = nodeToString(obj);
42         f = format_node_dump(s);
43         pfree(s);
44         printf("%s\n", f);
45         fflush(stdout);
46         pfree(f);
47 }
48
49 /*
50  * pprint
51  *        pretty-print contents of Node to stdout
52  */
53 void
54 pprint(void *obj)
55 {
56         char       *s;
57         char       *f;
58
59         s = nodeToString(obj);
60         f = pretty_format_node_dump(s);
61         pfree(s);
62         printf("%s\n", f);
63         fflush(stdout);
64         pfree(f);
65 }
66
67 /*
68  * elog_node_display
69  *        send pretty-printed contents of Node to postmaster log
70  */
71 void
72 elog_node_display(int lev, const char *title, void *obj, bool pretty)
73 {
74         char       *s;
75         char       *f;
76
77         s = nodeToString(obj);
78         if (pretty)
79                 f = pretty_format_node_dump(s);
80         else
81                 f = format_node_dump(s);
82         pfree(s);
83         ereport(lev,
84                         (errmsg_internal("%s:", title),
85                          errdetail_internal("%s", f)));
86         pfree(f);
87 }
88
89 /*
90  * Format a nodeToString output for display on a terminal.
91  *
92  * The result is a palloc'd string.
93  *
94  * This version just tries to break at whitespace.
95  */
96 char *
97 format_node_dump(const char *dump)
98 {
99 #define LINELEN         78
100         char            line[LINELEN + 1];
101         StringInfoData str;
102         int                     i;
103         int                     j;
104         int                     k;
105
106         initStringInfo(&str);
107         i = 0;
108         for (;;)
109         {
110                 for (j = 0; j < LINELEN && dump[i] != '\0'; i++, j++)
111                         line[j] = dump[i];
112                 if (dump[i] == '\0')
113                         break;
114                 if (dump[i] == ' ')
115                 {
116                         /* ok to break at adjacent space */
117                         i++;
118                 }
119                 else
120                 {
121                         for (k = j - 1; k > 0; k--)
122                                 if (line[k] == ' ')
123                                         break;
124                         if (k > 0)
125                         {
126                                 /* back up; will reprint all after space */
127                                 i -= (j - k - 1);
128                                 j = k;
129                         }
130                 }
131                 line[j] = '\0';
132                 appendStringInfo(&str, "%s\n", line);
133         }
134         if (j > 0)
135         {
136                 line[j] = '\0';
137                 appendStringInfo(&str, "%s\n", line);
138         }
139         return str.data;
140 #undef LINELEN
141 }
142
143 /*
144  * Format a nodeToString output for display on a terminal.
145  *
146  * The result is a palloc'd string.
147  *
148  * This version tries to indent intelligently.
149  */
150 char *
151 pretty_format_node_dump(const char *dump)
152 {
153 #define INDENTSTOP      3
154 #define MAXINDENT       60
155 #define LINELEN         78
156         char            line[LINELEN + 1];
157         StringInfoData str;
158         int                     indentLev;
159         int                     indentDist;
160         int                     i;
161         int                     j;
162
163         initStringInfo(&str);
164         indentLev = 0;                          /* logical indent level */
165         indentDist = 0;                         /* physical indent distance */
166         i = 0;
167         for (;;)
168         {
169                 for (j = 0; j < indentDist; j++)
170                         line[j] = ' ';
171                 for (; j < LINELEN && dump[i] != '\0'; i++, j++)
172                 {
173                         line[j] = dump[i];
174                         switch (line[j])
175                         {
176                                 case '}':
177                                         if (j != indentDist)
178                                         {
179                                                 /* print data before the } */
180                                                 line[j] = '\0';
181                                                 appendStringInfo(&str, "%s\n", line);
182                                         }
183                                         /* print the } at indentDist */
184                                         line[indentDist] = '}';
185                                         line[indentDist + 1] = '\0';
186                                         appendStringInfo(&str, "%s\n", line);
187                                         /* outdent */
188                                         if (indentLev > 0)
189                                         {
190                                                 indentLev--;
191                                                 indentDist = Min(indentLev * INDENTSTOP, MAXINDENT);
192                                         }
193                                         j = indentDist - 1;
194                                         /* j will equal indentDist on next loop iteration */
195                                         /* suppress whitespace just after } */
196                                         while (dump[i + 1] == ' ')
197                                                 i++;
198                                         break;
199                                 case ')':
200                                         /* force line break after ), unless another ) follows */
201                                         if (dump[i + 1] != ')')
202                                         {
203                                                 line[j + 1] = '\0';
204                                                 appendStringInfo(&str, "%s\n", line);
205                                                 j = indentDist - 1;
206                                                 while (dump[i + 1] == ' ')
207                                                         i++;
208                                         }
209                                         break;
210                                 case '{':
211                                         /* force line break before { */
212                                         if (j != indentDist)
213                                         {
214                                                 line[j] = '\0';
215                                                 appendStringInfo(&str, "%s\n", line);
216                                         }
217                                         /* indent */
218                                         indentLev++;
219                                         indentDist = Min(indentLev * INDENTSTOP, MAXINDENT);
220                                         for (j = 0; j < indentDist; j++)
221                                                 line[j] = ' ';
222                                         line[j] = dump[i];
223                                         break;
224                                 case ':':
225                                         /* force line break before : */
226                                         if (j != indentDist)
227                                         {
228                                                 line[j] = '\0';
229                                                 appendStringInfo(&str, "%s\n", line);
230                                         }
231                                         j = indentDist;
232                                         line[j] = dump[i];
233                                         break;
234                         }
235                 }
236                 line[j] = '\0';
237                 if (dump[i] == '\0')
238                         break;
239                 appendStringInfo(&str, "%s\n", line);
240         }
241         if (j > 0)
242                 appendStringInfo(&str, "%s\n", line);
243         return str.data;
244 #undef INDENTSTOP
245 #undef MAXINDENT
246 #undef LINELEN
247 }
248
249 /*
250  * print_rt
251  *        print contents of range table
252  */
253 void
254 print_rt(List *rtable)
255 {
256         ListCell   *l;
257         int                     i = 1;
258
259         printf("resno\trefname  \trelid\tinFromCl\n");
260         printf("-----\t---------\t-----\t--------\n");
261         foreach(l, rtable)
262         {
263                 RangeTblEntry *rte = lfirst(l);
264
265                 switch (rte->rtekind)
266                 {
267                         case RTE_RELATION:
268                                 printf("%d\t%s\t%u\t%c",
269                                            i, rte->eref->aliasname, rte->relid, rte->relkind);
270                                 break;
271                         case RTE_SUBQUERY:
272                                 printf("%d\t%s\t[subquery]",
273                                            i, rte->eref->aliasname);
274                                 break;
275                         case RTE_JOIN:
276                                 printf("%d\t%s\t[join]",
277                                            i, rte->eref->aliasname);
278                                 break;
279                         case RTE_FUNCTION:
280                                 printf("%d\t%s\t[rangefunction]",
281                                            i, rte->eref->aliasname);
282                                 break;
283                         case RTE_VALUES:
284                                 printf("%d\t%s\t[values list]",
285                                            i, rte->eref->aliasname);
286                                 break;
287                         case RTE_CTE:
288                                 printf("%d\t%s\t[cte]",
289                                            i, rte->eref->aliasname);
290                                 break;
291                         default:
292                                 printf("%d\t%s\t[unknown rtekind]",
293                                            i, rte->eref->aliasname);
294                 }
295
296                 printf("\t%s\t%s\n",
297                            (rte->inh ? "inh" : ""),
298                            (rte->inFromCl ? "inFromCl" : ""));
299                 i++;
300         }
301 }
302
303
304 /*
305  * print_expr
306  *        print an expression
307  */
308 void
309 print_expr(Node *expr, List *rtable)
310 {
311         if (expr == NULL)
312         {
313                 printf("<>");
314                 return;
315         }
316
317         if (IsA(expr, Var))
318         {
319                 Var                *var = (Var *) expr;
320                 char       *relname,
321                                    *attname;
322
323                 switch (var->varno)
324                 {
325                         case INNER:
326                                 relname = "INNER";
327                                 attname = "?";
328                                 break;
329                         case OUTER:
330                                 relname = "OUTER";
331                                 attname = "?";
332                                 break;
333                         default:
334                                 {
335                                         RangeTblEntry *rte;
336
337                                         Assert(var->varno > 0 &&
338                                                    (int) var->varno <= list_length(rtable));
339                                         rte = rt_fetch(var->varno, rtable);
340                                         relname = rte->eref->aliasname;
341                                         attname = get_rte_attribute_name(rte, var->varattno);
342                                 }
343                                 break;
344                 }
345                 printf("%s.%s", relname, attname);
346         }
347         else if (IsA(expr, Const))
348         {
349                 Const      *c = (Const *) expr;
350                 Oid                     typoutput;
351                 bool            typIsVarlena;
352                 char       *outputstr;
353
354                 if (c->constisnull)
355                 {
356                         printf("NULL");
357                         return;
358                 }
359
360                 getTypeOutputInfo(c->consttype,
361                                                   &typoutput, &typIsVarlena);
362
363                 outputstr = OidOutputFunctionCall(typoutput, c->constvalue);
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 (list_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                 ListCell   *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 PathKeys
409  */
410 void
411 print_pathkeys(List *pathkeys, List *rtable)
412 {
413         ListCell   *i;
414
415         printf("(");
416         foreach(i, pathkeys)
417         {
418                 PathKey    *pathkey = (PathKey *) lfirst(i);
419                 EquivalenceClass *eclass;
420                 ListCell   *k;
421                 bool            first = true;
422
423                 eclass = pathkey->pk_eclass;
424                 /* chase up, in case pathkey is non-canonical */
425                 while (eclass->ec_merged)
426                         eclass = eclass->ec_merged;
427
428                 printf("(");
429                 foreach(k, eclass->ec_members)
430                 {
431                         EquivalenceMember *mem = (EquivalenceMember *) lfirst(k);
432
433                         if (first)
434                                 first = false;
435                         else
436                                 printf(", ");
437                         print_expr((Node *) mem->em_expr, rtable);
438                 }
439                 printf(")");
440                 if (lnext(i))
441                         printf(", ");
442         }
443         printf(")\n");
444 }
445
446 /*
447  * print_tl
448  *        print targetlist in a more legible way.
449  */
450 void
451 print_tl(List *tlist, List *rtable)
452 {
453         ListCell   *tl;
454
455         printf("(\n");
456         foreach(tl, tlist)
457         {
458                 TargetEntry *tle = (TargetEntry *) lfirst(tl);
459
460                 printf("\t%d %s\t", tle->resno,
461                            tle->resname ? tle->resname : "<null>");
462                 if (tle->ressortgroupref != 0)
463                         printf("(%u):\t", tle->ressortgroupref);
464                 else
465                         printf("    :\t");
466                 print_expr((Node *) tle->expr, rtable);
467                 printf("\n");
468         }
469         printf(")\n");
470 }
471
472 /*
473  * print_slot
474  *        print out the tuple with the given TupleTableSlot
475  */
476 void
477 print_slot(TupleTableSlot *slot)
478 {
479         if (TupIsNull(slot))
480         {
481                 printf("tuple is null.\n");
482                 return;
483         }
484         if (!slot->tts_tupleDescriptor)
485         {
486                 printf("no tuple descriptor.\n");
487                 return;
488         }
489
490         debugtup(slot, NULL);
491 }