PostgreSQL Source Code  git master
hashvalidate.c File Reference
#include "postgres.h"
#include "access/amvalidate.h"
#include "access/hash.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_proc.h"
#include "catalog/pg_type.h"
#include "parser/parse_coerce.h"
#include "utils/builtins.h"
#include "utils/fmgroids.h"
#include "utils/regproc.h"
#include "utils/syscache.h"
Include dependency graph for hashvalidate.c:

Go to the source code of this file.

Functions

static bool check_hash_func_signature (Oid funcid, int16 amprocnum, Oid argtype)
 
bool hashvalidate (Oid opclassoid)
 

Function Documentation

◆ check_hash_func_signature()

static bool check_hash_func_signature ( Oid  funcid,
int16  amprocnum,
Oid  argtype 
)
static

Definition at line 272 of file hashvalidate.c.

References elog, ERROR, GETSTRUCT, HASHEXTENDED_PROC, HASHSTANDARD_PROC, HeapTupleIsValid, IsBinaryCoercible(), ObjectIdGetDatum, PROCOID, ReleaseSysCache(), and SearchSysCache1().

Referenced by hashvalidate().

273 {
274  bool result = true;
275  Oid restype;
276  int16 nargs;
277  HeapTuple tp;
278  Form_pg_proc procform;
279 
280  switch (amprocnum)
281  {
282  case HASHSTANDARD_PROC:
283  restype = INT4OID;
284  nargs = 1;
285  break;
286 
287  case HASHEXTENDED_PROC:
288  restype = INT8OID;
289  nargs = 2;
290  break;
291 
292  default:
293  elog(ERROR, "invalid amprocnum");
294  }
295 
297  if (!HeapTupleIsValid(tp))
298  elog(ERROR, "cache lookup failed for function %u", funcid);
299  procform = (Form_pg_proc) GETSTRUCT(tp);
300 
301  if (procform->prorettype != restype || procform->proretset ||
302  procform->pronargs != nargs)
303  result = false;
304 
305  if (!IsBinaryCoercible(argtype, procform->proargtypes.values[0]))
306  {
307  /*
308  * Some of the built-in hash opclasses cheat by using hash functions
309  * that are different from but physically compatible with the opclass
310  * datatype. In some of these cases, even a "binary coercible" check
311  * fails because there's no relevant cast. For the moment, fix it by
312  * having a whitelist of allowed cases. Test the specific function
313  * identity, not just its input type, because hashvarlena() takes
314  * INTERNAL and allowing any such function seems too scary.
315  */
316  if ((funcid == F_HASHINT4 || funcid == F_HASHINT4EXTENDED) &&
317  (argtype == DATEOID ||
318  argtype == XIDOID || argtype == CIDOID))
319  /* okay, allowed use of hashint4() */ ;
320  else if ((funcid == F_HASHINT8 || funcid == F_HASHINT8EXTENDED) &&
321  (argtype == XID8OID))
322  /* okay, allowed use of hashint8() */ ;
323  else if ((funcid == F_TIMESTAMP_HASH ||
324  funcid == F_TIMESTAMP_HASH_EXTENDED) &&
325  argtype == TIMESTAMPTZOID)
326  /* okay, allowed use of timestamp_hash() */ ;
327  else if ((funcid == F_HASHCHAR || funcid == F_HASHCHAREXTENDED) &&
328  argtype == BOOLOID)
329  /* okay, allowed use of hashchar() */ ;
330  else if ((funcid == F_HASHVARLENA || funcid == F_HASHVARLENAEXTENDED) &&
331  argtype == BYTEAOID)
332  /* okay, allowed use of hashvarlena() */ ;
333  else
334  result = false;
335  }
336 
337  /* If function takes a second argument, it must be for a 64-bit salt. */
338  if (nargs == 2 && procform->proargtypes.values[1] != INT8OID)
339  result = false;
340 
341  ReleaseSysCache(tp);
342  return result;
343 }
signed short int16
Definition: c.h:354
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
#define HASHEXTENDED_PROC
Definition: hash.h:354
unsigned int Oid
Definition: postgres_ext.h:31
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:43
#define HASHSTANDARD_PROC
Definition: hash.h:353
bool IsBinaryCoercible(Oid srctype, Oid targettype)
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1116
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1164
FormData_pg_proc * Form_pg_proc
Definition: pg_proc.h:133
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define elog(elevel,...)
Definition: elog.h:214

