bytebase/bytebase · error

failed to get execution plan

Error message

failed to get execution plan

What it means

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'.

Source

Thrown at backend/plugin/db/mssql/mssql.go:460

		}
		// Ensure explain is turned off after processing
		defer func() {
			if _, 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
				slog.Warn("failed to disable explain mode", log.BBError(err))
			}
		}()

		var results []*v1pb.QueryResult

		// Process each statement with explain enabled
		for _, singleSQL := range singleSQLs {
			startTime := time.Now()

			queryResult, err := func() (*v1pb.QueryResult, error) {
				// Execute query to get execution plan
				rows, err := conn.QueryContext(ctx, singleSQL.Text)
				if err != nil {
					return nil, errors.Wrap(err, "failed to get execution plan")
				}
				defer rows.Close()

				// Convert to query result
				r, err := util.RowsToQueryResult(rows, makeValueByTypeName, convertValue, queryContext.MaximumSQLResultSize)
				if err != nil {
					return nil, errors.Wrap(err, "failed to convert execution plan results")
				}

				if err = rows.Err(); err != nil {
					return nil, errors.Wrap(err, "error after processing rows")
				}

				return r, nil
			}()

			stop := false
			if err != nil {

View on GitHub (pinned to 1870550677)

Solutions

  1. Read the underlying wrapped SQL Server error for the real cause
  2. Fix the SQL syntax or missing object references
  3. Verify permissions to execute the statement
  4. Check the statement type is compatible with SHOWPLAN modes

Example fix

null
Defensive patterns

Strategy: validation

Validate before calling

// validate SQL compiles before explain: check referenced objects exist
for _, obj := range referencedTables(sql) { if !tableExists(ctx, conn, obj) { return fmt.Errorf("unknown table %s", obj) } }

Try / catch

res, err := driver.QueryConn(ctx, conn, sql, explainCtx)
if err != nil && strings.Contains(err.Error(), "failed to get execution plan") {
    return nil, fmt.Errorf("SQL not explainable, see cause: %w", err)
}

Prevention

When it happens

Trigger: 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.

Common situations: 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).

Understand the failure class

Background: "query failed", "%w: SQL error" — wrapped database query errors in Go libraries explained — this error's family across 3 libraries.

Related errors


AI-assisted analysis of bytebase/bytebase@1870550677 (2026-09-06). Data as JSON: /api/errors/c8b51df26d6fb570. Report an issue: GitHub.