UNPKG

982 BJavaScriptView 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 c.relname AS name,
12 pg_size_pretty(sum(c.relpages::bigint*8192)::bigint) AS size
13FROM pg_class c
14LEFT JOIN pg_namespace n ON (n.oid = c.relnamespace)
15WHERE n.nspname NOT IN ('pg_catalog', 'information_schema')
16AND n.nspname !~ '^pg_toast'
17AND c.relkind='i'
18GROUP BY c.relname
19ORDER BY sum(c.relpages) DESC;
20 `
21
22 let output = yield pg.psql.exec(db, query)
23 process.stdout.write(output)
24}
25
26const cmd = {
27 topic: 'pg',
28 description: 'show the size of indexes, descending by size',
29 needsApp: true,
30 needsAuth: true,
31 args: [{name: 'database', optional: true}],
32 run: cli.command(co.wrap(run))
33}
34
35module.exports = [
36 Object.assign({command: 'index-size'}, cmd),
37 Object.assign({command: 'index_size', hidden: true}, cmd)
38]