Putting Concurrency to the Test
Perform a few tests to learn more about how concurrency works in SQL.
We'll cover the following...
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 ...