Skip to content

pg_statecharts

Hierarchical state machines and transactional event handling

pg_statecharts · Hierarchical state machines and transactional event handling

Overview

IDExtensionPackageVersionCategoryLicenseLanguage
2695pg_statechartspg_statecharts0.2.0FEATMITSQL
AttributeHas BinaryHas LibraryNeed LoadHas DDLRelocatableTrusted
----d--NoNoNoYesnono
Relationships
Schemasfsm
Requiresltree
Need Bypg_statecharts_dev
Siblingspg_statecharts_dev
ltree in public

Packages

TypeRepoVersionPG Major CompatibilityPackage PatternDependencies
EXTPIGSTY0.2.019 18 17 16 15pg_statechartsltree
RPMPIGSTY0.2.019 18 17 16 15pg_statecharts_$vpostgresql$v-contrib
DEBPIGSTY0.2.019 18 17 16 15postgresql-$v-pg-statechartspostgresql-$v
Linux / PGPG19PG18PG17PG16PG15
el8.x86_64N/APIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0N/A
el8.aarch64N/APIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0N/A
el9.x86_64PIGSTY 0.2.0PIGSTY 0.2.0PIGSTY 0.2.0PIGSTY 0.2.0N/A
el9.aarch64PIGSTY 0.2.0PIGSTY 0.2.0PIGSTY 0.2.0PIGSTY 0.2.0N/A
el10.x86_64PIGSTY 0.2.0PIGSTY 0.2.0PIGSTY 0.2.0PIGSTY 0.2.0N/A
el10.aarch64PIGSTY 0.2.0PIGSTY 0.2.0PIGSTY 0.2.0PIGSTY 0.2.0N/A
d12.x86_64PIGSTY 0.2.0PIGSTY 0.2.0PIGSTY 0.2.0PIGSTY 0.2.0N/A
d12.aarch64PIGSTY 0.2.0PIGSTY 0.2.0PIGSTY 0.2.0PIGSTY 0.2.0N/A
d13.x86_64PIGSTY 0.2.0PIGSTY 0.2.0PIGSTY 0.2.0PIGSTY 0.2.0N/A
d13.aarch64PIGSTY 0.2.0PIGSTY 0.2.0PIGSTY 0.2.0PIGSTY 0.2.0N/A
u22.x86_64PIGSTY 0.2.0PIGSTY 0.2.0PIGSTY 0.2.0PIGSTY 0.2.0N/A
u22.aarch64PIGSTY 0.2.0PIGSTY 0.2.0PIGSTY 0.2.0PIGSTY 0.2.0N/A
u24.x86_64PIGSTY 0.2.0PIGSTY 0.2.0PIGSTY 0.2.0PIGSTY 0.2.0N/A
u24.aarch64PIGSTY 0.2.0PIGSTY 0.2.0PIGSTY 0.2.0PIGSTY 0.2.0N/A
u26.x86_64PIGSTY 0.2.0PIGSTY 0.2.0PIGSTY 0.2.0PIGSTY 0.2.0N/A
u26.aarch64PIGSTY 0.2.0PIGSTY 0.2.0PIGSTY 0.2.0PIGSTY 0.2.0N/A
PackageVersionOSORGSIZEFile URL
pg_statecharts_190.2.0el9.x86_64pigsty37.0 KiBpg_statecharts_19-0.2.0-1PGSTY.el9.noarch.rpm
pg_statecharts_190.2.0el9.aarch64pigsty37.0 KiBpg_statecharts_19-0.2.0-1PGSTY.el9.noarch.rpm
pg_statecharts_190.2.0el10.x86_64pigsty37.2 KiBpg_statecharts_19-0.2.0-1PGSTY.el10.noarch.rpm
pg_statecharts_190.2.0el10.aarch64pigsty37.2 KiBpg_statecharts_19-0.2.0-1PGSTY.el10.noarch.rpm
postgresql-19-pg-statecharts0.2.0d12.x86_64pigsty31.1 KiBpostgresql-19-pg-statecharts_0.2.0-1PGSTY~bookworm_all.deb
postgresql-19-pg-statecharts0.2.0d12.aarch64pigsty31.1 KiBpostgresql-19-pg-statecharts_0.2.0-1PGSTY~bookworm_all.deb
postgresql-19-pg-statecharts0.2.0d13.x86_64pigsty31.1 KiBpostgresql-19-pg-statecharts_0.2.0-1PGSTY~trixie_all.deb
postgresql-19-pg-statecharts0.2.0d13.aarch64pigsty31.1 KiBpostgresql-19-pg-statecharts_0.2.0-1PGSTY~trixie_all.deb
postgresql-19-pg-statecharts0.2.0u22.x86_64pigsty31.9 KiBpostgresql-19-pg-statecharts_0.2.0-1PGSTY~jammy_all.deb
postgresql-19-pg-statecharts0.2.0u22.aarch64pigsty31.9 KiBpostgresql-19-pg-statecharts_0.2.0-1PGSTY~jammy_all.deb
postgresql-19-pg-statecharts0.2.0u24.x86_64pigsty31.6 KiBpostgresql-19-pg-statecharts_0.2.0-1PGSTY~noble_all.deb
postgresql-19-pg-statecharts0.2.0u24.aarch64pigsty31.6 KiBpostgresql-19-pg-statecharts_0.2.0-1PGSTY~noble_all.deb
postgresql-19-pg-statecharts0.2.0u26.x86_64pigsty31.7 KiBpostgresql-19-pg-statecharts_0.2.0-1PGSTY~resolute_all.deb
postgresql-19-pg-statecharts0.2.0u26.aarch64pigsty31.7 KiBpostgresql-19-pg-statecharts_0.2.0-1PGSTY~resolute_all.deb
PackageVersionOSORGSIZEFile URL
pg_statecharts_180.1.0el8.x86_64pigsty31.8 KiBpg_statecharts_18-0.1.0-1PGSTY.el8.noarch.rpm
pg_statecharts_180.1.0el8.aarch64pigsty31.8 KiBpg_statecharts_18-0.1.0-1PGSTY.el8.noarch.rpm
pg_statecharts_180.2.0el9.x86_64pigsty37.0 KiBpg_statecharts_18-0.2.0-1PGSTY.el9.noarch.rpm
pg_statecharts_180.2.0el9.aarch64pigsty37.0 KiBpg_statecharts_18-0.2.0-1PGSTY.el9.noarch.rpm
pg_statecharts_180.2.0el10.x86_64pigsty37.2 KiBpg_statecharts_18-0.2.0-1PGSTY.el10.noarch.rpm
pg_statecharts_180.2.0el10.aarch64pigsty37.2 KiBpg_statecharts_18-0.2.0-1PGSTY.el10.noarch.rpm
postgresql-18-pg-statecharts0.2.0d12.x86_64pigsty31.1 KiBpostgresql-18-pg-statecharts_0.2.0-1PGSTY~bookworm_all.deb
postgresql-18-pg-statecharts0.2.0d12.aarch64pigsty31.1 KiBpostgresql-18-pg-statecharts_0.2.0-1PGSTY~bookworm_all.deb
postgresql-18-pg-statecharts0.2.0d13.x86_64pigsty31.1 KiBpostgresql-18-pg-statecharts_0.2.0-1PGSTY~trixie_all.deb
postgresql-18-pg-statecharts0.2.0d13.aarch64pigsty31.1 KiBpostgresql-18-pg-statecharts_0.2.0-1PGSTY~trixie_all.deb
postgresql-18-pg-statecharts0.2.0u22.x86_64pigsty31.9 KiBpostgresql-18-pg-statecharts_0.2.0-1PGSTY~jammy_all.deb
postgresql-18-pg-statecharts0.2.0u22.aarch64pigsty31.9 KiBpostgresql-18-pg-statecharts_0.2.0-1PGSTY~jammy_all.deb
postgresql-18-pg-statecharts0.2.0u24.x86_64pigsty31.6 KiBpostgresql-18-pg-statecharts_0.2.0-1PGSTY~noble_all.deb
postgresql-18-pg-statecharts0.2.0u24.aarch64pigsty31.6 KiBpostgresql-18-pg-statecharts_0.2.0-1PGSTY~noble_all.deb
postgresql-18-pg-statecharts0.2.0u26.x86_64pigsty31.6 KiBpostgresql-18-pg-statecharts_0.2.0-1PGSTY~resolute_all.deb
postgresql-18-pg-statecharts0.2.0u26.aarch64pigsty31.6 KiBpostgresql-18-pg-statecharts_0.2.0-1PGSTY~resolute_all.deb
PackageVersionOSORGSIZEFile URL
pg_statecharts_170.1.0el8.x86_64pigsty31.8 KiBpg_statecharts_17-0.1.0-1PGSTY.el8.noarch.rpm
pg_statecharts_170.1.0el8.aarch64pigsty31.8 KiBpg_statecharts_17-0.1.0-1PGSTY.el8.noarch.rpm
pg_statecharts_170.2.0el9.x86_64pigsty37.0 KiBpg_statecharts_17-0.2.0-1PGSTY.el9.noarch.rpm
pg_statecharts_170.2.0el9.aarch64pigsty37.0 KiBpg_statecharts_17-0.2.0-1PGSTY.el9.noarch.rpm
pg_statecharts_170.2.0el10.x86_64pigsty37.2 KiBpg_statecharts_17-0.2.0-1PGSTY.el10.noarch.rpm
pg_statecharts_170.2.0el10.aarch64pigsty37.2 KiBpg_statecharts_17-0.2.0-1PGSTY.el10.noarch.rpm
postgresql-17-pg-statecharts0.2.0d12.x86_64pigsty31.1 KiBpostgresql-17-pg-statecharts_0.2.0-1PGSTY~bookworm_all.deb
postgresql-17-pg-statecharts0.2.0d12.aarch64pigsty31.1 KiBpostgresql-17-pg-statecharts_0.2.0-1PGSTY~bookworm_all.deb
postgresql-17-pg-statecharts0.2.0d13.x86_64pigsty31.1 KiBpostgresql-17-pg-statecharts_0.2.0-1PGSTY~trixie_all.deb
postgresql-17-pg-statecharts0.2.0d13.aarch64pigsty31.1 KiBpostgresql-17-pg-statecharts_0.2.0-1PGSTY~trixie_all.deb
postgresql-17-pg-statecharts0.2.0u22.x86_64pigsty31.9 KiBpostgresql-17-pg-statecharts_0.2.0-1PGSTY~jammy_all.deb
postgresql-17-pg-statecharts0.2.0u22.aarch64pigsty31.9 KiBpostgresql-17-pg-statecharts_0.2.0-1PGSTY~jammy_all.deb
postgresql-17-pg-statecharts0.2.0u24.x86_64pigsty31.6 KiBpostgresql-17-pg-statecharts_0.2.0-1PGSTY~noble_all.deb
postgresql-17-pg-statecharts0.2.0u24.aarch64pigsty31.6 KiBpostgresql-17-pg-statecharts_0.2.0-1PGSTY~noble_all.deb
postgresql-17-pg-statecharts0.2.0u26.x86_64pigsty31.6 KiBpostgresql-17-pg-statecharts_0.2.0-1PGSTY~resolute_all.deb
postgresql-17-pg-statecharts0.2.0u26.aarch64pigsty31.6 KiBpostgresql-17-pg-statecharts_0.2.0-1PGSTY~resolute_all.deb
PackageVersionOSORGSIZEFile URL
pg_statecharts_160.1.0el8.x86_64pigsty31.8 KiBpg_statecharts_16-0.1.0-1PGSTY.el8.noarch.rpm
pg_statecharts_160.1.0el8.aarch64pigsty31.8 KiBpg_statecharts_16-0.1.0-1PGSTY.el8.noarch.rpm
pg_statecharts_160.2.0el9.x86_64pigsty37.0 KiBpg_statecharts_16-0.2.0-1PGSTY.el9.noarch.rpm
pg_statecharts_160.2.0el9.aarch64pigsty37.0 KiBpg_statecharts_16-0.2.0-1PGSTY.el9.noarch.rpm
pg_statecharts_160.2.0el10.x86_64pigsty37.2 KiBpg_statecharts_16-0.2.0-1PGSTY.el10.noarch.rpm
pg_statecharts_160.2.0el10.aarch64pigsty37.2 KiBpg_statecharts_16-0.2.0-1PGSTY.el10.noarch.rpm
postgresql-16-pg-statecharts0.2.0d12.x86_64pigsty31.1 KiBpostgresql-16-pg-statecharts_0.2.0-1PGSTY~bookworm_all.deb
postgresql-16-pg-statecharts0.2.0d12.aarch64pigsty31.1 KiBpostgresql-16-pg-statecharts_0.2.0-1PGSTY~bookworm_all.deb
postgresql-16-pg-statecharts0.2.0d13.x86_64pigsty31.1 KiBpostgresql-16-pg-statecharts_0.2.0-1PGSTY~trixie_all.deb
postgresql-16-pg-statecharts0.2.0d13.aarch64pigsty31.1 KiBpostgresql-16-pg-statecharts_0.2.0-1PGSTY~trixie_all.deb
postgresql-16-pg-statecharts0.2.0u22.x86_64pigsty31.9 KiBpostgresql-16-pg-statecharts_0.2.0-1PGSTY~jammy_all.deb
postgresql-16-pg-statecharts0.2.0u22.aarch64pigsty31.9 KiBpostgresql-16-pg-statecharts_0.2.0-1PGSTY~jammy_all.deb
postgresql-16-pg-statecharts0.2.0u24.x86_64pigsty31.6 KiBpostgresql-16-pg-statecharts_0.2.0-1PGSTY~noble_all.deb
postgresql-16-pg-statecharts0.2.0u24.aarch64pigsty31.6 KiBpostgresql-16-pg-statecharts_0.2.0-1PGSTY~noble_all.deb
postgresql-16-pg-statecharts0.2.0u26.x86_64pigsty31.6 KiBpostgresql-16-pg-statecharts_0.2.0-1PGSTY~resolute_all.deb
postgresql-16-pg-statecharts0.2.0u26.aarch64pigsty31.6 KiBpostgresql-16-pg-statecharts_0.2.0-1PGSTY~resolute_all.deb

