copyfuncs.c 52.9 KB
Newer Older
1
/*-------------------------------------------------------------------------
2
 *
3
 * copyfuncs.c
4
 *	  Copy functions for Postgres tree nodes.
5
 *
6 7 8 9 10 11
 * NOTE: we currently support copying all node types found in parse and
 * plan trees.  We do not support copying executor state trees; there
 * is no need for that, and no point in maintaining all the code that
 * would be needed.  We also do not support copying Path trees, mainly
 * because the circular linkages between RelOptInfo and Path nodes can't
 * be handled easily in a simple depth-first traversal.
12 13
 *
 *
B
Bruce Momjian 已提交
14
 * Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
B
Add:  
Bruce Momjian 已提交
15
 * Portions Copyright (c) 1994, Regents of the University of California
16 17
 *
 * IDENTIFICATION
18
 *	  $Header: /cvsroot/pgsql/src/backend/nodes/copyfuncs.c,v 1.238 2003/01/23 23:38:56 petere Exp $
19 20 21 22 23 24
 *
 *-------------------------------------------------------------------------
 */

#include "postgres.h"

25 26 27
#include "nodes/parsenodes.h"
#include "nodes/plannodes.h"
#include "nodes/relation.h"
28
#include "utils/datum.h"
29 30 31


/*
32 33 34 35
 * Macros to simplify copying of different kinds of fields.  Use these
 * wherever possible to reduce the chance for silly typos.  Note that these
 * hard-wire the convention that the local variables in a Copy routine are
 * named 'newnode' and 'from'.
36
 */
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61

/* Copy a simple scalar field (int, float, bool, enum, etc) */
#define COPY_SCALAR_FIELD(fldname) \
	(newnode->fldname = from->fldname)

/* Copy a field that is a pointer to some kind of Node or Node tree */
#define COPY_NODE_FIELD(fldname) \
	(newnode->fldname = copyObject(from->fldname))

/* Copy a field that is a pointer to a list of integers */
#define COPY_INTLIST_FIELD(fldname) \
	(newnode->fldname = listCopy(from->fldname))

/* Copy a field that is a pointer to a C string, or perhaps NULL */
#define COPY_STRING_FIELD(fldname) \
	(newnode->fldname = from->fldname ? pstrdup(from->fldname) : (char *) NULL)

/* Copy a field that is a pointer to a simple palloc'd object of size sz */
#define COPY_POINTER_FIELD(fldname, sz) \
	do { \
		Size	_size = (sz); \
		newnode->fldname = palloc(_size); \
		memcpy(newnode->fldname, from->fldname, _size); \
	} while (0)

62 63

/*
64
 * listCopy
65 66 67 68 69 70 71 72 73
 *	  This copy function only copies the "cons-cells" of the list, not the
 *	  pointed-to objects.  (Use copyObject if you want a "deep" copy.)
 *
 *	  We also use this function for copying lists of integers, which is
 *	  grotty but unlikely to break --- it could fail if sizeof(pointer)
 *	  is less than sizeof(int), but I don't know any such machines...
 *
 *	  Note that copyObject will surely coredump if applied to a list
 *	  of integers!
74
 */
75
List *
76
listCopy(List *list)
77
{
78 79 80 81 82 83 84
	List	   *newlist,
			   *l,
			   *nl;

	/* rather ugly coding for speed... */
	if (list == NIL)
		return NIL;
85

86
	newlist = nl = makeList1(lfirst(list));
87 88

	foreach(l, lnext(list))
89
	{
90
		lnext(nl) = makeList1(lfirst(l));
91
		nl = lnext(nl);
92
	}
93
	return newlist;
94
}
95

96
/* ****************************************************************
97
 *					 plannodes.h copy functions
98 99 100
 * ****************************************************************
 */

101 102
/*
 * CopyPlanFields
103
 *
104 105
 *		This function copies the fields of the Plan node.  It is used by
 *		all the copy functions for classes which inherit from Plan.
106 107
 */
static void
108
CopyPlanFields(Plan *from, Plan *newnode)
109
{
110 111 112 113 114 115 116 117
	COPY_SCALAR_FIELD(startup_cost);
	COPY_SCALAR_FIELD(total_cost);
	COPY_SCALAR_FIELD(plan_rows);
	COPY_SCALAR_FIELD(plan_width);
	COPY_NODE_FIELD(targetlist);
	COPY_NODE_FIELD(qual);
	COPY_NODE_FIELD(lefttree);
	COPY_NODE_FIELD(righttree);
118
	COPY_NODE_FIELD(initPlan);
119 120 121
	COPY_INTLIST_FIELD(extParam);
	COPY_INTLIST_FIELD(locParam);
	COPY_SCALAR_FIELD(nParamExec);
122 123
}

124 125
/*
 * _copyPlan
126
 */
127
static Plan *
128
_copyPlan(Plan *from)
129
{
130
	Plan	   *newnode = makeNode(Plan);
131

132
	/*
133
	 * copy node superclass fields
134 135 136 137
	 */
	CopyPlanFields(from, newnode);

	return newnode;
138 139 140
}


141 142
/*
 * _copyResult
143
 */
144
static Result *
145
_copyResult(Result *from)
146
{
147
	Result	   *newnode = makeNode(Result);
148

149 150
	/*
	 * copy node superclass fields
151 152 153
	 */
	CopyPlanFields((Plan *) from, (Plan *) newnode);

154 155
	/*
	 * copy remainder of node
156
	 */
157
	COPY_NODE_FIELD(resconstantqual);
158 159

	return newnode;
160 161
}

162 163
/*
 * _copyAppend
164
 */
165
static Append *
B
Bruce Momjian 已提交
166
_copyAppend(Append *from)
167
{
168
	Append	   *newnode = makeNode(Append);
169

170 171
	/*
	 * copy node superclass fields
172 173 174
	 */
	CopyPlanFields((Plan *) from, (Plan *) newnode);

175 176
	/*
	 * copy remainder of node
177
	 */
178 179
	COPY_NODE_FIELD(appendplans);
	COPY_SCALAR_FIELD(isTarget);
180 181

	return newnode;
182 183 184
}


185 186
/*
 * CopyScanFields
187
 *
188 189
 *		This function copies the fields of the Scan node.  It is used by
 *		all the copy functions for classes which inherit from Scan.
190 191
 */
static void
192
CopyScanFields(Scan *from, Scan *newnode)
193
{
194 195 196
	CopyPlanFields((Plan *) from, (Plan *) newnode);

	COPY_SCALAR_FIELD(scanrelid);
197 198
}

199 200
/*
 * _copyScan
201
 */
202
static Scan *
203
_copyScan(Scan *from)
204
{
205
	Scan	   *newnode = makeNode(Scan);
206

207 208
	/*
	 * copy node superclass fields
209
	 */
B
Bruce Momjian 已提交
210
	CopyScanFields((Scan *) from, (Scan *) newnode);
211 212

	return newnode;
213 214
}

215 216
/*
 * _copySeqScan
217 218
 */
static SeqScan *
219
_copySeqScan(SeqScan *from)
220
{
221
	SeqScan    *newnode = makeNode(SeqScan);
222

223 224
	/*
	 * copy node superclass fields
225 226 227 228
	 */
	CopyScanFields((Scan *) from, (Scan *) newnode);

	return newnode;
229 230
}

231 232
/*
 * _copyIndexScan
233 234
 */
static IndexScan *
235
_copyIndexScan(IndexScan *from)
236
{
237
	IndexScan  *newnode = makeNode(IndexScan);
238

239 240
	/*
	 * copy node superclass fields
241 242 243
	 */
	CopyScanFields((Scan *) from, (Scan *) newnode);

244 245
	/*
	 * copy remainder of node
246
	 */
247 248 249 250
	COPY_INTLIST_FIELD(indxid);
	COPY_NODE_FIELD(indxqual);
	COPY_NODE_FIELD(indxqualorig);
	COPY_SCALAR_FIELD(indxorderdir);
251 252

	return newnode;
253 254
}

255 256
/*
 * _copyTidScan
257 258 259 260
 */
static TidScan *
_copyTidScan(TidScan *from)
{
261
	TidScan    *newnode = makeNode(TidScan);
262

263 264
	/*
	 * copy node superclass fields
265 266
	 */
	CopyScanFields((Scan *) from, (Scan *) newnode);
267 268 269

	/*
	 * copy remainder of node
270
	 */
271 272
	COPY_NODE_FIELD(tideval);

273 274 275
	return newnode;
}

276 277
/*
 * _copySubqueryScan
278 279 280 281
 */
static SubqueryScan *
_copySubqueryScan(SubqueryScan *from)
{
B
Bruce Momjian 已提交
282
	SubqueryScan *newnode = makeNode(SubqueryScan);
283

284 285
	/*
	 * copy node superclass fields
286 287 288
	 */
	CopyScanFields((Scan *) from, (Scan *) newnode);

289 290
	/*
	 * copy remainder of node
291
	 */
292
	COPY_NODE_FIELD(subplan);
293 294 295 296

	return newnode;
}

297 298
/*
 * _copyFunctionScan
299 300 301 302 303 304 305 306 307 308 309 310 311
 */
static FunctionScan *
_copyFunctionScan(FunctionScan *from)
{
	FunctionScan *newnode = makeNode(FunctionScan);

	/*
	 * copy node superclass fields
	 */
	CopyScanFields((Scan *) from, (Scan *) newnode);

	return newnode;
}
312

313 314
/*
 * CopyJoinFields
315
 *
316 317
 *		This function copies the fields of the Join node.  It is used by
 *		all the copy functions for classes which inherit from Join.
318 319
 */
static void
320
CopyJoinFields(Join *from, Join *newnode)
321
{
322 323 324 325
	CopyPlanFields((Plan *) from, (Plan *) newnode);

	COPY_SCALAR_FIELD(jointype);
	COPY_NODE_FIELD(joinqual);
326 327 328
}


329 330
/*
 * _copyJoin
331
 */
332
static Join *
333
_copyJoin(Join *from)
334
{
335
	Join	   *newnode = makeNode(Join);
336

337 338
	/*
	 * copy node superclass fields
339 340 341 342
	 */
	CopyJoinFields(from, newnode);

	return newnode;
343 344 345
}


346 347
/*
 * _copyNestLoop
348 349
 */
static NestLoop *
350
_copyNestLoop(NestLoop *from)
351
{
352
	NestLoop   *newnode = makeNode(NestLoop);
353

354 355
	/*
	 * copy node superclass fields
356 357 358 359
	 */
	CopyJoinFields((Join *) from, (Join *) newnode);

	return newnode;
360 361 362
}


363 364
/*
 * _copyMergeJoin
365 366
 */
static MergeJoin *
367
_copyMergeJoin(MergeJoin *from)
368
{
369
	MergeJoin  *newnode = makeNode(MergeJoin);
370

371 372
	/*
	 * copy node superclass fields
373 374 375
	 */
	CopyJoinFields((Join *) from, (Join *) newnode);

376 377
	/*
	 * copy remainder of node
378
	 */
379
	COPY_NODE_FIELD(mergeclauses);
380 381

	return newnode;
382 383
}

384 385
/*
 * _copyHashJoin
386 387
 */
static HashJoin *
388
_copyHashJoin(HashJoin *from)
389
{
390
	HashJoin   *newnode = makeNode(HashJoin);
391

392 393
	/*
	 * copy node superclass fields
394 395 396
	 */
	CopyJoinFields((Join *) from, (Join *) newnode);

397 398
	/*
	 * copy remainder of node
399
	 */
400
	COPY_NODE_FIELD(hashclauses);
401 402

	return newnode;
403 404 405
}


