PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
regress.c File Reference
#include "postgres.h"
#include <float.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_type.h"
#include "commands/sequence.h"
#include "commands/trigger.h"
#include "executor/executor.h"
#include "executor/spi.h"
#include "miscadmin.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 P_MAXDIG   12
 
#define LDELIM   '('
 
#define RDELIM   ')'
 
#define DELIM   ','
 
#define NARGS   3
 
#define TTDUMMY_INFINITY   999999
 

Functions

PATHpoly2path (POLYGON *poly)
 
void regress_lseg_construct (LSEG *lseg, Point *pt1, Point *pt2)
 
 PG_FUNCTION_INFO_V1 (regress_dist_ptpath)
 
Datum regress_dist_ptpath (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (regress_path_dist)
 
Datum regress_path_dist (PG_FUNCTION_ARGS)
 
 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 (boxarea)
 
Datum boxarea (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (reverse_name)
 
Datum reverse_name (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (funny_dup17)
 
Datum funny_dup17 (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_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)
 

Variables

 PG_MODULE_MAGIC
 
static TransactionId fd17b_xid = InvalidTransactionId
 
static TransactionId fd17a_xid = InvalidTransactionId
 
static int fd17b_level = 0
 
static int fd17a_level = 0
 
static bool fd17b_recursion = true
 
static bool fd17a_recursion = true
 
static SPIPlanPtr splan = NULL
 
static bool ttoff = false
 

Macro Definition Documentation

#define DELIM   ','

Definition at line 44 of file regress.c.

Referenced by widget_in().

#define LDELIM   '('

Definition at line 42 of file regress.c.

Referenced by widget_in().

#define NARGS   3

Definition at line 242 of file regress.c.

Referenced by widget_in().

#define P_MAXDIG   12

Definition at line 41 of file regress.c.

Referenced by poly2path().

#define RDELIM   ')'

Definition at line 43 of file regress.c.

Referenced by poly2path(), and widget_in().

#define TTDUMMY_INFINITY   999999

Definition at line 447 of file regress.c.

Referenced by ttdummy().

Function Documentation

Datum boxarea ( PG_FUNCTION_ARGS  )

Definition at line 297 of file regress.c.

References Abs, BOX::high, BOX::low, PG_GETARG_BOX_P, PG_RETURN_FLOAT8, Point::x, and Point::y.

298 {
299  BOX *box = PG_GETARG_BOX_P(0);
300  double width,
301  height;
302 
303  width = Abs(box->high.x - box->low.x);
304  height = Abs(box->high.y - box->low.y);
305  PG_RETURN_FLOAT8(width * height);
306 }
Definition: geo_decls.h:102
#define PG_GETARG_BOX_P(n)
Definition: geo_decls.h:161
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:326
double y
Definition: geo_decls.h:60
#define Abs(x)
Definition: c.h:801
double x
Definition: geo_decls.h:60
Point low
Definition: geo_decls.h:104
Point high
Definition: geo_decls.h:104
Datum funny_dup17 ( PG_FUNCTION_ARGS  )

Definition at line 340 of file regress.c.

References CALLED_AS_TRIGGER, CStringGetDatum, DatumGetInt32, DEBUG4, DirectFunctionCall1, elog, ERROR, fd17a_level, fd17a_recursion, fd17a_xid, fd17b_level, fd17b_recursion, fd17b_xid, GetCurrentTransactionId(), int4in(), InvalidTransactionId, NAMEDATALEN, palloc(), PointerGetDatum, RelationData::rd_att, SPI_connect(), SPI_exec(), SPI_finish(), SPI_fname(), SPI_getrelname(), SPI_gettype(), SPI_getvalue(), SPI_processed, SPI_tuptable, TriggerData::tg_event, TriggerData::tg_relation, TriggerData::tg_trigtuple, TransactionIdIsCurrentTransactionId(), TRIGGER_FIRED_BEFORE, SPITupleTable::tupdesc, UINT64_FORMAT, and SPITupleTable::vals.

341 {
342  TriggerData *trigdata = (TriggerData *) fcinfo->context;
343  TransactionId *xid;
344  int *level;
345  bool *recursion;
346  Relation rel;
347  TupleDesc tupdesc;
348  HeapTuple tuple;
349  char *query,
350  *fieldval,
351  *fieldtype;
352  char *when;
353  uint64 inserted;
354  int selected = 0;
355  int ret;
356 
357  if (!CALLED_AS_TRIGGER(fcinfo))
358  elog(ERROR, "funny_dup17: not fired by trigger manager");
359 
360  tuple = trigdata->tg_trigtuple;
361  rel = trigdata->tg_relation;
362  tupdesc = rel->rd_att;
363  if (TRIGGER_FIRED_BEFORE(trigdata->tg_event))
364  {
365  xid = &fd17b_xid;
366  level = &fd17b_level;
367  recursion = &fd17b_recursion;
368  when = "BEFORE";
369  }
370  else
371  {
372  xid = &fd17a_xid;
373  level = &fd17a_level;
374  recursion = &fd17a_recursion;
375  when = "AFTER ";
376  }
377 
379  {
380  *xid = GetCurrentTransactionId();
381  *level = 0;
382  *recursion = true;
383  }
384 
385  if (*level == 17)
386  {
387  *recursion = false;
388  return PointerGetDatum(tuple);
389  }
390 
391  if (!(*recursion))
392  return PointerGetDatum(tuple);
393 
394  (*level)++;
395 
396  SPI_connect();
397 
398  fieldval = SPI_getvalue(tuple, tupdesc, 1);
399  fieldtype = SPI_gettype(tupdesc, 1);
400 
401  query = (char *) palloc(100 + NAMEDATALEN * 3 +
402  strlen(fieldval) + strlen(fieldtype));
403 
404  sprintf(query, "insert into %s select * from %s where %s = '%s'::%s",
405  SPI_getrelname(rel), SPI_getrelname(rel),
406  SPI_fname(tupdesc, 1),
407  fieldval, fieldtype);
408 
409  if ((ret = SPI_exec(query, 0)) < 0)
410  elog(ERROR, "funny_dup17 (fired %s) on level %3d: SPI_exec (insert ...) returned %d",
411  when, *level, ret);
412 
413  inserted = SPI_processed;
414 
415  sprintf(query, "select count (*) from %s where %s = '%s'::%s",
416  SPI_getrelname(rel),
417  SPI_fname(tupdesc, 1),
418  fieldval, fieldtype);
419 
420  if ((ret = SPI_exec(query, 0)) < 0)
421  elog(ERROR, "funny_dup17 (fired %s) on level %3d: SPI_exec (select ...) returned %d",
422  when, *level, ret);
423 
424  if (SPI_processed > 0)
425  {
428  SPI_tuptable->vals[0],
430  1
431  ))));
432  }
433 
434  elog(DEBUG4, "funny_dup17 (fired %s) on level %3d: " UINT64_FORMAT "/%d tuples inserted/selected",
435  when, *level, inserted, selected);
436 
437  SPI_finish();
438 
439  (*level)--;
440 
441  if (*level == 0)
442  *xid = InvalidTransactionId;
443 
444  return PointerGetDatum(tuple);
445 }
static TransactionId fd17a_xid
Definition: regress.c:331
uint32 TransactionId
Definition: c.h:391
#define DatumGetInt32(X)
Definition: postgres.h:478
bool TransactionIdIsCurrentTransactionId(TransactionId xid)
Definition: xact.c:774
int SPI_connect(void)
Definition: spi.c:84
#define PointerGetDatum(X)
Definition: postgres.h:562
Datum int4in(PG_FUNCTION_ARGS)
Definition: int.c:266
int SPI_finish(void)
Definition: spi.c:148
SPITupleTable * SPI_tuptable
Definition: spi.c:41
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:585
HeapTuple * vals
Definition: spi.h:28
#define DEBUG4
Definition: elog.h:22
uint64 SPI_processed
Definition: spi.c:39
HeapTuple tg_trigtuple
Definition: trigger.h:35
char * SPI_gettype(TupleDesc tupdesc, int fnumber)
Definition: spi.c:854
char * SPI_fname(TupleDesc tupdesc, int fnumber)
Definition: spi.c:784
char * SPI_getvalue(HeapTuple tuple, TupleDesc tupdesc, int fnumber)
Definition: spi.c:806
#define NAMEDATALEN
#define ERROR
Definition: elog.h:43
int SPI_exec(const char *src, long tcount)
Definition: spi.c:332
static int fd17b_level
Definition: regress.c:332
static bool fd17a_recursion
Definition: regress.c:335
static int fd17a_level
Definition: regress.c:333
TransactionId GetCurrentTransactionId(void)
Definition: xact.c:418
#define CStringGetDatum(X)
Definition: postgres.h:584
#define InvalidTransactionId
Definition: transam.h:31
Definition: type.h:82
static TransactionId fd17b_xid
Definition: regress.c:330
static bool fd17b_recursion
Definition: regress.c:334
TupleDesc tupdesc
Definition: spi.h:27
TupleDesc rd_att
Definition: rel.h:115
char * SPI_getrelname(Relation rel)
Definition: spi.c:912
#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
void * palloc(Size size)
Definition: mcxt.c:848
#define elog
Definition: elog.h:219
#define UINT64_FORMAT
Definition: c.h:301
Relation tg_relation
Definition: trigger.h:34
Datum int44in ( PG_FUNCTION_ARGS  )

