Thread: questions about concurrency control in Postgresql

questions about concurrency control in Postgresql

From
黄晓骋
Date:
<div class="Section1"><p class="MsoNormal"><span lang="EN-US">Hello,</span><p class="MsoPlainText"><span lang="EN-US">I
thinkin Postgresql, concurrency control acts like this:</span><p class="MsoPlainText"><span lang="EN-US">tuple's
infomaskshows if it is being updated. If it is being updated now, the latter transaction should reread the tuple and
getthe newer tuple. During the progress of getting the newer tuple, it must use transaction lock, I mean
XactLockTableWait(...).</span><pclass="MsoPlainText"><span lang="EN-US">From the above, I think the tuple lock is
unnecessary,because it uses transaction lock.</span><p class="MsoPlainText"><span lang="EN-US">Besides, tuple lock is
unlockedafter the tuple is updated but not after the transaction commits. I mean it's not 2PL.</span><p
class="MsoPlainText"><spanlang="EN-US">So, may you tell me why there is tuple lock in Postgresql ? Is the tuple lock
necessary?</span><pclass="MsoPlainText"><span lang="EN-US"> </span><p class="MsoNormal"><span
lang="EN-US">Thanks,</span><pclass="MsoNormal"><span lang="EN-US"> </span><p class="MsoNormal"><span
lang="EN-US">--HuangXiaocheng</span><p class="MsoNormal"><span lang="EN-US">--Database & Information System Lab,
NankaiUniversity</span><p class="MsoNormal"><span lang="EN-US"> </span></div><br /><br />__________ Information from
ESETNOD32 Antivirus, version of virus signature database 4668 (20091207) __________<br /><br />The message was checked
byESET NOD32 Antivirus.<br /><br /><a href="http://www.eset.com">http://www.eset.com</a><br /> 

Re: questions about concurrency control in Postgresql

From
Daniel Farina
Date:
2009/12/7 黄晓骋 <huangxclife@gmail.com>:
> Hello,
>
> I think in Postgresql, concurrency control acts like this:
>
> tuple's infomask shows if it is being updated. If it is being updated now,
> the latter transaction should reread the tuple and get the newer tuple.
> During the progress of getting the newer tuple, it must use transaction
> lock, I mean XactLockTableWait(...).

That is a table lock...depending on the lock, other backends may be
allowed to update tuples in the relation still.  Fine-grained tuple
locks are used to prevent unnecessary contention for a table-wide
lock.

See the documentation at the manual page:

http://www.postgresql.org/docs/8.4/static/explicit-locking.html

It gives a thorough treatment of table and row locking levels and
conflicts, as well as what gets what locks.

fdr


Re: questions about concurrency control in Postgresql

From
Greg Stark
Date:
2009/12/8 黄晓骋 <huangxclife@gmail.com>:
> From the above, I think the tuple lock is unnecessary, because it uses
> transaction lock.
>
> Besides, tuple lock is unlocked after the tuple is updated but not after the
> transaction commits. I mean it's not 2PL.

It's a two step process. An update marks the tuple locked. Another
transaction which comes along and wants to lock the tuple waits on the
transaction marked on the tuple. When the first transaction commits or
aborts then the second transaction can proceed and lock the tuple
itself. The reason we need both locks is because the first transaction
cannot go around the whole database finding every tuple it ever locked
to unlock it, firstly that could be a very large list and secondly
there would be no way to do that atomically.

Tuple locks and all user-visible locks are indeed held until the end
of the transaction.

--
greg


答复: questions about concurrency control in Postgresql

From
黄晓骋
Date:
>It's a two step process. An update marks the tuple locked. Another
>transaction which comes along and wants to lock the tuple waits on the
>transaction marked on the tuple. When the first transaction commits or
>aborts then the second transaction can proceed and lock the tuple
>itself.
I agree with it.

>The reason we need both locks is because the first transaction
>cannot go around the whole database finding every tuple it ever locked
>to unlock it, firstly that could be a very large list and secondly
>there would be no way to do that atomically.

