Issue: PostgresqlDatabase:is_connection_usable returns True, although the database is shut down
Author: parallaxe
State: closed
Created: 2022-11-23 10:24:40

I'm using peewee 3.15.4 with psycopg2-binary to connect to a postgres-database. To test how the system behaves in case that the database is not available, I shut the database down.

In the code I call `is_connection_usable` to check if the database is available. It returns `True`, although the database is no longer available. The termination of the connection is logged, so it is recognised by any of the modules involved, but it does not propagate up to the `PostgresqlDatabase`-instance. Any ideas what is going on here?

```
ERROR:root:terminating connection due to administrator command
server closed the connection unexpectedly
	This probably means the server terminated abnormally
	before or while processing the request.

ERROR:root:terminating connection due to administrator command
server closed the connection unexpectedly
	This probably means the server terminated abnormally
	before or while processing the request.

ERROR:root:connection already closed
ERROR:root:connection already closed
Database status True
```

There could potentially be a lot of overhead in checking liveness so Peewee uses two things:

1. Internal flag indicating whether we successfully connected.
2. Whether psycopg2's underlying connection transaction state is not in error.

If you are just using plain old psycopg2 and try this experiment you'll see that there's not much info available for "detecting" this:

```python
import psycopg2
import psycopg2.extensions

conn = psycopg2.connect(dbname='peewee_test')
print('connected!')
print('closed?', conn.closed)
print('status?', conn.status)
print('poll?', conn.poll())
curs = conn.cursor()
curs.execute('select 1')
curs.fetchall()
conn.commit()

input('Shut down database now, then press Enter.')

print('closed?', conn.closed)
print('status?', conn.status)
print('poll?', conn.poll())
curs = conn.cursor()
curs.execute('select 1')  # We will first detect the problem here.
curs.fetchall()
conn.commit()
```

Outputs:

```
connected!
closed? 0
status? 1
poll? 0
Shut down database now, then press Enter.
    <---- NOTE: I shut down the database server. The psycopg2 connection still reports
                 that it is not closed and its status is READY.
closed? 0
status? 1
poll? 0
Traceback (most recent call last):
  File "x.py", line 20, in <module>
    curs.execute('select 1')
psycopg2.errors.AdminShutdown: terminating connection due to unexpected postmaster exit
server closed the connection unexpectedly
        This probably means the server terminated abnormally
        before or while processing the request.
```

You can ask the psycopg2 folks what the best way would be to detect this before executing queries, but typically you probably want to be monitoring your database server and handling keeping the db running separately from your python app. Similarly, within your Python app, what exactly would you do if the database was not running, anyways? It's not like you can just reconnect because there's presumably nothing to reconnect to if its shut down.

You can always subclass and override `is_connection_usable` to attempt to execute something like `SELECT 1` and handle an exception there, but I've opted against that for the reasons mentioned above.