remove.c 12.6 KB
Newer Older
1 2 3
/*-------------------------------------------------------------------------
 *
 * remove.c--
4
 *	  POSTGRES remove (function | type | operator ) utilty code.
5 6 7 8 9
 *
 * Copyright (c) 1994, Regents of the University of California
 *
 *
 * IDENTIFICATION
B
Bruce Momjian 已提交
10
 *	  $Header: /cvsroot/pgsql/src/backend/commands/Attic/remove.c,v 1.13 1997/09/08 20:55:27 momjian Exp $
11 12 13
 *
 *-------------------------------------------------------------------------
 */
M
Marc G. Fournier 已提交
14
#include <postgres.h>
15

M
Marc G. Fournier 已提交
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
#include <utils/acl.h>
#include <access/heapam.h>
#include <utils/builtins.h>
#include <utils/syscache.h>
#include <catalog/catname.h>
#include <commands/defrem.h>
#include <miscadmin.h>
#include <catalog/pg_aggregate.h>
#include <catalog/pg_language.h>
#include <catalog/pg_operator.h>
#include <catalog/pg_proc.h>
#include <parser/catalog_utils.h>
#include <storage/bufmgr.h>
#include <fmgr.h>
#ifndef HAVE_MEMMOVE
31
#include <regex/utils.h>
M
Marc G. Fournier 已提交
32
#else
33
#include <string.h>
M
Marc G. Fournier 已提交
34
#endif
35 36 37

/*
 * RemoveOperator --
38
 *		Deletes an operator.
39 40
 *
 * Exceptions:
41 42 43 44
 *		BadArg if name is invalid.
 *		BadArg if type1 is invalid.
 *		"WARN" if operator nonexistent.
 *		...
45 46
 */
void
47 48 49
RemoveOperator(char *operatorName,		/* operator name */
			   char *typeName1, /* first type name */
			   char *typeName2) /* optional second type name */
50
{
51 52 53 54 55 56
	Relation	relation;
	HeapScanDesc scan;
	HeapTuple	tup;
	Oid			typeId1 = InvalidOid;
	Oid			typeId2 = InvalidOid;
	bool		defined;
57
	ItemPointerData itemPointerData;
58 59 60
	Buffer		buffer;
	ScanKeyData operatorKey[3];
	char	   *userName;
61 62 63 64 65 66 67 68 69

	if (typeName1)
	{
		typeId1 = TypeGet(typeName1, &defined);
		if (!OidIsValid(typeId1))
		{
			elog(WARN, "RemoveOperator: type '%s' does not exist", typeName1);
			return;
		}
70
	}
71 72 73 74 75 76 77 78 79

	if (typeName2)
	{
		typeId2 = TypeGet(typeName2, &defined);
		if (!OidIsValid(typeId2))
		{
			elog(WARN, "RemoveOperator: type '%s' does not exist", typeName2);
			return;
		}
80
	}
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101

	ScanKeyEntryInitialize(&operatorKey[0], 0x0,
						   Anum_pg_operator_oprname,
						   NameEqualRegProcedure,
						   PointerGetDatum(operatorName));

	ScanKeyEntryInitialize(&operatorKey[1], 0x0,
						   Anum_pg_operator_oprleft,
						   ObjectIdEqualRegProcedure,
						   ObjectIdGetDatum(typeId1));

	ScanKeyEntryInitialize(&operatorKey[2], 0x0,
						   Anum_pg_operator_oprright,
						   ObjectIdEqualRegProcedure,
						   ObjectIdGetDatum(typeId2));

	relation = heap_openr(OperatorRelationName);
	scan = heap_beginscan(relation, 0, NowTimeQual, 3, operatorKey);
	tup = heap_getnext(scan, 0, &buffer);
	if (HeapTupleIsValid(tup))
	{
102
#ifndef NO_SECURITY
103 104 105 106 107 108
		userName = GetPgUserName();
		if (!pg_ownercheck(userName,
						   (char *) ObjectIdGetDatum(tup->t_oid),
						   OPROID))
			elog(WARN, "RemoveOperator: operator '%s': permission denied",
				 operatorName);
109
#endif
110 111
		ItemPointerCopy(&tup->t_ctid, &itemPointerData);
		heap_delete(relation, &itemPointerData);
112
	}
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
	else
	{
		if (OidIsValid(typeId1) && OidIsValid(typeId2))
		{
			elog(WARN, "RemoveOperator: binary operator '%s' taking '%s' and '%s' does not exist",
				 operatorName,
				 typeName1,
				 typeName2);
		}
		else if (OidIsValid(typeId1))
		{
			elog(WARN, "RemoveOperator: right unary operator '%s' taking '%s' does not exist",
				 operatorName,
				 typeName1);
		}
		else
		{
			elog(WARN, "RemoveOperator: left unary operator '%s' taking '%s' does not exist",
				 operatorName,
				 typeName2);
		}
	}
	heap_endscan(scan);
	heap_close(relation);
137 138 139 140 141 142 143 144
}

