pg_operator.c 31.3 KB
Newer Older
1 2 3
/*-------------------------------------------------------------------------
 *
 * pg_operator.c--
4
 *	  routines to support manipulation of the pg_operator relation
5 6 7 8 9
 *
 * Copyright (c) 1994, Regents of the University of California
 *
 *
 * IDENTIFICATION
10
 *	  $Header: /cvsroot/pgsql/src/backend/catalog/pg_operator.c,v 1.26 1998/07/27 19:37:49 vadim Exp $
11 12
 *
 * NOTES
13 14
 *	  these routines moved here from commands/define.c and somewhat cleaned up.
 *
15 16
 *-------------------------------------------------------------------------
 */
B
Bruce Momjian 已提交
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
#include "postgres.h"

#include "access/heapam.h"
#include "catalog/catname.h"
#include "catalog/pg_operator.h"
#include "catalog/pg_proc.h"
#include "catalog/pg_type.h"
#include "fmgr.h"
#include "miscadmin.h"
#include "parser/parse_oper.h"
#include "storage/bufmgr.h"
#include "utils/builtins.h"
#include "utils/syscache.h"
#include "utils/tqual.h"

M
Marc G. Fournier 已提交
32
#ifndef HAVE_MEMMOVE
33
#include <regex/utils.h>
M
Marc G. Fournier 已提交
34
#else
35
#include <string.h>
M
Marc G. Fournier 已提交
36
#endif
37

38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
static Oid
OperatorGetWithOpenRelation(Relation pg_operator_desc,
							const char *operatorName,
							Oid leftObjectId,
							Oid rightObjectId);
static Oid
OperatorGet(char *operatorName,
			char *leftTypeName,
			char *rightTypeName);

static Oid
OperatorShellMakeWithOpenRelation(Relation pg_operator_desc,
								  char *operatorName,
								  Oid leftObjectId,
								  Oid rightObjectId);
static Oid
OperatorShellMake(char *operatorName,
				  char *leftTypeName,
				  char *rightTypeName);

static void
OperatorDef(char *operatorName,
60 61 62 63 64 65 66 67 68 69 70 71
			int definedOK,
			char *leftTypeName,
			char *rightTypeName,
			char *procedureName,
			uint16 precedence,
			bool isLeftAssociative,
			char *commutatorName,
			char *negatorName,
			char *restrictionName,
			char *oinName,
			bool canHash,
			char *leftSortName,
72
			char *rightSortName);
73
static void OperatorUpd(Oid baseId, Oid commId, Oid negId);
74

75
/* ----------------------------------------------------------------
76
 *		OperatorGetWithOpenRelation
77
 *
78 79
 *		preforms a scan on pg_operator for an operator tuple
 *		with given name and left/right type oids.
80
 * ----------------------------------------------------------------
81 82 83 84
 *	  pg_operator_desc	-- reldesc for pg_operator
 *	  operatorName		-- name of operator to fetch
 *	  leftObjectId		-- left oid of operator to fetch
 *	  rightObjectId		-- right oid of operator to fetch
85
 */
86
static Oid
87
OperatorGetWithOpenRelation(Relation pg_operator_desc,
88 89 90
							const char *operatorName,
							Oid leftObjectId,
							Oid rightObjectId)
91
{
92 93 94
	HeapScanDesc pg_operator_scan;
	Oid			operatorObjectId;
	HeapTuple	tup;
95 96

	static ScanKeyData opKey[3] = {
B
Bruce Momjian 已提交
97 98 99
		{0, Anum_pg_operator_oprname, F_NAMEEQ},
		{0, Anum_pg_operator_oprleft, F_OIDEQ},
		{0, Anum_pg_operator_oprright, F_OIDEQ},
100 101
	};

B
Bruce Momjian 已提交
102 103 104
	fmgr_info(F_NAMEEQ, &opKey[0].sk_func);
	fmgr_info(F_OIDEQ, &opKey[1].sk_func);
	fmgr_info(F_OIDEQ, &opKey[2].sk_func);
105 106 107
	opKey[0].sk_nargs = opKey[0].sk_func.fn_nargs;
	opKey[1].sk_nargs = opKey[1].sk_func.fn_nargs;
	opKey[2].sk_nargs = opKey[2].sk_func.fn_nargs;
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122

	/* ----------------
	 *	form scan key
	 * ----------------
	 */
	opKey[0].sk_argument = PointerGetDatum(operatorName);
	opKey[1].sk_argument = ObjectIdGetDatum(leftObjectId);
	opKey[2].sk_argument = ObjectIdGetDatum(rightObjectId);

	/* ----------------
	 *	begin the scan
	 * ----------------
	 */
	pg_operator_scan = heap_beginscan(pg_operator_desc,
									  0,
123
									  SnapshotSelf,
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
									  3,
									  opKey);

	/* ----------------
	 *	fetch the operator tuple, if it exists, and determine
	 *	the proper return oid value.
	 * ----------------
	 */
	tup = heap_getnext(pg_operator_scan, 0, (Buffer *) 0);
	operatorObjectId = HeapTupleIsValid(tup) ? tup->t_oid : InvalidOid;

	/* ----------------
	 *	close the scan and return the oid.
	 * ----------------
	 */
	heap_endscan(pg_operator_scan);

	return
		operatorObjectId;
143 144 145
}

