Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1aykAP-0001f5-31 for pgsql-bugs@arkaria.postgresql.org; Fri, 06 May 2016 18:07:57 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1aykAO-0004mF-MF for pgsql-bugs@arkaria.postgresql.org; Fri, 06 May 2016 18:07:56 +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 1ayk9l-0003ev-B6 for pgsql-bugs@postgresql.org; Fri, 06 May 2016 18:07:17 +0000 Received: from mail-lf0-x235.google.com ([2a00:1450:4010:c07::235]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84_2) (envelope-from ) id 1ayk9h-000898-Vv for pgsql-bugs@postgresql.org; Fri, 06 May 2016 18:07:16 +0000 Received: by mail-lf0-x235.google.com with SMTP id u64so140456545lff.3 for ; Fri, 06 May 2016 11:07:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=subject:to:references:cc:from:message-id:date:user-agent :mime-version:in-reply-to:content-transfer-encoding; bh=xR70rlPrCSRqpqEg5vjTgjdvroCua3wTfIHK7Gk32io=; b=FRBIco4YUVruGeS9vBwua9c4Xzay4aWzxOXWQAcNptctefSFxa2d9zRJow/YzXphKm k3AKmVvLPsSKXHq77EBrLMW0VfI4ozu3uk8AkjKPUq0IfXA/THH5vc2AHO2OIRJhE0l2 Grg7p8Zg5T4loQidI3gxUgfW4E66jxIMn7pTtBdhhygjxrK3byPR3NYGWywYC7gg4NOf /33hhK/S2BZDiKoaXlxkmTkJqLBSV1GVxMiSFEK3L4sGmWeoCcWdTwnqPoKCMh2uHv7Y QoPTW1Ti0y81tGt2rrr1Igu8otaX398dhfbLnLa/5uWOX7FoAsnx4toAF3OiOt5m/xwX fsUQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:subject:to:references:cc:from:message-id:date :user-agent:mime-version:in-reply-to:content-transfer-encoding; bh=xR70rlPrCSRqpqEg5vjTgjdvroCua3wTfIHK7Gk32io=; b=FDCLJpjPzl3iKDa+jFImpLgBxwSIn7Xyd0t/UTUuVkXTs6KJRepYPGzZpXQfb+EN78 6sViHU2goxtdGuZHMjSVo/dzcnSR6iIHRNj9VPgn5rqNSYMG9h9+AmuJMoappIohirdd Xn1DlCsa4Zv5owDAY6MUFAl6zUWiBoqr9Md5U+BLJCTCTcudqwZUO9AlexLnF3dYINBM ZOBanhpf2+5sQl8nK1ZZGvQFSne0U1A3oZGmva4hHW8KUkoJw6mc/r4mltq/ixT7lYi8 2ny1v0YFa8zhiqpwRvfcArHrDvaFzTI3XDwR0qVSlo6bsEb8RYY8FjYM0bAXChRX6nnS pKHA== X-Gm-Message-State: AOPr4FWu9XWMGeUuJs1lRtESVjGTPH4FFr8FZZhZLIF/CWYplUUUc4t4Cs0Ojz7b9jdxRw== X-Received: by 10.25.160.77 with SMTP id j74mr1017402lfe.72.1462558033435; Fri, 06 May 2016 11:07:13 -0700 (PDT) Received: from [192.168.47.131] ([193.187.83.5]) by smtp.gmail.com with ESMTPSA id d199sm2724606lfg.4.2016.05.06.11.07.12 (version=TLSv1/SSLv3 cipher=OTHER); Fri, 06 May 2016 11:07:12 -0700 (PDT) Subject: Re: BUG #14126: INSERT ON CONFLICT DO NOTHING auto increments serial primary key when no insert happens. To: cwire4@gmail.com References: <20160506065528.2693.64808@wrigleys.postgresql.org> Cc: Francisco Olarte , "pgsql-bugs@postgresql.org" From: Vitaly Burovoy X-Enigmail-Draft-Status: N1110 Message-ID: <572CDE1F.2010703@gmail.com> Date: Fri, 6 May 2016 18:10:39 +0000 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.7.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit X-Pg-Spam-Score: -2.7 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org On 05/06/16 13:39, Francisco Olarte wrote: > On Fri, May 6, 2016 at 8:55 AM, wrote: >> It's unclear to me if this is desired behavior or not, but when doing the >> following: > .... >> (previous successful key value + number of failed inserts) >> It seems to me that intuitively the key should only increment on a >> successful insert, not just an insert statement being executed. For one, >> this unnecessarily reduces the keyspace available for the column. > > I suspect this is "working as designed". The same things happen if you > do a lot of inserts and then rollback a transaction. This is because > serial use sequences, which are not mean to generate exact correlative > values, but to a mean to generate unique keys with very high > concurrency. > > What sequences do is grab a chunk of values ( may be of 1 or more, it > depends ) per backend needing them and each time you ask for one they > give you a unique value and burn it. This is great for concurrency, > and it is fast. The fact they normally use correlative numbers is > normally a by-product of being the easier way of generating different > ids, but their purpose is not to do it exactly. > > Think of it, if they needed to generate exact correlative amounts > anytime someone needed a number they would need to be locked until the > operation using it commits or rolls back, and informs everyone. It can > be done this way, but is much slower and normally not needed. > > Francisco Olarte. A bit more explanation: default value for serial columns are gotten from a sequence not at saving tuple in a storage, but _before_ tuple tries to pass triggers, checks (constraints, PKs, uniqueness, not nulls etc.) and inserts it to a table. And, as Francisco mentioned, sequence being increased doesn't reverts if transaction rolls back or insert does nothing. That's why its value monotonically increased even if number of real insertions is not changed. -- Best regards, Vitaly Burovoy -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs