Jump to solution
Details

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%.

Open PR/Commit
@@ -16,6 +16,13 @@ Python 3.3.0 (unreleased) +Python 3.2.6 (unreleased) +^^^^^^^^^^^^^^^^^^^^^^^^^ +
fix.md
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
Production impact:
  • 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

Discussion

High-signal excerpts from the issue thread (symptoms, repros, edge-cases).

“Thank you @AndrewJackson2020”
@dvarrazzo · 2025-03-10 · source
“Hello, thank you very much for the report”
@dvarrazzo · 2025-03-10 · source
“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…”
@AndrewJackson2020 · 2025-03-10 · source

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
signature.txt
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.txt
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.

When NOT to use: Do not use this fix if the application requires strict session attribute enforcement.

Fix reference: https://github.com/psycopg/psycopg/pull/1022

Last verified: 2026-02-09. Validate in your environment.

Get updates

We publish verified fixes weekly. No spam.

Subscribe

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.