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-2018, 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/heapam.h"
19 #include "access/htup_details.h"
20 #include "access/sysattr.h"
21 #include "catalog/dependency.h"
22 #include "catalog/indexing.h"
23 #include "catalog/objectaccess.h"
24 #include "catalog/pg_collation.h"
25 #include "catalog/pg_namespace.h"
26 #include "mb/pg_wchar.h"
27 #include "utils/builtins.h"
28 #include "utils/fmgroids.h"
29 #include "utils/pg_locale.h"
30 #include "utils/rel.h"
31 #include "utils/syscache.h"
32 #include "utils/tqual.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  int32 collencoding,
50  const char *collcollate, const char *collctype,
51  const char *collversion,
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 = heap_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  heap_close(rel, NoLock);
132  return InvalidOid;
133  }
134  else if (if_not_exists)
135  {
136  heap_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);
156  values[Anum_pg_collation_collname - 1] = NameGetDatum(&name_name);
157  values[Anum_pg_collation_collnamespace - 1] = ObjectIdGetDatum(collnamespace);
158  values[Anum_pg_collation_collowner - 1] = ObjectIdGetDatum(collowner);
159  values[Anum_pg_collation_collprovider - 1] = CharGetDatum(collprovider);
160  values[Anum_pg_collation_collencoding - 1] = Int32GetDatum(collencoding);
161  namestrcpy(&name_collate, collcollate);
162  values[Anum_pg_collation_collcollate - 1] = NameGetDatum(&name_collate);
163  namestrcpy(&name_ctype, collctype);
164  values[Anum_pg_collation_collctype - 1] = NameGetDatum(&name_ctype);
165  if (collversion)
166  values[Anum_pg_collation_collversion - 1] = CStringGetTextDatum(collversion);
167  else
168  nulls[Anum_pg_collation_collversion - 1] = true;
169 
170  tup = heap_form_tuple(tupDesc, values, nulls);
171 
172  /* insert a new tuple */
173  oid = 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, HeapTupleGetOid(tup),
189  collowner);
190 
191  /* dependency on extension */
192  recordDependencyOnCurrentExtension(&myself, false);
193 
194  /* Post creation hook for new collation */
195  InvokeObjectPostCreateHook(CollationRelationId, oid, 0);
196 
197  heap_freetuple(tup);
198  heap_close(rel, NoLock);
199 
200  return oid;
201 }
202 
203 /*
204  * RemoveCollationById
205  *
206  * Remove a tuple from pg_collation by Oid. This function is solely
207  * called inside catalog/dependency.c
208  */
209 void
211 {
212  Relation rel;
213  ScanKeyData scanKeyData;
214  SysScanDesc scandesc;
215  HeapTuple tuple;
216 
217  rel = heap_open(CollationRelationId, RowExclusiveLock);
218 
219  ScanKeyInit(&scanKeyData,
221  BTEqualStrategyNumber, F_OIDEQ,
222  ObjectIdGetDatum(collationOid));
223 
224  scandesc = systable_beginscan(rel, CollationOidIndexId, true,
225  NULL, 1, &scanKeyData);
226 
227  tuple = systable_getnext(scandesc);
228 
229  if (HeapTupleIsValid(tuple))
230  CatalogTupleDelete(rel, &tuple->t_self);
231  else
232  elog(ERROR, "could not find tuple for collation %u", collationOid);
233 
234  systable_endscan(scandesc);
235 
237 }
#define NameGetDatum(X)
Definition: postgres.h:580
#define CollationOidIndexId
Definition: indexing.h:120
void systable_endscan(SysScanDesc sysscan)
Definition: genam.c:502
#define InvokeObjectPostCreateHook(classId, objectId, subId)
Definition: objectaccess.h:145
#define RelationGetDescr(relation)
Definition: rel.h:433
#define ObjectIdAttributeNumber
Definition: sysattr.h:22
#define PointerGetDatum(X)
Definition: postgres.h:541
int errcode(int sqlerrcode)
Definition: elog.c:575
void CatalogTupleDelete(Relation heapRel, ItemPointer tid)
Definition: indexing.c:256
void recordDependencyOn(const ObjectAddress *depender, const ObjectAddress *referenced, DependencyType behavior)
Definition: pg_depend.c:44
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: heaptuple.c:1074
#define heap_close(r, l)
Definition: heapam.h:97
void recordDependencyOnOwner(Oid classId, Oid objectId, Oid owner)
Definition: pg_shdepend.c:159
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1773
unsigned int Oid
Definition: postgres_ext.h:31
int namestrcpy(Name name, const char *str)
Definition: name.c:216
#define OidIsValid(objectId)
Definition: c.h:605
void RemoveCollationById(Oid collationOid)
Definition: pg_collation.c:210
SysScanDesc systable_beginscan(Relation heapRelation, Oid indexId, bool indexOK, Snapshot snapshot, int nkeys, ScanKey key)
Definition: genam.c:331
signed int int32
Definition: c.h:313
HeapTuple systable_getnext(SysScanDesc sysscan)
Definition: genam.c:419
#define ObjectIdGetDatum(X)
Definition: postgres.h:492
#define ERROR
Definition: elog.h:43
#define SearchSysCacheExists3(cacheId, key1, key2, key3)
Definition: syscache.h:186
Oid CatalogTupleInsert(Relation heapRel, HeapTuple tup)
Definition: indexing.c:163
ItemPointerData t_self
Definition: htup.h:65
Definition: c.h:570
#define NoLock
Definition: lockdefs.h:34
#define RowExclusiveLock
Definition: lockdefs.h:38
#define ereport(elevel, rest)
Definition: elog.h:122
#define AssertArg(condition)
Definition: c.h:701
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
uintptr_t Datum
Definition: postgres.h:367
int GetDatabaseEncoding(void)
Definition: mbutils.c:1004
Relation heap_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1294
#define InvalidOid
Definition: postgres_ext.h:36
#define NOTICE
Definition: elog.h:37
const char * pg_encoding_to_char(int encoding)
Definition: encnames.c:607
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define Assert(condition)
Definition: c.h:699
#define ShareRowExclusiveLock
Definition: lockdefs.h:42
void recordDependencyOnCurrentExtension(const ObjectAddress *object, bool isReplace)
Definition: pg_depend.c:139
#define CharGetDatum(X)
Definition: postgres.h:401
static Datum values[MAXATTR]
Definition: bootstrap.c:164
#define Int32GetDatum(X)
Definition: postgres.h:464
int errmsg(const char *fmt,...)
Definition: elog.c:797
void ScanKeyInit(ScanKey entry, AttrNumber attributeNumber, StrategyNumber strategy, RegProcedure procedure, Datum argument)
Definition: scankey.c:76
#define CStringGetTextDatum(s)
Definition: builtins.h:95
#define elog
Definition: elog.h:219
#define HeapTupleGetOid(tuple)
Definition: htup_details.h:707
#define ERRCODE_DUPLICATE_OBJECT
Definition: streamutil.c:32
#define BTEqualStrategyNumber
Definition: stratnum.h:31