{"record":{"id":"08d4d69652b2e854","repo":"vitessio/vitess","slug":"failed-to-set-session-foreign-key-checks-w","errorCode":null,"errorMessage":"failed to set session foreign_key_checks: %w","messagePattern":"failed to set session foreign_key_checks: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"go/vt/vttablet/tabletmanager/vreplication/vplayer.go","lineNumber":242,"sourceCode":"\t\t// the copy phase, i.e., for catchup and fastforward.\n\t\tmustUpdate = true\n\t} else if vp.vr.state == binlogdatapb.VReplicationWorkflowState_Running {\n\t\t// If the vreplication workflow is in Running state, we must update the foreign_key_checks\n\t\t// state for all workflow types.\n\t\tmustUpdate = true\n\t}\n\tif !mustUpdate {\n\t\treturn nil\n\t}\n\tdbForeignKeyChecksEnabled := flags2&NoForeignKeyCheckFlagBitmask != NoForeignKeyCheckFlagBitmask\n\n\tif vp.foreignKeyChecksStateInitialized /* already set earlier */ &&\n\t\tdbForeignKeyChecksEnabled == vp.foreignKeyChecksEnabled /* no change in the state, no need to update */ {\n\t\treturn nil\n\t}\n\tlog.Info(\"Setting this session's foreign_key_checks to \" + strconv.FormatBool(dbForeignKeyChecksEnabled))\n\tif _, err := vp.query(ctx, \"set @@session.foreign_key_checks=\"+strconv.FormatBool(dbForeignKeyChecksEnabled)); err != nil {\n\t\treturn fmt.Errorf(\"failed to set session foreign_key_checks: %w\", err)\n\t}\n\tvp.foreignKeyChecksEnabled = dbForeignKeyChecksEnabled\n\tif !vp.foreignKeyChecksStateInitialized {\n\t\tlog.Info(\"First foreign_key_checks update to: \" + strconv.FormatBool(dbForeignKeyChecksEnabled))\n\t\tvp.foreignKeyChecksStateInitialized = true\n\t}\n\treturn nil\n}\n\n// runWithRecover invokes fn and converts any panic into an error so the\n// caller's child goroutine doesn't crash the entire vttablet process. The\n// panic value and stack are logged with workflow context. Used to wrap\n// fetchAndApply's child goroutine bodies (#20360) which would otherwise\n// escape controller.runBlp's recover (that runs on a different goroutine).\nfunc runWithRecover(workflow, where string, fn func() error) (err error) {\n\tdefer func() {\n\t\tif x := recover(); x != nil {\n\t\t\tlog.Error(\"caught panic\",","sourceCodeStart":224,"sourceCodeEnd":260,"githubUrl":"https://github.com/vitessio/vitess/blob/01a25a7d176f94613b8d59d799f438380a8760e4/go/vt/vttablet/tabletmanager/vreplication/vplayer.go#L224-L260","documentation":"The vplayer wraps a failure to execute SET @@session.foreign_key_checks=<true|false> on the target MySQL session. VReplication toggles foreign key checks per row event (using the event's flags) so that replicated rows can be applied in dependency order without violating FK constraints on the target. If this session statement fails, the row event cannot be applied safely.","triggerScenarios":"vp.query(ctx, \"set @@session.foreign_key_checks=...\") returns an error during updateFKCheck, invoked from applyRowEvent for a row event whose flags indicate the FK state must change (or on the first event when state is uninitialized).","commonSituations":"Target MySQL/MariaDB connection dropped mid-stream; target database in a read-only or super_read_only state; an external process killing the vreplication session; restricted privileges on the replication user (though FK checks usually needs none, restricted setups may fail).","solutions":["Check the target tablet's MySQL error log and the wrapped error for the root cause (connection lost, read-only, etc.)","Verify the target MySQL server is writable and the vreplication user session is healthy; restart the workflow / RestoreData if the connection was lost","If targeting MariaDB or an old MySQL flavor, confirm @@session.foreign_key_checks is a supported variable in that flavor","Check for external session-killers (proxies, DBAs, max_execution_time) that terminate long-lived vreplication connections"],"exampleFix":null,"handlingStrategy":"retry","validationCode":"// Check the target is healthy and writable before starting the workflow\nout, _ := exec.Command(\"mysql\", \"-h\", targetHost, \"-e\", \"SELECT @@foreign_key_checks, @@read_only\").Output()\n// Expect: 1, 0 — writable with FK checks supported","typeGuard":null,"tryCatchPattern":"if err := applyRowEvent(ctx, ev); err != nil {\n    var retryable = strings.Contains(err.Error(), \"failed to set session foreign_key_checks\")\n    if retryable && isConnectionError(err) {\n        // rely on vreplication's built-in retry/backoff; monitor via _vt.vreplication Message\n    }\n}","preventionTips":["Keep target tablets reachable and MySQL sessions free of external killers (proxies, DBA kill scripts)","Ensure the target is not left in read_only/super_read_only during vreplication","Verify binlog/flavor support for foreign_key_checks on MariaDB or older MySQL targets","Monitor _vt.vreplication Message/State for stalled streams"],"tags":["vreplication","mysql","foreign-keys","session-variable"],"backgroundTag":"mysql-session-variable-set-failed","analyzedSha":"01a25a7d176f94613b8d59d799f438380a8760e4","analyzedAt":"2026-09-01T17:28:30.605Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}