copyfuncs.c 60.9 KB
Newer Older
1
/*-------------------------------------------------------------------------
2
 *
3
 * copyfuncs.c
4
 *	  Copy functions for Postgres tree nodes.
5
 *
6 7 8
 * NOTE: a general convention when copying or comparing plan nodes is
 * that we ignore the executor state subnode.  We do not need to look
 * at it because no current uses of copyObject() or equal() need to
B
Bruce Momjian 已提交
9
 * deal with already-executing plan trees.	By leaving the state subnodes
10 11 12 13
 * out, we avoid needing to write copy/compare routines for all the
 * different executor state node types.
 *
 *
14
 * Portions Copyright (c) 1996-2001, 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.160 2001/11/05 05:00:14 tgl Exp $
19 20 21 22 23 24
 *
 *-------------------------------------------------------------------------
 */

#include "postgres.h"

25
#include "optimizer/clauses.h"
26
#include "optimizer/planmain.h"
27 28 29 30 31 32 33 34 35


/*
 * Node_Copy
 *	  a macro to simplify calling of copyObject on the specified field
 */
#define Node_Copy(from, newnode, field) \
	((newnode)->field = copyObject((from)->field))

36 37

/*
38
 * listCopy
39 40 41 42 43 44 45 46 47
 *	  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!
48
 */
49
List *
50
listCopy(List *list)
51
{
52 53 54 55 56 57 58
	List	   *newlist,
			   *l,
			   *nl;

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

60
	newlist = nl = makeList1(lfirst(list));
61 62

	foreach(l, lnext(list))
63
	{
64
		lnext(nl) = makeList1(lfirst(l));
65
		nl = lnext(nl);
66
	}
67
	return newlist;
68
}
69

70
/* ****************************************************************
71
 *					 plannodes.h copy functions
72 73 74 75
 * ****************************************************************
 */

/* ----------------
76
 *		CopyPlanFields
77
 *
78 79
 *		This function copies the fields of the Plan node.  It is used by
 *		all the copy functions for classes which inherit from Plan.
80 81 82
 * ----------------
 */
static void
83
CopyPlanFields(Plan *from, Plan *newnode)
84
{
85 86
	newnode->startup_cost = from->startup_cost;
	newnode->total_cost = from->total_cost;
87
	newnode->plan_rows = from->plan_rows;
88
	newnode->plan_width = from->plan_width;
89
	/* state is NOT copied */
90 91 92 93
	Node_Copy(from, newnode, targetlist);
	Node_Copy(from, newnode, qual);
	Node_Copy(from, newnode, lefttree);
	Node_Copy(from, newnode, righttree);
94 95 96
	newnode->extParam = listCopy(from->extParam);
	newnode->locParam = listCopy(from->locParam);
	newnode->chgParam = listCopy(from->chgParam);
V
Vadim B. Mikheev 已提交
97
	Node_Copy(from, newnode, initPlan);
98
	/* subPlan list must point to subplans in the new subtree, not the old */
99
	if (from->subPlan != NIL)
100 101
		newnode->subPlan = nconc(pull_subplans((Node *) newnode->targetlist),
								 pull_subplans((Node *) newnode->qual));
V
Vadim B. Mikheev 已提交
102
	else
103
		newnode->subPlan = NIL;
V
Vadim B. Mikheev 已提交
104
	newnode->nParamExec = from->nParamExec;
105 106 107
}

/* ----------------
108
 *		_copyPlan
109 110
 * ----------------
 */
111
static Plan *
112
_copyPlan(Plan *from)
113
{
114
	Plan	   *newnode = makeNode(Plan);
115

116 117
	/*
	 * copy the node superclass fields
118 119 120 121
	 */
	CopyPlanFields(from, newnode);

	return newnode;
122 123 124 125
}


/* ----------------
126
 *		_copyResult
127 128
 * ----------------
 */
129
static Result *
130
_copyResult(Result *from)
131
{
132
	Result	   *newnode = makeNode(Result);
133

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

139 140
	/*
	 * copy remainder of node
141 142 143
	 */
	Node_Copy(from, newnode, resconstantqual);

B
Bruce Momjian 已提交
144 145 146
	/*
	 * We must add subplans in resconstantqual to the new plan's subPlan
	 * list
147
	 */
148 149
	if (from->plan.subPlan != NIL)
		newnode->plan.subPlan = nconc(newnode->plan.subPlan,
150
								pull_subplans(newnode->resconstantqual));
151

152
	return newnode;
153 154 155
}

/* ----------------
156
 *		_copyAppend
157 158
 * ----------------
 */
159
static Append *
B
Bruce Momjian 已提交
160
_copyAppend(Append *from)
161
{
162
	Append	   *newnode = makeNode(Append);
163

164 165
	/*
	 * copy node superclass fields
166 167 168
	 */
	CopyPlanFields((Plan *) from, (Plan *) newnode);

169 170
	/*
	 * copy remainder of node
171
	 */
172
	Node_Copy(from, newnode, appendplans);
173
	newnode->isTarget = from->isTarget;
174 175

	return newnode;
176 177 178 179
}


/* ----------------
180
 *		CopyScanFields
181
 *
182 183
 *		This function copies the fields of the Scan node.  It is used by
 *		all the copy functions for classes which inherit from Scan.
184 185 186
 * ----------------
 */
static void
187
CopyScanFields(Scan *from, Scan *newnode)
188
{
189 190
	newnode->scanrelid = from->scanrelid;
	return;
191 192 193
}

/* ----------------
194
 *		_copyScan
195 196
 * ----------------
 */
197
static Scan *
198
_copyScan(Scan *from)
199
{
200
	Scan	   *newnode = makeNode(Scan);
201

202 203
	/*
	 * copy node superclass fields
204 205
	 */
	CopyPlanFields((Plan *) from, (Plan *) newnode);
B
Bruce Momjian 已提交
206
	CopyScanFields((Scan *) from, (Scan *) newnode);
207 208

	return newnode;
209 210 211
}

/* ----------------
212
 *		_copySeqScan
213 214 215
 * ----------------
 */
static SeqScan *
216
_copySeqScan(SeqScan *from)
217
{
218
	SeqScan    *newnode = makeNode(SeqScan);
219

220 221
	/*
	 * copy node superclass fields
222 223 224 225 226
	 */
	CopyPlanFields((Plan *) from, (Plan *) newnode);
	CopyScanFields((Scan *) from, (Scan *) newnode);

	return newnode;
227 228 229
}

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

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

244 245
	/*
	 * copy remainder of node
246 247 248
	 */
	newnode->indxid = listCopy(from->indxid);
	Node_Copy(from, newnode, indxqual);
V
Vadim B. Mikheev 已提交
249
	Node_Copy(from, newnode, indxqualorig);
250
	newnode->indxorderdir = from->indxorderdir;
251

252 253 254 255 256 257
	/*
	 * We must add subplans in index quals to the new plan's subPlan list
	 */
	if (from->scan.plan.subPlan != NIL)
	{
		newnode->scan.plan.subPlan = nconc(newnode->scan.plan.subPlan,
258
							  pull_subplans((Node *) newnode->indxqual));
259
		newnode->scan.plan.subPlan = nconc(newnode->scan.plan.subPlan,
260
						  pull_subplans((Node *) newnode->indxqualorig));
261 262
	}

263
	return newnode;
264 265
}

266
/* ----------------
267
 *				_copyTidScan
268 269 270 271 272
 * ----------------
 */
static TidScan *
_copyTidScan(TidScan *from)
{
273
	TidScan    *newnode = makeNode(TidScan);
274

275 276
	/*
	 * copy node superclass fields
277 278 279
	 */
	CopyPlanFields((Plan *) from, (Plan *) newnode);
	CopyScanFields((Scan *) from, (Scan *) newnode);
280 281 282

	/*
	 * copy remainder of node
283 284 285 286 287 288 289
	 */
	newnode->needRescan = from->needRescan;
	Node_Copy(from, newnode, tideval);

	return newnode;
}

290 291 292 293 294 295 296
/* ----------------
 *		_copySubqueryScan
 * ----------------
 */
static SubqueryScan *
_copySubqueryScan(SubqueryScan *from)
{
B
Bruce Momjian 已提交
297
	SubqueryScan *newnode = makeNode(SubqueryScan);
298

299 300
	/*
	 * copy node superclass fields
301 302 303 304
	 */
	CopyPlanFields((Plan *) from, (Plan *) newnode);
	CopyScanFields((Scan *) from, (Scan *) newnode);

305 306
	/*
	 * copy remainder of node
307 308 309 310 311 312
	 */
	Node_Copy(from, newnode, subplan);

	return newnode;
}

313

314
/* ----------------
315
 *		CopyJoinFields
316
 *
317 318
 *		This function copies the fields of the Join node.  It is used by
 *		all the copy functions for classes which inherit from Join.
319 320 321
 * ----------------
 */
static void
322
CopyJoinFields(Join *from, Join *newnode)
323
{
324 325 326 327 328
	newnode->jointype = from->jointype;
	Node_Copy(from, newnode, joinqual);
	/* subPlan list must point to subplans in the new subtree, not the old */
	if (from->plan.subPlan != NIL)
		newnode->plan.subPlan = nconc(newnode->plan.subPlan,
B
Bruce Momjian 已提交
329
							  pull_subplans((Node *) newnode->joinqual));
330 331 332 333
}


/* ----------------
334
 *		_copyJoin
335 336
 * ----------------
 */
337
static Join *
338
_copyJoin(Join *from)
339
{
340
	Join	   *newnode = makeNode(Join);
341

342 343
	/*
	 * copy node superclass fields
344 345 346 347 348
	 */
	CopyPlanFields((Plan *) from, (Plan *) newnode);
	CopyJoinFields(from, newnode);

	return newnode;
349 350 351 352
}


/* ----------------
353
 *		_copyNestLoop
354 355 356
 * ----------------
 */
static NestLoop *
357
_copyNestLoop(NestLoop *from)
358
{
359
	NestLoop   *newnode = makeNode(NestLoop);
360

361 362
	/*
	 * copy node superclass fields
363 364 365 366 367
	 */
	CopyPlanFields((Plan *) from, (Plan *) newnode);
	CopyJoinFields((Join *) from, (Join *) newnode);

	return newnode;
368 369 370 371
}


/* ----------------
372
 *		_copyMergeJoin
373 374 375
 * ----------------
 */
