Simplify the implementations of the to_reg* functions.

Given the soft-input-error feature, we can reduce these functions
to be just thin wrappers around a soft-error call of the
corresponding datatype input function.  This means less code and
more certainty that the to_reg* functions match the normal input
behavior.

Notably, it also means that they will accept numeric OID input,
which they didn't before.  It's not clear to me if that omission
had more than laziness behind it, but it doesn't seem like
something we need to work hard to preserve.

Discussion: https://postgr.es/m/3910031.1672095600@sss.pgh.pa.us
This commit is contained in:
Tom Lane 2022-12-27 12:33:04 -05:00
parent 858e776c84
commit 3ea7329c9a
2 changed files with 63 additions and 159 deletions

View File

@ -24100,8 +24100,7 @@ SELECT collation for ('foo' COLLATE "de_DE");
obtained by casting the string to type <type>regclass</type> (see obtained by casting the string to type <type>regclass</type> (see
<xref linkend="datatype-oid"/>); however, this function will return <xref linkend="datatype-oid"/>); however, this function will return
<literal>NULL</literal> rather than throwing an error if the name is <literal>NULL</literal> rather than throwing an error if the name is
not found. Also unlike the cast, this does not accept not found.
a numeric OID as input.
</para></entry> </para></entry>
</row> </row>
@ -24118,8 +24117,7 @@ SELECT collation for ('foo' COLLATE "de_DE");
obtained by casting the string to type <type>regcollation</type> (see obtained by casting the string to type <type>regcollation</type> (see
<xref linkend="datatype-oid"/>); however, this function will return <xref linkend="datatype-oid"/>); however, this function will return
<literal>NULL</literal> rather than throwing an error if the name is <literal>NULL</literal> rather than throwing an error if the name is
not found. Also unlike the cast, this does not accept not found.
a numeric OID as input.
</para></entry> </para></entry>
</row> </row>
@ -24136,8 +24134,7 @@ SELECT collation for ('foo' COLLATE "de_DE");
obtained by casting the string to type <type>regnamespace</type> (see obtained by casting the string to type <type>regnamespace</type> (see
<xref linkend="datatype-oid"/>); however, this function will return <xref linkend="datatype-oid"/>); however, this function will return
<literal>NULL</literal> rather than throwing an error if the name is <literal>NULL</literal> rather than throwing an error if the name is
not found. Also unlike the cast, this does not accept not found.
a numeric OID as input.
</para></entry> </para></entry>
</row> </row>
@ -24154,8 +24151,7 @@ SELECT collation for ('foo' COLLATE "de_DE");
obtained by casting the string to type <type>regoper</type> (see obtained by casting the string to type <type>regoper</type> (see
<xref linkend="datatype-oid"/>); however, this function will return <xref linkend="datatype-oid"/>); however, this function will return
<literal>NULL</literal> rather than throwing an error if the name is <literal>NULL</literal> rather than throwing an error if the name is
not found or is ambiguous. Also unlike the cast, this does not accept not found or is ambiguous.
a numeric OID as input.
</para></entry> </para></entry>
</row> </row>
@ -24172,8 +24168,7 @@ SELECT collation for ('foo' COLLATE "de_DE");
obtained by casting the string to type <type>regoperator</type> (see obtained by casting the string to type <type>regoperator</type> (see
<xref linkend="datatype-oid"/>); however, this function will return <xref linkend="datatype-oid"/>); however, this function will return
<literal>NULL</literal> rather than throwing an error if the name is <literal>NULL</literal> rather than throwing an error if the name is
not found. Also unlike the cast, this does not accept not found.
a numeric OID as input.
</para></entry> </para></entry>
</row> </row>
@ -24190,8 +24185,7 @@ SELECT collation for ('foo' COLLATE "de_DE");
obtained by casting the string to type <type>regproc</type> (see obtained by casting the string to type <type>regproc</type> (see
<xref linkend="datatype-oid"/>); however, this function will return <xref linkend="datatype-oid"/>); however, this function will return
<literal>NULL</literal> rather than throwing an error if the name is <literal>NULL</literal> rather than throwing an error if the name is
not found or is ambiguous. Also unlike the cast, this does not accept not found or is ambiguous.
a numeric OID as input.
</para></entry> </para></entry>
</row> </row>
@ -24208,8 +24202,7 @@ SELECT collation for ('foo' COLLATE "de_DE");
obtained by casting the string to type <type>regprocedure</type> (see obtained by casting the string to type <type>regprocedure</type> (see
<xref linkend="datatype-oid"/>); however, this function will return <xref linkend="datatype-oid"/>); however, this function will return
<literal>NULL</literal> rather than throwing an error if the name is <literal>NULL</literal> rather than throwing an error if the name is
not found. Also unlike the cast, this does not accept not found.
a numeric OID as input.
</para></entry> </para></entry>
</row> </row>
@ -24226,8 +24219,7 @@ SELECT collation for ('foo' COLLATE "de_DE");
obtained by casting the string to type <type>regrole</type> (see obtained by casting the string to type <type>regrole</type> (see
<xref linkend="datatype-oid"/>); however, this function will return <xref linkend="datatype-oid"/>); however, this function will return
<literal>NULL</literal> rather than throwing an error if the name is <literal>NULL</literal> rather than throwing an error if the name is
not found. Also unlike the cast, this does not accept not found.
a numeric OID as input.
</para></entry> </para></entry>
</row> </row>
@ -24244,8 +24236,7 @@ SELECT collation for ('foo' COLLATE "de_DE");
obtained by casting the string to type <type>regtype</type> (see obtained by casting the string to type <type>regtype</type> (see
<xref linkend="datatype-oid"/>); however, this function will return <xref linkend="datatype-oid"/>); however, this function will return
<literal>NULL</literal> rather than throwing an error if the name is <literal>NULL</literal> rather than throwing an error if the name is
not found. Also unlike the cast, this does not accept not found.
a numeric OID as input.
</para></entry> </para></entry>
</row> </row>
</tbody> </tbody>

