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 401 of file collationcmds.c.

402 {
403  Relation rel;
404  Oid collOid;
405  HeapTuple tup;
406  Form_pg_collation collForm;
407  Datum datum;
408  bool isnull;
409  char *oldversion;
410  char *newversion;
411  ObjectAddress address;
412 
413  rel = table_open(CollationRelationId, RowExclusiveLock);
414  collOid = get_collation_oid(stmt->collname, false);
415 
416  if (collOid == DEFAULT_COLLATION_OID)
417  ereport(ERROR,
418  (errmsg("cannot refresh version of default collation"),
419  errhint("Use ALTER DATABASE ... REFRESH COLLATION VERSION instead.")));
420 
421  if (!object_ownercheck(CollationRelationId, collOid, GetUserId()))
423  NameListToString(stmt->collname));
424 
426  if (!HeapTupleIsValid(tup))
427  elog(ERROR, "cache lookup failed for collation %u", collOid);
428 
429  collForm = (Form_pg_collation) GETSTRUCT(tup);
430  datum = SysCacheGetAttr(COLLOID, tup, Anum_pg_collation_collversion, &isnull);
431  oldversion = isnull ? NULL : TextDatumGetCString(datum);
432 
433  datum = SysCacheGetAttrNotNull(COLLOID, tup, collForm->collprovider == COLLPROVIDER_ICU ? Anum_pg_collation_colliculocale : Anum_pg_collation_collcollate);
434  newversion = get_collation_actual_version(collForm->collprovider, TextDatumGetCString(datum));
435 
436  /* cannot change from NULL to non-NULL or vice versa */
437  if ((!oldversion && newversion) || (oldversion && !newversion))
438  elog(ERROR, "invalid collation version change");
439  else if (oldversion && newversion && strcmp(newversion, oldversion) != 0)
440  {
441  bool nulls[Natts_pg_collation];
442  bool replaces[Natts_pg_collation];
443  Datum values[Natts_pg_collation];
444 
445  ereport(NOTICE,
446  (errmsg("changing version from %s to %s",
447  oldversion, newversion)));
448 
449  memset(values, 0, sizeof(values));
450  memset(nulls, false, sizeof(nulls));
451  memset(replaces, false, sizeof(replaces));
452 
453  values[Anum_pg_collation_collversion - 1] = CStringGetTextDatum(newversion);
454  replaces[Anum_pg_collation_collversion - 1] = true;
455 
456  tup = heap_modify_tuple(tup, RelationGetDescr(rel),
457  values, nulls, replaces);
458  }
459  else
460  ereport(NOTICE,
461  (errmsg("version has not changed")));
462 
463  CatalogTupleUpdate(rel, &tup->t_self, tup);
464 
465  InvokeObjectPostAlterHook(CollationRelationId, collOid, 0);
466 
467  ObjectAddressSet(address, CollationRelationId, collOid);
468 
469  heap_freetuple(tup);
470  table_close(rel, NoLock);
471 
472  return address;
473 }
@ ACLCHECK_NOT_OWNER
Definition: acl.h:185
void aclcheck_error(AclResult aclerr, ObjectType objtype, const char *objectname)
Definition: aclchk.c:2673
bool object_ownercheck(Oid classid, Oid objectid, Oid roleid)
Definition: aclchk.c:3976
static Datum values[MAXATTR]
Definition: bootstrap.c:156
#define CStringGetTextDatum(s)
Definition: builtins.h:94
#define TextDatumGetCString(d)
Definition: builtins.h:95
int errhint(const char *fmt,...)
Definition: elog.c:1316
int errmsg(const char *fmt,...)
Definition: elog.c:1069
#define ERROR
Definition: elog.h:39
#define NOTICE
Definition: elog.h:35
#define ereport(elevel,...)
Definition: elog.h:149
HeapTuple heap_modify_tuple(HeapTuple tuple, TupleDesc tupleDesc, Datum *replValues, bool *replIsnull, bool *doReplace)
Definition: heaptuple.c:1113
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1338
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define GETSTRUCT(TUP)
Definition: htup_details.h:653
#define stmt
Definition: indent_codes.h:59
void CatalogTupleUpdate(Relation heapRel, ItemPointer otid, HeapTuple tup)
Definition: indexing.c:313
#define NoLock
Definition: lockdefs.h:34
#define RowExclusiveLock
Definition: lockdefs.h:38
Oid GetUserId(void)
Definition: miscinit.c:510
Oid get_collation_oid(List *collname, bool missing_ok)
Definition: namespace.c:3648
char * NameListToString(List *names)
Definition: namespace.c:3145
#define InvokeObjectPostAlterHook(classId, objectId, subId)
Definition: objectaccess.h:197
#define ObjectAddressSet(addr, class_id, object_id)
Definition: objectaddress.h:40
@ OBJECT_COLLATION
Definition: parsenodes.h:2089
FormData_pg_collation * Form_pg_collation
Definition: pg_collation.h:58
char * get_collation_actual_version(char collprovider, const char *collcollate)
Definition: pg_locale.c:1649
uintptr_t Datum
Definition: postgres.h:64
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:252
unsigned int Oid
Definition: postgres_ext.h:31
#define RelationGetDescr(relation)
Definition: rel.h:530
ItemPointerData t_self
Definition: htup.h:65
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:1079
Datum SysCacheGetAttrNotNull(int cacheId, HeapTuple tup, AttrNumber attributeNumber)
Definition: syscache.c:1110
#define SearchSysCacheCopy1(cacheId, key1)
Definition: syscache.h:182
@ COLLOID
Definition: syscache.h:50
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:126
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:40

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

