]> granicus.if.org Git - postgresql/blob - src/backend/nodes/copyfuncs.c
Update copyrights to 2003.
[postgresql] / src / backend / nodes / copyfuncs.c
1 /*-------------------------------------------------------------------------
2  *
3  * copyfuncs.c
4  *        Copy functions for Postgres tree nodes.
5  *
6  * NOTE: we currently support copying all node types found in parse and
7  * plan trees.  We do not support copying executor state trees; there
8  * is no need for that, and no point in maintaining all the code that
9  * would be needed.  We also do not support copying Path trees, mainly
10  * because the circular linkages between RelOptInfo and Path nodes can't
11  * be handled easily in a simple depth-first traversal.
12  *
13  *
14  * Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group
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.262 2003/08/04 02:39:59 momjian Exp $
19  *
20  *-------------------------------------------------------------------------
21  */
22
23 #include "postgres.h"
24
25 #include "nodes/parsenodes.h"
26 #include "nodes/plannodes.h"
27 #include "nodes/relation.h"
28 #include "utils/datum.h"
29
30
31 /*
32  * Macros to simplify copying of different kinds of fields.  Use these
33  * wherever possible to reduce the chance for silly typos.      Note that these
34  * hard-wire the convention that the local variables in a Copy routine are
35  * named 'newnode' and 'from'.
36  */
37
38 /* Copy a simple scalar field (int, float, bool, enum, etc) */
39 #define COPY_SCALAR_FIELD(fldname) \
40         (newnode->fldname = from->fldname)
41
42 /* Copy a field that is a pointer to some kind of Node or Node tree */
43 #define COPY_NODE_FIELD(fldname) \
44         (newnode->fldname = copyObject(from->fldname))
45
46 /* Copy a field that is a pointer to a list of integers */
47 #define COPY_INTLIST_FIELD(fldname) \
48         (newnode->fldname = listCopy(from->fldname))
49
50 /* Copy a field that is a pointer to a list of Oids */
51 #define COPY_OIDLIST_FIELD(fldname) \
52         (newnode->fldname = listCopy(from->fldname))
53
54 /* Copy a field that is a pointer to a Bitmapset */
55 #define COPY_BITMAPSET_FIELD(fldname) \
56         (newnode->fldname = bms_copy(from->fldname))
57
58 /* Copy a field that is a pointer to a C string, or perhaps NULL */
59 #define COPY_STRING_FIELD(fldname) \
60         (newnode->fldname = from->fldname ? pstrdup(from->fldname) : (char *) NULL)
61
62 /* Copy a field that is a pointer to a simple palloc'd object of size sz */
63 #define COPY_POINTER_FIELD(fldname, sz) \
64         do { \
65                 Size    _size = (sz); \
66                 newnode->fldname = palloc(_size); \
67                 memcpy(newnode->fldname, from->fldname, _size); \
68         } while (0)
69
70
71 /*
72  * listCopy
73  *        This copy function only copies the "cons-cells" of the list, not the
74  *        pointed-to objects.  (Use copyObject if you want a "deep" copy.)
75  *
76  *        We also use this function for copying lists of integers and Oids,
77  *        which is notationally a bit ugly, but perfectly safe.
78  *
79  *        Note that copyObject will surely coredump if applied to a list
80  *        of integers or Oids!
81  */
82 List *
83 listCopy(List *list)
84 {
85         List       *newlist,
86                            *oldl,
87                            *newcell,
88                            *prev;
89
90         /* rather ugly coding for speed... */
91         if (list == NIL)
92                 return NIL;
93
94         newcell = makeNode(List);
95         newcell->elem = list->elem;
96
97         newlist = prev = newcell;
98
99         foreach(oldl, lnext(list))
100         {
101                 newcell = makeNode(List);
102                 newcell->elem = oldl->elem;
103                 prev->next = newcell;
104                 prev = newcell;
105         }
106         prev->next = NIL;
107
108         return newlist;
109 }
110
111 /* ****************************************************************
112  *                                       plannodes.h copy functions
113  * ****************************************************************
114  */
115
116 /*
117  * CopyPlanFields
118  *
119  *              This function copies the fields of the Plan node.  It is used by
120  *              all the copy functions for classes which inherit from Plan.
121  */
122 static void
123 CopyPlanFields(Plan *from, Plan *newnode)
124 {
125         COPY_SCALAR_FIELD(startup_cost);
126         COPY_SCALAR_FIELD(total_cost);
127         COPY_SCALAR_FIELD(plan_rows);
128         COPY_SCALAR_FIELD(plan_width);
129         COPY_NODE_FIELD(targetlist);
130         COPY_NODE_FIELD(qual);
131         COPY_NODE_FIELD(lefttree);
132         COPY_NODE_FIELD(righttree);
133         COPY_NODE_FIELD(initPlan);
134         COPY_BITMAPSET_FIELD(extParam);
135         COPY_BITMAPSET_FIELD(allParam);
136         COPY_SCALAR_FIELD(nParamExec);
137 }
138
139 /*
140  * _copyPlan
141  */
142 static Plan *
143 _copyPlan(Plan *from)
144 {
145         Plan       *newnode = makeNode(Plan);
146
147         /*
148          * copy node superclass fields
149          */
150         CopyPlanFields(from, newnode);
151
152         return newnode;
153 }
154
155
156 /*
157  * _copyResult
158  */
159 static Result *
160 _copyResult(Result *from)
161 {
162         Result     *newnode = makeNode(Result);
163
164         /*
165          * copy node superclass fields
166          */
167         CopyPlanFields((Plan *) from, (Plan *) newnode);
168
169         /*
170          * copy remainder of node
171          */
172         COPY_NODE_FIELD(resconstantqual);
173
174         return newnode;
175 }
176
177 /*
178  * _copyAppend
179  */
180 static Append *
181 _copyAppend(Append *from)
182 {
183         Append     *newnode = makeNode(Append);
184
185         /*
186          * copy node superclass fields
187          */
188         CopyPlanFields((Plan *) from, (Plan *) newnode);
189
190         /*
191          * copy remainder of node
192          */
193         COPY_NODE_FIELD(appendplans);
194         COPY_SCALAR_FIELD(isTarget);
195
196         return newnode;
197 }
198
199
200 /*
201  * CopyScanFields
202  *
203  *              This function copies the fields of the Scan node.  It is used by
204  *              all the copy functions for classes which inherit from Scan.
205  */
206 static void
207 CopyScanFields(Scan *from, Scan *newnode)
208 {
209         CopyPlanFields((Plan *) from, (Plan *) newnode);
210
211         COPY_SCALAR_FIELD(scanrelid);
212 }
213
214 /*
215  * _copyScan
216  */
217 static Scan *
218 _copyScan(Scan *from)
219 {
220         Scan       *newnode = makeNode(Scan);
221
222         /*
223          * copy node superclass fields
224          */
225         CopyScanFields((Scan *) from, (Scan *) newnode);
226
227         return newnode;
228 }
229
230 /*
231  * _copySeqScan
232  */
233 static SeqScan *
234 _copySeqScan(SeqScan *from)
235 {
236         SeqScan    *newnode = makeNode(SeqScan);
237
238         /*
239          * copy node superclass fields
240          */
241         CopyScanFields((Scan *) from, (Scan *) newnode);
242
243         return newnode;
244 }
245
246 /*
247  * _copyIndexScan
248  */
249 static IndexScan *
250 _copyIndexScan(IndexScan *from)
251 {
252         IndexScan  *newnode = makeNode(IndexScan);
253
254         /*
255          * copy node superclass fields
256          */
257         CopyScanFields((Scan *) from, (Scan *) newnode);
258
259         /*
260          * copy remainder of node
261          */
262         COPY_OIDLIST_FIELD(indxid);
263         COPY_NODE_FIELD(indxqual);
264         COPY_NODE_FIELD(indxqualorig);
265         COPY_SCALAR_FIELD(indxorderdir);
266
267         return newnode;
268 }
269
270 /*
271  * _copyTidScan
272  */
273 static TidScan *
274 _copyTidScan(TidScan *from)
275 {
276         TidScan    *newnode = makeNode(TidScan);
277
278         /*
279          * copy node superclass fields
280          */
281         CopyScanFields((Scan *) from, (Scan *) newnode);
282
283         /*
284          * copy remainder of node
285          */
286         COPY_NODE_FIELD(tideval);
287
288         return newnode;
289 }
290
291 /*
292  * _copySubqueryScan
293  */
294 static SubqueryScan *
295 _copySubqueryScan(SubqueryScan *from)
296 {
297         SubqueryScan *newnode = makeNode(SubqueryScan);
298
299         /*
300          * copy node superclass fields
301          */
302         CopyScanFields((Scan *) from, (Scan *) newnode);
303
304         /*
305          * copy remainder of node
306          */
307         COPY_NODE_FIELD(subplan);
308
309         return newnode;
310 }
311
312 /*
313  * _copyFunctionScan
314  */
315 static FunctionScan *
316 _copyFunctionScan(FunctionScan *from)
317 {
318         FunctionScan *newnode = makeNode(FunctionScan);
319
320         /*
321          * copy node superclass fields
322          */
323         CopyScanFields((Scan *) from, (Scan *) newnode);
324
325         return newnode;
326 }
327
328 /*
329  * CopyJoinFields
330  *
331  *              This function copies the fields of the Join node.  It is used by
332  *              all the copy functions for classes which inherit from Join.
333  */
334 static void
335 CopyJoinFields(Join *from, Join *newnode)
336 {
337         CopyPlanFields((Plan *) from, (Plan *) newnode);
338
339         COPY_SCALAR_FIELD(jointype);
340         COPY_NODE_FIELD(joinqual);
341 }
342
343
344 /*
345  * _copyJoin
346  */
347 static Join *
348 _copyJoin(Join *from)
349 {
350         Join       *newnode = makeNode(Join);
351
352         /*
353          * copy node superclass fields
354          */
355         CopyJoinFields(from, newnode);
356
357         return newnode;
358 }
359
360
361 /*
362  * _copyNestLoop
363  */
364 static NestLoop *
365 _copyNestLoop(NestLoop *from)
366 {
367         NestLoop   *newnode = makeNode(NestLoop);
368
369         /*
370          * copy node superclass fields
371          */
372         CopyJoinFields((Join *) from, (Join *) newnode);
373
374         return newnode;
375 }
376
377
378 /*
379  * _copyMergeJoin
380  */
381 static MergeJoin *
382 _copyMergeJoin(MergeJoin *from)
383 {
384         MergeJoin  *newnode = makeNode(MergeJoin);
385
386         /*
387          * copy node superclass fields
388          */
389         CopyJoinFields((Join *) from, (Join *) newnode);
390
391         /*
392          * copy remainder of node
393          */
394         COPY_NODE_FIELD(mergeclauses);
395
396         return newnode;
397 }
398
399 /*
400  * _copyHashJoin
401  */
402 static HashJoin *
403 _copyHashJoin(HashJoin *from)
404 {
405         HashJoin   *newnode = makeNode(HashJoin);
406
407         /*
408          * copy node superclass fields
409          */
410         CopyJoinFields((Join *) from, (Join *) newnode);
411
412         /*
413          * copy remainder of node
414          */
415         COPY_NODE_FIELD(hashclauses);
416
417         return newnode;
418 }
419
420
421 /*
422  * _copyMaterial
423  */
424 static Material *
425 _copyMaterial(Material *from)
426 {
427         Material   *newnode = makeNode(Material);
428
429         /*
430          * copy node superclass fields
431          */
432         CopyPlanFields((Plan *) from, (Plan *) newnode);
433
434         return newnode;
435 }
436
437
438 /*
439  * _copySort
440  */
441 static Sort *
442 _copySort(Sort *from)
443 {
444         Sort       *newnode = makeNode(Sort);
445
446         /*
447          * copy node superclass fields
448          */
449         CopyPlanFields((Plan *) from, (Plan *) newnode);
450
451         COPY_SCALAR_FIELD(numCols);
452         COPY_POINTER_FIELD(sortColIdx, from->numCols * sizeof(AttrNumber));
453         COPY_POINTER_FIELD(sortOperators, from->numCols * sizeof(Oid));
454
455         return newnode;
456 }
457
458
459 /*
460  * _copyGroup
461  */
462 static Group *
463 _copyGroup(Group *from)
464 {
465         Group      *newnode = makeNode(Group);
466
467         CopyPlanFields((Plan *) from, (Plan *) newnode);
468
469         COPY_SCALAR_FIELD(numCols);
470         COPY_POINTER_FIELD(grpColIdx, from->numCols * sizeof(AttrNumber));
471
472         return newnode;
473 }
474
475 /*
476  * _copyAgg
477  */
478 static Agg *
479 _copyAgg(Agg *from)
480 {
481         Agg                *newnode = makeNode(Agg);
482
483         CopyPlanFields((Plan *) from, (Plan *) newnode);
484
485         COPY_SCALAR_FIELD(aggstrategy);
486         COPY_SCALAR_FIELD(numCols);
487         if (from->numCols > 0)
488                 COPY_POINTER_FIELD(grpColIdx, from->numCols * sizeof(AttrNumber));
489         COPY_SCALAR_FIELD(numGroups);
490
491         return newnode;
492 }
493
494 /*
495  * _copyUnique
496  */
497 static Unique *
498 _copyUnique(Unique *from)
499 {
500         Unique     *newnode = makeNode(Unique);
501
502         /*
503          * copy node superclass fields
504          */
505         CopyPlanFields((Plan *) from, (Plan *) newnode);
506
507         /*
508          * copy remainder of node
509          */
510         COPY_SCALAR_FIELD(numCols);
511         COPY_POINTER_FIELD(uniqColIdx, from->numCols * sizeof(AttrNumber));
512
513         return newnode;
514 }
515
516 /*
517  * _copyHash
518  */
519 static Hash *
520 _copyHash(Hash *from)
521 {
522         Hash       *newnode = makeNode(Hash);
523
524         /*
525          * copy node superclass fields
526          */
527         CopyPlanFields((Plan *) from, (Plan *) newnode);
528
529         /*
530          * copy remainder of node
531          */
532         COPY_NODE_FIELD(hashkeys);
533
534         return newnode;
535 }
536
537 /*
538  * _copySetOp
539  */
540 static SetOp *
541 _copySetOp(SetOp *from)
542 {
543         SetOp      *newnode = makeNode(SetOp);
544
545         /*
546          * copy node superclass fields
547          */
548         CopyPlanFields((Plan *) from, (Plan *) newnode);
549
550         /*
551          * copy remainder of node
552          */
553         COPY_SCALAR_FIELD(cmd);
554         COPY_SCALAR_FIELD(numCols);
555         COPY_POINTER_FIELD(dupColIdx, from->numCols * sizeof(AttrNumber));
556         COPY_SCALAR_FIELD(flagColIdx);
557
558         return newnode;
559 }
560
561 /*
562  * _copyLimit
563  */
564 static Limit *
565 _copyLimit(Limit *from)
566 {
567         Limit      *newnode = makeNode(Limit);
568
569         /*
570          * copy node superclass fields
571          */
572         CopyPlanFields((Plan *) from, (Plan *) newnode);
573
574         /*
575          * copy remainder of node
576          */
577         COPY_NODE_FIELD(limitOffset);
578         COPY_NODE_FIELD(limitCount);
579
580         return newnode;
581 }
582
583 /* ****************************************************************
584  *                                         primnodes.h copy functions
585  * ****************************************************************
586  */
587
588 /*
589  * _copyResdom
590  */
591 static Resdom *
592 _copyResdom(Resdom *from)
593 {
594         Resdom     *newnode = makeNode(Resdom);
595
596         COPY_SCALAR_FIELD(resno);
597         COPY_SCALAR_FIELD(restype);
598         COPY_SCALAR_FIELD(restypmod);
599         COPY_STRING_FIELD(resname);
600         COPY_SCALAR_FIELD(ressortgroupref);
601         COPY_SCALAR_FIELD(resorigtbl);
602         COPY_SCALAR_FIELD(resorigcol);
603         COPY_SCALAR_FIELD(resjunk);
604
605         return newnode;
606 }
607
608 /*
609  * _copyAlias
610  */
611 static Alias *
612 _copyAlias(Alias *from)
613 {
614         Alias      *newnode = makeNode(Alias);
615
616         COPY_STRING_FIELD(aliasname);
617         COPY_NODE_FIELD(colnames);
618
619         return newnode;
620 }
621
622 /*
623  * _copyRangeVar
624  */
625 static RangeVar *
626 _copyRangeVar(RangeVar *from)
627 {
628         RangeVar   *newnode = makeNode(RangeVar);
629
630         COPY_STRING_FIELD(catalogname);
631         COPY_STRING_FIELD(schemaname);
632         COPY_STRING_FIELD(relname);
633         COPY_SCALAR_FIELD(inhOpt);
634         COPY_SCALAR_FIELD(istemp);
635         COPY_NODE_FIELD(alias);
636
637         return newnode;
638 }
639
640 /*
641  * We don't need a _copyExpr because Expr is an abstract supertype which
642  * should never actually get instantiated.      Also, since it has no common
643  * fields except NodeTag, there's no need for a helper routine to factor
644  * out copying the common fields...
645  */
646
647 /*
648  * _copyVar
649  */
650 static Var *
651 _copyVar(Var *from)
652 {
653         Var                *newnode = makeNode(Var);
654
655         COPY_SCALAR_FIELD(varno);
656         COPY_SCALAR_FIELD(varattno);
657         COPY_SCALAR_FIELD(vartype);
658         COPY_SCALAR_FIELD(vartypmod);
659         COPY_SCALAR_FIELD(varlevelsup);
660         COPY_SCALAR_FIELD(varnoold);
661         COPY_SCALAR_FIELD(varoattno);
662
663         return newnode;
664 }
665
666 /*
667  * _copyConst
668  */
669 static Const *
670 _copyConst(Const *from)
671 {
672         Const      *newnode = makeNode(Const);
673
674         COPY_SCALAR_FIELD(consttype);
675         COPY_SCALAR_FIELD(constlen);
676
677         if (from->constbyval || from->constisnull)
678         {
679                 /*
680                  * passed by value so just copy the datum. Also, don't try to copy
681                  * struct when value is null!
682                  */
683                 newnode->constvalue = from->constvalue;
684         }
685         else
686         {
687                 /*
688                  * passed by reference.  We need a palloc'd copy.
689                  */
690                 newnode->constvalue = datumCopy(from->constvalue,
691                                                                                 from->constbyval,
692                                                                                 from->constlen);
693         }
694
695         COPY_SCALAR_FIELD(constisnull);
696         COPY_SCALAR_FIELD(constbyval);
697
698         return newnode;
699 }
700
701 /*
702  * _copyParam
703  */
704 static Param *
705 _copyParam(Param *from)
706 {
707         Param      *newnode = makeNode(Param);
708
709         COPY_SCALAR_FIELD(paramkind);
710         COPY_SCALAR_FIELD(paramid);
711         COPY_STRING_FIELD(paramname);
712         COPY_SCALAR_FIELD(paramtype);
713
714         return newnode;
715 }
716
717 /*
718  * _copyAggref
719  */
720 static Aggref *
721 _copyAggref(Aggref *from)
722 {
723         Aggref     *newnode = makeNode(Aggref);
724
725         COPY_SCALAR_FIELD(aggfnoid);
726         COPY_SCALAR_FIELD(aggtype);
727         COPY_NODE_FIELD(target);
728         COPY_SCALAR_FIELD(agglevelsup);
729         COPY_SCALAR_FIELD(aggstar);
730         COPY_SCALAR_FIELD(aggdistinct);
731
732         return newnode;
733 }
734
735 /*
736  * _copyArrayRef
737  */
738 static ArrayRef *
739 _copyArrayRef(ArrayRef *from)
740 {
741         ArrayRef   *newnode = makeNode(ArrayRef);
742
743         COPY_SCALAR_FIELD(refrestype);
744         COPY_SCALAR_FIELD(refarraytype);
745         COPY_SCALAR_FIELD(refelemtype);
746         COPY_NODE_FIELD(refupperindexpr);
747         COPY_NODE_FIELD(reflowerindexpr);
748         COPY_NODE_FIELD(refexpr);
749         COPY_NODE_FIELD(refassgnexpr);
750
751         return newnode;
752 }
753
754 /*
755  * _copyFuncExpr
756  */
757 static FuncExpr *
758 _copyFuncExpr(FuncExpr * from)
759 {
760         FuncExpr   *newnode = makeNode(FuncExpr);
761
762         COPY_SCALAR_FIELD(funcid);
763         COPY_SCALAR_FIELD(funcresulttype);
764         COPY_SCALAR_FIELD(funcretset);
765         COPY_SCALAR_FIELD(funcformat);
766         COPY_NODE_FIELD(args);
767
768         return newnode;
769 }
770
771 /*
772  * _copyOpExpr
773  */
774 static OpExpr *
775 _copyOpExpr(OpExpr * from)
776 {
777         OpExpr     *newnode = makeNode(OpExpr);
778
779         COPY_SCALAR_FIELD(opno);
780         COPY_SCALAR_FIELD(opfuncid);
781         COPY_SCALAR_FIELD(opresulttype);
782         COPY_SCALAR_FIELD(opretset);
783         COPY_NODE_FIELD(args);
784
785         return newnode;
786 }
787
788 /*
789  * _copyDistinctExpr (same as OpExpr)
790  */
791 static DistinctExpr *
792 _copyDistinctExpr(DistinctExpr * from)
793 {
794         DistinctExpr *newnode = makeNode(DistinctExpr);
795
796         COPY_SCALAR_FIELD(opno);
797         COPY_SCALAR_FIELD(opfuncid);
798         COPY_SCALAR_FIELD(opresulttype);
799         COPY_SCALAR_FIELD(opretset);
800         COPY_NODE_FIELD(args);
801
802         return newnode;
803 }
804
805 /*
806  * _copyScalarArrayOpExpr
807  */
808 static ScalarArrayOpExpr *
809 _copyScalarArrayOpExpr(ScalarArrayOpExpr * from)
810 {
811         ScalarArrayOpExpr *newnode = makeNode(ScalarArrayOpExpr);
812
813         COPY_SCALAR_FIELD(opno);
814         COPY_SCALAR_FIELD(opfuncid);
815         COPY_SCALAR_FIELD(useOr);
816         COPY_NODE_FIELD(args);
817
818         return newnode;
819 }
820
821 /*
822  * _copyBoolExpr
823  */
824 static BoolExpr *
825 _copyBoolExpr(BoolExpr * from)
826 {
827         BoolExpr   *newnode = makeNode(BoolExpr);
828
829         COPY_SCALAR_FIELD(boolop);
830         COPY_NODE_FIELD(args);
831
832         return newnode;
833 }
834
835 /*
836  * _copySubLink
837  */
838 static SubLink *
839 _copySubLink(SubLink *from)
840 {
841         SubLink    *newnode = makeNode(SubLink);
842
843         COPY_SCALAR_FIELD(subLinkType);
844         COPY_SCALAR_FIELD(useOr);
845         COPY_NODE_FIELD(lefthand);
846         COPY_NODE_FIELD(operName);
847         COPY_OIDLIST_FIELD(operOids);
848         COPY_NODE_FIELD(subselect);
849
850         return newnode;
851 }
852
853 /*
854  * _copySubPlan
855  */
856 static SubPlan *
857 _copySubPlan(SubPlan *from)
858 {
859         SubPlan    *newnode = makeNode(SubPlan);
860
861         COPY_SCALAR_FIELD(subLinkType);
862         COPY_SCALAR_FIELD(useOr);
863         COPY_NODE_FIELD(exprs);
864         COPY_INTLIST_FIELD(paramIds);
865         COPY_NODE_FIELD(plan);
866         COPY_SCALAR_FIELD(plan_id);
867         COPY_NODE_FIELD(rtable);
868         COPY_SCALAR_FIELD(useHashTable);
869         COPY_SCALAR_FIELD(unknownEqFalse);
870         COPY_INTLIST_FIELD(setParam);
871         COPY_INTLIST_FIELD(parParam);
872         COPY_NODE_FIELD(args);
873
874         return newnode;
875 }
876
877 /*
878  * _copyFieldSelect
879  */
880 static FieldSelect *
881 _copyFieldSelect(FieldSelect *from)
882 {
883         FieldSelect *newnode = makeNode(FieldSelect);
884
885         COPY_NODE_FIELD(arg);
886         COPY_SCALAR_FIELD(fieldnum);
887         COPY_SCALAR_FIELD(resulttype);
888         COPY_SCALAR_FIELD(resulttypmod);
889
890         return newnode;
891 }
892
893 /*
894  * _copyRelabelType
895  */
896 static RelabelType *
897 _copyRelabelType(RelabelType *from)
898 {
899         RelabelType *newnode = makeNode(RelabelType);
900
901         COPY_NODE_FIELD(arg);
902         COPY_SCALAR_FIELD(resulttype);
903         COPY_SCALAR_FIELD(resulttypmod);
904         COPY_SCALAR_FIELD(relabelformat);
905
906         return newnode;
907 }
908
909 /*
910  * _copyCaseExpr
911  */
912 static CaseExpr *
913 _copyCaseExpr(CaseExpr *from)
914 {
915         CaseExpr   *newnode = makeNode(CaseExpr);
916
917         COPY_SCALAR_FIELD(casetype);
918         COPY_NODE_FIELD(arg);
919         COPY_NODE_FIELD(args);
920         COPY_NODE_FIELD(defresult);
921
922         return newnode;
923 }
924
925 /*
926  * _copyCaseWhen
927  */
928 static CaseWhen *
929 _copyCaseWhen(CaseWhen *from)
930 {
931         CaseWhen   *newnode = makeNode(CaseWhen);
932
933         COPY_NODE_FIELD(expr);
934         COPY_NODE_FIELD(result);
935
936         return newnode;
937 }
938
939 /*
940  * _copyArrayExpr
941  */
942 static ArrayExpr *
943 _copyArrayExpr(ArrayExpr * from)
944 {
945         ArrayExpr  *newnode = makeNode(ArrayExpr);
946
947         COPY_SCALAR_FIELD(array_typeid);
948         COPY_SCALAR_FIELD(element_typeid);
949         COPY_NODE_FIELD(elements);
950         COPY_SCALAR_FIELD(ndims);
951
952         return newnode;
953 }
954
955 /*
956  * _copyCoalesceExpr
957  */
958 static CoalesceExpr *
959 _copyCoalesceExpr(CoalesceExpr * from)
960 {
961         CoalesceExpr *newnode = makeNode(CoalesceExpr);
962
963         COPY_SCALAR_FIELD(coalescetype);
964         COPY_NODE_FIELD(args);
965
966         return newnode;
967 }
968
969 /*
970  * _copyNullIfExpr (same as OpExpr)
971  */
972 static NullIfExpr *
973 _copyNullIfExpr(NullIfExpr * from)
974 {
975         NullIfExpr *newnode = makeNode(NullIfExpr);
976
977         COPY_SCALAR_FIELD(opno);
978         COPY_SCALAR_FIELD(opfuncid);
979         COPY_SCALAR_FIELD(opresulttype);
980         COPY_SCALAR_FIELD(opretset);
981         COPY_NODE_FIELD(args);
982
983         return newnode;
984 }
985
986 /*
987  * _copyNullTest
988  */
989 static NullTest *
990 _copyNullTest(NullTest *from)
991 {
992         NullTest   *newnode = makeNode(NullTest);
993
994         COPY_NODE_FIELD(arg);
995         COPY_SCALAR_FIELD(nulltesttype);
996
997         return newnode;
998 }
999
1000 /*
1001  * _copyBooleanTest
1002  */
1003 static BooleanTest *
1004 _copyBooleanTest(BooleanTest *from)
1005 {
1006         BooleanTest *newnode = makeNode(BooleanTest);
1007
1008         COPY_NODE_FIELD(arg);
1009         COPY_SCALAR_FIELD(booltesttype);
1010
1011         return newnode;
1012 }
1013
1014 /*
1015  * _copyCoerceToDomain
1016  */
1017 static CoerceToDomain *
1018 _copyCoerceToDomain(CoerceToDomain * from)
1019 {
1020         CoerceToDomain *newnode = makeNode(CoerceToDomain);
1021
1022         COPY_NODE_FIELD(arg);
1023         COPY_SCALAR_FIELD(resulttype);
1024         COPY_SCALAR_FIELD(resulttypmod);
1025         COPY_SCALAR_FIELD(coercionformat);
1026
1027         return newnode;
1028 }
1029
1030 /*
1031  * _copyCoerceToDomainValue
1032  */
1033 static CoerceToDomainValue *
1034 _copyCoerceToDomainValue(CoerceToDomainValue * from)
1035 {
1036         CoerceToDomainValue *newnode = makeNode(CoerceToDomainValue);
1037
1038         COPY_SCALAR_FIELD(typeId);
1039         COPY_SCALAR_FIELD(typeMod);
1040
1041         return newnode;
1042 }
1043
1044 /*
1045  * _copySetToDefault
1046  */
1047 static SetToDefault *
1048 _copySetToDefault(SetToDefault * from)
1049 {
1050         SetToDefault *newnode = makeNode(SetToDefault);
1051
1052         COPY_SCALAR_FIELD(typeId);
1053         COPY_SCALAR_FIELD(typeMod);
1054
1055         return newnode;
1056 }
1057
1058 /*
1059  * _copyTargetEntry
1060  */
1061 static TargetEntry *
1062 _copyTargetEntry(TargetEntry *from)
1063 {
1064         TargetEntry *newnode = makeNode(TargetEntry);
1065
1066         COPY_NODE_FIELD(resdom);
1067         COPY_NODE_FIELD(expr);
1068
1069         return newnode;
1070 }
1071
1072 /*
1073  * _copyRangeTblRef
1074  */
1075 static RangeTblRef *
1076 _copyRangeTblRef(RangeTblRef *from)
1077 {
1078         RangeTblRef *newnode = makeNode(RangeTblRef);
1079
1080         COPY_SCALAR_FIELD(rtindex);
1081
1082         return newnode;
1083 }
1084
1085 /*
1086  * _copyJoinExpr
1087  */
1088 static JoinExpr *
1089 _copyJoinExpr(JoinExpr *from)
1090 {
1091         JoinExpr   *newnode = makeNode(JoinExpr);
1092
1093         COPY_SCALAR_FIELD(jointype);
1094         COPY_SCALAR_FIELD(isNatural);
1095         COPY_NODE_FIELD(larg);
1096         COPY_NODE_FIELD(rarg);
1097         COPY_NODE_FIELD(using);
1098         COPY_NODE_FIELD(quals);
1099         COPY_NODE_FIELD(alias);
1100         COPY_SCALAR_FIELD(rtindex);
1101
1102         return newnode;
1103 }
1104
1105 /*
1106  * _copyFromExpr
1107  */
1108 static FromExpr *
1109 _copyFromExpr(FromExpr *from)
1110 {
1111         FromExpr   *newnode = makeNode(FromExpr);
1112
1113         COPY_NODE_FIELD(fromlist);
1114         COPY_NODE_FIELD(quals);
1115
1116         return newnode;
1117 }
1118
1119 /* ****************************************************************
1120  *                                              relation.h copy functions
1121  *
1122  * We don't support copying RelOptInfo, IndexOptInfo, or Path nodes.
1123  * There are some subsidiary structs that are useful to copy, though.
1124  * ****************************************************************
1125  */
1126
1127 /*
1128  * _copyPathKeyItem
1129  */
1130 static PathKeyItem *
1131 _copyPathKeyItem(PathKeyItem *from)
1132 {
1133         PathKeyItem *newnode = makeNode(PathKeyItem);
1134
1135         COPY_NODE_FIELD(key);
1136         COPY_SCALAR_FIELD(sortop);
1137
1138         return newnode;
1139 }
1140
1141 /*
1142  * _copyRestrictInfo
1143  */
1144 static RestrictInfo *
1145 _copyRestrictInfo(RestrictInfo *from)
1146 {
1147         RestrictInfo *newnode = makeNode(RestrictInfo);
1148
1149         COPY_NODE_FIELD(clause);
1150         COPY_SCALAR_FIELD(ispusheddown);
1151         COPY_NODE_FIELD(subclauseindices);      /* XXX probably bad */
1152         COPY_SCALAR_FIELD(eval_cost);
1153         COPY_SCALAR_FIELD(this_selec);
1154         COPY_BITMAPSET_FIELD(left_relids);
1155         COPY_BITMAPSET_FIELD(right_relids);
1156         COPY_SCALAR_FIELD(mergejoinoperator);
1157         COPY_SCALAR_FIELD(left_sortop);
1158         COPY_SCALAR_FIELD(right_sortop);
1159
1160         /*
1161          * Do not copy pathkeys, since they'd not be canonical in a copied
1162          * query
1163          */
1164         newnode->left_pathkey = NIL;
1165         newnode->right_pathkey = NIL;
1166
1167         COPY_SCALAR_FIELD(left_mergescansel);
1168         COPY_SCALAR_FIELD(right_mergescansel);
1169         COPY_SCALAR_FIELD(hashjoinoperator);
1170         COPY_SCALAR_FIELD(left_bucketsize);
1171         COPY_SCALAR_FIELD(right_bucketsize);
1172
1173         return newnode;
1174 }
1175
1176 /*
1177  * _copyJoinInfo
1178  */
1179 static JoinInfo *
1180 _copyJoinInfo(JoinInfo *from)
1181 {
1182         JoinInfo   *newnode = makeNode(JoinInfo);
1183
1184         COPY_BITMAPSET_FIELD(unjoined_relids);
1185         COPY_NODE_FIELD(jinfo_restrictinfo);
1186
1187         return newnode;
1188 }
1189
1190 /*
1191  * _copyInClauseInfo
1192  */
1193 static InClauseInfo *
1194 _copyInClauseInfo(InClauseInfo * from)
1195 {
1196         InClauseInfo *newnode = makeNode(InClauseInfo);
1197
1198         COPY_BITMAPSET_FIELD(lefthand);
1199         COPY_BITMAPSET_FIELD(righthand);
1200         COPY_NODE_FIELD(sub_targetlist);
1201
1202         return newnode;
1203 }
1204
1205 /* ****************************************************************
1206  *                                      parsenodes.h copy functions
1207  * ****************************************************************
1208  */
1209
1210 static RangeTblEntry *
1211 _copyRangeTblEntry(RangeTblEntry *from)
1212 {
1213         RangeTblEntry *newnode = makeNode(RangeTblEntry);
1214
1215         COPY_SCALAR_FIELD(rtekind);
1216         COPY_SCALAR_FIELD(relid);
1217         COPY_NODE_FIELD(subquery);
1218         COPY_NODE_FIELD(funcexpr);
1219         COPY_NODE_FIELD(coldeflist);
1220         COPY_SCALAR_FIELD(jointype);
1221         COPY_NODE_FIELD(joinaliasvars);
1222         COPY_NODE_FIELD(alias);
1223         COPY_NODE_FIELD(eref);
1224         COPY_SCALAR_FIELD(inh);
1225         COPY_SCALAR_FIELD(inFromCl);
1226         COPY_SCALAR_FIELD(checkForRead);
1227         COPY_SCALAR_FIELD(checkForWrite);
1228         COPY_SCALAR_FIELD(checkAsUser);
1229
1230         return newnode;
1231 }
1232
1233 static FkConstraint *
1234 _copyFkConstraint(FkConstraint *from)
1235 {
1236         FkConstraint *newnode = makeNode(FkConstraint);
1237
1238         COPY_STRING_FIELD(constr_name);
1239         COPY_NODE_FIELD(pktable);
1240         COPY_NODE_FIELD(fk_attrs);
1241         COPY_NODE_FIELD(pk_attrs);
1242         COPY_SCALAR_FIELD(fk_matchtype);
1243         COPY_SCALAR_FIELD(fk_upd_action);
1244         COPY_SCALAR_FIELD(fk_del_action);
1245         COPY_SCALAR_FIELD(deferrable);
1246         COPY_SCALAR_FIELD(initdeferred);
1247         COPY_SCALAR_FIELD(skip_validation);
1248
1249         return newnode;
1250 }
1251
1252 static SortClause *
1253 _copySortClause(SortClause *from)
1254 {
1255         SortClause *newnode = makeNode(SortClause);
1256
1257         COPY_SCALAR_FIELD(tleSortGroupRef);
1258         COPY_SCALAR_FIELD(sortop);
1259
1260         return newnode;
1261 }
1262
1263 static GroupClause *
1264 _copyGroupClause(GroupClause *from)
1265 {
1266         GroupClause *newnode = makeNode(GroupClause);
1267
1268         COPY_SCALAR_FIELD(tleSortGroupRef);
1269         COPY_SCALAR_FIELD(sortop);
1270
1271         return newnode;
1272 }
1273
1274 static A_Expr *
1275 _copyAExpr(A_Expr *from)
1276 {
1277         A_Expr     *newnode = makeNode(A_Expr);
1278
1279         COPY_SCALAR_FIELD(kind);
1280         COPY_NODE_FIELD(name);
1281         COPY_NODE_FIELD(lexpr);
1282         COPY_NODE_FIELD(rexpr);
1283
1284         return newnode;
1285 }
1286
1287 static ColumnRef *
1288 _copyColumnRef(ColumnRef *from)
1289 {
1290         ColumnRef  *newnode = makeNode(ColumnRef);
1291
1292         COPY_NODE_FIELD(fields);
1293         COPY_NODE_FIELD(indirection);
1294
1295         return newnode;
1296 }
1297
1298 static ParamRef *
1299 _copyParamRef(ParamRef *from)
1300 {
1301         ParamRef   *newnode = makeNode(ParamRef);
1302
1303         COPY_SCALAR_FIELD(number);
1304         COPY_NODE_FIELD(fields);
1305         COPY_NODE_FIELD(indirection);
1306
1307         return newnode;
1308 }
1309
1310 static A_Const *
1311 _copyAConst(A_Const *from)
1312 {
1313         A_Const    *newnode = makeNode(A_Const);
1314
1315         /* This part must duplicate _copyValue */
1316         COPY_SCALAR_FIELD(val.type);
1317         switch (from->val.type)
1318         {
1319                 case T_Integer:
1320                         COPY_SCALAR_FIELD(val.val.ival);
1321                         break;
1322                 case T_Float:
1323                 case T_String:
1324                 case T_BitString:
1325                         COPY_STRING_FIELD(val.val.str);
1326                         break;
1327                 case T_Null:
1328                         /* nothing to do */
1329                         break;
1330                 default:
1331                         elog(ERROR, "unrecognized node type: %d",
1332                                  (int) from->val.type);
1333                         break;
1334         }
1335
1336         COPY_NODE_FIELD(typename);
1337
1338         return newnode;
1339 }
1340
1341 static FuncCall *
1342 _copyFuncCall(FuncCall *from)
1343 {
1344         FuncCall   *newnode = makeNode(FuncCall);
1345
1346         COPY_NODE_FIELD(funcname);
1347         COPY_NODE_FIELD(args);
1348         COPY_SCALAR_FIELD(agg_star);
1349         COPY_SCALAR_FIELD(agg_distinct);
1350
1351         return newnode;
1352 }
1353
1354 static A_Indices *
1355 _copyAIndices(A_Indices *from)
1356 {
1357         A_Indices  *newnode = makeNode(A_Indices);
1358
1359         COPY_NODE_FIELD(lidx);
1360         COPY_NODE_FIELD(uidx);
1361
1362         return newnode;
1363 }
1364
1365 static ExprFieldSelect *
1366 _copyExprFieldSelect(ExprFieldSelect *from)
1367 {
1368         ExprFieldSelect *newnode = makeNode(ExprFieldSelect);
1369
1370         COPY_NODE_FIELD(arg);
1371         COPY_NODE_FIELD(fields);
1372         COPY_NODE_FIELD(indirection);
1373
1374         return newnode;
1375 }
1376
1377 static ResTarget *
1378 _copyResTarget(ResTarget *from)
1379 {
1380         ResTarget  *newnode = makeNode(ResTarget);
1381
1382         COPY_STRING_FIELD(name);
1383         COPY_NODE_FIELD(indirection);
1384         COPY_NODE_FIELD(val);
1385
1386         return newnode;
1387 }
1388
1389 static TypeName *
1390 _copyTypeName(TypeName *from)
1391 {
1392         TypeName   *newnode = makeNode(TypeName);
1393
1394         COPY_NODE_FIELD(names);
1395         COPY_SCALAR_FIELD(typeid);
1396         COPY_SCALAR_FIELD(timezone);
1397         COPY_SCALAR_FIELD(setof);
1398         COPY_SCALAR_FIELD(pct_type);
1399         COPY_SCALAR_FIELD(typmod);
1400         COPY_NODE_FIELD(arrayBounds);
1401
1402         return newnode;
1403 }
1404
1405 static SortGroupBy *
1406 _copySortGroupBy(SortGroupBy *from)
1407 {
1408         SortGroupBy *newnode = makeNode(SortGroupBy);
1409
1410         COPY_NODE_FIELD(useOp);
1411         COPY_NODE_FIELD(node);
1412
1413         return newnode;
1414 }
1415
1416 static RangeSubselect *
1417 _copyRangeSubselect(RangeSubselect *from)
1418 {
1419         RangeSubselect *newnode = makeNode(RangeSubselect);
1420
1421         COPY_NODE_FIELD(subquery);
1422         COPY_NODE_FIELD(alias);
1423
1424         return newnode;
1425 }
1426
1427 static RangeFunction *
1428 _copyRangeFunction(RangeFunction *from)
1429 {
1430         RangeFunction *newnode = makeNode(RangeFunction);
1431
1432         COPY_NODE_FIELD(funccallnode);
1433         COPY_NODE_FIELD(alias);
1434         COPY_NODE_FIELD(coldeflist);
1435
1436         return newnode;
1437 }
1438
1439 static TypeCast *
1440 _copyTypeCast(TypeCast *from)
1441 {
1442         TypeCast   *newnode = makeNode(TypeCast);
1443
1444         COPY_NODE_FIELD(arg);
1445         COPY_NODE_FIELD(typename);
1446
1447         return newnode;
1448 }
1449
1450 static IndexElem *
1451 _copyIndexElem(IndexElem *from)
1452 {
1453         IndexElem  *newnode = makeNode(IndexElem);
1454
1455         COPY_STRING_FIELD(name);
1456         COPY_NODE_FIELD(expr);
1457         COPY_NODE_FIELD(opclass);
1458
1459         return newnode;
1460 }
1461
1462 static ColumnDef *
1463 _copyColumnDef(ColumnDef *from)
1464 {
1465         ColumnDef  *newnode = makeNode(ColumnDef);
1466
1467         COPY_STRING_FIELD(colname);
1468         COPY_NODE_FIELD(typename);
1469         COPY_SCALAR_FIELD(inhcount);
1470         COPY_SCALAR_FIELD(is_local);
1471         COPY_SCALAR_FIELD(is_not_null);
1472         COPY_NODE_FIELD(raw_default);
1473         COPY_STRING_FIELD(cooked_default);
1474         COPY_NODE_FIELD(constraints);
1475         COPY_NODE_FIELD(support);
1476
1477         return newnode;
1478 }
1479
1480 static Constraint *
1481 _copyConstraint(Constraint *from)
1482 {
1483         Constraint *newnode = makeNode(Constraint);
1484
1485         COPY_SCALAR_FIELD(contype);
1486         COPY_STRING_FIELD(name);
1487         COPY_NODE_FIELD(raw_expr);
1488         COPY_STRING_FIELD(cooked_expr);
1489         COPY_NODE_FIELD(keys);
1490
1491         return newnode;
1492 }
1493
1494 static DefElem *
1495 _copyDefElem(DefElem *from)
1496 {
1497         DefElem    *newnode = makeNode(DefElem);
1498
1499         COPY_STRING_FIELD(defname);
1500         COPY_NODE_FIELD(arg);
1501
1502         return newnode;
1503 }
1504
1505 static Query *
1506 _copyQuery(Query *from)
1507 {
1508         Query      *newnode = makeNode(Query);
1509
1510         COPY_SCALAR_FIELD(commandType);
1511         COPY_SCALAR_FIELD(querySource);
1512         COPY_SCALAR_FIELD(canSetTag);
1513         COPY_NODE_FIELD(utilityStmt);
1514         COPY_SCALAR_FIELD(resultRelation);
1515         COPY_NODE_FIELD(into);
1516         COPY_SCALAR_FIELD(hasAggs);
1517         COPY_SCALAR_FIELD(hasSubLinks);
1518         COPY_NODE_FIELD(rtable);
1519         COPY_NODE_FIELD(jointree);
1520         COPY_INTLIST_FIELD(rowMarks);
1521         COPY_NODE_FIELD(targetList);
1522         COPY_NODE_FIELD(groupClause);
1523         COPY_NODE_FIELD(havingQual);
1524         COPY_NODE_FIELD(distinctClause);
1525         COPY_NODE_FIELD(sortClause);
1526         COPY_NODE_FIELD(limitOffset);
1527         COPY_NODE_FIELD(limitCount);
1528         COPY_NODE_FIELD(setOperations);
1529         COPY_INTLIST_FIELD(resultRelations);
1530         COPY_NODE_FIELD(in_info_list);
1531         COPY_SCALAR_FIELD(hasJoinRTEs);
1532
1533         /*
1534          * We do not copy the other planner internal fields: base_rel_list,
1535          * other_rel_list, join_rel_list, equi_key_list, query_pathkeys. That
1536          * would get us into copying RelOptInfo/Path trees, which we don't
1537          * want to do.  It is necessary to copy in_info_list and hasJoinRTEs
1538          * for the benefit of inheritance_planner(), which may try to copy a
1539          * Query in which these are already set.
1540          */
1541
1542         return newnode;
1543 }
1544
1545 static InsertStmt *
1546 _copyInsertStmt(InsertStmt *from)
1547 {
1548         InsertStmt *newnode = makeNode(InsertStmt);
1549
1550         COPY_NODE_FIELD(relation);
1551         COPY_NODE_FIELD(cols);
1552         COPY_NODE_FIELD(targetList);
1553         COPY_NODE_FIELD(selectStmt);
1554
1555         return newnode;
1556 }
1557
1558 static DeleteStmt *
1559 _copyDeleteStmt(DeleteStmt *from)
1560 {
1561         DeleteStmt *newnode = makeNode(DeleteStmt);
1562
1563         COPY_NODE_FIELD(relation);
1564         COPY_NODE_FIELD(whereClause);
1565
1566         return newnode;
1567 }
1568
1569 static UpdateStmt *
1570 _copyUpdateStmt(UpdateStmt *from)
1571 {
1572         UpdateStmt *newnode = makeNode(UpdateStmt);
1573
1574         COPY_NODE_FIELD(relation);
1575         COPY_NODE_FIELD(targetList);
1576         COPY_NODE_FIELD(whereClause);
1577         COPY_NODE_FIELD(fromClause);
1578
1579         return newnode;
1580 }
1581
1582 static SelectStmt *
1583 _copySelectStmt(SelectStmt *from)
1584 {
1585         SelectStmt *newnode = makeNode(SelectStmt);
1586
1587         COPY_NODE_FIELD(distinctClause);
1588         COPY_NODE_FIELD(into);
1589         COPY_NODE_FIELD(intoColNames);
1590         COPY_NODE_FIELD(targetList);
1591         COPY_NODE_FIELD(fromClause);
1592         COPY_NODE_FIELD(whereClause);
1593         COPY_NODE_FIELD(groupClause);
1594         COPY_NODE_FIELD(havingClause);
1595         COPY_NODE_FIELD(sortClause);
1596         COPY_NODE_FIELD(limitOffset);
1597         COPY_NODE_FIELD(limitCount);
1598         COPY_NODE_FIELD(forUpdate);
1599         COPY_SCALAR_FIELD(op);
1600         COPY_SCALAR_FIELD(all);
1601         COPY_NODE_FIELD(larg);
1602         COPY_NODE_FIELD(rarg);
1603
1604         return newnode;
1605 }
1606
1607 static SetOperationStmt *
1608 _copySetOperationStmt(SetOperationStmt *from)
1609 {
1610         SetOperationStmt *newnode = makeNode(SetOperationStmt);
1611
1612         COPY_SCALAR_FIELD(op);
1613         COPY_SCALAR_FIELD(all);
1614         COPY_NODE_FIELD(larg);
1615         COPY_NODE_FIELD(rarg);
1616         COPY_OIDLIST_FIELD(colTypes);
1617
1618         return newnode;
1619 }
1620
1621 static AlterTableStmt *
1622 _copyAlterTableStmt(AlterTableStmt *from)
1623 {
1624         AlterTableStmt *newnode = makeNode(AlterTableStmt);
1625
1626         COPY_SCALAR_FIELD(subtype);
1627         COPY_NODE_FIELD(relation);
1628         COPY_STRING_FIELD(name);
1629         COPY_NODE_FIELD(def);
1630         COPY_SCALAR_FIELD(behavior);
1631
1632         return newnode;
1633 }
1634
1635 static AlterDomainStmt *
1636 _copyAlterDomainStmt(AlterDomainStmt * from)
1637 {
1638         AlterDomainStmt *newnode = makeNode(AlterDomainStmt);
1639
1640         COPY_SCALAR_FIELD(subtype);
1641         COPY_NODE_FIELD(typename);
1642         COPY_STRING_FIELD(name);
1643         COPY_NODE_FIELD(def);
1644         COPY_SCALAR_FIELD(behavior);
1645
1646         return newnode;
1647 }
1648
1649 static GrantStmt *
1650 _copyGrantStmt(GrantStmt *from)
1651 {
1652         GrantStmt  *newnode = makeNode(GrantStmt);
1653
1654         COPY_SCALAR_FIELD(is_grant);
1655         COPY_SCALAR_FIELD(objtype);
1656         COPY_NODE_FIELD(objects);
1657         COPY_INTLIST_FIELD(privileges);
1658         COPY_NODE_FIELD(grantees);
1659         COPY_SCALAR_FIELD(grant_option);
1660         COPY_SCALAR_FIELD(behavior);
1661
1662         return newnode;
1663 }
1664
1665 static PrivGrantee *
1666 _copyPrivGrantee(PrivGrantee *from)
1667 {
1668         PrivGrantee *newnode = makeNode(PrivGrantee);
1669
1670         COPY_STRING_FIELD(username);
1671         COPY_STRING_FIELD(groupname);
1672
1673         return newnode;
1674 }
1675
1676 static FuncWithArgs *
1677 _copyFuncWithArgs(FuncWithArgs *from)
1678 {
1679         FuncWithArgs *newnode = makeNode(FuncWithArgs);
1680
1681         COPY_NODE_FIELD(funcname);
1682         COPY_NODE_FIELD(funcargs);
1683
1684         return newnode;
1685 }
1686
1687 static DeclareCursorStmt *
1688 _copyDeclareCursorStmt(DeclareCursorStmt * from)
1689 {
1690         DeclareCursorStmt *newnode = makeNode(DeclareCursorStmt);
1691
1692         COPY_STRING_FIELD(portalname);
1693         COPY_SCALAR_FIELD(options);
1694         COPY_NODE_FIELD(query);
1695
1696         return newnode;
1697 }
1698
1699 static ClosePortalStmt *
1700 _copyClosePortalStmt(ClosePortalStmt *from)
1701 {
1702         ClosePortalStmt *newnode = makeNode(ClosePortalStmt);
1703
1704         COPY_STRING_FIELD(portalname);
1705
1706         return newnode;
1707 }
1708
1709 static ClusterStmt *
1710 _copyClusterStmt(ClusterStmt *from)
1711 {
1712         ClusterStmt *newnode = makeNode(ClusterStmt);
1713
1714         COPY_NODE_FIELD(relation);
1715         COPY_STRING_FIELD(indexname);
1716
1717         return newnode;
1718 }
1719
1720 static CopyStmt *
1721 _copyCopyStmt(CopyStmt *from)
1722 {
1723         CopyStmt   *newnode = makeNode(CopyStmt);
1724
1725         COPY_NODE_FIELD(relation);
1726         COPY_NODE_FIELD(attlist);
1727         COPY_SCALAR_FIELD(is_from);
1728         COPY_STRING_FIELD(filename);
1729         COPY_NODE_FIELD(options);
1730
1731         return newnode;
1732 }
1733
1734 static CreateStmt *
1735 _copyCreateStmt(CreateStmt *from)
1736 {
1737         CreateStmt *newnode = makeNode(CreateStmt);
1738
1739         COPY_NODE_FIELD(relation);
1740         COPY_NODE_FIELD(tableElts);
1741         COPY_NODE_FIELD(inhRelations);
1742         COPY_NODE_FIELD(constraints);
1743         COPY_SCALAR_FIELD(hasoids);
1744         COPY_SCALAR_FIELD(oncommit);
1745
1746         return newnode;
1747 }
1748
1749 static InhRelation *
1750 _copyInhRelation(InhRelation * from)
1751 {
1752         InhRelation *newnode = makeNode(InhRelation);
1753
1754         COPY_NODE_FIELD(relation);
1755         COPY_SCALAR_FIELD(including_defaults);
1756
1757         return newnode;
1758 }
1759
1760 static DefineStmt *
1761 _copyDefineStmt(DefineStmt *from)
1762 {
1763         DefineStmt *newnode = makeNode(DefineStmt);
1764
1765         COPY_SCALAR_FIELD(kind);
1766         COPY_NODE_FIELD(defnames);
1767         COPY_NODE_FIELD(definition);
1768
1769         return newnode;
1770 }
1771
1772 static DropStmt *
1773 _copyDropStmt(DropStmt *from)
1774 {
1775         DropStmt   *newnode = makeNode(DropStmt);
1776
1777         COPY_NODE_FIELD(objects);
1778         COPY_SCALAR_FIELD(removeType);
1779         COPY_SCALAR_FIELD(behavior);
1780
1781         return newnode;
1782 }
1783
1784 static TruncateStmt *
1785 _copyTruncateStmt(TruncateStmt *from)
1786 {
1787         TruncateStmt *newnode = makeNode(TruncateStmt);
1788
1789         COPY_NODE_FIELD(relation);
1790
1791         return newnode;
1792 }
1793
1794 static CommentStmt *
1795 _copyCommentStmt(CommentStmt *from)
1796 {
1797         CommentStmt *newnode = makeNode(CommentStmt);
1798
1799         COPY_SCALAR_FIELD(objtype);
1800         COPY_NODE_FIELD(objname);
1801         COPY_NODE_FIELD(objargs);
1802         COPY_STRING_FIELD(comment);
1803
1804         return newnode;
1805 }
1806
1807 static FetchStmt *
1808 _copyFetchStmt(FetchStmt *from)
1809 {
1810         FetchStmt  *newnode = makeNode(FetchStmt);
1811
1812         COPY_SCALAR_FIELD(direction);
1813         COPY_SCALAR_FIELD(howMany);
1814         COPY_STRING_FIELD(portalname);
1815         COPY_SCALAR_FIELD(ismove);
1816
1817         return newnode;
1818 }
1819
1820 static IndexStmt *
1821 _copyIndexStmt(IndexStmt *from)
1822 {
1823         IndexStmt  *newnode = makeNode(IndexStmt);
1824
1825         COPY_STRING_FIELD(idxname);
1826         COPY_NODE_FIELD(relation);
1827         COPY_STRING_FIELD(accessMethod);
1828         COPY_NODE_FIELD(indexParams);
1829         COPY_NODE_FIELD(whereClause);
1830         COPY_NODE_FIELD(rangetable);
1831         COPY_SCALAR_FIELD(unique);
1832         COPY_SCALAR_FIELD(primary);
1833         COPY_SCALAR_FIELD(isconstraint);
1834
1835         return newnode;
1836 }
1837
1838 static CreateFunctionStmt *
1839 _copyCreateFunctionStmt(CreateFunctionStmt *from)
1840 {
1841         CreateFunctionStmt *newnode = makeNode(CreateFunctionStmt);
1842
1843         COPY_SCALAR_FIELD(replace);
1844         COPY_NODE_FIELD(funcname);
1845         COPY_NODE_FIELD(argTypes);
1846         COPY_NODE_FIELD(returnType);
1847         COPY_NODE_FIELD(options);
1848         COPY_NODE_FIELD(withClause);
1849
1850         return newnode;
1851 }
1852
1853 static RemoveAggrStmt *
1854 _copyRemoveAggrStmt(RemoveAggrStmt *from)
1855 {
1856         RemoveAggrStmt *newnode = makeNode(RemoveAggrStmt);
1857
1858         COPY_NODE_FIELD(aggname);
1859         COPY_NODE_FIELD(aggtype);
1860         COPY_SCALAR_FIELD(behavior);
1861
1862         return newnode;
1863 }
1864
1865 static RemoveFuncStmt *
1866 _copyRemoveFuncStmt(RemoveFuncStmt *from)
1867 {
1868         RemoveFuncStmt *newnode = makeNode(RemoveFuncStmt);
1869
1870         COPY_NODE_FIELD(funcname);
1871         COPY_NODE_FIELD(args);
1872         COPY_SCALAR_FIELD(behavior);
1873
1874         return newnode;
1875 }
1876
1877 static RemoveOperStmt *
1878 _copyRemoveOperStmt(RemoveOperStmt *from)
1879 {
1880         RemoveOperStmt *newnode = makeNode(RemoveOperStmt);
1881
1882         COPY_NODE_FIELD(opname);
1883         COPY_NODE_FIELD(args);
1884         COPY_SCALAR_FIELD(behavior);
1885
1886         return newnode;
1887 }
1888
1889 static RemoveOpClassStmt *
1890 _copyRemoveOpClassStmt(RemoveOpClassStmt *from)
1891 {
1892         RemoveOpClassStmt *newnode = makeNode(RemoveOpClassStmt);
1893
1894         COPY_NODE_FIELD(opclassname);
1895         COPY_STRING_FIELD(amname);
1896         COPY_SCALAR_FIELD(behavior);
1897
1898         return newnode;
1899 }
1900
1901 static RenameStmt *
1902 _copyRenameStmt(RenameStmt *from)
1903 {
1904         RenameStmt *newnode = makeNode(RenameStmt);
1905
1906         COPY_NODE_FIELD(relation);
1907         COPY_NODE_FIELD(object);
1908         COPY_NODE_FIELD(objarg);
1909         COPY_STRING_FIELD(subname);
1910         COPY_STRING_FIELD(newname);
1911         COPY_SCALAR_FIELD(renameType);
1912
1913         return newnode;
1914 }
1915
1916 static RuleStmt *
1917 _copyRuleStmt(RuleStmt *from)
1918 {
1919         RuleStmt   *newnode = makeNode(RuleStmt);
1920
1921         COPY_NODE_FIELD(relation);
1922         COPY_STRING_FIELD(rulename);
1923         COPY_NODE_FIELD(whereClause);
1924         COPY_SCALAR_FIELD(event);
1925         COPY_SCALAR_FIELD(instead);
1926         COPY_NODE_FIELD(actions);
1927         COPY_SCALAR_FIELD(replace);
1928
1929         return newnode;
1930 }
1931
1932 static NotifyStmt *
1933 _copyNotifyStmt(NotifyStmt *from)
1934 {
1935         NotifyStmt *newnode = makeNode(NotifyStmt);
1936
1937         COPY_NODE_FIELD(relation);
1938
1939         return newnode;
1940 }
1941
1942 static ListenStmt *
1943 _copyListenStmt(ListenStmt *from)
1944 {
1945         ListenStmt *newnode = makeNode(ListenStmt);
1946
1947         COPY_NODE_FIELD(relation);
1948
1949         return newnode;
1950 }
1951
1952 static UnlistenStmt *
1953 _copyUnlistenStmt(UnlistenStmt *from)
1954 {
1955         UnlistenStmt *newnode = makeNode(UnlistenStmt);
1956
1957         COPY_NODE_FIELD(relation);
1958
1959         return newnode;
1960 }
1961
1962 static TransactionStmt *
1963 _copyTransactionStmt(TransactionStmt *from)
1964 {
1965         TransactionStmt *newnode = makeNode(TransactionStmt);
1966
1967         COPY_SCALAR_FIELD(kind);
1968         COPY_NODE_FIELD(options);
1969
1970         return newnode;
1971 }
1972
1973 static CompositeTypeStmt *
1974 _copyCompositeTypeStmt(CompositeTypeStmt *from)
1975 {
1976         CompositeTypeStmt *newnode = makeNode(CompositeTypeStmt);
1977
1978         COPY_NODE_FIELD(typevar);
1979         COPY_NODE_FIELD(coldeflist);
1980
1981         return newnode;
1982 }
1983
1984 static ViewStmt *
1985 _copyViewStmt(ViewStmt *from)
1986 {
1987         ViewStmt   *newnode = makeNode(ViewStmt);
1988
1989         COPY_NODE_FIELD(view);
1990         COPY_NODE_FIELD(aliases);
1991         COPY_NODE_FIELD(query);
1992         COPY_SCALAR_FIELD(replace);
1993
1994         return newnode;
1995 }
1996
1997 static LoadStmt *
1998 _copyLoadStmt(LoadStmt *from)
1999 {
2000         LoadStmt   *newnode = makeNode(LoadStmt);
2001
2002         COPY_STRING_FIELD(filename);
2003
2004         return newnode;
2005 }
2006
2007 static CreateDomainStmt *
2008 _copyCreateDomainStmt(CreateDomainStmt *from)
2009 {
2010         CreateDomainStmt *newnode = makeNode(CreateDomainStmt);
2011
2012         COPY_NODE_FIELD(domainname);
2013         COPY_NODE_FIELD(typename);
2014         COPY_NODE_FIELD(constraints);
2015
2016         return newnode;
2017 }
2018
2019 static CreateOpClassStmt *
2020 _copyCreateOpClassStmt(CreateOpClassStmt *from)
2021 {
2022         CreateOpClassStmt *newnode = makeNode(CreateOpClassStmt);
2023
2024         COPY_NODE_FIELD(opclassname);
2025         COPY_STRING_FIELD(amname);
2026         COPY_NODE_FIELD(datatype);
2027         COPY_NODE_FIELD(items);
2028         COPY_SCALAR_FIELD(isDefault);
2029
2030         return newnode;
2031 }
2032
2033 static CreateOpClassItem *
2034 _copyCreateOpClassItem(CreateOpClassItem *from)
2035 {
2036         CreateOpClassItem *newnode = makeNode(CreateOpClassItem);
2037
2038         COPY_SCALAR_FIELD(itemtype);
2039         COPY_NODE_FIELD(name);
2040         COPY_NODE_FIELD(args);
2041         COPY_SCALAR_FIELD(number);
2042         COPY_SCALAR_FIELD(recheck);
2043         COPY_NODE_FIELD(storedtype);
2044
2045         return newnode;
2046 }
2047
2048 static CreatedbStmt *
2049 _copyCreatedbStmt(CreatedbStmt *from)
2050 {
2051         CreatedbStmt *newnode = makeNode(CreatedbStmt);
2052
2053         COPY_STRING_FIELD(dbname);
2054         COPY_NODE_FIELD(options);
2055
2056         return newnode;
2057 }
2058
2059 static AlterDatabaseSetStmt *
2060 _copyAlterDatabaseSetStmt(AlterDatabaseSetStmt *from)
2061 {
2062         AlterDatabaseSetStmt *newnode = makeNode(AlterDatabaseSetStmt);
2063
2064         COPY_STRING_FIELD(dbname);
2065         COPY_STRING_FIELD(variable);
2066         COPY_NODE_FIELD(value);
2067
2068         return newnode;
2069 }
2070
2071 static DropdbStmt *
2072 _copyDropdbStmt(DropdbStmt *from)
2073 {
2074         DropdbStmt *newnode = makeNode(DropdbStmt);
2075
2076         COPY_STRING_FIELD(dbname);
2077
2078         return newnode;
2079 }
2080
2081 static VacuumStmt *
2082 _copyVacuumStmt(VacuumStmt *from)
2083 {
2084         VacuumStmt *newnode = makeNode(VacuumStmt);
2085
2086         COPY_SCALAR_FIELD(vacuum);
2087         COPY_SCALAR_FIELD(full);
2088         COPY_SCALAR_FIELD(analyze);
2089         COPY_SCALAR_FIELD(freeze);
2090         COPY_SCALAR_FIELD(verbose);
2091         COPY_NODE_FIELD(relation);
2092         COPY_NODE_FIELD(va_cols);
2093
2094         return newnode;
2095 }
2096
2097 static ExplainStmt *
2098 _copyExplainStmt(ExplainStmt *from)
2099 {
2100         ExplainStmt *newnode = makeNode(ExplainStmt);
2101
2102         COPY_NODE_FIELD(query);
2103         COPY_SCALAR_FIELD(verbose);
2104         COPY_SCALAR_FIELD(analyze);
2105
2106         return newnode;
2107 }
2108
2109 static CreateSeqStmt *
2110 _copyCreateSeqStmt(CreateSeqStmt *from)
2111 {
2112         CreateSeqStmt *newnode = makeNode(CreateSeqStmt);
2113
2114         COPY_NODE_FIELD(sequence);
2115         COPY_NODE_FIELD(options);
2116
2117         return newnode;
2118 }
2119
2120 static AlterSeqStmt *
2121 _copyAlterSeqStmt(AlterSeqStmt * from)
2122 {
2123         AlterSeqStmt *newnode = makeNode(AlterSeqStmt);
2124
2125         COPY_NODE_FIELD(sequence);
2126         COPY_NODE_FIELD(options);
2127
2128         return newnode;
2129 }
2130
2131 static VariableSetStmt *
2132 _copyVariableSetStmt(VariableSetStmt *from)
2133 {
2134         VariableSetStmt *newnode = makeNode(VariableSetStmt);
2135
2136         COPY_STRING_FIELD(name);
2137         COPY_NODE_FIELD(args);
2138         COPY_SCALAR_FIELD(is_local);
2139
2140         return newnode;
2141 }
2142
2143 static VariableShowStmt *
2144 _copyVariableShowStmt(VariableShowStmt *from)
2145 {
2146         VariableShowStmt *newnode = makeNode(VariableShowStmt);
2147
2148         COPY_STRING_FIELD(name);
2149
2150         return newnode;
2151 }
2152
2153 static VariableResetStmt *
2154 _copyVariableResetStmt(VariableResetStmt *from)
2155 {
2156         VariableResetStmt *newnode = makeNode(VariableResetStmt);
2157
2158         COPY_STRING_FIELD(name);
2159
2160         return newnode;
2161 }
2162
2163 static CreateTrigStmt *
2164 _copyCreateTrigStmt(CreateTrigStmt *from)
2165 {
2166         CreateTrigStmt *newnode = makeNode(CreateTrigStmt);
2167
2168         COPY_STRING_FIELD(trigname);
2169         COPY_NODE_FIELD(relation);
2170         COPY_NODE_FIELD(funcname);
2171         COPY_NODE_FIELD(args);
2172         COPY_SCALAR_FIELD(before);
2173         COPY_SCALAR_FIELD(row);
2174         strcpy(newnode->actions, from->actions);        /* in-line string field */
2175         COPY_SCALAR_FIELD(isconstraint);
2176         COPY_SCALAR_FIELD(deferrable);
2177         COPY_SCALAR_FIELD(initdeferred);
2178         COPY_NODE_FIELD(constrrel);
2179
2180         return newnode;
2181 }
2182
2183 static DropPropertyStmt *
2184 _copyDropPropertyStmt(DropPropertyStmt *from)
2185 {
2186         DropPropertyStmt *newnode = makeNode(DropPropertyStmt);
2187
2188         COPY_NODE_FIELD(relation);
2189         COPY_STRING_FIELD(property);
2190         COPY_SCALAR_FIELD(removeType);
2191         COPY_SCALAR_FIELD(behavior);
2192
2193         return newnode;
2194 }
2195
2196 static CreatePLangStmt *
2197 _copyCreatePLangStmt(CreatePLangStmt *from)
2198 {
2199         CreatePLangStmt *newnode = makeNode(CreatePLangStmt);
2200
2201         COPY_STRING_FIELD(plname);
2202         COPY_NODE_FIELD(plhandler);
2203         COPY_NODE_FIELD(plvalidator);
2204         COPY_SCALAR_FIELD(pltrusted);
2205
2206         return newnode;
2207 }
2208
2209 static DropPLangStmt *
2210 _copyDropPLangStmt(DropPLangStmt *from)
2211 {
2212         DropPLangStmt *newnode = makeNode(DropPLangStmt);
2213
2214         COPY_STRING_FIELD(plname);
2215         COPY_SCALAR_FIELD(behavior);
2216
2217         return newnode;
2218 }
2219
2220 static CreateUserStmt *
2221 _copyCreateUserStmt(CreateUserStmt *from)
2222 {
2223         CreateUserStmt *newnode = makeNode(CreateUserStmt);
2224
2225         COPY_STRING_FIELD(user);
2226         COPY_NODE_FIELD(options);
2227
2228         return newnode;
2229 }
2230
2231 static AlterUserStmt *
2232 _copyAlterUserStmt(AlterUserStmt *from)
2233 {
2234         AlterUserStmt *newnode = makeNode(AlterUserStmt);
2235
2236         COPY_STRING_FIELD(user);
2237         COPY_NODE_FIELD(options);
2238
2239         return newnode;
2240 }
2241
2242 static AlterUserSetStmt *
2243 _copyAlterUserSetStmt(AlterUserSetStmt *from)
2244 {
2245         AlterUserSetStmt *newnode = makeNode(AlterUserSetStmt);
2246
2247         COPY_STRING_FIELD(user);
2248         COPY_STRING_FIELD(variable);
2249         COPY_NODE_FIELD(value);
2250
2251         return newnode;
2252 }
2253
2254 static DropUserStmt *
2255 _copyDropUserStmt(DropUserStmt *from)
2256 {
2257         DropUserStmt *newnode = makeNode(DropUserStmt);
2258
2259         COPY_NODE_FIELD(users);
2260
2261         return newnode;
2262 }
2263
2264 static LockStmt *
2265 _copyLockStmt(LockStmt *from)
2266 {
2267         LockStmt   *newnode = makeNode(LockStmt);
2268
2269         COPY_NODE_FIELD(relations);
2270         COPY_SCALAR_FIELD(mode);
2271
2272         return newnode;
2273 }
2274
2275 static ConstraintsSetStmt *
2276 _copyConstraintsSetStmt(ConstraintsSetStmt *from)
2277 {
2278         ConstraintsSetStmt *newnode = makeNode(ConstraintsSetStmt);
2279
2280         COPY_NODE_FIELD(constraints);
2281         COPY_SCALAR_FIELD(deferred);
2282
2283         return newnode;
2284 }
2285
2286 static CreateGroupStmt *
2287 _copyCreateGroupStmt(CreateGroupStmt *from)
2288 {
2289         CreateGroupStmt *newnode = makeNode(CreateGroupStmt);
2290
2291         COPY_STRING_FIELD(name);
2292         COPY_NODE_FIELD(options);
2293
2294         return newnode;
2295 }
2296
2297 static AlterGroupStmt *
2298 _copyAlterGroupStmt(AlterGroupStmt *from)
2299 {
2300         AlterGroupStmt *newnode = makeNode(AlterGroupStmt);
2301
2302         COPY_STRING_FIELD(name);
2303         COPY_SCALAR_FIELD(action);
2304         COPY_NODE_FIELD(listUsers);
2305
2306         return newnode;
2307 }
2308
2309 static DropGroupStmt *
2310 _copyDropGroupStmt(DropGroupStmt *from)
2311 {
2312         DropGroupStmt *newnode = makeNode(DropGroupStmt);
2313
2314         COPY_STRING_FIELD(name);
2315
2316         return newnode;
2317 }
2318
2319 static ReindexStmt *
2320 _copyReindexStmt(ReindexStmt *from)
2321 {
2322         ReindexStmt *newnode = makeNode(ReindexStmt);
2323
2324         COPY_SCALAR_FIELD(kind);
2325         COPY_NODE_FIELD(relation);
2326         COPY_STRING_FIELD(name);
2327         COPY_SCALAR_FIELD(force);
2328         COPY_SCALAR_FIELD(all);
2329
2330         return newnode;
2331 }
2332
2333 static CreateSchemaStmt *
2334 _copyCreateSchemaStmt(CreateSchemaStmt *from)
2335 {
2336         CreateSchemaStmt *newnode = makeNode(CreateSchemaStmt);
2337
2338         COPY_STRING_FIELD(schemaname);
2339         COPY_STRING_FIELD(authid);
2340         COPY_NODE_FIELD(schemaElts);
2341
2342         return newnode;
2343 }
2344
2345 static CreateConversionStmt *
2346 _copyCreateConversionStmt(CreateConversionStmt *from)
2347 {
2348         CreateConversionStmt *newnode = makeNode(CreateConversionStmt);
2349
2350         COPY_NODE_FIELD(conversion_name);
2351         COPY_STRING_FIELD(for_encoding_name);
2352         COPY_STRING_FIELD(to_encoding_name);
2353         COPY_NODE_FIELD(func_name);
2354         COPY_SCALAR_FIELD(def);
2355
2356         return newnode;
2357 }
2358
2359 static CreateCastStmt *
2360 _copyCreateCastStmt(CreateCastStmt *from)
2361 {
2362         CreateCastStmt *newnode = makeNode(CreateCastStmt);
2363
2364         COPY_NODE_FIELD(sourcetype);
2365         COPY_NODE_FIELD(targettype);
2366         COPY_NODE_FIELD(func);
2367         COPY_SCALAR_FIELD(context);
2368
2369         return newnode;
2370 }
2371
2372 static DropCastStmt *
2373 _copyDropCastStmt(DropCastStmt *from)
2374 {
2375         DropCastStmt *newnode = makeNode(DropCastStmt);
2376
2377         COPY_NODE_FIELD(sourcetype);
2378         COPY_NODE_FIELD(targettype);
2379         COPY_SCALAR_FIELD(behavior);
2380
2381         return newnode;
2382 }
2383
2384 static PrepareStmt *
2385 _copyPrepareStmt(PrepareStmt *from)
2386 {
2387         PrepareStmt *newnode = makeNode(PrepareStmt);
2388
2389         COPY_STRING_FIELD(name);
2390         COPY_NODE_FIELD(argtypes);
2391         COPY_OIDLIST_FIELD(argtype_oids);
2392         COPY_NODE_FIELD(query);
2393
2394         return newnode;
2395 }
2396
2397 static ExecuteStmt *
2398 _copyExecuteStmt(ExecuteStmt *from)
2399 {
2400         ExecuteStmt *newnode = makeNode(ExecuteStmt);
2401
2402         COPY_STRING_FIELD(name);
2403         COPY_NODE_FIELD(into);
2404         COPY_NODE_FIELD(params);
2405
2406         return newnode;
2407 }
2408
2409 static DeallocateStmt *
2410 _copyDeallocateStmt(DeallocateStmt *from)
2411 {
2412         DeallocateStmt *newnode = makeNode(DeallocateStmt);
2413
2414         COPY_STRING_FIELD(name);
2415
2416         return newnode;
2417 }
2418
2419
2420 /* ****************************************************************
2421  *                                      pg_list.h copy functions
2422  * ****************************************************************
2423  */
2424
2425 static Value *
2426 _copyValue(Value *from)
2427 {
2428         Value      *newnode = makeNode(Value);
2429
2430         /* See also _copyAConst when changing this code! */
2431
2432         COPY_SCALAR_FIELD(type);
2433         switch (from->type)
2434         {
2435                 case T_Integer:
2436                         COPY_SCALAR_FIELD(val.ival);
2437                         break;
2438                 case T_Float:
2439                 case T_String:
2440                 case T_BitString:
2441                         COPY_STRING_FIELD(val.str);
2442                         break;
2443                 case T_Null:
2444                         /* nothing to do */
2445                         break;
2446                 default:
2447                         elog(ERROR, "unrecognized node type: %d",
2448                                  (int) from->type);
2449                         break;
2450         }
2451         return newnode;
2452 }
2453
2454 /*
2455  * copyObject
2456  *
2457  * Create a copy of a Node tree or list.  This is a "deep" copy: all
2458  * substructure is copied too, recursively.
2459  */
2460 void *
2461 copyObject(void *from)
2462 {
2463         void       *retval;
2464
2465         if (from == NULL)
2466                 return NULL;
2467
2468         switch (nodeTag(from))
2469         {
2470                         /*
2471                          * PLAN NODES
2472                          */
2473                 case T_Plan:
2474                         retval = _copyPlan(from);
2475                         break;
2476                 case T_Result:
2477                         retval = _copyResult(from);
2478                         break;
2479                 case T_Append:
2480                         retval = _copyAppend(from);
2481                         break;
2482                 case T_Scan:
2483                         retval = _copyScan(from);
2484                         break;
2485                 case T_SeqScan:
2486                         retval = _copySeqScan(from);
2487                         break;
2488                 case T_IndexScan:
2489                         retval = _copyIndexScan(from);
2490                         break;
2491                 case T_TidScan:
2492                         retval = _copyTidScan(from);
2493                         break;
2494                 case T_SubqueryScan:
2495                         retval = _copySubqueryScan(from);
2496                         break;
2497                 case T_FunctionScan:
2498                         retval = _copyFunctionScan(from);
2499                         break;
2500                 case T_Join:
2501                         retval = _copyJoin(from);
2502                         break;
2503                 case T_NestLoop:
2504                         retval = _copyNestLoop(from);
2505                         break;
2506                 case T_MergeJoin:
2507                         retval = _copyMergeJoin(from);
2508                         break;
2509                 case T_HashJoin:
2510                         retval = _copyHashJoin(from);
2511                         break;
2512                 case T_Material:
2513                         retval = _copyMaterial(from);
2514                         break;
2515                 case T_Sort:
2516                         retval = _copySort(from);
2517                         break;
2518                 case T_Group:
2519                         retval = _copyGroup(from);
2520                         break;
2521                 case T_Agg:
2522                         retval = _copyAgg(from);
2523                         break;
2524                 case T_Unique:
2525                         retval = _copyUnique(from);
2526                         break;
2527                 case T_Hash:
2528                         retval = _copyHash(from);
2529                         break;
2530                 case T_SetOp:
2531                         retval = _copySetOp(from);
2532                         break;
2533                 case T_Limit:
2534                         retval = _copyLimit(from);
2535                         break;
2536
2537                         /*
2538                          * PRIMITIVE NODES
2539                          */
2540                 case T_Resdom:
2541                         retval = _copyResdom(from);
2542                         break;
2543                 case T_Alias:
2544                         retval = _copyAlias(from);
2545                         break;
2546                 case T_RangeVar:
2547                         retval = _copyRangeVar(from);
2548                         break;
2549                 case T_Var:
2550                         retval = _copyVar(from);
2551                         break;
2552                 case T_Const:
2553                         retval = _copyConst(from);
2554                         break;
2555                 case T_Param:
2556                         retval = _copyParam(from);
2557                         break;
2558                 case T_Aggref:
2559                         retval = _copyAggref(from);
2560                         break;
2561                 case T_ArrayRef:
2562                         retval = _copyArrayRef(from);
2563                         break;
2564                 case T_FuncExpr:
2565                         retval = _copyFuncExpr(from);
2566                         break;
2567                 case T_OpExpr:
2568                         retval = _copyOpExpr(from);
2569                         break;
2570                 case T_DistinctExpr:
2571                         retval = _copyDistinctExpr(from);
2572                         break;
2573                 case T_ScalarArrayOpExpr:
2574                         retval = _copyScalarArrayOpExpr(from);
2575                         break;
2576                 case T_BoolExpr:
2577                         retval = _copyBoolExpr(from);
2578                         break;
2579                 case T_SubLink:
2580                         retval = _copySubLink(from);
2581                         break;
2582                 case T_SubPlan:
2583                         retval = _copySubPlan(from);
2584                         break;
2585                 case T_FieldSelect:
2586                         retval = _copyFieldSelect(from);
2587                         break;
2588                 case T_RelabelType:
2589                         retval = _copyRelabelType(from);
2590                         break;
2591                 case T_CaseExpr:
2592                         retval = _copyCaseExpr(from);
2593                         break;
2594                 case T_CaseWhen:
2595                         retval = _copyCaseWhen(from);
2596                         break;
2597                 case T_ArrayExpr:
2598                         retval = _copyArrayExpr(from);
2599                         break;
2600                 case T_CoalesceExpr:
2601                         retval = _copyCoalesceExpr(from);
2602                         break;
2603                 case T_NullIfExpr:
2604                         retval = _copyNullIfExpr(from);
2605                         break;
2606                 case T_NullTest:
2607                         retval = _copyNullTest(from);
2608                         break;
2609                 case T_BooleanTest:
2610                         retval = _copyBooleanTest(from);
2611                         break;
2612                 case T_CoerceToDomain:
2613                         retval = _copyCoerceToDomain(from);
2614                         break;
2615                 case T_CoerceToDomainValue:
2616                         retval = _copyCoerceToDomainValue(from);
2617                         break;
2618                 case T_SetToDefault:
2619                         retval = _copySetToDefault(from);
2620                         break;
2621                 case T_TargetEntry:
2622                         retval = _copyTargetEntry(from);
2623                         break;
2624                 case T_RangeTblRef:
2625                         retval = _copyRangeTblRef(from);
2626                         break;
2627                 case T_JoinExpr:
2628                         retval = _copyJoinExpr(from);
2629                         break;
2630                 case T_FromExpr:
2631                         retval = _copyFromExpr(from);
2632                         break;
2633
2634                         /*
2635                          * RELATION NODES
2636                          */
2637                 case T_PathKeyItem:
2638                         retval = _copyPathKeyItem(from);
2639                         break;
2640                 case T_RestrictInfo:
2641                         retval = _copyRestrictInfo(from);
2642                         break;
2643                 case T_JoinInfo:
2644                         retval = _copyJoinInfo(from);
2645                         break;
2646                 case T_InClauseInfo:
2647                         retval = _copyInClauseInfo(from);
2648                         break;
2649
2650                         /*
2651                          * VALUE NODES
2652                          */
2653                 case T_Integer:
2654                 case T_Float:
2655                 case T_String:
2656                 case T_BitString:
2657                 case T_Null:
2658                         retval = _copyValue(from);
2659                         break;
2660                 case T_List:
2661                         {
2662                                 List       *list = from,
2663                                                    *oldl,
2664                                                    *newcell,
2665                                                    *prev;
2666
2667                                 /* rather ugly coding for speed... */
2668                                 /* Note the input list cannot be NIL if we got here. */
2669                                 newcell = makeNode(List);
2670                                 lfirst(newcell) = copyObject(lfirst(list));
2671
2672                                 retval = (void *) newcell;
2673                                 prev = newcell;
2674
2675                                 foreach(oldl, lnext(list))
2676                                 {
2677                                         newcell = makeNode(List);
2678                                         lfirst(newcell) = copyObject(lfirst(oldl));
2679                                         prev->next = newcell;
2680                                         prev = newcell;
2681                                 }
2682                                 prev->next = NIL;
2683                         }
2684                         break;
2685
2686                         /*
2687                          * PARSE NODES
2688                          */
2689                 case T_Query:
2690                         retval = _copyQuery(from);
2691                         break;
2692                 case T_InsertStmt:
2693                         retval = _copyInsertStmt(from);
2694                         break;
2695                 case T_DeleteStmt:
2696                         retval = _copyDeleteStmt(from);
2697                         break;
2698                 case T_UpdateStmt:
2699                         retval = _copyUpdateStmt(from);
2700                         break;
2701                 case T_SelectStmt:
2702                         retval = _copySelectStmt(from);
2703                         break;
2704                 case T_SetOperationStmt:
2705                         retval = _copySetOperationStmt(from);
2706                         break;
2707                 case T_AlterTableStmt:
2708                         retval = _copyAlterTableStmt(from);
2709                         break;
2710                 case T_AlterDomainStmt:
2711                         retval = _copyAlterDomainStmt(from);
2712                         break;
2713                 case T_GrantStmt:
2714                         retval = _copyGrantStmt(from);
2715                         break;
2716                 case T_DeclareCursorStmt:
2717                         retval = _copyDeclareCursorStmt(from);
2718                         break;
2719                 case T_ClosePortalStmt:
2720                         retval = _copyClosePortalStmt(from);
2721                         break;
2722                 case T_ClusterStmt:
2723                         retval = _copyClusterStmt(from);
2724                         break;
2725                 case T_CopyStmt:
2726                         retval = _copyCopyStmt(from);
2727                         break;
2728                 case T_CreateStmt:
2729                         retval = _copyCreateStmt(from);
2730                         break;
2731                 case T_InhRelation:
2732                         retval = _copyInhRelation(from);
2733                         break;
2734                 case T_DefineStmt:
2735                         retval = _copyDefineStmt(from);
2736                         break;
2737                 case T_DropStmt:
2738                         retval = _copyDropStmt(from);
2739                         break;
2740                 case T_TruncateStmt:
2741                         retval = _copyTruncateStmt(from);
2742                         break;
2743                 case T_CommentStmt:
2744                         retval = _copyCommentStmt(from);
2745                         break;
2746                 case T_FetchStmt:
2747                         retval = _copyFetchStmt(from);
2748                         break;
2749                 case T_IndexStmt:
2750                         retval = _copyIndexStmt(from);
2751                         break;
2752                 case T_CreateFunctionStmt:
2753                         retval = _copyCreateFunctionStmt(from);
2754                         break;
2755                 case T_RemoveAggrStmt:
2756                         retval = _copyRemoveAggrStmt(from);
2757                         break;
2758                 case T_RemoveFuncStmt:
2759                         retval = _copyRemoveFuncStmt(from);
2760                         break;
2761                 case T_RemoveOperStmt:
2762                         retval = _copyRemoveOperStmt(from);
2763                         break;
2764                 case T_RemoveOpClassStmt:
2765                         retval = _copyRemoveOpClassStmt(from);
2766                         break;
2767                 case T_RenameStmt:
2768                         retval = _copyRenameStmt(from);
2769                         break;
2770                 case T_RuleStmt:
2771                         retval = _copyRuleStmt(from);
2772                         break;
2773                 case T_NotifyStmt:
2774                         retval = _copyNotifyStmt(from);
2775                         break;
2776                 case T_ListenStmt:
2777                         retval = _copyListenStmt(from);
2778                         break;
2779                 case T_UnlistenStmt:
2780                         retval = _copyUnlistenStmt(from);
2781                         break;
2782                 case T_TransactionStmt:
2783                         retval = _copyTransactionStmt(from);
2784                         break;
2785                 case T_CompositeTypeStmt:
2786                         retval = _copyCompositeTypeStmt(from);
2787                         break;
2788                 case T_ViewStmt:
2789                         retval = _copyViewStmt(from);
2790                         break;
2791                 case T_LoadStmt:
2792                         retval = _copyLoadStmt(from);
2793                         break;
2794                 case T_CreateDomainStmt:
2795                         retval = _copyCreateDomainStmt(from);
2796                         break;
2797                 case T_CreateOpClassStmt:
2798                         retval = _copyCreateOpClassStmt(from);
2799                         break;
2800                 case T_CreateOpClassItem:
2801                         retval = _copyCreateOpClassItem(from);
2802                         break;
2803                 case T_CreatedbStmt:
2804                         retval = _copyCreatedbStmt(from);
2805                         break;
2806                 case T_AlterDatabaseSetStmt:
2807                         retval = _copyAlterDatabaseSetStmt(from);
2808                         break;
2809                 case T_DropdbStmt:
2810                         retval = _copyDropdbStmt(from);
2811                         break;
2812                 case T_VacuumStmt:
2813                         retval = _copyVacuumStmt(from);
2814                         break;
2815                 case T_ExplainStmt:
2816                         retval = _copyExplainStmt(from);
2817                         break;
2818                 case T_CreateSeqStmt:
2819                         retval = _copyCreateSeqStmt(from);
2820                         break;
2821                 case T_AlterSeqStmt:
2822                         retval = _copyAlterSeqStmt(from);
2823                         break;
2824                 case T_VariableSetStmt:
2825                         retval = _copyVariableSetStmt(from);
2826                         break;
2827                 case T_VariableShowStmt:
2828                         retval = _copyVariableShowStmt(from);
2829                         break;
2830                 case T_VariableResetStmt:
2831                         retval = _copyVariableResetStmt(from);
2832                         break;
2833                 case T_CreateTrigStmt:
2834                         retval = _copyCreateTrigStmt(from);
2835                         break;
2836                 case T_DropPropertyStmt:
2837                         retval = _copyDropPropertyStmt(from);
2838                         break;
2839                 case T_CreatePLangStmt:
2840                         retval = _copyCreatePLangStmt(from);
2841                         break;
2842                 case T_DropPLangStmt:
2843                         retval = _copyDropPLangStmt(from);
2844                         break;
2845                 case T_CreateUserStmt:
2846                         retval = _copyCreateUserStmt(from);
2847                         break;
2848                 case T_AlterUserStmt:
2849                         retval = _copyAlterUserStmt(from);
2850                         break;
2851                 case T_AlterUserSetStmt:
2852                         retval = _copyAlterUserSetStmt(from);
2853                         break;
2854                 case T_DropUserStmt:
2855                         retval = _copyDropUserStmt(from);
2856                         break;
2857                 case T_LockStmt:
2858                         retval = _copyLockStmt(from);
2859                         break;
2860                 case T_ConstraintsSetStmt:
2861                         retval = _copyConstraintsSetStmt(from);
2862                         break;
2863                 case T_CreateGroupStmt:
2864                         retval = _copyCreateGroupStmt(from);
2865                         break;
2866                 case T_AlterGroupStmt:
2867                         retval = _copyAlterGroupStmt(from);
2868                         break;
2869                 case T_DropGroupStmt:
2870                         retval = _copyDropGroupStmt(from);
2871                         break;
2872                 case T_ReindexStmt:
2873                         retval = _copyReindexStmt(from);
2874                         break;
2875                 case T_CheckPointStmt:
2876                         retval = (void *) makeNode(CheckPointStmt);
2877                         break;
2878                 case T_CreateSchemaStmt:
2879                         retval = _copyCreateSchemaStmt(from);
2880                         break;
2881                 case T_CreateConversionStmt:
2882                         retval = _copyCreateConversionStmt(from);
2883                         break;
2884                 case T_CreateCastStmt:
2885                         retval = _copyCreateCastStmt(from);
2886                         break;
2887                 case T_DropCastStmt:
2888                         retval = _copyDropCastStmt(from);
2889                         break;
2890                 case T_PrepareStmt:
2891                         retval = _copyPrepareStmt(from);
2892                         break;
2893                 case T_ExecuteStmt:
2894                         retval = _copyExecuteStmt(from);
2895                         break;
2896                 case T_DeallocateStmt:
2897                         retval = _copyDeallocateStmt(from);
2898                         break;
2899
2900                 case T_A_Expr:
2901                         retval = _copyAExpr(from);
2902                         break;
2903                 case T_ColumnRef:
2904                         retval = _copyColumnRef(from);
2905                         break;
2906                 case T_ParamRef:
2907                         retval = _copyParamRef(from);
2908                         break;
2909                 case T_A_Const:
2910                         retval = _copyAConst(from);
2911                         break;
2912                 case T_FuncCall:
2913                         retval = _copyFuncCall(from);
2914                         break;
2915                 case T_A_Indices:
2916                         retval = _copyAIndices(from);
2917                         break;
2918                 case T_ExprFieldSelect:
2919                         retval = _copyExprFieldSelect(from);
2920                         break;
2921                 case T_ResTarget:
2922                         retval = _copyResTarget(from);
2923                         break;
2924                 case T_TypeCast:
2925                         retval = _copyTypeCast(from);
2926                         break;
2927                 case T_SortGroupBy:
2928                         retval = _copySortGroupBy(from);
2929                         break;
2930                 case T_RangeSubselect:
2931                         retval = _copyRangeSubselect(from);
2932                         break;
2933                 case T_RangeFunction:
2934                         retval = _copyRangeFunction(from);
2935                         break;
2936                 case T_TypeName:
2937                         retval = _copyTypeName(from);
2938                         break;
2939                 case T_IndexElem:
2940                         retval = _copyIndexElem(from);
2941                         break;
2942                 case T_ColumnDef:
2943                         retval = _copyColumnDef(from);
2944                         break;
2945                 case T_Constraint:
2946                         retval = _copyConstraint(from);
2947                         break;
2948                 case T_DefElem:
2949                         retval = _copyDefElem(from);
2950                         break;
2951                 case T_RangeTblEntry:
2952                         retval = _copyRangeTblEntry(from);
2953                         break;
2954                 case T_SortClause:
2955                         retval = _copySortClause(from);
2956                         break;
2957                 case T_GroupClause:
2958                         retval = _copyGroupClause(from);
2959                         break;
2960                 case T_FkConstraint:
2961                         retval = _copyFkConstraint(from);
2962                         break;
2963                 case T_PrivGrantee:
2964                         retval = _copyPrivGrantee(from);
2965                         break;
2966                 case T_FuncWithArgs:
2967                         retval = _copyFuncWithArgs(from);
2968                         break;
2969
2970                 default:
2971                         elog(ERROR, "unrecognized node type: %d", (int) nodeTag(from));
2972                         retval = from;          /* keep compiler quiet */
2973                         break;
2974         }
2975
2976         return retval;
2977 }