spanner interleaved table lock

103 views Asked by At

let say we have these tables:

CREATE TABLE Singers (
  SingerId   INT64 NOT NULL,
  FirstName  STRING(1024),
  LastName   STRING(1024),
  SingerInfo BYTES(MAX),
) PRIMARY KEY (SingerId);

CREATE TABLE Albums (
  SingerId     INT64 NOT NULL,
  AlbumId      INT64 NOT NULL,
  AlbumTitle   STRING(MAX),
) PRIMARY KEY (SingerId, AlbumId),
  INTERLEAVE IN PARENT Singers ON DELETE CASCADE;

I would like to perform a write to the Albums table, is the Singers table locked as well or is just the Albums table locked ?

I have searched for interleaved table lock and found a whole lot of nothing. However I did find this article which has this excerpt :

Unlike other approaches that lock entire tables or rows, the granularity of transactional locks in Spanner is a cell, or the intersection of a row and a column. This means that two transactions can read and modify different columns of the same row at the same time. To maximize the number of transactions that have access to a particular data cell at a given time, Cloud Spanner uses different lock modes

Does that mean the parent table is not locked while the interleaved child is updated ?

1

There are 1 answers

0
Knut Olav Løite On

Cloud Spanner does not take table locks, so in that sense the direct answer to your question is 'No'. Instead, Cloud Spanner takes locks at the cell level as described the article you referenced.

Specifically in the example that you are giving, inserting a row into the Albums table requires that the Singer record exists. This requires taking a lock on the primary key values of the specific Singer record. This again means that you cannot delete the Singer record while the transaction that is inserting the Album record is still running. You can however update the non-primary key values of the same Singer record (assuming that no other statements have taken any locks on any of these cells).

If you do try to delete the Singer record while the transaction that is inserting the Album record is still active, then one of the transactions will be aborted by Cloud Spanner.