alter.c 15.1 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
		case OBJECT_CONVERSION:
			RenameConversion(stmt->object, stmt->newname);
			break;

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

68 69 70 71 72 73 74 75
		case OBJECT_FDW:
			RenameForeignDataWrapper(stmt->subname, stmt->newname);
			break;

		case OBJECT_FOREIGN_SERVER:
			RenameForeignServer(stmt->subname, stmt->newname);
			break;

76 77 78 79 80 81 82 83 84 85 86 87
		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;

88 89 90 91
		case OBJECT_OPFAMILY:
			RenameOpFamily(stmt->object, stmt->subname, stmt->newname);
			break;

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

96 97 98 99
		case OBJECT_SCHEMA:
			RenameSchema(stmt->subname, stmt->newname);
			break;

100 101 102 103
		case OBJECT_TABLESPACE:
			RenameTableSpace(stmt->subname, stmt->newname);
			break;

104
		case OBJECT_TABLE:
105 106
		case OBJECT_SEQUENCE:
		case OBJECT_VIEW:
T
Tom Lane 已提交
107
		case OBJECT_INDEX:
108
		case OBJECT_COLUMN:
109
		case OBJECT_ATTRIBUTE:
110
		case OBJECT_TRIGGER:
R
Robert Haas 已提交
111
		case OBJECT_FOREIGN_TABLE:
B
Bruce Momjian 已提交
112 113
			{
				Oid			relid;
114

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

117 118 119 120 121
				/*
				 * Lock level used here should match what will be taken later,
				 * in RenameRelation, renameatt, or renametrig.
				 */
				relid = RangeVarGetRelid(stmt->relation, AccessExclusiveLock,
122
										 false);
123

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

							aclresult = pg_namespace_aclcheck(namespaceId,
141 142
															  GetUserId(),
															  ACL_CREATE);
B
Bruce Momjian 已提交
143 144
							if (aclresult != ACLCHECK_OK)
								aclcheck_error(aclresult, ACL_KIND_NAMESPACE,
B
Bruce Momjian 已提交
145
											get_namespace_name(namespaceId));
B
Bruce Momjian 已提交
146

147
							RenameRelation(relid, stmt->newname, stmt->renameType);
B
Bruce Momjian 已提交
148 149 150
							break;
						}
					case OBJECT_COLUMN:
151
					case OBJECT_ATTRIBUTE:
152
						renameatt(relid, stmt);
B
Bruce Momjian 已提交
153 154 155 156 157 158 159 160 161 162
						break;
					case OBJECT_TRIGGER:
						renametrig(relid,
								   stmt->subname,		/* old att name */
								   stmt->newname);		/* new att name */
						break;
					default:
						 /* can't happen */ ;
				}
				break;
163 164
			}

165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
		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;

181 182 183 184
		case OBJECT_TYPE:
			RenameType(stmt->object, stmt->newname);
			break;

185
		default:
186 187
			elog(ERROR, "unrecognized rename stmt type: %d",
				 (int) stmt->renameType);
188 189
	}
}
190

