Monday, December 15, 2025

No-Op Updates in PostgreSQL and MySQL


I’m lazy after I’m speakin’
I’m lazy after I stroll
I’m lazy after I’m dancin’
I’m lazy after I speak

 

X-Press 2 Feat. David Byrne – Lazy

Whereas getting ready a weblog submit to check how PostgreSQL and MySQL deal with locks, as a part of a collection protecting the totally different approaches to MVCC for these databases, I discovered an attention-grabbing habits relating to no-op updates. Whereas it isn’t a standard sample to situation no-op updates, utility logic that isn’t absolutely underneath the builders’ management typically results in such conditions. Understanding how totally different databases deal with these eventualities offers perception into their inside mechanisms and efficiency traits.

And what’s a no-op replace? A no-op replace in SQL is an replace operation that doesn’t truly change any knowledge: an UPDATE assertion that units columns to their present values, leading to no precise adjustments to the values saved within the database.

Replace in PostgreSQL

We create the desk and insert a row.

As anticipated, the desk has one dwell row and no lifeless rows. And sure, the database identify is Batman:

Now we carry out a no-op replace, setting the sample_text column to the identical worth it already has.

The shopper reviews that one row was up to date. However what’s the inside state of the desk?

The result’s attention-grabbing: PostgreSQL creates a brand new model of the row, marking the earlier model as lifeless, regardless that the information hasn’t modified. This habits is because of PostgreSQL’s MVCC implementation, which treats any replace as a modification that requires a brand new row model. If we repeat the no-op replace, the lifeless row depend will increase once more.

Replace in MySQL

Let’s begin by creating the identical desk and inserting a row.

Sadly, there isn’t any simple approach to examine the variety of rows within the desk and within the rollback segments in InnoDB. Nonetheless, we will monitor the variety of undo log pages written to disk, which provides us an thought of whether or not any adjustments have been made to the information. Clearly, this won’t work if there may be exercise from different periods or if we don’t have the corresponding metrics enabled. Due to this fact, be sure that you run this in isolation and with metrics enabled. There are different strategies that may very well be used, for instance, executing FLUSH TABLES and checking if the corresponding InnoDB file has modified, however this one is straightforward sufficient for our functions.

First, we reset the buffer web page metrics, buffer_page_written_undo_log belongs to this module.

As anticipated, the metric reveals zero pages written to the undo log. Now we carry out a no-op replace, setting the sample_text column to its present worth.

We see that MySQL tells us that one row matched, however no rows have been modified. And now we recheck the metric.

And no pages have been written to the undo log, indicating that no adjustments have been made to the information. Now we are going to run the replace, however we are going to truly change one row.

As we will see now, two pages have been written to the undo log, indicating that adjustments have been made to the information.

Bonus monitor

If the no-op replace in MySQL doesn’t modify any knowledge, does it lock the row? Let’s discover out by opening two periods. Within the first session, we begin a transaction and carry out the no-op replace.

Within the second session, we try to replace the identical row.

So, regardless that the no-op replace didn’t modify any knowledge, it nonetheless acquired a lock on the row, stopping different transactions from modifying it till the transaction is dedicated or rolled again. And the way does this occur? Nicely, you’ll have to attend until I end the weblog submit I discussed in the beginning!

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles