CREATE OR REPLACE FUNCTION public.updatedata(userid_ integer)
RETURNS integer
AS $$
DECLARE
userdata_ integer;
BEGIN
LOOP
BEGIN
PERFORM 1 FROM public.footable f WHERE f.userid=userid_ LIMIT 1 FOR UPDATE ;
userdata_:=(SELECT f.userdata FROM public.footable f WHERE f.userid=userid_ );
UPDATE public.footable f SET userdata = userdata_ + 1 WHERE f.userid=userid_ ;
EXIT ;
EXCEPTION WHEN others THEN
END;
END LOOP;
RETURN userdata_ + 1;
EXCEPTION WHEN others THEN
END $$ language plpgsql;
当我像这样使用锁定时,我可以防止"lost update"
用户数据列出现问题吗?"FOR UPDATE"
实际上我想首先使用serializable isolation
,但事实证明,在serializable isolation
某些错误中只能在 commıt( link ) 之后检测到