PostgreSQL Source Code  git master
regress.c File Reference
#include "postgres.h"
#include <math.h>
#include <signal.h>
#include "access/detoast.h"
#include "access/htup_details.h"
#include "access/transam.h"
#include "access/xact.h"
#include "catalog/pg_operator.h"
#include "catalog/pg_type.h"
#include "commands/sequence.h"
#include "commands/trigger.h"
#include "executor/executor.h"
#include "executor/spi.h"
#include "miscadmin.h"
#include "nodes/supportnodes.h"
#include "optimizer/optimizer.h"
#include "optimizer/plancat.h"
#include "port/atomics.h"
#include "utils/builtins.h"
#include "utils/geo_decls.h"
#include "utils/memutils.h"
#include "utils/rel.h"
#include "utils/typcache.h"
Include dependency graph for regress.c:

Go to the source code of this file.

Data Structures

struct  WIDGET
 

Macros

#define EXPECT_TRUE(expr)
 
#define EXPECT_EQ_U32(result_expr, expected_expr)
 
#define EXPECT_EQ_U64(result_expr, expected_expr)
 
#define LDELIM   '('
 
#define RDELIM   ')'
 
#define DELIM   ','
 
#define NARGS   3
 
#define TTDUMMY_INFINITY   999999
 

Functions

static void regress_lseg_construct (LSEG *lseg, Point *pt1, Point *pt2)
 
 PG_FUNCTION_INFO_V1 (interpt_pp)
 
