googleapis/mcp-toolbox · error
error executing sql: %w
Error message
error executing sql: %w
What it means
Thrown in ExecuteSql when the sqladmin Instances.ExecuteSql call (projects.instances.executeSql) fails. This uses the Cloud SQL Admin API's executeSql endpoint to run a statement against the instance.
Source
Thrown at internal/sources/cloudsqladmin/cloud_sql_admin.go:316
})
}
return instances, nil
}
func (s *Source) ExecuteSql(ctx context.Context, project, instance, database, sql string, accessToken string) (any, error) {
service, err := s.GetService(ctx, accessToken)
if err != nil {
return nil, err
}
req := &sqladmin.ExecuteSqlPayload{
Database: database,
SqlStatement: sql,
}
resp, err := service.Instances.ExecuteSql(project, instance, req).Do()
if err != nil {
return nil, fmt.Errorf("error executing sql: %w", err)
}
return resp, nil
}
func (s *Source) CreateInstance(ctx context.Context, project, name, dbVersion, rootPassword string, settings sqladmin.Settings, accessToken string) (any, error) {
instance := sqladmin.DatabaseInstance{
Name: name,
DatabaseVersion: dbVersion,
RootPassword: rootPassword,
Settings: &settings,
Project: project,
}
service, err := s.GetService(ctx, accessToken)
if err != nil {
return nil, err
}
View on GitHub (pinned to 8cc6e09de2)
Solutions
- Validate the SQL syntax (run it in a SQL client against the same instance).
- Confirm the target database exists on the instance and is spelled correctly.
- Ensure the executing user has the required grants for the statement.
- Verify the instance is in RUNNABLE state (get_instance) and retry after maintenance ends.
- Inspect the wrapped googleapi.Error body, which contains the underlying SQL error details.
Example fix
// before ExecuteSql(ctx, project, instance, "", "SELECT * FROM users", token) // empty database // after ExecuteSql(ctx, project, instance, "postgres", "SELECT * FROM users", token)
Defensive patterns
Strategy: try-catch
Validate before calling
sql := strings.TrimSpace(sqlStatement)
if sql == "" {
return fmt.Errorf("sql statement is required")
}
if database == "" {
return fmt.Errorf("database is required for execute_sql")
}
for _, stmt := range forbiddenPatterns.MatchString(sql) { /* block destructive ops if policy requires */ } Type guard
func isSQLSyntaxErr(err error) bool {
var gerr *googleapi.Error
if !errors.As(err, &gerr) {
return false
}
msg := strings.ToLower(gerr.Message)
return strings.Contains(msg, "syntax error") || strings.Contains(msg, "relation") && strings.Contains(msg, "does not exist")
} Try / catch
out, err := src.ExecuteSql(ctx, project, instance, database, sql, token)
if err != nil {
if isSQLSyntaxErr(err) {
return fmt.Errorf("SQL rejected by server (check syntax/objects): %w", err)
}
var gerr *googleapi.Error
if errors.As(err, &gerr) && gerr.Code >= 500 {
return fmt.Errorf("transient failure, consider retry: %w", err)
}
return fmt.Errorf("execute sql failed: %w", err)
} Prevention
- Test statements in a SQL client against the same instance/database before invoking.
- Always pass a valid, existing database name.
- Verify the executing user has grants for the statement type.
- Check instance is RUNNABLE (not in maintenance) before executing.
- Set a statement timeout in the SQL for long-running queries.
When it happens
Trigger: Calling the execute_sql tool with invalid SQL syntax, referencing a non-existent database, using an account without privileges for the statement, or when the instance is stopped/unreachable.
Common situations: Syntax errors in generated SQL; querying a database name that doesn't exist on the instance; executing DDL/DML with a user lacking grants; instance in maintenance or stopped state; statement timeout on long queries.
Related errors
- error cloning instance: %w
- error creating database: %w
- error creating user: %w
- error getting instance: %w
- error listing databases: %w
AI-assisted analysis of googleapis/mcp-toolbox@8cc6e09de2 (2026-09-05).
Data as JSON: /api/errors/e67dc431e6ab2436.
Report an issue: GitHub.