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 1jtfe6-0003LK-LE for pgsql-general@arkaria.postgresql.org; Thu, 09 Jul 2020 23:08: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 1jtfe4-0004iF-6Q for pgsql-general@arkaria.postgresql.org; Thu, 09 Jul 2020 23:08:00 +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 1jtfe3-0004i6-SI for pgsql-general@lists.postgresql.org; Thu, 09 Jul 2020 23:07:59 +0000 Received: from mail-pl1-x643.google.com ([2607:f8b0:4864:20::643]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1jtfe1-0000Bj-II for pgsql-general@lists.postgresql.org; Thu, 09 Jul 2020 23:07:58 +0000 Received: by mail-pl1-x643.google.com with SMTP id d10so1445233pll.3 for ; Thu, 09 Jul 2020 16:07:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=references:user-agent:from:to:subject:in-reply-to:message-id:date :mime-version:content-transfer-encoding; bh=MpNQaC5Gj840kvFPgbKljtCrrFmOzjfQ61EMmkS/ZQs=; b=t5oAvjsRR1av1BwVrqFQhoL8E6dQghvxC0ugRD06EFydTZFTrC45qFwvKyK49mdaqm 6VUb/z0O33HudG/9rhHrB5t6ZAd5NmM9tvhJpLBbFo18s5w2BciatJPl7aSufdv3ECxP h299z3O6Q36yd0S5/FIKZ3ntSeWlAxIms+QhPlYaI+iAdLQm5SmjJUGJoS2ljpMYOR0O ASzKtiNQwL0f+i2H714GvxSF7uulVmVJf1eOcCdER5TwOugUS5VfE9+Sx8Og4cN5R09h J2kPBD9V3tExZ0eDpSN0Zwyy9EIci3vp741qxPJtSauiVQbeADUhv9vS/F4Xk2o3Chj7 tMzQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:references:user-agent:from:to:subject :in-reply-to:message-id:date:mime-version:content-transfer-encoding; bh=MpNQaC5Gj840kvFPgbKljtCrrFmOzjfQ61EMmkS/ZQs=; b=qltVCJB6V6imkC98j8g70pSyTzu/QVJYGXMrmZJhYNzka+dmWbRRe4t0IeYLo3Jb6Z +X3PCqVKr7MG3NbMWUHGLpsJ634Fv7ER8syYfVJ5VwYJ/++8x17VmgYNJW21uded13yT HLV5zLzuRwMMZL3OBvcJdoC1gZiqTQfalPO8nDKl6M/A5tv/rDUySBkmTRKAgmi3UNxZ 6ln6rs5itYCyDLon5PQsDyEetP5ZQIsYxGC5alhhHxqpg++g5xcLu86NFTlPxDn2m6wL wDijYp+oQ5PDl5/zqtmf8pcgYGvUVkJlk9KhMlj40QscJ21MuKvEvdAPdqOQAezvOEHj 67jg== X-Gm-Message-State: AOAM532TjiYV9abUEVwePzAPogZXn3Vx2wt1mGoyK7vdwJ4wSAM8uxVB mxxwMHHwu1j4augzxiJdHlPaRN49 X-Google-Smtp-Source: ABdhPJxcJ5WOetNeqSK9gI+3bZtz0LwAmRLDhVFVmDXPa8eHWcde+Zc240Px3inyeIqJ95wHWB6QjA== X-Received: by 2002:a17:90a:b38b:: with SMTP id e11mr2591395pjr.120.1594336075413; Thu, 09 Jul 2020 16:07:55 -0700 (PDT) Received: from tim-desktop (203-173-25-239.dyn.iinet.net.au. [203.173.25.239]) by smtp.gmail.com with ESMTPSA id m68sm3722776pje.24.2020.07.09.16.07.53 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 09 Jul 2020 16:07:54 -0700 (PDT) References: User-agent: mu4e 1.5.4; emacs 27.0.91 From: Tim Cross To: pgsql-general@lists.postgresql.org Subject: Re: Efficiently advancing a sequence without risking it going backwards. In-reply-to: Message-ID: <87d0542soo.fsf@gmail.com> Date: Fri, 10 Jul 2020 09:07:51 +1000 MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Christopher Browne writes: > On Thu, 9 Jul 2020 at 12:59, Jeremy Schneider > wrote: > >> >> > On Jul 6, 2020, at 19:06, Paul McGarry wrote: >> > >> > I don't think I can use setval(), because it risks making sequences go >> backwards, 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 >> another 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. >> >> Total hack, but if your application or users can retry when the rare err= or >> is encountered then one idea is to rename the sequence momentarily while >> you do the setval() then rename it back. Do an initial check without >> renaming, then re-check after renaming and before the setval() call. >> >> If you put retry logic into your application then make sure to include >> back-off logic so you don=E2=80=99t get an outage induced by thundering = herd. >> > > This is increasingly looking like a set of attempts to intentionally abuse > what sequences were designed for. > > The use-case where you need a lock on the value so that there can't > possibly be a hole in the sequence points at the notion of having some > other kind of a function that takes out a lock on a table, and serially > gives out "MAX+1" as the next value. > > That isn't a very difficult function to write; the problem with it is that > that sort of function will forcibly serialize all inserts through the > function+table lock that is giving out "MAX+1" values. That's going to be > WAY slower than using a sequence object, and about 98% of the time, people > will prefer the sequence object, particularly because it's about 98% fast= er. > > I'm not quite sure if anyone has put out there a standard-ish idiom for > this; that seems like a not TOO difficult "exercise for the user." > > There will definitely be more failure cases, and *wildly* more fighting, = in > a concurrent environment, over tuple locks. > > - An obvious failure is that if one connection asks for the new MAX+1, ge= ts > it, and then the transaction fails, for some later, out-of-relevant-scope, > reason, you'll still potentially get some "holes" in the series of values. > > - If there are 10 connections trying to get MAX+1 concurrently, only one > can get it at a time, and that connection can't relinquish the lock until > its transaction has completed, and the 9 must wait, regardless of how much > work the "winner" still has to do. > > These are amongst the reasons why people conclude they *don't* want that > kind of functionality. > > It makes me think that the problem needs to be taken back to that initial > point of "I think I need some somewhat coordinated sequences", and poke at > what the *real* requirement is there, and why someone thinks that the > values should be "somewhat coordinated." Something seems off there. I agree and was going to write something similar. All the 'solutions' are problematic in one way or the other and seem to be due to a misconception about the role for sequences or some requirement which needs to be re-examined. --=20 Tim Cross