OSDN Git Service

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