云服务器内容精选

  • 操作步骤 参考连接数据库,连接数据库。 查看阻塞的查询语句及阻塞查询的表、模式信息。 1 2 3 4 5 6 7 8 9 10 11 SELECT w.query as waiting_query, w.pid as w_pid, w.usename as w_user, l.query as locking_query, l.pid as l_pid, l.usename as l_user, t.schemaname || '.' || t.relname as tablename from pg_stat_activity w join pg_locks l1 on w.pid = l1.pid and not l1.granted join pg_locks l2 on l1.relation = l2.relation and l2.granted join pg_stat_activity l on l2.pid = l.pid join pg_stat_user_tables t on l1.relation = t.relid where w.waiting; 该查询返回线程ID、用户信息、查询状态,以及导致阻塞的表、模式信息。 使用如下命令结束相应的会话。其中,139834762094352为线程ID。 1 SELECT PG_TERMINATE_BACKEND(139834762094352); 显示类似如下信息,表示结束会话成功。 PG_TERMINATE_BACKEND ---------------------- t (1 row) 显示类似如下信息,表示用户正在尝试结束当前会话,此时仅会重连会话,而不是结束会话。 FATAL: terminating connection due to administrator command FATAL: terminating connection due to administrator command The connection to the server was lost. Attempting reset: Succeeded. gsql客户端使用PG_TERMINATE_BACKEND函数终止本会话后台线程时,客户端不会退出而是自动重连。
  • 操作步骤 参考连接数据库,连接数据库。 查看阻塞的查询语句及阻塞查询的语句及其查询状态、会话ID(如下SQL在线程池开启的模式下适用)。 SELECT w.query AS waiting_query, w.state as w_state, w.datname as w_datname, w.usename as w_user, w.client_addr as w_client_addr, l.query AS locking_query, l.state as l_state, l.pid AS l_pid, l.sessionid as l_sessionid FROM pgxc_stat_activity AS w JOIN ( SELECT query_id,block_sessionid,global_sessionid,node_name FROM pgxc_thread_wait_status ) AS a ON a.query_id = w.query_id JOIN ( SELECT sessionid,node_name,global_sessionid FROM pgxc_thread_wait_status ) AS b ON b.sessionid = a.block_sessionid and b.node_name=a.node_name JOIN (select query,pid,sessionid,global_sessionid,state from pgxc_stat_activity ) l on substring(l.global_sessionid,0,instr(l.global_sessionid,'#')) ilike substring(b.global_sessionid,0,instr(b.global_sessionid,'#')) WHERE a.block_sessionid !=0; 该查询返回会话ID、用户信息、查询状态,以及导致阻塞的语句及其查询状态、会话ID。 使用如下命令结束相应的会话。其中,139834762094352为线程ID。 1 SELECT PG_TERMINATE_BACKEND(139834762094352); 显示类似如下信息,表示结束会话成功。 PG_TERMINATE_BACKEND ---------------------- t (1 row) 显示类似如下信息,表示用户正在尝试结束当前会话。 FATAL: terminating connection due to administrator command FATAL: terminating connection due to administrator command 1. gsql客户端使用PG_TERMINATE_BACKEND函数结束当前正在执行会话的后台线程时,如果当前的用户是初始用户,客户端不会退出而是自动重连,即还会返回“The connection to the server was lost. Attempting reset: Succeeded.”;否则客户端会重连失败,即返回“The connection to the server was lost. Attempting reset: Failed.”。这是因为只有初始用户可以免密登录,普遍用户不能免密登录,从而重连失败。 2. 对于使用PG_TERMINATE_BACKEND函数结束非活跃的后台线程时。如果打开了线程池,此时空闲的会话没有线程ID,无法结束会话。非线程池模式下,结束的会话不会自动重连。