Datum interpt_pp (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (overpaid)
 
Datum overpaid (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (widget_in)
 
 PG_FUNCTION_INFO_V1 (widget_out)
 
Datum widget_in (PG_FUNCTION_ARGS)
 
Datum widget_out (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (pt_in_widget)
 
Datum pt_in_widget (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (reverse_name)
 
Datum reverse_name (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (trigger_return_old)
 
Datum trigger_return_old (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (ttdummy)
 
Datum ttdummy (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (set_ttdummy)
 
Datum set_ttdummy (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (int44in)
 
Datum int44in (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (int44out)
 
Datum int44out (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (make_tuple_indirect)
 
Datum make_tuple_indirect (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (regress_putenv)
 
Datum regress_putenv (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (wait_pid)
 
Datum wait_pid (PG_FUNCTION_ARGS)
 
static void test_atomic_flag (void)
 
static void test_atomic_uint32 (void)
 
static void test_atomic_uint64 (void)
 
 PG_FUNCTION_INFO_V1 (test_atomic_ops)
 
Datum test_atomic_ops (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (test_fdw_handler)
 
Datum test_fdw_handler (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (test_support_func)
 
Datum test_support_func (PG_FUNCTION_ARGS)
 

Variables

 PG_MODULE_MAGIC
 
static SPIPlanPtr splan = NULL
 
static bool ttoff = false
 

Macro Definition Documentation

◆ DELIM

#define DELIM   ','

Definition at line 73 of file regress.c.

Referenced by widget_in().

◆ EXPECT_EQ_U32

#define EXPECT_EQ_U32 (   result_expr,
  expected_expr 
)
Value:
do { \
uint32 result = (result_expr); \
uint32 expected = (expected_expr); \
if (result != expected) \
elog(ERROR, \
"%s yielded %u, expected %s in file \"%s\" line %u", \
#result_expr, result, #expected_expr, __FILE__, __LINE__); \
} while (0)
#define ERROR
Definition: elog.h:43

Definition at line 51 of file regress.c.

Referenced by test_atomic_uint32().

◆ EXPECT_EQ_U64

#define EXPECT_EQ_U64 (   result_expr,
  expected_expr 
)
Value:
do { \
uint64 result = (result_expr); \
uint64 expected = (expected_expr); \
if (result != expected) \
elog(ERROR, \
"%s yielded " UINT64_FORMAT ", expected %s in file \"%s\" line %u", \
#result_expr, result, #expected_expr, __FILE__, __LINE__); \
} while (0)
#define ERROR
Definition: elog.h:43
#define UINT64_FORMAT
Definition: c.h:402

Definition at line 61 of file regress.c.

Referenced by test_atomic_uint64().

◆ EXPECT_TRUE

#define EXPECT_TRUE (   expr)
Value:
do { \
if (!(expr)) \
elog(ERROR, \
"%s was unexpectedly false in file \"%s\" line %u", \
#expr, __FILE__, __LINE__); \
} while (0)
#define ERROR
Definition: elog.h:43

Definition at line 43 of file regress.c.

Referenced by test_atomic_flag(), test_atomic_uint32(), and test_atomic_uint64().

◆ LDELIM

#define LDELIM   '('

Definition at line 71 of file regress.c.

Referenced by widget_in().

◆ NARGS

#define NARGS   3

Definition at line 162 of file regress.c.

Referenced by widget_in().

◆ RDELIM

#define RDELIM   ')'

Definition at line 72 of file regress.c.

Referenced by widget_in().

◆ TTDUMMY_INFINITY

#define TTDUMMY_INFINITY   999999

Definition at line 256 of file regress.c.

Referenced by ttdummy().

Function Documentation

◆ int44in()

Datum int44in ( PG_FUNCTION_ARGS  )

Definition at line 491 of file regress.c.

References i, int44out(), palloc(), PG_FUNCTION_INFO_V1(), PG_GETARG_CSTRING, and PG_RETURN_POINTER.

Referenced by set_ttdummy().

492 {
493  char *input_string = PG_GETARG_CSTRING(0);
494  int32 *result = (int32 *) palloc(4 * sizeof(int32));
495  int i;
496 
497  i = sscanf(input_string,
498  "%d, %d, %d, %d",
499  &result[0],
500  &result[1],
501  &result[2],
502  &result[3]);
503  while (i < 4)
504  result[i++] = 0;
505 
506  PG_RETURN_POINTER(result);
507 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:351
signed int int32
Definition: c.h:347
void * palloc(Size size)
Definition: mcxt.c:949
int i
#define PG_GETARG_CSTRING(n)
Definition: fmgr.h:272

◆ int44out()

Datum int44out ( PG_FUNCTION_ARGS  )

Definition at line 515 of file regress.c.

References make_tuple_indirect(), palloc(), PG_FUNCTION_INFO_V1(), PG_GETARG_POINTER, PG_RETURN_CSTRING, and snprintf.

Referenced by int44in().

516 {
517  int32 *an_array = (int32 *) PG_GETARG_POINTER(0);
518  char *result = (char *) palloc(16 * 4);
519 
520  snprintf(result, 16 * 4, "%d,%d,%d,%d",
521  an_array[0],
522  an_array[1],
523  an_array[2],
524  an_array[3]);
525 
526  PG_RETURN_CSTRING(result);
527 }
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:271
signed int int32
Definition: c.h:347
#define PG_RETURN_CSTRING(x)
Definition: fmgr.h:352
void * palloc(Size size)
Definition: mcxt.c:949
#define snprintf
Definition: port.h:192

◆ interpt_pp()

Datum interpt_pp ( PG_FUNCTION_ARGS  )

Definition at line 84 of file regress.c.

References DatumGetBool, DirectFunctionCall2, i, lseg_interpt(), lseg_intersect(), LsegPGetDatum, PATH::npts, PATH::p, PG_GETARG_PATH_P, PG_RETURN_DATUM, PG_RETURN_NULL, and regress_lseg_construct().

85 {
86  PATH *p1 = PG_GETARG_PATH_P(0);
87  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  found = false; /* Haven't found it yet */
95 
96  for (i = 0; i < p1->npts - 1 && !found; i++)
97  {
98  regress_lseg_construct(&seg1, &p1->p[i], &p1->p[i + 1]);
99  for (j = 0; j < p2->npts - 1 && !found; j++)
100  {
101  regress_lseg_construct(&seg2, &p2->p[j], &p2->p[j + 1]);
103  LsegPGetDatum(&seg1),
104  LsegPGetDatum(&seg2))))
105  found = true;
106  }
107  }
108 
109  if (!found)
110  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  */
118  LsegPGetDatum(&seg1),
119  LsegPGetDatum(&seg2)));
120 }
Definition: geo_decls.h:65
Datum lseg_interpt(PG_FUNCTION_ARGS)
Definition: geo_ops.c:2314
#define LsegPGetDatum(X)
Definition: geo_decls.h:140
Point p[FLEXIBLE_ARRAY_MEMBER]
Definition: geo_decls.h:80
int32 npts
Definition: geo_decls.h:77
#define DatumGetBool(X)
Definition: postgres.h:393
#define PG_GETARG_PATH_P(n)
Definition: geo_decls.h:147
static void regress_lseg_construct(LSEG *lseg, Point *pt1, Point *pt2)
Definition: regress.c:125
Datum lseg_intersect(PG_FUNCTION_ARGS)
Definition: geo_ops.c:2141
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:343
Definition: geo_decls.h:74
int i
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:617
#define PG_RETURN_NULL()
Definition: fmgr.h:335

◆ make_tuple_indirect()

Datum make_tuple_indirect ( PG_FUNCTION_ARGS  )

Definition at line 531 of file regress.c.

References attlen, DatumGetPointer, detoast_external_attr(), heap_deform_tuple(), heap_form_tuple(), HeapTupleHeaderGetDatumLength, HeapTupleHeaderGetTypeId, HeapTupleHeaderGetTypMod, i, INDIRECT_POINTER_SIZE, InvalidOid, ItemPointerSetInvalid, lookup_rowtype_tupdesc(), MemoryContextSwitchTo(), TupleDescData::natts, palloc(), palloc0(), pfree(), PG_FUNCTION_INFO_V1(), PG_GETARG_HEAPTUPLEHEADER, PG_RETURN_POINTER, varatt_indirect::pointer, PointerGetDatum, regress_putenv(), ReleaseTupleDesc, SET_VARTAG_EXTERNAL, HeapTupleData::t_data, HeapTupleData::t_len, HeapTupleData::t_self, HeapTupleData::t_tableOid, TopTransactionContext, TupleDescAttr, values, VARATT_IS_EXTERNAL_INDIRECT, VARATT_IS_EXTERNAL_ONDISK, VARDATA_EXTERNAL, VARSIZE_ANY, and VARTAG_INDIRECT.

Referenced by int44out().

532 {
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  tupType = HeapTupleHeaderGetTypeId(rec);
551  tupTypmod = HeapTupleHeaderGetTypMod(rec);
552  tupdesc = lookup_rowtype_tupdesc(tupType, tupTypmod);
553  ncolumns = tupdesc->natts;
554 
555  /* Build a temporary HeapTuple control structure */
557  ItemPointerSetInvalid(&(tuple.t_self));
558  tuple.t_tableOid = InvalidOid;
559  tuple.t_data = rec;
560 
561  values = (Datum *) palloc(ncolumns * sizeof(Datum));
562  nulls = (bool *) palloc(ncolumns * sizeof(bool));
563 
564  heap_deform_tuple(&tuple, tupdesc, values, nulls);
565 
567 
568  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  if (TupleDescAttr(tupdesc, i)->attisdropped ||
576  nulls[i] ||
577  TupleDescAttr(tupdesc, i)->attlen != -1)
578  continue;
579 
580  attr = (struct varlena *) DatumGetPointer(values[i]);
581 
582  /* don't recursively indirect */
583  if (VARATT_IS_EXTERNAL_INDIRECT(attr))
584  continue;
585 
586  /* copy datum, so it still lives later */
587  if (VARATT_IS_EXTERNAL_ONDISK(attr))
588  attr = detoast_external_attr(attr);
589  else
590  {
591  struct varlena *oldattr = attr;
592 
593  attr = palloc0(VARSIZE_ANY(oldattr));
594  memcpy(attr, oldattr, VARSIZE_ANY(oldattr));
595  }
596 
597  /* build indirection Datum */
598  new_attr = (struct varlena *) palloc0(INDIRECT_POINTER_SIZE);
599  redirect_pointer.pointer = attr;
601  memcpy(VARDATA_EXTERNAL(new_attr), &redirect_pointer,
602  sizeof(redirect_pointer));
603 
604  values[i] = PointerGetDatum(new_attr);
605  }
606 
607  newtup = heap_form_tuple(tupdesc, values, nulls);
608  pfree(values);
609  pfree(nulls);
610  ReleaseTupleDesc(tupdesc);
611 
612  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  PG_RETURN_POINTER(newtup->t_data);
624 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:351
#define VARATT_IS_EXTERNAL_ONDISK(PTR)
Definition: postgres.h:314
MemoryContext TopTransactionContext
Definition: mcxt.c:49
TupleDesc lookup_rowtype_tupdesc(Oid type_id, int32 typmod)
Definition: typcache.c:1652
#define PointerGetDatum(X)
Definition: postgres.h:556
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
struct varlena * detoast_external_attr(struct varlena *attr)
Definition: detoast.c:45
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: heaptuple.c:1020
#define PG_GETARG_HEAPTUPLEHEADER(n)
Definition: fmgr.h:306
unsigned int Oid
Definition: postgres_ext.h:31
#define VARDATA_EXTERNAL(PTR)
Definition: postgres.h:310
signed int int32
Definition: c.h:347
HeapTupleHeader t_data
Definition: htup.h:68
#define HeapTupleHeaderGetTypMod(tup)
Definition: htup_details.h:468
void pfree(void *pointer)
Definition: mcxt.c:1056
#define VARATT_IS_EXTERNAL_INDIRECT(PTR)
Definition: postgres.h:316
ItemPointerData t_self
Definition: htup.h:65
uint32 t_len
Definition: htup.h:64
#define SET_VARTAG_EXTERNAL(PTR, tag)
Definition: postgres.h:333
Oid t_tableOid
Definition: htup.h:66
#define INDIRECT_POINTER_SIZE
Definition: detoast.h:44
int16 attlen
Definition: pg_attribute.h:64
void * palloc0(Size size)
Definition: mcxt.c:980
uintptr_t Datum
Definition: postgres.h:367
#define HeapTupleHeaderGetTypeId(tup)
Definition: htup_details.h:458
#define VARSIZE_ANY(PTR)
Definition: postgres.h:335
#define InvalidOid
Definition: postgres_ext.h:36
#define DatumGetPointer(X)
Definition: postgres.h:549
void heap_deform_tuple(HeapTuple tuple, TupleDesc tupleDesc, Datum *values, bool *isnull)
Definition: heaptuple.c:1249
static Datum values[MAXATTR]
Definition: bootstrap.c:167
#define ItemPointerSetInvalid(pointer)
Definition: itemptr.h:172
void * palloc(Size size)
Definition: mcxt.c:949
int i
Definition: c.h:556
#define ReleaseTupleDesc(tupdesc)
Definition: tupdesc.h:122
#define HeapTupleHeaderGetDatumLength(tup)
Definition: htup_details.h:452

◆ overpaid()

Datum overpaid ( PG_FUNCTION_ARGS  )

Definition at line 136 of file regress.c.

References DatumGetInt32, GetAttributeByName(), PG_GETARG_HEAPTUPLEHEADER, PG_RETURN_BOOL, and PG_RETURN_NULL.

Referenced by regress_lseg_construct().

137 {
139  bool isnull;
140  int32 salary;
141 
142  salary = DatumGetInt32(GetAttributeByName(tuple, "salary", &isnull));
143  if (isnull)
144  PG_RETURN_NULL();
145  PG_RETURN_BOOL(salary > 699);
146 }
#define DatumGetInt32(X)
Definition: postgres.h:472
Datum GetAttributeByName(HeapTupleHeader tuple, const char *attname, bool *isNull)
Definition: execUtils.c:961
#define PG_GETARG_HEAPTUPLEHEADER(n)
Definition: fmgr.h:306
signed int int32
Definition: c.h:347
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349
#define PG_RETURN_NULL()
Definition: fmgr.h:335

◆ PG_FUNCTION_INFO_V1() [1/17]

◆ PG_FUNCTION_INFO_V1() [2/17]

PG_FUNCTION_INFO_V1 ( overpaid  )

◆ PG_FUNCTION_INFO_V1() [3/17]

PG_FUNCTION_INFO_V1 ( widget_in  )

◆ PG_FUNCTION_INFO_V1() [4/17]

PG_FUNCTION_INFO_V1 ( widget_out  )

◆ PG_FUNCTION_INFO_V1() [5/17]

PG_FUNCTION_INFO_V1 ( pt_in_widget  )

◆ PG_FUNCTION_INFO_V1() [6/17]

PG_FUNCTION_INFO_V1 ( reverse_name  )

◆ PG_FUNCTION_INFO_V1() [7/17]

PG_FUNCTION_INFO_V1 ( trigger_return_old  )

◆ PG_FUNCTION_INFO_V1() [8/17]

PG_FUNCTION_INFO_V1 ( ttdummy  )

◆ PG_FUNCTION_INFO_V1() [9/17]

PG_FUNCTION_INFO_V1 ( set_ttdummy  )

◆ PG_FUNCTION_INFO_V1() [10/17]

PG_FUNCTION_INFO_V1 ( int44in  )

◆ PG_FUNCTION_INFO_V1() [11/17]

PG_FUNCTION_INFO_V1 ( int44out  )

◆ PG_FUNCTION_INFO_V1() [12/17]

PG_FUNCTION_INFO_V1 ( make_tuple_indirect  )

◆ PG_FUNCTION_INFO_V1() [13/17]

PG_FUNCTION_INFO_V1 ( regress_putenv  )

◆ PG_FUNCTION_INFO_V1() [14/17]

PG_FUNCTION_INFO_V1 ( wait_pid  )

◆ PG_FUNCTION_INFO_V1() [15/17]

PG_FUNCTION_INFO_V1 ( test_atomic_ops  )

◆ PG_FUNCTION_INFO_V1() [16/17]

PG_FUNCTION_INFO_V1 ( test_fdw_handler  )

◆ PG_FUNCTION_INFO_V1() [17/17]

PG_FUNCTION_INFO_V1 ( test_support_func  )

◆ pt_in_widget()

Datum pt_in_widget ( PG_FUNCTION_ARGS  )

Definition at line 206 of file regress.c.

References WIDGET::center, DatumGetFloat8, DirectFunctionCall2, PG_FUNCTION_INFO_V1(), PG_GETARG_POINT_P, PG_GETARG_POINTER, PG_RETURN_BOOL, point_distance(), PointPGetDatum, and reverse_name().

Referenced by widget_out().

207 {
208  Point *point = PG_GETARG_POINT_P(0);
209  WIDGET *widget = (WIDGET *) PG_GETARG_POINTER(1);
210  float8 distance;
211 
213  PointPGetDatum(point),
214  PointPGetDatum(&widget->center)));
215 
216  PG_RETURN_BOOL(distance < widget->radius);
217 }
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:271
Datum point_distance(PG_FUNCTION_ARGS)
Definition: geo_ops.c:1950
double float8
Definition: c.h:492
#define PG_GETARG_POINT_P(n)
Definition: geo_decls.h:136
#define DatumGetFloat8(X)
Definition: postgres.h:714
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349
Point center
Definition: regress.c:155
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:617
#define PointPGetDatum(X)
Definition: geo_decls.h:135

◆ regress_lseg_construct()

static void regress_lseg_construct ( LSEG lseg,
Point pt1,
Point pt2 
)
static

Definition at line 125 of file regress.c.

References overpaid(), LSEG::p, PG_FUNCTION_INFO_V1(), Point::x, and Point::y.

Referenced by interpt_pp().

126 {
127  lseg->p[0].x = pt1->x;
128  lseg->p[0].y = pt1->y;
129  lseg->p[1].x = pt2->x;
130  lseg->p[1].y = pt2->y;
131 }
float8 x
Definition: geo_decls.h:57
float8 y
Definition: geo_decls.h:57
Point p[2]
Definition: geo_decls.h:67

◆ regress_putenv()

Datum regress_putenv ( PG_FUNCTION_ARGS  )

Definition at line 629 of file regress.c.

References elog, ERROR, MemoryContextSwitchTo(), PG_FUNCTION_INFO_V1(), PG_GETARG_POINTER, PG_RETURN_VOID, putenv, superuser(), text_to_cstring(), TopMemoryContext, and wait_pid().

Referenced by make_tuple_indirect().

630 {
631  MemoryContext oldcontext;
632  char *envbuf;
633 
634  if (!superuser())
635  elog(ERROR, "must be superuser to change environment variables");
636 
638  envbuf = text_to_cstring((text *) PG_GETARG_POINTER(0));
639  MemoryContextSwitchTo(oldcontext);
640 
641  if (putenv(envbuf) != 0)
642  elog(ERROR, "could not set environment variable: %m");
643 
644  PG_RETURN_VOID();
645 }
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
bool superuser(void)
Definition: superuser.c:46
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:271
#define ERROR
Definition: elog.h:43
MemoryContext TopMemoryContext
Definition: mcxt.c:44
#define PG_RETURN_VOID()
Definition: fmgr.h:339
#define putenv(x)
Definition: win32_port.h:474
char * text_to_cstring(const text *t)
Definition: varlena.c:204
#define elog(elevel,...)
Definition: elog.h:228
Definition: c.h:556

◆ reverse_name()

Datum reverse_name ( PG_FUNCTION_ARGS  )

Definition at line 222 of file regress.c.

References i, NAMEDATALEN, palloc0(), PG_FUNCTION_INFO_V1(), PG_GETARG_CSTRING, PG_RETURN_CSTRING, and trigger_return_old().

Referenced by pt_in_widget().

223 {
224  char *string = PG_GETARG_CSTRING(0);
225  int i;
226  int len;
227  char *new_string;
228 
229  new_string = palloc0(NAMEDATALEN);
230  for (i = 0; i < NAMEDATALEN && string[i]; ++i)
231  ;
232  if (i == NAMEDATALEN || !string[i])
233  --i;
234  len = i;
235  for (; i >= 0; --i)
236  new_string[len - i] = string[i];
237  PG_RETURN_CSTRING(new_string);
238 }
#define NAMEDATALEN
void * palloc0(Size size)
Definition: mcxt.c:980
#define PG_RETURN_CSTRING(x)
Definition: fmgr.h:352
int i
#define PG_GETARG_CSTRING(n)
Definition: fmgr.h:272

◆ set_ttdummy()

Datum set_ttdummy ( PG_FUNCTION_ARGS  )

Definition at line 453 of file regress.c.

References int44in(), PG_FUNCTION_INFO_V1(), PG_GETARG_INT32, PG_RETURN_INT32, and ttoff.

Referenced by ttdummy().

454 {
455  int32 on = PG_GETARG_INT32(0);
456 
457  if (ttoff) /* OFF currently */
458  {
459  if (on == 0)
460  PG_RETURN_INT32(0);
461 
462  /* turn ON */
463  ttoff = false;
464  PG_RETURN_INT32(0);
465  }
466 
467  /* ON currently */
468  if (on != 0)
469  PG_RETURN_INT32(1);
470 
471  /* turn OFF */
472  ttoff = true;
473 
474  PG_RETURN_INT32(1);
475 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:264
#define PG_RETURN_INT32(x)
Definition: fmgr.h:344
signed int int32
Definition: c.h:347
static bool ttoff
Definition: regress.c:259

◆ test_atomic_flag()

static void test_atomic_flag ( void  )
static

Definition at line 671 of file regress.c.

References EXPECT_TRUE, flag(), pg_atomic_clear_flag(), pg_atomic_init_flag(), pg_atomic_test_set_flag(), and pg_atomic_unlocked_test_flag().

Referenced by test_atomic_ops().

672 {
674 
675  pg_atomic_init_flag(&flag);
680  pg_atomic_clear_flag(&flag);
683  pg_atomic_clear_flag(&flag);
684 }
static void pg_atomic_clear_flag(volatile pg_atomic_flag *ptr)
Definition: atomics.h:209
#define EXPECT_TRUE(expr)
Definition: regress.c:43
static bool pg_atomic_test_set_flag(volatile pg_atomic_flag *ptr)
Definition: atomics.h:185
char * flag(int b)
Definition: test-ctype.c:33
static bool pg_atomic_unlocked_test_flag(volatile pg_atomic_flag *ptr)
Definition: atomics.h:198
static void pg_atomic_init_flag(volatile pg_atomic_flag *ptr)
Definition: atomics.h:172

◆ test_atomic_ops()

Datum test_atomic_ops ( PG_FUNCTION_ARGS  )

Definition at line 800 of file regress.c.

References PG_FUNCTION_INFO_V1(), PG_RETURN_BOOL, test_atomic_flag(), test_atomic_uint32(), test_atomic_uint64(), and test_fdw_handler().

Referenced by test_atomic_uint64().

801 {
803 
805 
807 
808  PG_RETURN_BOOL(true);
809 }
static void test_atomic_uint32(void)
Definition: regress.c:687
static void test_atomic_flag(void)
Definition: regress.c:671
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349
static void test_atomic_uint64(void)
Definition: regress.c:751

◆ test_atomic_uint32()

static void test_atomic_uint32 ( void  )
static

Definition at line 687 of file regress.c.

References elog, ERROR, EXPECT_EQ_U32, EXPECT_TRUE, i, pg_atomic_add_fetch_u32(), pg_atomic_compare_exchange_u32(), pg_atomic_exchange_u32(), pg_atomic_fetch_add_u32(), pg_atomic_fetch_and_u32(), pg_atomic_fetch_or_u32(), pg_atomic_fetch_sub_u32(), pg_atomic_init_u32(), pg_atomic_read_u32(), pg_atomic_sub_fetch_u32(), pg_atomic_write_u32(), PG_INT16_MAX, and PG_INT16_MIN.

Referenced by test_atomic_ops().

688 {
689  pg_atomic_uint32 var;
690  uint32 expected;
691  int i;
692 
693  pg_atomic_init_u32(&var, 0);
695  pg_atomic_write_u32(&var, 3);
698  3);
701  EXPECT_EQ_U32(pg_atomic_add_fetch_u32(&var, 10), 10);
704 
705  /* test around numerical limits */
706  EXPECT_EQ_U32(pg_atomic_fetch_add_u32(&var, INT_MAX), 0);
707  EXPECT_EQ_U32(pg_atomic_fetch_add_u32(&var, INT_MAX), INT_MAX);
708  pg_atomic_fetch_add_u32(&var, 2); /* wrap to 0 */
711  PG_INT16_MAX);
713  2 * PG_INT16_MAX + 1);
715  PG_INT16_MAX);
716  pg_atomic_fetch_add_u32(&var, 1); /* top up to UINT_MAX */
717  EXPECT_EQ_U32(pg_atomic_read_u32(&var), UINT_MAX);
718  EXPECT_EQ_U32(pg_atomic_fetch_sub_u32(&var, INT_MAX), UINT_MAX);
719  EXPECT_EQ_U32(pg_atomic_read_u32(&var), (uint32) INT_MAX + 1);
720  EXPECT_EQ_U32(pg_atomic_sub_fetch_u32(&var, INT_MAX), 1);
721  pg_atomic_sub_fetch_u32(&var, 1);
722 
723  /* fail exchange because of old expected */
724  expected = 10;
725  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  for (i = 0; i < 1000; i++)
729  {
730  expected = 0;
731  if (!pg_atomic_compare_exchange_u32(&var, &expected, 1))
732  break;
733  }
734  if (i == 1000)
735  elog(ERROR, "atomic_compare_exchange_u32() never succeeded");
737  pg_atomic_write_u32(&var, 0);
738 
739  /* try setting flagbits */
740  EXPECT_TRUE(!(pg_atomic_fetch_or_u32(&var, 1) & 1));
741  EXPECT_TRUE(pg_atomic_fetch_or_u32(&var, 2) & 1);
743  /* try clearing flagbits */
744  EXPECT_EQ_U32(pg_atomic_fetch_and_u32(&var, ~2) & 3, 3);
746  /* no bits set anymore */
748 }
static uint32 pg_atomic_fetch_sub_u32(volatile pg_atomic_uint32 *ptr, int32 sub_)
Definition: atomics.h:343
static bool pg_atomic_compare_exchange_u32(volatile pg_atomic_uint32 *ptr, uint32 *expected, uint32 newval)
Definition: atomics.h:311
static uint32 pg_atomic_sub_fetch_u32(volatile pg_atomic_uint32 *ptr, int32 sub_)
Definition: atomics.h:401
static uint32 pg_atomic_add_fetch_u32(volatile pg_atomic_uint32 *ptr, int32 add_)
Definition: atomics.h:386
#define EXPECT_TRUE(expr)
Definition: regress.c:43
static uint32 pg_atomic_exchange_u32(volatile pg_atomic_uint32 *ptr, uint32 newval)
Definition: atomics.h:292
#define ERROR
Definition: elog.h:43
static uint32 pg_atomic_fetch_and_u32(volatile pg_atomic_uint32 *ptr, uint32 and_)
Definition: atomics.h:358
unsigned int uint32
Definition: c.h:359
#define PG_INT16_MIN
Definition: c.h:438
#define EXPECT_EQ_U32(result_expr, expected_expr)
Definition: regress.c:51
static uint32 pg_atomic_fetch_add_u32(volatile pg_atomic_uint32 *ptr, int32 add_)
Definition: atomics.h:328
#define PG_INT16_MAX
Definition: c.h:439
#define elog(elevel,...)
Definition: elog.h:228
int i
static uint32 pg_atomic_fetch_or_u32(volatile pg_atomic_uint32 *ptr, uint32 or_)
Definition: atomics.h:372
static void pg_atomic_init_u32(volatile pg_atomic_uint32 *ptr, uint32 val)
Definition: atomics.h:223
static void pg_atomic_write_u32(volatile pg_atomic_uint32 *ptr, uint32 val)
Definition: atomics.h:258
static uint32 pg_atomic_read_u32(volatile pg_atomic_uint32 *ptr)
Definition: atomics.h:241

◆ test_atomic_uint64()

static void test_atomic_uint64 ( void  )
static

Definition at line 751 of file regress.c.

References elog, ERROR, EXPECT_EQ_U64, EXPECT_TRUE, i, pg_atomic_add_fetch_u64(), pg_atomic_compare_exchange_u64(), pg_atomic_exchange_u64(), pg_atomic_fetch_add_u64(), pg_atomic_fetch_and_u64(), pg_atomic_fetch_or_u64(), pg_atomic_fetch_sub_u64(), pg_atomic_init_u64(), pg_atomic_read_u64(), pg_atomic_sub_fetch_u64(), pg_atomic_write_u64(), PG_FUNCTION_INFO_V1(), and test_atomic_ops().

Referenced by test_atomic_ops().

752 {
753  pg_atomic_uint64 var;
754  uint64 expected;
755  int i;
756 
757  pg_atomic_init_u64(&var, 0);
759  pg_atomic_write_u64(&var, 3);
762  3);
765  EXPECT_EQ_U64(pg_atomic_add_fetch_u64(&var, 10), 10);
768 
769  /* fail exchange because of old expected */
770  expected = 10;
771  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  for (i = 0; i < 100; i++)
775  {
776  expected = 0;
777  if (!pg_atomic_compare_exchange_u64(&var, &expected, 1))
778  break;
779  }
780  if (i == 100)
781  elog(ERROR, "atomic_compare_exchange_u64() never succeeded");
783 
784  pg_atomic_write_u64(&var, 0);
785 
786  /* try setting flagbits */
787  EXPECT_TRUE(!(pg_atomic_fetch_or_u64(&var, 1) & 1));
788  EXPECT_TRUE(pg_atomic_fetch_or_u64(&var, 2) & 1);
790  /* try clearing flagbits */
791  EXPECT_EQ_U64((pg_atomic_fetch_and_u64(&var, ~2) & 3), 3);
793  /* no bits set anymore */
795 }
static uint64 pg_atomic_fetch_add_u64(volatile pg_atomic_uint64 *ptr, int64 add_)
Definition: atomics.h:467
static uint64 pg_atomic_sub_fetch_u64(volatile pg_atomic_uint64 *ptr, int64 sub_)
Definition: atomics.h:513
static uint64 pg_atomic_fetch_sub_u64(volatile pg_atomic_uint64 *ptr, int64 sub_)
Definition: atomics.h:476
static void pg_atomic_write_u64(volatile pg_atomic_uint64 *ptr, uint64 val)
Definition: atomics.h:438
static bool pg_atomic_compare_exchange_u64(volatile pg_atomic_uint64 *ptr, uint64 *expected, uint64 newval)
Definition: atomics.h:456
#define EXPECT_TRUE(expr)
Definition: regress.c:43
static uint64 pg_atomic_exchange_u64(volatile pg_atomic_uint64 *ptr, uint64 newval)
Definition: atomics.h:447
static void pg_atomic_init_u64(volatile pg_atomic_uint64 *ptr, uint64 val)
Definition: atomics.h:415
#define ERROR
Definition: elog.h:43
static uint64 pg_atomic_fetch_or_u64(volatile pg_atomic_uint64 *ptr, uint64 or_)
Definition: atomics.h:495
#define EXPECT_EQ_U64(result_expr, expected_expr)
Definition: regress.c:61
static uint64 pg_atomic_fetch_and_u64(volatile pg_atomic_uint64 *ptr, uint64 and_)
Definition: atomics.h:486
static uint64 pg_atomic_add_fetch_u64(volatile pg_atomic_uint64 *ptr, int64 add_)
Definition: atomics.h:504
static uint64 pg_atomic_read_u64(volatile pg_atomic_uint64 *ptr)
Definition: atomics.h:429
#define elog(elevel,...)
Definition: elog.h:228
int i

◆ test_fdw_handler()

Datum test_fdw_handler ( PG_FUNCTION_ARGS  )

Definition at line 813 of file regress.c.

References elog, ERROR, PG_FUNCTION_INFO_V1(), PG_RETURN_NULL, and test_support_func().

Referenced by test_atomic_ops().

814 {
815  elog(ERROR, "test_fdw_handler is not implemented");
816  PG_RETURN_NULL();
817 }
#define ERROR
Definition: elog.h:43
#define elog(elevel,...)
Definition: elog.h:228
#define PG_RETURN_NULL()
Definition: fmgr.h:335

◆ test_support_func()

Datum test_support_func ( PG_FUNCTION_ARGS  )

Definition at line 821 of file regress.c.

References SupportRequestSelectivity::args, generate_unaccent_rules::args, cpu_operator_cost, DatumGetInt32, SupportRequestSelectivity::inputcollid, SupportRequestSelectivity::is_join, IsA, join_selectivity(), SupportRequestSelectivity::jointype, linitial, lsecond, SupportRequestRows::node, SupportRequestCost::per_tuple, PG_GETARG_POINTER, PG_RETURN_POINTER, restriction_selectivity(), SupportRequestSelectivity::root, SupportRequestRows::rows, s1, SupportRequestSelectivity::selectivity, SupportRequestSelectivity::sjinfo, SupportRequestCost::startup, and SupportRequestSelectivity::varRelid.

Referenced by test_fdw_handler().

822 {
823  Node *rawreq = (Node *) PG_GETARG_POINTER(0);
824  Node *ret = NULL;
825 
826  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  */
833  Selectivity s1;
834 
835  if (req->is_join)
836  s1 = join_selectivity(req->root, Int4EqualOperator,
837  req->args,
838  req->inputcollid,
839  req->jointype,
840  req->sjinfo);
841  else
842  s1 = restriction_selectivity(req->root, Int4EqualOperator,
843  req->args,
844  req->inputcollid,
845  req->varRelid);
846 
847  req->selectivity = s1;
848  ret = (Node *) req;
849  }
850 
851  if (IsA(rawreq, SupportRequestCost))
852  {
853  /* Provide some generic estimate */
854  SupportRequestCost *req = (SupportRequestCost *) rawreq;
855 
856  req->startup = 0;
857  req->per_tuple = 2 * cpu_operator_cost;
858  ret = (Node *) req;
859  }
860 
861  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  SupportRequestRows *req = (SupportRequestRows *) rawreq;
868 
869  if (req->node && IsA(req->node, FuncExpr)) /* be paranoid */
870  {
871  List *args = ((FuncExpr *) req->node)->args;
872  Node *arg1 = linitial(args);
873  Node *arg2 = lsecond(args);
874 
875  if (IsA(arg1, Const) &&
876  !((Const *) arg1)->constisnull &&
877  IsA(arg2, Const) &&
878  !((Const *) arg2)->constisnull)
879  {
880  int32 val1 = DatumGetInt32(((Const *) arg1)->constvalue);
881  int32 val2 = DatumGetInt32(((Const *) arg2)->constvalue);
882 
883  req->rows = val2 - val1 + 1;
884  ret = (Node *) req;
885  }
886  }
887  }
888 
889  PG_RETURN_POINTER(ret);
890 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:351
#define IsA(nodeptr, _type_)
Definition: nodes.h:576
#define DatumGetInt32(X)
Definition: postgres.h:472
Selectivity restriction_selectivity(PlannerInfo *root, Oid operatorid, List *args, Oid inputcollid, int varRelid)
Definition: plancat.c:1763
Definition: nodes.h:525
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:271
double Selectivity
Definition: nodes.h:658
#define lsecond(l)
Definition: pg_list.h:200
signed int int32
Definition: c.h:347
#define linitial(l)
Definition: pg_list.h:195
char * s1
struct PlannerInfo * root
Definition: supportnodes.h:96
struct SpecialJoinInfo * sjinfo
Definition: supportnodes.h:103
double cpu_operator_cost
Definition: costsize.c:114
Selectivity join_selectivity(PlannerInfo *root, Oid operatorid, List *args, Oid inputcollid, JoinType jointype, SpecialJoinInfo *sjinfo)
Definition: plancat.c:1802
Definition: pg_list.h:50

◆ trigger_return_old()

Datum trigger_return_old ( PG_FUNCTION_ARGS  )

Definition at line 243 of file regress.c.

References CALLED_AS_TRIGGER, elog, ERROR, PointerGetDatum, and TriggerData::tg_trigtuple.

Referenced by reverse_name().

244 {
245  TriggerData *trigdata = (TriggerData *) fcinfo->context;
246  HeapTuple tuple;
247 
248  if (!CALLED_AS_TRIGGER(fcinfo))
249  elog(ERROR, "trigger_return_old: not fired by trigger manager");
250 
251  tuple = trigdata->tg_trigtuple;
252 
253  return PointerGetDatum(tuple);
254 }
#define PointerGetDatum(X)
Definition: postgres.h:556
HeapTuple tg_trigtuple
Definition: trigger.h:35
#define ERROR
Definition: elog.h:43
#define CALLED_AS_TRIGGER(fcinfo)
Definition: trigger.h:25
#define elog(elevel,...)
Definition: elog.h:228

◆ ttdummy()

Datum ttdummy ( PG_FUNCTION_ARGS  )

Definition at line 264 of file regress.c.

References generate_unaccent_rules::args, attnum, CALLED_AS_TRIGGER, CStringGetTextDatum, DatumGetInt64, DirectFunctionCall1, elog, ereport, errcode(), errmsg(), ERROR, i, Int32GetDatum, nextval(), palloc(), pfree(), PG_FUNCTION_INFO_V1(), PointerGetDatum, relname, set_ttdummy(), SPI_connect(), SPI_execp(), SPI_finish(), SPI_fnumber(), SPI_getbinval(), SPI_getrelname(), SPI_gettypeid(), SPI_keepplan(), SPI_modifytuple(), SPI_prepare(), SPI_result, SPI_result_code_string(), sprintf, TriggerData::tg_event, TriggerData::tg_newtuple, TriggerData::tg_relation, TriggerData::tg_trigger, TriggerData::tg_trigtuple, TRIGGER_FIRED_BEFORE, TRIGGER_FIRED_BY_INSERT, TRIGGER_FIRED_BY_UPDATE, TRIGGER_FIRED_FOR_ROW, TTDUMMY_INFINITY, and ttoff.

265 {
266  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  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  if (!CALLED_AS_TRIGGER(fcinfo))
288  elog(ERROR, "ttdummy: not fired by trigger manager");
289  if (!TRIGGER_FIRED_FOR_ROW(trigdata->tg_event))
290  elog(ERROR, "ttdummy: must be fired for row");
291  if (!TRIGGER_FIRED_BEFORE(trigdata->tg_event))
292  elog(ERROR, "ttdummy: must be fired before event");
293  if (TRIGGER_FIRED_BY_INSERT(trigdata->tg_event))
294  elog(ERROR, "ttdummy: cannot process INSERT event");
295  if (TRIGGER_FIRED_BY_UPDATE(trigdata->tg_event))
296  newtuple = trigdata->tg_newtuple;
297 
298  trigtuple = trigdata->tg_trigtuple;
299 
300  rel = trigdata->tg_relation;
301  relname = SPI_getrelname(rel);
302 
303  /* check if TT is OFF for this relation */
304  if (ttoff) /* OFF - nothing to do */
305  {
306  pfree(relname);
307  return PointerGetDatum((newtuple != NULL) ? newtuple : trigtuple);
308  }
309 
310  trigger = trigdata->tg_trigger;
311 
312  if (trigger->tgnargs != 2)
313  elog(ERROR, "ttdummy (%s): invalid (!= 2) number of arguments %d",
314  relname, trigger->tgnargs);
315 
316  args = trigger->tgargs;
317  tupdesc = rel->rd_att;
318  natts = tupdesc->natts;
319 
320  for (i = 0; i < 2; i++)
321  {
322  attnum[i] = SPI_fnumber(tupdesc, args[i]);
323  if (attnum[i] <= 0)
324  elog(ERROR, "ttdummy (%s): there is no attribute %s",
325  relname, args[i]);
326  if (SPI_gettypeid(tupdesc, attnum[i]) != INT4OID)
327  elog(ERROR, "ttdummy (%s): attribute %s must be of integer type",
328  relname, args[i]);
329  }
330 
331  oldon = SPI_getbinval(trigtuple, tupdesc, attnum[0], &isnull);
332  if (isnull)
333  elog(ERROR, "ttdummy (%s): %s must be NOT NULL", relname, args[0]);
334 
335  oldoff = SPI_getbinval(trigtuple, tupdesc, attnum[1], &isnull);
336  if (isnull)
337  elog(ERROR, "ttdummy (%s): %s must be NOT NULL", relname, args[1]);
338 
339  if (newtuple != NULL) /* UPDATE */
340  {
341  newon = SPI_getbinval(newtuple, tupdesc, attnum[0], &isnull);
342  if (isnull)
343  elog(ERROR, "ttdummy (%s): %s must be NOT NULL", relname, args[0]);
344  newoff = SPI_getbinval(newtuple, tupdesc, attnum[1], &isnull);
345  if (isnull)
346  elog(ERROR, "ttdummy (%s): %s must be NOT NULL", relname, args[1]);
347 
348  if (oldon != newon || oldoff != newoff)
349  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  if (newoff != TTDUMMY_INFINITY)
355  {
356  pfree(relname); /* allocated in upper executor context */
357  return PointerGetDatum(NULL);
358  }
359  }
360  else if (oldoff != TTDUMMY_INFINITY) /* DELETE */
361  {
362  pfree(relname);
363  return PointerGetDatum(NULL);
364  }
365 
366  newoff = DirectFunctionCall1(nextval, CStringGetTextDatum("ttdummy_seq"));
367  /* nextval now returns int64; coerce down to int32 */
368  newoff = Int32GetDatum((int32) DatumGetInt64(newoff));
369 
370  /* Connect to SPI manager */
371  if ((ret = SPI_connect()) < 0)
372  elog(ERROR, "ttdummy (%s): SPI_connect returned %d", relname, ret);
373 
374  /* Fetch tuple values and nulls */
375  cvals = (Datum *) palloc(natts * sizeof(Datum));
376  cnulls = (char *) palloc(natts * sizeof(char));
377  for (i = 0; i < natts; i++)
378  {
379  cvals[i] = SPI_getbinval((newtuple != NULL) ? newtuple : trigtuple,
380  tupdesc, i + 1, &isnull);
381  cnulls[i] = (isnull) ? 'n' : ' ';
382  }
383 
384  /* change date column(s) */
385  if (newtuple) /* UPDATE */
386  {
387  cvals[attnum[0] - 1] = newoff; /* start_date eq current date */
388  cnulls[attnum[0] - 1] = ' ';
389  cvals[attnum[1] - 1] = TTDUMMY_INFINITY; /* stop_date eq INFINITY */
390  cnulls[attnum[1] - 1] = ' ';
391  }
392  else
393  /* DELETE */
394  {
395  cvals[attnum[1] - 1] = newoff; /* stop_date eq current date */
396  cnulls[attnum[1] - 1] = ' ';
397  }
398 
399  /* if there is no plan ... */
400  if (splan == NULL)
401  {
402  SPIPlanPtr pplan;
403  Oid *ctypes;
404  char *query;
405 
406  /* allocate space in preparation */
407  ctypes = (Oid *) palloc(natts * sizeof(Oid));
408  query = (char *) palloc(100 + 16 * natts);
409 
410  /*
411  * Construct query: INSERT INTO _relation_ VALUES ($1, ...)
412  */
413  sprintf(query, "INSERT INTO %s VALUES (", relname);
414  for (i = 1; i <= natts; i++)
415  {
416  sprintf(query + strlen(query), "$%d%s",
417  i, (i < natts) ? ", " : ")");
418  ctypes[i - 1] = SPI_gettypeid(tupdesc, i);
419  }
420 
421  /* Prepare plan for query */
422  pplan = SPI_prepare(query, natts, ctypes);
423  if (pplan == NULL)
424  elog(ERROR, "ttdummy (%s): SPI_prepare returned %s", relname, SPI_result_code_string(SPI_result));
425 
426  if (SPI_keepplan(pplan))
427  elog(ERROR, "ttdummy (%s): SPI_keepplan failed", relname);
428 
429  splan = pplan;
430  }
431 
432  ret = SPI_execp(splan, cvals, cnulls, 0);
433 
434  if (ret < 0)
435  elog(ERROR, "ttdummy (%s): SPI_execp returned %d", relname, ret);
436 
437  /* Tuple to return to upper Executor ... */
438  if (newtuple) /* UPDATE */
439  rettuple = SPI_modifytuple(rel, trigtuple, 1, &(attnum[1]), &newoff, NULL);
440  else /* DELETE */
441  rettuple = trigtuple;
442 
443  SPI_finish(); /* don't forget say Bye to SPI mgr */
444 
445  pfree(relname);
446 
447  return PointerGetDatum(rettuple);
448 }
int SPI_fnumber(TupleDesc tupdesc, const char *fname)
Definition: spi.c:951
Oid SPI_gettypeid(TupleDesc tupdesc, int fnumber)
Definition: spi.c:1084
int SPI_connect(void)
Definition: spi.c:89
#define PointerGetDatum(X)
Definition: postgres.h:556
SPIPlanPtr SPI_prepare(const char *src, int nargs, Oid *argtypes)
Definition: spi.c:674
int SPI_finish(void)
Definition: spi.c:176
Datum nextval(PG_FUNCTION_ARGS)
Definition: sequence.c:549
int errcode(int sqlerrcode)
Definition: elog.c:608
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:615
NameData relname
Definition: pg_class.h:35
unsigned int Oid
Definition: postgres_ext.h:31
HeapTuple tg_trigtuple
Definition: trigger.h:35
signed int int32
Definition: c.h:347
static bool ttoff
Definition: regress.c:259
int SPI_result
Definition: spi.c:47
#define sprintf
Definition: port.h:194
void pfree(void *pointer)
Definition: mcxt.c:1056
#define ERROR
Definition: elog.h:43
Datum SPI_getbinval(HeapTuple tuple, TupleDesc tupdesc, int fnumber, bool *isnull)
Definition: spi.c:1028
#define DatumGetInt64(X)
Definition: postgres.h:607
const char * SPI_result_code_string(int code)
Definition: spi.c:1705
int SPI_keepplan(SPIPlanPtr plan)
Definition: spi.c:752
static SPIPlanPtr splan
Definition: regress.c:258
HeapTuple SPI_modifytuple(Relation rel, HeapTuple tuple, int natts, int *attnum, Datum *Values, const char *Nulls)
Definition: spi.c:882
#define ereport(elevel, rest)
Definition: elog.h:141
#define TTDUMMY_INFINITY
Definition: regress.c:256
uintptr_t Datum
Definition: postgres.h:367
Trigger * tg_trigger
Definition: trigger.h:37
HeapTuple tg_newtuple
Definition: trigger.h:36
char * SPI_getrelname(Relation rel)
Definition: spi.c:1102
int16 attnum
Definition: pg_attribute.h:79
#define CALLED_AS_TRIGGER(fcinfo)
Definition: trigger.h:25
TriggerEvent tg_event
Definition: trigger.h:33
#define TRIGGER_FIRED_BEFORE(event)
Definition: trigger.h:134
#define Int32GetDatum(X)
Definition: postgres.h:479
#define TRIGGER_FIRED_BY_INSERT(event)
Definition: trigger.h:116
void * palloc(Size size)
Definition: mcxt.c:949
int errmsg(const char *fmt,...)
Definition: elog.c:822
#define elog(elevel,...)
Definition: elog.h:228
int i
int SPI_execp(SPIPlanPtr plan, Datum *Values, const char *Nulls, long tcount)
Definition: spi.c:558
#define CStringGetTextDatum(s)
Definition: builtins.h:83
#define TRIGGER_FIRED_FOR_ROW(event)
Definition: trigger.h:128
#define TRIGGER_FIRED_BY_UPDATE(event)
Definition: trigger.h:122
Relation tg_relation
Definition: trigger.h:34

◆ wait_pid()

Datum wait_pid ( PG_FUNCTION_ARGS  )

Definition at line 651 of file regress.c.

References CHECK_FOR_INTERRUPTS, elog, ERROR, kill, PG_GETARG_INT32, PG_RETURN_VOID, pg_usleep(), and superuser().

Referenced by regress_putenv().

652 {
653  int pid = PG_GETARG_INT32(0);
654 
655  if (!superuser())
656  elog(ERROR, "must be superuser to check PID liveness");
657 
658  while (kill(pid, 0) == 0)
659  {
661  pg_usleep(50000);
662  }
663 
664  if (errno != ESRCH)
665  elog(ERROR, "could not check PID %d liveness: %m", pid);
666 
667  PG_RETURN_VOID();
668 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:264
bool superuser(void)
Definition: superuser.c:46
#define kill(pid, sig)
Definition: win32_port.h:426
void pg_usleep(long microsec)
Definition: signal.c:53
#define ERROR
Definition: elog.h:43
#define PG_RETURN_VOID()
Definition: fmgr.h:339
#define elog(elevel,...)
Definition: elog.h:228
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:99

◆ widget_in()

Datum widget_in ( PG_FUNCTION_ARGS  )

Definition at line 165 of file regress.c.

References WIDGET::center, DELIM, ereport, errcode(), errmsg(), ERROR, i, LDELIM, NARGS, palloc(), PG_GETARG_CSTRING, PG_RETURN_POINTER, WIDGET::radius, RDELIM, generate_unaccent_rules::str, Point::x, and Point::y.

166 {
167  char *str = PG_GETARG_CSTRING(0);
168  char *p,
169  *coord[NARGS];
170  int i;
171  WIDGET *result;
172 
173  for (i = 0, p = str; *p && i < NARGS && *p != RDELIM; p++)
174  {
175  if (*p == DELIM || (*p == LDELIM && i == 0))
176  coord[i++] = p + 1;
177  }
178 
179  if (i < NARGS)
180  ereport(ERROR,
181  (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
182  errmsg("invalid input syntax for type %s: \"%s\"",
183  "widget", str)));
184 
185  result = (WIDGET *) palloc(sizeof(WIDGET));
186  result->center.x = atof(coord[0]);
187  result->center.y = atof(coord[1]);
188  result->radius = atof(coord[2]);
189 
190  PG_RETURN_POINTER(result);
191 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:351
double radius
Definition: regress.c:156
#define RDELIM
Definition: regress.c:72
float8 x
Definition: geo_decls.h:57
int errcode(int sqlerrcode)
Definition: elog.c:608
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:141
#define DELIM
Definition: regress.c:73
float8 y
Definition: geo_decls.h:57
#define NARGS
Definition: regress.c:162
void * palloc(Size size)
Definition: mcxt.c:949
int errmsg(const char *fmt,...)
Definition: elog.c:822
Point center
Definition: regress.c:155
int i
#define PG_GETARG_CSTRING(n)
Definition: fmgr.h:272
#define LDELIM
Definition: regress.c:71

◆ widget_out()

Datum widget_out ( PG_FUNCTION_ARGS  )

Definition at line 194 of file regress.c.

References WIDGET::center, PG_FUNCTION_INFO_V1(), PG_GETARG_POINTER, PG_RETURN_CSTRING, psprintf(), pt_in_widget(), WIDGET::radius, generate_unaccent_rules::str, Point::x, and Point::y.

195 {
196  WIDGET *widget = (WIDGET *) PG_GETARG_POINTER(0);
197  char *str = psprintf("(%g,%g,%g)",
198  widget->center.x, widget->center.y, widget->radius);
199 
200  PG_RETURN_CSTRING(str);
201 }
double radius
Definition: regress.c:156
float8 x
Definition: geo_decls.h:57
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:271
float8 y
Definition: geo_decls.h:57
#define PG_RETURN_CSTRING(x)
Definition: fmgr.h:352
Point center
Definition: regress.c:155

Variable Documentation

◆ PG_MODULE_MAGIC

PG_MODULE_MAGIC

Definition at line 77 of file regress.c.

◆ splan

SPIPlanPtr splan = NULL
static

◆ ttoff

bool ttoff = false
static

Definition at line 259 of file regress.c.

Referenced by set_ttdummy(), and ttdummy().