Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit d3d53f9

Browse filesBrowse files
committed
Add a way to get the current function's OID in pl/pgsql.
Invent "GET DIAGNOSTICS oid_variable = PG_ROUTINE_OID". This is useful for avoiding the maintenance nuisances that come with embedding a function's name in its body, as one might do for logging purposes for example. Typically users would cast the result to regproc or regprocedure to get something human-readable, but we won't pre-judge whether that's appropriate. Pavel Stehule, reviewed by Kirk Wolak and myself Discussion: https://postgr.es/m/CAFj8pRA4zMd5pY-B89Gm64bDLRt-L+akOd34aD1j4PEstHHSVQ@mail.gmail.com
1 parent 4826759 commit d3d53f9
Copy full SHA for d3d53f9

File tree

8 files changed

+77
-4
lines changed
Filter options

8 files changed

+77
-4
lines changed

‎doc/src/sgml/plpgsql.sgml

Copy file name to clipboardExpand all lines: doc/src/sgml/plpgsql.sgml
+5
Original file line numberDiff line numberDiff line change
@@ -1639,6 +1639,11 @@ GET DIAGNOSTICS integer_var = ROW_COUNT;
16391639
<entry>line(s) of text describing the current call stack
16401640
(see <xref linkend="plpgsql-call-stack"/>)</entry>
16411641
</row>
1642+
<row>
1643+
<entry><literal>PG_ROUTINE_OID</literal></entry>
1644+
<entry><type>oid</type></entry>
1645+
<entry>OID of the current function</entry>
1646+
</row>
16421647
</tbody>
16431648
</tgroup>
16441649
</table>

‎src/pl/plpgsql/src/pl_exec.c

Copy file name to clipboardExpand all lines: src/pl/plpgsql/src/pl_exec.c
+6
Original file line numberDiff line numberDiff line change
@@ -2411,6 +2411,12 @@ exec_stmt_getdiag(PLpgSQL_execstate *estate, PLpgSQL_stmt_getdiag *stmt)
24112411
false, INT8OID, -1);
24122412
break;
24132413

2414+
case PLPGSQL_GETDIAG_ROUTINE_OID:
2415+
exec_assign_value(estate, var,
2416+
ObjectIdGetDatum(estate->func->fn_oid),
2417+
false, OIDOID, -1);
2418+
break;
2419+
24142420
case PLPGSQL_GETDIAG_ERROR_CONTEXT:
24152421
exec_assign_c_string(estate, var,
24162422
estate->cur_error->context);

‎src/pl/plpgsql/src/pl_funcs.c

