UNPKG

2.35 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 = `
11WITH table_opts AS (
12 SELECT
13 pg_class.oid, relname, nspname, array_to_string(reloptions, '') AS relopts
14 FROM
15 pg_class INNER JOIN pg_namespace ns ON relnamespace = ns.oid
16), vacuum_settings AS (
17 SELECT
18 oid, relname, nspname,
19 CASE
20 WHEN relopts LIKE '%autovacuum_vacuum_threshold%'
21 THEN regexp_replace(relopts, '.*autovacuum_vacuum_threshold=([0-9.]+).*', E'\\\\\\1')::integer
22 ELSE current_setting('autovacuum_vacuum_threshold')::integer
23 END AS autovacuum_vacuum_threshold,
24 CASE
25 WHEN relopts LIKE '%autovacuum_vacuum_scale_factor%'
26 THEN regexp_replace(relopts, '.*autovacuum_vacuum_scale_factor=([0-9.]+).*', E'\\\\\\1')::real
27 ELSE current_setting('autovacuum_vacuum_scale_factor')::real
28 END AS autovacuum_vacuum_scale_factor
29 FROM
30 table_opts
31)
32SELECT
33 vacuum_settings.nspname AS schema,
34 vacuum_settings.relname AS table,
35 to_char(psut.last_vacuum, 'YYYY-MM-DD HH24:MI') AS last_vacuum,
36 to_char(psut.last_autovacuum, 'YYYY-MM-DD HH24:MI') AS last_autovacuum,
37 to_char(pg_class.reltuples, '9G999G999G999') AS rowcount,
38 to_char(psut.n_dead_tup, '9G999G999G999') AS dead_rowcount,
39 to_char(autovacuum_vacuum_threshold
40 + (autovacuum_vacuum_scale_factor::numeric * pg_class.reltuples), '9G999G999G999') AS autovacuum_threshold,
41 CASE
42 WHEN autovacuum_vacuum_threshold + (autovacuum_vacuum_scale_factor::numeric * pg_class.reltuples) < psut.n_dead_tup
43 THEN 'yes'
44 END AS expect_autovacuum
45FROM
46 pg_stat_user_tables psut INNER JOIN pg_class ON psut.relid = pg_class.oid
47 INNER JOIN vacuum_settings ON pg_class.oid = vacuum_settings.oid
48ORDER BY 1
49`
50
51 let output = yield pg.psql.exec(db, query)
52 process.stdout.write(output)
53}
54
55const cmd = {
56 topic: 'pg',
57 description: 'show dead rows and whether an automatic vacuum is expected to be triggered',
58 needsApp: true,
59 needsAuth: true,
60 args: [{name: 'database', optional: true}],
61 run: cli.command(co.wrap(run))
62}
63
64module.exports = [
65 Object.assign({command: 'vacuum-stats'}, cmd),
66 Object.assign({command: 'vacuum_stats', hidden: true}, cmd)
67]