Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eP1vL-0000zW-Qp for pgsql-general@arkaria.postgresql.org; Wed, 13 Dec 2017 07:57:51 +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 1eP1vL-0003SZ-Bq for pgsql-general@arkaria.postgresql.org; Wed, 13 Dec 2017 07:57:51 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1eP1vL-0003SP-4N for pgsql-general@lists.postgresql.org; Wed, 13 Dec 2017 07:57:51 +0000 Received: from mx2.mailbox.org ([80.241.60.215]) by magus.postgresql.org with esmtps (TLS1.2:RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1eP1vD-0000ZL-Lm for pgsql-general@postgresql.org; Wed, 13 Dec 2017 07:57:50 +0000 Received: from smtp1.mailbox.org (smtp1.mailbox.org [80.241.60.240]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx2.mailbox.org (Postfix) with ESMTPS id EE5CB4D1C6; Wed, 13 Dec 2017 08:57:39 +0100 (CET) X-Virus-Scanned: amavisd-new at heinlein-support.de Received: from smtp1.mailbox.org ([80.241.60.240]) by spamfilter01.heinlein-hosting.de (spamfilter01.heinlein-hosting.de [80.241.56.115]) (amavisd-new, port 10030) with ESMTP id Q19uHULWsipL; Wed, 13 Dec 2017 08:57:36 +0100 (CET) Message-ID: <1513151855.2545.4.camel@cybertec.at> Subject: Re: inserts failed because of primary key conflict, weird sequence issue. what could cause a reset? From: Laurenz Albe To: jonathan vanasco , pgsql-general general Date: Wed, 13 Dec 2017 08:57:35 +0100 In-Reply-To: <0C538715-CC56-46B2-8E8B-0A565FBE92D2@2xlp.com> References: <0C538715-CC56-46B2-8E8B-0A565FBE92D2@2xlp.com> Organization: Cybertec Content-Type: text/plain; charset="UTF-8" Mime-Version: 1.0 Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Tue, 2017-12-12 at 18:10 -0500, jonathan vanasco wrote: > I have table where the primary key is a sequence. I was getting a lot of errors on insertions, > and noticed the current value was around 65k, but the max id in the database was around 160k. > everything works now that i've restarted it, but I can't find any record of a > restart sequence accidentally being put in. > > the only thing I can think of, is the sequence went up several hundred billion and restarted > from 0 when a drive crashed. but that seems incredibly unlikely given the last known actual max id. > > does anyone have an idea what could cause this? pg 9.6.6 on ubuntu There are only 2 ways: Either rows were inserted with explizit values for the serial column, or the sequence counter was reset. The latter is hard to observe in the log, since it will only be logged when all SELECT statements are logged. One idea to prevent that is to revoke the UPDATE privilege on the sequence. You can revoke that privilege from the owner as well. Nothing keeps the owner from granting the privilege to himself again, but until that is done, every attempt to reset the sequence will lead to an error. Yours, Laurenz Albe