/* ----------------------------------------------------------------
146
 *		OperatorGet
147
 *
148 149
 *		finds the operator associated with the specified name
 *		and left and right type names.
150 151
 * ----------------------------------------------------------------
 */
152
static Oid
153
OperatorGet(char *operatorName,
154 155
			char *leftTypeName,
			char *rightTypeName)
156
{
157
	Relation	pg_operator_desc;
158

159 160 161 162 163
	Oid			operatorObjectId;
	Oid			leftObjectId = InvalidOid;
	Oid			rightObjectId = InvalidOid;
	bool		leftDefined = false;
	bool		rightDefined = false;
164 165 166 167 168 169 170 171 172 173 174 175

	/* ----------------
	 *	look up the operator types.
	 *
	 *	Note: types must be defined before operators
	 * ----------------
	 */
	if (leftTypeName)
	{
		leftObjectId = TypeGet(leftTypeName, &leftDefined);

		if (!OidIsValid(leftObjectId) || !leftDefined)
B
Bruce Momjian 已提交
176
			elog(ERROR, "OperatorGet: left type '%s' nonexistent", leftTypeName);
177 178 179 180 181 182 183
	}

	if (rightTypeName)
	{
		rightObjectId = TypeGet(rightTypeName, &rightDefined);

		if (!OidIsValid(rightObjectId) || !rightDefined)
B
Bruce Momjian 已提交
184
			elog(ERROR, "OperatorGet: right type '%s' nonexistent",
185 186 187 188 189
				 rightTypeName);
	}

	if (!((OidIsValid(leftObjectId) && leftDefined) ||
		  (OidIsValid(rightObjectId) && rightDefined)))
B
Bruce Momjian 已提交
190
		elog(ERROR, "OperatorGet: no argument types??");
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215

	/* ----------------
	 *	open the pg_operator relation
	 * ----------------
	 */
	pg_operator_desc = heap_openr(OperatorRelationName);

	/* ----------------
	 *	get the oid for the operator with the appropriate name
	 *	and left/right types.
	 * ----------------
	 */
	operatorObjectId = OperatorGetWithOpenRelation(pg_operator_desc,
												   operatorName,
												   leftObjectId,
												   rightObjectId);

	/* ----------------
	 *	close the relation and return the operator oid.
	 * ----------------
	 */
	heap_close(pg_operator_desc);

	return
		operatorObjectId;
216 217 218
}

/* ----------------------------------------------------------------
219
 *		OperatorShellMakeWithOpenRelation
220 221 222
 *
 * ----------------------------------------------------------------
 */
223
static Oid
224
OperatorShellMakeWithOpenRelation(Relation pg_operator_desc,
225 226 227
								  char *operatorName,
								  Oid leftObjectId,
								  Oid rightObjectId)
228
{
229
	int			i;
230 231 232 233
	HeapTuple	tup;
	Datum		values[Natts_pg_operator];
	char		nulls[Natts_pg_operator];
	Oid			operatorObjectId;
234
	NameData	oname;
235
	TupleDesc	tupDesc;
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251

	/* ----------------
	 *	initialize our nulls[] and values[] arrays
	 * ----------------
	 */
	for (i = 0; i < Natts_pg_operator; ++i)
	{
		nulls[i] = ' ';
		values[i] = (Datum) NULL;		/* redundant, but safe */
	}

	/* ----------------
	 *	initialize values[] with the type name and
	 * ----------------
	 */
	i = 0;
252 253
	namestrcpy(&oname, operatorName);
	values[i++] = NameGetDatum(&oname);
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
	values[i++] = Int32GetDatum(GetUserId());
	values[i++] = (Datum) (uint16) 0;

	values[i++] = (Datum) 'b';	/* fill oprkind with a bogus value */

	values[i++] = (Datum) (bool) 0;
	values[i++] = (Datum) (bool) 0;
	values[i++] = ObjectIdGetDatum(leftObjectId);		/* <-- left oid */
	values[i++] = ObjectIdGetDatum(rightObjectId);		/* <-- right oid */
	values[i++] = ObjectIdGetDatum(InvalidOid);
	values[i++] = ObjectIdGetDatum(InvalidOid);
	values[i++] = ObjectIdGetDatum(InvalidOid);
	values[i++] = ObjectIdGetDatum(InvalidOid);
	values[i++] = ObjectIdGetDatum(InvalidOid);
	values[i++] = ObjectIdGetDatum(InvalidOid);
	values[i++] = ObjectIdGetDatum(InvalidOid);
	values[i++] = ObjectIdGetDatum(InvalidOid);

	/* ----------------
	 *	create a new operator tuple
	 * ----------------
	 */
	tupDesc = pg_operator_desc->rd_att;

	tup = heap_formtuple(tupDesc,
						 values,
						 nulls);

	/* ----------------
	 *	insert our "shell" operator tuple and
	 *	close the relation
	 * ----------------
	 */
	heap_insert(pg_operator_desc, tup);
	operatorObjectId = tup->t_oid;

	/* ----------------
	 *	free the tuple and return the operator oid
	 * ----------------
	 */
	pfree(tup);

	return
		operatorObjectId;
298 299 300
}

