PostgreSQL Source Code  git master
regress.c File Reference
#include "postgres.h"
#include <math.h>
#include <signal.h>
#include "access/htup_details.h"
#include "access/transam.h"
#include "access/tuptoaster.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/rel.h"
#include "utils/typcache.h"
#include "utils/memutils.h"
Include dependency graph for regress.c:

Go to the source code of this file.

Data Structures

struct  WIDGET
 

Macros

#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 46 of file regress.c.

Referenced by widget_in().

◆ LDELIM

#define LDELIM   '('

Definition at line 44 of file regress.c.

Referenced by widget_in().

◆ NARGS

#define NARGS   3

Definition at line 135 of file regress.c.

Referenced by widget_in().

◆ RDELIM

#define RDELIM   ')'

Definition at line 45 of file regress.c.

Referenced by widget_in().

◆ TTDUMMY_INFINITY

#define TTDUMMY_INFINITY   999999

Definition at line 229 of file regress.c.

Referenced by ttdummy().

Function Documentation

◆ int44in()

Datum int44in ( PG_FUNCTION_ARGS  )

Definition at line 464 of file regress.c.

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

Referenced by set_ttdummy().

465 {
466  char *input_string = PG_GETARG_CSTRING(0);
467  int32 *result = (int32 *) palloc(4 * sizeof(int32));
468  int i;
469 
470  i = sscanf(input_string,
471  "%d, %d, %d, %d",
472  &result[0],
473  &result[1],
474  &result[2],
475  &result[3]);
476  while (i < 4)
477  result[i++] = 0;
478 
479  PG_RETURN_POINTER(result);
480 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:351
signed int int32
Definition: c.h:346
void * palloc(Size size)
Definition: mcxt.c:924
int i
#define PG_GETARG_CSTRING(n)
Definition: fmgr.h:272

◆ int44out()

Datum int44out ( PG_FUNCTION_ARGS  )

Definition at line 488 of file regress.c.

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

Referenced by int44in().

489 {
490  int32 *an_array = (int32 *) PG_GETARG_POINTER(0);
491  char *result = (char *) palloc(16 * 4);
492 
493  snprintf(result, 16 * 4, "%d,%d,%d,%d",
494  an_array[0],
495  an_array[1],
496  an_array[2],
497  an_array[3]);
498 
499  PG_RETURN_CSTRING(result);
500 }
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:271
signed int int32
Definition: c.h:346
#define PG_RETURN_CSTRING(x)
Definition: fmgr.h:352
void * palloc(Size size)
Definition: mcxt.c:924
#define snprintf
Definition: port.h:192

◆ interpt_pp()

Datum interpt_pp ( PG_FUNCTION_ARGS  )

Definition at line 57 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().

58 {
59  PATH *p1 = PG_GETARG_PATH_P(0);
60  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  found = false; /* Haven't found it yet */
68 
69  for (i = 0; i < p1->npts - 1 && !found; i++)
70  {
71  regress_lseg_construct(&seg1, &p1->p[i], &p1->p[i + 1]);
72  for (j = 0; j < p2->npts - 1 && !found; j++)
73  {
74  regress_lseg_construct(&seg2, &p2->p[j], &p2->p[j + 1]);
76  LsegPGetDatum(&seg1),
77  LsegPGetDatum(&seg2))))
78  found = true;
79  }
80  }
81 
82  if (!found)
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  */
91  LsegPGetDatum(&seg1),
92  LsegPGetDatum(&seg2)));
93 }
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:98
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:618
#define PG_RETURN_NULL()
Definition: fmgr.h:335

◆ make_tuple_indirect()

Datum make_tuple_indirect ( PG_FUNCTION_ARGS  )

Definition at line 504 of file regress.c.

References attlen, DatumGetPointer, heap_deform_tuple(), heap_form_tuple(), heap_tuple_fetch_attr(), 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().

