Add cancel option to terminate queries

This commit is contained in:
Julien Riou 2018-06-30 11:11:24 +02:00
parent 750db87046
commit dcb07c225e
No known key found for this signature in database
GPG key ID: BA3E15176E45E85D
5 changed files with 22 additions and 1 deletions

View file

@ -38,6 +38,7 @@ type Config struct {
ExcludeUsers StringFlags `yaml:"exclude-users"`
ExcludeUsersRegex string `yaml:"exclude-users-regex"`
ExcludeUsersRegexCompiled *regexp.Regexp
Cancel bool `yaml:"cancel"`
}
func init() {

View file

@ -84,3 +84,17 @@ func (db *Db) TerminateSessions(sessions []Session) {
Panic(err)
}
}
// CancelSessions terminates current query of a list of sessions
func (db *Db) CancelSessions(sessions []Session) {
var pids []int64
for _, session := range sessions {
pids = append(pids, session.Pid)
}
if len(pids) > 0 {
query := `select pg_cancel_backend(pid) from pg_stat_activity where pid = any($1);`
log.Debugf("query: %s\n", query)
_, err := db.conn.Exec(query, pq.Array(pids))
Panic(err)
}
}