cockroach icon indicating copy to clipboard operation
cockroach copied to clipboard

sql: entire table appears locked even when updating just 1 row

Open Jaware opened this issue 8 years ago • 15 comments

the table is locked when one transaction is updating one line data by Id(auto create with index) .can cockroach lock one row?

Jaware avatar Apr 19 '18 11:04 Jaware

Hi @Jaware. CockroachDB doesn't need to lock rows because all transactions are serializable. Database locks are needed when running at a lower consistency level, like snapshot. Just put your reads and writes in the same transaction and it will work. Thanks for your interest in CockroachDB!

danhhz avatar Apr 19 '18 14:04 danhhz

ok,but when i put read in transaction,it got locked,because other transaction is executing. so you mean cockroachDB serializable transactions will lock the Database when executing?

Jaware avatar Apr 20 '18 03:04 Jaware

CockroachDB doesn't lock the database, no. Can you give details on the issue you're seeing? What transactions are you running? Why do you think it's locked?

danhhz avatar Apr 20 '18 17:04 danhhz

one method use seriable transaction contains read and update(or insert ),hundreds of threads execute the same code at the same time , then a lot of reads are locked

Jaware avatar Apr 21 '18 15:04 Jaware

read using two columns.,while update using id primary key column

Jaware avatar Apr 21 '18 15:04 Jaware

Can you show us which SQL queries you have run, and also how you have "measured" or "detected" that a lock was happening?

knz avatar Apr 21 '18 15:04 knz

select top 1 from app where name ='aa' and state =0 order by time desc ,update app set length =123 where id =1 . different thread has different name condition. lock because we see a lot of transaction retry errors ,and then we debug and find that if one transaction is not finished, other transaction 's select query is locked.

Jaware avatar Apr 21 '18 15:04 Jaware

different thread query and update different data ,can cockroach lock rows like mysql, and don't lock table

Jaware avatar Apr 21 '18 15:04 Jaware

I see same behavior. But as I realaized its OK, because CRDB uses SERIALIZATION level of consistency by default.

How repeat it:

t1, t2, t3 ...: logical time

thread1 t1: start transaction 1 t2: update row1 into table t3: wait, without commit

thread2 t3: start transaction 2 t4: select * from table; <--- locked here

insted of select *' we can write any statement that touched row1.

after when thread1 to commit transaction1, thread2 go ahed.

But more confuse I got when my application was terminated (and closed connection without commit), but transaction didnt rollback. And other transactions remain blocked for long time after.

avatar29A avatar Jul 05 '18 06:07 avatar29A

@avatar29A which version are you using? We have made improvements to the txn rollback in 2.0, and more improvements to expect in 2.1 too.

knz avatar Jul 05 '18 06:07 knz

@Jaware do you have an index on the time column? If you do not, that might explain why the transaction blocks.

knz avatar Jul 05 '18 06:07 knz

@jseldess @tim-o this is a common topic and probably needs a FAQ and tutorial: if there are many concurrent clients each updating single rows in a table (update by ID with an index on ID), then concurrent requests that are forced to scan the entire table (e.g. select by time with NO index on time) will have to either wait or retry. This retry/wait can be avoided by making the select skip over the updated rows, e.g. via a suitable index.

knz avatar Jul 05 '18 06:07 knz

Agreed, created docs ticket for tracking: https://github.com/cockroachdb/docs/issues/3360

tim-o avatar Jul 05 '18 16:07 tim-o

@knz

$ cockroach version

Build Tag:    v2.0.3
Build Time:   2018/06/18 16:11:33
Distribution: CCL
Platform:     linux amd64 (x86_64-unknown-linux-gnu)
Go Version:   go1.10
C Compiler:   gcc 6.3.0
Build SHA-1:  91715a9a95edbe716912173204fa4c0fc6724457
Build Type:   release

avatar29A avatar Jul 09 '18 05:07 avatar29A

Closing this due to inactivity, the docs issue will track the necessary documentation improvements. Feel free to keep using it to ask questions, @Jaware.

jordanlewis avatar Jul 19 '18 17:07 jordanlewis