MySQL Safe Update Mode (Error 1175) and Postgres Equivalent

MoleDB Safety settings with Local, Staging, and Production sections.

Error 1175 is MySQL refusing a write it thinks is too broad. MySQL safe update mode is the switch behind that refusal. The fix most pages stop at is sql_safe_updates = 0. That does silence the error. It also removes the only thing that was standing between a tired afternoon and DELETE FROM orders.

This page is the other way through it. Fix the statement when the statement is wrong, turn the guard off for one session when you really do mean every row, and then get a similar habit in PostgreSQL and SQLite. Those two engines do not have this setting. The protection has to come from a transaction, a trigger, or the client you type the SQL in.

The statements below were run on MySQL 8.0.46, PostgreSQL 16.15, and SQLite 3.45.1. Where a result is surprising, it is the result that came back, not a paraphrase of the manual. The manual is still worth reading: sql_safe_updates in the MySQL 8.0 reference.

What Error 1175 means

MySQL raises this when safe updates are on and an UPDATE or DELETE does not look restricted:

ERROR 1175 (HY000): You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column.

The wording says “update” even when the statement is DELETE. The tables below are the ones the rest of this section uses. Safe updates are on for the session:

CREATE TABLE accounts (
  id int PRIMARY KEY,
  email varchar(100),
  status varchar(20),
  KEY (email)
);

CREATE TABLE edge (
  id int PRIMARY KEY,
  status varchar(20)
);

CREATE TABLE tiny (
  id int PRIMARY KEY
);

INSERT INTO accounts (id, email, status) VALUES
  (1, 'a@example.test', 'active'),
  (2, 'b@example.test', 'active'),
  (3, 'c@example.test', 'closed');

INSERT INTO edge (id, status) VALUES
  (1, 'y'),
  (2, 'n');

INSERT INTO tiny (id) VALUES (1);

SET SESSION sql_safe_updates = 1;

Both of these failed the same way on 8.0.46:

DELETE FROM accounts;
UPDATE accounts SET status = 'closed';

A WHERE clause is not automatically enough. This also failed, because status was not a key:

UPDATE accounts SET status = 'closed' WHERE status = 'active';

So the mysql safe update mode delete people run into is often a real filter that MySQL still considers unsafe. The column in the predicate has to be a key column: a primary key or a secondary index. A non-unique index counts. email in the test table was only KEY (email), and this succeeded:

UPDATE accounts SET status = 'paused' WHERE email = 'b@example.test';

That success is also a warning. A non-unique key can match thousands of rows. Safe mode does not estimate how many rows you are about to change.

What sql_safe_updates actually checks

The check is structural. MySQL looks for a key column used in the WHERE clause, or for a LIMIT clause. It does not ask whether the key match is selective.

These were accepted on the test table:

UPDATE accounts SET status = 'closed' WHERE id = 1;
UPDATE accounts SET status = 'y' WHERE id IN (1, 2);
UPDATE accounts SET status = 'x' WHERE id > 0;
UPDATE accounts SET status = 'q' WHERE email LIKE 'a%';
UPDATE accounts SET status = 'active' LIMIT 1;

WHERE id > 0 updates every row if every id is positive, and it was still allowed, because id is the primary key. LIMIT 1 with no WHERE changed one arbitrary row. Both are legal under the setting and both can be the wrong statement.

These were rejected:

UPDATE accounts SET status = 'closed' WHERE email LIKE '%example.test';
UPDATE edge SET status = 'z' WHERE id = 1 OR status = 'y';

The leading-wildcard LIKE does not count as using the index, even though email is indexed. An OR that mixes a key predicate with a non-key predicate was rejected too. If you need that OR, key the write another way or turn the setting off for the session on purpose.

TRUNCATE is outside the check. With safe updates still on, this emptied the table:

TRUNCATE TABLE tiny;

If you are trying to prove the setting is on, use an UPDATE or DELETE, not TRUNCATE.

The server default, on this MySQL 8.0.46, is off for both the session and the global value. A plain mysql client will let a DELETE without a WHERE clause through until something turns the setting on. MySQL Workbench is the something, for a lot of people.

Three ways to fix it, safest first

Rewrite the query with a key in the WHERE clause

If the goal is “change these rows”, name them by a key. Prefer the primary key when you have it.

