execute_sql tool allows you to execute SQL statements against your database. It supports multiple statements in a single call (transactions), enforces readonly mode when configured, and limits result sets based on configuration.
Parameters
SQL statement(s) to execute. Multiple statements can be separated by semicolons (
;) for transaction support.Examples:- Single query:
SELECT * FROM users WHERE id = 1 - Multiple statements:
BEGIN; UPDATE accounts SET balance = balance - 100 WHERE id = 1; UPDATE accounts SET balance = balance + 100 WHERE id = 2; COMMIT;
Optional database identifier for multi-database configurations. If not provided, uses the default (first) database.Example:
"prod_pg" or "staging_mysql"Response Format
Array of result rows. Each row is an object with column names as keys.
Number of rows returned or affected by the query.
The database identifier that was used to execute the query.
Configuration
Theexecute_sql tool can be configured per-source in your dbhub.toml file:
Readonly Mode
Whenreadonly = true is configured, the tool only allows the following SQL operations:
- PostgreSQL:
SELECT,WITH,EXPLAIN,ANALYZE,SHOW - MySQL/MariaDB:
SELECT,WITH,EXPLAIN,ANALYZE,SHOW,DESCRIBE,DESC - SQLite:
SELECT,WITH,EXPLAIN,ANALYZE,PRAGMA - SQL Server:
SELECT,WITH,EXPLAIN,SHOWPLAN
READONLY_VIOLATION error.
Max Rows Limiting
Themax_rows configuration limits the number of rows returned from SELECT queries. This helps prevent:
- Overwhelming LLM context windows with large result sets
- Memory exhaustion from unbounded queries
- Excessive token usage
SQL Statement Splitting
The tool intelligently splits multiple SQL statements using dialect-aware parsing (viasrc/utils/sql-parser.ts). This ensures that semicolons inside:
- String literals (
'text with ; semicolon') - Comments (
-- comment ; here) - Quoted identifiers (
`table;name`) - Dollar-quoted blocks (PostgreSQL:
$$text ; here$$)
Transaction Support
Multiple statements are executed as a single transaction when separated by semicolons:Examples
Simple SELECT Query
Multi-Statement Transaction
Multi-Database Query
Complex Query with JOINs
Error Handling
READONLY_VIOLATION
Occurs when attempting write operations withreadonly = true configured:
EXECUTION_ERROR
Occurs when the database returns an error:Implementation Details
- Source:
src/tools/execute-sql.ts - Schema: Uses Zod schema validation for input parameters
- Readonly validation:
src/utils/allowed-keywords.ts - SQL parsing:
src/utils/sql-parser.ts - Connection management: Lazy connections via
ConnectorManager.ensureConnected() - Tool registration: Per-source via
getToolRegistry()
Best Practices
- Use readonly mode for production databases to prevent accidental data modification
- Set appropriate max_rows limits to avoid overwhelming context windows
- Use transactions for operations that must be atomic
- Specify source_id explicitly in multi-database configurations to avoid ambiguity
- Test write operations in development environments before production
- Use parameterized queries via custom tools for repeated operations