static MergeJoin *
376
_copyMergeJoin(MergeJoin *from)
377
{
378
	MergeJoin  *newnode = makeNode(MergeJoin);
379

380 381
	/*
	 * copy node superclass fields
382 383 384 385
	 */
	CopyPlanFields((Plan *) from, (Plan *) newnode);
	CopyJoinFields((Join *) from, (Join *) newnode);

386 387
	/*
	 * copy remainder of node
388 389 390
	 */
	Node_Copy(from, newnode, mergeclauses);

391 392 393
	/*
	 * We must add subplans in mergeclauses to the new plan's subPlan list
	 */
394 395
	if (from->join.plan.subPlan != NIL)
		newnode->join.plan.subPlan = nconc(newnode->join.plan.subPlan,
396
						  pull_subplans((Node *) newnode->mergeclauses));
397

398
	return newnode;
399 400 401
}

/* ----------------
402
 *		_copyHashJoin
403 404 405
 * ----------------
 */
static HashJoin *
406
_copyHashJoin(HashJoin *from)
407
{
408
	HashJoin   *newnode = makeNode(HashJoin);
409

410 411
	/*
	 * copy node superclass fields
412 413 414 415
	 */
	CopyPlanFields((Plan *) from, (Plan *) newnode);
	CopyJoinFields((Join *) from, (Join *) newnode);

416 417
	/*
	 * copy remainder of node
418 419 420 421
	 */
	Node_Copy(from, newnode, hashclauses);
	newnode->hashjoinop = from->hashjoinop;

422 423 424
	/*
	 * We must add subplans in hashclauses to the new plan's subPlan list
	 */
425 426
	if (from->join.plan.subPlan != NIL)
		newnode->join.plan.subPlan = nconc(newnode->join.plan.subPlan,
427
						   pull_subplans((Node *) newnode->hashclauses));
428

429
	return newnode;
430 431 432 433
}


/* ----------------
434
 *		_copyMaterial
435 436 437
 * ----------------
 */
static Material *
438
_copyMaterial(Material *from)
439
{
440
	Material   *newnode = makeNode(Material);
441

442 443
	/*
	 * copy node superclass fields
444 445 446 447
	 */
	CopyPlanFields((Plan *) from, (Plan *) newnode);

	return newnode;
448 449 450 451
}


/* ----------------
452
 *		_copySort
453 454
 * ----------------
 */
455
static Sort *
456
_copySort(Sort *from)
457
{
458
	Sort	   *newnode = makeNode(Sort);
459

460 461
	/*
	 * copy node superclass fields
462 463
	 */
	CopyPlanFields((Plan *) from, (Plan *) newnode);
464 465

	newnode->keycount = from->keycount;
466 467

	return newnode;
468 469
}

V
Vadim B. Mikheev 已提交
470 471 472 473 474 475 476 477 478

/* ----------------
 *		_copyGroup
 * ----------------
 */
static Group *
_copyGroup(Group *from)
{
	Group	   *newnode = makeNode(Group);
479

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

V
Vadim B. Mikheev 已提交
482 483
	newnode->tuplePerGroup = from->tuplePerGroup;
	newnode->numCols = from->numCols;
484 485
	newnode->grpColIdx = palloc(from->numCols * sizeof(AttrNumber));
	memcpy(newnode->grpColIdx, from->grpColIdx, from->numCols * sizeof(AttrNumber));
V
Vadim B. Mikheev 已提交
486 487 488 489

	return newnode;
}

490
/* ---------------
491
 *	_copyAgg
492 493
 * --------------
 */
494
static Agg *
B
Bruce Momjian 已提交
495
_copyAgg(Agg *from)
496
{
497
	Agg		   *newnode = makeNode(Agg);
498 499 500 501

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

	return newnode;
502 503
}

504 505 506 507 508 509 510
/* ---------------
 *	_copyGroupClause
 * --------------
 */
static GroupClause *
_copyGroupClause(GroupClause *from)
{
511
	GroupClause *newnode = makeNode(GroupClause);
512

513 514
	newnode->tleSortGroupRef = from->tleSortGroupRef;
	newnode->sortop = from->sortop;
515

516
	return newnode;
517 518
}

519
/* ----------------
520
 *		_copyUnique
521 522
 * ----------------
 */
523
static Unique *
524
_copyUnique(Unique *from)
525
{
526
	Unique	   *newnode = makeNode(Unique);
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
	newnode->numCols = from->numCols;
	newnode->uniqColIdx = palloc(from->numCols * sizeof(AttrNumber));
	memcpy(newnode->uniqColIdx, from->uniqColIdx, from->numCols * sizeof(AttrNumber));
539 540

	return newnode;
541 542
}

543 544 545 546 547 548 549 550 551
/* ----------------
 *		_copySetOp
 * ----------------
 */
static SetOp *
_copySetOp(SetOp *from)
{
	SetOp	   *newnode = makeNode(SetOp);

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

557 558
	/*
	 * copy remainder of node
559 560 561 562 563 564 565 566 567
	 */
	newnode->cmd = from->cmd;
	newnode->numCols = from->numCols;
	newnode->dupColIdx = palloc(from->numCols * sizeof(AttrNumber));
	memcpy(newnode->dupColIdx, from->dupColIdx, from->numCols * sizeof(AttrNumber));
	newnode->flagColIdx = from->flagColIdx;

	return newnode;
}
568

569 570 571 572 573 574 575 576 577
/* ----------------
 *		_copyLimit
 * ----------------
 */
static Limit *
_copyLimit(Limit *from)
{
	Limit	   *newnode = makeNode(Limit);

578 579
	/*
	 * copy node superclass fields
580 581 582
	 */
	CopyPlanFields((Plan *) from, (Plan *) newnode);

583 584
	/*
	 * copy remainder of node
585 586 587 588 589 590 591
	 */
	Node_Copy(from, newnode, limitOffset);
	Node_Copy(from, newnode, limitCount);

	return newnode;
}

592
/* ----------------
593
 *		_copyHash
594 595
 * ----------------
 */
596
static Hash *
597
_copyHash(Hash *from)
598
{
599
	Hash	   *newnode = makeNode(Hash);
600

601 602
	/*
	 * copy node superclass fields
603 604 605
	 */
	CopyPlanFields((Plan *) from, (Plan *) newnode);

606 607
	/*
	 * copy remainder of node
608 609 610 611
	 */
	Node_Copy(from, newnode, hashkey);

	return newnode;
612 613
}

V
Vadim B. Mikheev 已提交
614 615 616
static SubPlan *
_copySubPlan(SubPlan *from)
{
617 618
	SubPlan    *newnode = makeNode(SubPlan);

V
Vadim B. Mikheev 已提交
619 620 621
	Node_Copy(from, newnode, plan);
	newnode->plan_id = from->plan_id;
	Node_Copy(from, newnode, rtable);
622 623
	newnode->setParam = listCopy(from->setParam);
	newnode->parParam = listCopy(from->parParam);
V
Vadim B. Mikheev 已提交
624 625
	Node_Copy(from, newnode, sublink);

626
	/* do not copy execution state */
627
	newnode->needShutdown = false;
628 629
	newnode->curTuple = NULL;

V
Vadim B. Mikheev 已提交
630 631 632
	return newnode;
}

633
/* ****************************************************************
634
 *					   primnodes.h copy functions
635 636 637 638
 * ****************************************************************
 */

/* ----------------
639
 *		_copyResdom
640 641
 * ----------------
 */
642
static Resdom *
643
_copyResdom(Resdom *from)
644
{
645
	Resdom	   *newnode = makeNode(Resdom);
646 647 648

	newnode->resno = from->resno;
	newnode->restype = from->restype;
649
	newnode->restypmod = from->restypmod;
650
	if (from->resname != NULL)
B
Bruce Momjian 已提交
651
		newnode->resname = pstrdup(from->resname);
652
	newnode->ressortgroupref = from->ressortgroupref;
653 654 655 656 657
	newnode->reskey = from->reskey;
	newnode->reskeyop = from->reskeyop;
	newnode->resjunk = from->resjunk;

	return newnode;
658 659
}

660
static Fjoin *
661
_copyFjoin(Fjoin *from)
662
{
663
	Fjoin	   *newnode = makeNode(Fjoin);
664

665 666
	/*
	 * copy node superclass fields
667 668 669 670 671 672 673 674 675 676 677 678 679
	 */

	newnode->fj_initialized = from->fj_initialized;
	newnode->fj_nNodes = from->fj_nNodes;

	Node_Copy(from, newnode, fj_innerNode);

	newnode->fj_results = (DatumPtr)
		palloc((from->fj_nNodes) * sizeof(Datum));
	memmove(from->fj_results,
			newnode->fj_results,
			(from->fj_nNodes) * sizeof(Datum));

B
Bruce Momjian 已提交
680 681
	newnode->fj_alwaysDone = (BoolPtr)
		palloc((from->fj_nNodes) * sizeof(bool));
682 683 684 685 686 687
	memmove(from->fj_alwaysDone,
			newnode->fj_alwaysDone,
			(from->fj_nNodes) * sizeof(bool));


	return newnode;
688 689 690
}

/* ----------------
691
 *		_copyExpr
692 693
 * ----------------
 */
694
static Expr *
695
_copyExpr(Expr *from)
696
{
697
	Expr	   *newnode = makeNode(Expr);
698

699 700
	/*
	 * copy node superclass fields
701 702 703 704 705 706 707 708
	 */
	newnode->typeOid = from->typeOid;
	newnode->opType = from->opType;

	Node_Copy(from, newnode, oper);
	Node_Copy(from, newnode, args);

	return newnode;
709 710 711
}

/* ----------------
712
 *		_copyVar
713 714
 * ----------------
 */
715
static Var *
716
_copyVar(Var *from)
717
{
718
	Var		   *newnode = makeNode(Var);
719

720 721
	/*
	 * copy remainder of node
722 723 724 725
	 */
	newnode->varno = from->varno;
	newnode->varattno = from->varattno;
	newnode->vartype = from->vartype;
726
	newnode->vartypmod = from->vartypmod;
727
	newnode->varlevelsup = from->varlevelsup;
728 729 730 731 732

	newnode->varnoold = from->varnoold;
	newnode->varoattno = from->varoattno;

	return newnode;
733 734
}

735 736 737 738 739 740 741 742 743 744 745 746
static Attr *
_copyAttr(Attr *from)
{
	Attr	   *newnode = makeNode(Attr);

	if (from->relname)
		newnode->relname = pstrdup(from->relname);
	Node_Copy(from, newnode, attrs);

	return newnode;
}

747
/* ----------------
748
 *		_copyOper
749 750
 * ----------------
 */
751
static Oper *
752
_copyOper(Oper *from)
753
{
754
	Oper	   *newnode = makeNode(Oper);
755

756 757
	/*
	 * copy remainder of node
758 759 760 761
	 */
	newnode->opno = from->opno;
	newnode->opid = from->opid;
	newnode->opresulttype = from->opresulttype;
762 763
	/* Do not copy the run-time state, if any */
	newnode->op_fcache = NULL;
764 765

	return newnode;
766 767 768
}

/* ----------------
769
 *		_copyConst
770 771
 * ----------------
 */
