@@ -383,6 +383,11 @@ class Client extends EventEmitter {
383383 const completedQuery = this . _pipelineQueue . shift ( )
384384 if ( completedQuery ) {
385385 completedQuery . handleReadyForQuery ( this . connection )
386+ } else {
387+ // No queries in pipeline queue, but we received readyForQuery
388+ // This might happen due to message timing in pipeline mode
389+ // Just mark as ready for more queries
390+ this . readyForQuery = true
386391 }
387392 }
388393
@@ -808,6 +813,12 @@ class Client extends EventEmitter {
808813 this . _pipelining = false
809814 this . _pipelineActive = false
810815
816+ // Clear any pending sync timer
817+ if ( this . _pipelineSyncTimer ) {
818+ clearTimeout ( this . _pipelineSyncTimer )
819+ this . _pipelineSyncTimer = null
820+ }
821+
811822 // Send exit pipeline mode command to server
812823 this . connection . exitPipelineMode ( )
813824 }
@@ -845,11 +856,29 @@ class Client extends EventEmitter {
845856 this . _pipelineQueue . splice ( index , 1 )
846857 }
847858 } )
859+ } else {
860+ // Schedule a sync after a short delay to allow batching
861+ this . _schedulePipelineSync ( )
848862 }
849863
850864 return result
851865 }
852866
867+ _schedulePipelineSync ( ) {
868+ // Clear any existing sync timer
869+ if ( this . _pipelineSyncTimer ) {
870+ clearTimeout ( this . _pipelineSyncTimer )
871+ }
872+
873+ // Schedule a sync after a short delay to allow multiple queries to batch
874+ this . _pipelineSyncTimer = setTimeout ( ( ) => {
875+ if ( this . _pipelining && this . _pipelineQueue . length > 0 ) {
876+ this . connection . pipelineSync ( )
877+ }
878+ this . _pipelineSyncTimer = null
879+ } , 0 ) // Use 0 delay to sync on next tick
880+ }
881+
853882 _submitPipelineQuery ( query ) {
854883 if ( typeof query . text !== 'string' && typeof query . name !== 'string' ) {
855884 return new Error ( 'A query must have either text or a name. Supplying neither is unsupported.' )
0 commit comments