PostgreSQL Source Code  git master
print.c File Reference
#include "postgres.h"
#include "access/printtup.h"
#include "lib/stringinfo.h"
#include "nodes/print.h"
#include "optimizer/clauses.h"
#include "parser/parsetree.h"
#include "utils/lsyscache.h"
Include dependency graph for print.c:

Go to the source code of this file.

Macros

#define LINELEN   78
 
#define INDENTSTOP   3
 
#define MAXINDENT   60
 
#define LINELEN   78
 

Functions

void print (const void *obj)
 
void pprint (const void *obj)
 
void elog_node_display (int lev, const char *title, const void *obj, bool pretty)
 
char * format_node_dump (const char *dump)
 
char * pretty_format_node_dump (const char *dump)
 
void print_rt (const List *rtable)
 
void print_expr (const Node *expr, const List *rtable)
 
void print_pathkeys (const List *pathkeys, const List *rtable)
 
void print_tl (const List *tlist, const List *rtable)
 
void print_slot (TupleTableSlot *slot)
 

Macro Definition Documentation

◆ INDENTSTOP

#define INDENTSTOP   3

Referenced by pretty_format_node_dump().

◆ LINELEN [1/2]

#define LINELEN   78

◆ LINELEN [2/2]

#define LINELEN   78

◆ MAXINDENT

#define MAXINDENT   60

Referenced by pretty_format_node_dump().

Function Documentation

◆ elog_node_display()

void elog_node_display ( int  lev,
const char *  title,
const void *  obj,
bool  pretty 
)

Definition at line 71 of file print.c.

References ereport, errdetail_internal(), errmsg_internal(), format_node_dump(), nodeToString(), pfree(), and pretty_format_node_dump().

Referenced by pg_plan_query(), and pg_rewrite_query().

72 {
73  char *s;
74  char *f;
75 
76  s = nodeToString(obj);
77  if (pretty)
79  else
80  f = format_node_dump(s);
81  pfree(s);
82  ereport(lev,
83  (errmsg_internal("%s:", title),
84  errdetail_internal("%s", f)));
85  pfree(f);
86 }
char * pretty_format_node_dump(const char *dump)
Definition: print.c:150
int errdetail_internal(const char *fmt,...)
Definition: elog.c:900
void pfree(void *pointer)
Definition: mcxt.c:949
#define ereport(elevel, rest)
Definition: elog.h:122
int errmsg_internal(const char *fmt,...)
Definition: elog.c:827
char * format_node_dump(const char *dump)
Definition: print.c:96
char * nodeToString(const void *obj)
Definition: outfuncs.c:4265

◆ format_node_dump()

char* format_node_dump ( const char *  dump)

Definition at line 96 of file print.c.

References appendStringInfo(), StringInfoData::data, i, initStringInfo(), LINELEN, and generate_unaccent_rules::str.

Referenced by elog_node_display(), and print().

97 {
98 #define LINELEN 78
99  char line[LINELEN + 1];
101  int i;
102  int j;
103  int k;
104 
105  initStringInfo(&str);
106  i = 0;
107  for (;;)
108  {
109  for (j = 0; j < LINELEN && dump[i] != '\0'; i++, j++)
110  line[j] = dump[i];
111  if (dump[i] == '\0')
112  break;
113  if (dump[i] == ' ')
114  {
115  /* ok to break at adjacent space */
116  i++;
117  }
118  else
119  {
120  for (k = j - 1; k > 0; k--)
121  if (line[k] == ' ')
122  break;
123  if (k > 0)
124  {
125  /* back up; will reprint all after space */
126  i -= (j - k - 1);
127  j = k;
128  }
129  }
130  line[j] = '\0';
131  appendStringInfo(&str, "%s\n", line);
132  }
133  if (j > 0)
134  {
135  line[j] = '\0';
136  appendStringInfo(&str, "%s\n", line);
137  }
138  return str.data;
139 #undef LINELEN
140 }
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:78
void initStringInfo(StringInfo str)
Definition: stringinfo.c:46
#define LINELEN
int i

