sql: entire table appears locked even when updating just 1 row
the table is locked when one transaction is updating one line data by Id(auto create with index) .can cockroach lock one row?
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!
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?
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?
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
read using two columns.,while update using id primary key column
Can you show us which SQL queries you have run, and also how you have "measured" or "detected" that a lock was happening?
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.
different thread query and update different data ,can cockroach lock rows like mysql, and don't lock table
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 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.
@Jaware do you have an index on the time column? If you do not, that might explain why the transaction blocks.
@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.
Agreed, created docs ticket for tracking: https://github.com/cockroachdb/docs/issues/3360
@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
Closing this due to inactivity, the docs issue will track the necessary documentation improvements. Feel free to keep using it to ask questions, @Jaware.