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 1jtgtB-0005rn-8U for pgsql-general@arkaria.postgresql.org; Fri, 10 Jul 2020 00:27:41 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1jtgtA-0006Gv-73 for pgsql-general@arkaria.postgresql.org; Fri, 10 Jul 2020 00:27:40 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jtgt9-0006Go-S4 for pgsql-general@lists.postgresql.org; Fri, 10 Jul 2020 00:27:39 +0000 Received: from mail-pl1-x635.google.com ([2607:f8b0:4864:20::635]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1jtgt7-00029j-97 for pgsql-general@postgresql.org; Fri, 10 Jul 2020 00:27:39 +0000 Received: by mail-pl1-x635.google.com with SMTP id o1so1523721plk.1 for ; Thu, 09 Jul 2020 17:27:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ardentperf-com.20150623.gappssmtp.com; s=20150623; h=content-transfer-encoding:mime-version:subject:from:in-reply-to:cc :date:message-id:references:to; bh=fdc0h86vNjzwCOd4W8pFBd1oxsHjeMzOaw5zzmJ9OhU=; b=SbCsJ6MA9f8Lr8p5N8CsxcpCTqha0fxlgFrPBuWYSzhscKpoTfvsvgl7YV719iyjng JwINOLZkHWW9PV94qgDELx/T0E2TJHSV91/zz1yr1Thhh6EgvI5a3sO5rUUkNcW6RBg1 yyBoN4TfGLVDTBujvo6l6PSzCggZweXniJkWqHhFci3aTh9REQEdWUbml7SV6xf9i/7b SpHoOV7IM/B+j7qMieQy8Xmay9GXh84BV67Mu4Nd0CXwQxO81RLUvVILOEq0xg1Il7D7 PbQL61vvBK6UVeLQwcdv7h9vztLMpXIEcCbUxJttMMJ2gacwHzD5/j6RFsvAHLbrMuyX bcJQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:content-transfer-encoding:mime-version:subject :from:in-reply-to:cc:date:message-id:references:to; bh=fdc0h86vNjzwCOd4W8pFBd1oxsHjeMzOaw5zzmJ9OhU=; b=aL7sh7ayYDTruLmUA1oh9yzEmGPVt1MT69u2nir8oRzu4qY3z7MKmShaf1rIIgaVck oPOblLEnEIPAFgZA3QlQhe9Z8bHeNOGxTn19ffANzKLz5MlvjNXHt0wCU9nzn4qtSxo2 UBg5vgM7pVxR1m/ogCDIxg+nlhYAUn6u5japH7eUl+Q6DWLcLVfbqQ5uuW4yr4CItxG3 I+FXLkl3M9cfSVTz7rJWqpnTD0wDqltcYVUgyYJxlPOvMr37KdjBOeXek4G67NpvrA6N A90RJX39kPu/cbbQGW6Ea45CCTZqGLC0506+0OAyxA/eNofu4GqC/gdYppjSFd7MaQZR w7og== X-Gm-Message-State: AOAM530noQCpP9++mNv2B7HNfsdFHo29CKqhzhdGGi0ZT2SZIe3Oe0Zn FR806rZ1zq43GBSMKT0uBe2dBg== X-Google-Smtp-Source: ABdhPJxrsmjHIH6gZjEoQVnUXnue9hs3CF7bTbHghCAR1zmRZVpLD3lkedqYLaaDnxDnVt98lVcDow== X-Received: by 2002:a17:90b:4d0f:: with SMTP id mw15mr2821564pjb.68.1594340855119; Thu, 09 Jul 2020 17:27:35 -0700 (PDT) Received: from [192.168.1.5] (71-212-54-53.tukw.qwest.net. [71.212.54.53]) by smtp.gmail.com with ESMTPSA id f6sm4199590pfe.174.2020.07.09.17.27.34 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 09 Jul 2020 17:27:34 -0700 (PDT) Content-Type: multipart/alternative; boundary=Apple-Mail-B7F10AC3-38B4-4A47-8B12-477F62E6C4D1 Content-Transfer-Encoding: 7bit Mime-Version: 1.0 (1.0) Subject: Re: Efficiently advancing a sequence without risking it going backwards. From: Jeremy Schneider In-Reply-To: Cc: Paul McGarry , PostgreSQL Mailing Lists Date: Thu, 9 Jul 2020 17:27:33 -0700 Message-Id: References: To: Christopher Browne X-Mailer: iPhone Mail (17F80) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --Apple-Mail-B7F10AC3-38B4-4A47-8B12-477F62E6C4D1 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable >> On Jul 9, 2020, at 14:08, Christopher Browne wrote: > =EF=BB=BF >> On Thu, 9 Jul 2020 at 12:59, Jeremy Schneider w= rote: >=20 >>=20 >> > On Jul 6, 2020, at 19:06, Paul McGarry wrote: >> >=20 >> > I don't think I can use setval(), because it risks making sequences go b= ackwards, eg: >> >=20 >> > 1) Check values >> > DB1sequence: 1234 >> > DB2sequence: 1233 (1 behind) >> > 2) setval('DB2sequence',1234); >> >=20 >> > but if between (1) and (2) there are 2 nextval(DB2sequence) calls on an= other process, (2) would take the sequence back from 1235 to 1234 and I wou= ld end up trying to create a duplicate key ID from the sequence. >>=20 >> An ability to =E2=80=9Clock=E2=80=9D the sequence momentarily would give y= ou the tool you need, but I don=E2=80=99t think it=E2=80=99s there. >=20 > The use-case where you need a lock on the value so that there can't possib= ly be a hole in the sequence OP asked for a way to call setval() with a guarantee the sequence will never= go backwards IIUC. His code can check that the new value he wants to set is= higher than the current value, but there=E2=80=99s a race condition where a= second connection could quickly advance the sequence between the check and t= he setval() call and then cause duplicates from the next call which is bad. The ideal solution is a setval_forward_only() or setval_no_duplicates() func= tion that does it atomically or something. If it were possible to =E2=80=9Cl= ock=E2=80=9D the entire sequence to prevent any other sessions from using it= at all, that would work too. Not locking a value, locking the whole thing. V= ery bad hack solution is renaming the sequence then renaming it back as a bl= unt form of locking... and to be clear I don=E2=80=99t think is a good idea I= just was saying that technically it might work. :) -Jeremy Sent from my TI-83 --Apple-Mail-B7F10AC3-38B4-4A47-8B12-477F62E6C4D1 Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: quoted-printable


On Jul 9, 2020, at 14:08, Chris= topher Browne <cbbrowne@gmail.com> wrote:

=EF=BB=BF
On Thu, 9 Jul 2020 at 12:59, Jeremy Schneider <schneider@ardentperf.com> wrote:
> On Jul 6, 2020, at 19:06, Paul McGarry <paul@paulmcgarry.com> wrote:
>
> I don't think I can use setval(), because it risks making sequences go b= ackwards, eg:
>
> 1) Check values
> DB1sequence: 1234
> DB2sequence: 1233 (1 behind)
> 2) setval('DB2sequence',1234);
>
> but if between (1) and (2) there are 2 nextval(DB2sequence) calls on an= other process,  (2) would take the sequence back from 1235 to 1234 and I= would end up trying to create a duplicate key ID from the sequence.

An ability to =E2=80=9Clock=E2=80=9D the sequence momentarily would give you= the tool you need, but I don=E2=80=99t think it=E2=80=99s there.

The use-case where you need a lock on the value so= that there can't possibly be a hole in the sequence
=

OP asked for a way to call setval() with a g= uarantee the sequence will never go backwards IIUC. His code can check that t= he new value he wants to set is higher than the current value, but there=E2=80= =99s a race condition where a second connection could quickly advance the se= quence between the check and the setval() call and then cause duplicates fro= m the next call which is bad.

The ideal solution is= a setval_forward_only() or setval_no_duplicates() function that does it ato= mically or something. If it were possible to =E2=80=9Clock=E2=80=9D the enti= re sequence to prevent any other sessions from using it at all, that would w= ork too. Not locking a value, locking the whole thing. Very bad hack solutio= n is renaming the sequence then renaming it back as a blunt form of locking.= .. and to be clear I don=E2=80=99t think is a good idea I just was saying th= at technically it might work.  :)

-Jeremy

Sent from my TI-83

= --Apple-Mail-B7F10AC3-38B4-4A47-8B12-477F62E6C4D1--