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  AssertArg(collname);
68  AssertArg(collnamespace);
69  AssertArg(collowner);
70  AssertArg((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  PointerGetDatum(collname),
81  Int32GetDatum(collencoding),
82  ObjectIdGetDatum(collnamespace)))
83  {
84  if (quiet)
85  return InvalidOid;
86  else if (if_not_exists)
87  {
90  collencoding == -1
91  ? errmsg("collation \"%s\" already exists, skipping",
92  collname)
93  : errmsg("collation \"%s\" for encoding \"%s\" already exists, skipping",
94  collname, pg_encoding_to_char(collencoding))));
95  return InvalidOid;
96  }
97  else
98  ereport(ERROR,
100  collencoding == -1
101  ? errmsg("collation \"%s\" already exists",
102  collname)
103  : errmsg("collation \"%s\" for encoding \"%s\" already exists",
104  collname, pg_encoding_to_char(collencoding))));
105  }
106 
107  /* open pg_collation; see below about the lock level */
108  rel = table_open(CollationRelationId, ShareRowExclusiveLock);
109 
110  /*
111  * Also forbid a specific-encoding collation shadowing an any-encoding
112  * collation, or an any-encoding collation being shadowed (see
113  * get_collation_name()). This test is not backed up by the unique index,
114  * so we take a ShareRowExclusiveLock earlier, to protect against
115  * concurrent changes fooling this check.
116  */
117  if ((collencoding == -1 &&
119  PointerGetDatum(collname),
121  ObjectIdGetDatum(collnamespace))) ||
122  (collencoding != -1 &&
124  PointerGetDatum(collname),
125  Int32GetDatum(-1),
126  ObjectIdGetDatum(collnamespace))))
127  {
128  if (quiet)
129  {
130  table_close(rel, NoLock);
131  return InvalidOid;
132  }
133  else if (if_not_exists)
134  {
135  table_close(rel, NoLock);
136  ereport(NOTICE,
138  errmsg("collation \"%s\" already exists, skipping",
139  collname)));
140  return InvalidOid;
141  }
142  else
143  ereport(ERROR,
145  errmsg("collation \"%s\" already exists",
146  collname)));
147  }
148 
149  tupDesc = RelationGetDescr(rel);
150 
151  /* form a tuple */
152  memset(nulls, 0, sizeof(nulls));
153 
154  namestrcpy(&name_name, collname);
155  oid = GetNewOidWithIndex(rel, CollationOidIndexId,
156  Anum_pg_collation_oid);
157  values[Anum_pg_collation_oid - 1] = ObjectIdGetDatum(oid);
158  values[Anum_pg_collation_collname - 1] = NameGetDatum(&name_name);
159  values[Anum_pg_collation_collnamespace - 1] = ObjectIdGetDatum(collnamespace);
160  values[Anum_pg_collation_collowner - 1] = ObjectIdGetDatum(collowner);
161  values[Anum_pg_collation_collprovider - 1] = CharGetDatum(collprovider);
162  values[Anum_pg_collation_collisdeterministic - 1] = BoolGetDatum(collisdeterministic);
163  values[Anum_pg_collation_collencoding - 1] = Int32GetDatum(collencoding);
164  if (collcollate)
165  values[Anum_pg_collation_collcollate - 1] = CStringGetTextDatum(collcollate);
166  else
167  nulls[Anum_pg_collation_collcollate - 1] = true;
168  if (collctype)
169  values[Anum_pg_collation_collctype - 1] = CStringGetTextDatum(collctype);
170  else
171  nulls[Anum_pg_collation_collctype - 1] = true;
172  if (colliculocale)
173  values[Anum_pg_collation_colliculocale - 1] = CStringGetTextDatum(colliculocale);
174  else
175  nulls[Anum_pg_collation_colliculocale - 1] = true;
176  if (collversion)
177  values[Anum_pg_collation_collversion - 1] = CStringGetTextDatum(collversion);
178  else
179  nulls[Anum_pg_collation_collversion - 1] = true;
180 
181  tup = heap_form_tuple(tupDesc, values, nulls);
182 
183  /* insert a new tuple */
184  CatalogTupleInsert(rel, tup);
185  Assert(OidIsValid(oid));
186 
187  /* set up dependencies for the new collation */
188  myself.classId = CollationRelationId;
189  myself.objectId = oid;
190  myself.objectSubId = 0;
191 
192  /* create dependency on namespace */
193  referenced.classId = NamespaceRelationId;
194  referenced.objectId = collnamespace;
195  referenced.objectSubId = 0;
196  recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL);
197 
198  /* create dependency on owner */
199  recordDependencyOnOwner(CollationRelationId, oid, collowner);
200 
201  /* dependency on extension */
202  recordDependencyOnCurrentExtension(&myself, false);
203 
204  /* Post creation hook for new collation */
205  InvokeObjectPostCreateHook(CollationRelationId, oid, 0);
206 
207  heap_freetuple(tup);
208  table_close(rel, NoLock);
209 
210  return oid;
211 }
static Datum values[MAXATTR]
Definition: bootstrap.c:156
#define CStringGetTextDatum(s)
Definition: builtins.h:85
#define OidIsValid(objectId)
Definition: c.h:721
#define AssertArg(condition)
Definition: c.h:817
Oid GetNewOidWithIndex(Relation relation, Oid indexId, AttrNumber oidcolumn)
Definition: catalog.c:391
@ DEPENDENCY_NORMAL
Definition: dependency.h:33
int errcode(int sqlerrcode)
Definition: elog.c:693
int errmsg(const char *fmt,...)
Definition: elog.c:904
#define ERROR
Definition: elog.h:33
#define NOTICE
Definition: elog.h:29
#define ereport(elevel,...)
Definition: elog.h:143
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:171
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:191
void recordDependencyOnOwner(Oid classId, Oid objectId, Oid owner)
Definition: pg_shdepend.c:164
uintptr_t Datum
Definition: postgres.h:411
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
#define BoolGetDatum(X)
Definition: postgres.h:446
#define Int32GetDatum(X)
Definition: postgres.h:523
#define NameGetDatum(X)
Definition: postgres.h:639
#define CharGetDatum(X)
Definition: postgres.h:460
#define PointerGetDatum(X)
Definition: postgres.h:600
#define InvalidOid
Definition: postgres_ext.h:36
unsigned int Oid
Definition: postgres_ext.h:31
#define RelationGetDescr(relation)
Definition: rel.h:514
#define ERRCODE_DUPLICATE_OBJECT
Definition: streamutil.c:32
Definition: c.h:687
@ COLLNAMEENCNSP
Definition: syscache.h:49
#define SearchSysCacheExists3(cacheId, key1, key2, key3)
Definition: syscache.h:192
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39

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

Referenced by DefineCollation(), and pg_import_system_collations().