digoal
2017-12-11
PostgreSQL , SQL超时
设置单条SQL的执行超时,防雪崩。
通常来说可以在SQL发起前设置事务级超时参数,SQL执行结束,重置。(如果SQL异常退出,会自动重置事务级参数)
begin;
......
set local statement_time='100ms';
select count(*) from a; -- 这条SQL的执行时间超过100MS则主动退出,并回滚整个事务
set local statement_timeout to default;
......
end;
例如这个QUERY,我们想让它100毫秒超时。
select count(*) as cnt, id from a where id<$1 group by id;
将它写到函数中,在函数中设置超时
create or replace function f1(int) returns setof record as $$
declare
begin
set local statement_timeout='100ms';
return query select count(*) as cnt, id from a where id<$1 group by id;
end;
$$ language plpgsql strict ;
调用SQL改成这样
select cnt,id from f1(1) as t(cnt int8, id int);
但是这么做实际上是没有效果的,原因是statement_timeout的设计之初是为交互性SQL设计的,在postgres.c中。
所以需要plpgsql超时,需要通过插件HOOK来实现。
statement_timeout is measured across an entire interactive command, not
individual commands within a function; and the timeout that applies to
an interactive command is determined at its beginning. So the above
doesn't do what you think.
1、实例级
修改
postgresql.conf
2、库级
alter database dbname set parameter=?;
3、用户级
alter role rolname set parameter=?;
4、会话级
set parameter=?;
5、事务级
begin;
set local parameter=?;
....
end;
6、函数级
alter function fun_name() set parameter=?;
1、空闲事务超时
idle_in_transaction_session_timeout = 2h
2、锁等待超时
lock_timeout = 1s
3、死锁检测超时间隔
deadlock_timeout = 1s
https://www.postgresql.org/docs/9.4/static/runtime-config-client.html
您的愿望将传达给PG kernel hacker、数据库厂商等, 帮助提高数据库产品质量和功能, 说不定下一个PG版本就有您提出的功能点. 针对非常好的提议,奖励限量版PG文化衫、纪念品、贴纸、PG热门书籍等,奖品丰富,快来许愿。开不开森.