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