Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 3ea7329

Browse files
committed
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
1 parent 858e776 commit 3ea7329

File tree

2 files changed

+63
-159
lines changed

2 files changed

+63
-159
lines changed

doc/src/sgml/func.sgml

+9-18
Original file line numberDiff line numberDiff line change
@@ -24100,8 +24100,7 @@ SELECT collation for ('foo' COLLATE "de_DE");
2410024100
obtained by casting the string to type <type>regclass</type> (see
2410124101
<xref linkend="datatype-oid"/>); however, this function will return
2410224102
<literal>NULL</literal> rather than throwing an error if the name is
24103-
not found. Also unlike the cast, this does not accept
24104-
a numeric OID as input.
24103+
not found.
2410524104
</para></entry>
2410624105
</row>
2410724106

@@ -24118,8 +24117,7 @@ SELECT collation for ('foo' COLLATE "de_DE");
2411824117
obtained by casting the string to type <type>regcollation</type> (see
2411924118
<xref linkend="datatype-oid"/>); however, this function will return
2412024119
<literal>NULL</literal> rather than throwing an error if the name is
24121-
not found. Also unlike the cast, this does not accept
24122-
a numeric OID as input.
24120+
not found.
2412324121
</para></entry>
2412424122
</row>
2412524123

@@ -24136,8 +24134,7 @@ SELECT collation for ('foo' COLLATE "de_DE");
2413624134
obtained by casting the string to type <type>regnamespace</type> (see
2413724135
<xref linkend="datatype-oid"/>); however, this function will return
2413824136
<literal>NULL</literal> rather than throwing an error if the name is
24139-
not found. Also unlike the cast, this does not accept
24140-
a numeric OID as input.
24137+
not found.
2414124138
</para></entry>
2414224139
</row>
2414324140

@@ -24154,8 +24151,7 @@ SELECT collation for ('foo' COLLATE "de_DE");
2415424151
obtained by casting the string to type <type>regoper</type> (see
2415524152
<xref linkend="datatype-oid"/>); however, this function will return
2415624153
<literal>NULL</literal> rather than throwing an error if the name is
24157-
not found or is ambiguous. Also unlike the cast, this does not accept
24158-
a numeric OID as input.
24154+
not found or is ambiguous.
2415924155
</para></entry>
2416024156
</row>
2416124157

@@ -24172,8 +24168,7 @@ SELECT collation for ('foo' COLLATE "de_DE");
2417224168
obtained by casting the string to type <type>regoperator</type> (see
2417324169
<xref linkend="datatype-oid"/>); however, this function will return
2417424170
<literal>NULL</literal> rather than throwing an error if the name is
24175-
not found. Also unlike the cast, this does not accept
24176-
a numeric OID as input.
24171+
not found.
2417724172
</para></entry>
2417824173
</row>
2417924174

@@ -24190,8 +24185,7 @@ SELECT collation for ('foo' COLLATE "de_DE");
2419024185
obtained by casting the string to type <type>regproc</type> (see
2419124186
<xref linkend="datatype-oid"/>); however, this function will return
2419224187
<literal>NULL</literal> rather than throwing an error if the name is
24193-
not found or is ambiguous. Also unlike the cast, this does not accept
24194-
a numeric OID as input.
24188+
not found or is ambiguous.
2419524189
</para></entry>
2419624190
</row>
2419724191

@@ -24208,8 +24202,7 @@ SELECT collation for ('foo' COLLATE "de_DE");
2420824202
obtained by casting the string to type <type>regprocedure</type> (see
2420924203
<xref linkend="datatype-oid"/>); however, this function will return
2421024204
<literal>NULL</literal> rather than throwing an error if the name is
24211-
not found. Also unlike the cast, this does not accept
24212-
a numeric OID as input.
24205+
not found.
2421324206
</para></entry>
2421424207
</row>
2421524208

