1 /*-------------------------------------------------------------------------
4 * routines to support manipulation of the pg_collation relation
6 * Portions Copyright (c) 1996-2011, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * src/backend/catalog/pg_collation.c
13 *-------------------------------------------------------------------------
17 #include "access/genam.h"
18 #include "access/heapam.h"
19 #include "access/sysattr.h"
20 #include "catalog/dependency.h"
21 #include "catalog/indexing.h"
22 #include "catalog/objectaccess.h"
23 #include "catalog/pg_collation.h"
24 #include "catalog/pg_collation_fn.h"
25 #include "catalog/pg_namespace.h"
26 #include "mb/pg_wchar.h"
27 #include "utils/builtins.h"
28 #include "utils/fmgroids.h"
29 #include "utils/syscache.h"
30 #include "utils/tqual.h"
36 * Add a new tuple to pg_collation.
39 CollationCreate(const char *collname, Oid collnamespace,
42 const char *collcollate, const char *collctype)
47 Datum values[Natts_pg_collation];
48 bool nulls[Natts_pg_collation];
57 AssertArg(collnamespace);
59 AssertArg(collcollate);
63 * Make sure there is no existing collation of same name & encoding.
65 * This would be caught by the unique index anyway; we're just giving a
66 * friendlier error message. The unique index provides a backstop against
69 if (SearchSysCacheExists3(COLLNAMEENCNSP,
70 PointerGetDatum(collname),
71 Int32GetDatum(collencoding),
72 ObjectIdGetDatum(collnamespace)))
74 (errcode(ERRCODE_DUPLICATE_OBJECT),
75 errmsg("collation \"%s\" for encoding \"%s\" already exists",
76 collname, pg_encoding_to_char(collencoding))));
79 * Also forbid matching an any-encoding entry. This test of course is not
80 * backed up by the unique index, but it's not a problem since we don't
81 * support adding any-encoding entries after initdb.
83 if (SearchSysCacheExists3(COLLNAMEENCNSP,
84 PointerGetDatum(collname),
86 ObjectIdGetDatum(collnamespace)))
88 (errcode(ERRCODE_DUPLICATE_OBJECT),
89 errmsg("collation \"%s\" already exists",
92 /* open pg_collation */
93 rel = heap_open(CollationRelationId, RowExclusiveLock);
94 tupDesc = RelationGetDescr(rel);
97 memset(nulls, 0, sizeof(nulls));
99 namestrcpy(&name_name, collname);
100 values[Anum_pg_collation_collname - 1] = NameGetDatum(&name_name);
101 values[Anum_pg_collation_collnamespace - 1] = ObjectIdGetDatum(collnamespace);
102 values[Anum_pg_collation_collowner - 1] = ObjectIdGetDatum(collowner);
103 values[Anum_pg_collation_collencoding - 1] = Int32GetDatum(collencoding);
104 namestrcpy(&name_collate, collcollate);
105 values[Anum_pg_collation_collcollate - 1] = NameGetDatum(&name_collate);
106 namestrcpy(&name_ctype, collctype);
107 values[Anum_pg_collation_collctype - 1] = NameGetDatum(&name_ctype);
109 tup = heap_form_tuple(tupDesc, values, nulls);
111 /* insert a new tuple */
112 oid = simple_heap_insert(rel, tup);
113 Assert(OidIsValid(oid));
115 /* update the index if any */
116 CatalogUpdateIndexes(rel, tup);
118 /* set up dependencies for the new collation */
119 myself.classId = CollationRelationId;
120 myself.objectId = oid;
121 myself.objectSubId = 0;
123 /* create dependency on namespace */
124 referenced.classId = NamespaceRelationId;
125 referenced.objectId = collnamespace;
126 referenced.objectSubId = 0;
127 recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL);
129 /* create dependency on owner */
130 recordDependencyOnOwner(CollationRelationId, HeapTupleGetOid(tup),
133 /* dependency on extension */
134 recordDependencyOnCurrentExtension(&myself);
136 /* Post creation hook for new collation */
137 InvokeObjectAccessHook(OAT_POST_CREATE,
138 CollationRelationId, oid, 0);
141 heap_close(rel, RowExclusiveLock);
147 * RemoveCollationById
149 * Remove a tuple from pg_collation by Oid. This function is solely
150 * called inside catalog/dependency.c
153 RemoveCollationById(Oid collationOid)
156 ScanKeyData scanKeyData;
157 SysScanDesc scandesc;
160 rel = heap_open(CollationRelationId, RowExclusiveLock);
162 ScanKeyInit(&scanKeyData,
163 ObjectIdAttributeNumber,
164 BTEqualStrategyNumber, F_OIDEQ,
165 ObjectIdGetDatum(collationOid));
167 scandesc = systable_beginscan(rel, CollationOidIndexId, true,
168 SnapshotNow, 1, &scanKeyData);
170 tuple = systable_getnext(scandesc);
172 if (HeapTupleIsValid(tuple))
173 simple_heap_delete(rel, &tuple->t_self);
175 elog(ERROR, "could not find tuple for collation %u", collationOid);
177 systable_endscan(scandesc);
179 heap_close(rel, RowExclusiveLock);