LCOV - code coverage report
Current view: top level - src/test/regress - regress.c (source / functions) Coverage Total Hit
Test: PostgreSQL 20devel Lines: 87.9 % 680 598
Test Date: 2026-08-26 18:15:54 Functions: 97.5 % 80 78
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 39.7 % 688 273

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

Generated by: LCOV version 2.0-1