PostgreSQL + Rails 并发澄清

发布于 2025-01-03 05:31:03 字数 403 浏览 0 评论 0原文

我正在构建一个后台作业来更新 Web 应用程序的用户统计信息。该作业目前需要 55-60 秒,我担心如果用户在该作业运行的同时尝试加载他的统计页面会发生什么。

根据我所读到的有关 PostgreSQL 和并发性的内容,如果两个客户端尝试访问同一行(一个更新,一个读取),并且我没有显式启动任何事务,第一个客户端只需等待第二个客户端结束。

因此,如果我理解正确的话,我可能遭受的唯一性能影响是用户尝试在更新行的同时加载其统计页面的可能性极小。除非我明确配置 Postgres 来执行此操作,否则整个统计表不会在 55-60 秒的工作期间被锁定,对吧?

这是正确的解释吗?还有其他我遗漏的因素吗?

(我提到 Rails 部分只是为了防止它与上述场景有任何关系)

(另外:PostgreSQL 版本是 9.0.4)

I'm building a background job that's updating users' statistics for a web application. The job currently takes 55-60 seconds, and I'm concerned about what would happen if a user were to try to load his stats page at the same time that job is running.

From what I've read about PostgreSQL and concurrency, if two clients attempt to access the same row (one updating and one reading), and I'm not explicitly starting any transactions, the first one just has to wait for the second one to finish.

So if I'm understanding that correctly, the only performance hit I'm likely to incur is on the infinitesimally small chance that a user tries to load his stats page at the same moment that the row is being updated. It's not like the whole stats table is locked up during the 55-60 second job unless I were to explicitly configure Postgres to do that, right?

Is that a correct interpretation? Are there other factors I'm missing?

(I mention the Rails part just in case it has any bearing on the above scenario)

(Also: the PostgreSQL version is 9.0.4)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我喜欢麦丽素 2025-01-10 05:31:03

这取决于事务隔离级别。如果我有你的情况 - 你正在谈论脏读以避免延迟。是的,如果您使用默认隔离级别,脏读是不可能的。仅当读取器尝试获取正在更新的同一行时,它才会等待写入器。

已提交读是 PostgreSQL 中的默认隔离级别。当事务在此隔离级别上运行时,SELECT 查询只能看到查询开始之前提交的数据;

隔离规范

It depends on transaction isolation level. If I've got your case - you are talking about Dirty Read avoiding delay. And YES, Dirty Read is impossible if you are using default isolation level. Reader will wait for the writer only when it will try to get the same row that is being updated.

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;

specs on ISOLATION

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文