mirror of
https://github.com/postgres/postgres.git
synced 2025-07-08 11:42:09 +03:00
Add 'noError' argument to encoding conversion functions.
With the 'noError' argument, you can try to convert a buffer without knowing the character boundaries beforehand. The functions now need to return the number of input bytes successfully converted. This is is a backwards-incompatible change, if you have created a custom encoding conversion with CREATE CONVERSION. This adds a check to pg_upgrade for that, refusing the upgrade if there are any user-defined encoding conversions. Custom conversions are very rare, there are no commonly used extensions that I know of that uses that feature. No other objects can depend on conversions, so if you do have one, you can fairly easily drop it before upgrading, and recreate it after the upgrade with an updated version. Add regression tests for built-in encoding conversions. This doesn't cover every conversion, but it covers all the internal functions in conv.c that are used to implement the conversions. Reviewed-by: John Naylor Discussion: https://www.postgresql.org/message-id/e7861509-3960-538a-9025-b75a61188e01%40iki.fi
This commit is contained in:
@ -45,8 +45,9 @@ CreateConversionCommand(CreateConversionStmt *stmt)
|
||||
const char *from_encoding_name = stmt->for_encoding_name;
|
||||
const char *to_encoding_name = stmt->to_encoding_name;
|
||||
List *func_name = stmt->func_name;
|
||||
static const Oid funcargs[] = {INT4OID, INT4OID, CSTRINGOID, INTERNALOID, INT4OID};
|
||||
static const Oid funcargs[] = {INT4OID, INT4OID, CSTRINGOID, INTERNALOID, INT4OID, BOOLOID};
|
||||
char result[1];
|
||||
Datum funcresult;
|
||||
|
||||
/* Convert list of names to a name and namespace */
|
||||
namespaceId = QualifiedNameGetCreationNamespace(stmt->conversion_name,
|
||||
@ -92,12 +93,12 @@ CreateConversionCommand(CreateConversionStmt *stmt)
|
||||
funcoid = LookupFuncName(func_name, sizeof(funcargs) / sizeof(Oid),
|
||||
funcargs, false);
|
||||
|
||||
/* Check it returns VOID, else it's probably the wrong function */
|
||||
if (get_func_rettype(funcoid) != VOIDOID)
|
||||
/* Check it returns int4, else it's probably the wrong function */
|
||||
if (get_func_rettype(funcoid) != INT4OID)
|
||||
ereport(ERROR,
|
||||
(errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
|
||||
errmsg("encoding conversion function %s must return type %s",
|
||||
NameListToString(func_name), "void")));
|
||||
NameListToString(func_name), "integer")));
|
||||
|
||||
/* Check we have EXECUTE rights for the function */
|
||||
aclresult = pg_proc_aclcheck(funcoid, GetUserId(), ACL_EXECUTE);
|
||||
@ -111,12 +112,23 @@ CreateConversionCommand(CreateConversionStmt *stmt)
|
||||
* string; the conversion function should throw an error if it can't
|
||||
* perform the requested conversion.
|
||||
*/
|
||||
OidFunctionCall5(funcoid,
|
||||
Int32GetDatum(from_encoding),
|
||||
Int32GetDatum(to_encoding),
|
||||
CStringGetDatum(""),
|
||||
CStringGetDatum(result),
|
||||
Int32GetDatum(0));
|
||||
funcresult = OidFunctionCall6(funcoid,
|
||||
Int32GetDatum(from_encoding),
|
||||
Int32GetDatum(to_encoding),
|
||||
CStringGetDatum(""),
|
||||
CStringGetDatum(result),
|
||||
Int32GetDatum(0),
|
||||
BoolGetDatum(false));
|
||||
|
||||
/*
|
||||
* The function should return 0 for empty input. Might as well check that,
|
||||
* too.
|
||||
*/
|
||||
if (DatumGetInt32(funcresult) != 0)
|
||||
ereport(ERROR,
|
||||
(errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
|
||||
errmsg("encoding conversion function %s returned incorrect result for empty input",
|
||||
NameListToString(func_name))));
|
||||
|
||||
/*
|
||||
* All seem ok, go ahead (possible failure would be a duplicate conversion
|
||||
|
Reference in New Issue
Block a user