PostgreSQL Source Code  git master
collationcmds.c File Reference
#include "postgres.h"
#include "access/htup_details.h"
#include "access/table.h"
#include "access/xact.h"
#include "catalog/dependency.h"
#include "catalog/indexing.h"
#include "catalog/namespace.h"
#include "catalog/objectaccess.h"
#include "catalog/pg_collation.h"
#include "commands/alter.h"
#include "commands/collationcmds.h"
#include "commands/comment.h"
#include "commands/dbcommands.h"
#include "commands/defrem.h"
#include "common/string.h"
#include "mb/pg_wchar.h"
#include "miscadmin.h"
#include "utils/acl.h"
#include "utils/builtins.h"
#include "utils/lsyscache.h"
#include "utils/pg_locale.h"
#include "utils/rel.h"
#include "utils/syscache.h"
Include dependency graph for collationcmds.c:

Go to the source code of this file.

Data Structures

struct  CollAliasData
 

Functions

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

Function Documentation

◆ AlterCollation()

ObjectAddress AlterCollation ( AlterCollationStmt stmt)

Definition at line 353 of file collationcmds.c.

354 {
355  Relation rel;
356  Oid collOid;
357  HeapTuple tup;
358  Form_pg_collation collForm;
359  Datum datum;
360  bool isnull;
361  char *oldversion;
362  char *newversion;
363  ObjectAddress address;
364 
365  rel = table_open(CollationRelationId, RowExclusiveLock);
366  collOid = get_collation_oid(stmt->collname, false);
367 
368  if (!pg_collation_ownercheck(collOid, GetUserId()))
370  NameListToString(stmt->collname));
371 
373  if (!HeapTupleIsValid(tup))
374  elog(ERROR, "cache lookup failed for collation %u", collOid);
375 
376  collForm = (Form_pg_collation) GETSTRUCT(tup);
377  datum = SysCacheGetAttr(COLLOID, tup, Anum_pg_collation_collversion, &isnull);
378  oldversion = isnull ? NULL : TextDatumGetCString(datum);
379 
380  datum = SysCacheGetAttr(COLLOID, tup, collForm->collprovider == COLLPROVIDER_ICU ? Anum_pg_collation_colliculocale : Anum_pg_collation_collcollate, &isnull);
381  if (isnull)
382  elog(ERROR, "unexpected null in pg_collation");
383  newversion = get_collation_actual_version(collForm->collprovider, TextDatumGetCString(datum));
384 
385  /* cannot change from NULL to non-NULL or vice versa */
386  if ((!oldversion && newversion) || (oldversion && !newversion))
387  elog(ERROR, "invalid collation version change");
388  else if (oldversion && newversion && strcmp(newversion, oldversion) != 0)
389  {
390  bool nulls[Natts_pg_collation];
391  bool replaces[Natts_pg_collation];
392  Datum values[Natts_pg_collation];
393 
394  ereport(NOTICE,
395  (errmsg("changing version from %s to %s",
396  oldversion, newversion)));
397 
398  memset(values, 0, sizeof(values));
399  memset(nulls, false, sizeof(nulls));
400  memset(replaces, false, sizeof(replaces));
401 
402  values[Anum_pg_collation_collversion - 1] = CStringGetTextDatum(newversion);
403  replaces[Anum_pg_collation_collversion - 1] = true;
404 
405  tup = heap_modify_tuple(tup, RelationGetDescr(rel),
406  values, nulls, replaces);
407  }
408  else
409  ereport(NOTICE,
410  (errmsg("version has not changed")));
411 
412  CatalogTupleUpdate(rel, &tup->t_self, tup);
413 
414  InvokeObjectPostAlterHook(CollationRelationId, collOid, 0);
415 
416  ObjectAddressSet(address, CollationRelationId, collOid);
417 
418  heap_freetuple(tup);
419  table_close(rel, NoLock);
420 
421  return address;
422 }
@ ACLCHECK_NOT_OWNER
Definition: acl.h:184
bool pg_collation_ownercheck(Oid coll_oid, Oid roleid)
Definition: aclchk.c:5615
void aclcheck_error(AclResult aclerr, ObjectType objtype, const char *objectname)
Definition: aclchk.c:3512
static Datum values[MAXATTR]
Definition: bootstrap.c:156
#define CStringGetTextDatum(s)
Definition: builtins.h:85
#define TextDatumGetCString(d)
Definition: builtins.h:86
int errmsg(const char *fmt,...)
Definition: elog.c:904
#define ERROR
Definition: elog.h:33
#define elog(elevel,...)
Definition: elog.h:218
#define NOTICE
Definition: elog.h:29
#define ereport(elevel,...)
Definition: elog.h:143
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:649
void CatalogTupleUpdate(Relation heapRel, ItemPointer otid, HeapTuple tup)
Definition: indexing.c:301
#define NoLock
Definition: lockdefs.h:34
#define RowExclusiveLock
Definition: lockdefs.h:38
Oid GetUserId(void)
Definition: miscinit.c:492
Oid get_collation_oid(List *name, bool missing_ok)
Definition: namespace.c:3647
char * NameListToString(List *names)
Definition: namespace.c:3148
#define InvokeObjectPostAlterHook(classId, objectId, subId)
Definition: objectaccess.h:195
#define ObjectAddressSet(addr, class_id, object_id)
Definition: objectaddress.h:40
@ OBJECT_COLLATION
Definition: parsenodes.h:2141
FormData_pg_collation * Form_pg_collation
Definition: pg_collation.h:57
char * get_collation_actual_version(char collprovider, const char *collcollate)
Definition: pg_locale.c:1684
uintptr_t Datum
Definition: postgres.h:411
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
unsigned int Oid
Definition: postgres_ext.h:31
#define RelationGetDescr(relation)
Definition: rel.h:515
ItemPointerData t_self
Definition: htup.h:65
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:1434
#define SearchSysCacheCopy1(cacheId, key1)
Definition: syscache.h:179
@ COLLOID
Definition: syscache.h:50
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39

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

