问题
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