{"record":{"id":"c8b51df26d6fb570","repo":"bytebase/bytebase","slug":"failed-to-get-execution-plan","errorCode":null,"errorMessage":"failed to get execution plan","messagePattern":"failed to get execution plan","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"backend/plugin/db/mssql/mssql.go","lineNumber":460,"sourceCode":"\t\t}\n\t\t// Ensure explain is turned off after processing\n\t\tdefer func() {\n\t\t\tif _, err := conn.ExecContext(ctx, fmt.Sprintf(\"SET %s OFF\", explain)); err != nil { // NOSONAR(go:S2077) explain is a hardcoded constant (\"SHOWPLAN_ALL\" or \"SHOWPLAN_XML\"), not user input\n\t\t\t\tslog.Warn(\"failed to disable explain mode\", log.BBError(err))\n\t\t\t}\n\t\t}()\n\n\t\tvar results []*v1pb.QueryResult\n\n\t\t// Process each statement with explain enabled\n\t\tfor _, singleSQL := range singleSQLs {\n\t\t\tstartTime := time.Now()\n\n\t\t\tqueryResult, err := func() (*v1pb.QueryResult, error) {\n\t\t\t\t// Execute query to get execution plan\n\t\t\t\trows, err := conn.QueryContext(ctx, singleSQL.Text)\n\t\t\t\tif err != nil {\n\t\t\t\t\treturn nil, errors.Wrap(err, \"failed to get execution plan\")\n\t\t\t\t}\n\t\t\t\tdefer rows.Close()\n\n\t\t\t\t// Convert to query result\n\t\t\t\tr, err := util.RowsToQueryResult(rows, makeValueByTypeName, convertValue, queryContext.MaximumSQLResultSize)\n\t\t\t\tif err != nil {\n\t\t\t\t\treturn nil, errors.Wrap(err, \"failed to convert execution plan results\")\n\t\t\t\t}\n\n\t\t\t\tif err = rows.Err(); err != nil {\n\t\t\t\t\treturn nil, errors.Wrap(err, \"error after processing rows\")\n\t\t\t\t}\n\n\t\t\t\treturn r, nil\n\t\t\t}()\n\n\t\t\tstop := false\n\t\t\tif err != nil {","sourceCodeStart":442,"sourceCodeEnd":478,"githubUrl":"https://github.com/bytebase/bytebase/blob/1870550677fe08f0d2a78c07acd27541464eb945/backend/plugin/db/mssql/mssql.go#L442-L478","documentation":"In explain mode, after enabling SHOWPLAN, the driver re-runs the statement to fetch its execution plan. If QueryContext returns an error for the statement itself, it is wrapped as 'failed to get execution plan'.","triggerScenarios":"Running QueryConn in explain mode where the SQL itself fails to compile or execute under SHOWPLAN_ALL/SHOWPLAN_XML — syntax errors, missing objects, permission issues.","commonSituations":"Explaining a query referencing a non-existent table or column; T-SQL syntax error; insufficient permissions to compile the plan; statements not supported by SHOWPLAN (e.g. some DDL).","solutions":["Read the underlying wrapped SQL Server error for the real cause","Fix the SQL syntax or missing object references","Verify permissions to execute the statement","Check the statement type is compatible with SHOWPLAN modes"],"exampleFix":"null","handlingStrategy":"validation","validationCode":"// validate SQL compiles before explain: check referenced objects exist\nfor _, obj := range referencedTables(sql) { if !tableExists(ctx, conn, obj) { return fmt.Errorf(\"unknown table %s\", obj) } }","typeGuard":null,"tryCatchPattern":"res, err := driver.QueryConn(ctx, conn, sql, explainCtx)\nif err != nil && strings.Contains(err.Error(), \"failed to get execution plan\") {\n    return nil, fmt.Errorf(\"SQL not explainable, see cause: %w\", err)\n}","preventionTips":["Fix syntax errors before requesting explain","Verify referenced objects exist","Check permissions for the explaining user"],"tags":["mssql","explain","execution-plan","sql-error"],"backgroundTag":"sql-query-failed","analyzedSha":"1870550677fe08f0d2a78c07acd27541464eb945","analyzedAt":"2026-09-06T21:16:13.665Z","contentChangedAt":"2026-09-06T21:16:13.665Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}