如何确定一个函数是否在postgresql中执行?

时间:2021-09-13 02:39:11

Suppose

假设

CREATE OR REPLACE FUNCTION report.dummy_func1() RETURNS integer AS $$
DECLARE
BEGIN
    PERFORM pg_sleep(10);
    RETURN 1;
END;
$$ LANGUAGE plpgsql;   

CREATE OR REPLACE FUNCTION report.dummy_func() RETURNS integer AS $$
DECLARE
BEGIN
    PERFORM pg_sleep(5);
    PERFORM report.dummy_func1();
    RETURN 1;
END;
$$ LANGUAGE plpgsql;   

Select report.dummy_func();

With the above setup, is there a way to identify which function is currently being executed?

使用上述设置,是否有一种方法可以识别当前正在执行的函数?

One way to identify is using pg_stat_activity, but it doesn't show which function is being executed.

识别的一种方法是使用pg_stat_activity,但它没有显示正在执行哪个函数。

Is there a reliable way to find whether a function is executing or not in postgresql.

是否有可靠的方法来确定函数是否在postgresql中执行。

My original requirement is to invoke a function, only if it is not already running. Is there a better way to achieve this in postgresql?

我的原始需求是调用一个函数,如果它还没有运行的话。在postgresql中是否有更好的方法来实现这一点?

1 个解决方案

#1


2  

My original requirement is to invoke a function, only if it is not already running. Is there a better way to achieve this in postgresql?

我的原始需求是调用一个函数,如果它还没有运行的话。在postgresql中是否有更好的方法来实现这一点?

It sounds like you should be using an advisory lock:

听起来你应该使用咨询锁:

http://www.postgresql.org/docs/current/static/explicit-locking.html

http://www.postgresql.org/docs/current/static/explicit-locking.html

Basically, something like:

基本上,像:

if not pg_try_advisory_lock(_key) then return -1; end if;
-- do stuff...
perform pg_advisory_unlock(_key);

#1


2  

My original requirement is to invoke a function, only if it is not already running. Is there a better way to achieve this in postgresql?

我的原始需求是调用一个函数,如果它还没有运行的话。在postgresql中是否有更好的方法来实现这一点?

It sounds like you should be using an advisory lock:

听起来你应该使用咨询锁:

http://www.postgresql.org/docs/current/static/explicit-locking.html

http://www.postgresql.org/docs/current/static/explicit-locking.html

Basically, something like:

基本上,像:

if not pg_try_advisory_lock(_key) then return -1; end if;
-- do stuff...
perform pg_advisory_unlock(_key);