如何在PostgreSQL中同时运行多个事务 [英] How to run multiple transactions concurrently in PostgreSQL

查看:375
本文介绍了如何在PostgreSQL中同时运行多个事务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在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. Can anyone give me some Idea?

推荐答案

打开多个 psql 会话,每个会话一个终端。

Open more than one psql session, one terminal per session.

如果您使用的是Windows,则可以通过启动 psql 通过开始菜单多次。在其他平台上,打开几个新的终端或终端选项卡,并在每个终端中分别启动 psql

If you're on Windows you can do that by launching psql via the Start menu multiple times. On other platforms open a couple of new terminals or terminal tabs and start psql in each.

我通常这样做当我检查锁定和并发问题时,用在以下答案中:

I routinely do this when I'm examining locking and concurrency issues, used in answers like:

  • https://stackoverflow.com/a/12456645/398670
  • https://stackoverflow.com/a/12831041/398670

...可能更多。要设置竞争条件时,一个有用的技巧是打开第三个 psql 会话和 BEGIN;锁表the_table_to_race_on; 。然后在其他会话中运行语句;他们会锁在锁上。 ROLLBACK 持有表锁的事务将与其他会话竞争。它不是完美的,因为它不能模拟偏移量开始时间的并发,但是它仍然非常有用。

... 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;. 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. It's not perfect, since it doesn't simulate offset-start-time concurrency, but it's still very helpful.

稍后针对类似主题的答案

这篇关于如何在PostgreSQL中同时运行多个事务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