@@ -24226,8 +24219,7 @@ SELECT collation for ('foo' COLLATE "de_DE");
2422624219
obtained by casting the string to type <type>regrole</type> (see
2422724220
<xref linkend="datatype-oid"/>); however, this function will return
2422824221
<literal>NULL</literal> rather than throwing an error if the name is
24229-
not found. Also unlike the cast, this does not accept
24230-
a numeric OID as input.
24222+
not found.
2423124223
</para></entry>
2423224224
</row>
2423324225

@@ -24244,8 +24236,7 @@ SELECT collation for ('foo' COLLATE "de_DE");
2424424236
obtained by casting the string to type <type>regtype</type> (see
2424524237
<xref linkend="datatype-oid"/>); however, this function will return
2424624238
<literal>NULL</literal> rather than throwing an error if the name is
24247-
not found. Also unlike the cast, this does not accept
24248-
a numeric OID as input.
24239+
not found.
2424924240
</para></entry>
2425024241
</row>
2425124242
</tbody>

src/backend/utils/adt/regproc.c

+54-141
Original file line numberDiff line numberDiff line change
@@ -118,24 +118,15 @@ Datum
118118
to_regproc(PG_FUNCTION_ARGS)
119119
{
120120
char *pro_name = text_to_cstring(PG_GETARG_TEXT_PP(0));
121-
List *names;
122-
FuncCandidateList clist;
121+
Datum result;
123122
ErrorSaveContext escontext = {T_ErrorSaveContext};
124123

125-
/*
126-
* Parse the name into components and see if it matches any pg_proc
127-
* entries in the current search path.
128-
*/
129-
names = stringToQualifiedNameList(pro_name, (Node *) &escontext);
130-
if (names == NIL)
131-
PG_RETURN_NULL();
132-
133-
clist = FuncnameGetCandidates(names, -1, NIL, false, false, false, true);
134-
135-
if (clist == NULL || clist->next != NULL)
124+
if (!DirectInputFunctionCallSafe(regprocin, pro_name,
125+
InvalidOid, -1,
126+
(Node *) &escontext,
127+
&result))
136128
PG_RETURN_NULL();
137-
138-
PG_RETURN_OID(clist->oid);
129+
PG_RETURN_DATUM(result);
139130
}
140131

141132
/*
@@ -287,31 +278,15 @@ Datum
287278
to_regprocedure(PG_FUNCTION_ARGS)
288279
{
289280
char *pro_name = text_to_cstring(PG_GETARG_TEXT_PP(0));
290-
List *names;
291-
int nargs;
292-
Oid argtypes[FUNC_MAX_ARGS];
293-
FuncCandidateList clist;
281+
Datum result;
294282
ErrorSaveContext escontext = {T_ErrorSaveContext};
295283

296-
/*
297-
* Parse the name and arguments, look up potential matches in the current
298-
* namespace search list, and scan to see which one exactly matches the
299-
* given argument types. (There will not be more than one match.)
300-
*/
301-
if (!parseNameAndArgTypes(pro_name, false,
302-
&names, &nargs, argtypes,
303-
(Node *) &escontext))
284+
if (!DirectInputFunctionCallSafe(regprocedurein, pro_name,
285+
InvalidOid, -1,
286+
(Node *) &escontext,
287+
&result))
304288
PG_RETURN_NULL();
305-
306-
clist = FuncnameGetCandidates(names, nargs, NIL, false, false, false, true);
307-
308-
for (; clist; clist = clist->next)
309-
{
310-
if (memcmp(clist->args, argtypes, nargs * sizeof(Oid)) == 0)
311-
PG_RETURN_OID(clist->oid);
312-
}
313-
314-
PG_RETURN_NULL();
289+
PG_RETURN_DATUM(result);
315290
}
316291

