LCOV - differential code coverage report
Current view: top level - src/test/regress - regress.c (source / functions) Coverage Total Hit UNC UBC GNC CBC EUB ECB DCB
Current: 0e5ff9b9b45a657aea12440478dc002e9b01f138 vs 0123ce131fca454009439dfa3b2266d1d40737d7 Lines: 90.4 % 551 498 17 36 64 434 1 5
Current Date: 2026-03-14 14:10:32 -0400 Functions: 97.1 % 70 68 2 10 58
Baseline: lcov-20260315-024220-baseline Branches: 40.8 % 530 216 47 267 33 183 44 18
Baseline Date: 2026-03-14 15:27:56 +0100 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(7,30] days: 100.0 % 4 4 3 1
(30,360] days: 85.0 % 153 130 17 6 61 69
(360..) days: 92.4 % 394 364 30 364 1
Function coverage date bins:
(7,30] days: 100.0 % 2 2 2
(30,360] days: 100.0 % 19 19 5 14
(360..) days: 95.9 % 49 47 2 3 44
Branch coverage date bins:
(30,360] days: 38.7 % 142 55 47 16 33 22 18 6
(360..) days: 35.8 % 450 161 251 161 26 12

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

Generated by: LCOV version 2.4-beta