505 {
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  tupType = HeapTupleHeaderGetTypeId(rec);
524  tupTypmod = HeapTupleHeaderGetTypMod(rec);
525  tupdesc = lookup_rowtype_tupdesc(tupType, tupTypmod);
526  ncolumns = tupdesc->natts;
527 
528  /* Build a temporary HeapTuple control structure */
530  ItemPointerSetInvalid(&(tuple.t_self));
531  tuple.t_tableOid = InvalidOid;
532  tuple.t_data = rec;
533 
534  values = (Datum *) palloc(ncolumns * sizeof(Datum));
535  nulls = (bool *) palloc(ncolumns * sizeof(bool));
536 
537  heap_deform_tuple(&tuple, tupdesc, values, nulls);
538 
540 
541  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  if (TupleDescAttr(tupdesc, i)->attisdropped ||
549  nulls[i] ||
550  TupleDescAttr(tupdesc, i)->attlen != -1)
551  continue;
552 
553  attr = (struct varlena *) DatumGetPointer(values[i]);
554 
555  /* don't recursively indirect */
556  if (VARATT_IS_EXTERNAL_INDIRECT(attr))
557  continue;
558 
559  /* copy datum, so it still lives later */
560  if (VARATT_IS_EXTERNAL_ONDISK(attr))
561  attr = heap_tuple_fetch_attr(attr);
562  else
563  {
564  struct varlena *oldattr = attr;
565 
566  attr = palloc0(VARSIZE_ANY(oldattr));
567  memcpy(attr, oldattr, VARSIZE_ANY(oldattr));
568  }
569 
570  /* build indirection Datum */
571  new_attr = (struct varlena *) palloc0(INDIRECT_POINTER_SIZE);
572  redirect_pointer.pointer = attr;
574  memcpy(VARDATA_EXTERNAL(new_attr), &redirect_pointer,
575  sizeof(redirect_pointer));
576 
577  values[i] = PointerGetDatum(new_attr);
578  }
579 
580  newtup = heap_form_tuple(tupdesc, values, nulls);
581  pfree(values);
582  pfree(nulls);
583  ReleaseTupleDesc(tupdesc);
584 
585  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  PG_RETURN_POINTER(newtup->t_data);
597 }
#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
struct varlena * heap_tuple_fetch_attr(struct varlena *attr)
Definition: tuptoaster.c:99
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
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:346
HeapTupleHeader t_data
Definition: htup.h:68
#define HeapTupleHeaderGetTypMod(tup)
Definition: htup_details.h:468
void pfree(void *pointer)
Definition: mcxt.c:1031
#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
int16 attlen
Definition: pg_attribute.h:64
#define INDIRECT_POINTER_SIZE
Definition: tuptoaster.h:102
void * palloc0(Size size)
Definition: mcxt.c:955
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:924
int i
Definition: c.h:549
#define ReleaseTupleDesc(tupdesc)
Definition: tupdesc.h:122
#define HeapTupleHeaderGetDatumLength(tup)
Definition: htup_details.h:452

◆ overpaid()

Datum overpaid ( PG_FUNCTION_ARGS  )

Definition at line 109 of file regress.c.

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

Referenced by regress_lseg_construct().

110 {
112  bool isnull;
113  int32 salary;
114 
115  salary = DatumGetInt32(GetAttributeByName(tuple, "salary", &isnull));
116  if (isnull)
117  PG_RETURN_NULL();
118  PG_RETURN_BOOL(salary > 699);
119 }
#define DatumGetInt32(X)
Definition: postgres.h:472
Datum GetAttributeByName(HeapTupleHeader tuple, const char *attname, bool *isNull)
Definition: execUtils.c:963
#define PG_GETARG_HEAPTUPLEHEADER(n)
Definition: fmgr.h:306
signed int int32
Definition: c.h:346
#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 179 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().

180 {
181  Point *point = PG_GETARG_POINT_P(0);
182  WIDGET *widget = (WIDGET *) PG_GETARG_POINTER(1);
183  float8 distance;
184 
186  PointPGetDatum(point),
187  PointPGetDatum(&widget->center)));
188 
189  PG_RETURN_BOOL(distance < widget->radius);
190 }
#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:491
#define PG_GETARG_POINT_P(n)
Definition: geo_decls.h:136
#define DatumGetFloat8(X)
Definition: postgres.h:728
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349
Point center
Definition: regress.c:128
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:618
#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 98 of file regress.c.

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

Referenced by interpt_pp().

99 {
100  lseg->p[0].x = pt1->x;
101  lseg->p[0].y = pt1->y;
102  lseg->p[1].x = pt2->x;
103  lseg->p[1].y = pt2->y;
104 }
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 602 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().

603 {
604  MemoryContext oldcontext;
605  char *envbuf;
606 
607  if (!superuser())
608  elog(ERROR, "must be superuser to change environment variables");
609 
611  envbuf = text_to_cstring((text *) PG_GETARG_POINTER(0));
612  MemoryContextSwitchTo(oldcontext);
613 
614  if (putenv(envbuf) != 0)
615  elog(ERROR, "could not set environment variable: %m");
616 
617  PG_RETURN_VOID();
618 }
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
bool superuser(void)
Definition: superuser.c:47
#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:483
char * text_to_cstring(const text *t)
Definition: varlena.c:204
#define elog(elevel,...)
Definition: elog.h:226
Definition: c.h:549

◆ reverse_name()

Datum reverse_name ( PG_FUNCTION_ARGS  )

Definition at line 195 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().

196 {
197  char *string = PG_GETARG_CSTRING(0);
198  int i;
199  int len;
200  char *new_string;
201 
202  new_string = palloc0(NAMEDATALEN);
203  for (i = 0; i < NAMEDATALEN && string[i]; ++i)
204  ;
205  if (i == NAMEDATALEN || !string[i])
206  --i;
207  len = i;
208  for (; i >= 0; --i)
209  new_string[len - i] = string[i];
210  PG_RETURN_CSTRING(new_string);
211 }
#define NAMEDATALEN
void * palloc0(Size size)
Definition: mcxt.c:955
#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 426 of file regress.c.

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