View File

@ -118,24 +118,15 @@ Datum
to_regproc(PG_FUNCTION_ARGS) to_regproc(PG_FUNCTION_ARGS)
{ {
char *pro_name = text_to_cstring(PG_GETARG_TEXT_PP(0)); char *pro_name = text_to_cstring(PG_GETARG_TEXT_PP(0));
List *names; Datum result;
FuncCandidateList clist;
ErrorSaveContext escontext = {T_ErrorSaveContext}; ErrorSaveContext escontext = {T_ErrorSaveContext};
/* if (!DirectInputFunctionCallSafe(regprocin, pro_name,
* Parse the name into components and see if it matches any pg_proc InvalidOid, -1,
* entries in the current search path. (Node *) &escontext,
*/ &result))
names = stringToQualifiedNameList(pro_name, (Node *) &escontext);
if (names == NIL)
PG_RETURN_NULL(); PG_RETURN_NULL();
PG_RETURN_DATUM(result);
clist = FuncnameGetCandidates(names, -1, NIL, false, false, false, true);
if (clist == NULL || clist->next != NULL)
PG_RETURN_NULL();
PG_RETURN_OID(clist->oid);
} }
/* /*
@ -287,31 +278,15 @@ Datum
to_regprocedure(PG_FUNCTION_ARGS) to_regprocedure(PG_FUNCTION_ARGS)
{ {
char *pro_name = text_to_cstring(PG_GETARG_TEXT_PP(0)); char *pro_name = text_to_cstring(PG_GETARG_TEXT_PP(0));
List *names; Datum result;
int nargs;
Oid argtypes[FUNC_MAX_ARGS];
FuncCandidateList clist;
ErrorSaveContext escontext = {T_ErrorSaveContext}; ErrorSaveContext escontext = {T_ErrorSaveContext};
/* if (!DirectInputFunctionCallSafe(regprocedurein, pro_name,
* Parse the name and arguments, look up potential matches in the current InvalidOid, -1,
* namespace search list, and scan to see which one exactly matches the (Node *) &escontext,
* given argument types. (There will not be more than one match.) &result))
*/
if (!parseNameAndArgTypes(pro_name, false,
&names, &nargs, argtypes,
(Node *) &escontext))
PG_RETURN_NULL();
clist = FuncnameGetCandidates(names, nargs, NIL, false, false, false, true);
for (; clist; clist = clist->next)
{
if (memcmp(clist->args, argtypes, nargs * sizeof(Oid)) == 0)
PG_RETURN_OID(clist->oid);
}
PG_RETURN_NULL(); PG_RETURN_NULL();
PG_RETURN_DATUM(result);
} }
/* /*
@ -552,24 +527,15 @@ Datum
to_regoper(PG_FUNCTION_ARGS) to_regoper(PG_FUNCTION_ARGS)
{ {
char *opr_name = text_to_cstring(PG_GETARG_TEXT_PP(0)); char *opr_name = text_to_cstring(PG_GETARG_TEXT_PP(0));
List *names; Datum result;
FuncCandidateList clist;
ErrorSaveContext escontext = {T_ErrorSaveContext}; ErrorSaveContext escontext = {T_ErrorSaveContext};
/* if (!DirectInputFunctionCallSafe(regoperin, opr_name,
* Parse the name into components and see if it matches any pg_operator InvalidOid, -1,
* entries in the current search path. (Node *) &escontext,
*/ &result))
names = stringToQualifiedNameList(opr_name, (Node *) &escontext);
if (names == NIL)
PG_RETURN_NULL(); PG_RETURN_NULL();
PG_RETURN_DATUM(result);
clist = OpernameGetCandidates(names, '\0', true);
if (clist == NULL || clist->next != NULL)
PG_RETURN_NULL();
PG_RETURN_OID(clist->oid);
} }
/* /*
@ -728,31 +694,15 @@ Datum
to_regoperator(PG_FUNCTION_ARGS) to_regoperator(PG_FUNCTION_ARGS)
{ {
char *opr_name_or_oid = text_to_cstring(PG_GETARG_TEXT_PP(0)); char *opr_name_or_oid = text_to_cstring(PG_GETARG_TEXT_PP(0));
Oid result; Datum result;
List *names;
int nargs;
Oid argtypes[FUNC_MAX_ARGS];
ErrorSaveContext escontext = {T_ErrorSaveContext}; ErrorSaveContext escontext = {T_ErrorSaveContext};
/* if (!DirectInputFunctionCallSafe(regoperatorin, opr_name_or_oid,
* Parse the name and arguments, look up potential matches in the current InvalidOid, -1,
* namespace search list, and scan to see which one exactly matches the (Node *) &escontext,
* given argument types. (There will not be more than one match.) &result))
*/
if (!parseNameAndArgTypes(opr_name_or_oid, true,
&names, &nargs, argtypes,
(Node *) &escontext))
PG_RETURN_NULL(); PG_RETURN_NULL();
PG_RETURN_DATUM(result);
if (nargs != 2)
PG_RETURN_NULL();
result = OpernameGetOprid(names, argtypes[0], argtypes[1]);
if (!OidIsValid(result))
PG_RETURN_NULL();
PG_RETURN_OID(result);
} }
/* /*
@ -975,25 +925,15 @@ Datum
to_regclass(PG_FUNCTION_ARGS) to_regclass(PG_FUNCTION_ARGS)
{ {
char *class_name = text_to_cstring(PG_GETARG_TEXT_PP(0)); char *class_name = text_to_cstring(PG_GETARG_TEXT_PP(0));
Oid result; Datum result;
List *names;
ErrorSaveContext escontext = {T_ErrorSaveContext}; ErrorSaveContext escontext = {T_ErrorSaveContext};
/* if (!DirectInputFunctionCallSafe(regclassin, class_name,
* Parse the name into components and see if it matches any pg_class InvalidOid, -1,
* entries in the current search path. (Node *) &escontext,
*/ &result))
names = stringToQualifiedNameList(class_name, (Node *) &escontext);
if (names == NIL)
PG_RETURN_NULL();
/* We might not even have permissions on this relation; don't lock it. */
result = RangeVarGetRelid(makeRangeVarFromNameList(names), NoLock, true);
if (OidIsValid(result))
PG_RETURN_OID(result);
else
PG_RETURN_NULL(); PG_RETURN_NULL();
PG_RETURN_DATUM(result);
} }
/* /*
@ -1128,24 +1068,15 @@ Datum
to_regcollation(PG_FUNCTION_ARGS) to_regcollation(PG_FUNCTION_ARGS)
{ {
char *collation_name = text_to_cstring(PG_GETARG_TEXT_PP(0)); char *collation_name = text_to_cstring(PG_GETARG_TEXT_PP(0));
Oid result; Datum result;
List *names;
ErrorSaveContext escontext = {T_ErrorSaveContext}; ErrorSaveContext escontext = {T_ErrorSaveContext};
/* if (!DirectInputFunctionCallSafe(regcollationin, collation_name,
* Parse the name into components and see if it matches any pg_collation InvalidOid, -1,
* entries in the current search path. (Node *) &escontext,
*/ &result))
names = stringToQualifiedNameList(collation_name, (Node *) &escontext);
if (names == NIL)
PG_RETURN_NULL();
result = get_collation_oid(names, true);
if (OidIsValid(result))
PG_RETURN_OID(result);
else
PG_RETURN_NULL(); PG_RETURN_NULL();
PG_RETURN_DATUM(result);
} }
/* /*
@ -1278,17 +1209,15 @@ Datum
to_regtype(PG_FUNCTION_ARGS) to_regtype(PG_FUNCTION_ARGS)
{ {
char *typ_name = text_to_cstring(PG_GETARG_TEXT_PP(0)); char *typ_name = text_to_cstring(PG_GETARG_TEXT_PP(0));
Oid result; Datum result;
int32 typmod;
ErrorSaveContext escontext = {T_ErrorSaveContext}; ErrorSaveContext escontext = {T_ErrorSaveContext};
/* if (!DirectInputFunctionCallSafe(regtypein, typ_name,
* Invoke the full parser to deal with special cases such as array syntax. InvalidOid, -1,
*/ (Node *) &escontext,
if (parseTypeString(typ_name, &result, &typmod, (Node *) &escontext)) &result))
PG_RETURN_OID(result);
else
PG_RETURN_NULL(); PG_RETURN_NULL();
PG_RETURN_DATUM(result);
} }
/* /*
@ -1634,23 +1563,15 @@ Datum
to_regrole(PG_FUNCTION_ARGS) to_regrole(PG_FUNCTION_ARGS)
{ {
char *role_name = text_to_cstring(PG_GETARG_TEXT_PP(0)); char *role_name = text_to_cstring(PG_GETARG_TEXT_PP(0));
Oid result; Datum result;
List *names;
ErrorSaveContext escontext = {T_ErrorSaveContext}; ErrorSaveContext escontext = {T_ErrorSaveContext};
names = stringToQualifiedNameList(role_name, (Node *) &escontext); if (!DirectInputFunctionCallSafe(regrolein, role_name,
if (names == NIL) InvalidOid, -1,
PG_RETURN_NULL(); (Node *) &escontext,
&result))
if (list_length(names) != 1)
PG_RETURN_NULL();
result = get_role_oid(strVal(linitial(names)), true);
if (OidIsValid(result))
PG_RETURN_OID(result);
else
PG_RETURN_NULL(); PG_RETURN_NULL();
PG_RETURN_DATUM(result);
} }
/* /*
@ -1759,23 +1680,15 @@ Datum
to_regnamespace(PG_FUNCTION_ARGS) to_regnamespace(PG_FUNCTION_ARGS)
{ {
char *nsp_name = text_to_cstring(PG_GETARG_TEXT_PP(0)); char *nsp_name = text_to_cstring(PG_GETARG_TEXT_PP(0));
Oid result; Datum result;
List *names;
ErrorSaveContext escontext = {T_ErrorSaveContext}; ErrorSaveContext escontext = {T_ErrorSaveContext};
names = stringToQualifiedNameList(nsp_name, (Node *) &escontext); if (!DirectInputFunctionCallSafe(regnamespacein, nsp_name,
if (names == NIL) InvalidOid, -1,
PG_RETURN_NULL(); (Node *) &escontext,
&result))
if (list_length(names) != 1)
PG_RETURN_NULL();
result = get_namespace_oid(strVal(linitial(names)), true);
if (OidIsValid(result))
PG_RETURN_OID(result);
else
PG_RETURN_NULL(); PG_RETURN_NULL();
PG_RETURN_DATUM(result);
} }
/* /*