UPDATE accounts
SET status = 'closed'
WHERE id = 1;

If the real condition is on a non-key column, select the keys first and write by those keys:

SELECT id FROM accounts WHERE status = 'active';

UPDATE accounts
SET status = 'closed'
WHERE id IN (1, 2);

Read the SELECT result before you run the UPDATE. IN (...) was accepted in the test. An IN list of hundreds of ids is still a wide write. That is a reason to look at the list, not a reason the server will stop you.

For a cleanup you intend to batch, LIMIT satisfies the check and also caps the write:

DELETE FROM accounts
WHERE status = 'closed'
ORDER BY id
LIMIT 100;

ORDER BY plus LIMIT makes the batch repeatable. LIMIT alone, with no WHERE, does not. It deletes whichever rows the storage engine returns first.

Turn it off for one session

When you do mean the whole table, say so in the session and turn the guard back on before you keep typing:

SET SQL_SAFE_UPDATES = 0;
DELETE FROM accounts;
SET SQL_SAFE_UPDATES = 1;

On the test server, the DELETE removed the remaining rows, and a new connection did not inherit that session value. SET sql_safe_updates = 0 and SET SQL_SAFE_UPDATES = 0 are the same statement. The session variable is what your current connection uses. The Workbench manual describes the GUI switch that sets it for you.

Do not put SET SQL_SAFE_UPDATES = 0 at the top of a shared script and leave it there. The next person who runs the file inherits a connection that will accept anything.

SET GLOBAL sql_safe_updates = 0 is a different change. It does not update the session you are sitting in. On 8.0.46, setting the global value to 1 left @@SESSION.sql_safe_updates at 0, and a brand-new connection came up as 1. Change the session you are about to use. Leave the global value alone unless you are the person who owns the server defaults.

Disable it in MySQL Workbench on Mac and Windows

Workbench enables Safe Updates by default. The preference is named:

Safe Updates (rejects UPDATEs and DELETEs with no restrictions)

On a Mac it lives under MySQL Workbench → Settings → SQL Editor. On Windows it is Edit → Preferences → SQL Editor. Clearing the box does not rewrite the connection you already opened. Disconnect, then connect again. The manual says the option requires a reconnection, and that matches the “I unchecked it and 1175 is still there” report.

Turning it off in Workbench is reasonable for a local scratch database you can drop. It is a bad default for a shared or production connection. You can leave the preference on and still run one intentional full-table statement with SET SQL_SAFE_UPDATES = 0 in that editor tab. Tabs on the same Workbench connection share the session, so set it back to 1 before you run the next file.

Why “=0 not working” happens

set sql_safe_updates =0 not working usually means the statement you think you ran is not the session you are looking at. The cases that showed up in testing:

  • You ran SET in connection A and the error came back on connection B. Workbench, a GUI, a connection pool, and a second terminal are different sessions.
  • You ran SET GLOBAL and expected the open tab to change. It does not.
  • You reconnected. Workbench applies Safe Updates at connect time, so your previous SET is gone.
  • The statement is still unsafe for a different reason. WHERE status = 'active' keeps failing after sql_safe_updates is 0 only if you are not actually on that session. If the setting really is 0, that statement runs. If it is still 1, a non-key WHERE is still 1175.
  • You tested with TRUNCATE. That statement never produced 1175 in the test, so it cannot tell you whether the setting is on.

Check the session you are about to use:

SELECT @@SESSION.sql_safe_updates, @@GLOBAL.sql_safe_updates;

If the session value is 1, either fix the statement or set the session to 0, run the one write, and set it back.

Is there sql_safe_updates in PostgreSQL?

People search for sql_safe_updates postgresql because they want the same refusal. PostgreSQL does not have the variable. On PostgreSQL 16.15:

SHOW sql_safe_updates;
ERROR:  unrecognized configuration parameter "sql_safe_updates"

Nothing in core checks that a DELETE mentions a key. A delete without a where clause is legal and, if you have already committed, it is gone. Protection is something you add.

The safeupdate extension

safeupdate is a third-party extension, not a PostgreSQL contrib module shipped with the server used here. It was not installed, so these commands were not executed on that machine. The usual shape, from the extension itself, is to load it into a session:

LOAD 'safeupdate';

