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