@@ -83,6 +83,8 @@ class Client extends EventEmitter {
8383 encoding : this . connectionParameters . client_encoding || 'utf8' ,
8484 } )
8585 this . _queryQueue = [ ]
86+ this . _sentQueryQueue = [ ]
87+ this . pipelining = false
8688 this . binary = c . binary || defaults . binary
8789 this . processID = null
8890 this . secretKey = null
@@ -126,6 +128,9 @@ class Client extends EventEmitter {
126128 this . _activeQuery = null
127129 }
128130
131+ this . _sentQueryQueue . forEach ( enqueueError )
132+ this . _sentQueryQueue . length = 0
133+
129134 this . _queryQueue . forEach ( enqueueError )
130135 this . _queryQueue . length = 0
131136 }
@@ -363,6 +368,10 @@ class Client extends EventEmitter {
363368 if ( activeQuery ) {
364369 activeQuery . handleReadyForQuery ( this . connection )
365370 }
371+ if ( this . pipelining && this . _sentQueryQueue . length > 0 ) {
372+ this . _activeQuery = this . _sentQueryQueue . shift ( )
373+ this . readyForQuery = false
374+ }
366375 this . _pulseQueryQueue ( )
367376 }
368377
@@ -476,6 +485,7 @@ class Client extends EventEmitter {
476485 // it again on the same client
477486 if ( activeQuery . name ) {
478487 this . connection . parsedStatements [ activeQuery . name ] = activeQuery . text
488+ delete this . connection . submittedNamedStatements [ activeQuery . name ]
479489 }
480490 }
481491
@@ -554,6 +564,8 @@ class Client extends EventEmitter {
554564 } )
555565 } else if ( client . _queryQueue . indexOf ( query ) !== - 1 ) {
556566 client . _queryQueue . splice ( client . _queryQueue . indexOf ( query ) , 1 )
567+ } else if ( client . _sentQueryQueue . indexOf ( query ) !== - 1 ) {
568+ client . _sentQueryQueue . splice ( client . _sentQueryQueue . indexOf ( query ) , 1 )
557569 }
558570 }
559571
@@ -577,6 +589,10 @@ class Client extends EventEmitter {
577589 }
578590
579591 _pulseQueryQueue ( ) {
592+ if ( this . pipelining ) {
593+ this . _pulsePipelinedQueryQueue ( )
594+ return
595+ }
580596 if ( this . readyForQuery === true ) {
581597 this . _activeQuery = this . _queryQueue . shift ( )
582598 const activeQuery = this . _getActiveQuery ( )
@@ -599,6 +615,31 @@ class Client extends EventEmitter {
599615 }
600616 }
601617
618+ _pulsePipelinedQueryQueue ( ) {
619+ if ( ! this . _connected ) {
620+ return
621+ }
622+ while ( this . _queryQueue . length > 0 ) {
623+ const query = this . _queryQueue . shift ( )
624+ this . hasExecuted = true
625+ const queryError = query . submit ( this . connection )
626+ if ( queryError ) {
627+ process . nextTick ( ( ) => {
628+ query . handleError ( queryError , this . connection )
629+ } )
630+ continue
631+ }
632+ this . _sentQueryQueue . push ( query )
633+ }
634+ if ( ! this . _activeQuery && this . _sentQueryQueue . length > 0 ) {
635+ this . _activeQuery = this . _sentQueryQueue . shift ( )
636+ this . readyForQuery = false
637+ }
638+ if ( ! this . _activeQuery && this . _sentQueryQueue . length === 0 && this . _queryQueue . length === 0 && this . hasExecuted ) {
639+ this . emit ( 'drain' )
640+ }
641+ }
642+
602643 query ( config , values , callback ) {
603644 // can take in strings, config object or query object
604645 let query
@@ -654,6 +695,11 @@ class Client extends EventEmitter {
654695 const index = this . _queryQueue . indexOf ( query )
655696 if ( index > - 1 ) {
656697 this . _queryQueue . splice ( index , 1 )
698+ } else if ( this . pipelining ) {
699+ const sentIndex = this . _sentQueryQueue . indexOf ( query )
700+ if ( sentIndex > - 1 ) {
701+ this . _sentQueryQueue . splice ( sentIndex , 1 )
702+ }
657703 }
658704
659705 this . _pulseQueryQueue ( )
@@ -687,7 +733,7 @@ class Client extends EventEmitter {
687733 return result
688734 }
689735
690- if ( this . _queryQueue . length > 0 ) {
736+ if ( this . _queryQueue . length > 0 && ! this . pipelining ) {
691737 queryQueueLengthDeprecationNotice ( )
692738 }
693739 this . _queryQueue . push ( query )
@@ -715,7 +761,7 @@ class Client extends EventEmitter {
715761 }
716762 }
717763
718- if ( this . _getActiveQuery ( ) || ! this . _queryable ) {
764+ if ( this . _getActiveQuery ( ) || this . _sentQueryQueue . length > 0 || ! this . _queryable ) {
719765 // if we have an active query we need to force a disconnect
720766 // on the socket - otherwise a hung query could block end forever
721767 this . connection . stream . destroy ( )
0 commit comments