Jul 2, 2024
About the shift / doctors examples, the right design should have two tables:
```
CREATE TABLE shifts (id int PRIMARY KEY);
CREATE TABLE shift_doctors (id int REFERENCES shifts, name text, on_call boolean);
```
and counting should lock the shift:
```
SELECT COUNT(*) FROM (
SELECT *
FROM shifts NATURAL JOIN shift_doctors
WHERE id = 1 AND on_call
FOR UPDATE OF shifts
) shift_doctors;
```
This is bug-free and has the advantage to wait on conflict rather than failing