/* ----------------------------------------------------------------
301
 *		OperatorShellMake
302
 *
303 304 305 306
 *		Specify operator name and left and right type names,
 *		fill an operator struct with this info and NULL's,
 *		call heap_insert and return the Oid
 *		to the caller.
307 308
 * ----------------------------------------------------------------
 */
309
static Oid
310
OperatorShellMake(char *operatorName,
311 312 313
				  char *leftTypeName,
				  char *rightTypeName)
{
314 315
	Relation	pg_operator_desc;
	Oid			operatorObjectId;
316

317 318 319 320
	Oid			leftObjectId = InvalidOid;
	Oid			rightObjectId = InvalidOid;
	bool		leftDefined = false;
	bool		rightDefined = false;
321 322 323 324 325 326 327 328 329 330 331 332 333

	/* ----------------
	 *	get the left and right type oid's for this operator
	 * ----------------
	 */
	if (leftTypeName)
		leftObjectId = TypeGet(leftTypeName, &leftDefined);

	if (rightTypeName)
		rightObjectId = TypeGet(rightTypeName, &rightDefined);

	if (!((OidIsValid(leftObjectId) && leftDefined) ||
		  (OidIsValid(rightObjectId) && rightDefined)))
B
Bruce Momjian 已提交
334
		elog(ERROR, "OperatorShellMake: no valid argument types??");
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359

	/* ----------------
	 *	open pg_operator
	 * ----------------
	 */
	pg_operator_desc = heap_openr(OperatorRelationName);

	/* ----------------
	 *	add a "shell" operator tuple to the operator relation
	 *	and recover the shell tuple's oid.
	 * ----------------
	 */
	operatorObjectId =
		OperatorShellMakeWithOpenRelation(pg_operator_desc,
										  operatorName,
										  leftObjectId,
										  rightObjectId);
	/* ----------------
	 *	close the operator relation and return the oid.
	 * ----------------
	 */
	heap_close(pg_operator_desc);

	return
		operatorObjectId;
360 361 362 363 364 365 366 367 368
}

/* --------------------------------
 * OperatorDef
 *
 * This routine gets complicated because it allows the user to
 * specify operators that do not exist.  For example, if operator
 * "op" is being defined, the negator operator "negop" and the
 * commutator "commop" can also be defined without specifying
369
 * any information other than their names.	Since in order to
370 371 372 373 374 375 376 377 378 379
 * add "op" to the PG_OPERATOR catalog, all the Oid's for these
 * operators must be placed in the fields of "op", a forward
 * declaration is done on the commutator and negator operators.
 * This is called creating a shell, and its main effect is to
 * create a tuple in the PG_OPERATOR catalog with minimal
 * information about the operator (just its name and types).
 * Forward declaration is used only for this purpose, it is
 * not available to the user as it is for type definition.
 *
 * Algorithm:
380 381 382 383
 *
 * check if operator already defined
 *	  if so issue error if not definedOk, this is a duplicate
 *	  but if definedOk, save the Oid -- filling in a shell
384 385
 * get the attribute types from relation descriptor for pg_operator
 * assign values to the fields of the operator:
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
 *	 operatorName
 *	 owner id (simply the user id of the caller)
 *	 precedence
 *	 operator "kind" either "b" for binary or "l" for left unary
 *	 isLeftAssociative boolean
 *	 canHash boolean
 *	 leftTypeObjectId -- type must already be defined
 *	 rightTypeObjectId -- this is optional, enter ObjectId=0 if none specified
 *	 resultType -- defer this, since it must be determined from
 *				   the pg_procedure catalog
 *	 commutatorObjectId -- if this is NULL, enter ObjectId=0
 *					  else if this already exists, enter it's ObjectId
 *					  else if this does not yet exist, and is not
 *						the same as the main operatorName, then create
 *						a shell and enter the new ObjectId
 *					  else if this does not exist but IS the same
 *						name as the main operator, set the ObjectId=0.
 *						Later OperatorCreate will make another call
 *						to OperatorDef which will cause this field
 *						to be filled in (because even though the names
 *						will be switched, they are the same name and
 *						at this point this ObjectId will then be defined)
 *	 negatorObjectId   -- same as for commutatorObjectId
 *	 leftSortObjectId  -- same as for commutatorObjectId
 *	 rightSortObjectId -- same as for commutatorObjectId
 *	 operatorProcedure -- must access the pg_procedure catalog to get the
 *				   ObjectId of the procedure that actually does the operator
 *				   actions this is required.  Do an amgetattr to find out the
 *				   return type of the procedure
 *	 restrictionProcedure -- must access the pg_procedure catalog to get
 *				   the ObjectId but this is optional
 *	 joinProcedure -- same as restrictionProcedure
418 419
 * now either insert or replace the operator into the pg_operator catalog
 * if the operator shell is being filled in
420 421 422
 *	 access the catalog in order to get a valid buffer
 *	 create a tuple using ModifyHeapTuple
 *	 get the t_ctid from the modified tuple and call RelationReplaceHeapTuple
423
 * else if a new operator is being created
424 425
 *	 create a tuple using heap_formtuple
 *	 call heap_insert
426
 * --------------------------------
427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
 *		"X" indicates an optional argument (i.e. one that can be NULL)
 *		operatorName;			-- operator name
 *		definedOK;				-- operator can already have an oid?
 *		leftTypeName;			-- X left type name
 *		rightTypeName;			-- X right type name
 *		procedureName;			-- procedure oid for operator code
 *		precedence;				-- operator precedence
 *		isLeftAssociative;		-- operator is left associative?
 *		commutatorName;			-- X commutator operator name
 *		negatorName;			-- X negator operator name
 *		restrictionName;		-- X restriction sel. procedure name
 *		joinName;				-- X join sel. procedure name
 *		canHash;				-- possible hash operator?
 *		leftSortName;			-- X left sort operator
 *		rightSortName;			-- X right sort operator
442 443 444
 */
