The Backend Engineering Show with Hussein Nasser

The cost rolling back transactions (postgres/mysql)


Listen Later

The cost of a long-running update transaction that eventually failed in Postgres (or any other database for that matter.
In Postgres, any DML transaction touching a row creates a new version of that row. if the row is referenced in indexes, those need to be updated with the new tuple id as well. There are exceptions with optimization such as heap only tuples (HOT) where the index doesn’t need to be updated but that doesn’t always happens.
If the transaction rolls back, then the new row versions created by this transaction (millions in my case) are now invalid and should NOT be read by any new transaction. You have two solutions to address this, do you clean all dead rows eagerly on transaction rollback? Or do you do it lazily as a post process?
Postgres does the lazy approach, a command called vacuum which is called periodically Postgres attempts to remove those dead rows and free up space in the page.
Whats the harm of leaving those dead rows in? Its not really correctness issues at all, in fact transactions know not to read those dead rows by checking the state of the transaction that created them. This is however expensive, the check to see of the transaction that created this row is committed or rolled-back. Also the fact that those dead rows live in disk pages with alive rows makes an IO not efficient as the database has to filter out dead rows. For example, a page may have contained 1000 rows, but only 1 live row and 999 dead rows, the database will make that IO but only will get a single row of it. Repeat that and you end up making more IOs. More IOs = slower performance.
Other databases do the eager approach and won’t let you even start the database before rolling back is successfully complete, using undo logs. Which one is right and which one is wrong? Here is the fun part! Nothing is wrong or right, its all decisions that we engineers make. Its all fundamentals. Its up to you to understand and pick. Anything can work. You can make anything work if you know what you are dealing with.
If you want to learn about the fundamentals of databases and demystify it check out my udemy course 
https://database.husseinnasser.com
...more
View all episodesView all episodes
Download on the App Store

The Backend Engineering Show with Hussein NasserBy Hussein Nasser

  • 4.9
  • 4.9
  • 4.9
  • 4.9
  • 4.9

4.9

40 ratings


More shows like The Backend Engineering Show with Hussein Nasser

View all
Software Engineering Radio by se-radio@computer.org

Software Engineering Radio

273 Listeners

The Changelog: Software Development, Open Source by Changelog Media

The Changelog: Software Development, Open Source

292 Listeners

Software Engineering Daily by Software Engineering Daily

Software Engineering Daily

624 Listeners

The Cloudcast by Massive Studios

The Cloudcast

153 Listeners

Talk Python To Me by Michael Kennedy

Talk Python To Me

585 Listeners

Soft Skills Engineering by Jamison Dance and Dave Smith

Soft Skills Engineering

288 Listeners

Python Bytes by Michael Kennedy and Brian Okken

Python Bytes

214 Listeners

Y Combinator Startup Podcast by Y Combinator

Y Combinator Startup Podcast

236 Listeners

Syntax - Tasty Web Development Treats by Wes Bos & Scott Tolinski - Full Stack JavaScript Web Developers

Syntax - Tasty Web Development Treats

983 Listeners

Practical AI by Practical AI LLC

Practical AI

212 Listeners

The Stack Overflow Podcast by The Stack Overflow Podcast

The Stack Overflow Podcast

62 Listeners

The Real Python Podcast by Real Python

The Real Python Podcast

141 Listeners

Dwarkesh Podcast by Dwarkesh Patel

Dwarkesh Podcast

505 Listeners

Big Technology Podcast by Alex Kantrowitz

Big Technology Podcast

477 Listeners

Oxide and Friends by Oxide Computer Company

Oxide and Friends

60 Listeners