我正在使用psql CLI命令行向Redshift发出查询。 (问题也适用于mysql)。查询可能需要很长时间才能执行。发送命令后,是否可以在运行时获取该查询的查询ID,而不必等到它完成。这可能吗?
例如,如果我需要在几小时/几天后中止该查询,我想向Redshift后端发送一个CANCEL / ABORT命令以获取该特定查询。注意:其他人将使用相同的数据库,因此我想知道我执行的特定查询的ID。
另外,假设psql命令在shell中运行。是否可以在应用程序中以编程方式(python)从该psql CLI进程获取查询ID?
答案 0 :(得分:1)
在mysql中,您可以运行select connection_id()
以在运行查询之前获取当前连接的连接ID。然后,您可以在启动长时间运行的查询后从另一个连接运行kill <id>
。
答案 1 :(得分:0)
对于Redshift,它是select * from stv_recents where status='Running'
,你可以从那里获得pid
答案 2 :(得分:0)
在Amazon Redshift中,有几种方法可以确定正在运行的查询 -
STV_INFLIGHT - 仅显示当前正在运行的查询(http://docs.aws.amazon.com/redshift/latest/dg/r_STV_INFLIGHT.html)
select userid, usename, query, pid, starttime, "text",
suspended from stv_inflight s, pg_user u
where s.userid = u.usesysid
STV_RECENTS - 显示当前正在运行且最近完成的状态为http://docs.aws.amazon.com/redshift/latest/dg/r_STV_RECENTS.html)
的查询select * from stv_recents;
现在,要中止正在运行的查询,您需要当前正在运行的查询的 pid (进程ID)。然后,您可以使用以下命令之一终止查询 -
select pg_cancel_backend(<pid>); OR
select pg_terminate_backend(<pid>) ; OR
cancel <pid>;
查询#2
select userid, usename, query, s.pid, c.remotehost, c.application_name,
starttime, "text",
suspended from stv_inflight s, stl_connection_log c, pg_user u
where s.userid = u.usesysid and s.pid = c.pid