outfuncs.c 40.7 KB
Newer Older
M
 
Marc G. Fournier 已提交
1
/*
2
 * outfuncs.c
3
 *	  routines to convert a node to ascii representation
4
 *
B
Bruce Momjian 已提交
5
 * Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
B
Add:  
Bruce Momjian 已提交
6
 * Portions Copyright (c) 1994, Regents of the University of California
7
 *
8
 *	$Header: /cvsroot/pgsql/src/backend/nodes/outfuncs.c,v 1.166 2002/08/04 19:48:09 momjian Exp $
9 10
 *
 * NOTES
11 12 13
 *	  Every (plan) node in POSTGRES has an associated "out" routine which
 *	  knows how to create its ascii representation. These functions are
 *	  useful for debugging as well as for storing plans in the system
14
 *	  catalogs (eg. views).
15
 */
16
#include "postgres.h"
17

18 19
#include <ctype.h>

B
Bruce Momjian 已提交
20 21 22
#include "lib/stringinfo.h"
#include "nodes/nodes.h"
#include "nodes/parsenodes.h"
23 24 25
#include "nodes/plannodes.h"
#include "nodes/primnodes.h"
#include "nodes/relation.h"
26
#include "parser/parse.h"
B
Bruce Momjian 已提交
27
#include "utils/datum.h"
28

29

30 31
#define booltostr(x)  ((x) ? "true" : "false")

32
static void _outDatum(StringInfo str, Datum value, int typlen, bool typbyval);
33
static void _outNode(StringInfo str, void *obj);
34

35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
/*
 * _outToken
 *	  Convert an ordinary string (eg, an identifier) into a form that
 *	  will be decoded back to a plain token by read.c's functions.
 *
 *	  If a null or empty string is given, it is encoded as "<>".
 */
static void
_outToken(StringInfo str, char *s)
{
	if (s == NULL || *s == '\0')
	{
		appendStringInfo(str, "<>");
		return;
	}
50

51 52
	/*
	 * Look for characters or patterns that are treated specially by
53 54
	 * read.c (either in pg_strtok() or in nodeRead()), and therefore need
	 * a protective backslash.
55 56 57 58 59
	 */
	/* These characters only need to be quoted at the start of the string */
	if (*s == '<' ||
		*s == '\"' ||
		*s == '@' ||
60
		isdigit((unsigned char) *s) ||
61 62
		((*s == '+' || *s == '-') &&
		 (isdigit((unsigned char) s[1]) || s[1] == '.')))
63 64 65 66 67 68 69 70 71 72 73
		appendStringInfoChar(str, '\\');
	while (*s)
	{
		/* These chars must be backslashed anywhere in the string */
		if (*s == ' ' || *s == '\n' || *s == '\t' ||
			*s == '(' || *s == ')' || *s == '{' || *s == '}' ||
			*s == '\\')
			appendStringInfoChar(str, '\\');
		appendStringInfoChar(str, *s++);
	}
}
74

75 76
/*
 * _outIntList -
77
 *	   converts a List of integers
78
 */
79
static void
80
_outIntList(StringInfo str, List *list)
81
{
B
Bruce Momjian 已提交
82
	List	   *l;
83

84
	appendStringInfoChar(str, '(');
85
	foreach(l, list)
86 87
		appendStringInfo(str, " %d", lfirsti(l));
	appendStringInfoChar(str, ')');
88 89
}

90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
/*
 * _outOidList -
 *	   converts a List of OIDs
 */
static void
_outOidList(StringInfo str, List *list)
{
	List	   *l;

	appendStringInfoChar(str, '(');
	foreach(l, list)
		appendStringInfo(str, " %u", (Oid) lfirsti(l));
	appendStringInfoChar(str, ')');
}

105 106 107
static void
_outCreateStmt(StringInfo str, CreateStmt *node)
{
108 109
	appendStringInfo(str, " CREATE :relation ");
	_outNode(str, node->relation);
110

111
	appendStringInfo(str, "	:tableElts ");
112
	_outNode(str, node->tableElts);
M
 
Marc G. Fournier 已提交
113

114 115
	appendStringInfo(str, " :inhRelations ");
	_outNode(str, node->inhRelations);
M
 
Marc G. Fournier 已提交
116

117
	appendStringInfo(str, " :constraints ");
118
	_outNode(str, node->constraints);
119

120
	appendStringInfo(str, " :hasoids %s ",
121
					 booltostr(node->hasoids));
122
}
123 124 125 126

static void
_outIndexStmt(StringInfo str, IndexStmt *node)
{
127 128
	appendStringInfo(str, " INDEX :idxname ");
	_outToken(str, node->idxname);
129 130
	appendStringInfo(str, " :relation ");
	_outNode(str, node->relation);
131 132 133
	appendStringInfo(str, " :accessMethod ");
	_outToken(str, node->accessMethod);
	appendStringInfo(str, " :indexParams ");
134
	_outNode(str, node->indexParams);
135 136 137 138
	appendStringInfo(str, " :whereClause ");
	_outNode(str, node->whereClause);
	appendStringInfo(str, " :rangetable ");
	_outNode(str, node->rangetable);
139
	appendStringInfo(str, " :unique %s :primary %s :isconstraint %s ",
140
					 booltostr(node->unique),
141 142
					 booltostr(node->primary),
					 booltostr(node->isconstraint));
143
}
144

145 146 147 148 149 150 151
static void
_outNotifyStmt(StringInfo str, NotifyStmt *node)
{
	appendStringInfo(str, "NOTIFY :relation ");
	_outNode(str, node->relation);
}

152 153 154
static void
_outSelectStmt(StringInfo str, SelectStmt *node)
{
155
	/* XXX this is pretty durn incomplete */
M
 
Marc G. Fournier 已提交
156
	appendStringInfo(str, "SELECT :where ");
157 158 159 160 161 162
	_outNode(str, node->whereClause);
}

static void
_outFuncCall(StringInfo str, FuncCall *node)
{
163
	appendStringInfo(str, "FUNCTION ");
164
	_outNode(str, node->funcname);
165
	appendStringInfo(str, " :args ");
166
	_outNode(str, node->args);
167
	appendStringInfo(str, " :agg_star %s :agg_distinct %s ",
168 169
					 booltostr(node->agg_star),
					 booltostr(node->agg_distinct));
170
}
171

172 173 174
static void
_outColumnDef(StringInfo str, ColumnDef *node)
{
175 176 177
	appendStringInfo(str, " COLUMNDEF :colname ");
	_outToken(str, node->colname);
	appendStringInfo(str, " :typename ");
178
	_outNode(str, node->typename);
179 180
	appendStringInfo(str, " :is_not_null %s :raw_default ",
					 booltostr(node->is_not_null));
181
	_outNode(str, node->raw_default);
182 183 184
	appendStringInfo(str, " :cooked_default ");
	_outToken(str, node->cooked_default);
	appendStringInfo(str, " :constraints ");
185
	_outNode(str, node->constraints);
186 187
	appendStringInfo(str, " :support ");
	_outNode(str, node->support);
188 189 190 191 192
}

static void
_outTypeName(StringInfo str, TypeName *node)
{
193 194 195 196 197
	appendStringInfo(str, " TYPENAME :names ");
	_outNode(str, node->names);
	appendStringInfo(str, " :typeid %u :timezone %s :setof %s"
					 " :pct_type %s typmod %d :arrayBounds ",
					 node->typeid,
198 199
					 booltostr(node->timezone),
					 booltostr(node->setof),
200
					 booltostr(node->pct_type),
B
Bruce Momjian 已提交
201
					 node->typmod);
202 203
	_outNode(str, node->arrayBounds);
}
204

205 206 207 208 209 210 211 212 213
static void
_outTypeCast(StringInfo str, TypeCast *node)
{
	appendStringInfo(str, " TYPECAST :arg ");
	_outNode(str, node->arg);
	appendStringInfo(str, " :typename ");
	_outNode(str, node->typename);
}

214 215 216
static void
_outIndexElem(StringInfo str, IndexElem *node)
{
217 218
	appendStringInfo(str, " INDEXELEM :name ");
	_outToken(str, node->name);
219 220
	appendStringInfo(str, " :funcname ");
	_outNode(str, node->funcname);
221
	appendStringInfo(str, " :args ");
222
	_outNode(str, node->args);
223 224
	appendStringInfo(str, " :opclass ");
	_outNode(str, node->opclass);
225
}
226