Referenced by ttdummy().

427 {
428  int32 on = PG_GETARG_INT32(0);
429 
430  if (ttoff) /* OFF currently */
431  {
432  if (on == 0)
433  PG_RETURN_INT32(0);
434 
435  /* turn ON */
436  ttoff = false;
437  PG_RETURN_INT32(0);
438  }
439 
440  /* ON currently */
441  if (on != 0)
442  PG_RETURN_INT32(1);
443 
444  /* turn OFF */
445  ttoff = true;
446 
447  PG_RETURN_INT32(1);
448 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:264
#define PG_RETURN_INT32(x)
Definition: fmgr.h:344
signed int int32
Definition: c.h:346
static bool ttoff
Definition: regress.c:232

◆ test_atomic_flag()

static void test_atomic_flag ( void  )
static

Definition at line 644 of file regress.c.

References elog, ERROR, 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().

645 {
647 
648  pg_atomic_init_flag(&flag);
649 
650  if (!pg_atomic_unlocked_test_flag(&flag))
651  elog(ERROR, "flag: unexpectedly set");
652 
653  if (!pg_atomic_test_set_flag(&flag))
654  elog(ERROR, "flag: couldn't set");
655 
656  if (pg_atomic_unlocked_test_flag(&flag))
657  elog(ERROR, "flag: unexpectedly unset");
658 
659  if (pg_atomic_test_set_flag(&flag))
660  elog(ERROR, "flag: set spuriously #2");
661 
662  pg_atomic_clear_flag(&flag);
663 
664  if (!pg_atomic_unlocked_test_flag(&flag))
665  elog(ERROR, "flag: unexpectedly set #2");
666 
667  if (!pg_atomic_test_set_flag(&flag))
668  elog(ERROR, "flag: couldn't set");
669 
670  pg_atomic_clear_flag(&flag);
671 }
static void pg_atomic_clear_flag(volatile pg_atomic_flag *ptr)
Definition: atomics.h:212
#define ERROR
Definition: elog.h:43
static bool pg_atomic_test_set_flag(volatile pg_atomic_flag *ptr)
Definition: atomics.h:188
char * flag(int b)
Definition: test-ctype.c:33
static bool pg_atomic_unlocked_test_flag(volatile pg_atomic_flag *ptr)
Definition: atomics.h:201
#define elog(elevel,...)
Definition: elog.h:226
static void pg_atomic_init_flag(volatile pg_atomic_flag *ptr)
Definition: atomics.h:175

◆ test_atomic_ops()

Datum test_atomic_ops ( PG_FUNCTION_ARGS  )

Definition at line 852 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().

853 {
855 
857 
859 
860  PG_RETURN_BOOL(true);
861 }
static void test_atomic_uint32(void)
Definition: regress.c:674
static void test_atomic_flag(void)
Definition: regress.c:644
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349
static void test_atomic_uint64(void)
Definition: regress.c:774

◆ test_atomic_uint32()

static void test_atomic_uint32 ( void  )
static

Definition at line 674 of file regress.c.

References elog, ERROR, 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(), and pg_atomic_write_u32().

Referenced by test_atomic_ops().

675 {
676  pg_atomic_uint32 var;
677  uint32 expected;
678  int i;
679 
680  pg_atomic_init_u32(&var, 0);
681 
682  if (pg_atomic_read_u32(&var) != 0)
683  elog(ERROR, "atomic_read_u32() #1 wrong");
684 
685  pg_atomic_write_u32(&var, 3);
686 
687  if (pg_atomic_read_u32(&var) != 3)
688  elog(ERROR, "atomic_read_u32() #2 wrong");
689 
690  if (pg_atomic_fetch_add_u32(&var, 1) != 3)
691  elog(ERROR, "atomic_fetch_add_u32() #1 wrong");
692 
693  if (pg_atomic_fetch_sub_u32(&var, 1) != 4)
694  elog(ERROR, "atomic_fetch_sub_u32() #1 wrong");
695 
696  if (pg_atomic_sub_fetch_u32(&var, 3) != 0)
697  elog(ERROR, "atomic_sub_fetch_u32() #1 wrong");
698 
699  if (pg_atomic_add_fetch_u32(&var, 10) != 10)
700  elog(ERROR, "atomic_add_fetch_u32() #1 wrong");
701 
702  if (pg_atomic_exchange_u32(&var, 5) != 10)
703  elog(ERROR, "pg_atomic_exchange_u32() #1 wrong");
704 
705  if (pg_atomic_exchange_u32(&var, 0) != 5)
706  elog(ERROR, "pg_atomic_exchange_u32() #0 wrong");
707 
708  /* test around numerical limits */
709  if (pg_atomic_fetch_add_u32(&var, INT_MAX) != 0)
710  elog(ERROR, "pg_atomic_fetch_add_u32() #2 wrong");
711 
712  if (pg_atomic_fetch_add_u32(&var, INT_MAX) != INT_MAX)
713  elog(ERROR, "pg_atomic_add_fetch_u32() #3 wrong");
714 
715  pg_atomic_fetch_add_u32(&var, 1); /* top up to UINT_MAX */
716 
717  if (pg_atomic_read_u32(&var) != UINT_MAX)
718  elog(ERROR, "atomic_read_u32() #2 wrong");
719 
720  if (pg_atomic_fetch_sub_u32(&var, INT_MAX) != UINT_MAX)
721  elog(ERROR, "pg_atomic_fetch_sub_u32() #2 wrong");
722 
723  if (pg_atomic_read_u32(&var) != (uint32) INT_MAX + 1)
724  elog(ERROR, "atomic_read_u32() #3 wrong: %u", pg_atomic_read_u32(&var));
725 
726  expected = pg_atomic_sub_fetch_u32(&var, INT_MAX);
727  if (expected != 1)
728  elog(ERROR, "pg_atomic_sub_fetch_u32() #3 wrong: %u", expected);
729 
730  pg_atomic_sub_fetch_u32(&var, 1);
731 
732  /* fail exchange because of old expected */
733  expected = 10;
734  if (pg_atomic_compare_exchange_u32(&var, &expected, 1))
735  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  for (i = 0; i < 1000; i++)
739  {
740  expected = 0;
741  if (!pg_atomic_compare_exchange_u32(&var, &expected, 1))
742  break;
743  }
744  if (i == 1000)
745  elog(ERROR, "atomic_compare_exchange_u32() never succeeded");
746  if (pg_atomic_read_u32(&var) != 1)
747  elog(ERROR, "atomic_compare_exchange_u32() didn't set value properly");
748 
749  pg_atomic_write_u32(&var, 0);
750 
751  /* try setting flagbits */
752  if (pg_atomic_fetch_or_u32(&var, 1) & 1)
753  elog(ERROR, "pg_atomic_fetch_or_u32() #1 wrong");
754 
755  if (!(pg_atomic_fetch_or_u32(&var, 2) & 1))
756  elog(ERROR, "pg_atomic_fetch_or_u32() #2 wrong");
757 
758  if (pg_atomic_read_u32(&var) != 3)
759  elog(ERROR, "invalid result after pg_atomic_fetch_or_u32()");
760 
761  /* try clearing flagbits */
762  if ((pg_atomic_fetch_and_u32(&var, ~2) & 3) != 3)
763  elog(ERROR, "pg_atomic_fetch_and_u32() #1 wrong");
764 
765  if (pg_atomic_fetch_and_u32(&var, ~1) != 1)
766  elog(ERROR, "pg_atomic_fetch_and_u32() #2 wrong: is %u",
767  pg_atomic_read_u32(&var));
768  /* no bits set anymore */
769  if (pg_atomic_fetch_and_u32(&var, ~0) != 0)
770  elog(ERROR, "pg_atomic_fetch_and_u32() #3 wrong");
771 }
static uint32 pg_atomic_fetch_sub_u32(volatile pg_atomic_uint32 *ptr, int32 sub_)
Definition: atomics.h:346
static bool pg_atomic_compare_exchange_u32(volatile pg_atomic_uint32 *ptr, uint32 *expected, uint32 newval)
Definition: atomics.h:314
static uint32 pg_atomic_sub_fetch_u32(volatile pg_atomic_uint32 *ptr, int32 sub_)
Definition: atomics.h:404
static uint32 pg_atomic_add_fetch_u32(volatile pg_atomic_uint32 *ptr, int32 add_)
Definition: atomics.h:389
static uint32 pg_atomic_exchange_u32(volatile pg_atomic_uint32 *ptr, uint32 newval)
Definition: atomics.h:295
#define ERROR
Definition: elog.h:43
static uint32 pg_atomic_fetch_and_u32(volatile pg_atomic_uint32 *ptr, uint32 and_)
Definition: atomics.h:361
unsigned int uint32
Definition: c.h:358
static uint32 pg_atomic_fetch_add_u32(volatile pg_atomic_uint32 *ptr, int32 add_)
Definition: atomics.h:331
#define elog(elevel,...)
Definition: elog.h:226
int i
static uint32 pg_atomic_fetch_or_u32(volatile pg_atomic_uint32 *ptr, uint32 or_)
Definition: atomics.h:375
static void pg_atomic_init_u32(volatile pg_atomic_uint32 *ptr, uint32 val)
Definition: atomics.h:226
static void pg_atomic_write_u32(volatile pg_atomic_uint32 *ptr, uint32 val)
Definition: atomics.h:261
static uint32 pg_atomic_read_u32(volatile pg_atomic_uint32 *ptr)
Definition: atomics.h:244

◆ test_atomic_uint64()

static void test_atomic_uint64 ( void  )
static

Definition at line 774 of file regress.c.

References elog, ERROR, 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(), test_atomic_ops(), and UINT64_FORMAT.

Referenced by test_atomic_ops().

775 {
776  pg_atomic_uint64 var;
777  uint64 expected;
778  int i;
779 
780  pg_atomic_init_u64(&var, 0);
781 
782  if (pg_atomic_read_u64(&var) != 0)
783  elog(ERROR, "atomic_read_u64() #1 wrong");
784 
785  pg_atomic_write_u64(&var, 3);
786 
787  if (pg_atomic_read_u64(&var) != 3)
788  elog(ERROR, "atomic_read_u64() #2 wrong");
789 
790  if (pg_atomic_fetch_add_u64(&var, 1) != 3)
791  elog(ERROR, "atomic_fetch_add_u64() #1 wrong");
792 
793  if (pg_atomic_fetch_sub_u64(&var, 1) != 4)
794  elog(ERROR, "atomic_fetch_sub_u64() #1 wrong");
795 
796  if (pg_atomic_sub_fetch_u64(&var, 3) != 0)
797  elog(ERROR, "atomic_sub_fetch_u64() #1 wrong");
798 
799  if (pg_atomic_add_fetch_u64(&var, 10) != 10)
800  elog(ERROR, "atomic_add_fetch_u64() #1 wrong");
801 
802  if (pg_atomic_exchange_u64(&var, 5) != 10)
803  elog(ERROR, "pg_atomic_exchange_u64() #1 wrong");
804 
805  if (pg_atomic_exchange_u64(&var, 0) != 5)
806  elog(ERROR, "pg_atomic_exchange_u64() #0 wrong");
807 
808  /* fail exchange because of old expected */
809  expected = 10;
810  if (pg_atomic_compare_exchange_u64(&var, &expected, 1))
811  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  for (i = 0; i < 100; i++)
815  {
816  expected = 0;
817  if (!pg_atomic_compare_exchange_u64(&var, &expected, 1))
818  break;
819  }
820  if (i == 100)
821  elog(ERROR, "atomic_compare_exchange_u64() never succeeded");
822  if (pg_atomic_read_u64(&var) != 1)
823  elog(ERROR, "atomic_compare_exchange_u64() didn't set value properly");
824 
825  pg_atomic_write_u64(&var, 0);
826 
827  /* try setting flagbits */
828  if (pg_atomic_fetch_or_u64(&var, 1) & 1)
829  elog(ERROR, "pg_atomic_fetch_or_u64() #1 wrong");
830 
831  if (!(pg_atomic_fetch_or_u64(&var, 2) & 1))
832  elog(ERROR, "pg_atomic_fetch_or_u64() #2 wrong");
833 
834  if (pg_atomic_read_u64(&var) != 3)
835  elog(ERROR, "invalid result after pg_atomic_fetch_or_u64()");
836 
837  /* try clearing flagbits */
838  if ((pg_atomic_fetch_and_u64(&var, ~2) & 3) != 3)
839  elog(ERROR, "pg_atomic_fetch_and_u64() #1 wrong");
840 
841  if (pg_atomic_fetch_and_u64(&var, ~1) != 1)
842  elog(ERROR, "pg_atomic_fetch_and_u64() #2 wrong: is " UINT64_FORMAT,
843  pg_atomic_read_u64(&var));
844  /* no bits set anymore */
845  if (pg_atomic_fetch_and_u64(&var, ~0) != 0)
846  elog(ERROR, "pg_atomic_fetch_and_u64() #3 wrong");
847 }
static uint64 pg_atomic_fetch_add_u64(volatile pg_atomic_uint64 *ptr, int64 add_)
Definition: atomics.h:470
static uint64 pg_atomic_sub_fetch_u64(volatile pg_atomic_uint64 *ptr, int64 sub_)
Definition: atomics.h:516
static uint64 pg_atomic_fetch_sub_u64(volatile pg_atomic_uint64 *ptr, int64 sub_)
Definition: atomics.h:479
static void pg_atomic_write_u64(volatile pg_atomic_uint64 *ptr, uint64 val)
Definition: atomics.h:441
static bool pg_atomic_compare_exchange_u64(volatile pg_atomic_uint64 *ptr, uint64 *expected, uint64 newval)
Definition: atomics.h:459
static uint64 pg_atomic_exchange_u64(volatile pg_atomic_uint64 *ptr, uint64 newval)
Definition: atomics.h:450
static void pg_atomic_init_u64(volatile pg_atomic_uint64 *ptr, uint64 val)
Definition: atomics.h:418
#define ERROR
Definition: elog.h:43
static uint64 pg_atomic_fetch_or_u64(volatile pg_atomic_uint64 *ptr, uint64 or_)
Definition: atomics.h:498
static uint64 pg_atomic_fetch_and_u64(volatile pg_atomic_uint64 *ptr, uint64 and_)
Definition: atomics.h:489
static uint64 pg_atomic_add_fetch_u64(volatile pg_atomic_uint64 *ptr, int64 add_)
Definition: atomics.h:507
static uint64 pg_atomic_read_u64(volatile pg_atomic_uint64 *ptr)
Definition: atomics.h:432
#define elog(elevel,...)
Definition: elog.h:226
int i
#define UINT64_FORMAT
Definition: c.h:401

◆ test_fdw_handler()

Datum test_fdw_handler ( PG_FUNCTION_ARGS  )

Definition at line 865 of file regress.c.

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

Referenced by test_atomic_ops().

866 {
867  elog(ERROR, "test_fdw_handler is not implemented");
868  PG_RETURN_NULL();
869 }
#define ERROR
Definition: elog.h:43
#define elog(elevel,...)
Definition: elog.h:226
#define PG_RETURN_NULL()
Definition: fmgr.h:335

◆ test_support_func()

Datum test_support_func ( PG_FUNCTION_ARGS  )

Definition at line 873 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().

874 {
875  Node *rawreq = (Node *) PG_GETARG_POINTER(0);
876  Node *ret = NULL;
877 
878  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  */
885  Selectivity s1;
886 
887  if (req->is_join)
888  s1 = join_selectivity(req->root, Int4EqualOperator,
889  req->args,
890  req->inputcollid,
891  req->jointype,
892  req->sjinfo);
893  else
894  s1 = restriction_selectivity(req->root, Int4EqualOperator,
895  req->args,
896  req->inputcollid,
897  req->varRelid);
898 
899  req->selectivity = s1;
900  ret = (Node *) req;
901  }
902 
903  if (IsA(rawreq, SupportRequestCost))
904  {
905  /* Provide some generic estimate */
906  SupportRequestCost *req = (SupportRequestCost *) rawreq;
907 
908  req->startup = 0;
909  req->per_tuple = 2 * cpu_operator_cost;
910  ret = (Node *) req;
911  }
912 
913  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  SupportRequestRows *req = (SupportRequestRows *) rawreq;
920 
921  if (req->node && IsA(req->node, FuncExpr)) /* be paranoid */
922  {
923  List *args = ((FuncExpr *) req->node)->args;
924  Node *arg1 = linitial(args);
925  Node *arg2 = lsecond(args);
926 
927  if (IsA(arg1, Const) &&
928  !((Const *) arg1)->constisnull &&
929  IsA(arg2, Const) &&
930  !((Const *) arg2)->constisnull)
931  {
932  int32 val1 = DatumGetInt32(((Const *) arg1)->constvalue);
933  int32 val2 = DatumGetInt32(((Const *) arg2)->constvalue);
934 
935  req->rows = val2 - val1 + 1;
936  ret = (Node *) req;
937  }
938  }
939  }
940 
941  PG_RETURN_POINTER(ret);
942 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:351
#define IsA(nodeptr, _type_)
Definition: nodes.h:575
#define DatumGetInt32(X)
Definition: postgres.h:472
Selectivity restriction_selectivity(PlannerInfo *root, Oid operatorid, List *args, Oid inputcollid, int varRelid)
Definition: plancat.c:1764
Definition: nodes.h:524
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:271
double Selectivity
Definition: nodes.h:657
#define lsecond(l)
Definition: pg_list.h:200
signed int int32
Definition: c.h:346
#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:1803
Definition: pg_list.h:50

◆ trigger_return_old()

Datum trigger_return_old ( PG_FUNCTION_ARGS  )

Definition at line 216 of file regress.c.

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

Referenced by reverse_name().

217 {
218  TriggerData *trigdata = (TriggerData *) fcinfo->context;
219  HeapTuple tuple;
220 
221  if (!CALLED_AS_TRIGGER(fcinfo))
222  elog(ERROR, "trigger_return_old: not fired by trigger manager");
223 
224  tuple = trigdata->tg_trigtuple;
225 
226  return PointerGetDatum(tuple);
227 }
#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:226

◆ ttdummy()

Datum ttdummy ( PG_FUNCTION_ARGS  )

Definition at line 237 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.

238 {
239  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  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  if (!CALLED_AS_TRIGGER(fcinfo))
261  elog(ERROR, "ttdummy: not fired by trigger manager");
262  if (!TRIGGER_FIRED_FOR_ROW(trigdata->tg_event))
263  elog(ERROR, "ttdummy: must be fired for row");
264  if (!TRIGGER_FIRED_BEFORE(trigdata->tg_event))
265  elog(ERROR, "ttdummy: must be fired before event");
266  if (TRIGGER_FIRED_BY_INSERT(trigdata->tg_event))
267  elog(ERROR, "ttdummy: cannot process INSERT event");
268  if (TRIGGER_FIRED_BY_UPDATE(trigdata->tg_event))
269  newtuple = trigdata->tg_newtuple;
270 
271  trigtuple = trigdata->tg_trigtuple;
272 
273  rel = trigdata->tg_relation;
274  relname = SPI_getrelname(rel);
275 
276  /* check if TT is OFF for this relation */
277  if (ttoff) /* OFF - nothing to do */
278  {
279  pfree(relname);
280  return PointerGetDatum((newtuple != NULL) ? newtuple : trigtuple);
281  }
282 
283  trigger = trigdata->tg_trigger;
284 
285  if (trigger->tgnargs != 2)
286  elog(ERROR, "ttdummy (%s): invalid (!= 2) number of arguments %d",
287  relname, trigger->tgnargs);
288 
289  args = trigger->tgargs;
290  tupdesc = rel->rd_att;
291  natts = tupdesc->natts;
292 
293  for (i = 0; i < 2; i++)
294  {
295  attnum[i] = SPI_fnumber(tupdesc, args[i]);
296  if (attnum[i] <= 0)
297  elog(ERROR, "ttdummy (%s): there is no attribute %s",
298  relname, args[i]);
299  if (SPI_gettypeid(tupdesc, attnum[i]) != INT4OID)
300  elog(ERROR, "ttdummy (%s): attribute %s must be of integer type",
301  relname, args[i]);
302  }
303 
304  oldon = SPI_getbinval(trigtuple, tupdesc, attnum[0], &isnull);
305  if (isnull)
306  elog(ERROR, "ttdummy (%s): %s must be NOT NULL", relname, args[0]);
307 
308  oldoff = SPI_getbinval(trigtuple, tupdesc, attnum[1], &isnull);
309  if (isnull)
310  elog(ERROR, "ttdummy (%s): %s must be NOT NULL", relname, args[1]);
311 
312  if (newtuple != NULL) /* UPDATE */
313  {
314  newon = SPI_getbinval(newtuple, tupdesc, attnum[0], &isnull);
315  if (isnull)
316  elog(ERROR, "ttdummy (%s): %s must be NOT NULL", relname, args[0]);
317  newoff = SPI_getbinval(newtuple, tupdesc, attnum[1], &isnull);
318  if (isnull)
319  elog(ERROR, "ttdummy (%s): %s must be NOT NULL", relname, args[1]);
320 
321  if (oldon != newon || oldoff != newoff)
322  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  if (newoff != TTDUMMY_INFINITY)
328  {
329  pfree(relname); /* allocated in upper executor context */
330  return PointerGetDatum(NULL);
331  }
332  }
333  else if (oldoff != TTDUMMY_INFINITY) /* DELETE */
334  {
335  pfree(relname);
336  return PointerGetDatum(NULL);
337  }
338 
339  newoff = DirectFunctionCall1(nextval, CStringGetTextDatum("ttdummy_seq"));
340  /* nextval now returns int64; coerce down to int32 */
341  newoff = Int32GetDatum((int32) DatumGetInt64(newoff));
342 
343  /* Connect to SPI manager */
344  if ((ret = SPI_connect()) < 0)
345  elog(ERROR, "ttdummy (%s): SPI_connect returned %d", relname, ret);
346 
347  /* Fetch tuple values and nulls */
348  cvals = (Datum *) palloc(natts * sizeof(Datum));
349  cnulls = (char *) palloc(natts * sizeof(char));
350  for (i = 0; i < natts; i++)
351  {
352  cvals[i] = SPI_getbinval((newtuple != NULL) ? newtuple : trigtuple,
353  tupdesc, i + 1, &isnull);
354  cnulls[i] = (isnull) ? 'n' : ' ';
355  }
356 
357  /* change date column(s) */
358  if (newtuple) /* UPDATE */
359  {
360  cvals[attnum[0] - 1] = newoff; /* start_date eq current date */
361  cnulls[attnum[0] - 1] = ' ';
362  cvals[attnum[1] - 1] = TTDUMMY_INFINITY; /* stop_date eq INFINITY */
363  cnulls[attnum[1] - 1] = ' ';
364  }
365  else
366  /* DELETE */
367  {
368  cvals[attnum[1] - 1] = newoff; /* stop_date eq current date */
369  cnulls[attnum[1] - 1] = ' ';
370  }
371 
372  /* if there is no plan ... */
373  if (splan == NULL)
374  {
375  SPIPlanPtr pplan;
376  Oid *ctypes;
377  char *query;
378 
379  /* allocate space in preparation */
380  ctypes = (Oid *) palloc(natts * sizeof(Oid));
381  query = (char *) palloc(100 + 16 * natts);
382 
383  /*
384  * Construct query: INSERT INTO _relation_ VALUES ($1, ...)
385  */
386  sprintf(query, "INSERT INTO %s VALUES (", relname);
387  for (i = 1; i <= natts; i++)
388  {
389  sprintf(query + strlen(query), "$%d%s",
390  i, (i < natts) ? ", " : ")");
391  ctypes[i - 1] = SPI_gettypeid(tupdesc, i);
392  }
393 
394  /* Prepare plan for query */
395  pplan = SPI_prepare(query, natts, ctypes);
396  if (pplan == NULL)
397  elog(ERROR, "ttdummy (%s): SPI_prepare returned %s", relname, SPI_result_code_string(SPI_result));
398 
399  if (SPI_keepplan(pplan))
400  elog(ERROR, "ttdummy (%s): SPI_keepplan failed", relname);
401 
402  splan = pplan;
403  }
404 
405  ret = SPI_execp(splan, cvals, cnulls, 0);
406 
407  if (ret < 0)
408  elog(ERROR, "ttdummy (%s): SPI_execp returned %d", relname, ret);
409 
410  /* Tuple to return to upper Executor ... */
411  if (newtuple) /* UPDATE */
412  rettuple = SPI_modifytuple(rel, trigtuple, 1, &(attnum[1]), &newoff, NULL);
413  else /* DELETE */
414  rettuple = trigtuple;
415 
416  SPI_finish(); /* don't forget say Bye to SPI mgr */
417 
418  pfree(relname);
419 
420  return PointerGetDatum(rettuple);
421 }
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:570
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:616
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:346
static bool ttoff
Definition: regress.c:232
int SPI_result
Definition: spi.c:47
#define sprintf
Definition: port.h:194
void pfree(void *pointer)
Definition: mcxt.c:1031
#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:231
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:229
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:924
int errmsg(const char *fmt,...)
Definition: elog.c:784
#define elog(elevel,...)
Definition: elog.h:226
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 624 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().