static void
OperatorDef(char *operatorName,
445 446 447 448 449 450 451 452 453 454 455 456 457
			int definedOK,
			char *leftTypeName,
			char *rightTypeName,
			char *procedureName,
			uint16 precedence,
			bool isLeftAssociative,
			char *commutatorName,
			char *negatorName,
			char *restrictionName,
			char *joinName,
			bool canHash,
			char *leftSortName,
			char *rightSortName)
458
{
459
	int			i,
460 461
				j;
	Relation	pg_operator_desc;
462

463 464 465
	HeapScanDesc pg_operator_scan;
	HeapTuple	tup;
	Buffer		buffer;
466
	ItemPointerData itemPointerData;
467 468 469 470 471 472 473 474 475 476 477 478 479 480
	char		nulls[Natts_pg_operator];
	char		replaces[Natts_pg_operator];
	Datum		values[Natts_pg_operator];
	Oid			other_oid = 0;
	Oid			operatorObjectId;
	Oid			leftTypeId = InvalidOid;
	Oid			rightTypeId = InvalidOid;
	Oid			commutatorId = InvalidOid;
	Oid			negatorId = InvalidOid;
	bool		leftDefined = false;
	bool		rightDefined = false;
	char	   *name[4];
	Oid			typeId[8];
	int			nargs;
481
	NameData		oname;
482
	TupleDesc	tupDesc;
483 484

	static ScanKeyData opKey[3] = {
B
Bruce Momjian 已提交
485 486 487
		{0, Anum_pg_operator_oprname, F_NAMEEQ},
		{0, Anum_pg_operator_oprleft, F_OIDEQ},
		{0, Anum_pg_operator_oprright, F_OIDEQ},
488 489
	};

B
Bruce Momjian 已提交
490 491 492
	fmgr_info(F_NAMEEQ, &opKey[0].sk_func);
	fmgr_info(F_OIDEQ, &opKey[1].sk_func);
	fmgr_info(F_OIDEQ, &opKey[2].sk_func);
493 494 495
	opKey[0].sk_nargs = opKey[0].sk_func.fn_nargs;
	opKey[1].sk_nargs = opKey[1].sk_func.fn_nargs;
	opKey[2].sk_nargs = opKey[2].sk_func.fn_nargs;
496 497 498 499 500 501

	operatorObjectId = OperatorGet(operatorName,
								   leftTypeName,
								   rightTypeName);

	if (OidIsValid(operatorObjectId) && !definedOK)
B
Bruce Momjian 已提交
502
		elog(ERROR, "OperatorDef: operator \"%s\" already defined",
503 504 505 506 507 508 509 510 511 512
			 operatorName);

	if (leftTypeName)
		leftTypeId = TypeGet(leftTypeName, &leftDefined);

	if (rightTypeName)
		rightTypeId = TypeGet(rightTypeName, &rightDefined);

	if (!((OidIsValid(leftTypeId && leftDefined)) ||
		  (OidIsValid(rightTypeId && rightDefined))))
B
Bruce Momjian 已提交
513
		elog(ERROR, "OperatorGet: no argument types??");
514 515 516 517 518 519 520 521 522 523 524 525 526 527 528

	for (i = 0; i < Natts_pg_operator; ++i)
	{
		values[i] = (Datum) NULL;
		replaces[i] = 'r';
		nulls[i] = ' ';
	}

	/* ----------------
	 * Look up registered procedures -- find the return type
	 * of procedureName to place in "result" field.
	 * Do this before shells are created so we don't
	 * have to worry about deleting them later.
	 * ----------------
	 */
B
Bruce Momjian 已提交
529
	MemSet(typeId, 0, 8 * sizeof(Oid));
530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545
	if (!leftTypeName)
	{
		typeId[0] = rightTypeId;
		nargs = 1;
	}
	else if (!rightTypeName)
	{
		typeId[0] = leftTypeId;
		nargs = 1;
	}
	else
	{
		typeId[0] = leftTypeId;
		typeId[1] = rightTypeId;
		nargs = 2;
	}
546
	tup = SearchSysCacheTuple(PRONAME,
547 548 549 550 551 552
							  PointerGetDatum(procedureName),
							  Int32GetDatum(nargs),
							  PointerGetDatum(typeId),
							  0);

	if (!PointerIsValid(tup))
553
		func_error("OperatorDef", procedureName, nargs, typeId, NULL);
554 555 556 557 558 559 560 561 562 563 564 565

	values[Anum_pg_operator_oprcode - 1] = ObjectIdGetDatum(tup->t_oid);
	values[Anum_pg_operator_oprresult - 1] =
		ObjectIdGetDatum(((Form_pg_proc)
						  GETSTRUCT(tup))->prorettype);

	/* ----------------
	 *	find restriction
	 * ----------------
	 */
	if (restrictionName)
	{							/* optional */
B
Bruce Momjian 已提交
566
		MemSet(typeId, 0, 8 * sizeof(Oid));
567 568 569 570 571 572 573 574 575 576 577
		typeId[0] = OIDOID;		/* operator OID */
		typeId[1] = OIDOID;		/* relation OID */
		typeId[2] = INT2OID;	/* attribute number */
		typeId[3] = 0;			/* value - can be any type	*/
		typeId[4] = INT4OID;	/* flags - left or right selectivity */
		tup = SearchSysCacheTuple(PRONAME,
								  PointerGetDatum(restrictionName),
								  Int32GetDatum(5),
								  PointerGetDatum(typeId),
								  0);
		if (!HeapTupleIsValid(tup))
578
			func_error("OperatorDef", restrictionName, 5, typeId, NULL);
579 580 581 582 583 584 585 586 587 588 589 590

		values[Anum_pg_operator_oprrest - 1] = ObjectIdGetDatum(tup->t_oid);
	}
	else
		values[Anum_pg_operator_oprrest - 1] = ObjectIdGetDatum(InvalidOid);

	/* ----------------
	 *	find join - only valid for binary operators
	 * ----------------
	 */
	if (joinName)
	{							/* optional */
B
Bruce Momjian 已提交
591
		MemSet(typeId, 0, 8 * sizeof(Oid));
592 593 594 595 596 597 598 599 600 601 602 603
		typeId[0] = OIDOID;		/* operator OID */
		typeId[1] = OIDOID;		/* relation OID 1 */
		typeId[2] = INT2OID;	/* attribute number 1 */
		typeId[3] = OIDOID;		/* relation OID 2 */
		typeId[4] = INT2OID;	/* attribute number 2 */

		tup = SearchSysCacheTuple(PRONAME,
								  PointerGetDatum(joinName),
								  Int32GetDatum(5),
								  PointerGetDatum(typeId),
								  0);
		if (!HeapTupleIsValid(tup))
604
			func_error("OperatorDef", joinName, 5, typeId, NULL);
605 606 607 608 609 610 611 612 613 614 615

		values[Anum_pg_operator_oprjoin - 1] = ObjectIdGetDatum(tup->t_oid);
	}
	else
		values[Anum_pg_operator_oprjoin - 1] = ObjectIdGetDatum(InvalidOid);

	/* ----------------
	 * set up values in the operator tuple
	 * ----------------
	 */
	i = 0;
616 617
	namestrcpy(&oname, operatorName);
	values[i++] = NameGetDatum(&oname);
618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676
	values[i++] = Int32GetDatum(GetUserId());
	values[i++] = UInt16GetDatum(precedence);
	values[i++] = leftTypeName ? (rightTypeName ? 'b' : 'r') : 'l';
	values[i++] = Int8GetDatum(isLeftAssociative);
	values[i++] = Int8GetDatum(canHash);
	values[i++] = ObjectIdGetDatum(leftTypeId);
	values[i++] = ObjectIdGetDatum(rightTypeId);

	++i;						/* Skip "prorettype", this was done above */

	/*
	 * Set up the other operators.	If they do not currently exist, set up
	 * shells in order to get ObjectId's and call OperatorDef again later
	 * to fill in the shells.
	 */
	name[0] = commutatorName;
	name[1] = negatorName;
	name[2] = leftSortName;
	name[3] = rightSortName;

	for (j = 0; j < 4; ++j)
	{
		if (name[j])
		{

			/* for the commutator, switch order of arguments */
			if (j == 0)
			{
				other_oid = OperatorGet(name[j], rightTypeName, leftTypeName);
				commutatorId = other_oid;
			}
			else
			{
				other_oid = OperatorGet(name[j], leftTypeName, rightTypeName);
				if (j == 1)
					negatorId = other_oid;
			}

			if (OidIsValid(other_oid))	/* already in catalogs */
				values[i++] = ObjectIdGetDatum(other_oid);
			else if (strcmp(operatorName, name[j]) != 0)
			{
				/* not in catalogs, different from operator */

				/* for the commutator, switch order of arguments */
				if (j == 0)
				{
					other_oid = OperatorShellMake(name[j],
												  rightTypeName,
												  leftTypeName);
				}
				else
				{
					other_oid = OperatorShellMake(name[j],
												  leftTypeName,
												  rightTypeName);
				}

				if (!OidIsValid(other_oid))
B
Bruce Momjian 已提交
677
					elog(ERROR,
678 679 680 681 682 683 684 685 686
						 "OperatorDef: can't create operator '%s'",
						 name[j]);
				values[i++] = ObjectIdGetDatum(other_oid);

			}
			else
/* not in catalogs, same as operator ??? */
				values[i++] = ObjectIdGetDatum(InvalidOid);

687
		}
688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707
		else
/* new operator is optional */
			values[i++] = ObjectIdGetDatum(InvalidOid);
	}

	/* last three fields were filled in first */

	/*
	 * If we are adding to an operator shell, get its t_ctid and a buffer.
	 */
	pg_operator_desc = heap_openr(OperatorRelationName);

	if (operatorObjectId)
	{
		opKey[0].sk_argument = PointerGetDatum(operatorName);
		opKey[1].sk_argument = ObjectIdGetDatum(leftTypeId);
		opKey[2].sk_argument = ObjectIdGetDatum(rightTypeId);

		pg_operator_scan = heap_beginscan(pg_operator_desc,
										  0,
708
										  SnapshotSelf,
709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727
										  3,
										  opKey);

		tup = heap_getnext(pg_operator_scan, 0, &buffer);
		if (HeapTupleIsValid(tup))
		{
			tup = heap_modifytuple(tup,
								   buffer,
								   pg_operator_desc,
								   values,
								   nulls,
								   replaces);

			ItemPointerCopy(&tup->t_ctid, &itemPointerData);
			setheapoverride(true);
			heap_replace(pg_operator_desc, &itemPointerData, tup);
			setheapoverride(false);
		}
		else
B
Bruce Momjian 已提交
728
			elog(ERROR, "OperatorDef: no operator %d", other_oid);
729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760

		heap_endscan(pg_operator_scan);

	}
	else
	{
		tupDesc = pg_operator_desc->rd_att;
		tup = heap_formtuple(tupDesc, values, nulls);

		heap_insert(pg_operator_desc, tup);
		operatorObjectId = tup->t_oid;
	}

	heap_close(pg_operator_desc);

	/*
	 * It's possible that we're creating a skeleton operator here for the
	 * commute or negate attributes of a real operator.  If we are, then
	 * we're done.  If not, we may need to update the negator and
	 * commutator for this attribute.  The reason for this is that the
	 * user may want to create two operators (say < and >=).  When he
	 * defines <, if he uses >= as the negator or commutator, he won't be
	 * able to insert it later, since (for some reason) define operator
	 * defines it for him.	So what he does is to define > without a
	 * negator or commutator.  Then he defines >= with < as the negator
	 * and commutator.	As a side effect, this will update the > tuple if
	 * it has no commutator or negator defined.
	 *
	 * Alstublieft, Tom Vijlbrief.
	 */
	if (!definedOK)
		OperatorUpd(operatorObjectId, commutatorId, negatorId);
761 762 763 764 765
}

