Search code examples
tarantool

get curren value of box.sequence in tarantool


I got sequence current value 3

localhost:3401> box.sequence.S:next()
---
- 3
...

I can't find current value in the object

localhost:3401> box.sequence.S
---
- step: 1
  id: 1
  min: 1
  cache: 0
  uid: 1
  max: 9223372036854775807
  cycle: false
  name: S
  start: 1
...

How to get current value? Didn't find any way in documentation.


Solution

  • As you can see, your sequence S has id=1

    There is system space box.space._sequence_data which has current values of all sequences. All you need to do is just select your sequence id from the space: box.space._sequence_data:select(1).

    For example, my_seq is 4th sequence in my database.

    tarantool> my_seq = box.schema.sequence.create('MySeq', {start=111})
    ---
    ...
    
    tarantool> box.space._sequence_data:select(my_seq.id)
    ---
    - []
    ...
    
    tarantool> my_seq:next()
    ---
    - 111
    ...
    
    tarantool> box.space._sequence_data:select(my_seq.id)
    ---
    - - [4, 111]
    

    As you can see, right after creation _sequence_data doesn't have any details about new sequence until first use of my_seq:next() which initialises the sequence.

    As you can see box.space._sequence_data:select(my_seq.id) returns tuple with number of the sequence and current value.

    Please be aware of absence of guarantees for this method. And there are some issues with replication.

    In 2.4.1 sequence:currval() is introduced: https://github.com/tarantool/tarantool/issues/4752