772
static Const *
773
_copyConst(Const *from)
774
{
775
	Const	   *newnode = makeNode(Const);
776

777 778
	/*
	 * copy remainder of node
779
	 */
780 781 782
	newnode->consttype = from->consttype;
	newnode->constlen = from->constlen;

783
	if (from->constbyval || from->constisnull)
784
	{
785 786 787 788
		/*
		 * passed by value so just copy the datum. Also, don't try to copy
		 * struct when value is null!
		 *
789
		 */
790
		newnode->constvalue = from->constvalue;
791
	}
792
	else
793
	{
794 795
		/*
		 * not passed by value. datum contains a pointer.
796
		 */
797 798 799 800 801 802 803 804
		int			length = from->constlen;

		if (length == -1)		/* variable-length type? */
			length = VARSIZE(from->constvalue);
		newnode->constvalue = PointerGetDatum(palloc(length));
		memcpy(DatumGetPointer(newnode->constvalue),
			   DatumGetPointer(from->constvalue),
			   length);
805
	}
806

807 808
	newnode->constisnull = from->constisnull;
	newnode->constbyval = from->constbyval;
B
Bruce Momjian 已提交
809 810
	newnode->constisset = from->constisset;
	newnode->constiscast = from->constiscast;
811 812

	return newnode;
813 814 815
}

/* ----------------
816
 *		_copyParam
817 818
 * ----------------
 */
819
static Param *
820
_copyParam(Param *from)
821
{
822
	Param	   *newnode = makeNode(Param);
823

824 825
	/*
	 * copy remainder of node
826 827 828 829 830 831 832 833 834
	 */
	newnode->paramkind = from->paramkind;
	newnode->paramid = from->paramid;

	if (from->paramname != NULL)
		newnode->paramname = pstrdup(from->paramname);
	newnode->paramtype = from->paramtype;

	return newnode;
835 836 837
}

/* ----------------
838
 *		_copyFunc
839 840
 * ----------------
 */
841
static Func *
842
_copyFunc(Func *from)
843
{
844
	Func	   *newnode = makeNode(Func);
845

846 847
	/*
	 * copy remainder of node
848 849 850
	 */
	newnode->funcid = from->funcid;
	newnode->functype = from->functype;
851 852
	/* Do not copy the run-time state, if any */
	newnode->func_fcache = NULL;
853 854

	return newnode;
855 856 857
}

/* ----------------
B
Bruce Momjian 已提交
858
 *		_copyAggref
859 860
 * ----------------
 */
B
Bruce Momjian 已提交
861
static Aggref *
862
_copyAggref(Aggref *from)
863
{
B
Bruce Momjian 已提交
864
	Aggref	   *newnode = makeNode(Aggref);
865

866 867
	/*
	 * copy remainder of node
868 869 870 871 872
	 */
	newnode->aggname = pstrdup(from->aggname);
	newnode->basetype = from->basetype;
	newnode->aggtype = from->aggtype;
	Node_Copy(from, newnode, target);
873 874
	newnode->aggstar = from->aggstar;
	newnode->aggdistinct = from->aggdistinct;
875
	newnode->aggno = from->aggno;		/* probably not needed */
876 877

	return newnode;
878 879
}

880 881 882 883 884 885 886
/* ----------------
 *		_copySubLink
 * ----------------
 */
static SubLink *
_copySubLink(SubLink *from)
{
887
	SubLink    *newnode = makeNode(SubLink);
888

889 890
	/*
	 * copy remainder of node
891 892 893 894
	 */
	newnode->subLinkType = from->subLinkType;
	newnode->useor = from->useor;
	Node_Copy(from, newnode, lefthand);
B
Bruce Momjian 已提交
895
	Node_Copy(from, newnode, oper);
896 897 898 899 900
	Node_Copy(from, newnode, subselect);

	return newnode;
}

901 902 903 904 905 906 907 908 909
/* ----------------
 *		_copyFieldSelect
 * ----------------
 */
static FieldSelect *
_copyFieldSelect(FieldSelect *from)
{
	FieldSelect *newnode = makeNode(FieldSelect);

910 911
	/*
	 * copy remainder of node
912 913 914 915 916 917 918 919 920
	 */
	Node_Copy(from, newnode, arg);
	newnode->fieldnum = from->fieldnum;
	newnode->resulttype = from->resulttype;
	newnode->resulttypmod = from->resulttypmod;

	return newnode;
}

921 922 923 924 925 926 927
/* ----------------
 *		_copyRelabelType
 * ----------------
 */
static RelabelType *
_copyRelabelType(RelabelType *from)
{
928
	RelabelType *newnode = makeNode(RelabelType);
929

930 931
	/*
	 * copy remainder of node
932 933 934 935 936 937 938 939
	 */
	Node_Copy(from, newnode, arg);
	newnode->resulttype = from->resulttype;
	newnode->resulttypmod = from->resulttypmod;

	return newnode;
}

940 941 942 943 944 945 946 947 948 949
static RangeTblRef *
_copyRangeTblRef(RangeTblRef *from)
{
	RangeTblRef *newnode = makeNode(RangeTblRef);

	newnode->rtindex = from->rtindex;

	return newnode;
}

950 951 952
static FromExpr *
_copyFromExpr(FromExpr *from)
{
B
Bruce Momjian 已提交
953
	FromExpr   *newnode = makeNode(FromExpr);
954 955 956 957 958 959 960

	Node_Copy(from, newnode, fromlist);
	Node_Copy(from, newnode, quals);

	return newnode;
}

961 962 963
static JoinExpr *
_copyJoinExpr(JoinExpr *from)
{
B
Bruce Momjian 已提交
964
	JoinExpr   *newnode = makeNode(JoinExpr);
965 966 967 968 969 970 971 972 973 974 975 976 977 978

	newnode->jointype = from->jointype;
	newnode->isNatural = from->isNatural;
	Node_Copy(from, newnode, larg);
	Node_Copy(from, newnode, rarg);
	Node_Copy(from, newnode, using);
	Node_Copy(from, newnode, quals);
	Node_Copy(from, newnode, alias);
	Node_Copy(from, newnode, colnames);
	Node_Copy(from, newnode, colvars);

	return newnode;
}

T
Thomas G. Lockhart 已提交
979 980 981 982 983
/* ----------------
 *		_copyCaseExpr
 * ----------------
 */
static CaseExpr *
984
_copyCaseExpr(CaseExpr *from)
T
Thomas G. Lockhart 已提交
985 986 987
{
	CaseExpr   *newnode = makeNode(CaseExpr);

988 989
	/*
	 * copy remainder of node
T
Thomas G. Lockhart 已提交
990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004
	 */
	newnode->casetype = from->casetype;

	Node_Copy(from, newnode, arg);
	Node_Copy(from, newnode, args);
	Node_Copy(from, newnode, defresult);

	return newnode;
}

/* ----------------
 *		_copyCaseWhen
 * ----------------
 */
static CaseWhen *
1005
_copyCaseWhen(CaseWhen *from)
T
Thomas G. Lockhart 已提交
1006 1007 1008
{
	CaseWhen   *newnode = makeNode(CaseWhen);

1009 1010
	/*
	 * copy remainder of node
T
Thomas G. Lockhart 已提交
1011 1012 1013 1014 1015 1016 1017
	 */
	Node_Copy(from, newnode, expr);
	Node_Copy(from, newnode, result);

	return newnode;
}

1018 1019 1020 1021 1022 1023 1024
/* ----------------
 *		_copyNullTest
 * ----------------
 */
static NullTest *
_copyNullTest(NullTest *from)
{
1025
	NullTest   *newnode = makeNode(NullTest);
1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053

	/*
	 * copy remainder of node
	 */
	Node_Copy(from, newnode, arg);
	newnode->nulltesttype = from->nulltesttype;

	return newnode;
}

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

	/*
	 * copy remainder of node
	 */
	Node_Copy(from, newnode, arg);
	newnode->booltesttype = from->booltesttype;

	return newnode;
}

1054
static ArrayRef *
B
Bruce Momjian 已提交
1055
_copyArrayRef(ArrayRef *from)
1056
{
1057
	ArrayRef   *newnode = makeNode(ArrayRef);
1058

1059 1060
	/*
	 * copy remainder of node
1061 1062 1063
	 */
	newnode->refattrlength = from->refattrlength;
	newnode->refelemlength = from->refelemlength;
B
Bruce Momjian 已提交
1064
	newnode->refelemtype = from->refelemtype;
1065 1066 1067 1068 1069 1070 1071 1072
	newnode->refelembyval = from->refelembyval;

	Node_Copy(from, newnode, refupperindexpr);
	Node_Copy(from, newnode, reflowerindexpr);
	Node_Copy(from, newnode, refexpr);
	Node_Copy(from, newnode, refassgnexpr);

	return newnode;
1073 1074 1075
}

/* ****************************************************************
1076
 *						relation.h copy functions
1077 1078 1079 1080
 * ****************************************************************
 */

/* ----------------
B
Bruce Momjian 已提交
1081
 *		_copyRelOptInfo
1082 1083 1084
 * ----------------
 */
/*
B
Bruce Momjian 已提交
1085 1086 1087
 *	when you change this, also make sure to fix up xfunc_copyRelOptInfo in
 *	planner/path/xfunc.c accordingly!!!
 *		-- JMH, 8/2/93
1088
 */
B
Bruce Momjian 已提交
1089
static RelOptInfo *
1090
_copyRelOptInfo(RelOptInfo *from)
1091
{
1092
	RelOptInfo *newnode = makeNode(RelOptInfo);
1093 1094 1095

	newnode->relids = listCopy(from->relids);

1096
	newnode->rows = from->rows;
1097
	newnode->width = from->width;
1098

1099 1100
	Node_Copy(from, newnode, targetlist);
	Node_Copy(from, newnode, pathlist);
1101 1102 1103
	/* XXX cheapest-path fields should point to members of pathlist? */
	Node_Copy(from, newnode, cheapest_startup_path);
	Node_Copy(from, newnode, cheapest_total_path);
1104 1105
	newnode->pruneable = from->pruneable;

1106
	newnode->issubquery = from->issubquery;
1107
	Node_Copy(from, newnode, indexlist);
1108 1109
	newnode->pages = from->pages;
	newnode->tuples = from->tuples;
1110
	Node_Copy(from, newnode, subplan);
1111

1112
	Node_Copy(from, newnode, baserestrictinfo);
1113
	newnode->baserestrictcost = from->baserestrictcost;
1114
	newnode->outerjoinset = listCopy(from->outerjoinset);
1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128
	Node_Copy(from, newnode, joininfo);
	Node_Copy(from, newnode, innerjoin);

	return newnode;
}

/* ----------------
 *		_copyIndexOptInfo
 * ----------------
 */
static IndexOptInfo *
_copyIndexOptInfo(IndexOptInfo *from)
{
	IndexOptInfo *newnode = makeNode(IndexOptInfo);
1129
	Size		len;
1130 1131 1132 1133 1134

	newnode->indexoid = from->indexoid;
	newnode->pages = from->pages;
	newnode->tuples = from->tuples;

1135 1136 1137
	newnode->ncolumns = from->ncolumns;
	newnode->nkeys = from->nkeys;

1138 1139
	if (from->classlist)
	{
1140 1141 1142 1143
		/* copy the trailing zero too */
		len = (from->ncolumns + 1) * sizeof(Oid);
		newnode->classlist = (Oid *) palloc(len);
		memcpy(newnode->classlist, from->classlist, len);
1144
	}
1145 1146 1147

	if (from->indexkeys)
	{
1148 1149 1150 1151
		/* copy the trailing zero too */
		len = (from->nkeys + 1) * sizeof(int);
		newnode->indexkeys = (int *) palloc(len);
		memcpy(newnode->indexkeys, from->indexkeys, len);
1152
	}
1153 1154 1155

	if (from->ordering)
	{
1156 1157 1158 1159
		/* copy the trailing zero too */
		len = (from->ncolumns + 1) * sizeof(Oid);
		newnode->ordering = (Oid *) palloc(len);
		memcpy(newnode->ordering, from->ordering, len);
1160
	}
1161

1162
	newnode->relam = from->relam;
1163
	newnode->amcostestimate = from->amcostestimate;
1164 1165
	newnode->indproc = from->indproc;
	Node_Copy(from, newnode, indpred);
1166
	newnode->unique = from->unique;
1167

1168
	return newnode;
1169 1170 1171
}

/* ----------------
1172
 *		CopyPathFields
1173
 *
1174 1175
 *		This function copies the fields of the Path node.  It is used by
 *		all the copy functions for classes which inherit from Path.
1176 1177 1178
 * ----------------
 */
static void
1179
CopyPathFields(Path *from, Path *newnode)
1180
{
1181 1182 1183 1184 1185 1186 1187
	/*
	 * Modify the next line, since it causes the copying to cycle (i.e.
	 * the parent points right back here! -- JMH, 7/7/92. Old version:
	 * Node_Copy(from, newnode, parent);
	 */
	newnode->parent = from->parent;

1188 1189
	newnode->startup_cost = from->startup_cost;
	newnode->total_cost = from->total_cost;
1190

1191
	newnode->pathtype = from->pathtype;
1192

1193
	Node_Copy(from, newnode, pathkeys);
1194 1195 1196
}

/* ----------------
1197
 *		_copyPath
1198 1199
 * ----------------
 */
1200
static Path *
1201
_copyPath(Path *from)
1202
{
1203
	Path	   *newnode = makeNode(Path);
1204 1205 1206 1207

	CopyPathFields(from, newnode);

	return newnode;
1208 1209 1210
}

/* ----------------
1211
 *		_copyIndexPath
1212 1213 1214
 * ----------------
 */
static IndexPath *
1215
_copyIndexPath(IndexPath *from)
1216
{
1217
	IndexPath  *newnode = makeNode(IndexPath);
1218

1219 1220
	/*
	 * copy the node superclass fields
1221 1222 1223
	 */
	CopyPathFields((Path *) from, (Path *) newnode);

1224 1225
	/*
	 * copy remainder of node
1226
	 */
1227
	Node_Copy(from, newnode, indexinfo);
1228
	Node_Copy(from, newnode, indexqual);
1229
	newnode->indexscandir = from->indexscandir;
1230
	newnode->joinrelids = listCopy(from->joinrelids);
1231
	newnode->alljoinquals = from->alljoinquals;
1232
	newnode->rows = from->rows;
1233 1234

	return newnode;
1235 1236
}

1237
/* ----------------
1238
 *				_copyTidPath
1239 1240 1241 1242 1243
 * ----------------
 */
static TidPath *
_copyTidPath(TidPath *from)
{
1244
	TidPath    *newnode = makeNode(TidPath);
1245

1246 1247
	/*
	 * copy the node superclass fields
1248 1249 1250
	 */
	CopyPathFields((Path *) from, (Path *) newnode);

1251 1252
	/*
	 * copy remainder of node
1253 1254 1255 1256 1257 1258
	 */
	Node_Copy(from, newnode, tideval);
	newnode->unjoined_relids = listCopy(from->unjoined_relids);

	return newnode;
}
1259

1260 1261 1262 1263 1264 1265 1266
/* ----------------
 *				_copyAppendPath
 * ----------------
 */
static AppendPath *
_copyAppendPath(AppendPath *from)
{
B
Bruce Momjian 已提交
1267
	AppendPath *newnode = makeNode(AppendPath);
1268

1269 1270
	/*
	 * copy the node superclass fields
1271 1272 1273
	 */
	CopyPathFields((Path *) from, (Path *) newnode);

1274 1275
	/*
	 * copy remainder of node
1276 1277 1278 1279 1280 1281
	 */
	Node_Copy(from, newnode, subpaths);

	return newnode;
}

1282
/* ----------------
1283
 *		CopyJoinPathFields
1284
 *
1285 1286
 *		This function copies the fields of the JoinPath node.  It is used by
 *		all the copy functions for classes which inherit from JoinPath.
1287 1288 1289
 * ----------------
 */
static void
1290
CopyJoinPathFields(JoinPath *from, JoinPath *newnode)
1291
{
1292
	newnode->jointype = from->jointype;
1293 1294
	Node_Copy(from, newnode, outerjoinpath);
	Node_Copy(from, newnode, innerjoinpath);
1295
	Node_Copy(from, newnode, joinrestrictinfo);
1296 1297 1298
}

/* ----------------
1299
 *		_copyNestPath
1300 1301
 * ----------------
 */
1302
static NestPath *
1303
_copyNestPath(NestPath *from)
1304
{
1305
	NestPath   *newnode = makeNode(NestPath);
1306

1307 1308
	/*
	 * copy the node superclass fields
1309 1310
	 */
	CopyPathFields((Path *) from, (Path *) newnode);
1311
	CopyJoinPathFields((JoinPath *) from, (JoinPath *) newnode);
1312 1313

	return newnode;
1314 1315 1316
}

/* ----------------
1317
 *		_copyMergePath
1318 1319 1320
 * ----------------
 */
static MergePath *
1321
_copyMergePath(MergePath *from)
1322
{
1323
	MergePath  *newnode = makeNode(MergePath);
1324

1325 1326
	/*
	 * copy the node superclass fields
1327 1328
	 */
	CopyPathFields((Path *) from, (Path *) newnode);
1329
	CopyJoinPathFields((JoinPath *) from, (JoinPath *) newnode);
1330

1331 1332
	/*
	 * copy the remainder of the node
1333 1334 1335 1336 1337 1338
	 */
	Node_Copy(from, newnode, path_mergeclauses);
	Node_Copy(from, newnode, outersortkeys);
	Node_Copy(from, newnode, innersortkeys);

	return newnode;
1339 1340 1341
}

/* ----------------
1342
 *		_copyHashPath
1343 1344 1345
 * ----------------
 */
static HashPath *
1346
_copyHashPath(HashPath *from)
1347
{
1348
	HashPath   *newnode = makeNode(HashPath);
1349

1350 1351
	/*
	 * copy the node superclass fields
1352 1353
	 */
	CopyPathFields((Path *) from, (Path *) newnode);
1354
	CopyJoinPathFields((JoinPath *) from, (JoinPath *) newnode);
1355

1356 1357
	/*
	 * copy remainder of node
1358 1359 1360 1361
	 */
	Node_Copy(from, newnode, path_hashclauses);

	return newnode;
1362 1363 1364
}

/* ----------------
1365
 *		_copyPathKeyItem
1366 1367
 * ----------------
 */
1368 1369
static PathKeyItem *
_copyPathKeyItem(PathKeyItem *from)
1370
{
1371
	PathKeyItem *newnode = makeNode(PathKeyItem);
1372

1373 1374
	/*
	 * copy remainder of node
1375
	 */
1376 1377
	Node_Copy(from, newnode, key);
	newnode->sortop = from->sortop;
1378 1379

	return newnode;
1380 1381 1382
}

/* ----------------
1383
 *		_copyRestrictInfo
1384 1385
 * ----------------
 */
1386
static RestrictInfo *
1387
_copyRestrictInfo(RestrictInfo *from)
1388
{
1389
	RestrictInfo *newnode = makeNode(RestrictInfo);
1390

1391 1392
	/*
	 * copy remainder of node
1393 1394
	 */
	Node_Copy(from, newnode, clause);
1395
	newnode->ispusheddown = from->ispusheddown;
1396
	Node_Copy(from, newnode, subclauseindices);
1397 1398
	newnode->eval_cost = from->eval_cost;
	newnode->this_selec = from->this_selec;
1399 1400 1401
	newnode->mergejoinoperator = from->mergejoinoperator;
	newnode->left_sortop = from->left_sortop;
	newnode->right_sortop = from->right_sortop;
B
Bruce Momjian 已提交
1402 1403 1404 1405 1406

	/*
	 * Do not copy pathkeys, since they'd not be canonical in a copied
	 * query
	 */
1407 1408
	newnode->left_pathkey = NIL;
	newnode->right_pathkey = NIL;
1409
	newnode->hashjoinoperator = from->hashjoinoperator;
1410 1411
	newnode->left_bucketsize = from->left_bucketsize;
	newnode->right_bucketsize = from->right_bucketsize;
1412 1413

	return newnode;
1414 1415 1416
}

/* ----------------
1417
 *		_copyJoinInfo
1418 1419
 * ----------------
 */
1420
static JoinInfo *
1421
_copyJoinInfo(JoinInfo *from)
1422
{
1423
	JoinInfo   *newnode = makeNode(JoinInfo);
1424

1425 1426
	/*
	 * copy remainder of node
1427
	 */
B
Bruce Momjian 已提交
1428
	newnode->unjoined_relids = listCopy(from->unjoined_relids);
1429
	Node_Copy(from, newnode, jinfo_restrictinfo);
1430 1431

	return newnode;
1432 1433
}

1434
static Iter *
1435
_copyIter(Iter *from)
1436
{
1437
	Iter	   *newnode = makeNode(Iter);
1438 1439 1440

	Node_Copy(from, newnode, iterexpr);
	newnode->itertype = from->itertype;
1441

1442
	return newnode;
1443 1444
}