406 407
/*
 * _copyMaterial
408 409
 */
static Material *
410
_copyMaterial(Material *from)
411
{
412
	Material   *newnode = makeNode(Material);
413

414 415
	/*
	 * copy node superclass fields
416 417 418 419
	 */
	CopyPlanFields((Plan *) from, (Plan *) newnode);

	return newnode;
420 421 422
}


423 424
/*
 * _copySort
425
 */
426
static Sort *
427
_copySort(Sort *from)
428
{
429
	Sort	   *newnode = makeNode(Sort);
430

431 432
	/*
	 * copy node superclass fields
433 434
	 */
	CopyPlanFields((Plan *) from, (Plan *) newnode);
435

436
	COPY_SCALAR_FIELD(keycount);
437 438

	return newnode;
439 440
}

V
Vadim B. Mikheev 已提交
441

442 443
/*
 * _copyGroup
V
Vadim B. Mikheev 已提交
444 445 446 447 448
 */
static Group *
_copyGroup(Group *from)
{
	Group	   *newnode = makeNode(Group);
449

V
Vadim B. Mikheev 已提交
450
	CopyPlanFields((Plan *) from, (Plan *) newnode);
B
Bruce Momjian 已提交
451

452 453
	COPY_SCALAR_FIELD(numCols);
	COPY_POINTER_FIELD(grpColIdx, from->numCols * sizeof(AttrNumber));
V
Vadim B. Mikheev 已提交
454 455 456 457

	return newnode;
}

458 459
/*
 * _copyAgg
460
 */
461
static Agg *
B
Bruce Momjian 已提交
462
_copyAgg(Agg *from)
463
{
464
	Agg		   *newnode = makeNode(Agg);
465 466 467

	CopyPlanFields((Plan *) from, (Plan *) newnode);

468 469
	COPY_SCALAR_FIELD(aggstrategy);
	COPY_SCALAR_FIELD(numCols);
470
	if (from->numCols > 0)
471 472
		COPY_POINTER_FIELD(grpColIdx, from->numCols * sizeof(AttrNumber));
	COPY_SCALAR_FIELD(numGroups);
473

474
	return newnode;
475 476
}

477 478
/*
 * _copyUnique
479
 */
480
static Unique *
481
_copyUnique(Unique *from)
482
{
483
	Unique	   *newnode = makeNode(Unique);
484

485 486
	/*
	 * copy node superclass fields
487 488 489
	 */
	CopyPlanFields((Plan *) from, (Plan *) newnode);

490 491
	/*
	 * copy remainder of node
492
	 */
493 494
	COPY_SCALAR_FIELD(numCols);
	COPY_POINTER_FIELD(uniqColIdx, from->numCols * sizeof(AttrNumber));
495 496

	return newnode;
497 498
}

499
/*
500
 * _copyHash
501
 */
502 503
static Hash *
_copyHash(Hash *from)
504
{
505
	Hash	   *newnode = makeNode(Hash);
506

507 508
	/*
	 * copy node superclass fields
509 510 511
	 */
	CopyPlanFields((Plan *) from, (Plan *) newnode);

512 513
	/*
	 * copy remainder of node
514
	 */
515
	COPY_NODE_FIELD(hashkeys);
516 517 518

	return newnode;
}
519

520
/*
521
 * _copySetOp
522
 */
523 524
static SetOp *
_copySetOp(SetOp *from)
525
{
526
	SetOp	   *newnode = makeNode(SetOp);
527

528 529
	/*
	 * copy node superclass fields
530 531 532
	 */
	CopyPlanFields((Plan *) from, (Plan *) newnode);

533 534
	/*
	 * copy remainder of node
535
	 */
536 537 538 539
	COPY_SCALAR_FIELD(cmd);
	COPY_SCALAR_FIELD(numCols);
	COPY_POINTER_FIELD(dupColIdx, from->numCols * sizeof(AttrNumber));
	COPY_SCALAR_FIELD(flagColIdx);
540 541 542 543

	return newnode;
}

544
/*
545
 * _copyLimit
546
 */
547 548
static Limit *
_copyLimit(Limit *from)
549
{
550
	Limit	   *newnode = makeNode(Limit);
551

552 553
	/*
	 * copy node superclass fields
554 555 556
	 */
	CopyPlanFields((Plan *) from, (Plan *) newnode);

557 558
	/*
	 * copy remainder of node
559
	 */
560 561
	COPY_NODE_FIELD(limitOffset);
	COPY_NODE_FIELD(limitCount);
562 563

	return newnode;
564 565 566
}

/* ****************************************************************
567
 *					   primnodes.h copy functions
568 569 570
 * ****************************************************************
 */

571 572
/*
 * _copyResdom
573
 */
574
static Resdom *
575
_copyResdom(Resdom *from)
576
{
577
	Resdom	   *newnode = makeNode(Resdom);
578

579 580 581 582 583 584 585 586
	COPY_SCALAR_FIELD(resno);
	COPY_SCALAR_FIELD(restype);
	COPY_SCALAR_FIELD(restypmod);
	COPY_STRING_FIELD(resname);
	COPY_SCALAR_FIELD(ressortgroupref);
	COPY_SCALAR_FIELD(reskey);
	COPY_SCALAR_FIELD(reskeyop);
	COPY_SCALAR_FIELD(resjunk);
587 588

	return newnode;
589 590
}

591 592 593
/*
 * _copyAlias
 */
594 595 596 597
static Alias *
_copyAlias(Alias *from)
{
	Alias	   *newnode = makeNode(Alias);
598

599 600
	COPY_STRING_FIELD(aliasname);
	COPY_NODE_FIELD(colnames);
601

602 603
	return newnode;
}
604

605 606 607
/*
 * _copyRangeVar
 */
608 609 610 611 612 613 614 615 616 617 618
static RangeVar *
_copyRangeVar(RangeVar *from)
{
	RangeVar   *newnode = makeNode(RangeVar);

	COPY_STRING_FIELD(catalogname);
	COPY_STRING_FIELD(schemaname);
	COPY_STRING_FIELD(relname);
	COPY_SCALAR_FIELD(inhOpt);
	COPY_SCALAR_FIELD(istemp);
	COPY_NODE_FIELD(alias);
619 620

	return newnode;
621 622
}

623
/*
624 625 626 627
 * We don't need a _copyExpr because Expr is an abstract supertype which
 * should never actually get instantiated.  Also, since it has no common
 * fields except NodeTag, there's no need for a helper routine to factor
 * out copying the common fields...
628 629
 */

630 631
/*
 * _copyVar
632
 */
633
static Var *
634
_copyVar(Var *from)
635
{
636
	Var		   *newnode = makeNode(Var);
637

638 639 640 641 642 643 644
	COPY_SCALAR_FIELD(varno);
	COPY_SCALAR_FIELD(varattno);
	COPY_SCALAR_FIELD(vartype);
	COPY_SCALAR_FIELD(vartypmod);
	COPY_SCALAR_FIELD(varlevelsup);
	COPY_SCALAR_FIELD(varnoold);
	COPY_SCALAR_FIELD(varoattno);
645 646

	return newnode;
647 648
}

649 650
/*
 * _copyConst
651
 */
652
static Const *
653
_copyConst(Const *from)
654
{
655
	Const	   *newnode = makeNode(Const);
656

657 658
	COPY_SCALAR_FIELD(consttype);
	COPY_SCALAR_FIELD(constlen);
659

660
	if (from->constbyval || from->constisnull)
661
	{
662 663 664
		/*
		 * passed by value so just copy the datum. Also, don't try to copy
		 * struct when value is null!
665
		 */
666
		newnode->constvalue = from->constvalue;
667
	}
668
	else
669
	{
670
		/*
671
		 * passed by reference.  We need a palloc'd copy.
672
		 */
673 674 675
		newnode->constvalue = datumCopy(from->constvalue,
										from->constbyval,
										from->constlen);
676
	}
677

678 679
	COPY_SCALAR_FIELD(constisnull);
	COPY_SCALAR_FIELD(constbyval);
680 681

	return newnode;
682 683
}

684 685
/*
 * _copyParam
686
 */
687
static Param *
688
_copyParam(Param *from)
689
{
690
	Param	   *newnode = makeNode(Param);
691

692 693 694 695
	COPY_SCALAR_FIELD(paramkind);
	COPY_SCALAR_FIELD(paramid);
	COPY_STRING_FIELD(paramname);
	COPY_SCALAR_FIELD(paramtype);
696 697

	return newnode;
698 699
}

700
/*
701
 * _copyAggref
702
 */
703 704
static Aggref *
_copyAggref(Aggref *from)
705
{
706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744
	Aggref	   *newnode = makeNode(Aggref);

	COPY_SCALAR_FIELD(aggfnoid);
	COPY_SCALAR_FIELD(aggtype);
	COPY_NODE_FIELD(target);
	COPY_SCALAR_FIELD(aggstar);
	COPY_SCALAR_FIELD(aggdistinct);

	return newnode;
}

/*
 * _copyArrayRef
 */
static ArrayRef *
_copyArrayRef(ArrayRef *from)
{
	ArrayRef   *newnode = makeNode(ArrayRef);

	COPY_SCALAR_FIELD(refrestype);
	COPY_SCALAR_FIELD(refattrlength);
	COPY_SCALAR_FIELD(refelemlength);
	COPY_SCALAR_FIELD(refelembyval);
	COPY_SCALAR_FIELD(refelemalign);
	COPY_NODE_FIELD(refupperindexpr);
	COPY_NODE_FIELD(reflowerindexpr);
	COPY_NODE_FIELD(refexpr);
	COPY_NODE_FIELD(refassgnexpr);

	return newnode;
}

/*
 * _copyFuncExpr
 */
static FuncExpr *
_copyFuncExpr(FuncExpr *from)
{
	FuncExpr	   *newnode = makeNode(FuncExpr);
745

746 747 748 749
	COPY_SCALAR_FIELD(funcid);
	COPY_SCALAR_FIELD(funcresulttype);
	COPY_SCALAR_FIELD(funcretset);
	COPY_SCALAR_FIELD(funcformat);
750
	COPY_NODE_FIELD(args);
751

752
	return newnode;
753 754
}

755
/*
756
 * _copyOpExpr
757
 */
758 759
static OpExpr *
_copyOpExpr(OpExpr *from)
760
{
761
	OpExpr	   *newnode = makeNode(OpExpr);
762

763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798
	COPY_SCALAR_FIELD(opno);
	COPY_SCALAR_FIELD(opfuncid);
	COPY_SCALAR_FIELD(opresulttype);
	COPY_SCALAR_FIELD(opretset);
	COPY_NODE_FIELD(args);

	return newnode;
}

/*
 * _copyDistinctExpr
 */
static DistinctExpr *
_copyDistinctExpr(DistinctExpr *from)
{
	DistinctExpr	   *newnode = makeNode(DistinctExpr);

	COPY_SCALAR_FIELD(opno);
	COPY_SCALAR_FIELD(opfuncid);
	COPY_SCALAR_FIELD(opresulttype);
	COPY_SCALAR_FIELD(opretset);
	COPY_NODE_FIELD(args);

	return newnode;
}

/*
 * _copyBoolExpr
 */
static BoolExpr *
_copyBoolExpr(BoolExpr *from)
{
	BoolExpr	   *newnode = makeNode(BoolExpr);

	COPY_SCALAR_FIELD(boolop);
	COPY_NODE_FIELD(args);
799 800

	return newnode;
801 802
}

803 804
/*
 * _copySubLink
805 806 807 808
 */
