Will a transaction see inserts from another concurrent transaction if they are in Repeatable Read isolation level in Postgresql?How to produce phantom reads?Transaction Isolation on select, insert, deleteHow do I set the transaction isolation level in SQLAlchemy for PostgreSQL?What are the options for storing hierarchical data in a relational database?Run a PostgreSQL .sql file using command line argumentsTransaction Repeatable Read Isolation does not work properly in PostgreSQLREPEATABLE READ and READ UNCOMMITTED isolation levels for transactionsAre postgresql transaction levels repeatable read and serializable the same?PostgreSQL Transaction Isolation READ UNCOMMITTEDBehavior of PostgreSQL isolation levelsPostgreSQL 11 REPEATABLE READ Isolation Level
What should I do about a religious player who refuses to accept the existence of multiple gods in D&D?
What does it mean by "d-ism of Leibniz" and "dotage of Newton" in simple English?
What is a natural deduction proof from ~(A↔B) to ~(A→B)?
Make a formula to get the highest score
What is the intuition behind uniform continuity?
The deliberate use of misleading terminology
How to decline physical affection from a child whose parents are pressuring them?
Rotated Position of Integers
Orientable with respect to complex cobordism?
Modern approach to radio buttons
Why don't I have ground wiring on any of my outlets?
Why does my electric oven present the option of 40A and 50A breakers?
What is a simple, physical situation where complex numbers emerge naturally?
Cryptography and patents
Does a component pouch automatically contain components?
How can I offer a test ride while selling a bike?
If a massive object like Jupiter flew past the Earth how close would it need to come to pull people off of the surface?
Do adult Russians normally hand-write Cyrillic as cursive or as block letters?
Creating Fictional Slavic Place Names
Can a helicopter mask itself from Radar?
Can you use a concentration spell while using Mantle of Majesty?
Joist hangers to use for rough cut 2x8 (2 3/4" x 8 3/4")?
If a problem only occurs randomly once in every N times on average, how many tests do I have to perform to be certain that it's now fixed?
Looking for an old image of designing a cpu with plan laid out / being edited on a literal floor
Will a transaction see inserts from another concurrent transaction if they are in Repeatable Read isolation level in Postgresql?
How to produce phantom reads?Transaction Isolation on select, insert, deleteHow do I set the transaction isolation level in SQLAlchemy for PostgreSQL?What are the options for storing hierarchical data in a relational database?Run a PostgreSQL .sql file using command line argumentsTransaction Repeatable Read Isolation does not work properly in PostgreSQLREPEATABLE READ and READ UNCOMMITTED isolation levels for transactionsAre postgresql transaction levels repeatable read and serializable the same?PostgreSQL Transaction Isolation READ UNCOMMITTEDBehavior of PostgreSQL isolation levelsPostgreSQL 11 REPEATABLE READ Isolation Level
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
If during execution of transaction A, a concurrent transaction B adds a row that fits the search criteria of transaction A and commits, what will happen when transaction A commits? Will it include the new row, or will the transaction fail?
Assuming they are run in Repeatable Read isolation level in Postgresql.
sql postgresql
add a comment |
If during execution of transaction A, a concurrent transaction B adds a row that fits the search criteria of transaction A and commits, what will happen when transaction A commits? Will it include the new row, or will the transaction fail?
Assuming they are run in Repeatable Read isolation level in Postgresql.
sql postgresql
add a comment |
If during execution of transaction A, a concurrent transaction B adds a row that fits the search criteria of transaction A and commits, what will happen when transaction A commits? Will it include the new row, or will the transaction fail?
Assuming they are run in Repeatable Read isolation level in Postgresql.
sql postgresql
If during execution of transaction A, a concurrent transaction B adds a row that fits the search criteria of transaction A and commits, what will happen when transaction A commits? Will it include the new row, or will the transaction fail?
Assuming they are run in Repeatable Read isolation level in Postgresql.
sql postgresql
sql postgresql
edited Mar 24 at 10:55
netok
asked Mar 24 at 10:49
netoknetok
6617
6617
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
The transaction A won't fail. It won't see the data inserted by transaction B.
Demo:
create table accounts (id bigserial primary key, balance bigint);
insert into accounts(balance) values (50), (100), (200);
Transaction A:
begin transaction isolation level repeatable read;
select * from accounts where balance >= 100;
id | balance
----+---------
2 | 100
3 | 200
(2 rows)
Transaction B:
begin;
insert into accounts(balance) values (300);
INSERT 0 1
commit;
Transaction A:
select * from accounts where balance >= 100;
id | balance
----+---------
2 | 100
3 | 200
(2 rows)
No (4, 300)
row.
(tested on PostgreSQL 11.2)
Note that PostgreSQL has stronger guarantees on REPEATABLE READ
isolation level. It prevents phantom reads.
From the documentation:
The Repeatable Read isolation level only sees data committed before the transaction began; it never sees either uncommitted data or changes committed during transaction execution by concurrent transactions. (However, the query does see the effects of previous updates executed within its own transaction, even though they are not yet committed.) This is a stronger guarantee than is required by the SQL standard for this isolation level
From the Table 13.1. Transaction Isolation Levels:
Phantom reads are allowed at REPEATABLE READ
isolation level but not in PG
See also:
- How to produce phantom reads
Update
If the statement
update accounts set balance = balance + 30 where balance >= 100;
is issued as the last statement of transaction A only 2 rows will be updated because from the point of view of transaction A there are only two rows satisfying the balance >= 100
predicate:
update accounts set balance = balance + 30 where balance >= 100;
UPDATE 2
After a commit:
commit;
COMMIT
select * from accounts;
id | balance
----+---------
1 | 50
4 | 300
2 | 130
3 | 230
(4 rows)
Only rows returned by this
select * from accounts where balance >= 100;
statement are updated (excluding the row inserted by transaction B)
Note the transaction A will fail if it tries to update
a row changed by another committed concurrent transaction:
A
begin transaction isolation level repeatable read;
BEGIN
select * from accounts where id = 1;
id | balance
----+---------
1 | 50
(1 row)
B
begin;
BEGIN
update accounts set balance = balance + 10;
UPDATE 3
commit;
A:
-- By some logic based on the fact that the balance is 50 we decided to update it to 60
-- balance is updated by committed concurrent transaction
update accounts set balance = 60 where id = 1;
ERROR: could not serialize access due to concurrent update
The error is expected. From the documentation:
If the first updater rolls back, then its effects are negated and the repeatable read transaction can proceed with updating the originally found row. But if the first updater commits (and actually updated or deleted the row, not just locked it) then the repeatable read transaction will be rolled back with the message
ERROR: could not serialize access due to concurrent update
because a repeatable read transaction cannot modify or lock rows changed by other transactions after the repeatable read transaction began.
It is expected that applications will retry failed transactions:
When an application receives this error message, it should abort the current transaction and retry the whole transaction from the beginning.
Can you also try UPDATE in the last query in transaction A ? For example,update accounts set balance = balance + 30;
. I am not sure if it will fail, or just change the rows it knows.
– netok
Mar 24 at 13:18
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55323024%2fwill-a-transaction-see-inserts-from-another-concurrent-transaction-if-they-are-i%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The transaction A won't fail. It won't see the data inserted by transaction B.
Demo:
create table accounts (id bigserial primary key, balance bigint);
insert into accounts(balance) values (50), (100), (200);
Transaction A:
begin transaction isolation level repeatable read;
select * from accounts where balance >= 100;
id | balance
----+---------
2 | 100
3 | 200
(2 rows)
Transaction B:
begin;
insert into accounts(balance) values (300);
INSERT 0 1
commit;
Transaction A:
select * from accounts where balance >= 100;
id | balance
----+---------
2 | 100
3 | 200
(2 rows)
No (4, 300)
row.
(tested on PostgreSQL 11.2)
Note that PostgreSQL has stronger guarantees on REPEATABLE READ
isolation level. It prevents phantom reads.
From the documentation:
The Repeatable Read isolation level only sees data committed before the transaction began; it never sees either uncommitted data or changes committed during transaction execution by concurrent transactions. (However, the query does see the effects of previous updates executed within its own transaction, even though they are not yet committed.) This is a stronger guarantee than is required by the SQL standard for this isolation level
From the Table 13.1. Transaction Isolation Levels:
Phantom reads are allowed at REPEATABLE READ
isolation level but not in PG
See also:
- How to produce phantom reads
Update
If the statement
update accounts set balance = balance + 30 where balance >= 100;
is issued as the last statement of transaction A only 2 rows will be updated because from the point of view of transaction A there are only two rows satisfying the balance >= 100
predicate:
update accounts set balance = balance + 30 where balance >= 100;
UPDATE 2
After a commit:
commit;
COMMIT
select * from accounts;
id | balance
----+---------
1 | 50
4 | 300
2 | 130
3 | 230
(4 rows)
Only rows returned by this
select * from accounts where balance >= 100;
statement are updated (excluding the row inserted by transaction B)
Note the transaction A will fail if it tries to update
a row changed by another committed concurrent transaction:
A
begin transaction isolation level repeatable read;
BEGIN
select * from accounts where id = 1;
id | balance
----+---------
1 | 50
(1 row)
B
begin;
BEGIN
update accounts set balance = balance + 10;
UPDATE 3
commit;
A:
-- By some logic based on the fact that the balance is 50 we decided to update it to 60
-- balance is updated by committed concurrent transaction
update accounts set balance = 60 where id = 1;
ERROR: could not serialize access due to concurrent update
The error is expected. From the documentation:
If the first updater rolls back, then its effects are negated and the repeatable read transaction can proceed with updating the originally found row. But if the first updater commits (and actually updated or deleted the row, not just locked it) then the repeatable read transaction will be rolled back with the message
ERROR: could not serialize access due to concurrent update
because a repeatable read transaction cannot modify or lock rows changed by other transactions after the repeatable read transaction began.
It is expected that applications will retry failed transactions:
When an application receives this error message, it should abort the current transaction and retry the whole transaction from the beginning.
Can you also try UPDATE in the last query in transaction A ? For example,update accounts set balance = balance + 30;
. I am not sure if it will fail, or just change the rows it knows.
– netok
Mar 24 at 13:18
add a comment |
The transaction A won't fail. It won't see the data inserted by transaction B.
Demo:
create table accounts (id bigserial primary key, balance bigint);
insert into accounts(balance) values (50), (100), (200);
Transaction A:
begin transaction isolation level repeatable read;
select * from accounts where balance >= 100;
id | balance
----+---------
2 | 100
3 | 200
(2 rows)
Transaction B:
begin;
insert into accounts(balance) values (300);
INSERT 0 1
commit;
Transaction A:
select * from accounts where balance >= 100;
id | balance
----+---------
2 | 100
3 | 200
(2 rows)
No (4, 300)
row.
(tested on PostgreSQL 11.2)
Note that PostgreSQL has stronger guarantees on REPEATABLE READ
isolation level. It prevents phantom reads.
From the documentation:
The Repeatable Read isolation level only sees data committed before the transaction began; it never sees either uncommitted data or changes committed during transaction execution by concurrent transactions. (However, the query does see the effects of previous updates executed within its own transaction, even though they are not yet committed.) This is a stronger guarantee than is required by the SQL standard for this isolation level
From the Table 13.1. Transaction Isolation Levels:
Phantom reads are allowed at REPEATABLE READ
isolation level but not in PG
See also:
- How to produce phantom reads
Update
If the statement
update accounts set balance = balance + 30 where balance >= 100;
is issued as the last statement of transaction A only 2 rows will be updated because from the point of view of transaction A there are only two rows satisfying the balance >= 100
predicate:
update accounts set balance = balance + 30 where balance >= 100;
UPDATE 2
After a commit:
commit;
COMMIT
select * from accounts;
id | balance
----+---------
1 | 50
4 | 300
2 | 130
3 | 230
(4 rows)
Only rows returned by this
select * from accounts where balance >= 100;
statement are updated (excluding the row inserted by transaction B)
Note the transaction A will fail if it tries to update
a row changed by another committed concurrent transaction:
A
begin transaction isolation level repeatable read;
BEGIN
select * from accounts where id = 1;
id | balance
----+---------
1 | 50
(1 row)
B
begin;
BEGIN
update accounts set balance = balance + 10;
UPDATE 3
commit;
A:
-- By some logic based on the fact that the balance is 50 we decided to update it to 60
-- balance is updated by committed concurrent transaction
update accounts set balance = 60 where id = 1;
ERROR: could not serialize access due to concurrent update
The error is expected. From the documentation:
If the first updater rolls back, then its effects are negated and the repeatable read transaction can proceed with updating the originally found row. But if the first updater commits (and actually updated or deleted the row, not just locked it) then the repeatable read transaction will be rolled back with the message
ERROR: could not serialize access due to concurrent update
because a repeatable read transaction cannot modify or lock rows changed by other transactions after the repeatable read transaction began.
It is expected that applications will retry failed transactions:
When an application receives this error message, it should abort the current transaction and retry the whole transaction from the beginning.
Can you also try UPDATE in the last query in transaction A ? For example,update accounts set balance = balance + 30;
. I am not sure if it will fail, or just change the rows it knows.
– netok
Mar 24 at 13:18
add a comment |
The transaction A won't fail. It won't see the data inserted by transaction B.
Demo:
create table accounts (id bigserial primary key, balance bigint);
insert into accounts(balance) values (50), (100), (200);
Transaction A:
begin transaction isolation level repeatable read;
select * from accounts where balance >= 100;
id | balance
----+---------
2 | 100
3 | 200
(2 rows)
Transaction B:
begin;
insert into accounts(balance) values (300);
INSERT 0 1
commit;
Transaction A:
select * from accounts where balance >= 100;
id | balance
----+---------
2 | 100
3 | 200
(2 rows)
No (4, 300)
row.
(tested on PostgreSQL 11.2)
Note that PostgreSQL has stronger guarantees on REPEATABLE READ
isolation level. It prevents phantom reads.
From the documentation:
The Repeatable Read isolation level only sees data committed before the transaction began; it never sees either uncommitted data or changes committed during transaction execution by concurrent transactions. (However, the query does see the effects of previous updates executed within its own transaction, even though they are not yet committed.) This is a stronger guarantee than is required by the SQL standard for this isolation level
From the Table 13.1. Transaction Isolation Levels:
Phantom reads are allowed at REPEATABLE READ
isolation level but not in PG
See also:
- How to produce phantom reads
Update
If the statement
update accounts set balance = balance + 30 where balance >= 100;
is issued as the last statement of transaction A only 2 rows will be updated because from the point of view of transaction A there are only two rows satisfying the balance >= 100
predicate:
update accounts set balance = balance + 30 where balance >= 100;
UPDATE 2
After a commit:
commit;
COMMIT
select * from accounts;
id | balance
----+---------
1 | 50
4 | 300
2 | 130
3 | 230
(4 rows)
Only rows returned by this
select * from accounts where balance >= 100;
statement are updated (excluding the row inserted by transaction B)
Note the transaction A will fail if it tries to update
a row changed by another committed concurrent transaction:
A
begin transaction isolation level repeatable read;
BEGIN
select * from accounts where id = 1;
id | balance
----+---------
1 | 50
(1 row)
B
begin;
BEGIN
update accounts set balance = balance + 10;
UPDATE 3
commit;
A:
-- By some logic based on the fact that the balance is 50 we decided to update it to 60
-- balance is updated by committed concurrent transaction
update accounts set balance = 60 where id = 1;
ERROR: could not serialize access due to concurrent update
The error is expected. From the documentation:
If the first updater rolls back, then its effects are negated and the repeatable read transaction can proceed with updating the originally found row. But if the first updater commits (and actually updated or deleted the row, not just locked it) then the repeatable read transaction will be rolled back with the message
ERROR: could not serialize access due to concurrent update
because a repeatable read transaction cannot modify or lock rows changed by other transactions after the repeatable read transaction began.
It is expected that applications will retry failed transactions:
When an application receives this error message, it should abort the current transaction and retry the whole transaction from the beginning.
The transaction A won't fail. It won't see the data inserted by transaction B.
Demo:
create table accounts (id bigserial primary key, balance bigint);
insert into accounts(balance) values (50), (100), (200);
Transaction A:
begin transaction isolation level repeatable read;
select * from accounts where balance >= 100;
id | balance
----+---------
2 | 100
3 | 200
(2 rows)
Transaction B:
begin;
insert into accounts(balance) values (300);
INSERT 0 1
commit;
Transaction A:
select * from accounts where balance >= 100;
id | balance
----+---------
2 | 100
3 | 200
(2 rows)
No (4, 300)
row.
(tested on PostgreSQL 11.2)
Note that PostgreSQL has stronger guarantees on REPEATABLE READ
isolation level. It prevents phantom reads.
From the documentation:
The Repeatable Read isolation level only sees data committed before the transaction began; it never sees either uncommitted data or changes committed during transaction execution by concurrent transactions. (However, the query does see the effects of previous updates executed within its own transaction, even though they are not yet committed.) This is a stronger guarantee than is required by the SQL standard for this isolation level
From the Table 13.1. Transaction Isolation Levels:
Phantom reads are allowed at REPEATABLE READ
isolation level but not in PG
See also:
- How to produce phantom reads
Update
If the statement
update accounts set balance = balance + 30 where balance >= 100;
is issued as the last statement of transaction A only 2 rows will be updated because from the point of view of transaction A there are only two rows satisfying the balance >= 100
predicate:
update accounts set balance = balance + 30 where balance >= 100;
UPDATE 2
After a commit:
commit;
COMMIT
select * from accounts;
id | balance
----+---------
1 | 50
4 | 300
2 | 130
3 | 230
(4 rows)
Only rows returned by this
select * from accounts where balance >= 100;
statement are updated (excluding the row inserted by transaction B)
Note the transaction A will fail if it tries to update
a row changed by another committed concurrent transaction:
A
begin transaction isolation level repeatable read;
BEGIN
select * from accounts where id = 1;
id | balance
----+---------
1 | 50
(1 row)
B
begin;
BEGIN
update accounts set balance = balance + 10;
UPDATE 3
commit;
A:
-- By some logic based on the fact that the balance is 50 we decided to update it to 60
-- balance is updated by committed concurrent transaction
update accounts set balance = 60 where id = 1;
ERROR: could not serialize access due to concurrent update
The error is expected. From the documentation:
If the first updater rolls back, then its effects are negated and the repeatable read transaction can proceed with updating the originally found row. But if the first updater commits (and actually updated or deleted the row, not just locked it) then the repeatable read transaction will be rolled back with the message
ERROR: could not serialize access due to concurrent update
because a repeatable read transaction cannot modify or lock rows changed by other transactions after the repeatable read transaction began.
It is expected that applications will retry failed transactions:
When an application receives this error message, it should abort the current transaction and retry the whole transaction from the beginning.
edited Mar 24 at 14:15
answered Mar 24 at 12:03
caco3caco3
2,5032823
2,5032823
Can you also try UPDATE in the last query in transaction A ? For example,update accounts set balance = balance + 30;
. I am not sure if it will fail, or just change the rows it knows.
– netok
Mar 24 at 13:18
add a comment |
Can you also try UPDATE in the last query in transaction A ? For example,update accounts set balance = balance + 30;
. I am not sure if it will fail, or just change the rows it knows.
– netok
Mar 24 at 13:18
Can you also try UPDATE in the last query in transaction A ? For example,
update accounts set balance = balance + 30;
. I am not sure if it will fail, or just change the rows it knows.– netok
Mar 24 at 13:18
Can you also try UPDATE in the last query in transaction A ? For example,
update accounts set balance = balance + 30;
. I am not sure if it will fail, or just change the rows it knows.– netok
Mar 24 at 13:18
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55323024%2fwill-a-transaction-see-inserts-from-another-concurrent-transaction-if-they-are-i%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown