{"record":{"id":"b93d19322a00ccf8","repo":"googleapis/mcp-toolbox","slug":"unable-to-execute-query-w","errorCode":null,"errorMessage":"unable to execute query: %w","messagePattern":"unable to execute query: %w","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/sources/alloydbpg/alloydb_pg.go","lineNumber":122,"sourceCode":"}\n\nfunc (s *Source) SourceType() string {\n\treturn SourceType\n}\n\nfunc (s *Source) ToConfig() sources.SourceConfig {\n\treturn s.Config\n}\n\nfunc (s *Source) PostgresPool() *pgxpool.Pool {\n\treturn s.Pool\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.Pool.Query(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\tfields := results.FieldDescriptions()\n\tout := []any{}\n\tfor results.Next() {\n\t\tv, err := results.Values()\n\t\tif err != nil {\n\t\t\treturn nil, fmt.Errorf(\"unable to parse row: %w\", err)\n\t\t}\n\t\trow := orderedmap.Row{}\n\t\tfor i, f := range fields {\n\t\t\tval := sources.NormalizeValue(v[i], f.DataTypeOID)\n\t\t\trow.Add(f.Name, val)\n\t\t}\n\t\tout = append(out, row)\n\t}\n\t// this will catch actual query execution errors","sourceCodeStart":104,"sourceCodeEnd":140,"githubUrl":"https://github.com/googleapis/mcp-toolbox/blob/8cc6e09de2ad7b8bffc77751799585a1401a48eb/internal/sources/alloydbpg/alloydb_pg.go#L104-L140","documentation":"RunSQL executes a statement (with an optional sqlcommenter comment prepended) through the pgx pool. This error is returned when pool.Query fails — i.e. the query could not be started: syntax errors, unknown tables/columns, permission denials, or connection drops at query submission time.","triggerScenarios":"Calling RunSQL with SQL that Postgres rejects at parse/plan/start time: invalid syntax, nonexistent relation, missing SELECT permission, or a connection lost before results stream.","commonSituations":"LLM-generated SQL referencing wrong schema/table names, tool user lacking grants on target tables, typo in column names, or the DB connection dropped mid-session.","solutions":["Read the wrapped Postgres error for the exact SQLSTATE/message.","Run the statement manually in psql to reproduce and fix syntax/schema issues.","Grant the configured user the required privileges on the schema/tables.","Verify connection health; if connections are dropping, check network/idle timeouts.","For LLM-driven usage, improve tool descriptions so the model produces valid SQL against the actual schema."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"-- Validate identifiers/schema before running generated SQL\nSELECT table_schema, table_name FROM information_schema.tables\nWHERE table_schema = 'public' AND table_name = 'my_table';\n-- check grants\nSELECT has_table_privilege('app_user', 'my_table', 'SELECT');","typeGuard":null,"tryCatchPattern":"out, err := src.RunSQL(ctx, stmt, params)\nif err != nil {\n    var pgErr *pgconn.PgError\n    if errors.As(err, &pgErr) {\n        log.Printf(\"SQL failed: code=%s message=%s\", pgErr.Code, pgErr.Message)\n    }\n    return fmt.Errorf(\"run sql failed: %w\", err)\n}","preventionTips":["Test statements in psql before production use","Grant the configured DB user least-privilege access up front","Use fully qualified table names (schema.table)","Constrain LLM-generated SQL to documented schemas via tool descriptions"],"tags":["go","sql","postgres","query-execution","alloydb"],"backgroundTag":"sql-query-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"}