- 
          
- 
                Notifications
    You must be signed in to change notification settings 
- Fork 89
          Implement COPY … FROM STDIN queries
          #566
        
          New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Open
      
      
            ahoppen
  wants to merge
  7
  commits into
  vapor:main
  
    
      
        
          
  
    
      Choose a base branch
      
     
    
      
        
      
      
        
          
          
        
        
          
            
              
              
              
  
           
        
        
          
            
              
              
           
        
       
     
  
        
          
            
          
            
          
        
       
    
      
from
ahoppen:copy-from
  
      
      
   
  
    
  
  
  
 
  
      
    base: main
Could not load branches
            
              
  
    Branch not found: {{ refName }}
  
            
                
      Loading
              
            Could not load tags
            
            
              Nothing to show
            
              
  
            
                
      Loading
              
            Are you sure you want to change the base?
            Some commits from the old base branch may be removed from the timeline,
            and old review comments may become outdated.
          
          
                
     Open
            
            
          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
    
  
  
    
    
              
                    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 
                  
              
              
            
            
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Another review round. Thanks so much for pushing through this!
COPY … FROM STDINCOPY … FROM STDIN queries
      This implements support for COPY operations using `COPY … FROM STDIN` queries for fast data transfer from the client to the backend.
              
                    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
        
      Swift 5.10 does not support `#isolation`, which we use.
  
    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