LCOV - code coverage report
Current view: top level - src/test/regress - regress.c (source / functions) Hit Total Coverage
Test: PostgreSQL 13beta1 Lines: 338 374 90.4 %
Date: 2020-06-01 09:07:10 Functions: 39 41 95.1 %
Legend: Lines: hit not hit

          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-2020, 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/pg_operator.h"
      27             : #include "catalog/pg_type.h"
      28             : #include "commands/sequence.h"
      29             : #include "commands/trigger.h"
      30             : #include "executor/executor.h"
      31             : #include "executor/spi.h"
      32             : #include "miscadmin.h"
      33             : #include "nodes/supportnodes.h"
      34             : #include "optimizer/optimizer.h"
      35             : #include "optimizer/plancat.h"
      36             : #include "port/atomics.h"
      37             : #include "utils/builtins.h"
      38             : #include "utils/geo_decls.h"
      39             : #include "utils/memutils.h"
      40             : #include "utils/rel.h"
      41             : #include "utils/typcache.h"
      42             : 
      43             : #define EXPECT_TRUE(expr)   \
      44             :     do { \
      45             :         if (!(expr)) \
      46             :             elog(ERROR, \
      47             :                  "%s was unexpectedly false in file \"%s\" line %u", \
      48             :                  #expr, __FILE__, __LINE__); \
      49             :     } while (0)
      50             : 
      51             : #define EXPECT_EQ_U32(result_expr, expected_expr)   \
      52             :     do { \
      53             :         uint32      result = (result_expr); \
      54             :         uint32      expected = (expected_expr); \
      55             :         if (result != expected) \
      56             :             elog(ERROR, \
      57             :                  "%s yielded %u, expected %s in file \"%s\" line %u", \
      58             :                  #result_expr, result, #expected_expr, __FILE__, __LINE__); \
      59             :     } while (0)
      60             : 
      61             : #define EXPECT_EQ_U64(result_expr, expected_expr)   \
      62             :     do { \
      63             :         uint64      result = (result_expr); \
      64             :         uint64      expected = (expected_expr); \
      65             :         if (result != expected) \
      66             :             elog(ERROR, \
      67             :                  "%s yielded " UINT64_FORMAT ", expected %s in file \"%s\" line %u", \
      68             :                  #result_expr, result, #expected_expr, __FILE__, __LINE__); \
      69             :     } while (0)
      70             : 
      71             : #define LDELIM          '('
      72             : #define RDELIM          ')'
      73             : #define DELIM           ','
      74             : 
      75             : static void regress_lseg_construct(LSEG *lseg, Point *pt1, Point *pt2);
      76             : 
      77          60 : PG_MODULE_MAGIC;
      78             : 
      79             : 
      80             : /* return the point where two paths intersect, or NULL if no intersection. */
      81          10 : PG_FUNCTION_INFO_V1(interpt_pp);
      82             : 
      83             : Datum
      84        3584 : interpt_pp(PG_FUNCTION_ARGS)
      85             : {
      86        3584 :     PATH       *p1 = PG_GETARG_PATH_P(0);
      87        3584 :     PATH       *p2 = PG_GETARG_PATH_P(1);
      88             :     int         i,
      89             :                 j;
      90             :     LSEG        seg1,
      91             :                 seg2;
      92             :     bool        found;          /* We've found the intersection */
      93             : 
      94        3584 :     found = false;              /* Haven't found it yet */
      95             : 
      96       11764 :     for (i = 0; i < p1->npts - 1 && !found; i++)
      97             :     {
      98        8180 :         regress_lseg_construct(&seg1, &p1->p[i], &p1->p[i + 1]);
      99       25092 :         for (j = 0; j < p2->npts - 1 && !found; j++)
     100             :         {
     101       16912 :             regress_lseg_construct(&seg2, &p2->p[j], &p2->p[j + 1]);
     102       16912 :             if (DatumGetBool(DirectFunctionCall2(lseg_intersect,
     103             :                                                  LsegPGetDatum(&seg1),
     104             :                                                  LsegPGetDatum(&seg2))))
     105        3576 :                 found = true;
     106             :         }
     107             :     }
     108             : 
     109        3584 :     if (!found)
     110           8 :         PG_RETURN_NULL();
     111             : 
     112             :     /*
     113             :      * Note: DirectFunctionCall2 will kick out an error if lseg_interpt()
     114             :      * returns NULL, but that should be impossible since we know the two
     115             :      * segments intersect.
     116             :      */
     117        3576 :     PG_RETURN_DATUM(DirectFunctionCall2(lseg_interpt,
     118             :                                         LsegPGetDatum(&seg1),
     119             :                                         LsegPGetDatum(&seg2)));
     120             : }
     121             : 
     122             : 
     123             : /* like lseg_construct, but assume space already allocated */
     124             : static void
     125       25092 : regress_lseg_construct(LSEG *lseg, Point *pt1, Point *pt2)
     126             : {
     127       25092 :     lseg->p[0].x = pt1->x;
     128       25092 :     lseg->p[0].y = pt1->y;
     129       25092 :     lseg->p[1].x = pt2->x;
     130       25092 :     lseg->p[1].y = pt2->y;
     131       25092 : }
     132             : 
     133          10 : PG_FUNCTION_INFO_V1(overpaid);
     134             : 
     135             : Datum
     136          24 : overpaid(PG_FUNCTION_ARGS)
     137             : {
     138          24 :     HeapTupleHeader tuple = PG_GETARG_HEAPTUPLEHEADER(0);
     139             :     bool        isnull;
     140             :     int32       salary;
     141             : 
     142          24 :     salary = DatumGetInt32(GetAttributeByName(tuple, "salary", &isnull));
     143          24 :     if (isnull)
     144           0 :         PG_RETURN_NULL();
     145          24 :     PG_RETURN_BOOL(salary > 699);
     146             : }
     147             : 
     148             : /* New type "widget"
     149             :  * This used to be "circle", but I added circle to builtins,
     150             :  *  so needed to make sure the names do not collide. - tgl 97/04/21
     151             :  */
     152             : 
     153             : typedef struct
     154             : {
     155             :     Point       center;
     156             :     double      radius;
     157             : } WIDGET;
     158             : 
     159          14 : PG_FUNCTION_INFO_V1(widget_in);
     160          10 : PG_FUNCTION_INFO_V1(widget_out);
     161             : 
     162             : #define NARGS   3
     163             : 
     164             : Datum
     165          16 : widget_in(PG_FUNCTION_ARGS)
     166             : {
     167          16 :     char       *str = PG_GETARG_CSTRING(0);
     168             :     char       *p,
     169             :                *coord[NARGS];
     170             :     int         i;
     171             :     WIDGET     *result;
     172             : 
     173         112 :     for (i = 0, p = str; *p && i < NARGS && *p != RDELIM; p++)
     174             :     {
     175          96 :         if (*p == DELIM || (*p == LDELIM && i == 0))
     176          48 :             coord[i++] = p + 1;
     177             :     }
     178             : 
     179          16 :     if (i < NARGS)
     180           0 :         ereport(ERROR,
     181             :                 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
     182             :                  errmsg("invalid input syntax for type %s: \"%s\"",
     183             :                         "widget", str)));
     184             : 
     185          16 :     result = (WIDGET *) palloc(sizeof(WIDGET));
     186          16 :     result->center.x = atof(coord[0]);
     187          16 :     result->center.y = atof(coord[1]);
     188          16 :     result->radius = atof(coord[2]);
     189             : 
     190          16 :     PG_RETURN_POINTER(result);
     191             : }
     192             : 
     193             : Datum
     194           8 : widget_out(PG_FUNCTION_ARGS)
     195             : {
     196           8 :     WIDGET     *widget = (WIDGET *) PG_GETARG_POINTER(0);
     197           8 :     char       *str = psprintf("(%g,%g,%g)",
     198             :                                widget->center.x, widget->center.y, widget->radius);
     199             : 
     200           8 :     PG_RETURN_CSTRING(str);
     201             : }
     202             : 
     203          10 : PG_FUNCTION_INFO_V1(pt_in_widget);
     204             : 
     205             : Datum
     206           8 : pt_in_widget(PG_FUNCTION_ARGS)
     207             : {
     208           8 :     Point      *point = PG_GETARG_POINT_P(0);
     209           8 :     WIDGET     *widget = (WIDGET *) PG_GETARG_POINTER(1);
     210             :     float8      distance;
     211             : 
     212           8 :     distance = DatumGetFloat8(DirectFunctionCall2(point_distance,
     213             :                                                   PointPGetDatum(point),
     214             :                                                   PointPGetDatum(&widget->center)));
     215             : 
     216           8 :     PG_RETURN_BOOL(distance < widget->radius);
     217             : }
     218             : 
     219          10 : PG_FUNCTION_INFO_V1(reverse_name);
     220             : 
     221             : Datum
     222          32 : reverse_name(PG_FUNCTION_ARGS)
     223             : {
     224          32 :     char       *string = PG_GETARG_CSTRING(0);
     225             :     int         i;
     226             :     int         len;
     227             :     char       *new_string;
     228             : 
     229          32 :     new_string = palloc0(NAMEDATALEN);
     230         224 :     for (i = 0; i < NAMEDATALEN && string[i]; ++i)
     231             :         ;
     232          32 :     if (i == NAMEDATALEN || !string[i])
     233          32 :         --i;
     234          32 :     len = i;
     235         224 :     for (; i >= 0; --i)
     236         192 :         new_string[len - i] = string[i];
     237          32 :     PG_RETURN_CSTRING(new_string);
     238             : }
     239             : 
     240          10 : PG_FUNCTION_INFO_V1(trigger_return_old);
     241             : 
     242             : Datum
     243          48 : trigger_return_old(PG_FUNCTION_ARGS)
     244             : {
     245          48 :     TriggerData *trigdata = (TriggerData *) fcinfo->context;
     246             :     HeapTuple   tuple;
     247             : 
     248          48 :     if (!CALLED_AS_TRIGGER(fcinfo))
     249           0 :         elog(ERROR, "trigger_return_old: not fired by trigger manager");
     250             : 
     251          48 :     tuple = trigdata->tg_trigtuple;
     252             : 
     253          48 :     return PointerGetDatum(tuple);
     254             : }
     255             : 
     256             : #define TTDUMMY_INFINITY    999999
     257             : 
     258             : static SPIPlanPtr splan = NULL;
     259             : static bool ttoff = false;
     260             : 
     261          10 : PG_FUNCTION_INFO_V1(ttdummy);
     262             : 
     263             : Datum
     264          40 : ttdummy(PG_FUNCTION_ARGS)
     265             : {
     266          40 :     TriggerData *trigdata = (TriggerData *) fcinfo->context;
     267             :     Trigger    *trigger;        /* to get trigger name */
     268             :     char      **args;           /* arguments */
     269             :     int         attnum[2];      /* fnumbers of start/stop columns */
     270             :     Datum       oldon,
     271             :                 oldoff;
     272             :     Datum       newon,
     273             :                 newoff;
     274             :     Datum      *cvals;          /* column values */
     275             :     char       *cnulls;         /* column nulls */
     276             :     char       *relname;        /* triggered relation name */
     277             :     Relation    rel;            /* triggered relation */
     278             :     HeapTuple   trigtuple;
     279          40 :     HeapTuple   newtuple = NULL;
     280             :     HeapTuple   rettuple;
     281             :     TupleDesc   tupdesc;        /* tuple description */
     282             :     int         natts;          /* # of attributes */
     283             :     bool        isnull;         /* to know is some column NULL or not */
     284             :     int         ret;
     285             :     int         i;
     286             : 
     287          40 :     if (!CALLED_AS_TRIGGER(fcinfo))
     288           0 :         elog(ERROR, "ttdummy: not fired by trigger manager");
     289          40 :     if (!TRIGGER_FIRED_FOR_ROW(trigdata->tg_event))
     290           0 :         elog(ERROR, "ttdummy: must be fired for row");
     291          40 :     if (!TRIGGER_FIRED_BEFORE(trigdata->tg_event))
     292           0 :         elog(ERROR, "ttdummy: must be fired before event");
     293          40 :     if (TRIGGER_FIRED_BY_INSERT(trigdata->tg_event))
     294           0 :         elog(ERROR, "ttdummy: cannot process INSERT event");
     295          40 :     if (TRIGGER_FIRED_BY_UPDATE(trigdata->tg_event))
     296          32 :         newtuple = trigdata->tg_newtuple;
     297             : 
     298          40 :     trigtuple = trigdata->tg_trigtuple;
     299             : 
     300          40 :     rel = trigdata->tg_relation;
     301          40 :     relname = SPI_getrelname(rel);
     302             : 
     303             :     /* check if TT is OFF for this relation */
     304          40 :     if (ttoff)                  /* OFF - nothing to do */
     305             :     {
     306          20 :         pfree(relname);
     307          20 :         return PointerGetDatum((newtuple != NULL) ? newtuple : trigtuple);
     308             :     }
     309             : 
     310          20 :     trigger = trigdata->tg_trigger;
     311             : 
     312          20 :     if (trigger->tgnargs != 2)
     313           0 :         elog(ERROR, "ttdummy (%s): invalid (!= 2) number of arguments %d",
     314             :              relname, trigger->tgnargs);
     315             : 
     316          20 :     args = trigger->tgargs;
     317          20 :     tupdesc = rel->rd_att;
     318          20 :     natts = tupdesc->natts;
     319             : 
     320          60 :     for (i = 0; i < 2; i++)
     321             :     {
     322          40 :         attnum[i] = SPI_fnumber(tupdesc, args[i]);
     323          40 :         if (attnum[i] <= 0)
     324           0 :             elog(ERROR, "ttdummy (%s): there is no attribute %s",
     325             :                  relname, args[i]);
     326          40 :         if (SPI_gettypeid(tupdesc, attnum[i]) != INT4OID)
     327           0 :             elog(ERROR, "ttdummy (%s): attribute %s must be of integer type",
     328             :                  relname, args[i]);
     329             :     }
     330             : 
     331          20 :     oldon = SPI_getbinval(trigtuple, tupdesc, attnum[0], &isnull);
     332          20 :     if (isnull)
     333           0 :         elog(ERROR, "ttdummy (%s): %s must be NOT NULL", relname, args[0]);
     334             : 
     335          20 :     oldoff = SPI_getbinval(trigtuple, tupdesc, attnum[1], &isnull);
     336          20 :     if (isnull)
     337           0 :         elog(ERROR, "ttdummy (%s): %s must be NOT NULL", relname, args[1]);
     338             : 
     339          20 :     if (newtuple != NULL)       /* UPDATE */
     340             :     {
     341          16 :         newon = SPI_getbinval(newtuple, tupdesc, attnum[0], &isnull);
     342          16 :         if (isnull)
     343           0 :             elog(ERROR, "ttdummy (%s): %s must be NOT NULL", relname, args[0]);
     344          16 :         newoff = SPI_getbinval(newtuple, tupdesc, attnum[1], &isnull);
     345          16 :         if (isnull)
     346           0 :             elog(ERROR, "ttdummy (%s): %s must be NOT NULL", relname, args[1]);
     347             : 
     348          16 :         if (oldon != newon || oldoff != newoff)
     349           4 :             ereport(ERROR,
     350             :                     (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
     351             :                      errmsg("ttdummy (%s): you cannot change %s and/or %s columns (use set_ttdummy)",
     352             :                             relname, args[0], args[1])));
     353             : 
     354          12 :         if (newoff != TTDUMMY_INFINITY)
     355             :         {
     356           4 :             pfree(relname);     /* allocated in upper executor context */
     357           4 :             return PointerGetDatum(NULL);
     358             :         }
     359             :     }
     360           4 :     else if (oldoff != TTDUMMY_INFINITY)    /* DELETE */
     361             :     {
     362           0 :         pfree(relname);
     363           0 :         return PointerGetDatum(NULL);
     364             :     }
     365             : 
     366          12 :     newoff = DirectFunctionCall1(nextval, CStringGetTextDatum("ttdummy_seq"));
     367             :     /* nextval now returns int64; coerce down to int32 */
     368          12 :     newoff = Int32GetDatum((int32) DatumGetInt64(newoff));
     369             : 
     370             :     /* Connect to SPI manager */
     371          12 :     if ((ret = SPI_connect()) < 0)
     372           0 :         elog(ERROR, "ttdummy (%s): SPI_connect returned %d", relname, ret);
     373             : 
     374             :     /* Fetch tuple values and nulls */
     375          12 :     cvals = (Datum *) palloc(natts * sizeof(Datum));
     376          12 :     cnulls = (char *) palloc(natts * sizeof(char));
     377          60 :     for (i = 0; i < natts; i++)
     378             :     {
     379          48 :         cvals[i] = SPI_getbinval((newtuple != NULL) ? newtuple : trigtuple,
     380             :                                  tupdesc, i + 1, &isnull);
     381          48 :         cnulls[i] = (isnull) ? 'n' : ' ';
     382             :     }
     383             : 
     384             :     /* change date column(s) */
     385          12 :     if (newtuple)               /* UPDATE */
     386             :     {
     387           8 :         cvals[attnum[0] - 1] = newoff;  /* start_date eq current date */
     388           8 :         cnulls[attnum[0] - 1] = ' ';
     389           8 :         cvals[attnum[1] - 1] = TTDUMMY_INFINITY;    /* stop_date eq INFINITY */
     390           8 :         cnulls[attnum[1] - 1] = ' ';
     391             :     }
     392             :     else
     393             :         /* DELETE */
     394             :     {
     395           4 :         cvals[attnum[1] - 1] = newoff;  /* stop_date eq current date */
     396           4 :         cnulls[attnum[1] - 1] = ' ';
     397             :     }
     398             : 
     399             :     /* if there is no plan ... */
     400          12 :     if (splan == NULL)
     401             :     {
     402             :         SPIPlanPtr  pplan;
     403             :         Oid        *ctypes;
     404             :         char       *query;
     405             : 
     406             :         /* allocate space in preparation */
     407           4 :         ctypes = (Oid *) palloc(natts * sizeof(Oid));
     408           4 :         query = (char *) palloc(100 + 16 * natts);
     409             : 
     410             :         /*
     411             :          * Construct query: INSERT INTO _relation_ VALUES ($1, ...)
     412             :          */
     413           4 :         sprintf(query, "INSERT INTO %s VALUES (", relname);
     414          20 :         for (i = 1; i <= natts; i++)
     415             :         {
     416          16 :             sprintf(query + strlen(query), "$%d%s",
     417             :                     i, (i < natts) ? ", " : ")");
     418          16 :             ctypes[i - 1] = SPI_gettypeid(tupdesc, i);
     419             :         }
     420             : 
     421             :         /* Prepare plan for query */
     422           4 :         pplan = SPI_prepare(query, natts, ctypes);
     423           4 :         if (pplan == NULL)
     424           0 :             elog(ERROR, "ttdummy (%s): SPI_prepare returned %s", relname, SPI_result_code_string(SPI_result));
     425             : 
     426           4 :         if (SPI_keepplan(pplan))
     427           0 :             elog(ERROR, "ttdummy (%s): SPI_keepplan failed", relname);
     428             : 
     429           4 :         splan = pplan;
     430             :     }
     431             : 
     432          12 :     ret = SPI_execp(splan, cvals, cnulls, 0);
     433             : 
     434          12 :     if (ret < 0)
     435           0 :         elog(ERROR, "ttdummy (%s): SPI_execp returned %d", relname, ret);
     436             : 
     437             :     /* Tuple to return to upper Executor ... */
     438          12 :     if (newtuple)               /* UPDATE */
     439           8 :         rettuple = SPI_modifytuple(rel, trigtuple, 1, &(attnum[1]), &newoff, NULL);
     440             :     else                        /* DELETE */
     441           4 :         rettuple = trigtuple;
     442             : 
     443          12 :     SPI_finish();               /* don't forget say Bye to SPI mgr */
     444             : 
     445          12 :     pfree(relname);
     446             : 
     447          12 :     return PointerGetDatum(rettuple);
     448             : }
     449             : 
     450          10 : PG_FUNCTION_INFO_V1(set_ttdummy);
     451             : 
     452             : Datum
     453          12 : set_ttdummy(PG_FUNCTION_ARGS)
     454             : {
     455          12 :     int32       on = PG_GETARG_INT32(0);
     456             : 
     457          12 :     if (ttoff)                  /* OFF currently */
     458             :     {
     459           4 :         if (on == 0)
     460           0 :             PG_RETURN_INT32(0);
     461             : 
     462             :         /* turn ON */
     463           4 :         ttoff = false;
     464           4 :         PG_RETURN_INT32(0);
     465             :     }
     466             : 
     467             :     /* ON currently */
     468           8 :     if (on != 0)
     469           0 :         PG_RETURN_INT32(1);
     470             : 
     471             :     /* turn OFF */
     472           8 :     ttoff = true;
     473             : 
     474           8 :     PG_RETURN_INT32(1);
     475             : }
     476             : 
     477             : 
     478             : /*
     479             :  * Type int44 has no real-world use, but the regression tests use it
     480             :  * (under the alias "city_budget").  It's a four-element vector of int4's.
     481             :  */
     482             : 
     483             : /*
     484             :  *      int44in         - converts "num, num, ..." to internal form
     485             :  *
     486             :  *      Note: Fills any missing positions with zeroes.
     487             :  */
     488          10 : PG_FUNCTION_INFO_V1(int44in);
     489             : 
     490             : Datum
     491           8 : int44in(PG_FUNCTION_ARGS)
     492             : {
     493           8 :     char       *input_string = PG_GETARG_CSTRING(0);
     494           8 :     int32      *result = (int32 *) palloc(4 * sizeof(int32));
     495             :     int         i;
     496             : 
     497           8 :     i = sscanf(input_string,
     498             :                "%d, %d, %d, %d",
     499             :                &result[0],
     500             :                &result[1],
     501             :                &result[2],
     502             :                &result[3]);
     503          12 :     while (i < 4)
     504           4 :         result[i++] = 0;
     505             : 
     506           8 :     PG_RETURN_POINTER(result);
     507             : }
     508             : 
     509             : /*
     510             :  *      int44out        - converts internal form to "num, num, ..."
     511             :  */
     512          14 : PG_FUNCTION_INFO_V1(int44out);
     513             : 
     514             : Datum
     515          16 : int44out(PG_FUNCTION_ARGS)
     516             : {
     517          16 :     int32      *an_array = (int32 *) PG_GETARG_POINTER(0);
     518          16 :     char       *result = (char *) palloc(16 * 4);
     519             : 
     520          48 :     snprintf(result, 16 * 4, "%d,%d,%d,%d",
     521             :              an_array[0],
     522          16 :              an_array[1],
     523          16 :              an_array[2],
     524          16 :              an_array[3]);
     525             : 
     526          16 :     PG_RETURN_CSTRING(result);
     527             : }
     528             : 
     529          10 : PG_FUNCTION_INFO_V1(make_tuple_indirect);
     530             : Datum
     531          84 : make_tuple_indirect(PG_FUNCTION_ARGS)
     532             : {
     533          84 :     HeapTupleHeader rec = PG_GETARG_HEAPTUPLEHEADER(0);
     534             :     HeapTupleData tuple;
     535             :     int         ncolumns;
     536             :     Datum      *values;
     537             :     bool       *nulls;
     538             : 
     539             :     Oid         tupType;
     540             :     int32       tupTypmod;
     541             :     TupleDesc   tupdesc;
     542             : 
     543             :     HeapTuple   newtup;
     544             : 
     545             :     int         i;
     546             : 
     547             :     MemoryContext old_context;
     548             : 
     549             :     /* Extract type info from the tuple itself */
     550          84 :     tupType = HeapTupleHeaderGetTypeId(rec);
     551          84 :     tupTypmod = HeapTupleHeaderGetTypMod(rec);
     552          84 :     tupdesc = lookup_rowtype_tupdesc(tupType, tupTypmod);
     553          84 :     ncolumns = tupdesc->natts;
     554             : 
     555             :     /* Build a temporary HeapTuple control structure */
     556          84 :     tuple.t_len = HeapTupleHeaderGetDatumLength(rec);
     557          84 :     ItemPointerSetInvalid(&(tuple.t_self));
     558          84 :     tuple.t_tableOid = InvalidOid;
     559          84 :     tuple.t_data = rec;
     560             : 
     561          84 :     values = (Datum *) palloc(ncolumns * sizeof(Datum));
     562          84 :     nulls = (bool *) palloc(ncolumns * sizeof(bool));
     563             : 
     564          84 :     heap_deform_tuple(&tuple, tupdesc, values, nulls);
     565             : 
     566          84 :     old_context = MemoryContextSwitchTo(TopTransactionContext);
     567             : 
     568         420 :     for (i = 0; i < ncolumns; i++)
     569             :     {
     570             :         struct varlena *attr;
     571             :         struct varlena *new_attr;
     572             :         struct varatt_indirect redirect_pointer;
     573             : 
     574             :         /* only work on existing, not-null varlenas */
     575         336 :         if (TupleDescAttr(tupdesc, i)->attisdropped ||
     576         336 :             nulls[i] ||
     577         292 :             TupleDescAttr(tupdesc, i)->attlen != -1)
     578         128 :             continue;
     579             : 
     580         208 :         attr = (struct varlena *) DatumGetPointer(values[i]);
     581             : 
     582             :         /* don't recursively indirect */
     583         208 :         if (VARATT_IS_EXTERNAL_INDIRECT(attr))
     584           0 :             continue;
     585             : 
     586             :         /* copy datum, so it still lives later */
     587         208 :         if (VARATT_IS_EXTERNAL_ONDISK(attr))
     588           0 :             attr = detoast_external_attr(attr);
     589             :         else
     590             :         {
     591         208 :             struct varlena *oldattr = attr;
     592             : 
     593         208 :             attr = palloc0(VARSIZE_ANY(oldattr));
     594         208 :             memcpy(attr, oldattr, VARSIZE_ANY(oldattr));
     595             :         }
     596             : 
     597             :         /* build indirection Datum */
     598         208 :         new_attr = (struct varlena *) palloc0(INDIRECT_POINTER_SIZE);
     599         208 :         redirect_pointer.pointer = attr;
     600         208 :         SET_VARTAG_EXTERNAL(new_attr, VARTAG_INDIRECT);
     601         208 :         memcpy(VARDATA_EXTERNAL(new_attr), &redirect_pointer,
     602             :                sizeof(redirect_pointer));
     603             : 
     604         208 :         values[i] = PointerGetDatum(new_attr);
     605             :     }
     606             : 
     607          84 :     newtup = heap_form_tuple(tupdesc, values, nulls);
     608          84 :     pfree(values);
     609          84 :     pfree(nulls);
     610          84 :     ReleaseTupleDesc(tupdesc);
     611             : 
     612          84 :     MemoryContextSwitchTo(old_context);
     613             : 
     614             :     /*
     615             :      * We intentionally don't use PG_RETURN_HEAPTUPLEHEADER here, because that
     616             :      * would cause the indirect toast pointers to be flattened out of the
     617             :      * tuple immediately, rendering subsequent testing irrelevant.  So just
     618             :      * return the HeapTupleHeader pointer as-is.  This violates the general
     619             :      * rule that composite Datums shouldn't contain toast pointers, but so
     620             :      * long as the regression test scripts don't insert the result of this
     621             :      * function into a container type (record, array, etc) it should be OK.
     622             :      */
     623          84 :     PG_RETURN_POINTER(newtup->t_data);
     624             : }
     625             : 
     626           4 : PG_FUNCTION_INFO_V1(regress_putenv);
     627             : 
     628             : Datum
     629           2 : regress_putenv(PG_FUNCTION_ARGS)
     630             : {
     631             :     MemoryContext oldcontext;
     632             :     char       *envbuf;
     633             : 
     634           2 :     if (!superuser())
     635           0 :         elog(ERROR, "must be superuser to change environment variables");
     636             : 
     637           2 :     oldcontext = MemoryContextSwitchTo(TopMemoryContext);
     638           2 :     envbuf = text_to_cstring((text *) PG_GETARG_POINTER(0));
     639           2 :     MemoryContextSwitchTo(oldcontext);
     640             : 
     641           2 :     if (putenv(envbuf) != 0)
     642           0 :         elog(ERROR, "could not set environment variable: %m");
     643             : 
     644           2 :     PG_RETURN_VOID();
     645             : }
     646             : 
     647             : /* Sleep until no process has a given PID. */
     648           6 : PG_FUNCTION_INFO_V1(wait_pid);
     649             : 
     650             : Datum
     651           2 : wait_pid(PG_FUNCTION_ARGS)
     652             : {
     653           2 :     int         pid = PG_GETARG_INT32(0);
     654             : 
     655           2 :     if (!superuser())
     656           0 :         elog(ERROR, "must be superuser to check PID liveness");
     657             : 
     658           6 :     while (kill(pid, 0) == 0)
     659             :     {
     660           4 :         CHECK_FOR_INTERRUPTS();
     661           4 :         pg_usleep(50000);
     662             :     }
     663             : 
     664           2 :     if (errno != ESRCH)
     665           0 :         elog(ERROR, "could not check PID %d liveness: %m", pid);
     666             : 
     667           2 :     PG_RETURN_VOID();
     668             : }
     669             : 
     670             : static void
     671           4 : test_atomic_flag(void)
     672             : {
     673             :     pg_atomic_flag flag;
     674             : 
     675           4 :     pg_atomic_init_flag(&flag);
     676           4 :     EXPECT_TRUE(pg_atomic_unlocked_test_flag(&flag));
     677           4 :     EXPECT_TRUE(pg_atomic_test_set_flag(&flag));
     678           4 :     EXPECT_TRUE(!pg_atomic_unlocked_test_flag(&flag));
     679           4 :     EXPECT_TRUE(!pg_atomic_test_set_flag(&flag));
     680           4 :     pg_atomic_clear_flag(&flag);
     681           4 :     EXPECT_TRUE(pg_atomic_unlocked_test_flag(&flag));
     682           4 :     EXPECT_TRUE(pg_atomic_test_set_flag(&flag));
     683           4 :     pg_atomic_clear_flag(&flag);
     684           4 : }
     685             : 
     686             : static void
     687           4 : test_atomic_uint32(void)
     688             : {
     689             :     pg_atomic_uint32 var;
     690             :     uint32      expected;
     691             :     int         i;
     692             : 
     693           4 :     pg_atomic_init_u32(&var, 0);
     694           4 :     EXPECT_EQ_U32(pg_atomic_read_u32(&var), 0);
     695           4 :     pg_atomic_write_u32(&var, 3);
     696           4 :     EXPECT_EQ_U32(pg_atomic_read_u32(&var), 3);
     697           4 :     EXPECT_EQ_U32(pg_atomic_fetch_add_u32(&var, pg_atomic_read_u32(&var) - 2),
     698             :                   3);
     699           4 :     EXPECT_EQ_U32(pg_atomic_fetch_sub_u32(&var, 1), 4);
     700           4 :     EXPECT_EQ_U32(pg_atomic_sub_fetch_u32(&var, 3), 0);
     701           4 :     EXPECT_EQ_U32(pg_atomic_add_fetch_u32(&var, 10), 10);
     702           4 :     EXPECT_EQ_U32(pg_atomic_exchange_u32(&var, 5), 10);
     703           4 :     EXPECT_EQ_U32(pg_atomic_exchange_u32(&var, 0), 5);
     704             : 
     705             :     /* test around numerical limits */
     706           4 :     EXPECT_EQ_U32(pg_atomic_fetch_add_u32(&var, INT_MAX), 0);
     707           4 :     EXPECT_EQ_U32(pg_atomic_fetch_add_u32(&var, INT_MAX), INT_MAX);
     708           4 :     pg_atomic_fetch_add_u32(&var, 2);   /* wrap to 0 */
     709           4 :     EXPECT_EQ_U32(pg_atomic_fetch_add_u32(&var, PG_INT16_MAX), 0);
     710           4 :     EXPECT_EQ_U32(pg_atomic_fetch_add_u32(&var, PG_INT16_MAX + 1),
     711             :                   PG_INT16_MAX);
     712           4 :     EXPECT_EQ_U32(pg_atomic_fetch_add_u32(&var, PG_INT16_MIN),
     713             :                   2 * PG_INT16_MAX + 1);
     714           4 :     EXPECT_EQ_U32(pg_atomic_fetch_add_u32(&var, PG_INT16_MIN - 1),
     715             :                   PG_INT16_MAX);
     716           4 :     pg_atomic_fetch_add_u32(&var, 1);   /* top up to UINT_MAX */
     717           4 :     EXPECT_EQ_U32(pg_atomic_read_u32(&var), UINT_MAX);
     718           4 :     EXPECT_EQ_U32(pg_atomic_fetch_sub_u32(&var, INT_MAX), UINT_MAX);
     719           4 :     EXPECT_EQ_U32(pg_atomic_read_u32(&var), (uint32) INT_MAX + 1);
     720           4 :     EXPECT_EQ_U32(pg_atomic_sub_fetch_u32(&var, INT_MAX), 1);
     721           4 :     pg_atomic_sub_fetch_u32(&var, 1);
     722             : 
     723             :     /* fail exchange because of old expected */
     724           4 :     expected = 10;
     725           4 :     EXPECT_TRUE(!pg_atomic_compare_exchange_u32(&var, &expected, 1));
     726             : 
     727             :     /* CAS is allowed to fail due to interrupts, try a couple of times */
     728           8 :     for (i = 0; i < 1000; i++)
     729             :     {
     730           8 :         expected = 0;
     731           8 :         if (!pg_atomic_compare_exchange_u32(&var, &expected, 1))
     732           4 :             break;
     733             :     }
     734           4 :     if (i == 1000)
     735           0 :         elog(ERROR, "atomic_compare_exchange_u32() never succeeded");
     736           4 :     EXPECT_EQ_U32(pg_atomic_read_u32(&var), 1);
     737           4 :     pg_atomic_write_u32(&var, 0);
     738             : 
     739             :     /* try setting flagbits */
     740           4 :     EXPECT_TRUE(!(pg_atomic_fetch_or_u32(&var, 1) & 1));
     741           4 :     EXPECT_TRUE(pg_atomic_fetch_or_u32(&var, 2) & 1);
     742           4 :     EXPECT_EQ_U32(pg_atomic_read_u32(&var), 3);
     743             :     /* try clearing flagbits */
     744           4 :     EXPECT_EQ_U32(pg_atomic_fetch_and_u32(&var, ~2) & 3, 3);
     745           4 :     EXPECT_EQ_U32(pg_atomic_fetch_and_u32(&var, ~1), 1);
     746             :     /* no bits set anymore */
     747           4 :     EXPECT_EQ_U32(pg_atomic_fetch_and_u32(&var, ~0), 0);
     748           4 : }
     749             : 
     750             : static void
     751           4 : test_atomic_uint64(void)
     752             : {
     753             :     pg_atomic_uint64 var;
     754             :     uint64      expected;
     755             :     int         i;
     756             : 
     757           4 :     pg_atomic_init_u64(&var, 0);
     758           4 :     EXPECT_EQ_U64(pg_atomic_read_u64(&var), 0);
     759           4 :     pg_atomic_write_u64(&var, 3);
     760           4 :     EXPECT_EQ_U64(pg_atomic_read_u64(&var), 3);
     761           4 :     EXPECT_EQ_U64(pg_atomic_fetch_add_u64(&var, pg_atomic_read_u64(&var) - 2),
     762             :                   3);
     763           4 :     EXPECT_EQ_U64(pg_atomic_fetch_sub_u64(&var, 1), 4);
     764           4 :     EXPECT_EQ_U64(pg_atomic_sub_fetch_u64(&var, 3), 0);
     765           4 :     EXPECT_EQ_U64(pg_atomic_add_fetch_u64(&var, 10), 10);
     766           4 :     EXPECT_EQ_U64(pg_atomic_exchange_u64(&var, 5), 10);
     767           4 :     EXPECT_EQ_U64(pg_atomic_exchange_u64(&var, 0), 5);
     768             : 
     769             :     /* fail exchange because of old expected */
     770           4 :     expected = 10;
     771           4 :     EXPECT_TRUE(!pg_atomic_compare_exchange_u64(&var, &expected, 1));
     772             : 
     773             :     /* CAS is allowed to fail due to interrupts, try a couple of times */
     774           8 :     for (i = 0; i < 100; i++)
     775             :     {
     776           8 :         expected = 0;
     777           8 :         if (!pg_atomic_compare_exchange_u64(&var, &expected, 1))
     778           4 :             break;
     779             :     }
     780           4 :     if (i == 100)
     781           0 :         elog(ERROR, "atomic_compare_exchange_u64() never succeeded");
     782           4 :     EXPECT_EQ_U64(pg_atomic_read_u64(&var), 1);
     783             : 
     784           4 :     pg_atomic_write_u64(&var, 0);
     785             : 
     786             :     /* try setting flagbits */
     787           4 :     EXPECT_TRUE(!(pg_atomic_fetch_or_u64(&var, 1) & 1));
     788           4 :     EXPECT_TRUE(pg_atomic_fetch_or_u64(&var, 2) & 1);
     789           4 :     EXPECT_EQ_U64(pg_atomic_read_u64(&var), 3);
     790             :     /* try clearing flagbits */
     791           4 :     EXPECT_EQ_U64((pg_atomic_fetch_and_u64(&var, ~2) & 3), 3);
     792           4 :     EXPECT_EQ_U64(pg_atomic_fetch_and_u64(&var, ~1), 1);
     793             :     /* no bits set anymore */
     794           4 :     EXPECT_EQ_U64(pg_atomic_fetch_and_u64(&var, ~0), 0);
     795           4 : }
     796             : 
     797             : 
     798          10 : PG_FUNCTION_INFO_V1(test_atomic_ops);
     799             : Datum
     800           4 : test_atomic_ops(PG_FUNCTION_ARGS)
     801             : {
     802           4 :     test_atomic_flag();
     803             : 
     804           4 :     test_atomic_uint32();
     805             : 
     806           4 :     test_atomic_uint64();
     807             : 
     808           4 :     PG_RETURN_BOOL(true);
     809             : }
     810             : 
     811           6 : PG_FUNCTION_INFO_V1(test_fdw_handler);
     812             : Datum
     813           0 : test_fdw_handler(PG_FUNCTION_ARGS)
     814             : {
     815           0 :     elog(ERROR, "test_fdw_handler is not implemented");
     816             :     PG_RETURN_NULL();
     817             : }
     818             : 
     819          10 : PG_FUNCTION_INFO_V1(test_support_func);
     820             : Datum
     821          40 : test_support_func(PG_FUNCTION_ARGS)
     822             : {
     823          40 :     Node       *rawreq = (Node *) PG_GETARG_POINTER(0);
     824          40 :     Node       *ret = NULL;
     825             : 
     826          40 :     if (IsA(rawreq, SupportRequestSelectivity))
     827             :     {
     828             :         /*
     829             :          * Assume that the target is int4eq; that's safe as long as we don't
     830             :          * attach this to any other boolean-returning function.
     831             :          */
     832           4 :         SupportRequestSelectivity *req = (SupportRequestSelectivity *) rawreq;
     833             :         Selectivity s1;
     834             : 
     835           4 :         if (req->is_join)
     836           0 :             s1 = join_selectivity(req->root, Int4EqualOperator,
     837             :                                   req->args,
     838             :                                   req->inputcollid,
     839             :                                   req->jointype,
     840           0 :                                   req->sjinfo);
     841             :         else
     842           4 :             s1 = restriction_selectivity(req->root, Int4EqualOperator,
     843             :                                          req->args,
     844             :                                          req->inputcollid,
     845             :                                          req->varRelid);
     846             : 
     847           4 :         req->selectivity = s1;
     848           4 :         ret = (Node *) req;
     849             :     }
     850             : 
     851          40 :     if (IsA(rawreq, SupportRequestCost))
     852             :     {
     853             :         /* Provide some generic estimate */
     854          12 :         SupportRequestCost *req = (SupportRequestCost *) rawreq;
     855             : 
     856          12 :         req->startup = 0;
     857          12 :         req->per_tuple = 2 * cpu_operator_cost;
     858          12 :         ret = (Node *) req;
     859             :     }
     860             : 
     861          40 :     if (IsA(rawreq, SupportRequestRows))
     862             :     {
     863             :         /*
     864             :          * Assume that the target is generate_series_int4; that's safe as long
     865             :          * as we don't attach this to any other set-returning function.
     866             :          */
     867           8 :         SupportRequestRows *req = (SupportRequestRows *) rawreq;
     868             : 
     869           8 :         if (req->node && IsA(req->node, FuncExpr))    /* be paranoid */
     870             :         {
     871           8 :             List       *args = ((FuncExpr *) req->node)->args;
     872           8 :             Node       *arg1 = linitial(args);
     873           8 :             Node       *arg2 = lsecond(args);
     874             : 
     875           8 :             if (IsA(arg1, Const) &&
     876           8 :                 !((Const *) arg1)->constisnull &&
     877           8 :                 IsA(arg2, Const) &&
     878           8 :                 !((Const *) arg2)->constisnull)
     879             :             {
     880           8 :                 int32       val1 = DatumGetInt32(((Const *) arg1)->constvalue);
     881           8 :                 int32       val2 = DatumGetInt32(((Const *) arg2)->constvalue);
     882             : 
     883           8 :                 req->rows = val2 - val1 + 1;
     884           8 :                 ret = (Node *) req;
     885             :             }
     886             :         }
     887             :     }
     888             : 
     889          40 :     PG_RETURN_POINTER(ret);
     890             : }
     891             : 
     892           6 : PG_FUNCTION_INFO_V1(test_opclass_options_func);
     893             : Datum
     894           0 : test_opclass_options_func(PG_FUNCTION_ARGS)
     895             : {
     896           0 :     PG_RETURN_NULL();
     897             : }

Generated by: LCOV version 1.13