◆ DefineCollation()

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

Definition at line 53 of file collationcmds.c.

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;
67  char *collctype;
68  char *colliculocale;
69  bool collisdeterministic;
70  int collencoding;
71  char collprovider;
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  if (*defelp != NULL)
112  errorConflictingDefElem(defel, pstate);
113  *defelp = defel;
114  }
115 
116  if (localeEl && (lccollateEl || lcctypeEl))
117  ereport(ERROR,
118  errcode(ERRCODE_SYNTAX_ERROR),
119  errmsg("conflicting or redundant options"),
120  errdetail("LOCALE cannot be specified together with LC_COLLATE or LC_CTYPE."));
121 
122  if (fromEl && list_length(parameters) != 1)
123  ereport(ERROR,
124  errcode(ERRCODE_SYNTAX_ERROR),
125  errmsg("conflicting or redundant options"),
126  errdetail("FROM cannot be specified together with any other options."));
127 
128  if (fromEl)
129  {
130  Oid collid;
131  HeapTuple tp;
132  Datum datum;
133  bool isnull;
134 
135  collid = get_collation_oid(defGetQualifiedName(fromEl), false);
137  if (!HeapTupleIsValid(tp))
138  elog(ERROR, "cache lookup failed for collation %u", collid);
139 
140  collprovider = ((Form_pg_collation) GETSTRUCT(tp))->collprovider;
141  collisdeterministic = ((Form_pg_collation) GETSTRUCT(tp))->collisdeterministic;
142  collencoding = ((Form_pg_collation) GETSTRUCT(tp))->collencoding;
143 
144  datum = SysCacheGetAttr(COLLOID, tp, Anum_pg_collation_collcollate, &isnull);
145  if (!isnull)
146  collcollate = TextDatumGetCString(datum);
147  else
148  collcollate = NULL;
149 
150  datum = SysCacheGetAttr(COLLOID, tp, Anum_pg_collation_collctype, &isnull);
151  if (!isnull)
152  collctype = TextDatumGetCString(datum);
153  else
154  collctype = NULL;
155 
156  datum = SysCacheGetAttr(COLLOID, tp, Anum_pg_collation_colliculocale, &isnull);
157  if (!isnull)
158  colliculocale = TextDatumGetCString(datum);
159  else
160  colliculocale = NULL;
161 
162  ReleaseSysCache(tp);
163 
164  /*
165  * Copying the "default" collation is not allowed because most code
166  * checks for DEFAULT_COLLATION_OID instead of COLLPROVIDER_DEFAULT,
167  * and so having a second collation with COLLPROVIDER_DEFAULT would
168  * not work and potentially confuse or crash some code. This could be
169  * fixed with some legwork.
170  */
171  if (collprovider == COLLPROVIDER_DEFAULT)
172  ereport(ERROR,
173  (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
174  errmsg("collation \"default\" cannot be copied")));
175  }
176  else
177  {
178  char *collproviderstr = NULL;
179 
180  collcollate = NULL;
181  collctype = NULL;
182  colliculocale = NULL;
183 
184  if (providerEl)
185  collproviderstr = defGetString(providerEl);
186 
187  if (deterministicEl)
188  collisdeterministic = defGetBoolean(deterministicEl);
189  else
190  collisdeterministic = true;
191 
192  if (versionEl)
193  collversion = defGetString(versionEl);
194 
195  if (collproviderstr)
196  {
197  if (pg_strcasecmp(collproviderstr, "icu") == 0)
198  collprovider = COLLPROVIDER_ICU;
199  else if (pg_strcasecmp(collproviderstr, "libc") == 0)
200  collprovider = COLLPROVIDER_LIBC;
201  else
202  ereport(ERROR,
203  (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
204  errmsg("unrecognized collation provider: %s",
205  collproviderstr)));
206  }
207  else
208  collprovider = COLLPROVIDER_LIBC;
209 
210  if (localeEl)
211  {
212  if (collprovider == COLLPROVIDER_LIBC)
213  {
214  collcollate = defGetString(localeEl);
215  collctype = defGetString(localeEl);
216  }
217  else
218  colliculocale = defGetString(localeEl);
219  }
220 
221  if (lccollateEl)
222  collcollate = defGetString(lccollateEl);
223 
224  if (lcctypeEl)
225  collctype = defGetString(lcctypeEl);
226 
227  if (collprovider == COLLPROVIDER_LIBC)
228  {
229  if (!collcollate)
230  ereport(ERROR,
231  (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
232  errmsg("parameter \"lc_collate\" must be specified")));
233 
234  if (!collctype)
235  ereport(ERROR,
236  (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
237  errmsg("parameter \"lc_ctype\" must be specified")));
238  }
239  else if (collprovider == COLLPROVIDER_ICU)
240  {
241  if (!colliculocale)
242  ereport(ERROR,
243  (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
244  errmsg("parameter \"locale\" must be specified")));
245  }
246 
247  /*
248  * Nondeterministic collations are currently only supported with ICU
249  * because that's the only case where it can actually make a
250  * difference. So we can save writing the code for the other
251  * providers.
252  */
253  if (!collisdeterministic && collprovider != COLLPROVIDER_ICU)
254  ereport(ERROR,
255  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
256  errmsg("nondeterministic collations not supported with this provider")));
257 
258  if (collprovider == COLLPROVIDER_ICU)
259  {
260 #ifdef USE_ICU
261  /*
262  * We could create ICU collations with collencoding == database
263  * encoding, but it seems better to use -1 so that it matches the
264  * way initdb would create ICU collations. However, only allow
265  * one to be created when the current database's encoding is
266  * supported. Otherwise the collation is useless, plus we get
267  * surprising behaviors like not being able to drop the collation.
268  *
269  * Skip this test when !USE_ICU, because the error we want to
270  * throw for that isn't thrown till later.
271  */
273  ereport(ERROR,
274  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
275  errmsg("current database's encoding is not supported with this provider")));
276 #endif
277  collencoding = -1;
278  }
279  else
280  {
281  collencoding = GetDatabaseEncoding();
282  check_encoding_locale_matches(collencoding, collcollate, collctype);
283  }
284  }
285 
286  if (!collversion)
287  collversion = get_collation_actual_version(collprovider, collprovider == COLLPROVIDER_ICU ? colliculocale : collcollate);
288 
289  newoid = CollationCreate(collName,
290  collNamespace,
291  GetUserId(),
292  collprovider,
293  collisdeterministic,
294  collencoding,
295  collcollate,
296  collctype,
297  colliculocale,
298  collversion,
299  if_not_exists,
300  false); /* not quiet */
301 
302  if (!OidIsValid(newoid))
303  return InvalidObjectAddress;
304 
305  /*
306  * Check that the locales can be loaded. NB: pg_newlocale_from_collation
307  * is only supposed to be called on non-C-equivalent locales.
308  */
310  if (!lc_collate_is_c(newoid) || !lc_ctype_is_c(newoid))
311  (void) pg_newlocale_from_collation(newoid);
312 
313  ObjectAddressSet(address, CollationRelationId, newoid);
314 
315  return address;
316 }
AclResult
Definition: acl.h:181
@ ACLCHECK_OK
Definition: acl.h:182
AclResult pg_namespace_aclcheck(Oid nsp_oid, Oid roleid, AclMode mode)
Definition: aclchk.c:5109
#define OidIsValid(objectId)
Definition: c.h:710
void check_encoding_locale_matches(int encoding, const char *collate, const char *ctype)
Definition: dbcommands.c:1434
bool defGetBoolean(DefElem *def)
Definition: define.c:108
List * defGetQualifiedName(DefElem *def)
Definition: define.c:220
char * defGetString(DefElem *def)
Definition: define.c:49
void errorConflictingDefElem(DefElem *defel, ParseState *pstate)
Definition: define.c:352
int errdetail(const char *fmt,...)
Definition: elog.c:1037
int errcode(int sqlerrcode)
Definition: elog.c:693
bool is_encoding_supported_by_icu(int encoding)
Definition: encnames.c:459
char * get_namespace_name(Oid nspid)
Definition: lsyscache.c:3326
int GetDatabaseEncoding(void)
Definition: mbutils.c:1210
Oid QualifiedNameGetCreationNamespace(List *names, char **objname_p)
Definition: namespace.c:3041
const ObjectAddress InvalidObjectAddress
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:110
@ OBJECT_SCHEMA
Definition: parsenodes.h:2170
#define ACL_CREATE
Definition: parsenodes.h:91
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 *collversion, bool if_not_exists, bool quiet)
Definition: pg_collation.c:46
#define lfirst_node(type, lc)
Definition: pg_list.h:172
static int list_length(const List *l)
Definition: pg_list.h:149
bool lc_collate_is_c(Oid collation)
Definition: pg_locale.c:1336
pg_locale_t pg_newlocale_from_collation(Oid collid)
Definition: pg_locale.c:1518
bool lc_ctype_is_c(Oid collation)
Definition: pg_locale.c:1389
int pg_strcasecmp(const char *s1, const char *s2)
Definition: pgstrcasecmp.c:36
char * defname
Definition: parsenodes.h:765
int location
Definition: parsenodes.h:769
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1221
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1173
void CommandCounterIncrement(void)
Definition: xact.c:1074

