[Tarantool-patches] [tarantool-patches] Re: [PATCH v1 1/1] sql: fix fk violation for autoincremented field

Kirill Shcherbatov kshcherbatov at tarantool.org
Mon Oct 21 11:38:57 MSK 2019


On 17.10.2019 22:20, Konstantin Osipov wrote:
> * Kirill Shcherbatov <kshcherbatov at tarantool.org> [19/10/17 17:36]:
>> Fk constraints used to ignore incremented fields that are
>> represented with NULL placeholders in a tuple during FK tests in
>> the SQL frontend. A factual value is inserted in a tuple a bit
>> later, during DML 'insert' operation on the server side.
>>
>> To work around this issue a new OP_NextSequenceValue opcode is
>> introduced. This new operation retrieves a next sequence value
>> (that will be factually inserted in a tuple later) to test it
>> for fk constraint compatibility.
> 
> If you added a method to query the next sequence value, why don't
> you set it in SQL? This way you don't have to query it, you
> increment
> and use it in SQL right away.

1. "why don't you set it in SQL"
I've spend much time to deal with this concept and now I think that is is not really good idea also.

Consider the following example:
box.execute("CREATE TABLE t (i INT PRIMARY KEY AUTOINCREMENT, a INT check (a > 0));")
box.execute("INSERT OR IGNORE INTO t VALUES (null, 1), (null, -1), (null, 2);")
- autoincrement_ids:
  - 1
  - 3
  row_count: 2


As you see, the generated ids are attached to the response only when the operation is done.

2. Dealing with problem as proposed, if autoincremented ids were generated beforehand, i.e.
(null, 1), (null, -1), (null, 2) -> (1, 1), (2, -1), (3, 2)
we wouldn't attach them to the response during generation, because
(for some reason, like ck constraint) some parts of the DML request could
be declined.

At the same time in OP_IdxInsert (in this case) we have no information about the id is
generated for the last tuple processed. (in the current Tarantool's architecture it is
possible, because the generation is guaranteed to be performed during the tuple insertion
- so we take a look for the sequence state).

3. Unfortunately, my previously proposed approach is not correct also:
box.execute("CREATE TABLE t1 (s1 INTEGER PRIMARY KEY);")
box.execute("CREATE TABLE t2 (s1 INTEGER PRIMARY KEY AUTOINCREMENT, FOREIGN KEY (s1) REFERENCES t1);")
box.execute("INSERT INTO t2 VALUES (NULL), (NULL), (NULL);")

Looking for the next value of the sequence without advancing the sequence state is
not correct, as you could imagine.

4. You know, a completely correct solution for the problem of the ticket #4565 is evaluating
fk constraints on the server side :(



More information about the Tarantool-patches mailing list