size of the Undo tablespace metalink 提供的 crontab

SOLUTION

  1. To check the current size of the Undo tablespace:

select sum(a.bytes) as undo_size from vdatafile a, vtablespace b, dba_tablespaces c where c.contents = 'UNDO' and c.status = 'ONLINE' and b.name = c.tablespace_name and a.ts# = b.ts#;

  1. To check the free space (unallocated) space within Undo tablespace:

select sum(bytes)/1024/1024 "mb" from dba_free_space where tablespace_name ='<undo tablespace name>';

3.To Check the space available within the allocated Undo tablespace:

select tablespace_name , sum(blocks)*8/(1024) reusable_space from dba_undo_extents where status='EXPIRED' group by tablespace_name;

  1. To Check the space allocated in the Undo tablespace:

select tablespace_name, sum(blocks) * 8 / (1024) space_in_use

from dba_undo_extents

where status IN ('ACTIVE', 'UNEXPIRED')

group by tablespace_name;

Alternatively, below one SQL can be used as well:

with free_sz as

(select tablespace_name, sum(f.bytes) / 1048576 / 1024 free_gb

from dba_free_space f

group by tablespace_name),

a as

(select tablespace_name,

sum(case

when status = 'EXPIRED' then

blocks

end) * 8 / 1048576 reusable_space_gb,

sum(case

when status in ('ACTIVE', 'UNEXPIRED') then

blocks

end) * 8 / 1048576 allocated_gb

from dba_undo_extents

where status in ('ACTIVE', 'EXPIRED', 'UNEXPIRED')

group by tablespace_name),

undo_sz as

(select tablespace_name, df.user_bytes / 1048576 / 1024 user_sz_gb

from dba_tablespaces ts

join dba_data_files df

using (tablespace_name)

where ts.contents = 'UNDO'

and ts.status = 'ONLINE')

select tablespace_name,

user_sz_gb,

free_gb,

reusable_space_gb,

allocated_gb,

free_gb + reusable_space_gb + allocated_gb total

from undo_sz

join free_sz

using (tablespace_name)

join a

using (tablespace_name);


GOAL

Sometimes customer need to check the undo tablespace usage history, by using crontab and Linux shell script, we can give customer flexible ability to check undo tablespace usage information, or other dictionary view information. I am giving an example to show the method. This has been tested in Oracle Enterprise Linux 6.5 and bash shell. If using other kind of OS or shell, the script might need to be changed accordingly.

Disclaimer:

NOTE: In the images and/or the document content below, the user information and environment data used represents fictitious data from the Oracle sample schema(s), Public Documentation delivered with an Oracle database product or other training material. Any similarity to actual environments, actual persons, living or dead, is purely coincidental and not intended in any manner.

SOLUTION

Step 1. Edit shell script to set oracle database related environment variables.

$ vi <PATH_To_Your_Script_Directory>/SetEnv.sh

$ cat <PATH_To_Your_Script_Directory>/SetEnv.sh

#!/bin/bash

export ORACLE_HOME=<PATH_To_DB_HOME>

export ORACLE_BASE=<PATH_To_ORACLE_BASE>

export ORACLE_SID=<oracle sid>

export PATH=PATH:ORACLE_HOME/bin

$

Grant running permission:

$ chmod u+x <PATH_To_Your_Script_Directory>/SetEnv.sh

Step 2. Edit shell script to run sql.

$ vi <PATH_To_Your_Script_Directory>/CheckUndo.sh

In this shell script, we run the ". <PATH_To_Your_Script_Directory>/SetEnv.sh" to set environment variable.

$ cat <PATH_To_Your_Script_Directory>/CheckUndo.sh

#!/bin/bash

. <PATH_To_Your_Script_Directory>/SetEnv.sh

sqlplus -s sys/oracle 'as sysdba' <<EOF

spool <PATH_To_Your_Script_Directory>/CheckUndo.txt APPEND

set linesize 200

col tablespace_name for a20

select to_char(sysdate,'yyyymmdd hh24:mi:ss') date_time from dual;

-- <Your sql statement for undo tablespace usage querying>

-- OR

with free_sz as ( select tablespace_name, sum(f.bytes)/1048576/1024 free_gb from dba_free_space f group by tablespace_name ) , a as ( select tablespace_name , sum(case when status = 'EXPIRED' then blocks end)*8/1048576 reusable_space_gb , sum(case when status in ('ACTIVE', 'UNEXPIRED') then blocks end)*8/1048576 allocated_gb from dba_undo_extents where status in ('ACTIVE', 'EXPIRED', 'UNEXPIRED') group by tablespace_name ) , undo_sz as ( select tablespace_name, df.user_bytes/1048576/1024 user_sz_gb from dba_tablespaces ts join dba_data_files df using (tablespace_name) where ts.contents = 'UNDO' and ts.status = 'ONLINE' ) select tablespace_name, user_sz_gb, free_gb, reusable_space_gb, allocated_gb , free_gb + reusable_space_gb + allocated_gb total from undo_sz join free_sz using (tablespace_name) join a using (tablespace_name);

