为什么我在使用apache基准测试进行性能测试时会遇到Postgresql死锁?

时间:2017-03-12 10:08:49

标签: python apache postgresql psycopg2 database-deadlocks

我使用以下内容。

  
      
  1. Postgresql - 版本9.5
  2.   
  3. Flask web framework
  4.   
  5. Pgbouncer - PostgreSQL的轻量级连接池。
  6.   
  7. psycopg2 - 版本2.7
  8.   

我的API工作正常,直到我使用psycog2游标使用apache基准测试来测试性能。

交易是:

cursor.execute("select id from table where product_name='xyz' limit 1 for update");

使用上面的id我执行以下操作:

cursor.execute('update table set locked_time=<time> where id=<id_from_above>')

最后我使用了

进行提交

conn.commit()

其中conn是pyscopg2连接对象

表的架构如下所示:

    Column   |           Type           | Modifiers 
-------------+--------------------------+----------- 
 id          | interger                 | not null default nextval('table_id_seq'::regclass) 
 locked_time | timestamp with time zone | not null
 cust_id     | integer                  | 
Indexes:
    "table_pkey" PRIMARY KEY, btree (id)
Foreign-key constraints:
    "table_cust_id_fkey" FOREIGN KEY (cust_id) REFERENCES customer(id)

我正在使用apache基准测试ab测试性能:

ab -n500 -c40 <API>

导致死锁的可能原因是什么? 此外,如果我在使用ab进行测试时减少并发性,例如:

ab -n500 -c10.

死锁:

TransactionRollbackError: deadlock detected
DETAIL:  Process 18938 waits for ShareLock on transaction 107698; blocked by process 18950.
Process 18950 waits for ShareLock on transaction 107701; blocked by process 18938.
HINT:  See server log for query details.
CONTEXT:  while locking tuple (19671,40) in relation "table"

0 个答案:

没有答案