@@ -374,7 +374,7 @@ WHERE oid = $1::regclass`,
374374 * If (partial) replication was done before on this slot, this clears the state
375375 * and starts again from scratch.
376376 */
377- async startInitialReplication ( replicationConnection : pgwire . PgConnection , status : InitResult ) {
377+ async startInitialReplication ( status : InitResult ) {
378378 // If anything here errors, the entire replication process is aborted,
379379 // and all connections are closed, including this one.
380380 const db = await this . connections . snapshotConnection ( ) ;
@@ -396,7 +396,12 @@ WHERE oid = $1::regclass`,
396396
397397 // We use the replication connection here, not a pool.
398398 // The replication slot must be created before we start snapshotting tables.
399- await replicationConnection . query ( `CREATE_REPLICATION_SLOT ${ slotName } LOGICAL pgoutput` ) ;
399+ const initReplicationConnection = await this . connections . replicationConnection ( ) ;
400+ try {
401+ await initReplicationConnection . query ( `CREATE_REPLICATION_SLOT ${ slotName } LOGICAL pgoutput` ) ;
402+ } finally {
403+ await initReplicationConnection . end ( ) ;
404+ }
400405
401406 this . logger . info ( `Created replication slot ${ slotName } ` ) ;
402407 }
@@ -815,37 +820,34 @@ WHERE oid = $1::regclass`,
815820
816821 async replicate ( ) {
817822 try {
818- // If anything errors here, the entire replication process is halted, and
819- // all connections automatically closed, including this one.
820- const initReplicationConnection = await this . connections . replicationConnection ( ) ;
821- await this . initReplication ( initReplicationConnection ) ;
822- await initReplicationConnection . end ( ) ;
823+ await this . initReplication ( ) ;
823824
824- // At this point, the above connection has often timed out, so we start a new one
825- const streamReplicationConnection = await this . connections . replicationConnection ( ) ;
826- await this . streamChanges ( streamReplicationConnection ) ;
827- await streamReplicationConnection . end ( ) ;
825+ // At this point, the above connection has often timed out, so we start a new one in streamChanges().
826+ await this . streamChanges ( ) ;
828827 } catch ( e ) {
829828 await this . storage . reportError ( e ) ;
830829 throw e ;
831830 }
832831 }
833832
834- async initReplication ( replicationConnection : pgwire . PgConnection ) {
833+ async initReplication ( ) {
835834 const result = await this . initSlot ( ) ;
836835 if ( result . needsInitialSync ) {
837- await this . startInitialReplication ( replicationConnection , result ) ;
836+ await this . startInitialReplication ( result ) ;
838837 }
839838 }
840839
841- async streamChanges ( replicationConnection : pgwire . PgConnection ) {
840+ async streamChanges ( ) {
841+ const streamReplicationConnection = await this . connections . replicationConnection ( ) ;
842842 try {
843- await this . streamChangesInternal ( replicationConnection ) ;
843+ await this . streamChangesInternal ( streamReplicationConnection ) ;
844844 } catch ( e ) {
845845 if ( isReplicationSlotInvalidError ( e ) ) {
846846 throw new MissingReplicationSlotError ( e . message , e ) ;
847847 }
848848 throw e ;
849+ } finally {
850+ await streamReplicationConnection . end ( ) ;
849851 }
850852 }
851853
0 commit comments