PostgreSQL Source Code  git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
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_opfamily.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 293 of file nbtvalidate.c.

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

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 41 of file nbtvalidate.c.

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