Class: Pcrd::Cutover::Sequences

Inherits:
Object
  • Object
show all
Defined in:
lib/pcrd/cutover/sequences.rb

Overview

Advances sequences on the target cluster to be safely ahead of the source.

Called during cutover after writes on the source have stopped (maintenance mode active). At that point the source sequence is frozen and we can safely compute the correct target value.

For each serial/bigserial/identity column in the migrated tables:

1. Read last_value + is_called from the source sequence
2. Read MAX(pk_col) from the source table (covers rolled-back transactions
   that consumed sequence values without committing a row)
3. Take the maximum of both + safety_buffer
4. Call setval on the target sequence

Returns an Array<SequenceResult> describing every setval performed.

Defined Under Namespace

Classes: SequenceResult

Instance Method Summary collapse

Constructor Details

#initialize(source_pool:, target_pool:, safety_buffer: 1_000) ⇒ Sequences

Returns a new instance of Sequences.



31
32
33
34
35
# File 'lib/pcrd/cutover/sequences.rb', line 31

def initialize(source_pool:, target_pool:, safety_buffer: 1_000)
  @source   = source_pool
  @target   = target_pool
  @buffer   = safety_buffer
end

Instance Method Details

#advance(table_names) ⇒ Object

Advances sequences for all serial/identity columns in the given tables. Returns Array<SequenceResult>.



39
40
41
42
43
44
45
46
47
48
# File 'lib/pcrd/cutover/sequences.rb', line 39

def advance(table_names)
  results = []
  table_names.each do |table_name|
    sequences_for_table(table_name).each do |col_name, seq_name|
      result = advance_one(table_name, col_name, seq_name)
      results << result if result
    end
  end
  results
end