Files
supabase/apps/studio/data/database/keys.ts
Joshen Lim c7803b8b9b Chore/add sessions database connections (#48094)
## Context

Initial work for Top for Postgres - adds a "Sessions" section under a
new Observability segment "Database Connections"
NOTE: All the copywriting and naming might change - not sure what's an
ideal title for this
We'll also be iteratively building on top of this UI, adding more
actionable signals instead of just information
Changes are featured flagged, off for public

- This would essentially replace the "View ongoing queries" in the SQL
Editor by providing a dedicated UI
  - It checks against `pg_stat_activity` as per the ongoing queries UI
- We'll also subsequently deprecate the "Ongoing queries" UI in the SQL
editor
- Defaults into a "live mode" where the data is refreshed every 3
seconds via long-polling
<img width="983" height="474" alt="image"
src="https://github.com/user-attachments/assets/16402fe4-0b53-4f9e-9342-cdda26e3778a"
/>
- Supports filtering by state  
<img width="374" height="282" alt="image"
src="https://github.com/user-attachments/assets/562f8fbe-2dc6-48e7-8ec0-de7ffb8348d1"
/>
- Users can also terminate queries through here
<img width="247" height="164" alt="image"
src="https://github.com/user-attachments/assets/23a639dc-8f96-473a-a823-605b0bab02ee"
/>





<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

# Release Notes

* **New Features**
* Added an Observability **Database Connections** page with a live
**Sessions** activity table (state/roles filtering, blocked-by details,
session duration, and per-session termination with confirmation).
* Included a **Live/Pause** toggle to control automatic refresh (~3
seconds).

* **Enhancements**
* Improved Reports selection filtering: supports optional option
quantities, better popover styling, sorted apply behavior, and shows
quantity inline.
* Query performance duration formatting now supports configurable
decimal precision.
  * Tooltips can now render richer content (string or React node).
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2026-07-21 16:52:03 +08:00

76 lines
3.7 KiB
TypeScript

import { sqlKeys } from '@/data/sql/keys'
export const databaseKeys = {
schemas: (projectRef: string | undefined) => ['projects', projectRef, 'schemas'] as const,
keywords: (projectRef: string | undefined) => ['projects', projectRef, 'keywords'] as const,
migrations: (projectRef: string | undefined) => ['projects', projectRef, 'migrations'] as const,
tableColumns: (
projectRef: string | undefined,
schema: string | undefined,
table: string | undefined
) => ['projects', projectRef, 'table-columns', schema, table] as const,
databaseFunctions: (projectRef: string | undefined, schema?: string) =>
['projects', projectRef, 'database-functions', schema].filter(Boolean),
entityDefinition: (projectRef: string | undefined, id?: number) =>
['projects', projectRef, 'entity-definition', id] as const,
entityDefinitions: (projectRef: string | undefined, schemas: string[]) =>
['projects', projectRef, 'entity-definitions', schemas] as const,
tableDefinition: (projectRef: string | undefined, id?: number) =>
['projects', projectRef, 'table-definition', id] as const,
viewDefinition: (projectRef: string | undefined, id?: number, includeCreateStatement?: boolean) =>
['projects', projectRef, 'view-definition', id, includeCreateStatement ?? false] as const,
backups: (projectRef: string | undefined) =>
['projects', projectRef, 'database', 'backups'] as const,
poolingConfiguration: (projectRef: string | undefined) =>
['projects', projectRef, 'database', 'pooling-configuration'] as const,
indexesFromQuery: (projectRef: string | undefined, query: string) =>
['projects', projectRef, 'indexes', { query }] as const,
indexAdvisorFromQuery: (
projectRef: string | undefined,
query: string,
connectionString?: string
) => {
// Use only the host (no credentials) as a safe cache discriminator
let connectionFingerprint: string | undefined
if (connectionString) {
try {
connectionFingerprint = new URL(connectionString).host
} catch {
connectionFingerprint = undefined
}
}
return ['projects', projectRef, 'index-advisor', { query, connectionFingerprint }] as const
},
tableConstraints: (projectRef: string | undefined, id?: number) =>
['projects', projectRef, 'table-constraints', id] as const,
foreignKeyConstraints: (projectRef: string | undefined, schema?: string, options = {}) =>
['projects', projectRef, 'foreign-key-constraints', schema, options] as const,
databaseSize: (projectRef: string | undefined) =>
['projects', projectRef, 'database-size'] as const,
maxConnections: (projectRef: string | undefined) =>
['projects', projectRef, 'max-connections'] as const,
pgbouncerStatus: (projectRef: string | undefined) =>
['projects', projectRef, 'pgbouncer', 'status'] as const,
pgbouncerConfig: (projectRef: string | undefined) =>
['projects', projectRef, 'pgbouncer', 'config'] as const,
checkPrimaryKeysExists: (
projectRef: string | undefined,
tables: { name: string; schema: string }[]
) => ['projects', projectRef, 'check-primary-keys', tables] as const,
tableIndexAdvisor: (
projectRef: string | undefined,
schema: string | undefined,
table: string | undefined
) => ['projects', projectRef, 'table-index-advisor', schema, table] as const,
supamonitorEnabled: (projectRef: string | undefined) =>
['projects', projectRef, 'supamonitor-enabled'] as const,
databaseActivity: (projectRef: string | undefined) =>
['projects', projectRef, 'database-activity'] as const,
}
export const getLiveTupleEstimateKey = (
projectRef: string | undefined,
table: string,
schema = 'public'
) => sqlKeys.query(projectRef, ['live-tuple-estimate', schema, table])