我正在研究一个简单的功能,它会自动更新表格中的某些内容。
create or replace function total()
returns void as $$
declare
sum int;
begin
sum = (SELECT count(copy_id) FROM copies);
update totalbooks
set all_books = sum
where num = 1;
end;
$$ language plpgsql;
如果我执行“选择总计();” 它工作得很好,所以我做了一个函数触发器,以便它自动更新:
create or replace function total1() returns trigger as $$
begin
perform (select total());
return null;
end;
$$ language plpgsql;
但是在我执行这个之后:
create trigger total2
after update
on totalbooks
for each row
execute procedure total1();
它给了我一条错误消息:
ERROR: stack depth limit exceeded
HINT: Increase the configuration parameter "max_stack_depth" (currently 3072kB), after ensuring the platform's stack depth limit is adequate.
CONTEXT: SQL statement "SELECT (SELECT count(copy_id) FROM copies)"
PL/pgSQL function total() line 5 at assignment
SQL statement "SELECT (select total())"
PL/pgSQL function total1() line 3 at PERFORM
SQL statement "update totalbooks
set all_books = sum
where num = 1"
PL/pgSQL function total() line 6 at SQL statement
SQL statement "SELECT (select total())"
PL/pgSQL function total1() line 3 at PERFORM
SQL statement "update totalbooks
set all_books = sum
where num = 1"
PL/pgSQL function total() line 6 at SQL statement
SQL statement "SELECT (select total())"
PL/pgSQL function total1() line 3 at PERFORM
SQL statement "update totalbooks
set all_books = sum
where num = 1"
PL/pgSQL function total() line 6 at SQL statement
SQL statement "SELECT (select total())"
PL/pgSQL function total1() line 3 at PERFORM
SQL statement "update totalbooks
set all_books = sum
where num = 1"
PL/pgSQL function total() line 6 at SQL statement
SQL statement "SELECT (select total())"
PL/pgSQL function total1() line 3 at PERFORM
SQL statement "update totalbooks
set all_books = sum
where num = 1"
PL/pgSQL function total() line 6 at SQL statement
SQL statement "SELECT (select total())"
PL/pgSQL function total1() line 3 at PERFORM
SQL statement "update totalbooks
set all_books = sum
where num = 1"
PL/pgSQL function total() line 6 at SQL statement
SQL statement "SELECT (select total())"
显然我的触发器有问题。请帮忙。
我正在使用 Postgres 9.2.4,由 Visual C++ build 1600 编译,64 位
编辑:
我尝试了 pg_trigger_depth(),但现在触发器不会自动更新??我仍然需要执行'select total()'
这是我的新代码:
create or replace function total()
returns void as $$
declare
sum int;
begin
sum = (SELECT count(copy_id) FROM copies);
update totalbooks
set all_books = sum;
end;
$$ language plpgsql;
create or replace function total1() returns trigger as $$
begin
perform (select total());
return null;
end;
$$ language plpgsql;
create trigger total2
after update
on totalbooks
for each row
WHEN (pg_trigger_depth()=0)
execute procedure total1();