Files
supabase/packages/pg-meta/test/sql/studio/entities-enrich-bench.test.ts
Joshen Lim 6abbb5700d Joshenlim/fe 5274 re database tables reshuffles the (#54521)
## Context

On Database > Tables, schemas with more than 50 relations reshuffle as
you scroll.
- Tables were paged by `oid`, then the merged list was re-sorted by name
on the client
- Views, materialized views and foreign tables came from three separate
unpaginated queries and were merged into the same sorted list, so the
list was never stable across pages

## Changes involved

Am hence opting to use the same query that we fetch entities in the
Table Editor for the Database > Tables page with a few tweaks
- `TableList` now uses `getEntityTypesSQL` instead of 4 separate queries
to fetch tables, views, materialised views and partition tables
- `getEntityTypesSQL` gets a new `enrich` prop which fetches additional
data that the Database > Tables page requires (e.g column count, row
count, size, etc
- Selecting "Edit" or "Duplicate" actions in the Database > Tables page
will fetch the table's details on demand
- Also fixed searching via `getEntityTypesSQL` to escape `%` and `_` in
`search` with `escapeIlikeLiteral`, otherwise searching `my_table` also
matched `myXtable`

## Performance

As with every change to SQL queries in the dashboard, we benchmark the
performance to ensure that the dashboard never runs a heavy query
against the project's DB. This just benchmarks `enrich` off vs on with
`EXPLAIN ANALYZE` on synthetic schemas of 500, 2,000 and 8,000 tables.
Execution time, median of 5 warm runs:

| Schema tables | limit 50 | limit 200 |
|---|---|---|
| 500 | +7.6 ms | +35.2 ms |
| 2,000 | +8.6 ms | +34.9 ms |
| 8,000 | +8.0 ms | +36.0 ms |

The extra cost is about 0.16 ms per row, the same at every schema size,
and the same on deep pages. It follows the page size, not the schema
size.

## To test

Create more than 50 relations in a schema (tables, views, materialized
views, a partitioned table), then on Database > Tables:

- [ ] Scroll through the list. New rows append at the bottom in
alphabetical order, with views mixed in.
- [ ] Untick and tick entity types. The list filters correctly, and
unticking all shows the empty state.
- [ ] Search `alph_` returns nothing when no name contains `alph_`, and
`zz_alpha` finds the matching rows.
- [ ] Create, edit, duplicate and delete a table. The list refreshes.
- [ ] Table Editor sidebar still lists and searches as before.
2026-12-04 14:49:17 +07:00

71 lines
2.1 KiB
TypeScript

import { afterAll, expect, test } from 'vitest'
import { getEntityTypesSQL } from '../../../src'
import { explainAnalyze } from '../../db/plan-guard'
import { buildStressCatalog } from '../../db/stress-catalog'
import { cleanupRoot, createTestDatabase } from '../../db/utils'
/**
* Benchmark: does `enrich` cost scale with the page size or with the schema size?
* Only runs when PG_META_BENCH=1, e.g.
* PG_META_BENCH=1 PG_META_BENCH_SIZES=500,2000,8000 pnpm vitest run entities-enrich-bench
*/
const RUN = process.env.PG_META_BENCH === '1'
const SIZES = (process.env.PG_META_BENCH_SIZES ?? '500,2000,8000').split(',').map(Number)
const RUNS = 5
afterAll(async () => {
await cleanupRoot()
})
const median = (xs: number[]) => [...xs].sort((a, b) => a - b)[Math.floor(xs.length / 2)]
test.skipIf(!RUN).each(SIZES)(
'enrich cost vs schema size (%i tables)',
async (tableCount) => {
const db = await createTestDatabase()
try {
await buildStressCatalog(db, tableCount)
await db.executeQuery('analyze;')
const time = async (limit: number, page: number, enrich: boolean) => {
const sql = getEntityTypesSQL({
schemas: ['stress'],
sort: 'alphabetical',
filterTypes: ['r', 'v', 'm', 'f', 'p'],
limit,
page,
enrich,
})
await explainAnalyze(db, sql) // warm
const runs: number[] = []
for (let i = 0; i < RUNS; i++) runs.push((await explainAnalyze(db, sql)).executionTimeMs)
return median(runs)
}
const rows: Record<string, string | number>[] = []
for (const [limit, page] of [
[50, 0],
[50, 5],
[200, 0],
] as const) {
const off = await time(limit, page, false)
const on = await time(limit, page, true)
rows.push({
tables: tableCount,
limit,
page,
'off ms': +off.toFixed(1),
'on ms': +on.toFixed(1),
'delta ms': +(on - off).toFixed(1),
})
}
console.table(rows)
expect(rows.length).toBeGreaterThan(0)
} finally {
await db.cleanup()
}
},
600_000
)