Skip to content

Commit dfc0219

Browse files
committed
Add to_regprocedure() and to_regoperator().
These are natural complements to the functions added by commit 0886fc6, but they weren't included in the original patch for some reason. Add them. Patch by me, per a complaint by Tom Lane. Review by Tatsuo Ishii.
1 parent 1a81daa commit dfc0219

File tree

7 files changed

+199
-5
lines changed

7 files changed

+199
-5
lines changed

doc/src/sgml/func.sgml

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15293,10 +15293,18 @@ SELECT pg_type_is_visible('myschema.widget'::regtype);
1529315293
<primary>to_regproc</primary>
1529415294
</indexterm>
1529515295

15296+
<indexterm>
15297+
<primary>to_regprocedure</primary>
15298+
</indexterm>
15299+
1529615300
<indexterm>
1529715301
<primary>to_regoper</primary>
1529815302
</indexterm>
1529915303

15304+
<indexterm>
15305+
<primary>to_regoperator</primary>
15306+
</indexterm>
15307+
1530015308
<indexterm>
1530115309
<primary>to_regtype</primary>
1530215310
</indexterm>
@@ -15481,11 +15489,21 @@ SELECT pg_type_is_visible('myschema.widget'::regtype);
1548115489
<entry><type>regproc</type></entry>
1548215490
<entry>get the oid of the named function</entry>
1548315491
</row>
15492+
<row>
15493+
<entry><literal><function>to_regprocedure(<parameter>func_name</parameter>)</function></literal></entry>
15494+
<entry><type>regprocedure</type></entry>
15495+
<entry>get the oid of the named function</entry>
15496+
</row>
1548415497
<row>
1548515498
<entry><literal><function>to_regoper(<parameter>operator_name</parameter>)</function></literal></entry>
1548615499
<entry><type>regoper</type></entry>
1548715500
<entry>get the oid of the named operator</entry>
1548815501
</row>
15502+
<row>
15503+
<entry><literal><function>to_regoperator(<parameter>operator_name</parameter>)</function></literal></entry>
15504+
<entry><type>regoperator</type></entry>
15505+
<entry>get the oid of the named operator</entry>
15506+
</row>
1548915507
<row>
1549015508
<entry><literal><function>to_regtype(<parameter>type_name</parameter>)</function></literal></entry>
1549115509
<entry><type>regtype</type></entry>
@@ -15658,10 +15676,12 @@ SELECT collation for ('foo' COLLATE "de_DE");
1565815676

1565915677
<para>
1566015678
The <function>to_regclass</function>, <function>to_regproc</function>,
15661-
<function>to_regoper</function> and <function>to_regtype</function>
15662-
translate relation, function, operator, and type names to objects of
15663-
type <type>regclass</>, <type>regproc</>, <type>regoper</> and
15664-
<type>regtype</>, respectively. These functions differ from a cast from
15679+
<function>to_regprocedure</function>, <function>to_regoper</function>,
15680+
<function>to_regoperator</function>, and <function>to_regtype</function>
15681+
functions translate relation, function, operator, and type names to objects
15682+
of type <type>regclass</>, <type>regproc</>, <type>regprocedure</type>,
15683+
<type>regoper</>, <type>regoperator</type>, and <type>regtype</>,
15684+
respectively. These functions differ from a cast from
1566515685
text in that they don't accept a numeric OID, and that they return null
1566615686
rather than throwing an error if the name is not found (or, for
1566715687
<function>to_regproc</function> and <function>to_regoper</function>, if

src/backend/utils/adt/regproc.c

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,38 @@ regprocedurein(PG_FUNCTION_ARGS)
322322
PG_RETURN_OID(result);
323323
}
324324

