UNPKG

997 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
12 pid,
13 now() - pg_stat_activity.query_start AS duration,
14 query AS query
15FROM
16 pg_stat_activity
17WHERE
18 pg_stat_activity.query <> ''::text
19 AND state <> 'idle'
20 AND now() - pg_stat_activity.query_start > interval '5 minutes'
21ORDER BY
22 now() - pg_stat_activity.query_start DESC;
23`
24
25 let output = yield pg.psql.exec(db, query)
26 process.stdout.write(output)
27}
28
29const cmd = {
30 topic: 'pg',
31 description: 'show all queries longer than five minutes by descending duration',
32 needsApp: true,
33 needsAuth: true,
34 args: [{name: 'database', optional: true}],
35 run: cli.command(co.wrap(run))
36}
37
38module.exports = [
39 Object.assign({command: 'long-running-queries'}, cmd),
40 Object.assign({command: 'long_running_queries', hidden: true}, cmd)
41]