PostgreSQL Source Code git master
nbtvalidate.c File Reference
#include "postgres.h"
#include "access/amvalidate.h"
#include "access/htup_details.h"
#include "access/nbtree.h"
#include "access/xact.h"
#include "catalog/pg_am.h"
#include "catalog/pg_amop.h"
#include "catalog/pg_amproc.h"
#include "catalog/pg_opclass.h"
#include "catalog/pg_type.h"
#include "utils/builtins.h"
#include "utils/lsyscache.h"
#include "utils/regproc.h"
#include "utils/syscache.h"
Include dependency graph for nbtvalidate.c:

Go to the source code of this file.

Functions

bool btvalidate (Oid opclassoid)
 
void btadjustmembers (Oid opfamilyoid, Oid opclassoid, List *operators, List *functions)
 

Function Documentation

◆ btadjustmembers()

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

Definition at line 284 of file nbtvalidate.c.

288{
289 Oid opcintype;
290 ListCell *lc;
291
292 /*
293 * Btree operators and comparison support functions are always "loose"
294 * members of the opfamily if they are cross-type. If they are not
295 * cross-type, we prefer to tie them to the appropriate opclass ... but if
296 * the user hasn't created one, we can't do that, and must fall back to
297 * using the opfamily dependency. (We mustn't force creation of an
298 * opclass in such a case, as leaving an incomplete opclass laying about
299 * would be bad. Throwing an error is another undesirable alternative.)
300 *
301 * This behavior results in a bit of a dump/reload hazard, in that the
302 * order of restoring objects could affect what dependencies we end up
303 * with. pg_dump's existing behavior will preserve the dependency choices
304 * in most cases, but not if a cross-type operator has been bound tightly
305 * into an opclass. That's a mistake anyway, so silently "fixing" it
306 * isn't awful.
307 *
308 * Optional support functions are always "loose" family members.
309 *
310 * To avoid repeated lookups, we remember the most recently used opclass's
311 * input type.
312 */
313 if (OidIsValid(opclassoid))
314 {
315 /* During CREATE OPERATOR CLASS, need CCI to see the pg_opclass row */
317 opcintype = get_opclass_input_type(opclassoid);
318 }
319 else
320 opcintype = InvalidOid;
321
322 /*
323 * We handle operators and support functions almost identically, so rather
324 * than duplicate this code block, just join the lists.
325 */
326 foreach(lc, list_concat_copy(operators, functions))
327 {
329
330 if (op->is_func && op->number != BTORDER_PROC)
331 {
332 /* Optional support proc, so always a soft family dependency */
333 op->ref_is_hard = false;
334 op->ref_is_family = true;
335 op->refobjid = opfamilyoid;
336 }
337 else if (op->lefttype != op->righttype)
338 {
339 /* Cross-type, so always a soft family dependency */
340 op->ref_is_hard = false;
341 op->ref_is_family = true;
342 op->refobjid = opfamilyoid;
343 }
344 else
345 {
346 /* Not cross-type; is there a suitable opclass? */
347 if (op->lefttype != opcintype)
348 {
349 /* Avoid repeating this expensive lookup, even if it fails */
350 opcintype = op->lefttype;
351 opclassoid = opclass_for_family_datatype(BTREE_AM_OID,
352 opfamilyoid,
353 opcintype);
354 }
355 if (OidIsValid(opclassoid))
356 {
357 /* Hard dependency on opclass */
358 op->ref_is_hard = true;
359 op->ref_is_family = false;
360 op->refobjid = opclassoid;
361 }
362 else
363 {
364 /* We're stuck, so make a soft dependency on the opfamily */
365 op->ref_is_hard = false;
366 op->ref_is_family = true;
367 op->refobjid = opfamilyoid;
368 }
369 }
370 }
371}
Oid opclass_for_family_datatype(Oid amoid, Oid opfamilyoid, Oid datatypeoid)
Definition: amvalidate.c:236
#define OidIsValid(objectId)
Definition: c.h:746
List * list_concat_copy(const List *list1, const List *list2)
Definition: list.c:598
Oid get_opclass_input_type(Oid opclass)
Definition: lsyscache.c:1247
#define BTORDER_PROC
Definition: nbtree.h:712
#define lfirst(lc)
Definition: pg_list.h:172
#define InvalidOid
Definition: postgres_ext.h:37
unsigned int Oid
Definition: postgres_ext.h:32
static const struct fns functions
Definition: regcomp.c:358
Oid refobjid
Definition: amapi.h:96
Oid lefttype
Definition: amapi.h:91
bool ref_is_family
Definition: amapi.h:95
Oid righttype
Definition: amapi.h:92
int number
Definition: amapi.h:90
bool is_func
Definition: amapi.h:88
bool ref_is_hard
Definition: amapi.h:94
void CommandCounterIncrement(void)
Definition: xact.c:1100

