在Postgres中杀死特定的SQL查询

时间:2019-01-04 05:27:48

标签: python mysql postgresql

要杀死mysql中具有特定字符串的查询,我可以执行以下操作:

kill_string = 'LONG-QUERY'
cursor = self.conn.cursor()
cursor.execute("SHOW FULL processlist")
for _row in cursor.fetchall():
    _id = _row[0]
    _query = _row[-1] or ''
    if kill_string in _query:
        cursor.execute('KILL %s' % _id)

Postgres中的等效内容是什么?

1 个答案:

答案 0 :(得分:0)

非常相似,命令如下:

kill_string = 'LONG-QUERY'

cursor.execute("SELECT pid, query FROM pg_stat_activity")
for _row in cursor.fetchall():
    _id = _row[0]
    _query = _row[-1] or ''
    if kill_string in _query:
        cursor.execute('SELECT pg_terminate_backend(%s)' % _id)