Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nDcxB-0004mg-47 for pgsql-general@arkaria.postgresql.org; Sat, 29 Jan 2022 01:55:01 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1nDcx9-0000M0-EO for pgsql-general@arkaria.postgresql.org; Sat, 29 Jan 2022 01:54:59 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nDcx9-0000Lr-2g for pgsql-general@lists.postgresql.org; Sat, 29 Jan 2022 01:54:59 +0000 Received: from mail-pf1-x432.google.com ([2607:f8b0:4864:20::432]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1nDcx6-0003mR-Nm for pgsql-general@lists.postgresql.org; Sat, 29 Jan 2022 01:54:58 +0000 Received: by mail-pf1-x432.google.com with SMTP id i30so7765287pfk.8 for ; Fri, 28 Jan 2022 17:54:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=entrata.com; s=google; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=ppeq85JGnwZYA0+mGFy8rgrESUN4CMzaOXHusc0RxGE=; b=hyArljQMYYbfNMLy+xxq3wJmyYwvHZDwiy8/TtcI/CcfBTYGkPS8jZPZzNY2tP3axr yzuKNOY6GhKW4ps5arI08xsAj//QJjagpVvc5ytZChyfGH98AuQPqyVnFZ/pkueq1nQo T8Q6EJMQ6S0NWcgwx/01+qnfTemF6d3e8TN6RhOxg7PC178xplcpHs/ta3QFHWiVTepZ AikZy8FTdu8D0/TJog3dI9t3ync5H576x5ufGuqSTFgWiqPUUGlaSIuiLJJGIMZdO3CY FD7hThXmbS37yoOo5GTx0Nik2HWhzn439X/sMw5uP7SoG2I+ezZDoxCyVCIfY5B/sDgn z2zQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=ppeq85JGnwZYA0+mGFy8rgrESUN4CMzaOXHusc0RxGE=; b=coSjhVKyAwOvvuvz9pH65VCVgHkg0EM6R20HvowyvjsFHfOg17FDSQowFM4DSx55Fi R2oY3zPhb3r117ZqxmL3+SW41JIg0QxBKvF4PKPIofMUaGd9QKdo1HULouXddabNRc1i YBOJ4YgsJBWI6krZzlbphOHRR0fCHy68eUGoph9KPFLoh+B5eOI9eBMNO2OJfJUnsehN gvFaPAepm5xu7EOgIp/Vft4CP+DCybgBdUkl4FwUYAZyYqKWy38cyXIwt9ud/62LlL2l 51tQ7UPqYv8LOKD5UiJ0Bw1Jmx6rd/bI41D0jlX2VVPafjEZUf2IMt6Aup2GXzFlh1mt MbYQ== X-Gm-Message-State: AOAM532X0g9mLY06Rn+MqCyu8ALhZzfH0Tv3JB2KsrQLRlcXtt+XHEUv Vgc8sSgiF/NxFyA+N1KHGbf13LapSnTeURUCpsbDOA== X-Google-Smtp-Source: ABdhPJyTUgKbhnaAl8aYwAE+o1CoOfqxxDZQrWXPh5I8Bnji7nnhPJPksYhKR1lFHDUk2k1cCGUM5Kt+OVcDzmG23qc= X-Received: by 2002:a65:48c5:: with SMTP id o5mr8677937pgs.284.1643421295593; Fri, 28 Jan 2022 17:54:55 -0800 (PST) MIME-Version: 1.0 References: <54e6a2bc-3546-88c7-0382-c74f23e4b668@gmail.com> <2858894.1643414894@sss.pgh.pa.us> <1b2e1d4e-298e-86ef-5875-0c89719d48f1@gmail.com> In-Reply-To: <1b2e1d4e-298e-86ef-5875-0c89719d48f1@gmail.com> From: Michael Lewis Date: Fri, 28 Jan 2022 18:54:29 -0700 Message-ID: Subject: Re: pg_try_advisory_lock is waiting? To: Mladen Gogala Cc: Tom Lane , pgsql-general@lists.postgresql.org Content-Type: multipart/alternative; boundary="0000000000008a653505d6aed6a8" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --0000000000008a653505d6aed6a8 Content-Type: text/plain; charset="UTF-8" On Fri, Jan 28, 2022 at 5:34 PM Mladen Gogala wrote: > pg_try_advisory_lock returned TRUE even without "FOR UPDATE" clause in the > subquery. Shouldn't it return false because it can't lock the row until the > uncommitted update finishes? > > > > The rows being updated or not is unrelated to advisory locks. Your example shows you locking on key 0 and then on key 1. That should be allowed. If you tried key 0 twice, then that is when you would get back "false" from the function call. You could establish a pattern of using the table OID as the first lock key and the primary key value (if a single column) as the second advisory lock key with the two parameter version of the function. But it is totally up to your code to honor that advisory lock, or not. Again, why use advisory locks and not select for update? Perhaps just because you don't want to deal with the failed transaction? What should happen when some other process cannot get a lock on that row? Do you want to wait and retry? Have you looked into the "skip locked" option? If you use "returning id" with that, you'd be able to check if you got the lock or not. --0000000000008a653505d6aed6a8 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
On Fri, Jan 28, 2022 at 5:34 PM Mladen Go= gala <gogala.mladen@gmail.com= > wrote:
=20 =20 =20
pg_try_advisory_lock returned TRUE even without "FOR UPDATE" clause in the subquery. Shouldn&#= 39;t it return false because it can't lock the row until the uncommitted update finishes?
The row= s being updated or not is unrelated to advisory locks. Your example shows y= ou locking on key 0 and then on key 1. That should be allowed. If you tried= key 0 twice, then that is when you would get back "false" from t= he function call.

You could establish a pattern of= using the table OID as the first lock key and the primary key value (if a = single column) as the second advisory lock key with the two parameter versi= on of the function. But it is totally up to your code to honor that advisor= y lock, or not.

Again, why use advisory locks and not select for upd= ate? Perhaps just because you don't want to deal with the failed transa= ction? What should happen when some other process cannot get a lock on that= row? Do you want to wait and retry? Have you looked into=C2=A0the "sk= ip locked" option? If you use "returning id" with that, you&= #39;d be able to check if you got the lock or not.
--0000000000008a653505d6aed6a8--