227
static void
228
_outQuery(StringInfo str, Query *node)
229
{
230
	appendStringInfo(str, " QUERY :command %d :utility ", node->commandType);
231

232 233 234 235 236 237 238
	/*
	 * Hack to work around missing outfuncs routines for a lot of the
	 * utility-statement node types.  (The only one we actually *need*
	 * for rules support is NotifyStmt.)  Someday we ought to support
	 * 'em all, but for the meantime do this to avoid getting lots of
	 * warnings when running with debug_print_parse on.
	 */
239 240 241 242 243 244 245
	if (node->utilityStmt)
	{
		switch (nodeTag(node->utilityStmt))
		{
			case T_CreateStmt:
			case T_IndexStmt:
			case T_NotifyStmt:
246
				_outNode(str, node->utilityStmt);
247 248
				break;
			default:
249 250
				appendStringInfo(str, "?");
				break;
251 252
		}
	}
253
	else
254
		appendStringInfo(str, "<>");
255

256
	appendStringInfo(str, " :resultRelation %d :into ",
257
					 node->resultRelation);
258
	_outNode(str, node->into);
259

260
	appendStringInfo(str, " :isPortal %s :isBinary %s"
261
					 " :hasAggs %s :hasSubLinks %s :rtable ",
262 263 264 265
					 booltostr(node->isPortal),
					 booltostr(node->isBinary),
					 booltostr(node->hasAggs),
					 booltostr(node->hasSubLinks));
266
	_outNode(str, node->rtable);
M
 
Marc G. Fournier 已提交
267

268 269 270
	appendStringInfo(str, " :jointree ");
	_outNode(str, node->jointree);

271 272 273
	appendStringInfo(str, " :rowMarks ");
	_outIntList(str, node->rowMarks);

274 275
	appendStringInfo(str, " :targetList ");
	_outNode(str, node->targetList);
M
 
Marc G. Fournier 已提交
276

277 278
	appendStringInfo(str, " :groupClause ");
	_outNode(str, node->groupClause);
M
 
Marc G. Fournier 已提交
279

280
	appendStringInfo(str, " :havingQual ");
281
	_outNode(str, node->havingQual);
M
 
Marc G. Fournier 已提交
282

283 284
	appendStringInfo(str, " :distinctClause ");
	_outNode(str, node->distinctClause);
B
Hi!  
Bruce Momjian 已提交
285

286 287
	appendStringInfo(str, " :sortClause ");
	_outNode(str, node->sortClause);
288

B
Bruce Momjian 已提交
289 290
	appendStringInfo(str, " :limitOffset ");
	_outNode(str, node->limitOffset);
M
 
Marc G. Fournier 已提交
291

B
Bruce Momjian 已提交
292 293
	appendStringInfo(str, " :limitCount ");
	_outNode(str, node->limitCount);
294 295 296

	appendStringInfo(str, " :setOperations ");
	_outNode(str, node->setOperations);
297 298 299

	appendStringInfo(str, " :resultRelations ");
	_outIntList(str, node->resultRelations);
300 301 302
}

static void
303
_outSortClause(StringInfo str, SortClause *node)
304
{
305
	appendStringInfo(str, " SORTCLAUSE :tleSortGroupRef %u :sortop %u ",
306
					 node->tleSortGroupRef, node->sortop);
307 308 309 310 311
}

static void
_outGroupClause(StringInfo str, GroupClause *node)
{
312
	appendStringInfo(str, " GROUPCLAUSE :tleSortGroupRef %u :sortop %u ",
313
					 node->tleSortGroupRef, node->sortop);
314 315
}

316 317 318 319 320
static void
_outSetOperationStmt(StringInfo str, SetOperationStmt *node)
{
	appendStringInfo(str, " SETOPERATIONSTMT :op %d :all %s :larg ",
					 (int) node->op,
321
					 booltostr(node->all));
322 323 324 325
	_outNode(str, node->larg);
	appendStringInfo(str, " :rarg ");
	_outNode(str, node->rarg);
	appendStringInfo(str, " :colTypes ");
326
	_outOidList(str, node->colTypes);
327 328
}

329 330
/*
 * print the basic stuff of all nodes that inherit from Plan
331 332
 *
 * NOTE: we deliberately omit the execution state (EState)
333 334
 */
static void
335
_outPlanInfo(StringInfo str, Plan *node)
336
{
B
Bruce Momjian 已提交
337
	appendStringInfo(str,
338
					 ":startup_cost %.2f :total_cost %.2f :rows %.0f :width %d :qptargetlist ",
339 340
					 node->startup_cost,
					 node->total_cost,
341
					 node->plan_rows,
342
					 node->plan_width);
343
	_outNode(str, node->targetlist);
M
 
Marc G. Fournier 已提交
344

345
	appendStringInfo(str, " :qpqual ");
346
	_outNode(str, node->qual);
M
 
Marc G. Fournier 已提交
347

348
	appendStringInfo(str, " :lefttree ");
349
	_outNode(str, node->lefttree);
M
 
Marc G. Fournier 已提交
350

351
	appendStringInfo(str, " :righttree ");
352
	_outNode(str, node->righttree);
M
 
Marc G. Fournier 已提交
353

V
Vadim B. Mikheev 已提交
354 355
	appendStringInfo(str, " :extprm ");
	_outIntList(str, node->extParam);
M
 
Marc G. Fournier 已提交
356

V
Vadim B. Mikheev 已提交
357 358
	appendStringInfo(str, " :locprm ");
	_outIntList(str, node->locParam);
M
 
Marc G. Fournier 已提交
359

V
Vadim B. Mikheev 已提交
360 361
	appendStringInfo(str, " :initplan ");
	_outNode(str, node->initPlan);
M
 
Marc G. Fournier 已提交
362

M
 
Marc G. Fournier 已提交
363
	appendStringInfo(str, " :nprm %d ", node->nParamExec);
364 365 366
}

/*
367
 *	Stuff from plannodes.h
368 369
 */
static void
370
_outPlan(StringInfo str, Plan *node)
371
{
372
	appendStringInfo(str, " PLAN ");
373
	_outPlanInfo(str, (Plan *) node);
374 375 376
}

static void
377
_outResult(StringInfo str, Result *node)
378
{
379
	appendStringInfo(str, " RESULT ");
380 381
	_outPlanInfo(str, (Plan *) node);

382
	appendStringInfo(str, " :resconstantqual ");
383 384
	_outNode(str, node->resconstantqual);

385 386 387
}

/*
388
 *	Append is a subclass of Plan.
389 390
 */
static void
B
Bruce Momjian 已提交
391
_outAppend(StringInfo str, Append *node)
392
{
393
	appendStringInfo(str, " APPEND ");
394 395
	_outPlanInfo(str, (Plan *) node);

396 397
	appendStringInfo(str, " :appendplans ");
	_outNode(str, node->appendplans);
398

399
	appendStringInfo(str, " :isTarget %s ",
400
					 booltostr(node->isTarget));
401 402 403
}

/*
404
 *	Join is a subclass of Plan
405 406
 */
static void
407
_outJoin(StringInfo str, Join *node)
408
{
409
	appendStringInfo(str, " JOIN ");
410
	_outPlanInfo(str, (Plan *) node);
411 412 413
	appendStringInfo(str, " :jointype %d :joinqual ",
					 (int) node->jointype);
	_outNode(str, node->joinqual);
414 415 416
}

/*
417
 *	NestLoop is a subclass of Join
418 419
 */
static void
420
_outNestLoop(StringInfo str, NestLoop *node)
421
{
422
	appendStringInfo(str, " NESTLOOP ");
423
	_outPlanInfo(str, (Plan *) node);
424 425 426
	appendStringInfo(str, " :jointype %d :joinqual ",
					 (int) node->join.jointype);
	_outNode(str, node->join.joinqual);
427 428 429
}

/*
430
 *	MergeJoin is a subclass of Join
431 432
 */
static void
433
_outMergeJoin(StringInfo str, MergeJoin *node)
434
{
435
	appendStringInfo(str, " MERGEJOIN ");
436
	_outPlanInfo(str, (Plan *) node);
437 438 439
	appendStringInfo(str, " :jointype %d :joinqual ",
					 (int) node->join.jointype);
	_outNode(str, node->join.joinqual);
440

441
	appendStringInfo(str, " :mergeclauses ");
442
	_outNode(str, node->mergeclauses);
443 444 445
}

