PostgreSQL Source Code  git master
gistvalidate.c File Reference
#include "postgres.h"
#include "access/amvalidate.h"
#include "access/gist_private.h"
#include "access/htup_details.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/lsyscache.h"
#include "utils/regproc.h"
#include "utils/syscache.h"
Include dependency graph for gistvalidate.c:

Go to the source code of this file.

Functions

bool gistvalidate (Oid opclassoid)
 
void gistadjustmembers (Oid opfamilyoid, Oid opclassoid, List *operators, List *functions)
 

Function Documentation

◆ gistadjustmembers()

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

Definition at line 295 of file gistvalidate.c.

299 {
300  ListCell *lc;
301 
302  /*
303  * Operator members of a GiST opfamily should never have hard
304  * dependencies, since their connection to the opfamily depends only on
305  * what the support functions think, and that can be altered. For
306  * consistency, we make all soft dependencies point to the opfamily,
307  * though a soft dependency on the opclass would work as well in the
308  * CREATE OPERATOR CLASS case.
309  */
310  foreach(lc, operators)
311  {
312  OpFamilyMember *op = (OpFamilyMember *) lfirst(lc);
313 
314  op->ref_is_hard = false;
315  op->ref_is_family = true;
316  op->refobjid = opfamilyoid;
317  }
318 
319  /*
320  * Required support functions should have hard dependencies. Preferably
321  * those are just dependencies on the opclass, but if we're in ALTER
322  * OPERATOR FAMILY, we leave the dependency pointing at the whole
323  * opfamily. (Given that GiST opclasses generally don't share opfamilies,
324  * it seems unlikely to be worth working harder.)
325  */
326  foreach(lc, functions)
327  {
328  OpFamilyMember *op = (OpFamilyMember *) lfirst(lc);
329 
330  switch (op->number)
331  {
333  case GIST_UNION_PROC:
334  case GIST_PENALTY_PROC:
335  case GIST_PICKSPLIT_PROC:
336  case GIST_EQUAL_PROC:
337  /* Required support function */
338  op->ref_is_hard = true;
339  break;
340  case GIST_COMPRESS_PROC:
342  case GIST_DISTANCE_PROC:
343  case GIST_FETCH_PROC:
344  case GIST_OPTIONS_PROC:
346  case GIST_STRATNUM_PROC:
347  /* Optional, so force it to be a soft family dependency */
348  op->ref_is_hard = false;
349  op->ref_is_family = true;
350  op->refobjid = opfamilyoid;
351  break;
352  default:
353  ereport(ERROR,
354  (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
355  errmsg("support function number %d is invalid for access method %s",
356  op->number, "gist")));
357  break;
358  }
359  }
360 }
int errcode(int sqlerrcode)
Definition: elog.c:853
int errmsg(const char *fmt,...)
Definition: elog.c:1070
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:149
#define GIST_STRATNUM_PROC
Definition: gist.h:42
#define GIST_DECOMPRESS_PROC
Definition: gist.h:34
#define GIST_PICKSPLIT_PROC
Definition: gist.h:36
#define GIST_CONSISTENT_PROC
Definition: gist.h:31
#define GIST_UNION_PROC
Definition: gist.h:32
#define GIST_FETCH_PROC
Definition: gist.h:39
#define GIST_SORTSUPPORT_PROC
Definition: gist.h:41
#define GIST_COMPRESS_PROC
Definition: gist.h:33
#define GIST_PENALTY_PROC
Definition: gist.h:35
#define GIST_OPTIONS_PROC
Definition: gist.h:40
#define GIST_DISTANCE_PROC
Definition: gist.h:38
#define GIST_EQUAL_PROC
Definition: gist.h:37
#define lfirst(lc)
Definition: pg_list.h:172
static const struct fns functions
Definition: regcomp.c:356
Oid refobjid
Definition: amapi.h:90
bool ref_is_family
Definition: amapi.h:89
int number
Definition: amapi.h:84
bool ref_is_hard
Definition: amapi.h:88

References ereport, errcode(), errmsg(), ERROR, functions, GIST_COMPRESS_PROC, GIST_CONSISTENT_PROC, GIST_DECOMPRESS_PROC, GIST_DISTANCE_PROC, GIST_EQUAL_PROC, GIST_FETCH_PROC, GIST_OPTIONS_PROC, GIST_PENALTY_PROC, GIST_PICKSPLIT_PROC, GIST_SORTSUPPORT_PROC, GIST_STRATNUM_PROC, GIST_UNION_PROC, lfirst, OpFamilyMember::number, OpFamilyMember::ref_is_family, OpFamilyMember::ref_is_hard, and OpFamilyMember::refobjid.

Referenced by gisthandler().

◆ gistvalidate()

bool gistvalidate ( Oid  opclassoid)

Definition at line 33 of file gistvalidate.c.

34 {
35  bool result = true;
36  HeapTuple classtup;
37  Form_pg_opclass classform;
38  Oid opfamilyoid;
39  Oid opcintype;
40  Oid opckeytype;
41  char *opclassname;
42  HeapTuple familytup;
43  Form_pg_opfamily familyform;
44  char *opfamilyname;
45  CatCList *proclist,
46  *oprlist;
47  List *grouplist;
48  OpFamilyOpFuncGroup *opclassgroup;
49  int i;
50  ListCell *lc;
51 
52  /* Fetch opclass information */
53  classtup = SearchSysCache1(CLAOID, ObjectIdGetDatum(opclassoid));
54  if (!HeapTupleIsValid(classtup))
55  elog(ERROR, "cache lookup failed for operator class %u", opclassoid);
56  classform = (Form_pg_opclass) GETSTRUCT(classtup);
57 
58  opfamilyoid = classform->opcfamily;
59  opcintype = classform->opcintype;
60  opckeytype = classform->opckeytype;
61  if (!OidIsValid(opckeytype))
62  opckeytype = opcintype;
63  opclassname = NameStr(classform->opcname);
64 
65  /* Fetch opfamily information */
66  familytup = SearchSysCache1(OPFAMILYOID, ObjectIdGetDatum(opfamilyoid));
67  if (!HeapTupleIsValid(familytup))
68  elog(ERROR, "cache lookup failed for operator family %u", opfamilyoid);
69  familyform = (Form_pg_opfamily) GETSTRUCT(familytup);
70 
71  opfamilyname = NameStr(familyform->opfname);
72 
73  /* Fetch all operators and support functions of the opfamily */
74  oprlist = SearchSysCacheList1(AMOPSTRATEGY, ObjectIdGetDatum(opfamilyoid));
75  proclist = SearchSysCacheList1(AMPROCNUM, ObjectIdGetDatum(opfamilyoid));
76 
77  /* Check individual support functions */
78  for (i = 0; i < proclist->n_members; i++)
79  {
80  HeapTuple proctup = &proclist->members[i]->tuple;
81  Form_pg_amproc procform = (Form_pg_amproc) GETSTRUCT(proctup);
82  bool ok;
83 
84  /*
85  * All GiST support functions should be registered with matching
86  * left/right types
87  */
88  if (procform->amproclefttype != procform->amprocrighttype)
89  {
90  ereport(INFO,
91  (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
92  errmsg("operator family \"%s\" of access method %s contains support function %s with different left and right input types",
93  opfamilyname, "gist",
94  format_procedure(procform->amproc))));
95  result = false;
96  }
97 
98  /*
99  * We can't check signatures except within the specific opclass, since
100  * we need to know the associated opckeytype in many cases.
101  */
102  if (procform->amproclefttype != opcintype)
103  continue;
104 
105  /* Check procedure numbers and function signatures */
106  switch (procform->amprocnum)
107  {
109  ok = check_amproc_signature(procform->amproc, BOOLOID, false,
110  5, 5, INTERNALOID, opcintype,
111  INT2OID, OIDOID, INTERNALOID);
112  break;
113  case GIST_UNION_PROC:
114  ok = check_amproc_signature(procform->amproc, opckeytype, false,
115  2, 2, INTERNALOID, INTERNALOID);
116  break;
117  case GIST_COMPRESS_PROC:
119  case GIST_FETCH_PROC:
120  ok = check_amproc_signature(procform->amproc, INTERNALOID, true,
121  1, 1, INTERNALOID);
122  break;
123  case GIST_PENALTY_PROC:
124  ok = check_amproc_signature(procform->amproc, INTERNALOID, true,
125  3, 3, INTERNALOID,
126  INTERNALOID, INTERNALOID);
127  break;
128  case GIST_PICKSPLIT_PROC:
129  ok = check_amproc_signature(procform->amproc, INTERNALOID, true,
130  2, 2, INTERNALOID, INTERNALOID);
131  break;
132  case GIST_EQUAL_PROC:
133  ok = check_amproc_signature(procform->amproc, INTERNALOID, false,
134  3, 3, opckeytype, opckeytype,
135  INTERNALOID);
136  break;
137  case GIST_DISTANCE_PROC:
138  ok = check_amproc_signature(procform->amproc, FLOAT8OID, false,
139  5, 5, INTERNALOID, opcintype,
140  INT2OID, OIDOID, INTERNALOID);
141  break;
142  case GIST_OPTIONS_PROC:
143  ok = check_amoptsproc_signature(procform->amproc);
144  break;
146  ok = check_amproc_signature(procform->amproc, VOIDOID, true,
147  1, 1, INTERNALOID);
148  break;
149  case GIST_STRATNUM_PROC:
150  ok = check_amproc_signature(procform->amproc, INT2OID, true,
151  1, 1, INT2OID);
152  break;
153  default:
154  ereport(INFO,
155  (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
156  errmsg("operator family \"%s\" of access method %s contains function %s with invalid support number %d",
157  opfamilyname, "gist",
158  format_procedure(procform->amproc),
159  procform->amprocnum)));
160  result = false;
161  continue; /* don't want additional message */
162  }
163 
164  if (!ok)
165  {
166  ereport(INFO,
167  (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
168  errmsg("operator family \"%s\" of access method %s contains function %s with wrong signature for support number %d",
169  opfamilyname, "gist",
170  format_procedure(procform->amproc),
171  procform->amprocnum)));
172  result = false;
173  }
174  }
175 
176  /* Check individual operators */
177  for (i = 0; i < oprlist->n_members; i++)
178  {
179  HeapTuple oprtup = &oprlist->members[i]->tuple;
180  Form_pg_amop oprform = (Form_pg_amop) GETSTRUCT(oprtup);
181  Oid op_rettype;
182 
183  /* TODO: Check that only allowed strategy numbers exist */
184  if (oprform->amopstrategy < 1)
185  {
186  ereport(INFO,
187  (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
188  errmsg("operator family \"%s\" of access method %s contains operator %s with invalid strategy number %d",
189  opfamilyname, "gist",
190  format_operator(oprform->amopopr),
191  oprform->amopstrategy)));
192  result = false;
193  }
194 
195  /* GiST supports ORDER BY operators */
196  if (oprform->amoppurpose != AMOP_SEARCH)
197  {
198  /* ... but must have matching distance proc */
199  if (!OidIsValid(get_opfamily_proc(opfamilyoid,
200  oprform->amoplefttype,
201  oprform->amoplefttype,
203  {
204  ereport(INFO,
205  (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
206  errmsg("operator family \"%s\" of access method %s contains unsupported ORDER BY specification for operator %s",
207  opfamilyname, "gist",
208  format_operator(oprform->amopopr))));
209  result = false;
210  }
211  /* ... and operator result must match the claimed btree opfamily */
212  op_rettype = get_op_rettype(oprform->amopopr);
213  if (!opfamily_can_sort_type(oprform->amopsortfamily, op_rettype))
214  {
215  ereport(INFO,
216  (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
217  errmsg("operator family \"%s\" of access method %s contains incorrect ORDER BY opfamily specification for operator %s",
218  opfamilyname, "gist",
219  format_operator(oprform->amopopr))));
220  result = false;
221  }
222  }
223  else
224  {
225  /* Search operators must always return bool */
226  op_rettype = BOOLOID;
227  }
228 
229  /* Check operator signature */
230  if (!check_amop_signature(oprform->amopopr, op_rettype,
231  oprform->amoplefttype,
232  oprform->amoprighttype))
233  {
234  ereport(INFO,
235  (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
236  errmsg("operator family \"%s\" of access method %s contains operator %s with wrong signature",
237  opfamilyname, "gist",
238  format_operator(oprform->amopopr))));
239  result = false;
240  }
241  }
242 
243  /* Now check for inconsistent groups of operators/functions */
244  grouplist = identify_opfamily_groups(oprlist, proclist);
245  opclassgroup = NULL;
246  foreach(lc, grouplist)
247  {
248  OpFamilyOpFuncGroup *thisgroup = (OpFamilyOpFuncGroup *) lfirst(lc);
249 
250  /* Remember the group exactly matching the test opclass */
251  if (thisgroup->lefttype == opcintype &&
252  thisgroup->righttype == opcintype)
253  opclassgroup = thisgroup;
254 
255  /*
256  * There is not a lot we can do to check the operator sets, since each
257  * GiST opclass is more or less a law unto itself, and some contain
258  * only operators that are binary-compatible with the opclass datatype
259  * (meaning that empty operator sets can be OK). That case also means
260  * that we shouldn't insist on nonempty function sets except for the
261  * opclass's own group.
262  */
263  }
264 
265  /* Check that the originally-named opclass is complete */
266  for (i = 1; i <= GISTNProcs; i++)
267  {
268  if (opclassgroup &&
269  (opclassgroup->functionset & (((uint64) 1) << i)) != 0)
270  continue; /* got it */
271  if (i == GIST_DISTANCE_PROC || i == GIST_FETCH_PROC ||
275  continue; /* optional methods */
276  ereport(INFO,
277  (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
278  errmsg("operator class \"%s\" of access method %s is missing support function %d",
279  opclassname, "gist", i)));
280  result = false;
281  }
282 
283  ReleaseCatCacheList(proclist);
284  ReleaseCatCacheList(oprlist);
285  ReleaseSysCache(familytup);
286  ReleaseSysCache(classtup);
287 
288  return result;
289 }
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 opfamily_can_sort_type(Oid opfamilyoid, Oid datatypeoid)
Definition: amvalidate.c:271
bool check_amoptsproc_signature(Oid funcid)
Definition: amvalidate.c:192
#define NameStr(name)
Definition: c.h:749
#define OidIsValid(objectId)
Definition: c.h:778
void ReleaseCatCacheList(CatCList *list)
Definition: catcache.c:1985
#define elog(elevel,...)
Definition: elog.h:225
#define INFO
Definition: elog.h:34
#define GISTNProcs
Definition: gist.h:43
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define GETSTRUCT(TUP)
Definition: htup_details.h:653
int i
Definition: isn.c:73
Oid get_opfamily_proc(Oid opfamily, Oid lefttype, Oid righttype, int16 procnum)
Definition: lsyscache.c:796
Oid get_op_rettype(Oid opno)
Definition: lsyscache.c:1333
FormData_pg_amop * Form_pg_amop
Definition: pg_amop.h:88
FormData_pg_amproc * Form_pg_amproc
Definition: pg_amproc.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
unsigned int Oid
Definition: postgres_ext.h:31
char * format_operator(Oid operator_oid)
Definition: regproc.c:793
char * format_procedure(Oid procedure_oid)
Definition: regproc.c:299
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 check_amop_signature(), check_amoptsproc_signature(), check_amproc_signature(), elog, ereport, errcode(), errmsg(), ERROR, format_operator(), format_procedure(), OpFamilyOpFuncGroup::functionset, get_op_rettype(), get_opfamily_proc(), GETSTRUCT, GIST_COMPRESS_PROC, GIST_CONSISTENT_PROC, GIST_DECOMPRESS_PROC, GIST_DISTANCE_PROC, GIST_EQUAL_PROC, GIST_FETCH_PROC, GIST_OPTIONS_PROC, GIST_PENALTY_PROC, GIST_PICKSPLIT_PROC, GIST_SORTSUPPORT_PROC, GIST_STRATNUM_PROC, GIST_UNION_PROC, GISTNProcs, HeapTupleIsValid, i, identify_opfamily_groups(), INFO, OpFamilyOpFuncGroup::lefttype, lfirst, catclist::members, catclist::n_members, NameStr, ObjectIdGetDatum(), OidIsValid, opfamily_can_sort_type(), ReleaseCatCacheList(), ReleaseSysCache(), OpFamilyOpFuncGroup::righttype, SearchSysCache1(), SearchSysCacheList1, and catctup::tuple.

Referenced by gisthandler().