PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
pg_collation.c File Reference
#include "postgres.h"
#include "access/genam.h"
#include "access/heapam.h"
#include "access/htup_details.h"
#include "access/sysattr.h"
#include "catalog/dependency.h"
#include "catalog/indexing.h"
#include "catalog/objectaccess.h"
#include "catalog/pg_collation.h"
#include "catalog/pg_collation_fn.h"
#include "catalog/pg_namespace.h"
#include "mb/pg_wchar.h"
#include "utils/builtins.h"
#include "utils/fmgroids.h"
#include "utils/rel.h"
#include "utils/syscache.h"
#include "utils/tqual.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, int32 collencoding, const char *collcollate, const char *collctype, bool if_not_exists)
 
void RemoveCollationById (Oid collationOid)
 

Function Documentation

Oid CollationCreate ( const char *  collname,
Oid  collnamespace,
Oid  collowner,
int32  collencoding,
const char *  collcollate,
const char *  collctype,
bool  if_not_exists 
)

Definition at line 41 of file pg_collation.c.

References Anum_pg_collation_collcollate, Anum_pg_collation_collctype, Anum_pg_collation_collencoding, Anum_pg_collation_collname, Anum_pg_collation_collnamespace, Anum_pg_collation_collowner, Assert, AssertArg, CatalogTupleInsert(), ObjectAddress::classId, CollationRelationId, COLLNAMEENCNSP, DEPENDENCY_NORMAL, ereport, errcode(), ERRCODE_DUPLICATE_OBJECT, errmsg(), ERROR, heap_close, heap_form_tuple(), heap_freetuple(), heap_open(), HeapTupleGetOid, Int32GetDatum, InvalidOid, InvokeObjectPostCreateHook, NameGetDatum, NamespaceRelationId, namestrcpy(), Natts_pg_collation, NOTICE, ObjectAddress::objectId, ObjectIdGetDatum, ObjectAddress::objectSubId, OidIsValid, pg_encoding_to_char(), PointerGetDatum, recordDependencyOn(), recordDependencyOnCurrentExtension(), recordDependencyOnOwner(), RelationGetDescr, RowExclusiveLock, SearchSysCacheExists3, and values.

Referenced by DefineCollation(), and pg_import_system_collations().

