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