PostgreSQL - make two transactions run at the same time

99封情书 提交于 2020-01-15 06:58:26

问题


I need to make two sessions (two files) run concurrently (at the same time). Is there a way to do this using pg_sleep or some other function like "delayExecutionUntil(x_time)"?


回答1:


To get two transactions at the (almost) exact same time, you could schedule two or more invocations of psql at the same time in a Linux shell with the at command.

Like:

at '08:00 01.12.2012' -f script.sql

(The required timestamp format may depend on your system locale.)
Where script.sql contains something like:

psql mydb -p 5432 -c "INSERT INTO tbl (col) VALUES ('foo');

Just with a lot more rows to provoke the collisions you are after ..




回答2:


You can use table locks (see LOCK command in the docs) to synchronize the stuff:

  • Connection "Controller" locks the first table the real transactions will use.
  • create new "Worker-A" connection and start your transaction. It will block on the locked table.
  • create new "Worker-B" connection and start your transaction. It will block on the locked table.
  • the "Controller" connection releases the lock.
  • "Worker-A" and "Worker-B" should immediately start working - if their concurrency setting allows this of course.


来源:https://stackoverflow.com/questions/13662173/postgresql-make-two-transactions-run-at-the-same-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!