summaryrefslogtreecommitdiff
path: root/src/test
diff options
context:
space:
mode:
authorTom Lane2023-04-04 17:33:18 +0000
committerTom Lane2023-04-04 17:33:18 +0000
commitd3d53f955cf6ad755ba3682577e0f6fa10106438 (patch)
tree8572b2e5db878d357c9a350ae61b098451d7f5ba /src/test
parent482675987bcdffb390ae735cfd5f34b485ae97c6 (diff)
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
Diffstat (limited to 'src/test')
-rw-r--r--src/test/regress/expected/plpgsql.out31
-rw-r--r--src/test/regress/sql/plpgsql.sql29
2 files changed, 56 insertions, 4 deletions
diff --git a/src/test/regress/expected/plpgsql.out b/src/test/regress/expected/plpgsql.out
index 2d26be1a81..272f5d2111 100644
--- a/src/test/regress/expected/plpgsql.out
+++ b/src/test/regress/expected/plpgsql.out
@@ -5229,7 +5229,7 @@ NOTICE: outer_func() done
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()
@@ -5311,7 +5311,7 @@ NOTICE: outer_func() done
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()
@@ -5332,6 +5332,33 @@ NOTICE: outer_func() done
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
--
diff --git a/src/test/regress/sql/plpgsql.sql b/src/test/regress/sql/plpgsql.sql
index 98365e087f..924d524094 100644
--- a/src/test/regress/sql/plpgsql.sql
+++ b/src/test/regress/sql/plpgsql.sql
@@ -4206,7 +4206,7 @@ end;
$$ 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);
@@ -4261,13 +4261,38 @@ end;
$$ 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
--