Referenced by ProcessUtilitySlow().

◆ DefineCollation()

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

Definition at line 55 of file collationcmds.c.

56 {
57  char *collName;
58  Oid collNamespace;
59  AclResult aclresult;
60  ListCell *pl;
61  DefElem *fromEl = NULL;
62  DefElem *localeEl = NULL;
63  DefElem *lccollateEl = NULL;
64  DefElem *lcctypeEl = NULL;
65  DefElem *providerEl = NULL;
66  DefElem *deterministicEl = NULL;
67  DefElem *rulesEl = NULL;
68  DefElem *versionEl = NULL;
69  char *collcollate;
70  char *collctype;
71  char *colliculocale;
72  char *collicurules;
73  bool collisdeterministic;
74  int collencoding;
75  char collprovider;
76  char *collversion = NULL;
77  Oid newoid;
78  ObjectAddress address;
79 
80  collNamespace = QualifiedNameGetCreationNamespace(names, &collName);
81 
82  aclresult = object_aclcheck(NamespaceRelationId, collNamespace, GetUserId(), ACL_CREATE);
83  if (aclresult != ACLCHECK_OK)
84  aclcheck_error(aclresult, OBJECT_SCHEMA,
85  get_namespace_name(collNamespace));
86 
87  foreach(pl, parameters)
88  {
89  DefElem *defel = lfirst_node(DefElem, pl);
90  DefElem **defelp;
91 
92  if (strcmp(defel->defname, "from") == 0)
93  defelp = &fromEl;
94  else if (strcmp(defel->defname, "locale") == 0)
95  defelp = &localeEl;
96  else if (strcmp(defel->defname, "lc_collate") == 0)
97  defelp = &lccollateEl;
98  else if (strcmp(defel->defname, "lc_ctype") == 0)
99  defelp = &lcctypeEl;
100  else if (strcmp(defel->defname, "provider") == 0)
101  defelp = &providerEl;
102  else if (strcmp(defel->defname, "deterministic") == 0)
103  defelp = &deterministicEl;
104  else if (strcmp(defel->defname, "rules") == 0)
105  defelp = &rulesEl;
106  else if (strcmp(defel->defname, "version") == 0)
107  defelp = &versionEl;
108  else
109  {
110  ereport(ERROR,
111  (errcode(ERRCODE_SYNTAX_ERROR),
112  errmsg("collation attribute \"%s\" not recognized",
113  defel->defname),
114  parser_errposition(pstate, defel->location)));
115  break;
116  }
117  if (*defelp != NULL)
118  errorConflictingDefElem(defel, pstate);
119  *defelp = defel;
120  }
121 
122  if (localeEl && (lccollateEl || lcctypeEl))
123  ereport(ERROR,
124  errcode(ERRCODE_SYNTAX_ERROR),
125  errmsg("conflicting or redundant options"),
126  errdetail("LOCALE cannot be specified together with LC_COLLATE or LC_CTYPE."));
127 
128  if (fromEl && list_length(parameters) != 1)
129  ereport(ERROR,
130  errcode(ERRCODE_SYNTAX_ERROR),
131  errmsg("conflicting or redundant options"),
132  errdetail("FROM cannot be specified together with any other options."));
133 
134  if (fromEl)
135  {
136  Oid collid;
137  HeapTuple tp;
138  Datum datum;
139  bool isnull;
140 
141  collid = get_collation_oid(defGetQualifiedName(fromEl), false);
143  if (!HeapTupleIsValid(tp))
144  elog(ERROR, "cache lookup failed for collation %u", collid);
145 
146  collprovider = ((Form_pg_collation) GETSTRUCT(tp))->collprovider;
147  collisdeterministic = ((Form_pg_collation) GETSTRUCT(tp))->collisdeterministic;
148  collencoding = ((Form_pg_collation) GETSTRUCT(tp))->collencoding;
149 
150  datum = SysCacheGetAttr(COLLOID, tp, Anum_pg_collation_collcollate, &isnull);
151  if (!isnull)
152  collcollate = TextDatumGetCString(datum);
153  else
154  collcollate = NULL;
155 
156  datum = SysCacheGetAttr(COLLOID, tp, Anum_pg_collation_collctype, &isnull);
157  if (!isnull)
158  collctype = TextDatumGetCString(datum);
159  else
160  collctype = NULL;
161 
162  datum = SysCacheGetAttr(COLLOID, tp, Anum_pg_collation_colliculocale, &isnull);
163  if (!isnull)
164  colliculocale = TextDatumGetCString(datum);
165  else
166  colliculocale = NULL;
167 
168  /*
169  * When the ICU locale comes from an existing collation, do not
170  * canonicalize to a language tag.
171  */
172 
173  datum = SysCacheGetAttr(COLLOID, tp, Anum_pg_collation_collicurules, &isnull);
174  if (!isnull)
175  collicurules = TextDatumGetCString(datum);
176  else
177  collicurules = NULL;
178 
179  ReleaseSysCache(tp);
180 
181  /*
182  * Copying the "default" collation is not allowed because most code
183  * checks for DEFAULT_COLLATION_OID instead of COLLPROVIDER_DEFAULT,
184  * and so having a second collation with COLLPROVIDER_DEFAULT would
185  * not work and potentially confuse or crash some code. This could be
186  * fixed with some legwork.
187  */
188  if (collprovider == COLLPROVIDER_DEFAULT)
189  ereport(ERROR,
190  (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
191  errmsg("collation \"default\" cannot be copied")));
192  }
193  else
194  {
195  char *collproviderstr = NULL;
196 
197  collcollate = NULL;
198  collctype = NULL;
199  colliculocale = NULL;
200  collicurules = NULL;
201 
202  if (providerEl)
203  collproviderstr = defGetString(providerEl);
204 
205  if (deterministicEl)
206  collisdeterministic = defGetBoolean(deterministicEl);
207  else
208  collisdeterministic = true;
209 
210  if (rulesEl)
211  collicurules = defGetString(rulesEl);
212 
213  if (versionEl)
214  collversion = defGetString(versionEl);
215 
216  if (collproviderstr)
217  {
218  if (pg_strcasecmp(collproviderstr, "icu") == 0)
219  collprovider = COLLPROVIDER_ICU;
220  else if (pg_strcasecmp(collproviderstr, "libc") == 0)
221  collprovider = COLLPROVIDER_LIBC;
222  else
223  ereport(ERROR,
224  (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
225  errmsg("unrecognized collation provider: %s",
226  collproviderstr)));
227  }
228  else
229  collprovider = COLLPROVIDER_LIBC;
230 
231  if (localeEl)
232  {
233  if (collprovider == COLLPROVIDER_LIBC)
234  {
235  collcollate = defGetString(localeEl);
236  collctype = defGetString(localeEl);
237  }
238  else
239  colliculocale = defGetString(localeEl);
240  }
241 
242  if (lccollateEl)
243  collcollate = defGetString(lccollateEl);
244 
245  if (lcctypeEl)
246  collctype = defGetString(lcctypeEl);
247 
248  if (collprovider == COLLPROVIDER_LIBC)
249  {
250  if (!collcollate)
251  ereport(ERROR,
252  (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
253  errmsg("parameter \"lc_collate\" must be specified")));
254 
255  if (!collctype)
256  ereport(ERROR,
257  (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
258  errmsg("parameter \"lc_ctype\" must be specified")));
259  }
260  else if (collprovider == COLLPROVIDER_ICU)
261  {
262  if (!colliculocale)
263  ereport(ERROR,
264  (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
265  errmsg("parameter \"locale\" must be specified")));
266 
267  /*
268  * During binary upgrade, preserve the locale string. Otherwise,
269  * canonicalize to a language tag.
270  */
271  if (!IsBinaryUpgrade)
272  {
273  char *langtag = icu_language_tag(colliculocale,
275 
276  if (langtag && strcmp(colliculocale, langtag) != 0)
277  {
278  ereport(NOTICE,
279  (errmsg("using standard form \"%s\" for locale \"%s\"",
280  langtag, colliculocale)));
281 
282  colliculocale = langtag;
283  }
284  }
285 
286  icu_validate_locale(colliculocale);
287  }
288 
289  /*
290  * Nondeterministic collations are currently only supported with ICU
291  * because that's the only case where it can actually make a
292  * difference. So we can save writing the code for the other
293  * providers.
294  */
295  if (!collisdeterministic && collprovider != COLLPROVIDER_ICU)
296  ereport(ERROR,
297  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
298  errmsg("nondeterministic collations not supported with this provider")));
299 
300  if (collicurules && collprovider != COLLPROVIDER_ICU)
301  ereport(ERROR,
302  (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
303  errmsg("ICU rules cannot be specified unless locale provider is ICU")));
304 
305  if (collprovider == COLLPROVIDER_ICU)
306  {
307 #ifdef USE_ICU
308  /*
309  * We could create ICU collations with collencoding == database
310  * encoding, but it seems better to use -1 so that it matches the
311  * way initdb would create ICU collations. However, only allow
312  * one to be created when the current database's encoding is
313  * supported. Otherwise the collation is useless, plus we get
314  * surprising behaviors like not being able to drop the collation.
315  *
316  * Skip this test when !USE_ICU, because the error we want to
317  * throw for that isn't thrown till later.
318  */
320  ereport(ERROR,
321  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
322  errmsg("current database's encoding is not supported with this provider")));
323 #endif
324  collencoding = -1;
325  }
326  else
327  {
328  collencoding = GetDatabaseEncoding();
329  check_encoding_locale_matches(collencoding, collcollate, collctype);
330  }
331  }
332 
333  if (!collversion)
334  collversion = get_collation_actual_version(collprovider, collprovider == COLLPROVIDER_ICU ? colliculocale : collcollate);
335 
336  newoid = CollationCreate(collName,
337  collNamespace,
338  GetUserId(),
339  collprovider,
340  collisdeterministic,
341  collencoding,
342  collcollate,
343  collctype,
344  colliculocale,
345  collicurules,
346  collversion,
347  if_not_exists,
348  false); /* not quiet */
349 
350  if (!OidIsValid(newoid))
351  return InvalidObjectAddress;
352 
353  /*
354  * Check that the locales can be loaded. NB: pg_newlocale_from_collation
355  * is only supposed to be called on non-C-equivalent locales.
356  */
358  if (!lc_collate_is_c(newoid) || !lc_ctype_is_c(newoid))
359  (void) pg_newlocale_from_collation(newoid);
360 
361  ObjectAddressSet(address, CollationRelationId, newoid);
362 
363  return address;
364 }
AclResult
Definition: acl.h:182
@ ACLCHECK_OK
Definition: acl.h:183
AclResult object_aclcheck(Oid classid, Oid objectid, Oid roleid, AclMode mode)
Definition: aclchk.c:3775
#define OidIsValid(objectId)
Definition: c.h:759
Oid collid
void check_encoding_locale_matches(int encoding, const char *collate, const char *ctype)
Definition: dbcommands.c:1489
bool defGetBoolean(DefElem *def)
Definition: define.c:108
List * defGetQualifiedName(DefElem *def)
Definition: define.c:253
char * defGetString(DefElem *def)
Definition: define.c:49
void errorConflictingDefElem(DefElem *defel, ParseState *pstate)
Definition: define.c:385
int errdetail(const char *fmt,...)
Definition: elog.c:1202
int errcode(int sqlerrcode)
Definition: elog.c:858
bool is_encoding_supported_by_icu(int encoding)
Definition: encnames.c:462
bool IsBinaryUpgrade
Definition: globals.c:114
char * get_namespace_name(Oid nspid)
Definition: lsyscache.c:3324
int GetDatabaseEncoding(void)
Definition: mbutils.c:1268
Oid QualifiedNameGetCreationNamespace(List *names, char **objname_p)
Definition: namespace.c:3038
const ObjectAddress InvalidObjectAddress
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:111
@ OBJECT_SCHEMA
Definition: parsenodes.h:2118
#define ACL_CREATE
Definition: parsenodes.h:92
Oid CollationCreate(const char *collname, Oid collnamespace, Oid collowner, char collprovider, bool collisdeterministic, int32 collencoding, const char *collcollate, const char *collctype, const char *colliculocale, const char *collicurules, const char *collversion, bool if_not_exists, bool quiet)
Definition: pg_collation.c:46
#define lfirst_node(type, lc)
Definition: pg_list.h:176
static int list_length(const List *l)
Definition: pg_list.h:152
int icu_validation_level
Definition: pg_locale.c:99
bool lc_collate_is_c(Oid collation)
Definition: pg_locale.c:1266
void icu_validate_locale(const char *loc_str)
Definition: pg_locale.c:2856
pg_locale_t pg_newlocale_from_collation(Oid collid)
Definition: pg_locale.c:1479
bool lc_ctype_is_c(Oid collation)
Definition: pg_locale.c:1319
char * icu_language_tag(const char *loc_str, int elevel)
Definition: pg_locale.c:2783
int pg_strcasecmp(const char *s1, const char *s2)
Definition: pgstrcasecmp.c:36
char * defname
Definition: parsenodes.h:810
int location
Definition: parsenodes.h:814
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:866
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:818
void CommandCounterIncrement(void)
Definition: xact.c:1078