/* ----------------------------------------------------------------
 * OperatorUpd
 *
766 767 768 769 770 771
 *	For a given operator, look up its negator and commutator operators.
 *	If they are defined, but their negator and commutator operators
 *	(respectively) are not, then use the new operator for neg and comm.
 *	This solves a problem for users who need to insert two new operators
 *	which are the negator or commutator of each other.
 * ----------------------------------------------------------------
772 773 774 775
 */
static void
OperatorUpd(Oid baseId, Oid commId, Oid negId)
{
776
	int			i;
777 778 779 780
	Relation	pg_operator_desc;
	HeapScanDesc pg_operator_scan;
	HeapTuple	tup;
	Buffer		buffer;
781
	ItemPointerData itemPointerData;
782 783 784
	char		nulls[Natts_pg_operator];
	char		replaces[Natts_pg_operator];
	Datum		values[Natts_pg_operator];
785 786

	static ScanKeyData opKey[1] = {
B
Bruce Momjian 已提交
787
		{0, ObjectIdAttributeNumber, F_OIDEQ},
788 789
	};

B
Bruce Momjian 已提交
790
	fmgr_info(F_OIDEQ, &opKey[0].sk_func);
791
	opKey[0].sk_nargs = opKey[0].sk_func.fn_nargs;
792 793 794 795 796 797 798 799 800 801 802 803 804 805 806

	for (i = 0; i < Natts_pg_operator; ++i)
	{
		values[i] = (Datum) NULL;
		replaces[i] = ' ';
		nulls[i] = ' ';
	}

	pg_operator_desc = heap_openr(OperatorRelationName);

	/* check and update the commutator, if necessary */
	opKey[0].sk_argument = ObjectIdGetDatum(commId);

	pg_operator_scan = heap_beginscan(pg_operator_desc,
									  0,
807
									  SnapshotSelf,
808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852
									  1,
									  opKey);

	tup = heap_getnext(pg_operator_scan, 0, &buffer);

	/* if the commutator and negator are the same operator, do one update */
	if (commId == negId)
	{
		if (HeapTupleIsValid(tup))
		{
			OperatorTupleForm t;

			t = (OperatorTupleForm) GETSTRUCT(tup);
			if (!OidIsValid(t->oprcom)
				|| !OidIsValid(t->oprnegate))
			{

				if (!OidIsValid(t->oprnegate))
				{
					values[Anum_pg_operator_oprnegate - 1] =
						ObjectIdGetDatum(baseId);
					replaces[Anum_pg_operator_oprnegate - 1] = 'r';
				}

				if (!OidIsValid(t->oprcom))
				{
					values[Anum_pg_operator_oprcom - 1] =
						ObjectIdGetDatum(baseId);
					replaces[Anum_pg_operator_oprcom - 1] = 'r';
				}

				tup = heap_modifytuple(tup,
									   buffer,
									   pg_operator_desc,
									   values,
									   nulls,
									   replaces);

				ItemPointerCopy(&tup->t_ctid, &itemPointerData);

				setheapoverride(true);
				heap_replace(pg_operator_desc, &itemPointerData, tup);
				setheapoverride(false);

			}
853
		}
854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870
		heap_endscan(pg_operator_scan);

		heap_close(pg_operator_desc);

		/* release the buffer properly */
		if (BufferIsValid(buffer))
			ReleaseBuffer(buffer);

		return;
	}

	/* if commutator and negator are different, do two updates */
	if (HeapTupleIsValid(tup) &&
		!(OidIsValid(((OperatorTupleForm) GETSTRUCT(tup))->oprcom)))
	{
		values[Anum_pg_operator_oprcom - 1] = ObjectIdGetDatum(baseId);
		replaces[Anum_pg_operator_oprcom - 1] = 'r';
871
		tup = heap_modifytuple(tup,
872 873 874 875 876 877
							   buffer,
							   pg_operator_desc,
							   values,
							   nulls,
							   replaces);

878 879
		ItemPointerCopy(&tup->t_ctid, &itemPointerData);
		setheapoverride(true);
880
		heap_replace(pg_operator_desc, &itemPointerData, tup);
881 882
		setheapoverride(false);

883 884 885 886 887 888 889 890 891 892 893 894 895 896
		values[Anum_pg_operator_oprcom - 1] = (Datum) NULL;
		replaces[Anum_pg_operator_oprcom - 1] = ' ';

		/* release the buffer properly */
		if (BufferIsValid(buffer))
			ReleaseBuffer(buffer);

	}

	/* check and update the negator, if necessary */
	opKey[0].sk_argument = ObjectIdGetDatum(negId);

	pg_operator_scan = heap_beginscan(pg_operator_desc,
									  0,
897
									  SnapshotSelf,
898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918
									  1,
									  opKey);

	tup = heap_getnext(pg_operator_scan, 0, &buffer);
	if (HeapTupleIsValid(tup) &&
		!(OidIsValid(((OperatorTupleForm) GETSTRUCT(tup))->oprnegate)))
	{
		values[Anum_pg_operator_oprnegate - 1] = ObjectIdGetDatum(baseId);
		replaces[Anum_pg_operator_oprnegate - 1] = 'r';
		tup = heap_modifytuple(tup,
							   buffer,
							   pg_operator_desc,
							   values,
							   nulls,
							   replaces);

		ItemPointerCopy(&tup->t_ctid, &itemPointerData);

		setheapoverride(true);
		heap_replace(pg_operator_desc, &itemPointerData, tup);
		setheapoverride(false);
919 920 921 922
	}

	/* release the buffer properly */
	if (BufferIsValid(buffer))
923 924 925 926 927
		ReleaseBuffer(buffer);

	heap_endscan(pg_operator_scan);

	heap_close(pg_operator_desc);
928 929 930 931 932 933 934 935
}


