12

I have a sequence that generates tracking numbers for objects in my system. It had been working fine for quite some time.

Last week we noticed that it was starting to re-use values.

What seems to happen is that at different points in the evening, it will rollback to a value it had the previous day. It will then continue generating values from that point.

So for example I could get something like this:

10112
10113
10114
10115
10116
10117
10118
10113
10114
10115
10116
...

There does not seem to be any pattern in when it happens, duration between the first use and second use (as few as 10 min or several hours) or how many are rolled back (as few as 1 and as many as several hundred).

I thought about running a trace (and still may), but I don't think the sequence object is being directly modified. The reason I believe this is that the modify date is several days old and points to a time when we manually bumped the value up to try and eliminate duplicates. (And the issue has occurred several times since then.)

Does anyone have an idea of what could cause a sequence rollback and reuse values each night?

UPDATE: To answer a few questions in the comments:

  • @@Version:

    Microsoft SQL Server 2012 (SP1) - 11.0.3000.0 (X64) Oct 19 2012 13:38:57

  • Create Script:

    CREATE SEQUENCE [schemaName].[SequenceName] 
      AS [bigint]
      START WITH 410014104
      INCREMENT BY 1
      MINVALUE 410000000
      MAXVALUE 419999999
      CYCLE 
      CACHE 
    GO
    
  • I don't have a Unique Constraint (but I plan to put one on). However that will only help me know when I have reused a value. Not what caused the values to reset. I put a job on that would get a new value every 5 min an save it off. The times and value jumps do not follow a pattern.

  • I have checked the Event Logs to see if there is an error. The only think that is happening is this: http://support.microsoft.com/kb/2793634 We are applying the fix today. I don't think these are related, but it could be.
14
  • 1
    Why isn't there a PK or Unique constraint on this column? With that in place, this reuse will get caught, and you don't have to try to guess where it's coming from, unless your application code just swallows all errors... Commented Jun 16, 2014 at 21:23
  • 1
    sqlservercentral.com/Forums/Topic1395798-3077-1.aspx
    – Mihai
    Commented Jun 16, 2014 at 21:27
  • Can you show the definition of your sequence? Also can you check the error log to see if any significant events happened overnight (e.g. failover, service restart, memory issues, etc)? Commented Jun 16, 2014 at 21:47
  • 2
    What is @@VERSION? Also has anything changed about the environment? There is a connect item reporting something similar. The OP there reckons it was associated with 11.0.3000.0 Commented Jun 16, 2014 at 21:50
  • 2
    Well, CYCLE essentially tells SQL Server you are okay with reusing values. I have absolutely no idea why you're having this issue, and don't know that you'll find out why (how much time do you spend investigating why you got a flat tire, before you just replace it?). I still think your best bet is to have a constraint there to prevent duplicates, and to turn off caching in hopes of preventing reuse. Commented Jun 17, 2014 at 16:46

1 Answer 1

12

First, if you don't want duplicates in this column, state that explicitly.

ALTER TABLE dbo.whatever ADD CONSTRAINT uq_that_column UNIQUE (that_column);

(Or you may want to make that the primary key, or change the clustered index, or what have you...)

In any case, raising an error when you generate a duplicate is far better than just blindly inserting a duplicate that you'll just have to deal with later.

Next, consider that a SEQUENCE is just a number generator, and by default it has a cache of 50 values. Depending on how your transactions are set up, and what other critical events happen on a server, it is possible that SQL Server can "forget" that it generated certain values for you.

There was a bug on Connect (790161) before they eradicated Connect, that talked about these duplicate values, but it seems to have been addressed in this fix (thanks @Andomar):

FIX: Sequence object generates duplicate sequence values when SQL Server 2012 or SQL Server 2014 is under memory pressure

Assume that you create a sequence object that has the CACHE option enabled in Microsoft SQL Server 2012 or SQL Server 2014. When the instance is under memory pressure, and multiple concurrent connections request sequence values from the same sequence object, duplicate sequence values may be generated. In addition, a unique or primary key (PK) violation error occurs when the duplicate sequence value is inserted into a table.

A way to get around this (if that fix doesn't solve your issue, or you are stuck on an ancient and unsupported build without the fix) is to change the sequence to use NO CYCLE and NO CACHE, e.g.:

ALTER SEQUENCE dbo.mysequence NO CYCLE NO CACHE; 

Note that NO CACHE can affect performance and concurrency, but will help eliminate gaps, lost blocks and, who knows, maybe your problem too.

4
  • Well, I can't have it backing up. So if NO CACHE will fix it, then that is what I will do.
    – Vaccano
    Commented Jun 17, 2014 at 16:46
  • I thought that there may be transactions causing this. But the Sequence page on MSDN says "Sequence numbers are generated outside the scope of the current transaction. They are consumed whether the transaction using the sequence number is committed or rolled back." So I discarded my transaction theory. I agree there must be something else going on.
    – Vaccano
    Commented Jun 17, 2014 at 16:50
  • Turns out that just setting it with NO CYCLE was enough. (At least it did not happen last night.) Thank you for the help!
    – Vaccano
    Commented Jun 19, 2014 at 16:13
  • 2
    FIX: Sequence object generates duplicate sequence values when SQL Server 2012 or SQL Server 2014 is under memory pressure Assume that you create a sequence object that has the CACHE option enabled in Microsoft SQL Server 2012 or SQL Server 2014. When the instance is under memory pressure, and multiple concurrent connections request sequence values from the same sequence object, duplicate sequence values may be generated. In addition, a unique or primary key (PK) violation error occurs when the duplicate sequence value is inserted into a table.
    – Andomar
    Commented Sep 2, 2015 at 11:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.