PostgreSQL Source Code git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
regress.c
Go to the documentation of this file.
1/*------------------------------------------------------------------------
2 *
3 * regress.c
4 * Code for various C-language functions defined as part of the
5 * regression tests.
6 *
7 * This code is released under the terms of the PostgreSQL License.
8 *
9 * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
10 * Portions Copyright (c) 1994, Regents of the University of California
11 *
12 * src/test/regress/regress.c
13 *
14 *-------------------------------------------------------------------------
15 */
16
17#include "postgres.h"
18
19#include <math.h>
20#include <signal.h>
21
22#include "access/detoast.h"
23#include "access/htup_details.h"
24#include "catalog/namespace.h"
25#include "catalog/pg_operator.h"
26#include "catalog/pg_type.h"
27#include "commands/sequence.h"
28#include "commands/trigger.h"
29#include "executor/executor.h"
30#include "executor/spi.h"
31#include "funcapi.h"
32#include "mb/pg_wchar.h"
33#include "miscadmin.h"
34#include "nodes/supportnodes.h"
35#include "optimizer/optimizer.h"
36#include "optimizer/plancat.h"
37#include "parser/parse_coerce.h"
38#include "port/atomics.h"
39#include "postmaster/postmaster.h" /* for MAX_BACKENDS */
40#include "storage/spin.h"
41#include "utils/array.h"
42#include "utils/builtins.h"
43#include "utils/geo_decls.h"
44#include "utils/memutils.h"
45#include "utils/rel.h"
46#include "utils/typcache.h"
47
48#define EXPECT_TRUE(expr) \
49 do { \
50 if (!(expr)) \
51 elog(ERROR, \
52 "%s was unexpectedly false in file \"%s\" line %u", \
53 #expr, __FILE__, __LINE__); \
54 } while (0)
55
56#define EXPECT_EQ_U32(result_expr, expected_expr) \
57 do { \
58 uint32 actual_result = (result_expr); \
59 uint32 expected_result = (expected_expr); \
60 if (actual_result != expected_result) \
61 elog(ERROR, \
62 "%s yielded %u, expected %s in file \"%s\" line %u", \
63 #result_expr, actual_result, #expected_expr, __FILE__, __LINE__); \
64 } while (0)
65
66#define EXPECT_EQ_U64(result_expr, expected_expr) \
67 do { \
68 uint64 actual_result = (result_expr); \
69 uint64 expected_result = (expected_expr); \
70 if (actual_result != expected_result) \
71 elog(ERROR, \
72 "%s yielded " UINT64_FORMAT ", expected %s in file \"%s\" line %u", \
73 #result_expr, actual_result, #expected_expr, __FILE__, __LINE__); \
74 } while (0)
75
76#define LDELIM '('
77#define RDELIM ')'
78#define DELIM ','
79
80static void regress_lseg_construct(LSEG *lseg, Point *pt1, Point *pt2);
81
83 .name = "regress",
84 .version = PG_VERSION
85);
86
87
88/* return the point where two paths intersect, or NULL if no intersection. */
90
93{
94 PATH *p1 = PG_GETARG_PATH_P(0);
95 PATH *p2 = PG_GETARG_PATH_P(1);
96 int i,
97 j;
98 LSEG seg1,
99 seg2;
100 bool found; /* We've found the intersection */
101
102 found = false; /* Haven't found it yet */
103
104 for (i = 0; i < p1->npts - 1 && !found; i++)
105 {
106 regress_lseg_construct(&seg1, &p1->p[i], &p1->p[i + 1]);
107 for (j = 0; j < p2->npts - 1 && !found; j++)
108 {
109 regress_lseg_construct(&seg2, &p2->p[j], &p2->p[j + 1]);
111 LsegPGetDatum(&seg1),
112 LsegPGetDatum(&seg2))))
113 found = true;
114 }
115 }
116
117 if (!found)
119
120 /*
121 * Note: DirectFunctionCall2 will kick out an error if lseg_interpt()
122 * returns NULL, but that should be impossible since we know the two
123 * segments intersect.
124 */
126 LsegPGetDatum(&seg1),
127 LsegPGetDatum(&seg2)));
128}
129
130
131/* like lseg_construct, but assume space already allocated */
132static void
134{
135 lseg->p[0].x = pt1->x;
136 lseg->p[0].y = pt1->y;
137 lseg->p[1].x = pt2->x;
138 lseg->p[1].y = pt2->y;
139}
140
142
143Datum
145{
147 bool isnull;
148 int32 salary;
149
150 salary = DatumGetInt32(GetAttributeByName(tuple, "salary", &isnull));
151 if (isnull)
153 PG_RETURN_BOOL(salary > 699);
154}
155
156/* New type "widget"
157 * This used to be "circle", but I added circle to builtins,
158 * so needed to make sure the names do not collide. - tgl 97/04/21
159 */
160
161typedef struct
162{
164 double radius;
165} WIDGET;
166
169
170#define NARGS 3
171
172Datum
174{
175 char *str = PG_GETARG_CSTRING(0);
176 char *p,
177 *coord[NARGS];
178 int i;
179 WIDGET *result;
180
181 for (i = 0, p = str; *p && i < NARGS && *p != RDELIM; p++)
182 {
183 if (*p == DELIM || (*p == LDELIM && i == 0))
184 coord[i++] = p + 1;
185 }
186
187 /*
188 * Note: DON'T convert this error to "soft" style (errsave/ereturn). We
189 * want this data type to stay permanently in the hard-error world so that
190 * it can be used for testing that such cases still work reasonably.
191 */
192 if (i < NARGS)
194 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
195 errmsg("invalid input syntax for type %s: \"%s\"",
196 "widget", str)));
197
198 result = (WIDGET *) palloc(sizeof(WIDGET));
199 result->center.x = atof(coord[0]);
200 result->center.y = atof(coord[1]);
201 result->radius = atof(coord[2]);
202
203 PG_RETURN_POINTER(result);
204}
205
206Datum
208{
209 WIDGET *widget = (WIDGET *) PG_GETARG_POINTER(0);
210 char *str = psprintf("(%g,%g,%g)",
211 widget->center.x, widget->center.y, widget->radius);
212
214}
215
217
218Datum
220{
221 Point *point = PG_GETARG_POINT_P(0);
222 WIDGET *widget = (WIDGET *) PG_GETARG_POINTER(1);
223 float8 distance;
224
226 PointPGetDatum(point),
227 PointPGetDatum(&widget->center)));
228
229 PG_RETURN_BOOL(distance < widget->radius);
230}
231
233
234Datum
236{
237 char *string = PG_GETARG_CSTRING(0);
238 int i;
239 int len;
240 char *new_string;
241
242 new_string = palloc0(NAMEDATALEN);
243 for (i = 0; i < NAMEDATALEN && string[i]; ++i)
244 ;
245 if (i == NAMEDATALEN || !string[i])
246 --i;
247 len = i;
248 for (; i >= 0; --i)
249 new_string[len - i] = string[i];
250 PG_RETURN_CSTRING(new_string);
251}
252
254
255Datum
257{
258 TriggerData *trigdata = (TriggerData *) fcinfo->context;
259 HeapTuple tuple;
260
261 if (!CALLED_AS_TRIGGER(fcinfo))
262 elog(ERROR, "trigger_return_old: not fired by trigger manager");
263
264 tuple = trigdata->tg_trigtuple;
265
266 return PointerGetDatum(tuple);
267}
268
269
270/*
271 * Type int44 has no real-world use, but the regression tests use it
272 * (under the alias "city_budget"). It's a four-element vector of int4's.
273 */
274
275/*
276 * int44in - converts "num, num, ..." to internal form
277 *
278 * Note: Fills any missing positions with zeroes.
279 */
281
282Datum
284{
285 char *input_string = PG_GETARG_CSTRING(0);
286 int32 *result = (int32 *) palloc(4 * sizeof(int32));
287 int i;
288
289 i = sscanf(input_string,
290 "%d, %d, %d, %d",
291 &result[0],
292 &result[1],
293 &result[2],
294 &result[3]);
295 while (i < 4)
296 result[i++] = 0;
297
298 PG_RETURN_POINTER(result);
299}
300
301/*
302 * int44out - converts internal form to "num, num, ..."
303 */
305
306Datum
308{
309 int32 *an_array = (int32 *) PG_GETARG_POINTER(0);
310 char *result = (char *) palloc(16 * 4);
311
312 snprintf(result, 16 * 4, "%d,%d,%d,%d",
313 an_array[0],
314 an_array[1],
315 an_array[2],
316 an_array[3]);
317
318 PG_RETURN_CSTRING(result);
319}
320
322Datum
324{
325 char *path = text_to_cstring(PG_GETARG_TEXT_PP(0));
326
327 canonicalize_path(path);
329}
330
332Datum
334{
336 HeapTupleData tuple;
337 int ncolumns;
338 Datum *values;
339 bool *nulls;
340
341 Oid tupType;
342 int32 tupTypmod;
343 TupleDesc tupdesc;
344
345 HeapTuple newtup;
346
347 int i;
348
349 MemoryContext old_context;
350
351 /* Extract type info from the tuple itself */
352 tupType = HeapTupleHeaderGetTypeId(rec);
353 tupTypmod = HeapTupleHeaderGetTypMod(rec);
354 tupdesc = lookup_rowtype_tupdesc(tupType, tupTypmod);
355 ncolumns = tupdesc->natts;
356
357 /* Build a temporary HeapTuple control structure */
360 tuple.t_tableOid = InvalidOid;
361 tuple.t_data = rec;
362
363 values = (Datum *) palloc(ncolumns * sizeof(Datum));
364 nulls = (bool *) palloc(ncolumns * sizeof(bool));
365
366 heap_deform_tuple(&tuple, tupdesc, values, nulls);
367
369
370 for (i = 0; i < ncolumns; i++)
371 {
372 struct varlena *attr;
373 struct varlena *new_attr;
374 struct varatt_indirect redirect_pointer;
375
376 /* only work on existing, not-null varlenas */
377 if (TupleDescAttr(tupdesc, i)->attisdropped ||
378 nulls[i] ||
379 TupleDescAttr(tupdesc, i)->attlen != -1 ||
380 TupleDescAttr(tupdesc, i)->attstorage == TYPSTORAGE_PLAIN)
381 continue;
382
383 attr = (struct varlena *) DatumGetPointer(values[i]);
384
385 /* don't recursively indirect */
387 continue;
388
389 /* copy datum, so it still lives later */
391 attr = detoast_external_attr(attr);
392 else
393 {
394 struct varlena *oldattr = attr;
395
396 attr = palloc0(VARSIZE_ANY(oldattr));
397 memcpy(attr, oldattr, VARSIZE_ANY(oldattr));
398 }
399
400 /* build indirection Datum */
401 new_attr = (struct varlena *) palloc0(INDIRECT_POINTER_SIZE);
402 redirect_pointer.pointer = attr;
404 memcpy(VARDATA_EXTERNAL(new_attr), &redirect_pointer,
405 sizeof(redirect_pointer));
406
407 values[i] = PointerGetDatum(new_attr);
408 }
409
410 newtup = heap_form_tuple(tupdesc, values, nulls);
411 pfree(values);
412 pfree(nulls);
413 ReleaseTupleDesc(tupdesc);
414
415 MemoryContextSwitchTo(old_context);
416
417 /*
418 * We intentionally don't use PG_RETURN_HEAPTUPLEHEADER here, because that
419 * would cause the indirect toast pointers to be flattened out of the
420 * tuple immediately, rendering subsequent testing irrelevant. So just
421 * return the HeapTupleHeader pointer as-is. This violates the general
422 * rule that composite Datums shouldn't contain toast pointers, but so
423 * long as the regression test scripts don't insert the result of this
424 * function into a container type (record, array, etc) it should be OK.
425 */
426 PG_RETURN_POINTER(newtup->t_data);
427}
428
430
431Datum
433{
434#if !defined(WIN32) || defined(_MSC_VER)
435 extern char **environ;
436#endif
437 int nvals = 0;
438 ArrayType *result;
439 Datum *env;
440
441 for (char **s = environ; *s; s++)
442 nvals++;
443
444 env = palloc(nvals * sizeof(Datum));
445
446 for (int i = 0; i < nvals; i++)
448
449 result = construct_array_builtin(env, nvals, TEXTOID);
450
451 PG_RETURN_POINTER(result);
452}
453
455
456Datum
458{
459 char *envvar = text_to_cstring(PG_GETARG_TEXT_PP(0));
460 char *envval = text_to_cstring(PG_GETARG_TEXT_PP(1));
461
462 if (!superuser())
463 elog(ERROR, "must be superuser to change environment variables");
464
465 if (setenv(envvar, envval, 1) != 0)
466 elog(ERROR, "could not set environment variable: %m");
467
469}
470
471/* Sleep until no process has a given PID. */
473
474Datum
476{
477 int pid = PG_GETARG_INT32(0);
478
479 if (!superuser())
480 elog(ERROR, "must be superuser to check PID liveness");
481
482 while (kill(pid, 0) == 0)
483 {
485 pg_usleep(50000);
486 }
487
488 if (errno != ESRCH)
489 elog(ERROR, "could not check PID %d liveness: %m", pid);
490
492}
493
494static void
496{
497 pg_atomic_flag flag;
498
508}
509
510static void
512{
514 uint32 expected;
515 int i;
516
517 pg_atomic_init_u32(&var, 0);
519 pg_atomic_write_u32(&var, 3);
522 3);
528
529 /* test around numerical limits */
530 EXPECT_EQ_U32(pg_atomic_fetch_add_u32(&var, INT_MAX), 0);
531 EXPECT_EQ_U32(pg_atomic_fetch_add_u32(&var, INT_MAX), INT_MAX);
532 pg_atomic_fetch_add_u32(&var, 2); /* wrap to 0 */
537 2 * PG_INT16_MAX + 1);
540 pg_atomic_fetch_add_u32(&var, 1); /* top up to UINT_MAX */
541 EXPECT_EQ_U32(pg_atomic_read_u32(&var), UINT_MAX);
542 EXPECT_EQ_U32(pg_atomic_fetch_sub_u32(&var, INT_MAX), UINT_MAX);
543 EXPECT_EQ_U32(pg_atomic_read_u32(&var), (uint32) INT_MAX + 1);
544 EXPECT_EQ_U32(pg_atomic_sub_fetch_u32(&var, INT_MAX), 1);
546 expected = PG_INT16_MAX;
547 EXPECT_TRUE(!pg_atomic_compare_exchange_u32(&var, &expected, 1));
548 expected = PG_INT16_MAX + 1;
549 EXPECT_TRUE(!pg_atomic_compare_exchange_u32(&var, &expected, 1));
550 expected = PG_INT16_MIN;
551 EXPECT_TRUE(!pg_atomic_compare_exchange_u32(&var, &expected, 1));
552 expected = PG_INT16_MIN - 1;
553 EXPECT_TRUE(!pg_atomic_compare_exchange_u32(&var, &expected, 1));
554
555 /* fail exchange because of old expected */
556 expected = 10;
557 EXPECT_TRUE(!pg_atomic_compare_exchange_u32(&var, &expected, 1));
558
559 /* CAS is allowed to fail due to interrupts, try a couple of times */
560 for (i = 0; i < 1000; i++)
561 {
562 expected = 0;
563 if (!pg_atomic_compare_exchange_u32(&var, &expected, 1))
564 break;
565 }
566 if (i == 1000)
567 elog(ERROR, "atomic_compare_exchange_u32() never succeeded");
569 pg_atomic_write_u32(&var, 0);
570
571 /* try setting flagbits */
572 EXPECT_TRUE(!(pg_atomic_fetch_or_u32(&var, 1) & 1));
575 /* try clearing flagbits */
576 EXPECT_EQ_U32(pg_atomic_fetch_and_u32(&var, ~2) & 3, 3);
578 /* no bits set anymore */
580}
581
582static void
584{
586 uint64 expected;
587 int i;
588
589 pg_atomic_init_u64(&var, 0);
591 pg_atomic_write_u64(&var, 3);
594 3);
600
601 /* fail exchange because of old expected */
602 expected = 10;
603 EXPECT_TRUE(!pg_atomic_compare_exchange_u64(&var, &expected, 1));
604
605 /* CAS is allowed to fail due to interrupts, try a couple of times */
606 for (i = 0; i < 100; i++)
607 {
608 expected = 0;
609 if (!pg_atomic_compare_exchange_u64(&var, &expected, 1))
610 break;
611 }
612 if (i == 100)
613 elog(ERROR, "atomic_compare_exchange_u64() never succeeded");
615
616 pg_atomic_write_u64(&var, 0);
617
618 /* try setting flagbits */
619 EXPECT_TRUE(!(pg_atomic_fetch_or_u64(&var, 1) & 1));
622 /* try clearing flagbits */
623 EXPECT_EQ_U64((pg_atomic_fetch_and_u64(&var, ~2) & 3), 3);
625 /* no bits set anymore */
627}
628
629/*
630 * Perform, fairly minimal, testing of the spinlock implementation.
631 *
632 * It's likely worth expanding these to actually test concurrency etc, but
633 * having some regularly run tests is better than none.
634 */
635static void
637{
638 /*
639 * Basic tests for spinlocks, as well as the underlying operations.
640 *
641 * We embed the spinlock in a struct with other members to test that the
642 * spinlock operations don't perform too wide writes.
643 */
644 {
645 struct test_lock_struct
646 {
647 char data_before[4];
648 slock_t lock;
649 char data_after[4];
650 } struct_w_lock;
651
652 memcpy(struct_w_lock.data_before, "abcd", 4);
653 memcpy(struct_w_lock.data_after, "ef12", 4);
654
655 /* test basic operations via the SpinLock* API */
656 SpinLockInit(&struct_w_lock.lock);
657 SpinLockAcquire(&struct_w_lock.lock);
658 SpinLockRelease(&struct_w_lock.lock);
659
660 /* test basic operations via underlying S_* API */
661 S_INIT_LOCK(&struct_w_lock.lock);
662 S_LOCK(&struct_w_lock.lock);
663 S_UNLOCK(&struct_w_lock.lock);
664
665 /* and that "contended" acquisition works */
666 s_lock(&struct_w_lock.lock, "testfile", 17, "testfunc");
667 S_UNLOCK(&struct_w_lock.lock);
668
669 /*
670 * Check, using TAS directly, that a single spin cycle doesn't block
671 * when acquiring an already acquired lock.
672 */
673#ifdef TAS
674 S_LOCK(&struct_w_lock.lock);
675
676 if (!TAS(&struct_w_lock.lock))
677 elog(ERROR, "acquired already held spinlock");
678
679#ifdef TAS_SPIN
680 if (!TAS_SPIN(&struct_w_lock.lock))
681 elog(ERROR, "acquired already held spinlock");
682#endif /* defined(TAS_SPIN) */
683
684 S_UNLOCK(&struct_w_lock.lock);
685#endif /* defined(TAS) */
686
687 /*
688 * Verify that after all of this the non-lock contents are still
689 * correct.
690 */
691 if (memcmp(struct_w_lock.data_before, "abcd", 4) != 0)
692 elog(ERROR, "padding before spinlock modified");
693 if (memcmp(struct_w_lock.data_after, "ef12", 4) != 0)
694 elog(ERROR, "padding after spinlock modified");
695 }
696}
697
699Datum
701{
703
705
707
708 /*
709 * Arguably this shouldn't be tested as part of this function, but it's
710 * closely enough related that that seems ok for now.
711 */
713
714 PG_RETURN_BOOL(true);
715}
716
718Datum
720{
721 elog(ERROR, "test_fdw_handler is not implemented");
723}
724
726Datum
728{
729 Node *rawreq = (Node *) PG_GETARG_POINTER(0);
730 Node *ret = NULL;
731
732 if (IsA(rawreq, SupportRequestSelectivity))
733 {
734 /*
735 * Assume that the target is int4eq; that's safe as long as we don't
736 * attach this to any other boolean-returning function.
737 */
740
741 if (req->is_join)
742 s1 = join_selectivity(req->root, Int4EqualOperator,
743 req->args,
744 req->inputcollid,
745 req->jointype,
746 req->sjinfo);
747 else
748 s1 = restriction_selectivity(req->root, Int4EqualOperator,
749 req->args,
750 req->inputcollid,
751 req->varRelid);
752
753 req->selectivity = s1;
754 ret = (Node *) req;
755 }
756
757 if (IsA(rawreq, SupportRequestCost))
758 {
759 /* Provide some generic estimate */
760 SupportRequestCost *req = (SupportRequestCost *) rawreq;
761
762 req->startup = 0;
763 req->per_tuple = 2 * cpu_operator_cost;
764 ret = (Node *) req;
765 }
766
767 if (IsA(rawreq, SupportRequestRows))
768 {
769 /*
770 * Assume that the target is generate_series_int4; that's safe as long
771 * as we don't attach this to any other set-returning function.
772 */
773 SupportRequestRows *req = (SupportRequestRows *) rawreq;
774
775 if (req->node && IsA(req->node, FuncExpr)) /* be paranoid */
776 {
777 List *args = ((FuncExpr *) req->node)->args;
778 Node *arg1 = linitial(args);
779 Node *arg2 = lsecond(args);
780
781 if (IsA(arg1, Const) &&
782 !((Const *) arg1)->constisnull &&
783 IsA(arg2, Const) &&
784 !((Const *) arg2)->constisnull)
785 {
786 int32 val1 = DatumGetInt32(((Const *) arg1)->constvalue);
787 int32 val2 = DatumGetInt32(((Const *) arg2)->constvalue);
788
789 req->rows = val2 - val1 + 1;
790 ret = (Node *) req;
791 }
792 }
793 }
794
796}
797
799Datum
801{
803}
804
805/* one-time tests for encoding infrastructure */
807Datum
809{
810 /* Test pg_encoding_set_invalid() */
811 for (int i = 0; i < _PG_LAST_ENCODING_; i++)
812 {
813 char buf[2],
814 bigbuf[16];
815 int len,
816 mblen,
817 valid;
818
819 if (pg_encoding_max_length(i) == 1)
820 continue;
822 len = strnlen(buf, 2);
823 if (len != 2)
825 "official invalid string for encoding \"%s\" has length %d",
827 mblen = pg_encoding_mblen(i, buf);
828 if (mblen != 2)
830 "official invalid string for encoding \"%s\" has mblen %d",
831 pg_enc2name_tbl[i].name, mblen);
832 valid = pg_encoding_verifymbstr(i, buf, len);
833 if (valid != 0)
835 "official invalid string for encoding \"%s\" has valid prefix of length %d",
836 pg_enc2name_tbl[i].name, valid);
837 valid = pg_encoding_verifymbstr(i, buf, 1);
838 if (valid != 0)
840 "first byte of official invalid string for encoding \"%s\" has valid prefix of length %d",
841 pg_enc2name_tbl[i].name, valid);
842 memset(bigbuf, ' ', sizeof(bigbuf));
843 bigbuf[0] = buf[0];
844 bigbuf[1] = buf[1];
845 valid = pg_encoding_verifymbstr(i, bigbuf, sizeof(bigbuf));
846 if (valid != 0)
848 "trailing data changed official invalid string for encoding \"%s\" to have valid prefix of length %d",
849 pg_enc2name_tbl[i].name, valid);
850 }
851
853}
854
855/*
856 * Call an encoding conversion or verification function.
857 *
858 * Arguments:
859 * string bytea -- string to convert
860 * src_enc name -- source encoding
861 * dest_enc name -- destination encoding
862 * noError bool -- if set, don't ereport() on invalid or untranslatable
863 * input
864 *
865 * Result is a tuple with two attributes:
866 * int4 -- number of input bytes successfully converted
867 * bytea -- converted string
868 */
870Datum
872{
873 bytea *string = PG_GETARG_BYTEA_PP(0);
874 char *src_encoding_name = NameStr(*PG_GETARG_NAME(1));
875 int src_encoding = pg_char_to_encoding(src_encoding_name);
876 char *dest_encoding_name = NameStr(*PG_GETARG_NAME(2));
877 int dest_encoding = pg_char_to_encoding(dest_encoding_name);
878 bool noError = PG_GETARG_BOOL(3);
879 TupleDesc tupdesc;
880 char *src;
881 char *dst;
882 bytea *retval;
883 Size srclen;
884 Size dstsize;
885 Oid proc;
886 int convertedbytes;
887 int dstlen;
888 Datum values[2];
889 bool nulls[2] = {0};
890 HeapTuple tuple;
891
892 if (src_encoding < 0)
894 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
895 errmsg("invalid source encoding name \"%s\"",
896 src_encoding_name)));
897 if (dest_encoding < 0)
899 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
900 errmsg("invalid destination encoding name \"%s\"",
901 dest_encoding_name)));
902
903 /* Build a tuple descriptor for our result type */
904 if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
905 elog(ERROR, "return type must be a row type");
906 tupdesc = BlessTupleDesc(tupdesc);
907
908 srclen = VARSIZE_ANY_EXHDR(string);
909 src = VARDATA_ANY(string);
910
911 if (src_encoding == dest_encoding)
912 {
913 /* just check that the source string is valid */
914 int oklen;
915
916 oklen = pg_encoding_verifymbstr(src_encoding, src, srclen);
917
918 if (oklen == srclen)
919 {
920 convertedbytes = oklen;
921 retval = string;
922 }
923 else if (!noError)
924 {
925 report_invalid_encoding(src_encoding, src + oklen, srclen - oklen);
926 }
927 else
928 {
929 /*
930 * build bytea data type structure.
931 */
932 Assert(oklen < srclen);
933 convertedbytes = oklen;
934 retval = (bytea *) palloc(oklen + VARHDRSZ);
935 SET_VARSIZE(retval, oklen + VARHDRSZ);
936 memcpy(VARDATA(retval), src, oklen);
937 }
938 }
939 else
940 {
941 proc = FindDefaultConversionProc(src_encoding, dest_encoding);
942 if (!OidIsValid(proc))
944 (errcode(ERRCODE_UNDEFINED_FUNCTION),
945 errmsg("default conversion function for encoding \"%s\" to \"%s\" does not exist",
946 pg_encoding_to_char(src_encoding),
947 pg_encoding_to_char(dest_encoding))));
948
949 if (srclen >= (MaxAllocSize / (Size) MAX_CONVERSION_GROWTH))
951 (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
952 errmsg("out of memory"),
953 errdetail("String of %d bytes is too long for encoding conversion.",
954 (int) srclen)));
955
956 dstsize = (Size) srclen * MAX_CONVERSION_GROWTH + 1;
958
959 /* perform conversion */
960 convertedbytes = pg_do_encoding_conversion_buf(proc,
961 src_encoding,
962 dest_encoding,
963 (unsigned char *) src, srclen,
964 (unsigned char *) dst, dstsize,
965 noError);
966 dstlen = strlen(dst);
967
968 /*
969 * build bytea data type structure.
970 */
971 retval = (bytea *) palloc(dstlen + VARHDRSZ);
972 SET_VARSIZE(retval, dstlen + VARHDRSZ);
973 memcpy(VARDATA(retval), dst, dstlen);
974
975 pfree(dst);
976 }
977
978 values[0] = Int32GetDatum(convertedbytes);
979 values[1] = PointerGetDatum(retval);
980 tuple = heap_form_tuple(tupdesc, values, nulls);
981
983}
984
985/* Provide SQL access to IsBinaryCoercible() */
987Datum
989{
990 Oid srctype = PG_GETARG_OID(0);
991 Oid targettype = PG_GETARG_OID(1);
992
993 PG_RETURN_BOOL(IsBinaryCoercible(srctype, targettype));
994}
995
996/*
997 * Sanity checks for functions in relpath.h
998 */
1000Datum
1002{
1003 RelPathStr rpath;
1004
1005 /*
1006 * Verify that PROCNUMBER_CHARS and MAX_BACKENDS stay in sync.
1007 * Unfortunately I don't know how to express that in a way suitable for a
1008 * static assert.
1009 */
1010 if ((int) ceil(log10(MAX_BACKENDS)) != PROCNUMBER_CHARS)
1011 elog(WARNING, "mismatch between MAX_BACKENDS and PROCNUMBER_CHARS");
1012
1013 /* verify that the max-length relpath is generated ok */
1015 INIT_FORKNUM);
1016
1017 if (strlen(rpath.str) != REL_PATH_STR_MAXLEN)
1018 elog(WARNING, "maximum length relpath is if length %zu instead of %zu",
1019 strlen(rpath.str), REL_PATH_STR_MAXLEN);
1020
1022}
ArrayType * construct_array_builtin(Datum *elems, int nelems, Oid elmtype)
Definition: arrayfuncs.c:3381
static uint32 pg_atomic_fetch_and_u32(volatile pg_atomic_uint32 *ptr, uint32 and_)
Definition: atomics.h:396
static bool pg_atomic_compare_exchange_u32(volatile pg_atomic_uint32 *ptr, uint32 *expected, uint32 newval)
Definition: atomics.h:349
static void pg_atomic_write_u64(volatile pg_atomic_uint64 *ptr, uint64 val)
Definition: atomics.h:485
static void pg_atomic_clear_flag(volatile pg_atomic_flag *ptr)
Definition: atomics.h:207
static uint32 pg_atomic_fetch_or_u32(volatile pg_atomic_uint32 *ptr, uint32 or_)
Definition: atomics.h:410
static uint32 pg_atomic_sub_fetch_u32(volatile pg_atomic_uint32 *ptr, int32 sub_)
Definition: atomics.h:439
static uint32 pg_atomic_fetch_sub_u32(volatile pg_atomic_uint32 *ptr, int32 sub_)
Definition: atomics.h:381
static bool pg_atomic_compare_exchange_u64(volatile pg_atomic_uint64 *ptr, uint64 *expected, uint64 newval)
Definition: atomics.h:512
static void pg_atomic_init_u32(volatile pg_atomic_uint32 *ptr, uint32 val)
Definition: atomics.h:221
static uint32 pg_atomic_fetch_add_u32(volatile pg_atomic_uint32 *ptr, int32 add_)
Definition: atomics.h:366
static uint32 pg_atomic_add_fetch_u32(volatile pg_atomic_uint32 *ptr, int32 add_)
Definition: atomics.h:424
static uint64 pg_atomic_fetch_add_u64(volatile pg_atomic_uint64 *ptr, int64 add_)
Definition: atomics.h:522
static bool pg_atomic_test_set_flag(volatile pg_atomic_flag *ptr)
Definition: atomics.h:183
static uint64 pg_atomic_sub_fetch_u64(volatile pg_atomic_uint64 *ptr, int64 sub_)
Definition: atomics.h:568
static bool pg_atomic_unlocked_test_flag(volatile pg_atomic_flag *ptr)
Definition: atomics.h:196
static void pg_atomic_write_u32(volatile pg_atomic_uint32 *ptr, uint32 val)
Definition: atomics.h:276
static uint64 pg_atomic_fetch_and_u64(volatile pg_atomic_uint64 *ptr, uint64 and_)
Definition: atomics.h:541
static uint32 pg_atomic_read_u32(volatile pg_atomic_uint32 *ptr)
Definition: atomics.h:239
static uint64 pg_atomic_fetch_or_u64(volatile pg_atomic_uint64 *ptr, uint64 or_)
Definition: atomics.h:550
static uint64 pg_atomic_add_fetch_u64(volatile pg_atomic_uint64 *ptr, int64 add_)
Definition: atomics.h:559
static uint32 pg_atomic_exchange_u32(volatile pg_atomic_uint32 *ptr, uint32 newval)
Definition: atomics.h:330
static void pg_atomic_init_u64(volatile pg_atomic_uint64 *ptr, uint64 val)
Definition: atomics.h:453
static uint64 pg_atomic_read_u64(volatile pg_atomic_uint64 *ptr)
Definition: atomics.h:467
static uint64 pg_atomic_fetch_sub_u64(volatile pg_atomic_uint64 *ptr, int64 sub_)
Definition: atomics.h:531
static uint64 pg_atomic_exchange_u64(volatile pg_atomic_uint64 *ptr, uint64 newval)
Definition: atomics.h:503
static void pg_atomic_init_flag(volatile pg_atomic_flag *ptr)
Definition: atomics.h:170
static Datum values[MAXATTR]
Definition: bootstrap.c:151
#define CStringGetTextDatum(s)
Definition: builtins.h:97
#define NameStr(name)
Definition: c.h:717
#define VARHDRSZ
Definition: c.h:663
double float8
Definition: c.h:601
#define PG_INT16_MIN
Definition: c.h:556
int32_t int32
Definition: c.h:498
uint64_t uint64
Definition: c.h:503
uint32_t uint32
Definition: c.h:502
#define PG_INT16_MAX
Definition: c.h:557
#define OidIsValid(objectId)
Definition: c.h:746
size_t Size
Definition: c.h:576
double cpu_operator_cost
Definition: costsize.c:134
struct varlena * detoast_external_attr(struct varlena *attr)
Definition: detoast.c:45
#define INDIRECT_POINTER_SIZE
Definition: detoast.h:34
int errdetail(const char *fmt,...)
Definition: elog.c:1204
int errcode(int sqlerrcode)
Definition: elog.c:854
int errmsg(const char *fmt,...)
Definition: elog.c:1071
#define WARNING
Definition: elog.h:36
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:226
#define ereport(elevel,...)
Definition: elog.h:149
const pg_enc2name pg_enc2name_tbl[]
Definition: encnames.c:308
TupleDesc BlessTupleDesc(TupleDesc tupdesc)
Definition: execTuples.c:2260
Datum GetAttributeByName(HeapTupleHeader tuple, const char *attname, bool *isNull)
Definition: execUtils.c:1062
#define MaxAllocSize
Definition: fe_memutils.h:22
#define PG_RETURN_VOID()
Definition: fmgr.h:349
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
#define PG_GETARG_BYTEA_PP(n)
Definition: fmgr.h:308
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:684
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
#define PG_RETURN_CSTRING(x)
Definition: fmgr.h:362
#define PG_GETARG_CSTRING(n)
Definition: fmgr.h:277
#define PG_RETURN_NULL()
Definition: fmgr.h:345
#define PG_GETARG_NAME(n)
Definition: fmgr.h:278
#define PG_GETARG_HEAPTUPLEHEADER(n)
Definition: fmgr.h:312
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:372
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:274
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:353
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:361
#define PG_FUNCTION_ARGS
Definition: fmgr.h:193
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
TypeFuncClass get_call_result_type(FunctionCallInfo fcinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:276
@ TYPEFUNC_COMPOSITE
Definition: funcapi.h:149
static Datum HeapTupleGetDatum(const HeapTupleData *tuple)
Definition: funcapi.h:230
#define PG_GETARG_POINT_P(n)
Definition: geo_decls.h:185
static Datum LsegPGetDatum(const LSEG *X)
Definition: geo_decls.h:194
static Datum PointPGetDatum(const Point *X)
Definition: geo_decls.h:181
#define PG_GETARG_PATH_P(n)
Definition: geo_decls.h:216
Datum point_distance(PG_FUNCTION_ARGS)
Definition: geo_ops.c:1993
Datum lseg_intersect(PG_FUNCTION_ARGS)
Definition: geo_ops.c:2188
Datum lseg_interpt(PG_FUNCTION_ARGS)
Definition: geo_ops.c:2361
Assert(PointerIsAligned(start, uint64))
const char * str
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, const Datum *values, const bool *isnull)
Definition: heaptuple.c:1117
void heap_deform_tuple(HeapTuple tuple, TupleDesc tupleDesc, Datum *values, bool *isnull)
Definition: heaptuple.c:1346
static int32 HeapTupleHeaderGetTypMod(const HeapTupleHeaderData *tup)
Definition: htup_details.h:516
static uint32 HeapTupleHeaderGetDatumLength(const HeapTupleHeaderData *tup)
Definition: htup_details.h:492
static Oid HeapTupleHeaderGetTypeId(const HeapTupleHeaderData *tup)
Definition: htup_details.h:504
int j
Definition: isn.c:78
int i
Definition: isn.c:77
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:81
static void ItemPointerSetInvalid(ItemPointerData *pointer)
Definition: itemptr.h:184
int pg_do_encoding_conversion_buf(Oid proc, int src_encoding, int dest_encoding, unsigned char *src, int srclen, unsigned char *dest, int destlen, bool noError)
Definition: mbutils.c:469
void report_invalid_encoding(int encoding, const char *mbstr, int len)
Definition: mbutils.c:1698
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:1256
MemoryContext TopTransactionContext
Definition: mcxt.c:170
void pfree(void *pointer)
Definition: mcxt.c:2147
void * palloc0(Size size)
Definition: mcxt.c:1970
void * palloc(Size size)
Definition: mcxt.c:1940
MemoryContext CurrentMemoryContext
Definition: mcxt.c:159
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:123
Oid FindDefaultConversionProc(int32 for_encoding, int32 to_encoding)
Definition: namespace.c:4080
#define IsA(nodeptr, _type_)
Definition: nodes.h:164
double Selectivity
Definition: nodes.h:256
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:124
bool IsBinaryCoercible(Oid srctype, Oid targettype)
char attstorage
Definition: pg_attribute.h:108
int16 attlen
Definition: pg_attribute.h:59
#define NAMEDATALEN
const void size_t len
#define linitial(l)
Definition: pg_list.h:178
#define lsecond(l)
Definition: pg_list.h:183
static char * buf
Definition: pg_test_fsync.c:72
#define MAX_CONVERSION_GROWTH
Definition: pg_wchar.h:302
@ _PG_LAST_ENCODING_
Definition: pg_wchar.h:271
#define pg_encoding_to_char
Definition: pg_wchar.h:630
#define pg_char_to_encoding
Definition: pg_wchar.h:629
Selectivity restriction_selectivity(PlannerInfo *root, Oid operatorid, List *args, Oid inputcollid, int varRelid)
Definition: plancat.c:1969
Selectivity join_selectivity(PlannerInfo *root, Oid operatorid, List *args, Oid inputcollid, JoinType jointype, SpecialJoinInfo *sjinfo)
Definition: plancat.c:2008
void canonicalize_path(char *path)
Definition: path.c:337
#define snprintf
Definition: port.h:239
size_t strnlen(const char *str, size_t maxlen)
Definition: strnlen.c:26
static bool DatumGetBool(Datum X)
Definition: postgres.h:95
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:327
uintptr_t Datum
Definition: postgres.h:69
static float8 DatumGetFloat8(Datum X)
Definition: postgres.h:499
static Pointer DatumGetPointer(Datum X)
Definition: postgres.h:317
static Datum Int32GetDatum(int32 X)
Definition: postgres.h:217
static int32 DatumGetInt32(Datum X)
Definition: postgres.h:207
#define InvalidOid
Definition: postgres_ext.h:35
unsigned int Oid
Definition: postgres_ext.h:30
#define OID_MAX
Definition: postgres_ext.h:38
char * s1
char string[11]
Definition: preproc-type.c:52
#define MAX_BACKENDS
Definition: procnumber.h:39
char ** environ
char * psprintf(const char *fmt,...)
Definition: psprintf.c:43
static void test_spinlock(void)
Definition: regress.c:636
#define DELIM
Definition: regress.c:78
#define EXPECT_TRUE(expr)
Definition: regress.c:48
Datum regress_setenv(PG_FUNCTION_ARGS)
Definition: regress.c:457
static void test_atomic_uint32(void)
Definition: regress.c:511
Datum test_relpath(PG_FUNCTION_ARGS)
Definition: regress.c:1001
#define EXPECT_EQ_U32(result_expr, expected_expr)
Definition: regress.c:56
Datum test_atomic_ops(PG_FUNCTION_ARGS)
Definition: regress.c:700
Datum test_support_func(PG_FUNCTION_ARGS)
Definition: regress.c:727
PG_FUNCTION_INFO_V1(interpt_pp)
Datum int44out(PG_FUNCTION_ARGS)
Definition: regress.c:307
Datum test_opclass_options_func(PG_FUNCTION_ARGS)
Definition: regress.c:800
Datum test_fdw_handler(PG_FUNCTION_ARGS)
Definition: regress.c:719
#define EXPECT_EQ_U64(result_expr, expected_expr)
Definition: regress.c:66
Datum interpt_pp(PG_FUNCTION_ARGS)
Definition: regress.c:92
static void regress_lseg_construct(LSEG *lseg, Point *pt1, Point *pt2)
Definition: regress.c:133
Datum trigger_return_old(PG_FUNCTION_ARGS)
Definition: regress.c:256
#define RDELIM
Definition: regress.c:77
Datum int44in(PG_FUNCTION_ARGS)
Definition: regress.c:283
Datum get_environ(PG_FUNCTION_ARGS)
Definition: regress.c:432
Datum test_canonicalize_path(PG_FUNCTION_ARGS)
Definition: regress.c:323
Datum reverse_name(PG_FUNCTION_ARGS)
Definition: regress.c:235
Datum widget_in(PG_FUNCTION_ARGS)
Definition: regress.c:173
PG_MODULE_MAGIC_EXT(.name="regress",.version=PG_VERSION)
Datum wait_pid(PG_FUNCTION_ARGS)
Definition: regress.c:475
Datum widget_out(PG_FUNCTION_ARGS)
Definition: regress.c:207
static void test_atomic_flag(void)
Definition: regress.c:495
Datum pt_in_widget(PG_FUNCTION_ARGS)
Definition: regress.c:219
Datum test_enc_setup(PG_FUNCTION_ARGS)
Definition: regress.c:808
Datum test_enc_conversion(PG_FUNCTION_ARGS)
Definition: regress.c:871
static void test_atomic_uint64(void)
Definition: regress.c:583
Datum make_tuple_indirect(PG_FUNCTION_ARGS)
Definition: regress.c:333
Datum binary_coercible(PG_FUNCTION_ARGS)
Definition: regress.c:988
#define LDELIM
Definition: regress.c:76
#define NARGS
Definition: regress.c:170
Datum overpaid(PG_FUNCTION_ARGS)
Definition: regress.c:144
RelPathStr GetRelationPath(Oid dbOid, Oid spcOid, RelFileNumber relNumber, int procNumber, ForkNumber forkNumber)
Definition: relpath.c:143
#define REL_PATH_STR_MAXLEN
Definition: relpath.h:96
@ INIT_FORKNUM
Definition: relpath.h:61
#define PROCNUMBER_CHARS
Definition: relpath.h:84
int s_lock(volatile slock_t *lock, const char *file, int line, const char *func)
Definition: s_lock.c:98
#define TAS(lock)
Definition: s_lock.h:706
#define S_UNLOCK(lock)
Definition: s_lock.h:691
#define TAS_SPIN(lock)
Definition: s_lock.h:710
#define S_INIT_LOCK(lock)
Definition: s_lock.h:695
#define S_LOCK(lock)
Definition: s_lock.h:664
void pg_usleep(long microsec)
Definition: signal.c:53
#define SpinLockInit(lock)
Definition: spin.h:57
#define SpinLockRelease(lock)
Definition: spin.h:61
#define SpinLockAcquire(lock)
Definition: spin.h:59
ItemPointerData t_self
Definition: htup.h:65
uint32 t_len
Definition: htup.h:64
HeapTupleHeader t_data
Definition: htup.h:68
Oid t_tableOid
Definition: htup.h:66
Point p[2]
Definition: geo_decls.h:108
Definition: pg_list.h:54
Definition: nodes.h:135
Point p[FLEXIBLE_ARRAY_MEMBER]
Definition: geo_decls.h:121
int32 npts
Definition: geo_decls.h:118
float8 y
Definition: geo_decls.h:99
float8 x
Definition: geo_decls.h:98
char str[REL_PATH_STR_MAXLEN+1]
Definition: relpath.h:123
struct PlannerInfo * root
Definition: supportnodes.h:96
struct SpecialJoinInfo * sjinfo
Definition: supportnodes.h:103
HeapTuple tg_trigtuple
Definition: trigger.h:36
Point center
Definition: regress.c:163
double radius
Definition: regress.c:164
struct varlena * pointer
Definition: varatt.h:59
Definition: c.h:658
bool superuser(void)
Definition: superuser.c:46
char * flag(int b)
Definition: test-ctype.c:33
#define CALLED_AS_TRIGGER(fcinfo)
Definition: trigger.h:26
#define ReleaseTupleDesc(tupdesc)
Definition: tupdesc.h:219
static FormData_pg_attribute * TupleDescAttr(TupleDesc tupdesc, int i)
Definition: tupdesc.h:160
TupleDesc lookup_rowtype_tupdesc(Oid type_id, int32 typmod)
Definition: typcache.c:1922
#define VARATT_IS_EXTERNAL_ONDISK(PTR)
Definition: varatt.h:290
#define VARATT_IS_EXTERNAL_INDIRECT(PTR)
Definition: varatt.h:292
#define VARSIZE_ANY(PTR)
Definition: varatt.h:311
#define VARDATA(PTR)
Definition: varatt.h:278
#define SET_VARTAG_EXTERNAL(PTR, tag)
Definition: varatt.h:309
#define VARDATA_ANY(PTR)
Definition: varatt.h:324
#define VARDATA_EXTERNAL(PTR)
Definition: varatt.h:286
#define SET_VARSIZE(PTR, len)
Definition: varatt.h:305
@ VARTAG_INDIRECT
Definition: varatt.h:86
#define VARSIZE_ANY_EXHDR(PTR)
Definition: varatt.h:317
text * cstring_to_text(const char *s)
Definition: varlena.c:192
char * text_to_cstring(const text *t)
Definition: varlena.c:225
const char * name
void pg_encoding_set_invalid(int encoding, char *dst)
Definition: wchar.c:2049
int pg_encoding_verifymbstr(int encoding, const char *mbstr, int len)
Definition: wchar.c:2163
int pg_encoding_max_length(int encoding)
Definition: wchar.c:2174
int pg_encoding_mblen(int encoding, const char *mbstr)
Definition: wchar.c:2116
#define kill(pid, sig)
Definition: win32_port.h:493
#define setenv(x, y, z)
Definition: win32_port.h:545