new_customer
Web 应用程序每秒调用我的函数数次(但每个会话仅调用一次)。它所做的第一件事就是锁定customer
表(执行“如果不存在则插入”- 的简单变体upsert
)。
我对文档的理解是,其他调用new_customer
应该简单地排队,直到所有先前的调用都完成:
LOCK TABLE 获得表级锁,如有必要,等待释放任何冲突的锁。
为什么有时会陷入僵局?
定义:
create function new_customer(secret bytea) returns integer language sql
security definer set search_path = postgres,pg_temp as $$
lock customer in exclusive mode;
--
with w as ( insert into customer(customer_secret,customer_read_secret)
select secret,decode(md5(encode(secret, 'hex')),'hex')
where not exists(select * from customer where customer_secret=secret)
returning customer_id )
insert into collection(customer_id) select customer_id from w;
--
select customer_id from customer where customer_secret=secret;
$$;
来自日志的错误:
2015-07-28 08:02:58 BST 详细信息:进程 12380 等待数据库 12141 的关系 16438 上的 ExclusiveLock;被进程 12379 阻止。 进程 12379 等待数据库 12141 的关系 16438 上的 ExclusiveLock;被进程 12380 阻止。 进程 12380:选择 new_customer(decode($1::text, 'hex')) 进程 12379:选择 new_customer(decode($1::text, 'hex')) 2015-07-28 08:02:58 BST 提示:有关查询详细信息,请参阅服务器日志。 2015-07-28 08:02:58 BST 上下文:SQL 函数“new_customer”语句 1 2015-07-28 08:02:58 BST 声明:选择 new_customer(decode($1::text, 'hex'))
关系:
postgres=# select relname from pg_class where oid=16438;
┌──────────┐
│ relname │
├──────────┤
│ customer │
└──────────┘
编辑:
我已经设法获得了一个简单的可重现测试用例。对我来说,由于某种竞争条件,这看起来像是一个错误。
架构:
create table test( id serial primary key, val text );
create function f_test(v text) returns integer language sql security definer set search_path = postgres,pg_temp as $$
lock test in exclusive mode;
insert into test(val) select v where not exists(select * from test where val=v);
select id from test where val=v;
$$;
bash 脚本在两个 bash 会话中同时运行:
for i in {1..1000}; do psql postgres postgres -c "select f_test('blah')"; done
错误日志(通常是 1000 次调用的少数死锁):
2015-07-28 16:46:19 BST ERROR: deadlock detected
2015-07-28 16:46:19 BST DETAIL: Process 9394 waits for ExclusiveLock on relation 65605 of database 12141; blocked by process 9393.
Process 9393 waits for ExclusiveLock on relation 65605 of database 12141; blocked by process 9394.
Process 9394: select f_test('blah')
Process 9393: select f_test('blah')
2015-07-28 16:46:19 BST HINT: See server log for query details.
2015-07-28 16:46:19 BST CONTEXT: SQL function "f_test" statement 1
2015-07-28 16:46:19 BST STATEMENT: select f_test('blah')
编辑2:
@ypercube建议使用函数外部的变体lock table
:
for i in {1..1000}; do psql postgres postgres -c "begin; lock test in exclusive mode; select f_test('blah'); end"; done
有趣的是,这消除了死锁。
我将此发布到 pgsql-bugs, Tom Lane的回复表明这是一个锁升级问题,被 SQL 语言函数处理方式的机制所掩盖。本质上,生成的锁是在对表的排他锁之前
insert
获得的:这也解释了为什么将表锁定在包装 plpgsql 块中的函数外部(如@ypercube 所建议的那样)可以防止死锁。
假设您在调用 new_customer 之前运行了另一个语句,并且这些语句获得了一个与(基本上是客户表中的任何数据修改)冲突的锁
EXCLUSIVE
,那么解释非常简单。可以通过一个简单的示例重现该问题(甚至不包括函数):
第一次会议:
第二届会议
第一次会议
当第一个会话执行插入时,它会获取
ROW EXCLUSIVE
表上的锁。同时,会话 2 也尝试获取ROW EXCLUSIVE
锁,并尝试获取EXCLUSIVE
锁。此时它必须等待第一个会话,因为EXCLUSIVE
lock 与ROW EXCLUSIVE
. 最后,第 1 会话跳过鲨鱼并尝试获取EXCLUSIVE
锁,但由于锁是按顺序获取的,因此它在第 2 会话之后排队。反过来,这会等待第一个,从而产生死锁:这个问题的解决方案是尽早获取锁,通常作为事务中的第一件事。另一方面,PostgreSQL 工作负载只在一些非常罕见的情况下需要锁,所以我建议重新考虑你做 upsert 的方式(看看这篇文章http://www.depesz.com/2012/06/10 /why-is-upsert-so-complicated/)。