From d8a30eca2e231dabf8cc3a491f2a47a48472f4d4 Mon Sep 17 00:00:00 2001 From: Bruce Momjian Date: Wed, 4 Feb 2009 16:05:50 +0000 Subject: [PATCH] Update read committed documentation to better explain undesirable behavior of concurrent commands in cases where rows are being added and removed from matching query criteria. Minor word-smithing. --- doc/src/sgml/mvcc.sgml | 89 +++++++++++++++++++++++++++--------------- 1 file changed, 57 insertions(+), 32 deletions(-) diff --git a/doc/src/sgml/mvcc.sgml b/doc/src/sgml/mvcc.sgml index 5785811c6e..43055789be 100644 --- a/doc/src/sgml/mvcc.sgml +++ b/doc/src/sgml/mvcc.sgml @@ -1,4 +1,4 @@ - + Concurrency Control @@ -239,19 +239,19 @@ - Read Committed - is the default isolation level in PostgreSQL. - When a transaction runs on this isolation level, - a SELECT query sees only data committed before the - query began; it never sees either uncommitted data or changes committed - during query execution by concurrent transactions. (However, the - SELECT does see the effects of previous updates - executed within its own transaction, even though they are not yet - committed.) In effect, a SELECT query - sees a snapshot of the database as of the instant that that query - begins to run. Notice that two successive SELECT commands can - see different data, even though they are within a single transaction, if - other transactions + Read Committed is the default isolation + level in PostgreSQL. When a transaction + uses this isolation level, a SELECT query + (without a FOR UPDATE/SHARE clause) sees only data + committed before the query began; it never sees either uncommitted + data or changes committed during query execution by concurrent + transactions. In effect, a SELECT query sees + a snapshot of the database as of the instant the query begins to + run. However, SELECT does see the effects + of previous updates executed within its own transaction, even + though they are not yet committed. Also note that two successive + SELECT commands can see different data, even + though they are within a single transaction, if other transactions commit changes during execution of the first SELECT. @@ -271,22 +271,22 @@ otherwise it will attempt to apply its operation to the updated version of the row. The search condition of the command (the WHERE clause) is re-evaluated to see if the updated version of the row still matches the - search condition. If so, the second updater proceeds with its operation, - starting from the updated version of the row. (In the case of + search condition. If so, the second updater proceeds with its operation + using the updated version of the row. In the case of SELECT FOR UPDATE and SELECT FOR - SHARE, that means it is the updated version of the row that is - locked and returned to the client.) + SHARE, this means it is the updated version of the row that is + locked and returned to the client. Because of the above rule, it is possible for an updating command to see an inconsistent snapshot: it can see the effects of concurrent updating - commands that affected the same rows it is trying to update, but it + commands on the same rows it is trying to update, but it does not see effects of those commands on other rows in the database. This behavior makes Read Committed mode unsuitable for commands that - involve complex search conditions. However, it is just right for simpler + involve complex search conditions; however, it is just right for simpler cases. For example, consider updating bank balances with transactions - like + like: BEGIN; @@ -303,20 +303,45 @@ COMMIT; - Since in Read Committed mode each new command starts with a new snapshot - that includes all transactions committed up to that instant, subsequent - commands in the same transaction will see the effects of the committed - concurrent transaction in any case. The point at issue here is whether - or not within a single command we see an absolutely consistent - view of the database. + More complex usage can produce undesirable results in Read Committed + mode. For example, consider a DELETE command + operating on data that is being both added and removed from its + restriction criteria by another command, e.g. assume + website is a two-row table with + website.hits equaling 9 and + 10: + + +BEGIN; +UPDATE website SET hits = hits + 1; +-- run from another session: DELETE FROM website WHERE hits = 10; +COMMIT; + + + The DELETE will have no effect even though + there is a website.hits = 10 row before and + after the UPDATE. This occurs because the + pre-update row value 9 is skipped, and when the + UPDATE completes and DELETE + obtains a lock, the new row value is no longer 10 but + 11, which no longer matches the criteria. - The partial transaction isolation provided by Read Committed mode is - adequate for many applications, and this mode is fast and simple to use. - However, for applications that do complex queries and updates, it might - be necessary to guarantee a more rigorously consistent view of the - database than the Read Committed mode provides. + Because Read Committed mode starts each command with a new snapshot + that includes all transactions committed up to that instant, + subsequent commands in the same transaction will see the effects + of the committed concurrent transaction in any case. The point + at issue above is whether or not a single command + sees an absolutely consistent view of the database. + + + + The partial transaction isolation provided by Read Committed mode + is adequate for many applications, and this mode is fast and simple + to use; however, it is not sufficient for all cases. Applications + that do complex queries and updates might require a more rigorously + consistent view of the database than Read Committed mode provides.