{"record":{"id":"ab9c5d98e09e0b8e","repo":"bytebase/bytebase","slug":"failed-to-prepare-query-s-ab9c5d","errorCode":null,"errorMessage":"failed to prepare query: %s","messagePattern":"failed to prepare query: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"backend/plugin/db/tidb/sync.go","lineNumber":590,"sourceCode":"func (d *Driver) listPartitionTables(ctx context.Context, databaseName string) (map[db.TableKey][]*storepb.TablePartitionMetadata, error) {\n\tconst query string = `\n\t\tSELECT\n\t\t\tTABLE_NAME,\n\t\t\tPARTITION_NAME,\n\t\t\tSUBPARTITION_NAME,\n\t\t\tPARTITION_METHOD,\n\t\t\tSUBPARTITION_METHOD,\n\t\t\tPARTITION_EXPRESSION,\n\t\t\tSUBPARTITION_EXPRESSION,\n\t\t\tPARTITION_DESCRIPTION\n\t\tFROM INFORMATION_SCHEMA.PARTITIONS\n\t\tWHERE TABLE_SCHEMA = ? AND PARTITION_NAME IS NOT NULL\n\t\tORDER BY TABLE_NAME ASC, PARTITION_NAME ASC, SUBPARTITION_NAME ASC, PARTITION_ORDINAL_POSITION ASC, SUBPARTITION_ORDINAL_POSITION ASC;\n\t`\n\t// Prepare the query statement.\n\tstmt, err := d.db.PrepareContext(ctx, query)\n\tif err != nil {\n\t\treturn nil, errors.Wrapf(err, \"failed to prepare query: %s\", query)\n\t}\n\tdefer stmt.Close()\n\trows, err := stmt.QueryContext(ctx, databaseName)\n\tif err != nil {\n\t\treturn nil, util.FormatErrorWithQuery(err, query)\n\t}\n\tdefer rows.Close()\n\n\ttype partitionKey struct {\n\t\ttableName     string\n\t\tpartitionName string\n\t}\n\n\tpartitionMap := make(map[partitionKey]int)\n\tresult := make(map[db.TableKey][]*storepb.TablePartitionMetadata)\n\n\tfor rows.Next() {\n\t\tvar tableName, partitionName, partitionMethod string","sourceCodeStart":572,"sourceCodeEnd":608,"githubUrl":"https://github.com/bytebase/bytebase/blob/1870550677fe08f0d2a78c07acd27541464eb945/backend/plugin/db/tidb/sync.go#L572-L608","documentation":"listPartitionTables prepares the partition-metadata query against information_schema.PARTITIONS with database.DatabaseScopeToMySQLDatabaseName(...). PrepareContext fails before execution — the SQL could not be compiled/validated by the server or the driver. The wrapper includes the full query text for diagnosis.","triggerScenarios":"Calling listPartitionTables when the database connection is broken/closed, the context is canceled before prepare completes, or the server rejects the statement (rare for static SQL; more commonly a dead connection or driver issue).","commonSituations":"Connection dropped by TiDB or a proxy between syncs; context deadline exceeded while the server is overloaded; using a connection pool after the database was restarted; unix-socket vs TCP misconfiguration making the server unreachable at prepare time.","solutions":["Retry the sync — prepare failures over a healthy network are usually transient connection issues.","Verify connectivity to TiDB (ping/Telnet to host:port) and check that credentials and the DSN are valid.","Check server logs for aborted connections; raise proxy/LB idle timeouts if connections are being dropped.","Inspect the wrapped error text — if it names SQL syntax or privilege problems, fix those specifically (though this static query rarely fails for those reasons)."],"exampleFix":null,"handlingStrategy":"retry","validationCode":"// check connectivity before sync\nif err := sqlDB.PingContext(ctx); err != nil {\n\treturn fmt.Errorf(\"database unreachable before sync: %w\", err)\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Ping the connection before running metadata syncs","Set sane context deadlines and retry once on transient errors","Keep connection pool settings (ConnMaxLifetime) shorter than proxy idle timeouts","Watch TiDB server logs for connection aborts"],"tags":["database","tidb","prepare-statement","network"],"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"}