Definition at line 682 of file regress.c.

References i, palloc(), PG_GETARG_CSTRING, PG_RETURN_POINTER, and result.

683 {
684  char *input_string = PG_GETARG_CSTRING(0);
685  int32 *result = (int32 *) palloc(4 * sizeof(int32));
686  int i;
687 
688  i = sscanf(input_string,
689  "%d, %d, %d, %d",
690  &result[0],
691  &result[1],
692  &result[2],
693  &result[3]);
694  while (i < 4)
695  result[i++] = 0;
696 
697  PG_RETURN_POINTER(result);
698 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:321
return result
Definition: formatting.c:1633
signed int int32
Definition: c.h:246
void * palloc(Size size)
Definition: mcxt.c:848
int i
#define PG_GETARG_CSTRING(n)
Definition: fmgr.h:242
Datum int44out ( PG_FUNCTION_ARGS  )

Definition at line 706 of file regress.c.

References i, palloc(), PG_GETARG_POINTER, pg_ltoa(), PG_RETURN_CSTRING, and result.

707 {
708  int32 *an_array = (int32 *) PG_GETARG_POINTER(0);
709  char *result = (char *) palloc(16 * 4); /* Allow 14 digits + sign */
710  int i;
711  char *walk;
712 
713  walk = result;
714  for (i = 0; i < 4; i++)
715  {
716  pg_ltoa(an_array[i], walk);
717  while (*++walk != '\0')
718  ;
719  *walk++ = ' ';
720  }
721  *--walk = '\0';
722  PG_RETURN_CSTRING(result);
723 }
return result
Definition: formatting.c:1633
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:241
signed int int32
Definition: c.h:246
#define PG_RETURN_CSTRING(x)
Definition: fmgr.h:322
void * palloc(Size size)
Definition: mcxt.c:848
int i
void pg_ltoa(int32 value, char *a)
Definition: numutils.c:132
Datum interpt_pp ( PG_FUNCTION_ARGS  )

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

165 {
166  PATH *p1 = PG_GETARG_PATH_P(0);
167  PATH *p2 = PG_GETARG_PATH_P(1);
168  int i,
169  j;
170  LSEG seg1,
171  seg2;
172  bool found; /* We've found the intersection */
173 
174  found = false; /* Haven't found it yet */
175 
176  for (i = 0; i < p1->npts - 1 && !found; i++)
177  {
178  regress_lseg_construct(&seg1, &p1->p[i], &p1->p[i + 1]);
179  for (j = 0; j < p2->npts - 1 && !found; j++)
180  {
181  regress_lseg_construct(&seg2, &p2->p[j], &p2->p[j + 1]);
183  LsegPGetDatum(&seg1),
184  LsegPGetDatum(&seg2))))
185  found = true;
186  }
187  }
188 
189  if (!found)
190  PG_RETURN_NULL();
191 
192  /*
193  * Note: DirectFunctionCall2 will kick out an error if lseg_interpt()
194  * returns NULL, but that should be impossible since we know the two
195  * segments intersect.
196  */
198  LsegPGetDatum(&seg1),
199  LsegPGetDatum(&seg2)));
200 }
void regress_lseg_construct(LSEG *lseg, Point *pt1, Point *pt2)
Definition: regress.c:205
Definition: geo_decls.h:68
Datum lseg_interpt(PG_FUNCTION_ARGS)
Definition: geo_ops.c:2310
#define LsegPGetDatum(X)
Definition: geo_decls.h:143
Point p[FLEXIBLE_ARRAY_MEMBER]
Definition: geo_decls.h:83
int32 npts
Definition: geo_decls.h:80
#define DatumGetBool(X)
Definition: postgres.h:399
#define PG_GETARG_PATH_P(n)
Definition: geo_decls.h:150
Datum lseg_intersect(PG_FUNCTION_ARGS)
Definition: geo_ops.c:2051
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:313
Definition: geo_decls.h:77
int i
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:587
#define PG_RETURN_NULL()
Definition: fmgr.h:305
Datum make_tuple_indirect ( PG_FUNCTION_ARGS  )

Definition at line 727 of file regress.c.

References DatumGetPointer, heap_deform_tuple(), heap_form_tuple(), heap_tuple_fetch_attr(), HeapTupleHeaderGetDatumLength, HeapTupleHeaderGetTypeId, HeapTupleHeaderGetTypMod, i, INDIRECT_POINTER_SIZE, InvalidOid, ItemPointerSetInvalid, lookup_rowtype_tupdesc(), MemoryContextSwitchTo(), tupleDesc::natts, palloc(), palloc0(), pfree(), PG_GETARG_HEAPTUPLEHEADER, PG_RETURN_POINTER, varatt_indirect::pointer, PointerGetDatum, 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.