Operators who want it on every connection set session_preload_libraries to safeupdate. Read the extension’s install notes before you do that on a server other people use. If the library is not on disk, LOAD fails and you have not gained a guard. The trigger below is the version that was actually created and fired.

A statement-level trigger that caps affected rows

PostgreSQL 10 and newer can see the rows a statement touched through a transition table. This function rejects a DELETE that removes more than five rows. The table it was created on had 20 rows:

CREATE TABLE accounts (
  id int PRIMARY KEY
);

INSERT INTO accounts (id)
SELECT g FROM generate_series(1, 20) AS g;
CREATE OR REPLACE FUNCTION refuse_large_delete()
RETURNS trigger
LANGUAGE plpgsql
AS $$
DECLARE
  n bigint;
BEGIN
  SELECT count(*) INTO n FROM old_rows;
  IF n > 5 THEN
    RAISE EXCEPTION 'refusing DELETE that would remove % rows', n;
  END IF;
  RETURN NULL;
END;
$$;

CREATE TRIGGER accounts_delete_cap
AFTER DELETE ON accounts
REFERENCING OLD TABLE AS old_rows
FOR EACH STATEMENT
EXECUTE FUNCTION refuse_large_delete();

DELETE FROM accounts WHERE id = 1 removed one row. DELETE FROM accounts raised refusing DELETE that would remove 19 rows, and the table still had 19 rows afterwards. Raising from the trigger aborts the statement.

This is a cap, not a copy of MySQL’s rule. A DELETE without a WHERE clause that matches three rows is under the cap and will succeed. A careful DELETE that matches six specific keys is over the cap and will fail. Pick the number for the table. Five was only the test. Put the trigger on the tables you are afraid of, not on every audit log.

BEFORE statement triggers cannot see the row count, because the rows have not been deleted yet. AFTER plus an exception is what rolls the work back. Creating the trigger takes the TRIGGER privilege on the table and EXECUTE on the function. You do not have to own the table, and you do not have to be a superuser. CREATE TRIGGER states both requirements.

psql AUTOCOMMIT off as a habit

psql commits every statement unless you turn that off. With \set AUTOCOMMIT off, a delete sits in a transaction until you say COMMIT. This sequence was run against a two-row table:

\set AUTOCOMMIT off
DELETE FROM ac_demo;
SELECT count(*) FROM ac_demo;  -- 0
ROLLBACK;
SELECT count(*) FROM ac_demo;  -- 2

The rows came back. That only helps if you look before you commit, and if you remember that autocommit is off. Forgetting COMMIT and then wondering why a colleague cannot see your insert is the usual cost. For an interactive session against a database you can damage, it is a fair trade. It does nothing for a GUI, a migration runner, or an application pool. Those each have their own commit behaviour.

You can set it for every psql session in ~/.psqlrc:

\set AUTOCOMMIT off

Do that on your own machine. Do not hide it in a shared server config and surprise the next person.

SQLite: guardrails without server settings

SQLite has no sql_safe_updates and no TRUNCATE. Emptying a table is DELETE FROM table, and it is allowed. Two habits cover the gap. Both were run on SQLite 3.45.1.

A transaction you can undo. The table starts with five rows, so deleting the first three ids changes three:

CREATE TABLE users (
  id integer PRIMARY KEY
);

INSERT INTO users (id) VALUES (1), (2), (3), (4), (5);
BEGIN;
DELETE FROM users WHERE id <= 3;
SELECT changes();
ROLLBACK;

changes() returned 3, and after ROLLBACK the row count was back to where it started. changes() is the number of rows the previous statement actually modified. If you expected one row and it says 40000, roll back.

A connection that cannot write at all:

PRAGMA query_only = ON;
DELETE FROM users;

SQLite answered attempt to write a readonly database. The following SELECT changes(), count(*) was 0 and the original row count. PRAGMA query_only = OFF turns writes back on for that connection. Use this for a reporting window or a file you only meant to read. It is per connection, so the application that opens the file next is not affected.

SQLite has row triggers, not the statement-level transition tables used above. A trigger can RAISE(ABORT, ...) for a row that matches a condition you can see from OLD, but it cannot see “this statement has no WHERE” as a fact. The transaction and query_only are the honest tools.

