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

429{
430 Relation rel;
431 Oid collOid;
432 HeapTuple tup;
433 Form_pg_collation collForm;
434 Datum datum;
435 bool isnull;
436 char *oldversion;
437 char *newversion;
438 ObjectAddress address;
439
440 rel = table_open(CollationRelationId, RowExclusiveLock);
441 collOid = get_collation_oid(stmt->collname, false);
442
443 if (collOid == DEFAULT_COLLATION_OID)
445 (errmsg("cannot refresh version of default collation"),
446 /* translator: %s is an SQL command */
447 errhint("Use %s instead.",
448 "ALTER DATABASE ... REFRESH COLLATION VERSION")));
449
450 if (!object_ownercheck(CollationRelationId, collOid, GetUserId()))
452 NameListToString(stmt->collname));
453
454 tup = SearchSysCacheCopy1(COLLOID, ObjectIdGetDatum(collOid));
455 if (!HeapTupleIsValid(tup))
456 elog(ERROR, "cache lookup failed for collation %u", collOid);
457
458 collForm = (Form_pg_collation) GETSTRUCT(tup);
459 datum = SysCacheGetAttr(COLLOID, tup, Anum_pg_collation_collversion, &isnull);
460 oldversion = isnull ? NULL : TextDatumGetCString(datum);
461
462 if (collForm->collprovider == COLLPROVIDER_LIBC)
463 datum = SysCacheGetAttrNotNull(COLLOID, tup, Anum_pg_collation_collcollate);
464 else
465 datum = SysCacheGetAttrNotNull(COLLOID, tup, Anum_pg_collation_colllocale);
466
467 newversion = get_collation_actual_version(collForm->collprovider,
468 TextDatumGetCString(datum));
469
470 /* cannot change from NULL to non-NULL or vice versa */
471 if ((!oldversion && newversion) || (oldversion && !newversion))
472 elog(ERROR, "invalid collation version change");
473 else if (oldversion && newversion && strcmp(newversion, oldversion) != 0)
474 {
475 bool nulls[Natts_pg_collation];
476 bool replaces[Natts_pg_collation];
477 Datum values[Natts_pg_collation];
478
480 (errmsg("changing version from %s to %s",
481 oldversion, newversion)));
482
483 memset(values, 0, sizeof(values));
484 memset(nulls, false, sizeof(nulls));
485 memset(replaces, false, sizeof(replaces));
486
487 values[Anum_pg_collation_collversion - 1] = CStringGetTextDatum(newversion);
488 replaces[Anum_pg_collation_collversion - 1] = true;
489
490 tup = heap_modify_tuple(tup, RelationGetDescr(rel),
491 values, nulls, replaces);
492 }
493 else
495 (errmsg("version has not changed")));
496
497 CatalogTupleUpdate(rel, &tup->t_self, tup);
498
499 InvokeObjectPostAlterHook(CollationRelationId, collOid, 0);
500
501 ObjectAddressSet(address, CollationRelationId, collOid);
502
503 heap_freetuple(tup);
504 table_close(rel, NoLock);
505
506 return address;
507}
@ ACLCHECK_NOT_OWNER
Definition: acl.h:185
void aclcheck_error(AclResult aclerr, ObjectType objtype, const char *objectname)
Definition: aclchk.c:2654
bool object_ownercheck(Oid classid, Oid objectid, Oid roleid)
Definition: aclchk.c:4090
static Datum values[MAXATTR]
Definition: bootstrap.c:155
#define CStringGetTextDatum(s)
Definition: builtins.h:97
#define TextDatumGetCString(d)
Definition: builtins.h:98
int errhint(const char *fmt,...)
Definition: elog.c:1330
int errmsg(const char *fmt,...)
Definition: elog.c:1080
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:226
#define NOTICE
Definition: elog.h:35
#define ereport(elevel,...)
Definition: elog.h:150
HeapTuple heap_modify_tuple(HeapTuple tuple, TupleDesc tupleDesc, const Datum *replValues, const bool *replIsnull, const bool *doReplace)
Definition: heaptuple.c:1210
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1435
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
static void * GETSTRUCT(const HeapTupleData *tuple)
Definition: htup_details.h:728
#define stmt
Definition: indent_codes.h:59
void CatalogTupleUpdate(Relation heapRel, const ItemPointerData *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:469
char * NameListToString(const List *names)
Definition: namespace.c:3664
Oid get_collation_oid(List *collname, bool missing_ok)
Definition: namespace.c:4041
#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:2359
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:1247
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:262
uint64_t Datum
Definition: postgres.h:70
unsigned int Oid
Definition: postgres_ext.h:32
#define RelationGetDescr(relation)
Definition: rel.h:541
ItemPointerData t_self
Definition: htup.h:65
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:595
Datum SysCacheGetAttrNotNull(int cacheId, HeapTuple tup, AttrNumber attributeNumber)
Definition: syscache.c:625
#define SearchSysCacheCopy1(cacheId, key1)
Definition: syscache.h:91
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(), 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 57 of file collationcmds.c.

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

References ACL_CREATE, aclcheck_error(), ACLCHECK_OK, builtin_locale_encoding(), builtin_validate_locale(), check_encoding_locale_matches(), CollationCreate(), collid, 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, lfirst_node, list_length(), locale, 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 400 of file collationcmds.c.

401{
402 /* make sure the name doesn't already exist in new schema */
403 if (SearchSysCacheExists3(COLLNAMEENCNSP,
404 CStringGetDatum(collname),
406 ObjectIdGetDatum(nspOid)))
409 errmsg("collation \"%s\" for encoding \"%s\" already exists in schema \"%s\"",
410 collname, GetDatabaseEncodingName(),
411 get_namespace_name(nspOid))));
412
413 /* mustn't match an any-encoding entry, either */
414 if (SearchSysCacheExists3(COLLNAMEENCNSP,
415 CStringGetDatum(collname),
416 Int32GetDatum(-1),
417 ObjectIdGetDatum(nspOid)))
420 errmsg("collation \"%s\" already exists in schema \"%s\"",
421 collname, get_namespace_name(nspOid))));
422}
const char * GetDatabaseEncodingName(void)
Definition: mbutils.c:1270
static Datum CStringGetDatum(const char *X)
Definition: postgres.h:380
static Datum Int32GetDatum(int32 X)
Definition: postgres.h:222
#define ERRCODE_DUPLICATE_OBJECT
Definition: streamutil.c:30
#define SearchSysCacheExists3(cacheId, key1, key2, key3)
Definition: syscache.h:104

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

Referenced by AlterObjectNamespace_internal(), and AlterObjectRename_internal().