mirror of
https://github.com/postgres/postgres.git
synced 2025-11-19 13:42:17 +03:00
Add to_regtypemod function to extract typemod from a string type name.
In combination with to_regtype, this allows converting a string to the "canonicalized" form emitted by format_type. That usage requires parsing the string twice, which is slightly annoying but not really too expensive. We considered alternatives such as returning a record type, but that way was notationally uglier than this, and possibly less flexible. Like to_regtype(), we'd rather that this return NULL for any bad input, but the underlying type-parsing logic isn't yet capable of not throwing syntax errors. Adjust the documentation for both functions to point that out. In passing, fix up a couple of nearby entries in the System Catalog Information Functions table that had not gotten the word about our since-v13 convention for displaying function usage examples. David Wheeler and Erik Wienhold, reviewed by Pavel Stehule, Jim Jones, and others. Discussion: https://postgr.es/m/DF2324CA-2673-4ABE-B382-26B5770B6AA3@justatheory.com
This commit is contained in:
@@ -1220,6 +1220,26 @@ to_regtype(PG_FUNCTION_ARGS)
|
||||
PG_RETURN_DATUM(result);
|
||||
}
|
||||
|
||||
/*
|
||||
* to_regtypemod - converts "typename" to type modifier
|
||||
*
|
||||
* If the name is not found, we return NULL.
|
||||
*/
|
||||
Datum
|
||||
to_regtypemod(PG_FUNCTION_ARGS)
|
||||
{
|
||||
char *typ_name = text_to_cstring(PG_GETARG_TEXT_PP(0));
|
||||
Oid typid;
|
||||
int32 typmod;
|
||||
ErrorSaveContext escontext = {T_ErrorSaveContext};
|
||||
|
||||
/* We rely on parseTypeString to parse the input. */
|
||||
if (!parseTypeString(typ_name, &typid, &typmod, (Node *) &escontext))
|
||||
PG_RETURN_NULL();
|
||||
|
||||
PG_RETURN_INT32(typmod);
|
||||
}
|
||||
|
||||
/*
|
||||
* regtypeout - converts type OID to "typ_name"
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user