Errors and retries¶
Every driver failure derives from gres.errors.Error. Catch by recovery action: fix the call, fix the data, retry the transaction, or replace the session. Cancelling the calling task raises asyncio.CancelledError, which is not a Gres error.
Error
├── ConfigurationError invalid or unsupported connection setting
├── InterfaceError the handle cannot accept this call
├── PoolBusyError the pool's waiter queue is full
├── TimeoutError a deadline expired (a builtins.TimeoutError)
├── EncodingError a parameter cannot be encoded
├── DecodingError a result value cannot be represented
├── UnsupportedTypeError no codec for the PostgreSQL type
├── QueryShapeError the statement does not fit the method
├── CardinalityError wrong row count for fetch_one/fetch_optional
├── NotificationOverflowError a listener's buffer filled; messages lost
├── ConnectionError session failed (a builtins.ConnectionError)
│ ├── ProtocolError an unexpected backend message
│ └── TransactionLostError a managed transaction cannot continue
│ └── CommitOutcomeUnknownError COMMIT sent; no conclusive reply arrived
└── ServerError PostgreSQL rejected the operation
├── IntegrityError class 23
│ ├── UniqueViolation 23505
│ ├── ForeignKeyViolation 23503
│ ├── NotNullViolation 23502
│ ├── CheckViolation 23514
│ └── ExclusionViolation 23P01
├── DataError classes 21, 22
├── ProgrammingError classes 26, 3D, 3F, 42
├── OperationalError classes 08, 28, 40, 53, 55, 57, 58
│ ├── SerializationFailure 40001
│ ├── DeadlockDetected 40P01
│ ├── LockNotAvailable 55P03
│ └── QueryCanceled 57014
├── NotSupportedError class 0A
└── InternalError classes 25, XX
└── ReadOnlySqlTransaction 25006
A SQLSTATE without a dedicated subclass raises its two-character class error, or ServerError itself. Check error.sqlstate against the PostgreSQL error codes for other conditions.
Server diagnostics¶
ServerError exposes the fields PostgreSQL sent: sqlstate, severity, message, detail, hint, position, schema_name, table_name, column_name, datatype_name, and constraint_name (None when omitted). str(error) contains only the primary message; detail may include row values, so log it deliberately.
# Partial: tx is an open transaction; account, amount, and log are
# defined by the application.
from gres import errors
try:
await tx.execute(
'INSERT INTO account(id, balance) VALUES ($1, $2)', (account, amount)
)
except errors.CheckViolation as error:
log.warning('%s rejected by %s', error.sqlstate, error.constraint_name)
Local diagnostics¶
Local errors identify where conversion or binding failed without retaining sensitive values:
| Attribute | Meaning |
|---|---|
phase |
'bind' (placeholder lookup), 'describe' (type resolution), 'encode' (parameter conversion), or 'decode' (result conversion) |
parameter_position, parameter_name |
One-based $1 position or $name of the rejected parameter |
column_index, column_name |
Zero-based result column, or Arrow source field in a COPY |
python_type, postgres_type |
Rejected Python type and expected PostgreSQL type, when known |
EncodingError messages begin with the placeholder, e.g. $n: integer parameter requires int or str.
Retry safety¶
Three attributes on every Error report what is known about the failed call's effect:
| Attribute | Values |
|---|---|
dispatch_state |
'not_sent': statement never reached PostgreSQL. 'possibly_sent': it may have executed. None: refused before an operation started. |
connection_invalidated |
True when the physical session was discarded. A pool replaces it; a Connection is closed. |
transaction_outcome |
'committed', 'rolled_back', 'released', or 'unknown' when finishing a managed transaction failed; None otherwise. |
- Gres never replays a statement. After
'possibly_sent', a write may have taken effect: inside a transaction, roll back; outside one, reconcile state before retrying. - Safe server retries:
SerializationFailureandDeadlockDetectedroll back the whole transaction; repeat it from the start (see Retrying serialization failures). - Not sent:
dispatch_state == 'not_sent'had no server effect and can be repeated once its cause is resolved. - Commit outcome: on
CommitOutcomeUnknownError(or any failure during commit), inspecttx.outcome. Bounded cleanup can still receive a late acknowledgment and movetx.outcomefrom'unknown'to'committed'. A confirmed'committed'remains committed even if the socket drops right after; never replay while'unknown'. - Timeouts and cancellation: expiring a deadline or cancelling a running query cancels it on PostgreSQL and waits up to
cleanup_timeoutto restore the session (aborting any open transaction). If cleanup fails, the session closes (connection_invalidated=True). Either way, the statement may already have taken effect. - Cache invalidation: a server error clears cached unnamed input types on that session, not across a pool. After an external schema migration, call
invalidate_types()on each pool.