PostgreSQL Source Code  git master
pg_collation.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * pg_collation.c
4  * routines to support manipulation of the pg_collation relation
5  *
6  * Portions Copyright (c) 1996-2023, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  *
10  * IDENTIFICATION
11  * src/backend/catalog/pg_collation.c
12  *
13  *-------------------------------------------------------------------------
14  */
15 #include "postgres.h"
16 
17 #include "access/genam.h"
18 #include "access/htup_details.h"
19 #include "access/sysattr.h"
20 #include "access/table.h"
21 #include "catalog/catalog.h"
22 #include "catalog/dependency.h"
23 #include "catalog/indexing.h"
24 #include "catalog/objectaccess.h"
25 #include "catalog/pg_collation.h"
26 #include "catalog/pg_namespace.h"
27 #include "mb/pg_wchar.h"
28 #include "utils/builtins.h"
29 #include "utils/fmgroids.h"
30 #include "utils/pg_locale.h"
31 #include "utils/rel.h"
32 #include "utils/syscache.h"
33 
34 
35 /*
36  * CollationCreate
37  *
38  * Add a new tuple to pg_collation.
39  *
40  * if_not_exists: if true, don't fail on duplicate name, just print a notice
41  * and return InvalidOid.
42  * quiet: if true, don't fail on duplicate name, just silently return
43  * InvalidOid (overrides if_not_exists).
44  */
45 Oid
46 CollationCreate(const char *collname, Oid collnamespace,
47  Oid collowner,
48  char collprovider,
49  bool collisdeterministic,
50  int32 collencoding,
51  const char *collcollate, const char *collctype,
52  const char *colliculocale,
53  const char *collicurules,
54  const char *collversion,
55  bool if_not_exists,
56  bool quiet)
57 {
58  Relation rel;
59  TupleDesc tupDesc;
60  HeapTuple tup;
61  Datum values[Natts_pg_collation];
62  bool nulls[Natts_pg_collation];
63  NameData name_name;
64  Oid oid;
65  ObjectAddress myself,
66  referenced;
67 
68  Assert(collname);
69  Assert(collnamespace);
70  Assert(collowner);
71  Assert((collcollate && collctype) || colliculocale);
72 
73  /*
74  * Make sure there is no existing collation of same name & encoding.
75  *
76  * This would be caught by the unique index anyway; we're just giving a
77  * friendlier error message. The unique index provides a backstop against
78  * race conditions.
79  */
81  Anum_pg_collation_oid,
82  PointerGetDatum(collname),
83  Int32GetDatum(collencoding),
84  ObjectIdGetDatum(collnamespace));
85  if (OidIsValid(oid))
86  {
87  if (quiet)
88  return InvalidOid;
89  else if (if_not_exists)
90  {
91  /*
92  * If we are in an extension script, insist that the pre-existing
93  * object be a member of the extension, to avoid security risks.
94  */
95  ObjectAddressSet(myself, CollationRelationId, oid);
97 
98  /* OK to skip */
101  collencoding == -1
102  ? errmsg("collation \"%s\" already exists, skipping",
103  collname)
104  : errmsg("collation \"%s\" for encoding \"%s\" already exists, skipping",
105  collname, pg_encoding_to_char(collencoding))));
106  return InvalidOid;
107  }
108  else
109  ereport(ERROR,
111  collencoding == -1
112  ? errmsg("collation \"%s\" already exists",
113  collname)
114  : errmsg("collation \"%s\" for encoding \"%s\" already exists",
115  collname, pg_encoding_to_char(collencoding))));
116  }
117 
118  /* open pg_collation; see below about the lock level */
119  rel = table_open(CollationRelationId, ShareRowExclusiveLock);
120 
121  /*
122  * Also forbid a specific-encoding collation shadowing an any-encoding
123  * collation, or an any-encoding collation being shadowed (see
124  * get_collation_name()). This test is not backed up by the unique index,
125  * so we take a ShareRowExclusiveLock earlier, to protect against
126  * concurrent changes fooling this check.
127  */
128  if (collencoding == -1)
130  Anum_pg_collation_oid,
131  PointerGetDatum(collname),
133  ObjectIdGetDatum(collnamespace));
134  else
136  Anum_pg_collation_oid,
137  PointerGetDatum(collname),
138  Int32GetDatum(-1),
139  ObjectIdGetDatum(collnamespace));
140  if (OidIsValid(oid))
141  {
142  if (quiet)
143  {
144  table_close(rel, NoLock);
145  return InvalidOid;
146  }
147  else if (if_not_exists)
148  {
149  /*
150  * If we are in an extension script, insist that the pre-existing
151  * object be a member of the extension, to avoid security risks.
152  */
153  ObjectAddressSet(myself, CollationRelationId, oid);
155 
156  /* OK to skip */
157  table_close(rel, NoLock);
158  ereport(NOTICE,
160  errmsg("collation \"%s\" already exists, skipping",
161  collname)));
162  return InvalidOid;
163  }
164  else
165  ereport(ERROR,
167  errmsg("collation \"%s\" already exists",
168  collname)));
169  }
170 
171  tupDesc = RelationGetDescr(rel);
172 
173  /* form a tuple */
174  memset(nulls, 0, sizeof(nulls));
175 
176  namestrcpy(&name_name, collname);
177  oid = GetNewOidWithIndex(rel, CollationOidIndexId,
178  Anum_pg_collation_oid);
179  values[Anum_pg_collation_oid - 1] = ObjectIdGetDatum(oid);
180  values[Anum_pg_collation_collname - 1] = NameGetDatum(&name_name);
181  values[Anum_pg_collation_collnamespace - 1] = ObjectIdGetDatum(collnamespace);
182  values[Anum_pg_collation_collowner - 1] = ObjectIdGetDatum(collowner);
183  values[Anum_pg_collation_collprovider - 1] = CharGetDatum(collprovider);
184  values[Anum_pg_collation_collisdeterministic - 1] = BoolGetDatum(collisdeterministic);
185  values[Anum_pg_collation_collencoding - 1] = Int32GetDatum(collencoding);
186  if (collcollate)
187  values[Anum_pg_collation_collcollate - 1] = CStringGetTextDatum(collcollate);
188  else
189  nulls[Anum_pg_collation_collcollate - 1] = true;
190  if (collctype)
191  values[Anum_pg_collation_collctype - 1] = CStringGetTextDatum(collctype);
192  else
193  nulls[Anum_pg_collation_collctype - 1] = true;
194  if (colliculocale)
195  values[Anum_pg_collation_colliculocale - 1] = CStringGetTextDatum(colliculocale);
196  else
197  nulls[Anum_pg_collation_colliculocale - 1] = true;
198  if (collicurules)
199  values[Anum_pg_collation_collicurules - 1] = CStringGetTextDatum(collicurules);
200  else
201  nulls[Anum_pg_collation_collicurules - 1] = true;
202  if (collversion)
203  values[Anum_pg_collation_collversion - 1] = CStringGetTextDatum(collversion);
204  else
205  nulls[Anum_pg_collation_collversion - 1] = true;
206 
207  tup = heap_form_tuple(tupDesc, values, nulls);
208 
209  /* insert a new tuple */
210  CatalogTupleInsert(rel, tup);
211  Assert(OidIsValid(oid));
212 
213  /* set up dependencies for the new collation */
214  myself.classId = CollationRelationId;
215  myself.objectId = oid;
216  myself.objectSubId = 0;
217 
218  /* create dependency on namespace */
219  referenced.classId = NamespaceRelationId;
220  referenced.objectId = collnamespace;
221  referenced.objectSubId = 0;
222  recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL);
223 
224  /* create dependency on owner */
225  recordDependencyOnOwner(CollationRelationId, oid, collowner);
226 
227  /* dependency on extension */
228  recordDependencyOnCurrentExtension(&myself, false);
229 
230  /* Post creation hook for new collation */
231  InvokeObjectPostCreateHook(CollationRelationId, oid, 0);
232 
233  heap_freetuple(tup);
234  table_close(rel, NoLock);
235 
236  return oid;
237 }
static Datum values[MAXATTR]
Definition: bootstrap.c:156
#define CStringGetTextDatum(s)
Definition: builtins.h:94
signed int int32
Definition: c.h:483
#define OidIsValid(objectId)
Definition: c.h:764
Oid GetNewOidWithIndex(Relation relation, Oid indexId, AttrNumber oidcolumn)
Definition: catalog.c:393
@ DEPENDENCY_NORMAL
Definition: dependency.h:33
int errcode(int sqlerrcode)
Definition: elog.c:858
int errmsg(const char *fmt,...)
Definition: elog.c:1069
#define ERROR
Definition: elog.h:39
#define NOTICE
Definition: elog.h:35
#define ereport(elevel,...)
Definition: elog.h:149
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, const Datum *values, const bool *isnull)
Definition: heaptuple.c:1117
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1435
void CatalogTupleInsert(Relation heapRel, HeapTuple tup)
Definition: indexing.c:233
Assert(fmt[strlen(fmt) - 1] !='\n')
#define NoLock
Definition: lockdefs.h:34
#define ShareRowExclusiveLock
Definition: lockdefs.h:41
int GetDatabaseEncoding(void)
Definition: mbutils.c:1268
void namestrcpy(Name name, const char *str)
Definition: name.c:233
#define InvokeObjectPostCreateHook(classId, objectId, subId)
Definition: objectaccess.h:173
#define ObjectAddressSet(addr, class_id, object_id)
Definition: objectaddress.h:40
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 *collicurules, const char *collversion, bool if_not_exists, bool quiet)
Definition: pg_collation.c:46
void checkMembershipInCurrentExtension(const ObjectAddress *object)
Definition: pg_depend.c:257
void recordDependencyOn(const ObjectAddress *depender, const ObjectAddress *referenced, DependencyType behavior)
Definition: pg_depend.c:44
void recordDependencyOnCurrentExtension(const ObjectAddress *object, bool isReplace)
Definition: pg_depend.c:192
void recordDependencyOnOwner(Oid classId, Oid objectId, Oid owner)
Definition: pg_shdepend.c:165
#define pg_encoding_to_char
Definition: pg_wchar.h:563
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:322
uintptr_t Datum
Definition: postgres.h:64
static Datum BoolGetDatum(bool X)
Definition: postgres.h:102
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:252
static Datum NameGetDatum(const NameData *X)
Definition: postgres.h:373
static Datum Int32GetDatum(int32 X)
Definition: postgres.h:212
static Datum CharGetDatum(char X)
Definition: postgres.h:122
#define InvalidOid
Definition: postgres_ext.h:36
unsigned int Oid
Definition: postgres_ext.h:31
#define RelationGetDescr(relation)
Definition: rel.h:530
#define ERRCODE_DUPLICATE_OBJECT
Definition: streamutil.c:32
Definition: c.h:730
@ COLLNAMEENCNSP
Definition: syscache.h:49
#define GetSysCacheOid3(cacheId, oidcol, key1, key2, key3)
Definition: syscache.h:204
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:126
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:40