Flagsmith’s flag engine, written in SQL
pgsmith is a Postgres extension that gives the same answers as Flagsmith’s SDKs, so a query or a row-level security policy can check a flag without leaving the database. This page has downloaded PostgreSQL 19, compiled to WebAssembly, and installed the pgsmith 0.1.0 release into it.
The pages of this playground
Every page talks to the same PostgreSQL. A table you create in the SQL console is still there when you open the Flags page, until you reload.
select * from flagsmith.flags('user42');
Engine testsFetches the 230 cases of Flagsmith’s engine-test-data and runs each one on both of pgsmith’s tiers. Break pgsmith with one statement and watch the cases fail, then compare it with Flagsmith’s Python engine on inputs made up on the spot.flagsmith.evaluate_context(ctx, compiled)
FlagsType an identity and its traits. Each keystroke reruns get_flags() and shows the reason Flagsmith’s engine gives for every answer, and the segments the identity falls in.flagsmith.get_flags(id, traits, env)
RolloutsDrag a percentage split and FlagShift counts which of the 10,000 users would see a different flag, and names some of them, before the change ships.flagsmith.plan(env, candidate, 'app.users')
FlagsmithLoad an environment from Flagsmith’s API, a public demo project or your own, keep it in sync every five seconds, and check 50 users against the answers Flagsmith’s API gives.GET /api/v1/environment-document/
Putting pgsmith on your own database
-
Install the release
On Supabase and Amazon RDS, pipe the release’s pg_tle installer into psql. On any other PostgreSQL from 14 to 18, build the extension and run CREATE EXTENSION.
Supabase, Amazon RDS
curl -sL \ https://github.com/SriPrarabdha/pgsmith/releases/download/v0.1.0/pgsmith--0.1.0.pgtle.sql \ | psql "$DATABASE_URL"
Any PostgreSQL 14 to 18
CREATE EXTENSION pgsmith; -
Point it at Flagsmith
A small sync service listens for Flagsmith’s webhooks. On Supabase you can skip the service: pg_cron fetches the environment document from inside the database, with the key kept in Vault.
In the database, on Supabase
select vault.create_secret('ser.…', 'flagsmith'); select flagsmith.add_remote_source('production', 'https://flagsmith.example/api/v1/', 'flagsmith'); select flagsmith.schedule_sync('30 seconds');
-
Read flags in SQL
Your queries call pgsmith in the same statement that reads your tables, so checking a flag never goes over the network.
A row-level security policy
create policy offers_for_my_checkout on offers for select to authenticated using (checkout = (select my_checkout()));
What is real on this page
Most of it. The data is made up, and a few things a browser can’t do are done another way. The list fills in as the database starts.
- Database
- Starting…
- pgsmith
- The 0.1.0 release file, unchanged apart from two lines that only work inside CREATE EXTENSION.
- Test cases
- Fetched from Flagsmith/engine-test-data v3.13.2 through jsDelivr when you press Run on the Tests page. Nothing is bundled.
- Flagsmith’s engine
- The Tests page can download
flagsmith-flag-engine11.1.2 from PyPI and run it with Pyodide, Python compiled to WebAssembly. - Flagsmith’s API
- The Flagsmith page calls it straight from your browser. Your key stays in memory and goes only to the URL you type.
- Made up
- The 10,000 users in
app.users, and the Production and Staging environments, which were exported from a Flagsmith we ran locally. - Not here
- PGlite is single-user and has no background workers, so pg_cron sync can’t run. The Flagsmith page polls from the browser instead.