pg_manage_triggers
Manage PostgreSQL triggers by listing, creating, dropping, or enabling/disabling them. Define trigger operations with parameters like tableName, triggerName, functionName, and events for precise control.
Instructions
Manage PostgreSQL triggers - get, create, drop, and enable/disable triggers. Examples: operation="get" to list triggers, operation="create" with triggerName, tableName, functionName, operation="drop" with triggerName and tableName, operation="set_state" with triggerName, tableName, enable
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| cascade | No | Include CASCADE clause (for drop operation) | |
| connectionString | No | PostgreSQL connection string (optional) | |
| enable | No | Whether to enable the trigger (required for set_state operation) | |
| events | No | Trigger events (for create operation, defaults to ["INSERT"]) | |
| forEach | No | FOR EACH ROW or STATEMENT (for create operation, defaults to ROW) | |
| functionName | No | Function name (required for create operation) | |
| ifExists | No | Include IF EXISTS clause (for drop operation) | |
| operation | Yes | Operation: get (list triggers), create (new trigger), drop (remove trigger), set_state (enable/disable trigger) | |
| replace | No | Whether to replace trigger if exists (for create operation) | |
| schema | No | Schema name (defaults to public) | |
| tableName | No | Table name (optional filter for get, required for create/drop/set_state) | |
| timing | No | Trigger timing (for create operation, defaults to AFTER) | |
| triggerName | No | Trigger name (required for create/drop/set_state) | |
| when | No | WHEN clause condition (for create operation) |
Implementation Reference
- src/tools/triggers.ts:555-701 (handler)The primary handler implementation for the 'pg_manage_triggers' tool. Defines the tool object with name, description, input schema, and execute function that parses input, validates, and dispatches to specific operations (get, create, drop, set_state) using internal helper functions.export const manageTriggersTools: PostgresTool = { name: 'pg_manage_triggers', description: 'Manage PostgreSQL triggers - get, create, drop, and enable/disable triggers. Examples: operation="get" to list triggers, operation="create" with triggerName, tableName, functionName, operation="drop" with triggerName and tableName, operation="set_state" with triggerName, tableName, enable', inputSchema: z.object({ connectionString: z.string().optional().describe('PostgreSQL connection string (optional)'), operation: z.enum(['get', 'create', 'drop', 'set_state']).describe('Operation: get (list triggers), create (new trigger), drop (remove trigger), set_state (enable/disable trigger)'), // Common parameters schema: z.string().optional().describe('Schema name (defaults to public)'), tableName: z.string().optional().describe('Table name (optional filter for get, required for create/drop/set_state)'), // Trigger identification (for create/drop/set_state) triggerName: z.string().optional().describe('Trigger name (required for create/drop/set_state)'), // Create trigger parameters functionName: z.string().optional().describe('Function name (required for create operation)'), timing: z.enum(['BEFORE', 'AFTER', 'INSTEAD OF']).optional().describe('Trigger timing (for create operation, defaults to AFTER)'), events: z.array(z.enum(['INSERT', 'UPDATE', 'DELETE', 'TRUNCATE'])).optional().describe('Trigger events (for create operation, defaults to ["INSERT"])'), forEach: z.enum(['ROW', 'STATEMENT']).optional().describe('FOR EACH ROW or STATEMENT (for create operation, defaults to ROW)'), when: z.string().optional().describe('WHEN clause condition (for create operation)'), replace: z.boolean().optional().describe('Whether to replace trigger if exists (for create operation)'), // Drop trigger parameters ifExists: z.boolean().optional().describe('Include IF EXISTS clause (for drop operation)'), cascade: z.boolean().optional().describe('Include CASCADE clause (for drop operation)'), // Set state parameters enable: z.boolean().optional().describe('Whether to enable the trigger (required for set_state operation)') }), // biome-ignore lint/suspicious/noExplicitAny: <explanation> execute: async (args: any, getConnectionStringVal: GetConnectionStringFn): Promise<ToolOutput> => { const { connectionString: connStringArg, operation, schema, tableName, triggerName, functionName, timing, events, forEach, when, replace, ifExists, cascade, enable } = args as { connectionString?: string; operation: 'get' | 'create' | 'drop' | 'set_state'; schema?: string; tableName?: string; triggerName?: string; functionName?: string; timing?: 'BEFORE' | 'AFTER' | 'INSTEAD OF'; events?: ('INSERT' | 'UPDATE' | 'DELETE' | 'TRUNCATE')[]; forEach?: 'ROW' | 'STATEMENT'; when?: string; replace?: boolean; ifExists?: boolean; cascade?: boolean; enable?: boolean; }; try { switch (operation) { case 'get': { const result = await executeGetTriggers({ connectionString: connStringArg, tableName, schema: schema || 'public' }, getConnectionStringVal); const message = tableName ? `Triggers for table ${schema || 'public'}.${tableName}` : `Found ${result.length} triggers in schema ${schema || 'public'}`; return { content: [{ type: 'text', text: message }, { type: 'text', text: JSON.stringify(result, null, 2) }] }; } case 'create': { if (!triggerName || !tableName || !functionName) { return { content: [{ type: 'text', text: 'Error: triggerName, tableName, and functionName are required for create operation' }], isError: true }; } const result = await executeCreateTrigger({ connectionString: connStringArg, triggerName, tableName, functionName, schema: schema || 'public', timing: timing || 'AFTER', events: events || ['INSERT'], forEach: forEach || 'ROW', when, replace: replace || false }, getConnectionStringVal); return { content: [{ type: 'text', text: `Trigger ${result.name} created successfully on ${result.schema}.${result.table}` }, { type: 'text', text: JSON.stringify(result, null, 2) }] }; } case 'drop': { if (!triggerName || !tableName) { return { content: [{ type: 'text', text: 'Error: triggerName and tableName are required for drop operation' }], isError: true }; } const result = await executeDropTrigger({ connectionString: connStringArg, triggerName, tableName, schema: schema || 'public', ifExists: ifExists || false, cascade: cascade || false }, getConnectionStringVal); return { content: [{ type: 'text', text: `Trigger ${result.name} dropped successfully from ${result.schema}.${result.table}` }, { type: 'text', text: JSON.stringify(result, null, 2) }] }; } case 'set_state': { if (!triggerName || !tableName || enable === undefined) { return { content: [{ type: 'text', text: 'Error: triggerName, tableName, and enable are required for set_state operation' }], isError: true }; } const result = await executeSetTriggerState({ connectionString: connStringArg, triggerName, tableName, enable, schema: schema || 'public' }, getConnectionStringVal); return { content: [{ type: 'text', text: `Trigger ${result.name} ${result.enabled ? 'enabled' : 'disabled'} on ${result.schema}.${result.table}` }, { type: 'text', text: JSON.stringify(result, null, 2) }] }; } default: return { content: [{ type: 'text', text: `Error: Unknown operation "${operation}". Supported operations: get, create, drop, set_state` }], isError: true }; } } catch (error) { const errorMessage = error instanceof McpError ? error.message : (error instanceof Error ? error.message : String(error)); return { content: [{ type: 'text', text: `Error executing ${operation} operation: ${errorMessage}` }], isError: true }; } } };
- src/tools/triggers.ts:558-583 (schema)Input schema using Zod for validating parameters to pg_manage_triggers, supporting multiple operations with conditional required fields.inputSchema: z.object({ connectionString: z.string().optional().describe('PostgreSQL connection string (optional)'), operation: z.enum(['get', 'create', 'drop', 'set_state']).describe('Operation: get (list triggers), create (new trigger), drop (remove trigger), set_state (enable/disable trigger)'), // Common parameters schema: z.string().optional().describe('Schema name (defaults to public)'), tableName: z.string().optional().describe('Table name (optional filter for get, required for create/drop/set_state)'), // Trigger identification (for create/drop/set_state) triggerName: z.string().optional().describe('Trigger name (required for create/drop/set_state)'), // Create trigger parameters functionName: z.string().optional().describe('Function name (required for create operation)'), timing: z.enum(['BEFORE', 'AFTER', 'INSTEAD OF']).optional().describe('Trigger timing (for create operation, defaults to AFTER)'), events: z.array(z.enum(['INSERT', 'UPDATE', 'DELETE', 'TRUNCATE'])).optional().describe('Trigger events (for create operation, defaults to ["INSERT"])'), forEach: z.enum(['ROW', 'STATEMENT']).optional().describe('FOR EACH ROW or STATEMENT (for create operation, defaults to ROW)'), when: z.string().optional().describe('WHEN clause condition (for create operation)'), replace: z.boolean().optional().describe('Whether to replace trigger if exists (for create operation)'), // Drop trigger parameters ifExists: z.boolean().optional().describe('Include IF EXISTS clause (for drop operation)'), cascade: z.boolean().optional().describe('Include CASCADE clause (for drop operation)'), // Set state parameters enable: z.boolean().optional().describe('Whether to enable the trigger (required for set_state operation)') }),
- src/index.ts:225-257 (registration)Registration of the manageTriggersTools in the central allTools array in src/index.ts, which is passed to the PostgreSQLServer constructor to enable the tool in the MCP server.const allTools: PostgresTool[] = [ // Core Analysis & Debugging analyzeDatabaseTool, debugDatabaseTool, // Schema & Structure Management (Meta-Tools) manageSchemaTools, manageFunctionsTool, manageTriggersTools, manageIndexesTool, manageConstraintsTool, manageRLSTool, // User & Security Management manageUsersTool, // Query & Performance Management manageQueryTool, // Data Operations (Enhancement Tools) executeQueryTool, executeMutationTool, executeSqlTool, // Documentation & Metadata manageCommentsTool, // Data Migration & Monitoring exportTableDataTool, importTableDataTool, copyBetweenDatabasesTool, monitorDatabaseTool ];