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