325+
/*
326+
* to_regprocedure - converts "proname(args)" to proc OID
327+
*
328+
* If the name is not found, we return NULL.
329+
*/
330+
Datum
331+
to_regprocedure(PG_FUNCTION_ARGS)
332+
{
333+
char *pro_name = PG_GETARG_CSTRING(0);
334+
List *names;
335+
int nargs;
336+
Oid argtypes[FUNC_MAX_ARGS];
337+
FuncCandidateList clist;
338+
339+
/*
340+
* Parse the name and arguments, look up potential matches in the current
341+
* namespace search list, and scan to see which one exactly matches the
342+
* given argument types. (There will not be more than one match.)
343+
*/
344+
parseNameAndArgTypes(pro_name, false, &names, &nargs, argtypes);
345+
346+
clist = FuncnameGetCandidates(names, nargs, NIL, false, false, true);
347+
348+
for (; clist; clist = clist->next)
349+
{
350+
if (memcmp(clist->args, argtypes, nargs * sizeof(Oid)) == 0)
351+
PG_RETURN_OID(clist->oid);
352+
}
353+
354+
PG_RETURN_NULL();
355+
}
356+
325357
/*
326358
* format_procedure - converts proc OID to "pro_name(args)"
327359
*
@@ -721,6 +753,45 @@ regoperatorin(PG_FUNCTION_ARGS)
721753
PG_RETURN_OID(result);
722754
}
723755

756+
/*
757+
* to_regoperator - converts "oprname(args)" to operator OID
758+
*
759+
* If the name is not found, we return NULL.
760+
*/
761+
Datum
762+
to_regoperator(PG_FUNCTION_ARGS)
763+
{
764+
char *opr_name_or_oid = PG_GETARG_CSTRING(0);
765+
Oid result;
766+
List *names;
767+
int nargs;
768+
Oid argtypes[FUNC_MAX_ARGS];
769+
770+
/*
771+
* Parse the name and arguments, look up potential matches in the current
772+
* namespace search list, and scan to see which one exactly matches the
773+
* given argument types. (There will not be more than one match.)
774+
*/
775+
parseNameAndArgTypes(opr_name_or_oid, true, &names, &nargs, argtypes);
776+
if (nargs == 1)
777+
ereport(ERROR,
778+
(errcode(ERRCODE_UNDEFINED_PARAMETER),
779+
errmsg("missing argument"),
780+
errhint("Use NONE to denote the missing argument of a unary operator.")));
781+
if (nargs != 2)
782+
ereport(ERROR,
783+
(errcode(ERRCODE_TOO_MANY_ARGUMENTS),
784+
errmsg("too many arguments"),
785+
errhint("Provide two argument types for operator.")));
786+
787+
result = OpernameGetOprid(names, argtypes[0], argtypes[1]);
788+
789+
if (!OidIsValid(result))
790+
PG_RETURN_NULL();
791+
792+
PG_RETURN_OID(result);
793+
}
794+
724795
/*
725796
* format_operator - converts operator OID to "opr_name(args)"
726797
*

src/include/catalog/catversion.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,6 @@
5353
*/
5454

5555
/* yyyymmddN */
56-
#define CATALOG_VERSION_NO 201404123
56+
#define CATALOG_VERSION_NO 201404161
5757

5858
#endif

