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 1g1qe2-0005RF-Nj for pgsql-general@arkaria.postgresql.org; Mon, 17 Sep 2018 10:20:43 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1g1qe0-0008Dq-Rf for pgsql-general@arkaria.postgresql.org; Mon, 17 Sep 2018 10:20:40 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1g1qe0-0008DJ-9E for pgsql-general@lists.postgresql.org; Mon, 17 Sep 2018 10:20:40 +0000 Received: from mail-qk1-x72f.google.com ([2607:f8b0:4864:20::72f]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1g1qdw-0007uK-Ko for pgsql-general@postgresql.org; Mon, 17 Sep 2018 10:20:38 +0000 Received: by mail-qk1-x72f.google.com with SMTP id d131-v6so8651166qke.11 for ; Mon, 17 Sep 2018 03:20:36 -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=8LhdzaEIS2pyrLPAOzNB9qSj4RyS6Xfuv9ku1sEYClI=; b=WUSCo75XuYJDdz/1Csg9pCxsoDwE+NVUhe65pOhvADB6/Ojp88DVNuB/gjBW5MMtGn nvbqQcgS54PUODqr3WEvl/o5o3Aj/u+qrGANslTT2eOEWqv+Cr0MQFph3a+jE7TdrrMF JGOy3vBZRAjLAaaWWR7/O8yulia17b9x5WIGc1hamOj1lsVCN6gq+SQJNRcwyJ80mjLU sqImt0K9nYZj52RSHj4/wAiMhH9xHIx1L30DKK4+yv3DvAFYiwilwnYtXgqSxV+BaOYM rBMhl+H4XPR0LR6pYWn1/ydpbdN6DGsyaULdUbmL8ijznS42aHQyse/NqgPv0dMhEDMv DnMw== 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=8LhdzaEIS2pyrLPAOzNB9qSj4RyS6Xfuv9ku1sEYClI=; b=Gz/c/gPj98cIZ+te2ATvg2/PyiJrQ778NaGY0PapmVaVO3tAzbVRPPk7sMIlEm1rOM a/xOuIe+yyK1hnuYtmlcRf3srOAHcxSDLw1aWJpWda9XyrlHRXXc9Zonwrvl3X6wbqON RFpUf4+SiT0e3AmbutDJGHezMDx9fcRLmekxLoIcUnybT0DTNCxKkfItljdvj7Tiju0e C1CnjfCkRXXKVNXd2s5pROOlNjLSovxJKMGGfF8/mlW9+PdncUUkMowBOz53ViUN05AY FkTCkHkcNlDqpCOrYx1SGu2hS3tlYdA2pv5rntI7tWQnGy6m9kSrbNsvEKuXA+ntXVZ1 RlIg== X-Gm-Message-State: APzg51AZdoDO3dkcnRNVdYEFaspzEKLt4I/pg7wUd0VUJFriYyaILb5x jh1Hg8Sd+luLIpGyyK/nJvfIe+3V X-Google-Smtp-Source: ANB0VdaCvS7S7uFv9NkdFUehLu6nl0fN8YL8A/+tSmGSsu4dVNkK4WaFd3L1Hzj9EkOIDk1DzbcTlQ== X-Received: by 2002:a37:9904:: with SMTP id b4-v6mr16922777qke.102.1537179635543; Mon, 17 Sep 2018 03:20:35 -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 r7-v6sm10619674qkh.83.2018.09.17.03.20.33 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 17 Sep 2018 03:20:34 -0700 (PDT) Subject: Re: Logical locking beyond pg_advisory To: =?UTF-8?Q?Fabr=c3=adzio_de_Royes_Mello?= Cc: Postgres General References: <8d7f3295-c2d6-ef00-1300-4f6db798f76e@gmail.com> From: marcelo Message-ID: Date: Mon, 17 Sep 2018 07:20:29 -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="------------55BF23364E89CA3A64FA65DE" Content-Language: en-US X-Antivirus: Avast (VPS 180917-2, 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. --------------55BF23364E89CA3A64FA65DE Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit /Would be nice if you explain more about what kind of problem you want to solve./ There are two main questions "to solve" a) generally speaking, I like to get some form of "exclusive access" to the row before updating or deleting. None of the optimistic / pesimistic automatic variants of concurrency management glad me. Nor, of course, the "versioning" variants. b) some of the tables I´m working on have a "number" column (_not the PK_) which sometimes come from material, external sources, but sometimes must be assigned by the system. This could be solved two main ways b.1) Use a trigger to get the famous "max(n) + 1". At least in one of the cases, the number automatically generated must fall into different ranges conditioned by the value of another column, and the ranges vary between versions of the database. b.2) "Lock the entire table", get programmatically the next number for the correct range, assign it and free the table lock. Of course, and beforehand, all database manipulations are done thru applications. Till yesterday, I was working with the "advisory" family. Having a bigint as the only "lock identifier" I was working with a hash of the table name XORed with the id of the row or zero for the entire table. (All my tables have an autosequential integer id as PK). Even if I found a very robust hash algorithm for the table name, I cannot discard some collision once the id was xored. I tested five or six table names, along 20000 ids every one, without collision. But... Of course, I need the "full table lock" for inserts. So, it´s a very separated concern with updates and deletions. But... TIA On 17/09/2018 03:19 , Fabrízio de Royes Mello wrote: > > Em dom, 16 de set de 2018 às 17:53, marcelo > escreveu: > > > > 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? > > Would be nice if you explain more about what kind of problem you want > to solve. > > > b) What are the consequences of the large number of inserts and > deletions > > The first thing came to my mind with this approach is table bloat. > > > c) Performance. In fact, pg_advisory* implies a network roundtrip, > but (I think) no table operations. > > Yeap... no table operations. > > Regards, > > -- >    Fabrízio de Royes Mello         Timbira - http://www.timbira.com.br/ >    PostgreSQL: Consultoria, Desenvolvimento, Suporte 24x7 e Treinamento --- El software de antivirus Avast ha analizado este correo electrónico en busca de virus. https://www.avast.com/antivirus --------------55BF23364E89CA3A64FA65DE Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: 8bit Would be nice if you explain more about what kind of problem you want to solve.

There are two main questions "to solve"
a) generally speaking, I like to get some form of "exclusive access" to the row before updating or deleting. None of the optimistic / pesimistic automatic variants of concurrency management glad me. Nor, of course, the "versioning" variants.
b) some of the tables I´m working on have a "number" column (not the PK) which sometimes come from material, external sources, but sometimes must be assigned by the system. This could be solved two main ways
b.1) Use a trigger to get the famous "max(n) + 1". At least in one of the cases, the number automatically generated must fall into different ranges conditioned by the value of another column, and the ranges vary between versions of the database.
b.2) "Lock the entire table", get programmatically the next number for the correct range, assign it and free the table lock.

Of course, and beforehand, all database manipulations are done thru applications.
Till yesterday, I was working with the "advisory" family. Having a bigint as the only "lock identifier" I was working with a hash of the table name XORed with the id of the row or zero for the entire table. (All my tables have an autosequential integer id as PK).
Even if I found a very robust hash algorithm for the table name, I cannot discard some collision once the id was xored. I tested five or six table names, along 20000 ids every one, without collision. But...
Of course, I need the "full table lock" for inserts. So, it´s a very separated concern with updates and deletions. But...

TIA

 
On 17/09/2018 03:19 , Fabrízio de Royes Mello wrote:

Em dom, 16 de set de 2018 às 17:53, marcelo <marcelo.nicolet@gmail.com> escreveu:
>
> 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?

Would be nice if you explain more about what kind of problem you want to solve.

> b) What are the consequences of the large number of inserts and deletions

The first thing came to my mind with this approach is table bloat.

> c) Performance. In fact, pg_advisory* implies a network roundtrip, but (I think) no table operations.

Yeap... no table operations.

Regards,

--
   Fabrízio de Royes Mello         Timbira - http://www.timbira.com.br/
   PostgreSQL: Consultoria, Desenvolvimento, Suporte 24x7 e Treinamento


Libre de virus. www.avast.com
--------------55BF23364E89CA3A64FA65DE--