static SubLink *
_copySubLink(SubLink *from)
{
809
	SubLink    *newnode = makeNode(SubLink);
810

811
	COPY_SCALAR_FIELD(subLinkType);
812
	COPY_SCALAR_FIELD(useOr);
813
	COPY_NODE_FIELD(lefthand);
814 815
	COPY_NODE_FIELD(operName);
	COPY_INTLIST_FIELD(operOids);
816
	COPY_NODE_FIELD(subselect);
817 818 819 820

	return newnode;
}

821
/*
822
 * _copySubPlan
823
 */
824 825
static SubPlan *
_copySubPlan(SubPlan *from)
826
{
827
	SubPlan    *newnode = makeNode(SubPlan);
828

829
	COPY_SCALAR_FIELD(subLinkType);
830
	COPY_SCALAR_FIELD(useOr);
831 832
	COPY_NODE_FIELD(exprs);
	COPY_INTLIST_FIELD(paramIds);
833 834 835
	COPY_NODE_FIELD(plan);
	COPY_SCALAR_FIELD(plan_id);
	COPY_NODE_FIELD(rtable);
836 837
	COPY_SCALAR_FIELD(useHashTable);
	COPY_SCALAR_FIELD(unknownEqFalse);
838 839 840 841 842 843 844
	COPY_INTLIST_FIELD(setParam);
	COPY_INTLIST_FIELD(parParam);
	COPY_NODE_FIELD(args);

	return newnode;
}

845 846
/*
 * _copyFieldSelect
847 848 849 850 851 852
 */
static FieldSelect *
_copyFieldSelect(FieldSelect *from)
{
	FieldSelect *newnode = makeNode(FieldSelect);

853 854 855 856
	COPY_NODE_FIELD(arg);
	COPY_SCALAR_FIELD(fieldnum);
	COPY_SCALAR_FIELD(resulttype);
	COPY_SCALAR_FIELD(resulttypmod);
857 858 859 860

	return newnode;
}

861 862
/*
 * _copyRelabelType
863 864 865 866
 */
static RelabelType *
_copyRelabelType(RelabelType *from)
{
867
	RelabelType *newnode = makeNode(RelabelType);
868

869 870 871 872
	COPY_NODE_FIELD(arg);
	COPY_SCALAR_FIELD(resulttype);
	COPY_SCALAR_FIELD(resulttypmod);
	COPY_SCALAR_FIELD(relabelformat);
873 874 875 876

	return newnode;
}

877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 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 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982
/*
 * _copyCaseExpr
 */
static CaseExpr *
_copyCaseExpr(CaseExpr *from)
{
	CaseExpr   *newnode = makeNode(CaseExpr);

	COPY_SCALAR_FIELD(casetype);
	COPY_NODE_FIELD(arg);
	COPY_NODE_FIELD(args);
	COPY_NODE_FIELD(defresult);

	return newnode;
}

/*
 * _copyCaseWhen
 */
static CaseWhen *
_copyCaseWhen(CaseWhen *from)
{
	CaseWhen   *newnode = makeNode(CaseWhen);

	COPY_NODE_FIELD(expr);
	COPY_NODE_FIELD(result);

	return newnode;
}

/*
 * _copyNullTest
 */
static NullTest *
_copyNullTest(NullTest *from)
{
	NullTest   *newnode = makeNode(NullTest);

	COPY_NODE_FIELD(arg);
	COPY_SCALAR_FIELD(nulltesttype);

	return newnode;
}

/*
 * _copyBooleanTest
 */
static BooleanTest *
_copyBooleanTest(BooleanTest *from)
{
	BooleanTest *newnode = makeNode(BooleanTest);

	COPY_NODE_FIELD(arg);
	COPY_SCALAR_FIELD(booltesttype);

	return newnode;
}

/*
 * _copyConstraintTest
 */
static ConstraintTest *
_copyConstraintTest(ConstraintTest *from)
{
	ConstraintTest *newnode = makeNode(ConstraintTest);

	COPY_NODE_FIELD(arg);
	COPY_SCALAR_FIELD(testtype);
	COPY_STRING_FIELD(name);
	COPY_STRING_FIELD(domname);
	COPY_NODE_FIELD(check_expr);

	return newnode;
}

/*
 * _copyConstraintTestValue
 */
static ConstraintTestValue *
_copyConstraintTestValue(ConstraintTestValue *from)
{
	ConstraintTestValue *newnode = makeNode(ConstraintTestValue);

	COPY_SCALAR_FIELD(typeId);
	COPY_SCALAR_FIELD(typeMod);

	return newnode;
}

/*
 * _copyTargetEntry
 */
static TargetEntry *
_copyTargetEntry(TargetEntry *from)
{
	TargetEntry *newnode = makeNode(TargetEntry);

	COPY_NODE_FIELD(resdom);
	COPY_NODE_FIELD(expr);

	return newnode;
}

/*
 * _copyRangeTblRef
 */
983 984 985 986 987
static RangeTblRef *
_copyRangeTblRef(RangeTblRef *from)
{
	RangeTblRef *newnode = makeNode(RangeTblRef);

988
	COPY_SCALAR_FIELD(rtindex);
989 990 991 992

	return newnode;
}

993 994 995
/*
 * _copyJoinExpr
 */
996 997 998
static JoinExpr *
_copyJoinExpr(JoinExpr *from)
{
B
Bruce Momjian 已提交
999
	JoinExpr   *newnode = makeNode(JoinExpr);
1000

1001 1002 1003 1004 1005 1006 1007 1008
	COPY_SCALAR_FIELD(jointype);
	COPY_SCALAR_FIELD(isNatural);
	COPY_NODE_FIELD(larg);
	COPY_NODE_FIELD(rarg);
	COPY_NODE_FIELD(using);
	COPY_NODE_FIELD(quals);
	COPY_NODE_FIELD(alias);
	COPY_SCALAR_FIELD(rtindex);
T
Thomas G. Lockhart 已提交
1009 1010 1011 1012

	return newnode;
}

1013 1014 1015
/*
 * _copyFromExpr
 */
1016 1017
static FromExpr *
_copyFromExpr(FromExpr *from)
B
Bruce Momjian 已提交
1018
{
1019
	FromExpr   *newnode = makeNode(FromExpr);
B
Bruce Momjian 已提交
1020

1021 1022
	COPY_NODE_FIELD(fromlist);
	COPY_NODE_FIELD(quals);
B
Bruce Momjian 已提交
1023 1024 1025 1026

	return newnode;
}

1027
/* ****************************************************************
1028
 *						relation.h copy functions
1029
 *
1030 1031
 * We don't support copying RelOptInfo, IndexOptInfo, or Path nodes.
 * There are some subsidiary structs that are useful to copy, though.
1032 1033 1034
 * ****************************************************************
 */

1035 1036
/*
 * _copyPathKeyItem
1037
 */
1038 1039
static PathKeyItem *
_copyPathKeyItem(PathKeyItem *from)
1040
{
1041
	PathKeyItem *newnode = makeNode(PathKeyItem);
1042

1043 1044
	COPY_NODE_FIELD(key);
	COPY_SCALAR_FIELD(sortop);
1045 1046

	return newnode;
1047 1048
}

1049 1050
/*
 * _copyRestrictInfo
1051
 */
1052
static RestrictInfo *
1053
_copyRestrictInfo(RestrictInfo *from)
1054
{
1055
	RestrictInfo *newnode = makeNode(RestrictInfo);
1056

1057 1058 1059 1060 1061
	COPY_NODE_FIELD(clause);
	COPY_SCALAR_FIELD(ispusheddown);
	COPY_NODE_FIELD(subclauseindices); /* XXX probably bad */
	COPY_SCALAR_FIELD(eval_cost);
	COPY_SCALAR_FIELD(this_selec);
1062 1063
	COPY_INTLIST_FIELD(left_relids);
	COPY_INTLIST_FIELD(right_relids);
1064 1065 1066
	COPY_SCALAR_FIELD(mergejoinoperator);
	COPY_SCALAR_FIELD(left_sortop);
	COPY_SCALAR_FIELD(right_sortop);
B
Bruce Momjian 已提交
1067 1068 1069 1070 1071

	/*
	 * Do not copy pathkeys, since they'd not be canonical in a copied
	 * query
	 */
1072 1073
	newnode->left_pathkey = NIL;
	newnode->right_pathkey = NIL;
1074 1075 1076 1077 1078 1079

	COPY_SCALAR_FIELD(left_mergescansel);
	COPY_SCALAR_FIELD(right_mergescansel);
	COPY_SCALAR_FIELD(hashjoinoperator);
	COPY_SCALAR_FIELD(left_bucketsize);
	COPY_SCALAR_FIELD(right_bucketsize);
1080 1081

	return newnode;
1082 1083
}

1084 1085
/*
 * _copyJoinInfo
1086
 */
1087
static JoinInfo *
1088
_copyJoinInfo(JoinInfo *from)
1089
{
1090
	JoinInfo   *newnode = makeNode(JoinInfo);
1091

1092 1093
	COPY_INTLIST_FIELD(unjoined_relids);
	COPY_NODE_FIELD(jinfo_restrictinfo);
1094 1095

	return newnode;
1096 1097
}

1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112
/*
 * _copyInClauseInfo
 */
static InClauseInfo *
_copyInClauseInfo(InClauseInfo *from)
{
	InClauseInfo *newnode = makeNode(InClauseInfo);

	COPY_INTLIST_FIELD(lefthand);
	COPY_INTLIST_FIELD(righthand);
	COPY_NODE_FIELD(sub_targetlist);

	return newnode;
}

1113 1114 1115
/* ****************************************************************
 *					parsenodes.h copy functions
 * ****************************************************************
1116 1117 1118
 */

static RangeTblEntry *
1119
_copyRangeTblEntry(RangeTblEntry *from)
1120
{
1121
	RangeTblEntry *newnode = makeNode(RangeTblEntry);
1122

1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136
	COPY_SCALAR_FIELD(rtekind);
	COPY_SCALAR_FIELD(relid);
	COPY_NODE_FIELD(subquery);
	COPY_NODE_FIELD(funcexpr);
	COPY_NODE_FIELD(coldeflist);
	COPY_SCALAR_FIELD(jointype);
	COPY_NODE_FIELD(joinaliasvars);
	COPY_NODE_FIELD(alias);
	COPY_NODE_FIELD(eref);
	COPY_SCALAR_FIELD(inh);
	COPY_SCALAR_FIELD(inFromCl);
	COPY_SCALAR_FIELD(checkForRead);
	COPY_SCALAR_FIELD(checkForWrite);
	COPY_SCALAR_FIELD(checkAsUser);
1137 1138 1139 1140

	return newnode;
}

1141 1142 1143
static FkConstraint *
_copyFkConstraint(FkConstraint *from)
{
B
Bruce Momjian 已提交
1144
	FkConstraint *newnode = makeNode(FkConstraint);
1145

1146 1147 1148 1149 1150 1151 1152 1153 1154 1155
	COPY_STRING_FIELD(constr_name);
	COPY_NODE_FIELD(pktable);
	COPY_NODE_FIELD(fk_attrs);
	COPY_NODE_FIELD(pk_attrs);
	COPY_SCALAR_FIELD(fk_matchtype);
	COPY_SCALAR_FIELD(fk_upd_action);
	COPY_SCALAR_FIELD(fk_del_action);
	COPY_SCALAR_FIELD(deferrable);
	COPY_SCALAR_FIELD(initdeferred);
	COPY_SCALAR_FIELD(skip_validation);
B
Bruce Momjian 已提交
1156

1157 1158 1159
	return newnode;
}

