The Fix
Implemented libpq semantic for target_session_attrs=prefer-standby, allowing attempts to connect in standby mode before falling back to any mode.
Based on closed psycopg/psycopg issue #1021 · PR/commit linked
Production note: Most teams hit this during upgrades or environment changes. Roll out with a canary and smoke critical endpoints (health, OpenAPI/docs) before 100%.
@@ -16,6 +16,13 @@ Python 3.3.0 (unreleased)
+Python 3.2.6 (unreleased)
+^^^^^^^^^^^^^^^^^^^^^^^^^
+
Option A — Apply the official fix\nImplemented libpq semantic for target_session_attrs=prefer-standby, allowing attempts to connect in standby mode before falling back to any mode.\nWhen NOT to use: Do not use this fix if the application requires strict session attribute enforcement.\n\n
Why This Fix Works in Production
- Trigger: timeout = timeout_from_conninfo(params)
- Mechanism: The library did not handle the target_session_attrs=prefer-standby correctly during connection attempts
- If left unfixed, the same config can fail only in production (env differences), causing startup failures or partial feature outages.
Why This Breaks in Prod
- The library did not handle the target_session_attrs=prefer-standby correctly during connection attempts
- Production symptom (often without a traceback): timeout = timeout_from_conninfo(params)
Proof / Evidence
- GitHub issue: #1021
- Fix PR: https://github.com/psycopg/psycopg/pull/1022
- Reproduced locally: No (not executed)
- Last verified: 2026-02-09
- Confidence: 0.80
- Did this fix it?: Yes (upstream fix exists)
- Own content ratio: 0.76
Discussion
High-signal excerpts from the issue thread (symptoms, repros, edge-cases).
“Thank you @AndrewJackson2020”
“Hello, thank you very much for the report”
“I ran code similar to this and it worked for me (modified a pip install), copy pasted into blocks into the git source for the…”
Failure Signature (Search String)
- timeout = timeout_from_conninfo(params)
- Did you try if postgres behaves well in the other cases - if it fails an attempt when `target_session_attrs` doesn't mach the characteristic requested ( i.e. in the `read-write`,
Copy-friendly signature
Failure Signature
-----------------
timeout = timeout_from_conninfo(params)
Did you try if postgres behaves well in the other cases - if it fails an attempt when `target_session_attrs` doesn't mach the characteristic requested ( i.e. in the `read-write`, `read-only`, `primary`, `standby` modes)?
Error Message
Signature-only (no traceback captured)
Error Message
-------------
timeout = timeout_from_conninfo(params)
Did you try if postgres behaves well in the other cases - if it fails an attempt when `target_session_attrs` doesn't mach the characteristic requested ( i.e. in the `read-write`, `read-only`, `primary`, `standby` modes)?
What Broke
Connections failed to establish when prefer-standby was specified, leading to potential downtime.
Why It Broke
The library did not handle the target_session_attrs=prefer-standby correctly during connection attempts
Fix Options (Details)
Option A — Apply the official fix
Implemented libpq semantic for target_session_attrs=prefer-standby, allowing attempts to connect in standby mode before falling back to any mode.
Fix reference: https://github.com/psycopg/psycopg/pull/1022
Last verified: 2026-02-09. Validate in your environment.
When NOT to Use This Fix
- Do not use this fix if the application requires strict session attribute enforcement.
Did This Fix Work in Your Case?
Quick signal helps us prioritize which fixes to verify and improve.
Prevention
- Make timeouts explicit and test them (unit + integration) to avoid silent behavior changes.
- Instrument retries (attempt count + reason) and alert on spikes to catch dependency slowdowns.
Related Issues
No related fixes found.
Sources
We don’t republish the full GitHub discussion text. Use the links above for context.