how many objects can be locked per transaction

本文深入探讨了PostgreSQL中的锁机制,介绍了如何通过pg_locks视图查询当前锁的状态,并详细解析了max_locks_per_transaction参数的作用及其实现原理。通过创建测试函数,验证了系统在不同设置下对锁的支持能力。

在PostgreSQL中,通过pg_locks这个视图可以查看到当前被跟踪的锁状态。
或者直接通过如下SQL查询:
SELECT l.locktype, l.database, l.relation, l.page, l.tuple, l.virtualxid, l.transactionid, l.classid, l.objid, l.objsubid, l.virtualtransaction, l.pid, l.mode, l.granted FROM pg_lock_status() l(locktype, database, relation, page, tuple, virtualxid, transactionid,classid, objid, objsubid, virtualtransaction, pid, mode, granted);
这些锁的信息被存储在System V的共享内存中,理论上只要共享内存足够,就可以继续获得锁。
但是文档却是这么描述的(实际上不是精确的描述,但是这个参数是会影响System V的分配的,所以间接的影响了可以持有锁的数量):
max_locks_per_transaction (integer) -- 注意这是一个平均数,并不是用于限制单个事务的锁对象数量。

    The shared lock table tracks locks on max_locks_per_transaction * (max_connections + max_prepared_transactions) objects (e.g., tables); hence, no more than this many distinct objects can be locked at any one time. This parameter controls the average number of object locks allocated for each transaction; individual transactions can lock more objects as long as the locks of all transactions fit in the lock table. This is not the number of rows that can be locked; that value is unlimited. The default, 64, has historically proven sufficient, but you might need to raise this value if you have clients that touch many different tables in a single transaction. This parameter can only be set at server start.

    Increasing this parameter might cause PostgreSQL to request more System V shared memory than your operating system's default configuration allows. See Section 17.4.1 for information on how to adjust those parameters, if necessary.

    When running a standby server, you must set this parameter to the same or higher value than on the master server. Otherwise, queries will not be allowed in the standby server.

例:
假设 max_locks_per_transaction = 10
max_prepared_transactions = 0
max_connections = 40
按照文档的说明,整个数据库集群在同一时刻可以被锁的对象数应该等于400.但是实际上不只这个数量,(实际受到System V共享内存的限制)
创建两个测试函数,一个用于批量创建表,一个用于批量锁表。
CREATE OR REPLACE FUNCTION digoal.f_create_table(i_min integer, i_max integer)
 RETURNS void
 LANGUAGE plpgsql
AS $function$
declare
begin
for i in i_min..i_max loop
execute 'create table tbl_user_'||i||' (like tbl_user);';
end loop;
return;
end;
$function$

CREATE OR REPLACE FUNCTION digoal.f_lock_table(i_min integer, i_max integer)
 RETURNS void
 LANGUAGE plpgsql
AS $function$
declare
begin
for i in i_min..i_max loop
execute 'lock table tbl_user_'||i||' in exclusive mode;';
end loop;
return;
end;
$function$

digoal=> begin;
BEGIN
digoal=> select * from f_create_table(756,1857);
 f_create_table
----------------
 
(1 row)
digoal=> select count(distinct (database,relation)),count(*) from pg_locks;
 count | count
-------+-------
  1106 |  1107
(1 row)
此时数据库集群中被锁对象已经达到1106个,锁插槽使用1107个.已经超出400的限制,
继续申请锁看看会发生什么?
digoal=> select * from f_lock_table(1,4);
WARNING:  out of shared memory
CONTEXT:  SQL statement "lock table tbl_user_4 in exclusive mode;"
PL/pgSQL function "f_lock_table" line 4 at EXECUTE statement
ERROR:  out of shared memory
HINT:  You might need to increase max_locks_per_transaction.
CONTEXT:  SQL statement "lock table tbl_user_4 in exclusive mode;"
PL/pgSQL function "f_lock_table" line 4 at EXECUTE statement
再开连接的话也会有 out of shared memory 的报错.
postgres@db-172-16-3-33-> psql -h 127.0.0.1 postgres postgres
WARNING:  out of shared memory
psql: FATAL:  out of shared memory
HINT:  You might need to increase max_locks_per_transaction.

通 过增加max_locks_per_transaction或max_connections都可以达到加大System V共享内存的效果,但是具体能锁多少个对象不能使用max_locks_per_transaction * (max_connections + max_prepared_transactions)来精确计算,只是一个大概值。
当然,由于每一个连接都会消耗部分System V共享内存,因此当连接达到max_connections的时候,文档给出的公式会更加接近精确值。

乐播投屏是一款简单好用、功能强大的专业投屏软件,支持手机投屏电视、手机投电脑、电脑投电视等多种投屏方式。 多端兼容与跨网投屏:支持手机、平板、电脑等多种设备之间的自由组合投屏,且无需连接 WiFi,通过跨屏技术打破网络限制,扫一扫即可投屏。 广泛的应用支持:支持 10000+APP 投屏,包括综合视频、网盘与浏览器、美韩剧、斗鱼、虎牙等直播平台,还能将央视、湖南卫视等各大卫视的直播内容一键投屏。 高清流畅投屏体验:腾讯独家智能音画调校技术,支持 4K 高清画质、240Hz 超高帧率,低延迟不卡顿,能为用户提供更高清、流畅的视觉享受。 会议办公功能强大:拥有全球唯一的 “超级投屏空间”,扫码即投,无需安装。支持多人共享投屏、远程协作批注,PPT、Excel、视频等文件都能流畅展示,还具备企业级安全加密,保障会议资料不泄露。 多人互动功能:支持多人投屏,邀请好友加入投屏互动,远程也可加入。同时具备一屏多显、语音互动功能,支持多人连麦,实时语音交流。 文件支持全面:支持 PPT、PDF、Word、Excel 等办公文件,以及视频、图片等多种类型文件的投屏,还支持网盘直投,无需下载和转格式。 特色功能丰富:投屏时可同步录制投屏画面,部分版本还支持通过触控屏或电视端外接鼠标反控电脑,以及在投屏过程中用画笔实时标注等功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值