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