1445
static Stream *
1446
_copyStream(Stream *from)
1447
{
1448
	Stream	   *newnode = makeNode(Stream);
1449 1450 1451 1452

	newnode->pathptr = from->pathptr;
	newnode->cinfo = from->cinfo;
	newnode->clausetype = from->clausetype;
B
Bruce Momjian 已提交
1453

1454 1455 1456 1457 1458 1459
	newnode->upstream = (StreamPtr) NULL;		/* only copy nodes
												 * downwards! */
	Node_Copy(from, newnode, downstream);
	if (newnode->downstream)
		((Stream *) newnode->downstream)->upstream = (Stream *) newnode;

B
Bruce Momjian 已提交
1460 1461 1462 1463
	newnode->groupup = from->groupup;
	newnode->groupcost = from->groupcost;
	newnode->groupsel = from->groupsel;

1464
	return newnode;
1465 1466
}

1467 1468 1469
/* ****************************************************************
 *					parsenodes.h copy functions
 * ****************************************************************
1470 1471 1472
 */

static TargetEntry *
1473
_copyTargetEntry(TargetEntry *from)
1474
{
1475
	TargetEntry *newnode = makeNode(TargetEntry);
1476

1477 1478 1479 1480
	Node_Copy(from, newnode, resdom);
	Node_Copy(from, newnode, fjoin);
	Node_Copy(from, newnode, expr);
	return newnode;
1481 1482 1483
}

static RangeTblEntry *
1484
_copyRangeTblEntry(RangeTblEntry *from)
1485
{
1486
	RangeTblEntry *newnode = makeNode(RangeTblEntry);
1487 1488 1489

	if (from->relname)
		newnode->relname = pstrdup(from->relname);
B
Bruce Momjian 已提交
1490
	newnode->relid = from->relid;
1491
	Node_Copy(from, newnode, subquery);
1492 1493
	Node_Copy(from, newnode, alias);
	Node_Copy(from, newnode, eref);
B
Bruce Momjian 已提交
1494 1495
	newnode->inh = from->inh;
	newnode->inFromCl = from->inFromCl;
1496 1497 1498
	newnode->checkForRead = from->checkForRead;
	newnode->checkForWrite = from->checkForWrite;
	newnode->checkAsUser = from->checkAsUser;
1499 1500 1501 1502

	return newnode;
}

1503 1504 1505
static FkConstraint *
_copyFkConstraint(FkConstraint *from)
{
B
Bruce Momjian 已提交
1506
	FkConstraint *newnode = makeNode(FkConstraint);
1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518

	if (from->constr_name)
		newnode->constr_name = pstrdup(from->constr_name);
	if (from->pktable_name)
		newnode->pktable_name = pstrdup(from->pktable_name);
	Node_Copy(from, newnode, fk_attrs);
	Node_Copy(from, newnode, pk_attrs);
	if (from->match_type)
		newnode->match_type = pstrdup(from->match_type);
	newnode->actions = from->actions;
	newnode->deferrable = from->deferrable;
	newnode->initdeferred = from->initdeferred;
B
Bruce Momjian 已提交
1519

1520 1521 1522
	return newnode;
}

1523
static SortClause *
1524
_copySortClause(SortClause *from)
1525
{
1526
	SortClause *newnode = makeNode(SortClause);
1527

1528 1529
	newnode->tleSortGroupRef = from->tleSortGroupRef;
	newnode->sortop = from->sortop;
1530

1531
	return newnode;
1532 1533
}

1534 1535 1536
static A_Expr *
_copyAExpr(A_Expr *from)
{
B
Bruce Momjian 已提交
1537
	A_Expr	   *newnode = makeNode(A_Expr);
1538 1539 1540 1541 1542 1543 1544 1545 1546 1547

	newnode->oper = from->oper;
	if (from->opname)
		newnode->opname = pstrdup(from->opname);
	Node_Copy(from, newnode, lexpr);
	Node_Copy(from, newnode, rexpr);

	return newnode;
}

1548
static A_Const *
B
Bruce Momjian 已提交
1549
_copyAConst(A_Const *from)
1550
{
1551
	A_Const    *newnode = makeNode(A_Const);
1552

1553 1554
	newnode->val = *((Value *) (copyObject(&(from->val))));
	Node_Copy(from, newnode, typename);
1555

1556
	return newnode;
1557 1558
}

1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573
static ParamNo *
_copyParamNo(ParamNo *from)
{
	ParamNo    *newnode = makeNode(ParamNo);

	newnode->number = from->number;
	Node_Copy(from, newnode, typename);
	Node_Copy(from, newnode, indirection);

	return newnode;
}

static Ident *
_copyIdent(Ident *from)
{
B
Bruce Momjian 已提交
1574
	Ident	   *newnode = makeNode(Ident);
1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586

	if (from->name)
		newnode->name = pstrdup(from->name);
	Node_Copy(from, newnode, indirection);
	newnode->isRel = from->isRel;

	return newnode;
}

static FuncCall *
_copyFuncCall(FuncCall *from)
{
B
Bruce Momjian 已提交
1587
	FuncCall   *newnode = makeNode(FuncCall);
1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600

	if (from->funcname)
		newnode->funcname = pstrdup(from->funcname);
	Node_Copy(from, newnode, args);
	newnode->agg_star = from->agg_star;
	newnode->agg_distinct = from->agg_distinct;

	return newnode;
}

static A_Indices *
_copyAIndices(A_Indices *from)
{
B
Bruce Momjian 已提交
1601
	A_Indices  *newnode = makeNode(A_Indices);
1602 1603 1604 1605 1606 1607 1608 1609 1610 1611

	Node_Copy(from, newnode, lidx);
	Node_Copy(from, newnode, uidx);

	return newnode;
}

static ResTarget *
_copyResTarget(ResTarget *from)
{
B
Bruce Momjian 已提交
1612
	ResTarget  *newnode = makeNode(ResTarget);
1613 1614 1615 1616 1617 1618 1619 1620 1621

	if (from->name)
		newnode->name = pstrdup(from->name);
	Node_Copy(from, newnode, indirection);
	Node_Copy(from, newnode, val);

	return newnode;
}

1622
static TypeName *
1623
_copyTypeName(TypeName *from)
1624
{
1625
	TypeName   *newnode = makeNode(TypeName);
1626 1627 1628

	if (from->name)
		newnode->name = pstrdup(from->name);
B
Bruce Momjian 已提交
1629
	newnode->timezone = from->timezone;
1630
	newnode->setof = from->setof;
1631
	newnode->typmod = from->typmod;
1632 1633 1634
	Node_Copy(from, newnode, arrayBounds);

	return newnode;
1635 1636
}

1637 1638 1639
static SortGroupBy *
_copySortGroupBy(SortGroupBy *from)
{
B
Bruce Momjian 已提交
1640
	SortGroupBy *newnode = makeNode(SortGroupBy);
1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653

	if (from->useOp)
		newnode->useOp = pstrdup(from->useOp);
	Node_Copy(from, newnode, node);

	return newnode;
}

static RangeVar *
_copyRangeVar(RangeVar *from)
{
	RangeVar   *newnode = makeNode(RangeVar);

1654 1655
	if (from->relname)
		newnode->relname = pstrdup(from->relname);
1656
	newnode->inhOpt = from->inhOpt;
1657 1658 1659 1660 1661 1662 1663 1664
	Node_Copy(from, newnode, name);

	return newnode;
}

static RangeSubselect *
_copyRangeSubselect(RangeSubselect *from)
{
B
Bruce Momjian 已提交
1665
	RangeSubselect *newnode = makeNode(RangeSubselect);
1666 1667

	Node_Copy(from, newnode, subquery);
1668 1669 1670 1671 1672
	Node_Copy(from, newnode, name);

	return newnode;
}

1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683
static TypeCast *
_copyTypeCast(TypeCast *from)
{
	TypeCast   *newnode = makeNode(TypeCast);

	Node_Copy(from, newnode, arg);
	Node_Copy(from, newnode, typename);

	return newnode;
}

1684 1685 1686
static IndexElem *
_copyIndexElem(IndexElem *from)
{
B
Bruce Momjian 已提交
1687
	IndexElem  *newnode = makeNode(IndexElem);
1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700

	if (from->name)
		newnode->name = pstrdup(from->name);
	Node_Copy(from, newnode, args);
	if (from->class)
		newnode->class = pstrdup(from->class);

	return newnode;
}

static ColumnDef *
_copyColumnDef(ColumnDef *from)
{
B
Bruce Momjian 已提交
1701
	ColumnDef  *newnode = makeNode(ColumnDef);
1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717

	if (from->colname)
		newnode->colname = pstrdup(from->colname);
	Node_Copy(from, newnode, typename);
	newnode->is_not_null = from->is_not_null;
	Node_Copy(from, newnode, raw_default);
	if (from->cooked_default)
		newnode->cooked_default = pstrdup(from->cooked_default);
	Node_Copy(from, newnode, constraints);

	return newnode;
}

static Constraint *
_copyConstraint(Constraint *from)
{
B
Bruce Momjian 已提交
1718
	Constraint *newnode = makeNode(Constraint);
1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733

	newnode->contype = from->contype;
	if (from->name)
		newnode->name = pstrdup(from->name);
	Node_Copy(from, newnode, raw_expr);
	if (from->cooked_expr)
		newnode->cooked_expr = pstrdup(from->cooked_expr);
	Node_Copy(from, newnode, keys);

	return newnode;
}

static DefElem *
_copyDefElem(DefElem *from)
{
B
Bruce Momjian 已提交
1734
	DefElem    *newnode = makeNode(DefElem);
1735 1736 1737 1738 1739 1740 1741 1742

	if (from->defname)
		newnode->defname = pstrdup(from->defname);
	Node_Copy(from, newnode, arg);

	return newnode;
}

1743
static Query *
1744
_copyQuery(Query *from)
1745
{
1746
	Query	   *newnode = makeNode(Query);
1747

1748
	newnode->commandType = from->commandType;
1749
	Node_Copy(from, newnode, utilityStmt);
1750 1751 1752 1753
	newnode->resultRelation = from->resultRelation;
	if (from->into)
		newnode->into = pstrdup(from->into);
	newnode->isPortal = from->isPortal;
B
Bruce Momjian 已提交
1754
	newnode->isBinary = from->isBinary;
1755
	newnode->isTemp = from->isTemp;
1756 1757 1758
	newnode->hasAggs = from->hasAggs;
	newnode->hasSubLinks = from->hasSubLinks;

B
Bruce Momjian 已提交
1759
	Node_Copy(from, newnode, rtable);
1760 1761
	Node_Copy(from, newnode, jointree);

1762
	newnode->rowMarks = listCopy(from->rowMarks);
1763

1764 1765
	Node_Copy(from, newnode, targetList);

1766
	Node_Copy(from, newnode, groupClause);
B
Bruce Momjian 已提交
1767
	Node_Copy(from, newnode, havingQual);
1768 1769
	Node_Copy(from, newnode, distinctClause);
	Node_Copy(from, newnode, sortClause);
1770

B
Bruce Momjian 已提交
1771 1772 1773
	Node_Copy(from, newnode, limitOffset);
	Node_Copy(from, newnode, limitCount);

1774 1775
	Node_Copy(from, newnode, setOperations);

1776 1777
	newnode->resultRelations = listCopy(from->resultRelations);

1778 1779
	/*
	 * We do not copy the planner internal fields: base_rel_list,
1780 1781
	 * other_rel_list, join_rel_list, equi_key_list, query_pathkeys. Not
	 * entirely clear if this is right?
1782
	 */
1783

1784
	return newnode;
1785 1786
}