References ACL_CREATE, aclcheck_error(), ACLCHECK_OK, check_encoding_locale_matches(), CollationCreate(), 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, InvalidObjectAddress, is_encoding_supported_by_icu(), lc_collate_is_c(), lc_ctype_is_c(), lfirst_node, list_length(), DefElem::location, OBJECT_SCHEMA, ObjectAddressSet, ObjectIdGetDatum, OidIsValid, parser_errposition(), pg_namespace_aclcheck(), 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 325 of file collationcmds.c.

326 {
327  /* make sure the name doesn't already exist in new schema */
329  CStringGetDatum(collname),
331  ObjectIdGetDatum(nspOid)))
332  ereport(ERROR,
334  errmsg("collation \"%s\" for encoding \"%s\" already exists in schema \"%s\"",
335  collname, GetDatabaseEncodingName(),
336  get_namespace_name(nspOid))));
337 
338  /* mustn't match an any-encoding entry, either */
340  CStringGetDatum(collname),
341  Int32GetDatum(-1),
342  ObjectIdGetDatum(nspOid)))
343  ereport(ERROR,
345  errmsg("collation \"%s\" already exists in schema \"%s\"",
346  collname, get_namespace_name(nspOid))));
347 }
const char * GetDatabaseEncodingName(void)
Definition: mbutils.c:1216
#define CStringGetDatum(X)
Definition: postgres.h:622
#define Int32GetDatum(X)
Definition: postgres.h:523
#define ERRCODE_DUPLICATE_OBJECT
Definition: streamutil.c:32
@ COLLNAMEENCNSP
Definition: syscache.h:49
#define SearchSysCacheExists3(cacheId, key1, key2, key3)
Definition: syscache.h:192

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

◆ pg_collation_actual_version()

Datum pg_collation_actual_version ( PG_FUNCTION_ARGS  )

Definition at line 426 of file collationcmds.c.

427 {
428  Oid collid = PG_GETARG_OID(0);
429  HeapTuple tp;
430  char collprovider;
431  Datum datum;
432  bool isnull;
433  char *version;
434 
436  if (!HeapTupleIsValid(tp))
437  ereport(ERROR,
438  (errcode(ERRCODE_UNDEFINED_OBJECT),
439  errmsg("collation with OID %u does not exist", collid)));
440 
441  collprovider = ((Form_pg_collation) GETSTRUCT(tp))->collprovider;
442 
443  if (collprovider != COLLPROVIDER_DEFAULT)
444  {
445  datum = SysCacheGetAttr(COLLOID, tp, collprovider == COLLPROVIDER_ICU ? Anum_pg_collation_colliculocale : Anum_pg_collation_collcollate, &isnull);
446  if (isnull)
447  elog(ERROR, "unexpected null in pg_collation");
448  version = get_collation_actual_version(collprovider, TextDatumGetCString(datum));
449  }
450  else
451  version = NULL;
452 
453  ReleaseSysCache(tp);
454 
455  if (version)
457  else
458  PG_RETURN_NULL();
459 }
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
#define PG_RETURN_NULL()
Definition: fmgr.h:345
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:372
text * cstring_to_text(const char *s)
Definition: varlena.c:188

