alter.c 14.9 KB
Newer Older
1 2 3 4 5
/*-------------------------------------------------------------------------
 *
 * alter.c
 *	  Drivers for generic alter commands
 *
B
Bruce Momjian 已提交
6
 * Portions Copyright (c) 1996-2011, PostgreSQL Global Development Group
7 8 9 10
 * Portions Copyright (c) 1994, Regents of the University of California
 *
 *
 * IDENTIFICATION
11
 *	  src/backend/commands/alter.c
12 13 14 15 16
 *
 *-------------------------------------------------------------------------
 */
#include "postgres.h"

17 18
#include "catalog/dependency.h"
#include "catalog/indexing.h"
19
#include "catalog/namespace.h"
20
#include "catalog/pg_largeobject.h"
21
#include "catalog/pg_namespace.h"
22
#include "commands/alter.h"
P
Peter Eisentraut 已提交
23
#include "commands/collationcmds.h"
24 25 26
#include "commands/conversioncmds.h"
#include "commands/dbcommands.h"
#include "commands/defrem.h"
27
#include "commands/extension.h"
28 29 30
#include "commands/proclang.h"
#include "commands/schemacmds.h"
#include "commands/tablecmds.h"
31
#include "commands/tablespace.h"
32
#include "commands/trigger.h"
33
#include "commands/typecmds.h"
34 35
#include "commands/user.h"
#include "miscadmin.h"
36
#include "tcop/utility.h"
37
#include "utils/builtins.h"
38
#include "utils/lsyscache.h"
39
#include "utils/rel.h"
40
#include "utils/syscache.h"
41 42


43
/*
B
Bruce Momjian 已提交
44
 * Executes an ALTER OBJECT / RENAME TO statement.	Based on the object
45 46
 * type, the function appropriate to that type is executed.
 */
47 48 49 50 51 52
void
ExecRenameStmt(RenameStmt *stmt)
{
	switch (stmt->renameType)
	{
		case OBJECT_AGGREGATE:
T
Tom Lane 已提交
53
			RenameAggregate(stmt->object, stmt->objarg, stmt->newname);
54 55
			break;

P
Peter Eisentraut 已提交
56 57 58 59
		case OBJECT_COLLATION:
			RenameCollation(stmt->object, stmt->newname);
			break;

60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
		case OBJECT_CONVERSION:
			RenameConversion(stmt->object, stmt->newname);
			break;

		case OBJECT_DATABASE:
			RenameDatabase(stmt->subname, stmt->newname);
			break;

		case OBJECT_FUNCTION:
			RenameFunction(stmt->object, stmt->objarg, stmt->newname);
			break;

		case OBJECT_LANGUAGE:
			RenameLanguage(stmt->subname, stmt->newname);
			break;

		case OBJECT_OPCLASS:
			RenameOpClass(stmt->object, stmt->subname, stmt->newname);
			break;

80 81 82 83
		case OBJECT_OPFAMILY:
			RenameOpFamily(stmt->object, stmt->subname, stmt->newname);
			break;

84 85 86 87
		case OBJECT_ROLE:
			RenameRole(stmt->subname, stmt->newname);
			break;

88 89 90 91
		case OBJECT_SCHEMA:
			RenameSchema(stmt->subname, stmt->newname);
			break;

92 93 94 95
		case OBJECT_TABLESPACE:
			RenameTableSpace(stmt->subname, stmt->newname);
			break;

96
		case OBJECT_TABLE:
97 98
		case OBJECT_SEQUENCE:
		case OBJECT_VIEW:
T
Tom Lane 已提交
99
		case OBJECT_INDEX:
100
		case OBJECT_COLUMN:
101
		case OBJECT_ATTRIBUTE:
102
		case OBJECT_TRIGGER:
R
Robert Haas 已提交
103
		case OBJECT_FOREIGN_TABLE:
B
Bruce Momjian 已提交
104 105
			{
				Oid			relid;
106

B
Bruce Momjian 已提交
107
				CheckRelationOwnership(stmt->relation, true);
108

109 110 111 112 113 114
				/*
				 * Lock level used here should match what will be taken later,
				 * in RenameRelation, renameatt, or renametrig.
				 */
				relid = RangeVarGetRelid(stmt->relation, AccessExclusiveLock,
										 false, false);
115

B
Bruce Momjian 已提交
116
				switch (stmt->renameType)
117
				{
B
Bruce Momjian 已提交
118
					case OBJECT_TABLE:
119 120
					case OBJECT_SEQUENCE:
					case OBJECT_VIEW:
T
Tom Lane 已提交
121
					case OBJECT_INDEX:
R
Robert Haas 已提交
122
					case OBJECT_FOREIGN_TABLE:
B
Bruce Momjian 已提交
123 124 125
						{
							/*
							 * RENAME TABLE requires that we (still) hold
B
Bruce Momjian 已提交
126 127
							 * CREATE rights on the containing namespace, as
							 * well as ownership of the table.
B
Bruce Momjian 已提交
128 129 130 131 132
							 */
							Oid			namespaceId = get_rel_namespace(relid);
							AclResult	aclresult;

							aclresult = pg_namespace_aclcheck(namespaceId,
133 134
															  GetUserId(),
															  ACL_CREATE);
B
Bruce Momjian 已提交
135 136
							if (aclresult != ACLCHECK_OK)
								aclcheck_error(aclresult, ACL_KIND_NAMESPACE,
B
Bruce Momjian 已提交
137
											get_namespace_name(namespaceId));
B
Bruce Momjian 已提交
138

139
							RenameRelation(relid, stmt->newname, stmt->renameType);
B
Bruce Momjian 已提交
140 141 142
							break;
						}
					case OBJECT_COLUMN:
143
					case OBJECT_ATTRIBUTE:
144
						renameatt(relid, stmt);
B
Bruce Momjian 已提交
145 146 147 148 149 150 151 152 153 154
						break;
					case OBJECT_TRIGGER:
						renametrig(relid,
								   stmt->subname,		/* old att name */
								   stmt->newname);		/* new att name */
						break;
					default:
						 /* can't happen */ ;
				}
				break;
155 156
			}

157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
		case OBJECT_TSPARSER:
			RenameTSParser(stmt->object, stmt->newname);
			break;

		case OBJECT_TSDICTIONARY:
			RenameTSDictionary(stmt->object, stmt->newname);
			break;

		case OBJECT_TSTEMPLATE:
			RenameTSTemplate(stmt->object, stmt->newname);
			break;

		case OBJECT_TSCONFIGURATION:
			RenameTSConfiguration(stmt->object, stmt->newname);
			break;

173 174 175 176
		case OBJECT_TYPE:
			RenameType(stmt->object, stmt->newname);
			break;

177
		default:
178 179
			elog(ERROR, "unrecognized rename stmt type: %d",
				 (int) stmt->renameType);
180 181
	}
}
182

