Common SQL Errors

MySQL Error 1715: ER_BINLOG_UNSAFE_INSERT_SELECT_UPDATE - How to Fix and Prevent

Galaxy Team
August 7, 2025

<p>MySQL raises error 1715 when an INSERT … SELECT … ON DUPLICATE KEY UPDATE statement is considered unsafe for the binary log because its outcome depends on an unpredictable row order.</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 1715 (ER_BINLOG_UNSAFE_INSERT_SELECT_UPDATE)?

<p>MySQL Error 1715 (ER_BINLOG_UNSAFE_INSERT_SELECT_UPDATE) signals that INSERT … SELECT … ON DUPLICATE KEY UPDATE is non-deterministic for replication. Switch to row-based logging, add ORDER BY with LIMIT 1, or rewrite the logic to separate insert and update steps to resolve the problem.</p>

Error Highlights

Typical Error Message

INSERT... SELECT... ON DUPLICATE KEY UPDATE is unsafe

Error Type

Replication Error

Language

MySQL

Symbol

ER_BINLOG_UNSAFE_INSERT_SELECT_UPDATE

Error Code

1715

SQL State

HY000

Explanation

Table of Contents

What is MySQL Error 1715 (ER_BINLOG_UNSAFE_INSERT_SELECT_UPDATE)?

MySQL error 1715 appears when you execute an INSERT … SELECT … ON DUPLICATE KEY UPDATE statement under statement-based or mixed binary logging. The server marks the query as unsafe because the order in which rows are read from the SELECT decides which existing row is updated, and that order may vary between primary and replica servers.

The error prevents the statement from being written to the binary log, protecting replicas from diverging. While the query may succeed locally, replication would break without this safeguard.

What Causes This Error?

MySQL uses deterministic rules to ensure every statement produces identical results on all servers in a replication chain. If a query relies on non-guaranteed row order, it becomes non-deterministic. INSERT … SELECT … ON DUPLICATE KEY UPDATE is flagged because the first conflicting row encountered triggers the UPDATE clause, and that “first” row can change.

The risk is greatest when tables lack a unique ORDER BY, the SELECT spans multiple partitions, or the optimizer chooses different execution plans on different servers.

How to Fix MySQL Error 1715

Convert to row-based replication by setting binlog_format=row; row events store the actual changes, eliminating order concerns. This is the simplest global fix if you control the server.

If you must stay on statement-based logging, rewrite the logic: first INSERT IGNORE the selected rows, then perform an UPDATE that matches the same key set. Each statement is deterministic on its own.

Common Scenarios and Solutions

Bulk upsert from a staging table often triggers the error. Use INSERT INTO target SELECT * FROM staging for new rows, followed by UPDATE target JOIN staging ON key=key SET … for existing rows.

Self-referencing upserts also fail. Replace them with an UPDATE target SET col = (SELECT …) WHERE EXISTS(SELECT …) plus a separate INSERT for missing rows, or enable binlog_format=row for the session.

Best Practices to Avoid This Error

Prefer row-based replication for write-heavy systems using complex upserts. It avoids most unsafe-statement errors without query rewrites.

When you must upsert under statement-based logging, always supply a deterministic ORDER BY on the SELECT clause and ensure a LIMIT 1 in the UPDATE path so MySQL updates a predictable row.

Related Errors and Solutions

Error 1592 (ER_BINLOG_UNSAFE_STATEMENT) flags other unsafe statements like LIMIT without ORDER BY. The fix pattern mirrors error 1715: enable row-based logging or rewrite queries.

Error 1665 (ER_BINLOG_UNSAFE_AUTOINC_SELECT) involves INSERT … SELECT with auto-increment columns. Again, switch to row-based format or separate INSERT and SELECT steps.

Common Causes

Missing ORDER BY in the SELECT

Without ORDER BY, MySQL may fetch rows in different orders on primary and replica, making the update target unpredictable.

Mixed or statement-based binlog_format

Only statement events are affected. Row-based logging avoids the error entirely.

Different optimizer plans

Statistics differences can change join order, shifting which row matches first on replicas.

Partitioned or sharded tables

Row retrieval order across partitions can vary, especially with parallel query execution.

Related Errors

MySQL Error 1592: ER_BINLOG_UNSAFE_STATEMENT

Raised for statements that include non-deterministic constructs like LIMIT without ORDER BY under statement-based logging.

MySQL Error 1665: ER_BINLOG_UNSAFE_AUTOINC_SELECT

Indicates INSERT … SELECT involving an AUTO_INCREMENT column is unsafe. Row-based logging or query rewrite resolves it.

MySQL Error 1585: ER_BINLOG_LOGGING_IMPOSSIBLE

Occurs when a storage engine does not support logging. Switching engines or binlog format is required.

FAQs

Does error 1715 stop the query from running?

Yes, MySQL aborts the statement when binlog_format is STATEMENT or MIXED to prevent replication drift. Under ROW format, the query runs normally.

Which MySQL versions raise this error?

Error 1715 appears in MySQL 5.6 and later whenever statement-based logging encounters the unsafe pattern.

Can I ignore the warning instead of rewriting?

No. MySQL converts the warning to an error when log_bin = ON, so replication safety overrides application logic.

How does Galaxy help prevent this error?

Galaxy’s AI copilot flags unsafe upserts as you type and suggests deterministic rewrites or session-level binlog_format changes, reducing replication issues before code reaches production.

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