I need to take row level lock for update and at the same time allow other select queries to get their intended row which is not locked.
What I observed was if I take a lock on row1, no other select queries are allowed which are searching for other rows.
I have below table schema -
CREATE TABLE lock_test(
id int NOT NULL DEFAULT unique_rowid(),
col_with_unique_index text NOT NULL,
other_col text NOT NULL,
CONSTRAINT "primary" PRIMARY KEY (id ASC),
UNIQUE INDEX unique_idx (col_with_unique_index ASC),
FAMILY "primary"(id, col_with_unique_index, other_col));
Inserted below 2 rows -
insert into lock_test(col_with_unique_index, other_col) values('val1', 'some_val');
insert into lock_test(col_with_unique_index, other_col) values('val2', 'some_val');
Opened 2 terminals -
1st terminal -
begin;
select * from lock_test where col_with_unique_index = 'val1' for update;
2nd terminal -
select * from lock_test where col_with_unique_index = 'val2';
Expected 2nd terminal to show the result for val2 but it did not(went into waiting), instead after I executed commit in 1st terminal 2nd terminal showed the result.
I tried changing my where clause from col_with_unique_index to id which is the primary key here, and this time 2nd terminal did not wait and displayed the expected result.
I'm unable to understand the behaviour here. Can I only take row level locks if I have primary key in my where clause?