Age Owner Branch data TLA Line data Source code
1 : : /*-------------------------------------------------------------------------
2 : : *
3 : : * collationcmds.c
4 : : * collation-related commands support code
5 : : *
6 : : * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
7 : : * Portions Copyright (c) 1994, Regents of the University of California
8 : : *
9 : : *
10 : : * IDENTIFICATION
11 : : * src/backend/commands/collationcmds.c
12 : : *
13 : : *-------------------------------------------------------------------------
14 : : */
15 : : #include "postgres.h"
16 : :
17 : : #ifdef USE_ICU
18 : : #include <unicode/uloc.h>
19 : : #endif
20 : :
21 : : #include "access/htup_details.h"
22 : : #include "access/table.h"
23 : : #include "access/xact.h"
24 : : #include "catalog/indexing.h"
25 : : #include "catalog/namespace.h"
26 : : #include "catalog/objectaccess.h"
27 : : #include "catalog/pg_collation.h"
28 : : #include "catalog/pg_database.h"
29 : : #include "catalog/pg_namespace.h"
30 : : #include "commands/collationcmds.h"
31 : : #include "commands/comment.h"
32 : : #include "commands/dbcommands.h"
33 : : #include "commands/defrem.h"
34 : : #include "common/string.h"
35 : : #include "mb/pg_wchar.h"
36 : : #include "miscadmin.h"
37 : : #include "storage/fd.h"
38 : : #include "utils/acl.h"
39 : : #include "utils/builtins.h"
40 : : #include "utils/lsyscache.h"
41 : : #include "utils/pg_locale.h"
42 : : #include "utils/rel.h"
43 : : #include "utils/syscache.h"
44 : :
45 : :
46 : : typedef struct
47 : : {
48 : : char *localename; /* name of locale, as per "locale -a" */
49 : : char *alias; /* shortened alias for same */
50 : : int enc; /* encoding */
51 : : } CollAliasData;
52 : :
53 : :
54 : : /*
55 : : * CREATE COLLATION
56 : : */
57 : : ObjectAddress
3580 peter_e@gmx.net 58 :CBC 241 : DefineCollation(ParseState *pstate, List *names, List *parameters, bool if_not_exists)
59 : : {
60 : : char *collName;
61 : : Oid collNamespace;
62 : : AclResult aclresult;
63 : : ListCell *pl;
5711 bruce@momjian.us 64 : 241 : DefElem *fromEl = NULL;
65 : 241 : DefElem *localeEl = NULL;
66 : 241 : DefElem *lccollateEl = NULL;
67 : 241 : DefElem *lcctypeEl = NULL;
3537 peter_e@gmx.net 68 : 241 : DefElem *providerEl = NULL;
2808 peter@eisentraut.org 69 : 241 : DefElem *deterministicEl = NULL;
1361 70 : 241 : DefElem *rulesEl = NULL;
2031 tmunro@postgresql.or 71 : 241 : DefElem *versionEl = NULL;
72 : : char *collcollate;
73 : : char *collctype;
74 : : const char *colllocale;
75 : : char *collicurules;
76 : : bool collisdeterministic;
77 : : int collencoding;
78 : : char collprovider;
79 : 241 : char *collversion = NULL;
80 : : Oid newoid;
81 : : ObjectAddress address;
82 : :
5768 peter_e@gmx.net 83 : 241 : collNamespace = QualifiedNameGetCreationNamespace(names, &collName);
84 : :
1476 peter@eisentraut.org 85 : 241 : aclresult = object_aclcheck(NamespaceRelationId, collNamespace, GetUserId(), ACL_CREATE);
5768 peter_e@gmx.net 86 [ - + ]: 241 : if (aclresult != ACLCHECK_OK)
3283 peter_e@gmx.net 87 :UBC 0 : aclcheck_error(aclresult, OBJECT_SCHEMA,
5768 88 : 0 : get_namespace_name(collNamespace));
89 : :
5768 peter_e@gmx.net 90 [ + - + + :CBC 699 : foreach(pl, parameters)
+ + ]
91 : : {
3519 tgl@sss.pgh.pa.us 92 : 486 : DefElem *defel = lfirst_node(DefElem, pl);
93 : : DefElem **defelp;
94 : :
3228 95 [ + + ]: 486 : if (strcmp(defel->defname, "from") == 0)
5768 peter_e@gmx.net 96 : 39 : defelp = &fromEl;
3228 tgl@sss.pgh.pa.us 97 [ + + ]: 447 : else if (strcmp(defel->defname, "locale") == 0)
5768 peter_e@gmx.net 98 : 155 : defelp = &localeEl;
3228 tgl@sss.pgh.pa.us 99 [ + + ]: 292 : else if (strcmp(defel->defname, "lc_collate") == 0)
5768 peter_e@gmx.net 100 : 39 : defelp = &lccollateEl;
3228 tgl@sss.pgh.pa.us 101 [ + + ]: 253 : else if (strcmp(defel->defname, "lc_ctype") == 0)
5768 peter_e@gmx.net 102 : 35 : defelp = &lcctypeEl;
3228 tgl@sss.pgh.pa.us 103 [ + + ]: 218 : else if (strcmp(defel->defname, "provider") == 0)
3537 peter_e@gmx.net 104 : 154 : defelp = &providerEl;
2808 peter@eisentraut.org 105 [ + + ]: 64 : else if (strcmp(defel->defname, "deterministic") == 0)
106 : 33 : defelp = &deterministicEl;
1361 107 [ + + ]: 31 : else if (strcmp(defel->defname, "rules") == 0)
108 : 12 : defelp = &rulesEl;
2031 tmunro@postgresql.or 109 [ + + ]: 19 : else if (strcmp(defel->defname, "version") == 0)
110 : 15 : defelp = &versionEl;
111 : : else
112 : : {
5768 peter_e@gmx.net 113 [ + - ]: 4 : ereport(ERROR,
114 : : (errcode(ERRCODE_SYNTAX_ERROR),
115 : : errmsg("collation attribute \"%s\" not recognized",
116 : : defel->defname),
117 : : parser_errposition(pstate, defel->location)));
118 : : break;
119 : : }
1959 dean.a.rasheed@gmail 120 [ + + ]: 482 : if (*defelp != NULL)
121 : 24 : errorConflictingDefElem(defel, pstate);
5768 peter_e@gmx.net 122 : 458 : *defelp = defel;
123 : : }
124 : :
1959 dean.a.rasheed@gmail 125 [ + + + + : 213 : if (localeEl && (lccollateEl || lcctypeEl))
+ + ]
126 [ + - ]: 12 : ereport(ERROR,
127 : : errcode(ERRCODE_SYNTAX_ERROR),
128 : : errmsg("conflicting or redundant options"),
129 : : errdetail("LOCALE cannot be specified together with LC_COLLATE or LC_CTYPE."));
130 : :
131 [ + + + + ]: 201 : if (fromEl && list_length(parameters) != 1)
5768 peter_e@gmx.net 132 [ + - ]: 4 : ereport(ERROR,
133 : : errcode(ERRCODE_SYNTAX_ERROR),
134 : : errmsg("conflicting or redundant options"),
135 : : errdetail("FROM cannot be specified together with any other options."));
136 : :
137 [ + + ]: 197 : if (fromEl)
138 : : {
139 : : Oid collid;
140 : : HeapTuple tp;
141 : : Datum datum;
142 : : bool isnull;
143 : :
5711 bruce@momjian.us 144 : 35 : collid = get_collation_oid(defGetQualifiedName(fromEl), false);
5768 peter_e@gmx.net 145 : 31 : tp = SearchSysCache1(COLLOID, ObjectIdGetDatum(collid));
146 [ - + ]: 31 : if (!HeapTupleIsValid(tp))
5768 peter_e@gmx.net 147 [ # # ]:UBC 0 : elog(ERROR, "cache lookup failed for collation %u", collid);
148 : :
3537 peter_e@gmx.net 149 :CBC 31 : collprovider = ((Form_pg_collation) GETSTRUCT(tp))->collprovider;
2808 peter@eisentraut.org 150 : 31 : collisdeterministic = ((Form_pg_collation) GETSTRUCT(tp))->collisdeterministic;
3438 peter_e@gmx.net 151 : 31 : collencoding = ((Form_pg_collation) GETSTRUCT(tp))->collencoding;
152 : :
1766 peter@eisentraut.org 153 : 31 : datum = SysCacheGetAttr(COLLOID, tp, Anum_pg_collation_collcollate, &isnull);
154 [ + + ]: 31 : if (!isnull)
155 : 19 : collcollate = TextDatumGetCString(datum);
156 : : else
157 : 12 : collcollate = NULL;
158 : :
159 : 31 : datum = SysCacheGetAttr(COLLOID, tp, Anum_pg_collation_collctype, &isnull);
160 [ + + ]: 31 : if (!isnull)
161 : 19 : collctype = TextDatumGetCString(datum);
162 : : else
163 : 12 : collctype = NULL;
164 : :
994 jdavis@postgresql.or 165 : 31 : datum = SysCacheGetAttr(COLLOID, tp, Anum_pg_collation_colllocale, &isnull);
1717 peter@eisentraut.org 166 [ + + ]: 31 : if (!isnull)
994 jdavis@postgresql.or 167 : 8 : colllocale = TextDatumGetCString(datum);
168 : : else
169 : 23 : colllocale = NULL;
170 : :
171 : : /*
172 : : * When the ICU locale comes from an existing collation, do not
173 : : * canonicalize to a language tag.
174 : : */
175 : :
1361 peter@eisentraut.org 176 : 31 : datum = SysCacheGetAttr(COLLOID, tp, Anum_pg_collation_collicurules, &isnull);
177 [ - + ]: 31 : if (!isnull)
1361 peter@eisentraut.org 178 :UBC 0 : collicurules = TextDatumGetCString(datum);
179 : : else
1361 peter@eisentraut.org 180 :CBC 31 : collicurules = NULL;
181 : :
5768 peter_e@gmx.net 182 : 31 : ReleaseSysCache(tp);
183 : :
184 : : /*
185 : : * Copying the "default" collation is not allowed because most code
186 : : * checks for DEFAULT_COLLATION_OID instead of COLLPROVIDER_DEFAULT,
187 : : * and so having a second collation with COLLPROVIDER_DEFAULT would
188 : : * not work and potentially confuse or crash some code. This could be
189 : : * fixed with some legwork.
190 : : */
3455 191 [ + + ]: 31 : if (collprovider == COLLPROVIDER_DEFAULT)
192 [ + - ]: 4 : ereport(ERROR,
193 : : (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
194 : : errmsg("collation \"default\" cannot be copied")));
195 : : }
196 : : else
197 : : {
1723 peter@eisentraut.org 198 : 162 : char *collproviderstr = NULL;
199 : :
200 : 162 : collcollate = NULL;
201 : 162 : collctype = NULL;
994 jdavis@postgresql.or 202 : 162 : colllocale = NULL;
1361 peter@eisentraut.org 203 : 162 : collicurules = NULL;
204 : :
1723 205 [ + + ]: 162 : if (providerEl)
206 : 146 : collproviderstr = defGetString(providerEl);
207 : :
208 [ + + ]: 162 : if (deterministicEl)
209 : 25 : collisdeterministic = defGetBoolean(deterministicEl);
210 : : else
211 : 137 : collisdeterministic = true;
212 : :
1361 213 [ + + ]: 162 : if (rulesEl)
214 : 12 : collicurules = defGetString(rulesEl);
215 : :
1723 216 [ + + ]: 162 : if (versionEl)
217 : 3 : collversion = defGetString(versionEl);
218 : :
219 [ + + ]: 162 : if (collproviderstr)
220 : : {
990 jdavis@postgresql.or 221 [ + + ]: 146 : if (pg_strcasecmp(collproviderstr, "builtin") == 0)
222 : 42 : collprovider = COLLPROVIDER_BUILTIN;
223 [ + - ]: 104 : else if (pg_strcasecmp(collproviderstr, "icu") == 0)
1723 peter@eisentraut.org 224 : 104 : collprovider = COLLPROVIDER_ICU;
1723 peter@eisentraut.org 225 [ # # ]:UBC 0 : else if (pg_strcasecmp(collproviderstr, "libc") == 0)
226 : 0 : collprovider = COLLPROVIDER_LIBC;
227 : : else
228 [ # # ]: 0 : ereport(ERROR,
229 : : (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
230 : : errmsg("unrecognized collation provider: %s",
231 : : collproviderstr)));
232 : : }
233 : : else
1723 peter@eisentraut.org 234 :CBC 16 : collprovider = COLLPROVIDER_LIBC;
235 : :
1717 236 [ + + ]: 162 : if (localeEl)
237 : : {
238 [ + + ]: 135 : if (collprovider == COLLPROVIDER_LIBC)
239 : : {
240 : 1 : collcollate = defGetString(localeEl);
241 : 1 : collctype = defGetString(localeEl);
242 : : }
243 : : else
994 jdavis@postgresql.or 244 : 134 : colllocale = defGetString(localeEl);
245 : : }
246 : :
1717 peter@eisentraut.org 247 [ + + ]: 162 : if (lccollateEl)
248 : 23 : collcollate = defGetString(lccollateEl);
249 : :
250 [ + + ]: 162 : if (lcctypeEl)
251 : 19 : collctype = defGetString(lcctypeEl);
252 : :
990 jdavis@postgresql.or 253 [ + + ]: 162 : if (collprovider == COLLPROVIDER_BUILTIN)
254 : : {
255 [ + + ]: 42 : if (!colllocale)
256 [ + - ]: 8 : ereport(ERROR,
257 : : (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
258 : : errmsg("parameter \"%s\" must be specified",
259 : : "locale")));
260 : :
261 : 34 : colllocale = builtin_validate_locale(GetDatabaseEncoding(),
262 : : colllocale);
263 : : }
264 [ + + ]: 120 : else if (collprovider == COLLPROVIDER_LIBC)
265 : : {
1717 peter@eisentraut.org 266 [ - + ]: 16 : if (!collcollate)
1717 peter@eisentraut.org 267 [ # # ]:UBC 0 : ereport(ERROR,
268 : : (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
269 : : errmsg("parameter \"%s\" must be specified",
270 : : "lc_collate")));
271 : :
1717 peter@eisentraut.org 272 [ - + ]:CBC 16 : if (!collctype)
1717 peter@eisentraut.org 273 [ # # ]:UBC 0 : ereport(ERROR,
274 : : (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
275 : : errmsg("parameter \"%s\" must be specified",
276 : : "lc_ctype")));
277 : : }
1717 peter@eisentraut.org 278 [ + - ]:CBC 104 : else if (collprovider == COLLPROVIDER_ICU)
279 : : {
994 jdavis@postgresql.or 280 [ + + ]: 104 : if (!colllocale)
1717 peter@eisentraut.org 281 [ + - ]: 4 : ereport(ERROR,
282 : : (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
283 : : errmsg("parameter \"%s\" must be specified",
284 : : "locale")));
285 : :
286 : : /*
287 : : * During binary upgrade, preserve the locale string. Otherwise,
288 : : * canonicalize to a language tag.
289 : : */
1334 jdavis@postgresql.or 290 [ + + ]: 100 : if (!IsBinaryUpgrade)
291 : : {
994 292 : 99 : char *langtag = icu_language_tag(colllocale,
293 : : icu_validation_level);
294 : :
295 [ + + + + ]: 95 : if (langtag && strcmp(colllocale, langtag) != 0)
296 : : {
1334 297 [ + + ]: 67 : ereport(NOTICE,
298 : : (errmsg("using standard form \"%s\" for ICU locale \"%s\"",
299 : : langtag, colllocale)));
300 : :
994 301 : 67 : colllocale = langtag;
302 : : }
303 : : }
304 : :
305 : 96 : icu_validate_locale(colllocale);
306 : : }
307 : :
308 : : /*
309 : : * Nondeterministic collations are currently only supported with ICU
310 : : * because that's the only case where it can actually make a
311 : : * difference. So we can save writing the code for the other
312 : : * providers.
313 : : */
1723 peter@eisentraut.org 314 [ + + - + ]: 126 : if (!collisdeterministic && collprovider != COLLPROVIDER_ICU)
1723 peter@eisentraut.org 315 [ # # ]:UBC 0 : ereport(ERROR,
316 : : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
317 : : errmsg("nondeterministic collations not supported with this provider")));
318 : :
1360 peter@eisentraut.org 319 [ + + - + ]:CBC 126 : if (collicurules && collprovider != COLLPROVIDER_ICU)
1360 peter@eisentraut.org 320 [ # # ]:UBC 0 : ereport(ERROR,
321 : : (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
322 : : errmsg("ICU rules cannot be specified unless locale provider is ICU")));
323 : :
990 jdavis@postgresql.or 324 [ + + ]:CBC 126 : if (collprovider == COLLPROVIDER_BUILTIN)
325 : : {
985 326 : 22 : collencoding = builtin_locale_encoding(colllocale);
327 : : }
990 328 [ + + ]: 104 : else if (collprovider == COLLPROVIDER_ICU)
329 : : {
330 : : #ifdef USE_ICU
331 : : /*
332 : : * We could create ICU collations with collencoding == database
333 : : * encoding, but it seems better to use -1 so that it matches the
334 : : * way initdb would create ICU collations. However, only allow
335 : : * one to be created when the current database's encoding is
336 : : * supported. Otherwise the collation is useless, plus we get
337 : : * surprising behaviors like not being able to drop the collation.
338 : : *
339 : : * Skip this test when !USE_ICU, because the error we want to
340 : : * throw for that isn't thrown till later.
341 : : */
1912 tgl@sss.pgh.pa.us 342 [ - + ]: 88 : if (!is_encoding_supported_by_icu(GetDatabaseEncoding()))
1912 tgl@sss.pgh.pa.us 343 [ # # ]:UBC 0 : ereport(ERROR,
344 : : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
345 : : errmsg("current database's encoding is not supported with this provider")));
346 : : #endif
3438 peter_e@gmx.net 347 :CBC 88 : collencoding = -1;
348 : : }
349 : : else
350 : : {
351 : 16 : collencoding = GetDatabaseEncoding();
352 : 16 : check_encoding_locale_matches(collencoding, collcollate, collctype);
353 : : }
354 : : }
355 : :
2031 tmunro@postgresql.or 356 [ + + ]: 153 : if (!collversion)
357 : : {
358 : : const char *locale;
359 : :
990 jdavis@postgresql.or 360 [ + + ]: 150 : if (collprovider == COLLPROVIDER_LIBC)
361 : 35 : locale = collcollate;
362 : : else
363 : 115 : locale = colllocale;
364 : :
365 : 150 : collversion = get_collation_actual_version(collprovider, locale);
366 : : }
367 : :
5748 peter_e@gmx.net 368 : 153 : newoid = CollationCreate(collName,
369 : : collNamespace,
370 : : GetUserId(),
371 : : collprovider,
372 : : collisdeterministic,
373 : : collencoding,
374 : : collcollate,
375 : : collctype,
376 : : colllocale,
377 : : collicurules,
378 : : collversion,
379 : : if_not_exists,
380 : : false); /* not quiet */
381 : :
3601 382 [ + + ]: 148 : if (!OidIsValid(newoid))
383 : 1 : return InvalidObjectAddress;
384 : :
385 : : /* Check that the locales can be loaded. */
5748 386 : 147 : CommandCounterIncrement();
815 jdavis@postgresql.or 387 : 147 : (void) pg_newlocale_from_collation(newoid);
388 : :
3406 tgl@sss.pgh.pa.us 389 : 143 : ObjectAddressSet(address, CollationRelationId, newoid);
390 : :
4288 alvherre@alvh.no-ip. 391 : 143 : return address;
392 : : }
393 : :
394 : : /*
395 : : * Subroutine for ALTER COLLATION SET SCHEMA and RENAME
396 : : *
397 : : * Is there a collation with the same name of the given collation already in
398 : : * the given namespace? If so, raise an appropriate error message.
399 : : */
400 : : void
5065 401 : 12 : IsThereCollationInNamespace(const char *collname, Oid nspOid)
402 : : {
403 : : /* make sure the name doesn't already exist in new schema */
5741 tgl@sss.pgh.pa.us 404 [ - + ]: 12 : if (SearchSysCacheExists3(COLLNAMEENCNSP,
405 : : CStringGetDatum(collname),
406 : : Int32GetDatum(GetDatabaseEncoding()),
407 : : ObjectIdGetDatum(nspOid)))
5741 tgl@sss.pgh.pa.us 408 [ # # ]:UBC 0 : ereport(ERROR,
409 : : (errcode(ERRCODE_DUPLICATE_OBJECT),
410 : : errmsg("collation \"%s\" for encoding \"%s\" already exists in schema \"%s\"",
411 : : collname, GetDatabaseEncodingName(),
412 : : get_namespace_name(nspOid))));
413 : :
414 : : /* mustn't match an any-encoding entry, either */
5741 tgl@sss.pgh.pa.us 415 [ + + ]:CBC 12 : if (SearchSysCacheExists3(COLLNAMEENCNSP,
416 : : CStringGetDatum(collname),
417 : : Int32GetDatum(-1),
418 : : ObjectIdGetDatum(nspOid)))
419 [ + - ]: 4 : ereport(ERROR,
420 : : (errcode(ERRCODE_DUPLICATE_OBJECT),
421 : : errmsg("collation \"%s\" already exists in schema \"%s\"",
422 : : collname, get_namespace_name(nspOid))));
5768 peter_e@gmx.net 423 : 8 : }
424 : :
425 : : /*
426 : : * ALTER COLLATION
427 : : */
428 : : ObjectAddress
2031 tmunro@postgresql.or 429 : 4 : AlterCollation(AlterCollationStmt *stmt)
430 : : {
431 : : Relation rel;
432 : : Oid collOid;
433 : : HeapTuple tup;
434 : : Form_pg_collation collForm;
435 : : Datum datum;
436 : : bool isnull;
437 : : char *oldversion;
438 : : char *newversion;
439 : : ObjectAddress address;
440 : :
441 : 4 : rel = table_open(CollationRelationId, RowExclusiveLock);
442 : 4 : collOid = get_collation_oid(stmt->collname, false);
443 : :
1491 jdavis@postgresql.or 444 [ - + ]: 4 : if (collOid == DEFAULT_COLLATION_OID)
1491 jdavis@postgresql.or 445 [ # # ]:UBC 0 : ereport(ERROR,
446 : : (errcode(ERRCODE_WRONG_OBJECT_TYPE),
447 : : errmsg("cannot refresh version of default collation"),
448 : : /* translator: %s is an SQL command */
449 : : errhint("Use %s instead.",
450 : : "ALTER DATABASE ... REFRESH COLLATION VERSION")));
451 : :
1476 peter@eisentraut.org 452 [ - + ]:CBC 4 : if (!object_ownercheck(CollationRelationId, collOid, GetUserId()))
2031 tmunro@postgresql.or 453 :UBC 0 : aclcheck_error(ACLCHECK_NOT_OWNER, OBJECT_COLLATION,
454 : 0 : NameListToString(stmt->collname));
455 : :
2031 tmunro@postgresql.or 456 :CBC 4 : tup = SearchSysCacheCopy1(COLLOID, ObjectIdGetDatum(collOid));
457 [ - + ]: 4 : if (!HeapTupleIsValid(tup))
2031 tmunro@postgresql.or 458 [ # # ]:UBC 0 : elog(ERROR, "cache lookup failed for collation %u", collOid);
459 : :
2031 tmunro@postgresql.or 460 :CBC 4 : collForm = (Form_pg_collation) GETSTRUCT(tup);
1766 peter@eisentraut.org 461 : 4 : datum = SysCacheGetAttr(COLLOID, tup, Anum_pg_collation_collversion, &isnull);
462 [ + - ]: 4 : oldversion = isnull ? NULL : TextDatumGetCString(datum);
463 : :
990 jdavis@postgresql.or 464 [ - + ]: 4 : if (collForm->collprovider == COLLPROVIDER_LIBC)
990 jdavis@postgresql.or 465 :UBC 0 : datum = SysCacheGetAttrNotNull(COLLOID, tup, Anum_pg_collation_collcollate);
466 : : else
990 jdavis@postgresql.or 467 :CBC 4 : datum = SysCacheGetAttrNotNull(COLLOID, tup, Anum_pg_collation_colllocale);
468 : :
469 : 4 : newversion = get_collation_actual_version(collForm->collprovider,
470 : 4 : TextDatumGetCString(datum));
471 : :
472 : : /* cannot change from NULL to non-NULL or vice versa */
2031 tmunro@postgresql.or 473 [ - + - - : 4 : if ((!oldversion && newversion) || (oldversion && !newversion))
+ - - + ]
2031 tmunro@postgresql.or 474 [ # # ]:UBC 0 : elog(ERROR, "invalid collation version change");
2031 tmunro@postgresql.or 475 [ + - + - :CBC 4 : else if (oldversion && newversion && strcmp(newversion, oldversion) != 0)
- + ]
2031 tmunro@postgresql.or 476 :UBC 0 : {
477 : : bool nulls[Natts_pg_collation];
478 : : bool replaces[Natts_pg_collation];
479 : : Datum values[Natts_pg_collation];
480 : :
481 [ # # ]: 0 : ereport(NOTICE,
482 : : (errmsg("changing version from %s to %s",
483 : : oldversion, newversion)));
484 : :
485 : 0 : memset(values, 0, sizeof(values));
486 : 0 : memset(nulls, false, sizeof(nulls));
487 : 0 : memset(replaces, false, sizeof(replaces));
488 : :
489 : 0 : values[Anum_pg_collation_collversion - 1] = CStringGetTextDatum(newversion);
490 : 0 : replaces[Anum_pg_collation_collversion - 1] = true;
491 : :
492 : 0 : tup = heap_modify_tuple(tup, RelationGetDescr(rel),
493 : : values, nulls, replaces);
494 : : }
495 : : else
2031 tmunro@postgresql.or 496 [ + - ]:CBC 4 : ereport(NOTICE,
497 : : (errmsg("version has not changed")));
498 : :
499 : 4 : CatalogTupleUpdate(rel, &tup->t_self, tup);
500 : :
501 [ - + ]: 4 : InvokeObjectPostAlterHook(CollationRelationId, collOid, 0);
502 : :
503 : 4 : ObjectAddressSet(address, CollationRelationId, collOid);
504 : :
505 : 4 : heap_freetuple(tup);
506 : 4 : table_close(rel, NoLock);
507 : :
508 : 4 : return address;
509 : : }
510 : :
511 : :
512 : : Datum
2101 513 : 58 : pg_collation_actual_version(PG_FUNCTION_ARGS)
514 : : {
1289 tgl@sss.pgh.pa.us 515 : 58 : Oid collid = PG_GETARG_OID(0);
516 : : char provider;
517 : : char *locale;
518 : : char *version;
519 : : Datum datum;
520 : :
1491 jdavis@postgresql.or 521 [ - + ]: 58 : if (collid == DEFAULT_COLLATION_OID)
522 : : {
523 : : /* retrieve from pg_database */
524 : :
1491 jdavis@postgresql.or 525 :UBC 0 : HeapTuple dbtup = SearchSysCache1(DATABASEOID, ObjectIdGetDatum(MyDatabaseId));
526 : :
527 [ # # ]: 0 : if (!HeapTupleIsValid(dbtup))
528 [ # # ]: 0 : ereport(ERROR,
529 : : (errcode(ERRCODE_UNDEFINED_OBJECT),
530 : : errmsg("database with OID %u does not exist", MyDatabaseId)));
531 : :
532 : 0 : provider = ((Form_pg_database) GETSTRUCT(dbtup))->datlocprovider;
533 : :
990 534 [ # # ]: 0 : if (provider == COLLPROVIDER_LIBC)
535 : : {
536 : 0 : datum = SysCacheGetAttrNotNull(DATABASEOID, dbtup, Anum_pg_database_datcollate);
537 : 0 : locale = TextDatumGetCString(datum);
538 : : }
539 : : else
540 : : {
541 : 0 : datum = SysCacheGetAttrNotNull(DATABASEOID, dbtup, Anum_pg_database_datlocale);
542 : 0 : locale = TextDatumGetCString(datum);
543 : : }
544 : :
1491 545 : 0 : ReleaseSysCache(dbtup);
546 : : }
547 : : else
548 : : {
549 : : /* retrieve from pg_collation */
550 : :
1289 tgl@sss.pgh.pa.us 551 :CBC 58 : HeapTuple colltp = SearchSysCache1(COLLOID, ObjectIdGetDatum(collid));
552 : :
1491 jdavis@postgresql.or 553 [ - + ]: 58 : if (!HeapTupleIsValid(colltp))
1491 jdavis@postgresql.or 554 [ # # ]:UBC 0 : ereport(ERROR,
555 : : (errcode(ERRCODE_UNDEFINED_OBJECT),
556 : : errmsg("collation with OID %u does not exist", collid)));
557 : :
1491 jdavis@postgresql.or 558 :CBC 58 : provider = ((Form_pg_collation) GETSTRUCT(colltp))->collprovider;
559 [ - + ]: 58 : Assert(provider != COLLPROVIDER_DEFAULT);
560 : :
990 561 [ - + ]: 58 : if (provider == COLLPROVIDER_LIBC)
562 : : {
990 jdavis@postgresql.or 563 :UBC 0 : datum = SysCacheGetAttrNotNull(COLLOID, colltp, Anum_pg_collation_collcollate);
564 : 0 : locale = TextDatumGetCString(datum);
565 : : }
566 : : else
567 : : {
990 jdavis@postgresql.or 568 :CBC 58 : datum = SysCacheGetAttrNotNull(COLLOID, colltp, Anum_pg_collation_colllocale);
569 : 58 : locale = TextDatumGetCString(datum);
570 : : }
571 : :
1491 572 : 58 : ReleaseSysCache(colltp);
573 : : }
574 : :
575 : 58 : version = get_collation_actual_version(provider, locale);
3537 peter_e@gmx.net 576 [ + - ]: 58 : if (version)
577 : 58 : PG_RETURN_TEXT_P(cstring_to_text(version));
578 : : else
3537 peter_e@gmx.net 579 :UBC 0 : PG_RETURN_NULL();
580 : : }
581 : :
582 : :
583 : : /* will we use "locale -a" in pg_import_system_collations? */
584 : : #if !defined(WIN32)
585 : : #define READ_LOCALE_A_OUTPUT
586 : : #endif
587 : :
588 : : /* will we use EnumSystemLocalesEx in pg_import_system_collations? */
589 : : #ifdef WIN32
590 : : #define ENUM_SYSTEM_LOCALE
591 : : #endif
592 : :
593 : :
594 : : #ifdef READ_LOCALE_A_OUTPUT
595 : : /*
596 : : * "Normalize" a libc locale name, stripping off encoding tags such as
597 : : * ".utf8" (e.g., "en_US.utf8" -> "en_US", but "br_FR.iso885915@euro"
598 : : * -> "br_FR@euro"). Return true if a new, different name was
599 : : * generated.
600 : : */
601 : : static bool
3537 peter_e@gmx.net 602 :CBC 116 : normalize_libc_locale_name(char *new, const char *old)
603 : : {
3601 604 : 116 : char *n = new;
605 : 116 : const char *o = old;
606 : 116 : bool changed = false;
607 : :
608 [ + + ]: 580 : while (*o)
609 : : {
610 [ + + ]: 464 : if (*o == '.')
611 : : {
612 : : /* skip over encoding tag such as ".utf8" or ".UTF-8" */
613 : 116 : o++;
614 [ - + ]: 464 : while ((*o >= 'A' && *o <= 'Z')
615 [ + + + - ]: 580 : || (*o >= 'a' && *o <= 'z')
616 [ + + + - ]: 232 : || (*o >= '0' && *o <= '9')
617 [ + + - + ]: 696 : || (*o == '-'))
618 : 464 : o++;
619 : 116 : changed = true;
620 : : }
621 : : else
622 : 348 : *n++ = *o++;
623 : : }
624 : 116 : *n = '\0';
625 : :
626 : 116 : return changed;
627 : : }
628 : :
629 : : /*
630 : : * qsort comparator for CollAliasData items
631 : : */
632 : : static int
3445 tgl@sss.pgh.pa.us 633 : 58 : cmpaliases(const void *a, const void *b)
634 : : {
635 : 58 : const CollAliasData *ca = (const CollAliasData *) a;
636 : 58 : const CollAliasData *cb = (const CollAliasData *) b;
637 : :
638 : : /* comparing localename is enough because other fields are derived */
639 : 58 : return strcmp(ca->localename, cb->localename);
640 : : }
641 : : #endif /* READ_LOCALE_A_OUTPUT */
642 : :
643 : :
644 : : #ifdef USE_ICU
645 : : /*
646 : : * Get a comment (specifically, the display name) for an ICU locale.
647 : : * The result is a palloc'd string, or NULL if we can't get a comment
648 : : * or find that it's not all ASCII. (We can *not* accept non-ASCII
649 : : * comments, because the contents of template0 must be encoding-agnostic.)
650 : : */
651 : : static char *
3537 peter_e@gmx.net 652 : 50460 : get_icu_locale_comment(const char *localename)
653 : : {
654 : : UErrorCode status;
655 : : UChar displayname[128];
656 : : int32 len_uchar;
657 : : int32 i;
658 : : char *result;
659 : :
660 : 50460 : status = U_ZERO_ERROR;
3445 tgl@sss.pgh.pa.us 661 : 50460 : len_uchar = uloc_getDisplayName(localename, "en",
662 : : displayname, lengthof(displayname),
663 : : &status);
3537 peter_e@gmx.net 664 [ - + ]: 50460 : if (U_FAILURE(status))
3444 tgl@sss.pgh.pa.us 665 :UBC 0 : return NULL; /* no good reason to raise an error */
666 : :
667 : : /* Check for non-ASCII comment (can't use pg_is_ascii for this) */
3444 tgl@sss.pgh.pa.us 668 [ + + ]:CBC 853760 : for (i = 0; i < len_uchar; i++)
669 : : {
670 [ + + ]: 804286 : if (displayname[i] > 127)
671 : 986 : return NULL;
672 : : }
673 : :
674 : : /* OK, transcribe */
675 : 49474 : result = palloc(len_uchar + 1);
676 [ + + ]: 844538 : for (i = 0; i < len_uchar; i++)
677 : 795064 : result[i] = displayname[i];
678 : 49474 : result[len_uchar] = '\0';
679 : :
3537 peter_e@gmx.net 680 : 49474 : return result;
681 : : }
682 : : #endif /* USE_ICU */
683 : :
684 : :
685 : : /*
686 : : * Create a new collation using the input locale 'locale'. (subroutine for
687 : : * pg_import_system_collations())
688 : : *
689 : : * 'nspid' is the namespace id where the collation will be created.
690 : : *
691 : : * 'nvalidp' is incremented if the locale has a valid encoding.
692 : : *
693 : : * 'ncreatedp' is incremented if the collation is actually created. If the
694 : : * collation already exists it will quietly do nothing.
695 : : *
696 : : * The returned value is the encoding of the locale, -1 if the locale is not
697 : : * valid for creating a collation.
698 : : *
699 : : */
700 : : pg_attribute_unused()
701 : : static int
1425 peter@eisentraut.org 702 : 232 : create_collation_from_locale(const char *locale, int nspid,
703 : : int *nvalidp, int *ncreatedp)
704 : : {
705 : : int enc;
706 : : Oid collid;
707 : :
708 : : /*
709 : : * Some systems have locale names that don't consist entirely of ASCII
710 : : * letters (such as "bokmål" or "français"). This is pretty
711 : : * silly, since we need the locale itself to interpret the non-ASCII
712 : : * characters. We can't do much with those, so we filter them out.
713 : : */
714 [ - + ]: 232 : if (!pg_is_ascii(locale))
715 : : {
1425 peter@eisentraut.org 716 [ # # ]:UBC 0 : elog(DEBUG1, "skipping locale with non-ASCII name: \"%s\"", locale);
717 : 0 : return -1;
718 : : }
719 : :
1425 peter@eisentraut.org 720 :CBC 232 : enc = pg_get_encoding_from_locale(locale, false);
721 [ - + ]: 232 : if (enc < 0)
722 : : {
1425 peter@eisentraut.org 723 [ # # ]:UBC 0 : elog(DEBUG1, "skipping locale with unrecognized encoding: \"%s\"", locale);
724 : 0 : return -1;
725 : : }
1425 peter@eisentraut.org 726 [ + - + - :CBC 232 : if (!PG_VALID_BE_ENCODING(enc))
- + ]
727 : : {
1425 peter@eisentraut.org 728 [ # # ]:UBC 0 : elog(DEBUG1, "skipping locale with client-only encoding: \"%s\"", locale);
729 : 0 : return -1;
730 : : }
1425 peter@eisentraut.org 731 [ + + ]:CBC 232 : if (enc == PG_SQL_ASCII)
1289 tgl@sss.pgh.pa.us 732 : 116 : return -1; /* C/POSIX are already in the catalog */
733 : :
734 : : /* count valid locales found in operating system */
1425 peter@eisentraut.org 735 : 116 : (*nvalidp)++;
736 : :
737 : : /*
738 : : * Create a collation named the same as the locale, but quietly doing
739 : : * nothing if it already exists. This is the behavior we need even at
740 : : * initdb time, because some versions of "locale -a" can report the same
741 : : * locale name more than once. And it's convenient for later import runs,
742 : : * too, since you just about always want to add on new locales without a
743 : : * lot of chatter about existing ones.
744 : : */
745 : 116 : collid = CollationCreate(locale, nspid, GetUserId(),
746 : : COLLPROVIDER_LIBC, true, enc,
747 : : locale, locale, NULL, NULL,
748 : 116 : get_collation_actual_version(COLLPROVIDER_LIBC, locale),
749 : : true, true);
750 [ + - ]: 116 : if (OidIsValid(collid))
751 : : {
752 : 116 : (*ncreatedp)++;
753 : :
754 : : /* Must do CCI between inserts to handle duplicates correctly */
755 : 116 : CommandCounterIncrement();
756 : : }
757 : :
758 : 116 : return enc;
759 : : }
760 : :
761 : :
762 : : #ifdef ENUM_SYSTEM_LOCALE
763 : : /* parameter to be passed to the callback function win32_read_locale() */
764 : : typedef struct
765 : : {
766 : : Oid nspid;
767 : : int *ncreatedp;
768 : : int *nvalidp;
769 : : } CollParam;
770 : :
771 : : /*
772 : : * Callback function for EnumSystemLocalesEx() in
773 : : * pg_import_system_collations(). Creates a collation for every valid locale
774 : : * and a POSIX alias collation.
775 : : *
776 : : * The callback contract is to return TRUE to continue enumerating and FALSE
777 : : * to stop enumerating. We always want to continue.
778 : : */
779 : : static BOOL CALLBACK
780 : : win32_read_locale(LPWSTR pStr, DWORD dwFlags, LPARAM lparam)
781 : : {
782 : : CollParam *param = (CollParam *) lparam;
783 : : char localebuf[NAMEDATALEN];
784 : : int result;
785 : : int enc;
786 : :
787 : : (void) dwFlags;
788 : :
789 : : result = WideCharToMultiByte(CP_ACP, 0, pStr, -1, localebuf, NAMEDATALEN,
790 : : NULL, NULL);
791 : :
792 : : if (result == 0)
793 : : {
794 : : if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
795 : : elog(DEBUG1, "skipping locale with too-long name: \"%s\"", localebuf);
796 : : return TRUE;
797 : : }
798 : : if (localebuf[0] == '\0')
799 : : return TRUE;
800 : :
801 : : enc = create_collation_from_locale(localebuf, param->nspid,
802 : : param->nvalidp, param->ncreatedp);
803 : : if (enc < 0)
804 : : return TRUE;
805 : :
806 : : /*
807 : : * Windows will use hyphens between language and territory, where POSIX
808 : : * uses an underscore. Simply create a POSIX alias.
809 : : */
810 : : if (strchr(localebuf, '-'))
811 : : {
812 : : char alias[NAMEDATALEN];
813 : : Oid collid;
814 : :
815 : : strcpy(alias, localebuf);
816 : : for (char *p = alias; *p; p++)
817 : : if (*p == '-')
818 : : *p = '_';
819 : :
820 : : collid = CollationCreate(alias, param->nspid, GetUserId(),
821 : : COLLPROVIDER_LIBC, true, enc,
822 : : localebuf, localebuf, NULL, NULL,
823 : : get_collation_actual_version(COLLPROVIDER_LIBC, localebuf),
824 : : true, true);
825 : : if (OidIsValid(collid))
826 : : {
827 : : (*param->ncreatedp)++;
828 : :
829 : : CommandCounterIncrement();
830 : : }
831 : : }
832 : :
833 : : return TRUE;
834 : : }
835 : : #endif /* ENUM_SYSTEM_LOCALE */
836 : :
837 : :
838 : : /*
839 : : * pg_import_system_collations: add known system collations to pg_collation
840 : : */
841 : : Datum
3601 peter_e@gmx.net 842 : 58 : pg_import_system_collations(PG_FUNCTION_ARGS)
843 : : {
3445 tgl@sss.pgh.pa.us 844 : 58 : Oid nspid = PG_GETARG_OID(0);
845 : 58 : int ncreated = 0;
846 : :
3601 peter_e@gmx.net 847 [ - + ]: 58 : if (!superuser())
3601 peter_e@gmx.net 848 [ # # ]:UBC 0 : ereport(ERROR,
849 : : (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
850 : : errmsg("must be superuser to import system collations")));
851 : :
2091 tgl@sss.pgh.pa.us 852 [ - + ]:CBC 58 : if (!SearchSysCacheExists1(NAMESPACEOID, ObjectIdGetDatum(nspid)))
2091 tgl@sss.pgh.pa.us 853 [ # # ]:UBC 0 : ereport(ERROR,
854 : : (errcode(ERRCODE_UNDEFINED_SCHEMA),
855 : : errmsg("schema with OID %u does not exist", nspid)));
856 : :
857 : : /* Load collations known to libc, using "locale -a" to enumerate them */
858 : : #ifdef READ_LOCALE_A_OUTPUT
859 : : {
860 : : FILE *locale_a_handle;
861 : : char localebuf[LOCALE_NAME_BUFLEN];
3445 tgl@sss.pgh.pa.us 862 :CBC 58 : int nvalid = 0;
863 : : Oid collid;
864 : : CollAliasData *aliases;
865 : : int naliases,
866 : : maxaliases,
867 : : i;
868 : :
869 : : /* expansible array of aliases */
870 : 58 : maxaliases = 100;
103 michael@paquier.xyz 871 :GNC 58 : aliases = palloc_array(CollAliasData, maxaliases);
3445 tgl@sss.pgh.pa.us 872 :CBC 58 : naliases = 0;
873 : :
874 : 58 : locale_a_handle = OpenPipeStream("locale -a", "r");
875 [ - + ]: 58 : if (locale_a_handle == NULL)
3445 tgl@sss.pgh.pa.us 876 [ # # ]:UBC 0 : ereport(ERROR,
877 : : (errcode_for_file_access(),
878 : : errmsg("could not execute command \"%s\": %m",
879 : : "locale -a")));
880 : :
3445 tgl@sss.pgh.pa.us 881 [ + + ]:CBC 290 : while (fgets(localebuf, sizeof(localebuf), locale_a_handle))
882 : : {
883 : : size_t len;
884 : : int enc;
885 : : char alias[LOCALE_NAME_BUFLEN];
886 : :
887 : 232 : len = strlen(localebuf);
888 : :
889 [ + - - + ]: 232 : if (len == 0 || localebuf[len - 1] != '\n')
890 : : {
1901 tgl@sss.pgh.pa.us 891 [ # # ]:UBC 0 : elog(DEBUG1, "skipping locale with too-long name: \"%s\"", localebuf);
3445 tgl@sss.pgh.pa.us 892 :CBC 116 : continue;
893 : : }
894 : 232 : localebuf[len - 1] = '\0';
895 : :
1425 peter@eisentraut.org 896 : 232 : enc = create_collation_from_locale(localebuf, nspid, &nvalid, &ncreated);
3445 tgl@sss.pgh.pa.us 897 [ + + ]: 232 : if (enc < 0)
1901 898 : 116 : continue;
899 : :
900 : : /*
901 : : * Generate aliases such as "en_US" in addition to "en_US.utf8"
902 : : * for ease of use. Note that collation names are unique per
903 : : * encoding only, so this doesn't clash with "en_US" for LATIN1,
904 : : * say.
905 : : *
906 : : * However, it might conflict with a name we'll see later in the
907 : : * "locale -a" output. So save up the aliases and try to add them
908 : : * after we've read all the output.
909 : : */
3445 910 [ + - ]: 116 : if (normalize_libc_locale_name(alias, localebuf))
911 : : {
912 [ - + ]: 116 : if (naliases >= maxaliases)
913 : : {
3445 tgl@sss.pgh.pa.us 914 :UBC 0 : maxaliases *= 2;
103 michael@paquier.xyz 915 :UNC 0 : aliases = repalloc_array(aliases, CollAliasData, maxaliases);
916 : : }
3445 tgl@sss.pgh.pa.us 917 :CBC 116 : aliases[naliases].localename = pstrdup(localebuf);
918 : 116 : aliases[naliases].alias = pstrdup(alias);
919 : 116 : aliases[naliases].enc = enc;
920 : 116 : naliases++;
921 : : }
922 : : }
923 : :
924 : : /*
925 : : * We don't check the return value of this, because we want to support
926 : : * the case where there "locale" command does not exist. (This is
927 : : * unusual but can happen on minimalized Linux distributions, for
928 : : * example.) We will warn below if no locales could be found.
929 : : */
1472 peter@eisentraut.org 930 : 58 : ClosePipeStream(locale_a_handle);
931 : :
932 : : /*
933 : : * Before processing the aliases, sort them by locale name. The point
934 : : * here is that if "locale -a" gives us multiple locale names with the
935 : : * same encoding and base name, say "en_US.utf8" and "en_US.utf-8", we
936 : : * want to pick a deterministic one of them. First in ASCII sort
937 : : * order is a good enough rule. (Before PG 10, the code corresponding
938 : : * to this logic in initdb.c had an additional ordering rule, to
939 : : * prefer the locale name exactly matching the alias, if any. We
940 : : * don't need to consider that here, because we would have already
941 : : * created such a pg_collation entry above, and that one will win.)
942 : : */
3445 tgl@sss.pgh.pa.us 943 [ + - ]: 58 : if (naliases > 1)
1390 peter@eisentraut.org 944 : 58 : qsort(aliases, naliases, sizeof(CollAliasData), cmpaliases);
945 : :
946 : : /* Now add aliases, ignoring any that match pre-existing entries */
3445 tgl@sss.pgh.pa.us 947 [ + + ]: 174 : for (i = 0; i < naliases; i++)
948 : : {
949 : 116 : char *locale = aliases[i].localename;
950 : 116 : char *alias = aliases[i].alias;
951 : 116 : int enc = aliases[i].enc;
952 : :
953 : 116 : collid = CollationCreate(alias, nspid, GetUserId(),
954 : : COLLPROVIDER_LIBC, true, enc,
955 : : locale, locale, NULL, NULL,
2031 tmunro@postgresql.or 956 : 116 : get_collation_actual_version(COLLPROVIDER_LIBC, locale),
957 : : true, true);
3445 tgl@sss.pgh.pa.us 958 [ + + ]: 116 : if (OidIsValid(collid))
959 : : {
960 : 58 : ncreated++;
961 : :
962 : 58 : CommandCounterIncrement();
963 : : }
964 : : }
965 : :
966 : : /* Give a warning if "locale -a" seems to be malfunctioning */
967 [ - + ]: 58 : if (nvalid == 0)
3445 tgl@sss.pgh.pa.us 968 [ # # ]:UBC 0 : ereport(WARNING,
969 : : (errmsg("no usable system locales were found")));
970 : : }
971 : : #endif /* READ_LOCALE_A_OUTPUT */
972 : :
973 : : /*
974 : : * Load collations known to ICU
975 : : *
976 : : * We use uloc_countAvailable()/uloc_getAvailable() rather than
977 : : * ucol_countAvailable()/ucol_getAvailable(). The former returns a full
978 : : * set of language+region combinations, whereas the latter only returns
979 : : * language+region combinations if they are distinct from the language's
980 : : * base collation. So there might not be a de-DE or en-GB, which would be
981 : : * confusing.
982 : : */
983 : : #ifdef USE_ICU
984 : : {
985 : : int i;
986 : :
987 : : /*
988 : : * Start the loop at -1 to sneak in the root locale without too much
989 : : * code duplication.
990 : : */
3386 peter_e@gmx.net 991 [ + + ]:CBC 50518 : for (i = -1; i < uloc_countAvailable(); i++)
992 : : {
993 : : const char *name;
994 : : char *langtag;
995 : : char *icucomment;
996 : : Oid collid;
997 : :
3537 998 [ + + ]: 50460 : if (i == -1)
3482 bruce@momjian.us 999 : 58 : name = ""; /* ICU root locale */
1000 : : else
3386 peter_e@gmx.net 1001 : 50402 : name = uloc_getAvailable(i);
1002 : :
1334 jdavis@postgresql.or 1003 : 50460 : langtag = icu_language_tag(name, ERROR);
1004 : :
1005 : : /*
1006 : : * Be paranoid about not allowing any non-ASCII strings into
1007 : : * pg_collation
1008 : : */
1348 1009 [ - + ]: 50460 : if (!pg_is_ascii(langtag))
3444 tgl@sss.pgh.pa.us 1010 :UBC 0 : continue;
1011 : :
3537 peter_e@gmx.net 1012 :CBC 50460 : collid = CollationCreate(psprintf("%s-x-icu", langtag),
1013 : : nspid, GetUserId(),
1014 : : COLLPROVIDER_ICU, true, -1,
1015 : : NULL, NULL, langtag, NULL,
1348 jdavis@postgresql.or 1016 : 50460 : get_collation_actual_version(COLLPROVIDER_ICU, langtag),
1017 : : true, true);
3445 tgl@sss.pgh.pa.us 1018 [ + - ]: 50460 : if (OidIsValid(collid))
1019 : : {
1020 : 50460 : ncreated++;
1021 : :
1022 : 50460 : CommandCounterIncrement();
1023 : :
3444 1024 : 50460 : icucomment = get_icu_locale_comment(name);
1025 [ + + ]: 50460 : if (icucomment)
1026 : 49474 : CreateComments(collid, CollationRelationId, 0,
1027 : : icucomment);
1028 : : }
1029 : : }
1030 : : }
1031 : : #endif /* USE_ICU */
1032 : :
1033 : : /* Load collations known to WIN32 */
1034 : : #ifdef ENUM_SYSTEM_LOCALE
1035 : : {
1036 : : int nvalid = 0;
1037 : : CollParam param;
1038 : :
1039 : : param.nspid = nspid;
1040 : : param.ncreatedp = &ncreated;
1041 : : param.nvalidp = &nvalid;
1042 : :
1043 : : /*
1044 : : * Enumerate the locales that are either installed on or supported by
1045 : : * the OS.
1046 : : */
1047 : : if (!EnumSystemLocalesEx(win32_read_locale, LOCALE_ALL,
1048 : : (LPARAM) ¶m, NULL))
1049 : : _dosmaperr(GetLastError());
1050 : :
1051 : : /* Give a warning if EnumSystemLocalesEx seems to be malfunctioning */
1052 : : if (nvalid == 0)
1053 : : ereport(WARNING,
1054 : : (errmsg("no usable system locales were found")));
1055 : : }
1056 : : #endif /* ENUM_SYSTEM_LOCALE */
1057 : :
3445 1058 : 58 : PG_RETURN_INT32(ncreated);
1059 : : }
|