Sequence generating negative numbers

Started by Shantanu Shekharover 5 years ago5 messagesgeneral
Jump to latest
#1Shantanu Shekhar
shekharshan@yahoo.com

Team,
I have a sequence definition in Postgres 9.6.11 like so:
CREATE SEQUENCE IF NOT EXISTS org.my_seq  INCREMENT 1  MINVALUE 1  NO MAXVALUE  START 1  CACHE 20;
This sequence is used by a Java ORM framework to generate primary keys for one of our tables. The initial numbers generated by this sequence are as shown below:
-28 -27 -26 -25 -8 -7 1 2 52 53 72 92 93 94 112 113 132 133 152 172 192 193 212

I am unable to understand why the sequence would start with a negative number, particularly when the definition explicitly asks the sequence to start at 1. This has happened consistently in all of our environments. 
Thanks,
Shantanu

#2Adrian Klaver
adrian.klaver@aklaver.com
In reply to: Shantanu Shekhar (#1)
Re: Sequence generating negative numbers

On 8/19/20 3:15 PM, Shantanu Shekhar wrote:

Team,

I have a sequence definition in Postgres 9.6.11 like so:

CREATE SEQUENCE IF NOT EXISTS org.my_seq
  INCREMENT 1
  MINVALUE 1
  NO MAXVALUE
  START 1
  CACHE 20;

This sequence is used by a Java ORM framework to generate primary keys
for one of our tables. The initial numbers generated by this sequence
are as shown below:

-28 -27 -26 -25 -8 -7 1 2 52 53 72 92 93 94 112 113 132 133 152 172 192
193 212

I am unable to understand why the sequence would start with a negative
number, particularly when the definition explicitly asks the sequence to
start at 1. This has happened consistently in all of our environments.

Because something in the ORM is generating negative numbers and
supplying then directly to the PK field.

Thanks,

Shantanu

--
Adrian Klaver
adrian.klaver@aklaver.com

#3Adrian Klaver
adrian.klaver@aklaver.com
In reply to: Shantanu Shekhar (#1)
Re: Sequence generating negative numbers

On 8/19/20 3:24 PM, Shantanu Shekhar wrote:
Please reply to list also.
Ccing list.

Thanks Adrian,

I will reach out to the ORM team and see if they can help me understand
this behavior.

I should have asked earlier, is this sequence set as a DEFAULT on the PK
field or is it just being used by code to get numbers?

Shantanu
On Wednesday, August 19, 2020, 06:20:27 PM EDT, Adrian Klaver
<adrian.klaver@aklaver.com> wrote:

On 8/19/20 3:15 PM, Shantanu Shekhar wrote:

Team,

I have a sequence definition in Postgres 9.6.11 like so:

CREATE SEQUENCE IF NOT EXISTS org.my_seq
    INCREMENT 1
    MINVALUE 1
    NO MAXVALUE
    START 1
    CACHE 20;

This sequence is used by a Java ORM framework to generate primary keys
for one of our tables. The initial numbers generated by this sequence
are as shown below:

-28 -27 -26 -25 -8 -7 1 2 52 53 72 92 93 94 112 113 132 133 152 172 192
193 212

I am unable to understand why the sequence would start with a negative
number, particularly when the definition explicitly asks the sequence to
start at 1. This has happened consistently in all of our environments.

Because something in the ORM is generating negative numbers and
supplying then directly to the PK field.

Thanks,

Shantanu

--
Adrian Klaver
adrian.klaver@aklaver.com <mailto:adrian.klaver@aklaver.com>

--
Adrian Klaver
adrian.klaver@aklaver.com

#4Shantanu Shekhar
shekharshan@yahoo.com
In reply to: Adrian Klaver (#3)
Re: Sequence generating negative numbers

Adrian,
The sequence is not set as a default on PK. The sequence is exclusively being used by the ORM. 
Thanks,
Shantanu
On Wednesday, August 19, 2020, 06:28:53 PM EDT, Adrian Klaver <adrian.klaver@aklaver.com> wrote:

On 8/19/20 3:24 PM, Shantanu Shekhar wrote:
Please reply to list also.
Ccing list.

Thanks Adrian,

I will reach out to the ORM team and see if they can help me understand
this behavior.

I should have asked earlier, is this sequence set as a DEFAULT on the PK
field or is it just being used by code to get numbers?

Shantanu
On Wednesday, August 19, 2020, 06:20:27 PM EDT, Adrian Klaver
<adrian.klaver@aklaver.com> wrote:

On 8/19/20 3:15 PM, Shantanu Shekhar wrote:
  > Team,
  >
  > I have a sequence definition in Postgres 9.6.11 like so:
  >
  > CREATE SEQUENCE IF NOT EXISTS org.my_seq
  >    INCREMENT 1
  >    MINVALUE 1
  >    NO MAXVALUE
  >    START 1
  >    CACHE 20;
  >
  > This sequence is used by a Java ORM framework to generate primary keys
  > for one of our tables. The initial numbers generated by this sequence
  > are as shown below:
  >
  > -28 -27 -26 -25 -8 -7 1 2 52 53 72 92 93 94 112 113 132 133 152 172 192
  > 193 212
  >
  > I am unable to understand why the sequence would start with a negative
  > number, particularly when the definition explicitly asks the sequence to
  > start at 1. This has happened consistently in all of our environments.

Because something in the ORM is generating negative numbers and
supplying then directly to the PK field.

  >
  > Thanks,
  >
  > Shantanu

--
Adrian Klaver
adrian.klaver@aklaver.com <mailto:adrian.klaver@aklaver.com>

--
Adrian Klaver
adrian.klaver@aklaver.com

#5Adrian Klaver
adrian.klaver@aklaver.com
In reply to: Shantanu Shekhar (#4)
Re: Sequence generating negative numbers

On 8/20/20 9:21 AM, Shantanu Shekhar wrote:

Adrian,

The sequence is not set as a default on PK. The sequence is exclusively
being used by the ORM.

Then something there is generating negative numbers and using them to
populate the PK.

Thanks,

--
Adrian Klaver
adrian.klaver@aklaver.com