317292
/*
@@ -552,24 +527,15 @@ Datum
552527
to_regoper(PG_FUNCTION_ARGS)
553528
{
554529
char *opr_name = text_to_cstring(PG_GETARG_TEXT_PP(0));
555-
List *names;
556-
FuncCandidateList clist;
530+
Datum result;
557531
ErrorSaveContext escontext = {T_ErrorSaveContext};
558532

559-
/*
560-
* Parse the name into components and see if it matches any pg_operator
561-
* entries in the current search path.
562-
*/
563-
names = stringToQualifiedNameList(opr_name, (Node *) &escontext);
564-
if (names == NIL)
533+
if (!DirectInputFunctionCallSafe(regoperin, opr_name,
534+
InvalidOid, -1,
535+
(Node *) &escontext,
536+
&result))
565537
PG_RETURN_NULL();
566-
567-
clist = OpernameGetCandidates(names, '\0', true);
568-
569-
if (clist == NULL || clist->next != NULL)
570-
PG_RETURN_NULL();
571-
572-
PG_RETURN_OID(clist->oid);
538+
PG_RETURN_DATUM(result);
573539
}
574540

575541
/*
@@ -728,31 +694,15 @@ Datum
728694
to_regoperator(PG_FUNCTION_ARGS)
729695
{
730696
char *opr_name_or_oid = text_to_cstring(PG_GETARG_TEXT_PP(0));
731-
Oid result;
732-
List *names;
733-
int nargs;
734-
Oid argtypes[FUNC_MAX_ARGS];
697+
Datum result;
735698
ErrorSaveContext escontext = {T_ErrorSaveContext};
736699

737-
/*
738-
* Parse the name and arguments, look up potential matches in the current
739-
* namespace search list, and scan to see which one exactly matches the
740-
* given argument types. (There will not be more than one match.)
741-
*/
742-
if (!parseNameAndArgTypes(opr_name_or_oid, true,
743-
&names, &nargs, argtypes,
744-
(Node *) &escontext))
745-
PG_RETURN_NULL();
746-
747-
if (nargs != 2)
700+
if (!DirectInputFunctionCallSafe(regoperatorin, opr_name_or_oid,
701+
InvalidOid, -1,
702+
(Node *) &escontext,
703+
&result))
748704
PG_RETURN_NULL();
749-
750-
result = OpernameGetOprid(names, argtypes[0], argtypes[1]);
751-
752-
if (!OidIsValid(result))
753-
PG_RETURN_NULL();
754-
755-
PG_RETURN_OID(result);
705+
PG_RETURN_DATUM(result);
756706
}
757707

758708
/*
@@ -975,25 +925,15 @@ Datum
975925
to_regclass(PG_FUNCTION_ARGS)
976926
{
977927
char *class_name = text_to_cstring(PG_GETARG_TEXT_PP(0));
978-
Oid result;
979-
List *names;
928+
Datum result;
980929
ErrorSaveContext escontext = {T_ErrorSaveContext};
981930

982-
/*
983-
* Parse the name into components and see if it matches any pg_class
984-
* entries in the current search path.
985-
*/
986-
names = stringToQualifiedNameList(class_name, (Node *) &escontext);
987-
if (names == NIL)
988-
PG_RETURN_NULL();
989-
990-
/* We might not even have permissions on this relation; don't lock it. */
991-
result = RangeVarGetRelid(makeRangeVarFromNameList(names), NoLock, true);
992-
993-
if (OidIsValid(result))
994-
PG_RETURN_OID(result);
995-
else
931+
if (!DirectInputFunctionCallSafe(regclassin, class_name,
932+
InvalidOid, -1,
933+
(Node *) &escontext,
934+
&result))
996935
PG_RETURN_NULL();
936+
PG_RETURN_DATUM(result);
997937
}
998938

