Monday, October 12, 2009

. oraenv

The oraenv Utility: Setting Oracle Environment Variables

The oraenv (and its C shell counterpart coraenv) is a fundamental and invaluable utility for Oracle Database Administrators (DBAs) and developers working on Unix/Linux operating systems. It simplifies the process of correctly setting up the necessary environment variables to interact with a specific Oracle database instance.

What is oraenv?

oraenv is a shell script provided by Oracle that automates the configuration of essential environment variables required to connect to and manage an Oracle database. Without these variables correctly set, you might encounter errors when trying to use Oracle command-line utilities like sqlplus, rman, expdp, impdp, tnsping, and others.

Why is oraenv important?

While seemingly simple, oraenv is crucial for:

  • Consistency: It ensures that your shell environment is always configured correctly for the target Oracle database, reducing common "command not found" or TNS errors.

  • Multi-Instance/Multi-Version Environments: On servers hosting multiple Oracle database instances (each with a unique ORACLE_SID) or different Oracle software versions (each in a distinct ORACLE_HOME), oraenv allows you to quickly switch between these environments without manual configuration.

  • Ease of Use: It abstracts away the need to manually remember and set multiple environment variables, making daily DBA tasks more efficient.

What Environment Variables Does oraenv Set?

When executed, oraenv typically sets the following key environment variables:

  • ORACLE_SID: The System Identifier of the Oracle database instance you want to connect to.

  • ORACLE_HOME: The full path to the directory where the Oracle software for the specified ORACLE_SID is installed.

  • PATH: It prepends $ORACLE_HOME/bin to your system's PATH variable, allowing you to execute Oracle binaries directly from any directory.

  • LD_LIBRARY_PATH (or LIBPATH, SHLIB_PATH depending on the OS): This variable ensures that the operating system can locate the necessary Oracle shared libraries.

How to Use oraenv

The oraenv script must be "sourced" into your current shell environment for its changes to persist. This is done using a leading dot (.) followed by a space.

Basic Usage (Interactive):

. oraenv

Upon execution, oraenv will typically prompt you to enter the ORACLE_SID of the database you wish to configure your environment for:

ORACLE_SID = [your_current_sid] ?

You would then type the desired ORACLE_SID (e.g., PRODDB, TESTDB, or +ASM for an ASM instance) and press Enter. oraenv will then look up the corresponding ORACLE_HOME in the /etc/oratab file (or /var/opt/oracle/oratab on some Linux distributions) and set the environment variables accordingly.

Non-Interactive Usage (for scripting):

To use oraenv within scripts or to avoid the prompt, you can set the ORAENV_ASK environment variable to N before calling oraenv:

ORAENV_ASK=N
ORACLE_SID=PRODDB; export ORACLE_SID
. oraenv

In this example, the environment will be automatically set for the PRODDB instance without any user interaction.

Common Scenarios Where oraenv Helps

  • "sqlplus: command not found": This often means $ORACLE_HOME/bin is not in your PATH.

  • "ORA-12154: TNS:could not resolve the connect identifier specified": While this can be a TNSNAMES.ORA issue, it can also occur if ORACLE_HOME is not correctly set, preventing Oracle from finding the network/admin directory.

  • Switching between databases: Quickly change your shell's context from one database to another.

  • Running RMAN or Data Pump: Ensure the correct Oracle binaries and libraries are found.

In essence, oraenv is a simple yet powerful utility that every Oracle DBA and user on Unix/Linux systems should be familiar with, as it streamlines environment setup and prevents many common configuration-related issues.

Wednesday, October 7, 2009

DBA world

welcome to the DBA world.........................................

This blog will be helping to start a carrier as an ORACLE DBA.

Oh.....about me......................

I also don't know..................

Keep sharing knowledge here......

Thursday, October 1, 2009

Undo & Redo

Oracle Undo and Redo Mechanisms Explained

This document delves into the fundamental concepts of Undo and Redo in Oracle Database, clarifying their roles in ensuring data consistency, transaction integrity, and database recovery. It specifically addresses why undo information is stored in redo logs in addition to undo segments/tablespaces.

