Re: BUG #13920: pg_try_advisory_xact_lock bigint trouble

From: "Valeriy A(dot)" <mtakvel(at)gmail(dot)com>
To: Thomas Munro <thomas(dot)munro(at)enterprisedb(dot)com>
Cc: "David G(dot) Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com>, "pgsql-general(at)postgresql(dot)org" <pgsql-bugs(at)postgresql(dot)org>
Subject: Re: BUG #13920: pg_try_advisory_xact_lock bigint trouble
Date: 2016-03-09 10:46:36
Message-ID: CAHGCciMU+FPCu1v3QFwM5cmDujkaUqmMMSHU-yVZ9dECub+JUQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

Thanks Tomcas, that the answer for my problem. Greate thanks!

On Fri, Mar 4, 2016 at 10:15 PM, Thomas Munro <thomas(dot)munro(at)enterprisedb(dot)com
> wrote:

> On Tue, Feb 9, 2016 at 10:01 PM, Valeriy A. <mtakvel(at)gmail(dot)com> wrote:
> > Here my simple example
> >
> > --- SQL Begin
> > create table table1 (
> > id bigserial PRIMARY KEY
> > -- other fields);
> >
> > create table table2 (
> > id bigserial PRIMARY KEY
> > -- other fields);
> >
> >
> > CREATE FUNCTION do_action_on_table1(keyID bigint ) returns int2 LANGUAGE
> > plpgsql AS $$
> > DECLARE
> > isLocked boolean;
> > BEGIN
> > EXECUTE 'SELECT pg_try_advisory_xact_lock($1)' INTO isLocked USING
> keyID;
> >
> > if isLocked THEN
> > --SOME action on table1
> > RETURN 1;
> > END IF;
> >
> > RETURN 0;
> > END$$
> >
> > CREATE FUNCTION do_action_on_table2(keyID bigint ) returnS int2
> > LANGUAGE plpgsql
> > AS $$
> > DECLARE
> > isLocked boolean;
> > BEGIN
> > EXECUTE 'SELECT pg_try_advisory_xact_lock($1)' INTO isLocked USING
> keyID;
> >
> > if isLocked THEN
> > --SOME action on table2
> > RETURN 1;
> > END IF;
> >
> > RETURN 0;
> > END$$
> > -- SQL End
> >
> > In this case if sequences fields has same values then calls of functions
> > will be lock both tables and miss actions.
>
> Have you considered using row locks with SKIP LOCKED instead of
> advisory locks? Then you don't have to come up with your own scheme
> to map tables and keys to integer space. Something like this:
>
> CREATE OR REPLACE FUNCTION do_action_on_table1(keyID bigint)
> RETURNS int2 LANGUAGE plpgsql AS
> $$
> BEGIN
> PERFORM * FROM table1 WHERE id = $1 FOR UPDATE SKIP LOCKED;
> IF FOUND THEN
> -- some action on table1
> RETURN 1;
> END IF;
>
> RETURN 0;
> END
> $$;
>
> It seems likely you want to load data out of the row into variables
> for processing, so you could replace the PERFORM statement with a
> SELECT ... INTO ... FOR UPDATE SKIP LOCKED.
>
> --
> Thomas Munro
> http://www.enterprisedb.com
>

--
Thanks!
Valeriy

In response to

Browse pgsql-bugs by date

  From Date Subject
Next Message Amit Langote 2016-03-09 12:54:29 Re: gram.y comment issue
Previous Message 张文升 2016-03-09 06:04:00 gram.y comment issue