PostgreSQL Source Code git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
gistvalidate.c File Reference
#include "postgres.h"
#include "access/amvalidate.h"
#include "access/gist_private.h"
#include "access/htup_details.h"
#include "catalog/pg_amop.h"
#include "catalog/pg_amproc.h"
#include "catalog/pg_opclass.h"
#include "catalog/pg_type.h"
#include "utils/lsyscache.h"
#include "utils/regproc.h"
#include "utils/syscache.h"
Include dependency graph for gistvalidate.c:

Go to the source code of this file.

Functions

bool gistvalidate (Oid opclassoid)
 
void gistadjustmembers (Oid opfamilyoid, Oid opclassoid, List *operators, List *functions)
 

Function Documentation

◆ gistadjustmembers()

void gistadjustmembers ( Oid  opfamilyoid,
Oid  opclassoid,
List operators,
List functions 
)

Definition at line 288 of file gistvalidate.c.

292{
293 ListCell *lc;
294
295 /*
296 * Operator members of a GiST opfamily should never have hard
297 * dependencies, since their connection to the opfamily depends only on
298 * what the support functions think, and that can be altered. For
299 * consistency, we make all soft dependencies point to the opfamily,
300 * though a soft dependency on the opclass would work as well in the
301 * CREATE OPERATOR CLASS case.
302 */
303 foreach(lc, operators)
304 {
306
307 op->ref_is_hard = false;
308 op->ref_is_family = true;
309 op->refobjid = opfamilyoid;
310 }
311
312 /*
313 * Required support functions should have hard dependencies. Preferably
314 * those are just dependencies on the opclass, but if we're in ALTER
315 * OPERATOR FAMILY, we leave the dependency pointing at the whole
316 * opfamily. (Given that GiST opclasses generally don't share opfamilies,
317 * it seems unlikely to be worth working harder.)
318 */
319 foreach(lc, functions)
320 {
322
323 switch (op->number)
324 {
326 case GIST_UNION_PROC:
329 case GIST_EQUAL_PROC:
330 /* Required support function */
331 op->ref_is_hard = true;
332 break;
336 case GIST_FETCH_PROC:
340 /* Optional, so force it to be a soft family dependency */
341 op->ref_is_hard = false;
342 op->ref_is_family = true;
343 op->refobjid = opfamilyoid;
344 break;
345 default:
347 (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
348 errmsg("support function number %d is invalid for access method %s",
349 op->number, "gist")));
350 break;
351 }
352 }
353}
int errcode(int sqlerrcode)
Definition: elog.c:853
int errmsg(const char *fmt,...)
Definition: elog.c:1070
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:149
#define GIST_STRATNUM_PROC
Definition: gist.h:43
#define GIST_DECOMPRESS_PROC
Definition: gist.h:35
#define GIST_PICKSPLIT_PROC
Definition: gist.h:37
#define GIST_CONSISTENT_PROC
Definition: gist.h:32
#define GIST_UNION_PROC
Definition: gist.h:33
#define GIST_FETCH_PROC
Definition: gist.h:40
#define GIST_SORTSUPPORT_PROC
Definition: gist.h:42
#define GIST_COMPRESS_PROC
Definition: gist.h:34
#define GIST_PENALTY_PROC
Definition: gist.h:36
#define GIST_OPTIONS_PROC
Definition: gist.h:41
#define GIST_DISTANCE_PROC
Definition: gist.h:39
#define GIST_EQUAL_PROC
Definition: gist.h:38
#define lfirst(lc)
Definition: pg_list.h:172
static const struct fns functions
Definition: regcomp.c:358
Oid refobjid
Definition: amapi.h:96
bool ref_is_family
Definition: amapi.h:95
int number
Definition: amapi.h:90
bool ref_is_hard
Definition: amapi.h:94

References ereport, errcode(), errmsg(), ERROR, functions, GIST_COMPRESS_PROC, GIST_CONSISTENT_PROC, GIST_DECOMPRESS_PROC, GIST_DISTANCE_PROC, GIST_EQUAL_PROC, GIST_FETCH_PROC, GIST_OPTIONS_PROC, GIST_PENALTY_PROC, GIST_PICKSPLIT_PROC, GIST_SORTSUPPORT_PROC, GIST_STRATNUM_PROC, GIST_UNION_PROC, lfirst, OpFamilyMember::number, OpFamilyMember::ref_is_family, OpFamilyMember::ref_is_hard, and OpFamilyMember::refobjid.

