PostgreSQL for update skip locked 实现队列功能
PostgreSQL 大约 1790 字数据准备
create table if not exists queue(id int, content text, status text);
insert into queue values(1, '队列元素1', 'pending');
insert into queue values(2, '队列元素2', 'pending');
insert into queue values(3, '队列元素3', 'pending');
insert into queue values(4, '队列元素4', 'pending');
insert into queue values(5, '队列元素5', 'pending');
skip locked
使用for update
时,如果一行并其他session
读取,则会一直等待直到timeout
;
如果加了nowait
,则当选取的行被其他session
读取时会直接抛出异常。
如果加了skip locked
,则当选取的行被其他session
读取时会跳过改行,根据where
条件返回或,或其他行。
示例
skip locked 返回空
session 1
在事务中选择id
为1
的行。
z-blog=# begin;
BEGIN
z-blog=*# select * from queue where id = 1 for update skip locked;
id | content | status
----+-----------+---------
1 | 队列元素1 | pending
(1 row)
session 2
同样选择id
为1
的行,此时返回为空。
z-blog=# select * from queue where id = 1 for update skip locked;
id | content | status
----+---------+--------
(0 rows)
skip locked 返回其他行
session 1
在事务中选择了status
为pending
的任意一条。
z-blog=# begin;
BEGIN
z-blog=*# select * from queue where status='pending' limit 1 for update skip locked;
id | content | status
----+-----------+---------
1 | 队列元素1 | pending
(1 row)
session 2
同样选择了status
为pending
的任意一条,返回时跳过了id
为1
的行,从id
为2
的行开始选取一条。
z-blog=# select * from queue where status='pending' limit 1 for update skip locked;
id | content | status
----+-----------+---------
2 | 队列元素2 | pending
(1 row)
队列功能
begin; -- begin transaction
with temp as (
select id from queue where status='pending' limit 1 for update skip locked
)
update queue set status='succeeded' where queue.id = (select id from temp)
returning *;
-- bussiness logic
commit; -- commit transaction
查看数据
z-blog=# select * from queue;
id | content | status
----+-----------+-----------
2 | 队列元素2 | pending
3 | 队列元素3 | pending
4 | 队列元素4 | pending
5 | 队列元素5 | pending
1 | 队列元素1 | succeeded
(5 rows)
阅读 74 · 发布于 2023-11-13
————        END        ————
Give me a Star, Thanks:)
https://github.com/fendoudebb扫描下方二维码关注公众号和小程序↓↓↓

昵称:
随便看看
换一批
-
Vue sourceMap 代码混淆阅读 1373
-
SpringMVC 请求流程阅读 1827
-
IDEA 复制弹框错误提示信息快捷键阅读 1283
-
使用 logrotate 处理日志阅读 3425
-
OpenResty 中的几种防止 SQL 注入的方法阅读 5590
-
HTML 中使用 datalist 标签给输入框添加下拉框选项阅读 4646
-
算法:解析中缀表达式阅读 1346
-
Spring Boot JdbcTemplate in 语句注意事项阅读 3622
-
Spring Boot 注入 Filter 过滤器的几种方式阅读 775
-
Spring Boot Thymeleaf 时间处理类阅读 190