regress.c 15.7 KB
Newer Older
1
/*
B
Bruce Momjian 已提交
2
 * $Header: /cvsroot/pgsql/src/test/regress/regress.c,v 1.47 2001/03/22 04:01:44 momjian Exp $
3 4
 */

5
#include "postgres.h"
M
Marc G. Fournier 已提交
6

7 8
#include <float.h>				/* faked on sunos */

9
#include "utils/geo_decls.h"	/* includes <math.h> */
10
#include "executor/executor.h"	/* For GetAttributeByName */
11
#include "commands/sequence.h"	/* for nextval() */
12 13

#define P_MAXDIG 12
14 15 16
#define LDELIM			'('
#define RDELIM			')'
#define DELIM			','
17

18
typedef TupleTableSlot *TUPLE;
19

20 21
extern Datum regress_dist_ptpath(PG_FUNCTION_ARGS);
extern Datum regress_path_dist(PG_FUNCTION_ARGS);
22
extern PATH *poly2path(POLYGON *poly);
23
extern Datum interpt_pp(PG_FUNCTION_ARGS);
24
extern void regress_lseg_construct(LSEG *lseg, Point *pt1, Point *pt2);
25
extern Datum overpaid(PG_FUNCTION_ARGS);
26
extern Datum boxarea(PG_FUNCTION_ARGS);
27
extern char *reverse_name(char *string);
B
Bruce Momjian 已提交
28
extern int	oldstyle_length(int n, text *t);
29 30

/*
31
** Distance from a point to a path
32
*/
33 34
PG_FUNCTION_INFO_V1(regress_dist_ptpath);

35 36
Datum
regress_dist_ptpath(PG_FUNCTION_ARGS)
37
{
38 39 40 41
	Point	   *pt = PG_GETARG_POINT_P(0);
	PATH	   *path = PG_GETARG_PATH_P(1);
	float8		result = 0.0;	/* keep compiler quiet */
	float8		tmp;
42 43
	int			i;
	LSEG		lseg;
44 45 46

	switch (path->npts)
	{
47
		case 0:
48
			PG_RETURN_NULL();
49
		case 1:
50
			result = point_dt(pt, &path->p[0]);
51 52 53 54 55 56 57 58 59 60 61
			break;
		default:

			/*
			 * the distance from a point to a path is the smallest
			 * distance from the point to any of its constituent segments.
			 */
			Assert(path->npts > 1);
			for (i = 0; i < path->npts - 1; ++i)
			{
				regress_lseg_construct(&lseg, &path->p[i], &path->p[i + 1]);
62
				tmp = DatumGetFloat8(DirectFunctionCall2(dist_ps,
B
Bruce Momjian 已提交
63 64
													  PointPGetDatum(pt),
												  LsegPGetDatum(&lseg)));
65 66
				if (i == 0 || tmp < result)
					result = tmp;
67 68
			}
			break;
69
	}
70
	PG_RETURN_FLOAT8(result);
71 72 73 74
}

/* this essentially does a cartesian product of the lsegs in the
   two paths, and finds the min distance between any two lsegs */
75 76
PG_FUNCTION_INFO_V1(regress_path_dist);

77 78
Datum
regress_path_dist(PG_FUNCTION_ARGS)
79
{
80 81 82 83 84
	PATH	   *p1 = PG_GETARG_PATH_P(0);
	PATH	   *p2 = PG_GETARG_PATH_P(1);
	bool		have_min = false;
	float8		min = 0.0;		/* initialize to keep compiler quiet */
	float8		tmp;
85 86 87 88
	int			i,
				j;
	LSEG		seg1,
				seg2;
89 90

	for (i = 0; i < p1->npts - 1; i++)
91
	{
92 93 94 95 96
		for (j = 0; j < p2->npts - 1; j++)
		{
			regress_lseg_construct(&seg1, &p1->p[i], &p1->p[i + 1]);
			regress_lseg_construct(&seg2, &p2->p[j], &p2->p[j + 1]);

97 98
			tmp = DatumGetFloat8(DirectFunctionCall2(lseg_distance,
													 LsegPGetDatum(&seg1),
B
Bruce Momjian 已提交
99
												  LsegPGetDatum(&seg2)));
100 101 102 103 104
			if (!have_min || tmp < min)
			{
				min = tmp;
				have_min = true;
			}
105
		}
106
	}
107

B
Bruce Momjian 已提交
108
	if (!have_min)
109 110 111
		PG_RETURN_NULL();

	PG_RETURN_FLOAT8(min);
112 113
}

114
PATH *
115
poly2path(poly)
116
POLYGON    *poly;
117
{
118
	int			i;
119
	char	   *output = (char *) palloc(2 * (P_MAXDIG + 1) * poly->npts + 64);
120
	char		buf[2 * (P_MAXDIG) + 20];
121

122
	sprintf(output, "(1, %*d", P_MAXDIG, poly->npts);
123

124 125 126 127 128
	for (i = 0; i < poly->npts; i++)
	{
		sprintf(buf, ",%*g,%*g", P_MAXDIG, poly->p[i].x, P_MAXDIG, poly->p[i].y);
		strcat(output, buf);
	}
129

130 131
	sprintf(buf, "%c", RDELIM);
	strcat(output, buf);
132 133
	return DatumGetPathP(DirectFunctionCall1(path_in,
											 CStringGetDatum(output)));
134 135
}

136
/* return the point where two paths intersect, or NULL if no intersection. */
137 138
PG_FUNCTION_INFO_V1(interpt_pp);

139 140
Datum
interpt_pp(PG_FUNCTION_ARGS)
141
{
142 143
	PATH	   *p1 = PG_GETARG_PATH_P(0);
	PATH	   *p2 = PG_GETARG_PATH_P(1);
144 145 146 147 148
	int			i,
				j;
	LSEG		seg1,
				seg2;
	bool		found;			/* We've found the intersection */
149

150
	found = false;				/* Haven't found it yet */
B
Bryan Henderson 已提交
151

152
	for (i = 0; i < p1->npts - 1 && !found; i++)
153 154
	{
		regress_lseg_construct(&seg1, &p1->p[i], &p1->p[i + 1]);
155 156 157
		for (j = 0; j < p2->npts - 1 && !found; j++)
		{
			regress_lseg_construct(&seg2, &p2->p[j], &p2->p[j + 1]);
158 159 160
			if (DatumGetBool(DirectFunctionCall2(lseg_intersect,
												 LsegPGetDatum(&seg1),
												 LsegPGetDatum(&seg2))))
161 162
				found = true;
		}
163
	}
164

165 166
	if (!found)
		PG_RETURN_NULL();
167

B
Bruce Momjian 已提交
168 169
	/*
	 * Note: DirectFunctionCall2 will kick out an error if lseg_interpt()
170 171 172 173 174 175
	 * returns NULL, but that should be impossible since we know the two
	 * segments intersect.
	 */
	PG_RETURN_DATUM(DirectFunctionCall2(lseg_interpt,
										LsegPGetDatum(&seg1),
										LsegPGetDatum(&seg2)));
176 177 178 179
}


/* like lseg_construct, but assume space already allocated */
180
void
181
regress_lseg_construct(lseg, pt1, pt2)
182 183 184
LSEG	   *lseg;
Point	   *pt1;
Point	   *pt2;
185
{
186 187 188 189 190
	lseg->p[0].x = pt1->x;
	lseg->p[0].y = pt1->y;
	lseg->p[1].x = pt2->x;
	lseg->p[1].y = pt2->y;
	lseg->m = point_sl(pt1, pt2);
191 192
}

193 194
PG_FUNCTION_INFO_V1(overpaid);

195 196
Datum
overpaid(PG_FUNCTION_ARGS)
197
{
198
	TUPLE		tuple = (TUPLE) PG_GETARG_POINTER(0);
199
	bool		isnull;
200
	int32		salary;
201

202
	salary = DatumGetInt32(GetAttributeByName(tuple, "salary", &isnull));
203 204 205
	if (isnull)
		PG_RETURN_NULL();
	PG_RETURN_BOOL(salary > 699);
206 207
}

208 209
/* New type "widget"
 * This used to be "circle", but I added circle to builtins,
210
 *	so needed to make sure the names do not collide. - tgl 97/04/21
211 212
 */

213 214
typedef struct
{
215 216 217
	Point		center;
	double		radius;
}			WIDGET;
218

219 220
WIDGET	   *widget_in(char *str);
char	   *widget_out(WIDGET * widget);
221
extern Datum pt_in_widget(PG_FUNCTION_ARGS);
222 223 224

#define NARGS	3

225
WIDGET *
226
widget_in(str)
227
char	   *str;
228
{
229 230 231 232 233
	char	   *p,
			   *coord[NARGS],
				buf2[1000];
	int			i;
	WIDGET	   *result;
234 235

	if (str == NULL)
236
		return NULL;
237 238 239 240
	for (i = 0, p = str; *p && i < NARGS && *p != RDELIM; p++)
		if (*p == ',' || (*p == LDELIM && !i))
			coord[i++] = p + 1;
	if (i < NARGS - 1)
241
		return NULL;
242
	result = (WIDGET *) palloc(sizeof(WIDGET));
243 244 245 246
	result->center.x = atof(coord[0]);
	result->center.y = atof(coord[1]);
	result->radius = atof(coord[2]);

247
	sprintf(buf2, "widget_in: read (%f, %f, %f)\n", result->center.x,
248
			result->center.y, result->radius);
249
	return result;
250 251
}

252
char *
253
widget_out(widget)
254
WIDGET	   *widget;
255
{
256
	char	   *result;
257

258
	if (widget == NULL)
259
		return NULL;
260

261 262 263
	result = (char *) palloc(60);
	sprintf(result, "(%g,%g,%g)",
			widget->center.x, widget->center.y, widget->radius);
264
	return result;
265 266
}

267 268
PG_FUNCTION_INFO_V1(pt_in_widget);

269 270
Datum
pt_in_widget(PG_FUNCTION_ARGS)
271
{
272 273
	Point	   *point = PG_GETARG_POINT_P(0);
	WIDGET	   *widget = (WIDGET *) PG_GETARG_POINTER(1);
274

275
	PG_RETURN_BOOL(point_dt(point, &widget->center) < widget->radius);
276 277
}

278
#define ABS(X) ((X) >= 0 ? (X) : -(X))
279

280 281
PG_FUNCTION_INFO_V1(boxarea);

282 283
Datum
boxarea(PG_FUNCTION_ARGS)
284
{
285 286
	BOX		   *box = PG_GETARG_BOX_P(0);
	double		width,
287
				height;
288

289
	width = ABS(box->high.x - box->low.x);
290
	height = ABS(box->high.y - box->low.y);
291
	PG_RETURN_FLOAT8(width * height);
292 293
}

294
char *
295
reverse_name(char *string)
296
{
297
	int			i;
298 299
	int			len;
	char	   *new_string;
300

301
	if (!(new_string = palloc(NAMEDATALEN)))
302
	{
303
		fprintf(stderr, "reverse_name: palloc failed\n");
304
		return NULL;
305
	}
306 307
	MemSet(new_string, 0, NAMEDATALEN);
	for (i = 0; i < NAMEDATALEN && string[i]; ++i)
308
		;
309
	if (i == NAMEDATALEN || !string[i])
310 311 312 313
		--i;
	len = i;
	for (; i >= 0; --i)
		new_string[len - i] = string[i];
314
	return new_string;
315
}
V
Vadim B. Mikheev 已提交
316

317 318 319 320 321 322
/* This rather silly function is just to test that oldstyle functions
 * work correctly on toast-able inputs.
 */
int
oldstyle_length(int n, text *t)
{
B
Bruce Momjian 已提交
323
	int			len = 0;
324 325 326 327 328 329 330

	if (t)
		len = VARSIZE(t) - VARHDRSZ;

	return n + len;
}

V
Vadim B. Mikheev 已提交
331 332 333 334 335 336 337 338 339
#include "executor/spi.h"		/* this is what you need to work with SPI */
#include "commands/trigger.h"	/* -"- and triggers */

static TransactionId fd17b_xid = InvalidTransactionId;
static TransactionId fd17a_xid = InvalidTransactionId;
static int	fd17b_level = 0;
static int	fd17a_level = 0;
static bool fd17b_recursion = true;
static bool fd17a_recursion = true;
340
extern Datum funny_dup17(PG_FUNCTION_ARGS);
V
Vadim B. Mikheev 已提交
341

342 343
PG_FUNCTION_INFO_V1(funny_dup17);

344 345
Datum
funny_dup17(PG_FUNCTION_ARGS)
V
Vadim B. Mikheev 已提交
346
{
347
	TriggerData *trigdata = (TriggerData *) fcinfo->context;
V
Vadim B. Mikheev 已提交
348 349 350 351 352 353
	TransactionId *xid;
	int		   *level;
	bool	   *recursion;
	Relation	rel;
	TupleDesc	tupdesc;
	HeapTuple	tuple;
354 355 356
	char	   *query,
			   *fieldval,
			   *fieldtype;
V
Vadim B. Mikheev 已提交
357 358 359 360 361
	char	   *when;
	int			inserted;
	int			selected = 0;
	int			ret;

362 363 364 365 366
	if (!CALLED_AS_TRIGGER(fcinfo))
		elog(ERROR, "funny_dup17: not fired by trigger manager");

	tuple = trigdata->tg_trigtuple;
	rel = trigdata->tg_relation;
V
Vadim B. Mikheev 已提交
367
	tupdesc = rel->rd_att;
368
	if (TRIGGER_FIRED_BEFORE(trigdata->tg_event))
V
Vadim B. Mikheev 已提交
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
	{
		xid = &fd17b_xid;
		level = &fd17b_level;
		recursion = &fd17b_recursion;
		when = "BEFORE";
	}
	else
	{
		xid = &fd17a_xid;
		level = &fd17a_level;
		recursion = &fd17a_recursion;
		when = "AFTER ";
	}

	if (!TransactionIdIsCurrentTransactionId(*xid))
	{
		*xid = GetCurrentTransactionId();
		*level = 0;
		*recursion = true;
	}

	if (*level == 17)
	{
		*recursion = false;
393
		return PointerGetDatum(tuple);
V
Vadim B. Mikheev 已提交
394 395 396
	}

	if (!(*recursion))
397
		return PointerGetDatum(tuple);
V
Vadim B. Mikheev 已提交
398 399 400 401 402

	(*level)++;

	SPI_connect();

403 404 405
	fieldval = SPI_getvalue(tuple, tupdesc, 1);
	fieldtype = SPI_gettype(tupdesc, 1);

406
	query = (char *) palloc(100 + NAMEDATALEN * 3 +
407 408 409
							strlen(fieldval) + strlen(fieldtype));

	sprintf(query, "insert into %s select * from %s where %s = '%s'::%s",
V
Vadim B. Mikheev 已提交
410 411
			SPI_getrelname(rel), SPI_getrelname(rel),
			SPI_fname(tupdesc, 1),
412
			fieldval, fieldtype);
V
Vadim B. Mikheev 已提交
413

414
	if ((ret = SPI_exec(query, 0)) < 0)
B
Bruce Momjian 已提交
415
		elog(ERROR, "funny_dup17 (fired %s) on level %3d: SPI_exec (insert ...) returned %d",
V
Vadim B. Mikheev 已提交
416 417 418 419
			 when, *level, ret);

	inserted = SPI_processed;

420
	sprintf(query, "select count (*) from %s where %s = '%s'::%s",
V
Vadim B. Mikheev 已提交
421 422
			SPI_getrelname(rel),
			SPI_fname(tupdesc, 1),
423
			fieldval, fieldtype);
V
Vadim B. Mikheev 已提交
424

425
	if ((ret = SPI_exec(query, 0)) < 0)
B
Bruce Momjian 已提交
426
		elog(ERROR, "funny_dup17 (fired %s) on level %3d: SPI_exec (select ...) returned %d",
V
Vadim B. Mikheev 已提交
427 428 429 430
			 when, *level, ret);

	if (SPI_processed > 0)
	{
431
		selected = DatumGetInt32(DirectFunctionCall1(int4in,
B
Bruce Momjian 已提交
432 433 434 435 436
											CStringGetDatum(SPI_getvalue(
												   SPI_tuptable->vals[0],
												   SPI_tuptable->tupdesc,
																		 1
																	))));
V
Vadim B. Mikheev 已提交
437 438 439 440 441 442 443 444 445 446 447 448
	}

	elog(NOTICE, "funny_dup17 (fired %s) on level %3d: %d/%d tuples inserted/selected",
		 when, *level, inserted, selected);

	SPI_finish();

	(*level)--;

	if (*level == 0)
		*xid = InvalidTransactionId;

449
	return PointerGetDatum(tuple);
V
Vadim B. Mikheev 已提交
450
}
451

