Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

FE-2293 Fix query performance timeouts (#41456)

Jordi Enricd26bd9285722
diff --git a/apps/studio/components/interfaces/Reports/Reports.constants.ts b/apps/studio/components/interfaces/Reports/Reports.constants.ts
index cc8d2dda8f4..7d8a729559a 100644
--- a/apps/studio/components/interfaces/Reports/Reports.constants.ts
+++ b/apps/studio/components/interfaces/Reports/Reports.constants.ts
@@ -398,7 +398,7 @@ select
-- min_time,
-- max_time,
-- mean_time,
- statements.rows / statements.calls as avg_rows,
+ coalesce(statements.rows::numeric / nullif(statements.calls, 0), 0) as avg_rows,
statements.rows as rows_read,
case
when (statements.shared_blks_hit + statements.shared_blks_read) > 0
@@ -446,7 +446,12 @@ select
statements.calls,
statements.total_exec_time + statements.total_plan_time as total_time,
statements.mean_exec_time + statements.mean_plan_time as mean_time,
- ((statements.total_exec_time + statements.total_plan_time)/sum(statements.total_exec_time + statements.total_plan_time) OVER()) * 100 as prop_total_time${
+ coalesce(
+ ((statements.total_exec_time + statements.total_plan_time) /
+ nullif(sum(statements.total_exec_time + statements.total_plan_time) OVER(), 0)) *
+ 100,
+ 0
+ ) as prop_total_time${
runIndexAdvisor
? `,
case
@@ -492,7 +497,7 @@ select
-- min_time,
-- max_time,
-- mean_time,
- statements.rows / statements.calls as avg_rows${
+ coalesce(statements.rows::numeric / nullif(statements.calls, 0), 0) as avg_rows${
runIndexAdvisor
? `,
case
@@ -538,60 +543,66 @@ select
-- reports-query-performance-unified
set search_path to public, extensions;
- with query_results as (
+ with base as (
select
- auth.rolname,
- statements.query,
- statements.calls,
- -- -- Postgres 13, 14, 15
- statements.total_exec_time + statements.total_plan_time as total_time,
- statements.min_exec_time + statements.min_plan_time as min_time,
- statements.max_exec_time + statements.max_plan_time as max_time,
- statements.mean_exec_time + statements.mean_plan_time as mean_time,
- -- -- Postgres <= 12
- -- total_time,
- -- min_time,
- -- max_time,
- -- mean_time,
- statements.rows / statements.calls as avg_rows,
- statements.rows as rows_read,
- statements.shared_blks_hit as debug_hit,
- statements.shared_blks_read as debug_read,
- case
- when (statements.shared_blks_hit + statements.shared_blks_read) > 0
- then (statements.shared_blks_hit::numeric * 100.0) /
- (statements.shared_blks_hit + statements.shared_blks_read)
- else 0
- end as cache_hit_rate,
- ((statements.total_exec_time + statements.total_plan_time)/sum(statements.total_exec_time + statements.total_plan_time) OVER()) * 100 as prop_total_time${
- runIndexAdvisor
- ? `,
- case
- when (lower(statements.query) like 'select%' or lower(statements.query) like 'with pgrst%')
- then (
- select json_build_object(
- 'has_suggestion', array_length(index_statements, 1) > 0,
- 'startup_cost_before', startup_cost_before,
- 'startup_cost_after', startup_cost_after,
- 'total_cost_before', total_cost_before,
- 'total_cost_after', total_cost_after,
- 'index_statements', index_statements
- )
- from index_advisor(statements.query)
- )
- else null
- end as index_advisor_result`
- : ''
- }
- from pg_stat_statements as statements
- inner join pg_authid as auth on statements.userid = auth.oid
- ${where || ''}
- )
- select *
- from query_results
- ${filterIndexAdvisor && runIndexAdvisor ? `where (index_advisor_result->>'has_suggestion')::boolean = true` : ''}
+ auth.rolname,
+ statements.query,
+ statements.calls,
+ statements.total_exec_time + statements.total_plan_time as total_time,
+ statements.min_exec_time + statements.min_plan_time as min_time,
+ statements.max_exec_time + statements.max_plan_time as max_time,
+ statements.mean_exec_time + statements.mean_plan_time as mean_time,
+ coalesce(statements.rows::numeric / nullif(statements.calls, 0), 0) as avg_rows,
+ statements.rows as rows_read,
+ statements.shared_blks_hit as debug_hit,
+ statements.shared_blks_read as debug_read,
+ case
+ when (statements.shared_blks_hit + statements.shared_blks_read) > 0
+ then (statements.shared_blks_hit::numeric * 100.0) /
+ (statements.shared_blks_hit + statements.shared_blks_read)
+ else 0
+ end as cache_hit_rate,
+ coalesce(
+ ((statements.total_exec_time + statements.total_plan_time) /
+ nullif(sum(statements.total_exec_time + statements.total_plan_time) OVER(), 0)) *
+ 100,
+ 0
+ ) as prop_total_time
+ from pg_stat_statements as statements
+ inner join pg_authid as auth on statements.userid = auth.oid
+ ${where || ''}
${orderBy || 'order by total_time desc'}
- limit 20`
+ limit 50
+ ),
+ query_results as (
+ select
+ base.*${
+ runIndexAdvisor
+ ? `,
+ case
+ when (lower(base.query) like 'select%' or lower(base.query) like 'with pgrst%')
+ then (
+ select json_build_object(
+ 'has_suggestion', array_length(index_statements, 1) > 0,
+ 'startup_cost_before', startup_cost_before,
+ 'startup_cost_after', startup_cost_after,
+ 'total_cost_before', total_cost_before,
+ 'total_cost_after', total_cost_after,
+ 'index_statements', index_statements
+ )
+ from index_advisor(base.query)
+ )
+ else null
+ end as index_advisor_result`
+ : ''
+ }
+ from base
+ )
+ select *
+ from query_results
+ ${filterIndexAdvisor && runIndexAdvisor ? `where (index_advisor_result->>'has_suggestion')::boolean = true` : ''}
+ ${orderBy || 'order by total_time desc'}
+ limit 20`
return baseQuery
},
diff --git a/apps/studio/pages/project/[ref]/observability/query-performance.tsx b/apps/studio/pages/project/[ref]/observability/query-performance.tsx
index 43686bb31ad..95d3a690753 100644
--- a/apps/studio/pages/project/[ref]/observability/query-performance.tsx
+++ b/apps/studio/pages/project/[ref]/observability/query-performance.tsx
@@ -62,18 +62,6 @@ const QueryPerformanceReport: NextPageWithLayout = () => {
const isPgStatMonitorEnabled = project?.dbVersion === '17.4.1.076-psml-1'
- if (!ref) {
- return (
- <div className="h-full flex flex-col p-6">
- <Admonition
- type="destructive"
- title="Invalid project reference"
- description="Unable to load query performance data. Please ensure you have selected a valid project."
- />
- </div>
- )
- }
-
if (!isLoadingProject && !project) {
return (
<div className="h-full flex flex-col p-6">