Re: Advisory transaction lock for 128-bit space

From: Bill Moran <wmoran(at)potentialtech(dot)com>
To: Andrey Chursin <andll(at)danasoft(dot)ws>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Advisory transaction lock for 128-bit space
Date: 2012-03-07 18:47:46
Message-ID: 20120307134746.278a2c01.wmoran@potentialtech.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

In response to Andrey Chursin <andll(at)danasoft(dot)ws>:

> Hello.
> My application need to set advisory lock on UUID key, almost like it
> does pg_advisory_xact_lock function. The problem is argument type of
> this function - it consumes 8-byte value, not 16-byte.
>
> I can not lock on any(hi, low or middle) 8-byte part of UUID, as far
> as it can produce unexpected deadlock issues, because locking on some
> ID in this way will imply locking on more "wide" set of ID then I
> requested.
>
> Now I am doing the 'trick' using indexing insert/delete, e.g.:
> INSERT INTO table_with_uuid_pk(locking_value);
> DELETE FROM table_with_uuid_pk WHERE <inserted_row_above>;
>
> It works, but I did not found any description of such 'feature' of
> indexes. Can u, please, help to solve this synchronization issue, and
> comment the way I am dealing with it now(with index locking)
>
> P.S. The most significant fear I know have, is that currently used
> method suffers with same problem as locking for part of UUID - doest
> insert/delete really locks only on the value i passed to it?

Have you considered using row locking? If you need the lock to
extend across multiple transactions, then row locking won't work.

The problem with what you're doing is that if a process crashes, or
is otherwise uncleanly disconnected, the lock table is polluted.
My recommendation would be to add another column to the table with
the UUID key that keeps a unique 8 byte number that you can use
the advisory lock on. You can use a sequence to automatically
generate unique values for it, and as long as you don't exceed
2 billion rows, you'll be fine. Of course, if you expect that
you might exceed 2 billion rows, that won't work either.

--
Bill Moran
http://www.potentialtech.com
http://people.collaborativefusion.com/~wmoran/

In response to

Browse pgsql-general by date

  From Date Subject
Next Message mgould 2012-03-07 18:51:33 Converting stored procedures from SQL Anywhere to PostGres.
Previous Message Carlos Mennens 2012-03-07 18:33:51 Automated Backup Script Help (Linux)