简体   繁体   中英

Conflict on two PostgreSQL transactions with serializable isolation level

I have two concurrent SQL transactions with the most strict level of isolation (serializable)

According to here :

The SQL standard defines four levels of transaction isolation. The most strict is Serializable, which is defined by the standard in a paragraph which says that any concurrent execution of a set of Serializable transactions is guaranteed to produce the same effect as running them one at a time in some order

Or from MSDN regarding SET TRANSACTION ISOLATION LEVEL:

Places a range lock on the data set, preventing other users from updating or inserting rows into the data set until the transaction is complete. This is the most restrictive of the four isolation levels. Because concurrency is lower, use this option only when necessary. This option has the same effect as setting HOLDLOCK on all tables in all SELECT statements in a transaction.

But you see in the middle of the second transaction the table is empty. How is that possible and how can I fix it??

具有可序列化隔离级别的两个 Postgres 事务的冲突

This is a consequence of the way Postgres provides system catalogs. The relevant note in the documentation is clear enough:

Internal access to the system catalogs is not done using the isolation level of the current transaction. This means that newly created database objects such as tables are visible to concurrent Repeatable Read and Serializable transactions, even though the rows they contain are not.

The example you provided does not break the cited rules about the serializable isolation level. Note, that you start the second transaction when the table was dropped and created by the first transaction. The resulting behavior is as expected.

If you had started both transactions before the table was dropped, then one of them would be suspended until the other completes.

The SQL Server documentation is clearly not relevant.

But yes, that looks like a PostgreSQL bug that you should report. But please with an SQL script, not with an animated GIF...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM