LCOV - differential code coverage report
Current view: top level - src/test/regress - regress.c (source / functions) Coverage Total Hit LBC UIC UBC GBC GIC GNC CBC EUB ECB DCB
Current: Differential Code Coverage HEAD vs 15 Lines: 90.5 % 495 448 15 29 3 22 227 1 198 22 234 1
Current Date: 2023-04-08 17:13:01 Functions: 96.1 % 51 49 2 49 2 49
Baseline: 15 Line coverage date bins:
Baseline Date: 2023-04-08 15:09:40 (240..) days: 90.5 % 495 448 15 29 3 22 227 1 198 22 232
Legend: Lines: hit not hit Function coverage date bins:
(240..) days: 48.0 % 102 49 2 49 2 49

 Age         Owner                  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-2023, 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 "access/transam.h"
                                 25                 : #include "access/xact.h"
                                 26                 : #include "catalog/namespace.h"
                                 27                 : #include "catalog/pg_operator.h"
                                 28                 : #include "catalog/pg_type.h"
                                 29                 : #include "commands/sequence.h"
                                 30                 : #include "commands/trigger.h"
                                 31                 : #include "executor/executor.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 "storage/spin.h"
                                 42                 : #include "utils/array.h"
                                 43                 : #include "utils/builtins.h"
                                 44                 : #include "utils/geo_decls.h"
                                 45                 : #include "utils/lsyscache.h"
                                 46                 : #include "utils/memutils.h"
                                 47                 : #include "utils/rel.h"
                                 48                 : #include "utils/typcache.h"
                                 49                 : 
                                 50                 : #define EXPECT_TRUE(expr)   \
                                 51                 :     do { \
                                 52                 :         if (!(expr)) \
                                 53                 :             elog(ERROR, \
                                 54                 :                  "%s was unexpectedly false in file \"%s\" line %u", \
                                 55                 :                  #expr, __FILE__, __LINE__); \
                                 56                 :     } while (0)
                                 57                 : 
                                 58                 : #define EXPECT_EQ_U32(result_expr, expected_expr)   \
                                 59                 :     do { \
                                 60                 :         uint32      actual_result = (result_expr); \
                                 61                 :         uint32      expected_result = (expected_expr); \
                                 62                 :         if (actual_result != expected_result) \
                                 63                 :             elog(ERROR, \
                                 64                 :                  "%s yielded %u, expected %s in file \"%s\" line %u", \
                                 65                 :                  #result_expr, actual_result, #expected_expr, __FILE__, __LINE__); \
                                 66                 :     } while (0)
                                 67                 : 
                                 68                 : #define EXPECT_EQ_U64(result_expr, expected_expr)   \
                                 69                 :     do { \
                                 70                 :         uint64      actual_result = (result_expr); \
                                 71                 :         uint64      expected_result = (expected_expr); \
                                 72                 :         if (actual_result != expected_result) \
                                 73                 :             elog(ERROR, \
                                 74                 :                  "%s yielded " UINT64_FORMAT ", expected %s in file \"%s\" line %u", \
                                 75                 :                  #result_expr, actual_result, #expected_expr, __FILE__, __LINE__); \
                                 76                 :     } while (0)
                                 77                 : 
                                 78                 : #define LDELIM          '('
                                 79                 : #define RDELIM          ')'
                                 80                 : #define DELIM           ','
                                 81                 : 
                                 82                 : static void regress_lseg_construct(LSEG *lseg, Point *pt1, Point *pt2);
                                 83                 : 
 6158 bruce                      84 GIC          55 : PG_MODULE_MAGIC;
 6158 tgl                        85 ECB             : 
                                 86                 : 
                                 87                 : /* return the point where two paths intersect, or NULL if no intersection. */
 8175 tgl                        88 GIC           7 : PG_FUNCTION_INFO_V1(interpt_pp);
 8175 tgl                        89 ECB             : 
                                 90                 : Datum
 8288 tgl                        91 GIC        2688 : interpt_pp(PG_FUNCTION_ARGS)
 9770 scrappy                    92 ECB             : {
 8288 tgl                        93 GIC        2688 :     PATH       *p1 = PG_GETARG_PATH_P(0);
 8288 tgl                        94 CBC        2688 :     PATH       *p2 = PG_GETARG_PATH_P(1);
 9344 bruce                      95 ECB             :     int         i,
                                 96                 :                 j;
                                 97                 :     LSEG        seg1,
                                 98                 :                 seg2;
                                 99                 :     bool        found;          /* We've found the intersection */
                                100                 : 
 9345 bruce                     101 GIC        2688 :     found = false;              /* Haven't found it yet */
 9644 bryanh                    102 ECB             : 
 9345 bruce                     103 GIC        8823 :     for (i = 0; i < p1->npts - 1 && !found; i++)
 8288 tgl                       104 ECB             :     {
 8288 tgl                       105 GIC        6135 :         regress_lseg_construct(&seg1, &p1->p[i], &p1->p[i + 1]);
 9345 bruce                     106 CBC       18819 :         for (j = 0; j < p2->npts - 1 && !found; j++)
 9345 bruce                     107 ECB             :         {
 9345 bruce                     108 GIC       12684 :             regress_lseg_construct(&seg2, &p2->p[j], &p2->p[j + 1]);
 8288 tgl                       109 CBC       12684 :             if (DatumGetBool(DirectFunctionCall2(lseg_intersect,
 8288 tgl                       110 ECB             :                                                  LsegPGetDatum(&seg1),
                                111                 :                                                  LsegPGetDatum(&seg2))))
 9345 bruce                     112 GIC        2682 :                 found = true;
 9345 bruce                     113 ECB             :         }
                                114                 :     }
                                115                 : 
 8288 tgl                       116 GIC        2688 :     if (!found)
 8288 tgl                       117 CBC           6 :         PG_RETURN_NULL();
 9345 bruce                     118 ECB             : 
                                119                 :     /*
                                120                 :      * Note: DirectFunctionCall2 will kick out an error if lseg_interpt()
                                121                 :      * returns NULL, but that should be impossible since we know the two
                                122                 :      * segments intersect.
                                123                 :      */
 8288 tgl                       124 GIC        2682 :     PG_RETURN_DATUM(DirectFunctionCall2(lseg_interpt,
 8288 tgl                       125 ECB             :                                         LsegPGetDatum(&seg1),
                                126                 :                                         LsegPGetDatum(&seg2)));
                                127                 : }
                                128                 : 
                                129                 : 
                                130                 : /* like lseg_construct, but assume space already allocated */
                                131                 : static void
 7325 bruce                     132 GIC       18819 : regress_lseg_construct(LSEG *lseg, Point *pt1, Point *pt2)
 9770 scrappy                   133 ECB             : {
 9345 bruce                     134 GIC       18819 :     lseg->p[0].x = pt1->x;
 9345 bruce                     135 CBC       18819 :     lseg->p[0].y = pt1->y;
                                136           18819 :     lseg->p[1].x = pt2->x;
                                137           18819 :     lseg->p[1].y = pt2->y;
 9770 scrappy                   138           18819 : }
 9770 scrappy                   139 ECB             : 
 8175 tgl                       140 GIC           7 : PG_FUNCTION_INFO_V1(overpaid);
 8175 tgl                       141 ECB             : 
                                142                 : Datum
 8343 tgl                       143 GIC          18 : overpaid(PG_FUNCTION_ARGS)
 9770 scrappy                   144 ECB             : {
 6947 tgl                       145 GIC          18 :     HeapTupleHeader tuple = PG_GETARG_HEAPTUPLEHEADER(0);
 9344 bruce                     146 ECB             :     bool        isnull;
                                147                 :     int32       salary;
                                148                 : 
 8263 tgl                       149 GIC          18 :     salary = DatumGetInt32(GetAttributeByName(tuple, "salary", &isnull));
 8343 tgl                       150 CBC          18 :     if (isnull)
 8343 tgl                       151 LBC           0 :         PG_RETURN_NULL();
 8343 tgl                       152 GBC          18 :     PG_RETURN_BOOL(salary > 699);
 9770 scrappy                   153 ECB             : }
                                154                 : 
                                155                 : /* New type "widget"
                                156                 :  * This used to be "circle", but I added circle to builtins,
                                157                 :  *  so needed to make sure the names do not collide. - tgl 97/04/21
                                158                 :  */
                                159                 : 
                                160                 : typedef struct
                                161                 : {
                                162                 :     Point       center;
                                163                 :     double      radius;
                                164                 : } WIDGET;
                                165                 : 
 2202 andres                    166 GIC           7 : PG_FUNCTION_INFO_V1(widget_in);
 2202 andres                    167 CBC           7 : PG_FUNCTION_INFO_V1(widget_out);
 9770 scrappy                   168 ECB             : 
                                169                 : #define NARGS   3
                                170                 : 
                                171                 : Datum
 2202 andres                    172 GIC          30 : widget_in(PG_FUNCTION_ARGS)
 9770 scrappy                   173 ECB             : {
 2202 andres                    174 GIC          30 :     char       *str = PG_GETARG_CSTRING(0);
 9344 bruce                     175 ECB             :     char       *p,
                                176                 :                *coord[NARGS];
                                177                 :     int         i;
                                178                 :     WIDGET     *result;
                                179                 : 
 9770 scrappy                   180 GIC         183 :     for (i = 0, p = str; *p && i < NARGS && *p != RDELIM; p++)
 2647 tgl                       181 ECB             :     {
 2647 tgl                       182 GIC         153 :         if (*p == DELIM || (*p == LDELIM && i == 0))
 9770 scrappy                   183 CBC          81 :             coord[i++] = p + 1;
 2647 tgl                       184 ECB             :     }
                                185                 : 
                                186                 :     /*
                                187                 :      * Note: DON'T convert this error to "soft" style (errsave/ereturn).  We
                                188                 :      * want this data type to stay permanently in the hard-error world so that
                                189                 :      * it can be used for testing that such cases still work reasonably.
                                190                 :      */
 2647 tgl                       191 GIC          30 :     if (i < NARGS)
                                192               9 :         ereport(ERROR,
                                193                 :                 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
                                194                 :                  errmsg("invalid input syntax for type %s: \"%s\"",
                                195                 :                         "widget", str)));
                                196                 : 
 9483 scrappy                   197 CBC          21 :     result = (WIDGET *) palloc(sizeof(WIDGET));
 9770                           198              21 :     result->center.x = atof(coord[0]);
 9770 scrappy                   199 GIC          21 :     result->center.y = atof(coord[1]);
                                200              21 :     result->radius = atof(coord[2]);
                                201                 : 
 2202 andres                    202              21 :     PG_RETURN_POINTER(result);
 9770 scrappy                   203 ECB             : }
                                204                 : 
 2202 andres                    205                 : Datum
 2202 andres                    206 CBC           6 : widget_out(PG_FUNCTION_ARGS)
                                207                 : {
 2153 bruce                     208               6 :     WIDGET     *widget = (WIDGET *) PG_GETARG_POINTER(0);
 2153 bruce                     209 GIC           6 :     char       *str = psprintf("(%g,%g,%g)",
                                210                 :                                widget->center.x, widget->center.y, widget->radius);
                                211                 : 
 2202 andres                    212 CBC           6 :     PG_RETURN_CSTRING(str);
                                213                 : }
 9770 scrappy                   214 ECB             : 
 8175 tgl                       215 CBC           7 : PG_FUNCTION_INFO_V1(pt_in_widget);
                                216                 : 
                                217                 : Datum
 8335                           218               6 : pt_in_widget(PG_FUNCTION_ARGS)
                                219                 : {
 8335 tgl                       220 GIC           6 :     Point      *point = PG_GETARG_POINT_P(0);
 8335 tgl                       221 CBC           6 :     WIDGET     *widget = (WIDGET *) PG_GETARG_POINTER(1);
                                222                 :     float8      distance;
                                223                 : 
 1715 tomas.vondra              224               6 :     distance = DatumGetFloat8(DirectFunctionCall2(point_distance,
                                225                 :                                                   PointPGetDatum(point),
 1418 tgl                       226 ECB             :                                                   PointPGetDatum(&widget->center)));
 1715 tomas.vondra              227                 : 
 1715 tomas.vondra              228 GIC           6 :     PG_RETURN_BOOL(distance < widget->radius);
                                229                 : }
 9770 scrappy                   230 ECB             : 
 2202 andres                    231 GIC           7 : PG_FUNCTION_INFO_V1(reverse_name);
                                232                 : 
                                233                 : Datum
 2202 andres                    234 CBC          24 : reverse_name(PG_FUNCTION_ARGS)
                                235                 : {
 2202 andres                    236 GIC          24 :     char       *string = PG_GETARG_CSTRING(0);
 9188 bruce                     237 ECB             :     int         i;
                                238                 :     int         len;
                                239                 :     char       *new_string;
 9345                           240                 : 
 7196 tgl                       241 GIC          24 :     new_string = palloc0(NAMEDATALEN);
 9029 bruce                     242 CBC         168 :     for (i = 0; i < NAMEDATALEN && string[i]; ++i)
                                243                 :         ;
 9029 bruce                     244 GIC          24 :     if (i == NAMEDATALEN || !string[i])
 9345                           245              24 :         --i;
                                246              24 :     len = i;
 9345 bruce                     247 CBC         168 :     for (; i >= 0; --i)
                                248             144 :         new_string[len - i] = string[i];
 2202 andres                    249 GIC          24 :     PG_RETURN_CSTRING(new_string);
 8175 tgl                       250 ECB             : }
                                251                 : 
 1867 tgl                       252 CBC           7 : PG_FUNCTION_INFO_V1(trigger_return_old);
 1867 tgl                       253 ECB             : 
                                254                 : Datum
 1867 tgl                       255 CBC          45 : trigger_return_old(PG_FUNCTION_ARGS)
                                256                 : {
 1867 tgl                       257 GIC          45 :     TriggerData *trigdata = (TriggerData *) fcinfo->context;
 1867 tgl                       258 ECB             :     HeapTuple   tuple;
                                259                 : 
 1867 tgl                       260 GIC          45 :     if (!CALLED_AS_TRIGGER(fcinfo))
 1867 tgl                       261 LBC           0 :         elog(ERROR, "trigger_return_old: not fired by trigger manager");
                                262                 : 
 1867 tgl                       263 CBC          45 :     tuple = trigdata->tg_trigtuple;
                                264                 : 
 1867 tgl                       265 GIC          45 :     return PointerGetDatum(tuple);
 1867 tgl                       266 ECB             : }
 9341 vadim4o                   267 EUB             : 
                                268                 : #define TTDUMMY_INFINITY    999999
 9328 vadim4o                   269 ECB             : 
                                270                 : static SPIPlanPtr splan = NULL;
 9173 bruce                     271                 : static bool ttoff = false;
                                272                 : 
 8175 tgl                       273 GIC           7 : PG_FUNCTION_INFO_V1(ttdummy);
                                274                 : 
                                275                 : Datum
 8350                           276              30 : ttdummy(PG_FUNCTION_ARGS)
                                277                 : {
                                278              30 :     TriggerData *trigdata = (TriggerData *) fcinfo->context;
 9328 vadim4o                   279 ECB             :     Trigger    *trigger;        /* to get trigger name */
                                280                 :     char      **args;           /* arguments */
                                281                 :     int         attnum[2];      /* fnumbers of start/stop columns */
 9173 bruce                     282                 :     Datum       oldon,
                                283                 :                 oldoff;
                                284                 :     Datum       newon,
                                285                 :                 newoff;
                                286                 :     Datum      *cvals;          /* column values */
                                287                 :     char       *cnulls;         /* column nulls */
                                288                 :     char       *relname;        /* triggered relation name */
                                289                 :     Relation    rel;            /* triggered relation */
                                290                 :     HeapTuple   trigtuple;
 9328 vadim4o                   291 GIC          30 :     HeapTuple   newtuple = NULL;
                                292                 :     HeapTuple   rettuple;
                                293                 :     TupleDesc   tupdesc;        /* tuple description */
                                294                 :     int         natts;          /* # of attributes */
                                295                 :     bool        isnull;         /* to know is some column NULL or not */
                                296                 :     int         ret;
 9328 vadim4o                   297 ECB             :     int         i;
                                298                 : 
 8350 tgl                       299 GIC          30 :     if (!CALLED_AS_TRIGGER(fcinfo))
 8350 tgl                       300 UIC           0 :         elog(ERROR, "ttdummy: not fired by trigger manager");
 4566 tgl                       301 GIC          30 :     if (!TRIGGER_FIRED_FOR_ROW(trigdata->tg_event))
 4566 tgl                       302 UIC           0 :         elog(ERROR, "ttdummy: must be fired for row");
 4566 tgl                       303 GIC          30 :     if (!TRIGGER_FIRED_BEFORE(trigdata->tg_event))
 9224 bruce                     304 UIC           0 :         elog(ERROR, "ttdummy: must be fired before event");
 8350 tgl                       305 CBC          30 :     if (TRIGGER_FIRED_BY_INSERT(trigdata->tg_event))
 5911 bruce                     306 UBC           0 :         elog(ERROR, "ttdummy: cannot process INSERT event");
 8350 tgl                       307 CBC          30 :     if (TRIGGER_FIRED_BY_UPDATE(trigdata->tg_event))
 8350 tgl                       308 GBC          24 :         newtuple = trigdata->tg_newtuple;
 9173 bruce                     309 ECB             : 
 8350 tgl                       310 GBC          30 :     trigtuple = trigdata->tg_trigtuple;
 9173 bruce                     311 ECB             : 
 8350 tgl                       312 GBC          30 :     rel = trigdata->tg_relation;
 9328 vadim4o                   313 CBC          30 :     relname = SPI_getrelname(rel);
 9173 bruce                     314 ECB             : 
                                315                 :     /* check if TT is OFF for this relation */
 9173 bruce                     316 CBC          30 :     if (ttoff)                  /* OFF - nothing to do */
                                317                 :     {
                                318              15 :         pfree(relname);
 8350 tgl                       319              15 :         return PointerGetDatum((newtuple != NULL) ? newtuple : trigtuple);
                                320                 :     }
                                321                 : 
                                322              15 :     trigger = trigdata->tg_trigger;
                                323                 : 
 9328 vadim4o                   324              15 :     if (trigger->tgnargs != 2)
 9173 bruce                     325 LBC           0 :         elog(ERROR, "ttdummy (%s): invalid (!= 2) number of arguments %d",
                                326                 :              relname, trigger->tgnargs);
                                327                 : 
 9328 vadim4o                   328 CBC          15 :     args = trigger->tgargs;
 9328 vadim4o                   329 GIC          15 :     tupdesc = rel->rd_att;
 9328 vadim4o                   330 CBC          15 :     natts = tupdesc->natts;
 9173 bruce                     331 EUB             : 
 9173 bruce                     332 GIC          45 :     for (i = 0; i < 2; i++)
                                333                 :     {
 9173 bruce                     334 CBC          30 :         attnum[i] = SPI_fnumber(tupdesc, args[i]);
 2343 tgl                       335              30 :         if (attnum[i] <= 0)
 2343 tgl                       336 LBC           0 :             elog(ERROR, "ttdummy (%s): there is no attribute %s",
                                337                 :                  relname, args[i]);
 9173 bruce                     338 CBC          30 :         if (SPI_gettypeid(tupdesc, attnum[i]) != INT4OID)
 2343 tgl                       339 UIC           0 :             elog(ERROR, "ttdummy (%s): attribute %s must be of integer type",
 2343 tgl                       340 ECB             :                  relname, args[i]);
 9328 vadim4o                   341                 :     }
 9173 bruce                     342 EUB             : 
 9173 bruce                     343 GIC          15 :     oldon = SPI_getbinval(trigtuple, tupdesc, attnum[0], &isnull);
 9328 vadim4o                   344 CBC          15 :     if (isnull)
 9224 bruce                     345 UBC           0 :         elog(ERROR, "ttdummy (%s): %s must be NOT NULL", relname, args[0]);
                                346                 : 
 9173 bruce                     347 GIC          15 :     oldoff = SPI_getbinval(trigtuple, tupdesc, attnum[1], &isnull);
 9328 vadim4o                   348              15 :     if (isnull)
 9224 bruce                     349 LBC           0 :         elog(ERROR, "ttdummy (%s): %s must be NOT NULL", relname, args[1]);
 9173 bruce                     350 ECB             : 
 9173 bruce                     351 GBC          15 :     if (newtuple != NULL)       /* UPDATE */
                                352                 :     {
 9173 bruce                     353 CBC          12 :         newon = SPI_getbinval(newtuple, tupdesc, attnum[0], &isnull);
 9328 vadim4o                   354              12 :         if (isnull)
 9224 bruce                     355 UBC           0 :             elog(ERROR, "ttdummy (%s): %s must be NOT NULL", relname, args[0]);
 9173 bruce                     356 GIC          12 :         newoff = SPI_getbinval(newtuple, tupdesc, attnum[1], &isnull);
 9328 vadim4o                   357 CBC          12 :         if (isnull)
 9224 bruce                     358 UIC           0 :             elog(ERROR, "ttdummy (%s): %s must be NOT NULL", relname, args[1]);
 9173 bruce                     359 ECB             : 
 9173 bruce                     360 CBC          12 :         if (oldon != newon || oldoff != newoff)
 2807 tgl                       361 GBC           3 :             ereport(ERROR,
 2807 tgl                       362 ECB             :                     (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                                363                 :                      errmsg("ttdummy (%s): you cannot change %s and/or %s columns (use set_ttdummy)",
 2807 tgl                       364 EUB             :                             relname, args[0], args[1])));
                                365                 : 
 9173 bruce                     366 CBC           9 :         if (newoff != TTDUMMY_INFINITY)
 9328 vadim4o                   367 ECB             :         {
 9173 bruce                     368 GIC           3 :             pfree(relname);     /* allocated in upper executor context */
 8350 tgl                       369               3 :             return PointerGetDatum(NULL);
                                370                 :         }
                                371                 :     }
 2118 tgl                       372 CBC           3 :     else if (oldoff != TTDUMMY_INFINITY)    /* DELETE */
                                373                 :     {
 9173 bruce                     374 LBC           0 :         pfree(relname);
 8350 tgl                       375               0 :         return PointerGetDatum(NULL);
                                376                 :     }
                                377                 : 
 5493 tgl                       378 CBC           9 :     newoff = DirectFunctionCall1(nextval, CStringGetTextDatum("ttdummy_seq"));
                                379                 :     /* nextval now returns int64; coerce down to int32 */
 5493 tgl                       380 GBC           9 :     newoff = Int32GetDatum((int32) DatumGetInt64(newoff));
 9173 bruce                     381 EUB             : 
                                382                 :     /* Connect to SPI manager */
 9328 vadim4o                   383 GIC           9 :     if ((ret = SPI_connect()) < 0)
 9224 bruce                     384 LBC           0 :         elog(ERROR, "ttdummy (%s): SPI_connect returned %d", relname, ret);
                                385                 : 
 9328 vadim4o                   386 ECB             :     /* Fetch tuple values and nulls */
 9173 bruce                     387 GIC           9 :     cvals = (Datum *) palloc(natts * sizeof(Datum));
                                388               9 :     cnulls = (char *) palloc(natts * sizeof(char));
 9328 vadim4o                   389 CBC          45 :     for (i = 0; i < natts; i++)
 9328 vadim4o                   390 EUB             :     {
 9173 bruce                     391 GIC          36 :         cvals[i] = SPI_getbinval((newtuple != NULL) ? newtuple : trigtuple,
                                392                 :                                  tupdesc, i + 1, &isnull);
 9328 vadim4o                   393 CBC          36 :         cnulls[i] = (isnull) ? 'n' : ' ';
 9328 vadim4o                   394 ECB             :     }
 9173 bruce                     395                 : 
                                396                 :     /* change date column(s) */
 9173 bruce                     397 CBC           9 :     if (newtuple)               /* UPDATE */
                                398                 :     {
                                399               6 :         cvals[attnum[0] - 1] = newoff;  /* start_date eq current date */
 9328 vadim4o                   400 GIC           6 :         cnulls[attnum[0] - 1] = ' ';
 2118 tgl                       401               6 :         cvals[attnum[1] - 1] = TTDUMMY_INFINITY;    /* stop_date eq INFINITY */
 9328 vadim4o                   402               6 :         cnulls[attnum[1] - 1] = ' ';
 9328 vadim4o                   403 ECB             :     }
                                404                 :     else
 6758 bruce                     405                 :         /* DELETE */
 9328 vadim4o                   406                 :     {
 9173 bruce                     407 CBC           3 :         cvals[attnum[1] - 1] = newoff;  /* stop_date eq current date */
 9328 vadim4o                   408               3 :         cnulls[attnum[1] - 1] = ' ';
                                409                 :     }
                                410                 : 
                                411                 :     /* if there is no plan ... */
 9328 vadim4o                   412 GIC           9 :     if (splan == NULL)
 9328 vadim4o                   413 ECB             :     {
 5869 tgl                       414                 :         SPIPlanPtr  pplan;
                                415                 :         Oid        *ctypes;
                                416                 :         char       *query;
                                417                 : 
 8570                           418                 :         /* allocate space in preparation */
 9328 vadim4o                   419 GIC           3 :         ctypes = (Oid *) palloc(natts * sizeof(Oid));
 8570 tgl                       420               3 :         query = (char *) palloc(100 + 16 * natts);
                                421                 : 
                                422                 :         /*
                                423                 :          * Construct query: INSERT INTO _relation_ VALUES ($1, ...)
                                424                 :          */
 8570 tgl                       425 CBC           3 :         sprintf(query, "INSERT INTO %s VALUES (", relname);
 9328 vadim4o                   426              15 :         for (i = 1; i <= natts; i++)
                                427                 :         {
 8570 tgl                       428 GIC          12 :             sprintf(query + strlen(query), "$%d%s",
                                429                 :                     i, (i < natts) ? ", " : ")");
 9328 vadim4o                   430              12 :             ctypes[i - 1] = SPI_gettypeid(tupdesc, i);
 9328 vadim4o                   431 ECB             :         }
 9173 bruce                     432                 : 
                                433                 :         /* Prepare plan for query */
 8570 tgl                       434 CBC           3 :         pplan = SPI_prepare(query, natts, ctypes);
 9328 vadim4o                   435 GIC           3 :         if (pplan == NULL)
 2048 peter_e                   436 LBC           0 :             elog(ERROR, "ttdummy (%s): SPI_prepare returned %s", relname, SPI_result_code_string(SPI_result));
                                437                 : 
 4223 tgl                       438 GIC           3 :         if (SPI_keepplan(pplan))
 4223 tgl                       439 UIC           0 :             elog(ERROR, "ttdummy (%s): SPI_keepplan failed", relname);
 9173 bruce                     440 ECB             : 
 9328 vadim4o                   441 CBC           3 :         splan = pplan;
 9328 vadim4o                   442 EUB             :     }
                                443                 : 
 9328 vadim4o                   444 CBC           9 :     ret = SPI_execp(splan, cvals, cnulls, 0);
 9173 bruce                     445 EUB             : 
 9328 vadim4o                   446 GIC           9 :     if (ret < 0)
 9224 bruce                     447 LBC           0 :         elog(ERROR, "ttdummy (%s): SPI_execp returned %d", relname, ret);
                                448                 : 
                                449                 :     /* Tuple to return to upper Executor ... */
 9173 bruce                     450 CBC           9 :     if (newtuple)               /* UPDATE */
 2343 tgl                       451 GIC           6 :         rettuple = SPI_modifytuple(rel, trigtuple, 1, &(attnum[1]), &newoff, NULL);
 2118 tgl                       452 ECB             :     else                        /* DELETE */
 9328 vadim4o                   453 GBC           3 :         rettuple = trigtuple;
                                454                 : 
 9173 bruce                     455 GIC           9 :     SPI_finish();               /* don't forget say Bye to SPI mgr */
 9173 bruce                     456 ECB             : 
 9173 bruce                     457 CBC           9 :     pfree(relname);
                                458                 : 
 8350 tgl                       459               9 :     return PointerGetDatum(rettuple);
                                460                 : }
 9328 vadim4o                   461 ECB             : 
 8175 tgl                       462 GIC           7 : PG_FUNCTION_INFO_V1(set_ttdummy);
 8175 tgl                       463 ECB             : 
                                464                 : Datum
 8335 tgl                       465 CBC           9 : set_ttdummy(PG_FUNCTION_ARGS)
                                466                 : {
 8335 tgl                       467 GIC           9 :     int32       on = PG_GETARG_INT32(0);
 9173 bruce                     468 ECB             : 
 9173 bruce                     469 GIC           9 :     if (ttoff)                  /* OFF currently */
                                470                 :     {
 9328 vadim4o                   471 CBC           3 :         if (on == 0)
 8335 tgl                       472 UIC           0 :             PG_RETURN_INT32(0);
 9173 bruce                     473 ECB             : 
                                474                 :         /* turn ON */
 9328 vadim4o                   475 CBC           3 :         ttoff = false;
 8335 tgl                       476 GIC           3 :         PG_RETURN_INT32(0);
 9328 vadim4o                   477 ECB             :     }
 9173 bruce                     478 EUB             : 
                                479                 :     /* ON currently */
 9328 vadim4o                   480 GIC           6 :     if (on != 0)
 8335 tgl                       481 LBC           0 :         PG_RETURN_INT32(1);
 9173 bruce                     482 ECB             : 
                                483                 :     /* turn OFF */
 9328 vadim4o                   484 GIC           6 :     ttoff = true;
                                485                 : 
 8335 tgl                       486 CBC           6 :     PG_RETURN_INT32(1);
 9328 vadim4o                   487 EUB             : }
                                488                 : 
                                489                 : 
 7535 tgl                       490 ECB             : /*
                                491                 :  * Type int44 has no real-world use, but the regression tests use it
 1867                           492                 :  * (under the alias "city_budget").  It's a four-element vector of int4's.
                                493                 :  */
                                494                 : 
                                495                 : /*
                                496                 :  *      int44in         - converts "num, num, ..." to internal form
                                497                 :  *
                                498                 :  *      Note: Fills any missing positions with zeroes.
                                499                 :  */
 1867 tgl                       500 GIC           7 : PG_FUNCTION_INFO_V1(int44in);
                                501                 : 
                                502                 : Datum
                                503               6 : int44in(PG_FUNCTION_ARGS)
                                504                 : {
 7535                           505               6 :     char       *input_string = PG_GETARG_CSTRING(0);
 7535 tgl                       506 CBC           6 :     int32      *result = (int32 *) palloc(4 * sizeof(int32));
                                507                 :     int         i;
                                508                 : 
                                509               6 :     i = sscanf(input_string,
                                510                 :                "%d, %d, %d, %d",
 7535 tgl                       511 ECB             :                &result[0],
                                512                 :                &result[1],
                                513                 :                &result[2],
                                514                 :                &result[3]);
 7535 tgl                       515 CBC           9 :     while (i < 4)
 7535 tgl                       516 GIC           3 :         result[i++] = 0;
                                517                 : 
                                518               6 :     PG_RETURN_POINTER(result);
                                519                 : }
                                520                 : 
 7535 tgl                       521 ECB             : /*
 1867                           522                 :  *      int44out        - converts internal form to "num, num, ..."
                                523                 :  */
 1867 tgl                       524 CBC          11 : PG_FUNCTION_INFO_V1(int44out);
                                525                 : 
                                526                 : Datum
 1867 tgl                       527 GIC          14 : int44out(PG_FUNCTION_ARGS)
                                528                 : {
 7535                           529              14 :     int32      *an_array = (int32 *) PG_GETARG_POINTER(0);
 1867 tgl                       530 CBC          14 :     char       *result = (char *) palloc(16 * 4);
                                531                 : 
 1867 tgl                       532 GIC          14 :     snprintf(result, 16 * 4, "%d,%d,%d,%d",
 1867 tgl                       533 ECB             :              an_array[0],
 1867 tgl                       534 GIC          14 :              an_array[1],
 1867 tgl                       535 CBC          14 :              an_array[2],
                                536              14 :              an_array[3]);
                                537                 : 
 7535                           538              14 :     PG_RETURN_CSTRING(result);
                                539                 : }
 3568 rhaas                     540 ECB             : 
  433 tgl                       541 CBC           7 : PG_FUNCTION_INFO_V1(test_canonicalize_path);
  433 tgl                       542 ECB             : Datum
  433 tgl                       543 GIC          66 : test_canonicalize_path(PG_FUNCTION_ARGS)
  433 tgl                       544 ECB             : {
  433 tgl                       545 GIC          66 :     char       *path = text_to_cstring(PG_GETARG_TEXT_PP(0));
                                546                 : 
  433 tgl                       547 CBC          66 :     canonicalize_path(path);
  433 tgl                       548 GIC          66 :     PG_RETURN_TEXT_P(cstring_to_text(path));
  433 tgl                       549 ECB             : }
                                550                 : 
 3568 rhaas                     551 CBC           7 : PG_FUNCTION_INFO_V1(make_tuple_indirect);
                                552                 : Datum
                                553              63 : make_tuple_indirect(PG_FUNCTION_ARGS)
 3568 rhaas                     554 ECB             : {
 3568 rhaas                     555 GIC          63 :     HeapTupleHeader rec = PG_GETARG_HEAPTUPLEHEADER(0);
                                556                 :     HeapTupleData tuple;
 3568 rhaas                     557 ECB             :     int         ncolumns;
                                558                 :     Datum      *values;
                                559                 :     bool       *nulls;
                                560                 : 
                                561                 :     Oid         tupType;
                                562                 :     int32       tupTypmod;
                                563                 :     TupleDesc   tupdesc;
                                564                 : 
                                565                 :     HeapTuple   newtup;
                                566                 : 
                                567                 :     int         i;
                                568                 : 
                                569                 :     MemoryContext old_context;
                                570                 : 
                                571                 :     /* Extract type info from the tuple itself */
 3568 rhaas                     572 GIC          63 :     tupType = HeapTupleHeaderGetTypeId(rec);
                                573              63 :     tupTypmod = HeapTupleHeaderGetTypMod(rec);
                                574              63 :     tupdesc = lookup_rowtype_tupdesc(tupType, tupTypmod);
                                575              63 :     ncolumns = tupdesc->natts;
                                576                 : 
                                577                 :     /* Build a temporary HeapTuple control structure */
 3568 rhaas                     578 CBC          63 :     tuple.t_len = HeapTupleHeaderGetDatumLength(rec);
                                579              63 :     ItemPointerSetInvalid(&(tuple.t_self));
                                580              63 :     tuple.t_tableOid = InvalidOid;
                                581              63 :     tuple.t_data = rec;
                                582                 : 
 3568 rhaas                     583 GIC          63 :     values = (Datum *) palloc(ncolumns * sizeof(Datum));
 3568 rhaas                     584 CBC          63 :     nulls = (bool *) palloc(ncolumns * sizeof(bool));
 3568 rhaas                     585 ECB             : 
 3568 rhaas                     586 CBC          63 :     heap_deform_tuple(&tuple, tupdesc, values, nulls);
 3568 rhaas                     587 ECB             : 
 3568 rhaas                     588 GIC          63 :     old_context = MemoryContextSwitchTo(TopTransactionContext);
 3568 rhaas                     589 ECB             : 
 3568 rhaas                     590 CBC         315 :     for (i = 0; i < ncolumns; i++)
                                591                 :     {
 3568 rhaas                     592 ECB             :         struct varlena *attr;
                                593                 :         struct varlena *new_attr;
                                594                 :         struct varatt_indirect redirect_pointer;
                                595                 : 
                                596                 :         /* only work on existing, not-null varlenas */
 2058 andres                    597 GIC         252 :         if (TupleDescAttr(tupdesc, i)->attisdropped ||
 3568 rhaas                     598             252 :             nulls[i] ||
 2058 andres                    599             219 :             TupleDescAttr(tupdesc, i)->attlen != -1)
 3568 rhaas                     600              96 :             continue;
                                601                 : 
                                602             156 :         attr = (struct varlena *) DatumGetPointer(values[i]);
 3568 rhaas                     603 ECB             : 
                                604                 :         /* don't recursively indirect */
 3568 rhaas                     605 CBC         156 :         if (VARATT_IS_EXTERNAL_INDIRECT(attr))
 3568 rhaas                     606 LBC           0 :             continue;
                                607                 : 
 3568 rhaas                     608 ECB             :         /* copy datum, so it still lives later */
 3568 rhaas                     609 GIC         156 :         if (VARATT_IS_EXTERNAL_ONDISK(attr))
 1283 rhaas                     610 UIC           0 :             attr = detoast_external_attr(attr);
 3568 rhaas                     611 ECB             :         else
 3568 rhaas                     612 EUB             :         {
 3568 rhaas                     613 GIC         156 :             struct varlena *oldattr = attr;
                                614                 : 
 3568 rhaas                     615 CBC         156 :             attr = palloc0(VARSIZE_ANY(oldattr));
 3568 rhaas                     616 GBC         156 :             memcpy(attr, oldattr, VARSIZE_ANY(oldattr));
                                617                 :         }
                                618                 : 
 3568 rhaas                     619 ECB             :         /* build indirection Datum */
 3568 rhaas                     620 GIC         156 :         new_attr = (struct varlena *) palloc0(INDIRECT_POINTER_SIZE);
 3568 rhaas                     621 CBC         156 :         redirect_pointer.pointer = attr;
                                622             156 :         SET_VARTAG_EXTERNAL(new_attr, VARTAG_INDIRECT);
 3568 rhaas                     623 GIC         156 :         memcpy(VARDATA_EXTERNAL(new_attr), &redirect_pointer,
                                624                 :                sizeof(redirect_pointer));
                                625                 : 
 3568 rhaas                     626 CBC         156 :         values[i] = PointerGetDatum(new_attr);
 3568 rhaas                     627 ECB             :     }
                                628                 : 
 3568 rhaas                     629 CBC          63 :     newtup = heap_form_tuple(tupdesc, values, nulls);
 3568 rhaas                     630 GIC          63 :     pfree(values);
                                631              63 :     pfree(nulls);
 3568 rhaas                     632 CBC          63 :     ReleaseTupleDesc(tupdesc);
                                633                 : 
 3568 rhaas                     634 GIC          63 :     MemoryContextSwitchTo(old_context);
 3568 rhaas                     635 ECB             : 
 3265 tgl                       636                 :     /*
                                637                 :      * We intentionally don't use PG_RETURN_HEAPTUPLEHEADER here, because that
                                638                 :      * would cause the indirect toast pointers to be flattened out of the
                                639                 :      * tuple immediately, rendering subsequent testing irrelevant.  So just
                                640                 :      * return the HeapTupleHeader pointer as-is.  This violates the general
                                641                 :      * rule that composite Datums shouldn't contain toast pointers, but so
                                642                 :      * long as the regression test scripts don't insert the result of this
                                643                 :      * function into a container type (record, array, etc) it should be OK.
                                644                 :      */
 3265 tgl                       645 GIC          63 :     PG_RETURN_POINTER(newtup->t_data);
                                646                 : }
                                647                 : 
  830                           648               2 : PG_FUNCTION_INFO_V1(regress_setenv);
                                649                 : 
                                650                 : Datum
  830 tgl                       651 CBC           1 : regress_setenv(PG_FUNCTION_ARGS)
                                652                 : {
  830 tgl                       653 GIC           1 :     char       *envvar = text_to_cstring(PG_GETARG_TEXT_PP(0));
  830 tgl                       654 CBC           1 :     char       *envval = text_to_cstring(PG_GETARG_TEXT_PP(1));
                                655                 : 
 3183 noah                      656 GIC           1 :     if (!superuser())
 3183 noah                      657 LBC           0 :         elog(ERROR, "must be superuser to change environment variables");
                                658                 : 
  830 tgl                       659 CBC           1 :     if (setenv(envvar, envval, 1) != 0)
 3183 noah                      660 LBC           0 :         elog(ERROR, "could not set environment variable: %m");
                                661                 : 
 3183 noah                      662 CBC           1 :     PG_RETURN_VOID();
 3183 noah                      663 EUB             : }
                                664                 : 
 3183 noah                      665 ECB             : /* Sleep until no process has a given PID. */
 3183 noah                      666 GBC           3 : PG_FUNCTION_INFO_V1(wait_pid);
                                667                 : 
 3183 noah                      668 ECB             : Datum
 3183 noah                      669 GIC           1 : wait_pid(PG_FUNCTION_ARGS)
                                670                 : {
                                671               1 :     int         pid = PG_GETARG_INT32(0);
 3183 noah                      672 ECB             : 
 3183 noah                      673 GIC           1 :     if (!superuser())
 3183 noah                      674 UIC           0 :         elog(ERROR, "must be superuser to check PID liveness");
 3183 noah                      675 ECB             : 
 3183 noah                      676 GIC          11 :     while (kill(pid, 0) == 0)
 2955 noah                      677 ECB             :     {
 2955 noah                      678 GIC          10 :         CHECK_FOR_INTERRUPTS();
 3183 noah                      679 CBC          10 :         pg_usleep(50000);
 2955 noah                      680 EUB             :     }
                                681                 : 
 3183 noah                      682 CBC           1 :     if (errno != ESRCH)
 3183 noah                      683 UIC           0 :         elog(ERROR, "could not check PID %d liveness: %m", pid);
 3183 noah                      684 ECB             : 
 3183 noah                      685 CBC           1 :     PG_RETURN_VOID();
                                686                 : }
                                687                 : 
 3118 andres                    688 ECB             : static void
 3118 andres                    689 GBC           3 : test_atomic_flag(void)
                                690                 : {
 3118 andres                    691 ECB             :     pg_atomic_flag flag;
                                692                 : 
 3118 andres                    693 GIC           3 :     pg_atomic_init_flag(&flag);
 1282 noah                      694               3 :     EXPECT_TRUE(pg_atomic_unlocked_test_flag(&flag));
 1282 noah                      695 CBC           3 :     EXPECT_TRUE(pg_atomic_test_set_flag(&flag));
 1282 noah                      696 GIC           3 :     EXPECT_TRUE(!pg_atomic_unlocked_test_flag(&flag));
                                697               3 :     EXPECT_TRUE(!pg_atomic_test_set_flag(&flag));
 3118 andres                    698               3 :     pg_atomic_clear_flag(&flag);
 1282 noah                      699 CBC           3 :     EXPECT_TRUE(pg_atomic_unlocked_test_flag(&flag));
                                700               3 :     EXPECT_TRUE(pg_atomic_test_set_flag(&flag));
 3118 andres                    701               3 :     pg_atomic_clear_flag(&flag);
                                702               3 : }
 3118 andres                    703 ECB             : 
                                704                 : static void
 3118 andres                    705 CBC           3 : test_atomic_uint32(void)
 3118 andres                    706 ECB             : {
                                707                 :     pg_atomic_uint32 var;
 2878 bruce                     708                 :     uint32      expected;
                                709                 :     int         i;
                                710                 : 
 3118 andres                    711 CBC           3 :     pg_atomic_init_u32(&var, 0);
 1282 noah                      712 GIC           3 :     EXPECT_EQ_U32(pg_atomic_read_u32(&var), 0);
 3118 andres                    713               3 :     pg_atomic_write_u32(&var, 3);
 1282 noah                      714               3 :     EXPECT_EQ_U32(pg_atomic_read_u32(&var), 3);
                                715               3 :     EXPECT_EQ_U32(pg_atomic_fetch_add_u32(&var, pg_atomic_read_u32(&var) - 2),
                                716                 :                   3);
 1282 noah                      717 CBC           3 :     EXPECT_EQ_U32(pg_atomic_fetch_sub_u32(&var, 1), 4);
                                718               3 :     EXPECT_EQ_U32(pg_atomic_sub_fetch_u32(&var, 3), 0);
                                719               3 :     EXPECT_EQ_U32(pg_atomic_add_fetch_u32(&var, 10), 10);
                                720               3 :     EXPECT_EQ_U32(pg_atomic_exchange_u32(&var, 5), 10);
                                721               3 :     EXPECT_EQ_U32(pg_atomic_exchange_u32(&var, 0), 5);
                                722                 : 
 3118 andres                    723 ECB             :     /* test around numerical limits */
 1282 noah                      724 CBC           3 :     EXPECT_EQ_U32(pg_atomic_fetch_add_u32(&var, INT_MAX), 0);
                                725               3 :     EXPECT_EQ_U32(pg_atomic_fetch_add_u32(&var, INT_MAX), INT_MAX);
 1304                           726               3 :     pg_atomic_fetch_add_u32(&var, 2);   /* wrap to 0 */
 1282                           727               3 :     EXPECT_EQ_U32(pg_atomic_fetch_add_u32(&var, PG_INT16_MAX), 0);
 1282 noah                      728 GIC           3 :     EXPECT_EQ_U32(pg_atomic_fetch_add_u32(&var, PG_INT16_MAX + 1),
                                729                 :                   PG_INT16_MAX);
 1282 noah                      730 CBC           3 :     EXPECT_EQ_U32(pg_atomic_fetch_add_u32(&var, PG_INT16_MIN),
 1282 noah                      731 ECB             :                   2 * PG_INT16_MAX + 1);
 1282 noah                      732 CBC           3 :     EXPECT_EQ_U32(pg_atomic_fetch_add_u32(&var, PG_INT16_MIN - 1),
 1282 noah                      733 ECB             :                   PG_INT16_MAX);
 2878 bruce                     734 CBC           3 :     pg_atomic_fetch_add_u32(&var, 1);   /* top up to UINT_MAX */
 1282 noah                      735 GIC           3 :     EXPECT_EQ_U32(pg_atomic_read_u32(&var), UINT_MAX);
 1282 noah                      736 CBC           3 :     EXPECT_EQ_U32(pg_atomic_fetch_sub_u32(&var, INT_MAX), UINT_MAX);
 1282 noah                      737 GIC           3 :     EXPECT_EQ_U32(pg_atomic_read_u32(&var), (uint32) INT_MAX + 1);
 1282 noah                      738 CBC           3 :     EXPECT_EQ_U32(pg_atomic_sub_fetch_u32(&var, INT_MAX), 1);
 3118 andres                    739 GIC           3 :     pg_atomic_sub_fetch_u32(&var, 1);
  910 noah                      740 CBC           3 :     expected = PG_INT16_MAX;
                                741               3 :     EXPECT_TRUE(!pg_atomic_compare_exchange_u32(&var, &expected, 1));
                                742               3 :     expected = PG_INT16_MAX + 1;
                                743               3 :     EXPECT_TRUE(!pg_atomic_compare_exchange_u32(&var, &expected, 1));
                                744               3 :     expected = PG_INT16_MIN;
                                745               3 :     EXPECT_TRUE(!pg_atomic_compare_exchange_u32(&var, &expected, 1));
                                746               3 :     expected = PG_INT16_MIN - 1;
                                747               3 :     EXPECT_TRUE(!pg_atomic_compare_exchange_u32(&var, &expected, 1));
 3118 andres                    748 ECB             : 
                                749                 :     /* fail exchange because of old expected */
 3118 andres                    750 CBC           3 :     expected = 10;
 1282 noah                      751               3 :     EXPECT_TRUE(!pg_atomic_compare_exchange_u32(&var, &expected, 1));
 3118 andres                    752 ECB             : 
                                753                 :     /* CAS is allowed to fail due to interrupts, try a couple of times */
 3118 andres                    754 GIC           6 :     for (i = 0; i < 1000; i++)
                                755                 :     {
 3118 andres                    756 CBC           6 :         expected = 0;
                                757               6 :         if (!pg_atomic_compare_exchange_u32(&var, &expected, 1))
 3118 andres                    758 GIC           3 :             break;
                                759                 :     }
 3118 andres                    760 CBC           3 :     if (i == 1000)
 3118 andres                    761 UIC           0 :         elog(ERROR, "atomic_compare_exchange_u32() never succeeded");
 1282 noah                      762 CBC           3 :     EXPECT_EQ_U32(pg_atomic_read_u32(&var), 1);
 3118 andres                    763               3 :     pg_atomic_write_u32(&var, 0);
 3118 andres                    764 ECB             : 
                                765                 :     /* try setting flagbits */
 1282 noah                      766 CBC           3 :     EXPECT_TRUE(!(pg_atomic_fetch_or_u32(&var, 1) & 1));
 1282 noah                      767 GBC           3 :     EXPECT_TRUE(pg_atomic_fetch_or_u32(&var, 2) & 1);
 1282 noah                      768 CBC           3 :     EXPECT_EQ_U32(pg_atomic_read_u32(&var), 3);
 3118 andres                    769 ECB             :     /* try clearing flagbits */
 1282 noah                      770 GIC           3 :     EXPECT_EQ_U32(pg_atomic_fetch_and_u32(&var, ~2) & 3, 3);
                                771               3 :     EXPECT_EQ_U32(pg_atomic_fetch_and_u32(&var, ~1), 1);
 3118 andres                    772 ECB             :     /* no bits set anymore */
 1282 noah                      773 CBC           3 :     EXPECT_EQ_U32(pg_atomic_fetch_and_u32(&var, ~0), 0);
 3118 andres                    774               3 : }
                                775                 : 
 3118 andres                    776 ECB             : static void
 3118 andres                    777 CBC           3 : test_atomic_uint64(void)
                                778                 : {
 3118 andres                    779 ECB             :     pg_atomic_uint64 var;
 2878 bruce                     780                 :     uint64      expected;
                                781                 :     int         i;
                                782                 : 
 3118 andres                    783 CBC           3 :     pg_atomic_init_u64(&var, 0);
 1282 noah                      784 GIC           3 :     EXPECT_EQ_U64(pg_atomic_read_u64(&var), 0);
 3118 andres                    785               3 :     pg_atomic_write_u64(&var, 3);
 1282 noah                      786               3 :     EXPECT_EQ_U64(pg_atomic_read_u64(&var), 3);
                                787               3 :     EXPECT_EQ_U64(pg_atomic_fetch_add_u64(&var, pg_atomic_read_u64(&var) - 2),
                                788                 :                   3);
 1282 noah                      789 CBC           3 :     EXPECT_EQ_U64(pg_atomic_fetch_sub_u64(&var, 1), 4);
                                790               3 :     EXPECT_EQ_U64(pg_atomic_sub_fetch_u64(&var, 3), 0);
                                791               3 :     EXPECT_EQ_U64(pg_atomic_add_fetch_u64(&var, 10), 10);
                                792               3 :     EXPECT_EQ_U64(pg_atomic_exchange_u64(&var, 5), 10);
                                793               3 :     EXPECT_EQ_U64(pg_atomic_exchange_u64(&var, 0), 5);
                                794                 : 
 3118 andres                    795 ECB             :     /* fail exchange because of old expected */
 3118 andres                    796 CBC           3 :     expected = 10;
 1282 noah                      797               3 :     EXPECT_TRUE(!pg_atomic_compare_exchange_u64(&var, &expected, 1));
 3118 andres                    798 ECB             : 
                                799                 :     /* CAS is allowed to fail due to interrupts, try a couple of times */
 3118 andres                    800 GIC           6 :     for (i = 0; i < 100; i++)
                                801                 :     {
 3118 andres                    802 CBC           6 :         expected = 0;
                                803               6 :         if (!pg_atomic_compare_exchange_u64(&var, &expected, 1))
 3118 andres                    804 GIC           3 :             break;
                                805                 :     }
 3118 andres                    806 CBC           3 :     if (i == 100)
 3118 andres                    807 UIC           0 :         elog(ERROR, "atomic_compare_exchange_u64() never succeeded");
 1282 noah                      808 CBC           3 :     EXPECT_EQ_U64(pg_atomic_read_u64(&var), 1);
 3118 andres                    809 ECB             : 
 3118 andres                    810 CBC           3 :     pg_atomic_write_u64(&var, 0);
                                811                 : 
 3118 andres                    812 ECB             :     /* try setting flagbits */
 1282 noah                      813 GBC           3 :     EXPECT_TRUE(!(pg_atomic_fetch_or_u64(&var, 1) & 1));
 1282 noah                      814 CBC           3 :     EXPECT_TRUE(pg_atomic_fetch_or_u64(&var, 2) & 1);
 1282 noah                      815 GIC           3 :     EXPECT_EQ_U64(pg_atomic_read_u64(&var), 3);
 3118 andres                    816 ECB             :     /* try clearing flagbits */
 1282 noah                      817 GIC           3 :     EXPECT_EQ_U64((pg_atomic_fetch_and_u64(&var, ~2) & 3), 3);
                                818               3 :     EXPECT_EQ_U64(pg_atomic_fetch_and_u64(&var, ~1), 1);
 3118 andres                    819 ECB             :     /* no bits set anymore */
 1282 noah                      820 CBC           3 :     EXPECT_EQ_U64(pg_atomic_fetch_and_u64(&var, ~0), 0);
 3118 andres                    821               3 : }
                                822                 : 
 1035 andres                    823 ECB             : /*
                                824                 :  * Perform, fairly minimal, testing of the spinlock implementation.
                                825                 :  *
                                826                 :  * It's likely worth expanding these to actually test concurrency etc, but
                                827                 :  * having some regularly run tests is better than none.
                                828                 :  */
                                829                 : static void
 1035 andres                    830 GIC           3 : test_spinlock(void)
                                831                 : {
                                832                 :     /*
                                833                 :      * Basic tests for spinlocks, as well as the underlying operations.
                                834                 :      *
                                835                 :      * We embed the spinlock in a struct with other members to test that the
 1035 andres                    836 ECB             :      * spinlock operations don't perform too wide writes.
                                837                 :      */
                                838                 :     {
                                839                 :         struct test_lock_struct
                                840                 :         {
                                841                 :             char        data_before[4];
                                842                 :             slock_t     lock;
                                843                 :             char        data_after[4];
                                844                 :         }           struct_w_lock;
                                845                 : 
 1035 andres                    846 GIC           3 :         memcpy(struct_w_lock.data_before, "abcd", 4);
                                847               3 :         memcpy(struct_w_lock.data_after, "ef12", 4);
                                848                 : 
                                849                 :         /* test basic operations via the SpinLock* API */
                                850               3 :         SpinLockInit(&struct_w_lock.lock);
                                851               3 :         SpinLockAcquire(&struct_w_lock.lock);
 1035 andres                    852 CBC           3 :         SpinLockRelease(&struct_w_lock.lock);
 1035 andres                    853 ECB             : 
                                854                 :         /* test basic operations via underlying S_* API */
 1035 andres                    855 GIC           3 :         S_INIT_LOCK(&struct_w_lock.lock);
 1035 andres                    856 CBC           3 :         S_LOCK(&struct_w_lock.lock);
                                857               3 :         S_UNLOCK(&struct_w_lock.lock);
 1035 andres                    858 ECB             : 
                                859                 :         /* and that "contended" acquisition works */
 1035 andres                    860 GIC           3 :         s_lock(&struct_w_lock.lock, "testfile", 17, "testfunc");
 1035 andres                    861 CBC           3 :         S_UNLOCK(&struct_w_lock.lock);
 1035 andres                    862 ECB             : 
                                863                 :         /*
                                864                 :          * Check, using TAS directly, that a single spin cycle doesn't block
                                865                 :          * when acquiring an already acquired lock.
                                866                 :          */
                                867                 : #ifdef TAS
 1035 andres                    868 GIC           3 :         S_LOCK(&struct_w_lock.lock);
                                869                 : 
                                870               3 :         if (!TAS(&struct_w_lock.lock))
 1035 andres                    871 UIC           0 :             elog(ERROR, "acquired already held spinlock");
                                872                 : 
                                873                 : #ifdef TAS_SPIN
 1035 andres                    874 CBC           3 :         if (!TAS_SPIN(&struct_w_lock.lock))
 1035 andres                    875 UIC           0 :             elog(ERROR, "acquired already held spinlock");
 1035 andres                    876 ECB             : #endif                          /* defined(TAS_SPIN) */
 1035 andres                    877 EUB             : 
 1035 andres                    878 GIC           3 :         S_UNLOCK(&struct_w_lock.lock);
                                879                 : #endif                          /* defined(TAS) */
 1035 andres                    880 ECB             : 
 1035 andres                    881 EUB             :         /*
                                882                 :          * Verify that after all of this the non-lock contents are still
                                883                 :          * correct.
 1035 andres                    884 ECB             :          */
 1035 andres                    885 GIC           3 :         if (memcmp(struct_w_lock.data_before, "abcd", 4) != 0)
 1035 andres                    886 UIC           0 :             elog(ERROR, "padding before spinlock modified");
 1035 andres                    887 GIC           3 :         if (memcmp(struct_w_lock.data_after, "ef12", 4) != 0)
 1035 andres                    888 UIC           0 :             elog(ERROR, "padding after spinlock modified");
                                889                 :     }
                                890                 : 
 1035 andres                    891 ECB             :     /*
  697 tgl                       892 EUB             :      * Ensure that allocating more than INT32_MAX emulated spinlocks works.
  697 tgl                       893 ECB             :      * That's interesting because the spinlock emulation uses a 32bit integer
  697 tgl                       894 EUB             :      * to map spinlocks onto semaphores. There've been bugs...
                                895                 :      */
                                896                 : #ifndef HAVE_SPINLOCKS
                                897                 :     {
                                898                 :         /*
                                899                 :          * Initialize enough spinlocks to advance counter close to wraparound.
                                900                 :          * It's too expensive to perform acquire/release for each, as those
                                901                 :          * may be syscalls when the spinlock emulation is used (and even just
                                902                 :          * atomic TAS would be expensive).
                                903                 :          */
                                904                 :         for (uint32 i = 0; i < INT32_MAX - 100000; i++)
                                905                 :         {
                                906                 :             slock_t     lock;
                                907                 : 
                                908                 :             SpinLockInit(&lock);
                                909                 :         }
                                910                 : 
                                911                 :         for (uint32 i = 0; i < 200000; i++)
                                912                 :         {
                                913                 :             slock_t     lock;
                                914                 : 
                                915                 :             SpinLockInit(&lock);
                                916                 : 
                                917                 :             SpinLockAcquire(&lock);
                                918                 :             SpinLockRelease(&lock);
                                919                 :             SpinLockAcquire(&lock);
                                920                 :             SpinLockRelease(&lock);
                                921                 :         }
                                922                 :     }
                                923                 : #endif
 1035 andres                    924 GIC           3 : }
                                925                 : 
                                926                 : /*
                                927                 :  * Verify that performing atomic ops inside a spinlock isn't a
                                928                 :  * problem. Realistically that's only going to be a problem when both
                                929                 :  * --disable-spinlocks and --disable-atomics are used, but it's cheap enough
 1035 andres                    930 ECB             :  * to just always test.
                                931                 :  *
                                932                 :  * The test works by initializing enough atomics that we'd conflict if there
                                933                 :  * were an overlap between a spinlock and an atomic by holding a spinlock
                                934                 :  * while manipulating more than NUM_SPINLOCK_SEMAPHORES atomics.
                                935                 :  *
                                936                 :  * NUM_TEST_ATOMICS doesn't really need to be more than
                                937                 :  * NUM_SPINLOCK_SEMAPHORES, but it seems better to test a bit more
                                938                 :  * extensively.
                                939                 :  */
                                940                 : static void
 1035 andres                    941 GIC           3 : test_atomic_spin_nest(void)
                                942                 : {
                                943                 :     slock_t     lock;
                                944                 : #define NUM_TEST_ATOMICS (NUM_SPINLOCK_SEMAPHORES + NUM_ATOMICS_SEMAPHORES + 27)
                                945                 :     pg_atomic_uint32 atomics32[NUM_TEST_ATOMICS];
                                946                 :     pg_atomic_uint64 atomics64[NUM_TEST_ATOMICS];
 1035 andres                    947 ECB             : 
 1035 andres                    948 GIC           3 :     SpinLockInit(&lock);
                                949                 : 
                                950             660 :     for (int i = 0; i < NUM_TEST_ATOMICS; i++)
                                951                 :     {
                                952             657 :         pg_atomic_init_u32(&atomics32[i], 0);
                                953             657 :         pg_atomic_init_u64(&atomics64[i], 0);
 1035 andres                    954 ECB             :     }
                                955                 : 
                                956                 :     /* just so it's not all zeroes */
 1035 andres                    957 GIC         660 :     for (int i = 0; i < NUM_TEST_ATOMICS; i++)
 1035 andres                    958 ECB             :     {
 1035 andres                    959 CBC         657 :         EXPECT_EQ_U32(pg_atomic_fetch_add_u32(&atomics32[i], i), 0);
 1035 andres                    960 GIC         657 :         EXPECT_EQ_U64(pg_atomic_fetch_add_u64(&atomics64[i], i), 0);
                                961                 :     }
                                962                 : 
 1035 andres                    963 ECB             :     /* test whether we can do atomic op with lock held */
 1035 andres                    964 GIC           3 :     SpinLockAcquire(&lock);
 1035 andres                    965 CBC         660 :     for (int i = 0; i < NUM_TEST_ATOMICS; i++)
 1035 andres                    966 ECB             :     {
 1035 andres                    967 GIC         657 :         EXPECT_EQ_U32(pg_atomic_fetch_sub_u32(&atomics32[i], i), i);
                                968             657 :         EXPECT_EQ_U32(pg_atomic_read_u32(&atomics32[i]), 0);
                                969             657 :         EXPECT_EQ_U64(pg_atomic_fetch_sub_u64(&atomics64[i], i), i);
 1035 andres                    970 CBC         657 :         EXPECT_EQ_U64(pg_atomic_read_u64(&atomics64[i]), 0);
 1035 andres                    971 ECB             :     }
 1035 andres                    972 GIC           3 :     SpinLockRelease(&lock);
 1035 andres                    973 CBC           3 : }
 1035 andres                    974 ECB             : #undef NUM_TEST_ATOMICS
                                975                 : 
 3118 andres                    976 CBC           7 : PG_FUNCTION_INFO_V1(test_atomic_ops);
                                977                 : Datum
                                978               3 : test_atomic_ops(PG_FUNCTION_ARGS)
 3118 andres                    979 ECB             : {
 3118 andres                    980 GIC           3 :     test_atomic_flag();
                                981                 : 
 3118 andres                    982 CBC           3 :     test_atomic_uint32();
                                983                 : 
                                984               3 :     test_atomic_uint64();
                                985                 : 
 1035 andres                    986 ECB             :     /*
                                987                 :      * Arguably this shouldn't be tested as part of this function, but it's
                                988                 :      * closely enough related that that seems ok for now.
                                989                 :      */
 1035 andres                    990 CBC           3 :     test_spinlock();
                                991                 : 
 1035 andres                    992 GIC           3 :     test_atomic_spin_nest();
                                993                 : 
 3118                           994               3 :     PG_RETURN_BOOL(true);
                                995                 : }
 2032 rhaas                     996 ECB             : 
 2032 rhaas                     997 GIC           4 : PG_FUNCTION_INFO_V1(test_fdw_handler);
 2032 rhaas                     998 ECB             : Datum
 2032 rhaas                     999 UIC           0 : test_fdw_handler(PG_FUNCTION_ARGS)
 2032 rhaas                    1000 ECB             : {
 1867 tgl                      1001 UIC           0 :     elog(ERROR, "test_fdw_handler is not implemented");
                               1002                 :     PG_RETURN_NULL();
 2032 rhaas                    1003 ECB             : }
                               1004                 : 
 1520 tgl                      1005 GBC           7 : PG_FUNCTION_INFO_V1(test_support_func);
                               1006                 : Datum
                               1007              30 : test_support_func(PG_FUNCTION_ARGS)
                               1008                 : {
 1520 tgl                      1009 GIC          30 :     Node       *rawreq = (Node *) PG_GETARG_POINTER(0);
                               1010              30 :     Node       *ret = NULL;
 1520 tgl                      1011 ECB             : 
 1520 tgl                      1012 GIC          30 :     if (IsA(rawreq, SupportRequestSelectivity))
 1520 tgl                      1013 ECB             :     {
                               1014                 :         /*
                               1015                 :          * Assume that the target is int4eq; that's safe as long as we don't
                               1016                 :          * attach this to any other boolean-returning function.
                               1017                 :          */
 1520 tgl                      1018 CBC           3 :         SupportRequestSelectivity *req = (SupportRequestSelectivity *) rawreq;
                               1019                 :         Selectivity s1;
                               1020                 : 
 1520 tgl                      1021 GIC           3 :         if (req->is_join)
 1520 tgl                      1022 UIC           0 :             s1 = join_selectivity(req->root, Int4EqualOperator,
                               1023                 :                                   req->args,
 1520 tgl                      1024 ECB             :                                   req->inputcollid,
                               1025                 :                                   req->jointype,
 1520 tgl                      1026 UIC           0 :                                   req->sjinfo);
 1520 tgl                      1027 ECB             :         else
 1520 tgl                      1028 GBC           3 :             s1 = restriction_selectivity(req->root, Int4EqualOperator,
                               1029                 :                                          req->args,
                               1030                 :                                          req->inputcollid,
                               1031                 :                                          req->varRelid);
 1520 tgl                      1032 EUB             : 
 1520 tgl                      1033 GIC           3 :         req->selectivity = s1;
 1520 tgl                      1034 CBC           3 :         ret = (Node *) req;
                               1035                 :     }
                               1036                 : 
 1520 tgl                      1037 GIC          30 :     if (IsA(rawreq, SupportRequestCost))
                               1038                 :     {
 1520 tgl                      1039 ECB             :         /* Provide some generic estimate */
 1520 tgl                      1040 CBC           9 :         SupportRequestCost *req = (SupportRequestCost *) rawreq;
                               1041                 : 
 1520 tgl                      1042 GIC           9 :         req->startup = 0;
 1520 tgl                      1043 CBC           9 :         req->per_tuple = 2 * cpu_operator_cost;
 1520 tgl                      1044 GIC           9 :         ret = (Node *) req;
                               1045                 :     }
 1520 tgl                      1046 ECB             : 
 1520 tgl                      1047 GIC          30 :     if (IsA(rawreq, SupportRequestRows))
 1520 tgl                      1048 ECB             :     {
                               1049                 :         /*
                               1050                 :          * Assume that the target is generate_series_int4; that's safe as long
                               1051                 :          * as we don't attach this to any other set-returning function.
                               1052                 :          */
 1520 tgl                      1053 CBC           6 :         SupportRequestRows *req = (SupportRequestRows *) rawreq;
                               1054                 : 
 1520 tgl                      1055 GIC           6 :         if (req->node && IsA(req->node, FuncExpr))    /* be paranoid */
                               1056                 :         {
                               1057               6 :             List       *args = ((FuncExpr *) req->node)->args;
                               1058               6 :             Node       *arg1 = linitial(args);
 1520 tgl                      1059 CBC           6 :             Node       *arg2 = lsecond(args);
                               1060                 : 
                               1061               6 :             if (IsA(arg1, Const) &&
 1520 tgl                      1062 GIC           6 :                 !((Const *) arg1)->constisnull &&
 1520 tgl                      1063 CBC           6 :                 IsA(arg2, Const) &&
                               1064               6 :                 !((Const *) arg2)->constisnull)
 1520 tgl                      1065 ECB             :             {
 1520 tgl                      1066 GIC           6 :                 int32       val1 = DatumGetInt32(((Const *) arg1)->constvalue);
 1520 tgl                      1067 CBC           6 :                 int32       val2 = DatumGetInt32(((Const *) arg2)->constvalue);
 1520 tgl                      1068 ECB             : 
 1520 tgl                      1069 CBC           6 :                 req->rows = val2 - val1 + 1;
                               1070               6 :                 ret = (Node *) req;
                               1071                 :             }
 1520 tgl                      1072 ECB             :         }
                               1073                 :     }
                               1074                 : 
 1520 tgl                      1075 CBC          30 :     PG_RETURN_POINTER(ret);
 1520 tgl                      1076 ECB             : }
                               1077                 : 
 1105 akorotkov                1078 GIC           4 : PG_FUNCTION_INFO_V1(test_opclass_options_func);
                               1079                 : Datum
 1105 akorotkov                1080 UIC           0 : test_opclass_options_func(PG_FUNCTION_ARGS)
 1105 akorotkov                1081 ECB             : {
 1105 akorotkov                1082 UIC           0 :     PG_RETURN_NULL();
                               1083                 : }
  738 heikki.linnakangas       1084 ECB             : 
                               1085                 : /*
  738 heikki.linnakangas       1086 EUB             :  * Call an encoding conversion or verification function.
                               1087                 :  *
                               1088                 :  * Arguments:
                               1089                 :  *  string    bytea -- string to convert
                               1090                 :  *  src_enc   name  -- source encoding
                               1091                 :  *  dest_enc  name  -- destination encoding
                               1092                 :  *  noError   bool  -- if set, don't ereport() on invalid or untranslatable
                               1093                 :  *                     input
                               1094                 :  *
                               1095                 :  * Result is a tuple with two attributes:
                               1096                 :  *  int4    -- number of input bytes successfully converted
                               1097                 :  *  bytea   -- converted string
                               1098                 :  */
  738 heikki.linnakangas       1099 GIC           7 : PG_FUNCTION_INFO_V1(test_enc_conversion);
                               1100                 : Datum
                               1101            4899 : test_enc_conversion(PG_FUNCTION_ARGS)
                               1102                 : {
                               1103            4899 :     bytea      *string = PG_GETARG_BYTEA_PP(0);
                               1104            4899 :     char       *src_encoding_name = NameStr(*PG_GETARG_NAME(1));
  738 heikki.linnakangas       1105 CBC        4899 :     int         src_encoding = pg_char_to_encoding(src_encoding_name);
  738 heikki.linnakangas       1106 GIC        4899 :     char       *dest_encoding_name = NameStr(*PG_GETARG_NAME(2));
  738 heikki.linnakangas       1107 CBC        4899 :     int         dest_encoding = pg_char_to_encoding(dest_encoding_name);
  738 heikki.linnakangas       1108 GIC        4899 :     bool        noError = PG_GETARG_BOOL(3);
  738 heikki.linnakangas       1109 ECB             :     TupleDesc   tupdesc;
                               1110                 :     char       *src;
                               1111                 :     char       *dst;
                               1112                 :     bytea      *retval;
                               1113                 :     Size        srclen;
                               1114                 :     Size        dstsize;
                               1115                 :     Oid         proc;
                               1116                 :     int         convertedbytes;
                               1117                 :     int         dstlen;
                               1118                 :     Datum       values[2];
  267 peter                    1119 GNC        4899 :     bool        nulls[2] = {0};
                               1120                 :     HeapTuple   tuple;
                               1121                 : 
  738 heikki.linnakangas       1122 GIC        4899 :     if (src_encoding < 0)
  738 heikki.linnakangas       1123 UIC           0 :         ereport(ERROR,
                               1124                 :                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
  738 heikki.linnakangas       1125 ECB             :                  errmsg("invalid source encoding name \"%s\"",
                               1126                 :                         src_encoding_name)));
  738 heikki.linnakangas       1127 GIC        4899 :     if (dest_encoding < 0)
  738 heikki.linnakangas       1128 LBC           0 :         ereport(ERROR,
  738 heikki.linnakangas       1129 EUB             :                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                               1130                 :                  errmsg("invalid destination encoding name \"%s\"",
                               1131                 :                         dest_encoding_name)));
                               1132                 : 
  738 heikki.linnakangas       1133 ECB             :     /* Build a tuple descriptor for our result type */
  738 heikki.linnakangas       1134 GBC        4899 :     if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
  738 heikki.linnakangas       1135 UIC           0 :         elog(ERROR, "return type must be a row type");
  738 heikki.linnakangas       1136 GIC        4899 :     tupdesc = BlessTupleDesc(tupdesc);
                               1137                 : 
                               1138            4899 :     srclen = VARSIZE_ANY_EXHDR(string);
                               1139            4899 :     src = VARDATA_ANY(string);
  738 heikki.linnakangas       1140 ECB             : 
  738 heikki.linnakangas       1141 GBC        4899 :     if (src_encoding == dest_encoding)
  738 heikki.linnakangas       1142 ECB             :     {
                               1143                 :         /* just check that the source string is valid */
                               1144                 :         int         oklen;
                               1145                 : 
  738 heikki.linnakangas       1146 GIC        2046 :         oklen = pg_encoding_verifymbstr(src_encoding, src, srclen);
  738 heikki.linnakangas       1147 ECB             : 
  738 heikki.linnakangas       1148 GIC        2046 :         if (oklen == srclen)
                               1149                 :         {
                               1150             516 :             convertedbytes = oklen;
                               1151             516 :             retval = string;
  738 heikki.linnakangas       1152 ECB             :         }
  738 heikki.linnakangas       1153 GIC        1530 :         else if (!noError)
  738 heikki.linnakangas       1154 ECB             :         {
  738 heikki.linnakangas       1155 GIC         765 :             report_invalid_encoding(src_encoding, src + oklen, srclen - oklen);
  738 heikki.linnakangas       1156 ECB             :         }
                               1157                 :         else
                               1158                 :         {
                               1159                 :             /*
                               1160                 :              * build bytea data type structure.
                               1161                 :              */
  738 heikki.linnakangas       1162 GIC         765 :             Assert(oklen < srclen);
                               1163             765 :             convertedbytes = oklen;
                               1164             765 :             retval = (bytea *) palloc(oklen + VARHDRSZ);
                               1165             765 :             SET_VARSIZE(retval, oklen + VARHDRSZ);
                               1166             765 :             memcpy(VARDATA(retval), src, oklen);
                               1167                 :         }
  738 heikki.linnakangas       1168 ECB             :     }
                               1169                 :     else
                               1170                 :     {
  738 heikki.linnakangas       1171 CBC        2853 :         proc = FindDefaultConversionProc(src_encoding, dest_encoding);
                               1172            2853 :         if (!OidIsValid(proc))
  738 heikki.linnakangas       1173 UIC           0 :             ereport(ERROR,
                               1174                 :                     (errcode(ERRCODE_UNDEFINED_FUNCTION),
                               1175                 :                      errmsg("default conversion function for encoding \"%s\" to \"%s\" does not exist",
                               1176                 :                             pg_encoding_to_char(src_encoding),
  738 heikki.linnakangas       1177 ECB             :                             pg_encoding_to_char(dest_encoding))));
                               1178                 : 
  738 heikki.linnakangas       1179 GBC        2853 :         if (srclen >= (MaxAllocSize / (Size) MAX_CONVERSION_GROWTH))
  738 heikki.linnakangas       1180 UIC           0 :             ereport(ERROR,
                               1181                 :                     (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
                               1182                 :                      errmsg("out of memory"),
                               1183                 :                      errdetail("String of %d bytes is too long for encoding conversion.",
                               1184                 :                                (int) srclen)));
  738 heikki.linnakangas       1185 ECB             : 
  738 heikki.linnakangas       1186 GBC        2853 :         dstsize = (Size) srclen * MAX_CONVERSION_GROWTH + 1;
  738 heikki.linnakangas       1187 GIC        2853 :         dst = MemoryContextAlloc(CurrentMemoryContext, dstsize);
                               1188                 : 
                               1189                 :         /* perform conversion */
                               1190            2853 :         convertedbytes = pg_do_encoding_conversion_buf(proc,
                               1191                 :                                                        src_encoding,
  738 heikki.linnakangas       1192 ECB             :                                                        dest_encoding,
                               1193                 :                                                        (unsigned char *) src, srclen,
                               1194                 :                                                        (unsigned char *) dst, dstsize,
                               1195                 :                                                        noError);
  738 heikki.linnakangas       1196 CBC        1683 :         dstlen = strlen(dst);
                               1197                 : 
                               1198                 :         /*
                               1199                 :          * build bytea data type structure.
                               1200                 :          */
  738 heikki.linnakangas       1201 GIC        1683 :         retval = (bytea *) palloc(dstlen + VARHDRSZ);
  738 heikki.linnakangas       1202 CBC        1683 :         SET_VARSIZE(retval, dstlen + VARHDRSZ);
  738 heikki.linnakangas       1203 GIC        1683 :         memcpy(VARDATA(retval), dst, dstlen);
                               1204                 : 
                               1205            1683 :         pfree(dst);
                               1206                 :     }
  738 heikki.linnakangas       1207 ECB             : 
  738 heikki.linnakangas       1208 CBC        2964 :     values[0] = Int32GetDatum(convertedbytes);
  738 heikki.linnakangas       1209 GIC        2964 :     values[1] = PointerGetDatum(retval);
  738 heikki.linnakangas       1210 CBC        2964 :     tuple = heap_form_tuple(tupdesc, values, nulls);
                               1211                 : 
  738 heikki.linnakangas       1212 GIC        2964 :     PG_RETURN_DATUM(HeapTupleGetDatum(tuple));
  738 heikki.linnakangas       1213 ECB             : }
  698 tgl                      1214                 : 
                               1215                 : /* Provide SQL access to IsBinaryCoercible() */
  698 tgl                      1216 GIC           7 : PG_FUNCTION_INFO_V1(binary_coercible);
  698 tgl                      1217 ECB             : Datum
  698 tgl                      1218 GIC       18660 : binary_coercible(PG_FUNCTION_ARGS)
                               1219                 : {
                               1220           18660 :     Oid         srctype = PG_GETARG_OID(0);
  698 tgl                      1221 CBC       18660 :     Oid         targettype = PG_GETARG_OID(1);
                               1222                 : 
                               1223           18660 :     PG_RETURN_BOOL(IsBinaryCoercible(srctype, targettype));
                               1224                 : }
  367 akapila                  1225 ECB             : 
                               1226                 : /*
                               1227                 :  * Return the length of the portion of a tuple consisting of the given array
  352 noah                     1228                 :  * of data types.  The input data types must be fixed-length data types.
                               1229                 :  */
  352 noah                     1230 GIC           7 : PG_FUNCTION_INFO_V1(get_columns_length);
                               1231                 : Datum
                               1232              18 : get_columns_length(PG_FUNCTION_ARGS)
                               1233                 : {
  332 tgl                      1234              18 :     ArrayType  *ta = PG_GETARG_ARRAYTYPE_P(0);
  332 tgl                      1235 ECB             :     Oid        *type_oids;
                               1236                 :     int         ntypes;
  367 akapila                  1237 CBC          18 :     int         column_offset = 0;
                               1238                 : 
                               1239              18 :     if (ARR_HASNULL(ta) && array_contains_nulls(ta))
  367 akapila                  1240 UIC           0 :         elog(ERROR, "argument must not contain nulls");
                               1241                 : 
  367 akapila                  1242 CBC          18 :     if (ARR_NDIM(ta) > 1)
  367 akapila                  1243 UIC           0 :         elog(ERROR, "argument must be empty or one-dimensional array");
  367 akapila                  1244 ECB             : 
  367 akapila                  1245 GBC          18 :     type_oids = (Oid *) ARR_DATA_PTR(ta);
  367 akapila                  1246 GIC          18 :     ntypes = ArrayGetNItems(ARR_NDIM(ta), ARR_DIMS(ta));
  367 akapila                  1247 CBC          84 :     for (int i = 0; i < ntypes; i++)
  367 akapila                  1248 EUB             :     {
  332 tgl                      1249 GIC          66 :         Oid         typeoid = type_oids[i];
  367 akapila                  1250 ECB             :         int16       typlen;
                               1251                 :         bool        typbyval;
                               1252                 :         char        typalign;
                               1253                 : 
  367 akapila                  1254 CBC          66 :         get_typlenbyvalalign(typeoid, &typlen, &typbyval, &typalign);
                               1255                 : 
                               1256                 :         /* the data type must be fixed-length */
  352 noah                     1257 GIC          66 :         if (typlen < 0)
  367 akapila                  1258 UIC           0 :             elog(ERROR, "type %u is not fixed-length data type", typeoid);
  367 akapila                  1259 ECB             : 
  352 noah                     1260 GIC          66 :         column_offset = att_align_nominal(column_offset + typlen, typalign);
                               1261                 :     }
  367 akapila                  1262 ECB             : 
  367 akapila                  1263 GBC          18 :     PG_RETURN_INT32(column_offset);
                               1264                 : }
        

Generated by: LCOV version v1.16-55-g56c0a2a