1787 1788 1789 1790
static InsertStmt *
_copyInsertStmt(InsertStmt *from)
{
	InsertStmt *newnode = makeNode(InsertStmt);
B
Bruce Momjian 已提交
1791

1792 1793 1794 1795
	if (from->relname)
		newnode->relname = pstrdup(from->relname);
	Node_Copy(from, newnode, cols);
	Node_Copy(from, newnode, targetList);
1796
	Node_Copy(from, newnode, selectStmt);
1797 1798 1799 1800 1801 1802 1803 1804

	return newnode;
}

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

1806 1807 1808
	if (from->relname)
		newnode->relname = pstrdup(from->relname);
	Node_Copy(from, newnode, whereClause);
1809
	newnode->inhOpt = from->inhOpt;
1810 1811 1812 1813 1814 1815 1816 1817

	return newnode;
}

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

1819 1820 1821 1822 1823
	if (from->relname)
		newnode->relname = pstrdup(from->relname);
	Node_Copy(from, newnode, targetList);
	Node_Copy(from, newnode, whereClause);
	Node_Copy(from, newnode, fromClause);
1824
	newnode->inhOpt = from->inhOpt;
1825 1826 1827 1828 1829 1830 1831 1832

	return newnode;
}

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

1834 1835 1836
	Node_Copy(from, newnode, distinctClause);
	if (from->into)
		newnode->into = pstrdup(from->into);
1837
	newnode->istemp = from->istemp;
1838
	Node_Copy(from, newnode, intoColNames);
1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850
	Node_Copy(from, newnode, targetList);
	Node_Copy(from, newnode, fromClause);
	Node_Copy(from, newnode, whereClause);
	Node_Copy(from, newnode, groupClause);
	Node_Copy(from, newnode, havingClause);
	Node_Copy(from, newnode, sortClause);
	if (from->portalname)
		newnode->portalname = pstrdup(from->portalname);
	newnode->binary = from->binary;
	Node_Copy(from, newnode, limitOffset);
	Node_Copy(from, newnode, limitCount);
	Node_Copy(from, newnode, forUpdate);
1851 1852 1853 1854
	newnode->op = from->op;
	newnode->all = from->all;
	Node_Copy(from, newnode, larg);
	Node_Copy(from, newnode, rarg);
1855 1856 1857 1858

	return newnode;
}

1859 1860 1861 1862
static SetOperationStmt *
_copySetOperationStmt(SetOperationStmt *from)
{
	SetOperationStmt *newnode = makeNode(SetOperationStmt);
B
Bruce Momjian 已提交
1863

1864 1865 1866 1867 1868 1869 1870 1871 1872
	newnode->op = from->op;
	newnode->all = from->all;
	Node_Copy(from, newnode, larg);
	Node_Copy(from, newnode, rarg);
	newnode->colTypes = listCopy(from->colTypes);

	return newnode;
}

1873 1874 1875 1876
static AlterTableStmt *
_copyAlterTableStmt(AlterTableStmt *from)
{
	AlterTableStmt *newnode = makeNode(AlterTableStmt);
B
Bruce Momjian 已提交
1877

1878 1879 1880
	newnode->subtype = from->subtype;
	if (from->relname)
		newnode->relname = pstrdup(from->relname);
1881
	newnode->inhOpt = from->inhOpt;
1882 1883 1884 1885 1886 1887 1888 1889
	if (from->name)
		newnode->name = pstrdup(from->name);
	Node_Copy(from, newnode, def);
	newnode->behavior = from->behavior;

	return newnode;
}

1890 1891
static GrantStmt *
_copyGrantStmt(GrantStmt *from)
1892
{
1893
	GrantStmt  *newnode = makeNode(GrantStmt);
B
Bruce Momjian 已提交
1894

1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912
	newnode->is_grant = from->is_grant;
	Node_Copy(from, newnode, relnames);
	if (from->privileges)
		newnode->privileges = pstrdup(from->privileges);
	Node_Copy(from, newnode, grantees);

	return newnode;
}

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

	if (from->username)
		newnode->username = pstrdup(from->username);
	if (from->groupname)
		newnode->groupname = pstrdup(from->groupname);
1913 1914 1915 1916

	return newnode;
}

1917 1918 1919
static ClosePortalStmt *
_copyClosePortalStmt(ClosePortalStmt *from)
{
1920
	ClosePortalStmt *newnode = makeNode(ClosePortalStmt);
1921 1922 1923 1924 1925 1926 1927

	if (from->portalname)
		newnode->portalname = pstrdup(from->portalname);

	return newnode;
}

1928 1929 1930 1931
static ClusterStmt *
_copyClusterStmt(ClusterStmt *from)
{
	ClusterStmt *newnode = makeNode(ClusterStmt);
B
Bruce Momjian 已提交
1932

1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943
	if (from->relname)
		newnode->relname = pstrdup(from->relname);
	if (from->indexname)
		newnode->indexname = pstrdup(from->indexname);

	return newnode;
}

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

1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964
	newnode->binary = from->binary;
	if (from->relname)
		newnode->relname = pstrdup(from->relname);
	newnode->oids = from->oids;
	newnode->direction = from->direction;
	if (from->filename)
		newnode->filename = pstrdup(from->filename);
	if (from->delimiter)
		newnode->delimiter = pstrdup(from->delimiter);
	if (from->null_print)
		newnode->null_print = pstrdup(from->null_print);

	return newnode;
}

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

1966 1967 1968 1969
	newnode->relname = pstrdup(from->relname);
	Node_Copy(from, newnode, tableElts);
	Node_Copy(from, newnode, inhRelnames);
	Node_Copy(from, newnode, constraints);
1970 1971
	newnode->istemp = from->istemp;
	newnode->hasoids = from->hasoids;
1972 1973 1974 1975 1976 1977 1978 1979

	return newnode;
}

static VersionStmt *
_copyVersionStmt(VersionStmt *from)
{
	VersionStmt *newnode = makeNode(VersionStmt);
B
Bruce Momjian 已提交
1980

1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992
	newnode->relname = pstrdup(from->relname);
	newnode->direction = from->direction;
	newnode->fromRelname = pstrdup(from->fromRelname);
	newnode->date = pstrdup(from->date);

	return newnode;
}

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

1994 1995 1996 1997 1998 1999 2000 2001 2002 2003
	newnode->defType = from->defType;
	newnode->defname = pstrdup(from->defname);
	Node_Copy(from, newnode, definition);

	return newnode;
}

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

2006 2007
	Node_Copy(from, newnode, names);
	newnode->removeType = from->removeType;
2008 2009 2010 2011

	return newnode;
}

2012 2013 2014
static TruncateStmt *
_copyTruncateStmt(TruncateStmt *from)
{
2015
	TruncateStmt *newnode = makeNode(TruncateStmt);
2016 2017 2018 2019 2020 2021

	newnode->relName = pstrdup(from->relName);

	return newnode;
}

2022 2023 2024 2025
static CommentStmt *
_copyCommentStmt(CommentStmt *from)
{
	CommentStmt *newnode = makeNode(CommentStmt);
B
Bruce Momjian 已提交
2026

2027 2028
	newnode->objtype = from->objtype;
	newnode->objname = pstrdup(from->objname);
T
Tom Lane 已提交
2029
	if (from->objproperty)
B
Bruce Momjian 已提交
2030
		newnode->objproperty = pstrdup(from->objproperty);
2031 2032 2033 2034 2035 2036 2037 2038 2039
	Node_Copy(from, newnode, objlist);
	newnode->comment = pstrdup(from->comment);

	return newnode;
}

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

2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052
	newnode->direction = from->direction;
	newnode->howMany = from->howMany;
	newnode->portalname = pstrdup(from->portalname);
	newnode->ismove = from->ismove;

	return newnode;
}

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

2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070
	newnode->idxname = pstrdup(from->idxname);
	newnode->relname = pstrdup(from->relname);
	newnode->accessMethod = pstrdup(from->accessMethod);
	Node_Copy(from, newnode, indexParams);
	Node_Copy(from, newnode, whereClause);
	Node_Copy(from, newnode, rangetable);
	newnode->unique = from->unique;
	newnode->primary = from->primary;

	return newnode;
}

static ProcedureStmt *
_copyProcedureStmt(ProcedureStmt *from)
{
	ProcedureStmt *newnode = makeNode(ProcedureStmt);
B
Bruce Momjian 已提交
2071

2072
	newnode->replace = from->replace;
2073
	newnode->funcname = pstrdup(from->funcname);
2074
	Node_Copy(from, newnode, argTypes);
2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086
	Node_Copy(from, newnode, returnType);
	Node_Copy(from, newnode, withClause);
	Node_Copy(from, newnode, as);
	newnode->language = pstrdup(from->language);

	return newnode;
}

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

2088
	newnode->aggname = pstrdup(from->aggname);
2089
	Node_Copy(from, newnode, aggtype);
2090 2091 2092 2093 2094 2095 2096 2097

	return newnode;
}

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

2099 2100 2101 2102 2103 2104 2105 2106 2107 2108
	newnode->funcname = pstrdup(from->funcname);
	Node_Copy(from, newnode, args);

	return newnode;
}

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

2110 2111 2112 2113 2114 2115 2116 2117 2118 2119
	newnode->opname = pstrdup(from->opname);
	Node_Copy(from, newnode, args);

	return newnode;
}

static RenameStmt *
_copyRenameStmt(RenameStmt *from)
{
	RenameStmt *newnode = makeNode(RenameStmt);
B
Bruce Momjian 已提交
2120

2121
	newnode->relname = pstrdup(from->relname);
2122
	newnode->inhOpt = from->inhOpt;
2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133
	if (from->column)
		newnode->column = pstrdup(from->column);
	if (from->newname)
		newnode->newname = pstrdup(from->newname);

	return newnode;
}

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

2136 2137 2138 2139 2140 2141 2142 2143 2144 2145
	newnode->rulename = pstrdup(from->rulename);
	Node_Copy(from, newnode, whereClause);
	newnode->event = from->event;
	Node_Copy(from, newnode, object);
	newnode->instead = from->instead;
	Node_Copy(from, newnode, actions);

	return newnode;
}