You mean that 2PL is hard to realize actually, I agree too.
But it doesn't mean tuple lock is necessary.

>Tuple locks and all user-visible locks are indeed held until the end
>of the transaction.
I don't agree with it, for I see unlocktuple(...) in heap_update(...).

--Huang Xiaocheng
--Database & Information System Lab, Nankai University


__________ Information from ESET NOD32 Antivirus, version of virus signature database 4671 (20091208) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com




答复: questions about concurrency control in Postgresql

From
黄晓骋
Date:
I think I know why we need tuple lock.
Though we have tuple's infomask shows whether the tuple is being updated, before we set the tuple's infomask, there may
betwo transaction coming and updating the tuple. They both think the tuple is ok to be updated, and then it's wrong. 
In PostgreSQL, we can use buffer lock to solve the problem , but its granularity is not proper. So we must use tuple
lockto solve the problem. 
Thank you, Greg. You prompt me to think clearly about it.
Happy communicating with you, and thanks again.


--Huang Xiaocheng
--Database & Information System Lab, Nankai University

-----邮件原件-----
发件人: gsstark@gmail.com [mailto:gsstark@gmail.com] 代表 Greg Stark
发送时间: 2009年12月8日 20:16
收件人: 黄晓骋
抄送: pgsql-hackers@postgresql.org
主题: Re: questions about concurrency control in Postgresql

2009/12/8 黄晓骋 <huangxclife@gmail.com>:
> From the above, I think the tuple lock is unnecessary, because it uses
> transaction lock.
>
> Besides, tuple lock is unlocked after the tuple is updated but not after the
> transaction commits. I mean it's not 2PL.

It's a two step process. An update marks the tuple locked. Another
transaction which comes along and wants to lock the tuple waits on the
transaction marked on the tuple. When the first transaction commits or
aborts then the second transaction can proceed and lock the tuple
itself. The reason we need both locks is because the first transaction
cannot go around the whole database finding every tuple it ever locked
to unlock it, firstly that could be a very large list and secondly
there would be no way to do that atomically.

Tuple locks and all user-visible locks are indeed held until the end
of the transaction.

--
greg


__________ Information from ESET NOD32 Antivirus, version of virus signature database 4671 (20091208) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com



__________ Information from ESET NOD32 Antivirus, version of virus signature database 4674 (20091209) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com




答复: [HACKERS] 答复: questions about concurrency control in Postgresql

From
黄晓骋
Date:
You are right. I never consider the SELECT FOR UPDATE/SHARE type queries, so I got the wrong conclusion.
I have seen the content in the comment of heap_lock_tuple().

Thank you,
Best Regards,


--Huang Xiaocheng
--Database & Information System Lab, Nankai University

-----邮件原件-----
发件人: Alvaro Herrera [mailto:alvherre@commandprompt.com]
发送时间: 2009年12月10日 22:54
收件人: 黄晓骋
抄送: 'Greg Stark'; pgsql-hackers@postgresql.org
主题: Re: [HACKERS] 答复: questions about concurrency control in Postgresql

黄晓骋 escribió:
> I think I know why we need tuple lock.
> Though we have tuple's infomask shows whether the tuple is being updated, before we set the tuple's infomask, there
maybe two transaction coming and updating the tuple. They both think the tuple is ok to be updated, and then it's
wrong.
> In PostgreSQL, we can use buffer lock to solve the problem , but its granularity is not proper. So we must use tuple
lockto solve the problem. 
> Thank you, Greg. You prompt me to think clearly about it.

Actually it's the buffer lock that's used to protect most of infomask.
Tuple locks are only used while XMAX and some infomask bits are set for
SELECT FOR UPDATE/SHARE type queries.  That can take a while because it
may need I/O in pg_multixact, so the buffer lock is not appropriate to
hold for so long.

--
Alvaro Herrera                                http://www.CommandPrompt.com/
The PostgreSQL Company - Command Prompt, Inc.


__________ Information from ESET NOD32 Antivirus, version of virus signature database 4677 (20091210) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com



__________ Information from ESET NOD32 Antivirus, version of virus signature database 4687 (20091214) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com