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 "mb/pg_wchar.h"
#include "miscadmin.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 283 of file collationcmds.c.

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

Referenced by ProcessUtilitySlow().

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

◆ DefineCollation()

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

Definition at line 51 of file collationcmds.c.

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

Referenced by ProcessUtilitySlow().

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

◆ IsThereCollationInNamespace()

void IsThereCollationInNamespace ( const char *  collname,
Oid  nspOid 
)

Definition at line 255 of file collationcmds.c.

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

Referenced by AlterObjectNamespace_internal(), and AlterObjectRename_internal().

256 {
257  /* make sure the name doesn't already exist in new schema */
259  CStringGetDatum(collname),
261  ObjectIdGetDatum(nspOid)))
262  ereport(ERROR,
264  errmsg("collation \"%s\" for encoding \"%s\" already exists in schema \"%s\"",
265  collname, GetDatabaseEncodingName(),
266  get_namespace_name(nspOid))));
267 
268  /* mustn't match an any-encoding entry, either */
270  CStringGetDatum(collname),
271  Int32GetDatum(-1),
272  ObjectIdGetDatum(nspOid)))
273  ereport(ERROR,
275  errmsg("collation \"%s\" already exists in schema \"%s\"",
276  collname, get_namespace_name(nspOid))));
277 }
int errcode(int sqlerrcode)
Definition: elog.c:608
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:43
#define SearchSysCacheExists3(cacheId, key1, key2, key3)
Definition: syscache.h:187
char * get_namespace_name(Oid nspid)
Definition: lsyscache.c:3094
#define CStringGetDatum(X)
Definition: postgres.h:578
#define ereport(elevel, rest)
Definition: elog.h:141
int GetDatabaseEncoding(void)
Definition: mbutils.c:1046
const char * GetDatabaseEncodingName(void)
Definition: mbutils.c:1052
#define Int32GetDatum(X)
Definition: postgres.h:479
int errmsg(const char *fmt,...)
Definition: elog.c:822
#define ERRCODE_DUPLICATE_OBJECT
Definition: streamutil.c:31

◆ pg_collation_actual_version()

Datum pg_collation_actual_version ( PG_FUNCTION_ARGS  )

Definition at line 354 of file collationcmds.c.

References buf, COLLOID, cstring_to_text(), ereport, errcode(), errmsg(), ERROR, get_collation_actual_version(), GETSTRUCT, HeapTupleIsValid, i, IS_HIGHBIT_SET, lengthof, CollAliasData::localename, NameStr, ObjectIdGetDatum, palloc(), PG_GETARG_OID, PG_RETURN_NULL, PG_RETURN_TEXT_P, pstrdup(), ReleaseSysCache(), SearchSysCache1(), status(), and generate_unaccent_rules::str.

