Founder Notes
The Rollback Was Right
I pulled a project out of a shared repository last week and gave it one of its
own, history intact. The move itself was dull, which is what you want. What it
turned up was a bug that had been sitting there the whole time, and the bug is
the part worth writing down.
A test failed in the new repository. It failed in the old one too, so the move
had not caused it. Before touching the test I printed the settings each
database connection was actually running under, once per checkout from the
pool. The first checkout had the timezone I had configured and the statement
limit I had configured. Every checkout after it had neither.
Both settings were applied in a handler that runs when a connection is
created. That had seemed like exactly the right place. A connection is made
once and reused many times, so configure it at birth and stop thinking about
it.
Here is what I did not know. `SET` in Postgres is transactional, and a
connection pool rolls back when a connection is handed back to it, so that the
next borrower does not inherit somebody's half-finished work. My settings were
made inside a transaction I had not opened and did not know existed. They held
for the first use of each connection and were rolled off it on return — every
connection, every time, for the life of the process.
The queries kept answering
That is what made it slow to find. Every query after the first still ran and
still came back with an answer. It came back with the answer for a different
day.
Date arithmetic done on the server shifted by the server's offset from UTC, so
a screen told someone they had missed a record they had in fact filed, that
day, themselves. The record was right. The query was right. The comparison
happened in the wrong timezone, confidently. A second setting, a ceiling on how
long any one statement may run, had been coming off the same way.
I wrote it, but the pool owned it
The rollback is not a bug. It is one of the better things a connection pool
does. A shared resource handed back in an unknown state is a hazard to whoever
gets it next, so you return it to a known state before you lend it out again. I
depend on that behaviour everywhere, including in the code I was debugging.
And it cannot tell my configuration from my mess. At the protocol level there
is no difference between a setting I meant to outlive the transaction and
transaction state I forgot to clean up: both are session changes made while a
transaction was open. The pool discards both, for the same good reason.
So what I had actually done was write a durable-looking instruction into a
scope whose lifetime belonged to something else. The thing that erased it was
not malfunctioning. It was working.
What I changed
Both settings are now supplied as connection options when the connection is
opened, before any transaction exists for a rollback to unwind. The test that
covers it checks the second and third use of a connection rather than the
first, because a test handed a fresh connection passes whether or not this bug
is present — which is why it survived as long as it did.
The general form
A setting lasts as long as the scope you made it in, and you are usually not
the one who owns that scope.
The failure mode is not that something broke your configuration. It is that
something correct reset it, for a reason you would defend if it were pointed
out to you. So when you configure anything shared, the question is not whether
the setting took. It is who else is entitled to put that thing back to a known
state — and whether, from where they are standing, your configuration looks any
different from the mess they are there to clear up.