remove.c 10.7 KB
Newer Older
1 2
/*-------------------------------------------------------------------------
 *
3
 * remove.c
4
 *	  POSTGRES remove (function | type | operator ) utilty code.
5
 *
B
Add:  
Bruce Momjian 已提交
6 7
 * Portions Copyright (c) 1996-2000, PostgreSQL, Inc
 * Portions Copyright (c) 1994, Regents of the University of California
8 9 10
 *
 *
 * IDENTIFICATION
B
Add:  
Bruce Momjian 已提交
11
 *	  $Header: /cvsroot/pgsql/src/backend/commands/Attic/remove.c,v 1.45 2000/01/26 05:56:13 momjian Exp $
12 13 14
 *
 *-------------------------------------------------------------------------
 */
B
Bruce Momjian 已提交
15 16 17 18 19 20 21
#include "postgres.h"

#include "access/heapam.h"
#include "catalog/catname.h"
#include "catalog/pg_language.h"
#include "catalog/pg_proc.h"
#include "catalog/pg_type.h"
B
Hello.  
Bruce Momjian 已提交
22
#include "commands/comment.h"
B
Bruce Momjian 已提交
23 24 25 26 27 28
#include "commands/defrem.h"
#include "miscadmin.h"
#include "parser/parse_func.h"
#include "utils/acl.h"
#include "utils/syscache.h"

29 30

/*
B
Bruce Momjian 已提交
31
 * RemoveOperator
32
 *		Deletes an operator.
33 34
 *
 * Exceptions:
35 36 37 38
 *		BadArg if name is invalid.
 *		BadArg if type1 is invalid.
 *		"WARN" if operator nonexistent.
 *		...
39 40
 */
void
41 42 43
RemoveOperator(char *operatorName,		/* operator name */
			   char *typeName1, /* first type name */
			   char *typeName2) /* optional second type name */
44
{
45 46 47 48 49 50
	Relation	relation;
	HeapTuple	tup;
	Oid			typeId1 = InvalidOid;
	Oid			typeId2 = InvalidOid;
	bool		defined;
	char	   *userName;
51
	char		oprtype;
52

53 54 55 56 57
	if (typeName1)
	{
		typeId1 = TypeGet(typeName1, &defined);
		if (!OidIsValid(typeId1))
		{
58
			elog(ERROR, "RemoveOperator: type '%s' does not exist", typeName1);
59 60
			return;
		}
61
	}
62 63 64 65 66 67

	if (typeName2)
	{
		typeId2 = TypeGet(typeName2, &defined);
		if (!OidIsValid(typeId2))
		{
68
			elog(ERROR, "RemoveOperator: type '%s' does not exist", typeName2);
69 70
			return;
		}
71
	}
72

73 74 75 76 77 78 79
	if (OidIsValid(typeId1) && OidIsValid(typeId2))
		oprtype = 'b';
	else if (OidIsValid(typeId1))
		oprtype = 'l';
	else
		oprtype = 'r';

80 81
	relation = heap_openr(OperatorRelationName, RowExclusiveLock);

82
	tup = SearchSysCacheTupleCopy(OPERNAME,
83 84 85 86 87
								  PointerGetDatum(operatorName),
								  ObjectIdGetDatum(typeId1),
								  ObjectIdGetDatum(typeId2),
								  CharGetDatum(oprtype));

88 89
	if (HeapTupleIsValid(tup))
	{
90
#ifndef NO_SECURITY
91 92
		userName = GetPgUserName();
		if (!pg_ownercheck(userName,
93
						   (char *) ObjectIdGetDatum(tup->t_data->t_oid),
94
						   OPEROID))
95
			elog(ERROR, "RemoveOperator: operator '%s': permission denied",
96
				 operatorName);
97
#endif
B
Hello.  
Bruce Momjian 已提交
98 99 100 101 102 103


		/*** Delete any comments associated with this operator ***/

		DeleteComments(tup->t_data->t_oid);

V
Vadim B. Mikheev 已提交
104
		heap_delete(relation, &tup->t_self, NULL);
B
Hello.  
Bruce Momjian 已提交
105

106
	}
107 108 109 110
	else
	{
		if (OidIsValid(typeId1) && OidIsValid(typeId2))
		{
111
			elog(ERROR, "RemoveOperator: binary operator '%s' taking '%s' and '%s' does not exist",
112 113 114 115 116 117
				 operatorName,
				 typeName1,
				 typeName2);
		}
		else if (OidIsValid(typeId1))
		{
118
			elog(ERROR, "RemoveOperator: right unary operator '%s' taking '%s' does not exist",
119 120 121 122 123
				 operatorName,
				 typeName1);
		}
		else
		{
124
			elog(ERROR, "RemoveOperator: left unary operator '%s' taking '%s' does not exist",
125 126 127 128
				 operatorName,
				 typeName2);
		}
	}
129
	heap_freetuple(tup);
130
	heap_close(relation, RowExclusiveLock);
131 132 133 134 135 136 137 138
}

