Consider a table with fields ID, X, Y, and Z.
CREATE TABLE TABLE_NAME (
ID int NOT NULL,
X varchar(255) NOT NULL,
Y varchar(255) NOT NULL,
Z varchar(255) NOT NULL,
PRIMARY KEY (ID)
);
create index idx on TABLE_NAME (X, Y);
Suppose I have the following transaction with repeatable read isolation level -
SET TRANSACTION ISOLATION LEVEL REPEATABLE READ;
BEGIN TRANSACTION;
SELECT * FROM TABLE_NAME WHERE X="A" AND Y="B";
INSERT INTO TABLE (ID, X, Y, Z) VALUES (25, "A", "B", "C");
INSERT INTO TABLE (ID, X, Y, Z) VALUES (26, "A", "B", "D");
DELETE FROM TABLE_NAME WHERE X="A" AND Y="B" AND (Z != "C" OR Z != "D")
COMMIT TRANSACTION;
Now suppose I have a normal SELECT
statement being executed outside the transaction. I want to know what happens to the SELECT
statement if -
- It executes between the
SELECT
andINSERT
of the transaction. - It executes between the first
INSERT
statement and secondINSERT
statement of the transaction. - It executes between the
INSERT
statement 2 andDELETE
statement of the transaction. - It executes between
DELETE
statement and committing the transaction.
I did go through the docs, and these are my assumptions for the scenarios -
SELECT
only obtains a shared lock, so theSELECT
statement outside the transaction is allowed to execute and sees all related rows in the table.INSERT
obtains an exclusive lock the the newly inserted row. TheSELECT
statement outside the transaction is allowed to execute but it does not see the newly inserted row.- Same as #2, allowed but it does not see both newly inserted rows.
DELETE
obtains an exclusie lock, so theSELECT
statement outside the transaction is blocked, until the executing transaction is committed.
Is my assumption correct? Specially confused about #2 and #3 here.