@@ -434,8 +434,10 @@ WHERE oid = $1::regclass`,
434
434
await this . storage . startBatch (
435
435
{ zeroLSN : ZERO_LSN , defaultSchema : POSTGRES_DEFAULT_SCHEMA , storeCurrentData : true , skipExistingRows : true } ,
436
436
async ( batch ) => {
437
+ let tablesWithStatus : SourceTable [ ] = [ ] ;
437
438
for ( let tablePattern of sourceTables ) {
438
439
const tables = await this . getQualifiedTableNames ( batch , db , tablePattern ) ;
440
+ // Pre-get counts
439
441
for ( let table of tables ) {
440
442
if ( table . snapshotComplete ) {
441
443
logger . info ( `${ this . slot_name } Skipping ${ table . qualifiedName } - snapshot already done` ) ;
@@ -444,11 +446,17 @@ WHERE oid = $1::regclass`,
444
446
const count = await this . estimatedCountNumber ( db , table ) ;
445
447
table = await batch . updateTableProgress ( table , { totalEstimatedCount : count } ) ;
446
448
this . relationCache . update ( table ) ;
447
- await this . snapshotTableInTx ( batch , db , table ) ;
448
- await touch ( ) ;
449
+ tablesWithStatus . push ( table ) ;
450
+
451
+ logger . info ( `${ this . slot_name } To replicate: ${ table . qualifiedName } ${ table . formatSnapshotProgress ( ) } ` ) ;
449
452
}
450
453
}
451
454
455
+ for ( let table of tablesWithStatus ) {
456
+ await this . snapshotTableInTx ( batch , db , table ) ;
457
+ await touch ( ) ;
458
+ }
459
+
452
460
// Always commit the initial snapshot at zero.
453
461
// This makes sure we don't skip any changes applied before starting this snapshot,
454
462
// in the case of snapshot retries.
@@ -505,11 +513,9 @@ WHERE oid = $1::regclass`,
505
513
table : storage . SourceTable ,
506
514
limited ?: PrimaryKeyValue [ ]
507
515
) {
508
- logger . info ( `${ this . slot_name } Replicating ${ table . qualifiedName } ` ) ;
509
516
let totalEstimatedCount = table . snapshotStatus ?. totalEstimatedCount ;
510
517
let at = table . snapshotStatus ?. replicatedCount ?? 0 ;
511
518
let lastCountTime = 0 ;
512
- let lastLogIndex = 0 ;
513
519
let q : SnapshotQuery ;
514
520
// We do streaming on two levels:
515
521
// 1. Coarse level: DELCARE CURSOR, FETCH 10000 at a time.
@@ -519,15 +525,21 @@ WHERE oid = $1::regclass`,
519
525
} else if ( ChunkedSnapshotQuery . supports ( table ) ) {
520
526
// Single primary key - we can use the primary key for chunking
521
527
const orderByKey = table . replicaIdColumns [ 0 ] ;
522
- logger . info ( `Chunking ${ table . qualifiedName } by ${ orderByKey . name } ` ) ;
523
- q = new ChunkedSnapshotQuery ( db , table , this . snapshotChunkSize ) ;
528
+ q = new ChunkedSnapshotQuery ( db , table , this . snapshotChunkSize , table . snapshotStatus ?. lastKey ?? null ) ;
524
529
if ( table . snapshotStatus ?. lastKey != null ) {
525
- ( q as ChunkedSnapshotQuery ) . setLastKeySerialized ( table . snapshotStatus ! . lastKey ) ;
526
- logger . info ( `Resuming from ${ ( q as ChunkedSnapshotQuery ) . lastKey } ` ) ;
530
+ logger . info (
531
+ `${ this . slot_name } Replicating ${ table . qualifiedName } ${ table . formatSnapshotProgress ( ) } - resuming from ${ orderByKey . name } > ${ ( q as ChunkedSnapshotQuery ) . lastKey } `
532
+ ) ;
533
+ } else {
534
+ logger . info (
535
+ `${ this . slot_name } Replicating ${ table . qualifiedName } ${ table . formatSnapshotProgress ( ) } - resumable`
536
+ ) ;
527
537
}
528
538
} else {
529
539
// Fallback case - query the entire table
530
- logger . info ( `Snapshot ${ table . qualifiedName } without chunking` ) ;
540
+ logger . info (
541
+ `${ this . slot_name } Replicating ${ table . qualifiedName } ${ table . formatSnapshotProgress ( ) } - not resumable`
542
+ ) ;
531
543
q = new SimpleSnapshotQuery ( db , table , this . snapshotChunkSize ) ;
532
544
at = 0 ;
533
545
}
@@ -566,12 +578,6 @@ WHERE oid = $1::regclass`,
566
578
if ( rows . length > 0 ) {
567
579
hasRemainingData = true ;
568
580
}
569
- if ( rows . length > 0 && at - lastLogIndex >= 5000 ) {
570
- logger . info (
571
- `${ this . slot_name } Replicating ${ table . qualifiedName } ${ at } /${ limited ? `${ limited . length } L` : this . formatCount ( totalEstimatedCount ) } `
572
- ) ;
573
- lastLogIndex = at ;
574
- }
575
581
if ( this . abort_signal . aborted ) {
576
582
throw new Error ( `Aborted initial replication of ${ this . slot_name } ` ) ;
577
583
}
@@ -593,9 +599,6 @@ WHERE oid = $1::regclass`,
593
599
594
600
await touch ( ) ;
595
601
}
596
- logger . info (
597
- `${ this . slot_name } Replicated ${ table . qualifiedName } ${ at } /${ limited ? `${ limited . length } L` : this . formatCount ( totalEstimatedCount ) } `
598
- ) ;
599
602
600
603
if ( limited == null ) {
601
604
// Important: flush before marking progress
@@ -614,6 +617,10 @@ WHERE oid = $1::regclass`,
614
617
totalEstimatedCount : totalEstimatedCount
615
618
} ) ;
616
619
this . relationCache . update ( table ) ;
620
+
621
+ logger . info ( `${ this . slot_name } Replicating ${ table . qualifiedName } ${ table . formatSnapshotProgress ( ) } ` ) ;
622
+ } else {
623
+ logger . info ( `${ this . slot_name } Replicating ${ table . qualifiedName } ${ at } /${ limited . length } for resnapshot` ) ;
617
624
}
618
625
}
619
626
await batch . flush ( ) ;
0 commit comments