PostgreSQL Source Code  git master
collationcmds.h File Reference
Include dependency graph for collationcmds.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

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

Function Documentation

◆ AlterCollation()

ObjectAddress AlterCollation ( AlterCollationStmt stmt)

Definition at line 264 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_close, heap_freetuple(), heap_modify_tuple(), heap_open(), HeapTupleIsValid, InvokeObjectPostAlterHook, NameListToString(), NameStr, NoLock, NOTICE, OBJECT_COLLATION, ObjectAddressSet, ObjectIdGetDatum, pg_collation_ownercheck(), RelationGetDescr, RowExclusiveLock, SearchSysCacheCopy1, SysCacheGetAttr(), HeapTupleData::t_self, TextDatumGetCString, and values.

Referenced by ProcessUtilitySlow().

265 {
266  Relation rel;
267  Oid collOid;
268  HeapTuple tup;
269  Form_pg_collation collForm;
270  Datum collversion;
271  bool isnull;
272  char *oldversion;
273  char *newversion;
274  ObjectAddress address;
275 
276  rel = heap_open(CollationRelationId, RowExclusiveLock);
277  collOid = get_collation_oid(stmt->collname, false);
278 
279  if (!pg_collation_ownercheck(collOid, GetUserId()))
281  NameListToString(stmt->collname));
282 
284  if (!HeapTupleIsValid(tup))
285  elog(ERROR, "cache lookup failed for collation %u", collOid);
286 
287  collForm = (Form_pg_collation) GETSTRUCT(tup);
288  collversion = SysCacheGetAttr(COLLOID, tup, Anum_pg_collation_collversion,
289  &isnull);
290  oldversion = isnull ? NULL : TextDatumGetCString(collversion);
291 
292  newversion = get_collation_actual_version(collForm->collprovider, NameStr(collForm->collcollate));
293 
294  /* cannot change from NULL to non-NULL or vice versa */
295  if ((!oldversion && newversion) || (oldversion && !newversion))
296  elog(ERROR, "invalid collation version change");
297  else if (oldversion && newversion && strcmp(newversion, oldversion) != 0)
298  {
299  bool nulls[Natts_pg_collation];
300  bool replaces[Natts_pg_collation];
301  Datum values[Natts_pg_collation];
302 
303  ereport(NOTICE,
304  (errmsg("changing version from %s to %s",
305  oldversion, newversion)));
306 
307  memset(values, 0, sizeof(values));
308  memset(nulls, false, sizeof(nulls));
309  memset(replaces, false, sizeof(replaces));
310 
311  values[Anum_pg_collation_collversion - 1] = CStringGetTextDatum(newversion);
312  replaces[Anum_pg_collation_collversion - 1] = true;
313 
314  tup = heap_modify_tuple(tup, RelationGetDescr(rel),
315  values, nulls, replaces);
316  }
317  else
318  ereport(NOTICE,
319  (errmsg("version has not changed")));
320 
321  CatalogTupleUpdate(rel, &tup->t_self, tup);
322 
323  InvokeObjectPostAlterHook(CollationRelationId, collOid, 0);
324 
325  ObjectAddressSet(address, CollationRelationId, collOid);
326 
327  heap_freetuple(tup);
328  heap_close(rel, NoLock);
329 
330  return address;
331 }
bool pg_collation_ownercheck(Oid coll_oid, Oid roleid)
Definition: aclchk.c:5195
#define GETSTRUCT(TUP)
Definition: htup_details.h:668
#define RelationGetDescr(relation)
Definition: rel.h:433
Oid GetUserId(void)
Definition: miscinit.c:379
#define heap_close(r, l)
Definition: heapam.h:97
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1773
unsigned int Oid
Definition: postgres_ext.h:31
void aclcheck_error(AclResult aclerr, ObjectType objtype, const char *objectname)
Definition: aclchk.c:3349
#define ObjectIdGetDatum(X)
Definition: postgres.h:492
#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:122
#define InvokeObjectPostAlterHook(classId, objectId, subId)
Definition: objectaccess.h:163
char * NameListToString(List *names)
Definition: namespace.c:3082
#define TextDatumGetCString(d)
Definition: builtins.h:96
uintptr_t Datum
Definition: postgres.h:367
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:1368
Relation heap_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1294
#define NOTICE
Definition: elog.h:37
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
void CatalogTupleUpdate(Relation heapRel, ItemPointer otid, HeapTuple tup)
Definition: indexing.c:211
FormData_pg_collation * Form_pg_collation
Definition: pg_collation.h:49
#define ObjectAddressSet(addr, class_id, object_id)
Definition: objectaddress.h:40
static Datum values[MAXATTR]
Definition: bootstrap.c:164
#define SearchSysCacheCopy1(cacheId, key1)
Definition: syscache.h:173
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define NameStr(name)
Definition: c.h:576
#define CStringGetTextDatum(s)
Definition: builtins.h:95
#define elog
Definition: elog.h:219
HeapTuple heap_modify_tuple(HeapTuple tuple, TupleDesc tupleDesc, Datum *replValues, bool *replIsnull, bool *doReplace)
Definition: heaptuple.c:1173
Oid get_collation_oid(List *name, bool missing_ok)
Definition: namespace.c:3512
char * get_collation_actual_version(char collprovider, const char *collcollate)
Definition: pg_locale.c:1452

