Files
supabase/packages/pg-meta/test/db/stress-catalog.ts
Joshen Lim f87de2f0d0 Joshenlim/fe 4593 cmd k related resources while in table editor (#53079)
## Context

Building on towards resource-aware Cmd K, the changes done up so far
have all been to allow users to search for database resources from
anywhere in the project, with the intention to get you to the resource
quickly without having to go through the navigation hierarchy of the
dashboard.

This PR explores a way to quickly glance related resources for a table
in the Table Editor through Cmd K for that same intention, but listing
out the policies, triggers or functions that run on the table that
you're currently viewing in the Table Editor. E.g here we've got the
`blog_authors` table opened:
<img width="500" alt="image"
src="https://github.com/user-attachments/assets/78c9b85f-cd30-4564-a264-c1a2a596d5c2"
/>

Policies and triggers are fairly straight forward since we can determine
what's related by their target table. Functions are a bit more fuzzy as
we're trying to find what's related by checking the function definition,
if it does anything to the table. For e.g based on the screenshot above,
`blog_handle_new_user` has this definition which was why it was flagged
<img width="500" alt="image"
src="https://github.com/user-attachments/assets/fb8f3be4-05df-4d87-8440-8b75d95546ae"
/>

We're also deliberately decoupling loading of policies + triggers and
functions since searching functions takes slightly longer, so opting to
load results of policies + triggers first, then lazy load functions
subsequently once results are ready

 ## Performance

As usual, ran a benchmarking session with Claude just to make sure that
the added SQL doesn't cause any performance issues, these are aggregates
of 4–5 runs.

**Catalog size (stress catalog)**

| Query | 2,000 tables | 10,000 tables |
|---|---|---|
| Policies and triggers, hub table (many FKs) | 0.1 ms | 0.5–1.3 ms |
| Policies and triggers, ordinary table | ~0 ms | ~0 ms |
| Functions, every body matches | 1.4 ms | 4–8 ms |
| Functions, no match (full scan) | 1.8 ms | 7.3–7.7 ms |
| Functions, table not visible | 0 ms | 0 ms |

**Large function bodies** (random text, 2,000 tables plus 2,000 small
functions, plus the large ones added)

| Function source | Largest function | Functions: no match (full scan) |
Functions: every body matches |
|---|---|---|---|
| 56 MB (600 functions) | 94 KB | ~50 ms | 2 ms |
| 103 MB (+10 multi-MB) | 4.7 MB | ~92 ms | 2 ms |

## To test

Use a project with a table that has at least one policy, one trigger,
and a function that mentions the table in its body.

- [ ] Open that table in the Table Editor and press Cmd+K. A **Related
resources** section appears at the top with its policies, triggers, and
functions.
- [ ] Click a policy, a trigger, and a function. Each opens its editor
on the right resource.
- [ ] A trigger's function does not also appear under the functions
rows.
- [ ] Type part of one of those names. The section narrows to matching
rows and the project-wide entity results appear below. A resource can
appear in both.
- [ ] Type something that matches nothing in the table. The section
disappears rather than showing empty.
- [ ] Clear the input. The full section comes back without a loading
flicker, since the data is cached.
- [ ] Open the table editor on a table with no policies, triggers, or
referencing functions. Cmd+K shows no empty **Related resources**
section.
- [ ] Switch to a different table and press Cmd+K. The section shows
that table's resources, not the previous table's.
- [ ] Open Cmd+K from a non-Table Editor page, such as the SQL Editor.
The section is not shown.
- [ ] In the network tab, check that opening the menu sends one query
for policies and triggers and one for functions. Typing sends no
additional ones for this section.
2026-11-10 10:46:41 +08:00

183 lines
8.4 KiB
TypeScript

/**
* 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;`)
}
/**
* Build `count` user functions plus `count` extension-owned ones, so function
* search runs against a large pg_proc. The extension functions are members of
* pgcrypto (installed in its own schema), as in real projects where extension
* functions outnumber the user's own, and share names with the user ones to
* check they are excluded.
*
* Kept out of `buildStressCatalog` on purpose: growing pg_proc/pg_depend there
* shifts the plans of unrelated guarded queries. Build it on top of the stress
* catalog in its own database, so the rest of the query sees realistic stats.
*/
export async function buildStressFunctions(
db: TestDatabase,
count: number = STRESS_TABLE_COUNT
): Promise<void> {
await db.executeQuery(`
create schema if not exists stress;
create schema stress_ext;
create extension pgcrypto schema stress_ext;
create procedure stress.build_functions(n int) language plpgsql as $$
begin
for i in 0..n-1 loop
execute format('create function stress.fn_%s(a int) returns int language sql as ''select a -- reads stress.t_0''', i);
execute format('create function stress_ext.fn_%s() returns int language sql as ''select 1''', i);
execute format('alter extension pgcrypto add function stress_ext.fn_%s()', i);
if i % 100 = 99 then commit; end if;
end loop;
end $$;
`)
await db.executeQuery(`call stress.build_functions(${count});`)
await db.executeQuery(`analyze;`)
}
/**
* Build a user trigger on every 4th stress table, plus triggers that run one
* shared function on every 24th, so trigger search runs against
* a large pg_trigger. Foreign keys in the stress catalog already contribute the
* internal RI triggers a real catalog has, which the search must skip. Needs the
* stress tables, so call it after `buildStressCatalog`, and keep it in the same
* separate database as `buildStressFunctions` for the same plan-shift reason.
*/
export async function buildStressTriggers(
db: TestDatabase,
tableCount: number = STRESS_TABLE_COUNT
): Promise<void> {
await db.executeQuery(`
create function stress.trg() returns trigger language plpgsql as 'begin return new; end';
create function stress.audit_row_fn() returns trigger language plpgsql as 'begin return new; end';
create procedure stress.build_triggers(n int) language plpgsql as $$
begin
for i in 0..n-1 loop
if i % 4 = 0 then
execute format('create trigger trg_%s before insert on stress.t_%s for each row execute function stress.trg()', i, i);
end if;
-- Many triggers that run one function, with names that never match its name
if i % 24 = 0 then
execute format('create trigger aud_%s before update on stress.t_%s for each row execute function stress.audit_row_fn()', i, i);
end if;
if i % 100 = 99 then commit; end if;
end loop;
end $$;
`)
await db.executeQuery(`call stress.build_triggers(${tableCount});`)
await db.executeQuery(`analyze;`)
}