◆ hashvalidate()

bool hashvalidate ( Oid  opclassoid)

Definition at line 44 of file hashvalidate.c.

References AMOPSTRATEGY, AMPROCNUM, check_amop_signature(), check_amoptsproc_signature(), check_hash_func_signature(), CLAOID, elog, ereport, errcode(), errmsg(), ERROR, format_operator(), format_procedure(), format_type_be(), GETSTRUCT, HASHEXTENDED_PROC, HASHOPTIONS_PROC, HASHSTANDARD_PROC, HeapTupleIsValid, HTEqualStrategyNumber, HTMaxStrategyNumber, i, identify_opfamily_groups(), INFO, OpFamilyOpFuncGroup::lefttype, lfirst, list_append_unique_oid(), list_length(), list_member_oid(), catclist::members, catclist::n_members, NameStr, NIL, ObjectIdGetDatum, OidIsValid, OpFamilyOpFuncGroup::operatorset, OPFAMILYOID, ReleaseCatCacheList(), ReleaseSysCache(), OpFamilyOpFuncGroup::righttype, SearchSysCache1(), SearchSysCacheList1, and catctup::tuple.

Referenced by hashhandler().

45 {
46  bool result = true;
47  HeapTuple classtup;
48  Form_pg_opclass classform;
49  Oid opfamilyoid;
50  Oid opcintype;
51  char *opclassname;
52  HeapTuple familytup;
53  Form_pg_opfamily familyform;
54  char *opfamilyname;
55  CatCList *proclist,
56  *oprlist;
57  List *grouplist;
58  OpFamilyOpFuncGroup *opclassgroup;
59  List *hashabletypes = NIL;
60  int i;
61  ListCell *lc;
62 
63  /* Fetch opclass information */
64  classtup = SearchSysCache1(CLAOID, ObjectIdGetDatum(opclassoid));
65  if (!HeapTupleIsValid(classtup))
66  elog(ERROR, "cache lookup failed for operator class %u", opclassoid);
67  classform = (Form_pg_opclass) GETSTRUCT(classtup);
68 
69  opfamilyoid = classform->opcfamily;
70  opcintype = classform->opcintype;
71  opclassname = NameStr(classform->opcname);
72 
73  /* Fetch opfamily information */
74  familytup = SearchSysCache1(OPFAMILYOID, ObjectIdGetDatum(opfamilyoid));
75  if (!HeapTupleIsValid(familytup))
76  elog(ERROR, "cache lookup failed for operator family %u", opfamilyoid);
77  familyform = (Form_pg_opfamily) GETSTRUCT(familytup);
78 
79  opfamilyname = NameStr(familyform->opfname);
80 
81  /* Fetch all operators and support functions of the opfamily */
82  oprlist = SearchSysCacheList1(AMOPSTRATEGY, ObjectIdGetDatum(opfamilyoid));
83  proclist = SearchSysCacheList1(AMPROCNUM, ObjectIdGetDatum(opfamilyoid));
84 
85  /* Check individual support functions */
86  for (i = 0; i < proclist->n_members; i++)
87  {
88  HeapTuple proctup = &proclist->members[i]->tuple;
89  Form_pg_amproc procform = (Form_pg_amproc) GETSTRUCT(proctup);
90 
91  /*
92  * All hash functions should be registered with matching left/right
93  * types
94  */
95  if (procform->amproclefttype != procform->amprocrighttype)
96  {
97  ereport(INFO,
98  (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
99  errmsg("operator family \"%s\" of access method %s contains support function %s with different left and right input types",
100  opfamilyname, "hash",
101  format_procedure(procform->amproc))));
102  result = false;
103  }
104 
105  /* Check procedure numbers and function signatures */
106  switch (procform->amprocnum)
107  {
108  case HASHSTANDARD_PROC:
109  case HASHEXTENDED_PROC:
110  if (!check_hash_func_signature(procform->amproc, procform->amprocnum,
111  procform->amproclefttype))
112  {
113  ereport(INFO,
114  (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
115  errmsg("operator family \"%s\" of access method %s contains function %s with wrong signature for support number %d",
116  opfamilyname, "hash",
117  format_procedure(procform->amproc),
118  procform->amprocnum)));
119  result = false;
120  }
121  else
122  {
123  /* Remember which types we can hash */
124  hashabletypes =
125  list_append_unique_oid(hashabletypes,
126  procform->amproclefttype);
127  }
128  break;
129  case HASHOPTIONS_PROC:
130  if (!check_amoptsproc_signature(procform->amproc))
131  result = false;
132  break;
133  default:
134  ereport(INFO,
135  (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
136  errmsg("operator family \"%s\" of access method %s contains function %s with invalid support number %d",
137  opfamilyname, "hash",
138  format_procedure(procform->amproc),
139  procform->amprocnum)));
140  result = false;
141  break;
142  }
143  }
144 
145  /* Check individual operators */
146  for (i = 0; i < oprlist->n_members; i++)
147  {
148  HeapTuple oprtup = &oprlist->members[i]->tuple;
149  Form_pg_amop oprform = (Form_pg_amop) GETSTRUCT(oprtup);
150 
151  /* Check that only allowed strategy numbers exist */
152  if (oprform->amopstrategy < 1 ||
153  oprform->amopstrategy > HTMaxStrategyNumber)
154  {
155  ereport(INFO,
156  (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
157  errmsg("operator family \"%s\" of access method %s contains operator %s with invalid strategy number %d",
158  opfamilyname, "hash",
159  format_operator(oprform->amopopr),
160  oprform->amopstrategy)));
161  result = false;
162  }
163 
164  /* hash doesn't support ORDER BY operators */
165  if (oprform->amoppurpose != AMOP_SEARCH ||
166  OidIsValid(oprform->amopsortfamily))
167  {
168  ereport(INFO,
169  (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
170  errmsg("operator family \"%s\" of access method %s contains invalid ORDER BY specification for operator %s",
171  opfamilyname, "hash",
172  format_operator(oprform->amopopr))));
173  result = false;
174  }
175 
176  /* Check operator signature --- same for all hash strategies */
177  if (!check_amop_signature(oprform->amopopr, BOOLOID,
178  oprform->amoplefttype,
179  oprform->amoprighttype))
180  {
181  ereport(INFO,
182  (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
183  errmsg("operator family \"%s\" of access method %s contains operator %s with wrong signature",
184  opfamilyname, "hash",
185  format_operator(oprform->amopopr))));
186  result = false;
187  }
188 
189  /* There should be relevant hash functions for each datatype */
190  if (!list_member_oid(hashabletypes, oprform->amoplefttype) ||
191  !list_member_oid(hashabletypes, oprform->amoprighttype))
192  {
193  ereport(INFO,
194  (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
195  errmsg("operator family \"%s\" of access method %s lacks support function for operator %s",
196  opfamilyname, "hash",
197  format_operator(oprform->amopopr))));
198  result = false;
199  }
200  }
201 
202  /* Now check for inconsistent groups of operators/functions */
203  grouplist = identify_opfamily_groups(oprlist, proclist);
204  opclassgroup = NULL;
205  foreach(lc, grouplist)
206  {
207  OpFamilyOpFuncGroup *thisgroup = (OpFamilyOpFuncGroup *) lfirst(lc);
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  * Complain if there seems to be an incomplete set of operators for
216  * this datatype pair (implying that we have a hash function but no
217  * operator).
218  */
219  if (thisgroup->operatorset != (1 << HTEqualStrategyNumber))
220  {
221  ereport(INFO,
222  (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
223  errmsg("operator family \"%s\" of access method %s is missing operator(s) for types %s and %s",
224  opfamilyname, "hash",
225  format_type_be(thisgroup->lefttype),
226  format_type_be(thisgroup->righttype))));
227  result = false;
228  }
229  }
230 
231  /* Check that the originally-named opclass is supported */
232  /* (if group is there, we already checked it adequately above) */
233  if (!opclassgroup)
234  {
235  ereport(INFO,
236  (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
237  errmsg("operator class \"%s\" of access method %s is missing operator(s)",
238  opclassname, "hash")));
239  result = false;
240  }
241 
242  /*
243  * Complain if the opfamily doesn't have entries for all possible
244  * combinations of its supported datatypes. While missing cross-type
245  * operators are not fatal, it seems reasonable to insist that all
246  * built-in hash opfamilies be complete.
247  */
248  if (list_length(grouplist) !=
249  list_length(hashabletypes) * list_length(hashabletypes))
250  {
251  ereport(INFO,
252  (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
253  errmsg("operator family \"%s\" of access method %s is missing cross-type operator(s)",
254  opfamilyname, "hash")));
255  result = false;
256  }
257 
258  ReleaseCatCacheList(proclist);
259  ReleaseCatCacheList(oprlist);
260  ReleaseSysCache(familytup);
261  ReleaseSysCache(classtup);
262 
263  return result;
264 }
#define NIL
Definition: pg_list.h:65
int n_members
Definition: catcache.h:176
List * list_append_unique_oid(List *list, Oid datum)
Definition: list.c:1217
#define HTMaxStrategyNumber
Definition: stratnum.h:43
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
FormData_pg_amproc * Form_pg_amproc
Definition: pg_amproc.h:68
int errcode(int sqlerrcode)
Definition: elog.c:610
#define HASHEXTENDED_PROC
Definition: hash.h:354
#define INFO
Definition: elog.h:33
char * format_type_be(Oid type_oid)
Definition: format_type.c:327
char * format_operator(Oid operator_oid)
Definition: regproc.c:821
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:644
bool check_amoptsproc_signature(Oid funcid)
Definition: amvalidate.c:191
static bool check_hash_func_signature(Oid funcid, int16 amprocnum, Oid argtype)
Definition: hashvalidate.c:272
void ReleaseCatCacheList(CatCList *list)
Definition: catcache.c:1782
CatCTup * members[FLEXIBLE_ARRAY_MEMBER]
Definition: catcache.h:178
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:43
List * identify_opfamily_groups(CatCList *oprlist, CatCList *proclist)
Definition: amvalidate.c:42
#define HTEqualStrategyNumber
Definition: stratnum.h:41
#define SearchSysCacheList1(cacheId, key1)
Definition: syscache.h:210
#define HASHSTANDARD_PROC
Definition: hash.h:353
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1116
FormData_pg_opfamily * Form_pg_opfamily
Definition: pg_opfamily.h:51
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1164
#define HASHOPTIONS_PROC
Definition: hash.h:355
#define ereport(elevel,...)
Definition: elog.h:144
char * format_procedure(Oid procedure_oid)
Definition: regproc.c:324
bool list_member_oid(const List *list, Oid datum)
Definition: list.c:674
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define lfirst(lc)
Definition: pg_list.h:190
static int list_length(const List *l)
Definition: pg_list.h:169
bool check_amop_signature(Oid opno, Oid restype, Oid lefttype, Oid righttype)
Definition: amvalidate.c:205
int errmsg(const char *fmt,...)
Definition: elog.c:824
FormData_pg_amop * Form_pg_amop
Definition: pg_amop.h:88
#define elog(elevel,...)
Definition: elog.h:214
int i
#define NameStr(name)
Definition: c.h:615
HeapTupleData tuple
Definition: catcache.h:121
FormData_pg_opclass * Form_pg_opclass
Definition: pg_opclass.h:83
Definition: pg_list.h:50