{"record":{"id":"52acf5f18b1f2902","repo":"vitessio/vitess","slug":"could-not-get-the-original-sql-mode-on-target-v","errorCode":null,"errorMessage":"could not get the original sql_mode on target: %v","messagePattern":"could not get the original sql_mode on target: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"go/vt/vttablet/tabletmanager/vreplication/vreplicator.go","lineNumber":664,"sourceCode":"\t_, err := dbClient.Execute(fmt.Sprintf(\"set @@session.foreign_key_checks=%d\", vr.originalFKCheckSetting))\n\treturn err\n}\n\nfunc (vr *vreplicator) resetFKRestrictAfterCopy(dbClient *vdbClient) error {\n\tif !vr.needFKRestrict() {\n\t\treturn nil\n\t}\n\t_, err := dbClient.Execute(fmt.Sprintf(\"set @@session.restrict_fk_on_non_standard_key=%d\", vr.originalFKRestrict))\n\treturn err\n}\n\nfunc (vr *vreplicator) setSQLMode(ctx context.Context, dbClient *vdbClient) (func(), error) {\n\tresetFunc := func() {}\n\t// First save the original SQL mode if we have not already done so\n\tif vr.originalSQLMode == \"\" {\n\t\tres, err := dbClient.Execute(getSQLModeQuery)\n\t\tif err != nil || len(res.Rows) != 1 {\n\t\t\treturn resetFunc, fmt.Errorf(\"could not get the original sql_mode on target: %v\", err)\n\t\t}\n\t\tvr.originalSQLMode = res.Named().Row().AsString(\"sql_mode\", \"\")\n\t}\n\n\t// Create a callback function for resetting the original\n\t// SQL mode back at the end of the vreplication operation.\n\t// You should defer this callback wherever you call setSQLMode()\n\tresetFunc = func() {\n\t\tquery := fmt.Sprintf(setSQLModeQueryf, vr.originalSQLMode)\n\t\t_, err := dbClient.Execute(query)\n\t\tif err != nil {\n\t\t\tlog.Warn(fmt.Sprintf(\"Could not reset sql_mode on target using %s: %v\", query, err))\n\t\t}\n\t}\n\tvreplicationSQLMode := SQLMode\n\tsettings, _, err := vr.readSettings(ctx, dbClient)\n\tif err != nil {\n\t\treturn resetFunc, err","sourceCodeStart":646,"sourceCodeEnd":682,"githubUrl":"https://github.com/vitessio/vitess/blob/01a25a7d176f94613b8d59d799f438380a8760e4/go/vt/vttablet/tabletmanager/vreplication/vreplicator.go#L646-L682","documentation":"setSQLMode first saves the target's current sql_mode so it can restore it later. It runs 'SELECT @@session.sql_mode' and requires exactly one row; if the query errors or returns an unexpected row count, this error is raised. The vreplication copy phase cannot proceed safely without capturing (then overriding) sql_mode.","triggerScenarios":"Execute(getSQLModeQuery) returns an error (connection issue, permissions) OR len(res.Rows) != 1 — note a query error and empty result produce the same message, with err=nil in the empty-result case. Called by replicate and newClientConnection.","commonSituations":"Broken or just-reconnected MySQL connection; a proxy stripping or altering the SELECT result; mysqld restarted mid-copy; permission restrictions blocking @@session reads (rare, e.g. hardened setups).","solutions":["Check the embedded cause (%v): if it is blank, the query succeeded but returned zero rows — investigate any proxy/intermediary between tablet and mysqld.","Verify tablet-to-MySQL connectivity and that mysqld is up; reconnect/restart the tablet if the session was lost.","Run 'SELECT @@session.sql_mode' manually as the tablet's MySQL user to confirm access.","Restart the workflow so setSQLMode runs on a fresh, healthy connection."],"exampleFix":null,"handlingStrategy":"retry","validationCode":"// Verify the tablet's MySQL user can read session sql_mode:\n// mysql -u <vtuser> -p -h <target> -e \"SELECT @@session.sql_mode;\"\n// Expect exactly one row with one column.","typeGuard":null,"tryCatchPattern":"res, err := dbClient.Execute(getSQLModeQuery)\nif err != nil || len(res.Rows) != 1 {\n\t// reconnect or restart the stream; if err == nil, an intermediary\n\t// is mangling the result — check for proxies\n\treturn resetFunc, retryAfterReconnect()\n}","preventionTips":["Keep the tablet-mysqld connection healthy; monitor connection errors.","Do not insert proxies that alter result sets of session-variable SELECTs.","Grant the vt_dba-style user full session variable read access.","Restart workflows after mysqld restarts instead of continuing on stale sessions."],"tags":["vreplication","mysql","sql-mode"],"backgroundTag":"sql-mode-read-failed","analyzedSha":"01a25a7d176f94613b8d59d799f438380a8760e4","analyzedAt":"2026-09-01T17:28:30.605Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}