Line data Source code
1 : /*------------------------------------------------------------------------
2 : *
3 : * regress.c
4 : * Code for various C-language functions defined as part of the
5 : * regression tests.
6 : *
7 : * This code is released under the terms of the PostgreSQL License.
8 : *
9 : * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
10 : * Portions Copyright (c) 1994, Regents of the University of California
11 : *
12 : * src/test/regress/regress.c
13 : *
14 : *-------------------------------------------------------------------------
15 : */
16 :
17 : #include "postgres.h"
18 :
19 : #include <math.h>
20 : #include <signal.h>
21 :
22 : #include "access/detoast.h"
23 : #include "access/htup_details.h"
24 : #include "catalog/namespace.h"
25 : #include "catalog/pg_operator.h"
26 : #include "catalog/pg_type.h"
27 : #include "commands/sequence.h"
28 : #include "commands/trigger.h"
29 : #include "executor/executor.h"
30 : #include "executor/spi.h"
31 : #include "funcapi.h"
32 : #include "mb/pg_wchar.h"
33 : #include "miscadmin.h"
34 : #include "nodes/supportnodes.h"
35 : #include "optimizer/optimizer.h"
36 : #include "optimizer/plancat.h"
37 : #include "parser/parse_coerce.h"
38 : #include "port/atomics.h"
39 : #include "postmaster/postmaster.h" /* for MAX_BACKENDS */
40 : #include "storage/spin.h"
41 : #include "utils/array.h"
42 : #include "utils/builtins.h"
43 : #include "utils/geo_decls.h"
44 : #include "utils/memutils.h"
45 : #include "utils/rel.h"
46 : #include "utils/typcache.h"
47 :
48 : #define EXPECT_TRUE(expr) \
49 : do { \
50 : if (!(expr)) \
51 : elog(ERROR, \
52 : "%s was unexpectedly false in file \"%s\" line %u", \
53 : #expr, __FILE__, __LINE__); \
54 : } while (0)
55 :
56 : #define EXPECT_EQ_U32(result_expr, expected_expr) \
57 : do { \
58 : uint32 actual_result = (result_expr); \
59 : uint32 expected_result = (expected_expr); \
60 : if (actual_result != expected_result) \
61 : elog(ERROR, \
62 : "%s yielded %u, expected %s in file \"%s\" line %u", \
63 : #result_expr, actual_result, #expected_expr, __FILE__, __LINE__); \
64 : } while (0)
65 :
66 : #define EXPECT_EQ_U64(result_expr, expected_expr) \
67 : do { \
68 : uint64 actual_result = (result_expr); \
69 : uint64 expected_result = (expected_expr); \
70 : if (actual_result != expected_result) \
71 : elog(ERROR, \
72 : "%s yielded " UINT64_FORMAT ", expected %s in file \"%s\" line %u", \
73 : #result_expr, actual_result, #expected_expr, __FILE__, __LINE__); \
74 : } while (0)
75 :
76 : #define LDELIM '('
77 : #define RDELIM ')'
78 : #define DELIM ','
79 :
80 : static void regress_lseg_construct(LSEG *lseg, Point *pt1, Point *pt2);
81 :
82 126 : PG_MODULE_MAGIC_EXT(
83 : .name = "regress",
84 : .version = PG_VERSION
85 : );
86 :
87 :
88 : /* return the point where two paths intersect, or NULL if no intersection. */
89 16 : PG_FUNCTION_INFO_V1(interpt_pp);
90 :
91 : Datum
92 5376 : interpt_pp(PG_FUNCTION_ARGS)
93 : {
94 5376 : PATH *p1 = PG_GETARG_PATH_P(0);
95 5376 : PATH *p2 = PG_GETARG_PATH_P(1);
96 : int i,
97 : j;
98 : LSEG seg1,
99 : seg2;
100 : bool found; /* We've found the intersection */
101 :
102 5376 : found = false; /* Haven't found it yet */
103 :
104 17646 : for (i = 0; i < p1->npts - 1 && !found; i++)
105 : {
106 12270 : regress_lseg_construct(&seg1, &p1->p[i], &p1->p[i + 1]);
107 37638 : for (j = 0; j < p2->npts - 1 && !found; j++)
108 : {
109 25368 : regress_lseg_construct(&seg2, &p2->p[j], &p2->p[j + 1]);
110 25368 : if (DatumGetBool(DirectFunctionCall2(lseg_intersect,
111 : LsegPGetDatum(&seg1),
112 : LsegPGetDatum(&seg2))))
113 5364 : found = true;
114 : }
115 : }
116 :
117 5376 : if (!found)
118 12 : PG_RETURN_NULL();
119 :
120 : /*
121 : * Note: DirectFunctionCall2 will kick out an error if lseg_interpt()
122 : * returns NULL, but that should be impossible since we know the two
123 : * segments intersect.
124 : */
125 5364 : PG_RETURN_DATUM(DirectFunctionCall2(lseg_interpt,
126 : LsegPGetDatum(&seg1),
127 : LsegPGetDatum(&seg2)));
128 : }
129 :
130 :
131 : /* like lseg_construct, but assume space already allocated */
132 : static void
133 37638 : regress_lseg_construct(LSEG *lseg, Point *pt1, Point *pt2)
134 : {
135 37638 : lseg->p[0].x = pt1->x;
136 37638 : lseg->p[0].y = pt1->y;
137 37638 : lseg->p[1].x = pt2->x;
138 37638 : lseg->p[1].y = pt2->y;
139 37638 : }
140 :
141 16 : PG_FUNCTION_INFO_V1(overpaid);
142 :
143 : Datum
144 36 : overpaid(PG_FUNCTION_ARGS)
145 : {
146 36 : HeapTupleHeader tuple = PG_GETARG_HEAPTUPLEHEADER(0);
147 : bool isnull;
148 : int32 salary;
149 :
150 36 : salary = DatumGetInt32(GetAttributeByName(tuple, "salary", &isnull));
151 36 : if (isnull)
152 0 : PG_RETURN_NULL();
153 36 : PG_RETURN_BOOL(salary > 699);
154 : }
155 :
156 : /* New type "widget"
157 : * This used to be "circle", but I added circle to builtins,
158 : * so needed to make sure the names do not collide. - tgl 97/04/21
159 : */
160 :
161 : typedef struct
162 : {
163 : Point center;
164 : double radius;
165 : } WIDGET;
166 :
167 22 : PG_FUNCTION_INFO_V1(widget_in);
168 16 : PG_FUNCTION_INFO_V1(widget_out);
169 :
170 : #define NARGS 3
171 :
172 : Datum
173 66 : widget_in(PG_FUNCTION_ARGS)
174 : {
175 66 : char *str = PG_GETARG_CSTRING(0);
176 : char *p,
177 : *coord[NARGS];
178 : int i;
179 : WIDGET *result;
180 :
181 378 : for (i = 0, p = str; *p && i < NARGS && *p != RDELIM; p++)
182 : {
183 312 : if (*p == DELIM || (*p == LDELIM && i == 0))
184 162 : coord[i++] = p + 1;
185 : }
186 :
187 : /*
188 : * Note: DON'T convert this error to "soft" style (errsave/ereturn). We
189 : * want this data type to stay permanently in the hard-error world so that
190 : * it can be used for testing that such cases still work reasonably.
191 : */
192 66 : if (i < NARGS)
193 24 : ereport(ERROR,
194 : (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
195 : errmsg("invalid input syntax for type %s: \"%s\"",
196 : "widget", str)));
197 :
198 42 : result = (WIDGET *) palloc(sizeof(WIDGET));
199 42 : result->center.x = atof(coord[0]);
200 42 : result->center.y = atof(coord[1]);
201 42 : result->radius = atof(coord[2]);
202 :
203 42 : PG_RETURN_POINTER(result);
204 : }
205 :
206 : Datum
207 12 : widget_out(PG_FUNCTION_ARGS)
208 : {
209 12 : WIDGET *widget = (WIDGET *) PG_GETARG_POINTER(0);
210 12 : char *str = psprintf("(%g,%g,%g)",
211 : widget->center.x, widget->center.y, widget->radius);
212 :
213 12 : PG_RETURN_CSTRING(str);
214 : }
215 :
216 16 : PG_FUNCTION_INFO_V1(pt_in_widget);
217 :
218 : Datum
219 12 : pt_in_widget(PG_FUNCTION_ARGS)
220 : {
221 12 : Point *point = PG_GETARG_POINT_P(0);
222 12 : WIDGET *widget = (WIDGET *) PG_GETARG_POINTER(1);
223 : float8 distance;
224 :
225 12 : distance = DatumGetFloat8(DirectFunctionCall2(point_distance,
226 : PointPGetDatum(point),
227 : PointPGetDatum(&widget->center)));
228 :
229 12 : PG_RETURN_BOOL(distance < widget->radius);
230 : }
231 :
232 16 : PG_FUNCTION_INFO_V1(reverse_name);
233 :
234 : Datum
235 48 : reverse_name(PG_FUNCTION_ARGS)
236 : {
237 48 : char *string = PG_GETARG_CSTRING(0);
238 : int i;
239 : int len;
240 : char *new_string;
241 :
242 48 : new_string = palloc0(NAMEDATALEN);
243 336 : for (i = 0; i < NAMEDATALEN && string[i]; ++i)
244 : ;
245 48 : if (i == NAMEDATALEN || !string[i])
246 48 : --i;
247 48 : len = i;
248 336 : for (; i >= 0; --i)
249 288 : new_string[len - i] = string[i];
250 48 : PG_RETURN_CSTRING(new_string);
251 : }
252 :
253 16 : PG_FUNCTION_INFO_V1(trigger_return_old);
254 :
255 : Datum
256 90 : trigger_return_old(PG_FUNCTION_ARGS)
257 : {
258 90 : TriggerData *trigdata = (TriggerData *) fcinfo->context;
259 : HeapTuple tuple;
260 :
261 90 : if (!CALLED_AS_TRIGGER(fcinfo))
262 0 : elog(ERROR, "trigger_return_old: not fired by trigger manager");
263 :
264 90 : tuple = trigdata->tg_trigtuple;
265 :
266 90 : return PointerGetDatum(tuple);
267 : }
268 :
269 : #define TTDUMMY_INFINITY 999999
270 :
271 : static SPIPlanPtr splan = NULL;
272 : static bool ttoff = false;
273 :
274 16 : PG_FUNCTION_INFO_V1(ttdummy);
275 :
276 : Datum
277 60 : ttdummy(PG_FUNCTION_ARGS)
278 : {
279 60 : TriggerData *trigdata = (TriggerData *) fcinfo->context;
280 : Trigger *trigger; /* to get trigger name */
281 : char **args; /* arguments */
282 : int attnum[2]; /* fnumbers of start/stop columns */
283 : Datum oldon,
284 : oldoff;
285 : Datum newon,
286 : newoff;
287 : Datum *cvals; /* column values */
288 : char *cnulls; /* column nulls */
289 : char *relname; /* triggered relation name */
290 : Relation rel; /* triggered relation */
291 : HeapTuple trigtuple;
292 60 : HeapTuple newtuple = NULL;
293 : HeapTuple rettuple;
294 : TupleDesc tupdesc; /* tuple description */
295 : int natts; /* # of attributes */
296 : bool isnull; /* to know is some column NULL or not */
297 : int ret;
298 : int i;
299 :
300 60 : if (!CALLED_AS_TRIGGER(fcinfo))
301 0 : elog(ERROR, "ttdummy: not fired by trigger manager");
302 60 : if (!TRIGGER_FIRED_FOR_ROW(trigdata->tg_event))
303 0 : elog(ERROR, "ttdummy: must be fired for row");
304 60 : if (!TRIGGER_FIRED_BEFORE(trigdata->tg_event))
305 0 : elog(ERROR, "ttdummy: must be fired before event");
306 60 : if (TRIGGER_FIRED_BY_INSERT(trigdata->tg_event))
307 0 : elog(ERROR, "ttdummy: cannot process INSERT event");
308 60 : if (TRIGGER_FIRED_BY_UPDATE(trigdata->tg_event))
309 48 : newtuple = trigdata->tg_newtuple;
310 :
311 60 : trigtuple = trigdata->tg_trigtuple;
312 :
313 60 : rel = trigdata->tg_relation;
314 60 : relname = SPI_getrelname(rel);
315 :
316 : /* check if TT is OFF for this relation */
317 60 : if (ttoff) /* OFF - nothing to do */
318 : {
319 30 : pfree(relname);
320 30 : return PointerGetDatum((newtuple != NULL) ? newtuple : trigtuple);
321 : }
322 :
323 30 : trigger = trigdata->tg_trigger;
324 :
325 30 : if (trigger->tgnargs != 2)
326 0 : elog(ERROR, "ttdummy (%s): invalid (!= 2) number of arguments %d",
327 : relname, trigger->tgnargs);
328 :
329 30 : args = trigger->tgargs;
330 30 : tupdesc = rel->rd_att;
331 30 : natts = tupdesc->natts;
332 :
333 90 : for (i = 0; i < 2; i++)
334 : {
335 60 : attnum[i] = SPI_fnumber(tupdesc, args[i]);
336 60 : if (attnum[i] <= 0)
337 0 : elog(ERROR, "ttdummy (%s): there is no attribute %s",
338 : relname, args[i]);
339 60 : if (SPI_gettypeid(tupdesc, attnum[i]) != INT4OID)
340 0 : elog(ERROR, "ttdummy (%s): attribute %s must be of integer type",
341 : relname, args[i]);
342 : }
343 :
344 30 : oldon = SPI_getbinval(trigtuple, tupdesc, attnum[0], &isnull);
345 30 : if (isnull)
346 0 : elog(ERROR, "ttdummy (%s): %s must be NOT NULL", relname, args[0]);
347 :
348 30 : oldoff = SPI_getbinval(trigtuple, tupdesc, attnum[1], &isnull);
349 30 : if (isnull)
350 0 : elog(ERROR, "ttdummy (%s): %s must be NOT NULL", relname, args[1]);
351 :
352 30 : if (newtuple != NULL) /* UPDATE */
353 : {
354 24 : newon = SPI_getbinval(newtuple, tupdesc, attnum[0], &isnull);
355 24 : if (isnull)
356 0 : elog(ERROR, "ttdummy (%s): %s must be NOT NULL", relname, args[0]);
357 24 : newoff = SPI_getbinval(newtuple, tupdesc, attnum[1], &isnull);
358 24 : if (isnull)
359 0 : elog(ERROR, "ttdummy (%s): %s must be NOT NULL", relname, args[1]);
360 :
361 24 : if (oldon != newon || oldoff != newoff)
362 6 : ereport(ERROR,
363 : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
364 : errmsg("ttdummy (%s): you cannot change %s and/or %s columns (use set_ttdummy)",
365 : relname, args[0], args[1])));
366 :
367 18 : if (newoff != TTDUMMY_INFINITY)
368 : {
369 6 : pfree(relname); /* allocated in upper executor context */
370 6 : return PointerGetDatum(NULL);
371 : }
372 : }
373 6 : else if (oldoff != TTDUMMY_INFINITY) /* DELETE */
374 : {
375 0 : pfree(relname);
376 0 : return PointerGetDatum(NULL);
377 : }
378 :
379 18 : newoff = DirectFunctionCall1(nextval, CStringGetTextDatum("ttdummy_seq"));
380 : /* nextval now returns int64; coerce down to int32 */
381 18 : newoff = Int32GetDatum((int32) DatumGetInt64(newoff));
382 :
383 : /* Connect to SPI manager */
384 18 : SPI_connect();
385 :
386 : /* Fetch tuple values and nulls */
387 18 : cvals = (Datum *) palloc(natts * sizeof(Datum));
388 18 : cnulls = (char *) palloc(natts * sizeof(char));
389 90 : for (i = 0; i < natts; i++)
390 : {
391 72 : cvals[i] = SPI_getbinval((newtuple != NULL) ? newtuple : trigtuple,
392 : tupdesc, i + 1, &isnull);
393 72 : cnulls[i] = (isnull) ? 'n' : ' ';
394 : }
395 :
396 : /* change date column(s) */
397 18 : if (newtuple) /* UPDATE */
398 : {
399 12 : cvals[attnum[0] - 1] = newoff; /* start_date eq current date */
400 12 : cnulls[attnum[0] - 1] = ' ';
401 12 : cvals[attnum[1] - 1] = TTDUMMY_INFINITY; /* stop_date eq INFINITY */
402 12 : cnulls[attnum[1] - 1] = ' ';
403 : }
404 : else
405 : /* DELETE */
406 : {
407 6 : cvals[attnum[1] - 1] = newoff; /* stop_date eq current date */
408 6 : cnulls[attnum[1] - 1] = ' ';
409 : }
410 :
411 : /* if there is no plan ... */
412 18 : if (splan == NULL)
413 : {
414 : SPIPlanPtr pplan;
415 : Oid *ctypes;
416 : char *query;
417 :
418 : /* allocate space in preparation */
419 6 : ctypes = (Oid *) palloc(natts * sizeof(Oid));
420 6 : query = (char *) palloc(100 + 16 * natts);
421 :
422 : /*
423 : * Construct query: INSERT INTO _relation_ VALUES ($1, ...)
424 : */
425 6 : sprintf(query, "INSERT INTO %s VALUES (", relname);
426 30 : for (i = 1; i <= natts; i++)
427 : {
428 24 : sprintf(query + strlen(query), "$%d%s",
429 : i, (i < natts) ? ", " : ")");
430 24 : ctypes[i - 1] = SPI_gettypeid(tupdesc, i);
431 : }
432 :
433 : /* Prepare plan for query */
434 6 : pplan = SPI_prepare(query, natts, ctypes);
435 6 : if (pplan == NULL)
436 0 : elog(ERROR, "ttdummy (%s): SPI_prepare returned %s", relname, SPI_result_code_string(SPI_result));
437 :
438 6 : if (SPI_keepplan(pplan))
439 0 : elog(ERROR, "ttdummy (%s): SPI_keepplan failed", relname);
440 :
441 6 : splan = pplan;
442 : }
443 :
444 18 : ret = SPI_execp(splan, cvals, cnulls, 0);
445 :
446 18 : if (ret < 0)
447 0 : elog(ERROR, "ttdummy (%s): SPI_execp returned %d", relname, ret);
448 :
449 : /* Tuple to return to upper Executor ... */
450 18 : if (newtuple) /* UPDATE */
451 12 : rettuple = SPI_modifytuple(rel, trigtuple, 1, &(attnum[1]), &newoff, NULL);
452 : else /* DELETE */
453 6 : rettuple = trigtuple;
454 :
455 18 : SPI_finish(); /* don't forget say Bye to SPI mgr */
456 :
457 18 : pfree(relname);
458 :
459 18 : return PointerGetDatum(rettuple);
460 : }
461 :
462 16 : PG_FUNCTION_INFO_V1(set_ttdummy);
463 :
464 : Datum
465 18 : set_ttdummy(PG_FUNCTION_ARGS)
466 : {
467 18 : int32 on = PG_GETARG_INT32(0);
468 :
469 18 : if (ttoff) /* OFF currently */
470 : {
471 6 : if (on == 0)
472 0 : PG_RETURN_INT32(0);
473 :
474 : /* turn ON */
475 6 : ttoff = false;
476 6 : PG_RETURN_INT32(0);
477 : }
478 :
479 : /* ON currently */
480 12 : if (on != 0)
481 0 : PG_RETURN_INT32(1);
482 :
483 : /* turn OFF */
484 12 : ttoff = true;
485 :
486 12 : PG_RETURN_INT32(1);
487 : }
488 :
489 :
490 : /*
491 : * Type int44 has no real-world use, but the regression tests use it
492 : * (under the alias "city_budget"). It's a four-element vector of int4's.
493 : */
494 :
495 : /*
496 : * int44in - converts "num, num, ..." to internal form
497 : *
498 : * Note: Fills any missing positions with zeroes.
499 : */
500 18 : PG_FUNCTION_INFO_V1(int44in);
501 :
502 : Datum
503 16 : int44in(PG_FUNCTION_ARGS)
504 : {
505 16 : char *input_string = PG_GETARG_CSTRING(0);
506 16 : int32 *result = (int32 *) palloc(4 * sizeof(int32));
507 : int i;
508 :
509 16 : i = sscanf(input_string,
510 : "%d, %d, %d, %d",
511 : &result[0],
512 : &result[1],
513 : &result[2],
514 : &result[3]);
515 22 : while (i < 4)
516 6 : result[i++] = 0;
517 :
518 16 : PG_RETURN_POINTER(result);
519 : }
520 :
521 : /*
522 : * int44out - converts internal form to "num, num, ..."
523 : */
524 30 : PG_FUNCTION_INFO_V1(int44out);
525 :
526 : Datum
527 40 : int44out(PG_FUNCTION_ARGS)
528 : {
529 40 : int32 *an_array = (int32 *) PG_GETARG_POINTER(0);
530 40 : char *result = (char *) palloc(16 * 4);
531 :
532 40 : snprintf(result, 16 * 4, "%d,%d,%d,%d",
533 : an_array[0],
534 40 : an_array[1],
535 40 : an_array[2],
536 40 : an_array[3]);
537 :
538 40 : PG_RETURN_CSTRING(result);
539 : }
540 :
541 16 : PG_FUNCTION_INFO_V1(test_canonicalize_path);
542 : Datum
543 132 : test_canonicalize_path(PG_FUNCTION_ARGS)
544 : {
545 132 : char *path = text_to_cstring(PG_GETARG_TEXT_PP(0));
546 :
547 132 : canonicalize_path(path);
548 132 : PG_RETURN_TEXT_P(cstring_to_text(path));
549 : }
550 :
551 16 : PG_FUNCTION_INFO_V1(make_tuple_indirect);
552 : Datum
553 126 : make_tuple_indirect(PG_FUNCTION_ARGS)
554 : {
555 126 : HeapTupleHeader rec = PG_GETARG_HEAPTUPLEHEADER(0);
556 : HeapTupleData tuple;
557 : int ncolumns;
558 : Datum *values;
559 : bool *nulls;
560 :
561 : Oid tupType;
562 : int32 tupTypmod;
563 : TupleDesc tupdesc;
564 :
565 : HeapTuple newtup;
566 :
567 : int i;
568 :
569 : MemoryContext old_context;
570 :
571 : /* Extract type info from the tuple itself */
572 126 : tupType = HeapTupleHeaderGetTypeId(rec);
573 126 : tupTypmod = HeapTupleHeaderGetTypMod(rec);
574 126 : tupdesc = lookup_rowtype_tupdesc(tupType, tupTypmod);
575 126 : ncolumns = tupdesc->natts;
576 :
577 : /* Build a temporary HeapTuple control structure */
578 126 : tuple.t_len = HeapTupleHeaderGetDatumLength(rec);
579 126 : ItemPointerSetInvalid(&(tuple.t_self));
580 126 : tuple.t_tableOid = InvalidOid;
581 126 : tuple.t_data = rec;
582 :
583 126 : values = (Datum *) palloc(ncolumns * sizeof(Datum));
584 126 : nulls = (bool *) palloc(ncolumns * sizeof(bool));
585 :
586 126 : heap_deform_tuple(&tuple, tupdesc, values, nulls);
587 :
588 126 : old_context = MemoryContextSwitchTo(TopTransactionContext);
589 :
590 630 : for (i = 0; i < ncolumns; i++)
591 : {
592 : struct varlena *attr;
593 : struct varlena *new_attr;
594 : struct varatt_indirect redirect_pointer;
595 :
596 : /* only work on existing, not-null varlenas */
597 504 : if (TupleDescAttr(tupdesc, i)->attisdropped ||
598 504 : nulls[i] ||
599 438 : TupleDescAttr(tupdesc, i)->attlen != -1 ||
600 312 : TupleDescAttr(tupdesc, i)->attstorage == TYPSTORAGE_PLAIN)
601 192 : continue;
602 :
603 312 : attr = (struct varlena *) DatumGetPointer(values[i]);
604 :
605 : /* don't recursively indirect */
606 312 : if (VARATT_IS_EXTERNAL_INDIRECT(attr))
607 0 : continue;
608 :
609 : /* copy datum, so it still lives later */
610 312 : if (VARATT_IS_EXTERNAL_ONDISK(attr))
611 0 : attr = detoast_external_attr(attr);
612 : else
613 : {
614 312 : struct varlena *oldattr = attr;
615 :
616 312 : attr = palloc0(VARSIZE_ANY(oldattr));
617 312 : memcpy(attr, oldattr, VARSIZE_ANY(oldattr));
618 : }
619 :
620 : /* build indirection Datum */
621 312 : new_attr = (struct varlena *) palloc0(INDIRECT_POINTER_SIZE);
622 312 : redirect_pointer.pointer = attr;
623 312 : SET_VARTAG_EXTERNAL(new_attr, VARTAG_INDIRECT);
624 312 : memcpy(VARDATA_EXTERNAL(new_attr), &redirect_pointer,
625 : sizeof(redirect_pointer));
626 :
627 312 : values[i] = PointerGetDatum(new_attr);
628 : }
629 :
630 126 : newtup = heap_form_tuple(tupdesc, values, nulls);
631 126 : pfree(values);
632 126 : pfree(nulls);
633 126 : ReleaseTupleDesc(tupdesc);
634 :
635 126 : MemoryContextSwitchTo(old_context);
636 :
637 : /*
638 : * We intentionally don't use PG_RETURN_HEAPTUPLEHEADER here, because that
639 : * would cause the indirect toast pointers to be flattened out of the
640 : * tuple immediately, rendering subsequent testing irrelevant. So just
641 : * return the HeapTupleHeader pointer as-is. This violates the general
642 : * rule that composite Datums shouldn't contain toast pointers, but so
643 : * long as the regression test scripts don't insert the result of this
644 : * function into a container type (record, array, etc) it should be OK.
645 : */
646 126 : PG_RETURN_POINTER(newtup->t_data);
647 : }
648 :
649 4 : PG_FUNCTION_INFO_V1(get_environ);
650 :
651 : Datum
652 2 : get_environ(PG_FUNCTION_ARGS)
653 : {
654 : #if !defined(WIN32) || defined(_MSC_VER)
655 : extern char **environ;
656 : #endif
657 2 : int nvals = 0;
658 : ArrayType *result;
659 : Datum *env;
660 :
661 70 : for (char **s = environ; *s; s++)
662 68 : nvals++;
663 :
664 2 : env = palloc(nvals * sizeof(Datum));
665 :
666 70 : for (int i = 0; i < nvals; i++)
667 68 : env[i] = CStringGetTextDatum(environ[i]);
668 :
669 2 : result = construct_array_builtin(env, nvals, TEXTOID);
670 :
671 2 : PG_RETURN_POINTER(result);
672 : }
673 :
674 4 : PG_FUNCTION_INFO_V1(regress_setenv);
675 :
676 : Datum
677 2 : regress_setenv(PG_FUNCTION_ARGS)
678 : {
679 2 : char *envvar = text_to_cstring(PG_GETARG_TEXT_PP(0));
680 2 : char *envval = text_to_cstring(PG_GETARG_TEXT_PP(1));
681 :
682 2 : if (!superuser())
683 0 : elog(ERROR, "must be superuser to change environment variables");
684 :
685 2 : if (setenv(envvar, envval, 1) != 0)
686 0 : elog(ERROR, "could not set environment variable: %m");
687 :
688 2 : PG_RETURN_VOID();
689 : }
690 :
691 : /* Sleep until no process has a given PID. */
692 10 : PG_FUNCTION_INFO_V1(wait_pid);
693 :
694 : Datum
695 4 : wait_pid(PG_FUNCTION_ARGS)
696 : {
697 4 : int pid = PG_GETARG_INT32(0);
698 :
699 4 : if (!superuser())
700 0 : elog(ERROR, "must be superuser to check PID liveness");
701 :
702 10 : while (kill(pid, 0) == 0)
703 : {
704 6 : CHECK_FOR_INTERRUPTS();
705 6 : pg_usleep(50000);
706 : }
707 :
708 4 : if (errno != ESRCH)
709 0 : elog(ERROR, "could not check PID %d liveness: %m", pid);
710 :
711 4 : PG_RETURN_VOID();
712 : }
713 :
714 : static void
715 6 : test_atomic_flag(void)
716 : {
717 : pg_atomic_flag flag;
718 :
719 6 : pg_atomic_init_flag(&flag);
720 6 : EXPECT_TRUE(pg_atomic_unlocked_test_flag(&flag));
721 6 : EXPECT_TRUE(pg_atomic_test_set_flag(&flag));
722 6 : EXPECT_TRUE(!pg_atomic_unlocked_test_flag(&flag));
723 6 : EXPECT_TRUE(!pg_atomic_test_set_flag(&flag));
724 6 : pg_atomic_clear_flag(&flag);
725 6 : EXPECT_TRUE(pg_atomic_unlocked_test_flag(&flag));
726 6 : EXPECT_TRUE(pg_atomic_test_set_flag(&flag));
727 6 : pg_atomic_clear_flag(&flag);
728 6 : }
729 :
730 : static void
731 6 : test_atomic_uint32(void)
732 : {
733 : pg_atomic_uint32 var;
734 : uint32 expected;
735 : int i;
736 :
737 6 : pg_atomic_init_u32(&var, 0);
738 6 : EXPECT_EQ_U32(pg_atomic_read_u32(&var), 0);
739 6 : pg_atomic_write_u32(&var, 3);
740 6 : EXPECT_EQ_U32(pg_atomic_read_u32(&var), 3);
741 6 : EXPECT_EQ_U32(pg_atomic_fetch_add_u32(&var, pg_atomic_read_u32(&var) - 2),
742 : 3);
743 6 : EXPECT_EQ_U32(pg_atomic_fetch_sub_u32(&var, 1), 4);
744 6 : EXPECT_EQ_U32(pg_atomic_sub_fetch_u32(&var, 3), 0);
745 6 : EXPECT_EQ_U32(pg_atomic_add_fetch_u32(&var, 10), 10);
746 6 : EXPECT_EQ_U32(pg_atomic_exchange_u32(&var, 5), 10);
747 6 : EXPECT_EQ_U32(pg_atomic_exchange_u32(&var, 0), 5);
748 :
749 : /* test around numerical limits */
750 6 : EXPECT_EQ_U32(pg_atomic_fetch_add_u32(&var, INT_MAX), 0);
751 6 : EXPECT_EQ_U32(pg_atomic_fetch_add_u32(&var, INT_MAX), INT_MAX);
752 6 : pg_atomic_fetch_add_u32(&var, 2); /* wrap to 0 */
753 6 : EXPECT_EQ_U32(pg_atomic_fetch_add_u32(&var, PG_INT16_MAX), 0);
754 6 : EXPECT_EQ_U32(pg_atomic_fetch_add_u32(&var, PG_INT16_MAX + 1),
755 : PG_INT16_MAX);
756 6 : EXPECT_EQ_U32(pg_atomic_fetch_add_u32(&var, PG_INT16_MIN),
757 : 2 * PG_INT16_MAX + 1);
758 6 : EXPECT_EQ_U32(pg_atomic_fetch_add_u32(&var, PG_INT16_MIN - 1),
759 : PG_INT16_MAX);
760 6 : pg_atomic_fetch_add_u32(&var, 1); /* top up to UINT_MAX */
761 6 : EXPECT_EQ_U32(pg_atomic_read_u32(&var), UINT_MAX);
762 6 : EXPECT_EQ_U32(pg_atomic_fetch_sub_u32(&var, INT_MAX), UINT_MAX);
763 6 : EXPECT_EQ_U32(pg_atomic_read_u32(&var), (uint32) INT_MAX + 1);
764 6 : EXPECT_EQ_U32(pg_atomic_sub_fetch_u32(&var, INT_MAX), 1);
765 6 : pg_atomic_sub_fetch_u32(&var, 1);
766 6 : expected = PG_INT16_MAX;
767 6 : EXPECT_TRUE(!pg_atomic_compare_exchange_u32(&var, &expected, 1));
768 6 : expected = PG_INT16_MAX + 1;
769 6 : EXPECT_TRUE(!pg_atomic_compare_exchange_u32(&var, &expected, 1));
770 6 : expected = PG_INT16_MIN;
771 6 : EXPECT_TRUE(!pg_atomic_compare_exchange_u32(&var, &expected, 1));
772 6 : expected = PG_INT16_MIN - 1;
773 6 : EXPECT_TRUE(!pg_atomic_compare_exchange_u32(&var, &expected, 1));
774 :
775 : /* fail exchange because of old expected */
776 6 : expected = 10;
777 6 : EXPECT_TRUE(!pg_atomic_compare_exchange_u32(&var, &expected, 1));
778 :
779 : /* CAS is allowed to fail due to interrupts, try a couple of times */
780 12 : for (i = 0; i < 1000; i++)
781 : {
782 12 : expected = 0;
783 12 : if (!pg_atomic_compare_exchange_u32(&var, &expected, 1))
784 6 : break;
785 : }
786 6 : if (i == 1000)
787 0 : elog(ERROR, "atomic_compare_exchange_u32() never succeeded");
788 6 : EXPECT_EQ_U32(pg_atomic_read_u32(&var), 1);
789 6 : pg_atomic_write_u32(&var, 0);
790 :
791 : /* try setting flagbits */
792 6 : EXPECT_TRUE(!(pg_atomic_fetch_or_u32(&var, 1) & 1));
793 6 : EXPECT_TRUE(pg_atomic_fetch_or_u32(&var, 2) & 1);
794 6 : EXPECT_EQ_U32(pg_atomic_read_u32(&var), 3);
795 : /* try clearing flagbits */
796 6 : EXPECT_EQ_U32(pg_atomic_fetch_and_u32(&var, ~2) & 3, 3);
797 6 : EXPECT_EQ_U32(pg_atomic_fetch_and_u32(&var, ~1), 1);
798 : /* no bits set anymore */
799 6 : EXPECT_EQ_U32(pg_atomic_fetch_and_u32(&var, ~0), 0);
800 6 : }
801 :
802 : static void
803 6 : test_atomic_uint64(void)
804 : {
805 : pg_atomic_uint64 var;
806 : uint64 expected;
807 : int i;
808 :
809 6 : pg_atomic_init_u64(&var, 0);
810 6 : EXPECT_EQ_U64(pg_atomic_read_u64(&var), 0);
811 6 : pg_atomic_write_u64(&var, 3);
812 6 : EXPECT_EQ_U64(pg_atomic_read_u64(&var), 3);
813 6 : EXPECT_EQ_U64(pg_atomic_fetch_add_u64(&var, pg_atomic_read_u64(&var) - 2),
814 : 3);
815 6 : EXPECT_EQ_U64(pg_atomic_fetch_sub_u64(&var, 1), 4);
816 6 : EXPECT_EQ_U64(pg_atomic_sub_fetch_u64(&var, 3), 0);
817 6 : EXPECT_EQ_U64(pg_atomic_add_fetch_u64(&var, 10), 10);
818 6 : EXPECT_EQ_U64(pg_atomic_exchange_u64(&var, 5), 10);
819 6 : EXPECT_EQ_U64(pg_atomic_exchange_u64(&var, 0), 5);
820 :
821 : /* fail exchange because of old expected */
822 6 : expected = 10;
823 6 : EXPECT_TRUE(!pg_atomic_compare_exchange_u64(&var, &expected, 1));
824 :
825 : /* CAS is allowed to fail due to interrupts, try a couple of times */
826 12 : for (i = 0; i < 100; i++)
827 : {
828 12 : expected = 0;
829 12 : if (!pg_atomic_compare_exchange_u64(&var, &expected, 1))
830 6 : break;
831 : }
832 6 : if (i == 100)
833 0 : elog(ERROR, "atomic_compare_exchange_u64() never succeeded");
834 6 : EXPECT_EQ_U64(pg_atomic_read_u64(&var), 1);
835 :
836 6 : pg_atomic_write_u64(&var, 0);
837 :
838 : /* try setting flagbits */
839 6 : EXPECT_TRUE(!(pg_atomic_fetch_or_u64(&var, 1) & 1));
840 6 : EXPECT_TRUE(pg_atomic_fetch_or_u64(&var, 2) & 1);
841 6 : EXPECT_EQ_U64(pg_atomic_read_u64(&var), 3);
842 : /* try clearing flagbits */
843 6 : EXPECT_EQ_U64((pg_atomic_fetch_and_u64(&var, ~2) & 3), 3);
844 6 : EXPECT_EQ_U64(pg_atomic_fetch_and_u64(&var, ~1), 1);
845 : /* no bits set anymore */
846 6 : EXPECT_EQ_U64(pg_atomic_fetch_and_u64(&var, ~0), 0);
847 6 : }
848 :
849 : /*
850 : * Perform, fairly minimal, testing of the spinlock implementation.
851 : *
852 : * It's likely worth expanding these to actually test concurrency etc, but
853 : * having some regularly run tests is better than none.
854 : */
855 : static void
856 6 : test_spinlock(void)
857 : {
858 : /*
859 : * Basic tests for spinlocks, as well as the underlying operations.
860 : *
861 : * We embed the spinlock in a struct with other members to test that the
862 : * spinlock operations don't perform too wide writes.
863 : */
864 : {
865 : struct test_lock_struct
866 : {
867 : char data_before[4];
868 : slock_t lock;
869 : char data_after[4];
870 : } struct_w_lock;
871 :
872 6 : memcpy(struct_w_lock.data_before, "abcd", 4);
873 6 : memcpy(struct_w_lock.data_after, "ef12", 4);
874 :
875 : /* test basic operations via the SpinLock* API */
876 6 : SpinLockInit(&struct_w_lock.lock);
877 6 : SpinLockAcquire(&struct_w_lock.lock);
878 6 : SpinLockRelease(&struct_w_lock.lock);
879 :
880 : /* test basic operations via underlying S_* API */
881 6 : S_INIT_LOCK(&struct_w_lock.lock);
882 6 : S_LOCK(&struct_w_lock.lock);
883 6 : S_UNLOCK(&struct_w_lock.lock);
884 :
885 : /* and that "contended" acquisition works */
886 6 : s_lock(&struct_w_lock.lock, "testfile", 17, "testfunc");
887 6 : S_UNLOCK(&struct_w_lock.lock);
888 :
889 : /*
890 : * Check, using TAS directly, that a single spin cycle doesn't block
891 : * when acquiring an already acquired lock.
892 : */
893 : #ifdef TAS
894 6 : S_LOCK(&struct_w_lock.lock);
895 :
896 6 : if (!TAS(&struct_w_lock.lock))
897 0 : elog(ERROR, "acquired already held spinlock");
898 :
899 : #ifdef TAS_SPIN
900 6 : if (!TAS_SPIN(&struct_w_lock.lock))
901 0 : elog(ERROR, "acquired already held spinlock");
902 : #endif /* defined(TAS_SPIN) */
903 :
904 6 : S_UNLOCK(&struct_w_lock.lock);
905 : #endif /* defined(TAS) */
906 :
907 : /*
908 : * Verify that after all of this the non-lock contents are still
909 : * correct.
910 : */
911 6 : if (memcmp(struct_w_lock.data_before, "abcd", 4) != 0)
912 0 : elog(ERROR, "padding before spinlock modified");
913 6 : if (memcmp(struct_w_lock.data_after, "ef12", 4) != 0)
914 0 : elog(ERROR, "padding after spinlock modified");
915 : }
916 6 : }
917 :
918 16 : PG_FUNCTION_INFO_V1(test_atomic_ops);
919 : Datum
920 6 : test_atomic_ops(PG_FUNCTION_ARGS)
921 : {
922 6 : test_atomic_flag();
923 :
924 6 : test_atomic_uint32();
925 :
926 6 : test_atomic_uint64();
927 :
928 : /*
929 : * Arguably this shouldn't be tested as part of this function, but it's
930 : * closely enough related that that seems ok for now.
931 : */
932 6 : test_spinlock();
933 :
934 6 : PG_RETURN_BOOL(true);
935 : }
936 :
937 10 : PG_FUNCTION_INFO_V1(test_fdw_handler);
938 : Datum
939 0 : test_fdw_handler(PG_FUNCTION_ARGS)
940 : {
941 0 : elog(ERROR, "test_fdw_handler is not implemented");
942 : PG_RETURN_NULL();
943 : }
944 :
945 16 : PG_FUNCTION_INFO_V1(test_support_func);
946 : Datum
947 60 : test_support_func(PG_FUNCTION_ARGS)
948 : {
949 60 : Node *rawreq = (Node *) PG_GETARG_POINTER(0);
950 60 : Node *ret = NULL;
951 :
952 60 : if (IsA(rawreq, SupportRequestSelectivity))
953 : {
954 : /*
955 : * Assume that the target is int4eq; that's safe as long as we don't
956 : * attach this to any other boolean-returning function.
957 : */
958 6 : SupportRequestSelectivity *req = (SupportRequestSelectivity *) rawreq;
959 : Selectivity s1;
960 :
961 6 : if (req->is_join)
962 0 : s1 = join_selectivity(req->root, Int4EqualOperator,
963 : req->args,
964 : req->inputcollid,
965 : req->jointype,
966 0 : req->sjinfo);
967 : else
968 6 : s1 = restriction_selectivity(req->root, Int4EqualOperator,
969 : req->args,
970 : req->inputcollid,
971 : req->varRelid);
972 :
973 6 : req->selectivity = s1;
974 6 : ret = (Node *) req;
975 : }
976 :
977 60 : if (IsA(rawreq, SupportRequestCost))
978 : {
979 : /* Provide some generic estimate */
980 18 : SupportRequestCost *req = (SupportRequestCost *) rawreq;
981 :
982 18 : req->startup = 0;
983 18 : req->per_tuple = 2 * cpu_operator_cost;
984 18 : ret = (Node *) req;
985 : }
986 :
987 60 : if (IsA(rawreq, SupportRequestRows))
988 : {
989 : /*
990 : * Assume that the target is generate_series_int4; that's safe as long
991 : * as we don't attach this to any other set-returning function.
992 : */
993 12 : SupportRequestRows *req = (SupportRequestRows *) rawreq;
994 :
995 12 : if (req->node && IsA(req->node, FuncExpr)) /* be paranoid */
996 : {
997 12 : List *args = ((FuncExpr *) req->node)->args;
998 12 : Node *arg1 = linitial(args);
999 12 : Node *arg2 = lsecond(args);
1000 :
1001 12 : if (IsA(arg1, Const) &&
1002 12 : !((Const *) arg1)->constisnull &&
1003 12 : IsA(arg2, Const) &&
1004 12 : !((Const *) arg2)->constisnull)
1005 : {
1006 12 : int32 val1 = DatumGetInt32(((Const *) arg1)->constvalue);
1007 12 : int32 val2 = DatumGetInt32(((Const *) arg2)->constvalue);
1008 :
1009 12 : req->rows = val2 - val1 + 1;
1010 12 : ret = (Node *) req;
1011 : }
1012 : }
1013 : }
1014 :
1015 60 : PG_RETURN_POINTER(ret);
1016 : }
1017 :
1018 10 : PG_FUNCTION_INFO_V1(test_opclass_options_func);
1019 : Datum
1020 0 : test_opclass_options_func(PG_FUNCTION_ARGS)
1021 : {
1022 0 : PG_RETURN_NULL();
1023 : }
1024 :
1025 : /* one-time tests for encoding infrastructure */
1026 16 : PG_FUNCTION_INFO_V1(test_enc_setup);
1027 : Datum
1028 6 : test_enc_setup(PG_FUNCTION_ARGS)
1029 : {
1030 : /* Test pg_encoding_set_invalid() */
1031 258 : for (int i = 0; i < _PG_LAST_ENCODING_; i++)
1032 : {
1033 : char buf[2],
1034 : bigbuf[16];
1035 : int len,
1036 : mblen,
1037 : valid;
1038 :
1039 252 : if (pg_encoding_max_length(i) == 1)
1040 168 : continue;
1041 84 : pg_encoding_set_invalid(i, buf);
1042 84 : len = strnlen(buf, 2);
1043 84 : if (len != 2)
1044 0 : elog(WARNING,
1045 : "official invalid string for encoding \"%s\" has length %d",
1046 : pg_enc2name_tbl[i].name, len);
1047 84 : mblen = pg_encoding_mblen(i, buf);
1048 84 : if (mblen != 2)
1049 0 : elog(WARNING,
1050 : "official invalid string for encoding \"%s\" has mblen %d",
1051 : pg_enc2name_tbl[i].name, mblen);
1052 84 : valid = pg_encoding_verifymbstr(i, buf, len);
1053 84 : if (valid != 0)
1054 0 : elog(WARNING,
1055 : "official invalid string for encoding \"%s\" has valid prefix of length %d",
1056 : pg_enc2name_tbl[i].name, valid);
1057 84 : valid = pg_encoding_verifymbstr(i, buf, 1);
1058 84 : if (valid != 0)
1059 0 : elog(WARNING,
1060 : "first byte of official invalid string for encoding \"%s\" has valid prefix of length %d",
1061 : pg_enc2name_tbl[i].name, valid);
1062 84 : memset(bigbuf, ' ', sizeof(bigbuf));
1063 84 : bigbuf[0] = buf[0];
1064 84 : bigbuf[1] = buf[1];
1065 84 : valid = pg_encoding_verifymbstr(i, bigbuf, sizeof(bigbuf));
1066 84 : if (valid != 0)
1067 0 : elog(WARNING,
1068 : "trailing data changed official invalid string for encoding \"%s\" to have valid prefix of length %d",
1069 : pg_enc2name_tbl[i].name, valid);
1070 : }
1071 :
1072 6 : PG_RETURN_VOID();
1073 : }
1074 :
1075 : /*
1076 : * Call an encoding conversion or verification function.
1077 : *
1078 : * Arguments:
1079 : * string bytea -- string to convert
1080 : * src_enc name -- source encoding
1081 : * dest_enc name -- destination encoding
1082 : * noError bool -- if set, don't ereport() on invalid or untranslatable
1083 : * input
1084 : *
1085 : * Result is a tuple with two attributes:
1086 : * int4 -- number of input bytes successfully converted
1087 : * bytea -- converted string
1088 : */
1089 16 : PG_FUNCTION_INFO_V1(test_enc_conversion);
1090 : Datum
1091 9798 : test_enc_conversion(PG_FUNCTION_ARGS)
1092 : {
1093 9798 : bytea *string = PG_GETARG_BYTEA_PP(0);
1094 9798 : char *src_encoding_name = NameStr(*PG_GETARG_NAME(1));
1095 9798 : int src_encoding = pg_char_to_encoding(src_encoding_name);
1096 9798 : char *dest_encoding_name = NameStr(*PG_GETARG_NAME(2));
1097 9798 : int dest_encoding = pg_char_to_encoding(dest_encoding_name);
1098 9798 : bool noError = PG_GETARG_BOOL(3);
1099 : TupleDesc tupdesc;
1100 : char *src;
1101 : char *dst;
1102 : bytea *retval;
1103 : Size srclen;
1104 : Size dstsize;
1105 : Oid proc;
1106 : int convertedbytes;
1107 : int dstlen;
1108 : Datum values[2];
1109 9798 : bool nulls[2] = {0};
1110 : HeapTuple tuple;
1111 :
1112 9798 : if (src_encoding < 0)
1113 0 : ereport(ERROR,
1114 : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1115 : errmsg("invalid source encoding name \"%s\"",
1116 : src_encoding_name)));
1117 9798 : if (dest_encoding < 0)
1118 0 : ereport(ERROR,
1119 : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1120 : errmsg("invalid destination encoding name \"%s\"",
1121 : dest_encoding_name)));
1122 :
1123 : /* Build a tuple descriptor for our result type */
1124 9798 : if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
1125 0 : elog(ERROR, "return type must be a row type");
1126 9798 : tupdesc = BlessTupleDesc(tupdesc);
1127 :
1128 9798 : srclen = VARSIZE_ANY_EXHDR(string);
1129 9798 : src = VARDATA_ANY(string);
1130 :
1131 9798 : if (src_encoding == dest_encoding)
1132 : {
1133 : /* just check that the source string is valid */
1134 : int oklen;
1135 :
1136 4092 : oklen = pg_encoding_verifymbstr(src_encoding, src, srclen);
1137 :
1138 4092 : if (oklen == srclen)
1139 : {
1140 1032 : convertedbytes = oklen;
1141 1032 : retval = string;
1142 : }
1143 3060 : else if (!noError)
1144 : {
1145 1530 : report_invalid_encoding(src_encoding, src + oklen, srclen - oklen);
1146 : }
1147 : else
1148 : {
1149 : /*
1150 : * build bytea data type structure.
1151 : */
1152 : Assert(oklen < srclen);
1153 1530 : convertedbytes = oklen;
1154 1530 : retval = (bytea *) palloc(oklen + VARHDRSZ);
1155 1530 : SET_VARSIZE(retval, oklen + VARHDRSZ);
1156 1530 : memcpy(VARDATA(retval), src, oklen);
1157 : }
1158 : }
1159 : else
1160 : {
1161 5706 : proc = FindDefaultConversionProc(src_encoding, dest_encoding);
1162 5706 : if (!OidIsValid(proc))
1163 0 : ereport(ERROR,
1164 : (errcode(ERRCODE_UNDEFINED_FUNCTION),
1165 : errmsg("default conversion function for encoding \"%s\" to \"%s\" does not exist",
1166 : pg_encoding_to_char(src_encoding),
1167 : pg_encoding_to_char(dest_encoding))));
1168 :
1169 5706 : if (srclen >= (MaxAllocSize / (Size) MAX_CONVERSION_GROWTH))
1170 0 : ereport(ERROR,
1171 : (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
1172 : errmsg("out of memory"),
1173 : errdetail("String of %d bytes is too long for encoding conversion.",
1174 : (int) srclen)));
1175 :
1176 5706 : dstsize = (Size) srclen * MAX_CONVERSION_GROWTH + 1;
1177 5706 : dst = MemoryContextAlloc(CurrentMemoryContext, dstsize);
1178 :
1179 : /* perform conversion */
1180 5706 : convertedbytes = pg_do_encoding_conversion_buf(proc,
1181 : src_encoding,
1182 : dest_encoding,
1183 : (unsigned char *) src, srclen,
1184 : (unsigned char *) dst, dstsize,
1185 : noError);
1186 3366 : dstlen = strlen(dst);
1187 :
1188 : /*
1189 : * build bytea data type structure.
1190 : */
1191 3366 : retval = (bytea *) palloc(dstlen + VARHDRSZ);
1192 3366 : SET_VARSIZE(retval, dstlen + VARHDRSZ);
1193 3366 : memcpy(VARDATA(retval), dst, dstlen);
1194 :
1195 3366 : pfree(dst);
1196 : }
1197 :
1198 5928 : values[0] = Int32GetDatum(convertedbytes);
1199 5928 : values[1] = PointerGetDatum(retval);
1200 5928 : tuple = heap_form_tuple(tupdesc, values, nulls);
1201 :
1202 5928 : PG_RETURN_DATUM(HeapTupleGetDatum(tuple));
1203 : }
1204 :
1205 : /* Provide SQL access to IsBinaryCoercible() */
1206 16 : PG_FUNCTION_INFO_V1(binary_coercible);
1207 : Datum
1208 37572 : binary_coercible(PG_FUNCTION_ARGS)
1209 : {
1210 37572 : Oid srctype = PG_GETARG_OID(0);
1211 37572 : Oid targettype = PG_GETARG_OID(1);
1212 :
1213 37572 : PG_RETURN_BOOL(IsBinaryCoercible(srctype, targettype));
1214 : }
1215 :
1216 : /*
1217 : * Sanity checks for functions in relpath.h
1218 : */
1219 16 : PG_FUNCTION_INFO_V1(test_relpath);
1220 : Datum
1221 6 : test_relpath(PG_FUNCTION_ARGS)
1222 : {
1223 : RelPathStr rpath;
1224 :
1225 : /*
1226 : * Verify that PROCNUMBER_CHARS and MAX_BACKENDS stay in sync.
1227 : * Unfortunately I don't know how to express that in a way suitable for a
1228 : * static assert.
1229 : */
1230 : if ((int) ceil(log10(MAX_BACKENDS)) != PROCNUMBER_CHARS)
1231 : elog(WARNING, "mismatch between MAX_BACKENDS and PROCNUMBER_CHARS");
1232 :
1233 : /* verify that the max-length relpath is generated ok */
1234 6 : rpath = GetRelationPath(OID_MAX, OID_MAX, OID_MAX, MAX_BACKENDS - 1,
1235 : INIT_FORKNUM);
1236 :
1237 6 : if (strlen(rpath.str) != REL_PATH_STR_MAXLEN)
1238 0 : elog(WARNING, "maximum length relpath is if length %zu instead of %zu",
1239 : strlen(rpath.str), REL_PATH_STR_MAXLEN);
1240 :
1241 6 : PG_RETURN_VOID();
1242 : }
|