PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
pg_collation.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * pg_collation.c
4  * routines to support manipulation of the pg_collation relation
5  *
6  * Portions Copyright (c) 1996-2017, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  *
10  * IDENTIFICATION
11  * src/backend/catalog/pg_collation.c
12  *
13  *-------------------------------------------------------------------------
14  */
15 #include "postgres.h"
16 
17 #include "access/genam.h"
18 #include "access/heapam.h"
19 #include "access/htup_details.h"
20 #include "access/sysattr.h"
21 #include "catalog/dependency.h"
22 #include "catalog/indexing.h"
23 #include "catalog/objectaccess.h"
24 #include "catalog/pg_collation.h"
26 #include "catalog/pg_namespace.h"
27 #include "mb/pg_wchar.h"
28 #include "utils/builtins.h"
29 #include "utils/fmgroids.h"
30 #include "utils/rel.h"
31 #include "utils/syscache.h"
32 #include "utils/tqual.h"
33 
34 
35 /*
36  * CollationCreate
37  *
38  * Add a new tuple to pg_collation.
39  */
40 Oid
41 CollationCreate(const char *collname, Oid collnamespace,
42  Oid collowner,
43  int32 collencoding,
44  const char *collcollate, const char *collctype,
45  bool if_not_exists)
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 }
166 
167 /*
168  * RemoveCollationById
169  *
170  * Remove a tuple from pg_collation by Oid. This function is solely
171  * called inside catalog/dependency.c
172  */
173 void
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 NamespaceRelationId
Definition: pg_namespace.h:34
#define NameGetDatum(X)
Definition: postgres.h:603
#define CollationOidIndexId
Definition: indexing.h:121
void systable_endscan(SysScanDesc sysscan)
Definition: genam.c:493
#define InvokeObjectPostCreateHook(classId, objectId, subId)
Definition: objectaccess.h:145
#define RelationGetDescr(relation)
Definition: rel.h:425
#define ObjectIdAttributeNumber
Definition: sysattr.h:22
#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 CatalogTupleDelete(Relation heapRel, ItemPointer tid)
Definition: indexing.c:255
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
Oid CollationCreate(const char *collname, Oid collnamespace, Oid collowner, int32 collencoding, const char *collcollate, const char *collctype, bool if_not_exists)
Definition: pg_collation.c:41
#define OidIsValid(objectId)
Definition: c.h:534
void RemoveCollationById(Oid collationOid)
Definition: pg_collation.c:174
#define Anum_pg_collation_collnamespace
Definition: pg_collation.h:55
SysScanDesc systable_beginscan(Relation heapRelation, Oid indexId, bool indexOK, Snapshot snapshot, int nkeys, ScanKey key)
Definition: genam.c:322
signed int int32
Definition: c.h:253
HeapTuple systable_getnext(SysScanDesc sysscan)
Definition: genam.c:410
#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
ItemPointerData t_self
Definition: htup.h:65
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 HeapTupleIsValid(tuple)
Definition: htup.h:77
#define NULL
Definition: c.h:226
#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
void ScanKeyInit(ScanKey entry, AttrNumber attributeNumber, StrategyNumber strategy, RegProcedure procedure, Datum argument)
Definition: scankey.c:76
#define elog
Definition: elog.h:219
#define HeapTupleGetOid(tuple)
Definition: htup_details.h:695
#define ERRCODE_DUPLICATE_OBJECT
Definition: streamutil.c:34
#define BTEqualStrategyNumber
Definition: stratnum.h:31