vitessio/vitess · error

unable to select @@session.restrict_fk_on_non_standard_key

Error message

unable to select @@session.restrict_fk_on_non_standard_key

What it means

getSettingFKRestrict reads @@session.restrict_fk_on_non_standard_key (Vitess/MySQL FK restriction setting) from the target before applying replication, saving the original value. The error is returned when the result is not exactly one row and one column.

Source

Thrown at go/vt/vttablet/tabletmanager/vreplication/vreplicator.go:636

func (vr *vreplicator) needFKRestrict() bool {
	ok, err := vr.dbClient.SupportsCapability(capabilities.RestrictFKOnNonStandardKey)
	if err != nil {
		return false
	}
	return ok
}

func (vr *vreplicator) getSettingFKRestrict() error {
	if !vr.needFKRestrict() {
		return nil
	}
	qr, err := vr.dbClient.Execute("select @@session.restrict_fk_on_non_standard_key")
	if err != nil {
		return err
	}
	if len(qr.Rows) != 1 || len(qr.Fields) != 1 {
		return errors.New("unable to select @@session.restrict_fk_on_non_standard_key")
	}
	vr.originalFKRestrict, err = qr.Rows[0][0].ToCastInt64()
	if err != nil {
		return err
	}
	return nil
}

func (vr *vreplicator) resetFKCheckAfterCopy(dbClient *vdbClient) error {
	_, err := dbClient.Execute(fmt.Sprintf("set @@session.foreign_key_checks=%d", vr.originalFKCheckSetting))
	return err
}

func (vr *vreplicator) resetFKRestrictAfterCopy(dbClient *vdbClient) error {
	if !vr.needFKRestrict() {
		return nil
	}
	_, err := dbClient.Execute(fmt.Sprintf("set @@session.restrict_fk_on_non_standard_key=%d", vr.originalFKRestrict))

View on GitHub (pinned to 01a25a7d17)

Solutions

  1. Verify the target server supports restrict_fk_on_non_standard_key (check mysqld version and Vitess compatibility)
  2. Test `select @@session.restrict_fk_on_non_standard_key` manually against the target; if unknown-variable errors occur, upgrade or point replication at a supported target
  3. Reconnect/restart vreplication (or vttablet) if the result shape was corrupted by a stale connection
Defensive patterns

Strategy: validation

Validate before calling

// Confirm the target understands the session variable before starting vreplication
qr, err := conn.ExecuteFetch("SELECT @@session.restrict_fk_on_non_standard_key", 1, false)
if err != nil {
    return fmt.Errorf("target does not support restrict_fk_on_non_standard_key: %v", err)
}

Try / catch

if err := vr.getSettingFKRestrict(); err != nil {
    if strings.Contains(err.Error(), "restrict_fk_on_non_standard_key") {
        log.Warn("target missing FK restrict variable", slog.Any("error", err))
    }
    return err
}

Prevention

When it happens

Trigger: vr.dbClient.Execute("select @@session.restrict_fk_on_non_standard_key") returns a result with Rows/Fields cardinality != 1x1 — most commonly an unknown-variable error path variant or an empty result set.

Common situations: Target mysqld/MySQL version that doesn't define restrict_fk_on_non_standard_key (older versions return an error, which surfaces as the wrapped query error instead); running replication against a non-Vitess MySQL without the variable; broken connection/proxy.

Related errors


AI-assisted analysis of vitessio/vitess@01a25a7d17 (2026-09-01). Data as JSON: /api/errors/89b6326d3defa3cc. Report an issue: GitHub.