Referenced by gisthandler().

◆ gistvalidate()

bool gistvalidate ( Oid  opclassoid)

Definition at line 32 of file gistvalidate.c.

33{
34 bool result = true;
35 HeapTuple classtup;
36 Form_pg_opclass classform;
37 Oid opfamilyoid;
38 Oid opcintype;
39 Oid opckeytype;
40 char *opclassname;
41 char *opfamilyname;
42 CatCList *proclist,
43 *oprlist;
44 List *grouplist;
45 OpFamilyOpFuncGroup *opclassgroup;
46 int i;
47 ListCell *lc;
48
49 /* Fetch opclass information */
50 classtup = SearchSysCache1(CLAOID, ObjectIdGetDatum(opclassoid));
51 if (!HeapTupleIsValid(classtup))
52 elog(ERROR, "cache lookup failed for operator class %u", opclassoid);
53 classform = (Form_pg_opclass) GETSTRUCT(classtup);
54
55 opfamilyoid = classform->opcfamily;
56 opcintype = classform->opcintype;
57 opckeytype = classform->opckeytype;
58 if (!OidIsValid(opckeytype))
59 opckeytype = opcintype;
60 opclassname = NameStr(classform->opcname);
61
62 /* Fetch opfamily information */
63 opfamilyname = get_opfamily_name(opfamilyoid, false);
64
65 /* Fetch all operators and support functions of the opfamily */
66 oprlist = SearchSysCacheList1(AMOPSTRATEGY, ObjectIdGetDatum(opfamilyoid));
67 proclist = SearchSysCacheList1(AMPROCNUM, ObjectIdGetDatum(opfamilyoid));
68
69 /* Check individual support functions */
70 for (i = 0; i < proclist->n_members; i++)
71 {
72 HeapTuple proctup = &proclist->members[i]->tuple;
73 Form_pg_amproc procform = (Form_pg_amproc) GETSTRUCT(proctup);
74 bool ok;
75
76 /*
77 * All GiST support functions should be registered with matching
78 * left/right types
79 */
80 if (procform->amproclefttype != procform->amprocrighttype)
81 {
83 (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
84 errmsg("operator family \"%s\" of access method %s contains support function %s with different left and right input types",
85 opfamilyname, "gist",
86 format_procedure(procform->amproc))));
87 result = false;
88 }
89
90 /*
91 * We can't check signatures except within the specific opclass, since
92 * we need to know the associated opckeytype in many cases.
93 */
94 if (procform->amproclefttype != opcintype)
95 continue;
96
97 /* Check procedure numbers and function signatures */
98 switch (procform->amprocnum)
99 {
101 ok = check_amproc_signature(procform->amproc, BOOLOID, false,
102 5, 5, INTERNALOID, opcintype,
103 INT2OID, OIDOID, INTERNALOID);
104 break;
105 case GIST_UNION_PROC:
106 ok = check_amproc_signature(procform->amproc, opckeytype, false,
107 2, 2, INTERNALOID, INTERNALOID);
108 break;
111 case GIST_FETCH_PROC:
112 ok = check_amproc_signature(procform->amproc, INTERNALOID, true,
113 1, 1, INTERNALOID);
114 break;
116 ok = check_amproc_signature(procform->amproc, INTERNALOID, true,
117 3, 3, INTERNALOID,
118 INTERNALOID, INTERNALOID);
119 break;
121 ok = check_amproc_signature(procform->amproc, INTERNALOID, true,
122 2, 2, INTERNALOID, INTERNALOID);
123 break;
124 case GIST_EQUAL_PROC:
125 ok = check_amproc_signature(procform->amproc, INTERNALOID, false,
126 3, 3, opckeytype, opckeytype,
127 INTERNALOID);
128 break;
130 ok = check_amproc_signature(procform->amproc, FLOAT8OID, false,
131 5, 5, INTERNALOID, opcintype,
132 INT2OID, OIDOID, INTERNALOID);
133 break;
135 ok = check_amoptsproc_signature(procform->amproc);
136 break;
138 ok = check_amproc_signature(procform->amproc, VOIDOID, true,
139 1, 1, INTERNALOID);
140 break;
142 ok = check_amproc_signature(procform->amproc, INT2OID, true,
143 1, 1, INT4OID) &&
144 procform->amproclefttype == ANYOID &&
145 procform->amprocrighttype == ANYOID;
146 break;
147 default:
149 (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
150 errmsg("operator family \"%s\" of access method %s contains function %s with invalid support number %d",
151 opfamilyname, "gist",
152 format_procedure(procform->amproc),
153 procform->amprocnum)));
154 result = false;
155 continue; /* don't want additional message */
156 }
157
158 if (!ok)
159 {
161 (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
162 errmsg("operator family \"%s\" of access method %s contains function %s with wrong signature for support number %d",
163 opfamilyname, "gist",
164 format_procedure(procform->amproc),
165 procform->amprocnum)));
166 result = false;
167 }
168 }
169
170 /* Check individual operators */
171 for (i = 0; i < oprlist->n_members; i++)
172 {
173 HeapTuple oprtup = &oprlist->members[i]->tuple;
174 Form_pg_amop oprform = (Form_pg_amop) GETSTRUCT(oprtup);
175 Oid op_rettype;
176
177 /* TODO: Check that only allowed strategy numbers exist */
178 if (oprform->amopstrategy < 1)
179 {
181 (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
182 errmsg("operator family \"%s\" of access method %s contains operator %s with invalid strategy number %d",
183 opfamilyname, "gist",
184 format_operator(oprform->amopopr),
185 oprform->amopstrategy)));
186 result = false;
187 }
188
189 /* GiST supports ORDER BY operators */
190 if (oprform->amoppurpose != AMOP_SEARCH)
191 {
192 /* ... but must have matching distance proc */
193 if (!OidIsValid(get_opfamily_proc(opfamilyoid,
194 oprform->amoplefttype,
195 oprform->amoplefttype,
197 {
199 (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
200 errmsg("operator family \"%s\" of access method %s contains unsupported ORDER BY specification for operator %s",
201 opfamilyname, "gist",
202 format_operator(oprform->amopopr))));
203 result = false;
204 }
205 /* ... and operator result must match the claimed btree opfamily */
206 op_rettype = get_op_rettype(oprform->amopopr);
207 if (!opfamily_can_sort_type(oprform->amopsortfamily, op_rettype))
208 {
210 (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
211 errmsg("operator family \"%s\" of access method %s contains incorrect ORDER BY opfamily specification for operator %s",
212 opfamilyname, "gist",
213 format_operator(oprform->amopopr))));
214 result = false;
215 }
216 }
217 else
218 {
219 /* Search operators must always return bool */
220 op_rettype = BOOLOID;
221 }
222
223 /* Check operator signature */
224 if (!check_amop_signature(oprform->amopopr, op_rettype,
225 oprform->amoplefttype,
226 oprform->amoprighttype))
227 {
229 (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
230 errmsg("operator family \"%s\" of access method %s contains operator %s with wrong signature",
231 opfamilyname, "gist",
232 format_operator(oprform->amopopr))));
233 result = false;
234 }
235 }
236
237 /* Now check for inconsistent groups of operators/functions */
238 grouplist = identify_opfamily_groups(oprlist, proclist);
239 opclassgroup = NULL;
240 foreach(lc, grouplist)
241 {
243
244 /* Remember the group exactly matching the test opclass */
245 if (thisgroup->lefttype == opcintype &&
246 thisgroup->righttype == opcintype)
247 opclassgroup = thisgroup;
248
249 /*
250 * There is not a lot we can do to check the operator sets, since each
251 * GiST opclass is more or less a law unto itself, and some contain
252 * only operators that are binary-compatible with the opclass datatype
253 * (meaning that empty operator sets can be OK). That case also means
254 * that we shouldn't insist on nonempty function sets except for the
255 * opclass's own group.
256 */
257 }
258
259 /* Check that the originally-named opclass is complete */
260 for (i = 1; i <= GISTNProcs; i++)
261 {
262 if (opclassgroup &&
263 (opclassgroup->functionset & (((uint64) 1) << i)) != 0)
264 continue; /* got it */
265 if (i == GIST_DISTANCE_PROC || i == GIST_FETCH_PROC ||
269 continue; /* optional methods */
271 (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
272 errmsg("operator class \"%s\" of access method %s is missing support function %d",
273 opclassname, "gist", i)));
274 result = false;
275 }
276
277 ReleaseCatCacheList(proclist);
278 ReleaseCatCacheList(oprlist);
279 ReleaseSysCache(classtup);
280
281 return result;
282}
bool check_amproc_signature(Oid funcid, Oid restype, bool exact, int minargs, int maxargs,...)
Definition: amvalidate.c:152
bool check_amop_signature(Oid opno, Oid restype, Oid lefttype, Oid righttype)
Definition: amvalidate.c:206
List * identify_opfamily_groups(CatCList *oprlist, CatCList *proclist)
Definition: amvalidate.c:43
bool opfamily_can_sort_type(Oid opfamilyoid, Oid datatypeoid)
Definition: amvalidate.c:271
bool check_amoptsproc_signature(Oid funcid)
Definition: amvalidate.c:192
#define NameStr(name)
Definition: c.h:717
uint64_t uint64
Definition: c.h:503
#define OidIsValid(objectId)
Definition: c.h:746
void ReleaseCatCacheList(CatCList *list)
Definition: catcache.c:2071
#define elog(elevel,...)
Definition: elog.h:225
#define INFO
Definition: elog.h:34
#define GISTNProcs
Definition: gist.h:44
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
static void * GETSTRUCT(const HeapTupleData *tuple)
Definition: htup_details.h:728
int i
Definition: isn.c:72
Oid get_opfamily_proc(Oid opfamily, Oid lefttype, Oid righttype, int16 procnum)
Definition: lsyscache.c:809
Oid get_op_rettype(Oid opno)
Definition: lsyscache.c:1372
char * get_opfamily_name(Oid opfid, bool missing_ok)
Definition: lsyscache.c:1292
FormData_pg_amop * Form_pg_amop
Definition: pg_amop.h:88
FormData_pg_amproc * Form_pg_amproc
Definition: pg_amproc.h:68
FormData_pg_opclass * Form_pg_opclass
Definition: pg_opclass.h:83
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:257
unsigned int Oid
Definition: postgres_ext.h:32
char * format_procedure(Oid procedure_oid)
Definition: regproc.c:299
char * format_operator(Oid operator_oid)
Definition: regproc.c:793
Definition: pg_list.h:54
CatCTup * members[FLEXIBLE_ARRAY_MEMBER]
Definition: catcache.h:180
int n_members
Definition: catcache.h:178
HeapTupleData tuple
Definition: catcache.h:123
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:269
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:221
#define SearchSysCacheList1(cacheId, key1)
Definition: syscache.h:127

References check_amop_signature(), check_amoptsproc_signature(), check_amproc_signature(), elog, ereport, errcode(), errmsg(), ERROR, format_operator(), format_procedure(), OpFamilyOpFuncGroup::functionset, get_op_rettype(), get_opfamily_name(), get_opfamily_proc(), GETSTRUCT(), GIST_COMPRESS_PROC, GIST_CONSISTENT_PROC, GIST_DECOMPRESS_PROC, GIST_DISTANCE_PROC, GIST_EQUAL_PROC, GIST_FETCH_PROC, GIST_OPTIONS_PROC, GIST_PENALTY_PROC, GIST_PICKSPLIT_PROC, GIST_SORTSUPPORT_PROC, GIST_STRATNUM_PROC, GIST_UNION_PROC, GISTNProcs, HeapTupleIsValid, i, identify_opfamily_groups(), INFO, OpFamilyOpFuncGroup::lefttype, lfirst, catclist::members, catclist::n_members, NameStr, ObjectIdGetDatum(), OidIsValid, opfamily_can_sort_type(), ReleaseCatCacheList(), ReleaseSysCache(), OpFamilyOpFuncGroup::righttype, SearchSysCache1(), SearchSysCacheList1, and catctup::tuple.

Referenced by gisthandler().