简体   繁体   中英

PostgreSQL - make two transactions run at the same time

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)"?

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 ..

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM