OSDN Git Service

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