Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eYxU7-0004Oo-0f for pgsql-docs@arkaria.postgresql.org; Tue, 09 Jan 2018 17:14:47 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eYxU6-0006Sf-Hk for pgsql-docs@arkaria.postgresql.org; Tue, 09 Jan 2018 17:14:46 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1eYxU6-0006SU-93 for pgsql-docs@lists.postgresql.org; Tue, 09 Jan 2018 17:14:46 +0000 Received: from mail.wordtothewise.com ([184.105.179.154]) by makus.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1eYxU2-0001nA-JO for pgsql-docs@postgresql.org; Tue, 09 Jan 2018 17:14:44 +0000 Received: from satsuke.wordtothewise.com (204.11.227.194.static.etheric.net [204.11.227.194]) by mail.wordtothewise.com (Postfix) with ESMTPSA id 20C9F23379; Tue, 9 Jan 2018 09:15:25 -0800 (PST) Content-Type: text/plain; charset=us-ascii Mime-Version: 1.0 (Mac OS X Mail 10.3 \(3273\)) Subject: Re: Advice on Contiguous IDs From: Steve Atkins In-Reply-To: <5a546fcab6ae2e0000bdb8e4@polymail.io> Date: Tue, 9 Jan 2018 09:14:38 -0800 Cc: Brian McKiernan Content-Transfer-Encoding: quoted-printable Message-Id: References: <5a546fcab6ae2e0000bdb8e4@polymail.io> To: pgsql-docs@postgresql.org X-Mailer: Apple Mail (2.3273) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk > On Jan 9, 2018, at 1:06 AM, Brian McKiernan = wrote: >=20 >=20 > Hi Folks, >=20 > Looking for some help/advice - not sure if this is the appropriate = channel. pgsql-general would be a better bet. >=20 > My Issue: > My primary keys in a certain table are not contiguous. That itself isn't a problem at all. If there's a business requirement = for them to be contiguous that's the issue to consider first. >=20 > What I have done so far: > I have checked the documentation and found: = https://wiki.postgresql.org/wiki/FAQ#Why_are_there_gaps_in_the_numbering_o= f_my_sequence.2FSERIAL_column.3F_Why_aren.27t_my_sequence_nu > mbers_reused_on_transaction_abort.3F >=20 > My Question: > 1) What event would cause the CACHE clause in CREATE SEQUENCE to make = an out of sequence next number? It causes PostgreSQL to assign batches of numbers to each connection = that needs one, making it more likely that they'll be used out of order = or that some won't be used at all. Using cache just makes it more obvious, though. There's no guarantee = that a sequence will give you consecutive numbers, nor that they'll be = ordered, in general. About the only thing that is guaranteed is that = they'll be unique. > 2) In all cases am I correct in my thinking that in order to create = contiguous primary key IDs then performance will greatly suffer? Do we = have an idea of how bad this will generally be or what does that depend = upon? Yes. You will have to effectively serialize all inserts into those = tables, eliminating any concurrency. You'd need to have a pretty compelling hard business requirement for = consecutive numbers before it'd be worth considering. Cheers, Steve