OSDN Git Service

pgindent run. Make it all clean.
[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-2001, PostgreSQL Global Development Group
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.139 2001/03/22 03:59:31 momjian 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 = makeList1(lfirst(list));
62
63         foreach(l, lnext(list))
64         {
65                 lnext(nl) = makeList1(lfirst(l));
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
1428         /*
1429          * Do not copy pathkeys, since they'd not be canonical in a copied
1430          * query
1431          */
1432         newnode->left_pathkey = NIL;
1433         newnode->right_pathkey = NIL;
1434         newnode->hashjoinoperator = from->hashjoinoperator;
1435         newnode->left_dispersion = from->left_dispersion;
1436         newnode->right_dispersion = from->right_dispersion;
1437
1438         return newnode;
1439 }
1440
1441 /* ----------------
1442  *              _copyJoinInfo
1443  * ----------------
1444  */
1445 static JoinInfo *
1446 _copyJoinInfo(JoinInfo *from)
1447 {
1448         JoinInfo   *newnode = makeNode(JoinInfo);
1449
1450         /* ----------------
1451          *      copy remainder of node
1452          * ----------------
1453          */
1454         newnode->unjoined_relids = listCopy(from->unjoined_relids);
1455         Node_Copy(from, newnode, jinfo_restrictinfo);
1456
1457         return newnode;
1458 }
1459
1460 static Iter *
1461 _copyIter(Iter *from)
1462 {
1463         Iter       *newnode = makeNode(Iter);
1464
1465         Node_Copy(from, newnode, iterexpr);
1466         newnode->itertype = from->itertype;
1467
1468         return newnode;
1469 }
1470
1471 static Stream *
1472 _copyStream(Stream *from)
1473 {
1474         Stream     *newnode = makeNode(Stream);
1475
1476         newnode->pathptr = from->pathptr;
1477         newnode->cinfo = from->cinfo;
1478         newnode->clausetype = from->clausetype;
1479
1480         newnode->upstream = (StreamPtr) NULL;           /* only copy nodes
1481                                                                                                  * downwards! */
1482         Node_Copy(from, newnode, downstream);
1483         if (newnode->downstream)
1484                 ((Stream *) newnode->downstream)->upstream = (Stream *) newnode;
1485
1486         newnode->groupup = from->groupup;
1487         newnode->groupcost = from->groupcost;
1488         newnode->groupsel = from->groupsel;
1489
1490         return newnode;
1491 }
1492
1493 /* ****************************************************************
1494  *                                      parsenodes.h copy functions
1495  * ****************************************************************
1496  */
1497
1498 static TargetEntry *
1499 _copyTargetEntry(TargetEntry *from)
1500 {
1501         TargetEntry *newnode = makeNode(TargetEntry);
1502
1503         Node_Copy(from, newnode, resdom);
1504         Node_Copy(from, newnode, fjoin);
1505         Node_Copy(from, newnode, expr);
1506         return newnode;
1507 }
1508
1509 static RangeTblEntry *
1510 _copyRangeTblEntry(RangeTblEntry *from)
1511 {
1512         RangeTblEntry *newnode = makeNode(RangeTblEntry);
1513
1514         if (from->relname)
1515                 newnode->relname = pstrdup(from->relname);
1516         newnode->relid = from->relid;
1517         Node_Copy(from, newnode, subquery);
1518         Node_Copy(from, newnode, alias);
1519         Node_Copy(from, newnode, eref);
1520         newnode->inh = from->inh;
1521         newnode->inFromCl = from->inFromCl;
1522         newnode->checkForRead = from->checkForRead;
1523         newnode->checkForWrite = from->checkForWrite;
1524         newnode->checkAsUser = from->checkAsUser;
1525
1526         return newnode;
1527 }
1528
1529 static FkConstraint *
1530 _copyFkConstraint(FkConstraint *from)
1531 {
1532         FkConstraint *newnode = makeNode(FkConstraint);
1533
1534         if (from->constr_name)
1535                 newnode->constr_name = pstrdup(from->constr_name);
1536         if (from->pktable_name)
1537                 newnode->pktable_name = pstrdup(from->pktable_name);
1538         Node_Copy(from, newnode, fk_attrs);
1539         Node_Copy(from, newnode, pk_attrs);
1540         if (from->match_type)
1541                 newnode->match_type = pstrdup(from->match_type);
1542         newnode->actions = from->actions;
1543         newnode->deferrable = from->deferrable;
1544         newnode->initdeferred = from->initdeferred;
1545
1546         return newnode;
1547 }
1548
1549 static SortClause *
1550 _copySortClause(SortClause *from)
1551 {
1552         SortClause *newnode = makeNode(SortClause);
1553
1554         newnode->tleSortGroupRef = from->tleSortGroupRef;
1555         newnode->sortop = from->sortop;
1556
1557         return newnode;
1558 }
1559
1560 static A_Expr *
1561 _copyAExpr(A_Expr *from)
1562 {
1563         A_Expr     *newnode = makeNode(A_Expr);
1564
1565         newnode->oper = from->oper;
1566         if (from->opname)
1567                 newnode->opname = pstrdup(from->opname);
1568         Node_Copy(from, newnode, lexpr);
1569         Node_Copy(from, newnode, rexpr);
1570
1571         return newnode;
1572 }
1573
1574 static A_Const *
1575 _copyAConst(A_Const *from)
1576 {
1577         A_Const    *newnode = makeNode(A_Const);
1578
1579         newnode->val = *((Value *) (copyObject(&(from->val))));
1580         Node_Copy(from, newnode, typename);
1581
1582         return newnode;
1583 }
1584
1585 static ParamNo *
1586 _copyParamNo(ParamNo *from)
1587 {
1588         ParamNo    *newnode = makeNode(ParamNo);
1589
1590         newnode->number = from->number;
1591         Node_Copy(from, newnode, typename);
1592         Node_Copy(from, newnode, indirection);
1593
1594         return newnode;
1595 }
1596
1597 static Ident *
1598 _copyIdent(Ident *from)
1599 {
1600         Ident      *newnode = makeNode(Ident);
1601
1602         if (from->name)
1603                 newnode->name = pstrdup(from->name);
1604         Node_Copy(from, newnode, indirection);
1605         newnode->isRel = from->isRel;
1606
1607         return newnode;
1608 }
1609
1610 static FuncCall *
1611 _copyFuncCall(FuncCall *from)
1612 {
1613         FuncCall   *newnode = makeNode(FuncCall);
1614
1615         if (from->funcname)
1616                 newnode->funcname = pstrdup(from->funcname);
1617         Node_Copy(from, newnode, args);
1618         newnode->agg_star = from->agg_star;
1619         newnode->agg_distinct = from->agg_distinct;
1620
1621         return newnode;
1622 }
1623
1624 static A_Indices *
1625 _copyAIndices(A_Indices *from)
1626 {
1627         A_Indices  *newnode = makeNode(A_Indices);
1628
1629         Node_Copy(from, newnode, lidx);
1630         Node_Copy(from, newnode, uidx);
1631
1632         return newnode;
1633 }
1634
1635 static ResTarget *
1636 _copyResTarget(ResTarget *from)
1637 {
1638         ResTarget  *newnode = makeNode(ResTarget);
1639
1640         if (from->name)
1641                 newnode->name = pstrdup(from->name);
1642         Node_Copy(from, newnode, indirection);
1643         Node_Copy(from, newnode, val);
1644
1645         return newnode;
1646 }
1647
1648 static TypeName *
1649 _copyTypeName(TypeName *from)
1650 {
1651         TypeName   *newnode = makeNode(TypeName);
1652
1653         if (from->name)
1654                 newnode->name = pstrdup(from->name);
1655         newnode->timezone = from->timezone;
1656         newnode->setof = from->setof;
1657         newnode->typmod = from->typmod;
1658         Node_Copy(from, newnode, arrayBounds);
1659
1660         return newnode;
1661 }
1662
1663 static SortGroupBy *
1664 _copySortGroupBy(SortGroupBy *from)
1665 {
1666         SortGroupBy *newnode = makeNode(SortGroupBy);
1667
1668         if (from->useOp)
1669                 newnode->useOp = pstrdup(from->useOp);
1670         Node_Copy(from, newnode, node);
1671
1672         return newnode;
1673 }
1674
1675 static RangeVar *
1676 _copyRangeVar(RangeVar *from)
1677 {
1678         RangeVar   *newnode = makeNode(RangeVar);
1679
1680         if (from->relname)
1681                 newnode->relname = pstrdup(from->relname);
1682         newnode->inhOpt = from->inhOpt;
1683         Node_Copy(from, newnode, name);
1684
1685         return newnode;
1686 }
1687
1688 static RangeSubselect *
1689 _copyRangeSubselect(RangeSubselect *from)
1690 {
1691         RangeSubselect *newnode = makeNode(RangeSubselect);
1692
1693         Node_Copy(from, newnode, subquery);
1694         Node_Copy(from, newnode, name);
1695
1696         return newnode;
1697 }
1698
1699 static TypeCast *
1700 _copyTypeCast(TypeCast *from)
1701 {
1702         TypeCast   *newnode = makeNode(TypeCast);
1703
1704         Node_Copy(from, newnode, arg);
1705         Node_Copy(from, newnode, typename);
1706
1707         return newnode;
1708 }
1709
1710 static IndexElem *
1711 _copyIndexElem(IndexElem *from)
1712 {
1713         IndexElem  *newnode = makeNode(IndexElem);
1714
1715         if (from->name)
1716                 newnode->name = pstrdup(from->name);
1717         Node_Copy(from, newnode, args);
1718         if (from->class)
1719                 newnode->class = pstrdup(from->class);
1720
1721         return newnode;
1722 }
1723
1724 static ColumnDef *
1725 _copyColumnDef(ColumnDef *from)
1726 {
1727         ColumnDef  *newnode = makeNode(ColumnDef);
1728
1729         if (from->colname)
1730                 newnode->colname = pstrdup(from->colname);
1731         Node_Copy(from, newnode, typename);
1732         newnode->is_not_null = from->is_not_null;
1733         newnode->is_sequence = from->is_sequence;
1734         Node_Copy(from, newnode, raw_default);
1735         if (from->cooked_default)
1736                 newnode->cooked_default = pstrdup(from->cooked_default);
1737         Node_Copy(from, newnode, constraints);
1738
1739         return newnode;
1740 }
1741
1742 static Constraint *
1743 _copyConstraint(Constraint *from)
1744 {
1745         Constraint *newnode = makeNode(Constraint);
1746
1747         newnode->contype = from->contype;
1748         if (from->name)
1749                 newnode->name = pstrdup(from->name);
1750         Node_Copy(from, newnode, raw_expr);
1751         if (from->cooked_expr)
1752                 newnode->cooked_expr = pstrdup(from->cooked_expr);
1753         Node_Copy(from, newnode, keys);
1754
1755         return newnode;
1756 }
1757
1758 static DefElem *
1759 _copyDefElem(DefElem *from)
1760 {
1761         DefElem    *newnode = makeNode(DefElem);
1762
1763         if (from->defname)
1764                 newnode->defname = pstrdup(from->defname);
1765         Node_Copy(from, newnode, arg);
1766
1767         return newnode;
1768 }
1769
1770 static Query *
1771 _copyQuery(Query *from)
1772 {
1773         Query      *newnode = makeNode(Query);
1774
1775         newnode->commandType = from->commandType;
1776         Node_Copy(from, newnode, utilityStmt);
1777         newnode->resultRelation = from->resultRelation;
1778         if (from->into)
1779                 newnode->into = pstrdup(from->into);
1780         newnode->isPortal = from->isPortal;
1781         newnode->isBinary = from->isBinary;
1782         newnode->isTemp = from->isTemp;
1783         newnode->hasAggs = from->hasAggs;
1784         newnode->hasSubLinks = from->hasSubLinks;
1785
1786         Node_Copy(from, newnode, rtable);
1787         Node_Copy(from, newnode, jointree);
1788
1789         newnode->rowMarks = listCopy(from->rowMarks);
1790
1791         Node_Copy(from, newnode, targetList);
1792
1793         Node_Copy(from, newnode, groupClause);
1794         Node_Copy(from, newnode, havingQual);
1795         Node_Copy(from, newnode, distinctClause);
1796         Node_Copy(from, newnode, sortClause);
1797
1798         Node_Copy(from, newnode, limitOffset);
1799         Node_Copy(from, newnode, limitCount);
1800
1801         Node_Copy(from, newnode, setOperations);
1802
1803         newnode->resultRelations = listCopy(from->resultRelations);
1804
1805         /*
1806          * We do not copy the planner internal fields: base_rel_list,
1807          * join_rel_list, equi_key_list, query_pathkeys. Not entirely clear if
1808          * this is right?
1809          */
1810
1811         return newnode;
1812 }
1813
1814 static InsertStmt *
1815 _copyInsertStmt(InsertStmt *from)
1816 {
1817         InsertStmt *newnode = makeNode(InsertStmt);
1818
1819         if (from->relname)
1820                 newnode->relname = pstrdup(from->relname);
1821         Node_Copy(from, newnode, cols);
1822         Node_Copy(from, newnode, targetList);
1823         Node_Copy(from, newnode, selectStmt);
1824
1825         return newnode;
1826 }
1827
1828 static DeleteStmt *
1829 _copyDeleteStmt(DeleteStmt *from)
1830 {
1831         DeleteStmt *newnode = makeNode(DeleteStmt);
1832
1833         if (from->relname)
1834                 newnode->relname = pstrdup(from->relname);
1835         Node_Copy(from, newnode, whereClause);
1836         newnode->inhOpt = from->inhOpt;
1837
1838         return newnode;
1839 }
1840
1841 static UpdateStmt *
1842 _copyUpdateStmt(UpdateStmt *from)
1843 {
1844         UpdateStmt *newnode = makeNode(UpdateStmt);
1845
1846         if (from->relname)
1847                 newnode->relname = pstrdup(from->relname);
1848         Node_Copy(from, newnode, targetList);
1849         Node_Copy(from, newnode, whereClause);
1850         Node_Copy(from, newnode, fromClause);
1851         newnode->inhOpt = from->inhOpt;
1852
1853         return newnode;
1854 }
1855
1856 static SelectStmt *
1857 _copySelectStmt(SelectStmt *from)
1858 {
1859         SelectStmt *newnode = makeNode(SelectStmt);
1860
1861         Node_Copy(from, newnode, distinctClause);
1862         if (from->into)
1863                 newnode->into = pstrdup(from->into);
1864         newnode->istemp = from->istemp;
1865         Node_Copy(from, newnode, targetList);
1866         Node_Copy(from, newnode, fromClause);
1867         Node_Copy(from, newnode, whereClause);
1868         Node_Copy(from, newnode, groupClause);
1869         Node_Copy(from, newnode, havingClause);
1870         Node_Copy(from, newnode, sortClause);
1871         if (from->portalname)
1872                 newnode->portalname = pstrdup(from->portalname);
1873         newnode->binary = from->binary;
1874         Node_Copy(from, newnode, limitOffset);
1875         Node_Copy(from, newnode, limitCount);
1876         Node_Copy(from, newnode, forUpdate);
1877         newnode->op = from->op;
1878         newnode->all = from->all;
1879         Node_Copy(from, newnode, larg);
1880         Node_Copy(from, newnode, rarg);
1881
1882         return newnode;
1883 }
1884
1885 static SetOperationStmt *
1886 _copySetOperationStmt(SetOperationStmt *from)
1887 {
1888         SetOperationStmt *newnode = makeNode(SetOperationStmt);
1889
1890         newnode->op = from->op;
1891         newnode->all = from->all;
1892         Node_Copy(from, newnode, larg);
1893         Node_Copy(from, newnode, rarg);
1894         newnode->colTypes = listCopy(from->colTypes);
1895
1896         return newnode;
1897 }
1898
1899 static AlterTableStmt *
1900 _copyAlterTableStmt(AlterTableStmt *from)
1901 {
1902         AlterTableStmt *newnode = makeNode(AlterTableStmt);
1903
1904         newnode->subtype = from->subtype;
1905         if (from->relname)
1906                 newnode->relname = pstrdup(from->relname);
1907         newnode->inhOpt = from->inhOpt;
1908         if (from->name)
1909                 newnode->name = pstrdup(from->name);
1910         Node_Copy(from, newnode, def);
1911         newnode->behavior = from->behavior;
1912
1913         return newnode;
1914 }
1915
1916 static ChangeACLStmt *
1917 _copyChangeACLStmt(ChangeACLStmt *from)
1918 {
1919         ChangeACLStmt *newnode = makeNode(ChangeACLStmt);
1920
1921         Node_Copy(from, newnode, relNames);
1922         if (from->aclString)
1923                 newnode->aclString = pstrdup(from->aclString);
1924
1925         return newnode;
1926 }
1927
1928 static ClosePortalStmt *
1929 _copyClosePortalStmt(ClosePortalStmt *from)
1930 {
1931         ClosePortalStmt *newnode = makeNode(ClosePortalStmt);
1932
1933         if (from->portalname)
1934                 newnode->portalname = pstrdup(from->portalname);
1935
1936         return newnode;
1937 }
1938
1939 static ClusterStmt *
1940 _copyClusterStmt(ClusterStmt *from)
1941 {
1942         ClusterStmt *newnode = makeNode(ClusterStmt);
1943
1944         if (from->relname)
1945                 newnode->relname = pstrdup(from->relname);
1946         if (from->indexname)
1947                 newnode->indexname = pstrdup(from->indexname);
1948
1949         return newnode;
1950 }
1951
1952 static CopyStmt *
1953 _copyCopyStmt(CopyStmt *from)
1954 {
1955         CopyStmt   *newnode = makeNode(CopyStmt);
1956
1957         newnode->binary = from->binary;
1958         if (from->relname)
1959                 newnode->relname = pstrdup(from->relname);
1960         newnode->oids = from->oids;
1961         newnode->direction = from->direction;
1962         if (from->filename)
1963                 newnode->filename = pstrdup(from->filename);
1964         if (from->delimiter)
1965                 newnode->delimiter = pstrdup(from->delimiter);
1966         if (from->null_print)
1967                 newnode->null_print = pstrdup(from->null_print);
1968
1969         return newnode;
1970 }
1971
1972 static CreateStmt *
1973 _copyCreateStmt(CreateStmt *from)
1974 {
1975         CreateStmt *newnode = makeNode(CreateStmt);
1976
1977         newnode->istemp = from->istemp;
1978         newnode->relname = pstrdup(from->relname);
1979         Node_Copy(from, newnode, tableElts);
1980         Node_Copy(from, newnode, inhRelnames);
1981         Node_Copy(from, newnode, constraints);
1982
1983         return newnode;
1984 }
1985
1986 static VersionStmt *
1987 _copyVersionStmt(VersionStmt *from)
1988 {
1989         VersionStmt *newnode = makeNode(VersionStmt);
1990
1991         newnode->relname = pstrdup(from->relname);
1992         newnode->direction = from->direction;
1993         newnode->fromRelname = pstrdup(from->fromRelname);
1994         newnode->date = pstrdup(from->date);
1995
1996         return newnode;
1997 }
1998
1999 static DefineStmt *
2000 _copyDefineStmt(DefineStmt *from)
2001 {
2002         DefineStmt *newnode = makeNode(DefineStmt);
2003
2004         newnode->defType = from->defType;
2005         newnode->defname = pstrdup(from->defname);
2006         Node_Copy(from, newnode, definition);
2007
2008         return newnode;
2009 }
2010
2011 static DropStmt *
2012 _copyDropStmt(DropStmt *from)
2013 {
2014         DropStmt   *newnode = makeNode(DropStmt);
2015
2016         Node_Copy(from, newnode, names);
2017         newnode->removeType = from->removeType;
2018
2019         return newnode;
2020 }
2021
2022 static TruncateStmt *
2023 _copyTruncateStmt(TruncateStmt *from)
2024 {
2025         TruncateStmt *newnode = makeNode(TruncateStmt);
2026
2027         newnode->relName = pstrdup(from->relName);
2028
2029         return newnode;
2030 }
2031
2032 static CommentStmt *
2033 _copyCommentStmt(CommentStmt *from)
2034 {
2035         CommentStmt *newnode = makeNode(CommentStmt);
2036
2037         newnode->objtype = from->objtype;
2038         newnode->objname = pstrdup(from->objname);
2039         if (from->objproperty)
2040                 newnode->objproperty = pstrdup(from->objproperty);
2041         Node_Copy(from, newnode, objlist);
2042         newnode->comment = pstrdup(from->comment);
2043
2044         return newnode;
2045 }
2046
2047 static ExtendStmt *
2048 _copyExtendStmt(ExtendStmt *from)
2049 {
2050         ExtendStmt *newnode = makeNode(ExtendStmt);
2051
2052         newnode->idxname = pstrdup(from->idxname);
2053         Node_Copy(from, newnode, whereClause);
2054         Node_Copy(from, newnode, rangetable);
2055
2056         return newnode;
2057 }
2058
2059 static FetchStmt *
2060 _copyFetchStmt(FetchStmt *from)
2061 {
2062         FetchStmt  *newnode = makeNode(FetchStmt);
2063
2064         newnode->direction = from->direction;
2065         newnode->howMany = from->howMany;
2066         newnode->portalname = pstrdup(from->portalname);
2067         newnode->ismove = from->ismove;
2068
2069         return newnode;
2070 }
2071
2072 static IndexStmt *
2073 _copyIndexStmt(IndexStmt *from)
2074 {
2075         IndexStmt  *newnode = makeNode(IndexStmt);
2076
2077         newnode->idxname = pstrdup(from->idxname);
2078         newnode->relname = pstrdup(from->relname);
2079         newnode->accessMethod = pstrdup(from->accessMethod);
2080         Node_Copy(from, newnode, indexParams);
2081         Node_Copy(from, newnode, withClause);
2082         Node_Copy(from, newnode, whereClause);
2083         Node_Copy(from, newnode, rangetable);
2084         newnode->unique = from->unique;
2085         newnode->primary = from->primary;
2086
2087         return newnode;
2088 }
2089
2090 static ProcedureStmt *
2091 _copyProcedureStmt(ProcedureStmt *from)
2092 {
2093         ProcedureStmt *newnode = makeNode(ProcedureStmt);
2094
2095         newnode->funcname = pstrdup(from->funcname);
2096         Node_Copy(from, newnode, argTypes);
2097         Node_Copy(from, newnode, returnType);
2098         Node_Copy(from, newnode, withClause);
2099         Node_Copy(from, newnode, as);
2100         newnode->language = pstrdup(from->language);
2101
2102         return newnode;
2103 }
2104
2105 static RemoveAggrStmt *
2106 _copyRemoveAggrStmt(RemoveAggrStmt *from)
2107 {
2108         RemoveAggrStmt *newnode = makeNode(RemoveAggrStmt);
2109
2110         newnode->aggname = pstrdup(from->aggname);
2111         Node_Copy(from, newnode, aggtype);
2112
2113         return newnode;
2114 }
2115
2116 static RemoveFuncStmt *
2117 _copyRemoveFuncStmt(RemoveFuncStmt *from)
2118 {
2119         RemoveFuncStmt *newnode = makeNode(RemoveFuncStmt);
2120
2121         newnode->funcname = pstrdup(from->funcname);
2122         Node_Copy(from, newnode, args);
2123
2124         return newnode;
2125 }
2126
2127 static RemoveOperStmt *
2128 _copyRemoveOperStmt(RemoveOperStmt *from)
2129 {
2130         RemoveOperStmt *newnode = makeNode(RemoveOperStmt);
2131
2132         newnode->opname = pstrdup(from->opname);
2133         Node_Copy(from, newnode, args);
2134
2135         return newnode;
2136 }
2137
2138 static RenameStmt *
2139 _copyRenameStmt(RenameStmt *from)
2140 {
2141         RenameStmt *newnode = makeNode(RenameStmt);
2142
2143         newnode->relname = pstrdup(from->relname);
2144         newnode->inhOpt = from->inhOpt;
2145         if (from->column)
2146                 newnode->column = pstrdup(from->column);
2147         if (from->newname)
2148                 newnode->newname = pstrdup(from->newname);
2149
2150         return newnode;
2151 }
2152
2153 static RuleStmt *
2154 _copyRuleStmt(RuleStmt *from)
2155 {
2156         RuleStmt   *newnode = makeNode(RuleStmt);
2157
2158         newnode->rulename = pstrdup(from->rulename);
2159         Node_Copy(from, newnode, whereClause);
2160         newnode->event = from->event;
2161         Node_Copy(from, newnode, object);
2162         newnode->instead = from->instead;
2163         Node_Copy(from, newnode, actions);
2164
2165         return newnode;
2166 }
2167
2168 static NotifyStmt *
2169 _copyNotifyStmt(NotifyStmt *from)
2170 {
2171         NotifyStmt *newnode = makeNode(NotifyStmt);
2172
2173         if (from->relname)
2174                 newnode->relname = pstrdup(from->relname);
2175
2176         return newnode;
2177 }
2178
2179 static ListenStmt *
2180 _copyListenStmt(ListenStmt *from)
2181 {
2182         ListenStmt *newnode = makeNode(ListenStmt);
2183
2184         if (from->relname)
2185                 newnode->relname = pstrdup(from->relname);
2186
2187         return newnode;
2188 }
2189
2190 static UnlistenStmt *
2191 _copyUnlistenStmt(UnlistenStmt *from)
2192 {
2193         UnlistenStmt *newnode = makeNode(UnlistenStmt);
2194
2195         if (from->relname)
2196                 newnode->relname = pstrdup(from->relname);
2197
2198         return newnode;
2199 }
2200
2201 static TransactionStmt *
2202 _copyTransactionStmt(TransactionStmt *from)
2203 {
2204         TransactionStmt *newnode = makeNode(TransactionStmt);
2205
2206         newnode->command = from->command;
2207
2208         return newnode;
2209 }
2210
2211 static ViewStmt *
2212 _copyViewStmt(ViewStmt *from)
2213 {
2214         ViewStmt   *newnode = makeNode(ViewStmt);
2215
2216         if (from->viewname)
2217                 newnode->viewname = pstrdup(from->viewname);
2218         Node_Copy(from, newnode, aliases);
2219         Node_Copy(from, newnode, query);
2220
2221         return newnode;
2222 }
2223
2224 static LoadStmt *
2225 _copyLoadStmt(LoadStmt *from)
2226 {
2227         LoadStmt   *newnode = makeNode(LoadStmt);
2228
2229         if (from->filename)
2230                 newnode->filename = pstrdup(from->filename);
2231
2232         return newnode;
2233 }
2234
2235 static CreatedbStmt *
2236 _copyCreatedbStmt(CreatedbStmt *from)
2237 {
2238         CreatedbStmt *newnode = makeNode(CreatedbStmt);
2239
2240         if (from->dbname)
2241                 newnode->dbname = pstrdup(from->dbname);
2242         if (from->dbpath)
2243                 newnode->dbpath = pstrdup(from->dbpath);
2244         if (from->dbtemplate)
2245                 newnode->dbtemplate = pstrdup(from->dbtemplate);
2246         newnode->encoding = from->encoding;
2247
2248         return newnode;
2249 }
2250
2251 static DropdbStmt *
2252 _copyDropdbStmt(DropdbStmt *from)
2253 {
2254         DropdbStmt *newnode = makeNode(DropdbStmt);
2255
2256         if (from->dbname)
2257                 newnode->dbname = pstrdup(from->dbname);
2258
2259         return newnode;
2260 }
2261
2262 static VacuumStmt *
2263 _copyVacuumStmt(VacuumStmt *from)
2264 {
2265         VacuumStmt *newnode = makeNode(VacuumStmt);
2266
2267         newnode->verbose = from->verbose;
2268         newnode->analyze = from->analyze;
2269         if (from->vacrel)
2270                 newnode->vacrel = pstrdup(from->vacrel);
2271         Node_Copy(from, newnode, va_spec);
2272
2273         return newnode;
2274 }
2275
2276 static ExplainStmt *
2277 _copyExplainStmt(ExplainStmt *from)
2278 {
2279         ExplainStmt *newnode = makeNode(ExplainStmt);
2280
2281         Node_Copy(from, newnode, query);
2282         newnode->verbose = from->verbose;
2283
2284         return newnode;
2285 }
2286
2287 static CreateSeqStmt *
2288 _copyCreateSeqStmt(CreateSeqStmt *from)
2289 {
2290         CreateSeqStmt *newnode = makeNode(CreateSeqStmt);
2291
2292         if (from->seqname)
2293                 newnode->seqname = pstrdup(from->seqname);
2294         Node_Copy(from, newnode, options);
2295
2296         return newnode;
2297 }
2298
2299 static VariableSetStmt *
2300 _copyVariableSetStmt(VariableSetStmt *from)
2301 {
2302         VariableSetStmt *newnode = makeNode(VariableSetStmt);
2303
2304         if (from->name)
2305                 newnode->name = pstrdup(from->name);
2306         if (from->value)
2307                 newnode->value = pstrdup(from->value);
2308
2309         return newnode;
2310 }
2311
2312 static VariableShowStmt *
2313 _copyVariableShowStmt(VariableShowStmt *from)
2314 {
2315         VariableShowStmt *newnode = makeNode(VariableShowStmt);
2316
2317         if (from->name)
2318                 newnode->name = pstrdup(from->name);
2319
2320         return newnode;
2321 }
2322
2323 static VariableResetStmt *
2324 _copyVariableResetStmt(VariableResetStmt *from)
2325 {
2326         VariableResetStmt *newnode = makeNode(VariableResetStmt);
2327
2328         if (from->name)
2329                 newnode->name = pstrdup(from->name);
2330
2331         return newnode;
2332 }
2333
2334 static CreateTrigStmt *
2335 _copyCreateTrigStmt(CreateTrigStmt *from)
2336 {
2337         CreateTrigStmt *newnode = makeNode(CreateTrigStmt);
2338
2339         if (from->trigname)
2340                 newnode->trigname = pstrdup(from->trigname);
2341         if (from->relname)
2342                 newnode->relname = pstrdup(from->relname);
2343         if (from->funcname)
2344                 newnode->funcname = pstrdup(from->funcname);
2345         Node_Copy(from, newnode, args);
2346         newnode->before = from->before;
2347         newnode->row = from->row;
2348         memcpy(newnode->actions, from->actions, sizeof(from->actions));
2349         if (from->lang)
2350                 newnode->lang = pstrdup(from->lang);
2351         if (from->text)
2352                 newnode->text = pstrdup(from->text);
2353
2354         Node_Copy(from, newnode, attr);
2355         if (from->when)
2356                 newnode->when = pstrdup(from->when);
2357         newnode->isconstraint = from->isconstraint;
2358         newnode->deferrable = from->deferrable;
2359         newnode->initdeferred = from->initdeferred;
2360         if (from->constrrelname)
2361                 newnode->constrrelname = pstrdup(from->constrrelname);
2362
2363         return newnode;
2364 }
2365
2366 static DropTrigStmt *
2367 _copyDropTrigStmt(DropTrigStmt *from)
2368 {
2369         DropTrigStmt *newnode = makeNode(DropTrigStmt);
2370
2371         if (from->trigname)
2372                 newnode->trigname = pstrdup(from->trigname);
2373         if (from->relname)
2374                 newnode->relname = pstrdup(from->relname);
2375
2376         return newnode;
2377 }
2378
2379 static CreatePLangStmt *
2380 _copyCreatePLangStmt(CreatePLangStmt *from)
2381 {
2382         CreatePLangStmt *newnode = makeNode(CreatePLangStmt);
2383
2384         if (from->plname)
2385                 newnode->plname = pstrdup(from->plname);
2386         if (from->plhandler)
2387                 newnode->plhandler = pstrdup(from->plhandler);
2388         if (from->plcompiler)
2389                 newnode->plcompiler = pstrdup(from->plcompiler);
2390         newnode->pltrusted = from->pltrusted;
2391
2392         return newnode;
2393 }
2394
2395 static DropPLangStmt *
2396 _copyDropPLangStmt(DropPLangStmt *from)
2397 {
2398         DropPLangStmt *newnode = makeNode(DropPLangStmt);
2399
2400         if (from->plname)
2401                 newnode->plname = pstrdup(from->plname);
2402
2403         return newnode;
2404 }
2405
2406 static CreateUserStmt *
2407 _copyCreateUserStmt(CreateUserStmt *from)
2408 {
2409         CreateUserStmt *newnode = makeNode(CreateUserStmt);
2410
2411         if (from->user)
2412                 newnode->user = pstrdup(from->user);
2413         if (from->password)
2414                 newnode->password = pstrdup(from->password);
2415         newnode->sysid = from->sysid;
2416         newnode->createdb = from->createdb;
2417         newnode->createuser = from->createuser;
2418         Node_Copy(from, newnode, groupElts);
2419         if (from->validUntil)
2420                 newnode->validUntil = pstrdup(from->validUntil);
2421
2422         return newnode;
2423 }
2424
2425 static AlterUserStmt *
2426 _copyAlterUserStmt(AlterUserStmt *from)
2427 {
2428         AlterUserStmt *newnode = makeNode(AlterUserStmt);
2429
2430         if (from->user)
2431                 newnode->user = pstrdup(from->user);
2432         if (from->password)
2433                 newnode->password = pstrdup(from->password);
2434         newnode->createdb = from->createdb;
2435         newnode->createuser = from->createuser;
2436         if (from->validUntil)
2437                 newnode->validUntil = pstrdup(from->validUntil);
2438
2439         return newnode;
2440 }
2441
2442 static DropUserStmt *
2443 _copyDropUserStmt(DropUserStmt *from)
2444 {
2445         DropUserStmt *newnode = makeNode(DropUserStmt);
2446
2447         Node_Copy(from, newnode, users);
2448
2449         return newnode;
2450 }
2451
2452 static LockStmt *
2453 _copyLockStmt(LockStmt *from)
2454 {
2455         LockStmt   *newnode = makeNode(LockStmt);
2456
2457         if (from->relname)
2458                 newnode->relname = pstrdup(from->relname);
2459         newnode->mode = from->mode;
2460
2461         return newnode;
2462 }
2463
2464 static ConstraintsSetStmt *
2465 _copyConstraintsSetStmt(ConstraintsSetStmt *from)
2466 {
2467         ConstraintsSetStmt *newnode = makeNode(ConstraintsSetStmt);
2468
2469         Node_Copy(from, newnode, constraints);
2470         newnode->deferred = from->deferred;
2471
2472         return newnode;
2473 }
2474
2475 static CreateGroupStmt *
2476 _copyCreateGroupStmt(CreateGroupStmt *from)
2477 {
2478         CreateGroupStmt *newnode = makeNode(CreateGroupStmt);
2479
2480         if (from->name)
2481                 newnode->name = pstrdup(from->name);
2482         newnode->sysid = from->sysid;
2483         Node_Copy(from, newnode, initUsers);
2484
2485         return newnode;
2486 }
2487
2488 static AlterGroupStmt *
2489 _copyAlterGroupStmt(AlterGroupStmt *from)
2490 {
2491         AlterGroupStmt *newnode = makeNode(AlterGroupStmt);
2492
2493         if (from->name)
2494                 newnode->name = pstrdup(from->name);
2495         newnode->action = from->action;
2496         newnode->sysid = from->sysid;
2497         Node_Copy(from, newnode, listUsers);
2498
2499         return newnode;
2500 }
2501
2502 static DropGroupStmt *
2503 _copyDropGroupStmt(DropGroupStmt *from)
2504 {
2505         DropGroupStmt *newnode = makeNode(DropGroupStmt);
2506
2507         if (from->name)
2508                 newnode->name = pstrdup(from->name);
2509
2510         return newnode;
2511 }
2512
2513 static ReindexStmt *
2514 _copyReindexStmt(ReindexStmt *from)
2515 {
2516         ReindexStmt *newnode = makeNode(ReindexStmt);
2517
2518         newnode->reindexType = from->reindexType;
2519         if (from->name)
2520                 newnode->name = pstrdup(from->name);
2521         newnode->force = from->force;
2522         newnode->all = from->all;
2523
2524         return newnode;
2525 }
2526
2527
2528 /* ****************************************************************
2529  *                                      pg_list.h copy functions
2530  * ****************************************************************
2531  */
2532
2533 static Value *
2534 _copyValue(Value *from)
2535 {
2536         Value      *newnode = makeNode(Value);
2537
2538         newnode->type = from->type;
2539         switch (from->type)
2540         {
2541                 case T_Integer:
2542                         newnode->val.ival = from->val.ival;
2543                         break;
2544                 case T_Float:
2545                 case T_String:
2546                 case T_BitString:
2547                         newnode->val.str = pstrdup(from->val.str);
2548                         break;
2549                 default:
2550                         break;
2551         }
2552         return newnode;
2553 }
2554
2555 /* ----------------
2556  *              copyObject returns a copy of the node or list. If it is a list, it
2557  *              recursively copies its items.
2558  * ----------------
2559  */
2560 void *
2561 copyObject(void *from)
2562 {
2563         void       *retval;
2564
2565         if (from == NULL)
2566                 return NULL;
2567
2568         switch (nodeTag(from))
2569         {
2570
2571                         /*
2572                          * PLAN NODES
2573                          */
2574                 case T_Plan:
2575                         retval = _copyPlan(from);
2576                         break;
2577                 case T_Result:
2578                         retval = _copyResult(from);
2579                         break;
2580                 case T_Append:
2581                         retval = _copyAppend(from);
2582                         break;
2583                 case T_Scan:
2584                         retval = _copyScan(from);
2585                         break;
2586                 case T_SeqScan:
2587                         retval = _copySeqScan(from);
2588                         break;
2589                 case T_IndexScan:
2590                         retval = _copyIndexScan(from);
2591                         break;
2592                 case T_TidScan:
2593                         retval = _copyTidScan(from);
2594                         break;
2595                 case T_SubqueryScan:
2596                         retval = _copySubqueryScan(from);
2597                         break;
2598                 case T_Join:
2599                         retval = _copyJoin(from);
2600                         break;
2601                 case T_NestLoop:
2602                         retval = _copyNestLoop(from);
2603                         break;
2604                 case T_MergeJoin:
2605                         retval = _copyMergeJoin(from);
2606                         break;
2607                 case T_HashJoin:
2608                         retval = _copyHashJoin(from);
2609                         break;
2610                 case T_Material:
2611                         retval = _copyMaterial(from);
2612                         break;
2613                 case T_Sort:
2614                         retval = _copySort(from);
2615                         break;
2616                 case T_Group:
2617                         retval = _copyGroup(from);
2618                         break;
2619                 case T_Agg:
2620                         retval = _copyAgg(from);
2621                         break;
2622                 case T_Unique:
2623                         retval = _copyUnique(from);
2624                         break;
2625                 case T_SetOp:
2626                         retval = _copySetOp(from);
2627                         break;
2628                 case T_Limit:
2629                         retval = _copyLimit(from);
2630                         break;
2631                 case T_Hash:
2632                         retval = _copyHash(from);
2633                         break;
2634                 case T_SubPlan:
2635                         retval = _copySubPlan(from);
2636                         break;
2637
2638                         /*
2639                          * PRIMITIVE NODES
2640                          */
2641                 case T_Resdom:
2642                         retval = _copyResdom(from);
2643                         break;
2644                 case T_Fjoin:
2645                         retval = _copyFjoin(from);
2646                         break;
2647                 case T_Expr:
2648                         retval = _copyExpr(from);
2649                         break;
2650                 case T_Var:
2651                         retval = _copyVar(from);
2652                         break;
2653                 case T_Oper:
2654                         retval = _copyOper(from);
2655                         break;
2656                 case T_Const:
2657                         retval = _copyConst(from);
2658                         break;
2659                 case T_Param:
2660                         retval = _copyParam(from);
2661                         break;
2662                 case T_Aggref:
2663                         retval = _copyAggref(from);
2664                         break;
2665                 case T_SubLink:
2666                         retval = _copySubLink(from);
2667                         break;
2668                 case T_Func:
2669                         retval = _copyFunc(from);
2670                         break;
2671                 case T_ArrayRef:
2672                         retval = _copyArrayRef(from);
2673                         break;
2674                 case T_Iter:
2675                         retval = _copyIter(from);
2676                         break;
2677                 case T_FieldSelect:
2678                         retval = _copyFieldSelect(from);
2679                         break;
2680                 case T_RelabelType:
2681                         retval = _copyRelabelType(from);
2682                         break;
2683                 case T_RangeTblRef:
2684                         retval = _copyRangeTblRef(from);
2685                         break;
2686                 case T_FromExpr:
2687                         retval = _copyFromExpr(from);
2688                         break;
2689                 case T_JoinExpr:
2690                         retval = _copyJoinExpr(from);
2691                         break;
2692
2693                         /*
2694                          * RELATION NODES
2695                          */
2696                 case T_RelOptInfo:
2697                         retval = _copyRelOptInfo(from);
2698                         break;
2699                 case T_Path:
2700                         retval = _copyPath(from);
2701                         break;
2702                 case T_IndexPath:
2703                         retval = _copyIndexPath(from);
2704                         break;
2705                 case T_TidPath:
2706                         retval = _copyTidPath(from);
2707                         break;
2708                 case T_AppendPath:
2709                         retval = _copyAppendPath(from);
2710                         break;
2711                 case T_NestPath:
2712                         retval = _copyNestPath(from);
2713                         break;
2714                 case T_MergePath:
2715                         retval = _copyMergePath(from);
2716                         break;
2717                 case T_HashPath:
2718                         retval = _copyHashPath(from);
2719                         break;
2720                 case T_PathKeyItem:
2721                         retval = _copyPathKeyItem(from);
2722                         break;
2723                 case T_RestrictInfo:
2724                         retval = _copyRestrictInfo(from);
2725                         break;
2726                 case T_JoinInfo:
2727                         retval = _copyJoinInfo(from);
2728                         break;
2729                 case T_Stream:
2730                         retval = _copyStream(from);
2731                         break;
2732                 case T_IndexOptInfo:
2733                         retval = _copyIndexOptInfo(from);
2734                         break;
2735
2736                         /*
2737                          * VALUE NODES
2738                          */
2739                 case T_Integer:
2740                 case T_Float:
2741                 case T_String:
2742                 case T_BitString:
2743                         retval = _copyValue(from);
2744                         break;
2745                 case T_List:
2746                         {
2747                                 List       *list = from,
2748                                                    *l,
2749                                                    *nl;
2750
2751                                 /* rather ugly coding for speed... */
2752                                 /* Note the input list cannot be NIL if we got here. */
2753                                 nl = makeList1(copyObject(lfirst(list)));
2754                                 retval = nl;
2755
2756                                 foreach(l, lnext(list))
2757                                 {
2758                                         lnext(nl) = makeList1(copyObject(lfirst(l)));
2759                                         nl = lnext(nl);
2760                                 }
2761                         }
2762                         break;
2763
2764                         /*
2765                          * PARSE NODES
2766                          */
2767                 case T_Query:
2768                         retval = _copyQuery(from);
2769                         break;
2770                 case T_InsertStmt:
2771                         retval = _copyInsertStmt(from);
2772                         break;
2773                 case T_DeleteStmt:
2774                         retval = _copyDeleteStmt(from);
2775                         break;
2776                 case T_UpdateStmt:
2777                         retval = _copyUpdateStmt(from);
2778                         break;
2779                 case T_SelectStmt:
2780                         retval = _copySelectStmt(from);
2781                         break;
2782                 case T_SetOperationStmt:
2783                         retval = _copySetOperationStmt(from);
2784                         break;
2785                 case T_AlterTableStmt:
2786                         retval = _copyAlterTableStmt(from);
2787                         break;
2788                 case T_ChangeACLStmt:
2789                         retval = _copyChangeACLStmt(from);
2790                         break;
2791                 case T_ClosePortalStmt:
2792                         retval = _copyClosePortalStmt(from);
2793                         break;
2794                 case T_ClusterStmt:
2795                         retval = _copyClusterStmt(from);
2796                         break;
2797                 case T_CopyStmt:
2798                         retval = _copyCopyStmt(from);
2799                         break;
2800                 case T_CreateStmt:
2801                         retval = _copyCreateStmt(from);
2802                         break;
2803                 case T_VersionStmt:
2804                         retval = _copyVersionStmt(from);
2805                         break;
2806                 case T_DefineStmt:
2807                         retval = _copyDefineStmt(from);
2808                         break;
2809                 case T_DropStmt:
2810                         retval = _copyDropStmt(from);
2811                         break;
2812                 case T_TruncateStmt:
2813                         retval = _copyTruncateStmt(from);
2814                         break;
2815                 case T_CommentStmt:
2816                         retval = _copyCommentStmt(from);
2817                         break;
2818                 case T_ExtendStmt:
2819                         retval = _copyExtendStmt(from);
2820                         break;
2821                 case T_FetchStmt:
2822                         retval = _copyFetchStmt(from);
2823                         break;
2824                 case T_IndexStmt:
2825                         retval = _copyIndexStmt(from);
2826                         break;
2827                 case T_ProcedureStmt:
2828                         retval = _copyProcedureStmt(from);
2829                         break;
2830                 case T_RemoveAggrStmt:
2831                         retval = _copyRemoveAggrStmt(from);
2832                         break;
2833                 case T_RemoveFuncStmt:
2834                         retval = _copyRemoveFuncStmt(from);
2835                         break;
2836                 case T_RemoveOperStmt:
2837                         retval = _copyRemoveOperStmt(from);
2838                         break;
2839                 case T_RenameStmt:
2840                         retval = _copyRenameStmt(from);
2841                         break;
2842                 case T_RuleStmt:
2843                         retval = _copyRuleStmt(from);
2844                         break;
2845                 case T_NotifyStmt:
2846                         retval = _copyNotifyStmt(from);
2847                         break;
2848                 case T_ListenStmt:
2849                         retval = _copyListenStmt(from);
2850                         break;
2851                 case T_UnlistenStmt:
2852                         retval = _copyUnlistenStmt(from);
2853                         break;
2854                 case T_TransactionStmt:
2855                         retval = _copyTransactionStmt(from);
2856                         break;
2857                 case T_ViewStmt:
2858                         retval = _copyViewStmt(from);
2859                         break;
2860                 case T_LoadStmt:
2861                         retval = _copyLoadStmt(from);
2862                         break;
2863                 case T_CreatedbStmt:
2864                         retval = _copyCreatedbStmt(from);
2865                         break;
2866                 case T_DropdbStmt:
2867                         retval = _copyDropdbStmt(from);
2868                         break;
2869                 case T_VacuumStmt:
2870                         retval = _copyVacuumStmt(from);
2871                         break;
2872                 case T_ExplainStmt:
2873                         retval = _copyExplainStmt(from);
2874                         break;
2875                 case T_CreateSeqStmt:
2876                         retval = _copyCreateSeqStmt(from);
2877                         break;
2878                 case T_VariableSetStmt:
2879                         retval = _copyVariableSetStmt(from);
2880                         break;
2881                 case T_VariableShowStmt:
2882                         retval = _copyVariableShowStmt(from);
2883                         break;
2884                 case T_VariableResetStmt:
2885                         retval = _copyVariableResetStmt(from);
2886                         break;
2887                 case T_CreateTrigStmt:
2888                         retval = _copyCreateTrigStmt(from);
2889                         break;
2890                 case T_DropTrigStmt:
2891                         retval = _copyDropTrigStmt(from);
2892                         break;
2893                 case T_CreatePLangStmt:
2894                         retval = _copyCreatePLangStmt(from);
2895                         break;
2896                 case T_DropPLangStmt:
2897                         retval = _copyDropPLangStmt(from);
2898                         break;
2899                 case T_CreateUserStmt:
2900                         retval = _copyCreateUserStmt(from);
2901                         break;
2902                 case T_AlterUserStmt:
2903                         retval = _copyAlterUserStmt(from);
2904                         break;
2905                 case T_DropUserStmt:
2906                         retval = _copyDropUserStmt(from);
2907                         break;
2908                 case T_LockStmt:
2909                         retval = _copyLockStmt(from);
2910                         break;
2911                 case T_ConstraintsSetStmt:
2912                         retval = _copyConstraintsSetStmt(from);
2913                         break;
2914                 case T_CreateGroupStmt:
2915                         retval = _copyCreateGroupStmt(from);
2916                         break;
2917                 case T_AlterGroupStmt:
2918                         retval = _copyAlterGroupStmt(from);
2919                         break;
2920                 case T_DropGroupStmt:
2921                         retval = _copyDropGroupStmt(from);
2922                         break;
2923                 case T_ReindexStmt:
2924                         retval = _copyReindexStmt(from);
2925                         break;
2926                 case T_CheckPointStmt:
2927                         retval = (void *) makeNode(CheckPointStmt);
2928                         break;
2929
2930                 case T_A_Expr:
2931                         retval = _copyAExpr(from);
2932                         break;
2933                 case T_Attr:
2934                         retval = _copyAttr(from);
2935                         break;
2936                 case T_A_Const:
2937                         retval = _copyAConst(from);
2938                         break;
2939                 case T_ParamNo:
2940                         retval = _copyParamNo(from);
2941                         break;
2942                 case T_Ident:
2943                         retval = _copyIdent(from);
2944                         break;
2945                 case T_FuncCall:
2946                         retval = _copyFuncCall(from);
2947                         break;
2948                 case T_A_Indices:
2949                         retval = _copyAIndices(from);
2950                         break;
2951                 case T_ResTarget:
2952                         retval = _copyResTarget(from);
2953                         break;
2954                 case T_TypeCast:
2955                         retval = _copyTypeCast(from);
2956                         break;
2957                 case T_SortGroupBy:
2958                         retval = _copySortGroupBy(from);
2959                         break;
2960                 case T_RangeVar:
2961                         retval = _copyRangeVar(from);
2962                         break;
2963                 case T_RangeSubselect:
2964                         retval = _copyRangeSubselect(from);
2965                         break;
2966                 case T_TypeName:
2967                         retval = _copyTypeName(from);
2968                         break;
2969                 case T_IndexElem:
2970                         retval = _copyIndexElem(from);
2971                         break;
2972                 case T_ColumnDef:
2973                         retval = _copyColumnDef(from);
2974                         break;
2975                 case T_Constraint:
2976                         retval = _copyConstraint(from);
2977                         break;
2978                 case T_DefElem:
2979                         retval = _copyDefElem(from);
2980                         break;
2981                 case T_TargetEntry:
2982                         retval = _copyTargetEntry(from);
2983                         break;
2984                 case T_RangeTblEntry:
2985                         retval = _copyRangeTblEntry(from);
2986                         break;
2987                 case T_SortClause:
2988                         retval = _copySortClause(from);
2989                         break;
2990                 case T_GroupClause:
2991                         retval = _copyGroupClause(from);
2992                         break;
2993                 case T_CaseExpr:
2994                         retval = _copyCaseExpr(from);
2995                         break;
2996                 case T_CaseWhen:
2997                         retval = _copyCaseWhen(from);
2998                         break;
2999                 case T_FkConstraint:
3000                         retval = _copyFkConstraint(from);
3001                         break;
3002
3003                 default:
3004                         elog(ERROR, "copyObject: don't know how to copy node type %d",
3005                                  nodeTag(from));
3006                         retval = from;          /* keep compiler quiet */
3007                         break;
3008         }
3009         return retval;
3010 }