728 {
730  HeapTupleData tuple;
731  int ncolumns;
732  Datum *values;
733  bool *nulls;
734 
735  Oid tupType;
736  int32 tupTypmod;
737  TupleDesc tupdesc;
738 
739  HeapTuple newtup;
740 
741  int i;
742 
743  MemoryContext old_context;
744 
745  /* Extract type info from the tuple itself */
746  tupType = HeapTupleHeaderGetTypeId(rec);
747  tupTypmod = HeapTupleHeaderGetTypMod(rec);
748  tupdesc = lookup_rowtype_tupdesc(tupType, tupTypmod);
749  ncolumns = tupdesc->natts;
750 
751  /* Build a temporary HeapTuple control structure */
753  ItemPointerSetInvalid(&(tuple.t_self));
754  tuple.t_tableOid = InvalidOid;
755  tuple.t_data = rec;
756 
757  values = (Datum *) palloc(ncolumns * sizeof(Datum));
758  nulls = (bool *) palloc(ncolumns * sizeof(bool));
759 
760  heap_deform_tuple(&tuple, tupdesc, values, nulls);
761 
763 
764  for (i = 0; i < ncolumns; i++)
765  {
766  struct varlena *attr;
767  struct varlena *new_attr;
768  struct varatt_indirect redirect_pointer;
769 
770  /* only work on existing, not-null varlenas */
771  if (TupleDescAttr(tupdesc, i)->attisdropped ||
772  nulls[i] ||
773  TupleDescAttr(tupdesc, i)->attlen != -1)
774  continue;
775 
776  attr = (struct varlena *) DatumGetPointer(values[i]);
777 
778  /* don't recursively indirect */
779  if (VARATT_IS_EXTERNAL_INDIRECT(attr))
780  continue;
781 
782  /* copy datum, so it still lives later */
783  if (VARATT_IS_EXTERNAL_ONDISK(attr))
784  attr = heap_tuple_fetch_attr(attr);
785  else
786  {
787  struct varlena *oldattr = attr;
788 
789  attr = palloc0(VARSIZE_ANY(oldattr));
790  memcpy(attr, oldattr, VARSIZE_ANY(oldattr));
791  }
792 
793  /* build indirection Datum */
794  new_attr = (struct varlena *) palloc0(INDIRECT_POINTER_SIZE);
795  redirect_pointer.pointer = attr;
797  memcpy(VARDATA_EXTERNAL(new_attr), &redirect_pointer,
798  sizeof(redirect_pointer));
799 
800  values[i] = PointerGetDatum(new_attr);
801  }
802 
803  newtup = heap_form_tuple(tupdesc, values, nulls);
804  pfree(values);
805  pfree(nulls);
806  ReleaseTupleDesc(tupdesc);
807 
808  MemoryContextSwitchTo(old_context);
809 
810  /*
811  * We intentionally don't use PG_RETURN_HEAPTUPLEHEADER here, because that
812  * would cause the indirect toast pointers to be flattened out of the
813  * tuple immediately, rendering subsequent testing irrelevant. So just
814  * return the HeapTupleHeader pointer as-is. This violates the general
815  * rule that composite Datums shouldn't contain toast pointers, but so
816  * long as the regression test scripts don't insert the result of this
817  * function into a container type (record, array, etc) it should be OK.
818  */
819  PG_RETURN_POINTER(newtup->t_data);
820 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:321
#define VARATT_IS_EXTERNAL_ONDISK(PTR)
Definition: postgres.h:315
MemoryContext TopTransactionContext
Definition: mcxt.c:48
TupleDesc lookup_rowtype_tupdesc(Oid type_id, int32 typmod)
Definition: typcache.c:1486
#define PointerGetDatum(X)
Definition: postgres.h:562
struct varlena * heap_tuple_fetch_attr(struct varlena *attr)
Definition: tuptoaster.c:101
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:84
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: heaptuple.c:695
#define PG_GETARG_HEAPTUPLEHEADER(n)
Definition: fmgr.h:276
unsigned int Oid
Definition: postgres_ext.h:31
#define VARDATA_EXTERNAL(PTR)
Definition: postgres.h:311
int natts
Definition: tupdesc.h:73
signed int int32
Definition: c.h:246
HeapTupleHeader t_data
Definition: htup.h:67
#define HeapTupleHeaderGetTypMod(tup)
Definition: htup_details.h:455
void pfree(void *pointer)
Definition: mcxt.c:949
#define VARATT_IS_EXTERNAL_INDIRECT(PTR)
Definition: postgres.h:317
ItemPointerData t_self
Definition: htup.h:65
uint32 t_len
Definition: htup.h:64
#define SET_VARTAG_EXTERNAL(PTR, tag)
Definition: postgres.h:332
Oid t_tableOid
Definition: htup.h:66
#define INDIRECT_POINTER_SIZE
Definition: tuptoaster.h:102
void * palloc0(Size size)
Definition: mcxt.c:877
uintptr_t Datum
Definition: postgres.h:372
#define HeapTupleHeaderGetTypeId(tup)
Definition: htup_details.h:445
#define VARSIZE_ANY(PTR)
Definition: postgres.h:334
#define InvalidOid
Definition: postgres_ext.h:36
#define DatumGetPointer(X)
Definition: postgres.h:555
void heap_deform_tuple(HeapTuple tuple, TupleDesc tupleDesc, Datum *values, bool *isnull)
Definition: heaptuple.c:936
static Datum values[MAXATTR]
Definition: bootstrap.c:164
#define ItemPointerSetInvalid(pointer)
Definition: itemptr.h:150
void * palloc(Size size)
Definition: mcxt.c:848
int i
Definition: c.h:433
#define ReleaseTupleDesc(tupdesc)
Definition: tupdesc.h:115
#define HeapTupleHeaderGetDatumLength(tup)
Definition: htup_details.h:439
Datum overpaid ( PG_FUNCTION_ARGS  )

Definition at line 216 of file regress.c.

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

217 {
219  bool isnull;
220  int32 salary;
221 
222  salary = DatumGetInt32(GetAttributeByName(tuple, "salary", &isnull));
223  if (isnull)
224  PG_RETURN_NULL();
225  PG_RETURN_BOOL(salary > 699);
226 }
#define DatumGetInt32(X)
Definition: postgres.h:478
Datum GetAttributeByName(HeapTupleHeader tuple, const char *attname, bool *isNull)
Definition: execUtils.c:890
#define PG_GETARG_HEAPTUPLEHEADER(n)
Definition: fmgr.h:276
signed int int32
Definition: c.h:246
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
#define PG_RETURN_NULL()
Definition: fmgr.h:305
PG_FUNCTION_INFO_V1 ( regress_dist_ptpath  )
PG_FUNCTION_INFO_V1 ( regress_path_dist  )
PG_FUNCTION_INFO_V1 ( interpt_pp  )
PG_FUNCTION_INFO_V1 ( overpaid  )
PG_FUNCTION_INFO_V1 ( widget_in  )
PG_FUNCTION_INFO_V1 ( widget_out  )
PG_FUNCTION_INFO_V1 ( pt_in_widget  )
PG_FUNCTION_INFO_V1 ( boxarea  )
PG_FUNCTION_INFO_V1 ( reverse_name  )
PG_FUNCTION_INFO_V1 ( funny_dup17  )
PG_FUNCTION_INFO_V1 ( ttdummy  )
PG_FUNCTION_INFO_V1 ( set_ttdummy  )
PG_FUNCTION_INFO_V1 ( int44in  )
PG_FUNCTION_INFO_V1 ( int44out  )
PG_FUNCTION_INFO_V1 ( make_tuple_indirect  )
PG_FUNCTION_INFO_V1 ( regress_putenv  )
PG_FUNCTION_INFO_V1 ( wait_pid  )
PG_FUNCTION_INFO_V1 ( test_atomic_ops  )
PG_FUNCTION_INFO_V1 ( test_fdw_handler  )
PATH * poly2path ( POLYGON poly)

Definition at line 139 of file regress.c.

References buf, CStringGetDatum, DatumGetPathP, DirectFunctionCall1, i, POLYGON::npts, output(), POLYGON::p, P_MAXDIG, palloc(), path_in(), RDELIM, snprintf(), Point::x, and Point::y.

140 {
141  int i;
142  char *output = (char *) palloc(2 * (P_MAXDIG + 1) * poly->npts + 64);
143  char buf[2 * (P_MAXDIG) + 20];
144 
145  sprintf(output, "(1, %*d", P_MAXDIG, poly->npts);
146 
147  for (i = 0; i < poly->npts; i++)
148  {
149  snprintf(buf, sizeof(buf), ",%*g,%*g",
150  P_MAXDIG, poly->p[i].x, P_MAXDIG, poly->p[i].y);
151  strcat(output, buf);
152  }
153 
154  snprintf(buf, sizeof(buf), "%c", RDELIM);
155  strcat(output, buf);
157  CStringGetDatum(output)));
158 }
#define DatumGetPathP(X)
Definition: geo_decls.h:147
#define RDELIM
Definition: regress.c:43
static void output(uint64 loop_count)
double y
Definition: geo_decls.h:60
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:585
int32 npts
Definition: geo_decls.h:116
double x
Definition: geo_decls.h:60
#define P_MAXDIG
Definition: regress.c:41
static char * buf
Definition: pg_test_fsync.c:67
#define CStringGetDatum(X)
Definition: postgres.h:584
Point p[FLEXIBLE_ARRAY_MEMBER]
Definition: geo_decls.h:118
Datum path_in(PG_FUNCTION_ARGS)
Definition: geo_ops.c:1307
void * palloc(Size size)
Definition: mcxt.c:848
int i
Datum pt_in_widget ( PG_FUNCTION_ARGS  )

