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