References ACL_CREATE, aclcheck_error(), ACLCHECK_OK, check_encoding_locale_matches(), CollationCreate(), collid, COLLOID, CommandCounterIncrement(), defGetBoolean(), defGetQualifiedName(), defGetString(), DefElem::defname, elog(), ereport, errcode(), errdetail(), errmsg(), ERROR, errorConflictingDefElem(), get_collation_actual_version(), get_collation_oid(), get_namespace_name(), GetDatabaseEncoding(), GETSTRUCT, GetUserId(), HeapTupleIsValid, icu_language_tag(), icu_validate_locale(), icu_validation_level, InvalidObjectAddress, is_encoding_supported_by_icu(), IsBinaryUpgrade, lc_collate_is_c(), lc_ctype_is_c(), lfirst_node, list_length(), DefElem::location, NOTICE, object_aclcheck(), OBJECT_SCHEMA, ObjectAddressSet, ObjectIdGetDatum(), OidIsValid, parser_errposition(), pg_newlocale_from_collation(), pg_strcasecmp(), QualifiedNameGetCreationNamespace(), ReleaseSysCache(), SearchSysCache1(), SysCacheGetAttr(), and TextDatumGetCString.

Referenced by ProcessUtilitySlow().

◆ IsThereCollationInNamespace()

