Sequences

sequence

Auto-incrementing sequence of int64.

sequence_next()

Advance the sequence to its next value and return that value.

sequence_reset()

Reset the sequence to its initial state or the specified value.

type

sequence

Sequences - 图1

Sequences - 图2

Sequences - 图3

sequence

Auto-incrementing sequence of int64.

This type can be used to create auto-incrementing properties.

  1. scalar type TicketNo extending sequence;
  2. type Ticket {
  3. property number -> TicketNo {
  4. constraint exclusive;
  5. }
  6. }

The sequence is bound to the scalar type, not to the property, so if multiple properties use the same sequence type they will share the same counter. For each distinct counter, a separate scalar type that is extending sequence should be used.

function

sequence_next()

Sequences - 图4

Sequences - 图5

Sequences - 图6

std::sequence_next(seq: schema::ScalarType) -> int64

Advance the sequence to its next value and return that value.

Sequence advancement is done atomically, each concurrent session and transaction will receive a distinct sequence value.

  1. db>
  1. select sequence_next(introspect MySequence);
  1. {11}

function

sequence_reset()

Sequences - 图7

Sequences - 图8

Sequences - 图9

std::sequence_reset(seq: schema::ScalarType) -> int64std::sequence_reset( seq: schema::ScalarType, val: int64) -> int64

Reset the sequence to its initial state or the specified value.

The single-parameter form resets the sequence to its initial state, where the next sequence_next() call will return the first value in sequence. The two-parameters form allows changing the last returned value of the sequence.

  1. db>
  1. select sequence_reset(introspect MySequence);
  1. {1}
  1. db>
  1. select sequence_next(introspect MySequence);
  1. {1}
  1. db>
  1. select sequence_reset(introspect MySequence, 22);
  1. {22}
  1. db>
  1. select sequence_next(introspect MySequence);
  1. {23}

The sequence to be operated on by the functions above is specified by a schema::ScalarType object. If the sequence argument is known ahead of time and does not change, the recommended way to pass it is to use the introspect operator:

  1. select sequence_next(introspect MySequenceType);
  2. # or
  3. select sequence_next(introspect typeof MyObj.seq_prop);

This style will ensure that a reference to a sequence type from an expression is tracked properly to ensure schema referential integrity.

If, on the other hand, the operated sequence type is determined at run time via a query argument, it must be queried from the schema::ScalarType set directly like so:

  1. with
  2. SeqType := (
  3. select schema::ScalarType
  4. filter .name = <str>$seq_type_name
  5. )
  6. select
  7. sequence_next(SeqType);

Caution

To work efficiently in high concurrency without lock contention, a sequence_next() operation is never rolled back even if the containing transaction is aborted. This may result in gaps in the generated sequence. Likewise, sequence_reset() is not undone if the transaction is rolled back.