The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.
TypeScript aialternativeauthdatabasedenoembeddingsexamplefirebasenextjsoauth2pgvectorpostgispostgrespostgresqlpostgrestrealtimesupabasevectorswebsockets
/**
* Synthetic large-catalog builder shared by the catalog plan-guard tests.
*
* Context: a real production catalog had ~267K pg_class rows. Unscoped catalog
* scans in Studio introspection queries turned into O(catalog) seq scans on
* every dashboard open. To guard against that we build a large synthetic
* catalog in a `stress` schema and assert query plans stay scoped at scale.
*
* `TABLE_COUNT` defaults to 2000 to keep CI fast. Crank `PG_META_STRESS_TABLES`
* up (e.g. 12000+) for local investigation closer to real incident scale.
*/
import type { createTestDatabase } from './utils'
type TestDatabase = Awaited<ReturnType<typeof createTestDatabase>>
/** Default number of tables to build; override with PG_META_STRESS_TABLES. */
export const DEFAULT_STRESS_TABLE_COUNT = 2000
/** Resolved table count, honoring the PG_META_STRESS_TABLES env override. */
export const STRESS_TABLE_COUNT = Number(
process.env.PG_META_STRESS_TABLES ?? DEFAULT_STRESS_TABLE_COUNT
)
/**
* Build a synthetic `stress` schema with `tableCount` tables plus a handful of
* non-table relations (a view, a materialized view, and a partitioned table
* with one partition) so relkind-filtering queries have something to hit.
*
* Per table: a composite PK index, a unique constraint+index, a check
* constraint, and a composite FK to the previous table. Every 10th table also
* has a composite FK to t_0, making t_0 a hub with many incoming FKs (the
* structurally unavoidable pg_constraint seq scan, since there is no index on
* pg_constraint.confrelid). Using composite FKs exercises ordinal column
* pairing across the full stress catalog. Every other table also has RLS
* enabled with one policy, so pg_policy scales with the catalog.
*
* The catalog is built via a server-side procedure with batched commits every
* 100 tables -- a single transaction creating thousands of tables/indexes/
* constraints would exhaust the lock table. `analyze` runs at the end so the
* planner has real statistics.
*/
export async function buildStressCatalog(
db: TestDatabase,
tableCount: number = STRESS_TABLE_COUNT
): Promise<void> {
await db.executeQuery(`
create schema stress;
create procedure stress.build(n int) language plpgsql as $$
begin
for i in 0..n-1 loop
execute format(
'create table stress.t_%s (id int, tenant_id int, u int unique, c int check (c > 0), primary key (id, tenant_id)%s%s)',
i,
case when i > 0 then format(', fk_id int, fk_tenant_id int, foreign key (fk_id, fk_tenant_id) references stress.t_%s(id, tenant_id)', i - 1) else '' end,
case when i > 0 and i % 10 = 0 then ', hub_id int, hub_tenant_id int, foreign key (hub_id, hub_tenant_id) references stress.t_0(id, tenant_id)' else '' end
);
-- Every other table gets RLS + a policy so pg_policy grows with the
-- catalog (as it does in real projects) and planners don't treat it
-- as an empty table.
if i % 2 = 0 then
execute format('alter table stress.t_%s enable row level security', i);
execute format('create policy p_%s on stress.t_%s for select using (true)', i, i);
end if;
if i % 100 = 99 then commit; end if;
end loop;
end $$;
`)
// `call` performs commits internally, so it must run as its own statement: a
// pg client query is autocommit by default, but bundling it with other
// statements in one multi-statement message implicitly wraps the whole
// message in one transaction, which conflicts with the commits inside the
// procedure's loop.
await db.executeQuery(`call stress.build(${tableCount});`)
// A view, a materialized view, and a partitioned table (with one partition)
// so queries that filter by relkind (views, matviews, partitioned tables)
// exercise a non-empty result at scale.
await db.executeQuery(`
create view stress.v_hub as select id, u, c from stress.t_0;
create materialized view stress.mv_hub as select id, u, c from stress.t_0;
create table stress.p_root (id int not null, region text not null, primary key (id, region))
partition by list (region);
create table stress.p_root_east partition of stress.p_root for values in ('east');
`)
// Enums and composite types so the user-defined-types introspection query has
// non-trivial results at scale: multi-label enums with a deliberately
// NON-alphabetical label order (to exercise the enumsortorder ordering), plus
// composite types -- one of which has a dropped attribute (to exercise the
// `not attisdropped` filter). A bulk loop grows pg_type/pg_attribute/pg_enum
// so the scoped correlated subqueries are exercised against a large catalog.
await db.executeQuery(`
create type stress.mood as enum ('ecstatic', 'sad', 'happy', 'ok');
create type stress.priority as enum ('critical', 'low', 'high', 'medium');
create type stress.addr as (street text, city text, zip int);
create type stress.pair as (a int, dropme text, b text);
alter type stress.pair drop attribute dropme;
create procedure stress.build_types(n int) language plpgsql as $$
begin
for i in 0..n-1 loop
execute format('create type stress.e_%s as enum (''c'', ''a'', ''b'')', i);
execute format('create type stress.ct_%s as (x int, y text)', i);
if i % 100 = 99 then commit; end if;
end loop;
end $$;
`)
await db.executeQuery(`call stress.build_types(200);`)
await db.executeQuery(`analyze;`)
}