【发布时间】:2013-05-22 16:15:49
【问题描述】:
我正在开发一个简单的函数,它会自动更新表格中的内容。
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;
如果我执行“select total();”它工作得很好,所以我做了一个函数触发器,让它自动更新:
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();
【问题讨论】:
-
您的触发器由
totalbooks上的更新触发,然后更新表totalbooks,进而触发totalbooks上的触发器,更新表totalbook,在@ 上触发触发器987654330@ 然后更新表totalbooks然后触发totalbooks上的触发器,更新表totalbook这将触发触发器......(你明白了) -
@a_horse_with_no_name 所以这就像一遍又一遍地做事??那我该如何纠正这个?有什么想法吗??
标签: postgresql plpgsql