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