@@ -389,19 +389,25 @@ func runSnapshotRestore() error {
389389 return fmt .Errorf ("database connection failed: %w" , err )
390390 }
391391
392+ // Connect to check database state and version
393+ db , err := regresql .OpenDB (cfg .PgUri )
394+ if err != nil {
395+ return fmt .Errorf ("failed to connect: %w" , err )
396+ }
397+ defer db .Close ()
398+
392399 // Check if database has existing tables and warn if --clean not specified
393400 if ! snapshotClean {
394- db , err := regresql .OpenDB (cfg .PgUri )
395- if err == nil {
396- defer db .Close ()
397- var tableCount int
398- db .QueryRow ("SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = 'public' AND table_type = 'BASE TABLE'" ).Scan (& tableCount )
399- if tableCount > 0 {
400- return fmt .Errorf ("database has %d existing table(s). Use --clean to drop them before restore, or manually clear the database" , tableCount )
401- }
401+ var tableCount int
402+ db .QueryRow ("SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = 'public' AND table_type = 'BASE TABLE'" ).Scan (& tableCount )
403+ if tableCount > 0 {
404+ return fmt .Errorf ("database has %d existing table(s). Use --clean to drop them before restore, or manually clear the database" , tableCount )
402405 }
403406 }
404407
408+ // Detect PostgreSQL version for statistics support
409+ serverCtx , _ := regresql .CaptureServerContext (db )
410+
405411 inputPath := snapshotInput
406412 if inputPath == "" {
407413 inputPath = regresql .GetSnapshotPath (cfg .Snapshot , snapshotCwd )
@@ -422,10 +428,12 @@ func runSnapshotRestore() error {
422428 return err
423429 }
424430
431+ withStats := serverCtx != nil && serverCtx .MajorVersion () >= 18
425432 opts := regresql.RestoreOptions {
426- InputPath : inputPath ,
427- Format : format ,
428- Clean : snapshotClean ,
433+ InputPath : inputPath ,
434+ Format : format ,
435+ Clean : snapshotClean ,
436+ WithStatistics : withStats ,
429437 }
430438
431439 fmt .Printf ("Restoring database snapshot...\n " )
@@ -434,6 +442,9 @@ func runSnapshotRestore() error {
434442 if snapshotClean {
435443 fmt .Printf (" Mode: clean (drop existing objects)\n " )
436444 }
445+ if withStats {
446+ fmt .Printf (" Stats: restoring optimizer statistics (PG18+)\n " )
447+ }
437448 fmt .Println ()
438449
439450 if err := regresql .RestoreSnapshot (cfg .PgUri , opts ); err != nil {
0 commit comments