Line data Source code
1 : /*-------------------------------------------------------------------------
2 : *
3 : * brin_validate.c
4 : * Opclass validator for BRIN.
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/brin/brin_validate.c
11 : *
12 : *-------------------------------------------------------------------------
13 : */
14 : #include "postgres.h"
15 :
16 : #include "access/amvalidate.h"
17 : #include "access/brin_internal.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/syscache.h"
26 : #include "utils/regproc.h"
27 :
28 :
29 : /*
30 : * Validator for a BRIN opclass.
31 : *
32 : * Some of the checks done here cover the whole opfamily, and therefore are
33 : * redundant when checking each opclass in a family. But they don't run long
34 : * enough to be much of a problem, so we accept the duplication rather than
35 : * complicate the amvalidate API.
36 : */
37 : bool
38 31 : brinvalidate(Oid opclassoid)
39 : {
40 31 : bool result = true;
41 : HeapTuple classtup;
42 : Form_pg_opclass classform;
43 : Oid opfamilyoid;
44 : Oid opcintype;
45 : char *opclassname;
46 : HeapTuple familytup;
47 : Form_pg_opfamily familyform;
48 : char *opfamilyname;
49 : CatCList *proclist,
50 : *oprlist;
51 31 : uint64 allfuncs = 0;
52 31 : uint64 allops = 0;
53 : List *grouplist;
54 : OpFamilyOpFuncGroup *opclassgroup;
55 : int i;
56 : ListCell *lc;
57 :
58 : /* Fetch opclass information */
59 31 : classtup = SearchSysCache1(CLAOID, ObjectIdGetDatum(opclassoid));
60 31 : if (!HeapTupleIsValid(classtup))
61 0 : elog(ERROR, "cache lookup failed for operator class %u", opclassoid);
62 31 : classform = (Form_pg_opclass) GETSTRUCT(classtup);
63 :
64 31 : opfamilyoid = classform->opcfamily;
65 31 : opcintype = classform->opcintype;
66 31 : opclassname = NameStr(classform->opcname);
67 :
68 : /* Fetch opfamily information */
69 31 : familytup = SearchSysCache1(OPFAMILYOID, ObjectIdGetDatum(opfamilyoid));
70 31 : if (!HeapTupleIsValid(familytup))
71 0 : elog(ERROR, "cache lookup failed for operator family %u", opfamilyoid);
72 31 : familyform = (Form_pg_opfamily) GETSTRUCT(familytup);
73 :
74 31 : opfamilyname = NameStr(familyform->opfname);
75 :
76 : /* Fetch all operators and support functions of the opfamily */
77 31 : oprlist = SearchSysCacheList1(AMOPSTRATEGY, ObjectIdGetDatum(opfamilyoid));
78 31 : proclist = SearchSysCacheList1(AMPROCNUM, ObjectIdGetDatum(opfamilyoid));
79 :
80 : /* Check individual support functions */
81 379 : for (i = 0; i < proclist->n_members; i++)
82 : {
83 348 : HeapTuple proctup = &proclist->members[i]->tuple;
84 348 : Form_pg_amproc procform = (Form_pg_amproc) GETSTRUCT(proctup);
85 : bool ok;
86 :
87 : /* Check procedure numbers and function signatures */
88 348 : switch (procform->amprocnum)
89 : {
90 : case BRIN_PROCNUM_OPCINFO:
91 85 : ok = check_amproc_signature(procform->amproc, INTERNALOID, true,
92 : 1, 1, INTERNALOID);
93 85 : break;
94 : case BRIN_PROCNUM_ADDVALUE:
95 85 : ok = check_amproc_signature(procform->amproc, BOOLOID, true,
96 : 4, 4, INTERNALOID, INTERNALOID,
97 : INTERNALOID, INTERNALOID);
98 85 : break;
99 : case BRIN_PROCNUM_CONSISTENT:
100 85 : ok = check_amproc_signature(procform->amproc, BOOLOID, true,
101 : 3, 3, INTERNALOID, INTERNALOID,
102 : INTERNALOID);
103 85 : break;
104 : case BRIN_PROCNUM_UNION:
105 85 : ok = check_amproc_signature(procform->amproc, BOOLOID, true,
106 : 3, 3, INTERNALOID, INTERNALOID,
107 : INTERNALOID);
108 85 : break;
109 : default:
110 : /* Complain if it's not a valid optional proc number */
111 16 : if (procform->amprocnum < BRIN_FIRST_OPTIONAL_PROCNUM ||
112 8 : procform->amprocnum > BRIN_LAST_OPTIONAL_PROCNUM)
113 : {
114 0 : ereport(INFO,
115 : (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
116 : errmsg("operator family \"%s\" of access method %s contains function %s with invalid support number %d",
117 : opfamilyname, "brin",
118 : format_procedure(procform->amproc),
119 : procform->amprocnum)));
120 0 : result = false;
121 0 : continue; /* omit bad proc numbers from allfuncs */
122 : }
123 : /* Can't check signatures of optional procs, so assume OK */
124 8 : ok = true;
125 8 : break;
126 : }
127 :
128 348 : if (!ok)
129 : {
130 0 : ereport(INFO,
131 : (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
132 : errmsg("operator family \"%s\" of access method %s contains function %s with wrong signature for support number %d",
133 : opfamilyname, "brin",
134 : format_procedure(procform->amproc),
135 : procform->amprocnum)));
136 0 : result = false;
137 : }
138 :
139 : /* Track all valid procedure numbers seen in opfamily */
140 348 : allfuncs |= ((uint64) 1) << procform->amprocnum;
141 : }
142 :
143 : /* Check individual operators */
144 474 : for (i = 0; i < oprlist->n_members; i++)
145 : {
146 443 : HeapTuple oprtup = &oprlist->members[i]->tuple;
147 443 : Form_pg_amop oprform = (Form_pg_amop) GETSTRUCT(oprtup);
148 :
149 : /* Check that only allowed strategy numbers exist */
150 443 : if (oprform->amopstrategy < 1 || oprform->amopstrategy > 63)
151 : {
152 0 : ereport(INFO,
153 : (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
154 : errmsg("operator family \"%s\" of access method %s contains operator %s with invalid strategy number %d",
155 : opfamilyname, "brin",
156 : format_operator(oprform->amopopr),
157 : oprform->amopstrategy)));
158 0 : result = false;
159 : }
160 : else
161 : {
162 : /*
163 : * The set of operators supplied varies across BRIN opfamilies.
164 : * Our plan is to identify all operator strategy numbers used in
165 : * the opfamily and then complain about datatype combinations that
166 : * are missing any operator(s). However, consider only numbers
167 : * that appear in some non-cross-type case, since cross-type
168 : * operators may have unique strategies. (This is not a great
169 : * heuristic, in particular an erroneous number used in a
170 : * cross-type operator will not get noticed; but the core BRIN
171 : * opfamilies are messy enough to make it necessary.)
172 : */
173 443 : if (oprform->amoplefttype == oprform->amoprighttype)
174 241 : allops |= ((uint64) 1) << oprform->amopstrategy;
175 : }
176 :
177 : /* brin doesn't support ORDER BY operators */
178 886 : if (oprform->amoppurpose != AMOP_SEARCH ||
179 443 : OidIsValid(oprform->amopsortfamily))
180 : {
181 0 : ereport(INFO,
182 : (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
183 : errmsg("operator family \"%s\" of access method %s contains invalid ORDER BY specification for operator %s",
184 : opfamilyname, "brin",
185 : format_operator(oprform->amopopr))));
186 0 : result = false;
187 : }
188 :
189 : /* Check operator signature --- same for all brin strategies */
190 443 : if (!check_amop_signature(oprform->amopopr, BOOLOID,
191 : oprform->amoplefttype,
192 : oprform->amoprighttype))
193 : {
194 0 : ereport(INFO,
195 : (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
196 : errmsg("operator family \"%s\" of access method %s contains operator %s with wrong signature",
197 : opfamilyname, "brin",
198 : format_operator(oprform->amopopr))));
199 0 : result = false;
200 : }
201 : }
202 :
203 : /* Now check for inconsistent groups of operators/functions */
204 31 : grouplist = identify_opfamily_groups(oprlist, proclist);
205 31 : opclassgroup = NULL;
206 118 : foreach(lc, grouplist)
207 : {
208 87 : OpFamilyOpFuncGroup *thisgroup = (OpFamilyOpFuncGroup *) lfirst(lc);
209 :
210 : /* Remember the group exactly matching the test opclass */
211 134 : if (thisgroup->lefttype == opcintype &&
212 47 : thisgroup->righttype == opcintype)
213 31 : opclassgroup = thisgroup;
214 :
215 : /*
216 : * Some BRIN opfamilies expect cross-type support functions to exist,
217 : * and some don't. We don't know exactly which are which, so if we
218 : * find a cross-type operator for which there are no support functions
219 : * at all, let it pass. (Don't expect that all operators exist for
220 : * such cross-type cases, either.)
221 : */
222 89 : if (thisgroup->functionset == 0 &&
223 2 : thisgroup->lefttype != thisgroup->righttype)
224 2 : continue;
225 :
226 : /*
227 : * Else complain if there seems to be an incomplete set of either
228 : * operators or support functions for this datatype pair.
229 : */
230 85 : if (thisgroup->operatorset != allops)
231 : {
232 0 : ereport(INFO,
233 : (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
234 : errmsg("operator family \"%s\" of access method %s is missing operator(s) for types %s and %s",
235 : opfamilyname, "brin",
236 : format_type_be(thisgroup->lefttype),
237 : format_type_be(thisgroup->righttype))));
238 0 : result = false;
239 : }
240 85 : if (thisgroup->functionset != allfuncs)
241 : {
242 0 : ereport(INFO,
243 : (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
244 : errmsg("operator family \"%s\" of access method %s is missing support function(s) for types %s and %s",
245 : opfamilyname, "brin",
246 : format_type_be(thisgroup->lefttype),
247 : format_type_be(thisgroup->righttype))));
248 0 : result = false;
249 : }
250 : }
251 :
252 : /* Check that the originally-named opclass is complete */
253 31 : if (!opclassgroup || opclassgroup->operatorset != allops)
254 : {
255 0 : ereport(INFO,
256 : (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
257 : errmsg("operator class \"%s\" of access method %s is missing operator(s)",
258 : opclassname, "brin")));
259 0 : result = false;
260 : }
261 155 : for (i = 1; i <= BRIN_MANDATORY_NPROCS; i++)
262 : {
263 248 : if (opclassgroup &&
264 124 : (opclassgroup->functionset & (((int64) 1) << i)) != 0)
265 124 : continue; /* got it */
266 0 : ereport(INFO,
267 : (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
268 : errmsg("operator class \"%s\" of access method %s is missing support function %d",
269 : opclassname, "brin", i)));
270 0 : result = false;
271 : }
272 :
273 31 : ReleaseCatCacheList(proclist);
274 31 : ReleaseCatCacheList(oprlist);
275 31 : ReleaseSysCache(familytup);
276 31 : ReleaseSysCache(classtup);
277 :
278 31 : return result;
279 : }
|