/*
446
 *	HashJoin is a subclass of Join.
447 448
 */
static void
449
_outHashJoin(StringInfo str, HashJoin *node)
450
{
451
	appendStringInfo(str, " HASHJOIN ");
452
	_outPlanInfo(str, (Plan *) node);
453 454 455
	appendStringInfo(str, " :jointype %d :joinqual ",
					 (int) node->join.jointype);
	_outNode(str, node->join.joinqual);
456

457
	appendStringInfo(str, " :hashclauses ");
458
	_outNode(str, node->hashclauses);
459
	appendStringInfo(str, " :hashjoinop %u ",
B
Bruce Momjian 已提交
460
					 node->hashjoinop);
461 462
}

V
Vadim B. Mikheev 已提交
463 464 465
static void
_outSubPlan(StringInfo str, SubPlan *node)
{
M
 
Marc G. Fournier 已提交
466
	appendStringInfo(str, " SUBPLAN :plan ");
V
Vadim B. Mikheev 已提交
467
	_outNode(str, node->plan);
M
 
Marc G. Fournier 已提交
468

469
	appendStringInfo(str, " :planid %d :rtable ", node->plan_id);
V
Vadim B. Mikheev 已提交
470
	_outNode(str, node->rtable);
M
 
Marc G. Fournier 已提交
471

V
Vadim B. Mikheev 已提交
472
	appendStringInfo(str, " :setprm ");
473
	_outIntList(str, node->setParam);
M
 
Marc G. Fournier 已提交
474

V
Vadim B. Mikheev 已提交
475
	appendStringInfo(str, " :parprm ");
476
	_outIntList(str, node->parParam);
M
 
Marc G. Fournier 已提交
477

V
Vadim B. Mikheev 已提交
478 479 480 481
	appendStringInfo(str, " :slink ");
	_outNode(str, node->sublink);
}

482
/*
483
 *	Scan is a subclass of Node
484 485
 */
static void
486
_outScan(StringInfo str, Scan *node)
487
{
488
	appendStringInfo(str, " SCAN ");
489 490
	_outPlanInfo(str, (Plan *) node);

491
	appendStringInfo(str, " :scanrelid %u ", node->scanrelid);
492 493 494
}

/*
495
 *	SeqScan is a subclass of Scan
496 497
 */
static void
498
_outSeqScan(StringInfo str, SeqScan *node)
499
{
500
	appendStringInfo(str, " SEQSCAN ");
501 502
	_outPlanInfo(str, (Plan *) node);

503
	appendStringInfo(str, " :scanrelid %u ", node->scanrelid);
504 505 506
}

/*
507
 *	IndexScan is a subclass of Scan
508 509
 */
static void
510
_outIndexScan(StringInfo str, IndexScan *node)
511
{
512
	appendStringInfo(str, " INDEXSCAN ");
513 514
	_outPlanInfo(str, (Plan *) node);

515
	appendStringInfo(str, " :scanrelid %u :indxid ", node->scan.scanrelid);
516
	_outOidList(str, node->indxid);
517

518
	appendStringInfo(str, " :indxqual ");
519 520
	_outNode(str, node->indxqual);

V
Vadim B. Mikheev 已提交
521 522 523
	appendStringInfo(str, " :indxqualorig ");
	_outNode(str, node->indxqualorig);

524
	appendStringInfo(str, " :indxorderdir %d ", node->indxorderdir);
525 526
}

527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543
/*
 *	TidScan is a subclass of Scan
 */
static void
_outTidScan(StringInfo str, TidScan *node)
{
	appendStringInfo(str, " TIDSCAN ");
	_outPlanInfo(str, (Plan *) node);

	appendStringInfo(str, " :scanrelid %u ", node->scan.scanrelid);
	appendStringInfo(str, " :needrescan %d ", node->needRescan);

	appendStringInfo(str, " :tideval ");
	_outNode(str, node->tideval);

}

544 545 546 547 548 549 550 551 552 553 554 555 556
/*
 *	SubqueryScan is a subclass of Scan
 */
static void
_outSubqueryScan(StringInfo str, SubqueryScan *node)
{
	appendStringInfo(str, " SUBQUERYSCAN ");
	_outPlanInfo(str, (Plan *) node);

	appendStringInfo(str, " :scanrelid %u :subplan ", node->scan.scanrelid);
	_outNode(str, node->subplan);
}

557 558 559 560 561 562 563 564 565 566 567 568
/*
 *	FunctionScan is a subclass of Scan
 */
static void
_outFunctionScan(StringInfo str, FunctionScan *node)
{
	appendStringInfo(str, " FUNCTIONSCAN ");
	_outPlanInfo(str, (Plan *) node);

	appendStringInfo(str, " :scanrelid %u ", node->scan.scanrelid);
}

569
/*
570
 *	Material is a subclass of Plan
571 572 573 574 575 576 577 578
 */
static void
_outMaterial(StringInfo str, Material *node)
{
	appendStringInfo(str, " MATERIAL ");
	_outPlanInfo(str, (Plan *) node);
}

579
/*
580
 *	Sort is a subclass of Plan
581 582
 */
static void
583
_outSort(StringInfo str, Sort *node)
584
{
585
	appendStringInfo(str, " SORT ");
586
	_outPlanInfo(str, (Plan *) node);
587
	appendStringInfo(str, " :keycount %d ", node->keycount);
588 589 590
}

static void
B
Bruce Momjian 已提交
591
_outAgg(StringInfo str, Agg *node)
592
{
593
	appendStringInfo(str, " AGG ");
594
	_outPlanInfo(str, (Plan *) node);
595 596 597
}

static void
598
_outGroup(StringInfo str, Group *node)
599
{
600
	appendStringInfo(str, " GRP ");
601 602 603
	_outPlanInfo(str, (Plan *) node);

	/* the actual Group fields */
M
 
Marc G. Fournier 已提交
604
	appendStringInfo(str, " :numCols %d :tuplePerGroup %s ",
B
Bruce Momjian 已提交
605
					 node->numCols,
606
					 booltostr(node->tuplePerGroup));
607
}
608

609
static void
610
_outUnique(StringInfo str, Unique *node)
611
{
B
Bruce Momjian 已提交
612
	int			i;
613

614
	appendStringInfo(str, " UNIQUE ");
615 616
	_outPlanInfo(str, (Plan *) node);

617
	appendStringInfo(str, " :numCols %d :uniqColIdx ",
618
					 node->numCols);
619 620 621
	for (i = 0; i < node->numCols; i++)
		appendStringInfo(str, "%d ", (int) node->uniqColIdx[i]);
}
622

623 624 625
static void
_outSetOp(StringInfo str, SetOp *node)
{
B
Bruce Momjian 已提交
626
	int			i;
627 628 629 630 631 632 633 634 635 636 637 638

	appendStringInfo(str, " SETOP ");
	_outPlanInfo(str, (Plan *) node);

	appendStringInfo(str, " :cmd %d :numCols %d :dupColIdx ",
					 (int) node->cmd, node->numCols);
	for (i = 0; i < node->numCols; i++)
		appendStringInfo(str, "%d ", (int) node->dupColIdx[i]);
	appendStringInfo(str, " :flagColIdx %d ",
					 (int) node->flagColIdx);
}

639 640 641 642 643 644 645 646 647 648 649 650
static void
_outLimit(StringInfo str, Limit *node)
{
	appendStringInfo(str, " LIMIT ");
	_outPlanInfo(str, (Plan *) node);

	appendStringInfo(str, " :limitOffset ");
	_outNode(str, node->limitOffset);
	appendStringInfo(str, " :limitCount ");
	_outNode(str, node->limitCount);
}

651
/*
652
 *	Hash is a subclass of Plan
653 654
 */
static void
655
_outHash(StringInfo str, Hash *node)
656
{
657
	appendStringInfo(str, " HASH ");
658 659
	_outPlanInfo(str, (Plan *) node);

660
	appendStringInfo(str, " :hashkey ");
661
	_outNode(str, node->hashkey);
662 663 664 665
}

