The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.
TypeScript aialternativeauthdatabasedenoembeddingsexamplefirebasenextjsoauth2pgvectorpostgispostgrespostgresqlpostgrestrealtimesupabasevectorswebsockets
Add top blocker overview card (#48290)
Joshen Limfb7debec2585
diff --git a/apps/studio/components/interfaces/Observability/DatabaseConnections/DatabaseConnections.constants.ts b/apps/studio/components/interfaces/Observability/DatabaseConnections/DatabaseConnections.constants.tsindex be3a075cdc1..ab07cd26b47 100644--- a/apps/studio/components/interfaces/Observability/DatabaseConnections/DatabaseConnections.constants.ts+++ b/apps/studio/components/interfaces/Observability/DatabaseConnections/DatabaseConnections.constants.ts@@ -6,6 +6,8 @@export const WARN_DURATION_ACTIVE_QUERY = 30 // secondsexport const WARN_DURATION_IDLE_TXN = 10 // seconds+export const WARN_TOP_BLOCKER = 3 // If the query is blocking more than 3 queries+export const QUERY_STATE_TOOLTIP = {['active']: 'Currently executing a query.',['idle']: 'Connected, but not currently running a query.',diff --git a/apps/studio/components/interfaces/Observability/DatabaseConnections/Overview.tsx b/apps/studio/components/interfaces/Observability/DatabaseConnections/Overview.tsxindex d03ee79e09f..c7c64e60576 100644--- a/apps/studio/components/interfaces/Observability/DatabaseConnections/Overview.tsx+++ b/apps/studio/components/interfaces/Observability/DatabaseConnections/Overview.tsx@@ -9,7 +9,11 @@ import {MetricCardValue,} from 'ui-patterns/MetricCard'-import { WARN_DURATION_ACTIVE_QUERY, WARN_DURATION_IDLE_TXN } from './DatabaseConnections.constants'+import {+ WARN_DURATION_ACTIVE_QUERY,+ WARN_DURATION_IDLE_TXN,+ WARN_TOP_BLOCKER,+} from './DatabaseConnections.constants'import { formatDuration } from '@/components/interfaces/QueryPerformance/QueryPerformance.utils'import { useDatabaseRolesQuery } from '@/data/database-roles/database-roles-query'import { useDatabaseActivityQuery, type DatabaseActivity } from '@/data/database/activity-query'@@ -26,14 +30,6 @@ interface OverviewProps {live?: boolean}-/**- * [Joshen] Couple of nuances worth calling out to provide better signals for the user- * - Idle in transaction:- * - Only considers queries in that state, but running for longer than 10 seconds- * - Could otherwise be a query in mid-flight- * - Longest running:- * - Only considers queries that are active or idle in transaction- */export const Overview = ({ live }: OverviewProps) => {const { data: project } = useSelectedProjectQuery()const [, setSelectedPid] = useQueryState('pid', parseAsInteger)@@ -69,6 +65,22 @@ export const Overview = ({ live }: OverviewProps) => {longestRunningQuery?.activity.state === 'idle in transaction (aborted)') &&longestRunningQuery.duration >= WARN_DURATION_IDLE_TXN)+ const blockingCounts = (data ?? []).reduce<Map<number, number>>((counts, activity) => {+ activity.blocked_by.forEach((pid) => counts.set(pid, (counts.get(pid) ?? 0) + 1))+ return counts+ }, new Map())++ const queryBlockingTheMostQueries = [...blockingCounts].reduce<{+ activity: DatabaseActivity+ count: number+ } | null>((mostBlocking, [pid, count]) => {+ if (mostBlocking && count <= mostBlocking.count) return mostBlocking+ const activity = (data ?? []).find((x) => x.pid === pid)+ return activity ? { activity, count } : mostBlocking+ }, null)++ const warnTopBlocker = (queryBlockingTheMostQueries?.count ?? 0) >= WARN_TOP_BLOCKER+const { data: roles, isPending: isLoadingRoles } = useDatabaseRolesQuery({projectRef: project?.ref,@@ -99,7 +111,7 @@ export const Overview = ({ live }: OverviewProps) => {</div><div className="flex flex-col gap-y-2">- <div className="grid grid-cols-2 gap-2">+ <div className="grid grid-cols-2 md:grid-cols-3 gap-2"><MetricCard isLoading={isLoadingRoles || isLoadingMaxConnections}><MetricCardHeader><MetricCardLabel@@ -126,67 +138,47 @@ export const Overview = ({ live }: OverviewProps) => {</MetricCardContent></MetricCard>+ <MetricCard isLoading={isLoadingActivity}>+ <MetricCardHeader>+ <MetricCardLabel tooltip="Queries currently executing on the database.">+ Active queries+ </MetricCardLabel>+ </MetricCardHeader>+ <MetricCardContent>+ <MetricCardValue>{activeQueries?.length}</MetricCardValue>+ </MetricCardContent>+ </MetricCard>+<MetricCardisLoading={isLoadingActivity}- className={cn(warnLongestRunningQuery && 'bg-warning-200 border-warning-400')}+ className={cn(idleInTransactionQueries.length && 'bg-warning-200 border-warning-400')}><MetricCardHeader><MetricCardLabel- className={cn(warnLongestRunningQuery && 'text-foreground')}- tooltip="Only considers active or idle-in-transaction queries"+ className={cn(idleInTransactionQueries.length && 'text-foreground')}+ tooltip={+ <>+ <p>+ Transactions left open without running a query, which can hold locks and block+ table cleanup for as long as it stays open+ </p>+ <p className="mt-2">+ Typically indicates an app issue, such as a forgotten COMMIT or ROLLBACK.+ </p>+ </>+ }>- Longest running+ Idle in transaction</MetricCardLabel></MetricCardHeader><MetricCardContent><MetricCardValue- className={cn(- 'space-x-2',- longestRunningQuery === null- ? 'text-foreground-lighter'- : warnLongestRunningQuery- ? 'text-warning'- : 'text-foreground'- )}+ className={cn(idleInTransactionQueries.length > 0 && 'text-warning')}>- {longestRunningQuery === null ? (- '-'- ) : (- <>- <span>{formatDuration(longestRunningQuery.duration * 1000, 0)}</span>- <span className="text-foreground-lighter text-sm">·</span>- <span- role="button"- tabIndex={0}- className="text-foreground-lighter text-sm hover:text-foreground transition cursor-pointer"- onClick={() => setSelectedPid(longestRunningQuery.activity.pid)}- onKeyDown={(e) => {- if (e.key === 'Enter' || e.key === ' ') {- e.preventDefault()- setSelectedPid(longestRunningQuery.activity.pid)- }- }}- >- PID: {longestRunningQuery.activity.pid}- </span>- </>- )}+ {idleInTransactionQueries.length}</MetricCardValue></MetricCardContent></MetricCard>- </div>-- <div className="grid grid-cols-2 gap-2 lg:grid-cols-3">- <MetricCard isLoading={isLoadingActivity}>- <MetricCardHeader>- <MetricCardLabel tooltip="Queries currently executing on the database.">- Active queries- </MetricCardLabel>- </MetricCardHeader>- <MetricCardContent>- <MetricCardValue>{activeQueries?.length}</MetricCardValue>- </MetricCardContent>- </MetricCard><MetricCardisLoading={isLoadingActivity}@@ -202,8 +194,8 @@ export const Overview = ({ live }: OverviewProps) => {behind it.</p><p className="mt-2">- Typically caused by an uncommitted transaction, a long-running migration, or a- stuck idle-in-transaction session.+ Typically caused by a slow transaction, a long-running migration, or a stuck+ idle-in-transaction session.</p></>}@@ -220,31 +212,115 @@ export const Overview = ({ live }: OverviewProps) => {<MetricCardisLoading={isLoadingActivity}- className={cn(idleInTransactionQueries.length && 'bg-warning-200 border-warning-400')}+ className={cn(warnTopBlocker && 'bg-destructive-200 border-destructive-400')}><MetricCardHeader><MetricCardLabel- className={cn(idleInTransactionQueries.length && 'text-foreground')}- tooltip={+ className={cn(warnTopBlocker && 'text-foreground')}+ tooltip="The query blocking the most other queries"+ >+ Top blocker+ </MetricCardLabel>+ </MetricCardHeader>+ <MetricCardContent>+ <MetricCardValue+ className={cn(+ 'space-x-1',+ queryBlockingTheMostQueries === null+ ? 'text-foreground-lighter'+ : warnTopBlocker+ ? 'text-destructive'+ : 'text-foreground'+ )}+ >+ {queryBlockingTheMostQueries === null ? (+ '-'+ ) : (<>- <p>- Transactions left open without running a query, which can hold locks and block- table cleanup for as long as it stays open- </p>- <p className="mt-2">- Typically indicates an app issue, such as a forgotten COMMIT or ROLLBACK.- </p>+ <span+ role="button"+ tabIndex={0}+ className="normal-nums cursor-pointer hover:underline"+ onClick={() => setSelectedPid(queryBlockingTheMostQueries.activity.pid)}+ onKeyDown={(e) => {+ if (e.key === 'Enter' || e.key === ' ') {+ e.preventDefault()+ setSelectedPid(queryBlockingTheMostQueries.activity.pid)+ }+ }}+ >+ PID: {queryBlockingTheMostQueries?.activity.pid}+ </span>+ <span className="text-foreground-light text-xs">·</span>+ <span+ className={cn(+ 'text-xs',+ warnTopBlocker ? 'text-foreground-light' : 'text-foreground-lighter'+ )}+ >+ Blocking {queryBlockingTheMostQueries?.count} other{' '}+ {queryBlockingTheMostQueries?.count > 1 ? 'queries' : 'query'}+ </span></>- }+ )}+ </MetricCardValue>+ </MetricCardContent>+ </MetricCard>++ <MetricCard+ isLoading={isLoadingActivity}+ className={cn(warnLongestRunningQuery && 'bg-warning-200 border-warning-400')}+ >+ <MetricCardHeader>+ <MetricCardLabel+ className={cn(warnLongestRunningQuery && 'text-foreground')}+ tooltip="Only considers active or idle-in-transaction queries">- Idle in transaction+ Longest running</MetricCardLabel></MetricCardHeader><MetricCardContent><MetricCardValue- className={cn(idleInTransactionQueries.length > 0 && 'text-warning')}+ className={cn(+ 'space-x-1',+ longestRunningQuery === null+ ? 'text-foreground-lighter'+ : warnLongestRunningQuery+ ? 'text-warning'+ : 'text-foreground'+ )}>- {idleInTransactionQueries.length}+ {longestRunningQuery === null ? (+ '-'+ ) : (+ <>+ <span+ role="button"+ tabIndex={0}+ className="normal-nums hover:underline cursor-pointer"+ onClick={() => setSelectedPid(longestRunningQuery.activity.pid)}+ onKeyDown={(e) => {+ if (e.key === 'Enter' || e.key === ' ') {+ e.preventDefault()+ setSelectedPid(longestRunningQuery.activity.pid)+ }+ }}+ >+ PID: {longestRunningQuery.activity.pid}+ </span>+ <span className="text-foreground-lighter text-sm">·</span>+ <span+ className={cn(+ 'text-sm',+ warnLongestRunningQuery+ ? 'text-foreground-light'+ : 'text-foreground-lighter'+ )}+ >+ {formatDuration(longestRunningQuery.duration * 1000, 0)}+ </span>+ </>+ )}</MetricCardValue></MetricCardContent></MetricCard>