Oracle的UNDO是很多功能的核心基础,如果了解UNDO的原理,像一致性读、事务回滚、实例恢复等特性,就会更容易理解。徐老师写的这篇文章《实用小技巧:UNDO 100%占用不释放解决办法》,给我们介绍了UNDO空间占满不释放的一个场景,以及相应的解决,这是我们日常工作中可能碰到的,解决步骤可以直接借鉴参考。

1、问题现像

这个问题的起因是客户的存储没空间了,目前RAC1节点UNDOTBS1占用200多G,想要将UNDO释放一下。

undo_retention设置的是900,按说不应该增长到很大才对,以前确实没有注意,只是加过几次undo数据文件。

数据库的版本为12CR2,操作系统为solaris 11。

Oracle UNDO表空间占满的解决场景_SQL

查看发现有大量unexpired,

Oracle UNDO表空间占满的解决场景_SQL_02

Oracle Undo段中区3种状态(DBA_UNDO_EXTENTS的STATUS列):ACTIVE、EXPIRED和UNEXPIRED。

ACTIVE即未提交的Undo信息(活动):表示事物还在活动,该值对应的Undo段的DBA_ROLLBACK_SEGS.STATUS一定是ONLINE状态,一旦没有活动的事务在使用Undo段,那么对应的Undo段就变成OFFLINE状态。ACTIVE状态的Undo区不会被覆盖。

EXPIRED即过期的Undo信息(过期):表示事务已经提交且超过了UNDO_RETENTION指定时间,该状态可以被覆盖使用。

UNEXPIRED即提交的Undo信息(未过期):表示事务已经提交但是还没有超过UNDO_RETENTION指定时间,该状态可以被覆盖使用。

2、解决UNDO过大的问题

2.1、修改隐藏参数关闭autotune

查看隐藏参数_undo_autotune默认状态为true,

SELECT ksppinm, ksppstvl, ksppdesc 
FROM x$ksppi x, x$ksppcv y 
WHERE x.indx = y.indx AND ksppinm = '_undo_autotune';

Oracle UNDO表空间占满的解决场景_MySQL_03

修改隐藏参数来限制undo这里有2个方法

方法1,限制undo retention最大时间,就是在自动调整的结果上强制限制最大时间(试了效果不大)。

alter system set "_highthreshold_undoretention"=50000 scope=spfile;

方法2,直接关闭autotune(这个更干脆)

关于autotune可以查看官方文档Automatic Tuning of Undo Retention Common Issues (Doc ID 1579779.1)

alter system set "_undo_autotune"=false;

Oracle UNDO表空间占满的解决场景_MySQL_04

创建PDB参数文件,

Oracle UNDO表空间占满的解决场景_MySQL_05

改完效果,

Oracle UNDO表空间占满的解决场景_MySQL_06

Oracle UNDO表空间占满的解决场景_SQL_07

再检查undo情况,

Oracle UNDO表空间占满的解决场景_MySQL_08

隔了一天再检查,

Oracle UNDO表空间占满的解决场景_oracle_09

再检查状态基本都为expired状态,

select status, count(*) Num_Extents, sum(blocks) Num_Blocks, round((sum(bytes)/1024/1024),2) MB 
from dba_undo_extents 
group by status order by status;

Oracle UNDO表空间占满的解决场景_SQL_10

2.2、切换UNDO方法

数据库的版本是12.2。默认使用LOCAL UNDO。

先切换到PDB,

SQL> alter session set container=pdb;
Session altered.

动之前再确认是否开启本地UNDO,

SQL> select property_name, property_value 
from database_properties 
where property_name='LOCAL_UNDO_ENABLED';
PROPERTY_NAME PROPERTY_VALUE
----------------------- ---------------
LOCAL_UNDO_ENABLED TRUE

创建undo_01表空间,

CREATE UNDO TABLESPACE UNDO_1 datafile '+DATA' size 10G autoextend on;

PDB修改下原来11G基本一样,也要指定下SID(这里不是写数据库的实例名)

alter system set undo_tablespace=UNDO_01 sid='orcl1' scope=both;

然后使用如下SQL查看,回滚段是否己经切换到undo_01,

select dr.tablespace_name, dr.segment_name, vr.status 
from dba_rollback_segs dr, v$rollstat vr 
where dr.segment_id=vr.usn;

Oracle UNDO表空间占满的解决场景_数据库_11

确实无误后,执行删除原有UNDOTBS1,

drop tablespace UNDOTBS1 including contents and datafiles;

再查看,

SELECT * FROM DBA_DATA_FILES;

3、关于为啥node2上的undo表空间不叫undotbs2而叫undo_2?

第2个节点的名称很奇怪,原来11g的时候都是undotbs1和undotbs2。

在PDB里显示为1节点是undotbs1,2节点是undo_2。这个在12CR2的版本里是正常现像,RAC环境创建PDB的第2个节点就是默认叫这个名,可以参照官方文档说明。

With Local Undo Enabled and RAC Environment,The UNDO Tablespace of a PDB is UNDO_2 (Doc ID 2971554.1)

如果觉得别扭,可以手动创建undotbs2进行切换,具体方法参照官方文档,

If you need to change the UNDO tablespace of the PDB on node 2 from UNDO_2, do the following in the target PDB on node2:


Check the current UNDO tablespace. -- Connect to and execute the PDB on the target node.
alter session set container=;


show con_name


-- Check the current UNDO tablespace.


show parameter undo_tablespace


If there is no UNDO tablespace to change to, create a new UNDO tablespace.
CREATE UNDO TABLESPACE SIZE AUTOEXTEND ON ;


example:


CREATE UNDO TABLESPACE UNDOTBS2 datafile SIZE 10M AUTOEXTEND ON ;


Verify that the undo tablespace that you want to change has been created.


SELECT * FROM DBA_DATA_FILES;


Change the UNDO tablespace.
alter system set undo_tablespace= container=current  sid='' scope=spfile ;example:


alter system set undo_tablespace=UNDOTBS2 container=current  sid='orcl2' scope=spfile ;


Restart the PDB and confirm that you have switched to the new tablespace.
Restart the PDB:shutdown


startupPlease confirm that you have switched to the new tablespace.


See if the segment for the old undo tablespace (UNDO_2) disappears.


select dr.tablespace_name, dr.segment_name, vr.status from dba_rollback_segs dr, v$rollstat vr where dr.segment_id=vr.usn;


Drop the old UNDO tablespace.
drop tablespace UNDO_2 including contents and datafiles;


Verify that the original undo tablespace has been dropped.


SELECT * FROM   DBA_DATA_FILES;

4、参考资料

Document 1578639.1 (oracle.com)

如果您认为这篇文章有些帮助,还请不吝点下文章末尾的"点赞"