/* ----------------------------------------------------------------
 * OperatorCreate
 *
 * Algorithm:
 *
936 937 938 939 940 941 942 943 944 945 946 947
 *	Since the commutator, negator, leftsortoperator, and rightsortoperator
 *	can be defined implicitly through OperatorCreate, must check before
 *	the main operator is added to see if they already exist.  If they
 *	do not already exist, OperatorDef makes a "shell" for each undefined
 *	one, and then OperatorCreate must call OperatorDef again to fill in
 *	each shell.  All this is necessary in order to get the right ObjectId's
 *	filled into the right fields.
 *
 *	The "definedOk" flag indicates that OperatorDef can be called on
 *	the operator even though it already has an entry in the PG_OPERATOR
 *	relation.  This allows shells to be filled in.	The user cannot
 *	forward declare operators, this is strictly an internal capability.
948
 *
949 950 951 952 953 954 955
 *	When the shells are filled in by subsequent calls to OperatorDef,
 *	all the fields are the same as the definition of the original operator
 *	except that the target operator name and the original operatorName
 *	are switched.  In the case of commutator and negator, special flags
 *	are set to indicate their status, telling the executor(?) that
 *	the operands are to be switched, or the outcome of the procedure
 *	negated.
956 957
 *
 * ************************* NOTE NOTE NOTE ******************************
958 959 960 961 962
 *
 *	If the execution of this utility is interrupted, the pg_operator
 *	catalog may be left in an inconsistent state.  Similarly, if
 *	something is removed from the pg_operator, pg_type, or pg_procedure
 *	catalog while this is executing, the results may be inconsistent.
963 964
 * ----------------------------------------------------------------
 *
965 966 967 968 969 970 971 972 973 974 975 976 977 978 979
 * "X" indicates an optional argument (i.e. one that can be NULL)
 *		operatorName;			-- operator name
 *		leftTypeName;			-- X left type name
 *		rightTypeName;			-- X right type name
 *		procedureName;			-- procedure for operator
 *		precedence;				-- operator precedence
 *		isLeftAssociative;		-- operator is left associative
 *		commutatorName;			-- X commutator operator name
 *		negatorName;			-- X negator operator name
 *		restrictionName;		-- X restriction sel. procedure
 *		joinName;				-- X join sel. procedure name
 *		canHash;				-- operator hashes
 *		leftSortName;			-- X left sort operator
 *		rightSortName;			-- X right sort operator
 *
980 981 982
 */