#ifdef NOTYET
/*
 * this stuff is to support removing all reference to a type
 * don't use it  - pma 2/1/94
 */
/*
145 146
 *	SingleOpOperatorRemove
 *		Removes all operators that have operands or a result of type 'typeOid'.
147 148 149 150
 */
static void
SingleOpOperatorRemove(Oid typeOid)
{
151 152 153 154
	Relation	rdesc;
	ScanKeyData key[3];
	HeapScanDesc sdesc;
	HeapTuple	tup;
155
	ItemPointerData itemPointerData;
156 157 158
	Buffer		buffer;
	static		attnums[3] = {7, 8, 9}; /* left, right, return */
	register	i;
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173

	ScanKeyEntryInitialize(&key[0],
					   0, 0, ObjectIdEqualRegProcedure, (Datum) typeOid);
	rdesc = heap_openr(OperatorRelationName);
	for (i = 0; i < 3; ++i)
	{
		key[0].sk_attno = attnums[i];
		sdesc = heap_beginscan(rdesc, 0, NowTimeQual, 1, key);
		while (PointerIsValid(tup = heap_getnext(sdesc, 0, &buffer)))
		{
			ItemPointerCopy(&tup->t_ctid, &itemPointerData);
			/* XXX LOCK not being passed */
			heap_delete(rdesc, &itemPointerData);
		}
		heap_endscan(sdesc);
174
	}
175
	heap_close(rdesc);
176 177 178
}

/*
179 180 181 182
 *	AttributeAndRelationRemove
 *		Removes all entries in the attribute and relation relations
 *		that contain entries of type 'typeOid'.
 *		Currently nothing calls this code, it is untested.
183 184 185 186
 */
static void
AttributeAndRelationRemove(Oid typeOid)
{
187 188
	struct oidlist
	{
189
		Oid			reloid;
190 191 192
		struct oidlist *next;
	};
	struct oidlist *oidptr,
193 194 195 196 197
			   *optr;
	Relation	rdesc;
	ScanKeyData key[1];
	HeapScanDesc sdesc;
	HeapTuple	tup;
198
	ItemPointerData itemPointerData;
199
	Buffer		buffer;
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214

	/*
	 * Get the oid's of the relations to be removed by scanning the entire
	 * attribute relation. We don't need to remove the attributes here,
	 * because amdestroy will remove all attributes of the relation. XXX
	 * should check for duplicate relations
	 */

	ScanKeyEntryInitialize(&key[0],
					   0, 3, ObjectIdEqualRegProcedure, (Datum) typeOid);

	oidptr = (struct oidlist *) palloc(sizeof(*oidptr));
	oidptr->next = NULL;
	optr = oidptr;
	rdesc = heap_openr(AttributeRelationName);
215
	sdesc = heap_beginscan(rdesc, 0, NowTimeQual, 1, key);
216 217 218 219 220 221
	while (PointerIsValid(tup = heap_getnext(sdesc, 0, &buffer)))
	{
		ItemPointerCopy(&tup->t_ctid, &itemPointerData);
		optr->reloid = ((AttributeTupleForm) GETSTRUCT(tup))->attrelid;
		optr->next = (struct oidlist *) palloc(sizeof(*oidptr));
		optr = optr->next;
222
	}
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
	optr->next = NULL;
	heap_endscan(sdesc);
	heap_close(rdesc);


	ScanKeyEntryInitialize(&key[0], 0,
						   ObjectIdAttributeNumber,
						   ObjectIdEqualRegProcedure, (Datum) 0);
	optr = oidptr;
	rdesc = heap_openr(RelationRelationName);
	while (PointerIsValid((char *) optr->next))
	{
		key[0].sk_argument = (Datum) (optr++)->reloid;
		sdesc = heap_beginscan(rdesc, 0, NowTimeQual, 1, key);
		tup = heap_getnext(sdesc, 0, &buffer);
		if (PointerIsValid(tup))
		{
240
			char	   *name;
241 242 243 244 245 246 247

			name = (((Form_pg_class) GETSTRUCT(tup))->relname).data;
			heap_destroy(name);
		}
	}
	heap_endscan(sdesc);
	heap_close(rdesc);
248
}
249 250

#endif							/* NOTYET */
251 252

