Implement COPY … FROM STDIN queries#566
Merged
fabianfett merged 10 commits intovapor:mainfrom Dec 3, 2025
Merged
Conversation
fabianfett
reviewed
Jun 24, 2025
fabianfett
reviewed
Jun 24, 2025
fabianfett
reviewed
Jun 24, 2025
fabianfett
reviewed
Jun 24, 2025
fabianfett
reviewed
Jun 24, 2025
Sources/PostgresNIO/New/Connection State Machine/ExtendedQueryStateMachine.swift
Outdated
Show resolved
Hide resolved
fabianfett
reviewed
Jun 24, 2025
Sources/PostgresNIO/New/Connection State Machine/ConnectionStateMachine.swift
Outdated
Show resolved
Hide resolved
fabianfett
reviewed
Jul 3, 2025
fabianfett
reviewed
Jul 6, 2025
Collaborator
fabianfett
left a comment
There was a problem hiding this comment.
Another review round. Thanks so much for pushing through this!
COPY … FROM STDINCOPY … FROM STDIN queries
e9aa0bd to
c5f2928
Compare
fabianfett
reviewed
Jul 8, 2025
Sources/PostgresNIO/Connection/PostgresConnection+CopyFrom.swift
Outdated
Show resolved
Hide resolved
fabianfett
reviewed
Jul 8, 2025
Sources/PostgresNIO/Connection/PostgresConnection+CopyFrom.swift
Outdated
Show resolved
Hide resolved
fabianfett
reviewed
Jul 8, 2025
Sources/PostgresNIO/Connection/PostgresConnection+CopyFrom.swift
Outdated
Show resolved
Hide resolved
fabianfett
reviewed
Jul 8, 2025
fabianfett
reviewed
Jul 8, 2025
fabianfett
reviewed
Jul 8, 2025
fabianfett
reviewed
Jul 8, 2025
Sources/PostgresNIO/New/Connection State Machine/ExtendedQueryStateMachine.swift
Show resolved
Hide resolved
0e79469 to
1602d85
Compare
fabianfett
reviewed
Nov 28, 2025
Collaborator
fabianfett
left a comment
There was a problem hiding this comment.
Another round. Please also make sure that merge conflicts are resolved and tests pass
Sources/PostgresNIO/Connection/PostgresConnection+CopyFrom.swift
Outdated
Show resolved
Hide resolved
Sources/PostgresNIO/Connection/PostgresConnection+CopyFrom.swift
Outdated
Show resolved
Hide resolved
Sources/PostgresNIO/Connection/PostgresConnection+CopyFrom.swift
Outdated
Show resolved
Hide resolved
Sources/PostgresNIO/Connection/PostgresConnection+CopyFrom.swift
Outdated
Show resolved
Hide resolved
Sources/PostgresNIO/Connection/PostgresConnection+CopyFrom.swift
Outdated
Show resolved
Hide resolved
Sources/PostgresNIO/Connection/PostgresConnection+CopyFrom.swift
Outdated
Show resolved
Hide resolved
Sources/PostgresNIO/Connection/PostgresConnection+CopyFrom.swift
Outdated
Show resolved
Hide resolved
Sources/PostgresNIO/New/Connection State Machine/ConnectionStateMachine.swift
Outdated
Show resolved
Hide resolved
Sources/PostgresNIO/New/Connection State Machine/ExtendedQueryStateMachine.swift
Outdated
Show resolved
Hide resolved
This implements support for COPY operations using `COPY … FROM STDIN` queries for fast data transfer from the client to the backend.
Contributor
Author
|
Thanks for the review, Fabian 🙂 Rebased, updated tests to use Swift Testing and addressed/replied to the review comments. |
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## main #566 +/- ##
==========================================
+ Coverage 75.22% 75.56% +0.33%
==========================================
Files 132 134 +2
Lines 9534 9887 +353
==========================================
+ Hits 7172 7471 +299
- Misses 2362 2416 +54
🚀 New features to boost your workflow:
|
fabianfett
approved these changes
Dec 3, 2025
Collaborator
fabianfett
left a comment
There was a problem hiding this comment.
I think this is good to merge. Anything that comes up now we can do in a follow up.
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.
This implements support for COPY operations using
COPY … FROM STDINqueries for fast data transfer from the client to the backend.Performance
A quick note on local performance measurements: Inserting the numbers from 0 to 1,000,000 into a table that has two columns (
INTandVARCHAR) takes ~150ms. Depending on the exact implementation, the majority of the active CPU cycles are spent converting the numbers to strings, inside string interpolation or insideByteBuffer._setBytes. If I remove the code that sends theCopyDatamessages to the backend (but keep all other logic that might incur thread hopes), the test described above takes ~50ms and utilizes the CPU at ~200%, so the real bottleneck here is the Postges backend handling the data. For comparisonpsycopg2takes 210ms with the data to be written already prepared in aStringIOobject. So, performance-wise this PR should be good to go.Ideas for follow-up PRs
PostgresCopyFromWriterto reduce the number ofCopyDatamessages we need to send (and thus the protocol overhead). Alternatively, we can leave that kind of optimization to the client.COPY FROM.Fixes #290