1160
static SortClause *
1161
_copySortClause(SortClause *from)
1162
{
1163
	SortClause *newnode = makeNode(SortClause);
1164

1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177
	COPY_SCALAR_FIELD(tleSortGroupRef);
	COPY_SCALAR_FIELD(sortop);

	return newnode;
}

static GroupClause *
_copyGroupClause(GroupClause *from)
{
	GroupClause *newnode = makeNode(GroupClause);

	COPY_SCALAR_FIELD(tleSortGroupRef);
	COPY_SCALAR_FIELD(sortop);
1178

1179
	return newnode;
1180 1181
}

1182 1183 1184
static A_Expr *
_copyAExpr(A_Expr *from)
{
B
Bruce Momjian 已提交
1185
	A_Expr	   *newnode = makeNode(A_Expr);
1186

1187 1188 1189 1190
	COPY_SCALAR_FIELD(oper);
	COPY_NODE_FIELD(name);
	COPY_NODE_FIELD(lexpr);
	COPY_NODE_FIELD(rexpr);
1191 1192 1193 1194

	return newnode;
}

1195 1196
static ColumnRef *
_copyColumnRef(ColumnRef *from)
1197
{
B
Bruce Momjian 已提交
1198
	ColumnRef  *newnode = makeNode(ColumnRef);
1199

1200 1201
	COPY_NODE_FIELD(fields);
	COPY_NODE_FIELD(indirection);
1202

1203
	return newnode;
1204 1205
}

1206 1207
static ParamRef *
_copyParamRef(ParamRef *from)
1208
{
B
Bruce Momjian 已提交
1209
	ParamRef   *newnode = makeNode(ParamRef);
1210

1211 1212 1213
	COPY_SCALAR_FIELD(number);
	COPY_NODE_FIELD(fields);
	COPY_NODE_FIELD(indirection);
1214 1215 1216 1217

	return newnode;
}

1218 1219 1220 1221 1222
static A_Const *
_copyAConst(A_Const *from)
{
	A_Const    *newnode = makeNode(A_Const);

1223
	/* This part must duplicate _copyValue */
1224
	COPY_SCALAR_FIELD(val.type);
1225 1226 1227
	switch (from->val.type)
	{
		case T_Integer:
1228
			COPY_SCALAR_FIELD(val.val.ival);
1229 1230 1231 1232
			break;
		case T_Float:
		case T_String:
		case T_BitString:
1233
			COPY_STRING_FIELD(val.val.str);
1234 1235 1236 1237 1238 1239 1240 1241 1242
			break;
		case T_Null:
			/* nothing to do */
			break;
		default:
			elog(ERROR, "_copyAConst: unknown node type %d", from->val.type);
			break;
	}

1243
	COPY_NODE_FIELD(typename);
1244 1245 1246 1247

	return newnode;
}

1248 1249 1250
static FuncCall *
_copyFuncCall(FuncCall *from)
{
B
Bruce Momjian 已提交
1251
	FuncCall   *newnode = makeNode(FuncCall);
1252

1253 1254 1255 1256
	COPY_NODE_FIELD(funcname);
	COPY_NODE_FIELD(args);
	COPY_SCALAR_FIELD(agg_star);
	COPY_SCALAR_FIELD(agg_distinct);
1257 1258 1259 1260 1261 1262 1263

	return newnode;
}

static A_Indices *
_copyAIndices(A_Indices *from)
{
B
Bruce Momjian 已提交
1264
	A_Indices  *newnode = makeNode(A_Indices);
1265

1266 1267
	COPY_NODE_FIELD(lidx);
	COPY_NODE_FIELD(uidx);
1268 1269 1270 1271

	return newnode;
}

1272 1273 1274
static ExprFieldSelect *
_copyExprFieldSelect(ExprFieldSelect *from)
{
B
Bruce Momjian 已提交
1275
	ExprFieldSelect *newnode = makeNode(ExprFieldSelect);
1276

1277 1278 1279
	COPY_NODE_FIELD(arg);
	COPY_NODE_FIELD(fields);
	COPY_NODE_FIELD(indirection);
1280 1281 1282 1283

	return newnode;
}

1284 1285 1286
static ResTarget *
_copyResTarget(ResTarget *from)
{
B
Bruce Momjian 已提交
1287
	ResTarget  *newnode = makeNode(ResTarget);
1288

1289 1290 1291
	COPY_STRING_FIELD(name);
	COPY_NODE_FIELD(indirection);
	COPY_NODE_FIELD(val);
1292 1293 1294 1295

	return newnode;
}

1296
static TypeName *
1297
_copyTypeName(TypeName *from)
1298
{
1299
	TypeName   *newnode = makeNode(TypeName);
1300

1301 1302 1303 1304 1305 1306 1307
	COPY_NODE_FIELD(names);
	COPY_SCALAR_FIELD(typeid);
	COPY_SCALAR_FIELD(timezone);
	COPY_SCALAR_FIELD(setof);
	COPY_SCALAR_FIELD(pct_type);
	COPY_SCALAR_FIELD(typmod);
	COPY_NODE_FIELD(arrayBounds);
1308 1309

	return newnode;
1310 1311
}

1312 1313 1314
static SortGroupBy *
_copySortGroupBy(SortGroupBy *from)
{
B
Bruce Momjian 已提交
1315
	SortGroupBy *newnode = makeNode(SortGroupBy);
1316

1317 1318
	COPY_NODE_FIELD(useOp);
	COPY_NODE_FIELD(node);
1319 1320 1321 1322 1323 1324 1325

	return newnode;
}

static RangeSubselect *
_copyRangeSubselect(RangeSubselect *from)
{
B
Bruce Momjian 已提交
1326
	RangeSubselect *newnode = makeNode(RangeSubselect);
1327

1328 1329
	COPY_NODE_FIELD(subquery);
	COPY_NODE_FIELD(alias);
1330 1331 1332 1333

	return newnode;
}

1334 1335 1336
static RangeFunction *
_copyRangeFunction(RangeFunction *from)
{
B
Bruce Momjian 已提交
1337
	RangeFunction *newnode = makeNode(RangeFunction);
1338

1339 1340 1341
	COPY_NODE_FIELD(funccallnode);
	COPY_NODE_FIELD(alias);
	COPY_NODE_FIELD(coldeflist);
1342 1343 1344 1345

	return newnode;
}

1346 1347 1348 1349 1350
static TypeCast *
_copyTypeCast(TypeCast *from)
{
	TypeCast   *newnode = makeNode(TypeCast);

1351 1352
	COPY_NODE_FIELD(arg);
	COPY_NODE_FIELD(typename);
1353 1354 1355 1356

	return newnode;
}

1357 1358 1359
static IndexElem *
_copyIndexElem(IndexElem *from)
{
B
Bruce Momjian 已提交
1360
	IndexElem  *newnode = makeNode(IndexElem);
1361

1362 1363 1364 1365
	COPY_STRING_FIELD(name);
	COPY_NODE_FIELD(funcname);
	COPY_NODE_FIELD(args);
	COPY_NODE_FIELD(opclass);
1366 1367 1368 1369 1370 1371 1372

	return newnode;
}

static ColumnDef *
_copyColumnDef(ColumnDef *from)
{
B
Bruce Momjian 已提交
1373
	ColumnDef  *newnode = makeNode(ColumnDef);
1374

1375 1376 1377 1378 1379 1380 1381 1382 1383
	COPY_STRING_FIELD(colname);
	COPY_NODE_FIELD(typename);
	COPY_SCALAR_FIELD(inhcount);
	COPY_SCALAR_FIELD(is_local);
	COPY_SCALAR_FIELD(is_not_null);
	COPY_NODE_FIELD(raw_default);
	COPY_STRING_FIELD(cooked_default);
	COPY_NODE_FIELD(constraints);
	COPY_NODE_FIELD(support);
1384 1385 1386 1387 1388 1389 1390

	return newnode;
}

static Constraint *
_copyConstraint(Constraint *from)
{
B
Bruce Momjian 已提交
1391
	Constraint *newnode = makeNode(Constraint);
1392

1393 1394 1395 1396 1397
	COPY_SCALAR_FIELD(contype);
	COPY_STRING_FIELD(name);
	COPY_NODE_FIELD(raw_expr);
	COPY_STRING_FIELD(cooked_expr);
	COPY_NODE_FIELD(keys);
1398 1399 1400 1401

	return newnode;
}

1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438
static DefElem *
_copyDefElem(DefElem *from)
{
	DefElem    *newnode = makeNode(DefElem);

	COPY_STRING_FIELD(defname);
	COPY_NODE_FIELD(arg);

	return newnode;
}

static Query *
_copyQuery(Query *from)
{
	Query	   *newnode = makeNode(Query);

	COPY_SCALAR_FIELD(commandType);
	COPY_SCALAR_FIELD(querySource);
	COPY_NODE_FIELD(utilityStmt);
	COPY_SCALAR_FIELD(resultRelation);
	COPY_NODE_FIELD(into);
	COPY_SCALAR_FIELD(isPortal);
	COPY_SCALAR_FIELD(isBinary);
	COPY_SCALAR_FIELD(hasAggs);
	COPY_SCALAR_FIELD(hasSubLinks);
	COPY_NODE_FIELD(rtable);
	COPY_NODE_FIELD(jointree);
	COPY_INTLIST_FIELD(rowMarks);
	COPY_NODE_FIELD(targetList);
	COPY_NODE_FIELD(groupClause);
	COPY_NODE_FIELD(havingQual);
	COPY_NODE_FIELD(distinctClause);
	COPY_NODE_FIELD(sortClause);
	COPY_NODE_FIELD(limitOffset);
	COPY_NODE_FIELD(limitCount);
	COPY_NODE_FIELD(setOperations);
	COPY_INTLIST_FIELD(resultRelations);
1439

1440 1441
	/*
	 * We do not copy the planner internal fields: base_rel_list,
1442 1443 1444
	 * other_rel_list, join_rel_list, equi_key_list, in_info_list,
	 * query_pathkeys, hasJoinRTEs.  That would get us into copying
	 * RelOptInfo/Path trees, which we don't want to do.
1445
	 */
1446

1447
	return newnode;
1448 1449
}

1450 1451 1452 1453
static InsertStmt *
_copyInsertStmt(InsertStmt *from)
{
	InsertStmt *newnode = makeNode(InsertStmt);
B
Bruce Momjian 已提交
1454

1455 1456 1457 1458
	COPY_NODE_FIELD(relation);
	COPY_NODE_FIELD(cols);
	COPY_NODE_FIELD(targetList);
	COPY_NODE_FIELD(selectStmt);
1459 1460 1461 1462 1463 1464 1465 1466

	return newnode;
}

static DeleteStmt *
_copyDeleteStmt(DeleteStmt *from)
{
	DeleteStmt *newnode = makeNode(DeleteStmt);
B
Bruce Momjian 已提交
1467

1468 1469
	COPY_NODE_FIELD(relation);
	COPY_NODE_FIELD(whereClause);
1470 1471 1472 1473 1474 1475 1476 1477

	return newnode;
}

static UpdateStmt *
_copyUpdateStmt(UpdateStmt *from)
{
	UpdateStmt *newnode = makeNode(UpdateStmt);
B
Bruce Momjian 已提交
1478

1479 1480 1481 1482
	COPY_NODE_FIELD(relation);
	COPY_NODE_FIELD(targetList);
	COPY_NODE_FIELD(whereClause);
	COPY_NODE_FIELD(fromClause);
1483 1484 1485 1486 1487 1488 1489 1490

	return newnode;
}

static SelectStmt *
_copySelectStmt(SelectStmt *from)
{
	SelectStmt *newnode = makeNode(SelectStmt);
B
Bruce Momjian 已提交
1491

1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509
	COPY_NODE_FIELD(distinctClause);
	COPY_NODE_FIELD(into);
	COPY_NODE_FIELD(intoColNames);
	COPY_NODE_FIELD(targetList);
	COPY_NODE_FIELD(fromClause);
	COPY_NODE_FIELD(whereClause);
	COPY_NODE_FIELD(groupClause);
	COPY_NODE_FIELD(havingClause);
	COPY_NODE_FIELD(sortClause);
	COPY_STRING_FIELD(portalname);
	COPY_SCALAR_FIELD(binary);
	COPY_NODE_FIELD(limitOffset);
	COPY_NODE_FIELD(limitCount);
	COPY_NODE_FIELD(forUpdate);
	COPY_SCALAR_FIELD(op);
	COPY_SCALAR_FIELD(all);
	COPY_NODE_FIELD(larg);
	COPY_NODE_FIELD(rarg);
1510 1511 1512 1513

	return newnode;
}

1514 1515 1516 1517
static SetOperationStmt *
_copySetOperationStmt(SetOperationStmt *from)
{
	SetOperationStmt *newnode = makeNode(SetOperationStmt);
B
Bruce Momjian 已提交
1518

1519 1520 1521 1522 1523
	COPY_SCALAR_FIELD(op);
	COPY_SCALAR_FIELD(all);
	COPY_NODE_FIELD(larg);
	COPY_NODE_FIELD(rarg);
	COPY_INTLIST_FIELD(colTypes);
1524 1525 1526 1527

	return newnode;
}

1528 1529 1530 1531
static AlterTableStmt *
_copyAlterTableStmt(AlterTableStmt *from)
{
	AlterTableStmt *newnode = makeNode(AlterTableStmt);
B
Bruce Momjian 已提交
1532

1533 1534 1535 1536 1537
	COPY_SCALAR_FIELD(subtype);
	COPY_NODE_FIELD(relation);
	COPY_STRING_FIELD(name);
	COPY_NODE_FIELD(def);
	COPY_SCALAR_FIELD(behavior);
1538 1539 1540 1541

	return newnode;
}

B
Bruce Momjian 已提交
1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555
static AlterDomainStmt *
_copyAlterDomainStmt(AlterDomainStmt *from)
{
	AlterDomainStmt *newnode = makeNode(AlterDomainStmt);

	COPY_SCALAR_FIELD(subtype);
	COPY_NODE_FIELD(typename);
	COPY_STRING_FIELD(name);
	COPY_NODE_FIELD(def);
	COPY_SCALAR_FIELD(behavior);

	return newnode;
} 

1556 1557
static GrantStmt *
_copyGrantStmt(GrantStmt *from)
1558
{
1559
	GrantStmt  *newnode = makeNode(GrantStmt);
B
Bruce Momjian 已提交
1560

1561 1562 1563 1564 1565
	COPY_SCALAR_FIELD(is_grant);
	COPY_SCALAR_FIELD(objtype);
	COPY_NODE_FIELD(objects);
	COPY_INTLIST_FIELD(privileges);
	COPY_NODE_FIELD(grantees);
1566 1567
	COPY_SCALAR_FIELD(grant_option);
	COPY_SCALAR_FIELD(behavior);
1568 1569 1570 1571 1572 1573 1574 1575 1576

	return newnode;
}

static PrivGrantee *
_copyPrivGrantee(PrivGrantee *from)
{
	PrivGrantee *newnode = makeNode(PrivGrantee);

1577 1578
	COPY_STRING_FIELD(username);
	COPY_STRING_FIELD(groupname);
1579 1580 1581 1582

	return newnode;
}

1583 1584 1585 1586 1587
static FuncWithArgs *
_copyFuncWithArgs(FuncWithArgs *from)
{
	FuncWithArgs *newnode = makeNode(FuncWithArgs);

1588 1589
	COPY_NODE_FIELD(funcname);
	COPY_NODE_FIELD(funcargs);
1590 1591 1592 1593

	return newnode;
}

B
Bruce Momjian 已提交
1594 1595 1596 1597 1598 1599 1600 1601 1602
static InsertDefault *
_copyInsertDefault(InsertDefault *from)
{
	InsertDefault *newnode = makeNode(InsertDefault);

	return newnode;
}


1603 1604 1605
static ClosePortalStmt *
_copyClosePortalStmt(ClosePortalStmt *from)
{
1606
	ClosePortalStmt *newnode = makeNode(ClosePortalStmt);
1607

1608
	COPY_STRING_FIELD(portalname);
1609 1610 1611 1612

	return newnode;
}

1613 1614 1615 1616
static ClusterStmt *
_copyClusterStmt(ClusterStmt *from)
{
	ClusterStmt *newnode = makeNode(ClusterStmt);
B
Bruce Momjian 已提交
1617

1618 1619
	COPY_NODE_FIELD(relation);
	COPY_STRING_FIELD(indexname);
1620 1621 1622 1623 1624 1625 1626

	return newnode;
}

static CopyStmt *
_copyCopyStmt(CopyStmt *from)
{
B
Bruce Momjian 已提交
1627 1628
	CopyStmt   *newnode = makeNode(CopyStmt);

1629 1630 1631 1632 1633
	COPY_NODE_FIELD(relation);
	COPY_NODE_FIELD(attlist);
	COPY_SCALAR_FIELD(is_from);
	COPY_STRING_FIELD(filename);
	COPY_NODE_FIELD(options);
1634 1635 1636 1637 1638 1639 1640 1641

	return newnode;
}

static CreateStmt *
_copyCreateStmt(CreateStmt *from)
{
	CreateStmt *newnode = makeNode(CreateStmt);
B
Bruce Momjian 已提交
1642

1643 1644 1645 1646 1647 1648
	COPY_NODE_FIELD(relation);
	COPY_NODE_FIELD(tableElts);
	COPY_NODE_FIELD(inhRelations);
	COPY_NODE_FIELD(constraints);
	COPY_SCALAR_FIELD(hasoids);
	COPY_SCALAR_FIELD(oncommit);
1649 1650 1651 1652 1653 1654 1655 1656

	return newnode;
}

static DefineStmt *
_copyDefineStmt(DefineStmt *from)
{
	DefineStmt *newnode = makeNode(DefineStmt);
B
Bruce Momjian 已提交
1657

1658 1659 1660
	COPY_SCALAR_FIELD(defType);
	COPY_NODE_FIELD(defnames);
	COPY_NODE_FIELD(definition);
1661 1662 1663 1664 1665 1666 1667

	return newnode;
}

static DropStmt *
_copyDropStmt(DropStmt *from)
{
B
Bruce Momjian 已提交
1668 1669
	DropStmt   *newnode = makeNode(DropStmt);

1670 1671 1672
	COPY_NODE_FIELD(objects);
	COPY_SCALAR_FIELD(removeType);
	COPY_SCALAR_FIELD(behavior);
1673 1674 1675 1676

	return newnode;
}

1677 1678 1679
static TruncateStmt *
_copyTruncateStmt(TruncateStmt *from)
{
1680
	TruncateStmt *newnode = makeNode(TruncateStmt);
1681

1682
	COPY_NODE_FIELD(relation);
1683 1684 1685 1686

	return newnode;
}

1687 1688 1689 1690
static CommentStmt *
_copyCommentStmt(CommentStmt *from)
{
	CommentStmt *newnode = makeNode(CommentStmt);
B
Bruce Momjian 已提交
1691

1692 1693 1694 1695
	COPY_SCALAR_FIELD(objtype);
	COPY_NODE_FIELD(objname);
	COPY_NODE_FIELD(objargs);
	COPY_STRING_FIELD(comment);
1696 1697 1698 1699 1700 1701 1702

	return newnode;
}

static FetchStmt *
_copyFetchStmt(FetchStmt *from)
{
B
Bruce Momjian 已提交
1703 1704
	FetchStmt  *newnode = makeNode(FetchStmt);

1705 1706 1707 1708
	COPY_SCALAR_FIELD(direction);
	COPY_SCALAR_FIELD(howMany);
	COPY_STRING_FIELD(portalname);
	COPY_SCALAR_FIELD(ismove);
1709 1710 1711 1712 1713 1714 1715

	return newnode;
}

static IndexStmt *
_copyIndexStmt(IndexStmt *from)
{
B
Bruce Momjian 已提交
1716 1717
	IndexStmt  *newnode = makeNode(IndexStmt);

1718 1719 1720 1721 1722 1723 1724 1725 1726
	COPY_STRING_FIELD(idxname);
	COPY_NODE_FIELD(relation);
	COPY_STRING_FIELD(accessMethod);
	COPY_NODE_FIELD(indexParams);
	COPY_NODE_FIELD(whereClause);
	COPY_NODE_FIELD(rangetable);
	COPY_SCALAR_FIELD(unique);
	COPY_SCALAR_FIELD(primary);
	COPY_SCALAR_FIELD(isconstraint);
1727 1728 1729 1730

	return newnode;
}

1731 1732
static CreateFunctionStmt *
_copyCreateFunctionStmt(CreateFunctionStmt *from)
1733
{
1734
	CreateFunctionStmt *newnode = makeNode(CreateFunctionStmt);
B
Bruce Momjian 已提交
1735

1736 1737 1738 1739 1740 1741
	COPY_SCALAR_FIELD(replace);
	COPY_NODE_FIELD(funcname);
	COPY_NODE_FIELD(argTypes);
	COPY_NODE_FIELD(returnType);
	COPY_NODE_FIELD(options);
	COPY_NODE_FIELD(withClause);
1742 1743 1744 1745 1746 1747 1748 1749

	return newnode;
}

static RemoveAggrStmt *
_copyRemoveAggrStmt(RemoveAggrStmt *from)
{
	RemoveAggrStmt *newnode = makeNode(RemoveAggrStmt);
B
Bruce Momjian 已提交
1750

1751 1752 1753
	COPY_NODE_FIELD(aggname);
	COPY_NODE_FIELD(aggtype);
	COPY_SCALAR_FIELD(behavior);
1754 1755 1756 1757 1758 1759 1760 1761

	return newnode;
}

static RemoveFuncStmt *
_copyRemoveFuncStmt(RemoveFuncStmt *from)
{
	RemoveFuncStmt *newnode = makeNode(RemoveFuncStmt);
B
Bruce Momjian 已提交
1762

1763 1764 1765
	COPY_NODE_FIELD(funcname);
	COPY_NODE_FIELD(args);
	COPY_SCALAR_FIELD(behavior);
1766 1767 1768 1769 1770 1771 1772 1773

	return newnode;
}

static RemoveOperStmt *
_copyRemoveOperStmt(RemoveOperStmt *from)
{
	RemoveOperStmt *newnode = makeNode(RemoveOperStmt);
B
Bruce Momjian 已提交
1774

1775 1776 1777
	COPY_NODE_FIELD(opname);
	COPY_NODE_FIELD(args);
	COPY_SCALAR_FIELD(behavior);
1778 1779 1780 1781

	return newnode;
}

1782 1783 1784 1785 1786
static RemoveOpClassStmt *
_copyRemoveOpClassStmt(RemoveOpClassStmt *from)
{
	RemoveOpClassStmt *newnode = makeNode(RemoveOpClassStmt);

1787 1788 1789
	COPY_NODE_FIELD(opclassname);
	COPY_STRING_FIELD(amname);
	COPY_SCALAR_FIELD(behavior);
1790 1791 1792 1793

	return newnode;
}