#ifdef NOTYET
/*
 * this stuff is to support removing all reference to a type
 * don't use it  - pma 2/1/94
 */
/*
139 140
 *	SingleOpOperatorRemove
 *		Removes all operators that have operands or a result of type 'typeOid'.
141 142 143 144
 */
static void
SingleOpOperatorRemove(Oid typeOid)
{
145
	Relation	rel;
146
	ScanKeyData key[3];
147
	HeapScanDesc scan;
148 149
	HeapTuple	tup;
	static		attnums[3] = {7, 8, 9}; /* left, right, return */
150
	int			i;
151 152

	ScanKeyEntryInitialize(&key[0],
153
						   0, 0, F_OIDEQ, (Datum) typeOid);
154
	rel = heap_openr(OperatorRelationName, RowExclusiveLock);
155 156 157
	for (i = 0; i < 3; ++i)
	{
		key[0].sk_attno = attnums[i];
158
		scan = heap_beginscan(rel, 0, SnapshotNow, 1, key);
B
Hello.  
Bruce Momjian 已提交
159 160 161 162 163 164 165 166
		while (HeapTupleIsValid(tup = heap_getnext(scan, 0))) {

		  /*** This is apparently a routine not in use, but remove ***/
		  /*** any comments anyways ***/

		  DeleteComments(tup->t_data->t_oid);

		  heap_delete(rel, &tup->t_self, NULL);
167

B
Hello.  
Bruce Momjian 已提交
168 169
		}

170
		heap_endscan(scan);
171
	}
172
	heap_close(rel, RowExclusiveLock);
173 174 175
}

/*
176 177 178 179
 *	AttributeAndRelationRemove
 *		Removes all entries in the attribute and relation relations
 *		that contain entries of type 'typeOid'.
 *		Currently nothing calls this code, it is untested.
180 181 182 183
 */
static void
AttributeAndRelationRemove(Oid typeOid)
{
184 185
	struct oidlist
	{
186
		Oid			reloid;
187 188 189
		struct oidlist *next;
	};
	struct oidlist *oidptr,
190
			   *optr;
191
	Relation	rel;
192
	ScanKeyData key[1];
193
	HeapScanDesc scan;
194
	HeapTuple	tup;
195 196 197 198 199 200 201 202 203

	/*
	 * 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],
204
						   0, 3, F_OIDEQ, (Datum) typeOid);
205 206 207 208

	oidptr = (struct oidlist *) palloc(sizeof(*oidptr));
	oidptr->next = NULL;
	optr = oidptr;
209
	rel = heap_openr(AttributeRelationName, AccessShareLock);
210 211
	scan = heap_beginscan(rel, 0, SnapshotNow, 1, key);
	while (HeapTupleIsValid(tup = heap_getnext(scan, 0)))
212
	{
213
		optr->reloid = ((Form_pg_attribute) GETSTRUCT(tup))->attrelid;
214 215
		optr->next = (struct oidlist *) palloc(sizeof(*oidptr));
		optr = optr->next;
216
	}
217
	optr->next = NULL;
218
	heap_endscan(scan);
219
	heap_close(rel, AccessShareLock);
220

221
	optr = oidptr;
222 223 224

	ScanKeyEntryInitialize(&key[0], 0,
						   ObjectIdAttributeNumber,
B
Bruce Momjian 已提交
225
						   F_OIDEQ, (Datum) 0);
226 227
	/* get RowExclusiveLock because heap_destroy will need it */
	rel = heap_openr(RelationRelationName, RowExclusiveLock);
228 229 230
	while (PointerIsValid((char *) optr->next))
	{
		key[0].sk_argument = (Datum) (optr++)->reloid;
231 232 233
		scan = heap_beginscan(rel, 0, SnapshotNow, 1, key);
		tup = heap_getnext(scan, 0);
		if (HeapTupleIsValid(tup))
234
		{
235
			char	   *name;
236

237
			name = NameStr(((Form_pg_class) GETSTRUCT(tup))->relname);
238
			heap_drop_with_catalog(name);
239
		}
240
		heap_endscan(scan);
241
	}
242
	heap_close(rel, RowExclusiveLock);
243
}
244

