Converting unsigned to signed bigintsPosted by Roger Keays, 24 May 2008, 2:19 PM |
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.
| << Using c:set in Facelets | Back to Blog | A Simple Exercise in Scalability >> |
Comment posted by: Roger Keays on 2 June 2008, 6:23 AM
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.
Add a comment
Please visit http://www.ninthavenue.com.au/blog/converting-unsigned-to-signed-bigints to add your comments.