create table fsm.state_machine_event ( shard_id bigint not null, id bigint not null generated by default as identity ( start with 1 cache 32767 ), state_machine_id bigint not null, created_at timestamptz not null default now(), handled_at timestamptz, name text not null, data jsonb not null, primary key (shard_id, id), constraint created_should_be_before_handled check (created_at <= handled_at), constraint fk_state_machine foreign key(shard_id, state_machine_id) references fsm.state_machine(shard_id, id) on delete cascade ); create index idx_state_machine on fsm.state_machine_event(shard_id, state_machine_id) where handled_at is null; comment on table fsm.state_machine_event is 'Queue of events that need to be processed for the state machine.'; comment on column fsm.state_machine_event.created_at is $comment$ The timestamp when this event was created. The timestamp should be before "handled_at". $comment$; comment on column fsm.state_machine_event.handled_at is $comment$ The timestamp when this event was handled. The timestamp should be after "created_at". $comment$; comment on column fsm.state_machine_event.data is $comment$ The data to be sent to the callback handlers in on_entry, on_exit. $comment$;