Files
Joshen Lim 5eb68c5df2 Add indexes to cmd k related resources in table editor (#53204)
## Context

We previously added a "Related resources" section in Cmd+K if you're on
the Table Editor with a table opened, this included resources pertaining
to the table such as policies, triggers, and functions.

We're adding indexes as well in this PR - thinking that might also be
appropriate to have in this context
<img width="500" alt="image"
src="https://github.com/user-attachments/assets/5cd383ff-6617-49ba-bdb8-6f65d8fd0680"
/>

## Performance

Benchmarked with Claude against the stress catalog
(`PG_META_STRESS_TABLES=12000`), with master and this branch timed in
the same run on the same database, median of 9.

| Table | master | This PR | Worst of 9 |
|---|---|---|---|
| Hub table — 8 indexes, policy, many FK triggers | 1.6 ms | 2.3 ms |
2.8 ms |
| Policy + user trigger | 1.0 ms | 1.3 ms | 2.0 ms |
| Mid-chain table | 1.1 ms | 1.5 ms | 1.7 ms |
| Deliberately over-indexed table — 20 indexes | 0.8 ms | 2.0 ms | 4.6
ms |
| Table that doesn't exist | 1.1 ms | 1.5 ms | 4.5 ms |
2026-11-11 14:43:07 +08:00

57 lines
1.7 KiB
TypeScript

import type {
TableRelatedFunction,
TableRelatedIndex,
TableRelatedTrigger,
} from '@/data/database/table-related-resources-query'
// Past this, naming every column makes the description wider than the row can
// show. A count is more use than a column name cut in half.
const MAX_LISTED_INDEX_COLUMNS = 3
/**
* Badges for the command menu's "Related resources" section. Everything there
* belongs to the table that is already open, so the badges name the kind of
* resource rather than repeating the table.
*/
export function formatRelatedPolicyDetails(command: string) {
return `Policy • ${command}`
}
export function formatRelatedTriggerDetails({
activation,
events,
}: Pick<TableRelatedTrigger, 'activation' | 'events'>) {
return `Trigger • ${activation} ${events.join(' OR ')}`
}
// Primary keys and unique constraints each create an index of their own. Name
// them for what the user made, not for the index behind it.
function getIndexKind({
is_primary,
is_unique,
}: Pick<TableRelatedIndex, 'is_primary' | 'is_unique'>) {
if (is_primary) return 'Primary key'
if (is_unique) return 'Unique'
return 'Index'
}
export function formatRelatedIndexDetails({
columns,
is_primary,
is_unique,
}: Pick<TableRelatedIndex, 'columns' | 'is_primary' | 'is_unique'>) {
const kind = getIndexKind({ is_primary, is_unique })
if (columns.length === 0) return kind
if (columns.length > MAX_LISTED_INDEX_COLUMNS) return `${kind} • On ${columns.length} columns`
return `${kind} • On ${columns.join(', ')}`
}
export function formatRelatedFunctionDetails({
type,
args,
}: Pick<TableRelatedFunction, 'type' | 'args'>) {
const kind = type === 'procedure' ? 'Procedure' : 'Function'
return args.length === 0 ? kind : `${kind} (${args})`
}