1794 1795 1796 1797
static RenameStmt *
_copyRenameStmt(RenameStmt *from)
{
	RenameStmt *newnode = makeNode(RenameStmt);
B
Bruce Momjian 已提交
1798

1799 1800 1801 1802
	COPY_NODE_FIELD(relation);
	COPY_STRING_FIELD(oldname);
	COPY_STRING_FIELD(newname);
	COPY_SCALAR_FIELD(renameType);
1803 1804 1805 1806 1807 1808 1809

	return newnode;
}

static RuleStmt *
_copyRuleStmt(RuleStmt *from)
{
B
Bruce Momjian 已提交
1810 1811
	RuleStmt   *newnode = makeNode(RuleStmt);

1812 1813 1814 1815 1816 1817 1818
	COPY_NODE_FIELD(relation);
	COPY_STRING_FIELD(rulename);
	COPY_NODE_FIELD(whereClause);
	COPY_SCALAR_FIELD(event);
	COPY_SCALAR_FIELD(instead);
	COPY_NODE_FIELD(actions);
	COPY_SCALAR_FIELD(replace);
1819 1820 1821 1822

	return newnode;
}

1823 1824 1825
static NotifyStmt *
_copyNotifyStmt(NotifyStmt *from)
{
1826
	NotifyStmt *newnode = makeNode(NotifyStmt);
1827

1828
	COPY_NODE_FIELD(relation);
1829 1830 1831 1832 1833 1834 1835

	return newnode;
}

static ListenStmt *
_copyListenStmt(ListenStmt *from)
{
1836
	ListenStmt *newnode = makeNode(ListenStmt);
1837

1838
	COPY_NODE_FIELD(relation);
1839 1840 1841 1842 1843 1844 1845

	return newnode;
}

static UnlistenStmt *
_copyUnlistenStmt(UnlistenStmt *from)
{
1846
	UnlistenStmt *newnode = makeNode(UnlistenStmt);
1847

1848
	COPY_NODE_FIELD(relation);
1849 1850 1851 1852 1853 1854 1855

	return newnode;
}

static TransactionStmt *
_copyTransactionStmt(TransactionStmt *from)
{
1856
	TransactionStmt *newnode = makeNode(TransactionStmt);
1857

1858 1859
	COPY_SCALAR_FIELD(command);
	COPY_NODE_FIELD(options);
1860 1861 1862 1863

	return newnode;
}

B
Bruce Momjian 已提交
1864 1865 1866
static CompositeTypeStmt *
_copyCompositeTypeStmt(CompositeTypeStmt *from)
{
B
Bruce Momjian 已提交
1867
	CompositeTypeStmt *newnode = makeNode(CompositeTypeStmt);
B
Bruce Momjian 已提交
1868

1869 1870
	COPY_NODE_FIELD(typevar);
	COPY_NODE_FIELD(coldeflist);
B
Bruce Momjian 已提交
1871 1872 1873 1874

	return newnode;
}

1875 1876 1877 1878 1879
static ViewStmt *
_copyViewStmt(ViewStmt *from)
{
	ViewStmt   *newnode = makeNode(ViewStmt);

1880 1881 1882 1883
	COPY_NODE_FIELD(view);
	COPY_NODE_FIELD(aliases);
	COPY_NODE_FIELD(query);
	COPY_SCALAR_FIELD(replace);
1884 1885 1886 1887

	return newnode;
}

1888 1889 1890
static LoadStmt *
_copyLoadStmt(LoadStmt *from)
{
1891
	LoadStmt   *newnode = makeNode(LoadStmt);
1892

1893
	COPY_STRING_FIELD(filename);
1894 1895 1896 1897

	return newnode;
}

1898 1899 1900 1901 1902
static CreateDomainStmt *
_copyCreateDomainStmt(CreateDomainStmt *from)
{
	CreateDomainStmt *newnode = makeNode(CreateDomainStmt);

1903 1904 1905
	COPY_NODE_FIELD(domainname);
	COPY_NODE_FIELD(typename);
	COPY_NODE_FIELD(constraints);
1906 1907 1908 1909

	return newnode;
}

1910 1911 1912 1913 1914
static CreateOpClassStmt *
_copyCreateOpClassStmt(CreateOpClassStmt *from)
{
	CreateOpClassStmt *newnode = makeNode(CreateOpClassStmt);

1915 1916 1917 1918 1919
	COPY_NODE_FIELD(opclassname);
	COPY_STRING_FIELD(amname);
	COPY_NODE_FIELD(datatype);
	COPY_NODE_FIELD(items);
	COPY_SCALAR_FIELD(isDefault);
1920 1921 1922 1923 1924 1925 1926 1927 1928

	return newnode;
}

static CreateOpClassItem *
_copyCreateOpClassItem(CreateOpClassItem *from)
{
	CreateOpClassItem *newnode = makeNode(CreateOpClassItem);

1929 1930 1931 1932 1933 1934
	COPY_SCALAR_FIELD(itemtype);
	COPY_NODE_FIELD(name);
	COPY_NODE_FIELD(args);
	COPY_SCALAR_FIELD(number);
	COPY_SCALAR_FIELD(recheck);
	COPY_NODE_FIELD(storedtype);
1935 1936 1937 1938

	return newnode;
}

1939 1940 1941
static CreatedbStmt *
_copyCreatedbStmt(CreatedbStmt *from)
{
B
Bruce Momjian 已提交
1942
	CreatedbStmt *newnode = makeNode(CreatedbStmt);
1943

1944 1945
	COPY_STRING_FIELD(dbname);
	COPY_NODE_FIELD(options);
1946 1947 1948 1949

	return newnode;
}

1950 1951 1952 1953 1954
static AlterDatabaseSetStmt *
_copyAlterDatabaseSetStmt(AlterDatabaseSetStmt *from)
{
	AlterDatabaseSetStmt *newnode = makeNode(AlterDatabaseSetStmt);

1955 1956 1957
	COPY_STRING_FIELD(dbname);
	COPY_STRING_FIELD(variable);
	COPY_NODE_FIELD(value);
1958 1959 1960 1961

	return newnode;
}

1962 1963 1964
static DropdbStmt *
_copyDropdbStmt(DropdbStmt *from)
{
B
Bruce Momjian 已提交
1965
	DropdbStmt *newnode = makeNode(DropdbStmt);
1966

1967
	COPY_STRING_FIELD(dbname);
1968 1969 1970 1971 1972 1973 1974

	return newnode;
}

static VacuumStmt *
_copyVacuumStmt(VacuumStmt *from)
{
B
Bruce Momjian 已提交
1975
	VacuumStmt *newnode = makeNode(VacuumStmt);
1976

1977 1978 1979 1980 1981 1982 1983
	COPY_SCALAR_FIELD(vacuum);
	COPY_SCALAR_FIELD(full);
	COPY_SCALAR_FIELD(analyze);
	COPY_SCALAR_FIELD(freeze);
	COPY_SCALAR_FIELD(verbose);
	COPY_NODE_FIELD(relation);
	COPY_NODE_FIELD(va_cols);
1984 1985 1986 1987 1988 1989 1990

	return newnode;
}

static ExplainStmt *
_copyExplainStmt(ExplainStmt *from)
{
B
Bruce Momjian 已提交
1991
	ExplainStmt *newnode = makeNode(ExplainStmt);
1992

1993 1994 1995
	COPY_NODE_FIELD(query);
	COPY_SCALAR_FIELD(verbose);
	COPY_SCALAR_FIELD(analyze);
1996 1997 1998 1999 2000 2001 2002

	return newnode;
}

static CreateSeqStmt *
_copyCreateSeqStmt(CreateSeqStmt *from)
{
B
Bruce Momjian 已提交
2003
	CreateSeqStmt *newnode = makeNode(CreateSeqStmt);
2004

2005 2006
	COPY_NODE_FIELD(sequence);
	COPY_NODE_FIELD(options);
2007 2008 2009 2010

	return newnode;
}

2011 2012 2013
static VariableSetStmt *
_copyVariableSetStmt(VariableSetStmt *from)
{
2014
	VariableSetStmt *newnode = makeNode(VariableSetStmt);
2015

2016 2017 2018
	COPY_STRING_FIELD(name);
	COPY_NODE_FIELD(args);
	COPY_SCALAR_FIELD(is_local);
2019 2020 2021 2022

	return newnode;
}

2023 2024 2025 2026 2027
static VariableShowStmt *
_copyVariableShowStmt(VariableShowStmt *from)
{
	VariableShowStmt *newnode = makeNode(VariableShowStmt);

2028
	COPY_STRING_FIELD(name);
2029 2030 2031 2032

	return newnode;
}

2033 2034 2035
static VariableResetStmt *
_copyVariableResetStmt(VariableResetStmt *from)
{
2036
	VariableResetStmt *newnode = makeNode(VariableResetStmt);
2037

2038
	COPY_STRING_FIELD(name);
2039 2040 2041 2042

	return newnode;
}

2043 2044 2045 2046 2047
static CreateTrigStmt *
_copyCreateTrigStmt(CreateTrigStmt *from)
{
	CreateTrigStmt *newnode = makeNode(CreateTrigStmt);

2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058
	COPY_STRING_FIELD(trigname);
	COPY_NODE_FIELD(relation);
	COPY_NODE_FIELD(funcname);
	COPY_NODE_FIELD(args);
	COPY_SCALAR_FIELD(before);
	COPY_SCALAR_FIELD(row);
	strcpy(newnode->actions, from->actions); /* in-line string field */
	COPY_SCALAR_FIELD(isconstraint);
	COPY_SCALAR_FIELD(deferrable);
	COPY_SCALAR_FIELD(initdeferred);
	COPY_NODE_FIELD(constrrel);
2059 2060 2061 2062

	return newnode;
}

2063 2064
static DropPropertyStmt *
_copyDropPropertyStmt(DropPropertyStmt *from)
2065
{
2066
	DropPropertyStmt *newnode = makeNode(DropPropertyStmt);
2067

2068 2069 2070 2071
	COPY_NODE_FIELD(relation);
	COPY_STRING_FIELD(property);
	COPY_SCALAR_FIELD(removeType);
	COPY_SCALAR_FIELD(behavior);
2072 2073 2074 2075 2076 2077 2078 2079 2080

	return newnode;
}

static CreatePLangStmt *
_copyCreatePLangStmt(CreatePLangStmt *from)
{
	CreatePLangStmt *newnode = makeNode(CreatePLangStmt);

2081 2082 2083 2084
	COPY_STRING_FIELD(plname);
	COPY_NODE_FIELD(plhandler);
	COPY_NODE_FIELD(plvalidator);
	COPY_SCALAR_FIELD(pltrusted);
2085 2086 2087 2088 2089 2090 2091 2092 2093

	return newnode;
}

static DropPLangStmt *
_copyDropPLangStmt(DropPLangStmt *from)
{
	DropPLangStmt *newnode = makeNode(DropPLangStmt);

2094 2095
	COPY_STRING_FIELD(plname);
	COPY_SCALAR_FIELD(behavior);
2096 2097 2098 2099 2100 2101 2102 2103 2104

	return newnode;
}

static CreateUserStmt *
_copyCreateUserStmt(CreateUserStmt *from)
{
	CreateUserStmt *newnode = makeNode(CreateUserStmt);

2105 2106
	COPY_STRING_FIELD(user);
	COPY_NODE_FIELD(options);
2107 2108 2109 2110 2111 2112 2113 2114 2115

	return newnode;
}

static AlterUserStmt *
_copyAlterUserStmt(AlterUserStmt *from)
{
	AlterUserStmt *newnode = makeNode(AlterUserStmt);

2116 2117
	COPY_STRING_FIELD(user);
	COPY_NODE_FIELD(options);
2118 2119 2120 2121

	return newnode;
}

