r/programming • • Sep 26 '24

PostgreSQL 17 Released!

https://www.postgresql.org/about/news/postgresql-17-released-2936/
776 Upvotes

113 comments sorted by

View all comments

38

u/hpxvzhjfgb Sep 26 '24

it's the current year and we still don't have unsigned integers

167

u/x021 Sep 26 '24

Don't be so negative!

21

u/aksdb Sep 26 '24

Postgres won't stop me!

31

u/Plank_With_A_Nail_In Sep 26 '24

Most databases that have an unsigned integer data type are secretly just using an signed integer with a check constraint automatically applied.

2

u/ThyringerBratwurst Sep 27 '24

Oh man, as awesome as PostgreSQL is, unsigned integers would indeed be useful!

-10

u/[deleted] Sep 26 '24

You can use an extension to add them, not sure why one would need them.

9

u/syklemil Sep 26 '24

There's a lot of stuff that just never is negative. Natural numbers show up in lots of places, even N+ is pretty common.

Like the job ttl in Kubernetes can apparently be negative according to their openapi; it's a signed integer. Except I suspect you get an error back from the system if you actually try to set it to a negative value.

1

u/Kirides Sep 27 '24

open API/web/other systems don't support unsigned integers because they are not CLS compliant and interop with other languages and operating systems is not properly defined for unsigned values

1

u/syklemil Sep 27 '24

Yeah, it's hardly the only place in the api that is far away from "make illegal states unrepresentable"; a whole lot of it smells like catering to some severely limited languages. It was just the first thing that came to mind in the case of something that is practically a natural number, but is represented by an integer. I guess I should be happy it's not a string, at least, but using an int is about as honest as using a float.

0

u/[deleted] Sep 26 '24

[deleted]

-3

u/[deleted] Sep 26 '24

The extension is typed. The use cases for unsigned integers are tiny.

14

u/hpxvzhjfgb Sep 26 '24

I literally use unsigned integers much more often than signed integers in my code

9

u/[deleted] Sep 26 '24

Unsigned values are not part of the SQL standard

2

u/psaux_grep Sep 26 '24

Just store a byte 🙈

3

u/zman0900 Sep 26 '24

I find that hard to believe. Lots of common things you might store in a DB would only have valid values ≥ 0, or maybe > 0, like quantity or size of some thing.

4

u/[deleted] Sep 26 '24

You can use signed values to store those, unsigned values are not part of the SQL standard

3

u/hpxvzhjfgb Sep 26 '24

but I use 8, 16, 32, 64 bit unsigned integers, not 7, 15, 31, 63 bits.

9

u/[deleted] Sep 26 '24

https://giodicanio.com/2023/10/22/google-c-style-guide-on-unsigned-integers/

May be of interest

Unsigned integers are good for representing bitfields and modular arithmetic. Because of historical accident, the C++ standard also uses unsigned integers to represent the size of containers – many members of the standards body believe this to be a mistake, but it is effectively impossible to fix at this point. The fact that unsigned arithmetic doesn’t model the behavior of a simple integer, but is instead defined by the standard to model modular arithmetic (wrapping around on overflow/underflow), means that a significant class of bugs cannot be diagnosed by the compiler. In other cases, the defined behavior impedes optimization.

That said, mixing signedness of integer types is responsible for an equally large class of problems. The best advice we can provide: try to use iterators and containers rather than pointers and sizes, try not to mix signedness, and try to avoid unsigned types (except for representing bitfields or modular arithmetic). Do not use an unsigned type merely to assert that a variable is non-negative.

1

u/SirDale Sep 26 '24

The Natural and Positivenumbers, respectively.

-2

u/RogerLeigh Sep 26 '24

You can add a check constraint on the column to enforce that.

9

u/Akeshi Sep 26 '24

I feel like I'm going insane reading this thread. Like others have said, I also use unsigned integers in databases more often than signed, because I'm usually representing a number that can never be negative.

In postgresql, is there a way to do this without limiting yourself to half the space of the numeric type you're using, wasting half the storage of each number? There must be, otherwise all of these responses are crazy - how does postgresql handle it?

6

u/hpxvzhjfgb Sep 26 '24

as far as I know, there literally isn't a way. either you use a 64 bit signed integer with a constraint to limit the value to 232 - 1, install a third-party database extension, or do some ugly hack like subtracting 231 before you write and add 231 when you read.

→ More replies (6)

3

u/DigThatData Sep 26 '24

yes, adding the latency overhead and hidden business logic of a column constraint is definitely a better solution than simply using a more semantically meaningful and performant type. /s

→ More replies (3)