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