452
extern Datum ttdummy(PG_FUNCTION_ARGS);
453
extern Datum set_ttdummy(PG_FUNCTION_ARGS);
454 455 456

#define TTDUMMY_INFINITY	999999

457 458
static void *splan = NULL;
static bool ttoff = false;
459

460 461
PG_FUNCTION_INFO_V1(ttdummy);

462 463
Datum
ttdummy(PG_FUNCTION_ARGS)
464
{
465
	TriggerData *trigdata = (TriggerData *) fcinfo->context;
466 467 468
	Trigger    *trigger;		/* to get trigger name */
	char	  **args;			/* arguments */
	int			attnum[2];		/* fnumbers of start/stop columns */
469 470 471 472
	Datum		oldon,
				oldoff;
	Datum		newon,
				newoff;
473 474 475 476 477 478 479 480 481 482 483 484 485
	Datum	   *cvals;			/* column values */
	char	   *cnulls;			/* column nulls */
	char	   *relname;		/* triggered relation name */
	Relation	rel;			/* triggered relation */
	HeapTuple	trigtuple;
	HeapTuple	newtuple = NULL;
	HeapTuple	rettuple;
	TupleDesc	tupdesc;		/* tuple description */
	int			natts;			/* # of attributes */
	bool		isnull;			/* to know is some column NULL or not */
	int			ret;
	int			i;

486 487 488
	if (!CALLED_AS_TRIGGER(fcinfo))
		elog(ERROR, "ttdummy: not fired by trigger manager");
	if (TRIGGER_FIRED_FOR_STATEMENT(trigdata->tg_event))
B
Bruce Momjian 已提交
489
		elog(ERROR, "ttdummy: can't process STATEMENT events");
490
	if (TRIGGER_FIRED_AFTER(trigdata->tg_event))
B
Bruce Momjian 已提交
491
		elog(ERROR, "ttdummy: must be fired before event");
492
	if (TRIGGER_FIRED_BY_INSERT(trigdata->tg_event))
493
		elog(ERROR, "ttdummy: can't process INSERT event");
494 495
	if (TRIGGER_FIRED_BY_UPDATE(trigdata->tg_event))
		newtuple = trigdata->tg_newtuple;
496

497
	trigtuple = trigdata->tg_trigtuple;
498

499
	rel = trigdata->tg_relation;
500
	relname = SPI_getrelname(rel);
501

502
	/* check if TT is OFF for this relation */
503
	if (ttoff)					/* OFF - nothing to do */
504
	{
505
		pfree(relname);
506
		return PointerGetDatum((newtuple != NULL) ? newtuple : trigtuple);
507
	}
508

509
	trigger = trigdata->tg_trigger;
510 511

	if (trigger->tgnargs != 2)
512 513 514
		elog(ERROR, "ttdummy (%s): invalid (!= 2) number of arguments %d",
			 relname, trigger->tgnargs);

515 516 517
	args = trigger->tgargs;
	tupdesc = rel->rd_att;
	natts = tupdesc->natts;
518 519

	for (i = 0; i < 2; i++)
520
	{
521 522
		attnum[i] = SPI_fnumber(tupdesc, args[i]);
		if (attnum[i] < 0)
B
Bruce Momjian 已提交
523
			elog(ERROR, "ttdummy (%s): there is no attribute %s", relname, args[i]);
524 525 526
		if (SPI_gettypeid(tupdesc, attnum[i]) != INT4OID)
			elog(ERROR, "ttdummy (%s): attributes %s and %s must be of abstime type",
				 relname, args[0], args[1]);
527
	}
528 529

	oldon = SPI_getbinval(trigtuple, tupdesc, attnum[0], &isnull);
530
	if (isnull)
B
Bruce Momjian 已提交
531
		elog(ERROR, "ttdummy (%s): %s must be NOT NULL", relname, args[0]);
532 533

	oldoff = SPI_getbinval(trigtuple, tupdesc, attnum[1], &isnull);
534
	if (isnull)
B
Bruce Momjian 已提交
535
		elog(ERROR, "ttdummy (%s): %s must be NOT NULL", relname, args[1]);
536 537

	if (newtuple != NULL)		/* UPDATE */
538
	{
539
		newon = SPI_getbinval(newtuple, tupdesc, attnum[0], &isnull);
540
		if (isnull)
B
Bruce Momjian 已提交
541
			elog(ERROR, "ttdummy (%s): %s must be NOT NULL", relname, args[0]);
542
		newoff = SPI_getbinval(newtuple, tupdesc, attnum[1], &isnull);
543
		if (isnull)
B
Bruce Momjian 已提交
544
			elog(ERROR, "ttdummy (%s): %s must be NOT NULL", relname, args[1]);
545 546 547 548 549 550

		if (oldon != newon || oldoff != newoff)
			elog(ERROR, "ttdummy (%s): you can't change %s and/or %s columns (use set_ttdummy)",
				 relname, args[0], args[1]);

		if (newoff != TTDUMMY_INFINITY)
551
		{
552
			pfree(relname);		/* allocated in upper executor context */
553
			return PointerGetDatum(NULL);
554 555 556 557
		}
	}
	else if (oldoff != TTDUMMY_INFINITY)		/* DELETE */
	{
558
		pfree(relname);
559
		return PointerGetDatum(NULL);
560
	}
561

562
	{
B
Bruce Momjian 已提交
563 564
		text	   *seqname = DatumGetTextP(DirectFunctionCall1(textin,
										CStringGetDatum("ttdummy_seq")));
565

566 567
		newoff = DirectFunctionCall1(nextval,
									 PointerGetDatum(seqname));
568
		pfree(seqname);
569
	}
570

571 572
	/* Connect to SPI manager */
	if ((ret = SPI_connect()) < 0)
B
Bruce Momjian 已提交
573
		elog(ERROR, "ttdummy (%s): SPI_connect returned %d", relname, ret);
574

575
	/* Fetch tuple values and nulls */
576 577
	cvals = (Datum *) palloc(natts * sizeof(Datum));
	cnulls = (char *) palloc(natts * sizeof(char));
578 579
	for (i = 0; i < natts; i++)
	{
580 581
		cvals[i] = SPI_getbinval((newtuple != NULL) ? newtuple : trigtuple,
								 tupdesc, i + 1, &isnull);
582 583
		cnulls[i] = (isnull) ? 'n' : ' ';
	}
584

585
	/* change date column(s) */
586
	if (newtuple)				/* UPDATE */
587
	{
588
		cvals[attnum[0] - 1] = newoff;	/* start_date eq current date */
589
		cnulls[attnum[0] - 1] = ' ';
590
		cvals[attnum[1] - 1] = TTDUMMY_INFINITY;		/* stop_date eq INFINITY */
591 592
		cnulls[attnum[1] - 1] = ' ';
	}
593 594
	else
/* DELETE */
595
	{
596
		cvals[attnum[1] - 1] = newoff;	/* stop_date eq current date */
597 598
		cnulls[attnum[1] - 1] = ' ';
	}
599

600 601 602 603 604
	/* if there is no plan ... */
	if (splan == NULL)
	{
		void	   *pplan;
		Oid		   *ctypes;
605
		char	   *query;
606

607
		/* allocate space in preparation */
608
		ctypes = (Oid *) palloc(natts * sizeof(Oid));
609
		query = (char *) palloc(100 + 16 * natts);
610

611
		/*
612
		 * Construct query: INSERT INTO _relation_ VALUES ($1, ...)
613
		 */
614
		sprintf(query, "INSERT INTO %s VALUES (", relname);
615 616
		for (i = 1; i <= natts; i++)
		{
617
			sprintf(query + strlen(query), "$%d%s",
618
					i, (i < natts) ? ", " : ")");
619 620
			ctypes[i - 1] = SPI_gettypeid(tupdesc, i);
		}
621

622
		/* Prepare plan for query */
623
		pplan = SPI_prepare(query, natts, ctypes);
624
		if (pplan == NULL)
B
Bruce Momjian 已提交
625
			elog(ERROR, "ttdummy (%s): SPI_prepare returned %d", relname, SPI_result);
626

627 628
		pplan = SPI_saveplan(pplan);
		if (pplan == NULL)
B
Bruce Momjian 已提交
629
			elog(ERROR, "ttdummy (%s): SPI_saveplan returned %d", relname, SPI_result);
630

631 632
		splan = pplan;
	}
633

634
	ret = SPI_execp(splan, cvals, cnulls, 0);
635

636
	if (ret < 0)
B
Bruce Momjian 已提交
637
		elog(ERROR, "ttdummy (%s): SPI_execp returned %d", relname, ret);
638

639
	/* Tuple to return to upper Executor ... */
640
	if (newtuple)				/* UPDATE */
641 642
	{
		HeapTuple	tmptuple;
643 644 645

		tmptuple = SPI_copytuple(trigtuple);
		rettuple = SPI_modifytuple(rel, tmptuple, 1, &(attnum[1]), &newoff, NULL);
646
		SPI_freetuple(tmptuple);
647
	}
648 649
	else
/* DELETE */
650
		rettuple = trigtuple;
651 652 653 654

	SPI_finish();				/* don't forget say Bye to SPI mgr */

	pfree(relname);
655

656
	return PointerGetDatum(rettuple);
657 658
}

659 660
PG_FUNCTION_INFO_V1(set_ttdummy);

661 662
Datum
set_ttdummy(PG_FUNCTION_ARGS)
663
{
664
	int32		on = PG_GETARG_INT32(0);
665 666

	if (ttoff)					/* OFF currently */
667 668
	{
		if (on == 0)
669
			PG_RETURN_INT32(0);
670

671 672
		/* turn ON */
		ttoff = false;
673
		PG_RETURN_INT32(0);
674
	}
675

676 677
	/* ON currently */
	if (on != 0)
678
		PG_RETURN_INT32(1);
679

680 681
	/* turn OFF */
	ttoff = true;
682

683
	PG_RETURN_INT32(1);
684
}