Guardrails in your SQL client

Server settings are the wrong layer for some of this. A developer laptop, a staging box, and production do not deserve the same amount of friction. MySQL’s switch is on or off for the whole session. It cannot be “on for production, quiet for a database on your Mac”.

MoleDB keys its safety settings to the connection’s environment: Local, Staging, or Production. It flags a DELETE without a WHERE clause before the statement runs. On a Production connection it asks you to confirm a large write, and it asks again before a multi-statement run. The Safety page on moledb.com describes the large-write confirm, with a published default of 50 rows. A Local connection stays out of the way, so scratch work does not grow a dialog.

The comparison of Mac clients, including how other apps handle a production connection, is in best Postgres client for Mac.

MoleDB Safety settings with separate Local, Staging, and Production sections.
Safety settings are stored per environment.
MoleDB confirmation dialog before a large write on a Production connection.
A large write on a Production connection asks for a second confirmation.
MoleDB review dialog, framed in red, showing a change to orders before it is sent.
A production run shows the change before it is sent.

A client prompt is not a backup. It will not save you from a statement you confirm without reading. It will catch the one you did not mean to send.

Checklist: running UPDATE or DELETE on production

  1. SELECT the rows first, with the same predicate. If the count surprises you, stop.
  2. Prefer a primary key. A non-key column does not satisfy MySQL safe mode, and a non-unique key can still match half the table.
  3. Wrap the write in a transaction. In psql, \set AUTOCOMMIT off and look at the result before COMMIT. In MySQL, START TRANSACTION and ROLLBACK are there even when safe updates are off.
  4. Turn sql_safe_updates off for one statement, then back on. Do not change the global value to get a single job through.
  5. Remember TRUNCATE ignores MySQL’s check, and PostgreSQL has no check at all.
  6. On PostgreSQL, a row-cap trigger is a backstop for the tables where a wide delete is never legitimate.
  7. On SQLite, BEGIN and SELECT changes() before COMMIT. Use PRAGMA query_only = ON when the connection should not write.
  8. Take a backup, or know which backup you would restore, before a delete you cannot bound. A guardrail that lets the statement through is not a recovery plan.

The useful outcome is a statement you can explain, run, and undo. Turning the error off and hoping is how the error got its reputation.

FAQ

What is sql_safe_updates in MySQL?

sql_safe_updates is a session setting. When it is on, MySQL rejects UPDATE and DELETE statements that do not use a key column in the WHERE clause and do not have a LIMIT clause. The error is 1175. It does not count rows, it does not apply to TRUNCATE, and on MySQL 8.0.46 the server default is off. MySQL Workbench turns it on when it connects.

How to disable safe update mode in MySQL Workbench on Mac?

In MySQL Workbench on macOS, open Settings from the application menu, then SQL Editor, and clear Safe Updates (rejects UPDATEs and DELETEs with no restrictions). The checkbox is on by default. Workbench does not apply the change to a connection you already have open, so disconnect and connect again. The same checkbox is in the SQL Editor preferences on Windows.

Why is SET SQL_SAFE_UPDATES = 0 not working?

That statement changes only the session you ran it in. A new connection uses the global value, and Workbench turns Safe Updates back on when it reconnects. On MySQL 8.0.46, SET GLOBAL sql_safe_updates = 1 left the already-open session at 0 and a brand-new session came up at 1. TRUNCATE still succeeds while the setting is on, so a successful TRUNCATE is not proof that the setting is off.

Is there sql_safe_updates for PostgreSQL?

No. PostgreSQL 16.15 reports unrecognized configuration parameter for SHOW sql_safe_updates. Use a transaction you can roll back, or a statement-level trigger that rejects a DELETE above a row cap. A third-party extension named safeupdate can block WHERE-less writes if you install it. It is not part of PostgreSQL itself, and it was not installed on the server used to test this page.

Is DELETE without WHERE the same as TRUNCATE?

No. DELETE without a WHERE clause removes every row one by one and can fire row triggers. TRUNCATE empties the table in a different way and, in MySQL, is not blocked by sql_safe_updates. In PostgreSQL, both can roll back inside a transaction that has not been committed. SQLite has no TRUNCATE statement. DELETE without a WHERE clause there is the way you empty a table, and it can be rolled back before COMMIT.