• Uncategorized

About sql : Oracle—How-does-Oracle-manage-transaction-specific-DML-statements

Question Detail

Imagine I have this simple table:

Table Name: Table1
Columns:    Col1 NUMBER (Primary Key)
            Col2 NUMBER

If I insert a record into Table1 with no commit…

INSERT INTO Table1 (Col1, Col2) Values (100, 1234);

How does Oracle know that this next INSERT statement violates the PK constraint, since nothing has yet been committed to the database yet.

INSERT INTO Table1 (Col1, Col2) Values (100, 5678);

Where/how does Oracle manage the transactions so that it knows I’m violating the constraint when I haven’t even committed the transaction yet.

Question Answer

Oracle creates an index to enforce the primary key constraint (a unique index by default). When Session A inserts the first row, the index structure is updated but the change is not committed. When Session B tries to insert the second row, the index maintenance operation notes that there is already a pending entry in the index with that particular key. Session B cannot acquire the latch that protects the shared index structure so it will block until Session A’s transaction completes. At that point, Session B will either be able to acquire the latch and make its own modification to the index (because A rolled back) or it will note that the other entry has been committed and will throw a unique constraint violation (because A committed).

It’s because of the unique index that enforces the primary key constraint. Even though the insert into the data block is not yet committed, the attempt to add the duplicate entry into the index cannot succeed, even if it’s done in another session.

Just because you haven’t done a commit yet does not mean the first record hasn’t been sent to the server. Oracle already knows about you intentions to insert the first record. When you insert the second record Oracle knows for sure there is no way this will ever succeed without a constraint violation so it refuses.

If another user were to insert the second record, Oracle will accept it if the first record has not been committed yet. If the second user commits before you do, your commit will fail.

Unless a particular constraint is “deferred”, it will be checked at the point of the statement execution. If it is deferred, it will be checked at the end of the transaction. I’m assuming you did not defer your PRIMARY KEY and that’s why you get a violation even before you commit.

How this is really done is an implementation detail and may vary between different database systems and even versions of the same system. The application developer should probably not make too many assumptions about it. In Oracle’s case, PRIMARY KEY uses the underlying index for performance reasons, while there are systems out there that do not even require an index (if you can live with the corresponding performance hit).

BTW, a deferrable Oracle PRIMARY KEY constraint relies on a non-unique index (vs non-deferrable PRIMARY KEY that uses a unique index).

— EDIT —

I just realized you didn’t even commit the first INSERT. I think Justin’s answer explains nicely how what is essentially a lock contention causes one of the transactions to stall.

You may also like...

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.