OSDN Git Service

c789dc94467a2818701d37b15fd7759baa970f09
[pg-rex/syncrep.git] / src / backend / commands / conversioncmds.c
1 /*-------------------------------------------------------------------------
2  *
3  * conversioncmds.c
4  *        conversion creation command support code
5  *
6  * Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  *
10  * IDENTIFICATION
11  *        $PostgreSQL: pgsql/src/backend/commands/conversioncmds.c,v 1.34 2008/06/14 18:04:33 tgl Exp $
12  *
13  *-------------------------------------------------------------------------
14  */
15 #include "postgres.h"
16
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/syscache.h"
31
32 static void AlterConversionOwner_internal(Relation rel, Oid conversionOid,
33                                                           Oid newOwnerId);
34
35 /*
36  * CREATE CONVERSION
37  */
38 void
39 CreateConversionCommand(CreateConversionStmt *stmt)
40 {
41         Oid                     namespaceId;
42         char       *conversion_name;
43         AclResult       aclresult;
44         int                     from_encoding;
45         int                     to_encoding;
46         Oid                     funcoid;
47         const char *from_encoding_name = stmt->for_encoding_name;
48         const char *to_encoding_name = stmt->to_encoding_name;
49         List       *func_name = stmt->func_name;
50         static Oid      funcargs[] = {INT4OID, INT4OID, CSTRINGOID, INTERNALOID, INT4OID};
51
52         /* Convert list of names to a name and namespace */
53         namespaceId = QualifiedNameGetCreationNamespace(stmt->conversion_name,
54                                                                                                         &conversion_name);
55
56         /* Check we have creation rights in target namespace */
57         aclresult = pg_namespace_aclcheck(namespaceId, GetUserId(), ACL_CREATE);
58         if (aclresult != ACLCHECK_OK)
59                 aclcheck_error(aclresult, ACL_KIND_NAMESPACE,
60                                            get_namespace_name(namespaceId));
61
62         /* Check the encoding names */
63         from_encoding = pg_char_to_encoding(from_encoding_name);
64         if (from_encoding < 0)
65                 ereport(ERROR,
66                                 (errcode(ERRCODE_UNDEFINED_OBJECT),
67                                  errmsg("source encoding \"%s\" does not exist",
68                                                 from_encoding_name)));
69
70         to_encoding = pg_char_to_encoding(to_encoding_name);
71         if (to_encoding < 0)
72                 ereport(ERROR,
73                                 (errcode(ERRCODE_UNDEFINED_OBJECT),
74                                  errmsg("destination encoding \"%s\" does not exist",
75                                                 to_encoding_name)));
76
77         /*
78          * Check the existence of the conversion function. Function name could be
79          * a qualified name.
80          */
81         funcoid = LookupFuncName(func_name, sizeof(funcargs) / sizeof(Oid),
82                                                          funcargs, false);
83
84         /* Check we have EXECUTE rights for the function */
85         aclresult = pg_proc_aclcheck(funcoid, GetUserId(), ACL_EXECUTE);
86         if (aclresult != ACLCHECK_OK)
87                 aclcheck_error(aclresult, ACL_KIND_PROC,
88                                            NameListToString(func_name));
89
90         /*
91          * All seem ok, go ahead (possible failure would be a duplicate conversion
92          * name)
93          */
94         ConversionCreate(conversion_name, namespaceId, GetUserId(),
95                                          from_encoding, to_encoding, funcoid, stmt->def);
96 }
97
98 /*
99  * DROP CONVERSION
100  */
101 void
102 DropConversionsCommand(DropStmt *drop)
103 {
104         ObjectAddresses *objects;
105         ListCell *cell;
106
107         /*
108          * First we identify all the conversions, then we delete them in a single
109          * performMultipleDeletions() call.  This is to avoid unwanted
110          * DROP RESTRICT errors if one of the conversions depends on another.
111          * (Not that that is very likely, but we may as well do this consistently.)
112          */
113         objects = new_object_addresses();
114
115         foreach(cell, drop->objects)
116         {
117                 List            *name = (List *) lfirst(cell);
118                 Oid                     conversionOid;
119                 HeapTuple       tuple;
120                 Form_pg_conversion con;
121                 ObjectAddress object;
122
123                 conversionOid = FindConversionByName(name);
124
125                 if (!OidIsValid(conversionOid))
126                 {
127                         if (!drop->missing_ok)
128                         {
129                                 ereport(ERROR,
130                                                 (errcode(ERRCODE_UNDEFINED_OBJECT),
131                                                  errmsg("conversion \"%s\" does not exist",
132                                                                 NameListToString(name))));
133                         }
134                         else
135                         {
136                                 ereport(NOTICE,
137                                                 (errmsg("conversion \"%s\" does not exist, skipping",
138                                                                 NameListToString(name))));
139                         }
140                         continue;
141                 }
142
143                 tuple = SearchSysCache(CONVOID,
144                                                            ObjectIdGetDatum(conversionOid),
145                                                            0, 0, 0);
146                 if (!HeapTupleIsValid(tuple))
147                         elog(ERROR, "cache lookup failed for conversion %u",
148                                  conversionOid);
149                 con = (Form_pg_conversion) GETSTRUCT(tuple);
150
151                 /* Permission check: must own conversion or its namespace */
152                 if (!pg_conversion_ownercheck(conversionOid, GetUserId()) &&
153                         !pg_namespace_ownercheck(con->connamespace, GetUserId()))
154                         aclcheck_error(ACLCHECK_NOT_OWNER, ACL_KIND_CONVERSION,
155                                                    NameStr(con->conname));
156
157                 object.classId = ConversionRelationId;
158                 object.objectId = conversionOid;
159                 object.objectSubId = 0;
160
161                 add_exact_object_address(&object, objects);
162
163                 ReleaseSysCache(tuple);
164         }
165
166         performMultipleDeletions(objects, drop->behavior);
167
168         free_object_addresses(objects);
169 }
170
171 /*
172  * Rename conversion
173  */
174 void
175 RenameConversion(List *name, const char *newname)
176 {
177         Oid                     conversionOid;
178         Oid                     namespaceOid;
179         HeapTuple       tup;
180         Relation        rel;
181         AclResult       aclresult;
182
183         rel = heap_open(ConversionRelationId, RowExclusiveLock);
184
185         conversionOid = FindConversionByName(name);
186         if (!OidIsValid(conversionOid))
187                 ereport(ERROR,
188                                 (errcode(ERRCODE_UNDEFINED_OBJECT),
189                                  errmsg("conversion \"%s\" does not exist",
190                                                 NameListToString(name))));
191
192         tup = SearchSysCacheCopy(CONVOID,
193                                                          ObjectIdGetDatum(conversionOid),
194                                                          0, 0, 0);
195         if (!HeapTupleIsValid(tup)) /* should not happen */
196                 elog(ERROR, "cache lookup failed for conversion %u", conversionOid);
197
198         namespaceOid = ((Form_pg_conversion) GETSTRUCT(tup))->connamespace;
199
200         /* make sure the new name doesn't exist */
201         if (SearchSysCacheExists(CONNAMENSP,
202                                                          CStringGetDatum(newname),
203                                                          ObjectIdGetDatum(namespaceOid),
204                                                          0, 0))
205                 ereport(ERROR,
206                                 (errcode(ERRCODE_DUPLICATE_OBJECT),
207                                  errmsg("conversion \"%s\" already exists in schema \"%s\"",
208                                                 newname, get_namespace_name(namespaceOid))));
209
210         /* must be owner */
211         if (!pg_conversion_ownercheck(conversionOid, GetUserId()))
212                 aclcheck_error(ACLCHECK_NOT_OWNER, ACL_KIND_CONVERSION,
213                                            NameListToString(name));
214
215         /* must have CREATE privilege on namespace */
216         aclresult = pg_namespace_aclcheck(namespaceOid, GetUserId(), ACL_CREATE);
217         if (aclresult != ACLCHECK_OK)
218                 aclcheck_error(aclresult, ACL_KIND_NAMESPACE,
219                                            get_namespace_name(namespaceOid));
220
221         /* rename */
222         namestrcpy(&(((Form_pg_conversion) GETSTRUCT(tup))->conname), newname);
223         simple_heap_update(rel, &tup->t_self, tup);
224         CatalogUpdateIndexes(rel, tup);
225
226         heap_close(rel, NoLock);
227         heap_freetuple(tup);
228 }
229
230 /*
231  * Change conversion owner, by name
232  */
233 void
234 AlterConversionOwner(List *name, Oid newOwnerId)
235 {
236         Oid                     conversionOid;
237         Relation        rel;
238
239         rel = heap_open(ConversionRelationId, RowExclusiveLock);
240
241         conversionOid = FindConversionByName(name);
242         if (!OidIsValid(conversionOid))
243                 ereport(ERROR,
244                                 (errcode(ERRCODE_UNDEFINED_OBJECT),
245                                  errmsg("conversion \"%s\" does not exist",
246                                                 NameListToString(name))));
247
248         AlterConversionOwner_internal(rel, conversionOid, newOwnerId);
249
250         heap_close(rel, NoLock);
251 }
252
253 /*
254  * Change conversion owner, by oid
255  */
256 void
257 AlterConversionOwner_oid(Oid conversionOid, Oid newOwnerId)
258 {
259         Relation        rel;
260
261         rel = heap_open(ConversionRelationId, RowExclusiveLock);
262
263         AlterConversionOwner_internal(rel, conversionOid, newOwnerId);
264
265         heap_close(rel, NoLock);
266 }
267
268 /*
269  * AlterConversionOwner_internal
270  *
271  * Internal routine for changing the owner.  rel must be pg_conversion, already
272  * open and suitably locked; it will not be closed.
273  */
274 static void
275 AlterConversionOwner_internal(Relation rel, Oid conversionOid, Oid newOwnerId)
276 {
277         Form_pg_conversion convForm;
278         HeapTuple       tup;
279
280         Assert(RelationGetRelid(rel) == ConversionRelationId);
281
282         tup = SearchSysCacheCopy(CONVOID,
283                                                          ObjectIdGetDatum(conversionOid),
284                                                          0, 0, 0);
285         if (!HeapTupleIsValid(tup)) /* should not happen */
286                 elog(ERROR, "cache lookup failed for conversion %u", conversionOid);
287
288         convForm = (Form_pg_conversion) GETSTRUCT(tup);
289
290         /*
291          * If the new owner is the same as the existing owner, consider the
292          * command to have succeeded.  This is for dump restoration purposes.
293          */
294         if (convForm->conowner != newOwnerId)
295         {
296                 AclResult       aclresult;
297
298                 /* Superusers can always do it */
299                 if (!superuser())
300                 {
301                         /* Otherwise, must be owner of the existing object */
302                         if (!pg_conversion_ownercheck(HeapTupleGetOid(tup), GetUserId()))
303                                 aclcheck_error(ACLCHECK_NOT_OWNER, ACL_KIND_CONVERSION,
304                                                            NameStr(convForm->conname));
305
306                         /* Must be able to become new owner */
307                         check_is_member_of_role(GetUserId(), newOwnerId);
308
309                         /* New owner must have CREATE privilege on namespace */
310                         aclresult = pg_namespace_aclcheck(convForm->connamespace,
311                                                                                           newOwnerId,
312                                                                                           ACL_CREATE);
313                         if (aclresult != ACLCHECK_OK)
314                                 aclcheck_error(aclresult, ACL_KIND_NAMESPACE,
315                                                            get_namespace_name(convForm->connamespace));
316                 }
317
318                 /*
319                  * Modify the owner --- okay to scribble on tup because it's a copy
320                  */
321                 convForm->conowner = newOwnerId;
322
323                 simple_heap_update(rel, &tup->t_self, tup);
324
325                 CatalogUpdateIndexes(rel, tup);
326
327                 /* Update owner dependency reference */
328                 changeDependencyOnOwner(ConversionRelationId, conversionOid,
329                                                                 newOwnerId);
330         }
331
332         heap_freetuple(tup);
333 }