Source

pig build pkg pg_statecharts;		# build rpm/deb

Install

Make sure PGDG and PIGSTY repo available:

pig repo add pgsql -u   # add both repo and update cache

Install this extension with pig:

pig install pg_statecharts;		# install by package name

pig install pg_statecharts -v 19;   # install for PG 19
pig install pg_statecharts -v 18;   # install for PG 18
pig install pg_statecharts -v 17;   # install for PG 17
pig install pg_statecharts -v 16;   # install for PG 16

Create this extension with:

CREATE EXTENSION pg_statecharts CASCADE; -- requires ltree

Usage

Sources:

pg_statecharts 0.2.0 runs hierarchical state machines and their events transactionally inside PostgreSQL. This release is pure SQL, tested on PostgreSQL 16–18, and needs no preload or restart.

Core Workflow

Install ltree in public, then install the extension with a role that has the required object-creation privileges:

CREATE EXTENSION IF NOT EXISTS ltree WITH SCHEMA public;
CREATE EXTENSION pg_statecharts;

Deploy a statechart definition through SQL migrations or the development companion before starting a machine. For an already deployed chart named order, this starts its latest version and returns the machine row:

SELECT * FROM fsm.start_machine_with_latest_statechart(statechart_name => 'order', initial_data => '{}', shard_id => 1);

Events are inserted into fsm.state_machine_event with the shard, machine identifier, event name and JSON data. Its triggers process the event in the inserting transaction, including entry and exit callbacks. Callers must have the appropriate table and function privileges; chart callbacks are application code that needs its own review.

Catalog and Versions

Definitions and running machines live in fsm. fsm.to_semver parses numeric chart versions and fsm.semver_text renders them; stored versions are integer arrays so their native ordering remains numeric. Do not sort a textual rendering when version precedence matters.

Backups and Legacy Migration

The reviewed 0.1.0 source registers state tables and sequences for database dumps. The event inbox is intentionally omitted to avoid replaying callbacks during restoration. Backups made with 0.0.0 omitted machine state; take a fresh, restore-tested backup after upgrading.

An earlier 0.1.0 installation may predate dump registration, so check the installed catalog instead of trusting its version string. Do not drop a populated extension merely to refresh registration. Legacy Rust or Sqitch migrations require superuser work and the old semver dependency during conversion; follow the migration guide and rehearse on a restored copy. The old 0.0.0 path is no longer independently exercised upstream.

Version 0.2.0 Call Migration

Install matching SQL files and run ALTER EXTENSION pg_statecharts UPDATE;. Most functions now put optional shard_id last, defaulting to 1. Use the current named arguments: shard_id, machine_id, statechart_id, statechart_name, event_name and state_id. Deprecated names still resolve in 0.2.0, but are scheduled for removal in 0.3.0.

Audit positional calls before upgrading. In particular, fsm.create_machine(1, 42) and fsm.handle_machine_events(1, 42) still resolve but now interpret their two bigint arguments in the opposite order. Other changed signatures can break dependent objects or explicit grants; rehearse on a restored database. The release also fixes active-state checks returning states already exited and avoids rereading the event inbox during event handling.

Table reader