LCOV - code coverage report
Current view: top level - src/test/regress - regress.c (source / functions) Coverage Total Hit
Test: PostgreSQL 20devel Lines: 90.6 % 588 533
Test Date: 2026-07-12 23:15:38 Functions: 97.4 % 76 74
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 41.4 % 546 226

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

Generated by: LCOV version 2.0-1