Feature flags
Feature flags per tenant, per user and per custom context such as a brand, with targeting rules, overrides and percentage rollouts, evaluated the same way in RLS, in an OpenFeature provider and in the Flags SDK.
The flags block stores feature flags in Postgres and evaluates them in
three places: flag_enabled() in policies and functions, an OpenFeature
provider, and a Flags SDK adapter in the app. All of them
use the same rules and the same SHA-256 bucketing, so a user who sees a
feature in the UI also passes the policy behind it.
Flags decide what a tenant or user sees while a feature rolls out. What a tenant pays for stays in entitlements; a flag rule can still target plans.
better-supabase add flags # adds tenant as well| Table | Holds |
|---|---|
flags | key, type, description, variants, default_variant, enabled, archived, rules, rollout_percentage, rollout_variants, rollout_schedule |
flag_overrides | A variant for one organization_id, one user_id or one custom context value (context, context_key) |
flag_revisions | Every state upsert_flag left, numbered per flag, with a message and who changed it |
flag_segments | Named audiences that rules reuse, each one condition |
Only the service role reads and writes these tables. Manage flags from a migration, the Studio or an admin route that runs as the service role.
Defining flags
variants maps a variant name to its value. A boolean flag has the variants
on (true) and off (false) by default:
insert into better_supabase.flags (key, rules, rollout_percentage, rollout_variants) values
('new_editor', '[{"variant": "on", "plans": ["pro"]}, {"variant": "on", "roles": ["admin"]}]', 10,
'[{"variant": "on", "weight": 100}]');
insert into better_supabase.flags (key, type, variants, default_variant) values
('checkout_theme', 'string', '{"classic": "classic", "compact": "compact"}', 'classic');
insert into better_supabase.flag_overrides (flag_key, organization_id, variant)
values ('new_editor', '8d1c...', 'on');A flag resolves in this order:
- A disabled flag (
enabled = false) or an archived one returns its default variant. - A user override, then a tenant override, then a custom context override in the order the contexts are declared.
- The first rule whose lists all contain the caller. A rule lists any of
tenants,users,roles(the caller's role in the tenant),plans(entitlement lookup keys, one match is enough),contexts(a context name to its values, every one must match) andsegments(the caller must be in one of them). - The rollout: the first 32 bits of SHA-256 of
flag.target, modulo 10000, below the rollout percentage times 100, picks a variant fromrollout_variantsby a second bucket offlag/split.targetagainst the cumulative weights. The target is the user id, or the tenant id when there is no user. - The default variant, also for a target past the last weight.
The reason in each result follows OpenFeature: DISABLED,
TARGETING_MATCH, SPLIT or DEFAULT.
Custom contexts
A custom context targets flags by a value that isn't a tenant or a user,
such as the brand a white-label app serves. Declare each one with the table
and column that hold a tenant's value; tenant names the column with the
tenant id and defaults to id, for a column on the tenant table:
blocks: {
"flags": {
options: {
contexts: {
brand: { table: "public.organizations", column: "brand_id" },
},
},
},
},Rules and overrides then name the context:
update better_supabase.flags
set rules = '[{"variant": "on", "contexts": {"brand": ["acme"]}}]'
where key = 'new_editor';
insert into better_supabase.flag_overrides (flag_key, context, context_key, variant)
values ('new_editor', 'brand', 'globex', 'off');flag_evaluation(key, tenant, user, contexts) takes the values as
{"brand": "acme"}. A declared context it leaves out is read from the
tenant, and only for a flag whose rules or overrides name a context, so
flag_enabled() in a policy targets brands without extra arguments. The
OpenFeature provider and the Flags SDK adapter never read the tenant table;
pass the values through flagContext({ contexts }), contextClaims or the
Flags SDK identify.
Weighted splits and scheduled rollouts
rollout_variants splits the rollout between variants by weight, as
percentages of the targets inside it. A second bucket picks the variant, so
raising rollout_percentage never moves a target from one variant to
another. Weights that add up to less than 100 leave the rest on the default
variant:
await db.flags.upsert("checkout_theme", {
rolloutPercentage: 50,
rolloutVariants: [
{ variant: "compact", weight: 50 },
{ variant: "dense", weight: 25 },
],
});rolloutSchedule raises the percentage on its own. Before startAt the
rollout serves no one; then each step holds its percentage for
durationSeconds, and after the last step the rollout stays at
finalPercentage (the last step's percentage when unset). The schedule
replaces rolloutPercentage while it is set; pass null to clear it:
await db.flags.upsert("new_editor", {
rolloutVariants: [{ variant: "on", weight: 100 }],
rolloutSchedule: {
startAt: "2026-11-02T09:00:00Z",
steps: [
{ percentage: 5, durationSeconds: 86_400 },
{ percentage: 25, durationSeconds: 86_400 },
],
finalPercentage: 100,
},
});SQL and TypeScript compute the same percentage at the same time:
flag_rollout_percentage(percentage, schedule, at) and
rolloutPercentageAt(schedule, nowMs).
Segments
A segment names an audience once, such as the beta testers or the staff of
your largest customers, and rules reuse it by name. Each segment is one
condition with the same lists a rule has; a rule with segments matches a
caller in any of them, and its own lists must match as well. A rule that
names a missing segment matches no one through it:
await db.flags.saveSegment({
name: "beta-testers",
description: "Opted in from settings",
users: [ana.id, ben.id],
});
await db.flags.upsert("new_editor", {
rules: [{ variant: "on", segments: ["beta-testers"] }],
});Changing a segment changes every flag that names it at once.
flag_definitions() returns the segments a flag's rules name, so the
OpenFeature provider evaluates them without another query.
History and restore
Each upsert_flag records a revision: the flag as it was left, a message and
the user who saved it. history(key) lists them newest first, and
restore(key, revision) saves that revision again as a new one, so a
restore is itself in the history:
await db.flags.upsert(
"new_editor",
{ rolloutPercentage: 50 },
{ message: "Widen to half after the error rate held" },
);
const [latest, previous] = await db.flags.history("new_editor").orThrow();
await db.flags.restore("new_editor", previous.revision);Overrides are not part of a revision. A deleted flag keeps its revisions,
and restore brings it back.
Archiving a flag
upsert(key, { archived: true }) archives a flag: it serves its default
variant like a disabled flag, and Flags Explorer no longer lists it. Archive
a flag once no code reads it, and delete it later, so a deploy that still
checks it gets the default instead of an unknown flag.
Managing flags from an admin page
createFlags manages flags for platform staff with flags.manage (the
module's manage permission, checked with is_platform() when the
access module is installed) or the service role. It
calls list_flags, upsert_flag, delete_flag, set_flag_override,
flag_history, restore_flag and the segment functions,
which are granted to authenticated, so an app that only reaches the
database through the Data API manages flags with blocks.flags.api
and rpcTransport:
Server code calls the flags instance on db, in the caller's organization. With gen.hooks set, better-supabase gen writes the hooks:
export const { useList: useFlags } = defineDataHooks(
"flags",
(db: AppDb) => db.flags,
);import { createFlags, rpcTransport } from "better-supabase/blocks/flags";
await db.flags.upsert("new_editor", {
rolloutPercentage: 25,
rolloutVariants: [{ variant: "on", weight: 100 }],
});
await db.flags.override("new_editor", { organizationId }, "on");
await db.flags.override(
"new_editor",
{ context: "brand", contextKey: "acme" },
"on",
);
await db.flags.override("new_editor", { userId }, null); // removes it
const flags = await db.flags.list().orThrow();"use client";
import { useFlags } from "@/lib/supabase/hooks";
const rows = useFlags({ limit: 50 });upsert creates the flag or updates the fields you pass and keeps the rest.
Anyone else gets FLAGS_FORBIDDEN.
Your own fields and hooks
Columns you add to flags with alter table in your own schema files are fields of the block (details):, such as
an owner or a ticket. upsert writes them from fields, and list, upsert
and flag_definitions() return them under fields. Pass a Standard Schema
as fields to type them, parse them on reads and validate them on upsert,
and hooks to run code around a method (see
extending blocks):
import { z } from "zod";
const admin = createFlags({
transport: rpcTransport(supabase, { schema: "api" }),
fields: z.object({ owner: z.string().email() }),
hooks: {
upsert: {
after: (result, [key]) => console.info("flag saved", key, result.ok),
},
},
});
await admin.upsert("new_editor", { fields: { owner: "ana@acme.test" } });Owner and expiry
Give every flag an owner and a date by which it should be gone, so release flags don't pile up. Add the columns as extra columns:
alter table "better_supabase"."flags" add column "owner" text;
alter table "better_supabase"."flags" add column "expires_at" timestamptz;With an expires_at column, better-supabase doctor
reads the live database and warns about each flag past it that isn't
archived (BS336), naming the owner when the table has one. Remove the
flag's checks from the code, then archive it, or move expires_at when the
flag stays. expiredFlagsQuery(layout, installed) from
better-supabase/sql returns the query doctor runs, for your own report.
In policies
flag_enabled(key, tenant) is true when the flag resolves to true for the
caller in that tenant:
create policy "drafts_insert" on public.drafts for insert to authenticated
with check (better_supabase.flag_enabled('new_editor', organization_id));flag_enabled evaluates the flag for each row it checks. For a policy that
reads many rows, tenant_ids_with_flag(key) returns the caller's tenants
where the flag is on, and Postgres evaluates it once per statement:
create policy "drafts_read" on public.drafts for select to authenticated
using (organization_id in (select better_supabase.tenant_ids_with_flag('new_editor')));flag_evaluation(key, tenant, user) returns { value, variant, reason } for
any flag type, or null for an unknown flag. It is granted to the service role.
With OpenFeature
createFlagsProvider returns an object shaped like an OpenFeature server
Provider. It loads every flag with flag_definitions() (service role only)
and caches them for ttl milliseconds (default 30000). For staleTtl
milliseconds after that (default 300000) it serves the previous definitions
while one reload runs behind the evaluation, so no request waits on the Data
API once the cache is warm; staleTtl: 0 waits for every reload. This package never
imports OpenFeature; pass its ErrorCode enum so the types match:
import { ErrorCode, OpenFeature } from "@openfeature/server-sdk";
import {
createFlagsProvider,
sqlTransport,
} from "better-supabase/blocks/flags";
const provider = createFlagsProvider({
transport: sqlTransport(postgres.admin),
errorCodes: ErrorCode,
});
await OpenFeature.setProviderAndWait(provider);
export const flags = OpenFeature.getClient();A row whose type is not one the table allows resolves to the default
with PARSE_ERROR, or GENERAL when the errorCodes map has no
PARSE_ERROR.
flagContext(ctx) builds the evaluation context from verified claims: the
user id as targetingKey, the tenant (tenant_id, then
app_metadata.tenant_id), its plan features from the features claim and
the caller's role from the memberships claim:
import { flagContext } from "better-supabase/blocks/flags";
const details = await flags.getBooleanDetails(
"new_editor",
false,
flagContext({ jwtClaims: ctx.jwtClaims }),
);The memberships claim can have either shape: an object of tenant id to
role, as the memberships module's hook writes, or a list of
{ scope, id, roles } entries, as an authorization provider's hook may
write. For a list,
flagContext reads the entry whose id is the tenant and that has no
within (the root scope), or the entry of membershipScope when you set it.
A caller with several roles gets the first as role and all of them as
roles, and a rule's roles list matches any of them. For a claim of
another shape, pass roles: (claims, tenant) => ....
flagContext({ jwtClaims: ctx.jwtClaims }, { membershipScope: "organization" });Without the OpenFeature SDK, createFlagClient({ transport, errorCodes })
returns the four get*Details methods that withOpenFeature calls (see
middleware). evaluateFlag and
flagBucket are exported for tests and for flags defined in code
(createFlagsProvider({ definitions })).
Reference
| Export | Does |
|---|---|
createFlagsProvider(opts) | An OpenFeature-shaped provider over flag_definitions(), or fixed definitions; definitions() returns the cached list; onEvaluate sees each result |
createFlagClient(source) | getBooleanDetails, getStringDetails, getNumberDetails, getObjectDetails |
flagContext(ctx, opts?) | The evaluation context from jwtClaims; claim names are options |
evaluateFlag(flag, ctx, opts?) | { value, variant, reason }, as flag_evaluation() computes it; opts.now sets the time a schedule is read at |
flagBucket(key, target) | The rollout bucket, 0 to 9999 |
rolloutPercentageAt(schedule, now) | The percentage a rollout schedule serves at a time, as flag_rollout_percentage() computes it |
chooseSplitVariant(bucket, splits) | The variant a split bucket falls in, or undefined past the last weight |
expandSegments(rule, segments) | The conditions a rule's segments name; unknown names drop out |
createFlags(opts) | list, upsert, remove, override, history, restore, segments, saveSegment and removeSegment for platform staff or the service role |
createFlagsProvider takes onEvaluate(key, value, details), called after
each evaluation, for analytics or logs. An error it throws is ignored, so it
can't change a result.
The provider follows the OpenFeature specification pinned in
SPEC_PINS.openfeature (see standards).
Without the client
db.flags is this client. Code that does not go through bsb.db() builds it with a transport and an organization id:
import { createFlags, rpcTransport } from "better-supabase/blocks/flags";
const admin = createFlags({
transport: rpcTransport(supabase, { schema: "api" }),
});
await admin.upsert("new_editor", {
rolloutPercentage: 25,
rolloutVariants: [{ variant: "on", weight: 100 }],
});
await admin.override("new_editor", { organizationId }, "on");
await admin.override(
"new_editor",
{ context: "brand", contextKey: "acme" },
"on",
);
await admin.override("new_editor", { userId }, null); // removes it
const flags = await admin.list().orThrow();Staff helpers
isEnabled({ key, context }) answers whether a boolean flag is on for a context (tenant, user, roles and plans), evaluated like flag_evaluation(). An unknown flag is off. It reads list, so it needs the same permission.
tenantsWith({ key }) lists the organizations that have the flag on through a tenant override, for a staff view of who gets it early. A tenant that gets the flag through a rule or the rollout is not listed.
Last updated on