Postgre 存储过程编写:
create or replace procedure a.lockUser(int,int)
language plpgsql
as $$
begin
update a.t_user set lockout =true from (
SELECT distinct username lockuser
from a.t_log
where
(start_time between date_trunc('minute', (to_timestamp( $1 ))) - INTERVAL '1 minute' AND (to_timestamp( $1 )))
and username is not null
GROUP BY username
having count(1) > $2
) as lu
where username = lu.lockuser;
commit;
end;
$$
参数1:时间戳
Note:生成时间戳,select floor(extract(epoch from now()));
参数2:限制数
call a.lockUser(floor(extract(epoch from now()))::int,5)
使用pg_cron调用。
一分钟一次。
SELECT cron.schedule('* * * * *', $$ call a.lockUser(floor(extract(epoch from now()))::int,5)$$);