Redo and Undo: The Basics

  • Redo (Redo Log Buffer / Redo Log Files): Records all changes made to the database. This includes DML (Data Manipulation Language) operations, DDL (Data Definition Language) operations, and internal Oracle operations. Redo is primarily used for roll-forward during recovery (applying changes to bring datafiles up to date).

    • The Log Writer (LGWR) process writes both undo and redo for every DML in a transaction (committed/uncommitted), irrespective of whether a checkpoint has taken place or not.

    • Redo/undo for committed changes will always be in redo logs (LGWR writes on commit).

    • Redo/undo for uncommitted changes may be in redo logs (LGWR writes every 3 seconds, on commit, or when 1/3 full).

  • Undo (Undo Segments / Undo Tablespace): Records information needed to reverse (undo) changes made by transactions. Undo is primarily used for:

    • Rollback: Reversing uncommitted transactions.

    • Read Consistency: Providing a consistent view of data to queries that started before changes were committed.

    • Flashback Features: Enabling features like Flashback Query and Flashback Version Query.

Interaction with Checkpoints

A checkpoint is a database event that signals DBWR to write all dirty buffers (modified data blocks) from the database buffer cache to the datafiles.

  • On Checkpoint:

    • Undo present in buffers in the database buffer cache is written to the undo tablespace.

    • Dirty buffers containing committed/uncommitted data are written to datafiles.

  • Implications:

    • Datafiles may contain uncommitted data (buffers dirtied by uncommitted transactions and a checkpoint takes place before commit).

    • Committed data may not yet be in datafiles (if checkpointing has not occurred after the commit).

Where Undo Information Resides

Undo information can exist in multiple places:

  • Redo Logs: Contains both redo and undo records for all changes.

  • Buffer Cache: Undo information for recent changes may be present in the buffer cache if a checkpoint has not yet written it to the undo tablespace.

  • Undo Tablespace: This is the persistent storage for undo information, written from the buffer cache during checkpoints.

    • Undo information in the undo tablespace will never be overwritten until the transaction that generated it is no longer active (either committed or rolled back).

Why Undo is Also Stored in Redo Logs

Let's consider scenarios if undo were only stored in undo buffers and the undo tablespace:

Scenario I: Instance Crash

  1. A change is made but not committed.

  2. The redo for the change is in redo logs.

  3. The undo for the change is in the buffer cache.

  4. A checkpoint has not taken place, so the undo for the change is not yet in the undo tablespace.

  5. The instance crashes.

  6. The undo information present in the buffer cache is wiped out (as buffer cache is volatile memory).

  7. As part of instance recovery, redo is applied, and datafiles now contain the uncommitted data.

  8. Problem: The database cannot be opened because the undo information needed to roll back the uncommitted change is not available, leading to an inconsistent database state.

Scenario II: Media Recovery (Datafile Loss)

  1. A hot backup of a tablespace is taken.

  2. The datafile for the tablespace is lost or corrupted.

  3. The tablespace is taken offline, and the datafile is restored from backup.

  4. The datafile needs to be recovered (roll-forward).

  5. The redo logs and archivelogs contain redo data for both committed and uncommitted transactions.

  6. Assumption (for this scenario): Redo logs and archivelogs do not contain undo data.

  7. As part of recovery, redo for all committed/uncommitted changes is read from online/archive redo logs and applied.

  8. Problem: The tablespace cannot be brought online because the undo information needed to roll back uncommitted changes is not available.

Conclusion: The Necessity of Undo in Redo Logs

To resolve the problems highlighted in the scenarios above, undo information is also stored in redo logs.

During the roll-forward phase of instance or media recovery:

  • As changes (redo records in redo logs) are applied to datafiles, the undo information also present in the redo logs is used to generate undo segments.

  • These newly generated undo segments are then subsequently used during the rollback phase of instance or media recovery to roll back any uncommitted changes, ensuring the database achieves a consistent state.

This dual storage of undo information (in redo logs for recovery and in undo segments/tablespace for active transactions, read consistency, and flashback) is fundamental to Oracle's robust transaction management and recovery capabilities.