Common SQL Errors

MySQL Error 1686: ER_STORED_FUNCTION_PREVENTS_SWITCH_BINLOG_DIRECT - How to Fix and Prevent

Galaxy Team
August 7, 2025

<p>The server blocks switching binlog_direct_non_transactional_updates inside a stored function or trigger, raising error 1686.</p>

Sign up for the latest in common SQL errors from the Galaxy Team!
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.

What is MySQL error 1686?

<p>MySQL Error 1686: ER_STORED_FUNCTION_PREVENTS_SWITCH_BINLOG_DIRECT occurs when a stored function or trigger tries to change the binlog_direct_non_transactional_updates flag. Move the SET statement outside the routine or drop and recreate the routine without that statement to resolve the issue.</p>

Error Highlights

Typical Error Message

Cannot change the binlog direct flag inside a stored

Error Type

Replication Configuration Error

Language

MySQL

Symbol

ER_STORED_FUNCTION_PREVENTS_SWITCH_BINLOG_DIRECT

Error Code

1686

SQL State

HY000

Explanation

Table of Contents

What is MySQL error 1686?

MySQL raises error 1686 with the condition name ER_STORED_FUNCTION_PREVENTS_SWITCH_BINLOG_DIRECT when code inside a stored function, procedure, or trigger attempts to change the session variable binlog_direct_non_transactional_updates.

The variable controls whether non transactional tables write directly to the binary log. MySQL does not allow toggling that flag within stored program context, so the statement fails immediately.

What Causes This Error?

The error is triggered by any SET statement that targets binlog_direct_non_transactional_updates while the call stack is inside a stored routine or trigger.

It often appears in migration scripts that wrap DDL in procedures, or in replication maintenance code that tries to enable direct logging temporarily.

How to Fix MySQL Error 1686: ER_STORED_FUNCTION_PREVENTS_SWITCH_BINLOG_DIRECT

Remove the SET statement from the routine, or refactor logic so the session variable is changed outside the stored program. An external wrapper script or application layer call can set the variable, invoke the routine, then reset the variable afterward.

If the routine must manipulate non transactional tables, leave binlog_direct_non_transactional_updates unchanged and redesign the logging strategy or convert the tables to transactional engines like InnoDB.

Common Scenarios and Solutions

Backup utilities sometimes create temporary procedures that enable the flag. Rewrite those utilities to run SET commands before creating the procedure.

Triggers that log to MyISAM tables may attempt to toggle the flag. Move logging logic to application code or use AFTER triggers without modifying the variable.

Best Practices to Avoid This Error

Never include SET binlog_direct_non_transactional_updates statements inside stored routines. Document the restriction in code reviews.

Use transactional storage engines whenever possible so the flag never needs to be toggled. Monitor new procedures for forbidden SET statements using static analysis tools or a code linter in Galaxy.

Related Errors and Solutions

Error 1235 (ER_NOT_SUPPORTED_YET) can appear when attempting other unsupported operations inside triggers.

Error 1475 (ER_CANT_SET_GTID_PURGED_WHILE_SLAVE_STILL_RUNNING) is similar, blocking GTID changes in restricted contexts.

Common Causes

Stored routine contains SET binlog_direct_non_transactional_updates

Direct assignment inside a function or procedure immediately triggers error 1686.

Trigger tries to modify the flag

BEFORE or AFTER triggers that attempt to optimize logging by toggling the variable will fail.

Generated code from migration tools

Some auto generated scripts wrap DDL in procedures that set the flag without considering the restriction.

Related Errors

Error 1598: ER_BINLOG_PURGED_FLAG

Raised when attempting binary log changes that conflict with GTID enforcement.

Error 1442: ER_TRG_CANT_CHANGE_ROW

Occurs when a trigger tries to update the same table it monitors.

Error 1235: ER_NOT_SUPPORTED_YET

General error for unsupported operations inside stored routines.

FAQs

Can I safely ignore error 1686?

No. The server blocks the operation, so ignoring the error means your routine will never run as intended.

Does the error occur in MySQL 5.7 and 8.0?

Yes. The restriction exists in all supported versions that implement the binlog_direct_non_transactional_updates variable.

Will changing storage engines remove the need to toggle the flag?

Switching non transactional tables to InnoDB removes the requirement, eliminating the error source.

How does Galaxy help prevent this error?

Galaxy highlights session variable statements during code review and lets teams endorse corrected routines, reducing the chance of committing invalid SET commands.

Start Querying with the Modern SQL Editor Today!
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.

Check out some other errors

Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie Logo
Bauhealth Logo
Truvideo Logo