LCOV - differential code coverage report
Current view: top level - src/test/regress - regress.c (source / functions) Coverage Total Hit UNC UBC GNC CBC DCB
Current: 4b3c9325757dfd8f2312e9f1a8decf4431f344ae vs e87b024bfb8dd55e3049b1b6e6ab4e810cf1abc8 Lines: 88.0 % 716 630 86 9 621 9
Current Date: 2026-11-28 16:48:44 +0900 Functions: 97.7 % 88 86 2 7 79
Baseline: lcov-20261128-baseline Branches: 39.8 % 716 285 1 430 3 282
Baseline Date: 2026-11-28 16:48:48 +0900 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(7,30] days: 87.5 % 32 28 4 28
(30,360] days: 85.1 % 242 206 36 9 197
(360..) days: 89.6 % 442 396 46 396
Function coverage date bins:
(7,30] days: 100.0 % 8 8 8
(30,360] days: 100.0 % 26 26 3 23
(360..) days: 96.3 % 54 52 2 4 48
Branch coverage date bins:
(7,30] days: 38.9 % 18 7 11 7
(30,360] days: 41.8 % 244 102 1 141 3 99
(360..) days: 38.8 % 454 176 278 176

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

Generated by: LCOV version 2.0-1