UNPKG

1.13 kBJavaScriptView Raw
1'use strict'
2
3const cli = require('heroku-cli-util')
4
5const query = `
6SELECT 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
12FROM pg_catalog.pg_locks bl
13JOIN pg_catalog.pg_stat_activity a
14 ON bl.pid = a.pid
15JOIN pg_catalog.pg_locks kl
16 JOIN pg_catalog.pg_stat_activity ka
17 ON kl.pid = ka.pid
18ON bl.transactionid = kl.transactionid AND bl.pid != kl.pid
19WHERE NOT bl.granted
20`
21
22async 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
31const 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
40module.exports = [
41 Object.assign({ command: 'blocking' }, cmd)
42]