/*****************************************************************************
 *
666
 *	Stuff from primnodes.h.
667 668 669 670
 *
 *****************************************************************************/

/*
671
 *	Resdom is a subclass of Node
672 673
 */
static void
674
_outResdom(StringInfo str, Resdom *node)
675
{
676
	appendStringInfo(str,
677
				 " RESDOM :resno %d :restype %u :restypmod %d :resname ",
B
Bruce Momjian 已提交
678 679 680
					 node->resno,
					 node->restype,
					 node->restypmod);
681
	_outToken(str, node->resname);
682
	appendStringInfo(str, " :reskey %u :reskeyop %u :ressortgroupref %u :resjunk %s ",
B
Bruce Momjian 已提交
683
					 node->reskey,
684
					 node->reskeyop,
685
					 node->ressortgroupref,
686
					 booltostr(node->resjunk));
687 688 689
}

static void
690
_outFjoin(StringInfo str, Fjoin *node)
691
{
692
	int			i;
693

M
 
Marc G. Fournier 已提交
694
	appendStringInfo(str, " FJOIN :initialized %s :nNodes %d ",
695
					 booltostr(node->fj_initialized),
B
Bruce Momjian 已提交
696
					 node->fj_nNodes);
697 698 699 700

	appendStringInfo(str, " :innerNode ");
	_outNode(str, node->fj_innerNode);

701 702
	appendStringInfo(str, " :results @ 0x%p :alwaysdone",
					 node->fj_results);
703 704

	for (i = 0; i < node->fj_nNodes; i++)
705 706
		appendStringInfo(str,
						 booltostr(node->fj_alwaysDone[i]));
707 708 709
}

/*
710
 *	Expr is a subclass of Node
711 712
 */
static void
713
_outExpr(StringInfo str, Expr *node)
714
{
715
	char	   *opstr = NULL;
716

M
 
Marc G. Fournier 已提交
717
	appendStringInfo(str, " EXPR :typeOid %u ",
B
Bruce Momjian 已提交
718
					 node->typeOid);
719 720 721

	switch (node->opType)
	{
722 723 724
		case OP_EXPR:
			opstr = "op";
			break;
725 726 727
		case DISTINCT_EXPR:
			opstr = "distinct";
			break;
728 729 730 731 732 733 734 735 736 737 738 739
		case FUNC_EXPR:
			opstr = "func";
			break;
		case OR_EXPR:
			opstr = "or";
			break;
		case AND_EXPR:
			opstr = "and";
			break;
		case NOT_EXPR:
			opstr = "not";
			break;
V
Vadim B. Mikheev 已提交
740 741 742
		case SUBPLAN_EXPR:
			opstr = "subp";
			break;
743
	}
744 745 746
	appendStringInfo(str, " :opType ");
	_outToken(str, opstr);
	appendStringInfo(str, " :oper ");
747
	_outNode(str, node->oper);
M
 
Marc G. Fournier 已提交
748

749
	appendStringInfo(str, " :args ");
750
	_outNode(str, node->args);
751 752 753
}

/*
754
 *	Var is a subclass of Expr
755 756
 */
static void
757
_outVar(StringInfo str, Var *node)
758
{
B
Bruce Momjian 已提交
759
	appendStringInfo(str,
760
				" VAR :varno %u :varattno %d :vartype %u :vartypmod %d ",
B
Bruce Momjian 已提交
761 762 763 764
					 node->varno,
					 node->varattno,
					 node->vartype,
					 node->vartypmod);
M
 
Marc G. Fournier 已提交
765

766
	appendStringInfo(str, " :varlevelsup %u :varnoold %u :varoattno %d",
B
Bruce Momjian 已提交
767 768 769
					 node->varlevelsup,
					 node->varnoold,
					 node->varoattno);
770 771 772
}

/*
773
 *	Const is a subclass of Expr
774 775
 */
static void
776
_outConst(StringInfo str, Const *node)
777
{
B
Bruce Momjian 已提交
778
	appendStringInfo(str,
779 780
					 " CONST :consttype %u :constlen %d :constbyval %s"
					 " :constisnull %s :constvalue ",
B
Bruce Momjian 已提交
781 782
					 node->consttype,
					 node->constlen,
783 784
					 booltostr(node->constbyval),
					 booltostr(node->constisnull));
M
 
Marc G. Fournier 已提交
785

786
	if (node->constisnull)
B
Bruce Momjian 已提交
787
		appendStringInfo(str, "<>");
788
	else
789
		_outDatum(str, node->constvalue, node->constlen, node->constbyval);
790 791 792
}

/*
B
Bruce Momjian 已提交
793
 *	Aggref
794 795
 */
static void
796
_outAggref(StringInfo str, Aggref *node)
797
{
798 799
	appendStringInfo(str, " AGGREG :aggfnoid %u :aggtype %u :target ",
					 node->aggfnoid, node->aggtype);
800
	_outNode(str, node->target);
M
 
Marc G. Fournier 已提交
801

802
	appendStringInfo(str, " :aggstar %s :aggdistinct %s ",
803 804
					 booltostr(node->aggstar),
					 booltostr(node->aggdistinct));
805
	/* aggno is not dumped */
806 807
}

808 809 810 811 812 813
/*
 *	SubLink
 */
static void
_outSubLink(StringInfo str, SubLink *node)
{
B
Bruce Momjian 已提交
814 815 816
	appendStringInfo(str,
					 " SUBLINK :subLinkType %d :useor %s :lefthand ",
					 node->subLinkType,
817
					 booltostr(node->useor));
818
	_outNode(str, node->lefthand);
M
 
Marc G. Fournier 已提交
819

820
	appendStringInfo(str, " :oper ");
B
Bruce Momjian 已提交
821
	_outNode(str, node->oper);
M
 
Marc G. Fournier 已提交
822

823 824 825 826
	appendStringInfo(str, " :subselect ");
	_outNode(str, node->subselect);
}

827
/*
828
 *	ArrayRef is a subclass of Expr
829 830
 */
static void
B
Bruce Momjian 已提交
831
_outArrayRef(StringInfo str, ArrayRef *node)
832
{
B
Bruce Momjian 已提交
833
	appendStringInfo(str,
T
Tom Lane 已提交
834
		" ARRAYREF :refelemtype %u :refattrlength %d :refelemlength %d ",
B
Bruce Momjian 已提交
835 836 837
					 node->refelemtype,
					 node->refattrlength,
					 node->refelemlength);
M
 
Marc G. Fournier 已提交
838

B
Bruce Momjian 已提交
839 840
	appendStringInfo(str, " :refelembyval %c :refupperindex ",
					 node->refelembyval ? 't' : 'f');
841 842
	_outNode(str, node->refupperindexpr);

843
	appendStringInfo(str, " :reflowerindex ");
844 845
	_outNode(str, node->reflowerindexpr);

846
	appendStringInfo(str, " :refexpr ");
847 848
	_outNode(str, node->refexpr);

849
	appendStringInfo(str, " :refassgnexpr ");
850
	_outNode(str, node->refassgnexpr);
851 852 853
}

/*
854
 *	Func is a subclass of Expr
855 856
 */
static void
857
_outFunc(StringInfo str, Func *node)
858
{
859 860
	appendStringInfo(str,
					 " FUNC :funcid %u :funcresulttype %u :funcretset %s ",
B
Bruce Momjian 已提交
861
					 node->funcid,
862 863
					 node->funcresulttype,
					 booltostr(node->funcretset));
864 865 866
}

/*
867
 *	Oper is a subclass of Expr
868 869
 */
static void
870
_outOper(StringInfo str, Oper *node)
871
{
B
Bruce Momjian 已提交
872
	appendStringInfo(str,
873
					 " OPER :opno %u :opid %u :opresulttype %u :opretset %s ",
B
Bruce Momjian 已提交
874 875
					 node->opno,
					 node->opid,
876 877
					 node->opresulttype,
					 booltostr(node->opretset));
878 879 880
}

/*
881
 *	Param is a subclass of Expr
882 883
 */
static void
884
_outParam(StringInfo str, Param *node)
885
{
886
	appendStringInfo(str, " PARAM :paramkind %d :paramid %d :paramname ",
B
Bruce Momjian 已提交
887
					 node->paramkind,
888 889
					 node->paramid);
	_outToken(str, node->paramname);
890
	appendStringInfo(str, " :paramtype %u ", node->paramtype);
891 892
}

893 894 895 896 897 898 899 900 901 902
/*
 *	FieldSelect
 */
static void
_outFieldSelect(StringInfo str, FieldSelect *node)
{
	appendStringInfo(str, " FIELDSELECT :arg ");
	_outNode(str, node->arg);

	appendStringInfo(str, " :fieldnum %d :resulttype %u :resulttypmod %d ",
B
Bruce Momjian 已提交
903
				   node->fieldnum, node->resulttype, node->resulttypmod);
904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928
}

/*
 *	RelabelType
 */
static void
_outRelabelType(StringInfo str, RelabelType *node)
{
	appendStringInfo(str, " RELABELTYPE :arg ");
	_outNode(str, node->arg);

	appendStringInfo(str, " :resulttype %u :resulttypmod %d ",
					 node->resulttype, node->resulttypmod);
}

/*
 *	RangeTblRef
 */
static void
_outRangeTblRef(StringInfo str, RangeTblRef *node)
{
	appendStringInfo(str, " RANGETBLREF %d ",
					 node->rtindex);
}

929 930 931 932 933 934 935 936 937 938 939 940
/*
 *	FromExpr
 */
static void
_outFromExpr(StringInfo str, FromExpr *node)
{
	appendStringInfo(str, " FROMEXPR :fromlist ");
	_outNode(str, node->fromlist);
	appendStringInfo(str, " :quals ");
	_outNode(str, node->quals);
}

941 942 943 944 945 946 947 948
/*
 *	JoinExpr
 */
static void
_outJoinExpr(StringInfo str, JoinExpr *node)
{
	appendStringInfo(str, " JOINEXPR :jointype %d :isNatural %s :larg ",
					 (int) node->jointype,
949
					 booltostr(node->isNatural));
950 951 952 953 954 955 956 957 958
	_outNode(str, node->larg);
	appendStringInfo(str, " :rarg ");
	_outNode(str, node->rarg);
	appendStringInfo(str, " :using ");
	_outNode(str, node->using);
	appendStringInfo(str, " :quals ");
	_outNode(str, node->quals);
	appendStringInfo(str, " :alias ");
	_outNode(str, node->alias);
959
	appendStringInfo(str, " :rtindex %d ", node->rtindex);
960 961
}

962
/*
963
 *	TargetEntry is a subclass of Node.
964 965
 */
static void
966
_outTargetEntry(StringInfo str, TargetEntry *node)
967
{
M
 
Marc G. Fournier 已提交
968
	appendStringInfo(str, " TARGETENTRY :resdom ");
969 970
	_outNode(str, node->resdom);

971 972
	appendStringInfo(str, " :expr ");
	_outNode(str, node->expr);
973
}
974

975 976 977 978 979 980 981 982 983
static void
_outAlias(StringInfo str, Alias *node)
{
	appendStringInfo(str, " ALIAS :aliasname ");
	_outToken(str, node->aliasname);
	appendStringInfo(str, " :colnames ");
	_outNode(str, node->colnames);
}

984
static void
985
_outRangeTblEntry(StringInfo str, RangeTblEntry *node)
986
{
987 988
	/* put alias + eref first to make dump more legible */
	appendStringInfo(str, " RTE :alias ");
989 990 991
	_outNode(str, node->alias);
	appendStringInfo(str, " :eref ");
	_outNode(str, node->eref);
992 993 994 995 996 997
	appendStringInfo(str, " :rtekind %d ",
					 (int) node->rtekind);
	switch (node->rtekind)
	{
		case RTE_RELATION:
		case RTE_SPECIAL:
998
			appendStringInfo(str, ":relid %u", node->relid);
999 1000 1001 1002 1003
			break;
		case RTE_SUBQUERY:
			appendStringInfo(str, ":subquery ");
			_outNode(str, node->subquery);
			break;
1004 1005 1006
		case RTE_FUNCTION:
			appendStringInfo(str, ":funcexpr ");
			_outNode(str, node->funcexpr);
1007 1008
			appendStringInfo(str, ":coldeflist ");
			_outNode(str, node->coldeflist);
1009
			break;
1010
		case RTE_JOIN:
1011
			appendStringInfo(str, ":jointype %d :joinaliasvars ",
1012
							 (int) node->jointype);
1013
			_outNode(str, node->joinaliasvars);
1014 1015 1016 1017 1018
			break;
		default:
			elog(ERROR, "bogus rte kind %d", (int) node->rtekind);
			break;
	}
1019 1020
	appendStringInfo(str, " :inh %s :inFromCl %s :checkForRead %s"
					 " :checkForWrite %s :checkAsUser %u",
1021 1022 1023 1024
					 booltostr(node->inh),
					 booltostr(node->inFromCl),
					 booltostr(node->checkForRead),
					 booltostr(node->checkForWrite),
1025
					 node->checkAsUser);
1026 1027
}

1028
/*
1029
 *	Path is a subclass of Node.
1030 1031
 */
static void
1032
_outPath(StringInfo str, Path *node)
1033
{
1034
	appendStringInfo(str,
1035
	 " PATH :pathtype %d :startup_cost %.2f :total_cost %.2f :pathkeys ",
B
Bruce Momjian 已提交
1036
					 node->pathtype,
1037 1038
					 node->startup_cost,
					 node->total_cost);
1039
	_outNode(str, node->pathkeys);
1040 1041 1042
}

/*
1043
 *	IndexPath is a subclass of Path.
1044 1045
 */
static void
1046
_outIndexPath(StringInfo str, IndexPath *node)
1047
{
B
Bruce Momjian 已提交
1048
	appendStringInfo(str,
1049
					 " INDEXPATH :pathtype %d :startup_cost %.2f :total_cost %.2f :pathkeys ",
B
Bruce Momjian 已提交
1050
					 node->path.pathtype,
1051 1052
					 node->path.startup_cost,
					 node->path.total_cost);
1053
	_outNode(str, node->path.pathkeys);
1054

1055 1056
	appendStringInfo(str, " :indexinfo ");
	_outNode(str, node->indexinfo);
1057

1058
	appendStringInfo(str, " :indexqual ");
1059
	_outNode(str, node->indexqual);
1060

1061 1062
	appendStringInfo(str, " :indexscandir %d :joinrelids ",
					 (int) node->indexscandir);
1063
	_outIntList(str, node->joinrelids);
1064

1065
	appendStringInfo(str, " :alljoinquals %s :rows %.2f ",
1066
					 booltostr(node->alljoinquals),
1067
					 node->rows);
1068 1069
}

1070 1071 1072 1073 1074 1075 1076
/*
 *	TidPath is a subclass of Path.
 */
static void
_outTidPath(StringInfo str, TidPath *node)
{
	appendStringInfo(str,
1077
					 " TIDPATH :pathtype %d :startup_cost %.2f :total_cost %.2f :pathkeys ",
1078
					 node->path.pathtype,
1079 1080
					 node->path.startup_cost,
					 node->path.total_cost);
1081 1082 1083 1084 1085
	_outNode(str, node->path.pathkeys);

	appendStringInfo(str, " :tideval ");
	_outNode(str, node->tideval);

1086
	appendStringInfo(str, " :unjoined_relids ");
1087 1088 1089
	_outIntList(str, node->unjoined_relids);
}

1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106
/*
 *	AppendPath is a subclass of Path.
 */
static void
_outAppendPath(StringInfo str, AppendPath *node)
{
	appendStringInfo(str,
					 " APPENDPATH :pathtype %d :startup_cost %.2f :total_cost %.2f :pathkeys ",
					 node->path.pathtype,
					 node->path.startup_cost,
					 node->path.total_cost);
	_outNode(str, node->path.pathkeys);

	appendStringInfo(str, " :subpaths ");
	_outNode(str, node->subpaths);
}

1107
/*
1108
 *	NestPath is a subclass of Path
1109 1110
 */
