PostgreSQL Source Code  git master
collationcmds.h File Reference
Include dependency graph for collationcmds.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

ObjectAddress DefineCollation (ParseState *pstate, List *names, List *parameters, bool if_not_exists)
 
void IsThereCollationInNamespace (const char *collname, Oid nspOid)
 
ObjectAddress AlterCollation (AlterCollationStmt *stmt)
 

Function Documentation

◆ AlterCollation()

ObjectAddress AlterCollation ( AlterCollationStmt stmt)

Definition at line 285 of file collationcmds.c.

References aclcheck_error(), ACLCHECK_NOT_OWNER, CatalogTupleUpdate(), AlterCollationStmt::collname, COLLOID, CStringGetTextDatum, elog, ereport, errmsg(), ERROR, get_collation_actual_version(), get_collation_oid(), GETSTRUCT, GetUserId(), heap_freetuple(), heap_modify_tuple(), HeapTupleIsValid, InvokeObjectPostAlterHook, NameListToString(), NameStr, NoLock, NOTICE, OBJECT_COLLATION, ObjectAddressSet, ObjectIdGetDatum, pg_collation_ownercheck(), RelationGetDescr, RowExclusiveLock, SearchSysCacheCopy1, SysCacheGetAttr(), HeapTupleData::t_self, table_close(), table_open(), TextDatumGetCString, and values.

Referenced by ProcessUtilitySlow().

286 {
287  Relation rel;
288  Oid collOid;
289  HeapTuple tup;
290  Form_pg_collation collForm;
291  Datum collversion;
292  bool isnull;
293  char *oldversion;
294  char *newversion;
295  ObjectAddress address;
296 
297  rel = table_open(CollationRelationId, RowExclusiveLock);
298  collOid = get_collation_oid(stmt->collname, false);
299 
300  if (!pg_collation_ownercheck(collOid, GetUserId()))
302  NameListToString(stmt->collname));
303 
305  if (!HeapTupleIsValid(tup))
306  elog(ERROR, "cache lookup failed for collation %u", collOid);
307 
308  collForm = (Form_pg_collation) GETSTRUCT(tup);
309  collversion = SysCacheGetAttr(COLLOID, tup, Anum_pg_collation_collversion,
310  &isnull);
311  oldversion = isnull ? NULL : TextDatumGetCString(collversion);
312 
313  newversion = get_collation_actual_version(collForm->collprovider, NameStr(collForm->collcollate));
314 
315  /* cannot change from NULL to non-NULL or vice versa */
316  if ((!oldversion && newversion) || (oldversion && !newversion))
317  elog(ERROR, "invalid collation version change");
318  else if (oldversion && newversion && strcmp(newversion, oldversion) != 0)
319  {
320  bool nulls[Natts_pg_collation];
321  bool replaces[Natts_pg_collation];
322  Datum values[Natts_pg_collation];
323 
324  ereport(NOTICE,
325  (errmsg("changing version from %s to %s",
326  oldversion, newversion)));
327 
328  memset(values, 0, sizeof(values));
329  memset(nulls, false, sizeof(nulls));
330  memset(replaces, false, sizeof(replaces));
331 
332  values[Anum_pg_collation_collversion - 1] = CStringGetTextDatum(newversion);
333  replaces[Anum_pg_collation_collversion - 1] = true;
334 
335  tup = heap_modify_tuple(tup, RelationGetDescr(rel),
336  values, nulls, replaces);
337  }
338  else
339  ereport(NOTICE,
340  (errmsg("version has not changed")));
341 
342  CatalogTupleUpdate(rel, &tup->t_self, tup);
343 
344  InvokeObjectPostAlterHook(CollationRelationId, collOid, 0);
345 
346  ObjectAddressSet(address, CollationRelationId, collOid);
347 
348  heap_freetuple(tup);
349  table_close(rel, NoLock);
350 
351  return address;
352 }
bool pg_collation_ownercheck(Oid coll_oid, Oid roleid)
Definition: aclchk.c:5267
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167
#define GETSTRUCT(TUP)
Definition: htup_details.h:654
#define RelationGetDescr(relation)
Definition: rel.h:495
Oid GetUserId(void)
Definition: miscinit.c:478
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1338
unsigned int Oid
Definition: postgres_ext.h:31
void aclcheck_error(AclResult aclerr, ObjectType objtype, const char *objectname)
Definition: aclchk.c:3313
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
#define ERROR
Definition: elog.h:46
ItemPointerData t_self
Definition: htup.h:65
#define NoLock
Definition: lockdefs.h:34
#define RowExclusiveLock
Definition: lockdefs.h:38
#define InvokeObjectPostAlterHook(classId, objectId, subId)
Definition: objectaccess.h:175
char * NameListToString(List *names)
Definition: namespace.c:3101
#define TextDatumGetCString(d)
Definition: builtins.h:83
uintptr_t Datum
Definition: postgres.h:411
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:1388
#define ereport(elevel,...)
Definition: elog.h:157
#define NOTICE
Definition: elog.h:37
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
void CatalogTupleUpdate(Relation heapRel, ItemPointer otid, HeapTuple tup)
Definition: indexing.c:301
FormData_pg_collation * Form_pg_collation
Definition: pg_collation.h:56
#define ObjectAddressSet(addr, class_id, object_id)
Definition: objectaddress.h:40
static Datum values[MAXATTR]
Definition: bootstrap.c:166
#define SearchSysCacheCopy1(cacheId, key1)
Definition: syscache.h:175
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define elog(elevel,...)
Definition: elog.h:232
#define NameStr(name)
Definition: c.h:681
#define CStringGetTextDatum(s)
Definition: builtins.h:82
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
HeapTuple heap_modify_tuple(HeapTuple tuple, TupleDesc tupleDesc, Datum *replValues, bool *replIsnull, bool *doReplace)
Definition: heaptuple.c:1113
Oid get_collation_oid(List *name, bool missing_ok)
Definition: namespace.c:3600
char * get_collation_actual_version(char collprovider, const char *collcollate)
Definition: pg_locale.c:1645

