UNPKG

1.34 kBJavaScriptView Raw
1'use strict'
2
3const co = require('co')
4const cli = require('heroku-cli-util')
5const pg = require('heroku-pg')
6
7function * run (context, heroku) {
8 let db = yield pg.fetcher(heroku).database(context.app, context.args.database)
9
10 let query = `
11SELECT
12 schemaname || '.' || relname AS table,
13 indexrelname AS index,
14 pg_size_pretty(pg_relation_size(i.indexrelid)) AS index_size,
15 idx_scan as index_scans
16FROM pg_stat_user_indexes ui
17JOIN pg_index i ON ui.indexrelid = i.indexrelid
18WHERE NOT indisunique AND idx_scan < 50 AND pg_relation_size(relid) > 5 * 8192
19ORDER BY pg_relation_size(i.indexrelid) / nullif(idx_scan, 0) DESC NULLS FIRST,
20pg_relation_size(i.indexrelid) DESC;
21`
22
23 let output = yield pg.psql.exec(db, query)
24 process.stdout.write(output)
25}
26
27const cmd = {
28 topic: 'pg',
29 description: 'show unused and almost unused indexes',
30 help: `
31Ordered by their size relative to the number of index scans.
32Exclude indexes of very small tables (less than 5 pages),
33where the planner will almost invariably select a sequential scan,
34but may not in the future as the table grows`,
35 needsApp: true,
36 needsAuth: true,
37 args: [{name: 'database', optional: true}],
38 run: cli.command(co.wrap(run))
39}
40
41module.exports = [
42 Object.assign({command: 'unused-indexes'}, cmd),
43 Object.assign({command: 'unused_indexes', hidden: true}, cmd)
44]