Converting Unsigned Bigints to Signed in PostgreSQL

By , 24 May 2008

Converting Unsigned Bigints to Signed in PostgreSQL

Just upgrading Dspam and discovered that the new version uses (or recommends) a BIGINT column for tokens instead of NUMERIC(20) on postgresql for better performance. Dspam tokens are unsigned 64 bit values, but postgresql's BIGINTs are signed 64 bit values. The new dspam just marshals back and forth between signed and unsigned values which avoids the costly NUMERIC data type.

The problem is, of course that all our data is stored as unsigned NUMERIC values. Dspam has a program to convert the database, dspam_pg2int8, but it gave me a segmentation fault :( . Anyway, I figured out that it's not too hard to do yourself in SQL and came up with this script, which presumes postgres is using two's complement for negative values:

begin;
alter table dspam_token_data rename token to token2;
alter table dspam_token_data add column token bigint;
update dspam_token_data set token = 
       case when token2 < (2 ^ 63)
            then token2
            else token2 - (2 ^ 64) 
            end;
alter table dspam_token_data drop column token2;
commit;

Hope you find it helpful.

Converting Unsigned Bigints to Signed in PostgreSQL

About Roger Keays

Converting Unsigned Bigints to Signed in PostgreSQL

Roger Keays is an artist, an engineer, and a student of life. He has no fixed address and has left footprints on 40-something different countries around the world. Roger is addicted to surfing. His other interests are music, psychology, languages, the proper use of semicolons, and finding good food.

Leave a Comment

Please visit https://rogerkeays.com/blog/converting-unsigned-to-signed-bigints to add your comments.

Comment posted by: , 16 years ago

Oh yah, I had to dump and restore the database to fix the column order too. Dspam is fussy about that. Presumably it's faster if the columns are in a known order.