References COLLOID, cstring_to_text(), elog, ereport, errcode(), errmsg(), ERROR, get_collation_actual_version(), GETSTRUCT, HeapTupleIsValid, ObjectIdGetDatum, PG_GETARG_OID, PG_RETURN_NULL, PG_RETURN_TEXT_P, ReleaseSysCache(), SearchSysCache1(), SysCacheGetAttr(), and TextDatumGetCString.

◆ pg_import_system_collations()

Datum pg_import_system_collations ( PG_FUNCTION_ARGS  )

Definition at line 582 of file collationcmds.c.

583 {
584  Oid nspid = PG_GETARG_OID(0);
585  int ncreated = 0;
586 
587  if (!superuser())
588  ereport(ERROR,
589  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
590  errmsg("must be superuser to import system collations")));
591 
593  ereport(ERROR,
594  (errcode(ERRCODE_UNDEFINED_SCHEMA),
595  errmsg("schema with OID %u does not exist", nspid)));
596 
597  /* Load collations known to libc, using "locale -a" to enumerate them */
598 #ifdef READ_LOCALE_A_OUTPUT
599  {
600  FILE *locale_a_handle;
601  char localebuf[LOCALE_NAME_BUFLEN];
602  int nvalid = 0;
603  Oid collid;
604  CollAliasData *aliases;
605  int naliases,
606  maxaliases,
607  i;
608 
609  /* expansible array of aliases */
610  maxaliases = 100;
611  aliases = (CollAliasData *) palloc(maxaliases * sizeof(CollAliasData));
612  naliases = 0;
613 
614  locale_a_handle = OpenPipeStream("locale -a", "r");
615  if (locale_a_handle == NULL)
616  ereport(ERROR,
618  errmsg("could not execute command \"%s\": %m",
619  "locale -a")));
620 
621  while (fgets(localebuf, sizeof(localebuf), locale_a_handle))
622  {
623  size_t len;
624  int enc;
625  char alias[LOCALE_NAME_BUFLEN];
626 
627  len = strlen(localebuf);
628 
629  if (len == 0 || localebuf[len - 1] != '\n')
630  {
631  elog(DEBUG1, "skipping locale with too-long name: \"%s\"", localebuf);
632  continue;
633  }
634  localebuf[len - 1] = '\0';
635 
636  /*
637  * Some systems have locale names that don't consist entirely of
638  * ASCII letters (such as "bokmål" or "français").
639  * This is pretty silly, since we need the locale itself to
640  * interpret the non-ASCII characters. We can't do much with
641  * those, so we filter them out.
642  */
643  if (!pg_is_ascii(localebuf))
644  {
645  elog(DEBUG1, "skipping locale with non-ASCII name: \"%s\"", localebuf);
646  continue;
647  }
648 
649  enc = pg_get_encoding_from_locale(localebuf, false);
650  if (enc < 0)
651  {
652  elog(DEBUG1, "skipping locale with unrecognized encoding: \"%s\"",
653  localebuf);
654  continue;
655  }
657  {
658  elog(DEBUG1, "skipping locale with client-only encoding: \"%s\"", localebuf);
659  continue;
660  }
661  if (enc == PG_SQL_ASCII)
662  continue; /* C/POSIX are already in the catalog */
663 
664  /* count valid locales found in operating system */
665  nvalid++;
666 
667  /*
668  * Create a collation named the same as the locale, but quietly
669  * doing nothing if it already exists. This is the behavior we
670  * need even at initdb time, because some versions of "locale -a"
671  * can report the same locale name more than once. And it's
672  * convenient for later import runs, too, since you just about
673  * always want to add on new locales without a lot of chatter
674  * about existing ones.
675  */
676  collid = CollationCreate(localebuf, nspid, GetUserId(),
677  COLLPROVIDER_LIBC, true, enc,
678  localebuf, localebuf, NULL,
679  get_collation_actual_version(COLLPROVIDER_LIBC, localebuf),
680  true, true);
681  if (OidIsValid(collid))
682  {
683  ncreated++;
684 
685  /* Must do CCI between inserts to handle duplicates correctly */
687  }
688 
689  /*
690  * Generate aliases such as "en_US" in addition to "en_US.utf8"
691  * for ease of use. Note that collation names are unique per
692  * encoding only, so this doesn't clash with "en_US" for LATIN1,
693  * say.
694  *
695  * However, it might conflict with a name we'll see later in the
696  * "locale -a" output. So save up the aliases and try to add them
697  * after we've read all the output.
698  */
699  if (normalize_libc_locale_name(alias, localebuf))
700  {
701  if (naliases >= maxaliases)
702  {
703  maxaliases *= 2;
704  aliases = (CollAliasData *)
705  repalloc(aliases, maxaliases * sizeof(CollAliasData));
706  }
707  aliases[naliases].localename = pstrdup(localebuf);
708  aliases[naliases].alias = pstrdup(alias);
709  aliases[naliases].enc = enc;
710  naliases++;
711  }
712  }
713 
714  ClosePipeStream(locale_a_handle);
715 
716  /*
717  * Before processing the aliases, sort them by locale name. The point
718  * here is that if "locale -a" gives us multiple locale names with the
719  * same encoding and base name, say "en_US.utf8" and "en_US.utf-8", we
720  * want to pick a deterministic one of them. First in ASCII sort
721  * order is a good enough rule. (Before PG 10, the code corresponding
722  * to this logic in initdb.c had an additional ordering rule, to
723  * prefer the locale name exactly matching the alias, if any. We
724  * don't need to consider that here, because we would have already
725  * created such a pg_collation entry above, and that one will win.)
726  */
727  if (naliases > 1)
728  qsort((void *) aliases, naliases, sizeof(CollAliasData), cmpaliases);
729 
730  /* Now add aliases, ignoring any that match pre-existing entries */
731  for (i = 0; i < naliases; i++)
732  {
733  char *locale = aliases[i].localename;
734  char *alias = aliases[i].alias;
735  int enc = aliases[i].enc;
736 
737  collid = CollationCreate(alias, nspid, GetUserId(),
738  COLLPROVIDER_LIBC, true, enc,
739  locale, locale, NULL,
740  get_collation_actual_version(COLLPROVIDER_LIBC, locale),
741  true, true);
742  if (OidIsValid(collid))
743  {
744  ncreated++;
745 
747  }
748  }
749 
750  /* Give a warning if "locale -a" seems to be malfunctioning */
751  if (nvalid == 0)
753  (errmsg("no usable system locales were found")));
754  }
755 #endif /* READ_LOCALE_A_OUTPUT */
756 
757  /*
758  * Load collations known to ICU
759  *
760  * We use uloc_countAvailable()/uloc_getAvailable() rather than
761  * ucol_countAvailable()/ucol_getAvailable(). The former returns a full
762  * set of language+region combinations, whereas the latter only returns
763  * language+region combinations if they are distinct from the language's
764  * base collation. So there might not be a de-DE or en-GB, which would be
765  * confusing.
766  */
767 #ifdef USE_ICU
768  {
769  int i;
770 
771  /*
772  * Start the loop at -1 to sneak in the root locale without too much
773  * code duplication.
774  */
775  for (i = -1; i < uloc_countAvailable(); i++)
776  {
777  const char *name;
778  char *langtag;
779  char *icucomment;
780  const char *iculocstr;
781  Oid collid;
782 
783  if (i == -1)
784  name = ""; /* ICU root locale */
785  else
786  name = uloc_getAvailable(i);
787 
788  langtag = get_icu_language_tag(name);
789  iculocstr = U_ICU_VERSION_MAJOR_NUM >= 54 ? langtag : name;
790 
791  /*
792  * Be paranoid about not allowing any non-ASCII strings into
793  * pg_collation
794  */
795  if (!pg_is_ascii(langtag) || !pg_is_ascii(iculocstr))
796  continue;
797 
798  collid = CollationCreate(psprintf("%s-x-icu", langtag),
799  nspid, GetUserId(),
800  COLLPROVIDER_ICU, true, -1,
801  NULL, NULL, iculocstr,
802  get_collation_actual_version(COLLPROVIDER_ICU, iculocstr),
803  true, true);
804  if (OidIsValid(collid))
805  {
806  ncreated++;
807 
809 
810  icucomment = get_icu_locale_comment(name);
811  if (icucomment)
812  CreateComments(collid, CollationRelationId, 0,
813  icucomment);
814  }
815  }
816  }
817 #endif /* USE_ICU */
818 
819  PG_RETURN_INT32(ncreated);
820 }
void CreateComments(Oid oid, Oid classoid, int32 subid, const char *comment)
Definition: comment.c:143
int errcode_for_file_access(void)
Definition: elog.c:716
#define WARNING
Definition: elog.h:30
#define DEBUG1
Definition: elog.h:24
const char * name
Definition: encode.c:561
struct pg_encoding enc
Definition: encode.c:562
int ClosePipeStream(FILE *file)
Definition: fd.c:2870
FILE * OpenPipeStream(const char *command, const char *mode)
Definition: fd.c:2564
#define PG_RETURN_INT32(x)
Definition: fmgr.h:354
static char * locale
Definition: initdb.c:128
int i
Definition: isn.c:73
char * pstrdup(const char *in)
Definition: mcxt.c:1305
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:1188
void * palloc(Size size)
Definition: mcxt.c:1068
const void size_t len
#define LOCALE_NAME_BUFLEN
Definition: pg_locale.h:38
@ PG_SQL_ASCII
Definition: pg_wchar.h:224
#define PG_VALID_BE_ENCODING(_enc)
Definition: pg_wchar.h:279
int pg_get_encoding_from_locale(const char *ctype, bool write_message)
Definition: chklocale.c:451
#define qsort(a, b, c, d)
Definition: port.h:495
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
bool pg_is_ascii(const char *str)
Definition: string.c:99
char * localename
Definition: collationcmds.c:43
bool superuser(void)
Definition: superuser.c:46
@ NAMESPACEOID
Definition: syscache.h:70
#define SearchSysCacheExists1(cacheId, key1)
Definition: syscache.h:188

References CollAliasData::alias, ClosePipeStream(), CollationCreate(), CommandCounterIncrement(), CreateComments(), DEBUG1, elog, CollAliasData::enc, enc, ereport, errcode(), errcode_for_file_access(), errmsg(), ERROR, get_collation_actual_version(), GetUserId(), i, len, locale, LOCALE_NAME_BUFLEN, CollAliasData::localename, name, NAMESPACEOID, ObjectIdGetDatum, OidIsValid, OpenPipeStream(), palloc(), pg_get_encoding_from_locale(), PG_GETARG_OID, pg_is_ascii(), PG_RETURN_INT32, PG_SQL_ASCII, PG_VALID_BE_ENCODING, psprintf(), pstrdup(), qsort, repalloc(), SearchSysCacheExists1, superuser(), and WARNING.