2122 2123 2124 2125 2126
static AlterUserSetStmt *
_copyAlterUserSetStmt(AlterUserSetStmt *from)
{
	AlterUserSetStmt *newnode = makeNode(AlterUserSetStmt);

2127 2128 2129
	COPY_STRING_FIELD(user);
	COPY_STRING_FIELD(variable);
	COPY_NODE_FIELD(value);
2130 2131 2132 2133

	return newnode;
}

2134 2135 2136 2137 2138
static DropUserStmt *
_copyDropUserStmt(DropUserStmt *from)
{
	DropUserStmt *newnode = makeNode(DropUserStmt);

2139
	COPY_NODE_FIELD(users);
2140 2141 2142 2143

	return newnode;
}

2144 2145 2146
static LockStmt *
_copyLockStmt(LockStmt *from)
{
2147
	LockStmt   *newnode = makeNode(LockStmt);
2148

2149 2150
	COPY_NODE_FIELD(relations);
	COPY_SCALAR_FIELD(mode);
2151 2152 2153

	return newnode;
}
2154

2155 2156 2157
static ConstraintsSetStmt *
_copyConstraintsSetStmt(ConstraintsSetStmt *from)
{
B
Bruce Momjian 已提交
2158
	ConstraintsSetStmt *newnode = makeNode(ConstraintsSetStmt);
2159

2160 2161
	COPY_NODE_FIELD(constraints);
	COPY_SCALAR_FIELD(deferred);
2162 2163 2164 2165 2166 2167 2168

	return newnode;
}

static CreateGroupStmt *
_copyCreateGroupStmt(CreateGroupStmt *from)
{
B
Bruce Momjian 已提交
2169
	CreateGroupStmt *newnode = makeNode(CreateGroupStmt);
2170

2171 2172
	COPY_STRING_FIELD(name);
	COPY_NODE_FIELD(options);
2173 2174 2175 2176 2177 2178 2179

	return newnode;
}

static AlterGroupStmt *
_copyAlterGroupStmt(AlterGroupStmt *from)
{
B
Bruce Momjian 已提交
2180
	AlterGroupStmt *newnode = makeNode(AlterGroupStmt);
2181

2182 2183 2184
	COPY_STRING_FIELD(name);
	COPY_SCALAR_FIELD(action);
	COPY_NODE_FIELD(listUsers);
2185 2186 2187 2188 2189 2190 2191

	return newnode;
}

static DropGroupStmt *
_copyDropGroupStmt(DropGroupStmt *from)
{
B
Bruce Momjian 已提交
2192
	DropGroupStmt *newnode = makeNode(DropGroupStmt);
2193

2194
	COPY_STRING_FIELD(name);
2195 2196 2197 2198 2199 2200 2201

	return newnode;
}

static ReindexStmt *
_copyReindexStmt(ReindexStmt *from)
{
B
Bruce Momjian 已提交
2202
	ReindexStmt *newnode = makeNode(ReindexStmt);
2203

2204 2205 2206 2207 2208
	COPY_SCALAR_FIELD(reindexType);
	COPY_NODE_FIELD(relation);
	COPY_STRING_FIELD(name);
	COPY_SCALAR_FIELD(force);
	COPY_SCALAR_FIELD(all);
2209 2210 2211 2212

	return newnode;
}

2213 2214 2215 2216 2217
static CreateSchemaStmt *
_copyCreateSchemaStmt(CreateSchemaStmt *from)
{
	CreateSchemaStmt *newnode = makeNode(CreateSchemaStmt);

2218 2219 2220
	COPY_STRING_FIELD(schemaname);
	COPY_STRING_FIELD(authid);
	COPY_NODE_FIELD(schemaElts);
2221 2222 2223 2224

	return newnode;
}

2225 2226 2227 2228 2229
static CreateConversionStmt *
_copyCreateConversionStmt(CreateConversionStmt *from)
{
	CreateConversionStmt *newnode = makeNode(CreateConversionStmt);

2230 2231 2232 2233 2234
	COPY_NODE_FIELD(conversion_name);
	COPY_STRING_FIELD(for_encoding_name);
	COPY_STRING_FIELD(to_encoding_name);
	COPY_NODE_FIELD(func_name);
	COPY_SCALAR_FIELD(def);
2235 2236 2237 2238 2239 2240 2241 2242 2243

	return newnode;
}

static CreateCastStmt *
_copyCreateCastStmt(CreateCastStmt *from)
{
	CreateCastStmt *newnode = makeNode(CreateCastStmt);

2244 2245 2246 2247
	COPY_NODE_FIELD(sourcetype);
	COPY_NODE_FIELD(targettype);
	COPY_NODE_FIELD(func);
	COPY_SCALAR_FIELD(context);
2248 2249 2250 2251 2252 2253 2254 2255 2256

	return newnode;
}

static DropCastStmt *
_copyDropCastStmt(DropCastStmt *from)
{
	DropCastStmt *newnode = makeNode(DropCastStmt);

2257 2258 2259
	COPY_NODE_FIELD(sourcetype);
	COPY_NODE_FIELD(targettype);
	COPY_SCALAR_FIELD(behavior);
2260 2261 2262 2263

	return newnode;
}

2264 2265 2266 2267 2268
static PrepareStmt *
_copyPrepareStmt(PrepareStmt *from)
{
	PrepareStmt *newnode = makeNode(PrepareStmt);

2269 2270 2271 2272
	COPY_STRING_FIELD(name);
	COPY_NODE_FIELD(argtypes);
	COPY_INTLIST_FIELD(argtype_oids);
	COPY_NODE_FIELD(query);
2273 2274 2275 2276 2277 2278 2279 2280 2281

	return newnode;
}

static ExecuteStmt *
_copyExecuteStmt(ExecuteStmt *from)
{
	ExecuteStmt *newnode = makeNode(ExecuteStmt);

2282 2283 2284
	COPY_STRING_FIELD(name);
	COPY_NODE_FIELD(into);
	COPY_NODE_FIELD(params);
2285 2286 2287 2288 2289 2290 2291 2292 2293

	return newnode;
}

static DeallocateStmt *
_copyDeallocateStmt(DeallocateStmt *from)
{
	DeallocateStmt *newnode = makeNode(DeallocateStmt);

2294
	COPY_STRING_FIELD(name);
2295 2296 2297 2298

	return newnode;
}

2299 2300

/* ****************************************************************
2301
 *					pg_list.h copy functions
2302 2303 2304
 * ****************************************************************
 */

2305
static Value *
2306
_copyValue(Value *from)
2307
{
2308
	Value	   *newnode = makeNode(Value);
2309

2310 2311
	/* See also _copyAConst when changing this code! */

2312
	COPY_SCALAR_FIELD(type);
2313 2314
	switch (from->type)
	{
2315
		case T_Integer:
2316
			COPY_SCALAR_FIELD(val.ival);
2317 2318
			break;
		case T_Float:
2319
		case T_String:
2320
		case T_BitString:
2321
			COPY_STRING_FIELD(val.str);
2322
			break;
2323 2324 2325
		case T_Null:
			/* nothing to do */
			break;
2326
		default:
2327
			elog(ERROR, "_copyValue: unknown node type %d", from->type);
2328
			break;
2329 2330
	}
	return newnode;
2331 2332
}

2333 2334 2335 2336 2337
/*
 * copyObject
 *
 * Create a copy of a Node tree or list.  This is a "deep" copy: all
 * substructure is copied too, recursively.
2338
 */