2146 2147 2148
static NotifyStmt *
_copyNotifyStmt(NotifyStmt *from)
{
2149
	NotifyStmt *newnode = makeNode(NotifyStmt);
2150 2151 2152 2153 2154 2155 2156 2157 2158 2159

	if (from->relname)
		newnode->relname = pstrdup(from->relname);

	return newnode;
}

static ListenStmt *
_copyListenStmt(ListenStmt *from)
{
2160
	ListenStmt *newnode = makeNode(ListenStmt);
2161 2162 2163 2164 2165 2166 2167 2168 2169 2170

	if (from->relname)
		newnode->relname = pstrdup(from->relname);

	return newnode;
}

static UnlistenStmt *
_copyUnlistenStmt(UnlistenStmt *from)
{
2171
	UnlistenStmt *newnode = makeNode(UnlistenStmt);
2172 2173 2174 2175 2176 2177 2178 2179 2180 2181

	if (from->relname)
		newnode->relname = pstrdup(from->relname);

	return newnode;
}

static TransactionStmt *
_copyTransactionStmt(TransactionStmt *from)
{
2182
	TransactionStmt *newnode = makeNode(TransactionStmt);
2183 2184 2185 2186 2187 2188

	newnode->command = from->command;

	return newnode;
}

2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201
static ViewStmt *
_copyViewStmt(ViewStmt *from)
{
	ViewStmt   *newnode = makeNode(ViewStmt);

	if (from->viewname)
		newnode->viewname = pstrdup(from->viewname);
	Node_Copy(from, newnode, aliases);
	Node_Copy(from, newnode, query);

	return newnode;
}

2202 2203 2204
static LoadStmt *
_copyLoadStmt(LoadStmt *from)
{
2205
	LoadStmt   *newnode = makeNode(LoadStmt);
2206 2207 2208 2209 2210 2211 2212

	if (from->filename)
		newnode->filename = pstrdup(from->filename);

	return newnode;
}

2213 2214 2215
static CreatedbStmt *
_copyCreatedbStmt(CreatedbStmt *from)
{
B
Bruce Momjian 已提交
2216
	CreatedbStmt *newnode = makeNode(CreatedbStmt);
2217 2218 2219 2220 2221

	if (from->dbname)
		newnode->dbname = pstrdup(from->dbname);
	if (from->dbpath)
		newnode->dbpath = pstrdup(from->dbpath);
2222 2223
	if (from->dbtemplate)
		newnode->dbtemplate = pstrdup(from->dbtemplate);
2224 2225 2226 2227 2228 2229 2230 2231
	newnode->encoding = from->encoding;

	return newnode;
}

static DropdbStmt *
_copyDropdbStmt(DropdbStmt *from)
{
B
Bruce Momjian 已提交
2232
	DropdbStmt *newnode = makeNode(DropdbStmt);
2233 2234 2235 2236 2237 2238 2239 2240 2241 2242

	if (from->dbname)
		newnode->dbname = pstrdup(from->dbname);

	return newnode;
}

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

2245
	newnode->vacuum = from->vacuum;
2246
	newnode->full = from->full;
2247
	newnode->analyze = from->analyze;
2248
	newnode->freeze = from->freeze;
2249
	newnode->verbose = from->verbose;
2250 2251
	if (from->vacrel)
		newnode->vacrel = pstrdup(from->vacrel);
2252
	Node_Copy(from, newnode, va_cols);
2253 2254 2255 2256 2257 2258 2259

	return newnode;
}

static ExplainStmt *
_copyExplainStmt(ExplainStmt *from)
{
B
Bruce Momjian 已提交
2260
	ExplainStmt *newnode = makeNode(ExplainStmt);
2261 2262 2263

	Node_Copy(from, newnode, query);
	newnode->verbose = from->verbose;
2264
	newnode->analyze = from->analyze;
2265 2266 2267 2268 2269 2270 2271

	return newnode;
}

static CreateSeqStmt *
_copyCreateSeqStmt(CreateSeqStmt *from)
{
B
Bruce Momjian 已提交
2272
	CreateSeqStmt *newnode = makeNode(CreateSeqStmt);
2273 2274 2275 2276 2277 2278 2279 2280

	if (from->seqname)
		newnode->seqname = pstrdup(from->seqname);
	Node_Copy(from, newnode, options);

	return newnode;
}

2281 2282 2283
static VariableSetStmt *
_copyVariableSetStmt(VariableSetStmt *from)
{
2284
	VariableSetStmt *newnode = makeNode(VariableSetStmt);
2285 2286 2287

	if (from->name)
		newnode->name = pstrdup(from->name);
2288
	Node_Copy(from, newnode, args);
2289 2290 2291 2292

	return newnode;
}

2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303
static VariableShowStmt *
_copyVariableShowStmt(VariableShowStmt *from)
{
	VariableShowStmt *newnode = makeNode(VariableShowStmt);

	if (from->name)
		newnode->name = pstrdup(from->name);

	return newnode;
}

2304 2305 2306
static VariableResetStmt *
_copyVariableResetStmt(VariableResetStmt *from)
{
2307
	VariableResetStmt *newnode = makeNode(VariableResetStmt);
2308 2309 2310 2311 2312 2313 2314

	if (from->name)
		newnode->name = pstrdup(from->name);

	return newnode;
}

2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333
static CreateTrigStmt *
_copyCreateTrigStmt(CreateTrigStmt *from)
{
	CreateTrigStmt *newnode = makeNode(CreateTrigStmt);

	if (from->trigname)
		newnode->trigname = pstrdup(from->trigname);
	if (from->relname)
		newnode->relname = pstrdup(from->relname);
	if (from->funcname)
		newnode->funcname = pstrdup(from->funcname);
	Node_Copy(from, newnode, args);
	newnode->before = from->before;
	newnode->row = from->row;
	memcpy(newnode->actions, from->actions, sizeof(from->actions));
	if (from->lang)
		newnode->lang = pstrdup(from->lang);
	if (from->text)
		newnode->text = pstrdup(from->text);
B
Bruce Momjian 已提交
2334

2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393
	Node_Copy(from, newnode, attr);
	if (from->when)
		newnode->when = pstrdup(from->when);
	newnode->isconstraint = from->isconstraint;
	newnode->deferrable = from->deferrable;
	newnode->initdeferred = from->initdeferred;
	if (from->constrrelname)
		newnode->constrrelname = pstrdup(from->constrrelname);

	return newnode;
}

static DropTrigStmt *
_copyDropTrigStmt(DropTrigStmt *from)
{
	DropTrigStmt *newnode = makeNode(DropTrigStmt);

	if (from->trigname)
		newnode->trigname = pstrdup(from->trigname);
	if (from->relname)
		newnode->relname = pstrdup(from->relname);

	return newnode;
}

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

	if (from->plname)
		newnode->plname = pstrdup(from->plname);
	if (from->plhandler)
		newnode->plhandler = pstrdup(from->plhandler);
	if (from->plcompiler)
		newnode->plcompiler = pstrdup(from->plcompiler);
	newnode->pltrusted = from->pltrusted;

	return newnode;
}

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

	if (from->plname)
		newnode->plname = pstrdup(from->plname);

	return newnode;
}

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

	if (from->user)
		newnode->user = pstrdup(from->user);
2394
	Node_Copy(from, newnode, options);
2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405

	return newnode;
}

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

	if (from->user)
		newnode->user = pstrdup(from->user);
2406
	Node_Copy(from, newnode, options);
2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420

	return newnode;
}

static DropUserStmt *
_copyDropUserStmt(DropUserStmt *from)
{
	DropUserStmt *newnode = makeNode(DropUserStmt);

	Node_Copy(from, newnode, users);

	return newnode;
}

2421 2422 2423
static LockStmt *
_copyLockStmt(LockStmt *from)
{
2424
	LockStmt   *newnode = makeNode(LockStmt);
2425

2426
	Node_Copy(from, newnode, rellist);
2427

2428 2429 2430 2431
	newnode->mode = from->mode;

	return newnode;
}
2432

2433 2434 2435
static ConstraintsSetStmt *
_copyConstraintsSetStmt(ConstraintsSetStmt *from)
{
B
Bruce Momjian 已提交
2436
	ConstraintsSetStmt *newnode = makeNode(ConstraintsSetStmt);
2437 2438 2439 2440 2441 2442 2443 2444 2445 2446

	Node_Copy(from, newnode, constraints);
	newnode->deferred = from->deferred;

	return newnode;
}

static CreateGroupStmt *
_copyCreateGroupStmt(CreateGroupStmt *from)
{
B
Bruce Momjian 已提交
2447
	CreateGroupStmt *newnode = makeNode(CreateGroupStmt);
2448 2449 2450

	if (from->name)
		newnode->name = pstrdup(from->name);
2451
	Node_Copy(from, newnode, options);
2452 2453 2454 2455 2456 2457 2458

	return newnode;
}

static AlterGroupStmt *
_copyAlterGroupStmt(AlterGroupStmt *from)
{
B
Bruce Momjian 已提交
2459
	AlterGroupStmt *newnode = makeNode(AlterGroupStmt);
2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471

	if (from->name)
		newnode->name = pstrdup(from->name);
	newnode->action = from->action;
	Node_Copy(from, newnode, listUsers);

	return newnode;
}

static DropGroupStmt *
_copyDropGroupStmt(DropGroupStmt *from)
{
B
Bruce Momjian 已提交
2472
	DropGroupStmt *newnode = makeNode(DropGroupStmt);
2473 2474 2475 2476 2477 2478 2479 2480 2481 2482

	if (from->name)
		newnode->name = pstrdup(from->name);

	return newnode;
}

static ReindexStmt *
_copyReindexStmt(ReindexStmt *from)
{
B
Bruce Momjian 已提交
2483
	ReindexStmt *newnode = makeNode(ReindexStmt);
2484 2485 2486 2487 2488 2489 2490 2491 2492 2493

	newnode->reindexType = from->reindexType;
	if (from->name)
		newnode->name = pstrdup(from->name);
	newnode->force = from->force;
	newnode->all = from->all;

	return newnode;
}

2494 2495

/* ****************************************************************
2496
 *					pg_list.h copy functions
2497 2498 2499
 * ****************************************************************
 */

2500
static Value *
2501
_copyValue(Value *from)
2502
{
2503
	Value	   *newnode = makeNode(Value);
2504 2505 2506 2507

	newnode->type = from->type;
	switch (from->type)
	{
2508 2509 2510 2511
		case T_Integer:
			newnode->val.ival = from->val.ival;
			break;
		case T_Float:
2512
		case T_String:
2513
		case T_BitString:
2514
			newnode->val.str = pstrdup(from->val.str);
2515 2516 2517
			break;
		default:
			break;
2518 2519
	}
	return newnode;
2520 2521 2522
}