/*
253 254 255
 *	TypeRemove
 *		Removes the type 'typeName' and all attributes and relations that
 *		use it.
256 257
 */
void
258
RemoveType(char *typeName)		/* type name to be removed */
259
{
260 261 262 263
	Relation	relation;
	HeapScanDesc scan;
	HeapTuple	tup;
	Oid			typeOid;
264 265 266 267
	ItemPointerData itemPointerData;
	static ScanKeyData typeKey[1] = {
		{0, Anum_pg_type_typname, NameEqualRegProcedure}
	};
268 269
	char	   *shadow_type;
	char	   *userName;
270

271
#ifndef NO_SECURITY
272 273 274 275
	userName = GetPgUserName();
	if (!pg_ownercheck(userName, typeName, TYPNAME))
		elog(WARN, "RemoveType: type '%s': permission denied",
			 typeName);
276
#endif
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297

	relation = heap_openr(TypeRelationName);
	fmgr_info(typeKey[0].sk_procedure, &typeKey[0].sk_func,
			  &typeKey[0].sk_nargs);

	/* Delete the primary type */

	typeKey[0].sk_argument = PointerGetDatum(typeName);

	scan = heap_beginscan(relation, 0, NowTimeQual, 1, typeKey);
	tup = heap_getnext(scan, 0, (Buffer *) 0);
	if (!HeapTupleIsValid(tup))
	{
		heap_endscan(scan);
		heap_close(relation);
		elog(WARN, "RemoveType: type '%s' does not exist",
			 typeName);
	}
	typeOid = tup->t_oid;
	ItemPointerCopy(&tup->t_ctid, &itemPointerData);
	heap_delete(relation, &itemPointerData);
298
	heap_endscan(scan);
299 300 301 302 303 304 305 306 307 308

	/* Now, Delete the "array of" that type */
	shadow_type = makeArrayTypeName(typeName);
	typeKey[0].sk_argument = NameGetDatum(shadow_type);

	scan = heap_beginscan(relation, 0, NowTimeQual,
						  1, (ScanKey) typeKey);
	tup = heap_getnext(scan, 0, (Buffer *) 0);

	if (!HeapTupleIsValid(tup))
309
	{
310 311
		elog(WARN, "RemoveType: type '%s': array stub not found",
			 typeName);
312
	}
313 314 315 316 317 318
	typeOid = tup->t_oid;
	ItemPointerCopy(&tup->t_ctid, &itemPointerData);
	heap_delete(relation, &itemPointerData);
	heap_endscan(scan);

	heap_close(relation);
319 320 321 322
}

/*
 * RemoveFunction --
323
 *		Deletes a function.
324 325
 *
 * Exceptions:
326 327 328
 *		BadArg if name is invalid.
 *		"WARN" if function nonexistent.
 *		...
329 330
 */
void
331 332 333
RemoveFunction(char *functionName,		/* function name to be removed */
			   int nargs,
			   List * argNameList /* list of TypeNames */ )
334
{
335 336 337 338 339 340 341
	Relation	relation;
	HeapScanDesc scan;
	HeapTuple	tup;
	Buffer		buffer = InvalidBuffer;
	bool		bufferUsed = FALSE;
	Oid			argList[8];
	Form_pg_proc the_proc = NULL;
342 343 344 345
	ItemPointerData itemPointerData;
	static ScanKeyData key[3] = {
		{0, Anum_pg_proc_proname, NameEqualRegProcedure}
	};
346 347 348
	char	   *userName;
	char	   *typename;
	int			i;
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369

	memset(argList, 0, 8 * sizeof(Oid));
	for (i = 0; i < nargs; i++)
	{
/*		typename = ((TypeName*)(lfirst(argNameList)))->name; */
		typename = strVal(lfirst(argNameList));
		argNameList = lnext(argNameList);

		if (strcmp(typename, "opaque") == 0)
			argList[i] = 0;
		else
		{
			tup = SearchSysCacheTuple(TYPNAME, PointerGetDatum(typename),
									  0, 0, 0);

			if (!HeapTupleIsValid(tup))
			{
				elog(WARN, "RemoveFunction: type '%s' not found", typename);
			}
			argList[i] = tup->t_oid;
		}
370
	}
371 372 373 374 375 376 377

	tup = SearchSysCacheTuple(PRONAME, PointerGetDatum(functionName),
							  Int32GetDatum(nargs),
							  PointerGetDatum(argList), 0);
	if (!HeapTupleIsValid(tup))
		func_error("RemoveFunction", functionName, nargs, argList);

378
#ifndef NO_SECURITY
379 380 381 382 383
	userName = GetPgUserName();
	if (!pg_func_ownercheck(userName, functionName, nargs, argList))
	{
		elog(WARN, "RemoveFunction: function '%s': permission denied",
			 functionName);
384
	}
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400
#endif

	key[0].sk_argument = PointerGetDatum(functionName);

	fmgr_info(key[0].sk_procedure, &key[0].sk_func, &key[0].sk_nargs);

	relation = heap_openr(ProcedureRelationName);
	scan = heap_beginscan(relation, 0, NowTimeQual, 1, key);

	do
	{							/* hope this is ok because it's indexed */
		if (bufferUsed)
		{
			ReleaseBuffer(buffer);
			bufferUsed = FALSE;
		}
B
Bruce Momjian 已提交
401
		tup = heap_getnext(scan, 0, (Buffer *) &buffer);
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
		if (!HeapTupleIsValid(tup))
			break;
		bufferUsed = TRUE;
		the_proc = (Form_pg_proc) GETSTRUCT(tup);
	} while ((namestrcmp(&(the_proc->proname), functionName) == 0) &&
			 (the_proc->pronargs != nargs ||
			  !oid8eq(&(the_proc->proargtypes[0]), &argList[0])));


	if (!HeapTupleIsValid(tup) || namestrcmp(&(the_proc->proname),
											 functionName) != 0)
	{
		heap_endscan(scan);
		heap_close(relation);
		func_error("RemoveFunction", functionName, nargs, argList);
417
	}
418 419 420 421 422 423 424 425 426 427 428

	/* ok, function has been found */

	if (the_proc->prolang == INTERNALlanguageId)
		elog(WARN, "RemoveFunction: function \"%s\" is built-in",
			 functionName);

	ItemPointerCopy(&tup->t_ctid, &itemPointerData);
	heap_delete(relation, &itemPointerData);
	heap_endscan(scan);
	heap_close(relation);
429 430 431
}

void
432
RemoveAggregate(char *aggName, char *aggType)
433
{
434 435 436
	Relation	relation;
	HeapScanDesc scan;
	HeapTuple	tup;
437
	ItemPointerData itemPointerData;
438 439 440 441
	char	   *userName;
	Oid			basetypeID = InvalidOid;
	bool		defined;
	ScanKeyData aggregateKey[3];
442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465


	/*
	 * if a basetype is passed in, then attempt to find an aggregate for
	 * that specific type.
	 *
	 * else if the basetype is blank, then attempt to find an aggregate with
	 * a basetype of zero.	This is valid. It means that the aggregate is
	 * to apply to all basetypes.  ie, a counter of some sort.
	 *
	 */

	if (aggType)
	{
		basetypeID = TypeGet(aggType, &defined);
		if (!OidIsValid(basetypeID))
		{
			elog(WARN, "RemoveAggregate: type '%s' does not exist", aggType);
		}
	}
	else
	{
		basetypeID = 0;
	}
466 467 468 469

/*
#ifndef NO_SECURITY
*/
470 471 472 473 474 475 476 477 478 479 480 481 482 483
	userName = GetPgUserName();
	if (!pg_aggr_ownercheck(userName, aggName, basetypeID))
	{
		if (aggType)
		{
			elog(WARN, "RemoveAggregate: aggregate '%s' on type '%s': permission denied",
				 aggName, aggType);
		}
		else
		{
			elog(WARN, "RemoveAggregate: aggregate '%s': permission denied",
				 aggName);
		}
	}
484 485 486 487
/*
#endif
*/

488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519
	ScanKeyEntryInitialize(&aggregateKey[0], 0x0,
						   Anum_pg_aggregate_aggname,
						   NameEqualRegProcedure,
						   PointerGetDatum(aggName));

	ScanKeyEntryInitialize(&aggregateKey[1], 0x0,
						   Anum_pg_aggregate_aggbasetype,
						   ObjectIdEqualRegProcedure,
						   ObjectIdGetDatum(basetypeID));

	relation = heap_openr(AggregateRelationName);
	scan = heap_beginscan(relation, 0, NowTimeQual, 2, aggregateKey);
	tup = heap_getnext(scan, 0, (Buffer *) 0);
	if (!HeapTupleIsValid(tup))
	{
		heap_endscan(scan);
		heap_close(relation);
		if (aggType)
		{
			elog(WARN, "RemoveAggregate: aggregate '%s' for '%s' does not exist",
				 aggName, aggType);
		}
		else
		{
			elog(WARN, "RemoveAggregate: aggregate '%s' for all types does not exist",
				 aggName);
		}
	}
	ItemPointerCopy(&tup->t_ctid, &itemPointerData);
	heap_delete(relation, &itemPointerData);
	heap_endscan(scan);
	heap_close(relation);
520
}