Age Owner TLA Line data Source code
1 : /*-------------------------------------------------------------------------
2 : *
3 : * gistvalidate.c
4 : * Opclass validator for GiST.
5 : *
6 : * Portions Copyright (c) 1996-2023, PostgreSQL Global Development Group
7 : * Portions Copyright (c) 1994, Regents of the University of California
8 : *
9 : * IDENTIFICATION
10 : * src/backend/access/gist/gistvalidate.c
11 : *
12 : *-------------------------------------------------------------------------
13 : */
14 : #include "postgres.h"
15 :
16 : #include "access/amvalidate.h"
17 : #include "access/gist_private.h"
18 : #include "access/htup_details.h"
19 : #include "catalog/pg_amop.h"
20 : #include "catalog/pg_amproc.h"
21 : #include "catalog/pg_opclass.h"
22 : #include "catalog/pg_opfamily.h"
23 : #include "catalog/pg_type.h"
24 : #include "utils/builtins.h"
25 : #include "utils/lsyscache.h"
26 : #include "utils/regproc.h"
27 : #include "utils/syscache.h"
28 :
29 :
30 : /*
31 : * Validator for a GiST opclass.
32 : */
33 : bool
2639 tgl 34 CBC 61 : gistvalidate(Oid opclassoid)
35 : {
2635 36 61 : bool result = true;
37 : HeapTuple classtup;
38 : Form_pg_opclass classform;
39 : Oid opfamilyoid;
40 : Oid opcintype;
41 : Oid opckeytype;
42 : char *opclassname;
43 : HeapTuple familytup;
44 : Form_pg_opfamily familyform;
45 : char *opfamilyname;
46 : CatCList *proclist,
47 : *oprlist;
48 : List *grouplist;
49 : OpFamilyOpFuncGroup *opclassgroup;
50 : int i;
51 : ListCell *lc;
52 :
53 : /* Fetch opclass information */
2639 54 61 : classtup = SearchSysCache1(CLAOID, ObjectIdGetDatum(opclassoid));
55 61 : if (!HeapTupleIsValid(classtup))
2639 tgl 56 UBC 0 : elog(ERROR, "cache lookup failed for operator class %u", opclassoid);
2639 tgl 57 CBC 61 : classform = (Form_pg_opclass) GETSTRUCT(classtup);
58 :
59 61 : opfamilyoid = classform->opcfamily;
60 61 : opcintype = classform->opcintype;
2635 61 61 : opckeytype = classform->opckeytype;
62 61 : if (!OidIsValid(opckeytype))
63 12 : opckeytype = opcintype;
64 61 : opclassname = NameStr(classform->opcname);
65 :
66 : /* Fetch opfamily information */
67 61 : familytup = SearchSysCache1(OPFAMILYOID, ObjectIdGetDatum(opfamilyoid));
68 61 : if (!HeapTupleIsValid(familytup))
2635 tgl 69 UBC 0 : elog(ERROR, "cache lookup failed for operator family %u", opfamilyoid);
2635 tgl 70 CBC 61 : familyform = (Form_pg_opfamily) GETSTRUCT(familytup);
71 :
72 61 : opfamilyname = NameStr(familyform->opfname);
73 :
74 : /* Fetch all operators and support functions of the opfamily */
2639 75 61 : oprlist = SearchSysCacheList1(AMOPSTRATEGY, ObjectIdGetDatum(opfamilyoid));
76 61 : proclist = SearchSysCacheList1(AMPROCNUM, ObjectIdGetDatum(opfamilyoid));
77 :
78 : /* Check individual support functions */
79 521 : for (i = 0; i < proclist->n_members; i++)
80 : {
81 460 : HeapTuple proctup = &proclist->members[i]->tuple;
82 460 : Form_pg_amproc procform = (Form_pg_amproc) GETSTRUCT(proctup);
83 : bool ok;
84 :
85 : /*
86 : * All GiST support functions should be registered with matching
87 : * left/right types
88 : */
2635 89 460 : if (procform->amproclefttype != procform->amprocrighttype)
90 : {
2635 tgl 91 UBC 0 : ereport(INFO,
92 : (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
93 : errmsg("operator family \"%s\" of access method %s contains support function %s with different left and right input types",
94 : opfamilyname, "gist",
95 : format_procedure(procform->amproc))));
96 0 : result = false;
97 : }
98 :
99 : /*
100 : * We can't check signatures except within the specific opclass, since
101 : * we need to know the associated opckeytype in many cases.
102 : */
2635 tgl 103 CBC 460 : if (procform->amproclefttype != opcintype)
2635 tgl 104 UBC 0 : continue;
105 :
106 : /* Check procedure numbers and function signatures */
2635 tgl 107 CBC 460 : switch (procform->amprocnum)
108 : {
109 61 : case GIST_CONSISTENT_PROC:
110 61 : ok = check_amproc_signature(procform->amproc, BOOLOID, false,
111 : 5, 5, INTERNALOID, opcintype,
112 : INT2OID, OIDOID, INTERNALOID);
113 61 : break;
114 61 : case GIST_UNION_PROC:
115 61 : ok = check_amproc_signature(procform->amproc, opckeytype, false,
116 : 2, 2, INTERNALOID, INTERNALOID);
117 61 : break;
118 117 : case GIST_COMPRESS_PROC:
119 : case GIST_DECOMPRESS_PROC:
120 : case GIST_FETCH_PROC:
121 117 : ok = check_amproc_signature(procform->amproc, INTERNALOID, true,
122 : 1, 1, INTERNALOID);
123 117 : break;
124 61 : case GIST_PENALTY_PROC:
125 61 : ok = check_amproc_signature(procform->amproc, INTERNALOID, true,
126 : 3, 3, INTERNALOID,
127 : INTERNALOID, INTERNALOID);
128 61 : break;
129 61 : case GIST_PICKSPLIT_PROC:
130 61 : ok = check_amproc_signature(procform->amproc, INTERNALOID, true,
131 : 2, 2, INTERNALOID, INTERNALOID);
132 61 : break;
133 61 : case GIST_EQUAL_PROC:
134 61 : ok = check_amproc_signature(procform->amproc, INTERNALOID, false,
135 : 3, 3, opckeytype, opckeytype,
136 : INTERNALOID);
137 61 : break;
138 26 : case GIST_DISTANCE_PROC:
139 26 : ok = check_amproc_signature(procform->amproc, FLOAT8OID, false,
140 : 5, 5, INTERNALOID, opcintype,
141 : INT2OID, OIDOID, INTERNALOID);
142 26 : break;
1105 akorotkov 143 9 : case GIST_OPTIONS_PROC:
144 9 : ok = check_amoptsproc_signature(procform->amproc);
145 9 : break;
934 heikki.linnakangas 146 3 : case GIST_SORTSUPPORT_PROC:
147 3 : ok = check_amproc_signature(procform->amproc, VOIDOID, true,
148 : 1, 1, INTERNALOID);
149 3 : break;
2635 tgl 150 UBC 0 : default:
151 0 : ereport(INFO,
152 : (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
153 : errmsg("operator family \"%s\" of access method %s contains function %s with invalid support number %d",
154 : opfamilyname, "gist",
155 : format_procedure(procform->amproc),
156 : procform->amprocnum)));
157 0 : result = false;
158 0 : continue; /* don't want additional message */
159 : }
160 :
2635 tgl 161 CBC 460 : if (!ok)
162 : {
2635 tgl 163 UBC 0 : ereport(INFO,
164 : (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
165 : errmsg("operator family \"%s\" of access method %s contains function %s with wrong signature for support number %d",
166 : opfamilyname, "gist",
167 : format_procedure(procform->amproc),
168 : procform->amprocnum)));
169 0 : result = false;
170 : }
171 : }
172 :
173 : /* Check individual operators */
2639 tgl 174 CBC 589 : for (i = 0; i < oprlist->n_members; i++)
175 : {
176 528 : HeapTuple oprtup = &oprlist->members[i]->tuple;
177 528 : Form_pg_amop oprform = (Form_pg_amop) GETSTRUCT(oprtup);
178 : Oid op_rettype;
179 :
180 : /* TODO: Check that only allowed strategy numbers exist */
181 528 : if (oprform->amopstrategy < 1)
182 : {
2635 tgl 183 UBC 0 : ereport(INFO,
184 : (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
185 : errmsg("operator family \"%s\" of access method %s contains operator %s with invalid strategy number %d",
186 : opfamilyname, "gist",
187 : format_operator(oprform->amopopr),
188 : oprform->amopstrategy)));
189 0 : result = false;
190 : }
191 :
192 : /* GiST supports ORDER BY operators */
2635 tgl 193 CBC 528 : if (oprform->amoppurpose != AMOP_SEARCH)
194 : {
195 : /* ... but must have matching distance proc */
196 31 : if (!OidIsValid(get_opfamily_proc(opfamilyoid,
197 : oprform->amoplefttype,
198 : oprform->amoplefttype,
199 : GIST_DISTANCE_PROC)))
200 : {
2635 tgl 201 UBC 0 : ereport(INFO,
202 : (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
203 : errmsg("operator family \"%s\" of access method %s contains unsupported ORDER BY specification for operator %s",
204 : opfamilyname, "gist",
205 : format_operator(oprform->amopopr))));
206 0 : result = false;
207 : }
208 : /* ... and operator result must match the claimed btree opfamily */
2635 tgl 209 CBC 31 : op_rettype = get_op_rettype(oprform->amopopr);
210 31 : if (!opfamily_can_sort_type(oprform->amopsortfamily, op_rettype))
211 : {
2635 tgl 212 UBC 0 : ereport(INFO,
213 : (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
214 : errmsg("operator family \"%s\" of access method %s contains incorrect ORDER BY opfamily specification for operator %s",
215 : opfamilyname, "gist",
216 : format_operator(oprform->amopopr))));
217 0 : result = false;
218 : }
219 : }
220 : else
221 : {
222 : /* Search operators must always return bool */
2635 tgl 223 CBC 497 : op_rettype = BOOLOID;
224 : }
225 :
226 : /* Check operator signature */
227 528 : if (!check_amop_signature(oprform->amopopr, op_rettype,
228 : oprform->amoplefttype,
229 : oprform->amoprighttype))
230 : {
2635 tgl 231 UBC 0 : ereport(INFO,
232 : (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
233 : errmsg("operator family \"%s\" of access method %s contains operator %s with wrong signature",
234 : opfamilyname, "gist",
235 : format_operator(oprform->amopopr))));
236 0 : result = false;
237 : }
238 : }
239 :
240 : /* Now check for inconsistent groups of operators/functions */
2635 tgl 241 CBC 61 : grouplist = identify_opfamily_groups(oprlist, proclist);
242 61 : opclassgroup = NULL;
243 177 : foreach(lc, grouplist)
244 : {
245 116 : OpFamilyOpFuncGroup *thisgroup = (OpFamilyOpFuncGroup *) lfirst(lc);
246 :
247 : /* Remember the group exactly matching the test opclass */
248 116 : if (thisgroup->lefttype == opcintype &&
249 106 : thisgroup->righttype == opcintype)
250 61 : opclassgroup = thisgroup;
251 :
252 : /*
253 : * There is not a lot we can do to check the operator sets, since each
254 : * GiST opclass is more or less a law unto itself, and some contain
255 : * only operators that are binary-compatible with the opclass datatype
256 : * (meaning that empty operator sets can be OK). That case also means
257 : * that we shouldn't insist on nonempty function sets except for the
258 : * opclass's own group.
259 : */
260 : }
261 :
262 : /* Check that the originally-named opclass is complete */
2639 263 732 : for (i = 1; i <= GISTNProcs; i++)
264 : {
2635 265 671 : if (opclassgroup &&
266 671 : (opclassgroup->functionset & (((uint64) 1) << i)) != 0)
2639 267 460 : continue; /* got it */
2028 268 211 : if (i == GIST_DISTANCE_PROC || i == GIST_FETCH_PROC ||
1105 akorotkov 269 136 : i == GIST_COMPRESS_PROC || i == GIST_DECOMPRESS_PROC ||
697 tgl 270 58 : i == GIST_OPTIONS_PROC || i == GIST_SORTSUPPORT_PROC)
2639 271 211 : continue; /* optional methods */
2635 tgl 272 UBC 0 : ereport(INFO,
273 : (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
274 : errmsg("operator class \"%s\" of access method %s is missing support function %d",
275 : opclassname, "gist", i)));
276 0 : result = false;
277 : }
278 :
2639 tgl 279 CBC 61 : ReleaseCatCacheList(proclist);
280 61 : ReleaseCatCacheList(oprlist);
2635 281 61 : ReleaseSysCache(familytup);
282 61 : ReleaseSysCache(classtup);
283 :
284 61 : return result;
285 : }
286 :
287 : /*
288 : * Prechecking function for adding operators/functions to a GiST opfamily.
289 : */
290 : void
981 291 97 : gistadjustmembers(Oid opfamilyoid,
292 : Oid opclassoid,
293 : List *operators,
294 : List *functions)
295 : {
296 : ListCell *lc;
297 :
298 : /*
299 : * Operator members of a GiST opfamily should never have hard
300 : * dependencies, since their connection to the opfamily depends only on
301 : * what the support functions think, and that can be altered. For
302 : * consistency, we make all soft dependencies point to the opfamily,
303 : * though a soft dependency on the opclass would work as well in the
304 : * CREATE OPERATOR CLASS case.
305 : */
306 449 : foreach(lc, operators)
307 : {
308 352 : OpFamilyMember *op = (OpFamilyMember *) lfirst(lc);
309 :
310 352 : op->ref_is_hard = false;
311 352 : op->ref_is_family = true;
312 352 : op->refobjid = opfamilyoid;
313 : }
314 :
315 : /*
316 : * Required support functions should have hard dependencies. Preferably
317 : * those are just dependencies on the opclass, but if we're in ALTER
318 : * OPERATOR FAMILY, we leave the dependency pointing at the whole
319 : * opfamily. (Given that GiST opclasses generally don't share opfamilies,
320 : * it seems unlikely to be worth working harder.)
321 : */
322 473 : foreach(lc, functions)
323 : {
324 376 : OpFamilyMember *op = (OpFamilyMember *) lfirst(lc);
325 :
326 376 : switch (op->number)
327 : {
328 235 : case GIST_CONSISTENT_PROC:
329 : case GIST_UNION_PROC:
330 : case GIST_PENALTY_PROC:
331 : case GIST_PICKSPLIT_PROC:
332 : case GIST_EQUAL_PROC:
333 : /* Required support function */
334 235 : op->ref_is_hard = true;
335 235 : break;
336 141 : case GIST_COMPRESS_PROC:
337 : case GIST_DECOMPRESS_PROC:
338 : case GIST_DISTANCE_PROC:
339 : case GIST_FETCH_PROC:
340 : case GIST_OPTIONS_PROC:
341 : case GIST_SORTSUPPORT_PROC:
342 : /* Optional, so force it to be a soft family dependency */
343 141 : op->ref_is_hard = false;
344 141 : op->ref_is_family = true;
345 141 : op->refobjid = opfamilyoid;
346 141 : break;
981 tgl 347 UBC 0 : default:
348 0 : ereport(ERROR,
349 : (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
350 : errmsg("support function number %d is invalid for access method %s",
351 : op->number, "gist")));
352 : break;
353 : }
354 : }
981 tgl 355 CBC 97 : }
|