◆ DefineCollation()

ObjectAddress DefineCollation ( ParseState pstate,
List names,
List parameters,
bool  if_not_exists 
)

Definition at line 53 of file collationcmds.c.

References ACL_CREATE, aclcheck_error(), ACLCHECK_OK, check_encoding_locale_matches(), CollationCreate(), COLLOID, CommandCounterIncrement(), defGetBoolean(), defGetQualifiedName(), defGetString(), DefElem::defname, elog, ereport, errcode(), errmsg(), ERROR, get_collation_actual_version(), get_collation_oid(), get_namespace_name(), GetDatabaseEncoding(), GETSTRUCT, GetUserId(), HeapTupleIsValid, InvalidObjectAddress, lc_collate_is_c(), lc_ctype_is_c(), lfirst_node, list_length(), DefElem::location, NameStr, OBJECT_SCHEMA, ObjectAddressSet, ObjectIdGetDatum, OidIsValid, parser_errposition(), pg_namespace_aclcheck(), pg_newlocale_from_collation(), pg_strcasecmp(), pstrdup(), QualifiedNameGetCreationNamespace(), ReleaseSysCache(), and SearchSysCache1().

Referenced by ProcessUtilitySlow().

54 {
55  char *collName;
56  Oid collNamespace;
57  AclResult aclresult;
58  ListCell *pl;
59  DefElem *fromEl = NULL;
60  DefElem *localeEl = NULL;
61  DefElem *lccollateEl = NULL;
62  DefElem *lcctypeEl = NULL;
63  DefElem *providerEl = NULL;
64  DefElem *deterministicEl = NULL;
65  DefElem *versionEl = NULL;
66  char *collcollate = NULL;
67  char *collctype = NULL;
68  char *collproviderstr = NULL;
69  bool collisdeterministic = true;
70  int collencoding = 0;
71  char collprovider = 0;
72  char *collversion = NULL;
73  Oid newoid;
74  ObjectAddress address;
75 
76  collNamespace = QualifiedNameGetCreationNamespace(names, &collName);
77 
78  aclresult = pg_namespace_aclcheck(collNamespace, GetUserId(), ACL_CREATE);
79  if (aclresult != ACLCHECK_OK)
80  aclcheck_error(aclresult, OBJECT_SCHEMA,
81  get_namespace_name(collNamespace));
82 
83  foreach(pl, parameters)
84  {
85  DefElem *defel = lfirst_node(DefElem, pl);
86  DefElem **defelp;
87 
88  if (strcmp(defel->defname, "from") == 0)
89  defelp = &fromEl;
90  else if (strcmp(defel->defname, "locale") == 0)
91  defelp = &localeEl;
92  else if (strcmp(defel->defname, "lc_collate") == 0)
93  defelp = &lccollateEl;
94  else if (strcmp(defel->defname, "lc_ctype") == 0)
95  defelp = &lcctypeEl;
96  else if (strcmp(defel->defname, "provider") == 0)
97  defelp = &providerEl;
98  else if (strcmp(defel->defname, "deterministic") == 0)
99  defelp = &deterministicEl;
100  else if (strcmp(defel->defname, "version") == 0)
101  defelp = &versionEl;
102  else
103  {
104  ereport(ERROR,
105  (errcode(ERRCODE_SYNTAX_ERROR),
106  errmsg("collation attribute \"%s\" not recognized",
107  defel->defname),
108  parser_errposition(pstate, defel->location)));
109  break;
110  }
111 
112  *defelp = defel;
113  }
114 
115  if ((localeEl && (lccollateEl || lcctypeEl))
116  || (fromEl && list_length(parameters) != 1))
117  ereport(ERROR,
118  (errcode(ERRCODE_SYNTAX_ERROR),
119  errmsg("conflicting or redundant options")));
120 
121  if (fromEl)
122  {
123  Oid collid;
124  HeapTuple tp;
125 
126  collid = get_collation_oid(defGetQualifiedName(fromEl), false);
128  if (!HeapTupleIsValid(tp))
129  elog(ERROR, "cache lookup failed for collation %u", collid);
130 
131  collcollate = pstrdup(NameStr(((Form_pg_collation) GETSTRUCT(tp))->collcollate));
132  collctype = pstrdup(NameStr(((Form_pg_collation) GETSTRUCT(tp))->collctype));
133  collprovider = ((Form_pg_collation) GETSTRUCT(tp))->collprovider;
134  collisdeterministic = ((Form_pg_collation) GETSTRUCT(tp))->collisdeterministic;
135  collencoding = ((Form_pg_collation) GETSTRUCT(tp))->collencoding;
136 
137  ReleaseSysCache(tp);
138 
139  /*
140  * Copying the "default" collation is not allowed because most code
141  * checks for DEFAULT_COLLATION_OID instead of COLLPROVIDER_DEFAULT,
142  * and so having a second collation with COLLPROVIDER_DEFAULT would
143  * not work and potentially confuse or crash some code. This could be
144  * fixed with some legwork.
145  */
146  if (collprovider == COLLPROVIDER_DEFAULT)
147  ereport(ERROR,
148  (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
149  errmsg("collation \"default\" cannot be copied")));
150  }
151 
152  if (localeEl)
153  {
154  collcollate = defGetString(localeEl);
155  collctype = defGetString(localeEl);
156  }
157 
158  if (lccollateEl)
159  collcollate = defGetString(lccollateEl);
160 
161  if (lcctypeEl)
162  collctype = defGetString(lcctypeEl);
163 
164  if (providerEl)
165  collproviderstr = defGetString(providerEl);
166 
167  if (deterministicEl)
168  collisdeterministic = defGetBoolean(deterministicEl);
169 
170  if (versionEl)
171  collversion = defGetString(versionEl);
172 
173  if (collproviderstr)
174  {
175  if (pg_strcasecmp(collproviderstr, "icu") == 0)
176  collprovider = COLLPROVIDER_ICU;
177  else if (pg_strcasecmp(collproviderstr, "libc") == 0)
178  collprovider = COLLPROVIDER_LIBC;
179  else
180  ereport(ERROR,
181  (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
182  errmsg("unrecognized collation provider: %s",
183  collproviderstr)));
184  }
185  else if (!fromEl)
186  collprovider = COLLPROVIDER_LIBC;
187 
188  if (!collcollate)
189  ereport(ERROR,
190  (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
191  errmsg("parameter \"lc_collate\" must be specified")));
192 
193  if (!collctype)
194  ereport(ERROR,
195  (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
196  errmsg("parameter \"lc_ctype\" must be specified")));
197 
198  /*
199  * Nondeterministic collations are currently only supported with ICU
200  * because that's the only case where it can actually make a difference.
201  * So we can save writing the code for the other providers.
202  */
203  if (!collisdeterministic && collprovider != COLLPROVIDER_ICU)
204  ereport(ERROR,
205  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
206  errmsg("nondeterministic collations not supported with this provider")));
207 
208  if (!fromEl)
209  {
210  if (collprovider == COLLPROVIDER_ICU)
211  collencoding = -1;
212  else
213  {
214  collencoding = GetDatabaseEncoding();
215  check_encoding_locale_matches(collencoding, collcollate, collctype);
216  }
217  }
218 
219  if (!collversion)
220  collversion = get_collation_actual_version(collprovider, collcollate);
221 
222  newoid = CollationCreate(collName,
223  collNamespace,
224  GetUserId(),
225  collprovider,
226  collisdeterministic,
227  collencoding,
228  collcollate,
229  collctype,
230  collversion,
231  if_not_exists,
232  false); /* not quiet */
233 
234  if (!OidIsValid(newoid))
235  return InvalidObjectAddress;
236 
237  /*
238  * Check that the locales can be loaded. NB: pg_newlocale_from_collation
239  * is only supposed to be called on non-C-equivalent locales.
240  */
242  if (!lc_collate_is_c(newoid) || !lc_ctype_is_c(newoid))
243  (void) pg_newlocale_from_collation(newoid);
244 
245  ObjectAddressSet(address, CollationRelationId, newoid);
246 
247  return address;
248 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:654
void check_encoding_locale_matches(int encoding, const char *collate, const char *ctype)
Definition: dbcommands.c:755
Oid GetUserId(void)
Definition: miscinit.c:478
Oid QualifiedNameGetCreationNamespace(List *names, char **objname_p)
Definition: namespace.c:2994
char * pstrdup(const char *in)
Definition: mcxt.c:1299
int errcode(int sqlerrcode)
Definition: elog.c:698
int pg_strcasecmp(const char *s1, const char *s2)
Definition: pgstrcasecmp.c:36
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:710
AclResult pg_namespace_aclcheck(Oid nsp_oid, Oid roleid, AclMode mode)
Definition: aclchk.c:4761
void aclcheck_error(AclResult aclerr, ObjectType objtype, const char *objectname)
Definition: aclchk.c:3313
bool defGetBoolean(DefElem *def)
Definition: define.c:111
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
#define ERROR
Definition: elog.h:46
#define ACL_CREATE
Definition: parsenodes.h:92
bool lc_collate_is_c(Oid collation)
Definition: pg_locale.c:1321
char * defGetString(DefElem *def)
Definition: define.c:49
#define lfirst_node(type, lc)
Definition: pg_list.h:172
char * get_namespace_name(Oid nspid)
Definition: lsyscache.c:3316
int location
Definition: parsenodes.h:749
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1127
pg_locale_t pg_newlocale_from_collation(Oid collid)
Definition: pg_locale.c:1468
AclResult
Definition: acl.h:177
void CommandCounterIncrement(void)
Definition: xact.c:1021
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1175
int GetDatabaseEncoding(void)
Definition: mbutils.c:1210
#define ereport(elevel,...)
Definition: elog.h:157
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
List * defGetQualifiedName(DefElem *def)
Definition: define.c:223
static int list_length(const List *l)
Definition: pg_list.h:149
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:111
Oid CollationCreate(const char *collname, Oid collnamespace, Oid collowner, char collprovider, bool collisdeterministic, int32 collencoding, const char *collcollate, const char *collctype, const char *collversion, bool if_not_exists, bool quiet)
Definition: pg_collation.c:46
FormData_pg_collation * Form_pg_collation
Definition: pg_collation.h:56
#define ObjectAddressSet(addr, class_id, object_id)
Definition: objectaddress.h:40
const ObjectAddress InvalidObjectAddress
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define elog(elevel,...)
Definition: elog.h:232
#define NameStr(name)
Definition: c.h:681
char * defname
Definition: parsenodes.h:746
bool lc_ctype_is_c(Oid collation)
Definition: pg_locale.c:1371
Oid get_collation_oid(List *name, bool missing_ok)
Definition: namespace.c:3600
char * get_collation_actual_version(char collprovider, const char *collcollate)
Definition: pg_locale.c:1645

◆ IsThereCollationInNamespace()

void IsThereCollationInNamespace ( const char *  collname,
Oid  nspOid 
)

Definition at line 257 of file collationcmds.c.

References COLLNAMEENCNSP, CStringGetDatum, ereport, errcode(), ERRCODE_DUPLICATE_OBJECT, errmsg(), ERROR, get_namespace_name(), GetDatabaseEncoding(), GetDatabaseEncodingName(), Int32GetDatum, ObjectIdGetDatum, and SearchSysCacheExists3.

Referenced by AlterObjectNamespace_internal(), and AlterObjectRename_internal().

258 {
259  /* make sure the name doesn't already exist in new schema */
261  CStringGetDatum(collname),
263  ObjectIdGetDatum(nspOid)))
264  ereport(ERROR,
266  errmsg("collation \"%s\" for encoding \"%s\" already exists in schema \"%s\"",
267  collname, GetDatabaseEncodingName(),
268  get_namespace_name(nspOid))));
269 
270  /* mustn't match an any-encoding entry, either */
272  CStringGetDatum(collname),
273  Int32GetDatum(-1),
274  ObjectIdGetDatum(nspOid)))
275  ereport(ERROR,
277  errmsg("collation \"%s\" already exists in schema \"%s\"",
278  collname, get_namespace_name(nspOid))));
279 }
int errcode(int sqlerrcode)
Definition: elog.c:698
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
#define ERROR
Definition: elog.h:46
#define SearchSysCacheExists3(cacheId, key1, key2, key3)
Definition: syscache.h:188
char * get_namespace_name(Oid nspid)
Definition: lsyscache.c:3316
#define CStringGetDatum(X)
Definition: postgres.h:622
int GetDatabaseEncoding(void)
Definition: mbutils.c:1210
#define ereport(elevel,...)
Definition: elog.h:157
const char * GetDatabaseEncodingName(void)
Definition: mbutils.c:1216
#define Int32GetDatum(X)
Definition: postgres.h:523
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define ERRCODE_DUPLICATE_OBJECT
Definition: streamutil.c:32