Files
supabase/apps/studio/routes/project/$ref/observability/query-performance.tsx
Drew McLellan 8cda1fc977 fix(studio): apply query performance calls and total time filters in SQL (#53459)
## Problem

On the Query Performance page, the calls filter only applied to rows
already loaded in the grid. The page loads 20 rows at a time, so a
filter like `calls >= 200` showed "No queries detected" whenever none of
the loaded rows matched, even if matching queries existed further down.
The filter bar writes a `callsFilter` URL param, but the page only
passed a separate `minCalls` param to the SQL, which nothing set.

The total time filter had a related gap: only `>` and `>=` reached the
SQL, and the other operators were ignored.

Fixes part of FE-5092. The lazy-load fix is in #53457.

## Solution

- `generateQueryPerformanceSql` takes `callsFilter` and
`totalTimeFilter` and builds the `WHERE` clause for all six comparison
operators. The operator comes from a fixed lookup and the value goes
through `literal()`. Filters with a non-finite value or an unknown
operator are dropped.
- The page passes both filters to the query, and the unused `minCalls`
param is removed.
- The grid no longer filters search, roles or calls in memory, since the
SQL already does.

- The query search is now a literal, case-insensitive substring match.
It was a regex (`~*`), so characters like `[` or `(` produced a SQL
error while typing. This matches what the grid's old in-memory search
did.

Behavior changes to be aware of:

- Regex search no longer works. Searching for `a.*b` now looks for that
exact text.
- Old `?minCalls=` links no longer have any effect. The UI never
produced them.

## Review instructions

1. Open Observability → Query Performance on a project with more than 20
statements.
2. Add a Calls filter of `>= 200` (or a value higher than anything in
the first 20 rows). Confirm matching queries appear.
3. Try the other operators on Calls and on Total time, including `!=`.
4. Search for `[` and `pg_sleep(` and confirm the grid shows matches (or
an empty state) instead of a SQL error.
5. Clear the filters and confirm the full list returns.

## Checklist

Check all before review:

- [x] I have read
[CONTRIBUTING.md](https://github.com/supabase/supabase/blob/master/CONTRIBUTING.md)
- [ ] If I wrote a new docs topic or edited an existing topic, I used
the `/write-the-docs` or `/edit-the-docs` skill, which applies the docs
[style
guide](https://github.com/supabase/supabase/tree/master/apps/docs/style-guide)
2026-11-17 15:09:52 +00:00

112 lines
4.2 KiB
TypeScript

import { createFileRoute } from '@tanstack/react-router'
import { IS_PLATFORM, useParams } from 'common'
import { parseAsArrayOf, parseAsJson, parseAsString, useQueryStates } from 'nuqs'
import { Admonition } from 'ui-patterns/Admonition'
import { OBSERVABILITY_DOCS_HREFS } from '@/components/interfaces/Observability/Observability.constants'
import { useIndexAdvisorStatus } from '@/components/interfaces/QueryPerformance/hooks/useIsIndexAdvisorStatus'
import { useQueryPerformanceSort } from '@/components/interfaces/QueryPerformance/hooks/useQueryPerformanceSort'
import { QueryPerformance } from '@/components/interfaces/QueryPerformance/QueryPerformance'
import { type QuerySource } from '@/components/interfaces/QueryPerformance/QueryPerformance.types'
import { useQueryPerformanceInfiniteQuery } from '@/components/interfaces/QueryPerformance/useQueryPerformanceQuery'
import { PRESET_CONFIG } from '@/components/interfaces/Reports/Reports.constants'
import { Presets } from '@/components/interfaces/Reports/Reports.types'
import { queriesFactory } from '@/components/interfaces/Reports/Reports.utils'
import {
NumericFilter,
numericFilterSchema,
} from '@/components/interfaces/Reports/v2/ReportsNumericFilter'
import { DatabaseSelector } from '@/components/ui/DatabaseSelector'
import { DocsButton } from '@/components/ui/DocsButton'
import { useSelectedProjectQuery } from '@/hooks/misc/useSelectedProject'
export const Route = createFileRoute('/project/$ref/observability/query-performance')({
component: QueryPerformanceReport,
staticData: {
observabilityLayoutTitle: 'Query Performance',
},
})
const REPORT_TITLE = 'Query Performance'
const parseNumericFilter = (value: unknown): NumericFilter | null => {
const parsed = numericFilterSchema.safeParse(value)
return parsed.success ? parsed.data : null
}
function QueryPerformanceReport() {
const { ref } = useParams()
const { data: project, isLoading: isLoadingProject } = useSelectedProjectQuery()
const { isIndexAdvisorEnabled } = useIndexAdvisorStatus()
const { sort: sortConfig } = useQueryPerformanceSort()
const [
{
search: searchQuery,
roles,
sources,
callsFilter: callsFilterRaw,
totalTimeFilter: totalTimeFilterRaw,
indexAdvisor,
},
] = useQueryStates({
sort: parseAsString,
order: parseAsString,
search: parseAsString.withDefault(''),
roles: parseAsArrayOf(parseAsString).withDefault([]),
sources: parseAsArrayOf(parseAsString).withDefault([]),
callsFilter: parseAsJson(parseNumericFilter),
totalTimeFilter: parseAsJson(parseNumericFilter),
indexAdvisor: parseAsString.withDefault('false'),
})
const callsFilter = callsFilterRaw ?? undefined
const totalTimeFilter = totalTimeFilterRaw ?? undefined
const config = PRESET_CONFIG[Presets.QUERY_PERFORMANCE]
const hooks = queriesFactory(config.queries, ref ?? 'default')
const queryHitRate = hooks.queryHitRate()
const queryMetrics = hooks.queryMetrics()
const queryPerformanceQuery = useQueryPerformanceInfiniteQuery({
searchQuery,
orderBy: sortConfig || undefined,
preset: 'unified',
roles,
sources: sources as QuerySource[],
runIndexAdvisor: isIndexAdvisorEnabled,
callsFilter,
totalTimeFilter,
filterIndexAdvisor: indexAdvisor === 'true',
})
if (!isLoadingProject && !project) {
return (
<div className="h-full flex flex-col p-6">
<Admonition
type="destructive"
title="Project not found"
description="Unable to load project data. Please check your project reference and try again."
/>
</div>
)
}
return (
<div className="h-full flex flex-col">
<div className="w-full mb-0 flex lg:items-center justify-between gap-4 py-4 px-6 lg:flex-row flex-col">
<h3 className="text-foreground text-xl prose">{REPORT_TITLE}</h3>
<div className="flex items-center gap-2 flex-wrap">
<DocsButton href={OBSERVABILITY_DOCS_HREFS.queryPerformance} topic={REPORT_TITLE} />
{IS_PLATFORM && <DatabaseSelector />}
</div>
</div>
<QueryPerformance
queryHitRate={queryHitRate}
queryPerformanceQuery={queryPerformanceQuery}
queryMetrics={queryMetrics}
/>
</div>
)
}