355 {
356  Oid collid = PG_GETARG_OID(0);
357  HeapTuple tp;
358  char *collcollate;
359  char collprovider;
360  char *version;
361 
363  if (!HeapTupleIsValid(tp))
364  ereport(ERROR,
365  (errcode(ERRCODE_UNDEFINED_OBJECT),
366  errmsg("collation with OID %u does not exist", collid)));
367 
368  collcollate = pstrdup(NameStr(((Form_pg_collation) GETSTRUCT(tp))->collcollate));
369  collprovider = ((Form_pg_collation) GETSTRUCT(tp))->collprovider;
370 
371  ReleaseSysCache(tp);
372 
373  version = get_collation_actual_version(collprovider, collcollate);
374 
375  if (version)
377  else
378  PG_RETURN_NULL();
379 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
char * pstrdup(const char *in)
Definition: mcxt.c:1186
int errcode(int sqlerrcode)
Definition: elog.c:608
unsigned int Oid
Definition: postgres_ext.h:31
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:43
#define PG_GETARG_OID(n)
Definition: fmgr.h:270
#define ereport(elevel, rest)
Definition: elog.h:141
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1116
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1164
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:361
text * cstring_to_text(const char *s)
Definition: varlena.c:171
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
FormData_pg_collation * Form_pg_collation
Definition: pg_collation.h:51
int errmsg(const char *fmt,...)
Definition: elog.c:822
#define NameStr(name)
Definition: c.h:616
#define PG_RETURN_NULL()
Definition: fmgr.h:335
char * get_collation_actual_version(char collprovider, const char *collcollate)
Definition: pg_locale.c:1504

◆ pg_import_system_collations()

Datum pg_import_system_collations ( PG_FUNCTION_ARGS  )

Definition at line 519 of file collationcmds.c.

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, locale, CollAliasData::localename, name, NAMEDATALEN, OidIsValid, OpenPipeStream(), palloc(), pg_get_encoding_from_locale(), PG_GETARG_OID, PG_RETURN_INT32, PG_SQL_ASCII, PG_VALID_BE_ENCODING, psprintf(), pstrdup(), qsort, repalloc(), superuser(), and WARNING.

520 {
521  Oid nspid = PG_GETARG_OID(0);
522  int ncreated = 0;
523 
524  /* silence compiler warning if we have no locale implementation at all */
525  (void) nspid;
526 
527  if (!superuser())
528  ereport(ERROR,
529  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
530  (errmsg("must be superuser to import system collations"))));
531 
532  /* Load collations known to libc, using "locale -a" to enumerate them */
533 #ifdef READ_LOCALE_A_OUTPUT
534  {
535  FILE *locale_a_handle;
536  char localebuf[NAMEDATALEN]; /* we assume ASCII so this is fine */
537  int nvalid = 0;
538  Oid collid;
539  CollAliasData *aliases;
540  int naliases,
541  maxaliases,
542  i;
543 
544  /* expansible array of aliases */
545  maxaliases = 100;
546  aliases = (CollAliasData *) palloc(maxaliases * sizeof(CollAliasData));
547  naliases = 0;
548 
549  locale_a_handle = OpenPipeStream("locale -a", "r");
550  if (locale_a_handle == NULL)
551  ereport(ERROR,
553  errmsg("could not execute command \"%s\": %m",
554  "locale -a")));
555 
556  while (fgets(localebuf, sizeof(localebuf), locale_a_handle))
557  {
558  size_t len;
559  int enc;
560  char alias[NAMEDATALEN];
561 
562  len = strlen(localebuf);
563 
564  if (len == 0 || localebuf[len - 1] != '\n')
565  {
566  elog(DEBUG1, "locale name too long, skipped: \"%s\"", localebuf);
567  continue;
568  }
569  localebuf[len - 1] = '\0';
570 
571  /*
572  * Some systems have locale names that don't consist entirely of
573  * ASCII letters (such as "bokmål" or "français").
574  * This is pretty silly, since we need the locale itself to
575  * interpret the non-ASCII characters. We can't do much with
576  * those, so we filter them out.
577  */
578  if (!is_all_ascii(localebuf))
579  {
580  elog(DEBUG1, "locale name has non-ASCII characters, skipped: \"%s\"", localebuf);
581  continue;
582  }
583 
584  enc = pg_get_encoding_from_locale(localebuf, false);
585  if (enc < 0)
586  {
587  /* error message printed by pg_get_encoding_from_locale() */
588  continue;
589  }
590  if (!PG_VALID_BE_ENCODING(enc))
591  continue; /* ignore locales for client-only encodings */
592  if (enc == PG_SQL_ASCII)
593  continue; /* C/POSIX are already in the catalog */
594 
595  /* count valid locales found in operating system */
596  nvalid++;
597 
598  /*
599  * Create a collation named the same as the locale, but quietly
600  * doing nothing if it already exists. This is the behavior we
601  * need even at initdb time, because some versions of "locale -a"
602  * can report the same locale name more than once. And it's
603  * convenient for later import runs, too, since you just about
604  * always want to add on new locales without a lot of chatter
605  * about existing ones.
606  */
607  collid = CollationCreate(localebuf, nspid, GetUserId(),
608  COLLPROVIDER_LIBC, true, enc,
609  localebuf, localebuf,
610  get_collation_actual_version(COLLPROVIDER_LIBC, localebuf),
611  true, true);
612  if (OidIsValid(collid))
613  {
614  ncreated++;
615 
616  /* Must do CCI between inserts to handle duplicates correctly */
618  }
619 
620  /*
621  * Generate aliases such as "en_US" in addition to "en_US.utf8"
622  * for ease of use. Note that collation names are unique per
623  * encoding only, so this doesn't clash with "en_US" for LATIN1,
624  * say.
625  *
626  * However, it might conflict with a name we'll see later in the
627  * "locale -a" output. So save up the aliases and try to add them
628  * after we've read all the output.
629  */
630  if (normalize_libc_locale_name(alias, localebuf))
631  {
632  if (naliases >= maxaliases)
633  {
634  maxaliases *= 2;
635  aliases = (CollAliasData *)
636  repalloc(aliases, maxaliases * sizeof(CollAliasData));
637  }
638  aliases[naliases].localename = pstrdup(localebuf);
639  aliases[naliases].alias = pstrdup(alias);
640  aliases[naliases].enc = enc;
641  naliases++;
642  }
643  }
644 
645  ClosePipeStream(locale_a_handle);
646 
647  /*
648  * Before processing the aliases, sort them by locale name. The point
649  * here is that if "locale -a" gives us multiple locale names with the
650  * same encoding and base name, say "en_US.utf8" and "en_US.utf-8", we
651  * want to pick a deterministic one of them. First in ASCII sort
652  * order is a good enough rule. (Before PG 10, the code corresponding
653  * to this logic in initdb.c had an additional ordering rule, to
654  * prefer the locale name exactly matching the alias, if any. We
655  * don't need to consider that here, because we would have already
656  * created such a pg_collation entry above, and that one will win.)
657  */
658  if (naliases > 1)
659  qsort((void *) aliases, naliases, sizeof(CollAliasData), cmpaliases);
660 
661  /* Now add aliases, ignoring any that match pre-existing entries */
662  for (i = 0; i < naliases; i++)
663  {
664  char *locale = aliases[i].localename;
665  char *alias = aliases[i].alias;
666  int enc = aliases[i].enc;
667 
668  collid = CollationCreate(alias, nspid, GetUserId(),
669  COLLPROVIDER_LIBC, true, enc,
670  locale, locale,
671  get_collation_actual_version(COLLPROVIDER_LIBC, locale),
672  true, true);
673  if (OidIsValid(collid))
674  {
675  ncreated++;
676 
678  }
679  }
680 
681  /* Give a warning if "locale -a" seems to be malfunctioning */
682  if (nvalid == 0)
684  (errmsg("no usable system locales were found")));
685  }
686 #endif /* READ_LOCALE_A_OUTPUT */
687 
688  /*
689  * Load collations known to ICU
690  *
691  * We use uloc_countAvailable()/uloc_getAvailable() rather than
692  * ucol_countAvailable()/ucol_getAvailable(). The former returns a full
693  * set of language+region combinations, whereas the latter only returns
694  * language+region combinations of they are distinct from the language's
695  * base collation. So there might not be a de-DE or en-GB, which would be
696  * confusing.
697  */
698 #ifdef USE_ICU
699  {
700  int i;
701 
702  /*
703  * Start the loop at -1 to sneak in the root locale without too much
704  * code duplication.
705  */
706  for (i = -1; i < uloc_countAvailable(); i++)
707  {
708  const char *name;
709  char *langtag;
710  char *icucomment;
711  const char *collcollate;
712  Oid collid;
713 
714  if (i == -1)
715  name = ""; /* ICU root locale */
716  else
717  name = uloc_getAvailable(i);
718 
719  langtag = get_icu_language_tag(name);
720  collcollate = U_ICU_VERSION_MAJOR_NUM >= 54 ? langtag : name;
721 
722  /*
723  * Be paranoid about not allowing any non-ASCII strings into
724  * pg_collation
725  */
726  if (!is_all_ascii(langtag) || !is_all_ascii(collcollate))
727  continue;
728 
729  collid = CollationCreate(psprintf("%s-x-icu", langtag),
730  nspid, GetUserId(),
731  COLLPROVIDER_ICU, true, -1,
732  collcollate, collcollate,
733  get_collation_actual_version(COLLPROVIDER_ICU, collcollate),
734  true, true);
735  if (OidIsValid(collid))
736  {
737  ncreated++;
738 
740 
741  icucomment = get_icu_locale_comment(name);
742  if (icucomment)
743  CreateComments(collid, CollationRelationId, 0,
744  icucomment);
745  }
746  }
747  }
748 #endif /* USE_ICU */
749 
750  PG_RETURN_INT32(ncreated);
751 }
void CreateComments(Oid oid, Oid classoid, int32 subid, const char *comment)
Definition: comment.c:142
#define DEBUG1
Definition: elog.h:25
Oid GetUserId(void)
Definition: miscinit.c:380
char * localename
Definition: collationcmds.c:41
char * pstrdup(const char *in)
Definition: mcxt.c:1186
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
#define PG_RETURN_INT32(x)
Definition: fmgr.h:344
int errcode(int sqlerrcode)
Definition: elog.c:608
bool superuser(void)
Definition: superuser.c:46
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:645
int ClosePipeStream(FILE *file)
Definition: fd.c:2651
#define NAMEDATALEN
#define ERROR
Definition: elog.h:43
struct pg_encoding enc
Definition: encode.c:522
#define PG_GETARG_OID(n)
Definition: fmgr.h:270
int errcode_for_file_access(void)
Definition: elog.c:631
FILE * OpenPipeStream(const char *command, const char *mode)
Definition: fd.c:2345
#define ereport(elevel, rest)
Definition: elog.h:141
#define WARNING
Definition: elog.h:40
void CommandCounterIncrement(void)
Definition: xact.c:1005
int pg_get_encoding_from_locale(const char *ctype, bool write_message)
Definition: chklocale.c:433
#define PG_VALID_BE_ENCODING(_enc)
Definition: pg_wchar.h:295
Oid CollationCreate(const char *collname, Oid collnamespace, Oid collowner, char collprovider, bool collisdeterministic, int32 collencoding, const char *collcollate, const char *collctype, const char *collversion, bool if_not_exists, bool quiet)
Definition: pg_collation.c:46
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:1069
const char * name
Definition: encode.c:521
void * palloc(Size size)
Definition: mcxt.c:949
int errmsg(const char *fmt,...)
Definition: elog.c:822
#define elog(elevel,...)
Definition: elog.h:228
int i
static char * locale
Definition: initdb.c:125
#define qsort(a, b, c, d)
Definition: port.h:488
char * get_collation_actual_version(char collprovider, const char *collcollate)
Definition: pg_locale.c:1504