1 | 'use strict'
|
2 |
|
3 | const co = require('co')
|
4 | const cli = require('heroku-cli-util')
|
5 | const pg = require('heroku-pg')
|
6 |
|
7 | function * run (context, heroku) {
|
8 | let db = yield pg.fetcher(heroku).database(context.app, context.args.database)
|
9 |
|
10 | let query = `
|
11 | SELECT
|
12 | pid,
|
13 | now() - pg_stat_activity.query_start AS duration,
|
14 | query AS query
|
15 | FROM
|
16 | pg_stat_activity
|
17 | WHERE
|
18 | pg_stat_activity.query <> ''::text
|
19 | AND state <> 'idle'
|
20 | AND now() - pg_stat_activity.query_start > interval '5 minutes'
|
21 | ORDER 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 |
|
29 | const 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 |
|
38 | module.exports = [
|
39 | Object.assign({command: 'long-running-queries'}, cmd),
|
40 | Object.assign({command: 'long_running_queries', hidden: true}, cmd)
|
41 | ]
|