625 {
626  int pid = PG_GETARG_INT32(0);
627 
628  if (!superuser())
629  elog(ERROR, "must be superuser to check PID liveness");
630 
631  while (kill(pid, 0) == 0)
632  {
634  pg_usleep(50000);
635  }
636 
637  if (errno != ESRCH)
638  elog(ERROR, "could not check PID %d liveness: %m", pid);
639 
640  PG_RETURN_VOID();
641 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:264
bool superuser(void)
Definition: superuser.c:47
#define kill(pid, sig)
Definition: win32_port.h:435
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:226
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:99

◆ widget_in()

Datum widget_in ( PG_FUNCTION_ARGS  )

Definition at line 138 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.

139 {
140  char *str = PG_GETARG_CSTRING(0);
141  char *p,
142  *coord[NARGS];
143  int i;
144  WIDGET *result;
145 
146  for (i = 0, p = str; *p && i < NARGS && *p != RDELIM; p++)
147  {
148  if (*p == DELIM || (*p == LDELIM && i == 0))
149  coord[i++] = p + 1;
150  }
151 
152  if (i < NARGS)
153  ereport(ERROR,
154  (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
155  errmsg("invalid input syntax for type %s: \"%s\"",
156  "widget", str)));
157 
158  result = (WIDGET *) palloc(sizeof(WIDGET));
159  result->center.x = atof(coord[0]);
160  result->center.y = atof(coord[1]);
161  result->radius = atof(coord[2]);
162 
163  PG_RETURN_POINTER(result);
164 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:351
double radius
Definition: regress.c:129
#define RDELIM
Definition: regress.c:45
float8 x
Definition: geo_decls.h:57
int errcode(int sqlerrcode)
Definition: elog.c:570
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:141
#define DELIM
Definition: regress.c:46
float8 y
Definition: geo_decls.h:57
#define NARGS
Definition: regress.c:135
void * palloc(Size size)
Definition: mcxt.c:924
int errmsg(const char *fmt,...)
Definition: elog.c:784
Point center
Definition: regress.c:128
int i
#define PG_GETARG_CSTRING(n)
Definition: fmgr.h:272
#define LDELIM
Definition: regress.c:44

◆ widget_out()

Datum widget_out ( PG_FUNCTION_ARGS  )

Definition at line 167 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.

168 {
169  WIDGET *widget = (WIDGET *) PG_GETARG_POINTER(0);
170  char *str = psprintf("(%g,%g,%g)",
171  widget->center.x, widget->center.y, widget->radius);
172 
173  PG_RETURN_CSTRING(str);
174 }
double radius
Definition: regress.c:129
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:128

Variable Documentation

◆ PG_MODULE_MAGIC

PG_MODULE_MAGIC

Definition at line 50 of file regress.c.

◆ splan

SPIPlanPtr splan = NULL
static

◆ ttoff

bool ttoff = false
static

Definition at line 232 of file regress.c.

Referenced by set_ttdummy(), and ttdummy().