Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eEvpj-0004nQ-HX for pgsql-bugs@arkaria.postgresql.org; Wed, 15 Nov 2017 11:26:19 +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 1eEvpi-00042t-Vt for pgsql-bugs@arkaria.postgresql.org; Wed, 15 Nov 2017 11:26:18 +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 1eEvpi-00042j-OR for pgsql-bugs@lists.postgresql.org; Wed, 15 Nov 2017 11:26:18 +0000 Received: from lana.depesz.com ([88.198.49.178] helo=depesz.com) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1eEvpb-00033p-4Q for pgsql-bugs@postgresql.org; Wed, 15 Nov 2017 11:26:18 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=depesz.com; s=20170201; h=In-Reply-To:Content-Type:MIME-Version:References:Reply-To:Message-ID:Subject:Cc:To:Sender:From:Date; bh=scyzpf6u91kyFcUNHN9KgsZyrKUHtmVo21tuwDG+Jvo=; b=FHkGHSmTtfhNWGUW/8lCHMOF6gfq9JIdo2OskeVS6VKNxHqbak9u2m4oBtx5WsUyBhaFS1aU0IJnfPJJlqBUjdh6P7RUC5G5l3tp80HaBlXLYqGBy1qDo4b8VhcJZ5Vxw9sTdKhOds4IhZEzXJ9mqv6PNwQ5OL3NIvXKTiYOe/M=; Received: from lana.depesz.com ([88.198.49.178] helo=depesz.com) by depesz.com with esmtpa (Exim 4.84_2) (envelope-from ) id 1eEvpY-0000mD-WA; Wed, 15 Nov 2017 12:26:09 +0100 Date: Wed, 15 Nov 2017 12:26:08 +0100 From: hubert depesz lubaczewski Sender: depesz@depesz.com To: personal@rysmax.com Cc: pgsql-bugs@postgresql.org Subject: Re: BUG #14909: nextval() bug Message-ID: <20171115112608.GA32070@depesz.com> Reply-To: depesz@depesz.com References: <20171115105136.1473.62937@wrigleys.postgresql.org> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <20171115105136.1473.62937@wrigleys.postgresql.org> User-Agent: Mutt/1.5.23 (2014-03-12) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Wed, Nov 15, 2017 at 10:51:36AM +0000, personal@rysmax.com wrote: > CREATE TABLE "user" ( > id serial primary key not null, > email varchar(64) not null > ); > INSERT INTO "user" (id, email) VALUES ('1', 'admin@example.com'); > INSERT INTO "user" (id, email) VALUES ('2', 'user@example.com'); > > All done without errors. > > > > When i try to run the query bellow: > > INSERT INTO "user" (email) VALUES ('test@example.com'); > > and got the next error in pgAdmin and from the PHP: > > ERROR: duplicate key value violates unique constraint "user_pkey" > DETAIL: Key (id)=(1) already exists. > > but when i try to run this query 2 times the query executed without errors > on 3rd time. > I think this is a bug in nextval() function. No, it's not. when you insert providing value for id column, default (nextval) is not called, so sequence is not updated. So, when you finally try to insert without id, nextval is called and returns first unused (from the point of view of sequence) value - 1. Which already is in the table because you "forcibly" inserted it. To avoid this problem you have to either: 1. run setval(...) after insertiing data with some forced ids 2. always use nextval/default for id column. Best regards, depesz