OSDN Git Service

5b92a4c0c2816d29d6e3379e6422b20a29b8743d
[pg-rex/syncrep.git] / src / backend / catalog / pg_collation.c
1 /*-------------------------------------------------------------------------
2  *
3  * pg_collation.c
4  *        routines to support manipulation of the pg_collation relation
5  *
6  * Portions Copyright (c) 1996-2011, 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/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"
31
32
33 /*
34  * CollationCreate
35  *
36  * Add a new tuple to pg_collation.
37  */
38 Oid
39 CollationCreate(const char *collname, Oid collnamespace,
40                                 Oid collowner,
41                                 int32 collencoding,
42                                 const char *collcollate, const char *collctype)
43 {
44         Relation        rel;
45         TupleDesc       tupDesc;
46         HeapTuple       tup;
47         Datum           values[Natts_pg_collation];
48         bool            nulls[Natts_pg_collation];
49         NameData        name_name,
50                                 name_collate,
51                                 name_ctype;
52         Oid                     oid;
53         ObjectAddress myself,
54                                 referenced;
55
56         AssertArg(collname);
57         AssertArg(collnamespace);
58         AssertArg(collowner);
59         AssertArg(collcollate);
60         AssertArg(collctype);
61
62         /*
63          * Make sure there is no existing collation of same name & encoding.
64          *
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
67          * race conditions.
68          */
69         if (SearchSysCacheExists3(COLLNAMEENCNSP,
70                                                           PointerGetDatum(collname),
71                                                           Int32GetDatum(collencoding),
72                                                           ObjectIdGetDatum(collnamespace)))
73                 ereport(ERROR,
74                                 (errcode(ERRCODE_DUPLICATE_OBJECT),
75                                  errmsg("collation \"%s\" for encoding \"%s\" already exists",
76                                                 collname, pg_encoding_to_char(collencoding))));
77
78         /*
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.
82          */
83         if (SearchSysCacheExists3(COLLNAMEENCNSP,
84                                                           PointerGetDatum(collname),
85                                                           Int32GetDatum(-1),
86                                                           ObjectIdGetDatum(collnamespace)))
87                 ereport(ERROR,
88                                 (errcode(ERRCODE_DUPLICATE_OBJECT),
89                                  errmsg("collation \"%s\" already exists",
90                                                 collname)));
91
92         /* open pg_collation */
93         rel = heap_open(CollationRelationId, RowExclusiveLock);
94         tupDesc = RelationGetDescr(rel);
95
96         /* form a tuple */
97         memset(nulls, 0, sizeof(nulls));
98
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);
108
109         tup = heap_form_tuple(tupDesc, values, nulls);
110
111         /* insert a new tuple */
112         oid = simple_heap_insert(rel, tup);
113         Assert(OidIsValid(oid));
114
115         /* update the index if any */
116         CatalogUpdateIndexes(rel, tup);
117
118         /* set up dependencies for the new collation */
119         myself.classId = CollationRelationId;
120         myself.objectId = oid;
121         myself.objectSubId = 0;
122
123         /* create dependency on namespace */
124         referenced.classId = NamespaceRelationId;
125         referenced.objectId = collnamespace;
126         referenced.objectSubId = 0;
127         recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL);
128
129         /* create dependency on owner */
130         recordDependencyOnOwner(CollationRelationId, HeapTupleGetOid(tup),
131                                                         collowner);
132
133         /* dependency on extension */
134         recordDependencyOnCurrentExtension(&myself);
135
136         /* Post creation hook for new collation */
137         InvokeObjectAccessHook(OAT_POST_CREATE,
138                                                    CollationRelationId, oid, 0);
139
140         heap_freetuple(tup);
141         heap_close(rel, RowExclusiveLock);
142
143         return oid;
144 }
145
146 /*
147  * RemoveCollationById
148  *
149  * Remove a tuple from pg_collation by Oid. This function is solely
150  * called inside catalog/dependency.c
151  */
152 void
153 RemoveCollationById(Oid collationOid)
154 {
155         Relation        rel;
156         ScanKeyData scanKeyData;
157         SysScanDesc scandesc;
158         HeapTuple       tuple;
159
160         rel = heap_open(CollationRelationId, RowExclusiveLock);
161
162         ScanKeyInit(&scanKeyData,
163                                 ObjectIdAttributeNumber,
164                                 BTEqualStrategyNumber, F_OIDEQ,
165                                 ObjectIdGetDatum(collationOid));
166
167         scandesc = systable_beginscan(rel, CollationOidIndexId, true,
168                                                                   SnapshotNow, 1, &scanKeyData);
169
170         tuple = systable_getnext(scandesc);
171
172         if (HeapTupleIsValid(tuple))
173                 simple_heap_delete(rel, &tuple->t_self);
174         else
175                 elog(ERROR, "could not find tuple for collation %u", collationOid);
176
177         systable_endscan(scandesc);
178
179         heap_close(rel, RowExclusiveLock);
180 }