183 184 185 186 187 188 189 190 191 192
/*
 * Executes an ALTER OBJECT / SET SCHEMA statement.  Based on the object
 * type, the function appropriate to that type is executed.
 */
void
ExecAlterObjectSchemaStmt(AlterObjectSchemaStmt *stmt)
{
	switch (stmt->objectType)
	{
		case OBJECT_AGGREGATE:
T
Tom Lane 已提交
193 194 195 196
			AlterFunctionNamespace(stmt->object, stmt->objarg, true,
								   stmt->newschema);
			break;

P
Peter Eisentraut 已提交
197 198 199 200
		case OBJECT_COLLATION:
			AlterCollationNamespace(stmt->object, stmt->newschema);
			break;

201 202 203 204
		case OBJECT_CONVERSION:
			AlterConversionNamespace(stmt->object, stmt->newschema);
			break;

205 206 207 208
		case OBJECT_EXTENSION:
			AlterExtensionNamespace(stmt->object, stmt->newschema);
			break;

209
		case OBJECT_FUNCTION:
T
Tom Lane 已提交
210
			AlterFunctionNamespace(stmt->object, stmt->objarg, false,
211 212
								   stmt->newschema);
			break;
B
Bruce Momjian 已提交
213

214 215 216 217 218
		case OBJECT_OPERATOR:
			AlterOperatorNamespace(stmt->object, stmt->objarg, stmt->newschema);
			break;

		case OBJECT_OPCLASS:
219
			AlterOpClassNamespace(stmt->object, stmt->addname, stmt->newschema);
220 221 222
			break;

		case OBJECT_OPFAMILY:
223
			AlterOpFamilyNamespace(stmt->object, stmt->addname, stmt->newschema);
224 225
			break;

226 227
		case OBJECT_SEQUENCE:
		case OBJECT_TABLE:
228
		case OBJECT_VIEW:
R
Robert Haas 已提交
229
		case OBJECT_FOREIGN_TABLE:
230
			CheckRelationOwnership(stmt->relation, true);
231
			AlterTableNamespace(stmt->relation, stmt->newschema,
232
								stmt->objectType, AccessExclusiveLock);
233
			break;
B
Bruce Momjian 已提交
234

235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
		case OBJECT_TSPARSER:
			AlterTSParserNamespace(stmt->object, stmt->newschema);
			break;

		case OBJECT_TSDICTIONARY:
			AlterTSDictionaryNamespace(stmt->object, stmt->newschema);
			break;

		case OBJECT_TSTEMPLATE:
			AlterTSTemplateNamespace(stmt->object, stmt->newschema);
			break;

		case OBJECT_TSCONFIGURATION:
			AlterTSConfigurationNamespace(stmt->object, stmt->newschema);
			break;

251 252 253 254
		case OBJECT_TYPE:
		case OBJECT_DOMAIN:
			AlterTypeNamespace(stmt->object, stmt->newschema);
			break;
B
Bruce Momjian 已提交
255

256 257 258 259 260 261
		default:
			elog(ERROR, "unrecognized AlterObjectSchemaStmt type: %d",
				 (int) stmt->objectType);
	}
}

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
/*
 * Change an object's namespace given its classOid and object Oid.
 *
 * Objects that don't have a namespace should be ignored.
 *
 * This function is currently used only by ALTER EXTENSION SET SCHEMA,
 * so it only needs to cover object types that can be members of an
 * extension, and it doesn't have to deal with certain special cases
 * such as not wanting to process array types --- those should never
 * be direct members of an extension anyway.
 *
 * Returns the OID of the object's previous namespace, or InvalidOid if
 * object doesn't have a schema.
 */
