mirror of
https://github.com/supabase/supabase.git
synced 2026-12-04 15:39:13 +00:00
## 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.
241 lines
8.3 KiB
TypeScript
241 lines
8.3 KiB
TypeScript
import { afterAll, expect, test } from 'vitest'
|
|
|
|
import {
|
|
getEntitiesByIdsSQL,
|
|
getEntityTypesSQL,
|
|
getPartitionCountSQL,
|
|
getPartitionInfoSQL,
|
|
} from '../../../src'
|
|
import { cleanupRoot, createTestDatabase } from '../../db/utils'
|
|
|
|
afterAll(async () => {
|
|
await cleanupRoot()
|
|
})
|
|
|
|
type TestDatabase = Awaited<ReturnType<typeof createTestDatabase>>
|
|
|
|
const withTestDatabase = (name: string, fn: (db: TestDatabase) => Promise<void>) => {
|
|
test(name, async () => {
|
|
const db = await createTestDatabase()
|
|
try {
|
|
await fn(db)
|
|
} finally {
|
|
await db.cleanup()
|
|
}
|
|
})
|
|
}
|
|
|
|
type EntityTypesRow = {
|
|
data: {
|
|
entities: Array<{ name: string; type: string; is_partition: boolean }>
|
|
count: number
|
|
}
|
|
}
|
|
|
|
const listEntities = async (
|
|
db: TestDatabase,
|
|
namePrefix: string,
|
|
{ excludePartitions = false, parentId }: { excludePartitions?: boolean; parentId?: number } = {}
|
|
) => {
|
|
const [row] = await db.executeQuery<EntityTypesRow[]>(
|
|
getEntityTypesSQL({
|
|
schemas: ['public'],
|
|
sort: 'alphabetical',
|
|
filterTypes: ['r', 'v', 'm', 'f', 'p'],
|
|
excludePartitions,
|
|
parentId,
|
|
search: namePrefix,
|
|
limit: 100,
|
|
page: 0,
|
|
})
|
|
)
|
|
return row.data.entities.map((e) => `${e.name} ${e.type}${e.is_partition ? ' partition' : ''}`)
|
|
}
|
|
|
|
withTestDatabase('flags partitions, which are ordinary tables in the catalog', async (db) => {
|
|
await db.executeQuery(`
|
|
create table public.shade_plain (id int);
|
|
create table public.shade_parted (id int, hue int not null, primary key (id, hue))
|
|
partition by range (hue);
|
|
create table public.shade_parted_low partition of public.shade_parted
|
|
for values from (0) to (180);
|
|
create table public.shade_parted_rest partition of public.shade_parted default;
|
|
`)
|
|
|
|
// The parent reports as a partitioned table; its children report as tables,
|
|
// so is_partition is the only thing separating them from shade_plain
|
|
expect(await listEntities(db, 'shade_')).toEqual([
|
|
'shade_parted p',
|
|
'shade_parted_low r partition',
|
|
'shade_parted_rest r partition',
|
|
'shade_plain r',
|
|
])
|
|
})
|
|
|
|
withTestDatabase('does not flag non-table relations as partitions', async (db) => {
|
|
await db.executeQuery(`
|
|
create table public.tone_base (id int);
|
|
create view public.tone_view as select * from public.tone_base;
|
|
create materialized view public.tone_matview as select * from public.tone_base;
|
|
`)
|
|
|
|
expect(await listEntities(db, 'tone_')).toEqual(['tone_base r', 'tone_matview m', 'tone_view v'])
|
|
})
|
|
|
|
const oidOf = async (db: TestDatabase, name: string) => {
|
|
const [{ oid }] = await db.executeQuery<{ oid: number }[]>(
|
|
`select '${name}'::regclass::oid::int as oid`
|
|
)
|
|
return oid
|
|
}
|
|
|
|
withTestDatabase('excludePartitions hides child partitions at every depth', async (db) => {
|
|
await db.executeQuery(`
|
|
create table public.band_plain (id int);
|
|
create table public.band_parted (id int, hue int not null) partition by range (hue);
|
|
create table public.band_parted_low partition of public.band_parted
|
|
for values from (0) to (180) partition by range (hue);
|
|
create table public.band_parted_low_a partition of public.band_parted_low
|
|
for values from (0) to (90);
|
|
`)
|
|
|
|
expect(await listEntities(db, 'band_', { excludePartitions: true })).toEqual([
|
|
'band_parted p',
|
|
'band_plain r',
|
|
])
|
|
})
|
|
|
|
withTestDatabase(
|
|
'lists direct partitions of a parent and links a partition back to it',
|
|
async (db) => {
|
|
await db.executeQuery(`
|
|
create table public.rank_parted (id int, hue int not null) partition by range (hue);
|
|
create table public.rank_parted_low partition of public.rank_parted
|
|
for values from (0) to (180) partition by range (hue);
|
|
create table public.rank_parted_low_a partition of public.rank_parted_low
|
|
for values from (0) to (90);
|
|
create table public.rank_parted_rest partition of public.rank_parted default;
|
|
create table public.rank_legacy_a (id int);
|
|
create table public.rank_legacy_b (id int);
|
|
create table public.rank_legacy_child () inherits (public.rank_legacy_a, public.rank_legacy_b);
|
|
`)
|
|
const parentId = await oidOf(db, 'public.rank_parted')
|
|
const lowId = await oidOf(db, 'public.rank_parted_low')
|
|
|
|
// The grandchild stays behind its sub-partitioned parent
|
|
expect(await listEntities(db, 'rank_', { parentId })).toEqual([
|
|
'rank_parted_low p partition',
|
|
'rank_parted_rest r partition',
|
|
])
|
|
|
|
const [parent] = await db.executeQuery<{ name: string; parent_id: number | null }[]>(
|
|
getPartitionInfoSQL({ id: parentId })
|
|
)
|
|
const [low] = await db.executeQuery<{ name: string; parent_id: number | null }[]>(
|
|
getPartitionInfoSQL({ id: lowId })
|
|
)
|
|
expect(parent).toEqual({ name: 'rank_parted', parent_id: null })
|
|
expect(low).toEqual({ name: 'rank_parted_low', parent_id: parentId })
|
|
|
|
// Plain inheritance (even with several parents) isn't partitioning
|
|
const [legacy] = await db.executeQuery<{ name: string; parent_id: number | null }[]>(
|
|
getPartitionInfoSQL({ id: await oidOf(db, 'public.rank_legacy_child') })
|
|
)
|
|
expect(legacy).toEqual({ name: 'rank_legacy_child', parent_id: null })
|
|
}
|
|
)
|
|
|
|
withTestDatabase(
|
|
'getPartitionCountSQL counts direct partitions only, not nested ones or the table itself',
|
|
async (db) => {
|
|
await db.executeQuery(`
|
|
create table public.depth_parted (id int, hue int not null) partition by range (hue);
|
|
create table public.depth_parted_low partition of public.depth_parted
|
|
for values from (0) to (180) partition by range (hue);
|
|
create table public.depth_parted_low_a partition of public.depth_parted_low
|
|
for values from (0) to (90);
|
|
create table public.depth_parted_rest partition of public.depth_parted default;
|
|
`)
|
|
|
|
const [row] = await db.executeQuery<{ count: number }[]>(
|
|
getPartitionCountSQL({ id: await oidOf(db, 'public.depth_parted') })
|
|
)
|
|
// Only direct children, matching what a partition list shows: no grandchildren
|
|
expect(row.count).toBe(2)
|
|
}
|
|
)
|
|
|
|
withTestDatabase('getEntitiesByIdsSQL finds partitions and ignores unknown ids', async (db) => {
|
|
await db.executeQuery(`
|
|
create table public.view_parted (id int, hue int not null) partition by range (hue);
|
|
create table public.view_parted_low partition of public.view_parted for values from (0) to (180);
|
|
`)
|
|
|
|
const rows = await db.executeQuery<{ name: string; is_partition: boolean }[]>(
|
|
getEntitiesByIdsSQL({ ids: [await oidOf(db, 'public.view_parted_low'), 999999999] })
|
|
)
|
|
expect(rows).toEqual([expect.objectContaining({ name: 'view_parted_low', is_partition: true })])
|
|
})
|
|
|
|
withTestDatabase(
|
|
'lists partitions of a parent even when they live in another schema',
|
|
async (db) => {
|
|
await db.executeQuery(`
|
|
create schema tint;
|
|
create table public.cross_parted (id int, hue int not null) partition by range (hue);
|
|
create table tint.cross_child partition of public.cross_parted for values from (0) to (10);
|
|
`)
|
|
|
|
expect(
|
|
await listEntities(db, 'cross_', { parentId: await oidOf(db, 'public.cross_parted') })
|
|
).toEqual(['cross_child r partition'])
|
|
}
|
|
)
|
|
|
|
withTestDatabase('enrich adds column count, and size and rows only for tables', async (db) => {
|
|
await db.executeQuery(`
|
|
create table public.dye_base (id int, label text);
|
|
create view public.dye_view as select id from public.dye_base;
|
|
`)
|
|
|
|
const [row] = await db.executeQuery<
|
|
Array<{
|
|
data: {
|
|
entities: Array<{
|
|
name: string
|
|
columns_count: number
|
|
size: string | null
|
|
bytes: number | null
|
|
}>
|
|
}
|
|
}>
|
|
>(
|
|
getEntityTypesSQL({
|
|
schemas: ['public'],
|
|
sort: 'alphabetical',
|
|
filterTypes: ['r', 'v'],
|
|
search: 'dye_',
|
|
limit: 100,
|
|
page: 0,
|
|
enrich: true,
|
|
})
|
|
)
|
|
|
|
const [table, view] = row.data.entities
|
|
expect(table).toMatchObject({ name: 'dye_base', columns_count: 2 })
|
|
expect(table.size).toBeTruthy()
|
|
expect(view).toMatchObject({ name: 'dye_view', columns_count: 1, size: null, bytes: null })
|
|
})
|
|
|
|
withTestDatabase('search matches % and _ literally, not as wildcards', async (db) => {
|
|
await db.executeQuery(`
|
|
create table public.lit_a_table (id int);
|
|
create table public."litXaXtable" (id int);
|
|
create table public."lit%table" (id int);
|
|
create table public.litzzzzzzzztable (id int);
|
|
`)
|
|
|
|
expect(await listEntities(db, 'lit_a_')).toEqual(['lit_a_table r'])
|
|
expect(await listEntities(db, 'lit%')).toEqual(['lit%table r'])
|
|
})
|