OSDN Git Service

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