
# 长事务阻塞Undo空间回收
#### 问题现象
1. pg_log中打印如下错误：
   ```
   snapshot too old! the undo record has been forcibly discarded
   ```
   ```
   xid xxx, the undo size xxx of the transaction exceeds the threshold xxx. trans_undo_threshold_size xxx,undo_space_limit_size xxx.
   ```
   在真实报错信息中，上文中的xxx为实际数据。
   
2. global_recycle_xid（Undo子系统的全局回收事务XID）长时间不发生变化。 ![](https://support.huaweicloud.com/fg-gaussdb-cent/figure/zh-cn_image_0000002257960689.png "点击放大")
   
3. pg_running_xacts与pg_stat_activity视图查询存在长事务，阻塞oldestxmin和global_recycle_xid推进。如果pg_running_xacts中查询活跃事务的xmin和gs_txid_oldestxmin相等，且通过pid查询pg_stat_activity查询线程执行语句时间过长，则表明有长事务卡住了回收。
   ```
   select * from pg_running_xacts where xmin::text::bigint<>0 and vacuum <> 't' order by xmin::text::bigint asc limit 5;
   select * from gs_txid_oldestxmin();
   select * from pg_stat_activity where pid = 长事务所在线程PID;
   ```
   ![](https://support.huaweicloud.com/fg-gaussdb-cent/figure/zh-cn_image_0000002223080902.png "点击放大")
   
 
#### 处理方法
通过pg_terminate_session(pid, sessionid)终止长事务所在的会话（提醒：长事务无固定快速恢复手段，强制结束SQL语句为其中一种常用操作，属于高危操作，执行需谨慎，执行前需与业务及华为技术确认，避免造成业务失败或报错）。