Definition at line 286 of file regress.c.

References WIDGET::center, PG_GETARG_POINT_P, PG_GETARG_POINTER, PG_RETURN_BOOL, point_dt(), and WIDGET::radius.

287 {
288  Point *point = PG_GETARG_POINT_P(0);
289  WIDGET *widget = (WIDGET *) PG_GETARG_POINTER(1);
290 
291  PG_RETURN_BOOL(point_dt(point, &widget->center) < widget->radius);
292 }
double radius
Definition: regress.c:236
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:241
#define PG_GETARG_POINT_P(n)
Definition: geo_decls.h:139
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
double point_dt(Point *pt1, Point *pt2)
Definition: geo_ops.c:1906
Point center
Definition: regress.c:235
Datum regress_dist_ptpath ( PG_FUNCTION_ARGS  )

Definition at line 58 of file regress.c.

References Assert, DatumGetFloat8, DirectFunctionCall2, dist_ps(), i, LsegPGetDatum, PATH::npts, PATH::p, PG_GETARG_PATH_P, PG_GETARG_POINT_P, PG_RETURN_FLOAT8, PG_RETURN_NULL, point_dt(), PointPGetDatum, regress_lseg_construct(), and result.

59 {
60  Point *pt = PG_GETARG_POINT_P(0);
61  PATH *path = PG_GETARG_PATH_P(1);
62  float8 result = 0.0; /* keep compiler quiet */
63  float8 tmp;
64  int i;
65  LSEG lseg;
66 
67  switch (path->npts)
68  {
69  case 0:
71  case 1:
72  result = point_dt(pt, &path->p[0]);
73  break;
74  default:
75 
76  /*
77  * the distance from a point to a path is the smallest distance
78  * from the point to any of its constituent segments.
79  */
80  Assert(path->npts > 1);
81  for (i = 0; i < path->npts - 1; ++i)
82  {
83  regress_lseg_construct(&lseg, &path->p[i], &path->p[i + 1]);
85  PointPGetDatum(pt),
86  LsegPGetDatum(&lseg)));
87  if (i == 0 || tmp < result)
88  result = tmp;
89  }
90  break;
91  }
92  PG_RETURN_FLOAT8(result);
93 }
void regress_lseg_construct(LSEG *lseg, Point *pt1, Point *pt2)
Definition: regress.c:205
Definition: geo_decls.h:68
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:326
return result
Definition: formatting.c:1633
#define LsegPGetDatum(X)
Definition: geo_decls.h:143
double float8
Definition: c.h:375
#define PG_GETARG_POINT_P(n)
Definition: geo_decls.h:139
Point p[FLEXIBLE_ARRAY_MEMBER]
Definition: geo_decls.h:83
int32 npts
Definition: geo_decls.h:80
Datum dist_ps(PG_FUNCTION_ARGS)
Definition: geo_ops.c:2358
#define PG_GETARG_PATH_P(n)
Definition: geo_decls.h:150
#define DatumGetFloat8(X)
Definition: postgres.h:734
#define Assert(condition)
Definition: c.h:664
double point_dt(Point *pt1, Point *pt2)
Definition: geo_ops.c:1906
Definition: geo_decls.h:77
int i
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:587
#define PG_RETURN_NULL()
Definition: fmgr.h:305
#define PointPGetDatum(X)
Definition: geo_decls.h:138
void regress_lseg_construct ( LSEG lseg,
Point pt1,
Point pt2 
)

Definition at line 205 of file regress.c.

References LSEG::p, Point::x, and Point::y.

Referenced by interpt_pp(), regress_dist_ptpath(), and regress_path_dist().

206 {
207  lseg->p[0].x = pt1->x;
208  lseg->p[0].y = pt1->y;
209  lseg->p[1].x = pt2->x;
210  lseg->p[1].y = pt2->y;
211 }
double y
Definition: geo_decls.h:60
double x
Definition: geo_decls.h:60
Point p[2]
Definition: geo_decls.h:70
Datum regress_path_dist ( PG_FUNCTION_ARGS  )

Definition at line 102 of file regress.c.

References DatumGetFloat8, DirectFunctionCall2, i, lseg_distance(), LsegPGetDatum, PATH::npts, PATH::p, PG_GETARG_PATH_P, PG_RETURN_FLOAT8, PG_RETURN_NULL, and regress_lseg_construct().

103 {
104  PATH *p1 = PG_GETARG_PATH_P(0);
105  PATH *p2 = PG_GETARG_PATH_P(1);
106  bool have_min = false;
107  float8 min = 0.0; /* initialize to keep compiler quiet */
108  float8 tmp;
109  int i,
110  j;
111  LSEG seg1,
112  seg2;
113 
114  for (i = 0; i < p1->npts - 1; i++)
115  {
116  for (j = 0; j < p2->npts - 1; j++)
117  {
118  regress_lseg_construct(&seg1, &p1->p[i], &p1->p[i + 1]);
119  regress_lseg_construct(&seg2, &p2->p[j], &p2->p[j + 1]);
120 
122  LsegPGetDatum(&seg1),
123  LsegPGetDatum(&seg2)));
124  if (!have_min || tmp < min)
125  {
126  min = tmp;
127  have_min = true;
128  }
129  }
130  }
131 
132  if (!have_min)
133  PG_RETURN_NULL();
134 
135  PG_RETURN_FLOAT8(min);
136 }
void regress_lseg_construct(LSEG *lseg, Point *pt1, Point *pt2)
Definition: regress.c:205
Definition: geo_decls.h:68
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:326
#define LsegPGetDatum(X)
Definition: geo_decls.h:143
double float8
Definition: c.h:375
Point p[FLEXIBLE_ARRAY_MEMBER]
Definition: geo_decls.h:83
int32 npts
Definition: geo_decls.h:80
Datum lseg_distance(PG_FUNCTION_ARGS)
Definition: geo_ops.c:2209
#define PG_GETARG_PATH_P(n)
Definition: geo_decls.h:150
#define DatumGetFloat8(X)
Definition: postgres.h:734
Definition: geo_decls.h:77
int i
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:587
#define PG_RETURN_NULL()
Definition: fmgr.h:305
Datum regress_putenv ( PG_FUNCTION_ARGS  )

Definition at line 825 of file regress.c.

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

826 {
827  MemoryContext oldcontext;
828  char *envbuf;
829 
830  if (!superuser())
831  elog(ERROR, "must be superuser to change environment variables");
832 
834  envbuf = text_to_cstring((text *) PG_GETARG_POINTER(0));
835  MemoryContextSwitchTo(oldcontext);
836 
837  if (putenv(envbuf) != 0)
838  elog(ERROR, "could not set environment variable: %m");
839 
840  PG_RETURN_VOID();
841 }
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
bool superuser(void)
Definition: superuser.c:47
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:241
#define putenv(x)
Definition: win32.h:411
#define ERROR
Definition: elog.h:43
MemoryContext TopMemoryContext
Definition: mcxt.c:43
#define PG_RETURN_VOID()
Definition: fmgr.h:309
char * text_to_cstring(const text *t)
Definition: varlena.c:182
Definition: c.h:433
#define elog
Definition: elog.h:219
Datum reverse_name ( PG_FUNCTION_ARGS  )

