Putting Concurrency to the Test
Explore how PostgreSQL manages concurrency through isolation levels and locking mechanisms in transaction processing. Understand the impact of unique constraints on concurrency, analyze benchmark tests comparing insert and update operations under high concurrent loads, and learn strategies to improve scalability when multiple users access shared resources simultaneously.
We'll cover the following...
When we benchmark the concurrency properties of the two statements, we quickly realize that the activity table is badly designed. The unique constraint includes a timestamptz field, which in PostgreSQL is only precise down to the microsecond.
Fixing the table
This kind of made-up unique constraint means we now have these errors to deal with:
Error: Database error 23505: duplicate key value violates unique constraint "activity_messageid_datetime_action_key"
DETAIL: Key (messageid, datetime, action)=(1, 2022-06-01 10:17:39.079724+00, rt) already exists.
The best course of action here is to do this:
Comparing the concurrency scaling
Now, we can properly compare the concurrency scaling of the insert and the update-based versions. In case you might be curious about it, here’s the testing code that’s been used:
Performing Test
For the first test, it’s a typical ...