static void
1111
_outNestPath(StringInfo str, NestPath *node)
1112
{
B
Bruce Momjian 已提交
1113
	appendStringInfo(str,
1114
					 " NESTPATH :pathtype %d :startup_cost %.2f :total_cost %.2f :pathkeys ",
B
Bruce Momjian 已提交
1115
					 node->path.pathtype,
1116 1117
					 node->path.startup_cost,
					 node->path.total_cost);
1118
	_outNode(str, node->path.pathkeys);
1119 1120
	appendStringInfo(str, " :jointype %d :outerjoinpath ",
					 (int) node->jointype);
1121 1122 1123 1124 1125
	_outNode(str, node->outerjoinpath);
	appendStringInfo(str, " :innerjoinpath ");
	_outNode(str, node->innerjoinpath);
	appendStringInfo(str, " :joinrestrictinfo ");
	_outNode(str, node->joinrestrictinfo);
1126 1127 1128
}

/*
1129
 *	MergePath is a subclass of NestPath.
1130 1131
 */
static void
1132
_outMergePath(StringInfo str, MergePath *node)
1133
{
B
Bruce Momjian 已提交
1134
	appendStringInfo(str,
1135
					 " MERGEPATH :pathtype %d :startup_cost %.2f :total_cost %.2f :pathkeys ",
B
Bruce Momjian 已提交
1136
					 node->jpath.path.pathtype,
1137 1138
					 node->jpath.path.startup_cost,
					 node->jpath.path.total_cost);
1139
	_outNode(str, node->jpath.path.pathkeys);
1140 1141
	appendStringInfo(str, " :jointype %d :outerjoinpath ",
					 (int) node->jpath.jointype);
1142 1143 1144 1145 1146
	_outNode(str, node->jpath.outerjoinpath);
	appendStringInfo(str, " :innerjoinpath ");
	_outNode(str, node->jpath.innerjoinpath);
	appendStringInfo(str, " :joinrestrictinfo ");
	_outNode(str, node->jpath.joinrestrictinfo);
1147

1148
	appendStringInfo(str, " :path_mergeclauses ");
1149 1150
	_outNode(str, node->path_mergeclauses);

1151
	appendStringInfo(str, " :outersortkeys ");
1152 1153
	_outNode(str, node->outersortkeys);

1154
	appendStringInfo(str, " :innersortkeys ");
1155
	_outNode(str, node->innersortkeys);
1156 1157 1158
}

/*
1159
 *	HashPath is a subclass of NestPath.
1160 1161
 */
static void
1162
_outHashPath(StringInfo str, HashPath *node)
1163
{
B
Bruce Momjian 已提交
1164
	appendStringInfo(str,
1165
					 " HASHPATH :pathtype %d :startup_cost %.2f :total_cost %.2f :pathkeys ",
B
Bruce Momjian 已提交
1166
					 node->jpath.path.pathtype,
1167 1168
					 node->jpath.path.startup_cost,
					 node->jpath.path.total_cost);
1169
	_outNode(str, node->jpath.path.pathkeys);
1170 1171
	appendStringInfo(str, " :jointype %d :outerjoinpath ",
					 (int) node->jpath.jointype);
1172 1173 1174 1175 1176
	_outNode(str, node->jpath.outerjoinpath);
	appendStringInfo(str, " :innerjoinpath ");
	_outNode(str, node->jpath.innerjoinpath);
	appendStringInfo(str, " :joinrestrictinfo ");
	_outNode(str, node->jpath.joinrestrictinfo);
1177

1178
	appendStringInfo(str, " :path_hashclauses ");
1179
	_outNode(str, node->path_hashclauses);
1180 1181 1182
}

/*
1183
 *	PathKeyItem is a subclass of Node.
1184 1185
 */
static void
1186
_outPathKeyItem(StringInfo str, PathKeyItem *node)
1187
{
1188 1189 1190
	appendStringInfo(str, " PATHKEYITEM :sortop %u :key ",
					 node->sortop);
	_outNode(str, node->key);
1191 1192 1193
}

/*
1194
 *	RestrictInfo is a subclass of Node.
1195 1196
 */
static void
1197
_outRestrictInfo(StringInfo str, RestrictInfo *node)
1198
{
B
Bruce Momjian 已提交
1199
	appendStringInfo(str, " RESTRICTINFO :clause ");
1200 1201
	_outNode(str, node->clause);

1202
	appendStringInfo(str, " :ispusheddown %s :subclauseindices ",
1203
					 booltostr(node->ispusheddown));
1204
	_outNode(str, node->subclauseindices);
1205

1206 1207 1208
	appendStringInfo(str, " :mergejoinoperator %u ", node->mergejoinoperator);
	appendStringInfo(str, " :left_sortop %u ", node->left_sortop);
	appendStringInfo(str, " :right_sortop %u ", node->right_sortop);
M
 
Marc G. Fournier 已提交
1209
	appendStringInfo(str, " :hashjoinoperator %u ", node->hashjoinoperator);
1210 1211 1212
}

/*
1213
 *	JoinInfo is a subclass of Node.
1214 1215
 */
static void
1216
_outJoinInfo(StringInfo str, JoinInfo *node)
1217
{
B
Bruce Momjian 已提交
1218 1219
	appendStringInfo(str, " JINFO :unjoined_relids ");
	_outIntList(str, node->unjoined_relids);
1220

1221 1222
	appendStringInfo(str, " :jinfo_restrictinfo ");
	_outNode(str, node->jinfo_restrictinfo);
1223 1224 1225 1226 1227 1228
}

/*
 * Print the value of a Datum given its type.
 */
static void
1229
_outDatum(StringInfo str, Datum value, int typlen, bool typbyval)
1230
{
1231 1232
	Size		length,
				i;
1233
	char	   *s;
1234

1235
	length = datumGetSize(value, typbyval, typlen);
1236

1237
	if (typbyval)
1238 1239
	{
		s = (char *) (&value);
1240
		appendStringInfo(str, " %u [ ", (unsigned int) length);
1241
		for (i = 0; i < (Size) sizeof(Datum); i++)
1242
			appendStringInfo(str, "%d ", (int) (s[i]));
M
 
Marc G. Fournier 已提交
1243
		appendStringInfo(str, "] ");
1244
	}
1245
	else
1246
	{
1247 1248
		s = (char *) DatumGetPointer(value);
		if (!PointerIsValid(s))
M
 
Marc G. Fournier 已提交
1249
			appendStringInfo(str, " 0 [ ] ");
1250 1251
		else
		{
1252
			appendStringInfo(str, " %u [ ", (unsigned int) length);
1253
			for (i = 0; i < length; i++)
1254
				appendStringInfo(str, "%d ", (int) (s[i]));
M
 
Marc G. Fournier 已提交
1255
			appendStringInfo(str, "] ");
1256
		}
1257 1258 1259 1260
	}
}

static void
1261
_outStream(StringInfo str, Stream *node)
1262
{
B
Bruce Momjian 已提交
1263
	appendStringInfo(str,
B
Bruce Momjian 已提交
1264
	  " STREAM :pathptr @ %p :cinfo @ %p :clausetype %p :upstream @ %p ",
1265 1266 1267 1268
					 node->pathptr,
					 node->cinfo,
					 node->clausetype,
					 node->upstream);
1269

B
Bruce Momjian 已提交
1270
	appendStringInfo(str,
B
Bruce Momjian 已提交
1271
			 " :downstream @ %p :groupup %d :groupcost %f :groupsel %f ",
1272
					 node->downstream,
B
Bruce Momjian 已提交
1273 1274 1275
					 node->groupup,
					 node->groupcost,
					 node->groupsel);
1276
}
1277

1278 1279 1280
static void
_outAExpr(StringInfo str, A_Expr *node)
{
1281
	appendStringInfo(str, " AEXPR ");
1282 1283 1284
	switch (node->oper)
	{
		case AND:
1285
			appendStringInfo(str, "AND ");
1286 1287
			break;
		case OR:
1288
			appendStringInfo(str, "OR ");
1289 1290
			break;
		case NOT:
1291
			appendStringInfo(str, "NOT ");
1292
			break;
1293
		case OP:
1294
			_outNode(str, node->name);
1295
			appendStringInfo(str, " ");
1296
			break;
1297 1298 1299
		default:
			appendStringInfo(str, "?? ");
			break;
1300
	}
1301
	_outNode(str, node->lexpr);
1302
	appendStringInfo(str, " ");
1303 1304 1305
	_outNode(str, node->rexpr);
}

1306
static void
1307
_outValue(StringInfo str, Value *value)
1308
{
1309 1310
	switch (value->type)
	{
1311
		case T_Integer:
1312
			appendStringInfo(str, " %ld ", value->val.ival);
1313 1314
			break;
		case T_Float:
1315 1316 1317 1318

			/*
			 * We assume the value is a valid numeric literal and so does
			 * not need quoting.
1319 1320 1321 1322 1323 1324 1325
			 */
			appendStringInfo(str, " %s ", value->val.str);
			break;
		case T_String:
			appendStringInfo(str, " \"");
			_outToken(str, value->val.str);
			appendStringInfo(str, "\" ");
1326
			break;
1327
		case T_BitString:
1328 1329
			/* internal representation already has leading 'b' */
			appendStringInfo(str, " %s ", value->val.str);
1330
			break;
1331
		default:
B
Bruce Momjian 已提交
1332
			elog(WARNING, "_outValue: don't know how to print type %d ",
1333
				 value->type);
1334
			break;
1335
	}
1336 1337
}

1338
static void
1339
_outRangeVar(StringInfo str, RangeVar *node)
1340
{
1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353
	appendStringInfo(str, " RANGEVAR :relation ");
	/*
	 * we deliberately ignore catalogname here, since it is presently not
	 * semantically meaningful
	 */
	_outToken(str, node->schemaname);
	appendStringInfo(str, " . ");
	_outToken(str, node->relname);
	appendStringInfo(str, " :inhopt %d :istemp %s",
					(int) node->inhOpt,
					booltostr(node->istemp));
	appendStringInfo(str, " :alias ");
	_outNode(str, node->alias);
1354 1355
}

1356
static void
1357
_outColumnRef(StringInfo str, ColumnRef *node)
1358
{
1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378
	appendStringInfo(str, " COLUMNREF :fields ");
	_outNode(str, node->fields);
	appendStringInfo(str, " :indirection ");
	_outNode(str, node->indirection);
}

static void
_outParamRef(StringInfo str, ParamRef *node)
{
	appendStringInfo(str, " PARAMREF :number %d :fields ", node->number);
	_outNode(str, node->fields);
	appendStringInfo(str, " :indirection ");
	_outNode(str, node->indirection);
}

static void
_outIdent(StringInfo str, Ident *node)
{
	appendStringInfo(str, " IDENT ");
	_outToken(str, node->name);
1379 1380
}

1381 1382 1383
static void
_outAConst(StringInfo str, A_Const *node)
{
1384
	appendStringInfo(str, "CONST ");
1385
	_outValue(str, &(node->val));
1386 1387
	appendStringInfo(str, " :typename ");
	_outNode(str, node->typename);
1388 1389
}

1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400
static void
_outExprFieldSelect(StringInfo str, ExprFieldSelect *node)
{
	appendStringInfo(str, " EXPRFIELDSELECT :arg ");
	_outNode(str, node->arg);
	appendStringInfo(str, " :fields ");
	_outNode(str, node->fields);
	appendStringInfo(str, " :indirection ");
	_outNode(str, node->indirection);
}

T
Thomas G. Lockhart 已提交
1401 1402 1403
static void
_outConstraint(StringInfo str, Constraint *node)
{
1404
	appendStringInfo(str, " CONSTRAINT :name ");
1405 1406
	_outToken(str, node->name);
	appendStringInfo(str, " :type ");
T
Thomas G. Lockhart 已提交
1407 1408 1409 1410

	switch (node->contype)
	{
		case CONSTR_PRIMARY:
1411
			appendStringInfo(str, "PRIMARY_KEY :keys ");
T
Thomas G. Lockhart 已提交
1412 1413 1414 1415
			_outNode(str, node->keys);
			break;

		case CONSTR_CHECK:
1416
			appendStringInfo(str, "CHECK :raw ");
1417
			_outNode(str, node->raw_expr);
1418 1419
			appendStringInfo(str, " :cooked ");
			_outToken(str, node->cooked_expr);
T
Thomas G. Lockhart 已提交
1420 1421 1422
			break;

		case CONSTR_DEFAULT:
1423
			appendStringInfo(str, "DEFAULT :raw ");
1424
			_outNode(str, node->raw_expr);
1425 1426
			appendStringInfo(str, " :cooked ");
			_outToken(str, node->cooked_expr);
T
Thomas G. Lockhart 已提交
1427 1428 1429
			break;

		case CONSTR_NOTNULL:
1430
			appendStringInfo(str, "NOT_NULL");
T
Thomas G. Lockhart 已提交
1431 1432 1433
			break;

		case CONSTR_UNIQUE:
1434
			appendStringInfo(str, "UNIQUE :keys ");
T
Thomas G. Lockhart 已提交
1435 1436 1437 1438
			_outNode(str, node->keys);
			break;

		default:
1439
			appendStringInfo(str, "<unrecognized_constraint>");
T
Thomas G. Lockhart 已提交
1440 1441 1442 1443
			break;
	}
}

1444 1445 1446 1447 1448
static void
_outFkConstraint(StringInfo str, FkConstraint *node)
{
	appendStringInfo(str, " FKCONSTRAINT :constr_name ");
	_outToken(str, node->constr_name);
1449 1450
	appendStringInfo(str, " :pktable ");
	_outNode(str, node->pktable);
1451 1452 1453 1454
	appendStringInfo(str, " :fk_attrs ");
	_outNode(str, node->fk_attrs);
	appendStringInfo(str, " :pk_attrs ");
	_outNode(str, node->pk_attrs);
1455 1456 1457 1458
	appendStringInfo(str, " :fk_matchtype %c :fk_upd_action %c :fk_del_action %c :deferrable %s :initdeferred %s :skip_validation %s",
					 node->fk_matchtype,
					 node->fk_upd_action,
					 node->fk_del_action,
1459
					 booltostr(node->deferrable),
1460 1461
					 booltostr(node->initdeferred),
					 booltostr(node->skip_validation));
1462 1463
}

T
Thomas G. Lockhart 已提交
1464
static void
1465
_outCaseExpr(StringInfo str, CaseExpr *node)
T
Thomas G. Lockhart 已提交
1466
{
1467 1468 1469 1470 1471
	appendStringInfo(str, " CASE :casetype %u :arg ",
					 node->casetype);
	_outNode(str, node->arg);

	appendStringInfo(str, " :args ");
T
Thomas G. Lockhart 已提交
1472
	_outNode(str, node->args);
M
 
Marc G. Fournier 已提交
1473

1474
	appendStringInfo(str, " :defresult ");
T
Thomas G. Lockhart 已提交
1475 1476 1477 1478
	_outNode(str, node->defresult);
}

static void
1479
_outCaseWhen(StringInfo str, CaseWhen *node)
T
Thomas G. Lockhart 已提交
1480
{
1481
	appendStringInfo(str, " WHEN ");
T
Thomas G. Lockhart 已提交
1482
	_outNode(str, node->expr);
M
 
Marc G. Fournier 已提交
1483

1484
	appendStringInfo(str, " :then ");
T
Thomas G. Lockhart 已提交
1485 1486 1487
	_outNode(str, node->result);
}

1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513
/*
 *	NullTest
 */
static void
_outNullTest(StringInfo str, NullTest *node)
{
	appendStringInfo(str, " NULLTEST :arg ");
	_outNode(str, node->arg);

	appendStringInfo(str, " :nulltesttype %d ",
					 (int) node->nulltesttype);
}

/*
 *	BooleanTest
 */
static void
_outBooleanTest(StringInfo str, BooleanTest *node)
{
	appendStringInfo(str, " BOOLEANTEST :arg ");
	_outNode(str, node->arg);

	appendStringInfo(str, " :booltesttype %d ",
					 (int) node->booltesttype);
}

1514 1515
/*
 * _outNode -
1516
 *	  converts a Node into ascii string and append it to 'str'
1517 1518 1519 1520
 */
static void
_outNode(StringInfo str, void *obj)
{
1521 1522
	if (obj == NULL)
	{
B
Bruce Momjian 已提交
1523
		appendStringInfo(str, "<>");
1524 1525
		return;
	}
1526

1527
	if (IsA(obj, List))
1528
	{
1529
		List	   *l;
1530

1531
		appendStringInfoChar(str, '(');
1532 1533 1534 1535
		foreach(l, (List *) obj)
		{
			_outNode(str, lfirst(l));
			if (lnext(l))
1536
				appendStringInfoChar(str, ' ');
1537
		}
1538
		appendStringInfoChar(str, ')');
1539
	}
1540
	else if (IsA(obj, Integer) || IsA(obj, Float) || IsA(obj, String) || IsA(obj, BitString))
1541 1542 1543 1544
	{
		/* nodeRead does not want to see { } around these! */
		_outValue(str, obj);
	}
1545 1546
	else
	{
1547
		appendStringInfoChar(str, '{');
1548 1549
		switch (nodeTag(obj))
		{
1550 1551 1552 1553 1554 1555
			case T_CreateStmt:
				_outCreateStmt(str, obj);
				break;
			case T_IndexStmt:
				_outIndexStmt(str, obj);
				break;
1556 1557 1558 1559 1560 1561
			case T_NotifyStmt:
				_outNotifyStmt(str, obj);
				break;
			case T_SelectStmt:
				_outSelectStmt(str, obj);
				break;
1562 1563 1564
			case T_ColumnDef:
				_outColumnDef(str, obj);
				break;
1565 1566 1567
			case T_TypeName:
				_outTypeName(str, obj);
				break;
1568 1569 1570
			case T_TypeCast:
				_outTypeCast(str, obj);
				break;
1571 1572 1573
			case T_IndexElem:
				_outIndexElem(str, obj);
				break;
1574 1575 1576
			case T_Query:
				_outQuery(str, obj);
				break;
1577 1578 1579 1580 1581
			case T_SortClause:
				_outSortClause(str, obj);
				break;
			case T_GroupClause:
				_outGroupClause(str, obj);
1582
				break;
1583 1584 1585
			case T_SetOperationStmt:
				_outSetOperationStmt(str, obj);
				break;
1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615
			case T_Plan:
				_outPlan(str, obj);
				break;
			case T_Result:
				_outResult(str, obj);
				break;
			case T_Append:
				_outAppend(str, obj);
				break;
			case T_Join:
				_outJoin(str, obj);
				break;
			case T_NestLoop:
				_outNestLoop(str, obj);
				break;
			case T_MergeJoin:
				_outMergeJoin(str, obj);
				break;
			case T_HashJoin:
				_outHashJoin(str, obj);
				break;
			case T_Scan:
				_outScan(str, obj);
				break;
			case T_SeqScan:
				_outSeqScan(str, obj);
				break;
			case T_IndexScan:
				_outIndexScan(str, obj);
				break;
1616 1617 1618
			case T_TidScan:
				_outTidScan(str, obj);
				break;
1619 1620 1621
			case T_SubqueryScan:
				_outSubqueryScan(str, obj);
				break;
1622 1623 1624
			case T_FunctionScan:
				_outFunctionScan(str, obj);
				break;
1625 1626 1627
			case T_Material:
				_outMaterial(str, obj);
				break;
1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639
			case T_Sort:
				_outSort(str, obj);
				break;
			case T_Agg:
				_outAgg(str, obj);
				break;
			case T_Group:
				_outGroup(str, obj);
				break;
			case T_Unique:
				_outUnique(str, obj);
				break;
1640 1641 1642
			case T_SetOp:
				_outSetOp(str, obj);
				break;
1643 1644 1645
			case T_Limit:
				_outLimit(str, obj);
				break;
1646 1647 1648
			case T_Hash:
				_outHash(str, obj);
				break;
V
Vadim B. Mikheev 已提交
1649 1650 1651
			case T_SubPlan:
				_outSubPlan(str, obj);
				break;
1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666
			case T_Resdom:
				_outResdom(str, obj);
				break;
			case T_Fjoin:
				_outFjoin(str, obj);
				break;
			case T_Expr:
				_outExpr(str, obj);
				break;
			case T_Var:
				_outVar(str, obj);
				break;
			case T_Const:
				_outConst(str, obj);
				break;
B
Bruce Momjian 已提交
1667 1668
			case T_Aggref:
				_outAggref(str, obj);
1669
				break;
1670 1671 1672
			case T_SubLink:
				_outSubLink(str, obj);
				break;
1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684
			case T_ArrayRef:
				_outArrayRef(str, obj);
				break;
			case T_Func:
				_outFunc(str, obj);
				break;
			case T_Oper:
				_outOper(str, obj);
				break;
			case T_Param:
				_outParam(str, obj);
				break;
1685 1686 1687 1688 1689 1690 1691 1692 1693
			case T_FieldSelect:
				_outFieldSelect(str, obj);
				break;
			case T_RelabelType:
				_outRelabelType(str, obj);
				break;
			case T_RangeTblRef:
				_outRangeTblRef(str, obj);
				break;
1694 1695 1696
			case T_FromExpr:
				_outFromExpr(str, obj);
				break;
1697 1698 1699
			case T_JoinExpr:
				_outJoinExpr(str, obj);
				break;
1700 1701 1702
			case T_TargetEntry:
				_outTargetEntry(str, obj);
				break;
1703 1704 1705
			case T_Alias:
				_outAlias(str, obj);
				break;
1706 1707 1708 1709 1710 1711 1712 1713 1714
			case T_RangeTblEntry:
				_outRangeTblEntry(str, obj);
				break;
			case T_Path:
				_outPath(str, obj);
				break;
			case T_IndexPath:
				_outIndexPath(str, obj);
				break;
1715 1716 1717
			case T_TidPath:
				_outTidPath(str, obj);
				break;
1718 1719 1720
			case T_AppendPath:
				_outAppendPath(str, obj);
				break;
1721 1722
			case T_NestPath:
				_outNestPath(str, obj);
1723 1724 1725 1726 1727 1728 1729
				break;
			case T_MergePath:
				_outMergePath(str, obj);
				break;
			case T_HashPath:
				_outHashPath(str, obj);
				break;
1730 1731
			case T_PathKeyItem:
				_outPathKeyItem(str, obj);
1732
				break;
1733 1734
			case T_RestrictInfo:
				_outRestrictInfo(str, obj);
1735
				break;
1736 1737
			case T_JoinInfo:
				_outJoinInfo(str, obj);
1738 1739 1740 1741
				break;
			case T_Stream:
				_outStream(str, obj);
				break;
1742 1743 1744
			case T_A_Expr:
				_outAExpr(str, obj);
				break;
1745 1746 1747 1748 1749 1750 1751 1752 1753
			case T_RangeVar:
				_outRangeVar(str, obj);
				break;
			case T_ColumnRef:
				_outColumnRef(str, obj);
				break;
			case T_ParamRef:
				_outParamRef(str, obj);
				break;
1754 1755 1756 1757 1758 1759
			case T_Ident:
				_outIdent(str, obj);
				break;
			case T_A_Const:
				_outAConst(str, obj);
				break;
1760 1761 1762
			case T_ExprFieldSelect:
				_outExprFieldSelect(str, obj);
				break;
T
Thomas G. Lockhart 已提交
1763 1764 1765
			case T_Constraint:
				_outConstraint(str, obj);
				break;
1766 1767 1768
			case T_FkConstraint:
				_outFkConstraint(str, obj);
				break;
T
Thomas G. Lockhart 已提交
1769 1770 1771 1772 1773 1774
			case T_CaseExpr:
				_outCaseExpr(str, obj);
				break;
			case T_CaseWhen:
				_outCaseWhen(str, obj);
				break;
1775 1776 1777 1778 1779 1780
			case T_NullTest:
				_outNullTest(str, obj);
				break;
			case T_BooleanTest:
				_outBooleanTest(str, obj);
				break;
1781 1782 1783 1784
			case T_FuncCall:
				_outFuncCall(str, obj);
				break;

1785
			default:
B
Bruce Momjian 已提交
1786
				elog(WARNING, "_outNode: don't know how to print type %d ",
1787 1788
					 nodeTag(obj));
				break;
1789
		}
1790
		appendStringInfoChar(str, '}');
1791 1792 1793 1794 1795
	}
}

/*
 * nodeToString -
1796
 *	   returns the ascii representation of the Node as a palloc'd string
1797
 */
1798
char *
1799 1800
nodeToString(void *obj)
{
B
Bruce Momjian 已提交
1801
	StringInfoData str;
1802

1803 1804 1805 1806
	/* see stringinfo.h for an explanation of this maneuver */
	initStringInfo(&str);
	_outNode(&str, obj);
	return str.data;
1807
}