One thing I'd really like PostgreSQL to add is LIMIT on update statement as this makes batching easier. E.g. UPDATE user_profile SET followers_count = 0 WHERE followers_count IS NULL LIMIT 10000 I don't care which rows gets updated, only that no more than 10000 rows get updated. After each UPDATE I will COMMIT and then repeat as long as the UPDATE returns a positive number of rows updated. This makes it much easier t…
You can already achieve this with CTEs.
PostgreSQL internals: Things to know about update statements
31–40 of 61 posts
Re: PostgreSQL internals: Things to know about update statements
#32no it may not
this is fully standard compatible behavior and even expected behavior for _any_ SQL database implementing a read committed transaction insulation level
also it's a problem which on a theoretically level is not solvable with how SQL works, hence why on stricter isolation levels like serializable committing the transaction in such a situation will fail (in any SQL database) and needs to be retired. (Through in some unusual setups the db might be able to translate retry the transaction by itself, through at the cost of a ton of drawbacks)
Anyway reading the postgres documentation is always a good idea, it's not perfect but pretty good.
Re: PostgreSQL internals: Things to know about update statements
#33#2 is something that nearly no application I know handles, and it saddens me, but not to the extent that it seems to anger many of my fellows. It's perfectly okay to do so for YouTube, it's not worth bothering for many applications of less concurrency than that. Still, it fairly offends me that most applications exist in the middle, where they don't have read isolation, or use logical additions, or have a separate pr…
This is transaction 101. You simply lock the row via a SELECT … FOR UPDATE when you read the value. Any application that actually cares about consistency of its data must be doing this. Or just do it one step so the update increments the value in place. That way you have an implicit lock.
Re: PostgreSQL internals: Things to know about update statements
#34Earlier quoted context omitted.
This is transaction 101. You simply lock the row via a SELECT … FOR UPDATE when you read the value. Any application that actually cares about consistency of its data must be doing this. Or just do it one step so the update increments the value in place. That way you have an implicit lock.
I haven't used this, but I see its in Oracle and MySQL - does this exist in Postgresql or is this just the same as wrapping in a transaction where you're selecting on the row by ID or something? Just curious
what FOR UPDATE does is (simplified) to lock a mutex for each row the select statement returns which are released once the transaction commits
this has the effect that parallel running transactions have to wait until the new computed value is visible before reading it and in turn there is no problem with lost updates
just to be clear this is a very simplified explanation in many ways
Re: PostgreSQL internals: Things to know about update statements
#35Re: PostgreSQL internals: Things to know about update statements
#36This effectively changed the tone from an informational article to a hit piece. This is a huge accusation which is plain wrong.
Re: PostgreSQL internals: Things to know about update statements
#37> This behavior may well be interpreted as a violation of the SQL standard. This effectively changed the tone from an informational article to a hit piece. This is a huge accusation which is plain wrong.
Re: PostgreSQL internals: Things to know about update statements
#38Database locking can be a bit surprising at times. I recently stumbled over CREATE TABLE IF NOT EXISTS causing client timeouts - you might expect this to be near-instantaneous if the table exists, and it usually is, until it's not... because it always acquires an exclusive table lock, so it's blocked even by readers.
Re: PostgreSQL internals: Things to know about update statements
#39Earlier quoted context omitted.
This would also require adding the ORDER BY clause, as there might be applications where the order in which they are updated matters.
Is not any single query in Postgres a transaction? I don't think individual rows would be visible outside that transaction until all are updated.
Re: PostgreSQL internals: Things to know about update statements
#40#2 is something that nearly no application I know handles, and it saddens me, but not to the extent that it seems to anger many of my fellows. It's perfectly okay to do so for YouTube, it's not worth bothering for many applications of less concurrency than that. Still, it fairly offends me that most applications exist in the middle, where they don't have read isolation, or use logical additions, or have a separate pr…
I suspect with the current preferences to avoid exceptions, writing for automated retry becomes quite a bit harder. And I couldn't really get automated transaction retry to work for SQLite while still caching statement handles, treating them as prepared statements.