tarantool / tarantool/tarantool
`before_replace` triggers inconsistency when updating a primary key
Nobody has claimed this yet.
- Dominant language
- Lua
- Stars
- 3.7k
- Forks
- 419
- Avg merge
- 1d 23h
- Merged PRs (30d)
- 88
Description
Tarantool version: 2.11.0-entrypoint-358-g9965e3fe4 (current master at the time of writing)
I've noticed some strange behaviour with our before_replace triggers:
- A usual insert of a duplicate key to a unique index leads to an
ER_TUPLE_FOUNDerror. However, when the space has abefore_replacetrigger, the same insert completes just fine:
tarantool> box.cfg{}
---
...
tarantool> box.once('bootstrap', function()
> box.schema.space.create('test')
> box.space.test:create_index('pk')
> end)
---
...
tarantool> box.space.test:insert{1}
---
- [1]
...
tarantool> box.space.test:insert{1} -- duplicate
---
- error: Duplicate key exists in unique index "pk" in space "test" with old tuple
- [1] and new tuple - [1]
...
tarantool> box.space.test:before_replace(function(old, new) return new:update{{'=', 1, new[1]}} end)
---
- 'function: 0x0111209ec0'
...
tarantool> box.space.test:insert{1} -- same duplicate, but succeeds with a no-op before_replace trigger!
---
- [1]
...
- Usually it's not allowed to change tuple's fields indexed by primary key in
before_replacetriggers. But when inserting a tuple not present in space this works just fine, and also allows duplicates like in example 1:
tarantool> box.cfg{}
---
...
tarantool> box.once('bootstrap', function()
> box.schema.space.create('test')
> box.space.test:create_index('pk')
> end)
---
...
tarantool> box.space.test:insert{1}
---
- [1]
...
tarantool> box.space.test:select({}, {fullscan=true})
---
- - [1]
...
tarantool> box.space.test:before_replace(function(old, new) return new:update{{'=', 1, 2}} end)
---
- 'function: 0x0110d1da30'
...
tarantool> box.space.test:insert{1} -- error as expected
---
- error: Attempt to modify a tuple field which is part of primary index in space 'test'
...
tarantool> box.space.test:insert{3} -- no error for a new tuple!
---
- [2]
...
tarantool> box.space.test:insert{4} -- and even duplicates!
---
- [2]
...
So, first of all, seems like duplicate keys should be checked after before_replace triggers are executed, not earlier.
And, secondly, we should either disallow updating fields indexed by primary key completely or allow such updates and handle them correctly in all cases.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by running the Lua reproductions in the issue for duplicate inserts and primary-key changes inside before_replace triggers. Trace the space insertion and trigger path to determine when uniqueness and primary-index checks occur. Done means the trigger behavior is consistent for existing and new tuples, with regression coverage for the reported cases.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- lua
- Domain
- databases
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100