999939
/*
@@ -1128,24 +1068,15 @@ Datum
11281068
to_regcollation(PG_FUNCTION_ARGS)
11291069
{
11301070
char *collation_name = text_to_cstring(PG_GETARG_TEXT_PP(0));
1131-
Oid result;
1132-
List *names;
1071+
Datum result;
11331072
ErrorSaveContext escontext = {T_ErrorSaveContext};
11341073

1135-
/*
1136-
* Parse the name into components and see if it matches any pg_collation
1137-
* entries in the current search path.
1138-
*/
1139-
names = stringToQualifiedNameList(collation_name, (Node *) &escontext);
1140-
if (names == NIL)
1141-
PG_RETURN_NULL();
1142-
1143-
result = get_collation_oid(names, true);
1144-
1145-
if (OidIsValid(result))
1146-
PG_RETURN_OID(result);
1147-
else
1074+
if (!DirectInputFunctionCallSafe(regcollationin, collation_name,
1075+
InvalidOid, -1,
1076+
(Node *) &escontext,
1077+
&result))
11481078
PG_RETURN_NULL();
1079+
PG_RETURN_DATUM(result);
11491080
}
11501081

11511082
/*
@@ -1278,17 +1209,15 @@ Datum
12781209
to_regtype(PG_FUNCTION_ARGS)
12791210
{
12801211
char *typ_name = text_to_cstring(PG_GETARG_TEXT_PP(0));
1281-
Oid result;
1282-
int32 typmod;
1212+
Datum result;
12831213
ErrorSaveContext escontext = {T_ErrorSaveContext};
12841214

1285-
/*
1286-
* Invoke the full parser to deal with special cases such as array syntax.
1287-
*/
1288-
if (parseTypeString(typ_name, &result, &typmod, (Node *) &escontext))
1289-
PG_RETURN_OID(result);
1290-
else
1215+
if (!DirectInputFunctionCallSafe(regtypein, typ_name,
1216+
InvalidOid, -1,
1217+
(Node *) &escontext,
1218+
&result))
12911219
PG_RETURN_NULL();
1220+
PG_RETURN_DATUM(result);
12921221
}
12931222

12941223
/*
@@ -1634,23 +1563,15 @@ Datum
16341563
to_regrole(PG_FUNCTION_ARGS)
16351564
{
16361565
char *role_name = text_to_cstring(PG_GETARG_TEXT_PP(0));
1637-
Oid result;
1638-
List *names;
1566+
Datum result;
16391567
ErrorSaveContext escontext = {T_ErrorSaveContext};
16401568

1641-
names = stringToQualifiedNameList(role_name, (Node *) &escontext);
1642-
if (names == NIL)
1643-
PG_RETURN_NULL();
1644-
1645-
if (list_length(names) != 1)
1646-
PG_RETURN_NULL();
1647-
1648-
result = get_role_oid(strVal(linitial(names)), true);
1649-
1650-
if (OidIsValid(result))
1651-
PG_RETURN_OID(result);
1652-
else
1569+
if (!DirectInputFunctionCallSafe(regrolein, role_name,
1570+
InvalidOid, -1,
1571+
(Node *) &escontext,
1572+
&result))
16531573
PG_RETURN_NULL();
1574+
PG_RETURN_DATUM(result);
16541575
}
16551576

16561577
/*
@@ -1759,23 +1680,15 @@ Datum
17591680
to_regnamespace(PG_FUNCTION_ARGS)
17601681
{
17611682
char *nsp_name = text_to_cstring(PG_GETARG_TEXT_PP(0));
1762-
Oid result;
1763-
List *names;
1683+
Datum result;
17641684
ErrorSaveContext escontext = {T_ErrorSaveContext};
17651685

1766-
names = stringToQualifiedNameList(nsp_name, (Node *) &escontext);
1767-
if (names == NIL)
1768-
PG_RETURN_NULL();
1769-
1770-
if (list_length(names) != 1)
1771-
PG_RETURN_NULL();
1772-
1773-
result = get_namespace_oid(strVal(linitial(names)), true);
1774-
1775-
if (OidIsValid(result))
1776-
PG_RETURN_OID(result);
1777-
else
1686+
if (!DirectInputFunctionCallSafe(regnamespacein, nsp_name,
1687+
InvalidOid, -1,
1688+
(Node *) &escontext,
1689+
&result))
17781690
PG_RETURN_NULL();
1691+
PG_RETURN_DATUM(result);
17791692
}
17801693

17811694
/*

0 commit comments

Comments
 (0)