alter.c 14.7 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 36
#include "commands/user.h"
#include "miscadmin.h"
#include "parser/parse_clause.h"
37
#include "tcop/utility.h"
38
#include "utils/acl.h"
39
#include "utils/builtins.h"
40
#include "utils/lsyscache.h"
41
#include "utils/syscache.h"
42 43


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

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

61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
		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;

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

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

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

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

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

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

B
Bruce Momjian 已提交
110
				relid = RangeVarGetRelid(stmt->relation, false);
111

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

							aclresult = pg_namespace_aclcheck(namespaceId,
129 130
															  GetUserId(),
															  ACL_CREATE);
B
Bruce Momjian 已提交
131 132
							if (aclresult != ACLCHECK_OK)
								aclcheck_error(aclresult, ACL_KIND_NAMESPACE,
B
Bruce Momjian 已提交
133
											get_namespace_name(namespaceId));
B
Bruce Momjian 已提交
134

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

153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
		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;

169 170 171 172
		case OBJECT_TYPE:
			RenameType(stmt->object, stmt->newname);
			break;

173
		default:
174 175
			elog(ERROR, "unrecognized rename stmt type: %d",
				 (int) stmt->renameType);
176 177
	}
}
178

179 180 181 182 183 184 185 186 187 188
/*
 * 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 已提交
189 190 191 192
			AlterFunctionNamespace(stmt->object, stmt->objarg, true,
								   stmt->newschema);
			break;

P
Peter Eisentraut 已提交
193 194 195 196
		case OBJECT_COLLATION:
			AlterCollationNamespace(stmt->object, stmt->newschema);
			break;

197 198 199 200
		case OBJECT_CONVERSION:
			AlterConversionNamespace(stmt->object, stmt->newschema);
			break;

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

205
		case OBJECT_FUNCTION:
T
Tom Lane 已提交
206
			AlterFunctionNamespace(stmt->object, stmt->objarg, false,
207 208
								   stmt->newschema);
			break;
B
Bruce Momjian 已提交
209

210 211 212 213 214
		case OBJECT_OPERATOR:
			AlterOperatorNamespace(stmt->object, stmt->objarg, stmt->newschema);
			break;

		case OBJECT_OPCLASS:
215
			AlterOpClassNamespace(stmt->object, stmt->addname, stmt->newschema);
216 217 218
			break;

		case OBJECT_OPFAMILY:
219
			AlterOpFamilyNamespace(stmt->object, stmt->addname, stmt->newschema);
220 221
			break;

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

231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
		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;

247 248 249 250
		case OBJECT_TYPE:
		case OBJECT_DOMAIN:
			AlterTypeNamespace(stmt->object, stmt->newschema);
			break;
B
Bruce Momjian 已提交
251

252 253 254 255 256 257
		default:
			elog(ERROR, "unrecognized AlterObjectSchemaStmt type: %d",
				 (int) stmt->objectType);
	}
}

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 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
/*
 * 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:
		{
			Relation rel;
			Relation classRel;

			rel = relation_open(objid, AccessExclusiveLock);
			oldNspOid = RelationGetNamespace(rel);

			classRel = heap_open(RelationRelationId, RowExclusiveLock);

			AlterRelationNamespaceInternal(classRel,
										   objid,
										   oldNspOid,
										   nspOid,
										   true);

			heap_close(classRel, RowExclusiveLock);

			relation_close(rel, NoLock);
			break;
		}

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

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

P
Peter Eisentraut 已提交
314 315 316 317
		case OCLASS_COLLATION:
			oldNspOid = AlterCollationNamespace_oid(objid, nspOid);
			break;

318 319 320 321 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
		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;
}

357 358
/*
 * Generic function to change the namespace of a given object, for simple
359 360
 * 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).
361 362 363 364
 *
 * The AlterFooNamespace() calls just above will call a function whose job
 * is to lookup the arguments for the generic function here.
 *
365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
 * 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.
380
 */
381 382 383
Oid
AlterObjectNamespace(Relation rel, int oidCacheId, int nameCacheId,
					 Oid objid, Oid nspOid,
384
					 int Anum_name, int Anum_namespace, int Anum_owner,
385
					 AclObjectKind acl_kind)
