1 | 'use strict'
|
2 |
|
3 | const cli = require('heroku-cli-util')
|
4 |
|
5 | const query = `
|
6 | SELECT bl.pid AS blocked_pid,
|
7 | ka.query AS blocking_statement,
|
8 | now() - ka.query_start AS blocking_duration,
|
9 | kl.pid AS blocking_pid,
|
10 | a.query AS blocked_statement,
|
11 | now() - a.query_start AS blocked_duration
|
12 | FROM pg_catalog.pg_locks bl
|
13 | JOIN pg_catalog.pg_stat_activity a
|
14 | ON bl.pid = a.pid
|
15 | JOIN pg_catalog.pg_locks kl
|
16 | JOIN pg_catalog.pg_stat_activity ka
|
17 | ON kl.pid = ka.pid
|
18 | ON bl.transactionid = kl.transactionid AND bl.pid != kl.pid
|
19 | WHERE NOT bl.granted
|
20 | `
|
21 |
|
22 | async function run(context, heroku) {
|
23 | const fetcher = require('../lib/fetcher')(heroku)
|
24 | const psql = require('../lib/psql')
|
25 |
|
26 | let db = await fetcher.database(context.app, context.args.database)
|
27 | let output = await psql.exec(db, query)
|
28 | process.stdout.write(output)
|
29 | }
|
30 |
|
31 | const cmd = {
|
32 | topic: 'pg',
|
33 | description: 'display queries holding locks other queries are waiting to be released',
|
34 | needsApp: true,
|
35 | needsAuth: true,
|
36 | args: [{ name: 'database', optional: true }],
|
37 | run: cli.command({ preauth: true }, run)
|
38 | }
|
39 |
|
40 | module.exports = [
|
41 | Object.assign({ command: 'blocking' }, cmd)
|
42 | ]
|