Line data Source code
1 : /*-------------------------------------------------------------------------
2 : *
3 : * ginvalidate.c
4 : * Opclass validator for GIN.
5 : *
6 : * Portions Copyright (c) 1996-2017, PostgreSQL Global Development Group
7 : * Portions Copyright (c) 1994, Regents of the University of California
8 : *
9 : * IDENTIFICATION
10 : * src/backend/access/gin/ginvalidate.c
11 : *
12 : *-------------------------------------------------------------------------
13 : */
14 : #include "postgres.h"
15 :
16 : #include "access/amvalidate.h"
17 : #include "access/gin_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/syscache.h"
27 : #include "utils/regproc.h"
28 :
29 :
30 : /*
31 : * Validator for a GIN opclass.
32 : */
33 : bool
34 4 : ginvalidate(Oid opclassoid)
35 : {
36 4 : 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 */
54 4 : classtup = SearchSysCache1(CLAOID, ObjectIdGetDatum(opclassoid));
55 4 : if (!HeapTupleIsValid(classtup))
56 0 : elog(ERROR, "cache lookup failed for operator class %u", opclassoid);
57 4 : classform = (Form_pg_opclass) GETSTRUCT(classtup);
58 :
59 4 : opfamilyoid = classform->opcfamily;
60 4 : opcintype = classform->opcintype;
61 4 : opckeytype = classform->opckeytype;
62 4 : if (!OidIsValid(opckeytype))
63 0 : opckeytype = opcintype;
64 4 : opclassname = NameStr(classform->opcname);
65 :
66 : /* Fetch opfamily information */
67 4 : familytup = SearchSysCache1(OPFAMILYOID, ObjectIdGetDatum(opfamilyoid));
68 4 : if (!HeapTupleIsValid(familytup))
69 0 : elog(ERROR, "cache lookup failed for operator family %u", opfamilyoid);
70 4 : familyform = (Form_pg_opfamily) GETSTRUCT(familytup);
71 :
72 4 : opfamilyname = NameStr(familyform->opfname);
73 :
74 : /* Fetch all operators and support functions of the opfamily */
75 4 : oprlist = SearchSysCacheList1(AMOPSTRATEGY, ObjectIdGetDatum(opfamilyoid));
76 4 : proclist = SearchSysCacheList1(AMPROCNUM, ObjectIdGetDatum(opfamilyoid));
77 :
78 : /* Check individual support functions */
79 24 : for (i = 0; i < proclist->n_members; i++)
80 : {
81 20 : HeapTuple proctup = &proclist->members[i]->tuple;
82 20 : Form_pg_amproc procform = (Form_pg_amproc) GETSTRUCT(proctup);
83 : bool ok;
84 :
85 : /*
86 : * All GIN support functions should be registered with matching
87 : * left/right types
88 : */
89 20 : if (procform->amproclefttype != procform->amprocrighttype)
90 : {
91 0 : ereport(INFO,
92 : (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
93 : errmsg("operator family \"%s\" of access method %s contains support procedure %s with different left and right input types",
94 : opfamilyname, "gin",
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 : */
103 20 : if (procform->amproclefttype != opcintype)
104 0 : continue;
105 :
106 : /* Check procedure numbers and function signatures */
107 20 : switch (procform->amprocnum)
108 : {
109 : case GIN_COMPARE_PROC:
110 3 : ok = check_amproc_signature(procform->amproc, INT4OID, false,
111 : 2, 2, opckeytype, opckeytype);
112 3 : break;
113 : case GIN_EXTRACTVALUE_PROC:
114 : /* Some opclasses omit nullFlags */
115 4 : ok = check_amproc_signature(procform->amproc, INTERNALOID, false,
116 : 2, 3, opcintype, INTERNALOID,
117 : INTERNALOID);
118 4 : break;
119 : case GIN_EXTRACTQUERY_PROC:
120 : /* Some opclasses omit nullFlags and searchMode */
121 4 : ok = check_amproc_signature(procform->amproc, INTERNALOID, false,
122 : 5, 7, opcintype, INTERNALOID,
123 : INT2OID, INTERNALOID, INTERNALOID,
124 : INTERNALOID, INTERNALOID);
125 4 : break;
126 : case GIN_CONSISTENT_PROC:
127 : /* Some opclasses omit queryKeys and nullFlags */
128 4 : ok = check_amproc_signature(procform->amproc, BOOLOID, false,
129 : 6, 8, INTERNALOID, INT2OID,
130 : opcintype, INT4OID,
131 : INTERNALOID, INTERNALOID,
132 : INTERNALOID, INTERNALOID);
133 4 : break;
134 : case GIN_COMPARE_PARTIAL_PROC:
135 1 : ok = check_amproc_signature(procform->amproc, INT4OID, false,
136 : 4, 4, opckeytype, opckeytype,
137 : INT2OID, INTERNALOID);
138 1 : break;
139 : case GIN_TRICONSISTENT_PROC:
140 4 : ok = check_amproc_signature(procform->amproc, CHAROID, false,
141 : 7, 7, INTERNALOID, INT2OID,
142 : opcintype, INT4OID,
143 : INTERNALOID, INTERNALOID,
144 : INTERNALOID);
145 4 : break;
146 : default:
147 0 : ereport(INFO,
148 : (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
149 : errmsg("operator family \"%s\" of access method %s contains function %s with invalid support number %d",
150 : opfamilyname, "gin",
151 : format_procedure(procform->amproc),
152 : procform->amprocnum)));
153 0 : result = false;
154 0 : continue; /* don't want additional message */
155 : }
156 :
157 20 : if (!ok)
158 : {
159 0 : ereport(INFO,
160 : (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
161 : errmsg("operator family \"%s\" of access method %s contains function %s with wrong signature for support number %d",
162 : opfamilyname, "gin",
163 : format_procedure(procform->amproc),
164 : procform->amprocnum)));
165 0 : result = false;
166 : }
167 : }
168 :
169 : /* Check individual operators */
170 15 : for (i = 0; i < oprlist->n_members; i++)
171 : {
172 11 : HeapTuple oprtup = &oprlist->members[i]->tuple;
173 11 : Form_pg_amop oprform = (Form_pg_amop) GETSTRUCT(oprtup);
174 :
175 : /* TODO: Check that only allowed strategy numbers exist */
176 11 : if (oprform->amopstrategy < 1 || oprform->amopstrategy > 63)
177 : {
178 0 : ereport(INFO,
179 : (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
180 : errmsg("operator family \"%s\" of access method %s contains operator %s with invalid strategy number %d",
181 : opfamilyname, "gin",
182 : format_operator(oprform->amopopr),
183 : oprform->amopstrategy)));
184 0 : result = false;
185 : }
186 :
187 : /* gin doesn't support ORDER BY operators */
188 22 : if (oprform->amoppurpose != AMOP_SEARCH ||
189 11 : OidIsValid(oprform->amopsortfamily))
190 : {
191 0 : ereport(INFO,
192 : (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
193 : errmsg("operator family \"%s\" of access method %s contains invalid ORDER BY specification for operator %s",
194 : opfamilyname, "gin",
195 : format_operator(oprform->amopopr))));
196 0 : result = false;
197 : }
198 :
199 : /* Check operator signature --- same for all gin strategies */
200 11 : if (!check_amop_signature(oprform->amopopr, BOOLOID,
201 : oprform->amoplefttype,
202 : oprform->amoprighttype))
203 : {
204 0 : ereport(INFO,
205 : (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
206 : errmsg("operator family \"%s\" of access method %s contains operator %s with wrong signature",
207 : opfamilyname, "gin",
208 : format_operator(oprform->amopopr))));
209 0 : result = false;
210 : }
211 : }
212 :
213 : /* Now check for inconsistent groups of operators/functions */
214 4 : grouplist = identify_opfamily_groups(oprlist, proclist);
215 4 : opclassgroup = NULL;
216 11 : foreach(lc, grouplist)
217 : {
218 7 : OpFamilyOpFuncGroup *thisgroup = (OpFamilyOpFuncGroup *) lfirst(lc);
219 :
220 : /* Remember the group exactly matching the test opclass */
221 14 : if (thisgroup->lefttype == opcintype &&
222 7 : thisgroup->righttype == opcintype)
223 4 : opclassgroup = thisgroup;
224 :
225 : /*
226 : * There is not a lot we can do to check the operator sets, since each
227 : * GIN opclass is more or less a law unto itself, and some contain
228 : * only operators that are binary-compatible with the opclass datatype
229 : * (meaning that empty operator sets can be OK). That case also means
230 : * that we shouldn't insist on nonempty function sets except for the
231 : * opclass's own group.
232 : */
233 : }
234 :
235 : /* Check that the originally-named opclass is complete */
236 28 : for (i = 1; i <= GINNProcs; i++)
237 : {
238 48 : if (opclassgroup &&
239 24 : (opclassgroup->functionset & (((uint64) 1) << i)) != 0)
240 20 : continue; /* got it */
241 4 : if (i == GIN_COMPARE_PROC || i == GIN_COMPARE_PARTIAL_PROC)
242 4 : continue; /* optional method */
243 0 : if (i == GIN_CONSISTENT_PROC || i == GIN_TRICONSISTENT_PROC)
244 0 : continue; /* don't need both, see check below loop */
245 0 : ereport(INFO,
246 : (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
247 : errmsg("operator class \"%s\" of access method %s is missing support function %d",
248 : opclassname, "gin", i)));
249 0 : result = false;
250 : }
251 8 : if (!opclassgroup ||
252 4 : ((opclassgroup->functionset & (1 << GIN_CONSISTENT_PROC)) == 0 &&
253 0 : (opclassgroup->functionset & (1 << GIN_TRICONSISTENT_PROC)) == 0))
254 : {
255 0 : ereport(INFO,
256 : (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
257 : errmsg("operator class \"%s\" of access method %s is missing support function %d or %d",
258 : opclassname, "gin",
259 : GIN_CONSISTENT_PROC, GIN_TRICONSISTENT_PROC)));
260 0 : result = false;
261 : }
262 :
263 :
264 4 : ReleaseCatCacheList(proclist);
265 4 : ReleaseCatCacheList(oprlist);
266 4 : ReleaseSysCache(familytup);
267 4 : ReleaseSysCache(classtup);
268 :
269 4 : return result;
270 : }
|