1 /*-------------------------------------------------------------------------
4 * Drivers for generic alter commands
6 * Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
13 *-------------------------------------------------------------------------
17 #include "catalog/namespace.h"
18 #include "commands/alter.h"
19 #include "commands/conversioncmds.h"
20 #include "commands/dbcommands.h"
21 #include "commands/defrem.h"
22 #include "commands/proclang.h"
23 #include "commands/schemacmds.h"
24 #include "commands/tablecmds.h"
25 #include "commands/tablespace.h"
26 #include "commands/trigger.h"
27 #include "commands/typecmds.h"
28 #include "commands/user.h"
29 #include "miscadmin.h"
30 #include "parser/parse_clause.h"
31 #include "tcop/utility.h"
32 #include "utils/acl.h"
33 #include "utils/lsyscache.h"
37 * Executes an ALTER OBJECT / RENAME TO statement. Based on the object
38 * type, the function appropriate to that type is executed.
41 ExecRenameStmt(RenameStmt
*stmt
)
43 switch (stmt
->renameType
)
45 case OBJECT_AGGREGATE
:
46 RenameAggregate(stmt
->object
, stmt
->objarg
, stmt
->newname
);
49 case OBJECT_CONVERSION
:
50 RenameConversion(stmt
->object
, stmt
->newname
);
54 RenameDatabase(stmt
->subname
, stmt
->newname
);
58 RenameFunction(stmt
->object
, stmt
->objarg
, stmt
->newname
);
62 RenameLanguage(stmt
->subname
, stmt
->newname
);
66 RenameOpClass(stmt
->object
, stmt
->subname
, stmt
->newname
);
70 RenameOpFamily(stmt
->object
, stmt
->subname
, stmt
->newname
);
74 RenameRole(stmt
->subname
, stmt
->newname
);
78 RenameSchema(stmt
->subname
, stmt
->newname
);
81 case OBJECT_TABLESPACE
:
82 RenameTableSpace(stmt
->subname
, stmt
->newname
);
94 CheckRelationOwnership(stmt
->relation
, true);
96 relid
= RangeVarGetRelid(stmt
->relation
, false);
98 switch (stmt
->renameType
)
101 case OBJECT_SEQUENCE
:
106 * RENAME TABLE requires that we (still) hold
107 * CREATE rights on the containing namespace, as
108 * well as ownership of the table.
110 Oid namespaceId
= get_rel_namespace(relid
);
113 aclresult
= pg_namespace_aclcheck(namespaceId
,
116 if (aclresult
!= ACLCHECK_OK
)
117 aclcheck_error(aclresult
, ACL_KIND_NAMESPACE
,
118 get_namespace_name(namespaceId
));
120 RenameRelation(relid
, stmt
->newname
, stmt
->renameType
);
125 stmt
->subname
, /* old att name */
126 stmt
->newname
, /* new att name */
127 interpretInhOption(stmt
->relation
->inhOpt
), /* recursive? */
128 false); /* recursing already? */
132 stmt
->subname
, /* old att name */
133 stmt
->newname
); /* new att name */
141 case OBJECT_TSPARSER
:
142 RenameTSParser(stmt
->object
, stmt
->newname
);
145 case OBJECT_TSDICTIONARY
:
146 RenameTSDictionary(stmt
->object
, stmt
->newname
);
149 case OBJECT_TSTEMPLATE
:
150 RenameTSTemplate(stmt
->object
, stmt
->newname
);
153 case OBJECT_TSCONFIGURATION
:
154 RenameTSConfiguration(stmt
->object
, stmt
->newname
);
158 RenameType(stmt
->object
, stmt
->newname
);
162 elog(ERROR
, "unrecognized rename stmt type: %d",
163 (int) stmt
->renameType
);
168 * Executes an ALTER OBJECT / SET SCHEMA statement. Based on the object
169 * type, the function appropriate to that type is executed.
172 ExecAlterObjectSchemaStmt(AlterObjectSchemaStmt
*stmt
)
174 switch (stmt
->objectType
)
176 case OBJECT_AGGREGATE
:
177 AlterFunctionNamespace(stmt
->object
, stmt
->objarg
, true,
181 case OBJECT_FUNCTION
:
182 AlterFunctionNamespace(stmt
->object
, stmt
->objarg
, false,
186 case OBJECT_SEQUENCE
:
189 CheckRelationOwnership(stmt
->relation
, true);
190 AlterTableNamespace(stmt
->relation
, stmt
->newschema
,
196 AlterTypeNamespace(stmt
->object
, stmt
->newschema
);
200 elog(ERROR
, "unrecognized AlterObjectSchemaStmt type: %d",
201 (int) stmt
->objectType
);
206 * Executes an ALTER OBJECT / OWNER TO statement. Based on the object
207 * type, the function appropriate to that type is executed.
210 ExecAlterOwnerStmt(AlterOwnerStmt
*stmt
)
212 Oid newowner
= get_roleid_checked(stmt
->newowner
);
214 switch (stmt
->objectType
)
216 case OBJECT_AGGREGATE
:
217 AlterAggregateOwner(stmt
->object
, stmt
->objarg
, newowner
);
220 case OBJECT_CONVERSION
:
221 AlterConversionOwner(stmt
->object
, newowner
);
224 case OBJECT_DATABASE
:
225 AlterDatabaseOwner(strVal(linitial(stmt
->object
)), newowner
);
228 case OBJECT_FUNCTION
:
229 AlterFunctionOwner(stmt
->object
, stmt
->objarg
, newowner
);
232 case OBJECT_LANGUAGE
:
233 AlterLanguageOwner(strVal(linitial(stmt
->object
)), newowner
);
236 case OBJECT_OPERATOR
:
237 Assert(list_length(stmt
->objarg
) == 2);
238 AlterOperatorOwner(stmt
->object
,
239 (TypeName
*) linitial(stmt
->objarg
),
240 (TypeName
*) lsecond(stmt
->objarg
),
245 AlterOpClassOwner(stmt
->object
, stmt
->addname
, newowner
);
248 case OBJECT_OPFAMILY
:
249 AlterOpFamilyOwner(stmt
->object
, stmt
->addname
, newowner
);
253 AlterSchemaOwner(strVal(linitial(stmt
->object
)), newowner
);
256 case OBJECT_TABLESPACE
:
257 AlterTableSpaceOwner(strVal(linitial(stmt
->object
)), newowner
);
261 case OBJECT_DOMAIN
: /* same as TYPE */
262 AlterTypeOwner(stmt
->object
, newowner
);
265 case OBJECT_TSDICTIONARY
:
266 AlterTSDictionaryOwner(stmt
->object
, newowner
);
269 case OBJECT_TSCONFIGURATION
:
270 AlterTSConfigurationOwner(stmt
->object
, newowner
);
274 elog(ERROR
, "unrecognized AlterOwnerStmt type: %d",
275 (int) stmt
->objectType
);