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