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