get_database_connections
Monitor active database connections in self-hosted Supabase to track usage and identify idle sessions for optimization.
Instructions
Retrieves information about active database connections from pg_stat_activity.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
No arguments | |||
Implementation Reference
- The execute handler function that fetches active database connections by querying pg_stat_activity, using shared utilities for SQL execution and response handling.execute: async (input: GetDbConnectionsInput, context: ToolContext) => { const client = context.selfhostedClient; // Query pg_stat_activity // Note: Access to pg_stat_activity might require superuser or specific grants. const getConnectionsSql = ` SELECT pid, datname, usename, application_name, client_addr::text, -- Cast inet to text backend_start::text, -- Cast timestamp to text state, query FROM pg_stat_activity WHERE backend_type = 'client backend' -- Exclude background workers, etc. -- Optionally filter out self? -- AND pid != pg_backend_pid() ORDER BY backend_start `; const result = await executeSqlWithFallback(client, getConnectionsSql, true); return handleSqlResponse(result, GetDbConnectionsOutputSchema); },
- Zod schema defining the structure of the output: an array of database connection objects with fields like datname, usename, state, etc.const GetDbConnectionsOutputSchema = z.array(z.object({ datname: z.string().nullable().describe('Database name'), usename: z.string().nullable().describe('User name'), application_name: z.string().nullable().describe('Application name (e.g., PostgREST, psql)'), client_addr: z.string().nullable().describe('Client IP address'), backend_start: z.string().nullable().describe('Time when the backend process started'), state: z.string().nullable().describe('Current connection state (e.g., active, idle)'), query: z.string().nullable().describe('Last or current query being executed'), pid: z.number().describe('Process ID of the backend'), }));
- Zod input schema (empty object, no parameters required).const GetDbConnectionsInputSchema = z.object({}); type GetDbConnectionsInput = z.infer<typeof GetDbConnectionsInputSchema>;
- src/index.ts:16-16 (registration)Import of the getDatabaseConnectionsTool.import { getDatabaseConnectionsTool } from './tools/get_database_connections.js';
- src/index.ts:105-105 (registration)Registration of the tool in the availableTools object used by the MCP server.[getDatabaseConnectionsTool.name]: getDatabaseConnectionsTool as AppTool,