◆ 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(), 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 *versionEl = NULL;
63  char *collcollate = NULL;
64  char *collctype = NULL;
65  char *collproviderstr = NULL;
66  int collencoding = 0;
67  char collprovider = 0;
68  char *collversion = NULL;
69  Oid newoid;
70  ObjectAddress address;
71 
72  collNamespace = QualifiedNameGetCreationNamespace(names, &collName);
73 
74  aclresult = pg_namespace_aclcheck(collNamespace, GetUserId(), ACL_CREATE);
75  if (aclresult != ACLCHECK_OK)
76  aclcheck_error(aclresult, OBJECT_SCHEMA,
77  get_namespace_name(collNamespace));
78 
79  foreach(pl, parameters)
80  {
81  DefElem *defel = lfirst_node(DefElem, pl);
82  DefElem **defelp;
83 
84  if (strcmp(defel->defname, "from") == 0)
85  defelp = &fromEl;
86  else if (strcmp(defel->defname, "locale") == 0)
87  defelp = &localeEl;
88  else if (strcmp(defel->defname, "lc_collate") == 0)
89  defelp = &lccollateEl;
90  else if (strcmp(defel->defname, "lc_ctype") == 0)
91  defelp = &lcctypeEl;
92  else if (strcmp(defel->defname, "provider") == 0)
93  defelp = &providerEl;
94  else if (strcmp(defel->defname, "version") == 0)
95  defelp = &versionEl;
96  else
97  {
98  ereport(ERROR,
99  (errcode(ERRCODE_SYNTAX_ERROR),
100  errmsg("collation attribute \"%s\" not recognized",
101  defel->defname),
102  parser_errposition(pstate, defel->location)));
103  break;
104  }
105 
106  *defelp = defel;
107  }
108 
109  if ((localeEl && (lccollateEl || lcctypeEl))
110  || (fromEl && list_length(parameters) != 1))
111  ereport(ERROR,
112  (errcode(ERRCODE_SYNTAX_ERROR),
113  errmsg("conflicting or redundant options")));
114 
115  if (fromEl)
116  {
117  Oid collid;
118  HeapTuple tp;
119 
120  collid = get_collation_oid(defGetQualifiedName(fromEl), false);
122  if (!HeapTupleIsValid(tp))
123  elog(ERROR, "cache lookup failed for collation %u", collid);
124 
125  collcollate = pstrdup(NameStr(((Form_pg_collation) GETSTRUCT(tp))->collcollate));
126  collctype = pstrdup(NameStr(((Form_pg_collation) GETSTRUCT(tp))->collctype));
127  collprovider = ((Form_pg_collation) GETSTRUCT(tp))->collprovider;
128  collencoding = ((Form_pg_collation) GETSTRUCT(tp))->collencoding;
129 
130  ReleaseSysCache(tp);
131 
132  /*
133  * Copying the "default" collation is not allowed because most code
134  * checks for DEFAULT_COLLATION_OID instead of COLLPROVIDER_DEFAULT,
135  * and so having a second collation with COLLPROVIDER_DEFAULT would
136  * not work and potentially confuse or crash some code. This could be
137  * fixed with some legwork.
138  */
139  if (collprovider == COLLPROVIDER_DEFAULT)
140  ereport(ERROR,
141  (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
142  errmsg("collation \"default\" cannot be copied")));
143  }
144 
145  if (localeEl)
146  {
147  collcollate = defGetString(localeEl);
148  collctype = defGetString(localeEl);
149  }
150 
151  if (lccollateEl)
152  collcollate = defGetString(lccollateEl);
153 
154  if (lcctypeEl)
155  collctype = defGetString(lcctypeEl);
156 
157  if (providerEl)
158  collproviderstr = defGetString(providerEl);
159 
160  if (versionEl)
161  collversion = defGetString(versionEl);
162 
163  if (collproviderstr)
164  {
165  if (pg_strcasecmp(collproviderstr, "icu") == 0)
166  collprovider = COLLPROVIDER_ICU;
167  else if (pg_strcasecmp(collproviderstr, "libc") == 0)
168  collprovider = COLLPROVIDER_LIBC;
169  else
170  ereport(ERROR,
171  (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
172  errmsg("unrecognized collation provider: %s",
173  collproviderstr)));
174  }
175  else if (!fromEl)
176  collprovider = COLLPROVIDER_LIBC;
177 
178  if (!collcollate)
179  ereport(ERROR,
180  (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
181  errmsg("parameter \"lc_collate\" must be specified")));
182 
183  if (!collctype)
184  ereport(ERROR,
185  (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
186  errmsg("parameter \"lc_ctype\" must be specified")));
187 
188  if (!fromEl)
189  {
190  if (collprovider == COLLPROVIDER_ICU)
191  collencoding = -1;
192  else
193  {
194  collencoding = GetDatabaseEncoding();
195  check_encoding_locale_matches(collencoding, collcollate, collctype);
196  }
197  }
198 
199  if (!collversion)
200  collversion = get_collation_actual_version(collprovider, collcollate);
201 
202  newoid = CollationCreate(collName,
203  collNamespace,
204  GetUserId(),
205  collprovider,
206  collencoding,
207  collcollate,
208  collctype,
209  collversion,
210  if_not_exists,
211  false); /* not quiet */
212 
213  if (!OidIsValid(newoid))
214  return InvalidObjectAddress;
215 
216  /*
217  * Check that the locales can be loaded. NB: pg_newlocale_from_collation
218  * is only supposed to be called on non-C-equivalent locales.
219  */
221  if (!lc_collate_is_c(newoid) || !lc_ctype_is_c(newoid))
222  (void) pg_newlocale_from_collation(newoid);
223 
224  ObjectAddressSet(address, CollationRelationId, newoid);
225 
226  return address;
227 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:668
void check_encoding_locale_matches(int encoding, const char *collate, const char *ctype)
Definition: dbcommands.c:722
Oid GetUserId(void)
Definition: miscinit.c:379
Oid QualifiedNameGetCreationNamespace(List *names, char **objname_p)
Definition: namespace.c:2975
char * pstrdup(const char *in)
Definition: mcxt.c:1161
int errcode(int sqlerrcode)
Definition: elog.c:575
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:605
AclResult pg_namespace_aclcheck(Oid nsp_oid, Oid roleid, AclMode mode)
Definition: aclchk.c:4689
void aclcheck_error(AclResult aclerr, ObjectType objtype, const char *objectname)
Definition: aclchk.c:3349
#define ObjectIdGetDatum(X)
Definition: postgres.h:492
#define ERROR
Definition: elog.h:43
#define ACL_CREATE
Definition: parsenodes.h:84
bool lc_collate_is_c(Oid collation)
Definition: pg_locale.c:1128
char * defGetString(DefElem *def)
Definition: define.c:49
#define lfirst_node(type, lc)
Definition: pg_list.h:109
char * get_namespace_name(Oid nspid)
Definition: lsyscache.c:3051
int location
Definition: parsenodes.h:733
#define ereport(elevel, rest)
Definition: elog.h:122
Oid CollationCreate(const char *collname, Oid collnamespace, Oid collowner, char collprovider, int32 collencoding, const char *collcollate, const char *collctype, const char *collversion, bool if_not_exists, bool quiet)
Definition: pg_collation.c:46
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1112
pg_locale_t pg_newlocale_from_collation(Oid collid)
Definition: pg_locale.c:1275
AclResult
Definition: acl.h:178
void CommandCounterIncrement(void)
Definition: xact.c:914
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1160
int GetDatabaseEncoding(void)
Definition: mbutils.c:1004
#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:89
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:111
FormData_pg_collation * Form_pg_collation
Definition: pg_collation.h:49
#define ObjectAddressSet(addr, class_id, object_id)
Definition: objectaddress.h:40
const ObjectAddress InvalidObjectAddress
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define NameStr(name)
Definition: c.h:576
char * defname
Definition: parsenodes.h:730
#define elog
Definition: elog.h:219
bool lc_ctype_is_c(Oid collation)
Definition: pg_locale.c:1178
Oid get_collation_oid(List *name, bool missing_ok)
Definition: namespace.c:3512
char * get_collation_actual_version(char collprovider, const char *collcollate)
Definition: pg_locale.c:1452

◆ IsThereCollationInNamespace()

void IsThereCollationInNamespace ( const char *  collname,
Oid  nspOid 
)

Definition at line 236 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().

237 {
238  /* make sure the name doesn't already exist in new schema */
240  CStringGetDatum(collname),
242  ObjectIdGetDatum(nspOid)))
243  ereport(ERROR,
245  errmsg("collation \"%s\" for encoding \"%s\" already exists in schema \"%s\"",
246  collname, GetDatabaseEncodingName(),
247  get_namespace_name(nspOid))));
248 
249  /* mustn't match an any-encoding entry, either */
251  CStringGetDatum(collname),
252  Int32GetDatum(-1),
253  ObjectIdGetDatum(nspOid)))
254  ereport(ERROR,
256  errmsg("collation \"%s\" already exists in schema \"%s\"",
257  collname, get_namespace_name(nspOid))));
258 }
int errcode(int sqlerrcode)
Definition: elog.c:575
#define ObjectIdGetDatum(X)
Definition: postgres.h:492
#define ERROR
Definition: elog.h:43
#define SearchSysCacheExists3(cacheId, key1, key2, key3)
Definition: syscache.h:186
char * get_namespace_name(Oid nspid)
Definition: lsyscache.c:3051
#define CStringGetDatum(X)
Definition: postgres.h:563
#define ereport(elevel, rest)
Definition: elog.h:122
int GetDatabaseEncoding(void)
Definition: mbutils.c:1004
const char * GetDatabaseEncodingName(void)
Definition: mbutils.c:1010
#define Int32GetDatum(X)
Definition: postgres.h:464
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define ERRCODE_DUPLICATE_OBJECT
Definition: streamutil.c:32