]> granicus.if.org Git - postgresql/blob - src/test/regress/regress.c
Allow include files to compile own their own.
[postgresql] / src / test / regress / regress.c
1 /*
2  * $PostgreSQL: pgsql/src/test/regress/regress.c,v 1.68 2006/07/13 16:49:20 momjian Exp $
3  */
4
5 #include "postgres.h"
6
7 #include <float.h>                              /* faked on sunos */
8
9 #include "access/transam.h"
10 #include "utils/geo_decls.h"    /* includes <math.h> */
11 #include "executor/executor.h"  /* For GetAttributeByName */
12 #include "commands/sequence.h"  /* for nextval() */
13
14 #define P_MAXDIG 12
15 #define LDELIM                  '('
16 #define RDELIM                  ')'
17 #define DELIM                   ','
18
19 extern Datum regress_dist_ptpath(PG_FUNCTION_ARGS);
20 extern Datum regress_path_dist(PG_FUNCTION_ARGS);
21 extern PATH *poly2path(POLYGON *poly);
22 extern Datum interpt_pp(PG_FUNCTION_ARGS);
23 extern void regress_lseg_construct(LSEG *lseg, Point *pt1, Point *pt2);
24 extern Datum overpaid(PG_FUNCTION_ARGS);
25 extern Datum boxarea(PG_FUNCTION_ARGS);
26 extern char *reverse_name(char *string);
27 extern int      oldstyle_length(int n, text *t);
28 extern Datum int44in(PG_FUNCTION_ARGS);
29 extern Datum int44out(PG_FUNCTION_ARGS);
30
31 #ifdef PG_MODULE_MAGIC
32 PG_MODULE_MAGIC;
33 #endif
34
35
36 /*
37  * Distance from a point to a path
38  */
39 PG_FUNCTION_INFO_V1(regress_dist_ptpath);
40
41 Datum
42 regress_dist_ptpath(PG_FUNCTION_ARGS)
43 {
44         Point      *pt = PG_GETARG_POINT_P(0);
45         PATH       *path = PG_GETARG_PATH_P(1);
46         float8          result = 0.0;   /* keep compiler quiet */
47         float8          tmp;
48         int                     i;
49         LSEG            lseg;
50
51         switch (path->npts)
52         {
53                 case 0:
54                         PG_RETURN_NULL();
55                 case 1:
56                         result = point_dt(pt, &path->p[0]);
57                         break;
58                 default:
59
60                         /*
61                          * the distance from a point to a path is the smallest distance
62                          * from the point to any of its constituent segments.
63                          */
64                         Assert(path->npts > 1);
65                         for (i = 0; i < path->npts - 1; ++i)
66                         {
67                                 regress_lseg_construct(&lseg, &path->p[i], &path->p[i + 1]);
68                                 tmp = DatumGetFloat8(DirectFunctionCall2(dist_ps,
69                                                                                                                  PointPGetDatum(pt),
70                                                                                                           LsegPGetDatum(&lseg)));
71                                 if (i == 0 || tmp < result)
72                                         result = tmp;
73                         }
74                         break;
75         }
76         PG_RETURN_FLOAT8(result);
77 }
78
79 /*
80  * this essentially does a cartesian product of the lsegs in the
81  * two paths, and finds the min distance between any two lsegs
82  */
83 PG_FUNCTION_INFO_V1(regress_path_dist);
84
85 Datum
86 regress_path_dist(PG_FUNCTION_ARGS)
87 {
88         PATH       *p1 = PG_GETARG_PATH_P(0);
89         PATH       *p2 = PG_GETARG_PATH_P(1);
90         bool            have_min = false;
91         float8          min = 0.0;              /* initialize to keep compiler quiet */
92         float8          tmp;
93         int                     i,
94                                 j;
95         LSEG            seg1,
96                                 seg2;
97
98         for (i = 0; i < p1->npts - 1; i++)
99         {
100                 for (j = 0; j < p2->npts - 1; j++)
101                 {
102                         regress_lseg_construct(&seg1, &p1->p[i], &p1->p[i + 1]);
103                         regress_lseg_construct(&seg2, &p2->p[j], &p2->p[j + 1]);
104
105                         tmp = DatumGetFloat8(DirectFunctionCall2(lseg_distance,
106                                                                                                          LsegPGetDatum(&seg1),
107                                                                                                          LsegPGetDatum(&seg2)));
108                         if (!have_min || tmp < min)
109                         {
110                                 min = tmp;
111                                 have_min = true;
112                         }
113                 }
114         }
115
116         if (!have_min)
117                 PG_RETURN_NULL();
118
119         PG_RETURN_FLOAT8(min);
120 }
121
122 PATH *
123 poly2path(POLYGON *poly)
124 {
125         int                     i;
126         char       *output = (char *) palloc(2 * (P_MAXDIG + 1) * poly->npts + 64);
127         char            buf[2 * (P_MAXDIG) + 20];
128
129         sprintf(output, "(1, %*d", P_MAXDIG, poly->npts);
130
131         for (i = 0; i < poly->npts; i++)
132         {
133                 snprintf(buf, sizeof(buf), ",%*g,%*g",
134                                  P_MAXDIG, poly->p[i].x, P_MAXDIG, poly->p[i].y);
135                 strcat(output, buf);
136         }
137
138         snprintf(buf, sizeof(buf), "%c", RDELIM);
139         strcat(output, buf);
140         return DatumGetPathP(DirectFunctionCall1(path_in,
141                                                                                          CStringGetDatum(output)));
142 }
143
144 /* return the point where two paths intersect, or NULL if no intersection. */
145 PG_FUNCTION_INFO_V1(interpt_pp);
146
147 Datum
148 interpt_pp(PG_FUNCTION_ARGS)
149 {
150         PATH       *p1 = PG_GETARG_PATH_P(0);
151         PATH       *p2 = PG_GETARG_PATH_P(1);
152         int                     i,
153                                 j;
154         LSEG            seg1,
155                                 seg2;
156         bool            found;                  /* We've found the intersection */
157
158         found = false;                          /* Haven't found it yet */
159
160         for (i = 0; i < p1->npts - 1 && !found; i++)
161         {
162                 regress_lseg_construct(&seg1, &p1->p[i], &p1->p[i + 1]);
163                 for (j = 0; j < p2->npts - 1 && !found; j++)
164                 {
165                         regress_lseg_construct(&seg2, &p2->p[j], &p2->p[j + 1]);
166                         if (DatumGetBool(DirectFunctionCall2(lseg_intersect,
167                                                                                                  LsegPGetDatum(&seg1),
168                                                                                                  LsegPGetDatum(&seg2))))
169                                 found = true;
170                 }
171         }
172
173         if (!found)
174                 PG_RETURN_NULL();
175
176         /*
177          * Note: DirectFunctionCall2 will kick out an error if lseg_interpt()
178          * returns NULL, but that should be impossible since we know the two
179          * segments intersect.
180          */
181         PG_RETURN_DATUM(DirectFunctionCall2(lseg_interpt,
182                                                                                 LsegPGetDatum(&seg1),
183                                                                                 LsegPGetDatum(&seg2)));
184 }
185
186
187 /* like lseg_construct, but assume space already allocated */
188 void
189 regress_lseg_construct(LSEG *lseg, Point *pt1, Point *pt2)
190 {
191         lseg->p[0].x = pt1->x;
192         lseg->p[0].y = pt1->y;
193         lseg->p[1].x = pt2->x;
194         lseg->p[1].y = pt2->y;
195         lseg->m = point_sl(pt1, pt2);
196 }
197
198 PG_FUNCTION_INFO_V1(overpaid);
199
200 Datum
201 overpaid(PG_FUNCTION_ARGS)
202 {
203         HeapTupleHeader tuple = PG_GETARG_HEAPTUPLEHEADER(0);
204         bool            isnull;
205         int32           salary;
206
207         salary = DatumGetInt32(GetAttributeByName(tuple, "salary", &isnull));
208         if (isnull)
209                 PG_RETURN_NULL();
210         PG_RETURN_BOOL(salary > 699);
211 }
212
213 /* New type "widget"
214  * This used to be "circle", but I added circle to builtins,
215  *      so needed to make sure the names do not collide. - tgl 97/04/21
216  */
217
218 typedef struct
219 {
220         Point           center;
221         double          radius;
222 }       WIDGET;
223
224 WIDGET     *widget_in(char *str);
225 char       *widget_out(WIDGET * widget);
226 extern Datum pt_in_widget(PG_FUNCTION_ARGS);
227
228 #define NARGS   3
229
230 WIDGET *
231 widget_in(char *str)
232 {
233         char       *p,
234                            *coord[NARGS],
235                                 buf2[1000];
236         int                     i;
237         WIDGET     *result;
238
239         if (str == NULL)
240                 return NULL;
241         for (i = 0, p = str; *p && i < NARGS && *p != RDELIM; p++)
242                 if (*p == ',' || (*p == LDELIM && !i))
243                         coord[i++] = p + 1;
244         if (i < NARGS - 1)
245                 return NULL;
246         result = (WIDGET *) palloc(sizeof(WIDGET));
247         result->center.x = atof(coord[0]);
248         result->center.y = atof(coord[1]);
249         result->radius = atof(coord[2]);
250
251         snprintf(buf2, sizeof(buf2), "widget_in: read (%f, %f, %f)\n",
252                          result->center.x, result->center.y, result->radius);
253         return result;
254 }
255
256 char *
257 widget_out(WIDGET * widget)
258 {
259         char       *result;
260
261         if (widget == NULL)
262                 return NULL;
263
264         result = (char *) palloc(60);
265         sprintf(result, "(%g,%g,%g)",
266                         widget->center.x, widget->center.y, widget->radius);
267         return result;
268 }
269
270 PG_FUNCTION_INFO_V1(pt_in_widget);
271
272 Datum
273 pt_in_widget(PG_FUNCTION_ARGS)
274 {
275         Point      *point = PG_GETARG_POINT_P(0);
276         WIDGET     *widget = (WIDGET *) PG_GETARG_POINTER(1);
277
278         PG_RETURN_BOOL(point_dt(point, &widget->center) < widget->radius);
279 }
280
281 PG_FUNCTION_INFO_V1(boxarea);
282
283 Datum
284 boxarea(PG_FUNCTION_ARGS)
285 {
286         BOX                *box = PG_GETARG_BOX_P(0);
287         double          width,
288                                 height;
289
290         width = Abs(box->high.x - box->low.x);
291         height = Abs(box->high.y - box->low.y);
292         PG_RETURN_FLOAT8(width * height);
293 }
294
295 char *
296 reverse_name(char *string)
297 {
298         int                     i;
299         int                     len;
300         char       *new_string;
301
302         new_string = palloc0(NAMEDATALEN);
303         for (i = 0; i < NAMEDATALEN && string[i]; ++i)
304                 ;
305         if (i == NAMEDATALEN || !string[i])
306                 --i;
307         len = i;
308         for (; i >= 0; --i)
309                 new_string[len - i] = string[i];
310         return new_string;
311 }
312
313 /*
314  * This rather silly function is just to test that oldstyle functions
315  * work correctly on toast-able inputs.
316  */
317 int
318 oldstyle_length(int n, text *t)
319 {
320         int                     len = 0;
321
322         if (t)
323                 len = VARSIZE(t) - VARHDRSZ;
324
325         return n + len;
326 }
327
328 #include "executor/spi.h"               /* this is what you need to work with SPI */
329 #include "commands/trigger.h"   /* -"- and triggers */
330
331 static TransactionId fd17b_xid = InvalidTransactionId;
332 static TransactionId fd17a_xid = InvalidTransactionId;
333 static int      fd17b_level = 0;
334 static int      fd17a_level = 0;
335 static bool fd17b_recursion = true;
336 static bool fd17a_recursion = true;
337 extern Datum funny_dup17(PG_FUNCTION_ARGS);
338
339 PG_FUNCTION_INFO_V1(funny_dup17);
340
341 Datum
342 funny_dup17(PG_FUNCTION_ARGS)
343 {
344         TriggerData *trigdata = (TriggerData *) fcinfo->context;
345         TransactionId *xid;
346         int                *level;
347         bool       *recursion;
348         Relation        rel;
349         TupleDesc       tupdesc;
350         HeapTuple       tuple;
351         char       *query,
352                            *fieldval,
353                            *fieldtype;
354         char       *when;
355         int                     inserted;
356         int                     selected = 0;
357         int                     ret;
358
359         if (!CALLED_AS_TRIGGER(fcinfo))
360                 elog(ERROR, "funny_dup17: not fired by trigger manager");
361
362         tuple = trigdata->tg_trigtuple;
363         rel = trigdata->tg_relation;
364         tupdesc = rel->rd_att;
365         if (TRIGGER_FIRED_BEFORE(trigdata->tg_event))
366         {
367                 xid = &fd17b_xid;
368                 level = &fd17b_level;
369                 recursion = &fd17b_recursion;
370                 when = "BEFORE";
371         }
372         else
373         {
374                 xid = &fd17a_xid;
375                 level = &fd17a_level;
376                 recursion = &fd17a_recursion;
377                 when = "AFTER ";
378         }
379
380         if (!TransactionIdIsCurrentTransactionId(*xid))
381         {
382                 *xid = GetCurrentTransactionId();
383                 *level = 0;
384                 *recursion = true;
385         }
386
387         if (*level == 17)
388         {
389                 *recursion = false;
390                 return PointerGetDatum(tuple);
391         }
392
393         if (!(*recursion))
394                 return PointerGetDatum(tuple);
395
396         (*level)++;
397
398         SPI_connect();
399
400         fieldval = SPI_getvalue(tuple, tupdesc, 1);
401         fieldtype = SPI_gettype(tupdesc, 1);
402
403         query = (char *) palloc(100 + NAMEDATALEN * 3 +
404                                                         strlen(fieldval) + strlen(fieldtype));
405
406         sprintf(query, "insert into %s select * from %s where %s = '%s'::%s",
407                         SPI_getrelname(rel), SPI_getrelname(rel),
408                         SPI_fname(tupdesc, 1),
409                         fieldval, fieldtype);
410
411         if ((ret = SPI_exec(query, 0)) < 0)
412                 elog(ERROR, "funny_dup17 (fired %s) on level %3d: SPI_exec (insert ...) returned %d",
413                          when, *level, ret);
414
415         inserted = SPI_processed;
416
417         sprintf(query, "select count (*) from %s where %s = '%s'::%s",
418                         SPI_getrelname(rel),
419                         SPI_fname(tupdesc, 1),
420                         fieldval, fieldtype);
421
422         if ((ret = SPI_exec(query, 0)) < 0)
423                 elog(ERROR, "funny_dup17 (fired %s) on level %3d: SPI_exec (select ...) returned %d",
424                          when, *level, ret);
425
426         if (SPI_processed > 0)
427         {
428                 selected = DatumGetInt32(DirectFunctionCall1(int4in,
429                                                                                                 CStringGetDatum(SPI_getvalue(
430                                                                                                            SPI_tuptable->vals[0],
431                                                                                                            SPI_tuptable->tupdesc,
432                                                                                                                                                          1
433                                                                                                                                                 ))));
434         }
435
436         elog(DEBUG4, "funny_dup17 (fired %s) on level %3d: %d/%d tuples inserted/selected",
437                  when, *level, inserted, selected);
438
439         SPI_finish();
440
441         (*level)--;
442
443         if (*level == 0)
444                 *xid = InvalidTransactionId;
445
446         return PointerGetDatum(tuple);
447 }
448
449 extern Datum ttdummy(PG_FUNCTION_ARGS);
450 extern Datum set_ttdummy(PG_FUNCTION_ARGS);
451
452 #define TTDUMMY_INFINITY        999999
453
454 static void *splan = NULL;
455 static bool ttoff = false;
456
457 PG_FUNCTION_INFO_V1(ttdummy);
458
459 Datum
460 ttdummy(PG_FUNCTION_ARGS)
461 {
462         TriggerData *trigdata = (TriggerData *) fcinfo->context;
463         Trigger    *trigger;            /* to get trigger name */
464         char      **args;                       /* arguments */
465         int                     attnum[2];              /* fnumbers of start/stop columns */
466         Datum           oldon,
467                                 oldoff;
468         Datum           newon,
469                                 newoff;
470         Datum      *cvals;                      /* column values */
471         char       *cnulls;                     /* column nulls */
472         char       *relname;            /* triggered relation name */
473         Relation        rel;                    /* triggered relation */
474         HeapTuple       trigtuple;
475         HeapTuple       newtuple = NULL;
476         HeapTuple       rettuple;
477         TupleDesc       tupdesc;                /* tuple description */
478         int                     natts;                  /* # of attributes */
479         bool            isnull;                 /* to know is some column NULL or not */
480         int                     ret;
481         int                     i;
482
483         if (!CALLED_AS_TRIGGER(fcinfo))
484                 elog(ERROR, "ttdummy: not fired by trigger manager");
485         if (TRIGGER_FIRED_FOR_STATEMENT(trigdata->tg_event))
486                 elog(ERROR, "ttdummy: can't process STATEMENT events");
487         if (TRIGGER_FIRED_AFTER(trigdata->tg_event))
488                 elog(ERROR, "ttdummy: must be fired before event");
489         if (TRIGGER_FIRED_BY_INSERT(trigdata->tg_event))
490                 elog(ERROR, "ttdummy: can't process INSERT event");
491         if (TRIGGER_FIRED_BY_UPDATE(trigdata->tg_event))
492                 newtuple = trigdata->tg_newtuple;
493
494         trigtuple = trigdata->tg_trigtuple;
495
496         rel = trigdata->tg_relation;
497         relname = SPI_getrelname(rel);
498
499         /* check if TT is OFF for this relation */
500         if (ttoff)                                      /* OFF - nothing to do */
501         {
502                 pfree(relname);
503                 return PointerGetDatum((newtuple != NULL) ? newtuple : trigtuple);
504         }
505
506         trigger = trigdata->tg_trigger;
507
508         if (trigger->tgnargs != 2)
509                 elog(ERROR, "ttdummy (%s): invalid (!= 2) number of arguments %d",
510                          relname, trigger->tgnargs);
511
512         args = trigger->tgargs;
513         tupdesc = rel->rd_att;
514         natts = tupdesc->natts;
515
516         for (i = 0; i < 2; i++)
517         {
518                 attnum[i] = SPI_fnumber(tupdesc, args[i]);
519                 if (attnum[i] < 0)
520                         elog(ERROR, "ttdummy (%s): there is no attribute %s", relname, args[i]);
521                 if (SPI_gettypeid(tupdesc, attnum[i]) != INT4OID)
522                         elog(ERROR, "ttdummy (%s): attributes %s and %s must be of abstime type",
523                                  relname, args[0], args[1]);
524         }
525
526         oldon = SPI_getbinval(trigtuple, tupdesc, attnum[0], &isnull);
527         if (isnull)
528                 elog(ERROR, "ttdummy (%s): %s must be NOT NULL", relname, args[0]);
529
530         oldoff = SPI_getbinval(trigtuple, tupdesc, attnum[1], &isnull);
531         if (isnull)
532                 elog(ERROR, "ttdummy (%s): %s must be NOT NULL", relname, args[1]);
533
534         if (newtuple != NULL)           /* UPDATE */
535         {
536                 newon = SPI_getbinval(newtuple, tupdesc, attnum[0], &isnull);
537                 if (isnull)
538                         elog(ERROR, "ttdummy (%s): %s must be NOT NULL", relname, args[0]);
539                 newoff = SPI_getbinval(newtuple, tupdesc, attnum[1], &isnull);
540                 if (isnull)
541                         elog(ERROR, "ttdummy (%s): %s must be NOT NULL", relname, args[1]);
542
543                 if (oldon != newon || oldoff != newoff)
544                         elog(ERROR, "ttdummy (%s): you can't change %s and/or %s columns (use set_ttdummy)",
545                                  relname, args[0], args[1]);
546
547                 if (newoff != TTDUMMY_INFINITY)
548                 {
549                         pfree(relname);         /* allocated in upper executor context */
550                         return PointerGetDatum(NULL);
551                 }
552         }
553         else if (oldoff != TTDUMMY_INFINITY)            /* DELETE */
554         {
555                 pfree(relname);
556                 return PointerGetDatum(NULL);
557         }
558
559         {
560                 text       *seqname = DatumGetTextP(DirectFunctionCall1(textin,
561                                                                                         CStringGetDatum("ttdummy_seq")));
562
563                 newoff = DirectFunctionCall1(nextval,
564                                                                          PointerGetDatum(seqname));
565                 /* nextval now returns int64; coerce down to int32 */
566                 newoff = Int32GetDatum((int32) DatumGetInt64(newoff));
567                 pfree(seqname);
568         }
569
570         /* Connect to SPI manager */
571         if ((ret = SPI_connect()) < 0)
572                 elog(ERROR, "ttdummy (%s): SPI_connect returned %d", relname, ret);
573
574         /* Fetch tuple values and nulls */
575         cvals = (Datum *) palloc(natts * sizeof(Datum));
576         cnulls = (char *) palloc(natts * sizeof(char));
577         for (i = 0; i < natts; i++)
578         {
579                 cvals[i] = SPI_getbinval((newtuple != NULL) ? newtuple : trigtuple,
580                                                                  tupdesc, i + 1, &isnull);
581                 cnulls[i] = (isnull) ? 'n' : ' ';
582         }
583
584         /* change date column(s) */
585         if (newtuple)                           /* UPDATE */
586         {
587                 cvals[attnum[0] - 1] = newoff;  /* start_date eq current date */
588                 cnulls[attnum[0] - 1] = ' ';
589                 cvals[attnum[1] - 1] = TTDUMMY_INFINITY;                /* stop_date eq INFINITY */
590                 cnulls[attnum[1] - 1] = ' ';
591         }
592         else
593                 /* DELETE */
594         {
595                 cvals[attnum[1] - 1] = newoff;  /* stop_date eq current date */
596                 cnulls[attnum[1] - 1] = ' ';
597         }
598
599         /* if there is no plan ... */
600         if (splan == NULL)
601         {
602                 void       *pplan;
603                 Oid                *ctypes;
604                 char       *query;
605
606                 /* allocate space in preparation */
607                 ctypes = (Oid *) palloc(natts * sizeof(Oid));
608                 query = (char *) palloc(100 + 16 * natts);
609
610                 /*
611                  * Construct query: INSERT INTO _relation_ VALUES ($1, ...)
612                  */
613                 sprintf(query, "INSERT INTO %s VALUES (", relname);
614                 for (i = 1; i <= natts; i++)
615                 {
616                         sprintf(query + strlen(query), "$%d%s",
617                                         i, (i < natts) ? ", " : ")");
618                         ctypes[i - 1] = SPI_gettypeid(tupdesc, i);
619                 }
620
621                 /* Prepare plan for query */
622                 pplan = SPI_prepare(query, natts, ctypes);
623                 if (pplan == NULL)
624                         elog(ERROR, "ttdummy (%s): SPI_prepare returned %d", relname, SPI_result);
625
626                 pplan = SPI_saveplan(pplan);
627                 if (pplan == NULL)
628                         elog(ERROR, "ttdummy (%s): SPI_saveplan returned %d", relname, SPI_result);
629
630                 splan = pplan;
631         }
632
633         ret = SPI_execp(splan, cvals, cnulls, 0);
634
635         if (ret < 0)
636                 elog(ERROR, "ttdummy (%s): SPI_execp returned %d", relname, ret);
637
638         /* Tuple to return to upper Executor ... */
639         if (newtuple)                           /* UPDATE */
640         {
641                 HeapTuple       tmptuple;
642
643                 tmptuple = SPI_copytuple(trigtuple);
644                 rettuple = SPI_modifytuple(rel, tmptuple, 1, &(attnum[1]), &newoff, NULL);
645                 SPI_freetuple(tmptuple);
646         }
647         else
648                 /* DELETE */
649                 rettuple = trigtuple;
650
651         SPI_finish();                           /* don't forget say Bye to SPI mgr */
652
653         pfree(relname);
654
655         return PointerGetDatum(rettuple);
656 }
657
658 PG_FUNCTION_INFO_V1(set_ttdummy);
659
660 Datum
661 set_ttdummy(PG_FUNCTION_ARGS)
662 {
663         int32           on = PG_GETARG_INT32(0);
664
665         if (ttoff)                                      /* OFF currently */
666         {
667                 if (on == 0)
668                         PG_RETURN_INT32(0);
669
670                 /* turn ON */
671                 ttoff = false;
672                 PG_RETURN_INT32(0);
673         }
674
675         /* ON currently */
676         if (on != 0)
677                 PG_RETURN_INT32(1);
678
679         /* turn OFF */
680         ttoff = true;
681
682         PG_RETURN_INT32(1);
683 }
684
685
686 /*
687  * Type int44 has no real-world use, but the regression tests use it.
688  * It's a four-element vector of int4's.
689  */
690
691 /*
692  *              int44in                 - converts "num num ..." to internal form
693  *
694  *              Note: Fills any missing positions with zeroes.
695  */
696 PG_FUNCTION_INFO_V1(int44in);
697
698 Datum
699 int44in(PG_FUNCTION_ARGS)
700 {
701         char       *input_string = PG_GETARG_CSTRING(0);
702         int32      *result = (int32 *) palloc(4 * sizeof(int32));
703         int                     i;
704
705         i = sscanf(input_string,
706                            "%d, %d, %d, %d",
707                            &result[0],
708                            &result[1],
709                            &result[2],
710                            &result[3]);
711         while (i < 4)
712                 result[i++] = 0;
713
714         PG_RETURN_POINTER(result);
715 }
716
717 /*
718  *              int44out                - converts internal form to "num num ..."
719  */
720 PG_FUNCTION_INFO_V1(int44out);
721
722 Datum
723 int44out(PG_FUNCTION_ARGS)
724 {
725         int32      *an_array = (int32 *) PG_GETARG_POINTER(0);
726         char       *result = (char *) palloc(16 * 4);           /* Allow 14 digits +
727                                                                                                                  * sign */
728         int                     i;
729         char       *walk;
730
731         walk = result;
732         for (i = 0; i < 4; i++)
733         {
734                 pg_ltoa(an_array[i], walk);
735                 while (*++walk != '\0')
736                         ;
737                 *walk++ = ' ';
738         }
739         *--walk = '\0';
740         PG_RETURN_CSTRING(result);
741 }