PostgreSQL 14: lo_export/large object operations intermittently fail with 'could not open large object' when keepalives drop during long client transactions
Environment
PostgreSQL 14.6 on Ubuntu 22.04 LTS, replica streaming WAL via replication slot; application uses libpq-based client on Python (psycopg2) performing large object operations within long-lived transactions over a TCP connection with an intermediate NAT/firewall that drops idle flows after 300s.
Symptoms
Occasionally, during long transactions that create and later export large objects (lo_create + lo_open + lo_export), the server errors with 'could not open large object' for the OID that was created earlier in the same transaction. The client stacktrace shows the error after a network stall. If the same sequence is re-run in a fresh connection, it succeeds. The issue is intermittent and correlates with long idle periods between large object creation and export.
Observations
Postgres server logs show 'terminating connection due to administrator command' around the same time for some sessions; tcpdump on server shows RST from firewall/NAT targeting the client connection after ~300s of idle. When the client enables TCP keepalives with interval 60s, the failures drop to near-zero. The large object OIDs referenced are present in pg_largeobject and pg_largeobject_metadata when queried after a failure, but the transaction that attempted export is in an aborted state. VACUUM and autovacuum are not removing the objects (they remain).
Already attempted
Enabled client-side statement_timeout and increased application retries; added explicit periodic simple queries to keep connection active (workaround). Upgraded psycopg2 to latest; tested with a direct LAN connection bypassing NAT — problem not observed. Examined server-side logs; no disk corruption detected. Replaying problematic sequence in psql with same delays reproduced the error when the connection was dropped mid-transaction.
Question
How should I handle large object workflows over unreliable intermediate networks to avoid transient 'could not open large object' errors and ensure transaction safety, and what server/client-side configurations are recommended to prevent this class of failure?
Verified community troubleshooting
No answers yet..
Incident analysis
Most likely causes
Summary
Suggested Fixes
Continue troubleshooting
Related troubles
Matched from the title, description and tags
- Databases29% match
PostgreSQL logical replication slot grows until disk full after subscriber disconnects during nightly load
Open0 answers - Databases27% match
PostgreSQL replica stops streaming WAL after pg_repack on primary; replication slot reports missing WAL
Open0 answers - Databases23% match
PostgreSQL standby stops applying WAL during long-running transactions on primary after pg_basebackup restore
Open0 answers - Databases23% match
PostgreSQL logical replication falls behind after ALTER TABLE ADD COLUMN with non-null DEFAULT on large table
Open0 answers - Databases23% match
PostgreSQL WAL archiving stalls during pg_basebackup when archive_command uses rsync over SSH
Open0 answers