PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
pg_collation_fn.h File Reference
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

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)
 
void RemoveCollationById (Oid collationOid)
 

Function Documentation

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 at line 47 of file pg_collation.c.

References Anum_pg_collation_collcollate, Anum_pg_collation_collctype, Anum_pg_collation_collencoding, Anum_pg_collation_collname, Anum_pg_collation_collnamespace, Anum_pg_collation_collowner, Anum_pg_collation_collprovider, Anum_pg_collation_collversion, Assert, AssertArg, CatalogTupleInsert(), CharGetDatum, ObjectAddress::classId, CollationRelationId, COLLNAMEENCNSP, CStringGetTextDatum, DEPENDENCY_NORMAL, ereport, errcode(), ERRCODE_DUPLICATE_OBJECT, errmsg(), ERROR, GetDatabaseEncoding(), heap_close, heap_form_tuple(), heap_freetuple(), heap_open(), HeapTupleGetOid, Int32GetDatum, InvalidOid, InvokeObjectPostCreateHook, NameGetDatum, NamespaceRelationId, namestrcpy(), Natts_pg_collation, NoLock, NOTICE, ObjectAddress::objectId, ObjectIdGetDatum, ObjectAddress::objectSubId, OidIsValid, pg_encoding_to_char(), PointerGetDatum, recordDependencyOn(), recordDependencyOnCurrentExtension(), recordDependencyOnOwner(), RelationGetDescr, SearchSysCacheExists3, ShareRowExclusiveLock, and values.

Referenced by DefineCollation(), and pg_import_system_collations().

55 {
56  Relation rel;
57  TupleDesc tupDesc;
58  HeapTuple tup;
60  bool nulls[Natts_pg_collation];
61  NameData name_name,
62  name_collate,
63  name_ctype;
64  Oid oid;
65  ObjectAddress myself,
66  referenced;
67 
68  AssertArg(collname);
69  AssertArg(collnamespace);
70  AssertArg(collowner);
71  AssertArg(collcollate);
72  AssertArg(collctype);
73 
74  /*
75  * Make sure there is no existing collation of same name & encoding.
76  *
77  * This would be caught by the unique index anyway; we're just giving a
78  * friendlier error message. The unique index provides a backstop against
79  * race conditions.
80  */
82  PointerGetDatum(collname),
83  Int32GetDatum(collencoding),
84  ObjectIdGetDatum(collnamespace)))
85  {
86  if (quiet)
87  return InvalidOid;
88  else if (if_not_exists)
89  {
92  collencoding == -1
93  ? errmsg("collation \"%s\" already exists, skipping",
94  collname)
95  : errmsg("collation \"%s\" for encoding \"%s\" already exists, skipping",
96  collname, pg_encoding_to_char(collencoding))));
97  return InvalidOid;
98  }
99  else
100  ereport(ERROR,
102  collencoding == -1
103  ? errmsg("collation \"%s\" already exists",
104  collname)
105  : errmsg("collation \"%s\" for encoding \"%s\" already exists",
106  collname, pg_encoding_to_char(collencoding))));
107  }
108 
109  /* open pg_collation; see below about the lock level */
111 
112  /*
113  * Also forbid a specific-encoding collation shadowing an any-encoding
114  * collation, or an any-encoding collation being shadowed (see
115  * get_collation_name()). This test is not backed up by the unique index,
116  * so we take a ShareRowExclusiveLock earlier, to protect against
117  * concurrent changes fooling this check.
118  */
119  if ((collencoding == -1 &&
121  PointerGetDatum(collname),
123  ObjectIdGetDatum(collnamespace))) ||
124  (collencoding != -1 &&
126  PointerGetDatum(collname),
127  Int32GetDatum(-1),
128  ObjectIdGetDatum(collnamespace))))
129  {
130  if (quiet)
131  {
132  heap_close(rel, NoLock);
133  return InvalidOid;
134  }
135  else if (if_not_exists)
136  {
137  heap_close(rel, NoLock);
138  ereport(NOTICE,
140  errmsg("collation \"%s\" already exists, skipping",
141  collname)));
142  return InvalidOid;
143  }
144  else
145  ereport(ERROR,
147  errmsg("collation \"%s\" already exists",
148  collname)));
149  }
150 
151  tupDesc = RelationGetDescr(rel);
152 
153  /* form a tuple */
154  memset(nulls, 0, sizeof(nulls));
155 
156  namestrcpy(&name_name, collname);
157  values[Anum_pg_collation_collname - 1] = NameGetDatum(&name_name);
158  values[Anum_pg_collation_collnamespace - 1] = ObjectIdGetDatum(collnamespace);
159  values[Anum_pg_collation_collowner - 1] = ObjectIdGetDatum(collowner);
160  values[Anum_pg_collation_collprovider - 1] = CharGetDatum(collprovider);
161  values[Anum_pg_collation_collencoding - 1] = Int32GetDatum(collencoding);
162  namestrcpy(&name_collate, collcollate);
163  values[Anum_pg_collation_collcollate - 1] = NameGetDatum(&name_collate);
164  namestrcpy(&name_ctype, collctype);
165  values[Anum_pg_collation_collctype - 1] = NameGetDatum(&name_ctype);
166  if (collversion)
167  values[Anum_pg_collation_collversion - 1] = CStringGetTextDatum(collversion);
168  else
169  nulls[Anum_pg_collation_collversion - 1] = true;
170 
171  tup = heap_form_tuple(tupDesc, values, nulls);
172 
173  /* insert a new tuple */
174  oid = CatalogTupleInsert(rel, tup);
175  Assert(OidIsValid(oid));
176 
177  /* set up dependencies for the new collation */
178  myself.classId = CollationRelationId;
179  myself.objectId = oid;
180  myself.objectSubId = 0;
181 
182  /* create dependency on namespace */
183  referenced.classId = NamespaceRelationId;
184  referenced.objectId = collnamespace;
185  referenced.objectSubId = 0;
186  recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL);
187 
188  /* create dependency on owner */
190  collowner);
191 
192  /* dependency on extension */
193  recordDependencyOnCurrentExtension(&myself, false);
194 
195  /* Post creation hook for new collation */
197 
198  heap_freetuple(tup);
199  heap_close(rel, NoLock);
200 
201  return oid;
202 }
#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 InvokeObjectPostCreateHook(classId, objectId, subId)
Definition: objectaccess.h:145
#define RelationGetDescr(relation)
Definition: rel.h:428
#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 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
#define Anum_pg_collation_collnamespace
Definition: pg_collation.h:60
#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
Definition: c.h:493
#define Anum_pg_collation_collprovider
Definition: pg_collation.h:62
#define NoLock
Definition: lockdefs.h:34
#define ereport(elevel, rest)
Definition: elog.h:122
#define AssertArg(condition)
Definition: c.h:678
#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:1290
#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 Assert(condition)
Definition: c.h:676
#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
#define CStringGetTextDatum(s)
Definition: builtins.h:91
#define HeapTupleGetOid(tuple)
Definition: htup_details.h:695
#define ERRCODE_DUPLICATE_OBJECT
Definition: streamutil.c:32
void RemoveCollationById ( Oid  collationOid)

Definition at line 211 of file pg_collation.c.

References BTEqualStrategyNumber, CatalogTupleDelete(), CollationOidIndexId, CollationRelationId, elog, ERROR, heap_close, heap_open(), HeapTupleIsValid, NULL, ObjectIdAttributeNumber, ObjectIdGetDatum, RowExclusiveLock, ScanKeyInit(), systable_beginscan(), systable_endscan(), systable_getnext(), and HeapTupleData::t_self.

Referenced by doDeletion().

212 {
213  Relation rel;
214  ScanKeyData scanKeyData;
215  SysScanDesc scandesc;
216  HeapTuple tuple;
217 
219 
220  ScanKeyInit(&scanKeyData,
222  BTEqualStrategyNumber, F_OIDEQ,
223  ObjectIdGetDatum(collationOid));
224 
225  scandesc = systable_beginscan(rel, CollationOidIndexId, true,
226  NULL, 1, &scanKeyData);
227 
228  tuple = systable_getnext(scandesc);
229 
230  if (HeapTupleIsValid(tuple))
231  CatalogTupleDelete(rel, &tuple->t_self);
232  else
233  elog(ERROR, "could not find tuple for collation %u", collationOid);
234 
235  systable_endscan(scandesc);
236 
238 }
#define CollationOidIndexId
Definition: indexing.h:121
void systable_endscan(SysScanDesc sysscan)
Definition: genam.c:499
#define ObjectIdAttributeNumber
Definition: sysattr.h:22
void CatalogTupleDelete(Relation heapRel, ItemPointer tid)
Definition: indexing.c:255
#define heap_close(r, l)
Definition: heapam.h:97
SysScanDesc systable_beginscan(Relation heapRelation, Oid indexId, bool indexOK, Snapshot snapshot, int nkeys, ScanKey key)
Definition: genam.c:328
HeapTuple systable_getnext(SysScanDesc sysscan)
Definition: genam.c:416
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#define ERROR
Definition: elog.h:43
ItemPointerData t_self
Definition: htup.h:65
#define RowExclusiveLock
Definition: lockdefs.h:38
Relation heap_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1290
#define CollationRelationId
Definition: pg_collation.h:30
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
#define NULL
Definition: c.h:229
void ScanKeyInit(ScanKey entry, AttrNumber attributeNumber, StrategyNumber strategy, RegProcedure procedure, Datum argument)
Definition: scankey.c:76
#define elog
Definition: elog.h:219
#define BTEqualStrategyNumber
Definition: stratnum.h:31