[MEDIUM] Free COPY buffers when decoders raise - #742
Merged
larskanis merged 1 commit intoSep 3, 2026
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
PG::Connection#sync_get_copy_datareleases the libpq buffer after decoding, but a decoder exception bypassesPQfreemem. Repeated rejected rows therefore retain every COPY buffer and can exhaust process memory.This change wraps decoding in
rb_ensure, so the libpq buffer is released on both success and exception. Successful decoding behavior is unchanged.Reproduction
I used a custom
PG::SimpleDecoderwhosedecodemethod raises, then rescued each error while reading four local COPY streams of 32 one-MiB rows. With currentmaster, RSS rose each round:With this patch, memory stabilized after allocator warmup:
Each run called
GC.startbefore measuring.Verification
pg_service/libpq 15 expectation mismatch; the COPY coverage passed.git diff --checkpasses.Limitations
The memory measurements used a local PostgreSQL 15 server and RSS, so allocator retention accounts for the patched run's initial rise. No new automated spec is included.
Breaking changes
None.