]> granicus.if.org Git - postgresql/blob - src/test/regress/regress.c
Back out use of palloc0 in place if palloc/MemSet. Seems constant len
[postgresql] / src / test / regress / regress.c
1 /*
2  * $Header: /cvsroot/pgsql/src/test/regress/regress.c,v 1.53 2002/11/11 03:02:20 momjian 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 /* this essentially does a cartesian product of the lsegs in the
76    two paths, and finds the min distance between any two lsegs */
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(poly)
118 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                 sprintf(buf, ",%*g,%*g", P_MAXDIG, poly->p[i].x, P_MAXDIG, poly->p[i].y);
129                 strcat(output, buf);
130         }
131
132         sprintf(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, pt1, pt2)
184 LSEG       *lseg;
185 Point      *pt1;
186 Point      *pt2;
187 {
188         lseg->p[0].x = pt1->x;
189         lseg->p[0].y = pt1->y;
190         lseg->p[1].x = pt2->x;
191         lseg->p[1].y = pt2->y;
192         lseg->m = point_sl(pt1, pt2);
193 }
194
195 PG_FUNCTION_INFO_V1(overpaid);
196
197 Datum
198 overpaid(PG_FUNCTION_ARGS)
199 {
200         TUPLE           tuple = (TUPLE) PG_GETARG_POINTER(0);
201         bool            isnull;
202         int32           salary;
203
204         salary = DatumGetInt32(GetAttributeByName(tuple, "salary", &isnull));
205         if (isnull)
206                 PG_RETURN_NULL();
207         PG_RETURN_BOOL(salary > 699);
208 }
209
210 /* New type "widget"
211  * This used to be "circle", but I added circle to builtins,
212  *      so needed to make sure the names do not collide. - tgl 97/04/21
213  */
214
215 typedef struct
216 {
217         Point           center;
218         double          radius;
219 }       WIDGET;
220
221 WIDGET     *widget_in(char *str);
222 char       *widget_out(WIDGET * widget);
223 extern Datum pt_in_widget(PG_FUNCTION_ARGS);
224
225 #define NARGS   3
226
227 WIDGET *
228 widget_in(str)
229 char       *str;
230 {
231         char       *p,
232                            *coord[NARGS],
233                                 buf2[1000];
234         int                     i;
235         WIDGET     *result;
236
237         if (str == NULL)
238                 return NULL;
239         for (i = 0, p = str; *p && i < NARGS && *p != RDELIM; p++)
240                 if (*p == ',' || (*p == LDELIM && !i))
241                         coord[i++] = p + 1;
242         if (i < NARGS - 1)
243                 return NULL;
244         result = (WIDGET *) palloc(sizeof(WIDGET));
245         result->center.x = atof(coord[0]);
246         result->center.y = atof(coord[1]);
247         result->radius = atof(coord[2]);
248
249         sprintf(buf2, "widget_in: read (%f, %f, %f)\n", result->center.x,
250                         result->center.y, result->radius);
251         return result;
252 }
253
254 char *
255 widget_out(widget)
256 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 #define ABS(X) ((X) >= 0 ? (X) : -(X))
281
282 PG_FUNCTION_INFO_V1(boxarea);
283
284 Datum
285 boxarea(PG_FUNCTION_ARGS)
286 {
287         BOX                *box = PG_GETARG_BOX_P(0);
288         double          width,
289                                 height;
290
291         width = ABS(box->high.x - box->low.x);
292         height = ABS(box->high.y - box->low.y);
293         PG_RETURN_FLOAT8(width * height);
294 }
295
296 char *
297 reverse_name(char *string)
298 {
299         int                     i;
300         int                     len;
301         char       *new_string;
302
303         if (!(new_string = palloc(NAMEDATALEN)))
304         {
305                 fprintf(stderr, "reverse_name: palloc failed\n");
306                 return NULL;
307         }
308         MemSet(new_string, 0, NAMEDATALEN);
309         for (i = 0; i < NAMEDATALEN && string[i]; ++i)
310                 ;
311         if (i == NAMEDATALEN || !string[i])
312                 --i;
313         len = i;
314         for (; i >= 0; --i)
315                 new_string[len - i] = string[i];
316         return new_string;
317 }
318
319 /* This rather silly function is just to test that oldstyle functions
320  * work correctly on toast-able inputs.
321  */
322 int
323 oldstyle_length(int n, text *t)
324 {
325         int                     len = 0;
326
327         if (t)
328                 len = VARSIZE(t) - VARHDRSZ;
329
330         return n + len;
331 }
332
333 #include "executor/spi.h"               /* this is what you need to work with SPI */
334 #include "commands/trigger.h"   /* -"- and triggers */
335
336 static TransactionId fd17b_xid = InvalidTransactionId;
337 static TransactionId fd17a_xid = InvalidTransactionId;
338 static int      fd17b_level = 0;
339 static int      fd17a_level = 0;
340 static bool fd17b_recursion = true;
341 static bool fd17a_recursion = true;
342 extern Datum funny_dup17(PG_FUNCTION_ARGS);
343
344 PG_FUNCTION_INFO_V1(funny_dup17);
345
346 Datum
347 funny_dup17(PG_FUNCTION_ARGS)
348 {
349         TriggerData *trigdata = (TriggerData *) fcinfo->context;
350         TransactionId *xid;
351         int                *level;
352         bool       *recursion;
353         Relation        rel;
354         TupleDesc       tupdesc;
355         HeapTuple       tuple;
356         char       *query,
357                            *fieldval,
358                            *fieldtype;
359         char       *when;
360         int                     inserted;
361         int                     selected = 0;
362         int                     ret;
363
364         if (!CALLED_AS_TRIGGER(fcinfo))
365                 elog(ERROR, "funny_dup17: not fired by trigger manager");
366
367         tuple = trigdata->tg_trigtuple;
368         rel = trigdata->tg_relation;
369         tupdesc = rel->rd_att;
370         if (TRIGGER_FIRED_BEFORE(trigdata->tg_event))
371         {
372                 xid = &fd17b_xid;
373                 level = &fd17b_level;
374                 recursion = &fd17b_recursion;
375                 when = "BEFORE";
376         }
377         else
378         {
379                 xid = &fd17a_xid;
380                 level = &fd17a_level;
381                 recursion = &fd17a_recursion;
382                 when = "AFTER ";
383         }
384
385         if (!TransactionIdIsCurrentTransactionId(*xid))
386         {
387                 *xid = GetCurrentTransactionId();
388                 *level = 0;
389                 *recursion = true;
390         }
391
392         if (*level == 17)
393         {
394                 *recursion = false;
395                 return PointerGetDatum(tuple);
396         }
397
398         if (!(*recursion))
399                 return PointerGetDatum(tuple);
400
401         (*level)++;
402
403         SPI_connect();
404
405         fieldval = SPI_getvalue(tuple, tupdesc, 1);
406         fieldtype = SPI_gettype(tupdesc, 1);
407
408         query = (char *) palloc(100 + NAMEDATALEN * 3 +
409                                                         strlen(fieldval) + strlen(fieldtype));
410
411         sprintf(query, "insert into %s select * from %s where %s = '%s'::%s",
412                         SPI_getrelname(rel), 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 (insert ...) returned %d",
418                          when, *level, ret);
419
420         inserted = SPI_processed;
421
422         sprintf(query, "select count (*) from %s where %s = '%s'::%s",
423                         SPI_getrelname(rel),
424                         SPI_fname(tupdesc, 1),
425                         fieldval, fieldtype);
426
427         if ((ret = SPI_exec(query, 0)) < 0)
428                 elog(ERROR, "funny_dup17 (fired %s) on level %3d: SPI_exec (select ...) returned %d",
429                          when, *level, ret);
430
431         if (SPI_processed > 0)
432         {
433                 selected = DatumGetInt32(DirectFunctionCall1(int4in,
434                                                                                         CStringGetDatum(SPI_getvalue(
435                                                                                                    SPI_tuptable->vals[0],
436                                                                                                    SPI_tuptable->tupdesc,
437                                                                                                                                                  1
438                                                                                                                                         ))));
439         }
440
441         elog(DEBUG3, "funny_dup17 (fired %s) on level %3d: %d/%d tuples inserted/selected",
442                  when, *level, inserted, selected);
443
444         SPI_finish();
445
446         (*level)--;
447
448         if (*level == 0)
449                 *xid = InvalidTransactionId;
450
451         return PointerGetDatum(tuple);
452 }
453
454 extern Datum ttdummy(PG_FUNCTION_ARGS);
455 extern Datum set_ttdummy(PG_FUNCTION_ARGS);
456
457 #define TTDUMMY_INFINITY        999999
458
459 static void *splan = NULL;
460 static bool ttoff = false;
461
462 PG_FUNCTION_INFO_V1(ttdummy);
463
464 Datum
465 ttdummy(PG_FUNCTION_ARGS)
466 {
467         TriggerData *trigdata = (TriggerData *) fcinfo->context;
468         Trigger    *trigger;            /* to get trigger name */
469         char      **args;                       /* arguments */
470         int                     attnum[2];              /* fnumbers of start/stop columns */
471         Datum           oldon,
472                                 oldoff;
473         Datum           newon,
474                                 newoff;
475         Datum      *cvals;                      /* column values */
476         char       *cnulls;                     /* column nulls */
477         char       *relname;            /* triggered relation name */
478         Relation        rel;                    /* triggered relation */
479         HeapTuple       trigtuple;
480         HeapTuple       newtuple = NULL;
481         HeapTuple       rettuple;
482         TupleDesc       tupdesc;                /* tuple description */
483         int                     natts;                  /* # of attributes */
484         bool            isnull;                 /* to know is some column NULL or not */
485         int                     ret;
486         int                     i;
487
488         if (!CALLED_AS_TRIGGER(fcinfo))
489                 elog(ERROR, "ttdummy: not fired by trigger manager");
490         if (TRIGGER_FIRED_FOR_STATEMENT(trigdata->tg_event))
491                 elog(ERROR, "ttdummy: can't process STATEMENT events");
492         if (TRIGGER_FIRED_AFTER(trigdata->tg_event))
493                 elog(ERROR, "ttdummy: must be fired before event");
494         if (TRIGGER_FIRED_BY_INSERT(trigdata->tg_event))
495                 elog(ERROR, "ttdummy: can't process INSERT event");
496         if (TRIGGER_FIRED_BY_UPDATE(trigdata->tg_event))
497                 newtuple = trigdata->tg_newtuple;
498
499         trigtuple = trigdata->tg_trigtuple;
500
501         rel = trigdata->tg_relation;
502         relname = SPI_getrelname(rel);
503
504         /* check if TT is OFF for this relation */
505         if (ttoff)                                      /* OFF - nothing to do */
506         {
507                 pfree(relname);
508                 return PointerGetDatum((newtuple != NULL) ? newtuple : trigtuple);
509         }
510
511         trigger = trigdata->tg_trigger;
512
513         if (trigger->tgnargs != 2)
514                 elog(ERROR, "ttdummy (%s): invalid (!= 2) number of arguments %d",
515                          relname, trigger->tgnargs);
516
517         args = trigger->tgargs;
518         tupdesc = rel->rd_att;
519         natts = tupdesc->natts;
520
521         for (i = 0; i < 2; i++)
522         {
523                 attnum[i] = SPI_fnumber(tupdesc, args[i]);
524                 if (attnum[i] < 0)
525                         elog(ERROR, "ttdummy (%s): there is no attribute %s", relname, args[i]);
526                 if (SPI_gettypeid(tupdesc, attnum[i]) != INT4OID)
527                         elog(ERROR, "ttdummy (%s): attributes %s and %s must be of abstime type",
528                                  relname, args[0], args[1]);
529         }
530
531         oldon = SPI_getbinval(trigtuple, tupdesc, attnum[0], &isnull);
532         if (isnull)
533                 elog(ERROR, "ttdummy (%s): %s must be NOT NULL", relname, args[0]);
534
535         oldoff = SPI_getbinval(trigtuple, tupdesc, attnum[1], &isnull);
536         if (isnull)
537                 elog(ERROR, "ttdummy (%s): %s must be NOT NULL", relname, args[1]);
538
539         if (newtuple != NULL)           /* UPDATE */
540         {
541                 newon = SPI_getbinval(newtuple, tupdesc, attnum[0], &isnull);
542                 if (isnull)
543                         elog(ERROR, "ttdummy (%s): %s must be NOT NULL", relname, args[0]);
544                 newoff = SPI_getbinval(newtuple, tupdesc, attnum[1], &isnull);
545                 if (isnull)
546                         elog(ERROR, "ttdummy (%s): %s must be NOT NULL", relname, args[1]);
547
548                 if (oldon != newon || oldoff != newoff)
549                         elog(ERROR, "ttdummy (%s): you can't change %s and/or %s columns (use set_ttdummy)",
550                                  relname, args[0], args[1]);
551
552                 if (newoff != TTDUMMY_INFINITY)
553                 {
554                         pfree(relname);         /* allocated in upper executor context */
555                         return PointerGetDatum(NULL);
556                 }
557         }
558         else if (oldoff != TTDUMMY_INFINITY)            /* DELETE */
559         {
560                 pfree(relname);
561                 return PointerGetDatum(NULL);
562         }
563
564         {
565                 text       *seqname = DatumGetTextP(DirectFunctionCall1(textin,
566                                                                                 CStringGetDatum("ttdummy_seq")));
567
568                 newoff = DirectFunctionCall1(nextval,
569                                                                          PointerGetDatum(seqname));
570                 /* nextval now returns int64; coerce down to int32 */
571                 newoff = Int32GetDatum((int32) DatumGetInt64(newoff));
572                 pfree(seqname);
573         }
574
575         /* Connect to SPI manager */
576         if ((ret = SPI_connect()) < 0)
577                 elog(ERROR, "ttdummy (%s): SPI_connect returned %d", relname, ret);
578
579         /* Fetch tuple values and nulls */
580         cvals = (Datum *) palloc(natts * sizeof(Datum));
581         cnulls = (char *) palloc(natts * sizeof(char));
582         for (i = 0; i < natts; i++)
583         {
584                 cvals[i] = SPI_getbinval((newtuple != NULL) ? newtuple : trigtuple,
585                                                                  tupdesc, i + 1, &isnull);
586                 cnulls[i] = (isnull) ? 'n' : ' ';
587         }
588
589         /* change date column(s) */
590         if (newtuple)                           /* UPDATE */
591         {
592                 cvals[attnum[0] - 1] = newoff;  /* start_date eq current date */
593                 cnulls[attnum[0] - 1] = ' ';
594                 cvals[attnum[1] - 1] = TTDUMMY_INFINITY;                /* stop_date eq INFINITY */
595                 cnulls[attnum[1] - 1] = ' ';
596         }
597         else
598 /* DELETE */
599         {
600                 cvals[attnum[1] - 1] = newoff;  /* stop_date eq current date */
601                 cnulls[attnum[1] - 1] = ' ';
602         }
603
604         /* if there is no plan ... */
605         if (splan == NULL)
606         {
607                 void       *pplan;
608                 Oid                *ctypes;
609                 char       *query;
610
611                 /* allocate space in preparation */
612                 ctypes = (Oid *) palloc(natts * sizeof(Oid));
613                 query = (char *) palloc(100 + 16 * natts);
614
615                 /*
616                  * Construct query: INSERT INTO _relation_ VALUES ($1, ...)
617                  */
618                 sprintf(query, "INSERT INTO %s VALUES (", relname);
619                 for (i = 1; i <= natts; i++)
620                 {
621                         sprintf(query + strlen(query), "$%d%s",
622                                         i, (i < natts) ? ", " : ")");
623                         ctypes[i - 1] = SPI_gettypeid(tupdesc, i);
624                 }
625
626                 /* Prepare plan for query */
627                 pplan = SPI_prepare(query, natts, ctypes);
628                 if (pplan == NULL)
629                         elog(ERROR, "ttdummy (%s): SPI_prepare returned %d", relname, SPI_result);
630
631                 pplan = SPI_saveplan(pplan);
632                 if (pplan == NULL)
633                         elog(ERROR, "ttdummy (%s): SPI_saveplan returned %d", relname, SPI_result);
634
635                 splan = pplan;
636         }
637
638         ret = SPI_execp(splan, cvals, cnulls, 0);
639
640         if (ret < 0)
641                 elog(ERROR, "ttdummy (%s): SPI_execp returned %d", relname, ret);
642
643         /* Tuple to return to upper Executor ... */
644         if (newtuple)                           /* UPDATE */
645         {
646                 HeapTuple       tmptuple;
647
648                 tmptuple = SPI_copytuple(trigtuple);
649                 rettuple = SPI_modifytuple(rel, tmptuple, 1, &(attnum[1]), &newoff, NULL);
650                 SPI_freetuple(tmptuple);
651         }
652         else
653 /* DELETE */
654                 rettuple = trigtuple;
655
656         SPI_finish();                           /* don't forget say Bye to SPI mgr */
657
658         pfree(relname);
659
660         return PointerGetDatum(rettuple);
661 }
662
663 PG_FUNCTION_INFO_V1(set_ttdummy);
664
665 Datum
666 set_ttdummy(PG_FUNCTION_ARGS)
667 {
668         int32           on = PG_GETARG_INT32(0);
669
670         if (ttoff)                                      /* OFF currently */
671         {
672                 if (on == 0)
673                         PG_RETURN_INT32(0);
674
675                 /* turn ON */
676                 ttoff = false;
677                 PG_RETURN_INT32(0);
678         }
679
680         /* ON currently */
681         if (on != 0)
682                 PG_RETURN_INT32(1);
683
684         /* turn OFF */
685         ttoff = true;
686
687         PG_RETURN_INT32(1);
688 }
689
690
691 /*
692  * Type int44 has no real-world use, but the regression tests use it.
693  * It's a four-element vector of int4's.
694  */
695
696 /*
697  *              int44in                 - converts "num num ..." to internal form
698  *
699  *              Note: Fills any missing positions with zeroes.
700  */
701 PG_FUNCTION_INFO_V1(int44in);
702
703 Datum
704 int44in(PG_FUNCTION_ARGS)
705 {
706         char       *input_string = PG_GETARG_CSTRING(0);
707         int32      *result = (int32 *) palloc(4 * sizeof(int32));
708         int                     i;
709
710         i = sscanf(input_string,
711                            "%d, %d, %d, %d",
712                            &result[0],
713                            &result[1],
714                            &result[2],
715                            &result[3]);
716         while (i < 4)
717                 result[i++] = 0;
718
719         PG_RETURN_POINTER(result);
720 }
721
722 /*
723  *              int44out                - converts internal form to "num num ..."
724  */
725 PG_FUNCTION_INFO_V1(int44out);
726
727 Datum
728 int44out(PG_FUNCTION_ARGS)
729 {
730         int32      *an_array = (int32 *) PG_GETARG_POINTER(0);
731         char       *result = (char *) palloc(16 * 4);           /* Allow 14 digits +
732                                                                                                                  * sign */
733         int                     i;
734         char       *walk;
735
736         walk = result;
737         for (i = 0; i < 4; i++)
738         {
739                 pg_ltoa(an_array[i], walk);
740                 while (*++walk != '\0')
741                         ;
742                 *walk++ = ' ';
743         }
744         *--walk = '\0';
745         PG_RETURN_CSTRING(result);
746 }