oracle mysql postgresql opengauss 批量kill session
在Oracle, MySQL, PostgreSQL和openGauss数据库中,可以使用SQL命令批量kill session。以下是针对这些数据库的示例代码:
Oracle:
BEGIN
FOR s IN (SELECT sid, serial# FROM v$session WHERE username = 'YOUR_USER')
LOOP
EXECUTE IMMEDIATE 'ALTER SYSTEM KILL SESSION ''' || s.sid || ',' || s.serial# || '''';
END LOOP;
END;
/
MySQL:
SELECT CONCAT('KILL ', id, ';')
INTO @sql
FROM information_schema.processlist
WHERE user='YOUR_USER';
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
PostgreSQL:
SELECT pg_terminate_backend(pid)
FROM pg_stat_activity
WHERE usename = 'YOUR_USER';
openGauss:
DO
$do$
DECLARE
rs record;
BEGIN
FOR rs IN SELECT pid FROM pg_stat_activity WHERE usename = 'YOUR_USER'
LOOP
PERFORM pg_terminate_backend(rs.pid);
END LOOP;
END
$do$;
请将YOUR_USER
替换为实际需要kill session的用户名。注意,在实际操作中,应该谨慎执行这些命令,并确保你有足够的权限来kill其他用户的session。在Oracle中,通常需要DBA权限;在MySQL, PostgreSQL和openGauss中,用户需要有足够的权限来kill session。
评论已关闭