◆ pprint()

void pprint ( const void *  obj)

Definition at line 53 of file print.c.

References nodeToString(), pfree(), and pretty_format_node_dump().

Referenced by preprocess_expression().

54 {
55  char *s;
56  char *f;
57 
58  s = nodeToString(obj);
60  pfree(s);
61  printf("%s\n", f);
62  fflush(stdout);
63  pfree(f);
64 }
char * pretty_format_node_dump(const char *dump)
Definition: print.c:150
void pfree(void *pointer)
Definition: mcxt.c:949
char * nodeToString(const void *obj)
Definition: outfuncs.c:4265

◆ pretty_format_node_dump()

char* pretty_format_node_dump ( const char *  dump)

Definition at line 150 of file print.c.

References appendStringInfo(), StringInfoData::data, i, INDENTSTOP, initStringInfo(), LINELEN, MAXINDENT, Min, and generate_unaccent_rules::str.

Referenced by elog_node_display(), and pprint().

151 {
152 #define INDENTSTOP 3
153 #define MAXINDENT 60
154 #define LINELEN 78
155  char line[LINELEN + 1];
157  int indentLev;
158  int indentDist;
159  int i;
160  int j;
161 
162  initStringInfo(&str);
163  indentLev = 0; /* logical indent level */
164  indentDist = 0; /* physical indent distance */
165  i = 0;
166  for (;;)
167  {
168  for (j = 0; j < indentDist; j++)
169  line[j] = ' ';
170  for (; j < LINELEN && dump[i] != '\0'; i++, j++)
171  {
172  line[j] = dump[i];
173  switch (line[j])
174  {
175  case '}':
176  if (j != indentDist)
177  {
178  /* print data before the } */
179  line[j] = '\0';
180  appendStringInfo(&str, "%s\n", line);
181  }
182  /* print the } at indentDist */
183  line[indentDist] = '}';
184  line[indentDist + 1] = '\0';
185  appendStringInfo(&str, "%s\n", line);
186  /* outdent */
187  if (indentLev > 0)
188  {
189  indentLev--;
190  indentDist = Min(indentLev * INDENTSTOP, MAXINDENT);
191  }
192  j = indentDist - 1;
193  /* j will equal indentDist on next loop iteration */
194  /* suppress whitespace just after } */
195  while (dump[i + 1] == ' ')
196  i++;
197  break;
198  case ')':
199  /* force line break after ), unless another ) follows */
200  if (dump[i + 1] != ')')
201  {
202  line[j + 1] = '\0';
203  appendStringInfo(&str, "%s\n", line);
204  j = indentDist - 1;
205  while (dump[i + 1] == ' ')
206  i++;
207  }
208  break;
209  case '{':
210  /* force line break before { */
211  if (j != indentDist)
212  {
213  line[j] = '\0';
214  appendStringInfo(&str, "%s\n", line);
215  }
216  /* indent */
217  indentLev++;
218  indentDist = Min(indentLev * INDENTSTOP, MAXINDENT);
219  for (j = 0; j < indentDist; j++)
220  line[j] = ' ';
221  line[j] = dump[i];
222  break;
223  case ':':
224  /* force line break before : */
225  if (j != indentDist)
226  {
227  line[j] = '\0';
228  appendStringInfo(&str, "%s\n", line);
229  }
230  j = indentDist;
231  line[j] = dump[i];
232  break;
233  }
234  }
235  line[j] = '\0';
236  if (dump[i] == '\0')
237  break;
238  appendStringInfo(&str, "%s\n", line);
239  }
240  if (j > 0)
241  appendStringInfo(&str, "%s\n", line);
242  return str.data;
243 #undef INDENTSTOP
244 #undef MAXINDENT
245 #undef LINELEN
246 }
#define Min(x, y)
Definition: c.h:802
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:78
void initStringInfo(StringInfo str)
Definition: stringinfo.c:46
#define MAXINDENT
#define LINELEN
#define INDENTSTOP
int i