2339
void *
2340 2341
copyObject(void *from)
{
2342
	void	   *retval;
2343

2344 2345
	if (from == NULL)
		return NULL;
2346

2347
	switch (nodeTag(from))
2348
	{
2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369
			/*
			 * PLAN NODES
			 */
		case T_Plan:
			retval = _copyPlan(from);
			break;
		case T_Result:
			retval = _copyResult(from);
			break;
		case T_Append:
			retval = _copyAppend(from);
			break;
		case T_Scan:
			retval = _copyScan(from);
			break;
		case T_SeqScan:
			retval = _copySeqScan(from);
			break;
		case T_IndexScan:
			retval = _copyIndexScan(from);
			break;
2370 2371 2372
		case T_TidScan:
			retval = _copyTidScan(from);
			break;
2373 2374 2375
		case T_SubqueryScan:
			retval = _copySubqueryScan(from);
			break;
2376 2377 2378
		case T_FunctionScan:
			retval = _copyFunctionScan(from);
			break;
2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396
		case T_Join:
			retval = _copyJoin(from);
			break;
		case T_NestLoop:
			retval = _copyNestLoop(from);
			break;
		case T_MergeJoin:
			retval = _copyMergeJoin(from);
			break;
		case T_HashJoin:
			retval = _copyHashJoin(from);
			break;
		case T_Material:
			retval = _copyMaterial(from);
			break;
		case T_Sort:
			retval = _copySort(from);
			break;
V
Vadim B. Mikheev 已提交
2397 2398 2399
		case T_Group:
			retval = _copyGroup(from);
			break;
2400 2401 2402 2403 2404 2405
		case T_Agg:
			retval = _copyAgg(from);
			break;
		case T_Unique:
			retval = _copyUnique(from);
			break;
2406 2407 2408
		case T_Hash:
			retval = _copyHash(from);
			break;
2409 2410 2411
		case T_SetOp:
			retval = _copySetOp(from);
			break;
2412 2413 2414
		case T_Limit:
			retval = _copyLimit(from);
			break;
2415 2416 2417 2418 2419 2420 2421

			/*
			 * PRIMITIVE NODES
			 */
		case T_Resdom:
			retval = _copyResdom(from);
			break;
2422 2423 2424 2425 2426 2427
		case T_Alias:
			retval = _copyAlias(from);
			break;
		case T_RangeVar:
			retval = _copyRangeVar(from);
			break;
2428 2429 2430 2431 2432 2433 2434 2435 2436
		case T_Var:
			retval = _copyVar(from);
			break;
		case T_Const:
			retval = _copyConst(from);
			break;
		case T_Param:
			retval = _copyParam(from);
			break;
2437 2438 2439
		case T_Aggref:
			retval = _copyAggref(from);
			break;
2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454
		case T_ArrayRef:
			retval = _copyArrayRef(from);
			break;
		case T_FuncExpr:
			retval = _copyFuncExpr(from);
			break;
		case T_OpExpr:
			retval = _copyOpExpr(from);
			break;
		case T_DistinctExpr:
			retval = _copyDistinctExpr(from);
			break;
		case T_BoolExpr:
			retval = _copyBoolExpr(from);
			break;
2455 2456 2457
		case T_SubLink:
			retval = _copySubLink(from);
			break;
2458 2459
		case T_SubPlan:
			retval = _copySubPlan(from);
2460
			break;
2461 2462 2463
		case T_FieldSelect:
			retval = _copyFieldSelect(from);
			break;
2464 2465 2466
		case T_RelabelType:
			retval = _copyRelabelType(from);
			break;
2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487
		case T_CaseExpr:
			retval = _copyCaseExpr(from);
			break;
		case T_CaseWhen:
			retval = _copyCaseWhen(from);
			break;
		case T_NullTest:
			retval = _copyNullTest(from);
			break;
		case T_BooleanTest:
			retval = _copyBooleanTest(from);
			break;
		case T_ConstraintTest:
			retval = _copyConstraintTest(from);
			break;
		case T_ConstraintTestValue:
			retval = _copyConstraintTestValue(from);
			break;
		case T_TargetEntry:
			retval = _copyTargetEntry(from);
			break;
2488 2489 2490
		case T_RangeTblRef:
			retval = _copyRangeTblRef(from);
			break;
2491 2492 2493
		case T_JoinExpr:
			retval = _copyJoinExpr(from);
			break;
2494 2495 2496
		case T_FromExpr:
			retval = _copyFromExpr(from);
			break;
2497 2498 2499 2500

			/*
			 * RELATION NODES
			 */
2501 2502
		case T_PathKeyItem:
			retval = _copyPathKeyItem(from);
2503
			break;
2504 2505
		case T_RestrictInfo:
			retval = _copyRestrictInfo(from);
2506
			break;
2507 2508
		case T_JoinInfo:
			retval = _copyJoinInfo(from);
2509
			break;
2510 2511 2512
		case T_InClauseInfo:
			retval = _copyInClauseInfo(from);
			break;
2513 2514

			/*
2515
			 * VALUE NODES
2516
			 */
2517 2518 2519
		case T_Integer:
		case T_Float:
		case T_String:
2520
		case T_BitString:
2521
		case T_Null:
2522
			retval = _copyValue(from);
2523
			break;
2524 2525 2526 2527 2528 2529 2530 2531
		case T_List:
			{
				List	   *list = from,
						   *l,
						   *nl;

				/* rather ugly coding for speed... */
				/* Note the input list cannot be NIL if we got here. */
2532
				nl = makeList1(copyObject(lfirst(list)));
2533 2534 2535 2536
				retval = nl;

				foreach(l, lnext(list))
				{
2537
					lnext(nl) = makeList1(copyObject(lfirst(l)));
2538 2539 2540
					nl = lnext(nl);
				}
			}
2541
			break;
2542 2543 2544 2545

			/*
			 * PARSE NODES
			 */
2546 2547 2548
		case T_Query:
			retval = _copyQuery(from);
			break;
2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560
		case T_InsertStmt:
			retval = _copyInsertStmt(from);
			break;
		case T_DeleteStmt:
			retval = _copyDeleteStmt(from);
			break;
		case T_UpdateStmt:
			retval = _copyUpdateStmt(from);
			break;
		case T_SelectStmt:
			retval = _copySelectStmt(from);
			break;
2561 2562 2563
		case T_SetOperationStmt:
			retval = _copySetOperationStmt(from);
			break;
2564 2565 2566
		case T_AlterTableStmt:
			retval = _copyAlterTableStmt(from);
			break;
B
Bruce Momjian 已提交
2567 2568 2569
		case T_AlterDomainStmt:
			retval = _copyAlterDomainStmt(from);
			break;
2570 2571
		case T_GrantStmt:
			retval = _copyGrantStmt(from);
2572
			break;
2573 2574 2575
		case T_ClosePortalStmt:
			retval = _copyClosePortalStmt(from);
			break;
2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590
		case T_ClusterStmt:
			retval = _copyClusterStmt(from);
			break;
		case T_CopyStmt:
			retval = _copyCopyStmt(from);
			break;
		case T_CreateStmt:
			retval = _copyCreateStmt(from);
			break;
		case T_DefineStmt:
			retval = _copyDefineStmt(from);
			break;
		case T_DropStmt:
			retval = _copyDropStmt(from);
			break;
2591 2592 2593
		case T_TruncateStmt:
			retval = _copyTruncateStmt(from);
			break;
2594 2595 2596 2597 2598 2599 2600 2601 2602
		case T_CommentStmt:
			retval = _copyCommentStmt(from);
			break;
		case T_FetchStmt:
			retval = _copyFetchStmt(from);
			break;
		case T_IndexStmt:
			retval = _copyIndexStmt(from);
			break;
2603 2604
		case T_CreateFunctionStmt:
			retval = _copyCreateFunctionStmt(from);
2605 2606 2607 2608 2609 2610 2611 2612 2613 2614
			break;
		case T_RemoveAggrStmt:
			retval = _copyRemoveAggrStmt(from);
			break;
		case T_RemoveFuncStmt:
			retval = _copyRemoveFuncStmt(from);
			break;
		case T_RemoveOperStmt:
			retval = _copyRemoveOperStmt(from);
			break;
2615 2616 2617
		case T_RemoveOpClassStmt:
			retval = _copyRemoveOpClassStmt(from);
			break;
2618 2619 2620 2621 2622 2623
		case T_RenameStmt:
			retval = _copyRenameStmt(from);
			break;
		case T_RuleStmt:
			retval = _copyRuleStmt(from);
			break;
2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635
		case T_NotifyStmt:
			retval = _copyNotifyStmt(from);
			break;
		case T_ListenStmt:
			retval = _copyListenStmt(from);
			break;
		case T_UnlistenStmt:
			retval = _copyUnlistenStmt(from);
			break;
		case T_TransactionStmt:
			retval = _copyTransactionStmt(from);
			break;
B
Bruce Momjian 已提交
2636 2637 2638
		case T_CompositeTypeStmt:
			retval = _copyCompositeTypeStmt(from);
			break;
2639 2640 2641
		case T_ViewStmt:
			retval = _copyViewStmt(from);
			break;
2642 2643 2644
		case T_LoadStmt:
			retval = _copyLoadStmt(from);
			break;
2645 2646 2647
		case T_CreateDomainStmt:
			retval = _copyCreateDomainStmt(from);
			break;
2648 2649 2650 2651 2652 2653
		case T_CreateOpClassStmt:
			retval = _copyCreateOpClassStmt(from);
			break;
		case T_CreateOpClassItem:
			retval = _copyCreateOpClassItem(from);
			break;
2654 2655 2656
		case T_CreatedbStmt:
			retval = _copyCreatedbStmt(from);
			break;
2657 2658 2659
		case T_AlterDatabaseSetStmt:
			retval = _copyAlterDatabaseSetStmt(from);
			break;
2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671
		case T_DropdbStmt:
			retval = _copyDropdbStmt(from);
			break;
		case T_VacuumStmt:
			retval = _copyVacuumStmt(from);
			break;
		case T_ExplainStmt:
			retval = _copyExplainStmt(from);
			break;
		case T_CreateSeqStmt:
			retval = _copyCreateSeqStmt(from);
			break;
2672 2673 2674
		case T_VariableSetStmt:
			retval = _copyVariableSetStmt(from);
			break;
2675 2676 2677
		case T_VariableShowStmt:
			retval = _copyVariableShowStmt(from);
			break;
2678 2679 2680
		case T_VariableResetStmt:
			retval = _copyVariableResetStmt(from);
			break;
2681 2682 2683
		case T_CreateTrigStmt:
			retval = _copyCreateTrigStmt(from);
			break;
2684 2685
		case T_DropPropertyStmt:
			retval = _copyDropPropertyStmt(from);
2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698
			break;
		case T_CreatePLangStmt:
			retval = _copyCreatePLangStmt(from);
			break;
		case T_DropPLangStmt:
			retval = _copyDropPLangStmt(from);
			break;
		case T_CreateUserStmt:
			retval = _copyCreateUserStmt(from);
			break;
		case T_AlterUserStmt:
			retval = _copyAlterUserStmt(from);
			break;
2699 2700 2701
		case T_AlterUserSetStmt:
			retval = _copyAlterUserSetStmt(from);
			break;
2702 2703 2704
		case T_DropUserStmt:
			retval = _copyDropUserStmt(from);
			break;
2705 2706 2707
		case T_LockStmt:
			retval = _copyLockStmt(from);
			break;
2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722
		case T_ConstraintsSetStmt:
			retval = _copyConstraintsSetStmt(from);
			break;
		case T_CreateGroupStmt:
			retval = _copyCreateGroupStmt(from);
			break;
		case T_AlterGroupStmt:
			retval = _copyAlterGroupStmt(from);
			break;
		case T_DropGroupStmt:
			retval = _copyDropGroupStmt(from);
			break;
		case T_ReindexStmt:
			retval = _copyReindexStmt(from);
			break;
V
Vadim B. Mikheev 已提交
2723
		case T_CheckPointStmt:
B
Bruce Momjian 已提交
2724
			retval = (void *) makeNode(CheckPointStmt);
V
Vadim B. Mikheev 已提交
2725
			break;
2726 2727 2728
		case T_CreateSchemaStmt:
			retval = _copyCreateSchemaStmt(from);
			break;
2729 2730 2731 2732 2733 2734 2735 2736 2737
		case T_CreateConversionStmt:
			retval = _copyCreateConversionStmt(from);
			break;
		case T_CreateCastStmt:
			retval = _copyCreateCastStmt(from);
			break;
		case T_DropCastStmt:
			retval = _copyDropCastStmt(from);
			break;
2738 2739 2740 2741 2742 2743 2744 2745 2746
		case T_PrepareStmt:
			retval = _copyPrepareStmt(from);
			break;
		case T_ExecuteStmt:
			retval = _copyExecuteStmt(from);
			break;
		case T_DeallocateStmt:
			retval = _copyDeallocateStmt(from);
			break;
2747

2748 2749 2750
		case T_A_Expr:
			retval = _copyAExpr(from);
			break;
2751 2752 2753 2754 2755
		case T_ColumnRef:
			retval = _copyColumnRef(from);
			break;
		case T_ParamRef:
			retval = _copyParamRef(from);
2756
			break;
2757 2758 2759
		case T_A_Const:
			retval = _copyAConst(from);
			break;
2760 2761 2762 2763 2764 2765
		case T_FuncCall:
			retval = _copyFuncCall(from);
			break;
		case T_A_Indices:
			retval = _copyAIndices(from);
			break;
2766 2767 2768
		case T_ExprFieldSelect:
			retval = _copyExprFieldSelect(from);
			break;
2769 2770 2771
		case T_ResTarget:
			retval = _copyResTarget(from);
			break;
2772 2773 2774
		case T_TypeCast:
			retval = _copyTypeCast(from);
			break;
2775 2776 2777
		case T_SortGroupBy:
			retval = _copySortGroupBy(from);
			break;
2778 2779 2780
		case T_RangeSubselect:
			retval = _copyRangeSubselect(from);
			break;
2781 2782 2783
		case T_RangeFunction:
			retval = _copyRangeFunction(from);
			break;
2784 2785 2786
		case T_TypeName:
			retval = _copyTypeName(from);
			break;
2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798
		case T_IndexElem:
			retval = _copyIndexElem(from);
			break;
		case T_ColumnDef:
			retval = _copyColumnDef(from);
			break;
		case T_Constraint:
			retval = _copyConstraint(from);
			break;
		case T_DefElem:
			retval = _copyDefElem(from);
			break;
2799 2800
		case T_RangeTblEntry:
			retval = _copyRangeTblEntry(from);
2801
			break;
2802 2803 2804 2805 2806 2807
		case T_SortClause:
			retval = _copySortClause(from);
			break;
		case T_GroupClause:
			retval = _copyGroupClause(from);
			break;
2808 2809 2810
		case T_FkConstraint:
			retval = _copyFkConstraint(from);
			break;
2811 2812 2813
		case T_PrivGrantee:
			retval = _copyPrivGrantee(from);
			break;
2814 2815 2816
		case T_FuncWithArgs:
			retval = _copyFuncWithArgs(from);
			break;
B
Bruce Momjian 已提交
2817 2818 2819
		case T_InsertDefault:
			retval = _copyInsertDefault(from);
			break;
2820

2821
		default:
2822 2823 2824
			elog(ERROR, "copyObject: don't know how to copy node type %d",
				 nodeTag(from));
			retval = from;		/* keep compiler quiet */
2825
			break;
2826
	}
2827

2828
	return retval;
2829
}