386
{
387
	Oid			classId = RelationGetRelid(rel);
388 389 390
	Oid			oldNspOid;
	Datum       name, namespace;
	bool        isnull;
391
	HeapTuple	tup, newtup;
392 393 394 395
	Datum	   *values;
	bool	   *nulls;
	bool	   *replaces;

396
	tup = SearchSysCacheCopy1(oidCacheId, ObjectIdGetDatum(objid));
397
	if (!HeapTupleIsValid(tup)) /* should not happen */
398 399
		elog(ERROR, "cache lookup failed for object %u of catalog \"%s\"",
			 objid, RelationGetRelationName(rel));
400

401 402 403 404
	name = heap_getattr(tup, Anum_name, RelationGetDescr(rel), &isnull);
	Assert(!isnull);
	namespace = heap_getattr(tup, Anum_namespace, RelationGetDescr(rel), &isnull);
	Assert(!isnull);
405 406 407 408 409
	oldNspOid = DatumGetObjectId(namespace);

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

410
	/* Permission checks ... superusers can always do it */
411 412 413 414 415 416
	if (!superuser())
	{
		Datum       owner;
		Oid			ownerId;
		AclResult	aclresult;

417 418
		/* Fail if object does not have an explicit owner */
		if (Anum_owner <= 0)
419 420 421 422 423 424
			ereport(ERROR,
					(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
					 (errmsg("must be superuser to SET SCHEMA of %s",
							 getObjectDescriptionOids(classId, objid)))));

		/* Otherwise, must be owner of the existing object */
425 426
		owner = heap_getattr(tup, Anum_owner, RelationGetDescr(rel), &isnull);
		Assert(!isnull);
427 428 429 430 431 432
		ownerId = DatumGetObjectId(owner);

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

433 434
		/* User must have CREATE privilege on new namespace */
		aclresult = pg_namespace_aclcheck(nspOid, GetUserId(), ACL_CREATE);
435 436
		if (aclresult != ACLCHECK_OK)
			aclcheck_error(aclresult, ACL_KIND_NAMESPACE,
437
						   get_namespace_name(nspOid));
438 439
	}

440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457
	/*
	 * 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);
458
	replaces[Anum_namespace - 1] = true;
459 460
	newtup = heap_modify_tuple(tup, RelationGetDescr(rel),
							   values, nulls, replaces);
461 462 463 464 465 466 467 468 469 470 471 472 473

	/* 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);
474 475

	return oldNspOid;
476 477 478
}


479 480 481 482 483 484 485
/*
 * Executes an ALTER OBJECT / OWNER TO statement.  Based on the object
 * type, the function appropriate to that type is executed.
 */
void
ExecAlterOwnerStmt(AlterOwnerStmt *stmt)
{
486
	Oid			newowner = get_role_oid(stmt->newowner, false);
487 488 489 490

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

P
Peter Eisentraut 已提交
494 495 496 497
		case OBJECT_COLLATION:
			AlterCollationOwner(stmt->object, newowner);
			break;

498 499 500 501 502
		case OBJECT_CONVERSION:
			AlterConversionOwner(stmt->object, newowner);
			break;

		case OBJECT_DATABASE:
503
			AlterDatabaseOwner(strVal(linitial(stmt->object)), newowner);
504 505 506 507 508 509
			break;

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

510
		case OBJECT_LANGUAGE:
511
			AlterLanguageOwner(strVal(linitial(stmt->object)), newowner);
512 513
			break;

514
		case OBJECT_LARGEOBJECT:
515
			LargeObjectAlterOwner(oidparse(linitial(stmt->object)), newowner);
516 517
			break;

518
		case OBJECT_OPERATOR:
T
Tom Lane 已提交
519
			Assert(list_length(stmt->objarg) == 2);
520 521 522 523 524 525 526 527 528 529
			AlterOperatorOwner(stmt->object,
							   (TypeName *) linitial(stmt->objarg),
							   (TypeName *) lsecond(stmt->objarg),
							   newowner);
			break;

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

530 531 532 533
		case OBJECT_OPFAMILY:
			AlterOpFamilyOwner(stmt->object, stmt->addname, newowner);
			break;

534
		case OBJECT_SCHEMA:
535
			AlterSchemaOwner(strVal(linitial(stmt->object)), newowner);
536 537 538
			break;

		case OBJECT_TABLESPACE:
539
			AlterTableSpaceOwner(strVal(linitial(stmt->object)), newowner);
540 541 542 543 544 545 546
			break;

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

547 548 549 550 551 552 553 554
		case OBJECT_TSDICTIONARY:
			AlterTSDictionaryOwner(stmt->object, newowner);
			break;

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

555 556 557 558 559 560 561 562 563
		case OBJECT_FDW:
			AlterForeignDataWrapperOwner(strVal(linitial(stmt->object)),
										 newowner);
			break;

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

564 565 566 567 568
		default:
			elog(ERROR, "unrecognized AlterOwnerStmt type: %d",
				 (int) stmt->objectType);
	}
}