◆ print()

void print ( const void *  obj)

Definition at line 35 of file print.c.

References format_node_dump(), nodeToString(), and pfree().

36 {
37  char *s;
38  char *f;
39 
40  s = nodeToString(obj);
41  f = format_node_dump(s);
42  pfree(s);
43  printf("%s\n", f);
44  fflush(stdout);
45  pfree(f);
46 }
void pfree(void *pointer)
Definition: mcxt.c:949
char * format_node_dump(const char *dump)
Definition: print.c:96
char * nodeToString(const void *obj)
Definition: outfuncs.c:4265

◆ print_expr()

void print_expr ( const Node expr,
const List rtable 
)

Definition at line 316 of file print.c.

References Alias::aliasname, FuncExpr::args, OpExpr::args, Assert, Const::constisnull, Const::consttype, Const::constvalue, RangeTblEntry::eref, FuncExpr::funcid, get_func_name(), get_leftop(), get_opname(), get_rightop(), get_rte_attribute_name(), getTypeOutputInfo(), INDEX_VAR, INNER_VAR, IsA, lfirst, list_length(), lnext, OidOutputFunctionCall(), OpExpr::opno, OUTER_VAR, pfree(), print_expr(), rt_fetch, Var::varattno, and Var::varno.

Referenced by generate_partition_wise_join_paths(), print_expr(), print_pathkeys(), and print_tl().

317 {
318  if (expr == NULL)
319  {
320  printf("<>");
321  return;
322  }
323 
324  if (IsA(expr, Var))
325  {
326  const Var *var = (const Var *) expr;
327  char *relname,
328  *attname;
329 
330  switch (var->varno)
331  {
332  case INNER_VAR:
333  relname = "INNER";
334  attname = "?";
335  break;
336  case OUTER_VAR:
337  relname = "OUTER";
338  attname = "?";
339  break;
340  case INDEX_VAR:
341  relname = "INDEX";
342  attname = "?";
343  break;
344  default:
345  {
346  RangeTblEntry *rte;
347 
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);
353  }
354  break;
355  }
356  printf("%s.%s", relname, attname);
357  }
358  else if (IsA(expr, Const))
359  {
360  const Const *c = (const Const *) expr;
361  Oid typoutput;
362  bool typIsVarlena;
363  char *outputstr;
364 
365  if (c->constisnull)
366  {
367  printf("NULL");
368  return;
369  }
370 
372  &typoutput, &typIsVarlena);
373 
374  outputstr = OidOutputFunctionCall(typoutput, c->constvalue);
375  printf("%s", outputstr);
376  pfree(outputstr);
377  }
378  else if (IsA(expr, OpExpr))
379  {
380  const OpExpr *e = (const OpExpr *) expr;
381  char *opname;
382 
383  opname = get_opname(e->opno);
384  if (list_length(e->args) > 1)
385  {
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);
389  }
390  else
391  {
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);
395  }
396  }
397  else if (IsA(expr, FuncExpr))
398  {
399  const FuncExpr *e = (const FuncExpr *) expr;
400  char *funcname;
401  ListCell *l;
402 
403  funcname = get_func_name(e->funcid);
404  printf("%s(", ((funcname != NULL) ? funcname : "(invalid function)"));
405  foreach(l, e->args)
406  {
407  print_expr(lfirst(l), rtable);
408  if (lnext(l))
409  printf(",");
410  }
411  printf(")");
412  }
413  else
414  printf("unknown expr");
415 }
Datum constvalue
Definition: primnodes.h:196
#define IsA(nodeptr, _type_)
Definition: nodes.h:563
void getTypeOutputInfo(Oid type, Oid *typOutput, bool *typIsVarlena)
Definition: lsyscache.c:2665
List * args
Definition: primnodes.h:457
AttrNumber varattno
Definition: primnodes.h:168
unsigned int Oid
Definition: postgres_ext.h:31
Definition: primnodes.h:163
Oid consttype
Definition: primnodes.h:192
char * get_opname(Oid opno)
Definition: lsyscache.c:1119
void pfree(void *pointer)
Definition: mcxt.c:949
Oid funcid
Definition: primnodes.h:449
char * get_func_name(Oid funcid)
Definition: lsyscache.c:1412
Node * get_leftop(const Expr *clause)
Definition: clauses.c:199
char * c
void print_expr(const Node *expr, const List *rtable)
Definition: print.c:316
#define lnext(lc)
Definition: pg_list.h:105
#define rt_fetch(rangetable_index, rangetable)
Definition: parsetree.h:31
Index varno
Definition: primnodes.h:166
#define INNER_VAR
Definition: primnodes.h:153
char * get_rte_attribute_name(RangeTblEntry *rte, AttrNumber attnum)
#define Assert(condition)
Definition: c.h:670
#define lfirst(lc)
Definition: pg_list.h:106
char * aliasname
Definition: primnodes.h:42
static int list_length(const List *l)
Definition: pg_list.h:89
Node * get_rightop(const Expr *clause)
Definition: clauses.c:216
e
Definition: preproc-init.c:82
char * OidOutputFunctionCall(Oid functionId, Datum val)
Definition: fmgr.c:1742
Oid opno
Definition: primnodes.h:496
Alias * eref
Definition: parsenodes.h:1055
List * args
Definition: primnodes.h:502
#define INDEX_VAR
Definition: primnodes.h:155
#define OUTER_VAR
Definition: primnodes.h:154
bool constisnull
Definition: primnodes.h:197

