pg.ddx.io pgsql-general@postgresql.org mailing list archive
help / color / mirror / Atom feedQuestion about tuple´s lock
4+ messages / 2 participants
[nested] [flat]
* Question about tuple´s lock
@ 2023-10-24 16:27 Carlos Alves <carlos.alves@tecnisys.com.br>
0 siblings, 2 replies; 4+ messages in thread
From: Carlos Alves @ 2023-10-24 16:27 UTC (permalink / raw)
To: pgsql-general@lists.postgresql.org
Hi!
I need some help to understand why a transaction wiht a row delete
aquire a AccessExclusiveLock and a row update a ExclusiveLock.
To ilustrate, a made this scenario:
create table tblock
(
id int primary key,
value text
);
insert into tblock values (1, 'somevalue');
First start Transaction 1:
begin;
update tblock set value = 'othervalue' where id = 1;
-- keep this transaction opened
After, start Transaction 2:
begin;
update tblock set value = 'onemorevalue' where id = 1;
-- at this point, this transaction assumes a ExclusiveLock in the tuple;
-- keep this transaction opend
Then, start Transaction 3:
begin;
update tblock set value = 'lastofthevalues' where id = 1;
-- here i can see this transaction trying to aquire a
AccessExclusiveLock in this tuple
So, my question is: why a delete row requires a AccessExclusiveLock in
the tuple insteead of ExclusiveLock?
Bellow, a image of a query over pg_locks:
PostgreSQL version: 13
OS: Centos 7
Thanks in advance.
--
Carlos Alves
Especialista
Sia Trecho 08, lotes 245 / 255 / 265 || +55 (61) 3039-9700
71205-080 || Guará || Brasília, DF 0800-6020097
www.tecnisys.com.br [1]
Links:
------
[1] http://www.tecnisys.com.br
Attachments:
b9879537.png (image/png, 73.1K) download
view image
f62593aa.gif (image/gif, 43B) download
view image
04237a95.gif (image/gif, 3.1K) download
view image
^ permalink raw reply [nested|flat] 4+ messages in thread
* Re: Question about tuple´s lock
@ 2023-10-24 16:33 Carlos Alves <carlos.alves@tecnisys.com.br>
parent: Carlos Alves <carlos.alves@tecnisys.com.br>
1 sibling, 0 replies; 4+ messages in thread
From: Carlos Alves @ 2023-10-24 16:33 UTC (permalink / raw)
To: pgsql-general@lists.postgresql.org
Em 24/10/2023 13:27, Carlos Alves escreveu:
> Hi!
>
> I need some help to understand why a transaction wiht a row delete aquire a AccessExclusiveLock and a row update a ExclusiveLock.
>
> To ilustrate, a made this scenario:
>
> create table tblock
>
> (
>
> id int primary key,
>
> value text
>
> );
>
> insert into tblock values (1, 'somevalue');
>
> First start Transaction 1:
>
> begin;
>
> update tblock set value = 'othervalue' where id = 1;
>
> -- keep this transaction opened
>
> After, start Transaction 2:
>
> begin;
>
> update tblock set value = 'onemorevalue' where id = 1;
>
> -- at this point, this transaction assumes a ExclusiveLock in the tuple;
>
> -- keep this transaction opend
>
> Then, start Transaction 3:
>
> begin;
>
> update tblock set value = 'lastofthevalues' where id = 1;
>
> -- here i can see this transaction trying to aquire a AccessExclusiveLock in this tuple
>
> So, my question is: why a delete row requires a AccessExclusiveLock in the tuple insteead of ExclusiveLock?
>
> Bellow, a image of a query over pg_locks:
>
> PostgreSQL version: 13
>
> OS: Centos 7
>
> Thanks in advance.
I typed the last command wrong. Should be:
Transaction 3:
begin;
delete from tblock where id = 1;
Sorry!
Attachments:
b9879537.png (image/png, 73.1K) download
view image
f62593aa.gif (image/gif, 43B) download
view image
04237a95.gif (image/gif, 3.1K) download
view image
^ permalink raw reply [nested|flat] 4+ messages in thread
* Re: Question about tuple´s lock
@ 2023-10-24 20:31 Tom Lane <tgl@sss.pgh.pa.us>
parent: Carlos Alves <carlos.alves@tecnisys.com.br>
1 sibling, 1 reply; 4+ messages in thread
From: Tom Lane @ 2023-10-24 20:31 UTC (permalink / raw)
To: Carlos Alves <carlos.alves@tecnisys.com.br>; +Cc: pgsql-general@lists.postgresql.org
Carlos Alves <carlos.alves@tecnisys.com.br> writes:
> I need some help to understand why a transaction wiht a row delete
> aquire a AccessExclusiveLock and a row update a ExclusiveLock.
UPDATE can use the weaker lock type if it's not modifying any
column that is part of a unique index. This is to allow concurrency
with foreign-key checks that might wish to grab a read-only (shared)
lock on such a tuple.
A DELETE, or an UPDATE that is modifying key columns, has to
conflict with foreign-key checks.
regards, tom lane
^ permalink raw reply [nested|flat] 4+ messages in thread
* Re: Question about tuple´s lock
@ 2023-10-24 20:43 Carlos Alves <carlos.alves@tecnisys.com.br>
parent: Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 0 replies; 4+ messages in thread
From: Carlos Alves @ 2023-10-24 20:43 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: pgsql-general@lists.postgresql.org
Em 24/10/2023 17:31, Tom Lane escreveu:
> Carlos Alves <carlos.alves@tecnisys.com.br> writes:
>
>> I need some help to understand why a transaction wiht a row delete
>> aquire a AccessExclusiveLock and a row update a ExclusiveLock.
>
> UPDATE can use the weaker lock type if it's not modifying any
> column that is part of a unique index. This is to allow concurrency
> with foreign-key checks that might wish to grab a read-only (shared)
> lock on such a tuple.
>
> A DELETE, or an UPDATE that is modifying key columns, has to
> conflict with foreign-key checks.
>
> regards, tom lane
Tom,
thank you very much for your straight to the point answer!
regards
Carlos Alves
^ permalink raw reply [nested|flat] 4+ messages in thread
end of thread, other threads:[~2023-10-24 20:43 UTC | newest]
Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2023-10-24 16:27 Question about tuple´s lock Carlos Alves <carlos.alves@tecnisys.com.br>
2023-10-24 16:33 ` Carlos Alves <carlos.alves@tecnisys.com.br>
2023-10-24 20:31 ` Tom Lane <tgl@sss.pgh.pa.us>
2023-10-24 20:43 ` Carlos Alves <carlos.alves@tecnisys.com.br>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox