PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
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-2017, 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"
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 #include "utils/tqual.h"
34 
35 
36 /*
37  * CollationCreate
38  *
39  * Add a new tuple to pg_collation.
40  */
41 Oid
42 CollationCreate(const char *collname, Oid collnamespace,
43  Oid collowner,
44  char collprovider,
45  int32 collencoding,
46  const char *collcollate, const char *collctype,
47  const char *collversion,
48  bool if_not_exists)
49 {
50  Relation rel;
51  TupleDesc tupDesc;
52  HeapTuple tup;
54  bool nulls[Natts_pg_collation];
55  NameData name_name,
56  name_collate,
57  name_ctype;
58  Oid oid;
59  ObjectAddress myself,
60  referenced;
61 
62  AssertArg(collname);
63  AssertArg(collnamespace);
64  AssertArg(collowner);
65  AssertArg(collcollate);
66  AssertArg(collctype);
67 
68  /*
69  * Make sure there is no existing collation of same name & encoding.
70  *
71  * This would be caught by the unique index anyway; we're just giving a
72  * friendlier error message. The unique index provides a backstop against
73  * race conditions.
74  */
76  PointerGetDatum(collname),
77  Int32GetDatum(collencoding),
78  ObjectIdGetDatum(collnamespace)))
79  {
80  if (if_not_exists)
81  {
84  collencoding == -1
85  ? errmsg("collation \"%s\" already exists, skipping",
86  collname)
87  : errmsg("collation \"%s\" for encoding \"%s\" already exists, skipping",
88  collname, pg_encoding_to_char(collencoding))));
89  return InvalidOid;
90  }
91  else
92  ereport(ERROR,
94  collencoding == -1
95  ? errmsg("collation \"%s\" already exists",
96  collname)
97  : errmsg("collation \"%s\" for encoding \"%s\" already exists",
98  collname, pg_encoding_to_char(collencoding))));
99  }
100 
101  /* open pg_collation; see below about the lock level */
103 
104  /*
105  * Also forbid a specific-encoding collation shadowing an any-encoding
106  * collation, or an any-encoding collation being shadowed (see
107  * get_collation_name()). This test is not backed up by the unique index,
108  * so we take a ShareRowExclusiveLock earlier, to protect against
109  * concurrent changes fooling this check.
110  */
111  if ((collencoding == -1 &&
113  PointerGetDatum(collname),
115  ObjectIdGetDatum(collnamespace))) ||
116  (collencoding != -1 &&
118  PointerGetDatum(collname),
119  Int32GetDatum(-1),
120  ObjectIdGetDatum(collnamespace))))
121  {
122  if (if_not_exists)
123  {
124  heap_close(rel, NoLock);
125  ereport(NOTICE,
127  errmsg("collation \"%s\" already exists, skipping",
128  collname)));
129  return InvalidOid;
130  }
131  else
132  ereport(ERROR,
134  errmsg("collation \"%s\" already exists",
135  collname)));
136  }
137 
138  tupDesc = RelationGetDescr(rel);
139 
140  /* form a tuple */
141  memset(nulls, 0, sizeof(nulls));
142 
143  namestrcpy(&name_name, collname);
144  values[Anum_pg_collation_collname - 1] = NameGetDatum(&name_name);
145  values[Anum_pg_collation_collnamespace - 1] = ObjectIdGetDatum(collnamespace);
146  values[Anum_pg_collation_collowner - 1] = ObjectIdGetDatum(collowner);
147  values[Anum_pg_collation_collprovider - 1] = CharGetDatum(collprovider);
148  values[Anum_pg_collation_collencoding - 1] = Int32GetDatum(collencoding);
149  namestrcpy(&name_collate, collcollate);
150  values[Anum_pg_collation_collcollate - 1] = NameGetDatum(&name_collate);
151  namestrcpy(&name_ctype, collctype);
152  values[Anum_pg_collation_collctype - 1] = NameGetDatum(&name_ctype);
153  if (collversion)
154  values[Anum_pg_collation_collversion - 1] = CStringGetTextDatum(collversion);
155  else
156  nulls[Anum_pg_collation_collversion - 1] = true;
157 
158  tup = heap_form_tuple(tupDesc, values, nulls);
159 
160  /* insert a new tuple */
161  oid = CatalogTupleInsert(rel, tup);
162  Assert(OidIsValid(oid));
163 
164  /* set up dependencies for the new collation */
165  myself.classId = CollationRelationId;
166  myself.objectId = oid;
167  myself.objectSubId = 0;
168 
169  /* create dependency on namespace */
170  referenced.classId = NamespaceRelationId;
171  referenced.objectId = collnamespace;
172  referenced.objectSubId = 0;
173  recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL);
174 
175  /* create dependency on owner */
177  collowner);
178 
179  /* dependency on extension */
180  recordDependencyOnCurrentExtension(&myself, false);
181 
182  /* Post creation hook for new collation */
184 
185  heap_freetuple(tup);
186  heap_close(rel, NoLock);
187 
188  return oid;
189 }
190 
191 /*
192  * RemoveCollationById
193  *
194  * Remove a tuple from pg_collation by Oid. This function is solely
195  * called inside catalog/dependency.c
196  */
197 void
199 {
200  Relation rel;
201  ScanKeyData scanKeyData;
202  SysScanDesc scandesc;
203  HeapTuple tuple;
204 
206 
207  ScanKeyInit(&scanKeyData,
209  BTEqualStrategyNumber, F_OIDEQ,
210  ObjectIdGetDatum(collationOid));
211 
212  scandesc = systable_beginscan(rel, CollationOidIndexId, true,
213  NULL, 1, &scanKeyData);
214 
215  tuple = systable_getnext(scandesc);
216 
217  if (HeapTupleIsValid(tuple))
218  CatalogTupleDelete(rel, &tuple->t_self);
219  else
220  elog(ERROR, "could not find tuple for collation %u", collationOid);
221 
222  systable_endscan(scandesc);
223 
225 }
#define Anum_pg_collation_collversion
Definition: pg_collation.h:66
#define NamespaceRelationId
Definition: pg_namespace.h:34
#define NameGetDatum(X)
Definition: postgres.h:601
#define CollationOidIndexId
Definition: indexing.h:121
void systable_endscan(SysScanDesc sysscan)
Definition: genam.c:499
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)
Definition: pg_collation.c:42
#define InvokeObjectPostCreateHook(classId, objectId, subId)
Definition: objectaccess.h:145
#define RelationGetDescr(relation)
Definition: rel.h:428
#define ObjectIdAttributeNumber
Definition: sysattr.h:22
#define PointerGetDatum(X)
Definition: postgres.h:562
#define Natts_pg_collation
Definition: pg_collation.h:58
#define Anum_pg_collation_collcollate
Definition: pg_collation.h:64
int errcode(int sqlerrcode)
Definition: elog.c:575
#define Anum_pg_collation_collctype
Definition: pg_collation.h:65
void CatalogTupleDelete(Relation heapRel, ItemPointer tid)
Definition: indexing.c:255
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:692
#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:1372
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:538
void RemoveCollationById(Oid collationOid)
Definition: pg_collation.c:198
#define Anum_pg_collation_collnamespace
Definition: pg_collation.h:60
SysScanDesc systable_beginscan(Relation heapRelation, Oid indexId, bool indexOK, Snapshot snapshot, int nkeys, ScanKey key)
Definition: genam.c:328
signed int int32
Definition: c.h:256
HeapTuple systable_getnext(SysScanDesc sysscan)
Definition: genam.c:416
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#define ERROR
Definition: elog.h:43
#define SearchSysCacheExists3(cacheId, key1, key2, key3)
Definition: syscache.h:178
Oid CatalogTupleInsert(Relation heapRel, HeapTuple tup)
Definition: indexing.c:162
ItemPointerData t_self
Definition: htup.h:65
Definition: c.h:493
#define Anum_pg_collation_collprovider
Definition: pg_collation.h:62
#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:677
#define Anum_pg_collation_collname
Definition: pg_collation.h:59
#define Anum_pg_collation_collowner
Definition: pg_collation.h:61
uintptr_t Datum
Definition: postgres.h:372
int GetDatabaseEncoding(void)
Definition: mbutils.c:1015
Relation heap_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1284
#define CollationRelationId
Definition: pg_collation.h:30
#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:77
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
#define ShareRowExclusiveLock
Definition: lockdefs.h:42
void recordDependencyOnCurrentExtension(const ObjectAddress *object, bool isReplace)
Definition: pg_depend.c:139
#define CharGetDatum(X)
Definition: postgres.h:422
static Datum values[MAXATTR]
Definition: bootstrap.c:163
#define Int32GetDatum(X)
Definition: postgres.h:485
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define Anum_pg_collation_collencoding
Definition: pg_collation.h:63
void ScanKeyInit(ScanKey entry, AttrNumber attributeNumber, StrategyNumber strategy, RegProcedure procedure, Datum argument)
Definition: scankey.c:76
#define CStringGetTextDatum(s)
Definition: builtins.h:91
#define elog
Definition: elog.h:219
#define HeapTupleGetOid(tuple)
Definition: htup_details.h:695
#define ERRCODE_DUPLICATE_OBJECT
Definition: streamutil.c:31
#define BTEqualStrategyNumber
Definition: stratnum.h:31