PostgreSQL Source Code  git master
pg_collation.h File Reference
#include "catalog/genbki.h"
#include "catalog/pg_collation_d.h"
Include dependency graph for pg_collation.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Typedefs

typedef FormData_pg_collationForm_pg_collation
 

Functions

 CATALOG (pg_collation, 3456, CollationRelationId)
 
Oid CollationCreate (const char *collname, Oid collnamespace, Oid collowner, char collprovider, bool collisdeterministic, int32 collencoding, const char *collcollate, const char *collctype, const char *collversion, bool if_not_exists, bool quiet)
 
void RemoveCollationById (Oid collationOid)
 

Variables

 FormData_pg_collation
 

Typedef Documentation

◆ Form_pg_collation

Definition at line 51 of file pg_collation.h.

Function Documentation

◆ CATALOG()

CATALOG ( pg_collation  ,
3456  ,
CollationRelationId   
)

Definition at line 29 of file pg_collation.h.

References BKI_DEFAULT.

30 {
31  Oid oid; /* oid */
32  NameData collname; /* collation name */
33  Oid collnamespace; /* OID of namespace containing collation */
34  Oid collowner; /* owner of collation */
35  char collprovider; /* see constants below */
36  bool collisdeterministic BKI_DEFAULT(t);
37  int32 collencoding; /* encoding for this collation; -1 = "all" */
38  NameData collcollate; /* LC_COLLATE setting */
39  NameData collctype; /* LC_CTYPE setting */
40 #ifdef CATALOG_VARLEN /* variable-length fields start here */
41  text collversion; /* provider-dependent version of collation
42  * data */
43 #endif
unsigned int Oid
Definition: postgres_ext.h:31
#define BKI_DEFAULT(value)
Definition: genbki.h:35
signed int int32
Definition: c.h:346
Definition: c.h:603
FormData_pg_collation
Definition: pg_collation.h:44
Definition: c.h:549

◆ CollationCreate()

Oid CollationCreate ( const char *  collname,
Oid  collnamespace,
Oid  collowner,
char  collprovider,
bool  collisdeterministic,
int32  collencoding,
const char *  collcollate,
const char *  collctype,
const char *  collversion,
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, 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().

55 {
56  Relation rel;
57  TupleDesc tupDesc;
58  HeapTuple tup;
59  Datum values[Natts_pg_collation];
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 */
110  rel = table_open(CollationRelationId, ShareRowExclusiveLock);
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  table_close(rel, NoLock);
133  return InvalidOid;
134  }
135  else if (if_not_exists)
136  {
137  table_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);
158  Anum_pg_collation_oid);
159  values[Anum_pg_collation_oid - 1] = ObjectIdGetDatum(oid);
160  values[Anum_pg_collation_collname - 1] = NameGetDatum(&name_name);
161  values[Anum_pg_collation_collnamespace - 1] = ObjectIdGetDatum(collnamespace);
162  values[Anum_pg_collation_collowner - 1] = ObjectIdGetDatum(collowner);
163  values[Anum_pg_collation_collprovider - 1] = CharGetDatum(collprovider);
164  values[Anum_pg_collation_collisdeterministic - 1] = BoolGetDatum(collisdeterministic);
165  values[Anum_pg_collation_collencoding - 1] = Int32GetDatum(collencoding);
166  namestrcpy(&name_collate, collcollate);
167  values[Anum_pg_collation_collcollate - 1] = NameGetDatum(&name_collate);
168  namestrcpy(&name_ctype, collctype);
169  values[Anum_pg_collation_collctype - 1] = NameGetDatum(&name_ctype);
170  if (collversion)
171  values[Anum_pg_collation_collversion - 1] = CStringGetTextDatum(collversion);
172  else
173  nulls[Anum_pg_collation_collversion - 1] = true;
174 
175  tup = heap_form_tuple(tupDesc, values, nulls);
176 
177  /* insert a new tuple */
178  CatalogTupleInsert(rel, tup);
179  Assert(OidIsValid(oid));
180 
181  /* set up dependencies for the new collation */
182  myself.classId = CollationRelationId;
183  myself.objectId = oid;
184  myself.objectSubId = 0;
185 
186  /* create dependency on namespace */
187  referenced.classId = NamespaceRelationId;
188  referenced.objectId = collnamespace;
189  referenced.objectSubId = 0;
190  recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL);
191 
192  /* create dependency on owner */
193  recordDependencyOnOwner(CollationRelationId, oid, collowner);
194 
195  /* dependency on extension */
196  recordDependencyOnCurrentExtension(&myself, false);
197 
198  /* Post creation hook for new collation */
199  InvokeObjectPostCreateHook(CollationRelationId, oid, 0);
200 
201  heap_freetuple(tup);
202  table_close(rel, NoLock);
203 
204  return oid;
205 }
Oid GetNewOidWithIndex(Relation relation, Oid indexId, AttrNumber oidcolumn)
Definition: catalog.c:323
#define NameGetDatum(X)
Definition: postgres.h:595
#define CollationOidIndexId
Definition: indexing.h:123
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:133
#define InvokeObjectPostCreateHook(classId, objectId, subId)
Definition: objectaccess.h:145
#define RelationGetDescr(relation)
Definition: rel.h:445
#define PointerGetDatum(X)
Definition: postgres.h:556
int errcode(int sqlerrcode)
Definition: elog.c:570
void recordDependencyOn(const ObjectAddress *depender, const ObjectAddress *referenced, DependencyType behavior)
Definition: pg_depend.c:43
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:165
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1338
unsigned int Oid
Definition: postgres_ext.h:31
int namestrcpy(Name name, const char *str)
Definition: name.c:250
#define OidIsValid(objectId)
Definition: c.h:638
#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:603
#define NoLock
Definition: lockdefs.h:34
#define ereport(elevel, rest)
Definition: elog.h:141
#define AssertArg(condition)
Definition: c.h:734
uintptr_t Datum
Definition: postgres.h:367
int GetDatabaseEncoding(void)
Definition: mbutils.c:1046
#define BoolGetDatum(X)
Definition: postgres.h:402
#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:732
#define ShareRowExclusiveLock
Definition: lockdefs.h:42
void recordDependencyOnCurrentExtension(const ObjectAddress *object, bool isReplace)
Definition: pg_depend.c:138
#define CharGetDatum(X)
Definition: postgres.h:416
static Datum values[MAXATTR]
Definition: bootstrap.c:167
#define Int32GetDatum(X)
Definition: postgres.h:479
int errmsg(const char *fmt,...)
Definition: elog.c:784
#define CStringGetTextDatum(s)
Definition: builtins.h:83
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
#define ERRCODE_DUPLICATE_OBJECT
Definition: streamutil.c:33
void CatalogTupleInsert(Relation heapRel, HeapTuple tup)
Definition: indexing.c:183

