A couple of days back a friend of mine (happens to be a DBA) called me and asked a question which I was unable to answer at that time but figured out later that I actually know the answer and how come I forgot. Here is the question:
"When a transaction starts does an undo segment is assigned to it?"
To make myself sure I started to look into my blog. I found a post named "Rollback Segments" which I wrote back in 2007. Here is the rewrite of that post because apparently rollback segments don't exist anymore. Oracle has renamed rollback tablespace and segments with undo tablespace and segments. At that point both were used but now there is no such thing as rollback. There are numerous advantages of undo tablespace over rollback tablespace, one of them is automation. As for as the question goes the answer is no. I found the answer in my old blog and have underlined the lines below which gave me the answer.
Purposes:
The
primary purpose of undo segments is to hold old values. Means that
when you update a value the updated value is stored in the table and the
old value is stored in the undo segment. So whenever that old value
is needed again it is restored from undo segment. There are three
main purposes of storing old values and thus of undo segments:
- Transaction Rollback: Suppose you are working in a table. And you updated the name column value of a particular row from Scott to James. And then suddenly you realized that change you made was not right so you give the command of rollback. The original value i.e. Scott will be restored again. How did database come to know that the old value was Scott? The answer is through undo segment because it’s the undo segment that stores the old values.
- Transaction Recovery: The second purpose is transaction recovery. Suppose you have made a change and have not committed it. But another user working on the same database gave the command of commit. LGWR will write because the commit command is given. And when LGWR writes, it writes all the changes from redo buffer cache to on-line redo logs. But remember along other things redo logs also store the SCN of transaction and also whether it is committed by its user or not. Some how your DBWR also fires and writes the uncommitted change to data files. At this point in time your power supply goes off. Next time when instance will startup the SMON process will see that which transactions are stored correctly and also which are committed by their own users and which are not. If any transaction is not committed by its own user just like one above it will restore its old value; and it will take the old value from undo segment.
- Read Consistency: Read consistency is something that every good database must posses. It is a sort of yard stick through which the maturity of a RDBMS is measured. In oracle this feature is implemented by making use of undo segments. Suppose a user has given the query to fetch the first five rows from a table. The query has only fetched first three rows that another query came and updated the last two rows. Now the first query should get the old values because it was issued first then the query who updated the rows. Surely it will get the old values but not from the table but from rollback segment. However any other query given after the update command will get the new updated values. Both queries will get a consistent view of data with respect to time they arrived at database engine; this is Read Consistency.
Working:
Undo segments are maintained in a circular fashion. Usually more then one
transaction can write in one rollback segment. Up till 8i it was not
possible for multiple transactions to write in one extent. But in later
versions of 8i and in above all versions it is possible. But still it is
not possible for multiple transactions to write in one data block. Once
one extent is full it is switched to the other. This is known as Wrap.
When all the extents are full then instead of over writing the first one
a new extent is created at run time. It is called Extend.