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
<entry>line(s) of text describing the current call stack
(see <xref linkend="plpgsql-call-stack"/>)</entry>
</row>
+ <row>
+ <entry><literal>PG_ROUTINE_OID</literal></entry>
+ <entry><type>oid</type></entry>
+ <entry>OID of the current function</entry>
+ </row>
</tbody>
</tgroup>
</table>
false, INT8OID, -1);
break;
+ case PLPGSQL_GETDIAG_ROUTINE_OID:
+ exec_assign_value(estate, var,
+ ObjectIdGetDatum(estate->func->fn_oid),
+ false, OIDOID, -1);
+ break;
+
case PLPGSQL_GETDIAG_ERROR_CONTEXT:
exec_assign_c_string(estate, var,
estate->cur_error->context);
{
case PLPGSQL_GETDIAG_ROW_COUNT:
return "ROW_COUNT";
+ case PLPGSQL_GETDIAG_ROUTINE_OID:
+ return "PG_ROUTINE_OID";
case PLPGSQL_GETDIAG_CONTEXT:
return "PG_CONTEXT";
case PLPGSQL_GETDIAG_ERROR_CONTEXT:
%token <keyword> K_PG_EXCEPTION_CONTEXT
%token <keyword> K_PG_EXCEPTION_DETAIL
%token <keyword> K_PG_EXCEPTION_HINT
+%token <keyword> K_PG_ROUTINE_OID
%token <keyword> K_PRINT_STRICT_PARAMS
%token <keyword> K_PRIOR
%token <keyword> K_QUERY
{
/* these fields are disallowed in stacked case */
case PLPGSQL_GETDIAG_ROW_COUNT:
+ case PLPGSQL_GETDIAG_ROUTINE_OID:
if (new->is_stacked)
ereport(ERROR,
(errcode(ERRCODE_SYNTAX_ERROR),
if (tok_is_keyword(tok, &yylval,
K_ROW_COUNT, "row_count"))
$$ = PLPGSQL_GETDIAG_ROW_COUNT;
+ else if (tok_is_keyword(tok, &yylval,
+ K_PG_ROUTINE_OID, "pg_routine_oid"))
+ $$ = PLPGSQL_GETDIAG_ROUTINE_OID;
else if (tok_is_keyword(tok, &yylval,
K_PG_CONTEXT, "pg_context"))
$$ = PLPGSQL_GETDIAG_CONTEXT;
| K_PG_EXCEPTION_CONTEXT
| K_PG_EXCEPTION_DETAIL
| K_PG_EXCEPTION_HINT
+ | K_PG_ROUTINE_OID
| K_PRINT_STRICT_PARAMS
| K_PRIOR
| K_QUERY
PG_KEYWORD("pg_exception_context", K_PG_EXCEPTION_CONTEXT)
PG_KEYWORD("pg_exception_detail", K_PG_EXCEPTION_DETAIL)
PG_KEYWORD("pg_exception_hint", K_PG_EXCEPTION_HINT)
+PG_KEYWORD("pg_routine_oid", K_PG_ROUTINE_OID)
PG_KEYWORD("print_strict_params", K_PRINT_STRICT_PARAMS)
PG_KEYWORD("prior", K_PRIOR)
PG_KEYWORD("query", K_QUERY)
typedef enum PLpgSQL_getdiag_kind
{
PLPGSQL_GETDIAG_ROW_COUNT,
+ PLPGSQL_GETDIAG_ROUTINE_OID,
PLPGSQL_GETDIAG_CONTEXT,
PLPGSQL_GETDIAG_ERROR_CONTEXT,
PLPGSQL_GETDIAG_ERROR_DETAIL,
20
(1 row)
--- repeated call should to work
+-- repeated call should work
select outer_outer_func(20);
NOTICE: calling down into outer_func()
NOTICE: calling down into inner_func()
20
(1 row)
--- repeated call should to work
+-- repeated call should work
select outer_outer_func(20);
NOTICE: calling down into outer_func()
NOTICE: calling down into inner_func()
drop function outer_outer_func(int);
drop function outer_func(int);
drop function inner_func(int);
+-- Test pg_routine_oid
+create function current_function(text)
+returns regprocedure as $$
+declare
+ fn_oid regprocedure;
+begin
+ get diagnostics fn_oid = pg_routine_oid;
+ return fn_oid;
+end;
+$$ language plpgsql;
+select current_function('foo');
+ current_function
+------------------------
+ current_function(text)
+(1 row)
+
+drop function current_function(text);
+-- shouldn't fail in DO, even though there's no useful data
+do $$
+declare
+ fn_oid oid;
+begin
+ get diagnostics fn_oid = pg_routine_oid;
+ raise notice 'pg_routine_oid = %', fn_oid;
+end;
+$$;
+NOTICE: pg_routine_oid = 0
--
-- Test ASSERT
--
$$ language plpgsql;
select outer_outer_func(10);
--- repeated call should to work
+-- repeated call should work
select outer_outer_func(20);
drop function outer_outer_func(int);
$$ language plpgsql;
select outer_outer_func(10);
--- repeated call should to work
+-- repeated call should work
select outer_outer_func(20);
drop function outer_outer_func(int);
drop function outer_func(int);
drop function inner_func(int);
+-- Test pg_routine_oid
+create function current_function(text)
+returns regprocedure as $$
+declare
+ fn_oid regprocedure;
+begin
+ get diagnostics fn_oid = pg_routine_oid;
+ return fn_oid;
+end;
+$$ language plpgsql;
+
+select current_function('foo');
+
+drop function current_function(text);
+
+-- shouldn't fail in DO, even though there's no useful data
+do $$
+declare
+ fn_oid oid;
+begin
+ get diagnostics fn_oid = pg_routine_oid;
+ raise notice 'pg_routine_oid = %', fn_oid;
+end;
+$$;
+
--
-- Test ASSERT
--