Skip to content

Conversation

@lieut-data
Copy link
Contributor

@lieut-data lieut-data commented Jan 28, 2026

When PostgreSQL cannot fork a backend process (e.g., an external process limit), it sends a plain text error like Ecould not fork new process for connection: Resource temporarily unavailable before any protocol handshake. The 'E' byte gets parsed as an ErrorResponse message type, but the following bytes are plain text, not a binary length field.

This causes lib/pq to interpret ASCII text as a message length (e.g., "coul" = 0x636f756c = 1.6GB), leading to massive memory allocations and a likely OOM under connection pressure.

This fix matches libpq's behavior in fe-connect.c: if an ErrorResponse has msgLength < 8 or > 30000, treat it as a pre-protocol plain text error and read it as a null-terminated string instead.

There's a decent chance this is related to #638.

When PostgreSQL cannot fork a backend process (e.g., an external process
limit), it sends a plain text error like "Ecould not fork new process
for connection: Resource temporarily unavailable" before any protocol
handshake. The 'E' byte gets parsed as an ErrorResponse message type,
but the following bytes are plain text, not a binary length field.

This causes lib/pq to interpret ASCII text as a message length
(e.g., "coul" = 0x636f756c = 1.6GB), leading to massive memory
allocations and a likely OOM under connection pressure.

This fix matches libpq's behavior in fe-connect.c: if an ErrorResponse
has msgLength < 8 or > 30000, treat it as a pre-protocol plain text
error and read it as a null-terminated string instead.
@arp242
Copy link
Collaborator

arp242 commented Jan 28, 2026

Was just about to do a release, so fairly good timing on this.

Do these tweaks seem alright to you? c3c7698 (I can't update this PR because it's created from master in your fork instead of a branch; pushing from the git CLI doesn't let me update the master branch and I've never been able to figure out how to get around that other than using the GitHub web UI).

@arp242
Copy link
Collaborator

arp242 commented Jan 28, 2026

Merged via #1249

@lieut-data
Copy link
Contributor Author

Nice, thanks @arp242! I'll re-test with my project to verify, but it looks good!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants