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 1nXG4o-0007Gg-Su for pgsql-novice@arkaria.postgresql.org; Thu, 24 Mar 2022 05:32:02 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1nXG4n-00074e-Qn for pgsql-novice@arkaria.postgresql.org; Thu, 24 Mar 2022 05:32:01 +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 1nXG4n-00074V-H3 for pgsql-novice@lists.postgresql.org; Thu, 24 Mar 2022 05:32:01 +0000 Received: from mail-yw1-x1131.google.com ([2607:f8b0:4864:20::1131]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1nXG4l-0001OT-8Z for pgsql-novice@lists.postgresql.org; Thu, 24 Mar 2022 05:32:00 +0000 Received: by mail-yw1-x1131.google.com with SMTP id 00721157ae682-2db2add4516so39996007b3.1 for ; Wed, 23 Mar 2022 22:31:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=3vrCD9ZyMp9Mwl+BEDjeVNvXvHMeID7jnH9+h1M168Q=; b=CSSRfLHdEcY/4SHHZBif6K/Hh0znlUr4uBm/Hkz68wUkNmC3RRprm2O8rvSqi1mm+X CsjCFrAFGK+YOx4aBn/VXdlw3osZN79aVA4dq78Pyg6OkO/f8KkLLEZhusVSDoWU7XiT mc6/eVamWint8TTDorkj5ViWOn056irs1r2+doozZbrha50x1I5kUeP1JgGqLz81fs1F oAnvdYxjBZ+W0rjPrPY0+0MZEo4Whv0gLAhcL98ZcaRrvNEeSDbKmLCtcgujY/KAtsZC fSUYa8dCVDPqEXJ7C5yEb++8BVwO4OdRO1jpj6yxUZxF81+f6nLfaGPgRMpzSYasvqN5 19xg== 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=3vrCD9ZyMp9Mwl+BEDjeVNvXvHMeID7jnH9+h1M168Q=; b=xgturWJiUpLaXDvAXdGOQkYg+Xtue0nmQcC64S6gUYxcVLsl4rcBu4Y2Ax7bDoUO9H UJJljKN4ldAOIEj+7O0seVskHgaz4e3GgrSSQ682sQMo7qFk98VFWY6yCflP+BScxhxC iksX481Ryzh9BsoJoI/L+Wu0c97lS3oqS/YAK3JQzIa/00JcB8K1ebUfY4vgItxK8ix6 Pw4jdkFSYOnfVAqkYqRcdVkKq8ZuQU6tvpmkVizirxKuxIg2RkgyQHKuD5IN/wZtam92 g+h02S2Jp3/NIf+N5z0FwjCvvwAWYFWqoRO7RiRh+s2BzNo1AKoYBO3IBDVJZm0QbePr WnCQ== X-Gm-Message-State: AOAM532IUkrEaCujToyCMlH/Kh3rW37gn+xBD0kydj3UDDo1wzPIvE6H M7nY+//BnxBt/H0WVoknMXDqXHUIr8rMvsnQ7Qo= X-Google-Smtp-Source: ABdhPJxIJvSA4/tMGD7kk381oq4uFFKbLf09nBCnQd7z5dumy283r4Or9Mlxurq+J83VFZqrFvE7czdpguti4eGNcjc= X-Received: by 2002:a0d:f603:0:b0:2d1:57e5:234 with SMTP id g3-20020a0df603000000b002d157e50234mr3316117ywf.469.1648099918543; Wed, 23 Mar 2022 22:31:58 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: Joe Date: Wed, 23 Mar 2022 22:31:47 -0700 Message-ID: Subject: Re: SELECT FOR UPDATE on rows that don't exist To: "David G. Johnston" Cc: pgsql-novice Content-Type: multipart/alternative; boundary="00000000000032fdb105daf02aec" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --00000000000032fdb105daf02aec Content-Type: text/plain; charset="UTF-8" David, Thank you very much. I missed the advisory locks feature. That feels like the perfect thing; the BIGINT keys are a bit clunky but that's minor. Your description of the sequence of events for the two SELECT FOR UPDATE transactions makes sense. To apply it to my pseudo code, the second transaction would unblock when the row was deleted in the first rather than when the first transaction commits or rolls back. One question though: You've informed the system you are going to be updating rows on the table > but as yet have not given it specific rows to protect. > >> >> Without #*1, a simple experiment shows that two processes can be in the >> critical section at the same time. Add #*1 seems to achieve the desired >> behavior, but is it really? >> > > The index will not allow duplicates to be inserted and the first one to > try forces all other potential insertions to wait until the first one > commits; > Is this true in all situations? That is, will an index insertion in one transaction block the index insertion in another transaction? Presumably only only if the index entries match, but how does this work in practice? Does the second transaction block when it sees the conflict? Or does it proceed on its own version of the index proceeding until it tries to commit? Thanks again, Joe --00000000000032fdb105daf02aec Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
David,
Thank you very much. I missed the ad= visory locks feature. That feels like the perfect thing; the BIGINT keys ar= e a bit clunky but that's minor.

Your descript= ion of the sequence of events for the two SELECT FOR UPDATE transactions ma= kes sense. To apply it to my pseudo code, the second transaction would unbl= ock when the row was deleted in the first rather than when the first transa= ction commits or rolls back.

One question tho= ugh:

You've informed the s= ystem you are going to be updating rows on the table but as yet have not gi= ven it specific rows to protect.

Without #*1, a simple experim= ent shows that two processes can be in the critical section at the same tim= e. Add #*1 seems to achieve the desired behavior, but is it really?

The index will not allow duplicates to be inserted and the first= one to try forces all other potential insertions to wait until the first o= ne commits;

Is this true = in all situations? That is, will an index insertion in one transaction bloc= k the index insertion in another transaction? Presumably only only if the i= ndex entries match, but how does this work in practice? Does the second tra= nsaction block when it sees the conflict? Or does it proceed on its own ver= sion of the index proceeding until it tries to commit?

Thanks again,
Joe
--00000000000032fdb105daf02aec--