Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PostgreSQL: Constant for smallint maximum value?

Does PostgreSQL have a constant (like MAXFLOAT in Objective-C) for the maximum value that a smallint can be?

I know the PostgreSQL numeric types documentation says it's 32767, but I'd rather use a constant than hard coding a value that could change.

I'm using this number to prevent an error when incrementing a smallint, e.g.:

UPDATE populations
SET count = count + 1
WHERE city_id = 3
AND count < 32767;
like image 384
ma11hew28 Avatar asked Sep 17 '26 18:09

ma11hew28


1 Answers

Create it:

create function MAX_SMALLINT() returns smallint immutable language sql as '
  select 32767::smallint;
';

Use it:

UPDATE populations
SET count = count + 1
WHERE city_id = 3
AND count < MAX_SMALLINT();
like image 192
Neil McGuigan Avatar answered Sep 20 '26 18:09

Neil McGuigan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!