Copy file name to clipboardExpand all lines: src/pl/plpgsql/src/pl_funcs.c
+2
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,8 @@ plpgsql_getdiag_kindname(PLpgSQL_getdiag_kind kind)
303303
{
304304
case PLPGSQL_GETDIAG_ROW_COUNT:
305305
return "ROW_COUNT";
306+
case PLPGSQL_GETDIAG_ROUTINE_OID:
307+
return "PG_ROUTINE_OID";
306308
case PLPGSQL_GETDIAG_CONTEXT:
307309
return "PG_CONTEXT";
308310
case PLPGSQL_GETDIAG_ERROR_CONTEXT:

‎src/pl/plpgsql/src/pl_gram.y

Copy file name to clipboardExpand all lines: src/pl/plpgsql/src/pl_gram.y
+6
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,7 @@ static void check_raise_parameters(PLpgSQL_stmt_raise *stmt);
322322
%token <keyword> K_PG_EXCEPTION_CONTEXT
323323
%token <keyword> K_PG_EXCEPTION_DETAIL
324324
%token <keyword> K_PG_EXCEPTION_HINT
325+
%token <keyword> K_PG_ROUTINE_OID
325326
%token <keyword> K_PRINT_STRICT_PARAMS
326327
%token <keyword> K_PRIOR
327328
%token <keyword> K_QUERY
@@ -1008,6 +1009,7 @@ stmt_getdiag : K_GET getdiag_area_opt K_DIAGNOSTICS getdiag_list ';'
10081009
{
10091010
/* these fields are disallowed in stacked case */
10101011
case PLPGSQL_GETDIAG_ROW_COUNT:
1012+
case PLPGSQL_GETDIAG_ROUTINE_OID:
10111013
if (new->is_stacked)
10121014
ereport(ERROR,
10131015
(errcode(ERRCODE_SYNTAX_ERROR),
@@ -1090,6 +1092,9 @@ getdiag_item :
10901092
if (tok_is_keyword(tok, &yylval,
10911093
K_ROW_COUNT, "row_count"))
10921094
$$ = PLPGSQL_GETDIAG_ROW_COUNT;
1095+
else if (tok_is_keyword(tok, &yylval,
1096+
K_PG_ROUTINE_OID, "pg_routine_oid"))
1097+
$$ = PLPGSQL_GETDIAG_ROUTINE_OID;
10931098
else if (tok_is_keyword(tok, &yylval,
10941099
K_PG_CONTEXT, "pg_context"))
10951100
$$ = PLPGSQL_GETDIAG_CONTEXT;
@@ -2528,6 +2533,7 @@ unreserved_keyword :
25282533
| K_PG_EXCEPTION_CONTEXT
25292534
| K_PG_EXCEPTION_DETAIL
25302535
| K_PG_EXCEPTION_HINT
2536+
| K_PG_ROUTINE_OID
25312537
| K_PRINT_STRICT_PARAMS
25322538
| K_PRIOR
25332539
| K_QUERY

‎src/pl/plpgsql/src/pl_unreserved_kwlist.h

Copy file name to clipboardExpand all lines: src/pl/plpgsql/src/pl_unreserved_kwlist.h
+1
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ PG_KEYWORD("pg_datatype_name", K_PG_DATATYPE_NAME)
8585
PG_KEYWORD("pg_exception_context", K_PG_EXCEPTION_CONTEXT)
8686
PG_KEYWORD("pg_exception_detail", K_PG_EXCEPTION_DETAIL)
8787
PG_KEYWORD("pg_exception_hint", K_PG_EXCEPTION_HINT)
88+
PG_KEYWORD("pg_routine_oid", K_PG_ROUTINE_OID)
8889
PG_KEYWORD("print_strict_params", K_PRINT_STRICT_PARAMS)
8990
PG_KEYWORD("prior", K_PRIOR)
9091
PG_KEYWORD("query", K_QUERY)

‎src/pl/plpgsql/src/plpgsql.h

Copy file name to clipboardExpand all lines: src/pl/plpgsql/src/plpgsql.h
+1
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ enum
147147
typedef enum PLpgSQL_getdiag_kind
148148
{
149149
PLPGSQL_GETDIAG_ROW_COUNT,
150+
PLPGSQL_GETDIAG_ROUTINE_OID,
150151
PLPGSQL_GETDIAG_CONTEXT,
151152
PLPGSQL_GETDIAG_ERROR_CONTEXT,
152153
PLPGSQL_GETDIAG_ERROR_DETAIL,

‎src/test/regress/expected/plpgsql.out

Copy file name to clipboardExpand all lines: src/test/regress/expected/plpgsql.out
+29-2
Original file line numberDiff line numberDiff line change
@@ -5229,7 +5229,7 @@ NOTICE: outer_func() done
52295229
20
52305230
(1 row)
52315231

5232-
-- repeated call should to work
5232+
-- repeated call should work
52335233
select outer_outer_func(20);
52345234
NOTICE: calling down into outer_func()
52355235
NOTICE: calling down into inner_func()
@@ -5311,7 +5311,7 @@ NOTICE: outer_func() done
53115311
20
53125312
(1 row)
53135313

5314-
-- repeated call should to work
5314+
-- repeated call should work
53155315
select outer_outer_func(20);
53165316
NOTICE: calling down into outer_func()
53175317
NOTICE: calling down into inner_func()
@@ -5332,6 +5332,33 @@ NOTICE: outer_func() done
53325332
drop function outer_outer_func(int);
53335333
drop function outer_func(int);
53345334
drop function inner_func(int);
5335+
-- Test pg_routine_oid
5336+
create function current_function(text)
5337+
returns regprocedure as $$
5338+
declare
5339+
fn_oid regprocedure;
5340+
begin
5341+
get diagnostics fn_oid = pg_routine_oid;
5342+
return fn_oid;
5343+
end;
5344+
$$ language plpgsql;
5345+
select current_function('foo');
5346+
current_function
5347+
------------------------
5348+
current_function(text)
5349+
(1 row)
5350+
5351+
drop function current_function(text);
5352+
-- shouldn't fail in DO, even though there's no useful data
5353+
do $$
5354+
declare
5355+
fn_oid oid;
5356+
begin
5357+
get diagnostics fn_oid = pg_routine_oid;
5358+
raise notice 'pg_routine_oid = %', fn_oid;
5359+
end;
5360+
$$;
5361+
NOTICE: pg_routine_oid = 0
53355362
--
53365363
-- Test ASSERT
53375364
--

‎src/test/regress/sql/plpgsql.sql

Copy file name to clipboardExpand all lines: src/test/regress/sql/plpgsql.sql
+27-2
Original file line numberDiff line numberDiff line change
@@ -4206,7 +4206,7 @@ end;
42064206
$$ language plpgsql;
42074207
42084208
select outer_outer_func(10);
4209-
-- repeated call should to work
4209+
-- repeated call should work
42104210
select outer_outer_func(20);
42114211
42124212
drop function outer_outer_func(int);
@@ -4261,13 +4261,38 @@ end;
42614261
$$ language plpgsql;
42624262
42634263
select outer_outer_func(10);
4264-
-- repeated call should to work
4264+
-- repeated call should work
42654265
select outer_outer_func(20);
42664266
42674267
drop function outer_outer_func(int);
42684268
drop function outer_func(int);
42694269
drop function inner_func(int);
42704270
4271+
-- Test pg_routine_oid
4272+
create function current_function(text)
4273+
returns regprocedure as $$
4274+
declare
4275+
fn_oid regprocedure;
4276+
begin
4277+
get diagnostics fn_oid = pg_routine_oid;
4278+
return fn_oid;
4279+
end;
4280+
$$ language plpgsql;
4281+
4282+
select current_function('foo');
4283+
4284+
drop function current_function(text);
4285+
4286+
-- shouldn't fail in DO, even though there's no useful data
4287+
do $$
4288+
declare
4289+
fn_oid oid;
4290+
begin
4291+
get diagnostics fn_oid = pg_routine_oid;
4292+
raise notice 'pg_routine_oid = %', fn_oid;
4293+
end;
4294+
$$;
4295+
42714296
--
42724297
-- Test ASSERT
42734298
--

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.