1 /*-------------------------------------------------------------------------
2 *
3 * pg_conversion.c
4 * routines to support manipulation of the pg_conversion relation
5 *
6 * Portions Copyright (c) 1996-2018, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
8 *
9 *
10 * IDENTIFICATION
11 * src/backend/catalog/pg_conversion.c
12 *
13 *-------------------------------------------------------------------------
14 */
15 #include "postgres.h"
16
17 #include "access/heapam.h"
18 #include "access/htup_details.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_conversion.h"
24 #include "catalog/pg_namespace.h"
25 #include "catalog/pg_proc.h"
26 #include "mb/pg_wchar.h"
27 #include "utils/builtins.h"
28 #include "utils/catcache.h"
29 #include "utils/fmgroids.h"
30 #include "utils/rel.h"
31 #include "utils/syscache.h"
32 #include "utils/tqual.h"
33
34 /*
35 * ConversionCreate
36 *
37 * Add a new tuple to pg_conversion.
38 */
39 ObjectAddress
ConversionCreate(const char * conname,Oid connamespace,Oid conowner,int32 conforencoding,int32 contoencoding,Oid conproc,bool def)40 ConversionCreate(const char *conname, Oid connamespace,
41 Oid conowner,
42 int32 conforencoding, int32 contoencoding,
43 Oid conproc, bool def)
44 {
45 int i;
46 Relation rel;
47 TupleDesc tupDesc;
48 HeapTuple tup;
49 bool nulls[Natts_pg_conversion];
50 Datum values[Natts_pg_conversion];
51 NameData cname;
52 ObjectAddress myself,
53 referenced;
54
55 /* sanity checks */
56 if (!conname)
57 elog(ERROR, "no conversion name supplied");
58
59 /* make sure there is no existing conversion of same name */
60 if (SearchSysCacheExists2(CONNAMENSP,
61 PointerGetDatum(conname),
62 ObjectIdGetDatum(connamespace)))
63 ereport(ERROR,
64 (errcode(ERRCODE_DUPLICATE_OBJECT),
65 errmsg("conversion \"%s\" already exists", conname)));
66
67 if (def)
68 {
69 /*
70 * make sure there is no existing default <for encoding><to encoding>
71 * pair in this name space
72 */
73 if (FindDefaultConversion(connamespace,
74 conforencoding,
75 contoencoding))
76 ereport(ERROR,
77 (errcode(ERRCODE_DUPLICATE_OBJECT),
78 errmsg("default conversion for %s to %s already exists",
79 pg_encoding_to_char(conforencoding),
80 pg_encoding_to_char(contoencoding))));
81 }
82
83 /* open pg_conversion */
84 rel = heap_open(ConversionRelationId, RowExclusiveLock);
85 tupDesc = rel->rd_att;
86
87 /* initialize nulls and values */
88 for (i = 0; i < Natts_pg_conversion; i++)
89 {
90 nulls[i] = false;
91 values[i] = (Datum) NULL;
92 }
93
94 /* form a tuple */
95 namestrcpy(&cname, conname);
96 values[Anum_pg_conversion_conname - 1] = NameGetDatum(&cname);
97 values[Anum_pg_conversion_connamespace - 1] = ObjectIdGetDatum(connamespace);
98 values[Anum_pg_conversion_conowner - 1] = ObjectIdGetDatum(conowner);
99 values[Anum_pg_conversion_conforencoding - 1] = Int32GetDatum(conforencoding);
100 values[Anum_pg_conversion_contoencoding - 1] = Int32GetDatum(contoencoding);
101 values[Anum_pg_conversion_conproc - 1] = ObjectIdGetDatum(conproc);
102 values[Anum_pg_conversion_condefault - 1] = BoolGetDatum(def);
103
104 tup = heap_form_tuple(tupDesc, values, nulls);
105
106 /* insert a new tuple */
107 CatalogTupleInsert(rel, tup);
108
109 myself.classId = ConversionRelationId;
110 myself.objectId = HeapTupleGetOid(tup);
111 myself.objectSubId = 0;
112
113 /* create dependency on conversion procedure */
114 referenced.classId = ProcedureRelationId;
115 referenced.objectId = conproc;
116 referenced.objectSubId = 0;
117 recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL);
118
119 /* create dependency on namespace */
120 referenced.classId = NamespaceRelationId;
121 referenced.objectId = connamespace;
122 referenced.objectSubId = 0;
123 recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL);
124
125 /* create dependency on owner */
126 recordDependencyOnOwner(ConversionRelationId, HeapTupleGetOid(tup),
127 conowner);
128
129 /* dependency on extension */
130 recordDependencyOnCurrentExtension(&myself, false);
131
132 /* Post creation hook for new conversion */
133 InvokeObjectPostCreateHook(ConversionRelationId, HeapTupleGetOid(tup), 0);
134
135 heap_freetuple(tup);
136 heap_close(rel, RowExclusiveLock);
137
138 return myself;
139 }
140
141 /*
142 * RemoveConversionById
143 *
144 * Remove a tuple from pg_conversion by Oid. This function is solely
145 * called inside catalog/dependency.c
146 */
147 void
RemoveConversionById(Oid conversionOid)148 RemoveConversionById(Oid conversionOid)
149 {
150 Relation rel;
151 HeapTuple tuple;
152 HeapScanDesc scan;
153 ScanKeyData scanKeyData;
154
155 ScanKeyInit(&scanKeyData,
156 ObjectIdAttributeNumber,
157 BTEqualStrategyNumber, F_OIDEQ,
158 ObjectIdGetDatum(conversionOid));
159
160 /* open pg_conversion */
161 rel = heap_open(ConversionRelationId, RowExclusiveLock);
162
163 scan = heap_beginscan_catalog(rel, 1, &scanKeyData);
164
165 /* search for the target tuple */
166 if (HeapTupleIsValid(tuple = heap_getnext(scan, ForwardScanDirection)))
167 CatalogTupleDelete(rel, &tuple->t_self);
168 else
169 elog(ERROR, "could not find tuple for conversion %u", conversionOid);
170 heap_endscan(scan);
171 heap_close(rel, RowExclusiveLock);
172 }
173
174 /*
175 * FindDefaultConversion
176 *
177 * Find "default" conversion proc by for_encoding and to_encoding in the
178 * given namespace.
179 *
180 * If found, returns the procedure's oid, otherwise InvalidOid. Note that
181 * you get the procedure's OID not the conversion's OID!
182 */
183 Oid
FindDefaultConversion(Oid name_space,int32 for_encoding,int32 to_encoding)184 FindDefaultConversion(Oid name_space, int32 for_encoding, int32 to_encoding)
185 {
186 CatCList *catlist;
187 HeapTuple tuple;
188 Form_pg_conversion body;
189 Oid proc = InvalidOid;
190 int i;
191
192 catlist = SearchSysCacheList3(CONDEFAULT,
193 ObjectIdGetDatum(name_space),
194 Int32GetDatum(for_encoding),
195 Int32GetDatum(to_encoding));
196
197 for (i = 0; i < catlist->n_members; i++)
198 {
199 tuple = &catlist->members[i]->tuple;
200 body = (Form_pg_conversion) GETSTRUCT(tuple);
201 if (body->condefault)
202 {
203 proc = body->conproc;
204 break;
205 }
206 }
207 ReleaseSysCacheList(catlist);
208 return proc;
209 }
210