Skip to content

ADR-0041 — Default zero-RTT pool checkout check and connection defaults in from_conn_string

  • Status: Accepted
  • Date: 2026-12-05
  • Supersedes: none
  • Authors: project maintainers

Context

AsyncOpenPostgresCheckpointer.from_conn_string (and OpenPostgresCheckpointer.from_conn_string, which delegates to it) creates an AsyncConnectionPool with min_size=4 by default (src/open_postgres_checkpointer/aio.py). Previously it left psycopg_pool's check callback unset (None).

When PostgreSQL restarts or terminates idle backends (pg_terminate_backend, server shutdown, TCP reset), every idle connection in the pool becomes stale. Without a checkout check, the next turn checks out a dead socket and fails with OperationalError before psycopg_pool discards and replaces it.

Passing psycopg_pool's built-in AsyncConnectionPool.check_connection unconditionally executes await conn.execute("") on every checkout, adding a full network round trip before every single-statement aget_tuple, aput, aput_writes, alist, and alist_threads. Furthermore, when all min_size idle connections die together on a restart, psycopg_pool's _getconn_with_check_loop pops and backs off exponentially (AttemptWithBackoff: INITIAL_DELAY = 1.0, DELAY_BACKOFF = 2.0) on each dead connection sequentially, stalling across every dead slot in the pool and risking PoolTimeout on larger pools.

Decision

from_conn_string() will install a default checkout check callback whenever pool_kwargs does not specify "check". The default check polls the connection's socket non-blockingly (poll(0) on POSIX, select fallback on Windows) for unread data, EOF, hangup, or error — zero network round trips on healthy idle connections — and delegates to AsyncConnectionPool.check_connection only when the socket is closed, broken, invalid, or signals activity. When a check fails, it schedules one background pool.check() sweep so remaining dead idle connections are replaced during the first backoff sleep instead of stalling sequentially.

from_conn_string() will also set default libpq connection parameters when not already specified in conn_string, conn_kwargs, or the corresponding libpq environment variables (PGCONNECT_TIMEOUT, PGAPPNAME): TCP keepalives (keepalives=1, keepalives_idle=30, keepalives_interval=10, keepalives_count=3, skipped when keepalives is explicitly disabled), connect_timeout=10, and application_name="open-postgres-checkpointer".

Callers may override this with pool_kwargs={"check": AsyncConnectionPool.check_connection} (to ping on every checkout) or pool_kwargs={"check": None} (to disable checkout checks).

Alternatives considered

  • Default to AsyncConnectionPool.check_connection — simple one-liner, catches silent network blackholes too, but doubles network round trips on every single-statement operation and still suffers sequential backoff across dead idle slots on restart.
  • Keep check=None and only document pool_kwargs — leaves every default from_conn_string adopter exposed to post-restart turn failures until they discover and copy boilerplate.
  • Retry loop inside _connection() / _fetch() — requires draining up to max_size dead connections per call, duplicates pool lifecycle logic, and complicates multi-statement transactions (aprune, acopy_thread, asweep_expired).

Consequences

  • Healthy checkouts pay zero extra Postgres round trips while Postgres restarts and terminated backends recover transparently on the next turn without sequential backoff across every dead slot in the pool.
  • Benign unsolicited server frames (such as TLS 1.3 NewSessionTicket or NoticeResponse) trigger one check_connection pass to consume them, then return to zero-RTT checkouts.
  • Silent network blackholes (where a firewall drops packets with neither FIN nor RST) are bounded by the default TCP keepalive probes (30s idle, 3 × 10s probes) rather than the OS default (2 hours on Linux), after which the kernel flags the socket and poll(0) catches it with zero extra round trips. Deployments that need immediate per-checkout verification can still opt into pool_kwargs={"check": AsyncConnectionPool.check_connection}.
  • Default connect_timeout=10 prevents startup probes and background pool reconnects from hanging indefinitely on unreachable hosts, and default application_name="open-postgres-checkpointer" identifies saver sessions in pg_stat_activity, pg_locks, and rehydrate warnings.

References

  • src/open_postgres_checkpointer/_connection.py (merge_conn_kwargs, socket_needs_check, check_connection_fast, open_pool)
  • src/open_postgres_checkpointer/aio.py (from_conn_string) and src/open_postgres_checkpointer/rehydrate.py (rehydrate)
  • tests/test_from_conn_string.py
  • tests/integration/test_setup_and_pool.py
  • Adopting the saver