PostgreSQL Source Code  git master
pg_collation.c File Reference
#include "postgres.h"
#include "access/genam.h"
#include "access/htup_details.h"
#include "access/sysattr.h"
#include "access/table.h"
#include "catalog/catalog.h"
#include "catalog/dependency.h"
#include "catalog/indexing.h"
#include "catalog/objectaccess.h"
#include "catalog/pg_collation.h"
#include "catalog/pg_namespace.h"
#include "mb/pg_wchar.h"
#include "utils/builtins.h"
#include "utils/fmgroids.h"
#include "utils/pg_locale.h"
#include "utils/rel.h"
#include "utils/syscache.h"
Include dependency graph for pg_collation.c:

Go to the source code of this file.

Functions

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 *collversion, bool if_not_exists, bool quiet)
 

Function Documentation

◆ CollationCreate()

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 *  collversion,
bool  if_not_exists,
bool  quiet 
)

Definition at line 46 of file pg_collation.c.

56 {
57  Relation rel;
58  TupleDesc tupDesc;
59  HeapTuple tup;
60  Datum values[Natts_pg_collation];
61  bool nulls[Natts_pg_collation];
62  NameData name_name;
63  Oid oid;
64  ObjectAddress myself,
65  referenced;
66 
67  Assert(collname);
68  Assert(collnamespace);
69  Assert(collowner);
70  Assert((collcollate && collctype) || colliculocale);
71 
72  /*
73  * Make sure there is no existing collation of same name & encoding.
74  *
75  * This would be caught by the unique index anyway; we're just giving a
76  * friendlier error message. The unique index provides a backstop against
77  * race conditions.
78  */
80  Anum_pg_collation_oid,
81  PointerGetDatum(collname),
82  Int32GetDatum(collencoding),
83  ObjectIdGetDatum(collnamespace));
84  if (OidIsValid(oid))
85  {
86  if (quiet)
87  return InvalidOid;
88  else if (if_not_exists)
89  {
90  /*
91  * If we are in an extension script, insist that the pre-existing
92  * object be a member of the extension, to avoid security risks.
93  */
94  ObjectAddressSet(myself, CollationRelationId, oid);
96 
97  /* OK to skip */
100  collencoding == -1
101  ? errmsg("collation \"%s\" already exists, skipping",
102  collname)
103  : errmsg("collation \"%s\" for encoding \"%s\" already exists, skipping",
104  collname, pg_encoding_to_char(collencoding))));
105  return InvalidOid;
106  }
107  else
108  ereport(ERROR,
110  collencoding == -1
111  ? errmsg("collation \"%s\" already exists",
112  collname)
113  : errmsg("collation \"%s\" for encoding \"%s\" already exists",
114  collname, pg_encoding_to_char(collencoding))));
115  }
116 
117  /* open pg_collation; see below about the lock level */
118  rel = table_open(CollationRelationId, ShareRowExclusiveLock);
119 
120  /*
121  * Also forbid a specific-encoding collation shadowing an any-encoding
122  * collation, or an any-encoding collation being shadowed (see
123  * get_collation_name()). This test is not backed up by the unique index,
124  * so we take a ShareRowExclusiveLock earlier, to protect against
125  * concurrent changes fooling this check.
126  */
127  if (collencoding == -1)
129  Anum_pg_collation_oid,
130  PointerGetDatum(collname),
132  ObjectIdGetDatum(collnamespace));
133  else
135  Anum_pg_collation_oid,
136  PointerGetDatum(collname),
137  Int32GetDatum(-1),
138  ObjectIdGetDatum(collnamespace));
139  if (OidIsValid(oid))
140  {
141  if (quiet)
142  {
143  table_close(rel, NoLock);
144  return InvalidOid;
145  }
146  else if (if_not_exists)
147  {
148  /*
149  * If we are in an extension script, insist that the pre-existing
150  * object be a member of the extension, to avoid security risks.
151  */
152  ObjectAddressSet(myself, CollationRelationId, oid);
154 
155  /* OK to skip */
156  table_close(rel, NoLock);
157  ereport(NOTICE,
159  errmsg("collation \"%s\" already exists, skipping",
160  collname)));
161  return InvalidOid;
162  }
163  else
164  ereport(ERROR,
166  errmsg("collation \"%s\" already exists",
167  collname)));
168  }
169 
170  tupDesc = RelationGetDescr(rel);
171 
172  /* form a tuple */
173  memset(nulls, 0, sizeof(nulls));
174 
175  namestrcpy(&name_name, collname);
176  oid = GetNewOidWithIndex(rel, CollationOidIndexId,
177  Anum_pg_collation_oid);
178  values[Anum_pg_collation_oid - 1] = ObjectIdGetDatum(oid);
179  values[Anum_pg_collation_collname - 1] = NameGetDatum(&name_name);
180  values[Anum_pg_collation_collnamespace - 1] = ObjectIdGetDatum(collnamespace);
181  values[Anum_pg_collation_collowner - 1] = ObjectIdGetDatum(collowner);
182  values[Anum_pg_collation_collprovider - 1] = CharGetDatum(collprovider);
183  values[Anum_pg_collation_collisdeterministic - 1] = BoolGetDatum(collisdeterministic);
184  values[Anum_pg_collation_collencoding - 1] = Int32GetDatum(collencoding);
185  if (collcollate)
186  values[Anum_pg_collation_collcollate - 1] = CStringGetTextDatum(collcollate);
187  else
188  nulls[Anum_pg_collation_collcollate - 1] = true;
189  if (collctype)
190  values[Anum_pg_collation_collctype - 1] = CStringGetTextDatum(collctype);
191  else
192  nulls[Anum_pg_collation_collctype - 1] = true;
193  if (colliculocale)
194  values[Anum_pg_collation_colliculocale - 1] = CStringGetTextDatum(colliculocale);
195  else
196  nulls[Anum_pg_collation_colliculocale - 1] = true;
197  if (collversion)
198  values[Anum_pg_collation_collversion - 1] = CStringGetTextDatum(collversion);
199  else
200  nulls[Anum_pg_collation_collversion - 1] = true;
201 
202  tup = heap_form_tuple(tupDesc, values, nulls);
203 
204  /* insert a new tuple */
205  CatalogTupleInsert(rel, tup);
206  Assert(OidIsValid(oid));
207 
208  /* set up dependencies for the new collation */
209  myself.classId = CollationRelationId;
210  myself.objectId = oid;
211  myself.objectSubId = 0;
212 
213  /* create dependency on namespace */
214  referenced.classId = NamespaceRelationId;
215  referenced.objectId = collnamespace;
216  referenced.objectSubId = 0;
217  recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL);
218 
219  /* create dependency on owner */
220  recordDependencyOnOwner(CollationRelationId, oid, collowner);
221 
222  /* dependency on extension */
223  recordDependencyOnCurrentExtension(&myself, false);
224 
225  /* Post creation hook for new collation */
226  InvokeObjectPostCreateHook(CollationRelationId, oid, 0);
227 
228  heap_freetuple(tup);
229  table_close(rel, NoLock);
230 
231  return oid;
232 }
static Datum values[MAXATTR]
Definition: bootstrap.c:156
#define CStringGetTextDatum(s)
Definition: builtins.h:85
#define OidIsValid(objectId)
Definition: c.h:711
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:695
int errmsg(const char *fmt,...)
Definition: elog.c:906
#define ERROR
Definition: elog.h:35
#define NOTICE
Definition: elog.h:31
#define ereport(elevel,...)
Definition: elog.h:145
const char * pg_encoding_to_char(int encoding)
Definition: encnames.c:588
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: heaptuple.c:1020
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1338
void CatalogTupleInsert(Relation heapRel, HeapTuple tup)
Definition: indexing.c:221
Assert(fmt[strlen(fmt) - 1] !='\n')
#define NoLock
Definition: lockdefs.h:34
#define ShareRowExclusiveLock
Definition: lockdefs.h:41
int GetDatabaseEncoding(void)
Definition: mbutils.c:1210
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
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
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:670
uintptr_t Datum
Definition: postgres.h:412
static Datum BoolGetDatum(bool X)
Definition: postgres.h:450
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:600
static Datum NameGetDatum(const NameData *X)
Definition: postgres.h:721
static Datum Int32GetDatum(int32 X)
Definition: postgres.h:560
static Datum CharGetDatum(char X)
Definition: postgres.h:470
#define InvalidOid
Definition: postgres_ext.h:36
unsigned int Oid
Definition: postgres_ext.h:31
#define RelationGetDescr(relation)
Definition: rel.h:527
#define ERRCODE_DUPLICATE_OBJECT
Definition: streamutil.c:32
Definition: c.h:677
@ COLLNAMEENCNSP
Definition: syscache.h:49
#define GetSysCacheOid3(cacheId, oidcol, key1, key2, key3)
Definition: syscache.h:201
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:126
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:40

References Assert(), BoolGetDatum(), CatalogTupleInsert(), CharGetDatum(), checkMembershipInCurrentExtension(), ObjectAddress::classId, COLLNAMEENCNSP, CStringGetTextDatum, DEPENDENCY_NORMAL, ereport, errcode(), ERRCODE_DUPLICATE_OBJECT, errmsg(), ERROR, GetDatabaseEncoding(), GetNewOidWithIndex(), GetSysCacheOid3, heap_form_tuple(), heap_freetuple(), Int32GetDatum(), InvalidOid, InvokeObjectPostCreateHook, NameGetDatum(), namestrcpy(), NoLock, NOTICE, ObjectAddressSet, ObjectAddress::objectId, ObjectIdGetDatum(), ObjectAddress::objectSubId, OidIsValid, pg_encoding_to_char(), PointerGetDatum(), recordDependencyOn(), recordDependencyOnCurrentExtension(), recordDependencyOnOwner(), RelationGetDescr, ShareRowExclusiveLock, table_close(), table_open(), and values.

Referenced by DefineCollation(), and pg_import_system_collations().