Skip to content

Commit ae07ed2

Browse files
veksenclaude
andcommitted
fix: only run ANALYZE when no statisticsPath is provided
Move the ANALYZE call inside the else branch so it only runs when buildStatsFromDatabase needs accurate pg_class.relpages. When users provide their own stats export, ANALYZE is skipped entirely. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent cfb5add commit ae07ed2

1 file changed

Lines changed: 10 additions & 8 deletions

File tree

src/runner.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,16 @@ export class Runner {
6767
ignoredQueryHashes?: string[];
6868
}) {
6969
const db = connectToSource(options.postgresUrl);
70-
// Run ANALYZE before reading statistics so pg_statistic (column-level
71-
// stats like n_distinct) is populated deterministically from the current
72-
// data. Without this, autovacuum may or may not have analyzed tables,
73-
// causing the same query to produce different EXPLAIN costs across runs.
74-
await db.exec("ANALYZE");
75-
const statisticsMode = options.statisticsPath
76-
? Runner.decideStatisticsMode(options.statisticsPath)
77-
: await buildStatsFromDatabase(db);
70+
let statisticsMode: StatisticsMode;
71+
if (options.statisticsPath) {
72+
statisticsMode = Runner.decideStatisticsMode(options.statisticsPath);
73+
} else {
74+
// Run ANALYZE so pg_class.relpages and pg_statistic reflect the
75+
// current data. Without this, relpages can be 0 after fresh
76+
// inserts and column stats depend on autovacuum timing.
77+
await db.exec("ANALYZE");
78+
statisticsMode = await buildStatsFromDatabase(db);
79+
}
7880
const stats = await Statistics.fromPostgres(db, statisticsMode);
7981
const existingIndexes = await stats.getExistingIndexes();
8082
const optimizer = new IndexOptimizer(db, stats, existingIndexes);

0 commit comments

Comments
 (0)