OSDN Git Service

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