INFO 3611
Lecture 7: Managing Undo Data
•
Understanding undo data and transactions
•
Configuring and monitoring undo tablespaces
•
PL/SQL scripts for managing undo data
•
Resolving common undo-related issues
Lecture 7
A. Managing Undo Data: Understanding Undo Data and Transactions
In Oracle Database, the undo tablespace and undo data play a critical role in supporting transactions
and providing a mechanism for rolling back changes. Undo data ensures transaction consistency,
isolation, and the ability to recover from errors. Here are detailed notes on understanding undo data
and transactions:
1. What is Undo Data:
•
Undo Segments:
•
•
Rollback Segments (deprecated):
•
•
Undo data is stored in structures called undo segments, which are part of the undo
tablespace.
In older versions of Oracle Database, rollback segments were used, but they have
been largely replaced by automatic undo management.
Purpose:
•
Undo data maintains a record of changes made by transactions to the database. It
facilitates transaction rollback, read consistency, and recovery.
2. Undo Tablespaces:
•
Automatic Undo Management:
•
•
Undo Tablespace Size:
•
•
Oracle Database introduced automatic undo management, where the system
manages undo tablespaces and undo retention.
The size of the undo tablespace is crucial and depends on the workload, concurrent
transactions, and the duration of long-running transactions.
Retention Period:
•
Undo retention is the time the system retains undo data before overwriting it. It is
set based on the expected duration of long-running queries.
3. Transactions and Undo:
INFO 3611
•
Transaction Lifecycle:
•
•
Before Images:
•
•
Undo data stores the before-image of modified data, allowing for the reversal of
changes during a transaction rollback.
Read Consistency:
•
•
A transaction begins with a DML (Data Manipulation Language) statement like
INSERT, UPDATE, or DELETE.
Undo data supports read consistency by providing a consistent view of the data for
queries, even if other transactions are modifying the data concurrently.
Commit and Release:
•
Upon commit, undo data related to the committed transaction is no longer needed
for rollback and can be marked as free space.
4. Monitoring Undo:
•
V$UNDOSTAT View:
•
•
The V$UNDOSTAT view provides statistics about undo usage, such as undo space
consumption, transactions, and undo generation rates.
Undo Advisor:
•
Oracle Enterprise Manager (OEM) and Oracle SQL Developer offer tools to monitor
and tune undo tablespaces.
5. Undo Retention and Flashback Queries:
•
Flashback Queries:
•
•
Undo data enables flashback queries, allowing users to view data as it existed at a
previous point in time.
Setting Undo Retention:
•
Undo retention is set to ensure that sufficient undo data is retained for flashback
queries and long-running transactions.
6. Automatic Undo Retention Tuning:
•
Adaptive Undo Management:
•
•
Oracle Database can automatically adjust undo retention based on workload
patterns.
Automatic Tuning:
•
The database dynamically manages undo retention to balance between transaction
recovery and flashback requirements.
7. Managing Undo Space:
INFO 3611
•
Monitoring Space Usage:
•
•
Undo Tablespace Size Adjustment:
•
•
Regularly monitor undo space usage using tools like DBA_UNDO_EXTENTS and
DBA_UNDO_USAGE.
Adjust the size of the undo tablespace based on usage patterns and growth
projections.
Tablespace Shrink:
•
Periodically, consider shrinking the undo tablespace if it experiences substantial free
space.
8. Best Practices:
•
Regular Monitoring:
•
•
Appropriate Sizing:
•
•
Plan and set undo retention carefully to meet the needs of both long-running
transactions and flashback queries.
Performance Considerations:
•
•
Size the undo tablespace appropriately based on the workload and expected
duration of long-running transactions.
Undo Retention Planning:
•
•
Continuously monitor undo space usage, especially during peak activity periods.
Optimize undo management for performance, considering factors like disk I/O, undo
retention, and transaction rates.
Regular Maintenance:
•
Schedule regular maintenance tasks, such as shrinking the undo tablespace and
monitoring for any undo-related issues.
9. Troubleshooting Undo Issues:
•
Diagnostic Tools:
•
•
Use diagnostic tools like Oracle Trace files and alert logs to identify and troubleshoot
undo-related issues.
Undo Advisor Recommendations:
•
Analyze recommendations from the undo advisor to address any undo-related
challenges.
Understanding undo data and transactions is fundamental to maintaining the integrity and
consistency of the Oracle Database. Proper management of undo tablespaces, sizing considerations,
and monitoring undo space usage contribute to efficient transaction processing and support for
features like flashback queries. Regular maintenance and troubleshooting practices ensure the
stability and optimal performance of the database.
INFO 3611
B. Managing Undo Data: Configuring and Monitoring Undo Tablespaces
Configuring and monitoring undo tablespaces is essential for maintaining the stability and
performance of Oracle Database. Undo tablespaces play a crucial role in managing transactions,
providing read consistency, and supporting flashback queries. Here are detailed notes on configuring
and monitoring undo tablespaces:
1. Configuring Undo Tablespaces:
•
Automatic Undo Management:
•
•
Initialization Parameters:
•
•
Oracle Database recommends using automatic undo management, which simplifies
undo tablespace management.
Key initialization parameters related to undo tablespaces:
•
UNDO_MANAGEMENT: Set to 'AUTO' for automatic undo management.
•
UNDO_TABLESPACE: Specifies the default undo tablespace.
Create Undo Tablespace:
•
Create an undo tablespace using the CREATE UNDO TABLESPACE statement.
CREATE UNDO TABLESPACE undo_tbs
DATAFILE '/path/to/undo_tbs.dbf' SIZE 500M AUTOEXTEND ON NEXT 100M MAXSIZE 2G;
•
Assigning Undo Tablespace:
•
Assign the undo tablespace to a specific user using the ALTER USER statement.
2. Monitoring Undo Tablespaces:
•
V$UNDOSTAT View:
•
•
DBA_UNDO_EXTENTS:
•
•
The V$UNDOSTAT view provides statistics on undo space usage, transaction rates,
and other relevant information.
The DBA_UNDO_EXTENTS view contains information about active undo extents and
their status.
DBA_UNDO_USAGE:
INFO 3611
•
The DBA_UNDO_USAGE view displays historical statistics on undo space
consumption.
3. Monitoring Undo Retention:
•
Undo Retention Period:
•
•
Monitor and adjust the undo retention period to ensure sufficient undo data for
long-running transactions and flashback queries.
Adjusting Undo Retention:
•
Adjust undo retention dynamically based on workload patterns using the
UNDO_RETENTION parameter.
ALTER SYSTEM SET UNDO_RETENTION = 1800; -- Set retention to 1800 seconds (30 minutes)
4. Undo Tablespace Growth:
•
Autoextend Configuration:
•
Ensure that the undo tablespaces are configured with autoextend to accommodate
growing workload.
ALTER DATABASE DATAFILE '/path/to/undo_tbs.dbf' AUTOEXTEND ON NEXT 100M MAXSIZE 2G;
•
Monitoring Growth:
•
Regularly monitor undo tablespace growth using the DBA_DATA_FILES view.
SELECT FILE_NAME, BYTES, AUTOEXTENSIBLE, MAXBYTES FROM DBA_DATA_FILES WHERE
TABLESPACE_NAME = 'UNDO_TBS';
5. Managing Undo Space Pressure:
•
Alerts and Notifications:
•
•
Dynamic Performance Views:
•
•
Set up alerts and notifications to be informed of undo space pressure or impending
exhaustion.
Utilize dynamic performance views like V$UNDOSTAT to identify periods of high
undo activity and potential space pressure.
Adding Undo Tablespaces:
•
If undo tablespaces become full, consider adding additional undo tablespaces to
distribute the undo load.
CREATE UNDO TABLESPACE undo_tbs2 DATAFILE '/path/to/undo_tbs2.dbf' SIZE 500M AUTOEXTEND
ON NEXT 100M MAXSIZE 2G;
INFO 3611
6. Undo Advisor:
•
Enterprise Manager (OEM):
•
•
Use Oracle Enterprise Manager to access the Undo Advisor, which provides
recommendations for undo tablespace configuration.
SQL Tuning Advisor:
•
The SQL Tuning Advisor within OEM can provide insights into undo-related
performance improvements.
7. Best Practices:
•
Sizing Undo Tablespaces:
•
•
Regular Monitoring:
•
•
Dynamically adjust parameters like UNDO_RETENTION based on workload patterns
and performance requirements.
Automated Management:
•
•
Implement regular monitoring of undo space usage, transaction rates, and undo
retention.
Adjusting Parameters:
•
•
Size undo tablespaces appropriately based on workload and expected undo
retention periods.
Leverage automatic undo management features to simplify and automate undo
tablespace management.
Capacity Planning:
•
Engage in capacity planning to ensure that undo tablespaces can handle the
expected workload and growth.
Properly configuring and monitoring undo tablespaces is crucial for maintaining the health and
performance of the Oracle Database. By following best practices, adjusting parameters dynamically,
and employing monitoring tools, administrators can ensure that undo tablespaces efficiently support
transaction processing, read consistency, and flashback queries. Regular maintenance, capacity
planning, and proactive management contribute to a stable and responsive database environment.
C. Managing Undo Data: PL/SQL Scripts for Managing Undo Data
PL/SQL scripts provide a powerful and automated way to manage undo data in Oracle Database.
These scripts can be used to monitor undo tablespaces, adjust undo retention, analyze undo space
usage, and perform other essential tasks. Here are PL/SQL scripts for managing undo data:
1. Monitor Undo Space Usage:
•
Script to Display Undo Space Usage:
SET SERVEROUTPUT ON
INFO 3611
DECLARE
v_total_undo_space NUMBER;
v_used_undo_space NUMBER;
BEGIN
SELECT MAX(MAXBYTES), SUM(BYTES)
INTO v_total_undo_space, v_used_undo_space
FROM DBA_DATA_FILES
WHERE TABLESPACE_NAME = 'UNDOTBS1';
DBMS_OUTPUT.PUT_LINE('Total Undo Space: ' || v_total_undo_space / 1024 / 1024 || ' MB');
DBMS_OUTPUT.PUT_LINE('Used Undo Space: ' || v_used_undo_space / 1024 / 1024 || ' MB');
END;
/
2. Adjust Undo Retention:
•
Script to Dynamically Adjust Undo Retention:
DECLARE
v_target_retention NUMBER := 1800; -- Target retention in seconds (30 minutes)
BEGIN
EXECUTE IMMEDIATE 'ALTER SYSTEM SET UNDO_RETENTION=' || v_target_retention;
END;
/
3. Check Undo Retention Period:
•
Script to Check Current Undo Retention Period:
SET SERVEROUTPUT ON
DECLARE
v_current_retention NUMBER;
BEGIN
SELECT TO_NUMBER(VALUE)
INTO v_current_retention
FROM V$PARAMETER
WHERE NAME = 'undo_retention';
INFO 3611
DBMS_OUTPUT.PUT_LINE('Current Undo Retention: ' || v_current_retention || ' seconds');
END;
/
4. Display Undo Space Statistics:
•
Script to Display Undo Space Statistics:
SET SERVEROUTPUT ON
DECLARE
v_undo_stats SYS.DBMS_UNDO_ADV.UNDO_STATS_ARRAY;
BEGIN
v_undo_stats := SYS.DBMS_UNDO_ADV.GET_UNDO_STATS;
FOR i IN 1..v_undo_stats.COUNT LOOP
DBMS_OUTPUT.PUT_LINE('Undo ' || i || ' Space: ' || v_undo_stats(i).SPACE_NAME);
DBMS_OUTPUT.PUT_LINE(' Size: ' || v_undo_stats(i).SPACE_SIZE / 1024 / 1024 || ' MB');
DBMS_OUTPUT.PUT_LINE(' Used: ' || v_undo_stats(i).USED_SPACE / 1024 / 1024 || ' MB');
DBMS_OUTPUT.PUT_LINE(' Max Query Duration: ' || v_undo_stats(i).MAXQUERYDURATION || '
seconds');
END LOOP;
END;
/
5. Monitor Undo Tablespaces:
•
Script to Monitor Undo Tablespaces:
SET SERVEROUTPUT ON
DECLARE
v_tablespace_name VARCHAR2(30) := 'UNDOTBS1';
v_autoextensible VARCHAR2(3);
BEGIN
SELECT AUTOEXTENSIBLE
INTO v_autoextensible
FROM DBA_DATA_FILES
INFO 3611
WHERE TABLESPACE_NAME = v_tablespace_name AND ROWNUM = 1;
IF v_autoextensible = 'YES' THEN
DBMS_OUTPUT.PUT_LINE(v_tablespace_name || ' is autoextensible.');
ELSE
DBMS_OUTPUT.PUT_LINE(v_tablespace_name || ' is not autoextensible.');
END IF;
END;
/
6. Manage Undo Tablespaces:
•
Script to Add Undo Tablespace:
DECLARE
v_tablespace_name VARCHAR2(30) := 'UNDOTBS2';
v_datafile_path VARCHAR2(100) := '/path/to/undotbs2.dbf';
BEGIN
EXECUTE IMMEDIATE 'CREATE UNDO TABLESPACE ' || v_tablespace_name ||
' DATAFILE ''' || v_datafile_path || ''' SIZE 500M AUTOEXTEND ON NEXT 100M MAXSIZE
2G';
END;
/
•
Script to Shrink Undo Tablespace:
DECLARE
v_tablespace_name VARCHAR2(30) := 'UNDOTBS1';
v_new_size NUMBER := 500; -- New size in megabytes
BEGIN
EXECUTE IMMEDIATE 'ALTER DATABASE DATAFILE ''/path/to/undotbs1.dbf'' RESIZE ' || v_new_size
|| 'M';
END;
/
7. Undo Advisor Recommendations:
•
Script to View Undo Advisor Recommendations:
SET SERVEROUTPUT ON
INFO 3611
DECLARE
v_advisor_info CLOB;
BEGIN
v_advisor_info := DBMS_ADVISOR.GET_TASK_SCRIPT('UNDO_ADVISOR',
DBMS_ADVISOR.ALL_TASKS);
DBMS_OUTPUT.PUT_LINE(v_advisor_info);
END;
/
These PL/SQL scripts provide a foundation for managing undo data in Oracle Database. You can
customize and extend these scripts based on specific requirements and integrate them into regular
maintenance tasks. Regularly monitoring and adjusting undo-related parameters contribute to a
well-maintained and efficient database environment.
D. Managing Undo Data: Resolving Common Undo-Related Issues
Undo-related issues can impact the stability and performance of Oracle Database. Proper
management and resolution of these issues are crucial for maintaining a responsive and reliable
database environment. Here are notes with details on resolving common undo-related issues:
1. Undo Tablespaces Running Out of Space:
•
Symptoms:
•
•
Error messages indicating "ORA-30036: unable to extend segment in undo
tablespace."
Resolution:
•
Increase the size of the undo tablespace or add additional undo tablespaces.
ALTER DATABASE DATAFILE '/path/to/undo_tbs.dbf' AUTOEXTEND ON NEXT 100M MAXSIZE 2G;
2. Long-Running Transactions:
•
Symptoms:
•
•
High undo space consumption due to long-running transactions.
Resolution:
•
Identify and terminate or optimize long-running transactions.
INFO 3611
The V$TRANSACTION view provides information about active transactions in the Oracle database.
However, it's important to note that Oracle doesn't directly support a ROLLBACK TRANSACTION
syntax with a specific transaction identifier as you might find in some other database systems.
In Oracle, you generally perform a ROLLBACK statement without specifying a transaction identifier to
roll back the entire transaction. If you want to terminate a specific session or transaction, you can
use the ALTER SYSTEM KILL SESSION statement.
Here's an example of how you might use it:
-- Identify the session you want to terminate
SELECT SID, SERIAL# FROM V$SESSION WHERE TRANSACTION_ID = 'transaction_id';
-- Terminate the identified session
ALTER SYSTEM KILL SESSION 'SID,SERIAL#' IMMEDIATE;
Replace 'transaction_id' with the actual transaction identifier you want to investigate and terminate.
Remember that forcibly terminating a session can have consequences, so use the ALTER SYSTEM KILL
SESSION statement with caution and only when necessary.
Also, please note that querying or manipulating the V$ views may require appropriate privileges, and
only users with the necessary permissions should perform such actions.
3. Undo Retention Issues:
•
Symptoms:
•
•
Inadequate undo retention leading to ORA-01555 (snapshot too old) errors.
Resolution:
•
Increase the undo retention period dynamically.
ALTER SYSTEM SET UNDO_RETENTION = 1800; -- Set retention to 1800 seconds (30 minutes)
4. Frequent UNEXPIRED Message in Alert Log:
•
Symptoms:
•
•
Frequent UNEXPIRED message in the alert log indicating undo retention issues.
Resolution:
•
Adjust undo retention and monitor undo space usage.
ALTER SYSTEM SET UNDO_RETENTION = 1800; -- Set retention to 1800 seconds (30 minutes)
5. Insufficient Undo Space for Flashback Queries:
•
Symptoms:
INFO 3611
•
•
Flashback queries failing due to insufficient undo space.
Resolution:
•
Increase the undo tablespace size or adjust undo retention to ensure sufficient space
for flashback queries.
6. Undo Tablespaces in NEEDS RECOVERY State:
•
Symptoms:
•
•
Undo tablespaces marked as NEEDS RECOVERY due to unexpected shutdown or
crash.
Resolution:
•
Perform instance recovery to bring the database to a consistent state.
7. Frequent Undo Segment Shrinking:
•
Symptoms:
•
•
Frequent undo segment shrinking leading to performance issues.
Resolution:
•
Analyze the workload and undo retention settings to determine appropriate undo
space requirements.
8. Monitoring and Alerts:
•
Symptoms:
•
•
Lack of monitoring and alerts for undo-related issues.
Resolution:
•
Set up monitoring tools, alerts, and regular checks for undo space usage, longrunning transactions, and undo retention.
9. Frequent Autoextend Operations:
•
Symptoms:
•
•
Frequent autoextend operations causing performance degradation.
Resolution:
•
Monitor undo space growth and adjust autoextend settings accordingly. Consider
preallocating space if autoextend operations are impacting performance.
10. Large Undo Tablespace Fragments:
•
Symptoms:
•
Large fragments in the undo tablespace leading to inefficiency.
INFO 3611
•
Resolution:
•
Identify and reclaim space from large fragments using the
DBMS_UNDO_ADV.RECLAIM procedure.
11. High Undo Space Consumption During Batch Processing:
•
Symptoms:
•
•
High undo space consumption during batch processing.
Resolution:
•
Optimize batch processes to minimize undo usage. Consider breaking down large
transactions into smaller, more manageable units.
12. Undo Advisor Recommendations:
•
Symptoms:
•
•
Lack of insights into optimal undo configuration.
Resolution:
•
Utilize the Undo Advisor in Oracle Enterprise Manager or run PL/SQL scripts to view
recommendations.
SET SERVEROUTPUT ON
DECLARE
v_advisor_info CLOB;
BEGIN
v_advisor_info := DBMS_ADVISOR.GET_TASK_SCRIPT('UNDO_ADVISOR',
DBMS_ADVISOR.ALL_TASKS);
DBMS_OUTPUT.PUT_LINE(v_advisor_info);
END;
/
13. Database Hangs Due to Undo Contention:
•
Symptoms:
•
•
Database hangs or performance degradation due to undo contention.
Resolution:
•
Investigate and resolve undo contention issues. Consider adjusting undo tablespace
configuration, optimizing SQL queries, or increasing undo tablespaces.
INFO 3611
Proactive monitoring, regular maintenance, and prompt resolution of undo-related issues are
essential for ensuring the optimal performance and stability of Oracle Database. Tailor the resolution
strategies to the specific nature and symptoms of the encountered issues, and consider collaborating
with database administrators or Oracle support for more complex scenarios.
0
You can add this document to your study collection(s)
Sign in Available only to authorized usersYou can add this document to your saved list
Sign in Available only to authorized users(For complaints, use another form )