Definition at line 311 of file regress.c.

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

312 {
313  char *string = PG_GETARG_CSTRING(0);
314  int i;
315  int len;
316  char *new_string;
317 
318  new_string = palloc0(NAMEDATALEN);
319  for (i = 0; i < NAMEDATALEN && string[i]; ++i)
320  ;
321  if (i == NAMEDATALEN || !string[i])
322  --i;
323  len = i;
324  for (; i >= 0; --i)
325  new_string[len - i] = string[i];
326  PG_RETURN_CSTRING(new_string);
327 }
#define NAMEDATALEN
void * palloc0(Size size)
Definition: mcxt.c:877
#define PG_RETURN_CSTRING(x)
Definition: fmgr.h:322
int i
#define PG_GETARG_CSTRING(n)
Definition: fmgr.h:242
Datum set_ttdummy ( PG_FUNCTION_ARGS  )

Definition at line 644 of file regress.c.

References PG_GETARG_INT32, PG_RETURN_INT32, and ttoff.

645 {
646  int32 on = PG_GETARG_INT32(0);
647 
648  if (ttoff) /* OFF currently */
649  {
650  if (on == 0)
651  PG_RETURN_INT32(0);
652 
653  /* turn ON */
654  ttoff = false;
655  PG_RETURN_INT32(0);
656  }
657 
658  /* ON currently */
659  if (on != 0)
660  PG_RETURN_INT32(1);
661 
662  /* turn OFF */
663  ttoff = true;
664 
665  PG_RETURN_INT32(1);
666 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:234
#define PG_RETURN_INT32(x)
Definition: fmgr.h:314
signed int int32
Definition: c.h:246
static bool ttoff
Definition: regress.c:450
Datum test_atomic_ops ( PG_FUNCTION_ARGS  )

Definition at line 1077 of file regress.c.

References PG_RETURN_BOOL, test_atomic_uint32(), and test_atomic_uint64().

1078 {
1079  /* ---
1080  * Can't run the test under the semaphore emulation, it doesn't handle
1081  * checking two edge cases well:
1082  * - pg_atomic_unlocked_test_flag() always returns true
1083  * - locking a already locked flag blocks
1084  * it seems better to not test the semaphore fallback here, than weaken
1085  * the checks for the other cases. The semaphore code will be the same
1086  * everywhere, whereas the efficient implementations wont.
1087  * ---
1088  */
1089 #ifndef PG_HAVE_ATOMIC_FLAG_SIMULATION
1090  test_atomic_flag();
1091 #endif
1092 
1094 
1096 
1097  PG_RETURN_BOOL(true);
1098 }
static void test_atomic_uint32(void)
Definition: regress.c:899
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
static void test_atomic_uint64(void)
Definition: regress.c:999
static void test_atomic_uint32 ( void  )
static

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

900 {
901  pg_atomic_uint32 var;
902  uint32 expected;
903  int i;
904 
905  pg_atomic_init_u32(&var, 0);
906 
907  if (pg_atomic_read_u32(&var) != 0)
908  elog(ERROR, "atomic_read_u32() #1 wrong");
909 
910  pg_atomic_write_u32(&var, 3);
911 
912  if (pg_atomic_read_u32(&var) != 3)
913  elog(ERROR, "atomic_read_u32() #2 wrong");
914 
915  if (pg_atomic_fetch_add_u32(&var, 1) != 3)
916  elog(ERROR, "atomic_fetch_add_u32() #1 wrong");
917 
918  if (pg_atomic_fetch_sub_u32(&var, 1) != 4)
919  elog(ERROR, "atomic_fetch_sub_u32() #1 wrong");
920 
921  if (pg_atomic_sub_fetch_u32(&var, 3) != 0)
922  elog(ERROR, "atomic_sub_fetch_u32() #1 wrong");
923 
924  if (pg_atomic_add_fetch_u32(&var, 10) != 10)
925  elog(ERROR, "atomic_add_fetch_u32() #1 wrong");
926 
927  if (pg_atomic_exchange_u32(&var, 5) != 10)
928  elog(ERROR, "pg_atomic_exchange_u32() #1 wrong");
929 
930  if (pg_atomic_exchange_u32(&var, 0) != 5)
931  elog(ERROR, "pg_atomic_exchange_u32() #0 wrong");
932 
933  /* test around numerical limits */
934  if (pg_atomic_fetch_add_u32(&var, INT_MAX) != 0)
935  elog(ERROR, "pg_atomic_fetch_add_u32() #2 wrong");
936 
937  if (pg_atomic_fetch_add_u32(&var, INT_MAX) != INT_MAX)
938  elog(ERROR, "pg_atomic_add_fetch_u32() #3 wrong");
939 
940  pg_atomic_fetch_add_u32(&var, 1); /* top up to UINT_MAX */
941 
942  if (pg_atomic_read_u32(&var) != UINT_MAX)
943  elog(ERROR, "atomic_read_u32() #2 wrong");
944 
945  if (pg_atomic_fetch_sub_u32(&var, INT_MAX) != UINT_MAX)
946  elog(ERROR, "pg_atomic_fetch_sub_u32() #2 wrong");
947 
948  if (pg_atomic_read_u32(&var) != (uint32) INT_MAX + 1)
949  elog(ERROR, "atomic_read_u32() #3 wrong: %u", pg_atomic_read_u32(&var));
950 
951  expected = pg_atomic_sub_fetch_u32(&var, INT_MAX);
952  if (expected != 1)
953  elog(ERROR, "pg_atomic_sub_fetch_u32() #3 wrong: %u", expected);
954 
955  pg_atomic_sub_fetch_u32(&var, 1);
956 
957  /* fail exchange because of old expected */
958  expected = 10;
959  if (pg_atomic_compare_exchange_u32(&var, &expected, 1))
960  elog(ERROR, "atomic_compare_exchange_u32() changed value spuriously");
961 
962  /* CAS is allowed to fail due to interrupts, try a couple of times */
963  for (i = 0; i < 1000; i++)
964  {
965  expected = 0;
966  if (!pg_atomic_compare_exchange_u32(&var, &expected, 1))
967  break;
968  }
969  if (i == 1000)
970  elog(ERROR, "atomic_compare_exchange_u32() never succeeded");
971  if (pg_atomic_read_u32(&var) != 1)
972  elog(ERROR, "atomic_compare_exchange_u32() didn't set value properly");
973 
974  pg_atomic_write_u32(&var, 0);
975 
976  /* try setting flagbits */
977  if (pg_atomic_fetch_or_u32(&var, 1) & 1)
978  elog(ERROR, "pg_atomic_fetch_or_u32() #1 wrong");
979 
980  if (!(pg_atomic_fetch_or_u32(&var, 2) & 1))
981  elog(ERROR, "pg_atomic_fetch_or_u32() #2 wrong");
982 
983  if (pg_atomic_read_u32(&var) != 3)
984  elog(ERROR, "invalid result after pg_atomic_fetch_or_u32()");
985 
986  /* try clearing flagbits */
987  if ((pg_atomic_fetch_and_u32(&var, ~2) & 3) != 3)
988  elog(ERROR, "pg_atomic_fetch_and_u32() #1 wrong");
989 
990  if (pg_atomic_fetch_and_u32(&var, ~1) != 1)
991  elog(ERROR, "pg_atomic_fetch_and_u32() #2 wrong: is %u",
992  pg_atomic_read_u32(&var));
993  /* no bits set anymore */
994  if (pg_atomic_fetch_and_u32(&var, ~0) != 0)
995  elog(ERROR, "pg_atomic_fetch_and_u32() #3 wrong");
996 }
static uint32 pg_atomic_fetch_sub_u32(volatile pg_atomic_uint32 *ptr, int32 sub_)
Definition: atomics.h:354
static bool pg_atomic_compare_exchange_u32(volatile pg_atomic_uint32 *ptr, uint32 *expected, uint32 newval)
Definition: atomics.h:322
static uint32 pg_atomic_sub_fetch_u32(volatile pg_atomic_uint32 *ptr, int32 sub_)
Definition: atomics.h:412
static uint32 pg_atomic_add_fetch_u32(volatile pg_atomic_uint32 *ptr, int32 add_)
Definition: atomics.h:397
static uint32 pg_atomic_exchange_u32(volatile pg_atomic_uint32 *ptr, uint32 newval)
Definition: atomics.h:303
#define ERROR
Definition: elog.h:43
static uint32 pg_atomic_fetch_and_u32(volatile pg_atomic_uint32 *ptr, uint32 and_)
Definition: atomics.h:369
unsigned int uint32
Definition: c.h:258
static uint32 pg_atomic_fetch_add_u32(volatile pg_atomic_uint32 *ptr, int32 add_)
Definition: atomics.h:339
int i
static uint32 pg_atomic_fetch_or_u32(volatile pg_atomic_uint32 *ptr, uint32 or_)
Definition: atomics.h:383
static void pg_atomic_init_u32(volatile pg_atomic_uint32 *ptr, uint32 val)
Definition: atomics.h:234
#define elog
Definition: elog.h:219
static void pg_atomic_write_u32(volatile pg_atomic_uint32 *ptr, uint32 val)
Definition: atomics.h:269
static uint32 pg_atomic_read_u32(volatile pg_atomic_uint32 *ptr)
Definition: atomics.h:252
static void test_atomic_uint64 ( void  )
static

Definition at line 999 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(), and UINT64_FORMAT.

Referenced by test_atomic_ops().

1000 {
1001  pg_atomic_uint64 var;
1002  uint64 expected;
1003  int i;
1004 
1005  pg_atomic_init_u64(&var, 0);
1006 
1007  if (pg_atomic_read_u64(&var) != 0)
1008  elog(ERROR, "atomic_read_u64() #1 wrong");
1009 
1010  pg_atomic_write_u64(&var, 3);
1011 
1012  if (pg_atomic_read_u64(&var) != 3)
1013  elog(ERROR, "atomic_read_u64() #2 wrong");
1014 
1015  if (pg_atomic_fetch_add_u64(&var, 1) != 3)
1016  elog(ERROR, "atomic_fetch_add_u64() #1 wrong");
1017 
1018  if (pg_atomic_fetch_sub_u64(&var, 1) != 4)
1019  elog(ERROR, "atomic_fetch_sub_u64() #1 wrong");
1020 
1021  if (pg_atomic_sub_fetch_u64(&var, 3) != 0)
1022  elog(ERROR, "atomic_sub_fetch_u64() #1 wrong");
1023 
1024  if (pg_atomic_add_fetch_u64(&var, 10) != 10)
1025  elog(ERROR, "atomic_add_fetch_u64() #1 wrong");
1026 
1027  if (pg_atomic_exchange_u64(&var, 5) != 10)
1028  elog(ERROR, "pg_atomic_exchange_u64() #1 wrong");
1029 
1030  if (pg_atomic_exchange_u64(&var, 0) != 5)
1031  elog(ERROR, "pg_atomic_exchange_u64() #0 wrong");
1032 
1033  /* fail exchange because of old expected */
1034  expected = 10;
1035  if (pg_atomic_compare_exchange_u64(&var, &expected, 1))
1036  elog(ERROR, "atomic_compare_exchange_u64() changed value spuriously");
1037 
1038  /* CAS is allowed to fail due to interrupts, try a couple of times */
1039  for (i = 0; i < 100; i++)
1040  {
1041  expected = 0;
1042  if (!pg_atomic_compare_exchange_u64(&var, &expected, 1))
1043  break;
1044  }
1045  if (i == 100)
1046  elog(ERROR, "atomic_compare_exchange_u64() never succeeded");
1047  if (pg_atomic_read_u64(&var) != 1)
1048  elog(ERROR, "atomic_compare_exchange_u64() didn't set value properly");
1049 
1050  pg_atomic_write_u64(&var, 0);
1051 
1052  /* try setting flagbits */
1053  if (pg_atomic_fetch_or_u64(&var, 1) & 1)
1054  elog(ERROR, "pg_atomic_fetch_or_u64() #1 wrong");
1055 
1056  if (!(pg_atomic_fetch_or_u64(&var, 2) & 1))
1057  elog(ERROR, "pg_atomic_fetch_or_u64() #2 wrong");
1058 
1059  if (pg_atomic_read_u64(&var) != 3)
1060  elog(ERROR, "invalid result after pg_atomic_fetch_or_u64()");
1061 
1062  /* try clearing flagbits */
1063  if ((pg_atomic_fetch_and_u64(&var, ~2) & 3) != 3)
1064  elog(ERROR, "pg_atomic_fetch_and_u64() #1 wrong");
1065 
1066  if (pg_atomic_fetch_and_u64(&var, ~1) != 1)
1067  elog(ERROR, "pg_atomic_fetch_and_u64() #2 wrong: is " UINT64_FORMAT,
1068  pg_atomic_read_u64(&var));
1069  /* no bits set anymore */
1070  if (pg_atomic_fetch_and_u64(&var, ~0) != 0)
1071  elog(ERROR, "pg_atomic_fetch_and_u64() #3 wrong");
1072 }
static uint64 pg_atomic_fetch_add_u64(volatile pg_atomic_uint64 *ptr, int64 add_)
Definition: atomics.h:478
static uint64 pg_atomic_sub_fetch_u64(volatile pg_atomic_uint64 *ptr, int64 sub_)
Definition: atomics.h:524
static uint64 pg_atomic_fetch_sub_u64(volatile pg_atomic_uint64 *ptr, int64 sub_)
Definition: atomics.h:487
static void pg_atomic_write_u64(volatile pg_atomic_uint64 *ptr, uint64 val)
Definition: atomics.h:449
static bool pg_atomic_compare_exchange_u64(volatile pg_atomic_uint64 *ptr, uint64 *expected, uint64 newval)
Definition: atomics.h:467
static uint64 pg_atomic_exchange_u64(volatile pg_atomic_uint64 *ptr, uint64 newval)
Definition: atomics.h:458
static void pg_atomic_init_u64(volatile pg_atomic_uint64 *ptr, uint64 val)
Definition: atomics.h:426
#define ERROR
Definition: elog.h:43
static uint64 pg_atomic_fetch_or_u64(volatile pg_atomic_uint64 *ptr, uint64 or_)
Definition: atomics.h:506
static uint64 pg_atomic_fetch_and_u64(volatile pg_atomic_uint64 *ptr, uint64 and_)
Definition: atomics.h:497
static uint64 pg_atomic_add_fetch_u64(volatile pg_atomic_uint64 *ptr, int64 add_)
Definition: atomics.h:515
static uint64 pg_atomic_read_u64(volatile pg_atomic_uint64 *ptr)
Definition: atomics.h:440
int i
#define elog
Definition: elog.h:219
#define UINT64_FORMAT
Definition: c.h:301
Datum test_fdw_handler ( PG_FUNCTION_ARGS  )

Definition at line 1102 of file regress.c.

References PG_RETURN_NULL.

1103 {
1104  PG_RETURN_NULL();
1105 }
#define PG_RETURN_NULL()
Definition: fmgr.h:305
Datum ttdummy ( PG_FUNCTION_ARGS  )

Definition at line 455 of file regress.c.

References generate_unaccent_rules::args, CALLED_AS_TRIGGER, CStringGetTextDatum, DatumGetInt64, DirectFunctionCall1, elog, ereport, errcode(), errmsg(), ERROR, i, Int32GetDatum, INT4OID, nextval(), palloc(), pfree(), PointerGetDatum, SPI_connect(), SPI_execp(), SPI_finish(), SPI_fnumber(), SPI_getbinval(), SPI_getrelname(), SPI_gettypeid(), SPI_keepplan(), SPI_modifytuple(), SPI_prepare(), SPI_result, 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.

456 {
457  TriggerData *trigdata = (TriggerData *) fcinfo->context;
458  Trigger *trigger; /* to get trigger name */
459  char **args; /* arguments */
460  int attnum[2]; /* fnumbers of start/stop columns */
461  Datum oldon,
462  oldoff;
463  Datum newon,
464  newoff;
465  Datum *cvals; /* column values */
466  char *cnulls; /* column nulls */
467  char *relname; /* triggered relation name */
468  Relation rel; /* triggered relation */
469  HeapTuple trigtuple;
470  HeapTuple newtuple = NULL;
471  HeapTuple rettuple;
472  TupleDesc tupdesc; /* tuple description */
473  int natts; /* # of attributes */
474  bool isnull; /* to know is some column NULL or not */
475  int ret;
476  int i;
477 
478  if (!CALLED_AS_TRIGGER(fcinfo))
479  elog(ERROR, "ttdummy: not fired by trigger manager");
480  if (!TRIGGER_FIRED_FOR_ROW(trigdata->tg_event))
481  elog(ERROR, "ttdummy: must be fired for row");
482  if (!TRIGGER_FIRED_BEFORE(trigdata->tg_event))
483  elog(ERROR, "ttdummy: must be fired before event");
484  if (TRIGGER_FIRED_BY_INSERT(trigdata->tg_event))
485  elog(ERROR, "ttdummy: cannot process INSERT event");
486  if (TRIGGER_FIRED_BY_UPDATE(trigdata->tg_event))
487  newtuple = trigdata->tg_newtuple;
488 
489  trigtuple = trigdata->tg_trigtuple;
490 
491  rel = trigdata->tg_relation;
492  relname = SPI_getrelname(rel);
493 
494  /* check if TT is OFF for this relation */
495  if (ttoff) /* OFF - nothing to do */
496  {
497  pfree(relname);
498  return PointerGetDatum((newtuple != NULL) ? newtuple : trigtuple);
499  }
500 
501  trigger = trigdata->tg_trigger;
502 
503  if (trigger->tgnargs != 2)
504  elog(ERROR, "ttdummy (%s): invalid (!= 2) number of arguments %d",
505  relname, trigger->tgnargs);
506 
507  args = trigger->tgargs;
508  tupdesc = rel->rd_att;
509  natts = tupdesc->natts;
510 
511  for (i = 0; i < 2; i++)
512  {
513  attnum[i] = SPI_fnumber(tupdesc, args[i]);
514  if (attnum[i] <= 0)
515  elog(ERROR, "ttdummy (%s): there is no attribute %s",
516  relname, args[i]);
517  if (SPI_gettypeid(tupdesc, attnum[i]) != INT4OID)
518  elog(ERROR, "ttdummy (%s): attribute %s must be of integer type",
519  relname, args[i]);
520  }
521 
522  oldon = SPI_getbinval(trigtuple, tupdesc, attnum[0], &isnull);
523  if (isnull)
524  elog(ERROR, "ttdummy (%s): %s must be NOT NULL", relname, args[0]);
525 
526  oldoff = SPI_getbinval(trigtuple, tupdesc, attnum[1], &isnull);
527  if (isnull)
528  elog(ERROR, "ttdummy (%s): %s must be NOT NULL", relname, args[1]);
529 
530  if (newtuple != NULL) /* UPDATE */
531  {
532  newon = SPI_getbinval(newtuple, tupdesc, attnum[0], &isnull);
533  if (isnull)
534  elog(ERROR, "ttdummy (%s): %s must be NOT NULL", relname, args[0]);
535  newoff = SPI_getbinval(newtuple, tupdesc, attnum[1], &isnull);
536  if (isnull)
537  elog(ERROR, "ttdummy (%s): %s must be NOT NULL", relname, args[1]);
538 
539  if (oldon != newon || oldoff != newoff)
540  ereport(ERROR,
541  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
542  errmsg("ttdummy (%s): you cannot change %s and/or %s columns (use set_ttdummy)",
543  relname, args[0], args[1])));
544 
545  if (newoff != TTDUMMY_INFINITY)
546  {
547  pfree(relname); /* allocated in upper executor context */
548  return PointerGetDatum(NULL);
549  }
550  }
551  else if (oldoff != TTDUMMY_INFINITY) /* DELETE */
552  {
553  pfree(relname);
554  return PointerGetDatum(NULL);
555  }
556 
557  newoff = DirectFunctionCall1(nextval, CStringGetTextDatum("ttdummy_seq"));
558  /* nextval now returns int64; coerce down to int32 */
559  newoff = Int32GetDatum((int32) DatumGetInt64(newoff));
560 
561  /* Connect to SPI manager */
562  if ((ret = SPI_connect()) < 0)
563  elog(ERROR, "ttdummy (%s): SPI_connect returned %d", relname, ret);
564 
565  /* Fetch tuple values and nulls */
566  cvals = (Datum *) palloc(natts * sizeof(Datum));
567  cnulls = (char *) palloc(natts * sizeof(char));
568  for (i = 0; i < natts; i++)
569  {
570  cvals[i] = SPI_getbinval((newtuple != NULL) ? newtuple : trigtuple,
571  tupdesc, i + 1, &isnull);
572  cnulls[i] = (isnull) ? 'n' : ' ';
573  }
574 
575  /* change date column(s) */
576  if (newtuple) /* UPDATE */
577  {
578  cvals[attnum[0] - 1] = newoff; /* start_date eq current date */
579  cnulls[attnum[0] - 1] = ' ';
580  cvals[attnum[1] - 1] = TTDUMMY_INFINITY; /* stop_date eq INFINITY */
581  cnulls[attnum[1] - 1] = ' ';
582  }
583  else
584  /* DELETE */
585  {
586  cvals[attnum[1] - 1] = newoff; /* stop_date eq current date */
587  cnulls[attnum[1] - 1] = ' ';
588  }
589 
590  /* if there is no plan ... */
591  if (splan == NULL)
592  {
593  SPIPlanPtr pplan;
594  Oid *ctypes;
595  char *query;
596 
597  /* allocate space in preparation */
598  ctypes = (Oid *) palloc(natts * sizeof(Oid));
599  query = (char *) palloc(100 + 16 * natts);
600 
601  /*
602  * Construct query: INSERT INTO _relation_ VALUES ($1, ...)
603  */
604  sprintf(query, "INSERT INTO %s VALUES (", relname);
605  for (i = 1; i <= natts; i++)
606  {
607  sprintf(query + strlen(query), "$%d%s",
608  i, (i < natts) ? ", " : ")");
609  ctypes[i - 1] = SPI_gettypeid(tupdesc, i);
610  }
611 
612  /* Prepare plan for query */
613  pplan = SPI_prepare(query, natts, ctypes);
614  if (pplan == NULL)
615  elog(ERROR, "ttdummy (%s): SPI_prepare returned %d", relname, SPI_result);
616 
617  if (SPI_keepplan(pplan))
618  elog(ERROR, "ttdummy (%s): SPI_keepplan failed", relname);
619 
620  splan = pplan;
621  }
622 
623  ret = SPI_execp(splan, cvals, cnulls, 0);
624 
625  if (ret < 0)
626  elog(ERROR, "ttdummy (%s): SPI_execp returned %d", relname, ret);
627 
628  /* Tuple to return to upper Executor ... */
629  if (newtuple) /* UPDATE */
630  rettuple = SPI_modifytuple(rel, trigtuple, 1, &(attnum[1]), &newoff, NULL);
631  else /* DELETE */
632  rettuple = trigtuple;
633 
634  SPI_finish(); /* don't forget say Bye to SPI mgr */
635 
636  pfree(relname);
637 
638  return PointerGetDatum(rettuple);
639 }
int SPI_fnumber(TupleDesc tupdesc, const char *fname)
Definition: spi.c:761
Oid SPI_gettypeid(TupleDesc tupdesc, int fnumber)
Definition: spi.c:894
int SPI_connect(void)
Definition: spi.c:84
#define PointerGetDatum(X)
Definition: postgres.h:562
SPIPlanPtr SPI_prepare(const char *src, int nargs, Oid *argtypes)
Definition: spi.c:482
int SPI_finish(void)
Definition: spi.c:148
Datum nextval(PG_FUNCTION_ARGS)
Definition: sequence.c:539
#define INT4OID
Definition: pg_type.h:316
int errcode(int sqlerrcode)
Definition: elog.c:575
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:585
unsigned int Oid
Definition: postgres_ext.h:31
HeapTuple tg_trigtuple
Definition: trigger.h:35
signed int int32
Definition: c.h:246
static bool ttoff
Definition: regress.c:450
int SPI_result
Definition: spi.c:42
void pfree(void *pointer)
Definition: mcxt.c:949
#define ERROR
Definition: elog.h:43
Datum SPI_getbinval(HeapTuple tuple, TupleDesc tupdesc, int fnumber, bool *isnull)
Definition: spi.c:838
#define DatumGetInt64(X)
Definition: postgres.h:613
int SPI_keepplan(SPIPlanPtr plan)
Definition: spi.c:560
static SPIPlanPtr splan
Definition: regress.c:449
HeapTuple SPI_modifytuple(Relation rel, HeapTuple tuple, int natts, int *attnum, Datum *Values, const char *Nulls)
Definition: spi.c:690
#define ereport(elevel, rest)
Definition: elog.h:122
#define TTDUMMY_INFINITY
Definition: regress.c:447
uintptr_t Datum
Definition: postgres.h:372
Trigger * tg_trigger
Definition: trigger.h:37
HeapTuple tg_newtuple
Definition: trigger.h:36
char * SPI_getrelname(Relation rel)
Definition: spi.c:912
#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:485
#define TRIGGER_FIRED_BY_INSERT(event)
Definition: trigger.h:116
void * palloc(Size size)
Definition: mcxt.c:848
int errmsg(const char *fmt,...)
Definition: elog.c:797
int i
int SPI_execp(SPIPlanPtr plan, Datum *Values, const char *Nulls, long tcount)
Definition: spi.c:366
#define CStringGetTextDatum(s)
Definition: builtins.h:91
#define elog
Definition: elog.h:219
#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
Datum wait_pid ( PG_FUNCTION_ARGS  )