Oid
AlterObjectNamespace_oid(Oid classId, Oid objid, Oid nspOid)
{
	Oid			oldNspOid = InvalidOid;
	ObjectAddress dep;

	dep.classId = classId;
	dep.objectId = objid;
	dep.objectSubId = 0;

	switch (getObjectClass(&dep))
	{
		case OCLASS_CLASS:
289 290 291
			{
				Relation	rel;
				Relation	classRel;
292

293 294
				rel = relation_open(objid, AccessExclusiveLock);
				oldNspOid = RelationGetNamespace(rel);
295

296
				classRel = heap_open(RelationRelationId, RowExclusiveLock);
297

298 299 300 301 302
				AlterRelationNamespaceInternal(classRel,
											   objid,
											   oldNspOid,
											   nspOid,
											   true);
303

304
				heap_close(classRel, RowExclusiveLock);
305

306 307 308
				relation_close(rel, NoLock);
				break;
			}
309 310 311 312 313 314 315 316 317

		case OCLASS_PROC:
			oldNspOid = AlterFunctionNamespace_oid(objid, nspOid);
			break;

		case OCLASS_TYPE:
			oldNspOid = AlterTypeNamespace_oid(objid, nspOid);
			break;

P
Peter Eisentraut 已提交
318 319 320 321
		case OCLASS_COLLATION:
			oldNspOid = AlterCollationNamespace_oid(objid, nspOid);
			break;

322 323 324 325 326 327 328 329 330 331 332 333 334 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 360
		case OCLASS_CONVERSION:
			oldNspOid = AlterConversionNamespace_oid(objid, nspOid);
			break;

		case OCLASS_OPERATOR:
			oldNspOid = AlterOperatorNamespace_oid(objid, nspOid);
			break;

		case OCLASS_OPCLASS:
			oldNspOid = AlterOpClassNamespace_oid(objid, nspOid);
			break;

		case OCLASS_OPFAMILY:
			oldNspOid = AlterOpFamilyNamespace_oid(objid, nspOid);
			break;

		case OCLASS_TSPARSER:
			oldNspOid = AlterTSParserNamespace_oid(objid, nspOid);
			break;

		case OCLASS_TSDICT:
			oldNspOid = AlterTSDictionaryNamespace_oid(objid, nspOid);
			break;

		case OCLASS_TSTEMPLATE:
			oldNspOid = AlterTSTemplateNamespace_oid(objid, nspOid);
			break;

		case OCLASS_TSCONFIG:
			oldNspOid = AlterTSConfigurationNamespace_oid(objid, nspOid);
			break;

		default:
			break;
	}

	return oldNspOid;
}

361 362
/*
 * Generic function to change the namespace of a given object, for simple
363 364
 * cases (won't work for tables, nor other cases where we need to do more
 * than change the namespace column of a single catalog entry).
365 366 367 368
 *
 * The AlterFooNamespace() calls just above will call a function whose job
 * is to lookup the arguments for the generic function here.
 *
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
 * rel: catalog relation containing object (RowExclusiveLock'd by caller)
 * oidCacheId: syscache that indexes this catalog by OID
 * nameCacheId: syscache that indexes this catalog by name and namespace
 *		(pass -1 if there is none)
 * objid: OID of object to change the namespace of
 * nspOid: OID of new namespace
 * Anum_name: column number of catalog's name column
 * Anum_namespace: column number of catalog's namespace column
 * Anum_owner: column number of catalog's owner column, or -1 if none
 * acl_kind: ACL type for object, or -1 if none assigned
 *
 * If the object does not have an owner or permissions, pass -1 for
 * Anum_owner and acl_kind.  In this case the calling user must be superuser.
 *
 * Returns the OID of the object's previous namespace.
384
 */
385 386 387
Oid
AlterObjectNamespace(Relation rel, int oidCacheId, int nameCacheId,
					 Oid objid, Oid nspOid,
388
					 int Anum_name, int Anum_namespace, int Anum_owner,
389
					 AclObjectKind acl_kind)
390
{
391
	Oid			classId = RelationGetRelid(rel);
392
	Oid			oldNspOid;
393 394 395 396 397
	Datum		name,
				namespace;
	bool		isnull;
	HeapTuple	tup,
				newtup;
398 399 400 401
	Datum	   *values;
	bool	   *nulls;
	bool	   *replaces;

402
	tup = SearchSysCacheCopy1(oidCacheId, ObjectIdGetDatum(objid));
403
	if (!HeapTupleIsValid(tup)) /* should not happen */
404 405
		elog(ERROR, "cache lookup failed for object %u of catalog \"%s\"",
			 objid, RelationGetRelationName(rel));
406

407 408 409 410
	name = heap_getattr(tup, Anum_name, RelationGetDescr(rel), &isnull);
	Assert(!isnull);
	namespace = heap_getattr(tup, Anum_namespace, RelationGetDescr(rel), &isnull);
	Assert(!isnull);
411 412 413 414 415
	oldNspOid = DatumGetObjectId(namespace);

	/* Check basic namespace related issues */
	CheckSetNamespace(oldNspOid, nspOid, classId, objid);

416
	/* Permission checks ... superusers can always do it */
417 418
	if (!superuser())
	{
419
		Datum		owner;
420 421 422
		Oid			ownerId;
		AclResult	aclresult;

423 424
		/* Fail if object does not have an explicit owner */
		if (Anum_owner <= 0)
425 426
			ereport(ERROR,
					(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
P
Peter Eisentraut 已提交
427
					 (errmsg("must be superuser to set schema of %s",
428 429 430
							 getObjectDescriptionOids(classId, objid)))));

		/* Otherwise, must be owner of the existing object */
431 432
		owner = heap_getattr(tup, Anum_owner, RelationGetDescr(rel), &isnull);
		Assert(!isnull);
433 434 435 436 437 438
		ownerId = DatumGetObjectId(owner);

		if (!has_privs_of_role(GetUserId(), ownerId))
			aclcheck_error(ACLCHECK_NOT_OWNER, acl_kind,
						   NameStr(*(DatumGetName(name))));

439 440
		/* User must have CREATE privilege on new namespace */
		aclresult = pg_namespace_aclcheck(nspOid, GetUserId(), ACL_CREATE);
441 442
		if (aclresult != ACLCHECK_OK)
			aclcheck_error(aclresult, ACL_KIND_NAMESPACE,
443
						   get_namespace_name(nspOid));
444 445
	}

446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463
	/*
	 * Check for duplicate name (more friendly than unique-index failure).
	 * Since this is just a friendliness check, we can just skip it in cases
	 * where there isn't a suitable syscache available.
	 */
	if (nameCacheId >= 0 &&
		SearchSysCacheExists2(nameCacheId, name, ObjectIdGetDatum(nspOid)))
		ereport(ERROR,
				(errcode(ERRCODE_DUPLICATE_OBJECT),
				 errmsg("%s already exists in schema \"%s\"",
						getObjectDescriptionOids(classId, objid),
						get_namespace_name(nspOid))));

	/* Build modified tuple */
	values = palloc0(RelationGetNumberOfAttributes(rel) * sizeof(Datum));
	nulls = palloc0(RelationGetNumberOfAttributes(rel) * sizeof(bool));
	replaces = palloc0(RelationGetNumberOfAttributes(rel) * sizeof(bool));
	values[Anum_namespace - 1] = ObjectIdGetDatum(nspOid);
464
	replaces[Anum_namespace - 1] = true;
465 466
	newtup = heap_modify_tuple(tup, RelationGetDescr(rel),
							   values, nulls, replaces);
467 468 469 470 471 472 473 474 475 476 477 478 479

	/* Perform actual update */
	simple_heap_update(rel, &tup->t_self, newtup);
	CatalogUpdateIndexes(rel, newtup);

	/* Release memory */
	pfree(values);
	pfree(nulls);
	pfree(replaces);

	/* update dependencies to point to the new schema */
	changeDependencyFor(classId, objid,
						NamespaceRelationId, oldNspOid, nspOid);
480 481

	return oldNspOid;
482 483 484
}


485 486 487 488 489 490 491
/*
 * Executes an ALTER OBJECT / OWNER TO statement.  Based on the object
 * type, the function appropriate to that type is executed.
 */
void
ExecAlterOwnerStmt(AlterOwnerStmt *stmt)
{
492
	Oid			newowner = get_role_oid(stmt->newowner, false);
493 494 495 496

	switch (stmt->objectType)
	{
		case OBJECT_AGGREGATE:
T
Tom Lane 已提交
497
			AlterAggregateOwner(stmt->object, stmt->objarg, newowner);
498 499
			break;

P
Peter Eisentraut 已提交
500 501 502 503
		case OBJECT_COLLATION:
			AlterCollationOwner(stmt->object, newowner);
			break;

504 505 506 507 508
		case OBJECT_CONVERSION:
			AlterConversionOwner(stmt->object, newowner);
			break;

		case OBJECT_DATABASE:
509
			AlterDatabaseOwner(strVal(linitial(stmt->object)), newowner);
510 511 512 513 514 515
			break;

		case OBJECT_FUNCTION:
			AlterFunctionOwner(stmt->object, stmt->objarg, newowner);
			break;

516
		case OBJECT_LANGUAGE:
517
			AlterLanguageOwner(strVal(linitial(stmt->object)), newowner);
518 519
			break;

520
		case OBJECT_LARGEOBJECT:
521
			LargeObjectAlterOwner(oidparse(linitial(stmt->object)), newowner);
522 523
			break;

524
		case OBJECT_OPERATOR:
T
Tom Lane 已提交
525
			Assert(list_length(stmt->objarg) == 2);
526 527 528 529 530 531 532 533 534 535
			AlterOperatorOwner(stmt->object,
							   (TypeName *) linitial(stmt->objarg),
							   (TypeName *) lsecond(stmt->objarg),
							   newowner);
			break;

		case OBJECT_OPCLASS:
			AlterOpClassOwner(stmt->object, stmt->addname, newowner);
			break;

536 537 538 539
		case OBJECT_OPFAMILY:
			AlterOpFamilyOwner(stmt->object, stmt->addname, newowner);
			break;

540
		case OBJECT_SCHEMA:
541
			AlterSchemaOwner(strVal(linitial(stmt->object)), newowner);
542 543 544
			break;

		case OBJECT_TABLESPACE:
545
			AlterTableSpaceOwner(strVal(linitial(stmt->object)), newowner);
546 547 548 549 550 551 552
			break;

		case OBJECT_TYPE:
		case OBJECT_DOMAIN:		/* same as TYPE */
			AlterTypeOwner(stmt->object, newowner);
			break;

553 554 555 556 557 558 559 560
		case OBJECT_TSDICTIONARY:
			AlterTSDictionaryOwner(stmt->object, newowner);
			break;

		case OBJECT_TSCONFIGURATION:
			AlterTSConfigurationOwner(stmt->object, newowner);
			break;

561 562 563 564 565 566 567 568 569
		case OBJECT_FDW:
			AlterForeignDataWrapperOwner(strVal(linitial(stmt->object)),
										 newowner);
			break;

		case OBJECT_FOREIGN_SERVER:
			AlterForeignServerOwner(strVal(linitial(stmt->object)), newowner);
			break;

570 571 572 573 574
		default:
			elog(ERROR, "unrecognized AlterOwnerStmt type: %d",
				 (int) stmt->objectType);
	}
}