简体   繁体   English

Postgres:为单个表或单个读取选择较弱的事务隔离保证?

[英]Postgres: Opting in to weaker transaction isolation guarantees for a single table or a single read?

I'm writing a web app with Postgres 13 as the backend.我正在编写一个以 Postgres 13 作为后端的网络应用程序。 Most requests are wrapped in a transaction, using the SERIALIZABLE isolation level.大多数请求都包含在一个事务中,使用SERIALIZABLE隔离级别。

For most things, this works great.对于大多数事情,这很好用。 However, there are some cases where I'd like some reads in the transaction to have less strict isolation.但是,在某些情况下,我希望事务中的某些读取具有不那么严格的隔离。

For example, I'm introducing a global_flags table for infrequently-changed settings that any request might make use of:例如,我正在引入一个global_flags表,用于任何请求可能使用的不经常更改的设置:

await sqlAsync(`BEGIN; SET TRANSACTION ISOLATION LEVEL SERIALIZABLE`);

const batchSize = await sqlAsync(
    `SELECT value FROM global_flags WHERE name = 'batch_size'`);

// ... a bunch more work ...

await sqlAsync('COMMIT');

I'm a bit worried that when we manually make changes to global_flags entries, we might cause an increase in "serialization failure" errors for in-flight transactions.我有点担心当我们手动更改global_flags条目时,我们可能会导致global_flags事务的“序列化失败”错误增加。 Is there a way to tell Postgres that I don't need as strong of a consistency guarantee for reads of the global_flags table?有没有办法告诉 Postgres 我不需要对global_flags表的读取提供如此强的一致性保证?

You needn't worry a bit.你不必担心。

If the one transaction does nothing except change that flag, and the other just reads the table (and doesn't try to write to it!), the two transactions will have a simple RW or WR dependency on each other, and there will be no cycles that cause a serialization error.如果一个事务除了改变那个标志什么都不做,而另一个只是读取表(并且不尝试写入它!),这两个事务将具有简单的 RW 或 WR 依赖关系,并且将有没有导致序列化错误的循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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