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