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