◆ RemoveCollationById()

void RemoveCollationById ( Oid  collationOid)

Definition at line 214 of file pg_collation.c.

References BTEqualStrategyNumber, CatalogTupleDelete(), CollationOidIndexId, elog, ERROR, HeapTupleIsValid, ObjectIdGetDatum, RowExclusiveLock, ScanKeyInit(), systable_beginscan(), systable_endscan(), systable_getnext(), HeapTupleData::t_self, table_close(), and table_open().

Referenced by doDeletion().

215 {
216  Relation rel;
217  ScanKeyData scanKeyData;
218  SysScanDesc scandesc;
219  HeapTuple tuple;
220 
221  rel = table_open(CollationRelationId, RowExclusiveLock);
222 
223  ScanKeyInit(&scanKeyData,
224  Anum_pg_collation_oid,
225  BTEqualStrategyNumber, F_OIDEQ,
226  ObjectIdGetDatum(collationOid));
227 
228  scandesc = systable_beginscan(rel, CollationOidIndexId, true,
229  NULL, 1, &scanKeyData);
230 
231  tuple = systable_getnext(scandesc);
232 
233  if (HeapTupleIsValid(tuple))
234  CatalogTupleDelete(rel, &tuple->t_self);
235  else
236  elog(ERROR, "could not find tuple for collation %u", collationOid);
237 
238  systable_endscan(scandesc);
239 
241 }
#define CollationOidIndexId
Definition: indexing.h:123
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:133
void systable_endscan(SysScanDesc sysscan)
Definition: genam.c:525
void CatalogTupleDelete(Relation heapRel, ItemPointer tid)
Definition: indexing.c:269
SysScanDesc systable_beginscan(Relation heapRelation, Oid indexId, bool indexOK, Snapshot snapshot, int nkeys, ScanKey key)
Definition: genam.c:352
HeapTuple systable_getnext(SysScanDesc sysscan)
Definition: genam.c:444
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:43
ItemPointerData t_self
Definition: htup.h:65
#define RowExclusiveLock
Definition: lockdefs.h:38
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define elog(elevel,...)
Definition: elog.h:226
void ScanKeyInit(ScanKey entry, AttrNumber attributeNumber, StrategyNumber strategy, RegProcedure procedure, Datum argument)
Definition: scankey.c:76
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
#define BTEqualStrategyNumber
Definition: stratnum.h:31

Variable Documentation

◆ FormData_pg_collation

FormData_pg_collation

Definition at line 44 of file pg_collation.h.