0

For example, to prevent two users concurrently updating the same wiki page, you might try something like this, expecting the update to occur only if the content of the page hasn’t changed since the user started editing it:

UPDATE wiki_pages SET content = 'new content'
  WHERE id = 1234 AND content = 'old content';

Is it safe (can it prevent lost updates) in Postgresql with READ_COMMITTED (default) isolation level?

Iakov Burtsev
  • 377
  • 1
  • 2
  • 15
  • When using pessimistic locking it will probably work yes, as it locks the table or row (depending on the setting) until the update is completed before it allows any other query (I suppose the "old content" is a query param coming from the request on the web?) Some related content here: https://stackoverflow.com/questions/71335259/prevent-lost-updates-with-high-transaction-isolation-levels-is-this-a-common-mi – Jurgen Rutten Oct 24 '22 at 09:10
  • It depends on how you use it. If you don't bother to check how many rows got updated and then throw an error, it would cause lost updates, rather than prevent them. – jjanes Oct 24 '22 at 12:05
  • @JurgenRutten yeah. "old content" is a query param coming from the request on the web. I am interested in case READ_COMMITTED (default) isolation level? – Iakov Burtsev Oct 27 '22 at 08:07

0 Answers0