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 : :
461 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. */
9353 99 : 9 : PG_FUNCTION_INFO_V1(interpt_pp);
100 : :
101 : : Datum
9466 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 : :
10523 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 : : {
9466 tgl@sss.pgh.pa.us 116 : 8180 : regress_lseg_construct(&seg1, &p1->p[i], &p1->p[i + 1]);
10523 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]);
9466 tgl@sss.pgh.pa.us 120 [ + + ]: 16912 : if (DatumGetBool(DirectFunctionCall2(lseg_intersect,
121 : : LsegPGetDatum(&seg1),
122 : : LsegPGetDatum(&seg2))))
10523 bruce@momjian.us 123 : 3576 : found = true;
124 : : }
125 : : }
126 : :
9466 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
8503 bruce@momjian.us 143 : 25092 : regress_lseg_construct(LSEG *lseg, Point *pt1, Point *pt2)
144 : : {
10523 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;
10948 scrappy@hub.org 149 : 25092 : }
150 : :
9353 tgl@sss.pgh.pa.us 151 : 9 : PG_FUNCTION_INFO_V1(overpaid);
152 : :
153 : : Datum
9521 154 : 24 : overpaid(PG_FUNCTION_ARGS)
155 : : {
8125 156 : 24 : HeapTupleHeader tuple = PG_GETARG_HEAPTUPLEHEADER(0);
157 : : bool isnull;
158 : : int32 salary;
159 : :
9441 160 : 24 : salary = DatumGetInt32(GetAttributeByName(tuple, "salary", &isnull));
9521 161 [ - + ]: 24 : if (isnull)
9521 tgl@sss.pgh.pa.us 162 :UBC 0 : PG_RETURN_NULL();
9521 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 : :
3380 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 : :
10948 scrappy@hub.org 192 [ + + + + : 252 : for (i = 0, p = str; *p && i < NARGS && *p != RDELIM; p++)
+ + ]
193 : : {
3825 tgl@sss.pgh.pa.us 194 [ + + + + : 208 : if (*p == DELIM || (*p == LDELIM && i == 0))
+ - ]
10948 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 : : */
3825 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 : :
203 michael@paquier.xyz 209 :GNC 28 : result = palloc_object(WIDGET);
10948 scrappy@hub.org 210 :CBC 28 : result->center.x = atof(coord[0]);
211 : 28 : result->center.y = atof(coord[1]);
212 : 28 : result->radius = atof(coord[2]);
213 : :
3380 andres@anarazel.de 214 : 28 : PG_RETURN_POINTER(result);
215 : : }
216 : :
217 : : Datum
218 : 8 : widget_out(PG_FUNCTION_ARGS)
219 : : {
3331 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 : :
3380 andres@anarazel.de 224 : 8 : PG_RETURN_CSTRING(str);
225 : : }
226 : :
9353 tgl@sss.pgh.pa.us 227 : 9 : PG_FUNCTION_INFO_V1(pt_in_widget);
228 : :
229 : : Datum
9513 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 : :
2893 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 : :
3380 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 : :
8374 tgl@sss.pgh.pa.us 253 : 32 : new_string = palloc0(NAMEDATALEN);
10207 bruce@momjian.us 254 [ + - + + ]: 224 : for (i = 0; i < NAMEDATALEN && string[i]; ++i)
255 : : ;
256 [ + - + - ]: 32 : if (i == NAMEDATALEN || !string[i])
10523 257 : 32 : --i;
258 : 32 : len = i;
259 [ + + ]: 224 : for (; i >= 0; --i)
260 : 192 : new_string[len - i] = string[i];
3380 andres@anarazel.de 261 : 32 : PG_RETURN_CSTRING(new_string);
262 : : }
263 : :
3045 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))
3045 tgl@sss.pgh.pa.us 273 [ # # ]:UBC 0 : elog(ERROR, "trigger_return_old: not fired by trigger manager");
274 : :
3045 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 : : {
8713 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 : : */
3045 315 : 13 : PG_FUNCTION_INFO_V1(int44out);
316 : :
317 : : Datum
318 : 16 : int44out(PG_FUNCTION_ARGS)
319 : : {
8713 320 : 16 : int32 *an_array = (int32 *) PG_GETARG_POINTER(0);
3045 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 : :
8713 329 : 16 : PG_RETURN_CSTRING(result);
330 : : }
331 : :
1611 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 : :
4746 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 */
3236 andres@anarazel.de 388 [ + - ]: 336 : if (TupleDescAttr(tupdesc, i)->attisdropped ||
4746 rhaas@postgresql.org 389 [ + + ]: 336 : nulls[i] ||
743 michael@paquier.xyz 390 [ + + ]: 292 : TupleDescAttr(tupdesc, i)->attlen != -1 ||
391 [ - + ]: 208 : TupleDescAttr(tupdesc, i)->attstorage == TYPSTORAGE_PLAIN)
4746 rhaas@postgresql.org 392 : 128 : continue;
393 : :
139 michael@paquier.xyz 394 :GNC 208 : attr = (varlena *) DatumGetPointer(values[i]);
395 : :
396 : : /* don't recursively indirect */
4746 rhaas@postgresql.org 397 [ - + - - ]:CBC 208 : if (VARATT_IS_EXTERNAL_INDIRECT(attr))
4746 rhaas@postgresql.org 398 :UBC 0 : continue;
399 : :
400 : : /* copy datum, so it still lives later */
4746 rhaas@postgresql.org 401 [ - + - - ]:CBC 208 : if (VARATT_IS_EXTERNAL_ONDISK(attr))
2461 rhaas@postgresql.org 402 :UBC 0 : attr = detoast_external_attr(attr);
403 : : else
404 : : {
139 michael@paquier.xyz 405 :GNC 208 : varlena *oldattr = attr;
406 : :
4746 rhaas@postgresql.org 407 [ - + - - :CBC 208 : attr = palloc0(VARSIZE_ANY(oldattr));
- - - - +
+ ]
408 [ - + - - : 208 : memcpy(attr, oldattr, VARSIZE_ANY(oldattr));
- - - - +
+ ]
409 : : }
410 : :
411 : : /* build indirection Datum */
139 michael@paquier.xyz 412 :GNC 208 : new_attr = (varlena *) palloc0(INDIRECT_POINTER_SIZE);
4746 rhaas@postgresql.org 413 :CBC 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 : : */
4443 tgl@sss.pgh.pa.us 437 : 84 : PG_RETURN_POINTER(newtup->t_data);
438 : : }
439 : :
596 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 : :
2008 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 : :
4361 noah@leadboat.com 473 [ - + ]: 1 : if (!superuser())
4361 noah@leadboat.com 474 [ # # ]:UBC 0 : elog(ERROR, "must be superuser to change environment variables");
475 : :
2008 tgl@sss.pgh.pa.us 476 [ - + ]:CBC 1 : if (setenv(envvar, envval, 1) != 0)
4361 noah@leadboat.com 477 [ # # ]:UBC 0 : elog(ERROR, "could not set environment variable: %m");
478 : :
4361 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())
4361 noah@leadboat.com 491 [ # # ]:UBC 0 : elog(ERROR, "must be superuser to check PID liveness");
492 : :
4361 noah@leadboat.com 493 [ + + ]:CBC 6 : while (kill(pid, 0) == 0)
494 : : {
4133 495 [ - + ]: 4 : CHECK_FOR_INTERRUPTS();
4361 496 : 4 : pg_usleep(50000);
497 : : }
498 : :
499 [ - + ]: 2 : if (errno != ESRCH)
4361 noah@leadboat.com 500 [ # # ]:UBC 0 : elog(ERROR, "could not check PID %d liveness: %m", pid);
501 : :
4361 noah@leadboat.com 502 :CBC 2 : PG_RETURN_VOID();
503 : : }
504 : :
505 : : static void
4296 andres@anarazel.de 506 : 4 : test_atomic_flag(void)
507 : : {
508 : : pg_atomic_flag flag;
509 : :
510 : 4 : pg_atomic_init_flag(&flag);
2460 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));
4296 andres@anarazel.de 515 : 4 : pg_atomic_clear_flag(&flag);
2460 noah@leadboat.com 516 [ - + - - ]: 4 : EXPECT_TRUE(pg_atomic_unlocked_test_flag(&flag));
517 [ - + - - ]: 4 : EXPECT_TRUE(pg_atomic_test_set_flag(&flag));
4296 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);
2460 noah@leadboat.com 529 [ - + - - ]: 4 : EXPECT_EQ_U32(pg_atomic_read_u32(&var), 0);
4296 andres@anarazel.de 530 : 4 : pg_atomic_write_u32(&var, 3);
2460 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);
2482 543 : 4 : pg_atomic_fetch_add_u32(&var, 2); /* wrap to 0 */
2460 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);
4056 bruce@momjian.us 551 : 4 : pg_atomic_fetch_add_u32(&var, 1); /* top up to UINT_MAX */
2460 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);
4296 andres@anarazel.de 556 : 4 : pg_atomic_sub_fetch_u32(&var, 1);
2088 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 */
4296 andres@anarazel.de 567 : 4 : expected = 10;
2460 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 */
4296 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)
4296 andres@anarazel.de 578 [ # # ]:UBC 0 : elog(ERROR, "atomic_compare_exchange_u32() never succeeded");
2460 noah@leadboat.com 579 [ - + - - ]:CBC 4 : EXPECT_EQ_U32(pg_atomic_read_u32(&var), 1);
4296 andres@anarazel.de 580 : 4 : pg_atomic_write_u32(&var, 0);
581 : :
582 : : /* try setting flagbits */
2460 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);
4296 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);
2460 noah@leadboat.com 601 [ - + - - ]: 4 : EXPECT_EQ_U64(pg_atomic_read_u64(&var), 0);
4296 andres@anarazel.de 602 : 4 : pg_atomic_write_u64(&var, 3);
2460 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 */
4296 andres@anarazel.de 613 : 4 : expected = 10;
2460 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 */
4296 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)
4296 andres@anarazel.de 624 [ # # ]:UBC 0 : elog(ERROR, "atomic_compare_exchange_u64() never succeeded");
2460 noah@leadboat.com 625 [ - + - - ]:CBC 4 : EXPECT_EQ_U64(pg_atomic_read_u64(&var), 1);
626 : :
4296 andres@anarazel.de 627 : 4 : pg_atomic_write_u64(&var, 0);
628 : :
629 : : /* try setting flagbits */
2460 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);
4296 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
2213 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))
2213 andres@anarazel.de 688 [ # # ]:UBC 0 : elog(ERROR, "acquired already held spinlock");
689 : :
690 : : #ifdef TAS_SPIN
2213 andres@anarazel.de 691 [ - + - - ]:CBC 4 : if (!TAS_SPIN(&struct_w_lock.lock))
2213 andres@anarazel.de 692 [ # # ]:UBC 0 : elog(ERROR, "acquired already held spinlock");
693 : : #endif /* defined(TAS_SPIN) */
694 : :
2213 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)
2213 andres@anarazel.de 703 [ # # ]:UBC 0 : elog(ERROR, "padding before spinlock modified");
2213 andres@anarazel.de 704 [ - + ]:CBC 4 : if (memcmp(struct_w_lock.data_after, "ef12", 4) != 0)
2213 andres@anarazel.de 705 [ # # ]:UBC 0 : elog(ERROR, "padding after spinlock modified");
706 : : }
2213 andres@anarazel.de 707 :CBC 4 : }
708 : :
4296 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 : : */
2213 723 : 4 : test_spinlock();
724 : :
4296 725 : 4 : PG_RETURN_BOOL(true);
726 : : }
727 : :
3210 rhaas@postgresql.org 728 : 5 : PG_FUNCTION_INFO_V1(test_fdw_handler);
729 : : Datum
3210 rhaas@postgresql.org 730 :UBC 0 : test_fdw_handler(PG_FUNCTION_ARGS)
731 : : {
3045 tgl@sss.pgh.pa.us 732 [ # # ]: 0 : elog(ERROR, "test_fdw_handler is not implemented");
733 : : PG_RETURN_NULL();
734 : : }
735 : :
116 jdavis@postgresql.or 736 :GNC 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. */
13 741 : 12 : GetUserMapping(PG_GETARG_OID(0), PG_GETARG_OID(1));
116 742 : 8 : PG_RETURN_TEXT_P(cstring_to_text("dbname=regress_doesnotexist user=doesnotexist password=secret"));
743 : : }
744 : :
439 noah@leadboat.com 745 :CBC 9 : PG_FUNCTION_INFO_V1(is_catalog_text_unique_index_oid);
746 : : Datum
747 : 879 : is_catalog_text_unique_index_oid(PG_FUNCTION_ARGS)
748 : : {
326 peter@eisentraut.org 749 :GNC 879 : return BoolGetDatum(IsCatalogTextUniqueIndexOid(PG_GETARG_OID(0)));
750 : : }
751 : :
2698 tgl@sss.pgh.pa.us 752 :CBC 9 : PG_FUNCTION_INFO_V1(test_support_func);
753 : : Datum
754 : 60 : test_support_func(PG_FUNCTION_ARGS)
755 : : {
756 : 60 : Node *rawreq = (Node *) PG_GETARG_POINTER(0);
757 : 60 : Node *ret = NULL;
758 : :
759 [ + + ]: 60 : if (IsA(rawreq, SupportRequestSelectivity))
760 : : {
761 : : /*
762 : : * Assume that the target is int4eq; that's safe as long as we don't
763 : : * attach this to any other boolean-returning function.
764 : : */
765 : 5 : SupportRequestSelectivity *req = (SupportRequestSelectivity *) rawreq;
766 : : Selectivity s1;
767 : :
768 [ - + ]: 5 : if (req->is_join)
2698 tgl@sss.pgh.pa.us 769 :UBC 0 : s1 = join_selectivity(req->root, Int4EqualOperator,
770 : : req->args,
771 : : req->inputcollid,
772 : : req->jointype,
2698 tgl@sss.pgh.pa.us 773 :EUB : req->sjinfo);
774 : : else
2698 tgl@sss.pgh.pa.us 775 :CBC 5 : s1 = restriction_selectivity(req->root, Int4EqualOperator,
776 : : req->args,
777 : : req->inputcollid,
778 : : req->varRelid);
779 : :
780 : 5 : req->selectivity = s1;
781 : 5 : ret = (Node *) req;
782 : : }
783 : :
784 [ + + ]: 60 : if (IsA(rawreq, SupportRequestCost))
785 : : {
786 : : /* Provide some generic estimate */
787 : 15 : SupportRequestCost *req = (SupportRequestCost *) rawreq;
788 : :
789 : 15 : req->startup = 0;
790 : 15 : req->per_tuple = 2 * cpu_operator_cost;
791 : 15 : ret = (Node *) req;
792 : : }
793 : :
794 [ + + ]: 60 : if (IsA(rawreq, SupportRequestRows))
795 : : {
796 : : /*
797 : : * Assume that the target is generate_series_int4; that's safe as long
798 : : * as we don't attach this to any other set-returning function.
799 : : */
800 : 10 : SupportRequestRows *req = (SupportRequestRows *) rawreq;
801 : :
802 [ + - + - ]: 10 : if (req->node && IsA(req->node, FuncExpr)) /* be paranoid */
803 : : {
804 : 10 : List *args = ((FuncExpr *) req->node)->args;
805 : 10 : Node *arg1 = linitial(args);
806 : 10 : Node *arg2 = lsecond(args);
807 : :
808 [ + - ]: 10 : if (IsA(arg1, Const) &&
809 [ + - ]: 10 : !((Const *) arg1)->constisnull &&
810 [ + - ]: 10 : IsA(arg2, Const) &&
811 [ + - ]: 10 : !((Const *) arg2)->constisnull)
812 : : {
813 : 10 : int32 val1 = DatumGetInt32(((Const *) arg1)->constvalue);
814 : 10 : int32 val2 = DatumGetInt32(((Const *) arg2)->constvalue);
815 : :
816 : 10 : req->rows = val2 - val1 + 1;
817 : 10 : ret = (Node *) req;
818 : : }
819 : : }
820 : : }
821 : :
822 : 60 : PG_RETURN_POINTER(ret);
823 : : }
824 : :
220 tgl@sss.pgh.pa.us 825 :GNC 9 : PG_FUNCTION_INFO_V1(test_inline_in_from_support_func);
826 : : Datum
827 : 40 : test_inline_in_from_support_func(PG_FUNCTION_ARGS)
828 : : {
829 : 40 : Node *rawreq = (Node *) PG_GETARG_POINTER(0);
830 : :
831 [ + + ]: 40 : if (IsA(rawreq, SupportRequestInlineInFrom))
832 : : {
833 : : /*
834 : : * Assume that the target is foo_from_bar; that's safe as long as we
835 : : * don't attach this to any other function.
836 : : */
837 : 20 : SupportRequestInlineInFrom *req = (SupportRequestInlineInFrom *) rawreq;
838 : : StringInfoData sql;
839 : 20 : RangeTblFunction *rtfunc = req->rtfunc;
840 : 20 : FuncExpr *expr = (FuncExpr *) rtfunc->funcexpr;
841 : : Node *node;
842 : : Const *c;
843 : : char *colname;
844 : : char *tablename;
845 : : SQLFunctionParseInfoPtr pinfo;
846 : : List *raw_parsetree_list;
847 : : List *querytree_list;
848 : : Query *querytree;
849 : :
850 [ - + ]: 20 : if (list_length(expr->args) != 3)
851 : : {
220 tgl@sss.pgh.pa.us 852 [ # # ]:UNC 0 : ereport(WARNING, (errmsg("test_inline_in_from_support_func called with %d args but expected 3", list_length(expr->args))));
853 : 0 : PG_RETURN_POINTER(NULL);
854 : : }
855 : :
856 : : /* Get colname */
220 tgl@sss.pgh.pa.us 857 :GNC 20 : node = linitial(expr->args);
858 [ - + ]: 20 : if (!IsA(node, Const))
859 : : {
220 tgl@sss.pgh.pa.us 860 [ # # ]:UNC 0 : ereport(WARNING, (errmsg("test_inline_in_from_support_func called with non-Const parameters")));
861 : 0 : PG_RETURN_POINTER(NULL);
862 : : }
863 : :
220 tgl@sss.pgh.pa.us 864 :GNC 20 : c = (Const *) node;
865 [ + - - + ]: 20 : if (c->consttype != TEXTOID || c->constisnull)
866 : : {
220 tgl@sss.pgh.pa.us 867 [ # # ]:UNC 0 : ereport(WARNING, (errmsg("test_inline_in_from_support_func called with non-TEXT parameters")));
868 : 0 : PG_RETURN_POINTER(NULL);
869 : : }
220 tgl@sss.pgh.pa.us 870 :GNC 20 : colname = TextDatumGetCString(c->constvalue);
871 : :
872 : : /* Get tablename */
873 : 20 : node = lsecond(expr->args);
874 [ - + ]: 20 : if (!IsA(node, Const))
875 : : {
220 tgl@sss.pgh.pa.us 876 [ # # ]:UNC 0 : ereport(WARNING, (errmsg("test_inline_in_from_support_func called with non-Const parameters")));
877 : 0 : PG_RETURN_POINTER(NULL);
878 : : }
879 : :
220 tgl@sss.pgh.pa.us 880 :GNC 20 : c = (Const *) node;
881 [ + - - + ]: 20 : if (c->consttype != TEXTOID || c->constisnull)
882 : : {
220 tgl@sss.pgh.pa.us 883 [ # # ]:UNC 0 : ereport(WARNING, (errmsg("test_inline_in_from_support_func called with non-TEXT parameters")));
884 : 0 : PG_RETURN_POINTER(NULL);
885 : : }
220 tgl@sss.pgh.pa.us 886 :GNC 20 : tablename = TextDatumGetCString(c->constvalue);
887 : :
888 : : /* Begin constructing replacement SELECT query. */
889 : 20 : initStringInfo(&sql);
890 : 20 : appendStringInfo(&sql, "SELECT %s::text FROM %s",
891 : : quote_identifier(colname),
892 : : quote_identifier(tablename));
893 : :
894 : : /* Add filter expression if present. */
895 : 20 : node = lthird(expr->args);
896 [ + - + + ]: 20 : if (!(IsA(node, Const) && ((Const *) node)->constisnull))
897 : : {
898 : : /*
899 : : * We only filter if $3 is not constant-NULL. This is not a very
900 : : * exact implementation of the PL/pgSQL original, but it's close
901 : : * enough for demonstration purposes.
902 : : */
903 : 10 : appendStringInfo(&sql, " WHERE %s::text = $3",
904 : : quote_identifier(colname));
905 : : }
906 : :
907 : : /* Build a SQLFunctionParseInfo with the parameters of my function. */
908 : 20 : pinfo = prepare_sql_fn_parse_info(req->proc,
909 : : (Node *) expr,
910 : : expr->inputcollid);
911 : :
912 : : /* Parse the generated SQL. */
913 : 20 : raw_parsetree_list = pg_parse_query(sql.data);
914 [ - + ]: 20 : if (list_length(raw_parsetree_list) != 1)
915 : : {
220 tgl@sss.pgh.pa.us 916 [ # # ]:UNC 0 : ereport(WARNING, (errmsg("test_inline_in_from_support_func parsed to more than one node")));
917 : 0 : PG_RETURN_POINTER(NULL);
918 : : }
919 : :
920 : : /* Analyze the parse tree as if it were a SQL-language body. */
220 tgl@sss.pgh.pa.us 921 :GNC 20 : querytree_list = pg_analyze_and_rewrite_withcb(linitial(raw_parsetree_list),
922 : 20 : sql.data,
923 : : (ParserSetupHook) sql_fn_parser_setup,
924 : : pinfo, NULL);
925 [ - + ]: 20 : if (list_length(querytree_list) != 1)
926 : : {
220 tgl@sss.pgh.pa.us 927 [ # # ]:UNC 0 : ereport(WARNING, (errmsg("test_inline_in_from_support_func rewrote to more than one node")));
928 : 0 : PG_RETURN_POINTER(NULL);
929 : : }
930 : :
220 tgl@sss.pgh.pa.us 931 :GNC 20 : querytree = linitial(querytree_list);
932 [ - + ]: 20 : if (!IsA(querytree, Query))
933 : : {
220 tgl@sss.pgh.pa.us 934 [ # # ]:UNC 0 : ereport(WARNING, (errmsg("test_inline_in_from_support_func didn't parse to a Query")));
935 : 0 : PG_RETURN_POINTER(NULL);
936 : : }
937 : :
220 tgl@sss.pgh.pa.us 938 :GNC 20 : PG_RETURN_POINTER(querytree);
939 : : }
940 : :
941 : 20 : PG_RETURN_POINTER(NULL);
942 : : }
943 : :
2283 akorotkov@postgresql 944 :CBC 5 : PG_FUNCTION_INFO_V1(test_opclass_options_func);
945 : : Datum
2283 akorotkov@postgresql 946 :UBC 0 : test_opclass_options_func(PG_FUNCTION_ARGS)
947 : : {
948 : 0 : PG_RETURN_NULL();
949 : : }
950 : :
951 : : /* one-time tests for encoding infrastructure */
505 andres@anarazel.de 952 :CBC 9 : PG_FUNCTION_INFO_V1(test_enc_setup);
953 : : Datum
954 : 4 : test_enc_setup(PG_FUNCTION_ARGS)
955 : : {
956 : : /* Test pg_encoding_set_invalid() */
957 [ + + ]: 172 : for (int i = 0; i < _PG_LAST_ENCODING_; i++)
958 : : {
959 : : char buf[2],
960 : : bigbuf[16];
961 : : int len,
962 : : mblen,
963 : : valid;
964 : :
83 tmunro@postgresql.or 965 [ + - + - :GNC 168 : if (!PG_VALID_ENCODING(i))
+ + ]
966 : 116 : continue;
505 andres@anarazel.de 967 [ + + ]:CBC 164 : if (pg_encoding_max_length(i) == 1)
968 : 112 : continue;
969 : 52 : pg_encoding_set_invalid(i, buf);
970 : 52 : len = strnlen(buf, 2);
971 [ - + ]: 52 : if (len != 2)
505 andres@anarazel.de 972 [ # # ]:UBC 0 : elog(WARNING,
973 : : "official invalid string for encoding \"%s\" has length %d",
974 : : pg_enc2name_tbl[i].name, len);
505 andres@anarazel.de 975 :CBC 52 : mblen = pg_encoding_mblen(i, buf);
976 [ - + ]: 52 : if (mblen != 2)
505 andres@anarazel.de 977 [ # # ]:UBC 0 : elog(WARNING,
978 : : "official invalid string for encoding \"%s\" has mblen %d",
979 : : pg_enc2name_tbl[i].name, mblen);
505 andres@anarazel.de 980 :CBC 52 : valid = pg_encoding_verifymbstr(i, buf, len);
981 [ - + ]: 52 : if (valid != 0)
505 andres@anarazel.de 982 [ # # ]:UBC 0 : elog(WARNING,
983 : : "official invalid string for encoding \"%s\" has valid prefix of length %d",
984 : : pg_enc2name_tbl[i].name, valid);
505 andres@anarazel.de 985 :CBC 52 : valid = pg_encoding_verifymbstr(i, buf, 1);
986 [ - + ]: 52 : if (valid != 0)
505 andres@anarazel.de 987 [ # # ]:UBC 0 : elog(WARNING,
988 : : "first byte of official invalid string for encoding \"%s\" has valid prefix of length %d",
989 : : pg_enc2name_tbl[i].name, valid);
505 andres@anarazel.de 990 :CBC 52 : memset(bigbuf, ' ', sizeof(bigbuf));
991 : 52 : bigbuf[0] = buf[0];
992 : 52 : bigbuf[1] = buf[1];
993 : 52 : valid = pg_encoding_verifymbstr(i, bigbuf, sizeof(bigbuf));
994 [ - + ]: 52 : if (valid != 0)
505 andres@anarazel.de 995 [ # # ]:UBC 0 : elog(WARNING,
996 : : "trailing data changed official invalid string for encoding \"%s\" to have valid prefix of length %d",
997 : : pg_enc2name_tbl[i].name, valid);
998 : : }
999 : :
505 andres@anarazel.de 1000 :CBC 4 : PG_RETURN_VOID();
1001 : : }
1002 : :
1003 : : /*
1004 : : * Call an encoding conversion or verification function.
1005 : : *
1006 : : * Arguments:
1007 : : * string bytea -- string to convert
1008 : : * src_enc name -- source encoding
1009 : : * dest_enc name -- destination encoding
1010 : : * noError bool -- if set, don't ereport() on invalid or untranslatable
1011 : : * input
1012 : : *
1013 : : * Result is a tuple with two attributes:
1014 : : * int4 -- number of input bytes successfully converted
1015 : : * bytea -- converted string
1016 : : */
1916 heikki.linnakangas@i 1017 : 9 : PG_FUNCTION_INFO_V1(test_enc_conversion);
1018 : : Datum
1019 : 5152 : test_enc_conversion(PG_FUNCTION_ARGS)
1020 : : {
1021 : 5152 : bytea *string = PG_GETARG_BYTEA_PP(0);
1022 : 5152 : char *src_encoding_name = NameStr(*PG_GETARG_NAME(1));
1023 : 5152 : int src_encoding = pg_char_to_encoding(src_encoding_name);
1024 : 5152 : char *dest_encoding_name = NameStr(*PG_GETARG_NAME(2));
1025 : 5152 : int dest_encoding = pg_char_to_encoding(dest_encoding_name);
1026 : 5152 : bool noError = PG_GETARG_BOOL(3);
1027 : : TupleDesc tupdesc;
1028 : : char *src;
1029 : : char *dst;
1030 : : bytea *retval;
1031 : : Size srclen;
1032 : : Size dstsize;
1033 : : Oid proc;
1034 : : int convertedbytes;
1035 : : int dstlen;
1036 : : Datum values[2];
1445 peter@eisentraut.org 1037 : 5152 : bool nulls[2] = {0};
1038 : : HeapTuple tuple;
1039 : :
1916 heikki.linnakangas@i 1040 [ - + ]: 5152 : if (src_encoding < 0)
1916 heikki.linnakangas@i 1041 [ # # ]:UBC 0 : ereport(ERROR,
1042 : : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1043 : : errmsg("invalid source encoding name \"%s\"",
1044 : : src_encoding_name)));
1916 heikki.linnakangas@i 1045 [ - + ]:CBC 5152 : if (dest_encoding < 0)
1916 heikki.linnakangas@i 1046 [ # # ]:UBC 0 : ereport(ERROR,
1047 : : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1048 : : errmsg("invalid destination encoding name \"%s\"",
1049 : : dest_encoding_name)));
1050 : :
1051 : : /* Build a tuple descriptor for our result type */
1916 heikki.linnakangas@i 1052 [ - + ]:CBC 5152 : if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
1916 heikki.linnakangas@i 1053 [ # # ]:UBC 0 : elog(ERROR, "return type must be a row type");
1916 heikki.linnakangas@i 1054 :CBC 5152 : tupdesc = BlessTupleDesc(tupdesc);
1055 : :
1056 [ - + - - : 5152 : srclen = VARSIZE_ANY_EXHDR(string);
- - - - +
+ ]
1057 [ + + ]: 5152 : src = VARDATA_ANY(string);
1058 : :
1059 [ + + ]: 5152 : if (src_encoding == dest_encoding)
1060 : : {
1061 : : /* just check that the source string is valid */
1062 : : int oklen;
1063 : :
1064 : 2572 : oklen = pg_encoding_verifymbstr(src_encoding, src, srclen);
1065 : :
1066 [ + + ]: 2572 : if (oklen == srclen)
1067 : : {
1068 : 652 : convertedbytes = oklen;
1069 : 652 : retval = string;
1070 : : }
1071 [ + + ]: 1920 : else if (!noError)
1072 : : {
1073 : 960 : report_invalid_encoding(src_encoding, src + oklen, srclen - oklen);
1074 : : }
1075 : : else
1076 : : {
1077 : : /*
1078 : : * build bytea data type structure.
1079 : : */
1080 [ - + ]: 960 : Assert(oklen < srclen);
1081 : 960 : convertedbytes = oklen;
1082 : 960 : retval = (bytea *) palloc(oklen + VARHDRSZ);
1083 : 960 : SET_VARSIZE(retval, oklen + VARHDRSZ);
1084 : 960 : memcpy(VARDATA(retval), src, oklen);
1085 : : }
1086 : : }
1087 : : else
1088 : : {
1089 : 2580 : proc = FindDefaultConversionProc(src_encoding, dest_encoding);
1090 [ - + ]: 2580 : if (!OidIsValid(proc))
1916 heikki.linnakangas@i 1091 [ # # ]:UBC 0 : ereport(ERROR,
1092 : : (errcode(ERRCODE_UNDEFINED_FUNCTION),
1093 : : errmsg("default conversion function for encoding \"%s\" to \"%s\" does not exist",
1094 : : pg_encoding_to_char(src_encoding),
1095 : : pg_encoding_to_char(dest_encoding))));
1096 : :
1916 heikki.linnakangas@i 1097 [ - + ]:CBC 2580 : if (srclen >= (MaxAllocSize / (Size) MAX_CONVERSION_GROWTH))
1916 heikki.linnakangas@i 1098 [ # # ]:UBC 0 : ereport(ERROR,
1099 : : (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
1100 : : errmsg("out of memory"),
1101 : : errdetail("String of %d bytes is too long for encoding conversion.",
1102 : : (int) srclen)));
1103 : :
1916 heikki.linnakangas@i 1104 :CBC 2580 : dstsize = (Size) srclen * MAX_CONVERSION_GROWTH + 1;
1105 : 2580 : dst = MemoryContextAlloc(CurrentMemoryContext, dstsize);
1106 : :
1107 : : /* perform conversion */
1108 : 2580 : convertedbytes = pg_do_encoding_conversion_buf(proc,
1109 : : src_encoding,
1110 : : dest_encoding,
1111 : : (unsigned char *) src, srclen,
1112 : : (unsigned char *) dst, dstsize,
1113 : : noError);
1114 : 1548 : dstlen = strlen(dst);
1115 : :
1116 : : /*
1117 : : * build bytea data type structure.
1118 : : */
1119 : 1548 : retval = (bytea *) palloc(dstlen + VARHDRSZ);
1120 : 1548 : SET_VARSIZE(retval, dstlen + VARHDRSZ);
1121 : 1548 : memcpy(VARDATA(retval), dst, dstlen);
1122 : :
1123 : 1548 : pfree(dst);
1124 : : }
1125 : :
1126 : 3160 : values[0] = Int32GetDatum(convertedbytes);
1127 : 3160 : values[1] = PointerGetDatum(retval);
1128 : 3160 : tuple = heap_form_tuple(tupdesc, values, nulls);
1129 : :
1130 : 3160 : PG_RETURN_DATUM(HeapTupleGetDatum(tuple));
1131 : : }
1132 : :
1133 : : /* Convert bytea to text without validation for corruption tests from SQL. */
169 tmunro@postgresql.or 1134 : 8 : PG_FUNCTION_INFO_V1(test_bytea_to_text);
1135 : : Datum
1136 : 220 : test_bytea_to_text(PG_FUNCTION_ARGS)
1137 : : {
1138 : 220 : PG_RETURN_TEXT_P(PG_GETARG_BYTEA_PP(0));
1139 : : }
1140 : :
1141 : : /* And the reverse. */
1142 : 8 : PG_FUNCTION_INFO_V1(test_text_to_bytea);
1143 : : Datum
1144 : 200 : test_text_to_bytea(PG_FUNCTION_ARGS)
1145 : : {
1146 : 200 : PG_RETURN_BYTEA_P(PG_GETARG_TEXT_PP(0));
1147 : : }
1148 : :
1149 : : /* Corruption tests in C. */
1150 : 8 : PG_FUNCTION_INFO_V1(test_mblen_func);
1151 : : Datum
1152 : 24 : test_mblen_func(PG_FUNCTION_ARGS)
1153 : : {
1154 : 24 : const char *func = text_to_cstring(PG_GETARG_BYTEA_PP(0));
1155 : 24 : const char *encoding = text_to_cstring(PG_GETARG_BYTEA_PP(1));
1156 : 24 : text *string = PG_GETARG_BYTEA_PP(2);
1157 : 24 : int offset = PG_GETARG_INT32(3);
1158 [ + - ]: 24 : const char *data = VARDATA_ANY(string);
1159 [ - + - - : 24 : size_t size = VARSIZE_ANY_EXHDR(string);
- - - - +
- ]
1160 : 24 : int result = 0;
1161 : :
1162 [ + + ]: 24 : if (strcmp(func, "pg_mblen_unbounded") == 0)
1163 : 8 : result = pg_mblen_unbounded(data + offset);
1164 [ + + ]: 16 : else if (strcmp(func, "pg_mblen_cstr") == 0)
1165 : 4 : result = pg_mblen_cstr(data + offset);
1166 [ + + ]: 12 : else if (strcmp(func, "pg_mblen_with_len") == 0)
1167 : 4 : result = pg_mblen_with_len(data + offset, size - offset);
1168 [ + + ]: 8 : else if (strcmp(func, "pg_mblen_range") == 0)
1169 : 4 : result = pg_mblen_range(data + offset, data + size);
1170 [ + - ]: 4 : else if (strcmp(func, "pg_encoding_mblen") == 0)
1171 : 4 : result = pg_encoding_mblen(pg_char_to_encoding(encoding), data + offset);
1172 : : else
169 tmunro@postgresql.or 1173 [ # # ]:UBC 0 : elog(ERROR, "unknown function");
1174 : :
169 tmunro@postgresql.or 1175 :CBC 12 : PG_RETURN_INT32(result);
1176 : : }
1177 : :
1178 : 8 : PG_FUNCTION_INFO_V1(test_text_to_wchars);
1179 : : Datum
1180 : 200 : test_text_to_wchars(PG_FUNCTION_ARGS)
1181 : : {
1182 : 200 : const char *encoding_name = text_to_cstring(PG_GETARG_BYTEA_PP(0));
1183 : 200 : text *string = PG_GETARG_TEXT_PP(1);
1184 [ + - ]: 200 : const char *data = VARDATA_ANY(string);
1185 [ - + - - : 200 : size_t size = VARSIZE_ANY_EXHDR(string);
- - - - +
- ]
1186 : 200 : pg_wchar *wchars = palloc(sizeof(pg_wchar) * (size + 1));
1187 : : Datum *datums;
1188 : : int wlen;
1189 : : int encoding;
1190 : :
1191 : 200 : encoding = pg_char_to_encoding(encoding_name);
1192 [ - + ]: 200 : if (encoding < 0)
169 tmunro@postgresql.or 1193 [ # # ]:UBC 0 : elog(ERROR, "unknown encoding name: %s", encoding_name);
1194 : :
169 tmunro@postgresql.or 1195 [ + - ]:CBC 200 : if (size > 0)
1196 : : {
1197 : 200 : datums = palloc(sizeof(Datum) * size);
1198 : 200 : wlen = pg_encoding_mb2wchar_with_len(encoding,
1199 : : data,
1200 : : wchars,
1201 : : size);
1202 [ - + ]: 200 : Assert(wlen >= 0);
1203 [ - + ]: 200 : Assert(wlen <= size);
1204 [ - + ]: 200 : Assert(wchars[wlen] == 0);
1205 : :
1206 [ + + ]: 416 : for (int i = 0; i < wlen; ++i)
1207 : 216 : datums[i] = UInt32GetDatum(wchars[i]);
1208 : : }
1209 : : else
1210 : : {
169 tmunro@postgresql.or 1211 :UBC 0 : datums = NULL;
1212 : 0 : wlen = 0;
1213 : : }
1214 : :
169 tmunro@postgresql.or 1215 :CBC 200 : PG_RETURN_ARRAYTYPE_P(construct_array_builtin(datums, wlen, INT4OID));
1216 : : }
1217 : :
1218 : 8 : PG_FUNCTION_INFO_V1(test_wchars_to_text);
1219 : : Datum
1220 : 200 : test_wchars_to_text(PG_FUNCTION_ARGS)
1221 : : {
1222 : 200 : const char *encoding_name = text_to_cstring(PG_GETARG_BYTEA_PP(0));
1223 : 200 : ArrayType *array = PG_GETARG_ARRAYTYPE_P(1);
1224 : : Datum *datums;
1225 : : bool *nulls;
1226 : : char *mb;
1227 : : text *result;
1228 : : int wlen;
1229 : : int bytes;
1230 : : int encoding;
1231 : :
1232 : 200 : encoding = pg_char_to_encoding(encoding_name);
1233 [ - + ]: 200 : if (encoding < 0)
169 tmunro@postgresql.or 1234 [ # # ]:UBC 0 : elog(ERROR, "unknown encoding name: %s", encoding_name);
1235 : :
169 tmunro@postgresql.or 1236 :CBC 200 : deconstruct_array_builtin(array, INT4OID, &datums, &nulls, &wlen);
1237 : :
1238 [ + + ]: 200 : if (wlen > 0)
1239 : : {
1240 : 116 : pg_wchar *wchars = palloc(sizeof(pg_wchar) * wlen);
1241 : :
1242 [ + + ]: 332 : for (int i = 0; i < wlen; ++i)
1243 : : {
1244 [ - + ]: 216 : if (nulls[i])
169 tmunro@postgresql.or 1245 [ # # ]:UBC 0 : elog(ERROR, "unexpected NULL in array");
169 tmunro@postgresql.or 1246 :CBC 216 : wchars[i] = DatumGetInt32(datums[i]);
1247 : : }
1248 : :
1249 : 116 : mb = palloc(pg_encoding_max_length(encoding) * wlen + 1);
1250 : 116 : bytes = pg_encoding_wchar2mb_with_len(encoding, wchars, mb, wlen);
1251 : : }
1252 : : else
1253 : : {
1254 : 84 : mb = "";
1255 : 84 : bytes = 0;
1256 : : }
1257 : :
1258 : 200 : result = palloc(bytes + VARHDRSZ);
1259 : 200 : SET_VARSIZE(result, bytes + VARHDRSZ);
1260 : 200 : memcpy(VARDATA(result), mb, bytes);
1261 : :
1262 : 200 : PG_RETURN_TEXT_P(result);
1263 : : }
1264 : :
1265 : 8 : PG_FUNCTION_INFO_V1(test_valid_server_encoding);
1266 : : Datum
1267 : 200 : test_valid_server_encoding(PG_FUNCTION_ARGS)
1268 : : {
133 1269 : 200 : PG_RETURN_BOOL(pg_valid_server_encoding(text_to_cstring(PG_GETARG_TEXT_PP(0))) >= 0);
1270 : : }
1271 : :
1272 : : /* Provide SQL access to IsBinaryCoercible() */
1876 tgl@sss.pgh.pa.us 1273 : 9 : PG_FUNCTION_INFO_V1(binary_coercible);
1274 : : Datum
1275 : 25336 : binary_coercible(PG_FUNCTION_ARGS)
1276 : : {
1277 : 25336 : Oid srctype = PG_GETARG_OID(0);
1278 : 25336 : Oid targettype = PG_GETARG_OID(1);
1279 : :
1280 : 25336 : PG_RETURN_BOOL(IsBinaryCoercible(srctype, targettype));
1281 : : }
1282 : :
1283 : : /*
1284 : : * Sanity checks for functions in relpath.h
1285 : : */
490 andres@anarazel.de 1286 : 9 : PG_FUNCTION_INFO_V1(test_relpath);
1287 : : Datum
1288 : 4 : test_relpath(PG_FUNCTION_ARGS)
1289 : : {
1290 : : RelPathStr rpath;
1291 : :
1292 : : /*
1293 : : * Verify that PROCNUMBER_CHARS and MAX_BACKENDS stay in sync.
1294 : : * Unfortunately I don't know how to express that in a way suitable for a
1295 : : * static assert.
1296 : : */
1297 : : if ((int) ceil(log10(MAX_BACKENDS)) != PROCNUMBER_CHARS)
1298 : : elog(WARNING, "mismatch between MAX_BACKENDS and PROCNUMBER_CHARS");
1299 : :
1300 : : /* verify that the max-length relpath is generated ok */
1301 : 4 : rpath = GetRelationPath(OID_MAX, OID_MAX, OID_MAX, MAX_BACKENDS - 1,
1302 : : INIT_FORKNUM);
1303 : :
1304 [ - + ]: 4 : if (strlen(rpath.str) != REL_PATH_STR_MAXLEN)
490 andres@anarazel.de 1305 [ # # ]:UBC 0 : elog(WARNING, "maximum length relpath is if length %zu instead of %zu",
1306 : : strlen(rpath.str), REL_PATH_STR_MAXLEN);
1307 : :
490 andres@anarazel.de 1308 :CBC 4 : PG_RETURN_VOID();
1309 : : }
1310 : :
1311 : : /*
1312 : : * Simple test to verify NLS support, particularly that the PRI* macros work.
1313 : : *
1314 : : * A secondary objective is to verify that <inttypes.h>'s values for the
1315 : : * PRI* macros match what our snprintf.c code will do. Therefore, we run
1316 : : * the ereport() calls even when we know that translation will not happen.
1317 : : */
198 tgl@sss.pgh.pa.us 1318 :GNC 9 : PG_FUNCTION_INFO_V1(test_translation);
1319 : : Datum
1320 : 4 : test_translation(PG_FUNCTION_ARGS)
1321 : : {
1322 : : #ifdef ENABLE_NLS
1323 : : static bool inited = false;
1324 : :
1325 : : /*
1326 : : * Ideally we'd do this bit in a _PG_init() hook. However, it seems best
1327 : : * that the Solaris hack only get applied in the nls.sql test, so it
1328 : : * doesn't risk affecting other tests that load this module.
1329 : : */
1330 [ + - ]: 4 : if (!inited)
1331 : : {
1332 : : /*
1333 : : * Solaris' built-in gettext is not bright about associating locales
1334 : : * with message catalogs that are named after just the language.
1335 : : * Apparently the customary workaround is for users to set the
1336 : : * LANGUAGE environment variable to provide a mapping. Do so here to
1337 : : * ensure that the nls.sql regression test will work.
1338 : : */
1339 : : #if defined(__sun__)
1340 : : setenv("LANGUAGE", "es_ES.UTF-8:es", 1);
1341 : : #endif
1342 : 4 : pg_bindtextdomain(TEXTDOMAIN);
1343 : 4 : inited = true;
1344 : : }
1345 : :
1346 : : /*
1347 : : * If nls.sql failed to select a non-C locale, no translation will happen.
1348 : : * Report that so that we can distinguish this outcome from brokenness.
1349 : : * (We do this here, not in nls.sql, so as to need only 3 expected files.)
1350 : : */
196 1351 [ + - ]: 4 : if (strcmp(GetConfigOption("lc_messages", false, false), "C") == 0)
1352 [ + - ]: 4 : elog(NOTICE, "lc_messages is 'C'");
1353 : : #else
1354 : : elog(NOTICE, "NLS is not enabled");
1355 : : #endif
1356 : :
198 1357 [ + - ]: 4 : ereport(NOTICE,
1358 : : errmsg("translated PRId64 = %" PRId64, (int64) 424242424242));
1359 [ + - ]: 4 : ereport(NOTICE,
1360 : : errmsg("translated PRId32 = %" PRId32, (int32) -1234));
1361 [ + - ]: 4 : ereport(NOTICE,
1362 : : errmsg("translated PRIdMAX = %" PRIdMAX, (intmax_t) -123456789012));
1363 [ + - ]: 4 : ereport(NOTICE,
1364 : : errmsg("translated PRIdPTR = %" PRIdPTR, (intptr_t) -9999));
1365 : :
1366 [ + - ]: 4 : ereport(NOTICE,
1367 : : errmsg("translated PRIu64 = %" PRIu64, (uint64) 424242424242));
1368 [ + - ]: 4 : ereport(NOTICE,
1369 : : errmsg("translated PRIu32 = %" PRIu32, (uint32) -1234));
1370 [ + - ]: 4 : ereport(NOTICE,
1371 : : errmsg("translated PRIuMAX = %" PRIuMAX, (uintmax_t) 123456789012));
1372 [ + - ]: 4 : ereport(NOTICE,
1373 : : errmsg("translated PRIuPTR = %" PRIuPTR, (uintptr_t) 9999));
1374 : :
1375 [ + - ]: 4 : ereport(NOTICE,
1376 : : errmsg("translated PRIx64 = %" PRIx64, (uint64) 424242424242));
1377 [ + - ]: 4 : ereport(NOTICE,
1378 : : errmsg("translated PRIx32 = %" PRIx32, (uint32) -1234));
1379 [ + - ]: 4 : ereport(NOTICE,
1380 : : errmsg("translated PRIxMAX = %" PRIxMAX, (uintmax_t) 123456789012));
1381 [ + - ]: 4 : ereport(NOTICE,
1382 : : errmsg("translated PRIxPTR = %" PRIxPTR, (uintptr_t) 9999));
1383 : :
1384 [ + - ]: 4 : ereport(NOTICE,
1385 : : errmsg("translated PRIX64 = %" PRIX64, (uint64) 424242424242));
1386 [ + - ]: 4 : ereport(NOTICE,
1387 : : errmsg("translated PRIX32 = %" PRIX32, (uint32) -1234));
1388 [ + - ]: 4 : ereport(NOTICE,
1389 : : errmsg("translated PRIXMAX = %" PRIXMAX, (uintmax_t) 123456789012));
1390 [ + - ]: 4 : ereport(NOTICE,
1391 : : errmsg("translated PRIXPTR = %" PRIXPTR, (uintptr_t) 9999));
1392 : :
1393 : 4 : PG_RETURN_VOID();
1394 : : }
1395 : :
1396 : : /* Verify that pg_ticks_to_ns behaves correct, including overflow */
84 andres@anarazel.de 1397 : 9 : PG_FUNCTION_INFO_V1(test_instr_time);
1398 : : Datum
1399 : 4 : test_instr_time(PG_FUNCTION_ARGS)
1400 : : {
1401 : : instr_time t;
1402 : 4 : int64 test_ns[] = {0, 1000, INT64CONST(1000000000000000)};
1403 : : int64 max_err;
1404 : :
1405 : : /*
1406 : : * The ns-to-ticks-to-ns roundtrip may lose precision due to integer
1407 : : * truncation in the fixed-point conversion. The maximum error depends on
1408 : : * ticks_per_ns_scaled relative to the shift factor.
1409 : : */
1410 : 4 : max_err = (ticks_per_ns_scaled >> TICKS_TO_NS_SHIFT) + 1;
1411 : :
1412 [ + + ]: 16 : for (int i = 0; i < lengthof(test_ns); i++)
1413 : : {
1414 : : int64 result;
1415 : :
1416 : 12 : INSTR_TIME_SET_ZERO(t);
1417 : 12 : INSTR_TIME_ADD_NANOSEC(t, test_ns[i]);
1418 : 12 : result = INSTR_TIME_GET_NANOSEC(t);
1419 : :
1420 [ + - - + ]: 12 : if (result < test_ns[i] - max_err || result > test_ns[i])
84 andres@anarazel.de 1421 [ # # ]:UNC 0 : elog(ERROR,
1422 : : "INSTR_TIME_GET_NANOSEC(t) yielded " INT64_FORMAT
1423 : : ", expected " INT64_FORMAT " (max_err " INT64_FORMAT
1424 : : ") in file \"%s\" line %u",
1425 : : result, test_ns[i], max_err, __FILE__, __LINE__);
1426 : : }
1427 : :
84 andres@anarazel.de 1428 :GNC 4 : PG_RETURN_BOOL(true);
1429 : : }
1430 : :
1431 : : /*
1432 : : * test_pglz_compress
1433 : : *
1434 : : * Compress the input using pglz_compress(). Only the "always" strategy is
1435 : : * currently supported.
1436 : : *
1437 : : * Returns the compressed data, or NULL if compression fails.
1438 : : */
76 michael@paquier.xyz 1439 :CBC 8 : PG_FUNCTION_INFO_V1(test_pglz_compress);
1440 : : Datum
1441 : 16 : test_pglz_compress(PG_FUNCTION_ARGS)
1442 : : {
1443 : 16 : bytea *input = PG_GETARG_BYTEA_PP(0);
1444 [ - + ]: 16 : char *source = VARDATA_ANY(input);
1445 [ - + - - : 16 : int32 slen = VARSIZE_ANY_EXHDR(input);
- - - - -
+ ]
1446 : 16 : int32 maxout = PGLZ_MAX_OUTPUT(slen);
1447 : : bytea *result;
1448 : : int32 clen;
1449 : :
1450 : 16 : result = (bytea *) palloc(maxout + VARHDRSZ);
1451 : 16 : clen = pglz_compress(source, slen, VARDATA(result),
1452 : : PGLZ_strategy_always);
1453 [ - + ]: 16 : if (clen < 0)
76 michael@paquier.xyz 1454 :UBC 0 : PG_RETURN_NULL();
1455 : :
76 michael@paquier.xyz 1456 :CBC 16 : SET_VARSIZE(result, clen + VARHDRSZ);
1457 : 16 : PG_RETURN_BYTEA_P(result);
1458 : : }
1459 : :
1460 : : /*
1461 : : * test_pglz_decompress
1462 : : *
1463 : : * Decompress the input using pglz_decompress().
1464 : : *
1465 : : * The second argument is the expected uncompressed data size. The third
1466 : : * argument is here for the check_complete flag.
1467 : : *
1468 : : * Returns the decompressed data, or raises an error if decompression fails.
1469 : : */
1470 : 8 : PG_FUNCTION_INFO_V1(test_pglz_decompress);
1471 : : Datum
1472 : 56 : test_pglz_decompress(PG_FUNCTION_ARGS)
1473 : : {
1474 : 56 : bytea *input = PG_GETARG_BYTEA_PP(0);
1475 : 56 : int32 rawsize = PG_GETARG_INT32(1);
1476 : 56 : bool check_complete = PG_GETARG_BOOL(2);
1477 [ - + ]: 56 : char *source = VARDATA_ANY(input);
1478 [ - + - - : 56 : int32 slen = VARSIZE_ANY_EXHDR(input);
- - - - -
+ ]
1479 : : bytea *result;
1480 : : int32 dlen;
1481 : :
1482 [ - + ]: 56 : if (rawsize < 0)
76 michael@paquier.xyz 1483 [ # # ]:UBC 0 : elog(ERROR, "rawsize must not be negative");
1484 : :
76 michael@paquier.xyz 1485 :CBC 56 : result = (bytea *) palloc(rawsize + VARHDRSZ);
1486 : :
1487 : 56 : dlen = pglz_decompress(source, slen, VARDATA(result),
1488 : : rawsize, check_complete);
1489 [ + + ]: 56 : if (dlen < 0)
1490 [ + - ]: 44 : elog(ERROR, "pglz_decompress failed");
1491 : :
1492 : 12 : SET_VARSIZE(result, dlen + VARHDRSZ);
1493 : 12 : PG_RETURN_BYTEA_P(result);
1494 : : }
|