OSDN Git Service

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