postgres
postgres copied to clipboard
ECONNREFUSED inconsistency
sql client instances handle ECONNREFUSED errors inconsistently. This error is thrown when the database is unreachable. In the steps below, I am in control of the postgres process. I would think the same steps work with network access as well.
The Good
- Launch Postgres.
- Launch Node app and create client instance:
const sql = postgres(opts). - Shut down the Postgres process.
- Client uses
sqlinstance to run queries. These fail, as expected. - Relaunch Postgres.
- Client uses
sqlinstance to run queries. They succeed.
The Not-So-Good
- Shut down Postgres, or ensure it is not running.
- Launch Node app and create client instance:
const sql = postgres(opts). - Client uses
sqlinstance to run queries. These fail, as expected. Node throws this error, ONCE:
Error: connect ECONNREFUSED 127.0.0.1:5432
at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1592:16)
at cachedError (file:///Users/matt/Projects/pg-test/node_modules/postgres/src/query.js:171:23)
at new Query (file:///Users/matt/Projects/pg-test/node_modules/postgres/src/query.js:36:24)
at sql (file:///Users/matt/Projects/pg-test/node_modules/postgres/src/index.js:111:11)
at Function.listen (file:///Users/matt/Projects/pg-test/node_modules/postgres/src/index.js:179:35)
- Launch Postgres.
- Client uses
sqlinstance to run queries. These fail. This is unlike the first set of steps, where the connection is re-established and queries proceed as normal.
So should they behave the same? If this is expected behaviour, what is the right pattern to use to keep the sql instance valid?
Very nice description ! That is definitely not intended, I'll see if I can find some time to repro and look into it.
I test this issue with every release, and it is still present in v3.4.2