Post-mortem: the UPDATE without a WHERE
How I gave all 3,412 products in a web shop the same price on a Friday afternoon, how Jef fixed it, and what we do differently now.
I went back and forth on whether to write this. But I’ve learned so much from other people’s post-mortems that it would be hypocritical to stuff mine in a drawer. So here goes. Blameless, apart from the fact that it was my fault.
Summary#
On Friday 27 January at 16:08, I ran an UPDATE without a WHERE on the production database of a garden furniture web shop. All 3,412 products got a price of €49.95. The shop was online with wrong prices for 47 minutes. Six orders came in during that time, one of them for a lounge set that normally costs €1,899. By around 20:30 everything was restored, thanks to Jef’s backups and binlogs.
Timeline#
| Time | What happened |
|---|---|
| 15:55 | The client calls: the price of one parasol needs changing urgently, there’s a promotion running in a flyer. The shop’s back office can’t edit prices for products with variants (a known bug, on the list for months). |
| 16:05 | I open phpMyAdmin on the production server and type the query into the SQL window. |
| 16:08 | I run the query. “3412 rows affected.” I was expecting 1. |
| 16:09 | Panic. I put the shop into maintenance mode. No, wait, there isn’t one. I start looking for a way to take the shop offline. |
| 16:20 | I walk over to Jef. He looks at the sheet of A4 above his desk, then at me, and says: “Okay. Tell me.” |
| 16:55 | Jef puts an .htaccess with a temporary maintenance page online. The shop is offline. |
| 17:10 | Analysis: which backup do we have, and what has changed since? |
| 20:25 | Prices restored and verified on a copy. |
| 20:31 | Shop back online. |
What happened#
The query I meant to run:
UPDATE products SET price = 49.95 WHERE id = 1873;
The query I actually ran:
UPDATE products SET price = 49.95
I’d typed the query in two steps: first the SET part, then I’d look up the id in another tab. I looked up the id, came back, and clicked “Go”. I never typed the WHERE part. That simple and that stupid.
How Jef fixed it#
Jef had set up two things I’d until then filed under “sysadmin paranoia”:
- A
mysqldumpof every database each night at 3 a.m., kept for seven days. - MySQL’s binary log was switched on. It records every query that changes data.
The plan: restore that night’s dump into a separate database, replay the binlog up to just before my query, and then copy the prices from that copy back into production. That way the orders and customer data from that day stayed intact.
# load last night's dump into a separate database
mysql -e "CREATE DATABASE shop_restore"
gunzip < /backup/2012-01-27/shop.sql.gz | mysql shop_restore
# replay the binlog from the dump up to just before the mistake
mysqlbinlog --database=shop \
--start-datetime="2012-01-27 03:00:00" \
--stop-datetime="2012-01-27 16:08:00" \
/var/log/mysql/mysql-bin.000214 \
| sed 's/`shop`\./`shop_restore`./g' \
| mysql shop_restore
The sed was needed because some queries in the binlog referred to the database by name. Jef spent half an hour checking there was nothing in the binlog writing to some other table. I sat next to him and kept my mouth shut.
Then restoring the prices. With a WHERE this time, or rather with a join:
UPDATE shop.products p
JOIN shop_restore.products r ON r.id = p.id
SET p.price = r.price;
-- and then the one price that did need changing
UPDATE shop.products SET price = 49.95 WHERE id = 1873;
Before running that on production we tested it on a second copy and spot-checked twenty products. I compared every one of them by hand against the client’s catalogue.
The client followed up on the six orders manually. The buyer of the lounge set got an apology email and a discount voucher. The agency covered the cost of that. My boss was calmer about it than I’d expected.
What went wrong (besides me)#
- Working directly on production. There was no procedure for doing this kind of change on a copy first.
- phpMyAdmin on production, with a user that can do anything. The same user as the web shop itself.
- No transactions. The table was MyISAM. Even if I’d spotted the problem instantly, there was no
ROLLBACKto do. - No maintenance mode. It took 47 minutes to get the shop offline. That’s longer than the mistake itself.
- A bug in the back office that had been open for months and drove us into the database.
- Friday, four o’clock. I’ll say it before Jef does.
What we do differently now#
--safe-updates. The MySQL client has an option that refusesUPDATEandDELETEstatements without aWHEREon a key. It even has an alias that doesn’t mince words:--i-am-a-dummy. It’s now in everyone’s~/.my.cnf:
[mysql]
safe-updates
SELECTfirst, thenUPDATE. I write the query asSELECT * FROM products WHERE ...first, check that the row count makes sense, and only then replaceSELECT *withUPDATE ... SET.- InnoDB and transactions. The web shop’s tables have been converted to InnoDB. Manual changes happen between
START TRANSACTIONandCOMMIT, with a sanity-checkSELECTin between. - No more phpMyAdmin on production. Only SSH and the MySQL client, with a separate user that can do only what’s needed.
- A maintenance page we can switch on with a single file.
- The back office bug is fixed. It took two hours. Two hours.
- Backups get tested. Jef now does a restore drill every month. A backup you’ve never restored is just a pile of bytes.
Finally#
The worst moment wasn’t the query itself. It was the twelve minutes between the query and the moment I walked over to Jef. I tried to fix it myself first, before anyone noticed. For those twelve minutes the shop ran with wrong prices and nobody was working on it.
So if you’re a junior reading this: if you break something, say so straight away. Nobody gets angry about a mistake. They do get angry about a mistake you tried to hide.
Jef never mentioned it again, by the way. On Monday there was a new sheet of A4 on my desk, though. It just said “WHERE?”. It’s still up.