UNPKG

1.38 kBJavaScriptView Raw
1'use strict'
2
3const co = require('co')
4const cli = require('heroku-cli-util')
5const pg = require('heroku-pg')
6const util = require('../lib/util')
7
8function * run (context, heroku) {
9 let db = yield pg.fetcher(heroku).database(context.app, context.args.database)
10
11 yield util.ensurePGStatStatement(db)
12
13 let truncatedQueryString = context.flags.truncate
14 ? 'CASE WHEN length(query) <= 40 THEN query ELSE substr(query, 0, 39) || \'…\' END'
15 : 'query'
16
17 let query = `
18SELECT ${truncatedQueryString} AS qry,
19interval '1 millisecond' * total_time AS exec_time,
20to_char((total_time/sum(total_time) OVER()) * 100, 'FM90D0') || '%' AS prop_exec_time,
21to_char(calls, 'FM999G999G990') AS ncalls,
22interval '1 millisecond' * (blk_read_time + blk_write_time) AS sync_io_time
23FROM pg_stat_statements WHERE userid = (SELECT usesysid FROM pg_user WHERE usename = current_user LIMIT 1)
24ORDER BY calls DESC LIMIT 10
25`
26
27 let output = yield pg.psql.exec(db, query)
28 process.stdout.write(output)
29}
30
31const cmd = {
32 topic: 'pg',
33 description: 'show 10 queries that have longest execution time in aggregate',
34 needsApp: true,
35 needsAuth: true,
36 args: [{name: 'database', optional: true}],
37 flags: [
38 {name: 'truncate', char: 't', description: 'truncate queries to 40 characters'}
39 ],
40 run: cli.command(co.wrap(run))
41}
42
43module.exports = [
44 Object.assign({command: 'calls'}, cmd)
45]