Files
supabase/apps/studio/components/layouts/TableEditorLayout/TableRelatedResources.Commands.tsx
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

127 lines
4.7 KiB
TypeScript

import { useQuery } from '@tanstack/react-query'
import { useParams } from 'common'
import { FunctionSquareIcon, ListTreeIcon, ShieldIcon, ZapIcon } from 'lucide-react'
import type { ICommand } from 'ui-patterns/CommandMenu'
import { useCommandMenuOpen, useCurrentPage, useRegisterCommands } from 'ui-patterns/CommandMenu'
import {
formatRelatedFunctionDetails,
formatRelatedIndexDetails,
formatRelatedPolicyDetails,
formatRelatedTriggerDetails,
} from './TableRelatedResources.utils'
import { COMMAND_MENU_SECTIONS } from '@/components/interfaces/App/CommandMenu/CommandMenu.utils'
import {
getFunctionEditorRoute,
getIndexEditorRoute,
getPolicyEditorRoute,
getTriggerEditorRoute,
} from '@/components/interfaces/App/CommandMenu/DatabaseEntityRoutes.utils'
import { orderCommandSectionsByPriority } from '@/components/interfaces/App/CommandMenu/ordering'
import {
tableReferencingFunctionsQueryOptions,
tableRelatedResourcesQueryOptions,
} from '@/data/database/table-related-resources-query'
import { useTableEditorQuery } from '@/data/table-editor/table-editor-query'
import { useSelectedProjectQuery } from '@/hooks/misc/useSelectedProject'
import { useGrant } from '@/lib/grants'
// Above Shortcuts, which registers at 1
const RELATED_RESOURCES_PRIORITY = 0
const Badge = ({ children }: { children: string }) => (
<span className="text-xs text-foreground-lighter truncate pl-4 shrink-0">{children}</span>
)
/**
* Lists the policies, triggers and functions tied to the table currently open
* in the table editor, so the command menu answers "what else touches this
* table?" without the user having to type its name.
*
* Registered from the table editor layout, so it is scoped to that route.
*/
export function useTableRelatedResourcesCommands() {
const { id } = useParams()
const { data: project } = useSelectedProjectQuery()
const isOpen = useCommandMenuOpen()
const isRootLevel = useCurrentPage() === undefined
const tableId = Number(id)
const { data: table } = useTableEditorQuery({
projectRef: project?.ref,
connectionString: project?.connectionString,
id: Number.isFinite(tableId) ? tableId : undefined,
})
const grantResult = useGrant(
'POST /platform/pg-meta/{ref}/query',
{ projectRef: project?.ref },
{ variant: 'read' }
)
const canShowSection = isOpen && isRootLevel
const variables = {
projectRef: project?.ref,
connectionString: project?.connectionString,
schema: table?.schema,
name: table?.name,
grant: grantResult.grant,
}
// Two queries on purpose: policies and triggers are index probes that come
// back immediately, while the functions are found by reading function bodies
// and can take a few hundred milliseconds. Splitting them lets the rest of
// the section render without waiting.
const { data } = useQuery(
tableRelatedResourcesQueryOptions(variables, { enabled: canShowSection })
)
const { data: functionData } = useQuery(
tableReferencingFunctionsQueryOptions(variables, { enabled: canShowSection })
)
const projectRef = project?.ref ?? '_'
const policies = canShowSection ? (data?.policies ?? []) : []
const triggers = canShowSection ? (data?.triggers ?? []) : []
const indexes = canShowSection ? (data?.indexes ?? []) : []
const functions = canShowSection ? (functionData?.functions ?? []) : []
const commands: ICommand[] = [
...policies.map((policy) => ({
id: `related-policy-${policy.id}`,
name: policy.name,
icon: () => <ShieldIcon />,
badge: () => <Badge>{formatRelatedPolicyDetails(policy.command)}</Badge>,
route: getPolicyEditorRoute(projectRef, policy),
})),
...triggers.map((trigger) => ({
id: `related-trigger-${trigger.id}`,
name: trigger.name,
icon: () => <ZapIcon />,
badge: () => <Badge>{formatRelatedTriggerDetails(trigger)}</Badge>,
route: getTriggerEditorRoute(projectRef, trigger),
})),
...indexes.map((index) => ({
id: `related-index-${index.id}`,
name: index.name,
icon: () => <ListTreeIcon />,
badge: () => <Badge>{formatRelatedIndexDetails(index)}</Badge>,
route: getIndexEditorRoute(projectRef, index),
})),
...functions.map((fn) => ({
id: `related-function-${fn.id}`,
name: `${fn.schema}.${fn.name}`,
icon: () => <FunctionSquareIcon />,
badge: () => <Badge>{formatRelatedFunctionDetails(fn)}</Badge>,
route: getFunctionEditorRoute(projectRef, fn),
})),
]
useRegisterCommands(COMMAND_MENU_SECTIONS.RELATED_RESOURCES, commands, {
deps: [data, functionData, canShowSection, projectRef],
enabled: canShowSection && commands.length > 0,
orderSection: orderCommandSectionsByPriority,
sectionMeta: { priority: RELATED_RESOURCES_PRIORITY },
})
}