
Does a database connection still reach its server?
Source:R/util_connection_alive.R
csdb_connection_is_alive.RdReturns FALSE when a catalog request on connection shows that the
connection lost its server. Use it before cleanup SQL on a connection that
MAY have lost its server.
Value
TRUE or FALSE. FALSE for NULL, for a closed connection, and when the catalog request gives a condition that shows a lost connection.
Details
DBI::dbIsValid() cannot answer this question. It reports TRUE on an
odbc connection whose server ended the session. On such a connection, the
first statement gives an R error. The second statement crashes R with a
segfault in odbc 1.7.0 with psqlODBC 13.02, and tryCatch() cannot
catch a segfault.
The function asks the server whether the table csdb_connection_probe
exists. odbc sends that request as an ODBC catalog call, not as a
statement. On a lost connection it gives an R error, also after a failed
statement. This was measured on 2026-10-22 against PostgreSQL 16.15.
Only a condition whose message shows a lost connection gives FALSE. Any
other error or warning of the request gives TRUE, so a live connection
still gets its cleanup SQL. See csdb_connection_lost_pattern in the
source for the messages.
PostgreSQL releases the advisory locks, the role and the temporary tables of a session when the session ends. Cleanup SQL for them then has nothing to do.
Examples
con <- DBI::dbConnect(RSQLite::SQLite(), ":memory:")
csdb_connection_is_alive(con)
#> [1] TRUE
DBI::dbDisconnect(con)
csdb_connection_is_alive(con)
#> [1] FALSE