245
#endif	 /* NOTYET */
246 247

/*
248 249 250
 *	TypeRemove
 *		Removes the type 'typeName' and all attributes and relations that
 *		use it.
251 252
 */
void
253
RemoveType(char *typeName)		/* type name to be removed */
254
{
255 256 257 258 259
	Relation	relation;
	HeapTuple	tup;
	Oid			typeOid;
	char	   *shadow_type;
	char	   *userName;
260

261
#ifndef NO_SECURITY
262
	userName = GetPgUserName();
263
	if (!pg_ownercheck(userName, typeName, TYPENAME))
264
		elog(ERROR, "RemoveType: type '%s': permission denied",
265
			 typeName);
266
#endif
267

268 269
	relation = heap_openr(TypeRelationName, RowExclusiveLock);

270
	tup = SearchSysCacheTuple(TYPENAME,
271 272
							  PointerGetDatum(typeName),
							  0, 0, 0);
273 274
	if (!HeapTupleIsValid(tup))
	{
275
		heap_close(relation, RowExclusiveLock);
276
		elog(ERROR, "RemoveType: type '%s' does not exist", typeName);
277
	}
278

279
	typeOid = tup->t_data->t_oid;
B
Hello.  
Bruce Momjian 已提交
280 281 282 283 284

	/*** Delete any comments associated with this type ***/

	DeleteComments(typeOid);

V
Vadim B. Mikheev 已提交
285
	heap_delete(relation, &tup->t_self, NULL);
286 287 288

	/* Now, Delete the "array of" that type */
	shadow_type = makeArrayTypeName(typeName);
289
	tup = SearchSysCacheTuple(TYPENAME,
290 291
							  PointerGetDatum(shadow_type),
							  0, 0, 0);
292
	if (!HeapTupleIsValid(tup))
293
	{
294 295
		heap_close(relation, RowExclusiveLock);
		elog(ERROR, "RemoveType: type '%s' does not exist", shadow_type);
296
	}
297

V
Vadim B. Mikheev 已提交
298
	heap_delete(relation, &tup->t_self, NULL);
299

300
	heap_close(relation, RowExclusiveLock);
301 302 303
}

/*
B
Bruce Momjian 已提交
304
 * RemoveFunction
305
 *		Deletes a function.
306 307
 *
 * Exceptions:
308 309 310
 *		BadArg if name is invalid.
 *		"WARN" if function nonexistent.
 *		...
311 312
 */
void
313 314
RemoveFunction(char *functionName,		/* function name to be removed */
			   int nargs,
315
			   List *argNameList /* list of TypeNames */ )
316
{
317 318
	Relation	relation;
	HeapTuple	tup;
319
	Oid			argList[FUNC_MAX_ARGS];
320 321 322
	char	   *userName;
	char	   *typename;
	int			i;
323

324 325 326
	if (nargs > FUNC_MAX_ARGS)
		elog(ERROR, "functions cannot have more than %d arguments",
			 FUNC_MAX_ARGS);
327
	MemSet(argList, 0, FUNC_MAX_ARGS * sizeof(Oid));
328 329 330 331 332 333 334 335 336
	for (i = 0; i < nargs; i++)
	{
		typename = strVal(lfirst(argNameList));
		argNameList = lnext(argNameList);

		if (strcmp(typename, "opaque") == 0)
			argList[i] = 0;
		else
		{
337
			tup = SearchSysCacheTuple(TYPENAME,
338
									  PointerGetDatum(typename),
339 340 341
									  0, 0, 0);

			if (!HeapTupleIsValid(tup))
342
				elog(ERROR, "RemoveFunction: type '%s' not found", typename);
343
			argList[i] = tup->t_data->t_oid;
344
		}
345
	}
346

347
#ifndef NO_SECURITY
348 349 350
	userName = GetPgUserName();
	if (!pg_func_ownercheck(userName, functionName, nargs, argList))
	{
351
		elog(ERROR, "RemoveFunction: function '%s': permission denied",
352
			 functionName);
353
	}
354 355
#endif

356
	relation = heap_openr(ProcedureRelationName, RowExclusiveLock);
357
	tup = SearchSysCacheTuple(PROCNAME,
358 359 360 361
							  PointerGetDatum(functionName),
							  Int32GetDatum(nargs),
							  PointerGetDatum(argList),
							  0);
362

363
	if (!HeapTupleIsValid(tup))
364
	{
365
		heap_close(relation, RowExclusiveLock);
366
		func_error("RemoveFunction", functionName, nargs, argList, NULL);
367
	}
368

369 370
	if ((((Form_pg_proc) GETSTRUCT(tup))->prolang) == INTERNALlanguageId)
	{
371
		heap_close(relation, RowExclusiveLock);
372
		elog(ERROR, "RemoveFunction: function \"%s\" is built-in", functionName);
373
	}
374

B
Hello.  
Bruce Momjian 已提交
375 376 377 378
	/*** Delete any comments associated with this function ***/

	DeleteComments(tup->t_data->t_oid);

V
Vadim B. Mikheev 已提交
379
	heap_delete(relation, &tup->t_self, NULL);
380

381
	heap_close(relation, RowExclusiveLock);
382 383 384
}

void
385
RemoveAggregate(char *aggName, char *aggType)
386
{
387 388 389 390 391
	Relation	relation;
	HeapTuple	tup;
	char	   *userName;
	Oid			basetypeID = InvalidOid;
	bool		defined;
392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407


	/*
	 * 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))
408
			elog(ERROR, "RemoveAggregate: type '%s' does not exist", aggType);
409 410 411
	}
	else
		basetypeID = 0;
412 413

#ifndef NO_SECURITY
414 415 416 417 418
	userName = GetPgUserName();
	if (!pg_aggr_ownercheck(userName, aggName, basetypeID))
	{
		if (aggType)
		{
419
			elog(ERROR, "RemoveAggregate: aggregate '%s' on type '%s': permission denied",
420 421 422 423
				 aggName, aggType);
		}
		else
		{
424
			elog(ERROR, "RemoveAggregate: aggregate '%s': permission denied",
425 426 427
				 aggName);
		}
	}
428
#endif
429

430
	relation = heap_openr(AggregateRelationName, RowExclusiveLock);
431
	tup = SearchSysCacheTuple(AGGNAME,
432 433 434
							  PointerGetDatum(aggName),
							  ObjectIdGetDatum(basetypeID),
							  0, 0);
435

436 437
	if (!HeapTupleIsValid(tup))
	{
438
		heap_close(relation, RowExclusiveLock);
439 440
		if (aggType)
		{
441
			elog(ERROR, "RemoveAggregate: aggregate '%s' for '%s' does not exist",
442 443 444 445
				 aggName, aggType);
		}
		else
		{
446
			elog(ERROR, "RemoveAggregate: aggregate '%s' for all types does not exist",
447 448 449
				 aggName);
		}
	}
450

B
Hello.  
Bruce Momjian 已提交
451 452 453 454
	/*** Remove any comments related to this aggregate ***/

	DeleteComments(tup->t_data->t_oid);

V
Vadim B. Mikheev 已提交
455
	heap_delete(relation, &tup->t_self, NULL);
456

457
	heap_close(relation, RowExclusiveLock);
458
}