References BTORDER_PROC, CommandCounterIncrement(), functions, get_opclass_input_type(), InvalidOid, OpFamilyMember::is_func, OpFamilyMember::lefttype, lfirst, list_concat_copy(), OpFamilyMember::number, OidIsValid, opclass_for_family_datatype(), OpFamilyMember::ref_is_family, OpFamilyMember::ref_is_hard, OpFamilyMember::refobjid, and OpFamilyMember::righttype.

Referenced by bthandler().

◆ btvalidate()

bool btvalidate ( Oid  opclassoid)

Definition at line 40 of file nbtvalidate.c.

41{
42 bool result = true;
43 HeapTuple classtup;
44 Form_pg_opclass classform;
45 Oid opfamilyoid;
46 Oid opcintype;
47 char *opclassname;
48 char *opfamilyname;
49 CatCList *proclist,
50 *oprlist;
51 List *grouplist;
52 OpFamilyOpFuncGroup *opclassgroup;
53 List *familytypes;
54 int usefulgroups;
55 int i;
56 ListCell *lc;
57
58 /* Fetch opclass information */
59 classtup = SearchSysCache1(CLAOID, ObjectIdGetDatum(opclassoid));
60 if (!HeapTupleIsValid(classtup))
61 elog(ERROR, "cache lookup failed for operator class %u", opclassoid);
62 classform = (Form_pg_opclass) GETSTRUCT(classtup);
63
64 opfamilyoid = classform->opcfamily;
65 opcintype = classform->opcintype;
66 opclassname = NameStr(classform->opcname);
67
68 /* Fetch opfamily information */
69 opfamilyname = get_opfamily_name(opfamilyoid, false);
70
71 /* Fetch all operators and support functions of the opfamily */
72 oprlist = SearchSysCacheList1(AMOPSTRATEGY, ObjectIdGetDatum(opfamilyoid));
73 proclist = SearchSysCacheList1(AMPROCNUM, ObjectIdGetDatum(opfamilyoid));
74
75 /* Check individual support functions */
76 for (i = 0; i < proclist->n_members; i++)
77 {
78 HeapTuple proctup = &proclist->members[i]->tuple;
79 Form_pg_amproc procform = (Form_pg_amproc) GETSTRUCT(proctup);
80 bool ok;
81
82 /* Check procedure numbers and function signatures */
83 switch (procform->amprocnum)
84 {
85 case BTORDER_PROC:
86 ok = check_amproc_signature(procform->amproc, INT4OID, true,
87 2, 2, procform->amproclefttype,
88 procform->amprocrighttype);
89 break;
91 ok = check_amproc_signature(procform->amproc, VOIDOID, true,
92 1, 1, INTERNALOID);
93 break;
94 case BTINRANGE_PROC:
95 ok = check_amproc_signature(procform->amproc, BOOLOID, true,
96 5, 5,
97 procform->amproclefttype,
98 procform->amproclefttype,
99 procform->amprocrighttype,
100 BOOLOID, BOOLOID);
101 break;
103 ok = check_amproc_signature(procform->amproc, BOOLOID, true,
104 1, 1, OIDOID);
105 break;
106 case BTOPTIONS_PROC:
107 ok = check_amoptsproc_signature(procform->amproc);
108 break;
109 default:
111 (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
112 errmsg("operator family \"%s\" of access method %s contains function %s with invalid support number %d",
113 opfamilyname, "btree",
114 format_procedure(procform->amproc),
115 procform->amprocnum)));
116 result = false;
117 continue; /* don't want additional message */
118 }
119
120 if (!ok)
121 {
123 (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
124 errmsg("operator family \"%s\" of access method %s contains function %s with wrong signature for support number %d",
125 opfamilyname, "btree",
126 format_procedure(procform->amproc),
127 procform->amprocnum)));
128 result = false;
129 }
130 }
131
132 /* Check individual operators */
133 for (i = 0; i < oprlist->n_members; i++)
134 {
135 HeapTuple oprtup = &oprlist->members[i]->tuple;
136 Form_pg_amop oprform = (Form_pg_amop) GETSTRUCT(oprtup);
137
138 /* Check that only allowed strategy numbers exist */
139 if (oprform->amopstrategy < 1 ||
140 oprform->amopstrategy > BTMaxStrategyNumber)
141 {
143 (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
144 errmsg("operator family \"%s\" of access method %s contains operator %s with invalid strategy number %d",
145 opfamilyname, "btree",
146 format_operator(oprform->amopopr),
147 oprform->amopstrategy)));
148 result = false;
149 }
150
151 /* btree doesn't support ORDER BY operators */
152 if (oprform->amoppurpose != AMOP_SEARCH ||
153 OidIsValid(oprform->amopsortfamily))
154 {
156 (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
157 errmsg("operator family \"%s\" of access method %s contains invalid ORDER BY specification for operator %s",
158 opfamilyname, "btree",
159 format_operator(oprform->amopopr))));
160 result = false;
161 }
162
163 /* Check operator signature --- same for all btree strategies */
164 if (!check_amop_signature(oprform->amopopr, BOOLOID,
165 oprform->amoplefttype,
166 oprform->amoprighttype))
167 {
169 (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
170 errmsg("operator family \"%s\" of access method %s contains operator %s with wrong signature",
171 opfamilyname, "btree",
172 format_operator(oprform->amopopr))));
173 result = false;
174 }
175 }
176
177 /* Now check for inconsistent groups of operators/functions */
178 grouplist = identify_opfamily_groups(oprlist, proclist);
179 usefulgroups = 0;
180 opclassgroup = NULL;
181 familytypes = NIL;
182 foreach(lc, grouplist)
183 {
185
186 /*
187 * It is possible for an in_range support function to have a RHS type
188 * that is otherwise irrelevant to the opfamily --- for instance, SQL
189 * requires the datetime_ops opclass to have range support with an
190 * interval offset. So, if this group appears to contain only an
191 * in_range function, ignore it: it doesn't represent a pair of
192 * supported types.
193 */
194 if (thisgroup->operatorset == 0 &&
195 thisgroup->functionset == (1 << BTINRANGE_PROC))
196 continue;
197
198 /* Else count it as a relevant group */
199 usefulgroups++;
200
201 /* Remember the group exactly matching the test opclass */
202 if (thisgroup->lefttype == opcintype &&
203 thisgroup->righttype == opcintype)
204 opclassgroup = thisgroup;
205
206 /*
207 * Identify all distinct data types handled in this opfamily. This
208 * implementation is O(N^2), but there aren't likely to be enough
209 * types in the family for it to matter.
210 */
211 familytypes = list_append_unique_oid(familytypes, thisgroup->lefttype);
212 familytypes = list_append_unique_oid(familytypes, thisgroup->righttype);
213
214 /*
215 * Complain if there seems to be an incomplete set of either operators
216 * or support functions for this datatype pair. The sortsupport,
217 * in_range, and equalimage functions are considered optional.
218 */
219 if (thisgroup->operatorset !=
220 ((1 << BTLessStrategyNumber) |
222 (1 << BTEqualStrategyNumber) |
225 {
227 (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
228 errmsg("operator family \"%s\" of access method %s is missing operator(s) for types %s and %s",
229 opfamilyname, "btree",
230 format_type_be(thisgroup->lefttype),
231 format_type_be(thisgroup->righttype))));
232 result = false;
233 }
234 if ((thisgroup->functionset & (1 << BTORDER_PROC)) == 0)
235 {
237 (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
238 errmsg("operator family \"%s\" of access method %s is missing support function for types %s and %s",
239 opfamilyname, "btree",
240 format_type_be(thisgroup->lefttype),
241 format_type_be(thisgroup->righttype))));
242 result = false;
243 }
244 }
245
246 /* Check that the originally-named opclass is supported */
247 /* (if group is there, we already checked it adequately above) */
248 if (!opclassgroup)
249 {
251 (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
252 errmsg("operator class \"%s\" of access method %s is missing operator(s)",
253 opclassname, "btree")));
254 result = false;
255 }
256
257 /*
258 * Complain if the opfamily doesn't have entries for all possible
259 * combinations of its supported datatypes. While missing cross-type
260 * operators are not fatal, they do limit the planner's ability to derive
261 * additional qual clauses from equivalence classes, so it seems
262 * reasonable to insist that all built-in btree opfamilies be complete.
263 */
264 if (usefulgroups != (list_length(familytypes) * list_length(familytypes)))
265 {
267 (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
268 errmsg("operator family \"%s\" of access method %s is missing cross-type operator(s)",
269 opfamilyname, "btree")));
270 result = false;
271 }
272
273 ReleaseCatCacheList(proclist);
274 ReleaseCatCacheList(oprlist);
275 ReleaseSysCache(classtup);
276
277 return result;
278}
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 check_amoptsproc_signature(Oid funcid)
Definition: amvalidate.c:192
#define NameStr(name)
Definition: c.h:717
void ReleaseCatCacheList(CatCList *list)
Definition: catcache.c:2071
int errcode(int sqlerrcode)
Definition: elog.c:853
int errmsg(const char *fmt,...)
Definition: elog.c:1070
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:225
#define INFO
Definition: elog.h:34
#define ereport(elevel,...)
Definition: elog.h:149
char * format_type_be(Oid type_oid)
Definition: format_type.c:343
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
static void * GETSTRUCT(const HeapTupleData *tuple)
Definition: htup_details.h:728
int i
Definition: isn.c:74
List * list_append_unique_oid(List *list, Oid datum)
Definition: list.c:1380
char * get_opfamily_name(Oid opfid, bool missing_ok)
Definition: lsyscache.c:1336
#define BTEQUALIMAGE_PROC
Definition: nbtree.h:715
#define BTSORTSUPPORT_PROC
Definition: nbtree.h:713
#define BTINRANGE_PROC
Definition: nbtree.h:714
#define BTOPTIONS_PROC
Definition: nbtree.h:716
FormData_pg_amop * Form_pg_amop
Definition: pg_amop.h:88
FormData_pg_amproc * Form_pg_amproc
Definition: pg_amproc.h:68
static int list_length(const List *l)
Definition: pg_list.h:152
#define NIL
Definition: pg_list.h:68
FormData_pg_opclass * Form_pg_opclass
Definition: pg_opclass.h:83
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:257
char * format_procedure(Oid procedure_oid)
Definition: regproc.c:299
char * format_operator(Oid operator_oid)
Definition: regproc.c:793
#define BTGreaterStrategyNumber
Definition: stratnum.h:33
#define BTMaxStrategyNumber
Definition: stratnum.h:35
#define BTLessStrategyNumber
Definition: stratnum.h:29
#define BTEqualStrategyNumber
Definition: stratnum.h:31
#define BTLessEqualStrategyNumber
Definition: stratnum.h:30
#define BTGreaterEqualStrategyNumber
Definition: stratnum.h:32
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 BTEQUALIMAGE_PROC, BTEqualStrategyNumber, BTGreaterEqualStrategyNumber, BTGreaterStrategyNumber, BTINRANGE_PROC, BTLessEqualStrategyNumber, BTLessStrategyNumber, BTMaxStrategyNumber, BTOPTIONS_PROC, BTORDER_PROC, BTSORTSUPPORT_PROC, check_amop_signature(), check_amoptsproc_signature(), check_amproc_signature(), elog, ereport, errcode(), errmsg(), ERROR, format_operator(), format_procedure(), format_type_be(), OpFamilyOpFuncGroup::functionset, get_opfamily_name(), GETSTRUCT(), HeapTupleIsValid, i, identify_opfamily_groups(), INFO, OpFamilyOpFuncGroup::lefttype, lfirst, list_append_unique_oid(), list_length(), catclist::members, catclist::n_members, NameStr, NIL, ObjectIdGetDatum(), OidIsValid, OpFamilyOpFuncGroup::operatorset, ReleaseCatCacheList(), ReleaseSysCache(), OpFamilyOpFuncGroup::righttype, SearchSysCache1(), SearchSysCacheList1, and catctup::tuple.

Referenced by bthandler().