Lock sessions table when opening a new session
If two people try to start a session at exactly the same time, two open sessions can be created.
Take an exclusive lock on the sessions table during session creation to prevent this.
(Amazingly, this came up for the first time in 20 years, this week!)
The easiest way to do this Properly™ would be to add a unique constraint on endtime with the SQL NULLS NOT DISTINCT clause — that way, at most one row could have a null endtime.
Unfortunately, this was only implemented in Postgresql 15 — most current installations are using Postgresql 14 (or earlier).
Since this has only come up once in 20 years, we could wait a while before fixing it...