Received: from malur.postgresql.org ([2a02:16a8:dc51::56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1g1w0h-0002XN-9j for pgsql-general@arkaria.postgresql.org; Mon, 17 Sep 2018 16:04:27 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1g1w0f-0007m7-Cn for pgsql-general@arkaria.postgresql.org; Mon, 17 Sep 2018 16:04:25 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1g1w0f-0007lt-3F for pgsql-general@lists.postgresql.org; Mon, 17 Sep 2018 16:04:25 +0000 Received: from mail-qt0-x234.google.com ([2607:f8b0:400d:c0d::234]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1g1w0X-0004GG-T3 for pgsql-general@postgresql.org; Mon, 17 Sep 2018 16:04:24 +0000 Received: by mail-qt0-x234.google.com with SMTP id l42-v6so15718684qtf.13 for ; Mon, 17 Sep 2018 09:04:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:to:cc:references:from:message-id:date:user-agent :mime-version:in-reply-to:content-language; bh=fC5mqYG3iUbL21gn88zaBhsJJXokI5iexVjGhP4d+CM=; b=alBx+JNNFwWvKnQzot5Qjeiw6AK6C+RpmzD5DXvxEoraC5F21fQ+vOTY0an1RhuKM8 KLjMvI4xbqBqkJ7Q9WJEr1nvI2whFt8zZY7PnwSFmgCCZnsTvMdJu2CcS1uAPfDMBFL2 po6TZ0q4Di6vsOsaHe7utz8t69gvX83lGS30SEIutqjA3/4xyAbZYqEnlHRVnlvDNnhT UvGsRDkDuHRnY3WA1uBxzOA0dzesXCqFNSv4s4MVcZwKVVHy+NkLMy9IEV/Lz9pUjdX/ VSZvPScUqEyGjs+Lg+BrfwjJcoHk3q7eiydIQh/5dBrHLbTQeAr9xUg8fMGorfD5EnXz FPQg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language; bh=fC5mqYG3iUbL21gn88zaBhsJJXokI5iexVjGhP4d+CM=; b=JSYKvDd17oTP2ELl3Y/6Hf9fwsbpfE2emKXZrL0zXoiLjKgjBImRWQxTNjholWXjO4 m4l321Wm8W1HmWYNDmJC6rEuA9Pje3FU2Kj6xnPGnah0gJozZYQoMnt7G+OfqIaQlPHk yCCDHtkloN8sP3iyOZvX8s/cRoB+9YKZRN+0X44THGV5ItAP2aT3EbXq9o1X7886kjk9 uD18L9ZWfPLAyFgGXUPP70k6TlMtnJqWDID3lY7X1CeAfCpwe0VgX5OhkylIRwdHpLYj 6bTRC367dwfNUdWWRwdIPcrAXfltiu/7cDS/m/RjJJNJ6ea0dCjgRw5aueldXmBXdMSd 3tiw== X-Gm-Message-State: APzg51BZAZ9+e7mPDJm3Fei2w8/qr7yw2mLo1RLX7VwFn9c5BlLbVpf/ s1wE9C/9p9U/Q3LS5mUC2Us5Iklj X-Google-Smtp-Source: ANB0VdZCCfBkNAD1a8LCWomiMVCWxP+TMUwr2auobsgOYHy3sn0e77OHjlEC6ArYJyrynObzWxsHtg== X-Received: by 2002:aed:23f8:: with SMTP id k53-v6mr18506841qtc.230.1537200255959; Mon, 17 Sep 2018 09:04:15 -0700 (PDT) Received: from [192.168.1.250] (host130.190-229-77.telecom.net.ar. [190.229.77.130]) by smtp.gmail.com with ESMTPSA id k126-v6sm8089538qkb.91.2018.09.17.09.04.14 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 17 Sep 2018 09:04:15 -0700 (PDT) Subject: Re: Logical locking beyond pg_advisory To: Chris Travers , Merlin Moncure Cc: Postgres General References: <8d7f3295-c2d6-ef00-1300-4f6db798f76e@gmail.com> From: marcelo Message-ID: <2224096b-7095-372d-fb7b-f3e4add7d334@gmail.com> Date: Mon, 17 Sep 2018 13:04:10 -0300 User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:52.0) Gecko/20100101 Thunderbird/52.5.2 MIME-Version: 1.0 In-Reply-To: Content-Type: multipart/alternative; boundary="------------AFD7D24779CD45AB1F0ABC8A" Content-Language: en-US X-Antivirus: Avast (VPS 180917-4, 17/09/2018), Outbound message X-Antivirus-Status: Clean List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk This is a multi-part message in MIME format. --------------AFD7D24779CD45AB1F0ABC8A Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit On 17/09/2018 12:21 , Chris Travers wrote: > > > On Mon, Sep 17, 2018 at 5:09 PM Merlin Moncure > wrote: > > On Sun, Sep 16, 2018 at 3:53 PM marcelo > wrote: > > > > I need a mechanism of "logical locking" more ductile than the > pg_advisory family. > > I'm thinking of a table ("lock_table") that would be part of the > database, with columns > > * tablename varchar - name of the table "locked" > > * rowid integer, - id of the row "locked" > > * ownerid varchar, - identifier of the "user" who acquired the lock > > * acquired timestamp - to be able to release "abandoned" locks > after a certain time > > > > and a group of functions > > 1) lock_table (tablename varchar, ownerid varchar) bool - get to > lock over the entire table, setting rowid to zero > > 2) unlock_table (tablename varchar, ownerid varchar) bool - > unlock the table, if the owner is the recorded one > > 3) locked_table (tablename varchar, ownerid varchar) bool - ask > if the table is locked by some user other than the ownerid argument > > 4) lock_row (tablename varchar, rowid integer, ownerid varchar) > bool - similar to pg_try_advisory_lock > > 5) unlock_row (tablename varchar, rowid integer, ownerid > varchar) bool - similar to pg_advisory_unlock > > 6) unlock_all (ownerid varchar) bool - unlock all locks owned by > ownerid > > > > The timeout (default, maybe 15 minutes) is implicitly applied if > the lock is taken by another user (there will be no notification). > > Redundant locks are not queued, they simply return true, may be > after an update of the acquired column. > > Successful locks insert a new row, except the rare case of a > timeout, which becomes an update (ownerid and acquired) > > Unlock operations deletes the corresponding row > > > > My question is double > > a) What is the opinion on the project? > > b) What are the consequences of the large number of inserts and > deletions > > c) Performance. In fact, pg_advisory* implies a network > roundtrip, but (I think) no table operations. > > Why can't you use the advisory lock functions?  The challenge with > manually managed locks are they they are slow and you will lose the > coordination the database provides you.  For example, if your > application crashes you will have to clean up all held locks yourself. > Building out that infrastructure will be difficult. > > > First, I think in an ideal world, you wouldn't handle this problem > with either approach but sometimes you have to. > > I have done both approaches actually.  LedgerSMB uses its own lock > table because locks have to persist across multiple HTTP requests and > we have various automatic cleanup processes. > > When I was working on  the queue management stuff at Novozymes we used > advisory locks extensively. > > These two approaches have serious downsides: > 1.  Lock tables are *slow* and require careful thinking through > cleanup scenarios.  In LedgerSMB we tied to the application session > with an ON DELETE event that would unlock the row.  We estimated that > for every 2 seconds that the db spent doing useful work, it spent 42 > seconds managing the locks.....  Additionally the fact that locks take > effect on snapshot advance is a problem here. > > 2.  In my talk, "PostgreSQL at 10TB and  Beyond" I talk about a > problem we had using advisory locks for managing rows that were being > processed for deletion.  Since the deletion was the scan for items at > the head of an index, under heavy load we could spend long enough > checking dead rows that the locks could go away with our snapshot > failing to advance.  This would result in duplicate processing.  So > the fact that advisory locks don't really follow snapshot semantics is > a really big problem here since it means you can have race conditions > in advisory locks that can't happen with other locking issues.  I > still love advisory locks but they are not a good tool for this. > > The real solution most of the time is actually to lock the rows by > selecting FOR UPDATE and possibly SKIP LOCKED. The way update/delete > row locking in PostgreSQL works is usually good enough except in a few > rare edge cases.  Only in *very rare* cases do lock tables or advisory > locks make sense for actual row processing. > > > merlin > > > > -- > Best Wishes, > Chris Travers > > Efficito:  Hosted Accounting and ERP.  Robust and Flexible.  No vendor > lock-in. > http://www.efficito.com/learn_more I´m using an ORM (Devart´s) to access the database, so, I cannot "select ... FOR UPDATE". The application paradigm is that a user have a list of records (after a query) and she could update or delete any of them as the business rules allows it. So, at least an advisory lock is a must. I´m convinced by now: I would stay with advisory locks... expecting no app crash could occur... Thank you all. Marcelo --- El software de antivirus Avast ha analizado este correo electrónico en busca de virus. https://www.avast.com/antivirus --------------AFD7D24779CD45AB1F0ABC8A Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: 8bit

On 17/09/2018 12:21 , Chris Travers wrote:


On Mon, Sep 17, 2018 at 5:09 PM Merlin Moncure <mmoncure@gmail.com> wrote:
On Sun, Sep 16, 2018 at 3:53 PM marcelo <marcelo.nicolet@gmail.com> wrote:
>
> I need a mechanism of "logical locking" more ductile than the pg_advisory family.
> I'm thinking of a table ("lock_table") that would be part of the database, with columns
> * tablename varchar - name of the table "locked"
> * rowid integer, - id of the row "locked"
> * ownerid varchar, - identifier of the "user" who acquired the lock
> * acquired timestamp - to be able to release "abandoned" locks after a certain time
>
> and a group of functions
> 1) lock_table (tablename varchar, ownerid varchar) bool - get to lock over the entire table, setting rowid to zero
> 2) unlock_table (tablename varchar, ownerid varchar) bool - unlock the table, if the owner is the recorded one
> 3) locked_table (tablename varchar, ownerid varchar) bool - ask if the table is locked by some user other than the ownerid argument
> 4) lock_row (tablename varchar, rowid integer, ownerid varchar) bool - similar to pg_try_advisory_lock
> 5) unlock_row (tablename varchar, rowid integer, ownerid varchar) bool - similar to pg_advisory_unlock
> 6) unlock_all (ownerid varchar) bool - unlock all locks owned by ownerid
>
> The timeout (default, maybe 15 minutes) is implicitly applied if the lock is taken by another user (there will be no notification).
> Redundant locks are not queued, they simply return true, may be after an update of the acquired column.
> Successful locks insert a new row, except the rare case of a timeout, which becomes an update (ownerid and acquired)
> Unlock operations deletes the corresponding row
>
> My question is double
> a) What is the opinion on the project?
> b) What are the consequences of the large number of inserts and deletions
> c) Performance. In fact, pg_advisory* implies a network roundtrip, but (I think) no table operations.

Why can't you use the advisory lock functions?  The challenge with
manually managed locks are they they are slow and you will lose the
coordination the database provides you.  For example, if your
application crashes you will have to clean up all held locks yourself.
Building out that infrastructure will be difficult.

First, I think in an ideal world, you wouldn't handle this problem with either approach but sometimes you have to.

I have done both approaches actually.  LedgerSMB uses its own lock table because locks have to persist across multiple HTTP requests and we have various automatic cleanup processes.

When I was working on  the queue management stuff at Novozymes we used advisory locks extensively.

These two approaches have serious downsides:
1.  Lock tables are *slow* and require careful thinking through cleanup scenarios.  In LedgerSMB we tied to the application session with an ON DELETE event that would unlock the row.  We estimated that for every 2 seconds that the db spent doing useful work, it spent 42 seconds managing the locks.....  Additionally the fact that locks take effect on snapshot advance is a problem here.

2.  In my talk, "PostgreSQL at 10TB and  Beyond" I talk about a problem we had using advisory locks for managing rows that were being processed for deletion.  Since the deletion was the scan for items at the head of an index, under heavy load we could spend long enough checking dead rows that the locks could go away with our snapshot failing to advance.  This would result in duplicate processing.  So the fact that advisory locks don't really follow snapshot semantics is a really big problem here since it means you can have race conditions in advisory locks that can't happen with other locking issues.  I still love advisory locks but they are not a good tool for this.

The real solution most of the time is actually to lock the rows by selecting FOR UPDATE and possibly SKIP LOCKED.  The way update/delete row locking in PostgreSQL works is usually good enough except in a few rare edge cases.  Only in *very rare* cases do lock tables or advisory locks make sense for actual row processing.

merlin



--
Best Wishes,
Chris Travers

Efficito:  Hosted Accounting and ERP.  Robust and Flexible.  No vendor lock-in.
I´m using an ORM (Devart´s) to access the database, so, I cannot "select ... FOR UPDATE". The application paradigm is that a user have a list of records (after a query) and she could update or delete any of them as the business rules allows it. So, at least an advisory lock is a must.
I´m convinced by now: I would stay with advisory locks... expecting no app crash could occur...
Thank you all.
Marcelo

Libre de virus. www.avast.com
--------------AFD7D24779CD45AB1F0ABC8A--