191 192 193 194 195 196 197 198 199 200
/*
 * 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 已提交
201 202 203 204
			AlterFunctionNamespace(stmt->object, stmt->objarg, true,
								   stmt->newschema);
			break;

P
Peter Eisentraut 已提交
205 206 207 208
		case OBJECT_COLLATION:
			AlterCollationNamespace(stmt->object, stmt->newschema);
			break;

209 210 211 212
		case OBJECT_CONVERSION:
			AlterConversionNamespace(stmt->object, stmt->newschema);
			break;

213 214 215 216
		case OBJECT_EXTENSION:
			AlterExtensionNamespace(stmt->object, stmt->newschema);
			break;

217
		case OBJECT_FUNCTION:
T
Tom Lane 已提交
218
			AlterFunctionNamespace(stmt->object, stmt->objarg, false,
219 220
								   stmt->newschema);
			break;
B
Bruce Momjian 已提交
221

222 223 224 225 226
		case OBJECT_OPERATOR:
			AlterOperatorNamespace(stmt->object, stmt->objarg, stmt->newschema);
			break;

		case OBJECT_OPCLASS:
227
			AlterOpClassNamespace(stmt->object, stmt->addname, stmt->newschema);
228 229 230
			break;

		case OBJECT_OPFAMILY:
231
			AlterOpFamilyNamespace(stmt->object, stmt->addname, stmt->newschema);
232 233
			break;

234 235
		case OBJECT_SEQUENCE:
		case OBJECT_TABLE:
236
		case OBJECT_VIEW:
R
Robert Haas 已提交
237
		case OBJECT_FOREIGN_TABLE:
238
			CheckRelationOwnership(stmt->relation, true);
239
			AlterTableNamespace(stmt->relation, stmt->newschema,
240
								stmt->objectType, AccessExclusiveLock);
241
			break;
B
Bruce Momjian 已提交
242

243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
		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;

259 260 261 262
		case OBJECT_TYPE:
		case OBJECT_DOMAIN:
			AlterTypeNamespace(stmt->object, stmt->newschema);
			break;
B
Bruce Momjian 已提交
263

264 265 266 267 268 269
		default:
			elog(ERROR, "unrecognized AlterObjectSchemaStmt type: %d",
				 (int) stmt->objectType);
	}
}

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
/*
 * 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:
297 298 299
			{
				Relation	rel;
				Relation	classRel;
300

301 302
				rel = relation_open(objid, AccessExclusiveLock);
				oldNspOid = RelationGetNamespace(rel);
303

304
				classRel = heap_open(RelationRelationId, RowExclusiveLock);
305

306 307 308 309 310
				AlterRelationNamespaceInternal(classRel,
											   objid,
											   oldNspOid,
											   nspOid,
											   true);
311

312
				heap_close(classRel, RowExclusiveLock);
313

314 315 316
				relation_close(rel, NoLock);
				break;
			}
317 318 319 320 321 322 323 324 325

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

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

P
Peter Eisentraut 已提交
326 327 328 329
		case OCLASS_COLLATION:
			oldNspOid = AlterCollationNamespace_oid(objid, nspOid);
			break;

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 361 362 363 364 365 366 367 368
		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;
}

369 370
/*
 * Generic function to change the namespace of a given object, for simple
371 372
 * 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).
373 374 375 376
 *
 * The AlterFooNamespace() calls just above will call a function whose job
 * is to lookup the arguments for the generic function here.
 *
377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
 * 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.
392
 */
393 394 395
Oid
AlterObjectNamespace(Relation rel, int oidCacheId, int nameCacheId,
					 Oid objid, Oid nspOid,
396
					 int Anum_name, int Anum_namespace, int Anum_owner,
397
					 AclObjectKind acl_kind)
398
{
399
	Oid			classId = RelationGetRelid(rel);
400
	Oid			oldNspOid;
401 402 403 404 405
	Datum		name,
				namespace;
	bool		isnull;
	HeapTuple	tup,
				newtup;
406 407 408 409
	Datum	   *values;
	bool	   *nulls;
	bool	   *replaces;

410
	tup = SearchSysCacheCopy1(oidCacheId, ObjectIdGetDatum(objid));
411
	if (!HeapTupleIsValid(tup)) /* should not happen */
412 413
		elog(ERROR, "cache lookup failed for object %u of catalog \"%s\"",
			 objid, RelationGetRelationName(rel));
414

415 416 417 418
	name = heap_getattr(tup, Anum_name, RelationGetDescr(rel), &isnull);
	Assert(!isnull);
	namespace = heap_getattr(tup, Anum_namespace, RelationGetDescr(rel), &isnull);
	Assert(!isnull);
419 420 421 422 423
	oldNspOid = DatumGetObjectId(namespace);

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

424
	/* Permission checks ... superusers can always do it */
425 426
	if (!superuser())
	{
427
		Datum		owner;
428 429 430
		Oid			ownerId;
		AclResult	aclresult;

431 432
		/* Fail if object does not have an explicit owner */
		if (Anum_owner <= 0)
433 434
			ereport(ERROR,
					(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
P
Peter Eisentraut 已提交
435
					 (errmsg("must be superuser to set schema of %s",
436 437 438
							 getObjectDescriptionOids(classId, objid)))));

		/* Otherwise, must be owner of the existing object */
439 440
		owner = heap_getattr(tup, Anum_owner, RelationGetDescr(rel), &isnull);
		Assert(!isnull);
441 442 443 444 445 446
		ownerId = DatumGetObjectId(owner);

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

447 448
		/* User must have CREATE privilege on new namespace */
		aclresult = pg_namespace_aclcheck(nspOid, GetUserId(), ACL_CREATE);
449 450
		if (aclresult != ACLCHECK_OK)
			aclcheck_error(aclresult, ACL_KIND_NAMESPACE,
451
						   get_namespace_name(nspOid));
452 453
	}

454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471
	/*
	 * 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);
472
	replaces[Anum_namespace - 1] = true;
473 474
	newtup = heap_modify_tuple(tup, RelationGetDescr(rel),
							   values, nulls, replaces);
475 476 477 478 479 480 481 482 483 484 485 486 487

	/* 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);
488 489

	return oldNspOid;
490 491 492
}


493 494 495 496 497 498 499
/*
 * Executes an ALTER OBJECT / OWNER TO statement.  Based on the object
 * type, the function appropriate to that type is executed.
 */
void
ExecAlterOwnerStmt(AlterOwnerStmt *stmt)
{
500
	Oid			newowner = get_role_oid(stmt->newowner, false);
501 502 503 504

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

P
Peter Eisentraut 已提交
508 509 510 511
		case OBJECT_COLLATION:
			AlterCollationOwner(stmt->object, newowner);
			break;

512 513 514 515 516
		case OBJECT_CONVERSION:
			AlterConversionOwner(stmt->object, newowner);
			break;

		case OBJECT_DATABASE:
517
			AlterDatabaseOwner(strVal(linitial(stmt->object)), newowner);
518 519 520 521 522 523
			break;

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

524
		case OBJECT_LANGUAGE:
525
			AlterLanguageOwner(strVal(linitial(stmt->object)), newowner);
526 527
			break;

528
		case OBJECT_LARGEOBJECT:
529
			LargeObjectAlterOwner(oidparse(linitial(stmt->object)), newowner);
530 531
			break;

532
		case OBJECT_OPERATOR:
T
Tom Lane 已提交
533
			Assert(list_length(stmt->objarg) == 2);
534 535 536 537 538 539 540 541 542 543
			AlterOperatorOwner(stmt->object,
							   (TypeName *) linitial(stmt->objarg),
							   (TypeName *) lsecond(stmt->objarg),
							   newowner);
			break;

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

544 545 546 547
		case OBJECT_OPFAMILY:
			AlterOpFamilyOwner(stmt->object, stmt->addname, newowner);
			break;

548
		case OBJECT_SCHEMA:
549
			AlterSchemaOwner(strVal(linitial(stmt->object)), newowner);
550 551 552
			break;

		case OBJECT_TABLESPACE:
553
			AlterTableSpaceOwner(strVal(linitial(stmt->object)), newowner);
554 555 556 557 558 559 560
			break;

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

561 562 563 564 565 566 567 568
		case OBJECT_TSDICTIONARY:
			AlterTSDictionaryOwner(stmt->object, newowner);
			break;

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

569 570 571 572 573 574 575 576 577
		case OBJECT_FDW:
			AlterForeignDataWrapperOwner(strVal(linitial(stmt->object)),
										 newowner);
			break;

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

578 579 580 581 582
		default:
			elog(ERROR, "unrecognized AlterOwnerStmt type: %d",
				 (int) stmt->objectType);
	}
}