vektor
vektor

Reputation: 2926

Is there a MAX_INT constant in Postgres?

In Java I can say Integer.MAX_VALUE to get the largest number that the int type can hold.

Is there a similar constant/function in Postgres? I'd like to avoid hard-coding the number.

Edit: the reason I am asking is this. There is a legacy table with an ID of type integer, backed by a sequence. There is a lot of incoming rows into this table. I want to calculate how much time before the integer runs out, so I need to know "how many IDs are left" divided by "how fast we are spending them".

Upvotes: 37

Views: 30212

Answers (2)

Eto Kto
Eto Kto

Reputation: 96

There is a legacy table with an ID of type integer, backed by a sequence.

In that case, you can get the max value of the sequence by:
select seqmax from pg_sequence where seqrelid = 'your_sequence_name'::regclass.
This might be better than getting the MAX_INT, because sequence may have been created/altered with a specific max value that is different from MAX_INT.

Upvotes: 4

Nathaniel Waisbrot
Nathaniel Waisbrot

Reputation: 24493

There's no constant for this, but I think it's more reasonable to hard-code the number in Postgres than it is in Java.

In Java, the philosophical goal is for Integer to be an abstract value, so it makes sense that you'd want to behave as if you don't know what the max value is.

In Postgres, you're much closer to the bare metal and the definition of the integer type is that it is a 4-byte signed integer.

Upvotes: 20

Related Questions