5 ms·
> 5.2 Don't use char(n) even for fixed-length identifiers Well, i think "char(1) not null" is a valid choice for a very compact 1-byte field, "smallint" is twi
by out_of_protocol 2y ago
> 5.2 Don't use char(n) even for fixed-length identifiers
Well, i think "char(1) not null" is a valid choice for a very compact 1-byte field, "smallint" is twice as big
- deleted 2y ago[deleted]
- Volundr 2y agoA char can be multiple bytes. If 1 byte is what you want, you probably want bytea (with a constraint) or bit(8).
- FreakLegion 2y agoThere's also "char", with quotes, as a special type. It's always 1 byte. The other types have overhead. A single-byte bytea will actually use 2 bytes, char(1) will use at least that much, and bit(8) will use 7 bytes. Smallint is strictly better for representing small numbers.
- Volundr 2y agoTIL: https://www.postgresql.org/docs/17/datatype-character.html https://www.postgresql.org/docs/17/datatype-character.html (it's in the table at the bottom). I'd still be suspicious of using it, and the docs recommend against it, but it exists.
- FreakLegion 2y agoA good reason not to use it is that 49::"char"::int == 49, but 49::"char"::smallint == 1. In other words, int casts directly interpret the value as an integer, but smallint (and bigint) casts interpret it as an ASCII character, '1', which they then convert to the integer 1.