Skip to content

pgfsm

Finite State Machine enforcement with triggers and util functions.

pgfsm · Finite State Machine enforcement with triggers and util functions.

Overview

IDExtensionPackageVersionCategoryLicenseLanguage
100151pgfsmpgfsm0.0.1FEATCC0-1.0SQL

PostgreSQL · 17

Relationships
Requiresplpgsql

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:

CREATE SCHEMA fsm;
CREATE EXTENSION pgfsm WITH SCHEMA fsm;
SET search_path = fsm, public;

INSERT INTO transition(name, from_state, transition, to_state) VALUES
  ('turnstile', 'locked',   'coin', 'unlocked'),
  ('turnstile', 'unlocked', 'push', 'locked');

INSERT INTO machine(name, state)
VALUES ('turnstile', 'locked');

SELECT * FROM do_transition(1, 'coin');

Every usable state must appear as a from_state because the machine table has a foreign key to that column.

Important Objects

  • transition stores machine name, current state, transition label, and destination state.
  • machine stores state-machine instances.
  • transitions_for returns the transition row available from an instance’s current state.
  • states_for returns the declared source states for a machine definition.
  • do_transition applies a matching label and returns the updated instance.
  • check_valid_state_update and check_valid_state_insert back 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.

Table reader