]> granicus.if.org Git - postgresql/blob - src/backend/nodes/copyfuncs.c
Fix ARRAY[] construct so that in multidimensional case, elements can
[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.265 2003/08/17 23:43:25 tgl 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(multidims);
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 SortBy *
1406 _copySortBy(SortBy *from)
1407 {
1408         SortBy *newnode = makeNode(SortBy);
1409
1410         COPY_SCALAR_FIELD(sortby_kind);
1411         COPY_NODE_FIELD(useOp);
1412         COPY_NODE_FIELD(node);
1413
1414         return newnode;
1415 }
1416
1417 static RangeSubselect *
1418 _copyRangeSubselect(RangeSubselect *from)
1419 {
1420         RangeSubselect *newnode = makeNode(RangeSubselect);
1421
1422         COPY_NODE_FIELD(subquery);
1423         COPY_NODE_FIELD(alias);
1424
1425         return newnode;
1426 }
1427
1428 static RangeFunction *
1429 _copyRangeFunction(RangeFunction *from)
1430 {
1431         RangeFunction *newnode = makeNode(RangeFunction);
1432
1433         COPY_NODE_FIELD(funccallnode);
1434         COPY_NODE_FIELD(alias);
1435         COPY_NODE_FIELD(coldeflist);
1436
1437         return newnode;
1438 }
1439
1440 static TypeCast *
1441 _copyTypeCast(TypeCast *from)
1442 {
1443         TypeCast   *newnode = makeNode(TypeCast);
1444
1445         COPY_NODE_FIELD(arg);
1446         COPY_NODE_FIELD(typename);
1447
1448         return newnode;
1449 }
1450
1451 static IndexElem *
1452 _copyIndexElem(IndexElem *from)
1453 {
1454         IndexElem  *newnode = makeNode(IndexElem);
1455
1456         COPY_STRING_FIELD(name);
1457         COPY_NODE_FIELD(expr);
1458         COPY_NODE_FIELD(opclass);
1459
1460         return newnode;
1461 }
1462
1463 static ColumnDef *
1464 _copyColumnDef(ColumnDef *from)
1465 {
1466         ColumnDef  *newnode = makeNode(ColumnDef);
1467
1468         COPY_STRING_FIELD(colname);
1469         COPY_NODE_FIELD(typename);
1470         COPY_SCALAR_FIELD(inhcount);
1471         COPY_SCALAR_FIELD(is_local);
1472         COPY_SCALAR_FIELD(is_not_null);
1473         COPY_NODE_FIELD(raw_default);
1474         COPY_STRING_FIELD(cooked_default);
1475         COPY_NODE_FIELD(constraints);
1476         COPY_NODE_FIELD(support);
1477
1478         return newnode;
1479 }
1480
1481 static Constraint *
1482 _copyConstraint(Constraint *from)
1483 {
1484         Constraint *newnode = makeNode(Constraint);
1485
1486         COPY_SCALAR_FIELD(contype);
1487         COPY_STRING_FIELD(name);
1488         COPY_NODE_FIELD(raw_expr);
1489         COPY_STRING_FIELD(cooked_expr);
1490         COPY_NODE_FIELD(keys);
1491
1492         return newnode;
1493 }
1494
1495 static DefElem *
1496 _copyDefElem(DefElem *from)
1497 {
1498         DefElem    *newnode = makeNode(DefElem);
1499
1500         COPY_STRING_FIELD(defname);
1501         COPY_NODE_FIELD(arg);
1502
1503         return newnode;
1504 }
1505
1506 static Query *
1507 _copyQuery(Query *from)
1508 {
1509         Query      *newnode = makeNode(Query);
1510
1511         COPY_SCALAR_FIELD(commandType);
1512         COPY_SCALAR_FIELD(querySource);
1513         COPY_SCALAR_FIELD(canSetTag);
1514         COPY_NODE_FIELD(utilityStmt);
1515         COPY_SCALAR_FIELD(resultRelation);
1516         COPY_NODE_FIELD(into);
1517         COPY_SCALAR_FIELD(hasAggs);
1518         COPY_SCALAR_FIELD(hasSubLinks);
1519         COPY_NODE_FIELD(rtable);
1520         COPY_NODE_FIELD(jointree);
1521         COPY_INTLIST_FIELD(rowMarks);
1522         COPY_NODE_FIELD(targetList);
1523         COPY_NODE_FIELD(groupClause);
1524         COPY_NODE_FIELD(havingQual);
1525         COPY_NODE_FIELD(distinctClause);
1526         COPY_NODE_FIELD(sortClause);
1527         COPY_NODE_FIELD(limitOffset);
1528         COPY_NODE_FIELD(limitCount);
1529         COPY_NODE_FIELD(setOperations);
1530         COPY_INTLIST_FIELD(resultRelations);
1531         COPY_NODE_FIELD(in_info_list);
1532         COPY_SCALAR_FIELD(hasJoinRTEs);
1533
1534         /*
1535          * We do not copy the other planner internal fields: base_rel_list,
1536          * other_rel_list, join_rel_list, equi_key_list, query_pathkeys. That
1537          * would get us into copying RelOptInfo/Path trees, which we don't
1538          * want to do.  It is necessary to copy in_info_list and hasJoinRTEs
1539          * for the benefit of inheritance_planner(), which may try to copy a
1540          * Query in which these are already set.
1541          */
1542
1543         return newnode;
1544 }
1545
1546 static InsertStmt *
1547 _copyInsertStmt(InsertStmt *from)
1548 {
1549         InsertStmt *newnode = makeNode(InsertStmt);
1550
1551         COPY_NODE_FIELD(relation);
1552         COPY_NODE_FIELD(cols);
1553         COPY_NODE_FIELD(targetList);
1554         COPY_NODE_FIELD(selectStmt);
1555
1556         return newnode;
1557 }
1558
1559 static DeleteStmt *
1560 _copyDeleteStmt(DeleteStmt *from)
1561 {
1562         DeleteStmt *newnode = makeNode(DeleteStmt);
1563
1564         COPY_NODE_FIELD(relation);
1565         COPY_NODE_FIELD(whereClause);
1566
1567         return newnode;
1568 }
1569
1570 static UpdateStmt *
1571 _copyUpdateStmt(UpdateStmt *from)
1572 {
1573         UpdateStmt *newnode = makeNode(UpdateStmt);
1574
1575         COPY_NODE_FIELD(relation);
1576         COPY_NODE_FIELD(targetList);
1577         COPY_NODE_FIELD(whereClause);
1578         COPY_NODE_FIELD(fromClause);
1579
1580         return newnode;
1581 }
1582
1583 static SelectStmt *
1584 _copySelectStmt(SelectStmt *from)
1585 {
1586         SelectStmt *newnode = makeNode(SelectStmt);
1587
1588         COPY_NODE_FIELD(distinctClause);
1589         COPY_NODE_FIELD(into);
1590         COPY_NODE_FIELD(intoColNames);
1591         COPY_NODE_FIELD(targetList);
1592         COPY_NODE_FIELD(fromClause);
1593         COPY_NODE_FIELD(whereClause);
1594         COPY_NODE_FIELD(groupClause);
1595         COPY_NODE_FIELD(havingClause);
1596         COPY_NODE_FIELD(sortClause);
1597         COPY_NODE_FIELD(limitOffset);
1598         COPY_NODE_FIELD(limitCount);
1599         COPY_NODE_FIELD(forUpdate);
1600         COPY_SCALAR_FIELD(op);
1601         COPY_SCALAR_FIELD(all);
1602         COPY_NODE_FIELD(larg);
1603         COPY_NODE_FIELD(rarg);
1604
1605         return newnode;
1606 }
1607
1608 static SetOperationStmt *
1609 _copySetOperationStmt(SetOperationStmt *from)
1610 {
1611         SetOperationStmt *newnode = makeNode(SetOperationStmt);
1612
1613         COPY_SCALAR_FIELD(op);
1614         COPY_SCALAR_FIELD(all);
1615         COPY_NODE_FIELD(larg);
1616         COPY_NODE_FIELD(rarg);
1617         COPY_OIDLIST_FIELD(colTypes);
1618
1619         return newnode;
1620 }
1621
1622 static AlterTableStmt *
1623 _copyAlterTableStmt(AlterTableStmt *from)
1624 {
1625         AlterTableStmt *newnode = makeNode(AlterTableStmt);
1626
1627         COPY_SCALAR_FIELD(subtype);
1628         COPY_NODE_FIELD(relation);
1629         COPY_STRING_FIELD(name);
1630         COPY_NODE_FIELD(def);
1631         COPY_SCALAR_FIELD(behavior);
1632
1633         return newnode;
1634 }
1635
1636 static AlterDomainStmt *
1637 _copyAlterDomainStmt(AlterDomainStmt *from)
1638 {
1639         AlterDomainStmt *newnode = makeNode(AlterDomainStmt);
1640
1641         COPY_SCALAR_FIELD(subtype);
1642         COPY_NODE_FIELD(typename);
1643         COPY_STRING_FIELD(name);
1644         COPY_NODE_FIELD(def);
1645         COPY_SCALAR_FIELD(behavior);
1646
1647         return newnode;
1648 }
1649
1650 static GrantStmt *
1651 _copyGrantStmt(GrantStmt *from)
1652 {
1653         GrantStmt  *newnode = makeNode(GrantStmt);
1654
1655         COPY_SCALAR_FIELD(is_grant);
1656         COPY_SCALAR_FIELD(objtype);
1657         COPY_NODE_FIELD(objects);
1658         COPY_INTLIST_FIELD(privileges);
1659         COPY_NODE_FIELD(grantees);
1660         COPY_SCALAR_FIELD(grant_option);
1661         COPY_SCALAR_FIELD(behavior);
1662
1663         return newnode;
1664 }
1665
1666 static PrivGrantee *
1667 _copyPrivGrantee(PrivGrantee *from)
1668 {
1669         PrivGrantee *newnode = makeNode(PrivGrantee);
1670
1671         COPY_STRING_FIELD(username);
1672         COPY_STRING_FIELD(groupname);
1673
1674         return newnode;
1675 }
1676
1677 static FuncWithArgs *
1678 _copyFuncWithArgs(FuncWithArgs *from)
1679 {
1680         FuncWithArgs *newnode = makeNode(FuncWithArgs);
1681
1682         COPY_NODE_FIELD(funcname);
1683         COPY_NODE_FIELD(funcargs);
1684
1685         return newnode;
1686 }
1687
1688 static DeclareCursorStmt *
1689 _copyDeclareCursorStmt(DeclareCursorStmt *from)
1690 {
1691         DeclareCursorStmt *newnode = makeNode(DeclareCursorStmt);
1692
1693         COPY_STRING_FIELD(portalname);
1694         COPY_SCALAR_FIELD(options);
1695         COPY_NODE_FIELD(query);
1696
1697         return newnode;
1698 }
1699
1700 static ClosePortalStmt *
1701 _copyClosePortalStmt(ClosePortalStmt *from)
1702 {
1703         ClosePortalStmt *newnode = makeNode(ClosePortalStmt);
1704
1705         COPY_STRING_FIELD(portalname);
1706
1707         return newnode;
1708 }
1709
1710 static ClusterStmt *
1711 _copyClusterStmt(ClusterStmt *from)
1712 {
1713         ClusterStmt *newnode = makeNode(ClusterStmt);
1714
1715         COPY_NODE_FIELD(relation);
1716         COPY_STRING_FIELD(indexname);
1717
1718         return newnode;
1719 }
1720
1721 static CopyStmt *
1722 _copyCopyStmt(CopyStmt *from)
1723 {
1724         CopyStmt   *newnode = makeNode(CopyStmt);
1725
1726         COPY_NODE_FIELD(relation);
1727         COPY_NODE_FIELD(attlist);
1728         COPY_SCALAR_FIELD(is_from);
1729         COPY_STRING_FIELD(filename);
1730         COPY_NODE_FIELD(options);
1731
1732         return newnode;
1733 }
1734
1735 static CreateStmt *
1736 _copyCreateStmt(CreateStmt *from)
1737 {
1738         CreateStmt *newnode = makeNode(CreateStmt);
1739
1740         COPY_NODE_FIELD(relation);
1741         COPY_NODE_FIELD(tableElts);
1742         COPY_NODE_FIELD(inhRelations);
1743         COPY_NODE_FIELD(constraints);
1744         COPY_SCALAR_FIELD(hasoids);
1745         COPY_SCALAR_FIELD(oncommit);
1746
1747         return newnode;
1748 }
1749
1750 static InhRelation *
1751 _copyInhRelation(InhRelation *from)
1752 {
1753         InhRelation *newnode = makeNode(InhRelation);
1754
1755         COPY_NODE_FIELD(relation);
1756         COPY_SCALAR_FIELD(including_defaults);
1757
1758         return newnode;
1759 }
1760
1761 static DefineStmt *
1762 _copyDefineStmt(DefineStmt *from)
1763 {
1764         DefineStmt *newnode = makeNode(DefineStmt);
1765
1766         COPY_SCALAR_FIELD(kind);
1767         COPY_NODE_FIELD(defnames);
1768         COPY_NODE_FIELD(definition);
1769
1770         return newnode;
1771 }
1772
1773 static DropStmt *
1774 _copyDropStmt(DropStmt *from)
1775 {
1776         DropStmt   *newnode = makeNode(DropStmt);
1777
1778         COPY_NODE_FIELD(objects);
1779         COPY_SCALAR_FIELD(removeType);
1780         COPY_SCALAR_FIELD(behavior);
1781
1782         return newnode;
1783 }
1784
1785 static TruncateStmt *
1786 _copyTruncateStmt(TruncateStmt *from)
1787 {
1788         TruncateStmt *newnode = makeNode(TruncateStmt);
1789
1790         COPY_NODE_FIELD(relation);
1791
1792         return newnode;
1793 }
1794
1795 static CommentStmt *
1796 _copyCommentStmt(CommentStmt *from)
1797 {
1798         CommentStmt *newnode = makeNode(CommentStmt);
1799
1800         COPY_SCALAR_FIELD(objtype);
1801         COPY_NODE_FIELD(objname);
1802         COPY_NODE_FIELD(objargs);
1803         COPY_STRING_FIELD(comment);
1804
1805         return newnode;
1806 }
1807
1808 static FetchStmt *
1809 _copyFetchStmt(FetchStmt *from)
1810 {
1811         FetchStmt  *newnode = makeNode(FetchStmt);
1812
1813         COPY_SCALAR_FIELD(direction);
1814         COPY_SCALAR_FIELD(howMany);
1815         COPY_STRING_FIELD(portalname);
1816         COPY_SCALAR_FIELD(ismove);
1817
1818         return newnode;
1819 }
1820
1821 static IndexStmt *
1822 _copyIndexStmt(IndexStmt *from)
1823 {
1824         IndexStmt  *newnode = makeNode(IndexStmt);
1825
1826         COPY_STRING_FIELD(idxname);
1827         COPY_NODE_FIELD(relation);
1828         COPY_STRING_FIELD(accessMethod);
1829         COPY_NODE_FIELD(indexParams);
1830         COPY_NODE_FIELD(whereClause);
1831         COPY_NODE_FIELD(rangetable);
1832         COPY_SCALAR_FIELD(unique);
1833         COPY_SCALAR_FIELD(primary);
1834         COPY_SCALAR_FIELD(isconstraint);
1835
1836         return newnode;
1837 }
1838
1839 static CreateFunctionStmt *
1840 _copyCreateFunctionStmt(CreateFunctionStmt *from)
1841 {
1842         CreateFunctionStmt *newnode = makeNode(CreateFunctionStmt);
1843
1844         COPY_SCALAR_FIELD(replace);
1845         COPY_NODE_FIELD(funcname);
1846         COPY_NODE_FIELD(argTypes);
1847         COPY_NODE_FIELD(returnType);
1848         COPY_NODE_FIELD(options);
1849         COPY_NODE_FIELD(withClause);
1850
1851         return newnode;
1852 }
1853
1854 static RemoveAggrStmt *
1855 _copyRemoveAggrStmt(RemoveAggrStmt *from)
1856 {
1857         RemoveAggrStmt *newnode = makeNode(RemoveAggrStmt);
1858
1859         COPY_NODE_FIELD(aggname);
1860         COPY_NODE_FIELD(aggtype);
1861         COPY_SCALAR_FIELD(behavior);
1862
1863         return newnode;
1864 }
1865
1866 static RemoveFuncStmt *
1867 _copyRemoveFuncStmt(RemoveFuncStmt *from)
1868 {
1869         RemoveFuncStmt *newnode = makeNode(RemoveFuncStmt);
1870
1871         COPY_NODE_FIELD(funcname);
1872         COPY_NODE_FIELD(args);
1873         COPY_SCALAR_FIELD(behavior);
1874
1875         return newnode;
1876 }
1877
1878 static RemoveOperStmt *
1879 _copyRemoveOperStmt(RemoveOperStmt *from)
1880 {
1881         RemoveOperStmt *newnode = makeNode(RemoveOperStmt);
1882
1883         COPY_NODE_FIELD(opname);
1884         COPY_NODE_FIELD(args);
1885         COPY_SCALAR_FIELD(behavior);
1886
1887         return newnode;
1888 }
1889
1890 static RemoveOpClassStmt *
1891 _copyRemoveOpClassStmt(RemoveOpClassStmt *from)
1892 {
1893         RemoveOpClassStmt *newnode = makeNode(RemoveOpClassStmt);
1894
1895         COPY_NODE_FIELD(opclassname);
1896         COPY_STRING_FIELD(amname);
1897         COPY_SCALAR_FIELD(behavior);
1898
1899         return newnode;
1900 }
1901
1902 static RenameStmt *
1903 _copyRenameStmt(RenameStmt *from)
1904 {
1905         RenameStmt *newnode = makeNode(RenameStmt);
1906
1907         COPY_NODE_FIELD(relation);
1908         COPY_NODE_FIELD(object);
1909         COPY_NODE_FIELD(objarg);
1910         COPY_STRING_FIELD(subname);
1911         COPY_STRING_FIELD(newname);
1912         COPY_SCALAR_FIELD(renameType);
1913
1914         return newnode;
1915 }
1916
1917 static RuleStmt *
1918 _copyRuleStmt(RuleStmt *from)
1919 {
1920         RuleStmt   *newnode = makeNode(RuleStmt);
1921
1922         COPY_NODE_FIELD(relation);
1923         COPY_STRING_FIELD(rulename);
1924         COPY_NODE_FIELD(whereClause);
1925         COPY_SCALAR_FIELD(event);
1926         COPY_SCALAR_FIELD(instead);
1927         COPY_NODE_FIELD(actions);
1928         COPY_SCALAR_FIELD(replace);
1929
1930         return newnode;
1931 }
1932
1933 static NotifyStmt *
1934 _copyNotifyStmt(NotifyStmt *from)
1935 {
1936         NotifyStmt *newnode = makeNode(NotifyStmt);
1937
1938         COPY_NODE_FIELD(relation);
1939
1940         return newnode;
1941 }
1942
1943 static ListenStmt *
1944 _copyListenStmt(ListenStmt *from)
1945 {
1946         ListenStmt *newnode = makeNode(ListenStmt);
1947
1948         COPY_NODE_FIELD(relation);
1949
1950         return newnode;
1951 }
1952
1953 static UnlistenStmt *
1954 _copyUnlistenStmt(UnlistenStmt *from)
1955 {
1956         UnlistenStmt *newnode = makeNode(UnlistenStmt);
1957
1958         COPY_NODE_FIELD(relation);
1959
1960         return newnode;
1961 }
1962
1963 static TransactionStmt *
1964 _copyTransactionStmt(TransactionStmt *from)
1965 {
1966         TransactionStmt *newnode = makeNode(TransactionStmt);
1967
1968         COPY_SCALAR_FIELD(kind);
1969         COPY_NODE_FIELD(options);
1970
1971         return newnode;
1972 }
1973
1974 static CompositeTypeStmt *
1975 _copyCompositeTypeStmt(CompositeTypeStmt *from)
1976 {
1977         CompositeTypeStmt *newnode = makeNode(CompositeTypeStmt);
1978
1979         COPY_NODE_FIELD(typevar);
1980         COPY_NODE_FIELD(coldeflist);
1981
1982         return newnode;
1983 }
1984
1985 static ViewStmt *
1986 _copyViewStmt(ViewStmt *from)
1987 {
1988         ViewStmt   *newnode = makeNode(ViewStmt);
1989
1990         COPY_NODE_FIELD(view);
1991         COPY_NODE_FIELD(aliases);
1992         COPY_NODE_FIELD(query);
1993         COPY_SCALAR_FIELD(replace);
1994
1995         return newnode;
1996 }
1997
1998 static LoadStmt *
1999 _copyLoadStmt(LoadStmt *from)
2000 {
2001         LoadStmt   *newnode = makeNode(LoadStmt);
2002
2003         COPY_STRING_FIELD(filename);
2004
2005         return newnode;
2006 }
2007
2008 static CreateDomainStmt *
2009 _copyCreateDomainStmt(CreateDomainStmt *from)
2010 {
2011         CreateDomainStmt *newnode = makeNode(CreateDomainStmt);
2012
2013         COPY_NODE_FIELD(domainname);
2014         COPY_NODE_FIELD(typename);
2015         COPY_NODE_FIELD(constraints);
2016
2017         return newnode;
2018 }
2019
2020 static CreateOpClassStmt *
2021 _copyCreateOpClassStmt(CreateOpClassStmt *from)
2022 {
2023         CreateOpClassStmt *newnode = makeNode(CreateOpClassStmt);
2024
2025         COPY_NODE_FIELD(opclassname);
2026         COPY_STRING_FIELD(amname);
2027         COPY_NODE_FIELD(datatype);
2028         COPY_NODE_FIELD(items);
2029         COPY_SCALAR_FIELD(isDefault);
2030
2031         return newnode;
2032 }
2033
2034 static CreateOpClassItem *
2035 _copyCreateOpClassItem(CreateOpClassItem *from)
2036 {
2037         CreateOpClassItem *newnode = makeNode(CreateOpClassItem);
2038
2039         COPY_SCALAR_FIELD(itemtype);
2040         COPY_NODE_FIELD(name);
2041         COPY_NODE_FIELD(args);
2042         COPY_SCALAR_FIELD(number);
2043         COPY_SCALAR_FIELD(recheck);
2044         COPY_NODE_FIELD(storedtype);
2045
2046         return newnode;
2047 }
2048
2049 static CreatedbStmt *
2050 _copyCreatedbStmt(CreatedbStmt *from)
2051 {
2052         CreatedbStmt *newnode = makeNode(CreatedbStmt);
2053
2054         COPY_STRING_FIELD(dbname);
2055         COPY_NODE_FIELD(options);
2056
2057         return newnode;
2058 }
2059
2060 static AlterDatabaseSetStmt *
2061 _copyAlterDatabaseSetStmt(AlterDatabaseSetStmt *from)
2062 {
2063         AlterDatabaseSetStmt *newnode = makeNode(AlterDatabaseSetStmt);
2064
2065         COPY_STRING_FIELD(dbname);
2066         COPY_STRING_FIELD(variable);
2067         COPY_NODE_FIELD(value);
2068
2069         return newnode;
2070 }
2071
2072 static DropdbStmt *
2073 _copyDropdbStmt(DropdbStmt *from)
2074 {
2075         DropdbStmt *newnode = makeNode(DropdbStmt);
2076
2077         COPY_STRING_FIELD(dbname);
2078
2079         return newnode;
2080 }
2081
2082 static VacuumStmt *
2083 _copyVacuumStmt(VacuumStmt *from)
2084 {
2085         VacuumStmt *newnode = makeNode(VacuumStmt);
2086
2087         COPY_SCALAR_FIELD(vacuum);
2088         COPY_SCALAR_FIELD(full);
2089         COPY_SCALAR_FIELD(analyze);
2090         COPY_SCALAR_FIELD(freeze);
2091         COPY_SCALAR_FIELD(verbose);
2092         COPY_NODE_FIELD(relation);
2093         COPY_NODE_FIELD(va_cols);
2094
2095         return newnode;
2096 }
2097
2098 static ExplainStmt *
2099 _copyExplainStmt(ExplainStmt *from)
2100 {
2101         ExplainStmt *newnode = makeNode(ExplainStmt);
2102
2103         COPY_NODE_FIELD(query);
2104         COPY_SCALAR_FIELD(verbose);
2105         COPY_SCALAR_FIELD(analyze);
2106
2107         return newnode;
2108 }
2109
2110 static CreateSeqStmt *
2111 _copyCreateSeqStmt(CreateSeqStmt *from)
2112 {
2113         CreateSeqStmt *newnode = makeNode(CreateSeqStmt);
2114
2115         COPY_NODE_FIELD(sequence);
2116         COPY_NODE_FIELD(options);
2117
2118         return newnode;
2119 }
2120
2121 static AlterSeqStmt *
2122 _copyAlterSeqStmt(AlterSeqStmt *from)
2123 {
2124         AlterSeqStmt *newnode = makeNode(AlterSeqStmt);
2125
2126         COPY_NODE_FIELD(sequence);
2127         COPY_NODE_FIELD(options);
2128
2129         return newnode;
2130 }
2131
2132 static VariableSetStmt *
2133 _copyVariableSetStmt(VariableSetStmt *from)
2134 {
2135         VariableSetStmt *newnode = makeNode(VariableSetStmt);
2136
2137         COPY_STRING_FIELD(name);
2138         COPY_NODE_FIELD(args);
2139         COPY_SCALAR_FIELD(is_local);
2140
2141         return newnode;
2142 }
2143
2144 static VariableShowStmt *
2145 _copyVariableShowStmt(VariableShowStmt *from)
2146 {
2147         VariableShowStmt *newnode = makeNode(VariableShowStmt);
2148
2149         COPY_STRING_FIELD(name);
2150
2151         return newnode;
2152 }
2153
2154 static VariableResetStmt *
2155 _copyVariableResetStmt(VariableResetStmt *from)
2156 {
2157         VariableResetStmt *newnode = makeNode(VariableResetStmt);
2158
2159         COPY_STRING_FIELD(name);
2160
2161         return newnode;
2162 }
2163
2164 static CreateTrigStmt *
2165 _copyCreateTrigStmt(CreateTrigStmt *from)
2166 {
2167         CreateTrigStmt *newnode = makeNode(CreateTrigStmt);
2168
2169         COPY_STRING_FIELD(trigname);
2170         COPY_NODE_FIELD(relation);
2171         COPY_NODE_FIELD(funcname);
2172         COPY_NODE_FIELD(args);
2173         COPY_SCALAR_FIELD(before);
2174         COPY_SCALAR_FIELD(row);
2175         strcpy(newnode->actions, from->actions);        /* in-line string field */
2176         COPY_SCALAR_FIELD(isconstraint);
2177         COPY_SCALAR_FIELD(deferrable);
2178         COPY_SCALAR_FIELD(initdeferred);
2179         COPY_NODE_FIELD(constrrel);
2180
2181         return newnode;
2182 }
2183
2184 static DropPropertyStmt *
2185 _copyDropPropertyStmt(DropPropertyStmt *from)
2186 {
2187         DropPropertyStmt *newnode = makeNode(DropPropertyStmt);
2188
2189         COPY_NODE_FIELD(relation);
2190         COPY_STRING_FIELD(property);
2191         COPY_SCALAR_FIELD(removeType);
2192         COPY_SCALAR_FIELD(behavior);
2193
2194         return newnode;
2195 }
2196
2197 static CreatePLangStmt *
2198 _copyCreatePLangStmt(CreatePLangStmt *from)
2199 {
2200         CreatePLangStmt *newnode = makeNode(CreatePLangStmt);
2201
2202         COPY_STRING_FIELD(plname);
2203         COPY_NODE_FIELD(plhandler);
2204         COPY_NODE_FIELD(plvalidator);
2205         COPY_SCALAR_FIELD(pltrusted);
2206
2207         return newnode;
2208 }
2209
2210 static DropPLangStmt *
2211 _copyDropPLangStmt(DropPLangStmt *from)
2212 {
2213         DropPLangStmt *newnode = makeNode(DropPLangStmt);
2214
2215         COPY_STRING_FIELD(plname);
2216         COPY_SCALAR_FIELD(behavior);
2217
2218         return newnode;
2219 }
2220
2221 static CreateUserStmt *
2222 _copyCreateUserStmt(CreateUserStmt *from)
2223 {
2224         CreateUserStmt *newnode = makeNode(CreateUserStmt);
2225
2226         COPY_STRING_FIELD(user);
2227         COPY_NODE_FIELD(options);
2228
2229         return newnode;
2230 }
2231
2232 static AlterUserStmt *
2233 _copyAlterUserStmt(AlterUserStmt *from)
2234 {
2235         AlterUserStmt *newnode = makeNode(AlterUserStmt);
2236
2237         COPY_STRING_FIELD(user);
2238         COPY_NODE_FIELD(options);
2239
2240         return newnode;
2241 }
2242
2243 static AlterUserSetStmt *
2244 _copyAlterUserSetStmt(AlterUserSetStmt *from)
2245 {
2246         AlterUserSetStmt *newnode = makeNode(AlterUserSetStmt);
2247
2248         COPY_STRING_FIELD(user);
2249         COPY_STRING_FIELD(variable);
2250         COPY_NODE_FIELD(value);
2251
2252         return newnode;
2253 }
2254
2255 static DropUserStmt *
2256 _copyDropUserStmt(DropUserStmt *from)
2257 {
2258         DropUserStmt *newnode = makeNode(DropUserStmt);
2259
2260         COPY_NODE_FIELD(users);
2261
2262         return newnode;
2263 }
2264
2265 static LockStmt *
2266 _copyLockStmt(LockStmt *from)
2267 {
2268         LockStmt   *newnode = makeNode(LockStmt);
2269
2270         COPY_NODE_FIELD(relations);
2271         COPY_SCALAR_FIELD(mode);
2272
2273         return newnode;
2274 }
2275
2276 static ConstraintsSetStmt *
2277 _copyConstraintsSetStmt(ConstraintsSetStmt *from)
2278 {
2279         ConstraintsSetStmt *newnode = makeNode(ConstraintsSetStmt);
2280
2281         COPY_NODE_FIELD(constraints);
2282         COPY_SCALAR_FIELD(deferred);
2283
2284         return newnode;
2285 }
2286
2287 static CreateGroupStmt *
2288 _copyCreateGroupStmt(CreateGroupStmt *from)
2289 {
2290         CreateGroupStmt *newnode = makeNode(CreateGroupStmt);
2291
2292         COPY_STRING_FIELD(name);
2293         COPY_NODE_FIELD(options);
2294
2295         return newnode;
2296 }
2297
2298 static AlterGroupStmt *
2299 _copyAlterGroupStmt(AlterGroupStmt *from)
2300 {
2301         AlterGroupStmt *newnode = makeNode(AlterGroupStmt);
2302
2303         COPY_STRING_FIELD(name);
2304         COPY_SCALAR_FIELD(action);
2305         COPY_NODE_FIELD(listUsers);
2306
2307         return newnode;
2308 }
2309
2310 static DropGroupStmt *
2311 _copyDropGroupStmt(DropGroupStmt *from)
2312 {
2313         DropGroupStmt *newnode = makeNode(DropGroupStmt);
2314
2315         COPY_STRING_FIELD(name);
2316
2317         return newnode;
2318 }
2319
2320 static ReindexStmt *
2321 _copyReindexStmt(ReindexStmt *from)
2322 {
2323         ReindexStmt *newnode = makeNode(ReindexStmt);
2324
2325         COPY_SCALAR_FIELD(kind);
2326         COPY_NODE_FIELD(relation);
2327         COPY_STRING_FIELD(name);
2328         COPY_SCALAR_FIELD(force);
2329         COPY_SCALAR_FIELD(all);
2330
2331         return newnode;
2332 }
2333
2334 static CreateSchemaStmt *
2335 _copyCreateSchemaStmt(CreateSchemaStmt *from)
2336 {
2337         CreateSchemaStmt *newnode = makeNode(CreateSchemaStmt);
2338
2339         COPY_STRING_FIELD(schemaname);
2340         COPY_STRING_FIELD(authid);
2341         COPY_NODE_FIELD(schemaElts);
2342
2343         return newnode;
2344 }
2345
2346 static CreateConversionStmt *
2347 _copyCreateConversionStmt(CreateConversionStmt *from)
2348 {
2349         CreateConversionStmt *newnode = makeNode(CreateConversionStmt);
2350
2351         COPY_NODE_FIELD(conversion_name);
2352         COPY_STRING_FIELD(for_encoding_name);
2353         COPY_STRING_FIELD(to_encoding_name);
2354         COPY_NODE_FIELD(func_name);
2355         COPY_SCALAR_FIELD(def);
2356
2357         return newnode;
2358 }
2359
2360 static CreateCastStmt *
2361 _copyCreateCastStmt(CreateCastStmt *from)
2362 {
2363         CreateCastStmt *newnode = makeNode(CreateCastStmt);
2364
2365         COPY_NODE_FIELD(sourcetype);
2366         COPY_NODE_FIELD(targettype);
2367         COPY_NODE_FIELD(func);
2368         COPY_SCALAR_FIELD(context);
2369
2370         return newnode;
2371 }
2372
2373 static DropCastStmt *
2374 _copyDropCastStmt(DropCastStmt *from)
2375 {
2376         DropCastStmt *newnode = makeNode(DropCastStmt);
2377
2378         COPY_NODE_FIELD(sourcetype);
2379         COPY_NODE_FIELD(targettype);
2380         COPY_SCALAR_FIELD(behavior);
2381
2382         return newnode;
2383 }
2384
2385 static PrepareStmt *
2386 _copyPrepareStmt(PrepareStmt *from)
2387 {
2388         PrepareStmt *newnode = makeNode(PrepareStmt);
2389
2390         COPY_STRING_FIELD(name);
2391         COPY_NODE_FIELD(argtypes);
2392         COPY_OIDLIST_FIELD(argtype_oids);
2393         COPY_NODE_FIELD(query);
2394
2395         return newnode;
2396 }
2397
2398 static ExecuteStmt *
2399 _copyExecuteStmt(ExecuteStmt *from)
2400 {
2401         ExecuteStmt *newnode = makeNode(ExecuteStmt);
2402
2403         COPY_STRING_FIELD(name);
2404         COPY_NODE_FIELD(into);
2405         COPY_NODE_FIELD(params);
2406
2407         return newnode;
2408 }
2409
2410 static DeallocateStmt *
2411 _copyDeallocateStmt(DeallocateStmt *from)
2412 {
2413         DeallocateStmt *newnode = makeNode(DeallocateStmt);
2414
2415         COPY_STRING_FIELD(name);
2416
2417         return newnode;
2418 }
2419
2420
2421 /* ****************************************************************
2422  *                                      pg_list.h copy functions
2423  * ****************************************************************
2424  */
2425
2426 static Value *
2427 _copyValue(Value *from)
2428 {
2429         Value      *newnode = makeNode(Value);
2430
2431         /* See also _copyAConst when changing this code! */
2432
2433         COPY_SCALAR_FIELD(type);
2434         switch (from->type)
2435         {
2436                 case T_Integer:
2437                         COPY_SCALAR_FIELD(val.ival);
2438                         break;
2439                 case T_Float:
2440                 case T_String:
2441                 case T_BitString:
2442                         COPY_STRING_FIELD(val.str);
2443                         break;
2444                 case T_Null:
2445                         /* nothing to do */
2446                         break;
2447                 default:
2448                         elog(ERROR, "unrecognized node type: %d",
2449                                  (int) from->type);
2450                         break;
2451         }
2452         return newnode;
2453 }
2454
2455 /*
2456  * copyObject
2457  *
2458  * Create a copy of a Node tree or list.  This is a "deep" copy: all
2459  * substructure is copied too, recursively.
2460  */
2461 void *
2462 copyObject(void *from)
2463 {
2464         void       *retval;
2465
2466         if (from == NULL)
2467                 return NULL;
2468
2469         switch (nodeTag(from))
2470         {
2471                         /*
2472                          * PLAN NODES
2473                          */
2474                 case T_Plan:
2475                         retval = _copyPlan(from);
2476                         break;
2477                 case T_Result:
2478                         retval = _copyResult(from);
2479                         break;
2480                 case T_Append:
2481                         retval = _copyAppend(from);
2482                         break;
2483                 case T_Scan:
2484                         retval = _copyScan(from);
2485                         break;
2486                 case T_SeqScan:
2487                         retval = _copySeqScan(from);
2488                         break;
2489                 case T_IndexScan:
2490                         retval = _copyIndexScan(from);
2491                         break;
2492                 case T_TidScan:
2493                         retval = _copyTidScan(from);
2494                         break;
2495                 case T_SubqueryScan:
2496                         retval = _copySubqueryScan(from);
2497                         break;
2498                 case T_FunctionScan:
2499                         retval = _copyFunctionScan(from);
2500                         break;
2501                 case T_Join:
2502                         retval = _copyJoin(from);
2503                         break;
2504                 case T_NestLoop:
2505                         retval = _copyNestLoop(from);
2506                         break;
2507                 case T_MergeJoin:
2508                         retval = _copyMergeJoin(from);
2509                         break;
2510                 case T_HashJoin:
2511                         retval = _copyHashJoin(from);
2512                         break;
2513                 case T_Material:
2514                         retval = _copyMaterial(from);
2515                         break;
2516                 case T_Sort:
2517                         retval = _copySort(from);
2518                         break;
2519                 case T_Group:
2520                         retval = _copyGroup(from);
2521                         break;
2522                 case T_Agg:
2523                         retval = _copyAgg(from);
2524                         break;
2525                 case T_Unique:
2526                         retval = _copyUnique(from);
2527                         break;
2528                 case T_Hash:
2529                         retval = _copyHash(from);
2530                         break;
2531                 case T_SetOp:
2532                         retval = _copySetOp(from);
2533                         break;
2534                 case T_Limit:
2535                         retval = _copyLimit(from);
2536                         break;
2537
2538                         /*
2539                          * PRIMITIVE NODES
2540                          */
2541                 case T_Resdom:
2542                         retval = _copyResdom(from);
2543                         break;
2544                 case T_Alias:
2545                         retval = _copyAlias(from);
2546                         break;
2547                 case T_RangeVar:
2548                         retval = _copyRangeVar(from);
2549                         break;
2550                 case T_Var:
2551                         retval = _copyVar(from);
2552                         break;
2553                 case T_Const:
2554                         retval = _copyConst(from);
2555                         break;
2556                 case T_Param:
2557                         retval = _copyParam(from);
2558                         break;
2559                 case T_Aggref:
2560                         retval = _copyAggref(from);
2561                         break;
2562                 case T_ArrayRef:
2563                         retval = _copyArrayRef(from);
2564                         break;
2565                 case T_FuncExpr:
2566                         retval = _copyFuncExpr(from);
2567                         break;
2568                 case T_OpExpr:
2569                         retval = _copyOpExpr(from);
2570                         break;
2571                 case T_DistinctExpr:
2572                         retval = _copyDistinctExpr(from);
2573                         break;
2574                 case T_ScalarArrayOpExpr:
2575                         retval = _copyScalarArrayOpExpr(from);
2576                         break;
2577                 case T_BoolExpr:
2578                         retval = _copyBoolExpr(from);
2579                         break;
2580                 case T_SubLink:
2581                         retval = _copySubLink(from);
2582                         break;
2583                 case T_SubPlan:
2584                         retval = _copySubPlan(from);
2585                         break;
2586                 case T_FieldSelect:
2587                         retval = _copyFieldSelect(from);
2588                         break;
2589                 case T_RelabelType:
2590                         retval = _copyRelabelType(from);
2591                         break;
2592                 case T_CaseExpr:
2593                         retval = _copyCaseExpr(from);
2594                         break;
2595                 case T_CaseWhen:
2596                         retval = _copyCaseWhen(from);
2597                         break;
2598                 case T_ArrayExpr:
2599                         retval = _copyArrayExpr(from);
2600                         break;
2601                 case T_CoalesceExpr:
2602                         retval = _copyCoalesceExpr(from);
2603                         break;
2604                 case T_NullIfExpr:
2605                         retval = _copyNullIfExpr(from);
2606                         break;
2607                 case T_NullTest:
2608                         retval = _copyNullTest(from);
2609                         break;
2610                 case T_BooleanTest:
2611                         retval = _copyBooleanTest(from);
2612                         break;
2613                 case T_CoerceToDomain:
2614                         retval = _copyCoerceToDomain(from);
2615                         break;
2616                 case T_CoerceToDomainValue:
2617                         retval = _copyCoerceToDomainValue(from);
2618                         break;
2619                 case T_SetToDefault:
2620                         retval = _copySetToDefault(from);
2621                         break;
2622                 case T_TargetEntry:
2623                         retval = _copyTargetEntry(from);
2624                         break;
2625                 case T_RangeTblRef:
2626                         retval = _copyRangeTblRef(from);
2627                         break;
2628                 case T_JoinExpr:
2629                         retval = _copyJoinExpr(from);
2630                         break;
2631                 case T_FromExpr:
2632                         retval = _copyFromExpr(from);
2633                         break;
2634
2635                         /*
2636                          * RELATION NODES
2637                          */
2638                 case T_PathKeyItem:
2639                         retval = _copyPathKeyItem(from);
2640                         break;
2641                 case T_RestrictInfo:
2642                         retval = _copyRestrictInfo(from);
2643                         break;
2644                 case T_JoinInfo:
2645                         retval = _copyJoinInfo(from);
2646                         break;
2647                 case T_InClauseInfo:
2648                         retval = _copyInClauseInfo(from);
2649                         break;
2650
2651                         /*
2652                          * VALUE NODES
2653                          */
2654                 case T_Integer:
2655                 case T_Float:
2656                 case T_String:
2657                 case T_BitString:
2658                 case T_Null:
2659                         retval = _copyValue(from);
2660                         break;
2661                 case T_List:
2662                         {
2663                                 List       *list = from,
2664                                                    *oldl,
2665                                                    *newcell,
2666                                                    *prev;
2667
2668                                 /* rather ugly coding for speed... */
2669                                 /* Note the input list cannot be NIL if we got here. */
2670                                 newcell = makeNode(List);
2671                                 lfirst(newcell) = copyObject(lfirst(list));
2672
2673                                 retval = (void *) newcell;
2674                                 prev = newcell;
2675
2676                                 foreach(oldl, lnext(list))
2677                                 {
2678                                         newcell = makeNode(List);
2679                                         lfirst(newcell) = copyObject(lfirst(oldl));
2680                                         prev->next = newcell;
2681                                         prev = newcell;
2682                                 }
2683                                 prev->next = NIL;
2684                         }
2685                         break;
2686
2687                         /*
2688                          * PARSE NODES
2689                          */
2690                 case T_Query:
2691                         retval = _copyQuery(from);
2692                         break;
2693                 case T_InsertStmt:
2694                         retval = _copyInsertStmt(from);
2695                         break;
2696                 case T_DeleteStmt:
2697                         retval = _copyDeleteStmt(from);
2698                         break;
2699                 case T_UpdateStmt:
2700                         retval = _copyUpdateStmt(from);
2701                         break;
2702                 case T_SelectStmt:
2703                         retval = _copySelectStmt(from);
2704                         break;
2705                 case T_SetOperationStmt:
2706                         retval = _copySetOperationStmt(from);
2707                         break;
2708                 case T_AlterTableStmt:
2709                         retval = _copyAlterTableStmt(from);
2710                         break;
2711                 case T_AlterDomainStmt:
2712                         retval = _copyAlterDomainStmt(from);
2713                         break;
2714                 case T_GrantStmt:
2715                         retval = _copyGrantStmt(from);
2716                         break;
2717                 case T_DeclareCursorStmt:
2718                         retval = _copyDeclareCursorStmt(from);
2719                         break;
2720                 case T_ClosePortalStmt:
2721                         retval = _copyClosePortalStmt(from);
2722                         break;
2723                 case T_ClusterStmt:
2724                         retval = _copyClusterStmt(from);
2725                         break;
2726                 case T_CopyStmt:
2727                         retval = _copyCopyStmt(from);
2728                         break;
2729                 case T_CreateStmt:
2730                         retval = _copyCreateStmt(from);
2731                         break;
2732                 case T_InhRelation:
2733                         retval = _copyInhRelation(from);
2734                         break;
2735                 case T_DefineStmt:
2736                         retval = _copyDefineStmt(from);
2737                         break;
2738                 case T_DropStmt:
2739                         retval = _copyDropStmt(from);
2740                         break;
2741                 case T_TruncateStmt:
2742                         retval = _copyTruncateStmt(from);
2743                         break;
2744                 case T_CommentStmt:
2745                         retval = _copyCommentStmt(from);
2746                         break;
2747                 case T_FetchStmt:
2748                         retval = _copyFetchStmt(from);
2749                         break;
2750                 case T_IndexStmt:
2751                         retval = _copyIndexStmt(from);
2752                         break;
2753                 case T_CreateFunctionStmt:
2754                         retval = _copyCreateFunctionStmt(from);
2755                         break;
2756                 case T_RemoveAggrStmt:
2757                         retval = _copyRemoveAggrStmt(from);
2758                         break;
2759                 case T_RemoveFuncStmt:
2760                         retval = _copyRemoveFuncStmt(from);
2761                         break;
2762                 case T_RemoveOperStmt:
2763                         retval = _copyRemoveOperStmt(from);
2764                         break;
2765                 case T_RemoveOpClassStmt:
2766                         retval = _copyRemoveOpClassStmt(from);
2767                         break;
2768                 case T_RenameStmt:
2769                         retval = _copyRenameStmt(from);
2770                         break;
2771                 case T_RuleStmt:
2772                         retval = _copyRuleStmt(from);
2773                         break;
2774                 case T_NotifyStmt:
2775                         retval = _copyNotifyStmt(from);
2776                         break;
2777                 case T_ListenStmt:
2778                         retval = _copyListenStmt(from);
2779                         break;
2780                 case T_UnlistenStmt:
2781                         retval = _copyUnlistenStmt(from);
2782                         break;
2783                 case T_TransactionStmt:
2784                         retval = _copyTransactionStmt(from);
2785                         break;
2786                 case T_CompositeTypeStmt:
2787                         retval = _copyCompositeTypeStmt(from);
2788                         break;
2789                 case T_ViewStmt:
2790                         retval = _copyViewStmt(from);
2791                         break;
2792                 case T_LoadStmt:
2793                         retval = _copyLoadStmt(from);
2794                         break;
2795                 case T_CreateDomainStmt:
2796                         retval = _copyCreateDomainStmt(from);
2797                         break;
2798                 case T_CreateOpClassStmt:
2799                         retval = _copyCreateOpClassStmt(from);
2800                         break;
2801                 case T_CreateOpClassItem:
2802                         retval = _copyCreateOpClassItem(from);
2803                         break;
2804                 case T_CreatedbStmt:
2805                         retval = _copyCreatedbStmt(from);
2806                         break;
2807                 case T_AlterDatabaseSetStmt:
2808                         retval = _copyAlterDatabaseSetStmt(from);
2809                         break;
2810                 case T_DropdbStmt:
2811                         retval = _copyDropdbStmt(from);
2812                         break;
2813                 case T_VacuumStmt:
2814                         retval = _copyVacuumStmt(from);
2815                         break;
2816                 case T_ExplainStmt:
2817                         retval = _copyExplainStmt(from);
2818                         break;
2819                 case T_CreateSeqStmt:
2820                         retval = _copyCreateSeqStmt(from);
2821                         break;
2822                 case T_AlterSeqStmt:
2823                         retval = _copyAlterSeqStmt(from);
2824                         break;
2825                 case T_VariableSetStmt:
2826                         retval = _copyVariableSetStmt(from);
2827                         break;
2828                 case T_VariableShowStmt:
2829                         retval = _copyVariableShowStmt(from);
2830                         break;
2831                 case T_VariableResetStmt:
2832                         retval = _copyVariableResetStmt(from);
2833                         break;
2834                 case T_CreateTrigStmt:
2835                         retval = _copyCreateTrigStmt(from);
2836                         break;
2837                 case T_DropPropertyStmt:
2838                         retval = _copyDropPropertyStmt(from);
2839                         break;
2840                 case T_CreatePLangStmt:
2841                         retval = _copyCreatePLangStmt(from);
2842                         break;
2843                 case T_DropPLangStmt:
2844                         retval = _copyDropPLangStmt(from);
2845                         break;
2846                 case T_CreateUserStmt:
2847                         retval = _copyCreateUserStmt(from);
2848                         break;
2849                 case T_AlterUserStmt:
2850                         retval = _copyAlterUserStmt(from);
2851                         break;
2852                 case T_AlterUserSetStmt:
2853                         retval = _copyAlterUserSetStmt(from);
2854                         break;
2855                 case T_DropUserStmt:
2856                         retval = _copyDropUserStmt(from);
2857                         break;
2858                 case T_LockStmt:
2859                         retval = _copyLockStmt(from);
2860                         break;
2861                 case T_ConstraintsSetStmt:
2862                         retval = _copyConstraintsSetStmt(from);
2863                         break;
2864                 case T_CreateGroupStmt:
2865                         retval = _copyCreateGroupStmt(from);
2866                         break;
2867                 case T_AlterGroupStmt:
2868                         retval = _copyAlterGroupStmt(from);
2869                         break;
2870                 case T_DropGroupStmt:
2871                         retval = _copyDropGroupStmt(from);
2872                         break;
2873                 case T_ReindexStmt:
2874                         retval = _copyReindexStmt(from);
2875                         break;
2876                 case T_CheckPointStmt:
2877                         retval = (void *) makeNode(CheckPointStmt);
2878                         break;
2879                 case T_CreateSchemaStmt:
2880                         retval = _copyCreateSchemaStmt(from);
2881                         break;
2882                 case T_CreateConversionStmt:
2883                         retval = _copyCreateConversionStmt(from);
2884                         break;
2885                 case T_CreateCastStmt:
2886                         retval = _copyCreateCastStmt(from);
2887                         break;
2888                 case T_DropCastStmt:
2889                         retval = _copyDropCastStmt(from);
2890                         break;
2891                 case T_PrepareStmt:
2892                         retval = _copyPrepareStmt(from);
2893                         break;
2894                 case T_ExecuteStmt:
2895                         retval = _copyExecuteStmt(from);
2896                         break;
2897                 case T_DeallocateStmt:
2898                         retval = _copyDeallocateStmt(from);
2899                         break;
2900
2901                 case T_A_Expr:
2902                         retval = _copyAExpr(from);
2903                         break;
2904                 case T_ColumnRef:
2905                         retval = _copyColumnRef(from);
2906                         break;
2907                 case T_ParamRef:
2908                         retval = _copyParamRef(from);
2909                         break;
2910                 case T_A_Const:
2911                         retval = _copyAConst(from);
2912                         break;
2913                 case T_FuncCall:
2914                         retval = _copyFuncCall(from);
2915                         break;
2916                 case T_A_Indices:
2917                         retval = _copyAIndices(from);
2918                         break;
2919                 case T_ExprFieldSelect:
2920                         retval = _copyExprFieldSelect(from);
2921                         break;
2922                 case T_ResTarget:
2923                         retval = _copyResTarget(from);
2924                         break;
2925                 case T_TypeCast:
2926                         retval = _copyTypeCast(from);
2927                         break;
2928                 case T_SortBy:
2929                         retval = _copySortBy(from);
2930                         break;
2931                 case T_RangeSubselect:
2932                         retval = _copyRangeSubselect(from);
2933                         break;
2934                 case T_RangeFunction:
2935                         retval = _copyRangeFunction(from);
2936                         break;
2937                 case T_TypeName:
2938                         retval = _copyTypeName(from);
2939                         break;
2940                 case T_IndexElem:
2941                         retval = _copyIndexElem(from);
2942                         break;
2943                 case T_ColumnDef:
2944                         retval = _copyColumnDef(from);
2945                         break;
2946                 case T_Constraint:
2947                         retval = _copyConstraint(from);
2948                         break;
2949                 case T_DefElem:
2950                         retval = _copyDefElem(from);
2951                         break;
2952                 case T_RangeTblEntry:
2953                         retval = _copyRangeTblEntry(from);
2954                         break;
2955                 case T_SortClause:
2956                         retval = _copySortClause(from);
2957                         break;
2958                 case T_GroupClause:
2959                         retval = _copyGroupClause(from);
2960                         break;
2961                 case T_FkConstraint:
2962                         retval = _copyFkConstraint(from);
2963                         break;
2964                 case T_PrivGrantee:
2965                         retval = _copyPrivGrantee(from);
2966                         break;
2967                 case T_FuncWithArgs:
2968                         retval = _copyFuncWithArgs(from);
2969                         break;
2970
2971                 default:
2972                         elog(ERROR, "unrecognized node type: %d", (int) nodeTag(from));
2973                         retval = from;          /* keep compiler quiet */
2974                         break;
2975         }
2976
2977         return retval;
2978 }