0

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.

Is there a reliable way to find whether a function is executing or not in 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?

4

1 回答 1

2

我最初的要求是调用一个函数,前提是它尚未运行。在 postgresql 中是否有更好的方法来实现这一点?

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

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

基本上,类似:

if not pg_try_advisory_lock(_key) then return -1; end if;
-- do stuff...
perform pg_advisory_unlock(_key);
于 2013-08-20T09:57:01.913 回答