简体   繁体   English

如何在PostgreSQL中同时运行多个事务

[英]How to run multiple transactions concurrently in PostgreSQL

I want to do some basic experiment on PostgreSQL, for example to generate deadlocks, to create non-repeatable reads, etc. But I could not figure out how to run multiple transactions at once to see such behavior. 我想在PostgreSQL上做一些基本的实验,例如生成死锁,创建不可重复的读取等等。但我无法弄清楚如何一次运行多个事务来查看这种行为。 Can anyone give me some Idea? 谁能给我一些想法?

Open more than one psql session, one terminal per session. 打开多个psql会话,每个会话一个终端。

If you're on Windows you can do that by launching psql via the Start menu multiple times. 如果您使用的是Windows,则可以通过多次启动菜单启动psql来实现。 On other platforms open a couple of new terminals or terminal tabs and start psql in each. 在其他平台上打开几个新终端或终端选项卡,并在每个终端启动psql

I routinely do this when I'm examining locking and concurrency issues, used in answers like: 我在检查锁定和并发问题时经常这样做,在以下答案中使用:

... probably more. ......可能更多。 A useful trick when you want to set up a race condition is to open a third psql session and BEGIN; LOCK TABLE the_table_to_race_on; 想要设置竞争条件时,一个有用的技巧是打开第三个psql会话和BEGIN; LOCK TABLE the_table_to_race_on; BEGIN; LOCK TABLE the_table_to_race_on; . Then run statements in your other sessions; 然后在其他会话中运行语句; they'll block on the lock. 他们会阻挡锁。 ROLLBACK the transaction holding the table lock and the other sessions will race. ROLLBACK持有表锁的事务和其他会话将竞争。 It's not perfect, since it doesn't simulate offset-start-time concurrency, but it's still very helpful. 它并不完美,因为它不模拟偏移启动时并发,但它仍然非常有用。

Other alternatives are outlined in this later answer on a similar topic. 其他替代方案在后面的答案中概述了类似的主题。

pgbench is probably the best solution in yours case. pgbench可能是你的最佳解决方案。 It allows you to test different complex database resource contentions, deadlocks, multi-client, multi-threaded access. 它允许您测试不同的复杂数据库资源争用,死锁,多客户端,多线程访问。

To get dealocks you can simply right some script like this ('bench_script.sql): 要获得交易,你可以简单地对这样的一些脚本('bench_script.sql):

DECLARE cnt integer DEFAULT 0;
BEGIN;
LOCK TABLE schm.tbl IN SHARE MODE;
select count(*) from schm.tabl into cnt;
insert into schm.tbl values (1 + 9999*random(), 'test descr' );
END;

and pass it to pgbench with -f parameter. 并使用-f参数将其传递给pgbench。

For more detailed pgbench usage I would recommend to read the official manual for postgreSQL pgBench 有关pgbench的更详细用法,我建议您阅读postgreSQL pgBench的官方手册

and get acquented with my pgbench question resolved recently here . 最近在这里解决了我的pgbench问题。

Craig Ringer提供了一种手动打开多个事务的方法,如果发现不是很方便,可以使用pgbench一次运行多个事务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Postgresql中同时执行两个事务? - How to execute two transactions concurrently in Postgresql? 在PostgreSQL 9.6中创建被其他事务同时阻止的索引 - Create index concurrently blocked by other transactions in postgresql 9.6 postgresql 中的多个事务在一个 session 中? - multiple transactions in one session in postgresql? 同一连接上的 PostgreSQL 多个事务 - PostgreSQL multiple transactions on the same connection 处理同时填充 PostgreSQL 数据库的多个用户 - Handling multiple users concurrently populating a PostgreSQL database PostgreSQL - 让两个事务同时运行 - PostgreSQL - make two transactions run at the same time 如何在PostgreSQL中同时运行多个非原子计算的多个客户端中防止更新异常? - How to prevent update anomalies with multiple clients running non-atomic computations concurrently in PostgreSQL? 如何在Postgresql中同时执行DROP INDEX列表? - How to perform a list of DROP INDEX CONCURRENTLY in Postgresql? 在python中同时多次运行sql查询 - Run a sql query multiple times concurrently in python 如何在CakePHP3中运行事务,同时检索上一个插入ID,并且对PostgreSQL和MySQL都有效? - How do I run transactions in CakePHP3 while retrieving last insert id and work for both PostgreSQL and MySQL?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM