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