Definition at line 847 of file regress.c.

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

848 {
849  int pid = PG_GETARG_INT32(0);
850 
851  if (!superuser())
852  elog(ERROR, "must be superuser to check PID liveness");
853 
854  while (kill(pid, 0) == 0)
855  {
857  pg_usleep(50000);
858  }
859 
860  if (errno != ESRCH)
861  elog(ERROR, "could not check PID %d liveness: %m", pid);
862 
863  PG_RETURN_VOID();
864 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:234
bool superuser(void)
Definition: superuser.c:47
void pg_usleep(long microsec)
Definition: signal.c:53
#define ERROR
Definition: elog.h:43
#define PG_RETURN_VOID()
Definition: fmgr.h:309
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:98
#define elog
Definition: elog.h:219
Datum widget_in ( PG_FUNCTION_ARGS  )

Definition at line 245 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, result, _stringlist::str, Point::x, and Point::y.

246 {
247  char *str = PG_GETARG_CSTRING(0);
248  char *p,
249  *coord[NARGS];
250  int i;
251  WIDGET *result;
252 
253  for (i = 0, p = str; *p && i < NARGS && *p != RDELIM; p++)
254  {
255  if (*p == DELIM || (*p == LDELIM && i == 0))
256  coord[i++] = p + 1;
257  }
258 
259  if (i < NARGS)
260  ereport(ERROR,
261  (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
262  errmsg("invalid input syntax for type widget: \"%s\"",
263  str)));
264 
265  result = (WIDGET *) palloc(sizeof(WIDGET));
266  result->center.x = atof(coord[0]);
267  result->center.y = atof(coord[1]);
268  result->radius = atof(coord[2]);
269 
270  PG_RETURN_POINTER(result);
271 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:321
double radius
Definition: regress.c:236
#define RDELIM
Definition: regress.c:43
double y
Definition: geo_decls.h:60
int errcode(int sqlerrcode)
Definition: elog.c:575
return result
Definition: formatting.c:1633
double x
Definition: geo_decls.h:60
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
#define DELIM
Definition: regress.c:44
#define NARGS
Definition: regress.c:242
void * palloc(Size size)
Definition: mcxt.c:848
int errmsg(const char *fmt,...)
Definition: elog.c:797
Point center
Definition: regress.c:235
int i
#define PG_GETARG_CSTRING(n)
Definition: fmgr.h:242
#define LDELIM
Definition: regress.c:42
Datum widget_out ( PG_FUNCTION_ARGS  )

Definition at line 274 of file regress.c.

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

275 {
276  WIDGET *widget = (WIDGET *) PG_GETARG_POINTER(0);
277  char *str = psprintf("(%g,%g,%g)",
278  widget->center.x, widget->center.y, widget->radius);
279 
280  PG_RETURN_CSTRING(str);
281 }
double radius
Definition: regress.c:236
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
double y
Definition: geo_decls.h:60
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:241
double x
Definition: geo_decls.h:60
#define PG_RETURN_CSTRING(x)
Definition: fmgr.h:322
Point center
Definition: regress.c:235

Variable Documentation

int fd17a_level = 0
static

Definition at line 333 of file regress.c.

Referenced by funny_dup17().

bool fd17a_recursion = true
static

Definition at line 335 of file regress.c.

Referenced by funny_dup17().

TransactionId fd17a_xid = InvalidTransactionId
static

Definition at line 331 of file regress.c.

Referenced by funny_dup17().

int fd17b_level = 0
static

Definition at line 332 of file regress.c.

Referenced by funny_dup17().

bool fd17b_recursion = true
static

Definition at line 334 of file regress.c.

Referenced by funny_dup17().

TransactionId fd17b_xid = InvalidTransactionId
static

Definition at line 330 of file regress.c.

Referenced by funny_dup17().

PG_MODULE_MAGIC

Definition at line 49 of file regress.c.

SPIPlanPtr splan = NULL
static

Definition at line 449 of file regress.c.

Referenced by build_subplan(), ExecReScan(), get_rule_expr(), set_plan_refs(), and SS_process_ctes().

bool ttoff = false
static

Definition at line 450 of file regress.c.

Referenced by set_ttdummy(), and ttdummy().