pgfsm
pgfsm · Finite State Machine enforcement with triggers and util functions.
Overview
PostgreSQL · 17
| Relationships | |
|---|---|
| Requires | plpgsql |
Usage
Sources:
pgfsm is a compact SQL example for storing finite-state-machine definitions and instances in PostgreSQL. It enforces that each instance uses a declared state and that state updates follow an allowed destination. It is instructional code, not a general workflow engine.
Core Workflow
Install the extension into a dedicated fsm schema, define transitions, create an instance, and advance it by transition label:
Every usable state must appear as a from_state because the machine table has a foreign key to that column.
Important Objects
transitionstores machine name, current state, transition label, and destination state.machinestores state-machine instances.transitions_forreturns the transition row available from an instance’s current state.states_forreturns the declared source states for a machine definition.do_transitionapplies a matching label and returns the updated instance.check_valid_state_updateandcheck_valid_state_insertback the validation triggers.
Design Limits
The primary key on transition(name, from_state) permits only one outgoing transition row per state, so it cannot model branching by label. A direct update may choose the permitted destination without presenting the label; revoke direct table updates if transition labels matter. There is no history, callback execution, optimistic version, ownership model, or workflow-definition migration support. Extend the schema deliberately before using the pattern for application state.