4

I would like to test some variants of transaction concurrency in PostgreSQL and for that I need a script which would force two transaction to start at exactly the same time. Something that does not requires manual intervention ;) Any ideas?

7
  • 1
    Take a look at this jmeter.apache.org. I've only breifly used it and am not sure it allows you to sync up two calls to run at the same time without you creating custom logic for it, but it may. Commented Jun 11, 2013 at 19:36
  • thx JustBob if I don't find something easier to maintain will try with jmeter. Commented Jun 11, 2013 at 19:43
  • 2
    Are you sure you want "same time"? That's a tricky thing to define. Are you not after (the much easier): test when A starts before B, repeat test with B before A? Commented Jun 11, 2013 at 21:01
  • You might want to define "exactly the same time" a little better. What's the tolerance? Commented Jun 11, 2013 at 21:21
  • @RichardHuxton - I know it's tricky thats why I'm asking you ;) Only requirement I've got is to run both transactions concurrently. Maybe adding pg_sleep at the beginning of the first transaction would be good solution? then I've got some time to run the second one and both would see the same initial state of data? Commented Jun 11, 2013 at 22:03

1 Answer 1

2

You can homebrew this by taking a LOCK on a table, setting up your transactions, then releasing the lock by rolling back the transaction that got the lock. See this prior answer and its links for details on this approach. While I demonstrated it using three psql sessions it's equally viable to do it with bash co-processes, a Python script using psycopg2 and the multiprocessing or threading modules, etc. Fairly simple to do. Update: In fact here's an example I just wrote in python3.

For more sophisticated tests, grab the PostgreSQL source code and use the "isolationtester" tool in src/test/isolation which lets you write recipes that do complex orderings of commands. It doesn't support being built with PGXS (though such support would probably be pretty trivial to add) so you have to compile the whole PostgreSQL source tree, but that's quick enough. It'll run against your existing PostgreSQL so there's no need to install the one you compiled.

See src/test/isolation/README for more about the isolationtester tool. The docs are a little thin on the ground since it's an internal testing tool, but the existing tests cases should help you get started. Feel free to improve it to meet your needs and submit patches :)

Sign up to request clarification or add additional context in comments.

1 Comment

thank you for the script and isolationtester I could not find it through google ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.