void IsThereCollationInNamespace ( const char *  collname,
Oid  nspOid 
)

Definition at line 373 of file collationcmds.c.

374 {
375  /* make sure the name doesn't already exist in new schema */
377  CStringGetDatum(collname),
379  ObjectIdGetDatum(nspOid)))
380  ereport(ERROR,
382  errmsg("collation \"%s\" for encoding \"%s\" already exists in schema \"%s\"",
383  collname, GetDatabaseEncodingName(),
384  get_namespace_name(nspOid))));
385 
386  /* mustn't match an any-encoding entry, either */
388  CStringGetDatum(collname),
389  Int32GetDatum(-1),
390  ObjectIdGetDatum(nspOid)))
391  ereport(ERROR,
393  errmsg("collation \"%s\" already exists in schema \"%s\"",
394  collname, get_namespace_name(nspOid))));
395 }
const char * GetDatabaseEncodingName(void)
Definition: mbutils.c:1274
static Datum CStringGetDatum(const char *X)
Definition: postgres.h:350
static Datum Int32GetDatum(int32 X)
Definition: postgres.h:212
#define ERRCODE_DUPLICATE_OBJECT
Definition: streamutil.c:32
@ COLLNAMEENCNSP
Definition: syscache.h:49
#define SearchSysCacheExists3(cacheId, key1, key2, key3)
Definition: syscache.h:195

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().