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