PostgreSQL locking mode

寵の児 提交于 2019-12-08 07:23:01

问题


From this doc http://www.postgresql.org/docs/current/static/explicit-locking.html

I knew PostgreSQL provides various lock modes to control concurrent access to data in tables.

My problem is I have many sessions will accessing my DB , but I'm confuse should I made 1 big table with 40 column or many tables with fewer column (one to one relationship).

  1. Because when I select the data I will select all of it ---> it takes more time when I select from many tables using INNER JOIN, but it takes less time to select from 1 big table. So it will my php respond slower if I'm using many tables.

  2. But when I use just one table meanwhile many session will update my data in the table, I'm afraid of deadlocks or delay because commands UPDATE, DELETE, and INSERT acquire ROW EXCLUSIVE lock mode on the target table. In general, this lock mode will be acquired by any command that modifies data in a table.

Could anyone suggested which is the best approach should I made? One big table or many tables?


回答1:


It is true that INSERT, UPDATE or DELETE must acquire ROW EXCLUSIVE lock on table to be updated.

However, this lock does not prevent SELECT from working normally. SELECT only requires ACCESS SHARE lock. This lock is compatible with ROW EXCLUSIVE - in other words, you can perfectly execute SELECT while other data is updated by INSERT, UPDATE or DELETE, as long as you don't acquire any explicit locks.

In other words, you should never see any deadlocks using second approach (just don't use SELECT FOR UPDATE and you'll be fine).

Read more in PostgreSQL documentation.



来源:https://stackoverflow.com/questions/15893896/postgresql-locking-mode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!