{"record":{"id":"9fe0b369183a6c1c","repo":"googleapis/mcp-toolbox","slug":"unable-to-execute-query-w-9fe0b3","errorCode":null,"errorMessage":"unable to execute query: %w","messagePattern":"unable to execute query: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/sources/cloudsqlmysql/cloud_sql_mysql.go","lineNumber":137,"sourceCode":"\tif err := s.MySQLPool().QueryRowContext(ctx, \"SHOW VARIABLES LIKE 'performance_schema'\").Scan(&name, &value); err != nil {\n\t\treturn false, err\n\t}\n\treturn value == \"ON\", nil\n}\n\nfunc (s *Source) RetrieveSourceVersion(ctx context.Context) (string, error) {\n\tvar version string\n\tif err := s.MySQLPool().QueryRowContext(ctx, \"SELECT VERSION()\").Scan(&version); err != nil {\n\t\treturn \"\", err\n\t}\n\treturn version, nil\n}\n\nfunc (s *Source) RunSQL(ctx context.Context, statement string, params []any) (any, error) {\n\tstatement = sqlcommenter.PrependComment(ctx, statement, SourceType, s.SQLCommenter)\n\tresults, err := s.MySQLPool().QueryContext(ctx, statement, params...)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"unable to execute query: %w\", err)\n\t}\n\tdefer results.Close()\n\n\tcols, err := results.Columns()\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"unable to retrieve rows column name: %w\", err)\n\t}\n\n\t// create an array of values for each column, which can be re-used to scan each row\n\trawValues := make([]any, len(cols))\n\tvalues := make([]any, len(cols))\n\tfor i := range rawValues {\n\t\tvalues[i] = &rawValues[i]\n\t}\n\n\tcolTypes, err := results.ColumnTypes()\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"unable to get column types: %w\", err)","sourceCodeStart":119,"sourceCodeEnd":155,"githubUrl":"https://github.com/googleapis/mcp-toolbox/blob/8cc6e09de2ad7b8bffc77751799585a1401a48eb/internal/sources/cloudsqlmysql/cloud_sql_mysql.go#L119-L155","documentation":"RunSQL wraps any error returned by database/sql QueryContext with this message. It means MySQL rejected or failed to run the SQL statement — including connection failures at query time, syntax errors, unknown tables/columns, permission denials, and context cancellations (timeouts). The statement has sqlcommenter comments prepended before execution.","triggerScenarios":"Calling the execute_sql tool / Source.RunSQL with: syntactically invalid SQL, references to non-existent tables or columns, a user without the needed privileges, an empty result-set read failing at connection level, or the context being canceled while executing.","commonSituations":"LLM-generated SQL with typos, querying a table that exists in a different database, read-only user lacking SELECT grants, query exceeding a client timeout (context deadline exceeded), server closing idle connections, reserved-word misuse in identifiers.","solutions":["Inspect the wrapped MySQL error in the message: fix SQL syntax or identifier names (1054 unknown column, 1146 unknown table, 1064 syntax error).","Grant the configured user the required privileges (e.g. SELECT on the target schema).","Run the statement directly with a mysql client to confirm it is valid against the target database.","If it is a timeout, increase the request/context timeout or optimize the query (add indexes, add LIMIT).","If connections are dropping, check instance max_connections and network idle timeouts."],"exampleFix":"// before\nSELECT nam FROM users;\n// after\nSELECT name FROM users;","handlingStrategy":"try-catch","validationCode":"func validateSQL(statement, database string) error {\n    s := strings.TrimSpace(statement)\n    if s == \"\" {\n        return fmt.Errorf(\"empty statement\")\n    }\n    lower := strings.ToLower(s)\n    if !strings.HasPrefix(lower, \"select\") && !strings.HasPrefix(lower, \"show\") &&\n        !strings.HasPrefix(lower, \"describe\") && !strings.HasPrefix(lower, \"explain\") {\n        return fmt.Errorf(\"only read statements are supported\")\n    }\n    return nil\n}","typeGuard":"func isMySQLErrorCode(err error, codes ...uint16) bool {\n    var mysqlErr *mysql.MySQLError\n    if errors.As(err, &mysqlErr) {\n        for _, c := range codes {\n            if mysqlErr.Number == c {\n                return true\n            }\n        }\n    }\n    return false\n}","tryCatchPattern":"result, err := src.RunSQL(ctx, statement, params)\nif err != nil {\n    var mysqlErr *mysql.MySQLError\n    switch {\n    case errors.As(err, &mysqlErr) && mysqlErr.Number == 1146:\n        // unknown table: hint the caller to list tables\n    case errors.As(err, &mysqlErr) && mysqlErr.Number == 1064:\n        // syntax error: surface mysqlErr.Message back to the LLM to fix SQL\n    case errors.Is(err, context.DeadlineExceeded):\n        // timeout: retry with a larger deadline or add LIMIT\n    }\n    return err\n}","preventionTips":["Use tools like list_tables to discover actual table/column names before writing SQL.","Run ad-hoc statements against a staging database with a read-only user first.","Keep queries bounded with LIMIT and appropriate indexes to avoid context timeouts.","Use fully qualified names (schema.table) to avoid wrong-database mistakes.","Grant the configured user exactly the privileges the tools need (SELECT) and no more."],"tags":["mysql","sql","query-execution","cloudsql"],"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-08T15:18:49.778Z"}