UNPKG

1 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 c.relname AS name,
12 pg_size_pretty(pg_total_relation_size(c.oid)) 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='r'
18ORDER BY pg_total_relation_size(c.oid) DESC;
19`
20
21 let output = yield pg.psql.exec(db, query)
22 process.stdout.write(output)
23}
24
25const cmd = {
26 topic: 'pg',
27 description: 'show the size of the tables (including indexes), descending by size',
28 needsApp: true,
29 needsAuth: true,
30 args: [{name: 'database', optional: true}],
31 run: cli.command(co.wrap(run))
32}
33
34module.exports = [
35 Object.assign({command: 'total-table-size'}, cmd),
36 Object.assign({command: 'total_table_size', hidden: true}, cmd)
37]