Psycopg Pool may serve a disconnected connection
If the database is disconnected, the connections in the pool are still served. See https://www.psycopg.org/psycopg3/docs/advanced/pool.html#connection-quality (it is for psycopg3 but it applies to psycopg2).
They suggest to watch for disconnection like https://www.psycopg.org/psycopg3/docs/advanced/async.html#detecting-disconnections but this can not directly be applied with psycopg2 because there is no check method on the Pool.
So as the only way to clear the pool from disconnected connections, is to retrieve them and close them. I propose that each time a connection is get, we perform a testing query. This add an extra round-trip but we already have such thing for new connection with the call to `set_session`.
A watcher may be implemented later once we update to psycopg3 (#10901).
For the record, this has been detected when connecting to a PostgreSQL server behind a proxy (which is probably quite aggressive on the disconnection). And once there are disconnected connections in the pool, there are random failure (except if by chance the request receive a new connection).
issue