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