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

Generated by: LCOV version 1.13