Force a checkpoint in CREATE DATABASE before starting to copy the files,
[PostgreSQL.git] / src / backend / commands / alter.c
blobef0ac051ac33ee105111ea21582c1bc4570d99fa
1 /*-------------------------------------------------------------------------
3 * alter.c
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
10 * IDENTIFICATION
11 * $PostgreSQL$
13 *-------------------------------------------------------------------------
15 #include "postgres.h"
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.
40 void
41 ExecRenameStmt(RenameStmt *stmt)
43 switch (stmt->renameType)
45 case OBJECT_AGGREGATE:
46 RenameAggregate(stmt->object, stmt->objarg, stmt->newname);
47 break;
49 case OBJECT_CONVERSION:
50 RenameConversion(stmt->object, stmt->newname);
51 break;
53 case OBJECT_DATABASE:
54 RenameDatabase(stmt->subname, stmt->newname);
55 break;
57 case OBJECT_FUNCTION:
58 RenameFunction(stmt->object, stmt->objarg, stmt->newname);
59 break;
61 case OBJECT_LANGUAGE:
62 RenameLanguage(stmt->subname, stmt->newname);
63 break;
65 case OBJECT_OPCLASS:
66 RenameOpClass(stmt->object, stmt->subname, stmt->newname);
67 break;
69 case OBJECT_OPFAMILY:
70 RenameOpFamily(stmt->object, stmt->subname, stmt->newname);
71 break;
73 case OBJECT_ROLE:
74 RenameRole(stmt->subname, stmt->newname);
75 break;
77 case OBJECT_SCHEMA:
78 RenameSchema(stmt->subname, stmt->newname);
79 break;
81 case OBJECT_TABLESPACE:
82 RenameTableSpace(stmt->subname, stmt->newname);
83 break;
85 case OBJECT_TABLE:
86 case OBJECT_SEQUENCE:
87 case OBJECT_VIEW:
88 case OBJECT_INDEX:
89 case OBJECT_COLUMN:
90 case OBJECT_TRIGGER:
92 Oid relid;
94 CheckRelationOwnership(stmt->relation, true);
96 relid = RangeVarGetRelid(stmt->relation, false);
98 switch (stmt->renameType)
100 case OBJECT_TABLE:
101 case OBJECT_SEQUENCE:
102 case OBJECT_VIEW:
103 case OBJECT_INDEX:
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);
111 AclResult aclresult;
113 aclresult = pg_namespace_aclcheck(namespaceId,
114 GetUserId(),
115 ACL_CREATE);
116 if (aclresult != ACLCHECK_OK)
117 aclcheck_error(aclresult, ACL_KIND_NAMESPACE,
118 get_namespace_name(namespaceId));
120 RenameRelation(relid, stmt->newname, stmt->renameType);
121 break;
123 case OBJECT_COLUMN:
124 renameatt(relid,
125 stmt->subname, /* old att name */
126 stmt->newname, /* new att name */
127 interpretInhOption(stmt->relation->inhOpt), /* recursive? */
128 false); /* recursing already? */
129 break;
130 case OBJECT_TRIGGER:
131 renametrig(relid,
132 stmt->subname, /* old att name */
133 stmt->newname); /* new att name */
134 break;
135 default:
136 /* can't happen */ ;
138 break;
141 case OBJECT_TSPARSER:
142 RenameTSParser(stmt->object, stmt->newname);
143 break;
145 case OBJECT_TSDICTIONARY:
146 RenameTSDictionary(stmt->object, stmt->newname);
147 break;
149 case OBJECT_TSTEMPLATE:
150 RenameTSTemplate(stmt->object, stmt->newname);
151 break;
153 case OBJECT_TSCONFIGURATION:
154 RenameTSConfiguration(stmt->object, stmt->newname);
155 break;
157 case OBJECT_TYPE:
158 RenameType(stmt->object, stmt->newname);
159 break;
161 default:
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.
171 void
172 ExecAlterObjectSchemaStmt(AlterObjectSchemaStmt *stmt)
174 switch (stmt->objectType)
176 case OBJECT_AGGREGATE:
177 AlterFunctionNamespace(stmt->object, stmt->objarg, true,
178 stmt->newschema);
179 break;
181 case OBJECT_FUNCTION:
182 AlterFunctionNamespace(stmt->object, stmt->objarg, false,
183 stmt->newschema);
184 break;
186 case OBJECT_SEQUENCE:
187 case OBJECT_TABLE:
188 case OBJECT_VIEW:
189 CheckRelationOwnership(stmt->relation, true);
190 AlterTableNamespace(stmt->relation, stmt->newschema,
191 stmt->objectType);
192 break;
194 case OBJECT_TYPE:
195 case OBJECT_DOMAIN:
196 AlterTypeNamespace(stmt->object, stmt->newschema);
197 break;
199 default:
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.
209 void
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);
218 break;
220 case OBJECT_CONVERSION:
221 AlterConversionOwner(stmt->object, newowner);
222 break;
224 case OBJECT_DATABASE:
225 AlterDatabaseOwner(strVal(linitial(stmt->object)), newowner);
226 break;
228 case OBJECT_FUNCTION:
229 AlterFunctionOwner(stmt->object, stmt->objarg, newowner);
230 break;
232 case OBJECT_LANGUAGE:
233 AlterLanguageOwner(strVal(linitial(stmt->object)), newowner);
234 break;
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),
241 newowner);
242 break;
244 case OBJECT_OPCLASS:
245 AlterOpClassOwner(stmt->object, stmt->addname, newowner);
246 break;
248 case OBJECT_OPFAMILY:
249 AlterOpFamilyOwner(stmt->object, stmt->addname, newowner);
250 break;
252 case OBJECT_SCHEMA:
253 AlterSchemaOwner(strVal(linitial(stmt->object)), newowner);
254 break;
256 case OBJECT_TABLESPACE:
257 AlterTableSpaceOwner(strVal(linitial(stmt->object)), newowner);
258 break;
260 case OBJECT_TYPE:
261 case OBJECT_DOMAIN: /* same as TYPE */
262 AlterTypeOwner(stmt->object, newowner);
263 break;
265 case OBJECT_TSDICTIONARY:
266 AlterTSDictionaryOwner(stmt->object, newowner);
267 break;
269 case OBJECT_TSCONFIGURATION:
270 AlterTSConfigurationOwner(stmt->object, newowner);
271 break;
273 default:
274 elog(ERROR, "unrecognized AlterOwnerStmt type: %d",
275 (int) stmt->objectType);