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 1jtCoe-0007Sc-Be for pgsql-general@arkaria.postgresql.org; Wed, 08 Jul 2020 16:21:00 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1jtCod-0005SB-95 for pgsql-general@arkaria.postgresql.org; Wed, 08 Jul 2020 16:20: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 1jtCoc-0005S3-94 for pgsql-general@lists.postgresql.org; Wed, 08 Jul 2020 16:20:59 +0000 Received: from out3-smtp.messagingengine.com ([66.111.4.27]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jtCoZ-0000Nc-7p for pgsql-general@lists.postgresql.org; Wed, 08 Jul 2020 16:20:57 +0000 Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailout.nyi.internal (Postfix) with ESMTP id 2F5205C0190; Wed, 8 Jul 2020 12:20:54 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute4.internal (MEProxy); Wed, 08 Jul 2020 12:20:54 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= subject:to:cc:references:from:message-id:date:mime-version :in-reply-to:content-type:content-transfer-encoding; s=fm3; bh=f DTJTVknW4M42q0++v9yant6uaW039F1j8gpsQ19IIE=; b=TeqiPo9JIJVPel2Ar 6D92s2XHaS71qxR537DUyrrp1dBo6xLb99RjnUEl3lYy8j0MIVD3HnRAAHegSyQi ZF+3hjSW1tX1pqNwu6TTzjwXR/iF7E3xnrXMSJC8PxLUWWoqsV6q6WTHANn6GX/J X4GAcJEEW2Pc/6P/cFmL5+40E3i+uX/pb4/dp18MlywRwhWVyvEo6+fyY4vOjVKQ wynpsZuj6nsV4LoXSgVA/E/8Ft6f9Htssv6G3/MjiLqpdeAd21R/WBXpmx4TWLbJ cfsbKyRUg22Cv62jzpAdgh1zPyXlEBOxqUYebMDC8Ue9vyefumObgR622vD1EI5K HTK6A== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc: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=fDTJTVknW4M42q0++v9yant6uaW039F1j8gpsQ19I IE=; b=uAxXXnRCt84YbqxhJ/RwWj3bmTBXnogx3KfvcfXpRIeTsIjxQ0pqoLgJK Pn+1XCr2DsDQSjPEshxodCSQhGeQwdN2XWqTvUo94bjCsPFdNOXCGEXO53xwcDTI 5Allbbr7wndr8JO+mKpex2OovsRR9H7sJ6w8ZRneIocvmMHo7QJOzx8UT8pIAByc pGgsjHtMY2H1ZTiH93pRz2NdiJy0KqZS8j9g9ShtIItsXLGmXwtBRLjmpe5G0Nuu uJ7zDmAKzixnFKRPpNYImBflMUtwI7DSXnZuI5S+ne/ZnLWFBxeiKip9vOzQw1fw QmF1Wn2ns2McfTg2st7PBqjJtxcVQ== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduiedrudejgddutdduucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepuffvfhfhkffffgggjggtgfesthekredttdefjeenucfhrhhomheptegurhhi rghnucfmlhgrvhgvrhcuoegrughrihgrnhdrkhhlrghvvghrsegrkhhlrghvvghrrdgtoh hmqeenucggtffrrghtthgvrhhnpeduudefjeetveffhefgudehtdfhvdetkeeiueehfeev veduteekleeuheegudehhfenucffohhmrghinhepphhoshhtghhrvghsqhhlrdhorhhgne cukfhppeejgedrleefrddutdehrddukeeinecuvehluhhsthgvrhfuihiivgeptdenucfr rghrrghmpehmrghilhhfrhhomheprggurhhirghnrdhklhgrvhgvrhesrghklhgrvhgvrh drtghomh X-ME-Proxy: Received: from ranger.suse (74-93-105-186-washington.hfc.comcastbusiness.net [74.93.105.186]) by mail.messagingengine.com (Postfix) with ESMTPA id 58301306005F; Wed, 8 Jul 2020 12:20:53 -0400 (EDT) Subject: Re: BigSerial and txid issuance To: "Yorwerth, Adam" , "pgsql-general@lists.postgresql.org" Cc: "Cianciaruso, Lorenzo" References: <4478EF79-0699-4FBA-B7C1-95561EF1DB5E@tesco.com> From: Adrian Klaver Message-ID: Date: Wed, 8 Jul 2020 09:20:52 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.9.0 MIME-Version: 1.0 In-Reply-To: <4478EF79-0699-4FBA-B7C1-95561EF1DB5E@tesco.com> 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/8/20 7:09 AM, Yorwerth, Adam wrote: > Hi Everyone, > > We’re trying to solve a problem that relies on BigSerial and txid (as > returned by txid_current() ) values being issued consistently. > > Is it possible for two transactions to interleave their issuance of > these two variables? > > For example: > > Schema: > > CREATE TABLE EXAMPLE( >   offset bigint PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY NOT NULL, >   txid BIGINT NOT NULL DEFAULT 1 > ); > > Insert statement: > > "INSERT INTO EVENTS (txid) VALUES (txid_current());"; > > Prior to transactions executing offset is 10 and txid is 1000. > > Transaction 1 and 2 occur concurrently. > > Is it possible for transaction 1 to be issued txid 1001 and offset 12 > and transaction 2 to be issued txid 1002 and offset 11? Well IDENTITY is backed by a SEQUENCE and: https://www.postgresql.org/docs/12/sql-createsequence.html "Unexpected results might be obtained if a cache setting greater than one is used for a sequence object that will be used concurrently by multiple sessions. Each session will allocate and cache successive sequence values during one access to the sequence object and increase the sequence object's last_value accordingly. Then, the next cache-1 uses of nextval within that session simply return the preallocated values without touching the sequence object. So, any numbers allocated but not used within a session will be lost when that session ends, resulting in “holes” in the sequence. Furthermore, although multiple sessions are guaranteed to allocate distinct sequence values, the values might be generated out of sequence when all the sessions are considered. For example, with a cache setting of 10, session A might reserve values 1..10 and return nextval=1, then session B might reserve values 11..20 and return nextval=11 before session A has generated nextval=2. Thus, with a cache setting of one it is safe to assume that nextval values are generated sequentially; with a cache setting greater than one you should only assume that the nextval values are all distinct, not that they are generated purely sequentially. Also, last_value will reflect the latest value reserved by any session, whether or not it has yet been returned by nextval." > > Any help would be much appreciated. > > Regards, > > Adam > > This is a confidential email. Tesco may monitor and record all emails. > The views expressed in this email are those of the sender and not Tesco. > Tesco Stores Limited Company Number: 519500 Registered in England > Registered Office: Tesco House, Shire Park, Kestrel Way, Welwyn Garden > City, AL7 1GA VAT Registration Number: GB 220 4302 31 -- Adrian Klaver adrian.klaver@aklaver.com