1 /*-------------------------------------------------------------------------
4 * conversion creation command support code
6 * Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * $PostgreSQL: pgsql/src/backend/commands/conversioncmds.c,v 1.36 2008/11/14 17:40:56 tgl Exp $
13 *-------------------------------------------------------------------------
17 #include "access/heapam.h"
18 #include "catalog/dependency.h"
19 #include "catalog/indexing.h"
20 #include "catalog/pg_conversion.h"
21 #include "catalog/pg_conversion_fn.h"
22 #include "catalog/pg_type.h"
23 #include "commands/conversioncmds.h"
24 #include "mb/pg_wchar.h"
25 #include "miscadmin.h"
26 #include "parser/parse_func.h"
27 #include "utils/acl.h"
28 #include "utils/builtins.h"
29 #include "utils/lsyscache.h"
30 #include "utils/rel.h"
31 #include "utils/syscache.h"
33 static void AlterConversionOwner_internal(Relation rel, Oid conversionOid,
40 CreateConversionCommand(CreateConversionStmt *stmt)
43 char *conversion_name;
48 const char *from_encoding_name = stmt->for_encoding_name;
49 const char *to_encoding_name = stmt->to_encoding_name;
50 List *func_name = stmt->func_name;
51 static Oid funcargs[] = {INT4OID, INT4OID, CSTRINGOID, INTERNALOID, INT4OID};
53 /* Convert list of names to a name and namespace */
54 namespaceId = QualifiedNameGetCreationNamespace(stmt->conversion_name,
57 /* Check we have creation rights in target namespace */
58 aclresult = pg_namespace_aclcheck(namespaceId, GetUserId(), ACL_CREATE);
59 if (aclresult != ACLCHECK_OK)
60 aclcheck_error(aclresult, ACL_KIND_NAMESPACE,
61 get_namespace_name(namespaceId));
63 /* Check the encoding names */
64 from_encoding = pg_char_to_encoding(from_encoding_name);
65 if (from_encoding < 0)
67 (errcode(ERRCODE_UNDEFINED_OBJECT),
68 errmsg("source encoding \"%s\" does not exist",
69 from_encoding_name)));
71 to_encoding = pg_char_to_encoding(to_encoding_name);
74 (errcode(ERRCODE_UNDEFINED_OBJECT),
75 errmsg("destination encoding \"%s\" does not exist",
79 * Check the existence of the conversion function. Function name could be
82 funcoid = LookupFuncName(func_name, sizeof(funcargs) / sizeof(Oid),
85 /* Check it returns VOID, else it's probably the wrong function */
86 if (get_func_rettype(funcoid) != VOIDOID)
88 (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
89 errmsg("encoding conversion function %s must return type \"void\"",
90 NameListToString(func_name))));
92 /* Check we have EXECUTE rights for the function */
93 aclresult = pg_proc_aclcheck(funcoid, GetUserId(), ACL_EXECUTE);
94 if (aclresult != ACLCHECK_OK)
95 aclcheck_error(aclresult, ACL_KIND_PROC,
96 NameListToString(func_name));
99 * All seem ok, go ahead (possible failure would be a duplicate conversion
102 ConversionCreate(conversion_name, namespaceId, GetUserId(),
103 from_encoding, to_encoding, funcoid, stmt->def);
110 DropConversionsCommand(DropStmt *drop)
112 ObjectAddresses *objects;
116 * First we identify all the conversions, then we delete them in a single
117 * performMultipleDeletions() call. This is to avoid unwanted
118 * DROP RESTRICT errors if one of the conversions depends on another.
119 * (Not that that is very likely, but we may as well do this consistently.)
121 objects = new_object_addresses();
123 foreach(cell, drop->objects)
125 List *name = (List *) lfirst(cell);
128 Form_pg_conversion con;
129 ObjectAddress object;
131 conversionOid = FindConversionByName(name);
133 if (!OidIsValid(conversionOid))
135 if (!drop->missing_ok)
138 (errcode(ERRCODE_UNDEFINED_OBJECT),
139 errmsg("conversion \"%s\" does not exist",
140 NameListToString(name))));
145 (errmsg("conversion \"%s\" does not exist, skipping",
146 NameListToString(name))));
151 tuple = SearchSysCache(CONVOID,
152 ObjectIdGetDatum(conversionOid),
154 if (!HeapTupleIsValid(tuple))
155 elog(ERROR, "cache lookup failed for conversion %u",
157 con = (Form_pg_conversion) GETSTRUCT(tuple);
159 /* Permission check: must own conversion or its namespace */
160 if (!pg_conversion_ownercheck(conversionOid, GetUserId()) &&
161 !pg_namespace_ownercheck(con->connamespace, GetUserId()))
162 aclcheck_error(ACLCHECK_NOT_OWNER, ACL_KIND_CONVERSION,
163 NameStr(con->conname));
165 object.classId = ConversionRelationId;
166 object.objectId = conversionOid;
167 object.objectSubId = 0;
169 add_exact_object_address(&object, objects);
171 ReleaseSysCache(tuple);
174 performMultipleDeletions(objects, drop->behavior);
176 free_object_addresses(objects);
183 RenameConversion(List *name, const char *newname)
191 rel = heap_open(ConversionRelationId, RowExclusiveLock);
193 conversionOid = FindConversionByName(name);
194 if (!OidIsValid(conversionOid))
196 (errcode(ERRCODE_UNDEFINED_OBJECT),
197 errmsg("conversion \"%s\" does not exist",
198 NameListToString(name))));
200 tup = SearchSysCacheCopy(CONVOID,
201 ObjectIdGetDatum(conversionOid),
203 if (!HeapTupleIsValid(tup)) /* should not happen */
204 elog(ERROR, "cache lookup failed for conversion %u", conversionOid);
206 namespaceOid = ((Form_pg_conversion) GETSTRUCT(tup))->connamespace;
208 /* make sure the new name doesn't exist */
209 if (SearchSysCacheExists(CONNAMENSP,
210 CStringGetDatum(newname),
211 ObjectIdGetDatum(namespaceOid),
214 (errcode(ERRCODE_DUPLICATE_OBJECT),
215 errmsg("conversion \"%s\" already exists in schema \"%s\"",
216 newname, get_namespace_name(namespaceOid))));
219 if (!pg_conversion_ownercheck(conversionOid, GetUserId()))
220 aclcheck_error(ACLCHECK_NOT_OWNER, ACL_KIND_CONVERSION,
221 NameListToString(name));
223 /* must have CREATE privilege on namespace */
224 aclresult = pg_namespace_aclcheck(namespaceOid, GetUserId(), ACL_CREATE);
225 if (aclresult != ACLCHECK_OK)
226 aclcheck_error(aclresult, ACL_KIND_NAMESPACE,
227 get_namespace_name(namespaceOid));
230 namestrcpy(&(((Form_pg_conversion) GETSTRUCT(tup))->conname), newname);
231 simple_heap_update(rel, &tup->t_self, tup);
232 CatalogUpdateIndexes(rel, tup);
234 heap_close(rel, NoLock);
239 * Change conversion owner, by name
242 AlterConversionOwner(List *name, Oid newOwnerId)
247 rel = heap_open(ConversionRelationId, RowExclusiveLock);
249 conversionOid = FindConversionByName(name);
250 if (!OidIsValid(conversionOid))
252 (errcode(ERRCODE_UNDEFINED_OBJECT),
253 errmsg("conversion \"%s\" does not exist",
254 NameListToString(name))));
256 AlterConversionOwner_internal(rel, conversionOid, newOwnerId);
258 heap_close(rel, NoLock);
262 * Change conversion owner, by oid
265 AlterConversionOwner_oid(Oid conversionOid, Oid newOwnerId)
269 rel = heap_open(ConversionRelationId, RowExclusiveLock);
271 AlterConversionOwner_internal(rel, conversionOid, newOwnerId);
273 heap_close(rel, NoLock);
277 * AlterConversionOwner_internal
279 * Internal routine for changing the owner. rel must be pg_conversion, already
280 * open and suitably locked; it will not be closed.
283 AlterConversionOwner_internal(Relation rel, Oid conversionOid, Oid newOwnerId)
285 Form_pg_conversion convForm;
288 Assert(RelationGetRelid(rel) == ConversionRelationId);
290 tup = SearchSysCacheCopy(CONVOID,
291 ObjectIdGetDatum(conversionOid),
293 if (!HeapTupleIsValid(tup)) /* should not happen */
294 elog(ERROR, "cache lookup failed for conversion %u", conversionOid);
296 convForm = (Form_pg_conversion) GETSTRUCT(tup);
299 * If the new owner is the same as the existing owner, consider the
300 * command to have succeeded. This is for dump restoration purposes.
302 if (convForm->conowner != newOwnerId)
306 /* Superusers can always do it */
309 /* Otherwise, must be owner of the existing object */
310 if (!pg_conversion_ownercheck(HeapTupleGetOid(tup), GetUserId()))
311 aclcheck_error(ACLCHECK_NOT_OWNER, ACL_KIND_CONVERSION,
312 NameStr(convForm->conname));
314 /* Must be able to become new owner */
315 check_is_member_of_role(GetUserId(), newOwnerId);
317 /* New owner must have CREATE privilege on namespace */
318 aclresult = pg_namespace_aclcheck(convForm->connamespace,
321 if (aclresult != ACLCHECK_OK)
322 aclcheck_error(aclresult, ACL_KIND_NAMESPACE,
323 get_namespace_name(convForm->connamespace));
327 * Modify the owner --- okay to scribble on tup because it's a copy
329 convForm->conowner = newOwnerId;
331 simple_heap_update(rel, &tup->t_self, tup);
333 CatalogUpdateIndexes(rel, tup);
335 /* Update owner dependency reference */
336 changeDependencyOnOwner(ConversionRelationId, conversionOid,