20240307163119_denormalize_buffer_ops.sql

 1-- Add migration script here
 2
 3ALTER TABLE buffers ADD COLUMN latest_operation_epoch INTEGER;
 4ALTER TABLE buffers ADD COLUMN latest_operation_lamport_timestamp INTEGER;
 5ALTER TABLE buffers ADD COLUMN latest_operation_replica_id INTEGER;
 6
 7WITH ops AS (
 8    SELECT DISTINCT ON (buffer_id) buffer_id, epoch, lamport_timestamp, replica_id
 9    FROM buffer_operations
10    ORDER BY buffer_id, epoch DESC, lamport_timestamp DESC, replica_id DESC
11)
12UPDATE buffers
13SET latest_operation_epoch = ops.epoch,
14    latest_operation_lamport_timestamp = ops.lamport_timestamp,
15    latest_operation_replica_id = ops.replica_id
16FROM ops
17WHERE buffers.id = ops.buffer_id;