Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1bbGLz-0004EH-71 for pgsql-bugs@arkaria.postgresql.org; Sun, 21 Aug 2016 00:11:07 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1bbGHg-0003dr-AT for pgsql-bugs@arkaria.postgresql.org; Sun, 21 Aug 2016 00:06:40 +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 1bbGHf-0003dk-Su for pgsql-bugs@postgresql.org; Sun, 21 Aug 2016 00:06:39 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1bbGHX-0008Fd-Lb for pgsql-bugs@postgresql.org; Sun, 21 Aug 2016 00:06:39 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id u7L06THY009546; Sat, 20 Aug 2016 20:06:29 -0400 From: Tom Lane To: mitramaddy@gmail.com cc: pgsql-bugs@postgresql.org Subject: Re: BUG #14291: Sequence ID gets modified even for "on conflict" update In-reply-to: <20160820221708.1517.56191@wrigleys.postgresql.org> References: <20160820221708.1517.56191@wrigleys.postgresql.org> Comments: In-reply-to mitramaddy@gmail.com message dated "Sat, 20 Aug 2016 22:17:08 -0000" Date: Sat, 20 Aug 2016 20:06:29 -0400 Message-ID: <9545.1471737989@sss.pgh.pa.us> X-Pg-Spam-Score: -2.5 (--) 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 mitramaddy@gmail.com writes: > Expected result: Since we are only doing updates in step 5, the "start at" > for test_id_seq should remain at 2. > Actual Result: Even though there are no inserts, the "start at" for > test_id_seq increases to 6. This is not a bug. See previous discussions at, eg, https://www.postgresql.org/message-id/flat/20160105150227.1117.51692%40wrigleys.postgresql.org https://www.postgresql.org/message-id/flat/20160506065528.2693.64808%40wrigleys.postgresql.org The core reason why it's not a bug is that the INSERT is attempted in full and only after detecting a conflict in the attempted unique-index insertion does the code fall back to the ON CONFLICT path. More generally, though, it's not a terribly good idea to assume that the sequence of numbers obtained from a sequence object has no holes in it. The description of nextval() at https://www.postgresql.org/docs/9.5/static/functions-sequence.html specifically disclaims this: Important: To avoid blocking concurrent transactions that obtain numbers from the same sequence, a nextval operation is never rolled back; that is, once a value has been fetched it is considered used and will not be returned again. This is true even if the surrounding transaction later aborts, or if the calling query ends up not using the value. For example an INSERT with an ON CONFLICT clause will compute the to-be-inserted tuple, including doing any required nextval calls, before detecting any conflict that would cause it to follow the ON CONFLICT rule instead. Such cases will leave unused "holes" in the sequence of assigned values. Thus, PostgreSQL sequence objects cannot be used to obtain "gapless" sequences. regards, tom lane -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs