OSDN Git Service

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