{"record":{"id":"e67dc431e6ab2436","repo":"googleapis/mcp-toolbox","slug":"error-executing-sql-w","errorCode":null,"errorMessage":"error executing sql: %w","messagePattern":"error executing sql: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/sources/cloudsqladmin/cloud_sql_admin.go","lineNumber":316,"sourceCode":"\t\t})\n\t}\n\treturn instances, nil\n}\n\nfunc (s *Source) ExecuteSql(ctx context.Context, project, instance, database, sql string, accessToken string) (any, error) {\n\tservice, err := s.GetService(ctx, accessToken)\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\n\treq := &sqladmin.ExecuteSqlPayload{\n\t\tDatabase:     database,\n\t\tSqlStatement: sql,\n\t}\n\n\tresp, err := service.Instances.ExecuteSql(project, instance, req).Do()\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"error executing sql: %w\", err)\n\t}\n\treturn resp, nil\n}\n\nfunc (s *Source) CreateInstance(ctx context.Context, project, name, dbVersion, rootPassword string, settings sqladmin.Settings, accessToken string) (any, error) {\n\tinstance := sqladmin.DatabaseInstance{\n\t\tName:            name,\n\t\tDatabaseVersion: dbVersion,\n\t\tRootPassword:    rootPassword,\n\t\tSettings:        &settings,\n\t\tProject:         project,\n\t}\n\n\tservice, err := s.GetService(ctx, accessToken)\n\tif err != nil {\n\t\treturn nil, err\n\t}\n","sourceCodeStart":298,"sourceCodeEnd":334,"githubUrl":"https://github.com/googleapis/mcp-toolbox/blob/8cc6e09de2ad7b8bffc77751799585a1401a48eb/internal/sources/cloudsqladmin/cloud_sql_admin.go#L298-L334","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nExecuteSql(ctx, project, instance, \"\", \"SELECT * FROM users\", token) // empty database\n// after\nExecuteSql(ctx, project, instance, \"postgres\", \"SELECT * FROM users\", token)","handlingStrategy":"try-catch","validationCode":"sql := strings.TrimSpace(sqlStatement)\nif sql == \"\" {\n    return fmt.Errorf(\"sql statement is required\")\n}\nif database == \"\" {\n    return fmt.Errorf(\"database is required for execute_sql\")\n}\nfor _, stmt := range forbiddenPatterns.MatchString(sql) { /* block destructive ops if policy requires */ }","typeGuard":"func isSQLSyntaxErr(err error) bool {\n    var gerr *googleapi.Error\n    if !errors.As(err, &gerr) {\n        return false\n    }\n    msg := strings.ToLower(gerr.Message)\n    return strings.Contains(msg, \"syntax error\") || strings.Contains(msg, \"relation\") && strings.Contains(msg, \"does not exist\")\n}","tryCatchPattern":"out, err := src.ExecuteSql(ctx, project, instance, database, sql, token)\nif err != nil {\n    if isSQLSyntaxErr(err) {\n        return fmt.Errorf(\"SQL rejected by server (check syntax/objects): %w\", err)\n    }\n    var gerr *googleapi.Error\n    if errors.As(err, &gerr) && gerr.Code >= 500 {\n        return fmt.Errorf(\"transient failure, consider retry: %w\", err)\n    }\n    return fmt.Errorf(\"execute sql failed: %w\", err)\n}","preventionTips":["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."],"tags":["gcp","cloud-sql","sql","api-error"],"backgroundTag":"sql-execution-failed","analyzedSha":"8cc6e09de2ad7b8bffc77751799585a1401a48eb","analyzedAt":"2026-09-05T01:10:36.887Z","contentChangedAt":"2026-09-05T01:10:36.887Z","schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}