/* ----------------
2523 2524
 *		copyObject returns a copy of the node or list. If it is a list, it
 *		recursively copies its items.
2525 2526
 * ----------------
 */
2527
void *
2528 2529
copyObject(void *from)
{
2530
	void	   *retval;
2531

2532 2533
	if (from == NULL)
		return NULL;
2534

2535
	switch (nodeTag(from))
2536
	{
2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557
			/*
			 * 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;
2558 2559 2560
		case T_TidScan:
			retval = _copyTidScan(from);
			break;
2561 2562 2563
		case T_SubqueryScan:
			retval = _copySubqueryScan(from);
			break;
2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581
		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 已提交
2582 2583 2584
		case T_Group:
			retval = _copyGroup(from);
			break;
2585 2586 2587 2588 2589 2590
		case T_Agg:
			retval = _copyAgg(from);
			break;
		case T_Unique:
			retval = _copyUnique(from);
			break;
2591 2592 2593
		case T_SetOp:
			retval = _copySetOp(from);
			break;
2594 2595 2596
		case T_Limit:
			retval = _copyLimit(from);
			break;
2597 2598 2599
		case T_Hash:
			retval = _copyHash(from);
			break;
V
Vadim B. Mikheev 已提交
2600 2601 2602
		case T_SubPlan:
			retval = _copySubPlan(from);
			break;
2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627

			/*
			 * PRIMITIVE NODES
			 */
		case T_Resdom:
			retval = _copyResdom(from);
			break;
		case T_Fjoin:
			retval = _copyFjoin(from);
			break;
		case T_Expr:
			retval = _copyExpr(from);
			break;
		case T_Var:
			retval = _copyVar(from);
			break;
		case T_Oper:
			retval = _copyOper(from);
			break;
		case T_Const:
			retval = _copyConst(from);
			break;
		case T_Param:
			retval = _copyParam(from);
			break;
2628 2629 2630 2631 2632 2633
		case T_Aggref:
			retval = _copyAggref(from);
			break;
		case T_SubLink:
			retval = _copySubLink(from);
			break;
2634 2635 2636 2637 2638 2639
		case T_Func:
			retval = _copyFunc(from);
			break;
		case T_ArrayRef:
			retval = _copyArrayRef(from);
			break;
2640 2641
		case T_Iter:
			retval = _copyIter(from);
2642
			break;
2643 2644 2645
		case T_FieldSelect:
			retval = _copyFieldSelect(from);
			break;
2646 2647 2648
		case T_RelabelType:
			retval = _copyRelabelType(from);
			break;
2649 2650 2651
		case T_RangeTblRef:
			retval = _copyRangeTblRef(from);
			break;
2652 2653 2654
		case T_FromExpr:
			retval = _copyFromExpr(from);
			break;
2655 2656 2657
		case T_JoinExpr:
			retval = _copyJoinExpr(from);
			break;
2658 2659 2660 2661

			/*
			 * RELATION NODES
			 */
B
Bruce Momjian 已提交
2662
		case T_RelOptInfo:
B
Bruce Momjian 已提交
2663
			retval = _copyRelOptInfo(from);
2664 2665 2666 2667 2668 2669 2670
			break;
		case T_Path:
			retval = _copyPath(from);
			break;
		case T_IndexPath:
			retval = _copyIndexPath(from);
			break;
2671 2672 2673
		case T_TidPath:
			retval = _copyTidPath(from);
			break;
2674 2675 2676
		case T_AppendPath:
			retval = _copyAppendPath(from);
			break;
2677 2678
		case T_NestPath:
			retval = _copyNestPath(from);
2679 2680 2681 2682 2683 2684 2685
			break;
		case T_MergePath:
			retval = _copyMergePath(from);
			break;
		case T_HashPath:
			retval = _copyHashPath(from);
			break;
2686 2687
		case T_PathKeyItem:
			retval = _copyPathKeyItem(from);
2688
			break;
2689 2690
		case T_RestrictInfo:
			retval = _copyRestrictInfo(from);
2691
			break;
2692 2693
		case T_JoinInfo:
			retval = _copyJoinInfo(from);
2694 2695 2696 2697
			break;
		case T_Stream:
			retval = _copyStream(from);
			break;
2698 2699 2700
		case T_IndexOptInfo:
			retval = _copyIndexOptInfo(from);
			break;
2701 2702

			/*
2703
			 * VALUE NODES
2704
			 */
2705 2706 2707
		case T_Integer:
		case T_Float:
		case T_String:
2708
		case T_BitString:
2709
			retval = _copyValue(from);
2710
			break;
2711 2712 2713 2714 2715 2716 2717 2718
		case T_List:
			{
				List	   *list = from,
						   *l,
						   *nl;

				/* rather ugly coding for speed... */
				/* Note the input list cannot be NIL if we got here. */
2719
				nl = makeList1(copyObject(lfirst(list)));
2720 2721 2722 2723
				retval = nl;

				foreach(l, lnext(list))
				{
2724
					lnext(nl) = makeList1(copyObject(lfirst(l)));
2725 2726 2727
					nl = lnext(nl);
				}
			}
2728
			break;
2729 2730 2731 2732

			/*
			 * PARSE NODES
			 */
2733 2734 2735
		case T_Query:
			retval = _copyQuery(from);
			break;
2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747
		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;
2748 2749 2750
		case T_SetOperationStmt:
			retval = _copySetOperationStmt(from);
			break;
2751 2752 2753
		case T_AlterTableStmt:
			retval = _copyAlterTableStmt(from);
			break;
2754 2755
		case T_GrantStmt:
			retval = _copyGrantStmt(from);
2756
			break;
2757 2758 2759
		case T_ClosePortalStmt:
			retval = _copyClosePortalStmt(from);
			break;
2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777
		case T_ClusterStmt:
			retval = _copyClusterStmt(from);
			break;
		case T_CopyStmt:
			retval = _copyCopyStmt(from);
			break;
		case T_CreateStmt:
			retval = _copyCreateStmt(from);
			break;
		case T_VersionStmt:
			retval = _copyVersionStmt(from);
			break;
		case T_DefineStmt:
			retval = _copyDefineStmt(from);
			break;
		case T_DropStmt:
			retval = _copyDropStmt(from);
			break;
2778 2779 2780
		case T_TruncateStmt:
			retval = _copyTruncateStmt(from);
			break;
2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807
		case T_CommentStmt:
			retval = _copyCommentStmt(from);
			break;
		case T_FetchStmt:
			retval = _copyFetchStmt(from);
			break;
		case T_IndexStmt:
			retval = _copyIndexStmt(from);
			break;
		case T_ProcedureStmt:
			retval = _copyProcedureStmt(from);
			break;
		case T_RemoveAggrStmt:
			retval = _copyRemoveAggrStmt(from);
			break;
		case T_RemoveFuncStmt:
			retval = _copyRemoveFuncStmt(from);
			break;
		case T_RemoveOperStmt:
			retval = _copyRemoveOperStmt(from);
			break;
		case T_RenameStmt:
			retval = _copyRenameStmt(from);
			break;
		case T_RuleStmt:
			retval = _copyRuleStmt(from);
			break;
2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819
		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;
2820 2821 2822
		case T_ViewStmt:
			retval = _copyViewStmt(from);
			break;
2823 2824 2825
		case T_LoadStmt:
			retval = _copyLoadStmt(from);
			break;
2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840
		case T_CreatedbStmt:
			retval = _copyCreatedbStmt(from);
			break;
		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;
2841 2842 2843
		case T_VariableSetStmt:
			retval = _copyVariableSetStmt(from);
			break;
2844 2845 2846
		case T_VariableShowStmt:
			retval = _copyVariableShowStmt(from);
			break;
2847 2848 2849
		case T_VariableResetStmt:
			retval = _copyVariableResetStmt(from);
			break;
2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870
		case T_CreateTrigStmt:
			retval = _copyCreateTrigStmt(from);
			break;
		case T_DropTrigStmt:
			retval = _copyDropTrigStmt(from);
			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;
		case T_DropUserStmt:
			retval = _copyDropUserStmt(from);
			break;
2871 2872 2873
		case T_LockStmt:
			retval = _copyLockStmt(from);
			break;
2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888
		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 已提交
2889
		case T_CheckPointStmt:
B
Bruce Momjian 已提交
2890
			retval = (void *) makeNode(CheckPointStmt);
V
Vadim B. Mikheev 已提交
2891
			break;
2892

2893 2894 2895
		case T_A_Expr:
			retval = _copyAExpr(from);
			break;
2896 2897
		case T_Attr:
			retval = _copyAttr(from);
2898
			break;
2899 2900 2901
		case T_A_Const:
			retval = _copyAConst(from);
			break;
2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916
		case T_ParamNo:
			retval = _copyParamNo(from);
			break;
		case T_Ident:
			retval = _copyIdent(from);
			break;
		case T_FuncCall:
			retval = _copyFuncCall(from);
			break;
		case T_A_Indices:
			retval = _copyAIndices(from);
			break;
		case T_ResTarget:
			retval = _copyResTarget(from);
			break;
2917 2918 2919
		case T_TypeCast:
			retval = _copyTypeCast(from);
			break;
2920 2921 2922 2923 2924 2925
		case T_SortGroupBy:
			retval = _copySortGroupBy(from);
			break;
		case T_RangeVar:
			retval = _copyRangeVar(from);
			break;
2926 2927 2928
		case T_RangeSubselect:
			retval = _copyRangeSubselect(from);
			break;
2929 2930 2931
		case T_TypeName:
			retval = _copyTypeName(from);
			break;
2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943
		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;
2944 2945 2946 2947 2948
		case T_TargetEntry:
			retval = _copyTargetEntry(from);
			break;
		case T_RangeTblEntry:
			retval = _copyRangeTblEntry(from);
2949
			break;
2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961
		case T_SortClause:
			retval = _copySortClause(from);
			break;
		case T_GroupClause:
			retval = _copyGroupClause(from);
			break;
		case T_CaseExpr:
			retval = _copyCaseExpr(from);
			break;
		case T_CaseWhen:
			retval = _copyCaseWhen(from);
			break;
2962 2963 2964 2965 2966 2967
		case T_NullTest:
			retval = _copyNullTest(from);
			break;
		case T_BooleanTest:
			retval = _copyBooleanTest(from);
			break;
2968 2969 2970
		case T_FkConstraint:
			retval = _copyFkConstraint(from);
			break;
2971 2972 2973
		case T_PrivGrantee:
			retval = _copyPrivGrantee(from);
			break;
2974

2975
		default:
2976 2977 2978
			elog(ERROR, "copyObject: don't know how to copy node type %d",
				 nodeTag(from));
			retval = from;		/* keep compiler quiet */
2979
			break;
2980
	}
2981
	return retval;
2982
}