EOF

$

Grant running permission:

$ chmod u+x <PATH_To_Your_Script_Directory>/CheckUndo.sh

Step 3. Running the shell script manually to check to effect.

$ <PATH_To_Your_Script_Directory>/CheckUndo.sh

DATE_TIME


20240204 02:42:17

TABLESPACE_NAME USER_SZ_GB FREE_GB REUSABLE_SPACE_GB ALLOCATED_GB TOTAL


UNDOTBS1 .331054688 .314208984 .007080078 .009765625 .331054688

$

The result example looks like the following:

$ cat <PATH_To_Your_Script_Directory>/CheckUndo.txt

DATE_TIME


20240204 02:43:01

TABLESPACE_NAME USER_SZ_GB FREE_GB REUSABLE_SPACE_GB ALLOCATED_GB TOTAL


UNDOTBS1 .331054688 .314208984 .007080078 .009765625 .331054688

$

Step4. Make crontab setting, and check the effect.

Run as oracle user, to edit crontab setting:

For example, if we want to run the above shell script every 1 minute:

$ crontab -e

At the editing window, add the following line

* * * * * <PATH_To_Your_Script_Directory>/CheckUndo.sh

After saving it (similar to using vi), check the contents by:

$ crontab -l

* * * * * <PATH_To_Your_Script_Directory>/CheckUndo.sh

$

Step 5. Check the automatic running effect.

$ rm -f <PATH_To_Your_Script_Directory>/CheckUndo.txt

Wait for a few minutes, and then check:

$ cat <PATH_To_Your_Script_Directory>/CheckUndo.txt

DATE_TIME


20240204 02:44:01

TABLESPACE_NAME USER_SZ_GB FREE_GB REUSABLE_SPACE_GB ALLOCATED_GB TOTAL


UNDOTBS1 .331054688 .314208984 .007080078 .009765625 .331054688

DATE_TIME


20240204 02:45:02

TABLESPACE_NAME USER_SZ_GB FREE_GB REUSABLE_SPACE_GB ALLOCATED_GB TOTAL


UNDOTBS1 .331054688 .314208984 .007080078 .009765625 .331054688

$

We can see that the undo tablespace usage is recorded every 1 minute.

If we want that script to run every 10 minutes, we can change the content to:

10 * * * * <PATH_To_Your_Script_Directory>/CheckUndo.sh

If we cannot edit crontab as oracle user, we can edit via root user, and change the content to:

10 * * * * su - oracle -c <PATH_To_Your_Script_Directory>/CheckUndo.sh

The crontab setting varies depends on OS type and version, please confirm detail with OS vendor.

Please notice:

In this example the size of <PATH_To_Your_Script_Directory>/CheckUndo.txt grows along the time, so please check the size periodically, if it is too big, please move it to other location, and delete the original contents(eg: truncate --size 0 <PATH_To_Your_Script_Directory>/CheckUndo.txt).

相关推荐
Mephisto.java3 分钟前
【大数据学习 | HBASE高级】hbase-phoenix 与二次索引应用
大数据·sql·oracle·sqlite·json·hbase
Mephisto.java22 分钟前
【大数据学习 | flume】flume之常见的source组件
大数据·sql·oracle·json·database·flume
m0_6371469322 分钟前
如何在FastAPI中灵活使用路径参数、查询参数和请求体参数进行接口设计
数据库·oracle·fastapi
TPCloud26 分钟前
Linux服务器下oracle自动rman备份的实现
oracle·linux定时任务·rman备份
夜光小兔纸1 小时前
Oracle 单机及 RAC 环境 db_files 参数修改
数据库·oracle
瑕、疵3 小时前
使用机器学习优化数据库查询性能
数据库·机器学习·oracle
独泪了无痕3 小时前
【DM系列】详解 DM 字符串大小写敏感
数据库·oracle
莳花微语4 小时前
第四章 :YashanDB 数据库安全性(基础篇)
数据库·oracle
黄名富7 小时前
SQL 处理数列
数据库·sql·mysql·oracle
engchina17 小时前
Oracle ADB 导入 BANK_GRAPH 的学习数据
数据库·学习·oracle·graph