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