- Software and digital services
- Databases
08003
PostgreSQL 08003 connection_does_not_exist: closed handle used
Evidence linked to this decision
View source: PostgreSQL 18 Appendix A: Error CodesRisk level
Medium
Proceed carefully and stop if the check is unclear.
Urgency
Medium
Quick answer
SQLSTATE 08003 (connection_does_not_exist) means the application tried to run an operation on a PostgreSQL connection handle that is not open. Check the handle status and open a fresh session instead of retrying writes blindly.
Safety first
Stop and check this first
- Never reuse a PGconn pointer after PQfinish or share open libpq connections across forked Unix child processes; doing so corrupts socket state and causes unpredictable failures.
- Do not assume a statement outside an explicit BEGIN transaction block rolled back when a connection vanished; verify the database state before retrying the write.
Safe checks you can perform
- Check PQstatus, PQsocket, and PQtransactionStatus on the failing handle
- Audit application code for post-close reuse or Unix process forking
- Verify whether an interrupted write committed before reconnecting
Leave these tasks to a qualified professional
- Do not attempt this professional task: Refactor connection lifecycle and pool validation before query execution
- Do not attempt this professional task: Align client pool idle lifetime with server and proxy timeouts
What a technician must be able to justify
Technician visit
These are the steps they must carry out and show you. Tick them as they work: if steps are skipped, do not accept a part swap.
These are not instructions for opening or handling the equipment yourself. They are the checks that should support a technical conclusion.
Refactor connection lifecycle and pool validation before query execution
What they doHave the application developer discard or reset handles that report CONNECTION_BAD via PQreset or pool eviction, initialize connections only after process forking, and recreate session prepared statements after any reset.
They must show youthe updated connection checkout code and automated test results proving closed handles are replaced cleanly.
In writingthe pool max lifetime, idle validation setting, and commit hash fixing the handle reuse path.
Align client pool idle lifetime with server and proxy timeouts
What they doHave the DBA and platform engineer compare connection pool idle eviction timers against server session timeouts and TCP keepalive parameters so pooled handles are retired before the remote end closes them.
They must show youthe current postgresql.conf timeout and keepalive values alongside the application pool configuration.
In writingthe agreed idle timeout thresholds for the application pool, proxy, and PostgreSQL server.
Before accepting a part or repair
Nothing has been shown yet. Do not accept a part swap based on the code alone.
Follow these steps
Note: Follow the documented order. An error code identifies the affected system, but it does not prove by itself which part has failed.
Check PQstatus, PQsocket, and PQtransactionStatus on the failing handle
- Who:
- Your application developer inspects the connection state in the client runtime or debugger.
- How:
- Log the return values of PQstatus, PQsocket, and PQtransactionStatus (or the driver equivalents) immediately before the failing query call.
- Result that matters:
- An active connection reports CONNECTION_OK and a socket descriptor greater than or equal to 0, whereas a closed or broken connection reports CONNECTION_BAD, socket -1, and PQTRANS_UNKNOWN.
- If PQsocket returns -1 or the wrapper marks the handle closed before the call, that confirms the client tried to use a dead handle and points to application lifecycle or pool checkout logic.
- If the handle was CONNECTION_OK at checkout and turned CONNECTION_BAD after idle wait, that indicates an external idle timeout or earlier communication drop closed the channel.
- Evidence to keep:
- Save the logged status values, socket descriptor integer, and stack trace of the code path that invoked the closed handle.
Do not attempt to dereference a PGconn pointer after PQfinish has been called, as its memory has already been freed.
Evidence: PostgreSQL 18 libpq connection status functions
Audit application code for post-close reuse or Unix process forking
- Who:
- Your developer reviews worker initialization and connection cleanup paths.
- How:
- Search the codebase for connection close calls (such as PQfinish or pool release) that leave a reference reachable by async callbacks, and check whether worker processes fork after opening database connections.
- Result that matters:
- The libpq documentation warns that forking a process with open connections shares the underlying socket and leads to unpredictable results.
- If connections are opened before process fork calls, that confirms parent and child workers are competing on the same socket and triggering 08003 when one side closes it.
- If each worker opens its own connection after forking and never shares handles across threads, that rules out socket sharing and isolates the problem to pool eviction or timeout settings.
- Evidence to keep:
- Record the file names, line numbers, and worker startup sequence where connections are initialized and closed.
Evidence: PostgreSQL database connection control functions
Verify whether an interrupted write committed before reconnecting
- Who:
- Your developer or DBA checks the target table state in a read-only query on a new connection.
- How:
- Open a fresh database session and query the business key or transaction state of the operation that was running when the previous handle disappeared.
- Result that matters:
- When a backend closes a connection, it rolls back any open incomplete transaction block, but a single statement outside BEGIN and COMMIT might have finished before the disconnect was noticed.
- If the target row change is already present, that confirms the statement committed before the handle was lost and rules out re-running the write.
- If the change is absent and the statement ran inside an uncommitted transaction block, that confirms the backend rolled back the open transaction.
- Evidence to keep:
- Keep the read-only verification query result and timestamp before issuing any recovery write.
Never replay non-idempotent INSERT, UPDATE, or DELETE statements on a replacement connection without checking whether the previous statement committed.
Evidence: PostgreSQL 18 frontend/backend protocol overview · PostgreSQL 18 protocol message flow
Still not solved
If the error persists, stop here. See what the technician must do and show you.
Virtual technician
Does 08003 still appear after these checks?
This guided flow uses the exact code and your answers to choose a test, an expected result and the next branch.
Where this code applies
A code is meaningful only inside the right product and version context.
Primary scope
- System
- Software and digital services
- Brand
- PostgreSQL
- Product type
- Databases
Known code variants
- connection_does_not_exist
- PostgreSQL 08003
- SQLSTATE 08003
What it means
PostgreSQL Appendix A defines 08003 as connection_does_not_exist in Class 08 Connection Exception. It is raised when client code or a driver attempts to issue a command on a connection object that was never opened, was already closed with PQfinish, or transitioned to CONNECTION_BAD with socket descriptor -1 after an earlier disconnect. When a backend session closes, PostgreSQL rolls back any open incomplete transaction and discards session-scoped prepared statements and portals. Code 08003 alone does not prove whether application lifecycle bugs, pool eviction, or an earlier network drop closed the handle.
Warnings and stop conditions
- Mediumsource verified
Never reuse a PGconn pointer after PQfinish or share open libpq connections across forked Unix child processes; doing so corrupts socket state and causes unpredictable failures.
Evidence: PostgreSQL database connection control functions
- Lowsource verified
Do not assume a statement outside an explicit BEGIN transaction block rolled back when a connection vanished; verify the database state before retrying the write.
Evidence: PostgreSQL 18 frontend/backend protocol overview · PostgreSQL 18 protocol message flow
Symptoms and causes
Probable causes · in order
- Reusing a connection handle after PQfinish or explicit client closeCommon
Calling PQfinish closes the server session and frees the PGconn structure, and libpq rules state that the PGconn pointer must not be used again after PQfinish has been called.
Evidence: PostgreSQL database connection control functions
- Issuing queries on a handle already in CONNECTION_BAD stateCommon
A prior communication failure changes PQstatus from CONNECTION_OK to CONNECTION_BAD and sets PQsocket to -1, so subsequent commands fail until PQreset or a new connection succeeds.
Evidence: PostgreSQL database connection control functions · PostgreSQL 18 libpq connection status functions
- Sharing open libpq connections across forked Unix processesPossible
Forking a process on Unix with open libpq handles causes parent and child processes to share the same socket descriptor and operating system resources, closing or corrupting the session unexpectedly.
Evidence: PostgreSQL database connection control functions
- Referencing session-scoped prepared statements after session lossPossible
Prepared statements and portals exist only within a single backend session and are never shared across sessions, so a stale pooled handle that lost its backend cannot execute them.
Evidence: PostgreSQL 18 frontend/backend protocol overview · PostgreSQL 18 protocol message flow
Sources and technical references
- 1
PostgreSQL 18 Appendix A: Error Codes
PostgreSQL Global Development Group · 2026 · official_doc
- 2
PostgreSQL error and notice fields
PostgreSQL Global Development Group · 2026 · official_doc
- 3
PostgreSQL database connection control functions
PostgreSQL Global Development Group · 2026 · official_doc
Was this entry useful?