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 1jsdIN-0007rs-MS for pgsql-general@arkaria.postgresql.org; Tue, 07 Jul 2020 02:25:20 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1jsdIM-0000MR-0V for pgsql-general@arkaria.postgresql.org; Tue, 07 Jul 2020 02:25:18 +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 1jsdIK-0000MJ-AP for pgsql-general@lists.postgresql.org; Tue, 07 Jul 2020 02:25:17 +0000 Received: from out5-smtp.messagingengine.com ([66.111.4.29]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jsdIC-0004mA-WD for pgsql-general@postgresql.org; Tue, 07 Jul 2020 02:25:15 +0000 Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailout.nyi.internal (Postfix) with ESMTP id 7B0505C01BD; Mon, 6 Jul 2020 22:25:07 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute4.internal (MEProxy); Mon, 06 Jul 2020 22:25:07 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= subject:to:references:from:message-id:date:mime-version :in-reply-to:content-type:content-transfer-encoding; s=fm3; bh=K RA/hoo4U/lo+ciHi1c0RUBLLgFoufYftqA/VqmXC5k=; b=x0Yee3h7C0mGEIKkR AgTzzgZ981snj5Y88AiXrmIdwoWq5i7rsgT8WFhi4i/4A4mFV6wnswZdeD1WU1tH U+9WexwuITdu6gvIROiyA28kBdhMpw0K9Qg66JtScnEIGq+nfpxzsy3hwv5QBM7I 7vdQnK6Jb815yAlOpaReR7V+s2O2BfCYG+WwWjIZ5uRLn1n3foGnM4z4jHxCHswT nZ+Yq1ebdA+CYw/KVAZ7Ce/bYbM1CYTnOb66cK3R9EzsZ/iRNTZEYO6L6EBPnhJw NJeSn1+iayBsHMl242SIMR/rUtGqAcRM0WjTjmkAU0mQFfAcmG7o5tfaT9TOAPea qaojQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm3; bh=KRA/hoo4U/lo+ciHi1c0RUBLLgFoufYftqA/VqmXC 5k=; b=Brs9dC+XxvybAGSxzXvlLQLer14Dqf0Dk/X5PYq2GWcuM0EBHF0zx0il6 XEwJJSGEf5Rb7npH2KKRn/4qd3NRBKJBPPmQv7jH5r/GbKr/AzDCEBaOWtLS+8xm EwrWRRs6o+jS4otPG4gRb9LOV3xJ6LloQWLMz0tybK38l568Eqt1i1xpYylPP37I d9POAt4Z4xLuRL04akW/lLvCi/qsUAzJunEwLTROWkpWYcuKqZxRqnFayvNIEO52 T4sJSCtrTAwB+0SGyhl6F0AumHXNM4+Sdpzv9IVaD8NUivZbMQYUHcTh1U6LoP46 Z/yAkklrRV0mbBQo0RO3+zOug34Zg== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduiedrudeggdeiudcutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenuc fjughrpefuvfhfhffkffgfgggjtgfgsehtkeertddtfeejnecuhfhrohhmpeetughrihgr nhcumfhlrghvvghruceorggurhhirghnrdhklhgrvhgvrhesrghklhgrvhgvrhdrtghomh eqnecuggftrfgrthhtvghrnhepveevfeevfeeugeffkeekhfegieeufffgteeujedvgffh ueejjefhkeejleegtdegnecukfhppeejhedrudejvddrhedrvdefudenucevlhhushhtvg hrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpegrughrihgrnhdrkhhlrghv vghrsegrkhhlrghvvghrrdgtohhm X-ME-Proxy: Received: from [192.168.1.3] (75-172-5-231.tukw.qwest.net [75.172.5.231]) by mail.messagingengine.com (Postfix) with ESMTPA id C1A4330653F0; Mon, 6 Jul 2020 22:25:06 -0400 (EDT) Subject: Re: Efficiently advancing a sequence without risking it going backwards. To: Paul McGarry , pgsql-general@postgresql.org References: From: Adrian Klaver Message-ID: Date: Mon, 6 Jul 2020 19:25:06 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.10.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 7/6/20 7:06 PM, Paul McGarry wrote: > I have two sequences in different dbs which I want to keep roughly in > sync (they don't have to be exactly in sync, I am just keeping them in > the same ballpark). > > Currently I have a process which periodically checks the sequences and does: > > 1) Check values > DB1sequence: 1234 > DB2sequence: 1233 (1 behind) > 2) while (nextval('DB2sequence')<=1234); > > which works fine, but is pretty inefficient if the discrepancy is large > (ie calling nextval a hundred thousand times). > > 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. > > So what I really want is something equivalent to the setval, but with > "where DB2sequence <1234" logic so it doesn't overwrite the value if it > is already large. > > Is there such a mechanism? Well sequences are designed to be operated on independently from each session, so there is not much you can do about locking on a number. The best you can do is use setval() to increment the number by enough to get past any potential sequence advances in other sessions. Say advance by 10, 50 or 100 depending on what you think is a reasonable number of other sessions also hitting the sequence. > > Thanks for any help. > > Paul -- Adrian Klaver adrian.klaver@aklaver.com