{"record":{"id":"d1d72ad841b73e68","repo":"vitessio/vitess","slug":"unable-to-select-foreign-key-checks","errorCode":null,"errorMessage":"unable to select @@foreign_key_checks","messagePattern":"unable to select @@foreign_key_checks","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"go/vt/vttablet/tabletmanager/vreplication/vreplicator.go","lineNumber":610,"sourceCode":"\t\treturn nil\n\t}\n\tinsertLog(vr.dbClient, LogStateChange, vr.id, state.String(), message)\n\tvr.state = state\n\n\treturn nil\n}\n\nfunc encodeString(in string) string {\n\treturn sqltypes.EncodeStringSQL(in)\n}\n\nfunc (vr *vreplicator) getSettingFKCheck() error {\n\tqr, err := vr.dbClient.Execute(\"select @@foreign_key_checks\")\n\tif err != nil {\n\t\treturn err\n\t}\n\tif len(qr.Rows) != 1 || len(qr.Fields) != 1 {\n\t\treturn errors.New(\"unable to select @@foreign_key_checks\")\n\t}\n\tvr.originalFKCheckSetting, err = qr.Rows[0][0].ToCastInt64()\n\tif err != nil {\n\t\treturn err\n\t}\n\treturn nil\n}\n\nfunc (vr *vreplicator) needFKRestrict() bool {\n\tok, err := vr.dbClient.SupportsCapability(capabilities.RestrictFKOnNonStandardKey)\n\tif err != nil {\n\t\treturn false\n\t}\n\treturn ok\n}\n\nfunc (vr *vreplicator) getSettingFKRestrict() error {\n\tif !vr.needFKRestrict() {","sourceCodeStart":592,"sourceCodeEnd":628,"githubUrl":"https://github.com/vitessio/vitess/blob/01a25a7d176f94613b8d59d799f438380a8760e4/go/vt/vttablet/tabletmanager/vreplication/vreplicator.go#L592-L628","documentation":"getSettingFKCheck reads the current @@foreign_key_checks value from the target mysqld before replicating (it saves the setting, disables FK checks during apply, and restores it). This error is thrown when the query succeeds but returns anything other than exactly one row and one column, which should never happen.","triggerScenarios":"vr.dbClient.Execute(\"select @@foreign_key_checks\") returns a result whose Rows/Fields cardinality is not 1x1 — e.g. an empty/odd result from a misbehaving connection or proxy.","commonSituations":"A connection pool/proxy mangles results; the dbClient connection is in a broken state mid-query; custom MySQL builds or versions where the system variable read behaves oddly.","solutions":["Check the target mysqld health and version — confirm `select @@foreign_key_checks` works in a mysql client on the same instance","Reset/reconnect the vreplication dbClient connection (restart the workflow or vttablet) if the connection returned a corrupted result","If behind a MySQL proxy (e.g. Vitess itself or another proxy), ensure it forwards system-variable SELECTs correctly"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"// Verify the target supports the variable before relying on FK-check save/restore\nqr, err := conn.ExecuteFetch(\"SELECT @@foreign_key_checks\", 1, false)\nif err != nil || len(qr.Rows) != 1 || len(qr.Fields) != 1 {\n    return fmt.Errorf(\"target cannot read @@foreign_key_checks\")\n}","typeGuard":null,"tryCatchPattern":"if err := vr.getSettingFKCheck(); err != nil {\n    if strings.Contains(err.Error(), \"unable to select @@foreign_key_checks\") {\n        // reconnect dbClient and retry once\n        vr.dbClient.Close()\n        if rerr := vr.dbClient.Connect(); rerr == nil { return vr.getSettingFKCheck() }\n    }\n    return err\n}","preventionTips":["Keep the target on a supported MySQL version","Avoid proxies between vttablet and mysqld that mangle results","Monitor vreplication logs for connection corruption"],"tags":["vreplication","foreign-keys","system-variable","dbclient"],"backgroundTag":"unexpected-query-result","analyzedSha":"01a25a7d176f94613b8d59d799f438380a8760e4","analyzedAt":"2026-09-01T17:28:30.605Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}