PostgreSQL DBA(81) - Locks(FOR UPDATE SKIP LOCKED)
发表于:2025-11-14 作者:千家信息网编辑
千家信息网最后更新 2025年11月14日,本节介绍了PostgreSQL中的FOR UPDATE SKIP LOCKED,通过该Option可以提高某些场景下的并发性能.Session 1希望从tbl中id < 100的记录中随机选择一行:[
千家信息网最后更新 2025年11月14日PostgreSQL DBA(81) - Locks(FOR UPDATE SKIP LOCKED)
本节介绍了PostgreSQL中的FOR UPDATE SKIP LOCKED,通过该Option可以提高某些场景下的并发性能.
Session 1希望从tbl中id < 100的记录中随机选择一行:
[local]:5432 pg12@testdb=# select pg_backend_pid(); pg_backend_pid ---------------- 1591(1 row)Time: 8.613 ms[local]:5432 pg12@testdb=# begin;BEGINTime: 4.527 ms[local]:5432 pg12@testdb=#* select * from tbl where id in (1,2,3) limit 1 for update; id | c1 | c2 | c3 | c4 | c5 ----+-----+-----+-----+----+---- 1 | c11 | c21 | c31 | | c3(1 row)Time: 1.450 ms[local]:5432 pg12@testdb=#*下面是该SQL的锁信息
[local]:5432 pg12@testdb=# select pid,locktype,relation::regclass,mode,page,tuple,virtualxid,transactionid,virtualtransaction,granted,fastpath from pg_locks where relation='tbl'::regclass;-[ RECORD 1 ]------+-------------pid | 1591locktype | relationrelation | tblmode | RowShareLockpage | tuple | virtualxid | transactionid | virtualtransaction | 3/2granted | tfastpath | tTime: 1.627 ms假如Session 2也是希望从id < 100的记录中随机选择一行,但这时候会因为冲突而阻塞:
[local]:5432 pg12@testdb=# begin;BEGINTime: 0.962 ms[local]:5432 pg12@testdb=#* select * from tbl where id in (1,2,3) limit 1 for update;相关锁信息:
[local]:5432 pg12@testdb=# select pid,locktype,relation::regclass,mode,page,tuple,virtualxid,transactionid,virtualtransaction,granted,fastpath from pg_locks where relation='tbl'::regclass;-[ RECORD 1 ]------+--------------------pid | 1634locktype | relationrelation | tblmode | RowShareLockpage | tuple | virtualxid | transactionid | virtualtransaction | 4/16granted | tfastpath | t-[ RECORD 2 ]------+--------------------pid | 1591locktype | relationrelation | tblmode | RowShareLockpage | tuple | virtualxid | transactionid | virtualtransaction | 3/4granted | tfastpath | t-[ RECORD 3 ]------+--------------------pid | 1634locktype | tuplerelation | tblmode | AccessExclusiveLockpage | 0tuple | 1virtualxid | transactionid | virtualtransaction | 4/16granted | tfastpath | fTime: 1.276 msPostgreSQL提供FOR UPDATE SKIP LOCKED,在Session 2获取一行时可跳过locked的行,从而提高并发性能
[local]:5432 pg12@testdb=#* select * from tbl where id in (1,2,3) limit 1 for update SKIP LOCKED; id | c1 | c2 | c3 | c4 | c5 ----+-----+-----+-----+----+---- 2 | c12 | c22 | c32 | | c3(1 row)Time: 2.413 ms可以看到,使用SKIP LOCKED选项,Session 2并没有被阻塞而是获取了没有locked的tuple.
这时候的锁信息如下:
[local]:5432 pg12@testdb=# select pid,locktype,relation::regclass,mode,page,tuple,virtualxid,transactionid,virtualtransaction,granted,fastpath from pg_locks where relation='tbl'::regclass;-[ RECORD 1 ]------+-------------pid | 1634locktype | relationrelation | tblmode | RowShareLockpage | tuple | virtualxid | transactionid | virtualtransaction | 4/17granted | tfastpath | t-[ RECORD 2 ]------+-------------pid | 1591locktype | relationrelation | tblmode | RowShareLockpage | tuple | virtualxid | transactionid | virtualtransaction | 3/4granted | tfastpath | tTime: 0.978 ms参考资料
More concurrency: Improved locking in PostgreSQL
一行
信息
性能
选择
阻塞
参考资料
场景
而是
资料
冲突
参考
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
网络安全小作文三十字
服务器上文件大小
足球经理数据库什么意思
网络安全 马庆贺
前端连不上数据库
小糖互联网络科技有限公司
网络安全专业是做什么工作
怎么连接打印机服务器
最近几年网络安全事件
决战私服数据库表说明
小米软件开发科技园
剑灵总是服务器断开连接
地理试卷软件开发
远程服务器提示你的凭证不工作
特斯拉软件开发员
松江区数据库安防系统设备等级
福建古树名木全部录入数据库
软件开发市场技术环境
数据库链联表查询
软件开发流程和模式
网络安全技术课程思政教案
c 开发数据库应用系统
联想服务器阵列增加一块盘
深圳互联网科技公司排名
鲁大师 服务器
卫生院信息网络安全实施方案
b价出租16和32线程服务器
手机可以远程登录电脑服务器吗
广东大数据时间同步服务器
软件开发工程师(无线)