src/include/catalog/pg_proc.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,8 @@ DATA(insert OID = 45 ( regprocout PGNSP PGUID 12 1 0 0 0 f f f f t f s 1 0
175175
DESCR("I/O");
176176
DATA(insert OID = 3494 ( to_regproc PGNSP PGUID 12 1 0 0 0 f f f f t f s 1 0 24 "2275" _null_ _null_ _null_ _null_ to_regproc _null_ _null_ _null_ ));
177177
DESCR("convert proname to regproc");
178+
DATA(insert OID = 3479 ( to_regprocedure PGNSP PGUID 12 1 0 0 0 f f f f t f s 1 0 2202 "2275" _null_ _null_ _null_ _null_ to_regprocedure _null_ _null_ _null_ ));
179+
DESCR("convert proname to regprocedure");
178180
DATA(insert OID = 46 ( textin PGNSP PGUID 12 1 0 0 0 f f f f t f i 1 0 25 "2275" _null_ _null_ _null_ _null_ textin _null_ _null_ _null_ ));
179181
DESCR("I/O");
180182
DATA(insert OID = 47 ( textout PGNSP PGUID 12 1 0 0 0 f f f f t f i 1 0 2275 "25" _null_ _null_ _null_ _null_ textout _null_ _null_ _null_ ));
@@ -3351,6 +3353,8 @@ DATA(insert OID = 2215 ( regoperout PGNSP PGUID 12 1 0 0 0 f f f f t f s 1 0 2
33513353
DESCR("I/O");
33523354
DATA(insert OID = 3492 ( to_regoper PGNSP PGUID 12 1 0 0 0 f f f f t f s 1 0 2203 "2275" _null_ _null_ _null_ _null_ to_regoper _null_ _null_ _null_ ));
33533355
DESCR("convert operator name to regoper");
3356+
DATA(insert OID = 3476 ( to_regoperator PGNSP PGUID 12 1 0 0 0 f f f f t f s 1 0 2204 "2275" _null_ _null_ _null_ _null_ to_regoperator _null_ _null_ _null_ ));
3357+
DESCR("convert operator name to regoperator");
33543358
DATA(insert OID = 2216 ( regoperatorin PGNSP PGUID 12 1 0 0 0 f f f f t f s 1 0 2204 "2275" _null_ _null_ _null_ _null_ regoperatorin _null_ _null_ _null_ ));
33553359
DESCR("I/O");
33563360
DATA(insert OID = 2217 ( regoperatorout PGNSP PGUID 12 1 0 0 0 f f f f t f s 1 0 2275 "2204" _null_ _null_ _null_ _null_ regoperatorout _null_ _null_ _null_ ));

src/include/utils/builtins.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -602,6 +602,7 @@ extern char *regexp_fixed_prefix(text *text_re, bool case_insensitive,
602602
extern Datum regprocin(PG_FUNCTION_ARGS);
603603
extern Datum regprocout(PG_FUNCTION_ARGS);
604604
extern Datum to_regproc(PG_FUNCTION_ARGS);
605+
extern Datum to_regprocedure(PG_FUNCTION_ARGS);
605606
extern Datum regprocrecv(PG_FUNCTION_ARGS);
606607
extern Datum regprocsend(PG_FUNCTION_ARGS);
607608
extern Datum regprocedurein(PG_FUNCTION_ARGS);
@@ -613,6 +614,7 @@ extern Datum regoperout(PG_FUNCTION_ARGS);
613614
extern Datum regoperrecv(PG_FUNCTION_ARGS);
614615
extern Datum regopersend(PG_FUNCTION_ARGS);
615616
extern Datum to_regoper(PG_FUNCTION_ARGS);
617+
extern Datum to_regoperator(PG_FUNCTION_ARGS);
616618
extern Datum regoperatorin(PG_FUNCTION_ARGS);
617619
extern Datum regoperatorout(PG_FUNCTION_ARGS);
618620
extern Datum regoperatorrecv(PG_FUNCTION_ARGS);

src/test/regress/expected/regproc.out

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,24 @@ SELECT regoper('||/');
99
||/
1010
(1 row)
1111

12+
SELECT regoperator('+(int4,int4)');
13+
regoperator
14+
--------------------
15+
+(integer,integer)
16+
(1 row)
17+
1218
SELECT regproc('now');
1319
regproc
1420
---------
1521
now
1622
(1 row)
1723

24+
SELECT regprocedure('abs(numeric)');
25+
regprocedure
26+
--------------
27+
abs(numeric)
28+
(1 row)
29+
1830
SELECT regclass('pg_class');
1931
regclass
2032
----------
@@ -33,12 +45,24 @@ SELECT to_regoper('||/');
3345
||/
3446
(1 row)
3547

48+
SELECT to_regoperator('+(int4,int4)');
49+
to_regoperator
50+
--------------------
51+
+(integer,integer)
52+
(1 row)
53+
3654
SELECT to_regproc('now');
3755
to_regproc
3856
------------
3957
now
4058
(1 row)
4159

60+
SELECT to_regprocedure('abs(numeric)');
61+
to_regprocedure
62+
-----------------
63+
abs(numeric)
64+
(1 row)
65+
4266
SELECT to_regclass('pg_class');
4367
to_regclass
4468
-------------
@@ -58,12 +82,24 @@ SELECT regoper('pg_catalog.||/');
5882
||/
5983
(1 row)
6084

85+
SELECT regoperator('pg_catalog.+(int4,int4)');
86+
regoperator
87+
--------------------
88+
+(integer,integer)
89+
(1 row)
90+
6191
SELECT regproc('pg_catalog.now');
6292
regproc
6393
---------
6494
now
6595
(1 row)
6696

97+
SELECT regprocedure('pg_catalog.abs(numeric)');
98+
regprocedure
99+
--------------
100+
abs(numeric)
101+
(1 row)
102+
67103
SELECT regclass('pg_catalog.pg_class');
68104
regclass
69105
----------
@@ -88,6 +124,12 @@ SELECT to_regproc('pg_catalog.now');
88124
now
89125
(1 row)
90126

127+
SELECT to_regprocedure('pg_catalog.abs(numeric)');
128+
to_regprocedure
129+
-----------------
130+
abs(numeric)
131+
(1 row)
132+
91133
SELECT to_regclass('pg_catalog.pg_class');
92134
to_regclass
93135
-------------
@@ -106,10 +148,18 @@ SELECT regoper('||//');
106148
ERROR: operator does not exist: ||//
107149
LINE 3: SELECT regoper('||//');
108150
^
151+
SELECT regoperator('++(int4,int4)');
152+
ERROR: operator does not exist: ++(int4,int4)
153+
LINE 1: SELECT regoperator('++(int4,int4)');
154+
^
109155
SELECT regproc('know');
110156
ERROR: function "know" does not exist
111157
LINE 1: SELECT regproc('know');
112158
^
159+
SELECT regprocedure('absinthe(numeric)');
160+
ERROR: function "absinthe(numeric)" does not exist
161+
LINE 1: SELECT regprocedure('absinthe(numeric)');
162+
^
113163
SELECT regclass('pg_classes');
114164
ERROR: relation "pg_classes" does not exist
115165
LINE 1: SELECT regclass('pg_classes');
@@ -123,10 +173,18 @@ SELECT regoper('ng_catalog.||/');
123173
ERROR: schema "ng_catalog" does not exist
124174
LINE 1: SELECT regoper('ng_catalog.||/');
125175
^
176+
SELECT regoperator('ng_catalog.+(int4,int4)');
177+
ERROR: operator does not exist: ng_catalog.+(int4,int4)
178+
LINE 1: SELECT regoperator('ng_catalog.+(int4,int4)');
179+
^
126180
SELECT regproc('ng_catalog.now');
127181
ERROR: schema "ng_catalog" does not exist
128182
LINE 1: SELECT regproc('ng_catalog.now');
129183
^
184+
SELECT regprocedure('ng_catalog.abs(numeric)');
185+
ERROR: schema "ng_catalog" does not exist
186+
LINE 1: SELECT regprocedure('ng_catalog.abs(numeric)');
187+
^
130188
SELECT regclass('ng_catalog.pg_class');
131189
ERROR: schema "ng_catalog" does not exist
132190
LINE 1: SELECT regclass('ng_catalog.pg_class');
@@ -143,12 +201,24 @@ SELECT to_regoper('||//');
143201

144202
(1 row)
145203

204+
SELECT to_regoperator('++(int4,int4)');
205+
to_regoperator
206+
----------------
207+
208+
(1 row)
209+
146210
SELECT to_regproc('know');
147211
to_regproc
148212
------------
149213

150214
(1 row)
151215

216+
SELECT to_regprocedure('absinthe(numeric)');
217+
to_regprocedure
218+
-----------------
219+
220+
(1 row)
221+
152222
SELECT to_regclass('pg_classes');
153223
to_regclass
154224
-------------
@@ -168,12 +238,24 @@ SELECT to_regoper('ng_catalog.||/');
168238

169239
(1 row)
170240

241+
SELECT to_regoperator('ng_catalog.+(int4,int4)');
242+
to_regoperator
243+
----------------
244+
245+
(1 row)
246+
171247
SELECT to_regproc('ng_catalog.now');
172248
to_regproc
173249
------------
174250

175251
(1 row)
176252

253+
SELECT to_regprocedure('ng_catalog.abs(numeric)');
254+
to_regprocedure
255+
-----------------
256+
257+
(1 row)
258+
177259
SELECT to_regclass('ng_catalog.pg_class');
178260
to_regclass
179261
-------------

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy