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-2021, 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  bool if_not_exists,
53  bool quiet)
54 {
55  Relation rel;
56  TupleDesc tupDesc;
57  HeapTuple tup;
58  Datum values[Natts_pg_collation];
59  bool nulls[Natts_pg_collation];
60  NameData name_name,
61  name_collate,
62  name_ctype;
63  Oid oid;
64  ObjectAddress myself,
65  referenced;
66 
67  AssertArg(collname);
68  AssertArg(collnamespace);
69  AssertArg(collowner);
70  AssertArg(collcollate);
71  AssertArg(collctype);
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  PointerGetDatum(collname),
82  Int32GetDatum(collencoding),
83  ObjectIdGetDatum(collnamespace)))
84  {
85  if (quiet)
86  return InvalidOid;
87  else if (if_not_exists)
88  {
91  collencoding == -1
92  ? errmsg("collation \"%s\" already exists, skipping",
93  collname)
94  : errmsg("collation \"%s\" for encoding \"%s\" already exists, skipping",
95  collname, pg_encoding_to_char(collencoding))));
96  return InvalidOid;
97  }
98  else
99  ereport(ERROR,
101  collencoding == -1
102  ? errmsg("collation \"%s\" already exists",
103  collname)
104  : errmsg("collation \"%s\" for encoding \"%s\" already exists",
105  collname, pg_encoding_to_char(collencoding))));
106  }
107 
108  /* open pg_collation; see below about the lock level */
109  rel = table_open(CollationRelationId, ShareRowExclusiveLock);
110 
111  /*
112  * Also forbid a specific-encoding collation shadowing an any-encoding
113  * collation, or an any-encoding collation being shadowed (see
114  * get_collation_name()). This test is not backed up by the unique index,
115  * so we take a ShareRowExclusiveLock earlier, to protect against
116  * concurrent changes fooling this check.
117  */
118  if ((collencoding == -1 &&
120  PointerGetDatum(collname),
122  ObjectIdGetDatum(collnamespace))) ||
123  (collencoding != -1 &&
125  PointerGetDatum(collname),
126  Int32GetDatum(-1),
127  ObjectIdGetDatum(collnamespace))))
128  {
129  if (quiet)
130  {
131  table_close(rel, NoLock);
132  return InvalidOid;
133  }
134  else if (if_not_exists)
135  {
136  table_close(rel, NoLock);
137  ereport(NOTICE,
139  errmsg("collation \"%s\" already exists, skipping",
140  collname)));
141  return InvalidOid;
142  }
143  else
144  ereport(ERROR,
146  errmsg("collation \"%s\" already exists",
147  collname)));
148  }
149 
150  tupDesc = RelationGetDescr(rel);
151 
152  /* form a tuple */
153  memset(nulls, 0, sizeof(nulls));
154 
155  namestrcpy(&name_name, collname);
157  Anum_pg_collation_oid);
158  values[Anum_pg_collation_oid - 1] = ObjectIdGetDatum(oid);
159  values[Anum_pg_collation_collname - 1] = NameGetDatum(&name_name);
160  values[Anum_pg_collation_collnamespace - 1] = ObjectIdGetDatum(collnamespace);
161  values[Anum_pg_collation_collowner - 1] = ObjectIdGetDatum(collowner);
162  values[Anum_pg_collation_collprovider - 1] = CharGetDatum(collprovider);
163  values[Anum_pg_collation_collisdeterministic - 1] = BoolGetDatum(collisdeterministic);
164  values[Anum_pg_collation_collencoding - 1] = Int32GetDatum(collencoding);
165  namestrcpy(&name_collate, collcollate);
166  values[Anum_pg_collation_collcollate - 1] = NameGetDatum(&name_collate);
167  namestrcpy(&name_ctype, collctype);
168  values[Anum_pg_collation_collctype - 1] = NameGetDatum(&name_ctype);
169 
170  tup = heap_form_tuple(tupDesc, values, nulls);
171 
172  /* insert a new tuple */
173  CatalogTupleInsert(rel, tup);
174  Assert(OidIsValid(oid));
175 
176  /* set up dependencies for the new collation */
177  myself.classId = CollationRelationId;
178  myself.objectId = oid;
179  myself.objectSubId = 0;
180 
181  /* create dependency on namespace */
182  referenced.classId = NamespaceRelationId;
183  referenced.objectId = collnamespace;
184  referenced.objectSubId = 0;
185  recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL);
186 
187  /* create dependency on owner */
188  recordDependencyOnOwner(CollationRelationId, oid, collowner);
189 
190  /* dependency on extension */
191  recordDependencyOnCurrentExtension(&myself, false);
192 
193  /* Post creation hook for new collation */
194  InvokeObjectPostCreateHook(CollationRelationId, oid, 0);
195 
196  heap_freetuple(tup);
197  table_close(rel, NoLock);
198 
199  return oid;
200 }
Oid GetNewOidWithIndex(Relation relation, Oid indexId, AttrNumber oidcolumn)
Definition: catalog.c:315
#define NameGetDatum(X)
Definition: postgres.h:595
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167
#define InvokeObjectPostCreateHook(classId, objectId, subId)
Definition: objectaccess.h:151
#define RelationGetDescr(relation)
Definition: rel.h:483
void namestrcpy(Name name, const char *str)
Definition: name.c:233
#define PointerGetDatum(X)
Definition: postgres.h:556
Oid CollationCreate(const char *collname, Oid collnamespace, Oid collowner, char collprovider, bool collisdeterministic, int32 collencoding, const char *collcollate, const char *collctype, bool if_not_exists, bool quiet)
Definition: pg_collation.c:46
int errcode(int sqlerrcode)
Definition: elog.c:704
void recordDependencyOn(const ObjectAddress *depender, const ObjectAddress *referenced, DependencyType behavior)
Definition: pg_depend.c:46
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: heaptuple.c:1020
void recordDependencyOnOwner(Oid classId, Oid objectId, Oid owner)
Definition: pg_shdepend.c:165
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1338
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:698
signed int int32
Definition: c.h:417
#define CollationOidIndexId
Definition: pg_collation.h:52
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:45
#define SearchSysCacheExists3(cacheId, key1, key2, key3)
Definition: syscache.h:188
Definition: c.h:663
#define NoLock
Definition: lockdefs.h:34
#define AssertArg(condition)
Definition: c.h:794
uintptr_t Datum
Definition: postgres.h:367
int GetDatabaseEncoding(void)
Definition: mbutils.c:1151
#define BoolGetDatum(X)
Definition: postgres.h:402
#define InvalidOid
Definition: postgres_ext.h:36
#define ereport(elevel,...)
Definition: elog.h:155
#define NOTICE
Definition: elog.h:37
const char * pg_encoding_to_char(int encoding)
Definition: encnames.c:588
#define Assert(condition)
Definition: c.h:792
#define ShareRowExclusiveLock
Definition: lockdefs.h:42
void recordDependencyOnCurrentExtension(const ObjectAddress *object, bool isReplace)
Definition: pg_depend.c:220
#define CharGetDatum(X)
Definition: postgres.h:416
static Datum values[MAXATTR]
Definition: bootstrap.c:165
#define Int32GetDatum(X)
Definition: postgres.h:479
int errmsg(const char *fmt,...)
Definition: elog.c:915
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
#define ERRCODE_DUPLICATE_OBJECT
Definition: streamutil.c:32
void CatalogTupleInsert(Relation heapRel, HeapTuple tup)
Definition: indexing.c:222