◆ print_pathkeys()

void print_pathkeys ( const List pathkeys,
const List rtable 
)

Definition at line 422 of file print.c.

References EquivalenceClass::ec_members, EquivalenceClass::ec_merged, eclass(), EquivalenceMember::em_expr, i, lfirst, lnext, PathKey::pk_eclass, and print_expr().

Referenced by generate_partition_wise_join_paths().

423 {
424  const ListCell *i;
425 
426  printf("(");
427  foreach(i, pathkeys)
428  {
429  PathKey *pathkey = (PathKey *) lfirst(i);
431  ListCell *k;
432  bool first = true;
433 
434  eclass = pathkey->pk_eclass;
435  /* chase up, in case pathkey is non-canonical */
436  while (eclass->ec_merged)
437  eclass = eclass->ec_merged;
438 
439  printf("(");
440  foreach(k, eclass->ec_members)
441  {
443 
444  if (first)
445  first = false;
446  else
447  printf(", ");
448  print_expr((Node *) mem->em_expr, rtable);
449  }
450  printf(")");
451  if (lnext(i))
452  printf(", ");
453  }
454  printf(")\n");
455 }
Definition: nodes.h:512
static struct cvec * eclass(struct vars *v, chr c, int cases)
Definition: regc_locale.c:508
void print_expr(const Node *expr, const List *rtable)
Definition: print.c:316
#define lnext(lc)
Definition: pg_list.h:105
#define lfirst(lc)
Definition: pg_list.h:106
EquivalenceClass * pk_eclass
Definition: relation.h:939
int i
struct EquivalenceClass * ec_merged
Definition: relation.h:874
List * ec_members
Definition: relation.h:862

◆ print_rt()

void print_rt ( const List rtable)

Definition at line 253 of file print.c.

References Alias::aliasname, RangeTblEntry::eref, i, RangeTblEntry::inFromCl, RangeTblEntry::inh, lfirst, RangeTblEntry::relid, RangeTblEntry::relkind, RTE_CTE, RTE_FUNCTION, RTE_JOIN, RTE_NAMEDTUPLESTORE, RTE_RELATION, RTE_SUBQUERY, RTE_TABLEFUNC, RTE_VALUES, and RangeTblEntry::rtekind.

