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

Definition at line 46 of file pg_collation.c.

References Assert, AssertArg, BoolGetDatum, CatalogTupleInsert(), CharGetDatum, ObjectAddress::classId, CollationOidIndexId, COLLNAMEENCNSP, 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().

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 = table_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  table_close(rel, NoLock);
132  return InvalidOid;
133  }
134  else if (if_not_exists)
135  {
136  table_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);
157  Anum_pg_collation_oid);
158  values[Anum_pg_collation_oid - 1] = ObjectIdGetDatum(oid);
159  values[Anum_pg_collation_collname - 1] = NameGetDatum(&name_name);
160  values[Anum_pg_collation_collnamespace - 1] = ObjectIdGetDatum(collnamespace);
161  values[Anum_pg_collation_collowner - 1] = ObjectIdGetDatum(collowner);
162  values[Anum_pg_collation_collprovider - 1] = CharGetDatum(collprovider);
163  values[Anum_pg_collation_collisdeterministic - 1] = BoolGetDatum(collisdeterministic);
164  values[Anum_pg_collation_collencoding - 1] = Int32GetDatum(collencoding);
165  namestrcpy(&name_collate, collcollate);
166  values[Anum_pg_collation_collcollate - 1] = NameGetDatum(&name_collate);
167  namestrcpy(&name_ctype, collctype);
168  values[Anum_pg_collation_collctype - 1] = NameGetDatum(&name_ctype);
169 
170  tup = heap_form_tuple(tupDesc, values, nulls);
171 
172  /* insert a new tuple */
173  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, oid, collowner);
189 
190  /* dependency on extension */
191  recordDependencyOnCurrentExtension(&myself, false);
192 
193  /* Post creation hook for new collation */
194  InvokeObjectPostCreateHook(CollationRelationId, oid, 0);
195 
196  heap_freetuple(tup);
197  table_close(rel, NoLock);
198 
199  return oid;
200 }
Oid GetNewOidWithIndex(Relation relation, Oid indexId, AttrNumber oidcolumn)
Definition: catalog.c:315
#define NameGetDatum(X)
Definition: postgres.h:595
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167
#define InvokeObjectPostCreateHook(classId, objectId, subId)
Definition: objectaccess.h:151
#define RelationGetDescr(relation)
Definition: rel.h:483
void namestrcpy(Name name, const char *str)
Definition: name.c:233
#define PointerGetDatum(X)
Definition: postgres.h:556
int errcode(int sqlerrcode)
Definition: elog.c:691
void recordDependencyOn(const ObjectAddress *depender, const ObjectAddress *referenced, DependencyType behavior)
Definition: pg_depend.c:46
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: heaptuple.c:1020
void recordDependencyOnOwner(Oid classId, Oid objectId, Oid owner)
Definition: pg_shdepend.c:164
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1338
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:706
#define CollationOidIndexId
Definition: pg_collation.h:52
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:43
#define SearchSysCacheExists3(cacheId, key1, key2, key3)
Definition: syscache.h:187
Definition: c.h:671
#define NoLock
Definition: lockdefs.h:34
#define AssertArg(condition)
Definition: c.h:802
uintptr_t Datum
Definition: postgres.h:367
int GetDatabaseEncoding(void)
Definition: mbutils.c:1151
#define BoolGetDatum(X)
Definition: postgres.h:402
#define InvalidOid
Definition: postgres_ext.h:36
#define ereport(elevel,...)
Definition: elog.h:155
#define NOTICE
Definition: elog.h:37
const char * pg_encoding_to_char(int encoding)
Definition: encnames.c:588
#define Assert(condition)
Definition: c.h:800
#define ShareRowExclusiveLock
Definition: lockdefs.h:42
void recordDependencyOnCurrentExtension(const ObjectAddress *object, bool isReplace)
Definition: pg_depend.c:220
#define CharGetDatum(X)
Definition: postgres.h:416
static Datum values[MAXATTR]
Definition: bootstrap.c:165
#define Int32GetDatum(X)
Definition: postgres.h:479
int errmsg(const char *fmt,...)
Definition: elog.c:902
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
#define ERRCODE_DUPLICATE_OBJECT
Definition: streamutil.c:32
void CatalogTupleInsert(Relation heapRel, HeapTuple tup)
Definition: indexing.c:221