-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrunner.ts
More file actions
533 lines (518 loc) · 16.5 KB
/
runner.ts
File metadata and controls
533 lines (518 loc) · 16.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
import * as core from "@actions/core";
import * as prettier from "prettier";
import prettierPluginSql from "prettier-plugin-sql";
import csv from "fast-csv";
import { Readable } from "node:stream";
import { statSync, readFileSync } from "node:fs";
import { spawn } from "node:child_process";
import { fingerprint } from "@libpg-query/parser";
import { preprocessEncodedJson } from "./sql/json.ts";
import {
Analyzer,
ExportedStats,
IndexedTable,
IndexOptimizer,
type IndexRecommendation,
type Nudge,
type SQLCommenterTag,
type TableReference,
OptimizeResult,
type Postgres,
PostgresQueryBuilder,
Statistics,
StatisticsMode,
} from "@query-doctor/core";
import { ExplainedLog } from "./sql/pg_log.ts";
import { GithubReporter } from "./reporters/github/github.ts";
import {
deriveIndexStatistics,
type ReportContext,
type ReportIndexRecommendation,
type ReportQueryCostWarning,
type ReportStatistics,
} from "./reporters/reporter.ts";
import { DEFAULT_CONFIG, type AnalyzerConfig } from "./config.ts";
const bgBrightMagenta = (s: string) => `\x1b[105m${s}\x1b[0m`;
const yellow = (s: string) => `\x1b[33m${s}\x1b[0m`;
const blue = (s: string) => `\x1b[34m${s}\x1b[0m`;
import { env } from "./env.ts";
import { connectToSource } from "./sql/postgresjs.ts";
import { parse } from "@libpg-query/parser";
import { Connectable } from "./sync/connectable.ts";
import { buildStatsFromDatabase } from "./build-stats.ts";
export class Runner {
private readonly seenQueries = new Set<string>();
public readonly queryStats: ReportStatistics = {
total: 0,
errored: 0,
matched: 0,
optimized: 0,
};
constructor(
private readonly db: Postgres,
private readonly optimizer: IndexOptimizer,
private readonly existingIndexes: IndexedTable[],
private readonly stats: Statistics,
private readonly logPath: string,
private readonly maxCost?: number,
private readonly ignoredQueryHashes: Set<string> = new Set(),
) { }
static async build(options: {
postgresUrl: Connectable;
statisticsPath?: string;
maxCost?: number;
logPath: string;
ignoredQueryHashes?: string[];
}) {
const db = connectToSource(options.postgresUrl);
let statisticsMode: StatisticsMode;
if (options.statisticsPath) {
statisticsMode = Runner.decideStatisticsMode(options.statisticsPath);
} else {
// Run ANALYZE so pg_class.relpages and pg_statistic reflect the
// current data. Without this, relpages can be 0 after fresh
// inserts and column stats depend on autovacuum timing.
await db.exec("ANALYZE");
statisticsMode = await buildStatsFromDatabase(db);
}
const stats = await Statistics.fromPostgres(db, statisticsMode);
const existingIndexes = await stats.getExistingIndexes();
const optimizer = new IndexOptimizer(db, stats, existingIndexes);
return new Runner(
db,
optimizer,
existingIndexes,
stats,
options.logPath,
options.maxCost,
new Set(options.ignoredQueryHashes ?? []),
);
}
async close() {
await (this.db as unknown as { close(): Promise<void> }).close();
}
async run(config: AnalyzerConfig = DEFAULT_CONFIG) {
const startDate = new Date();
const logSize = statSync(this.logPath).size;
console.log(`logPath=${this.logPath},fileSize=${logSize}`);
const args = [
"--dump-raw-csv",
"--no-progressbar",
"-f",
"stderr",
this.logPath,
];
console.log(`pgbadger ${args.join(" ")}`);
const child = spawn("pgbadger", args, {
stdio: ["ignore", "pipe", "pipe"],
});
child.stderr!.pipe(process.stderr);
let error: Error | undefined;
const stream = csv
.parseStream(child.stdout!, {
headers: false,
})
.on("error", (err) => {
error = err;
});
const recommendations: ReportIndexRecommendation[] = [];
const queriesPastThreshold: ReportQueryCostWarning[] = [];
const allResults: QueryProcessResult[] = [];
console.time("total");
for await (const chunk of stream) {
const [
_timestamp,
_username,
_dbname,
_pid,
_client,
_sessionid,
loglevel,
_sqlstate,
_duration,
queryString,
_parameters,
_appname,
_backendtype,
_queryid,
] = chunk as string[];
if (loglevel !== "LOG" || !queryString.startsWith("plan:")) {
continue;
}
const planString: string = queryString.split("plan:")[1].trim();
const json = preprocessEncodedJson(planString);
if (!json) {
console.log("Skipping LOG that is not JSON", queryString);
continue;
}
let parsed: ExplainedLog;
try {
parsed = ExplainedLog.fromLog(json);
} catch (e) {
console.log(e);
console.log(
"Log line that looked like valid auto_explain was not valid json?",
);
continue;
}
const result = await this.processQuery(parsed);
if (result.kind !== "invalid") {
allResults.push(result);
}
switch (result.kind) {
case "error":
this.queryStats.errored++;
break;
case "cost_past_threshold":
queriesPastThreshold.push(result.warning);
break;
case "recommendation":
recommendations.push(result.recommendation);
break;
case "no_improvement":
case "zero_cost_plan":
case "invalid":
break;
}
}
await new Promise<void>((resolve) => child.on("close", () => resolve()));
console.log(
`Matched ${this.queryStats.matched} queries out of ${this.queryStats.total}`,
);
const filteredRecommendations =
config.minimumCost > 0
? recommendations.filter((r) => r.baseCost > config.minimumCost)
: recommendations;
const filteredThresholdWarnings =
config.minimumCost > 0
? queriesPastThreshold.filter((w) => w.baseCost > config.minimumCost)
: queriesPastThreshold;
const statistics = deriveIndexStatistics(filteredRecommendations);
const timeElapsed = Date.now() - startDate.getTime();
if (config.minimumCost > 0) {
const filtered =
recommendations.length -
filteredRecommendations.length +
(queriesPastThreshold.length - filteredThresholdWarnings.length);
if (filtered > 0) {
console.log(
`Filtered ${filtered} queries below minimumCost=${config.minimumCost} from PR comment`,
);
}
}
const reportContext: ReportContext = {
statisticsMode: this.stats.mode,
recommendations: filteredRecommendations,
queriesPastThreshold: filteredThresholdWarnings,
queryStats: Object.freeze(this.queryStats),
statistics,
error,
metadata: { logSize, timeElapsed },
};
console.timeEnd("total");
return { reportContext, allResults };
}
async report(reportContext: ReportContext) {
const reporter = new GithubReporter(env.GITHUB_TOKEN);
console.log(`Generating report (${reporter.provider()})`);
await reporter.report(reportContext);
}
async processQuery(log: ExplainedLog): Promise<QueryProcessResult> {
this.queryStats.total++;
const { query } = log;
const queryFingerprint = await fingerprint(query);
if (this.ignoredQueryHashes.has(queryFingerprint)) {
if (env.DEBUG) {
console.log("Skipping ignored query", queryFingerprint);
}
return { kind: "invalid" };
}
if (log.isIntrospection) {
if (env.DEBUG) {
console.log("Skipping introspection query", queryFingerprint);
}
return { kind: "invalid" };
}
if (this.seenQueries.has(queryFingerprint)) {
if (env.DEBUG) {
console.log("Skipping duplicate query", queryFingerprint);
}
return { kind: "invalid" };
}
this.seenQueries.add(queryFingerprint);
const analyzer = new Analyzer(parse);
const formattedQuery = await this.formatQuery(query);
const { indexesToCheck, ansiHighlightedQuery, referencedTables, nudges, tags } =
await analyzer.analyze(formattedQuery);
const selectsCatalog = referencedTables.find((ref) =>
ref.table.startsWith("pg_"),
);
if (selectsCatalog) {
if (env.DEBUG) {
console.log(
"Skipping query that selects from catalog tables",
selectsCatalog,
queryFingerprint,
);
}
return { kind: "invalid" };
}
const indexCandidates = analyzer.deriveIndexes(
this.stats.ownMetadata,
indexesToCheck,
referencedTables,
);
if (indexCandidates.length === 0) {
if (env.DEBUG) {
console.log(ansiHighlightedQuery);
console.log("No index candidates found", queryFingerprint);
}
if (typeof this.maxCost === "number" && log.plan.cost > this.maxCost) {
return {
kind: "cost_past_threshold",
rawQuery: query,
nudges,
tags,
referencedTables,
warning: {
fingerprint: queryFingerprint,
formattedQuery,
baseCost: log.plan.cost,
explainPlan: log.plan.json,
maxCost: this.maxCost,
},
};
}
}
return core.group<QueryProcessResult>(
`query:${queryFingerprint}`,
async (): Promise<QueryProcessResult> => {
console.time(`timing`);
this.printLegend();
console.log(ansiHighlightedQuery);
// TODO: give concrete type
let out: OptimizeResult;
this.queryStats.matched++;
try {
const builder = new PostgresQueryBuilder(query);
out = await this.optimizer.run(builder, indexCandidates);
} catch (err) {
console.error(err);
console.error(
`Something went wrong while running this query. Skipping`,
);
// this.queryStats.errored++;
console.timeEnd(`timing`);
return {
kind: "error",
error: err as Error,
fingerprint: queryFingerprint,
rawQuery: query,
formattedQuery,
nudges,
tags,
referencedTables,
};
}
if (out.kind === "ok") {
const existingIndexesForQuery = Array.from(out.existingIndexes)
.map((index) => {
const existing = this.existingIndexes.find(
(e) => e.index_name === index,
);
if (existing) {
return `${existing.schema_name}.${existing.table_name}(${existing.index_columns
.map((c) => `"${c.name}" ${c.order}`)
.join(", ")})`;
}
})
.filter((i) => i !== undefined);
if (out.newIndexes.size > 0) {
const costReductionPct = out.baseCost > 0
? ((out.baseCost - out.finalCost) / out.baseCost) * 100
: 0;
if (Math.round(costReductionPct) <= 0) {
console.log(
`Skipping recommendation with ${costReductionPct.toFixed(1)}% cost reduction (rounds to 0%)`,
);
console.timeEnd(`timing`);
return {
kind: "no_improvement",
fingerprint: queryFingerprint,
rawQuery: query,
formattedQuery,
cost: out.baseCost,
existingIndexes: existingIndexesForQuery,
nudges,
tags,
referencedTables,
explainPlan: out.baseExplainPlan,
};
}
this.queryStats.optimized++;
const newIndexRecommendations = Array.from(out.newIndexes)
.map((n) => out.triedIndexes.get(n))
.filter((n) => n !== undefined);
const newIndexes = newIndexRecommendations.map((n) => n.definition);
console.log(`New indexes: ${newIndexes.join(", ")}`);
return {
kind: "recommendation",
rawQuery: query,
nudges,
tags,
referencedTables,
indexRecommendations: newIndexRecommendations,
recommendation: {
fingerprint: queryFingerprint,
formattedQuery,
baseCost: out.baseCost,
baseExplainPlan: out.baseExplainPlan,
optimizedCost: out.finalCost,
existingIndexes: existingIndexesForQuery,
proposedIndexes: newIndexes,
explainPlan: out.explainPlan,
},
};
} else {
console.log("No new indexes found");
if (
typeof this.maxCost === "number" &&
out.finalCost > this.maxCost
) {
console.log(
"Query cost is too high",
out.finalCost,
this.maxCost,
);
return {
kind: "cost_past_threshold",
rawQuery: query,
nudges,
tags,
referencedTables,
warning: {
fingerprint: queryFingerprint,
formattedQuery,
baseCost: out.baseCost,
optimization: {
newCost: out.finalCost,
existingIndexes: existingIndexesForQuery,
proposedIndexes: [],
},
explainPlan: out.explainPlan,
maxCost: this.maxCost,
},
};
}
return {
kind: "no_improvement",
fingerprint: queryFingerprint,
rawQuery: query,
formattedQuery,
cost: out.baseCost,
existingIndexes: existingIndexesForQuery,
nudges,
tags,
referencedTables,
explainPlan: out.baseExplainPlan,
};
}
} else if (out.kind === "zero_cost_plan") {
console.log("Zero cost plan found");
console.log(out);
console.timeEnd(`timing`);
return {
kind: "zero_cost_plan",
explainPlan: out.explainPlan,
fingerprint: queryFingerprint,
rawQuery: query,
formattedQuery,
nudges,
tags,
referencedTables,
};
}
console.timeEnd(`timing`);
console.error(out);
throw new Error(`Unexpected output: ${out}`);
},
);
}
private async formatQuery(query: string): Promise<string> {
try {
return await prettier.format(query, {
parser: "sql",
plugins: [prettierPluginSql],
language: "postgresql",
keywordCase: "upper",
});
} catch {
return query;
}
}
private printLegend() {
console.log(`--Legend--------------------------`);
console.log(`| ${bgBrightMagenta(" column ")} | Candidate |`);
console.log(`| ${yellow(" column ")} | Ignored |`);
console.log(`| ${blue(" column ")} | Temp table reference |`);
console.log(`-----------------------------------`);
console.log();
}
private static decideStatisticsMode(path: string): StatisticsMode {
const data = readFileSync(path);
const json = JSON.parse(new TextDecoder().decode(data));
return Statistics.statsModeFromExport(ExportedStats.array().parse(json));
}
}
export type QueryProcessResult =
| {
kind: "invalid";
}
| {
kind: "cost_past_threshold";
rawQuery: string;
nudges: Nudge[];
tags: SQLCommenterTag[];
referencedTables: TableReference[];
warning: ReportQueryCostWarning;
}
| {
kind: "recommendation";
rawQuery: string;
nudges: Nudge[];
tags: SQLCommenterTag[];
referencedTables: TableReference[];
indexRecommendations: IndexRecommendation[];
recommendation: ReportIndexRecommendation;
}
| {
kind: "no_improvement";
fingerprint: string;
rawQuery: string;
formattedQuery: string;
cost: number;
existingIndexes: string[];
nudges: Nudge[];
tags: SQLCommenterTag[];
referencedTables: TableReference[];
explainPlan?: object;
} | {
kind: "error";
error: Error;
fingerprint: string;
rawQuery: string;
formattedQuery: string;
nudges: Nudge[];
tags: SQLCommenterTag[];
referencedTables: TableReference[];
}
| {
kind: "zero_cost_plan";
explainPlan: object;
fingerprint: string;
rawQuery: string;
formattedQuery: string;
nudges: Nudge[];
tags: SQLCommenterTag[];
referencedTables: TableReference[];
};