1
0
mirror of https://github.com/postgres/postgres.git synced 2025-08-19 23:22:23 +03:00

Remove hardcoded hash opclass function signature exceptions

hashvalidate(), which validates the signatures of support functions
for the hash AM, contained several hardcoded exceptions.  For example,
hash/date_ops support function 1 was hashint4(), which would
ordinarily fail validation because the function argument is int4, not
date.  But this works internally because int4 and date are of the same
size.  There are several more exceptions like this that happen to work
and were allowed historically but would now fail the function
signature validation.

This patch removes those exceptions by providing new support functions
that have the proper declared signatures.  They internally share most
of the code with the "wrong" functions they replace, so the behavior
is still the same.

With the exceptions gone, hashvalidate() is now simplified and relies
fully on check_amproc_signature().

hashvarlena() and hashvarlenaextended() are kept in pg_proc.dat
because some extensions currently use them to build hash functions for
their own types, and we need to keep exposing these functions as
"LANGUAGE internal" functions for that to continue to work.

Reviewed-by: Tom Lane <tgl@sss.pgh.pa.us>
Discussion: https://www.postgresql.org/message-id/flat/29c3b746-69e7-482a-b37c-dbbf7e5b009b@eisentraut.org
This commit is contained in:
Peter Eisentraut
2024-09-12 12:52:37 +02:00
parent 5bb9ba2739
commit 23d0b48468
9 changed files with 174 additions and 122 deletions

View File

@@ -376,6 +376,11 @@ hashtextextended(PG_FUNCTION_ARGS)
/*
* hashvarlena() can be used for any varlena datatype in which there are
* no non-significant bits, ie, distinct bitpatterns never compare as equal.
*
* (However, you need to define an SQL-level wrapper function around it with
* the concrete input data type; otherwise hashvalidate() won't accept it.
* Moreover, at least for built-in types, a C-level wrapper function is also
* recommended; otherwise, the opr_sanity test will get upset.)
*/
Datum
hashvarlena(PG_FUNCTION_ARGS)
@@ -406,3 +411,15 @@ hashvarlenaextended(PG_FUNCTION_ARGS)
return result;
}
Datum
hashbytea(PG_FUNCTION_ARGS)
{
return hashvarlena(fcinfo);
}
Datum
hashbyteaextended(PG_FUNCTION_ARGS)
{
return hashvarlenaextended(fcinfo);
}

View File

@@ -22,19 +22,13 @@
#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/lsyscache.h"
#include "utils/regproc.h"
#include "utils/syscache.h"
static bool check_hash_func_signature(Oid funcid, int16 amprocnum, Oid argtype);
/*
* Validator for a hash opclass.
*
@@ -90,6 +84,7 @@ hashvalidate(Oid opclassoid)
{
HeapTuple proctup = &proclist->members[i]->tuple;
Form_pg_amproc procform = (Form_pg_amproc) GETSTRUCT(proctup);
bool ok;
/*
* All hash functions should be registered with matching left/right
@@ -109,29 +104,15 @@ hashvalidate(Oid opclassoid)
switch (procform->amprocnum)
{
case HASHSTANDARD_PROC:
ok = check_amproc_signature(procform->amproc, INT4OID, true,
1, 1, procform->amproclefttype);
break;
case HASHEXTENDED_PROC:
if (!check_hash_func_signature(procform->amproc, procform->amprocnum,
procform->amproclefttype))
{
ereport(INFO,
(errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
errmsg("operator family \"%s\" of access method %s contains function %s with wrong signature for support number %d",
opfamilyname, "hash",
format_procedure(procform->amproc),
procform->amprocnum)));
result = false;
}
else
{
/* Remember which types we can hash */
hashabletypes =
list_append_unique_oid(hashabletypes,
procform->amproclefttype);
}
ok = check_amproc_signature(procform->amproc, INT8OID, true,
2, 2, procform->amproclefttype, INT8OID);
break;
case HASHOPTIONS_PROC:
if (!check_amoptsproc_signature(procform->amproc))
result = false;
ok = check_amoptsproc_signature(procform->amproc);
break;
default:
ereport(INFO,
@@ -141,7 +122,24 @@ hashvalidate(Oid opclassoid)
format_procedure(procform->amproc),
procform->amprocnum)));
result = false;
break;
continue; /* don't want additional message */
}
if (!ok)
{
ereport(INFO,
(errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
errmsg("operator family \"%s\" of access method %s contains function %s with wrong signature for support number %d",
opfamilyname, "hash",
format_procedure(procform->amproc),
procform->amprocnum)));
result = false;
}
/* Remember which types we can hash */
if (ok && (procform->amprocnum == HASHSTANDARD_PROC || procform->amprocnum == HASHEXTENDED_PROC))
{
hashabletypes = list_append_unique_oid(hashabletypes, procform->amproclefttype);
}
}
@@ -267,84 +265,6 @@ hashvalidate(Oid opclassoid)
}
/*
* We need a custom version of check_amproc_signature because of assorted
* hacks in the core hash opclass definitions.
*/
static bool
check_hash_func_signature(Oid funcid, int16 amprocnum, Oid argtype)
{
bool result = true;
Oid restype;
int16 nargs;
HeapTuple tp;
Form_pg_proc procform;
switch (amprocnum)
{
case HASHSTANDARD_PROC:
restype = INT4OID;
nargs = 1;
break;
case HASHEXTENDED_PROC:
restype = INT8OID;
nargs = 2;
break;
default:
elog(ERROR, "invalid amprocnum");
}
tp = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcid));
if (!HeapTupleIsValid(tp))
elog(ERROR, "cache lookup failed for function %u", funcid);
procform = (Form_pg_proc) GETSTRUCT(tp);
if (procform->prorettype != restype || procform->proretset ||
procform->pronargs != nargs)
result = false;
if (!IsBinaryCoercible(argtype, procform->proargtypes.values[0]))
{
/*
* Some of the built-in hash opclasses cheat by using hash functions
* that are different from but physically compatible with the opclass
* datatype. In some of these cases, even a "binary coercible" check
* fails because there's no relevant cast. For the moment, fix it by
* having a list of allowed cases. Test the specific function
* identity, not just its input type, because hashvarlena() takes
* INTERNAL and allowing any such function seems too scary.
*/
if ((funcid == F_HASHINT4 || funcid == F_HASHINT4EXTENDED) &&
(argtype == DATEOID ||
argtype == XIDOID || argtype == CIDOID))
/* okay, allowed use of hashint4() */ ;
else if ((funcid == F_HASHINT8 || funcid == F_HASHINT8EXTENDED) &&
(argtype == XID8OID))
/* okay, allowed use of hashint8() */ ;
else if ((funcid == F_TIMESTAMP_HASH ||
funcid == F_TIMESTAMP_HASH_EXTENDED) &&
argtype == TIMESTAMPTZOID)
/* okay, allowed use of timestamp_hash() */ ;
else if ((funcid == F_HASHCHAR || funcid == F_HASHCHAREXTENDED) &&
argtype == BOOLOID)
/* okay, allowed use of hashchar() */ ;
else if ((funcid == F_HASHVARLENA || funcid == F_HASHVARLENAEXTENDED) &&
argtype == BYTEAOID)
/* okay, allowed use of hashvarlena() */ ;
else
result = false;
}
/* If function takes a second argument, it must be for a 64-bit salt. */
if (nargs == 2 && procform->proargtypes.values[1] != INT8OID)
result = false;
ReleaseSysCache(tp);
return result;
}
/*
* Prechecking function for adding operators/functions to a hash opfamily.
*/