void
OperatorCreate(char *operatorName,
983 984 985 986 987 988 989 990 991 992 993 994
			   char *leftTypeName,
			   char *rightTypeName,
			   char *procedureName,
			   uint16 precedence,
			   bool isLeftAssociative,
			   char *commutatorName,
			   char *negatorName,
			   char *restrictionName,
			   char *joinName,
			   bool canHash,
			   char *leftSortName,
			   char *rightSortName)
995
{
996 997 998 999 1000
	Oid			commObjectId,
				negObjectId;
	Oid			leftSortObjectId,
				rightSortObjectId;
	int			definedOK;
1001 1002

	if (!leftTypeName && !rightTypeName)
B
Bruce Momjian 已提交
1003
		elog(ERROR, "OperatorCreate : at least one of leftarg or rightarg must be defined");
1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122

	/* ----------------
	 *	get the oid's of the operator's associated operators, if possible.
	 * ----------------
	 */
	if (commutatorName)
		commObjectId = OperatorGet(commutatorName,		/* commute type order */
								   rightTypeName,
								   leftTypeName);
	else
		commObjectId = 0;

	if (negatorName)
		negObjectId = OperatorGet(negatorName,
								  leftTypeName,
								  rightTypeName);
	else
		negObjectId = 0;

	if (leftSortName)
		leftSortObjectId = OperatorGet(leftSortName,
									   leftTypeName,
									   rightTypeName);
	else
		leftSortObjectId = 0;

	if (rightSortName)
		rightSortObjectId = OperatorGet(rightSortName,
										rightTypeName,
										leftTypeName);
	else
		rightSortObjectId = 0;

	/* ----------------
	 *	Use OperatorDef() to define the specified operator and
	 *	also create shells for the operator's associated operators
	 *	if they don't already exist.
	 *
	 *	This operator should not be defined yet.
	 * ----------------
	 */
	definedOK = 0;

	OperatorDef(operatorName,
				definedOK,
				leftTypeName,
				rightTypeName,
				procedureName,
				precedence,
				isLeftAssociative,
				commutatorName,
				negatorName,
				restrictionName,
				joinName,
				canHash,
				leftSortName,
				rightSortName);

	/* ----------------
	 *	Now fill in information in the operator's associated
	 *	operators.
	 *
	 *	These operators should be defined or have shells defined.
	 * ----------------
	 */
	definedOK = 1;

	if (!OidIsValid(commObjectId) && commutatorName)
		OperatorDef(commutatorName,
					definedOK,
					leftTypeName,		/* should eventually */
					rightTypeName,		/* commute order */
					procedureName,
					precedence,
					isLeftAssociative,
					operatorName,		/* commutator */
					negatorName,
					restrictionName,
					joinName,
					canHash,
					rightSortName,
					leftSortName);

	if (negatorName && !OidIsValid(negObjectId))
		OperatorDef(negatorName,
					definedOK,
					leftTypeName,
					rightTypeName,
					procedureName,
					precedence,
					isLeftAssociative,
					commutatorName,
					operatorName,		/* negator */
					restrictionName,
					joinName,
					canHash,
					leftSortName,
					rightSortName);

	if (leftSortName && !OidIsValid(leftSortObjectId))
		OperatorDef(leftSortName,
					definedOK,
					leftTypeName,
					rightTypeName,
					procedureName,
					precedence,
					isLeftAssociative,
					commutatorName,
					negatorName,
					restrictionName,
					joinName,
					canHash,
					operatorName,		/* left sort */
					rightSortName);

	if (rightSortName && !OidIsValid(rightSortObjectId))
		OperatorDef(rightSortName,
					definedOK,
					leftTypeName,
1123
					rightTypeName,
1124 1125 1126 1127 1128 1129 1130 1131 1132 1133
					procedureName,
					precedence,
					isLeftAssociative,
					commutatorName,
					negatorName,
					restrictionName,
					joinName,
					canHash,
					leftSortName,
					operatorName);		/* right sort */
1134
}