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