46 {
47  Relation rel;
48  TupleDesc tupDesc;
49  HeapTuple tup;
51  bool nulls[Natts_pg_collation];
52  NameData name_name,
53  name_collate,
54  name_ctype;
55  Oid oid;
56  ObjectAddress myself,
57  referenced;
58 
59  AssertArg(collname);
60  AssertArg(collnamespace);
61  AssertArg(collowner);
62  AssertArg(collcollate);
63  AssertArg(collctype);
64 
65  /*
66  * Make sure there is no existing collation of same name & encoding.
67  *
68  * This would be caught by the unique index anyway; we're just giving a
69  * friendlier error message. The unique index provides a backstop against
70  * race conditions.
71  */
73  PointerGetDatum(collname),
74  Int32GetDatum(collencoding),
75  ObjectIdGetDatum(collnamespace)))
76  {
77  if (if_not_exists)
78  {
81  errmsg("collation \"%s\" for encoding \"%s\" already exists, skipping",
82  collname, pg_encoding_to_char(collencoding))));
83  return InvalidOid;
84  }
85  else
86  ereport(ERROR,
88  errmsg("collation \"%s\" for encoding \"%s\" already exists",
89  collname, pg_encoding_to_char(collencoding))));
90  }
91 
92  /*
93  * Also forbid matching an any-encoding entry. This test of course is not
94  * backed up by the unique index, but it's not a problem since we don't
95  * support adding any-encoding entries after initdb.
96  */
98  PointerGetDatum(collname),
99  Int32GetDatum(-1),
100  ObjectIdGetDatum(collnamespace)))
101  {
102  if (if_not_exists)
103  {
104  ereport(NOTICE,
106  errmsg("collation \"%s\" already exists, skipping",
107  collname)));
108  return InvalidOid;
109  }
110  else
111  ereport(ERROR,
113  errmsg("collation \"%s\" already exists",
114  collname)));
115  }
116 
117  /* open pg_collation */
119  tupDesc = RelationGetDescr(rel);
120 
121  /* form a tuple */
122  memset(nulls, 0, sizeof(nulls));
123 
124  namestrcpy(&name_name, collname);
125  values[Anum_pg_collation_collname - 1] = NameGetDatum(&name_name);
126  values[Anum_pg_collation_collnamespace - 1] = ObjectIdGetDatum(collnamespace);
127  values[Anum_pg_collation_collowner - 1] = ObjectIdGetDatum(collowner);
128  values[Anum_pg_collation_collencoding - 1] = Int32GetDatum(collencoding);
129  namestrcpy(&name_collate, collcollate);
130  values[Anum_pg_collation_collcollate - 1] = NameGetDatum(&name_collate);
131  namestrcpy(&name_ctype, collctype);
132  values[Anum_pg_collation_collctype - 1] = NameGetDatum(&name_ctype);
133 
134  tup = heap_form_tuple(tupDesc, values, nulls);
135 
136  /* insert a new tuple */
137  oid = CatalogTupleInsert(rel, tup);
138  Assert(OidIsValid(oid));
139 
140  /* set up dependencies for the new collation */
141  myself.classId = CollationRelationId;
142  myself.objectId = oid;
143  myself.objectSubId = 0;
144 
145  /* create dependency on namespace */
146  referenced.classId = NamespaceRelationId;
147  referenced.objectId = collnamespace;
148  referenced.objectSubId = 0;
149  recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL);
150 
151  /* create dependency on owner */
153  collowner);
154 
155  /* dependency on extension */
156  recordDependencyOnCurrentExtension(&myself, false);
157 
158  /* Post creation hook for new collation */
160 
161  heap_freetuple(tup);
163 
164  return oid;
165 }
#define NamespaceRelationId
Definition: pg_namespace.h:34
#define NameGetDatum(X)
Definition: postgres.h:603
#define InvokeObjectPostCreateHook(classId, objectId, subId)
Definition: objectaccess.h:145
#define RelationGetDescr(relation)
Definition: rel.h:425
#define PointerGetDatum(X)
Definition: postgres.h:564
#define Natts_pg_collation
Definition: pg_collation.h:53
#define Anum_pg_collation_collcollate
Definition: pg_collation.h:58
int errcode(int sqlerrcode)
Definition: elog.c:575
#define Anum_pg_collation_collctype
Definition: pg_collation.h:59
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:692
#define heap_close(r, l)
Definition: heapam.h:97
void recordDependencyOnOwner(Oid classId, Oid objectId, Oid owner)
Definition: pg_shdepend.c:158
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1374
unsigned int Oid
Definition: postgres_ext.h:31
int namestrcpy(Name name, const char *str)
Definition: name.c:217
#define OidIsValid(objectId)
Definition: c.h:534
#define Anum_pg_collation_collnamespace
Definition: pg_collation.h:55
#define ObjectIdGetDatum(X)
Definition: postgres.h:515
#define ERROR
Definition: elog.h:43
#define SearchSysCacheExists3(cacheId, key1, key2, key3)
Definition: syscache.h:171
Oid CatalogTupleInsert(Relation heapRel, HeapTuple tup)
Definition: indexing.c:162
Definition: c.h:489
#define RowExclusiveLock
Definition: lockdefs.h:38
#define ereport(elevel, rest)
Definition: elog.h:122
#define AssertArg(condition)
Definition: c.h:673
#define Anum_pg_collation_collname
Definition: pg_collation.h:54
#define Anum_pg_collation_collowner
Definition: pg_collation.h:56
uintptr_t Datum
Definition: postgres.h:374
Relation heap_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1287
#define CollationRelationId
Definition: pg_collation.h:30
#define InvalidOid
Definition: postgres_ext.h:36
#define NOTICE
Definition: elog.h:37
const char * pg_encoding_to_char(int encoding)
Definition: encnames.c:531
#define Assert(condition)
Definition: c.h:671
void recordDependencyOnCurrentExtension(const ObjectAddress *object, bool isReplace)
Definition: pg_depend.c:139
static Datum values[MAXATTR]
Definition: bootstrap.c:162
#define Int32GetDatum(X)
Definition: postgres.h:487
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define Anum_pg_collation_collencoding
Definition: pg_collation.h:57
#define HeapTupleGetOid(tuple)
Definition: htup_details.h:695
#define ERRCODE_DUPLICATE_OBJECT
Definition: streamutil.c:34
void RemoveCollationById ( Oid  collationOid)

Definition at line 174 of file pg_collation.c.

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

Referenced by doDeletion().

175 {
176  Relation rel;
177  ScanKeyData scanKeyData;
178  SysScanDesc scandesc;
179  HeapTuple tuple;
180 
182 
183  ScanKeyInit(&scanKeyData,
185  BTEqualStrategyNumber, F_OIDEQ,
186  ObjectIdGetDatum(collationOid));
187 
188  scandesc = systable_beginscan(rel, CollationOidIndexId, true,
189  NULL, 1, &scanKeyData);
190 
191  tuple = systable_getnext(scandesc);
192 
193  if (HeapTupleIsValid(tuple))
194  CatalogTupleDelete(rel, &tuple->t_self);
195  else
196  elog(ERROR, "could not find tuple for collation %u", collationOid);
197 
198  systable_endscan(scandesc);
199 
201 }
#define CollationOidIndexId
Definition: indexing.h:121
void systable_endscan(SysScanDesc sysscan)
Definition: genam.c:493
#define ObjectIdAttributeNumber
Definition: sysattr.h:22
void CatalogTupleDelete(Relation heapRel, ItemPointer tid)
Definition: indexing.c:255
#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:322
HeapTuple systable_getnext(SysScanDesc sysscan)
Definition: genam.c:410
#define ObjectIdGetDatum(X)
Definition: postgres.h:515
#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:1287
#define CollationRelationId
Definition: pg_collation.h:30
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
#define NULL
Definition: c.h:226
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