254 {
255  const ListCell *l;
256  int i = 1;
257 
258  printf("resno\trefname \trelid\tinFromCl\n");
259  printf("-----\t---------\t-----\t--------\n");
260  foreach(l, rtable)
261  {
262  RangeTblEntry *rte = lfirst(l);
263 
264  switch (rte->rtekind)
265  {
266  case RTE_RELATION:
267  printf("%d\t%s\t%u\t%c",
268  i, rte->eref->aliasname, rte->relid, rte->relkind);
269  break;
270  case RTE_SUBQUERY:
271  printf("%d\t%s\t[subquery]",
272  i, rte->eref->aliasname);
273  break;
274  case RTE_JOIN:
275  printf("%d\t%s\t[join]",
276  i, rte->eref->aliasname);
277  break;
278  case RTE_FUNCTION:
279  printf("%d\t%s\t[rangefunction]",
280  i, rte->eref->aliasname);
281  break;
282  case RTE_TABLEFUNC:
283  printf("%d\t%s\t[table function]",
284  i, rte->eref->aliasname);
285  break;
286  case RTE_VALUES:
287  printf("%d\t%s\t[values list]",
288  i, rte->eref->aliasname);
289  break;
290  case RTE_CTE:
291  printf("%d\t%s\t[cte]",
292  i, rte->eref->aliasname);
293  break;
294  case RTE_NAMEDTUPLESTORE:
295  printf("%d\t%s\t[tuplestore]",
296  i, rte->eref->aliasname);
297  break;
298  default:
299  printf("%d\t%s\t[unknown rtekind]",
300  i, rte->eref->aliasname);
301  }
302 
303  printf("\t%s\t%s\n",
304  (rte->inh ? "inh" : ""),
305  (rte->inFromCl ? "inFromCl" : ""));
306  i++;
307  }
308 }
#define lfirst(lc)
Definition: pg_list.h:106
char * aliasname
Definition: primnodes.h:42
RTEKind rtekind
Definition: parsenodes.h:951
int i
Alias * eref
Definition: parsenodes.h:1055

◆ print_slot()

void print_slot ( TupleTableSlot slot)

Definition at line 488 of file print.c.

References debugtup(), TupleTableSlot::tts_tupleDescriptor, and TupIsNull.

489 {
490  if (TupIsNull(slot))
491  {
492  printf("tuple is null.\n");
493  return;
494  }
495  if (!slot->tts_tupleDescriptor)
496  {
497  printf("no tuple descriptor.\n");
498  return;
499  }
500 
501  debugtup(slot, NULL);
502 }
#define TupIsNull(slot)
Definition: tuptable.h:138
TupleDesc tts_tupleDescriptor
Definition: tuptable.h:121
bool debugtup(TupleTableSlot *slot, DestReceiver *self)
Definition: printtup.c:606

◆ print_tl()

void print_tl ( const List tlist,
const List rtable 
)

Definition at line 462 of file print.c.

References TargetEntry::expr, lfirst, print_expr(), TargetEntry::resname, TargetEntry::resno, and TargetEntry::ressortgroupref.

463 {
464  const ListCell *tl;
465 
466  printf("(\n");
467  foreach(tl, tlist)
468  {
469  TargetEntry *tle = (TargetEntry *) lfirst(tl);
470 
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);
475  else
476  printf(" :\t");
477  print_expr((Node *) tle->expr, rtable);
478  printf("\n");
479  }
480  printf(")\n");
481 }
Definition: nodes.h:512
char * resname
Definition: primnodes.h:1377
AttrNumber resno
Definition: primnodes.h:1376
void print_expr(const Node *expr, const List *rtable)
Definition: print.c:316
#define lfirst(lc)
Definition: pg_list.h:106
Expr * expr
Definition: primnodes.h:1375
Index ressortgroupref
Definition: primnodes.h:1378