0

我有一个要迁移到 PostgreSQL 的 Oracle 存储过程。

存储过程中有DBMS_APPLICATION_INFO.READ_MODULEDBMS_APPLICATION_INFO.SET_ACTIONDBMS_APPLICATION_INFO.SET_MODULE过程,我无法在 Postgres 中找到替代方法。

Postgres 中这些过程的最接近的替代方案是什么?

4

1 回答 1

3

您可以使用参数application_name

例子:

 postgres=# show application_name;
 application_name 
------------------
 psql
(1 row)

postgres=# select application_name from pg_stat_activity where pid = pg_backend_pid();
 application_name 
------------------
 psql
(1 row)

postgres=# set application_name='myapp';
SET
postgres=# select application_name from pg_stat_activity where pid = pg_backend_pid();
 application_name 
------------------
 myapp
(1 row)

postgres=# 

这是实现动作和模块的可能解决方案:

begin;
BEGIN

set application_name='my_module:my_action';
SET

select application_name from pg_stat_activity where pid = pg_backend_pid();
  application_name   
---------------------
 my_module:my_action
(1 row)

commit;
COMMIT

select application_name from pg_stat_activity where pid = pg_backend_pid();
  application_name   
---------------------
 my_module:my_action
(1 row)

do
$$
declare
 v_module text;
 v_action text;
 v_application_name text;
 v_pos int;
begin
 select application_name into v_application_name
 from pg_stat_activity 
 where pid = pg_backend_pid();
 v_pos = position(':' in v_application_name); 
 if (v_pos > 0)
 then
  v_module = substring(v_application_name, 1, v_pos - 1);
  v_action = substring(v_application_name, v_pos + 1);
  raise notice 'v_module=%, v_action=%', v_module, v_action;
 end if;
end;
$$;
NOTICE:  v_module=my_module, v_action=my_action
DO
  
于 2020-07-07T03:54:24.563 回答