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, 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

Function Documentation

◆ CATALOG()

CATALOG ( pg_collation  ,
3456  ,
CollationRelationId   
)

Definition at line 29 of file pg_collation.h.

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

◆ CollationCreate()

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 46 of file pg_collation.c.

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

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 = heap_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  heap_close(rel, NoLock);
132  return InvalidOid;
133  }
134  else if (if_not_exists)
135  {
136  heap_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);
156  values[Anum_pg_collation_collname - 1] = NameGetDatum(&name_name);
157  values[Anum_pg_collation_collnamespace - 1] = ObjectIdGetDatum(collnamespace);
158  values[Anum_pg_collation_collowner - 1] = ObjectIdGetDatum(collowner);
159  values[Anum_pg_collation_collprovider - 1] = CharGetDatum(collprovider);
160  values[Anum_pg_collation_collencoding - 1] = Int32GetDatum(collencoding);
161  namestrcpy(&name_collate, collcollate);
162  values[Anum_pg_collation_collcollate - 1] = NameGetDatum(&name_collate);
163  namestrcpy(&name_ctype, collctype);
164  values[Anum_pg_collation_collctype - 1] = NameGetDatum(&name_ctype);
165  if (collversion)
166  values[Anum_pg_collation_collversion - 1] = CStringGetTextDatum(collversion);
167  else
168  nulls[Anum_pg_collation_collversion - 1] = true;
169 
170  tup = heap_form_tuple(tupDesc, values, nulls);
171 
172  /* insert a new tuple */
173  oid = 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, HeapTupleGetOid(tup),
189  collowner);
190 
191  /* dependency on extension */
192  recordDependencyOnCurrentExtension(&myself, false);
193 
194  /* Post creation hook for new collation */
195  InvokeObjectPostCreateHook(CollationRelationId, oid, 0);
196 
197  heap_freetuple(tup);
198  heap_close(rel, NoLock);
199 
200  return oid;
201 }
#define NameGetDatum(X)
Definition: postgres.h:578
#define InvokeObjectPostCreateHook(classId, objectId, subId)
Definition: objectaccess.h:145
#define RelationGetDescr(relation)
Definition: rel.h:433
#define PointerGetDatum(X)
Definition: postgres.h:539
int errcode(int sqlerrcode)
Definition: elog.c:575
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:1074
#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:1773
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:605
#define ObjectIdGetDatum(X)
Definition: postgres.h:490
#define ERROR
Definition: elog.h:43
#define SearchSysCacheExists3(cacheId, key1, key2, key3)
Definition: syscache.h:186
Oid CatalogTupleInsert(Relation heapRel, HeapTuple tup)
Definition: indexing.c:163
Definition: c.h:570
#define NoLock
Definition: lockdefs.h:34
#define ereport(elevel, rest)
Definition: elog.h:122
#define AssertArg(condition)
Definition: c.h:701
uintptr_t Datum
Definition: postgres.h:365
int GetDatabaseEncoding(void)
Definition: mbutils.c:1004
Relation heap_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1294
#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:699
#define ShareRowExclusiveLock
Definition: lockdefs.h:42
void recordDependencyOnCurrentExtension(const ObjectAddress *object, bool isReplace)
Definition: pg_depend.c:139
#define CharGetDatum(X)
Definition: postgres.h:399
static Datum values[MAXATTR]
Definition: bootstrap.c:164
#define Int32GetDatum(X)
Definition: postgres.h:462
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define CStringGetTextDatum(s)
Definition: builtins.h:95
#define HeapTupleGetOid(tuple)
Definition: htup_details.h:712
#define ERRCODE_DUPLICATE_OBJECT
Definition: streamutil.c:32

◆ RemoveCollationById()

void RemoveCollationById ( Oid  collationOid)

Definition at line 210 of file pg_collation.c.

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

Referenced by doDeletion().

211 {
212  Relation rel;
213  ScanKeyData scanKeyData;
214  SysScanDesc scandesc;
215  HeapTuple tuple;
216 
217  rel = heap_open(CollationRelationId, RowExclusiveLock);
218 
219  ScanKeyInit(&scanKeyData,
221  BTEqualStrategyNumber, F_OIDEQ,
222  ObjectIdGetDatum(collationOid));
223 
224  scandesc = systable_beginscan(rel, CollationOidIndexId, true,
225  NULL, 1, &scanKeyData);
226 
227  tuple = systable_getnext(scandesc);
228 
229  if (HeapTupleIsValid(tuple))
230  CatalogTupleDelete(rel, &tuple->t_self);
231  else
232  elog(ERROR, "could not find tuple for collation %u", collationOid);
233 
234  systable_endscan(scandesc);
235 
237 }
#define CollationOidIndexId
Definition: indexing.h:121
void systable_endscan(SysScanDesc sysscan)
Definition: genam.c:502
#define ObjectIdAttributeNumber
Definition: sysattr.h:22
void CatalogTupleDelete(Relation heapRel, ItemPointer tid)
Definition: indexing.c:256
#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:331
HeapTuple systable_getnext(SysScanDesc sysscan)
Definition: genam.c:419
#define ObjectIdGetDatum(X)
Definition: postgres.h:490
#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:1294
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
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

Variable Documentation

◆ FormData_pg_collation

FormData_pg_collation

Definition at line 42 of file pg_collation.h.