1 | /*------------------------------------------------------------------------- |
2 | * |
3 | * ginvalidate.c |
4 | * Opclass validator for GIN. |
5 | * |
6 | * Portions Copyright (c) 1996-2019, 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 | ginvalidate(Oid opclassoid) |
35 | { |
36 | 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 | classtup = SearchSysCache1(CLAOID, ObjectIdGetDatum(opclassoid)); |
55 | if (!HeapTupleIsValid(classtup)) |
56 | elog(ERROR, "cache lookup failed for operator class %u" , opclassoid); |
57 | classform = (Form_pg_opclass) GETSTRUCT(classtup); |
58 | |
59 | opfamilyoid = classform->opcfamily; |
60 | opcintype = classform->opcintype; |
61 | opckeytype = classform->opckeytype; |
62 | if (!OidIsValid(opckeytype)) |
63 | opckeytype = opcintype; |
64 | opclassname = NameStr(classform->opcname); |
65 | |
66 | /* Fetch opfamily information */ |
67 | familytup = SearchSysCache1(OPFAMILYOID, ObjectIdGetDatum(opfamilyoid)); |
68 | if (!HeapTupleIsValid(familytup)) |
69 | elog(ERROR, "cache lookup failed for operator family %u" , opfamilyoid); |
70 | familyform = (Form_pg_opfamily) GETSTRUCT(familytup); |
71 | |
72 | opfamilyname = NameStr(familyform->opfname); |
73 | |
74 | /* Fetch all operators and support functions of the opfamily */ |
75 | oprlist = SearchSysCacheList1(AMOPSTRATEGY, ObjectIdGetDatum(opfamilyoid)); |
76 | proclist = SearchSysCacheList1(AMPROCNUM, ObjectIdGetDatum(opfamilyoid)); |
77 | |
78 | /* Check individual support functions */ |
79 | for (i = 0; i < proclist->n_members; i++) |
80 | { |
81 | HeapTuple proctup = &proclist->members[i]->tuple; |
82 | 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 | if (procform->amproclefttype != procform->amprocrighttype) |
90 | { |
91 | 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, "gin" , |
95 | format_procedure(procform->amproc)))); |
96 | 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 | if (procform->amproclefttype != opcintype) |
104 | continue; |
105 | |
106 | /* Check procedure numbers and function signatures */ |
107 | switch (procform->amprocnum) |
108 | { |
109 | case GIN_COMPARE_PROC: |
110 | ok = check_amproc_signature(procform->amproc, INT4OID, false, |
111 | 2, 2, opckeytype, opckeytype); |
112 | break; |
113 | case GIN_EXTRACTVALUE_PROC: |
114 | /* Some opclasses omit nullFlags */ |
115 | ok = check_amproc_signature(procform->amproc, INTERNALOID, false, |
116 | 2, 3, opcintype, INTERNALOID, |
117 | INTERNALOID); |
118 | break; |
119 | case GIN_EXTRACTQUERY_PROC: |
120 | /* Some opclasses omit nullFlags and searchMode */ |
121 | ok = check_amproc_signature(procform->amproc, INTERNALOID, false, |
122 | 5, 7, opcintype, INTERNALOID, |
123 | INT2OID, INTERNALOID, INTERNALOID, |
124 | INTERNALOID, INTERNALOID); |
125 | break; |
126 | case GIN_CONSISTENT_PROC: |
127 | /* Some opclasses omit queryKeys and nullFlags */ |
128 | ok = check_amproc_signature(procform->amproc, BOOLOID, false, |
129 | 6, 8, INTERNALOID, INT2OID, |
130 | opcintype, INT4OID, |
131 | INTERNALOID, INTERNALOID, |
132 | INTERNALOID, INTERNALOID); |
133 | break; |
134 | case GIN_COMPARE_PARTIAL_PROC: |
135 | ok = check_amproc_signature(procform->amproc, INT4OID, false, |
136 | 4, 4, opckeytype, opckeytype, |
137 | INT2OID, INTERNALOID); |
138 | break; |
139 | case GIN_TRICONSISTENT_PROC: |
140 | ok = check_amproc_signature(procform->amproc, CHAROID, false, |
141 | 7, 7, INTERNALOID, INT2OID, |
142 | opcintype, INT4OID, |
143 | INTERNALOID, INTERNALOID, |
144 | INTERNALOID); |
145 | break; |
146 | default: |
147 | 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 | result = false; |
154 | continue; /* don't want additional message */ |
155 | } |
156 | |
157 | if (!ok) |
158 | { |
159 | 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 | result = false; |
166 | } |
167 | } |
168 | |
169 | /* Check individual operators */ |
170 | for (i = 0; i < oprlist->n_members; i++) |
171 | { |
172 | HeapTuple oprtup = &oprlist->members[i]->tuple; |
173 | Form_pg_amop oprform = (Form_pg_amop) GETSTRUCT(oprtup); |
174 | |
175 | /* TODO: Check that only allowed strategy numbers exist */ |
176 | if (oprform->amopstrategy < 1 || oprform->amopstrategy > 63) |
177 | { |
178 | 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 | result = false; |
185 | } |
186 | |
187 | /* gin doesn't support ORDER BY operators */ |
188 | if (oprform->amoppurpose != AMOP_SEARCH || |
189 | OidIsValid(oprform->amopsortfamily)) |
190 | { |
191 | 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 | result = false; |
197 | } |
198 | |
199 | /* Check operator signature --- same for all gin strategies */ |
200 | if (!check_amop_signature(oprform->amopopr, BOOLOID, |
201 | oprform->amoplefttype, |
202 | oprform->amoprighttype)) |
203 | { |
204 | 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 | result = false; |
210 | } |
211 | } |
212 | |
213 | /* Now check for inconsistent groups of operators/functions */ |
214 | grouplist = identify_opfamily_groups(oprlist, proclist); |
215 | opclassgroup = NULL; |
216 | foreach(lc, grouplist) |
217 | { |
218 | OpFamilyOpFuncGroup *thisgroup = (OpFamilyOpFuncGroup *) lfirst(lc); |
219 | |
220 | /* Remember the group exactly matching the test opclass */ |
221 | if (thisgroup->lefttype == opcintype && |
222 | thisgroup->righttype == opcintype) |
223 | 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 | for (i = 1; i <= GINNProcs; i++) |
237 | { |
238 | if (opclassgroup && |
239 | (opclassgroup->functionset & (((uint64) 1) << i)) != 0) |
240 | continue; /* got it */ |
241 | if (i == GIN_COMPARE_PROC || i == GIN_COMPARE_PARTIAL_PROC) |
242 | continue; /* optional method */ |
243 | if (i == GIN_CONSISTENT_PROC || i == GIN_TRICONSISTENT_PROC) |
244 | continue; /* don't need both, see check below loop */ |
245 | 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 | result = false; |
250 | } |
251 | if (!opclassgroup || |
252 | ((opclassgroup->functionset & (1 << GIN_CONSISTENT_PROC)) == 0 && |
253 | (opclassgroup->functionset & (1 << GIN_TRICONSISTENT_PROC)) == 0)) |
254 | { |
255 | 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 | result = false; |
261 | } |
262 | |
263 | |
264 | ReleaseCatCacheList(proclist); |
265 | ReleaseCatCacheList(oprlist); |
266 | ReleaseSysCache(familytup); |
267 | ReleaseSysCache(classtup); |
268 | |
269 | return result; |
270 | } |
271 | |