NEW in PostgreSQL 16 - support for non-decimal notation of integer constants! PostgreSQL already has powerful support for string constants, with E't'
, E' 11'
, E'u0009'
and U&' 009'
all meaning the same thing (a “horizontal tab” character). However, PostgreSQL's support for numerical constants has - up until now - been rather limited; it didn't allow you to write integer numbers in bases other than 10. But PostgreSQL 16 implements the SQL:202x standard (draft), allowing you to use binary, octal and hexadecimal notation for integers.
Table of Contents
The advantage of using non-decimal notation are:
- It makes it easier to read and understand the code.
- Binary notation is used to store data in a computer's memory, so using binary notation in code makes it easier to interact with data stored in memory.
- Octal and hexadecimal notations allow for more concise representation of larger numbers.
- Octal and hexadecimal notations are more familiar to programmers and computer scientists.
Peter Eisentraut implemented a patch which adds support for the hexadecimal, octal, and binary integer literals:
0x42F
0o273
0b100101
The patch was reviewed and committed by John Naylor, Zhihong Yu, David Rowley, and Dean Rasheed.
It took almost a year to solve all the issues and to provide all the tests. Changes were introduced not only to the lexer but also to the integer type input functions. You can read more about input functions in the manual.
As usual, I strongly encourage you to read the whole discussion which happened during the implementation of this functionality. You can get an idea of how the community operates and what pitfalls may arise out of the blue! Consider this email thread a detective story! 🕵️
Do you remember the maximum 32-bit unsigned integer value? Me neither. You don't need to now!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
psql (16devel) Type 'help' for help. test=# select version(); version ---------------------------------------------------------------------------------------------------------- PostgreSQL 16devel on x86_64-pc-linux-gnu, compiled by gcc (Ubuntu 9.4.0-1ubuntu1~20.04.1) 9.4.0, 64-bit (1 row) test=# select 0xFFFFFFFF as maxint; maxint ------------ 4294967295 (1 row) |
The two hues that are mesmerizing the world right now are the colors of the Ukrainian flag.
Do you know Pantone has labeled them “Freedom Blue” and “Energizing Yellow”, a resounding representation of the importance of freedom from oppression and the energetic determination of a country to defend itself from a foreign invader?
You are currently viewing a placeholder content from X. To access the actual content, click the button below. Please note that doing so will share data with third-party providers.
So next time you want to have a country
table, don't forget to store the flag colors, e.g.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
test=# create table country( test(# name text not null unique, test(# flag_colors integer[] not null test(# ); CREATE TABLE test=# insert into country values ('Ukraine', array[0x0057B7, 0xFFDD00]); INSERT 0 1 test=# select * from country; name | flag_colors ---------+------------------ Ukraine | {22455,16768256} (1 row) |
Or maybe you want to store information about some file?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
test=# create table file( test(# filename text not null unique, test(# permission integer not null test(# ); CREATE TABLE test=# insert into file values ('/usr/local/pgsql/bin/pg_ctl', 0o755); -- rwxr-xr-x INSERT 0 1 test=# select * from file; filename | permission -----------------------------+------------- /usr/local/pgsql/bin/pg_ctl | 493 (1 row) |
PostgreSQL 16 will support hexadecimal, octal, and binary integer literals as 0x
, 0o
, and 0b
prefixes. These notations make it easier to read and understand code, interact with data stored in memory, and represent larger numbers more concisely. Check out out the "what's new" post series to know more about upcoming PostgreSQL versions.
+43 (0) 2622 93022-0
office@cybertec.at
You are currently viewing a placeholder content from Facebook. To access the actual content, click the button below. Please note that doing so will share data with third-party providers.
More InformationYou are currently viewing a placeholder content from X. To access the actual content, click the button below. Please note that doing so will share data with third-party providers.
More Information
Leave a Reply