SQL Sequence skips first value [message #467429] |
Fri, 23 July 2010 19:04 |
rkaula
Messages: 22 Registered: December 2005
|
Junior Member |
|
|
For some reason Oracle Sequence is skipping the first value. Here is an example of script.
drop sequence tseq;
drop table xyz;
create table xyz
(sno integer);
create sequence tseq
start with 1;
insert into xyz values (tseq.nextval);
select * from xyz;
Now the runtime of the script in SQL Developer:
drop sequence tseq succeeded.
drop table xyz succeeded.
create table succeeded.
create sequence succeeded.
1 rows inserted
SNO
----------------------
2
Why is it skipping the starting value 1. Is there something new in 11g that is causing it to skip the first value. I have many scripts and they all are having errors due to this issue.
Thanks.
|
|
|
|
|
|
|
Re: SQL Sequence skips first value [message #467434 is a reply to message #467433] |
Fri, 23 July 2010 21:21 |
rkaula
Messages: 22 Registered: December 2005
|
Junior Member |
|
|
I don't know what is meant by "reproduce what you desire." I have posted the error with a test script. What else should I do??
Let me explain: I recently installed Oracle 11g release 2 on a Windows 7 64-bit machine. Since I use Oracle database script files from a book for teaching purposes, I ran those scripts and started seeing errors. I have the same scripts running on previous Oracle versions (Oracle 9i, Oracle 10g, and Oracle 11g Release 1) and never had this error of the first sequence value getting skipped. If this error is specific to my machine, I guess I should uninstall release 2 and go back to release 1!!
Thanks for your help.
|
|
|
Re: SQL Sequence skips first value [message #467435 is a reply to message #467434] |
Fri, 23 July 2010 21:28 |
|
BlackSwan
Messages: 26766 Registered: January 2009 Location: SoCal
|
Senior Member |
|
|
>I don't know what is meant by "reproduce what you desire."
Post all SQL statements using sqlplus via COPY & PASTE so we can see EXACTLY what you do & how Oracle responds.
>I have posted the error with a test script.
REALLY? What error has been posted?
>What else should I do??
Post content of test script
[Updated on: Fri, 23 July 2010 21:30] Report message to a moderator
|
|
|
Re: SQL Sequence skips first value [message #467436 is a reply to message #467435] |
Fri, 23 July 2010 21:44 |
rkaula
Messages: 22 Registered: December 2005
|
Junior Member |
|
|
Maybe I didn't clear what I was requesting. The error is that the sequence is set to start at 1, but when the first insert statement with <sequence>.nextval is executed, the value inserted is not 1, but 2. It skips the starting value. Is it possible to make sure that the starting value is the first value inserted.
I had earlier copied/pasted from SQL Developer. Below is the SQL Plus runtime.
SQL*Plus: Release 11.2.0.1.0 Production on Fri Jul 23 21:32:31 2010
Copyright (c) 1982, 2010, Oracle. All rights reserved.
Enter user-name: rajeev
Enter password:
Connected to:
Personal Oracle Database 11g Release 11.2.0.1.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
SQL> drop sequence tseq;
Sequence dropped.
SQL> drop table xyz;
Table dropped.
SQL>
SQL> create table xyz
2 (sno integer);
Table created.
SQL>
SQL> create sequence tseq
2 start with 1;
Sequence created.
SQL>
SQL> insert into xyz values (tseq.nextval);
1 row created.
SQL>
SQL> select * from xyz;
SNO
----------
2
SQL>
|
|
|
Re: SQL Sequence skips first value [message #467437 is a reply to message #467436] |
Fri, 23 July 2010 22:46 |
|
BlackSwan
Messages: 26766 Registered: January 2009 Location: SoCal
|
Senior Member |
|
|
http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/statements_6015.htm#i2067093
Doc. says as follows:
Creating a Sequence: Example The following statement creates the sequence customers_seq in the sample schema oe.
This sequence could be used to provide customer ID numbers when rows are added to the customers table.
CREATE SEQUENCE customers_seq
START WITH 1000
INCREMENT BY 1
NOCACHE
NOCYCLE;
The first reference to customers_seq.nextval returns 1000. The second returns 1001.
Each subsequent reference will return a value 1 greater than the previous reference.
Below is what Oracle does for me.
SQL> @seq1
SQL> drop sequence tseq;
Sequence dropped.
SQL> drop table xyz;
Table dropped.
SQL> create table xyz (sno integer);
Table created.
SQL> create sequence tseq start with 1000;
Sequence created.
SQL> select * from user_sequences;
SEQUENCE_NAME MIN_VALUE MAX_VALUE INCREMENT_BY C O CACHE_SIZE LAST_NUMBER
------------------------------ ---------- ---------- ------------ - - ---------- -----------
TSEQ 1 1.0000E+28 1 N N 20 1000
SQL> insert into xyz values (tseq.nextval);
1 row created.
SQL> select * from xyz;
SNO
----------
1001
The results I see differs from what is documented, IMO.
[Updated on: Sat, 24 July 2010 01:42] by Moderator Report message to a moderator
|
|
|
|
Re: SQL Sequence skips first value [message #467443 is a reply to message #467429] |
Sat, 24 July 2010 05:13 |
|
Kevin Meade
Messages: 2103 Registered: December 1999 Location: Connecticut USA
|
Senior Member |
|
|
might be a problem.
Or it might be correct behavior.
A sequence is not guaranteed to return values in any given order unless you use the order clause. Unless you use ORDER, then on your first retrieval from the sequence, the sequence is only required to give you a value that is in the current set of available values, not the first value of the sequence. Consider this:
create sequence kev1 start with 1 increment by 1 cache 10;
Given this sequence, the first set of values that will be made available for use will be the numbers 1 thru 10. When you grab your first sequence value, the sequence is only required to give you some number from 1 to 10. 2 is a number from 1 to 10 so the sequence is behaving correctly given its definition.
Now consider this sequence:
create sequence kev1 start with 1 increment by 1 cache 10 order;
The first set of values from this sequence will also be the numbers 1 thru 10, but this sequence is required to give out numbers in order so the first number given will be 1 the second number will be 2 and so on.
I am not saying that there is not a problem. Only that your description does not yet break any rules for sequences as I understand them.
Now try this:
create sequence kev1 start with 1 increment by 1 cache 10 maxvalue 10;
This sequence does not have the ORDER clause and so can give numbers out in any order. If you fetch from the sequence 10 times, do you get all 10 numbers 1 thru 10? Or do you get an error? If you can get all numbers from 1 thru 10 before you have an error, then you are OK if not then you have found a true bug.
select kev1.nextval from dual;
/
/
/
/
/
/
/
/
/
Good luck, Kevin
[Updated on: Sat, 24 July 2010 05:16] Report message to a moderator
|
|
|
Re: SQL Sequence skips first value [message #467481 is a reply to message #467443] |
Sat, 24 July 2010 13:52 |
Solomon Yakobson
Messages: 3303 Registered: January 2010 Location: Connecticut, USA
|
Senior Member |
|
|
Kevin Meade wrote on Sat, 24 July 2010 06:13A sequence is not guaranteed to return values in any given order unless you use the order clause.
Can you provide an example where sequence values are returned not in order on a single instance? AFAIK, and also according to docs:
Quote:
CREATE SEQUENCE
ORDER is necessary only to guarantee ordered generation if you are using Oracle Database with Real Application Clusters. If you are using exclusive mode, sequence numbers are always generated in order.
ORDER in a sequence coordinates access to sequence between multiple nodes in RAC. It guarantees values are returned in order of requests made accross multiple instances of a database (RAC) by different nodes.
SY.
[Updated on: Sat, 24 July 2010 15:04] Report message to a moderator
|
|
|
Re: SQL Sequence skips first value [message #467482 is a reply to message #467429] |
Sat, 24 July 2010 14:59 |
|
Kevin Meade
Messages: 2103 Registered: December 1999 Location: Connecticut USA
|
Senior Member |
|
|
Yes really. As I said, sequences are not guaranteed to return values in order unless you use the ORDER clause.
For those who want to see docs with syakobson's quote, here is a link. You will find the following:
Quote:ORDER
Specify ORDER to guarantee that sequence numbers are generated in order of request. This clause is useful if you are using the sequence numbers as timestamps. Guaranteeing order is usually not important for sequences used to generate primary keys.
ORDER is necessary only to guarantee ordered generation if you are using Oracle Real Application Clusters. If you are using exclusive mode, then sequence numbers are always generated in order.
NOORDER
Specify NOORDER if you do not want to guarantee sequence numbers are generated in order of request. This is the default.
About 20% the databases in my company run on RAC and we are in the process of consolidation so this percentage is going up for me over the next several years.
I take the OP at their word, that they did in fact see the behavior they described. If so, then the code I have provided will allow the OP to determine if they have a problem.
I will admit however that my post did lack detail. I thank you for bringing this to the attention of the community. My description of sequences though correct is incomplete and given that more than half of all databases are NOT running RAC, it was a mistake for me to leave these details out. Hopefully when others see the actual documentation page for 11g in the link they will understand.
My advice to everyone is if you want a sequence to be guaranteed to generate values in order, use the ORDER clause. As the future moves forward, use of RAC will increase because successful database grow user counts like rabbits make baby rabbits. Additionally using ORDER in the definition of a sequence when necessary provides knowledge about the index in the metadata even if you never expect your database to go to RAC some day.
Good luck, and thank you syakobson for this clarification of detail. Kevin
[Updated on: Sat, 24 July 2010 15:06] Report message to a moderator
|
|
|
Re: SQL Sequence skips first value [message #467483 is a reply to message #467482] |
Sat, 24 July 2010 15:27 |
Solomon Yakobson
Messages: 3303 Registered: January 2010 Location: Connecticut, USA
|
Senior Member |
|
|
Kevin Meade wrote on Sat, 24 July 2010 15:59I take the OP at their word, that they did in fact see the behavior they described. If so, then the code I have provided will allow the OP to determine if they have a problem.
I, on the contrary, am quite sceptical it is what OP describes. But I will assume it. Then, even if OP has RAC, I find it highly unlikely someone on some other node managed to select from a sequence between OP issuing DROP & CREATE sequence. And, BTW, if someone on some other node managed to do it, creating sequence with ORDER would not prevent a thing. Anyway, if what OP described is correct I'd be executing same script in SQL*Developer again to make sure it it is reproducible. If it is, I'd run it from SQL*Plus and if it is reproducible there too, I'd be looking for something like DDL triggers.
SY.
|
|
|
Re: SQL Sequence skips first value [message #467484 is a reply to message #467429] |
Sat, 24 July 2010 15:46 |
|
Kevin Meade
Messages: 2103 Registered: December 1999 Location: Connecticut USA
|
Senior Member |
|
|
I agree with you. The likelyhood of sequences skipping the first value is low even if they are running on RAC. And the value of ORDERed sequences is skeptical except possiblty for using a sequence as a form of time-stamp.
But the meaning of ORDER on a sequence is still clear, and the code I provided will allow the OP to determine if they have the problem they described regardless of it they are on RAC or not. If they can get an error from the last sequence I provided before they generate all 10 initial values then there is indeed a bug in their release of 11g.
Yet as you point out, the best money is riding on Operator Error.
I also restate my commentary. If you want a sequence to give values in true sequence, then use ORDER. It protects you from rare issues on RAC and provides additional metadata on your intent behind the sequence.
Kevin
[Updated on: Sat, 24 July 2010 16:00] Report message to a moderator
|
|
|
|
Re: SQL Sequence skips first value [message #467486 is a reply to message #467485] |
Sat, 24 July 2010 16:09 |
Solomon Yakobson
Messages: 3303 Registered: January 2010 Location: Connecticut, USA
|
Senior Member |
|
|
BlackSwan wrote on Sat, 24 July 2010 16:55Comments?
And same on other version(s)? I do not have 11.2, but looks OK on:
SQL> select * from v$version
2 /
BANNER
--------------------------------------------------------------------------------
Oracle Database 11g Enterprise Edition Release 11.1.0.6.0 - Production
PL/SQL Release 11.1.0.6.0 - Production
CORE 11.1.0.6.0 Production
TNS for 32-bit Windows: Version 11.1.0.6.0 - Production
NLSRTL Version 11.1.0.6.0 - Production
SQL> create table xyz (sno integer);
Table created.
SQL> create sequence tseq start with 1000;
Sequence created.
SQL> insert into xyz values (tseq.nextval);
1 row created.
SQL> select * from xyz;
SNO
----------
1000
SQL>
SY.
|
|
|
|
|
|
|
Re: SQL Sequence skips first value [message #467491 is a reply to message #467429] |
Sat, 24 July 2010 16:23 |
|
Kevin Meade
Messages: 2103 Registered: December 1999 Location: Connecticut USA
|
Senior Member |
|
|
Well as in all things, we have to read as much as we can in order to understand the overall perspective on things. All documentation takes some amount of "interpretation". For example:
Quote:NOORDER
Specify NOORDER if you do not want to guarantee sequence numbers are generated in order of request. This is the default.
I read this and I hear
1) NOORDER is the default for all sequences
2) NOORDER means exactly what is says, not guaranteed to generate numbes in order or request.
I suppose to BlackSwan I who provided such a good example, I would reply to last post that if I create my sequence to start with 1 I expect to be able to get 1 at some point in taking numbers off the first cached set.
However I also agree that the following does demonstrate a bug:
SQL> drop sequence xyz;
Sequence dropped.
SQL> create sequence xyz;
Sequence created.
SQL> select * from user_sequences where sequence_name = 'XYZ';
SEQUENCE_NAME MIN_VALUE MAX_VALUE INCREMENT_BY C O CACHE_SIZE LAST_NUMBER
------------------------------ ---------- ---------- ------------ - - ---------- -----------
XYZ 1 1.0000E+27 1 N N 20 1
1 row selected.
Having not yet selected from this sequence, why does it report in its metadata that the last number I selected was 1?
Kevin
[Updated on: Sat, 24 July 2010 22:28] Report message to a moderator
|
|
|
|
|
|
|
|
|
Re: SQL Sequence skips first value [message #468706 is a reply to message #468703] |
Sun, 01 August 2010 13:41 |
Solomon Yakobson
Messages: 3303 Registered: January 2010 Location: Connecticut, USA
|
Senior Member |
|
|
Michel Cadot wrote on Sun, 01 August 2010 13:57Interesting, this seems to indicate that Oracle tries the insert, it fails because there is no segment, create the segement then restart the insert from the beginning.
Indeed, and I don't understand why. GTT is, in a sense, similar. There is no segment before first insert within a session or after commit, however select is not repeated:
SQL> create sequence s;
Sequence created.
SQL> create global temporary table test(x number);
Table created.
SQL> select count(*)
2 from v$tempseg_usage
3 where session_addr = (
4 select saddr
5 from v$session
6 where audsid = sys_context('userenv','sessionid')
7 )
8 /
COUNT(*)
----------
0
SQL> insert
2 into test
3 select s.nextval
4 from dual
5 connect by level <= 3
6 /
3 rows created.
SQL> select count(*)
2 from v$tempseg_usage
3 where session_addr = (
4 select saddr
5 from v$session
6 where audsid = sys_context('userenv','sessionid')
7 )
8 /
COUNT(*)
----------
1
SQL> select *
2 from test
3 /
X
----------
1
2
3
SQL> commit
2 /
Commit complete.
SQL> select count(*)
2 from v$tempseg_usage
3 where session_addr = (
4 select saddr
5 from v$session
6 where audsid = sys_context('userenv','sessionid')
7 )
8 /
COUNT(*)
----------
0
SQL>
So what can happen in creating initial segment that can affect SELECT results and therefore it has to be restarted?
SY.
|
|
|
|
|
|
Re: SQL Sequence skips first value [message #468717 is a reply to message #468716] |
Sun, 01 August 2010 14:16 |
Solomon Yakobson
Messages: 3303 Registered: January 2010 Location: Connecticut, USA
|
Senior Member |
|
|
Barbara Boehmer wrote on Sun, 01 August 2010 15:05I don't have access to metalink or whatever they call it nowadays, but for those of you who do, I found references to 1050193.1 about a bug with deferred segment creation and sequences skipping the first value in 11gR2.
And response from Oracle was as in many cases lately "is is not a bug - it is a feature":
Quote:After dev investigating the issue they concluded that is normal behavior given the default setup of 11.2.
SY.
[Updated on: Sun, 01 August 2010 14:17] Report message to a moderator
|
|
|
|
|
Re: SQL Sequence skips first value [message #468722 is a reply to message #468721] |
Sun, 01 August 2010 16:22 |
Solomon Yakobson
Messages: 3303 Registered: January 2010 Location: Connecticut, USA
|
Senior Member |
|
|
Michel,
I didn't have sequence in mind. Assume long running query where it takes long time to get to first row fetch. Now CTAS or INSERT SELECT with such query might take significantly longer. And I am not sure what so "big" about deferred segment creation (besides advertizing new feature) that Oracle made it default behavior.
SY.
|
|
|
|
|
Re: SQL Sequence skips first value [message #468725 is a reply to message #468724] |
Sun, 01 August 2010 17:35 |
Solomon Yakobson
Messages: 3303 Registered: January 2010 Location: Connecticut, USA
|
Senior Member |
|
|
Kevin Meade wrote on Sun, 01 August 2010 18:12The same issue may actually exist for any row insert using a sequence number generator when ever a new segment needs to be allocated.
Not sure what you mean. Segment (along with initial extent) is created once per table. Then it is next extent that is allocated when needed.
SY.
|
|
|
|