Re: How to migrate column type from uuid to serial - Mailing list pgsql-general

From Hemil Ruparel
Subject Re: How to migrate column type from uuid to serial
Date
Msg-id CANW1aT98ArvKYjJZng1MvDt4jjg7CT5CaQmHH+Hr16CtMM151Q@mail.gmail.com
Whole thread Raw
In response to Re: How to migrate column type from uuid to serial  (Francisco Olarte <folarte@peoplecall.com>)
Responses Re: How to migrate column type from uuid to serial  (Francisco Olarte <folarte@peoplecall.com>)
List pgsql-general
I was thinking UUID was not a very good choice for id. Serial would be a better one because I don't have a billion customers. It is more like a thousand. So when I saw the customer ID of the payment gateway cannot accept more than 32 characters, I thought UUID is overkill. So I want to migrate to using a serial int instead as the primary key.,

On Wed, Oct 7, 2020 at 10:48 PM Francisco Olarte <folarte@peoplecall.com> wrote:
Hemil:

On Wed, Oct 7, 2020 at 2:49 PM Hemil Ruparel <hemilruparel2002@gmail.com> wrote:
> I was integrating a payment gateway for my app when I noticed its maximum length of customer id string is 32. SIze of UUID is 36 (32 characters and 4 dashes). So I want to change the type of customer id to serial. The problem is by now, the column is being used at many places. How to migrate the column to serial without dropping the data?

An uuid is just a 128 bit number, as noted by many. Your problem is
the default encoding is 32 hex digits plus 4 hyphens. If your payment
gateway uses a 32 CHARs string  ( "maximum length of customer id
string is 32" ) you can just use other encodings. Passing to integer
and using decimal wont cut it ( you'll need 39 digits ), but using
just hex (without hyphens) will drop the string representation to 32
characters ( you can encode with a subst and postgres accepts it
without hyphens ).

If you want to buy a little more space for your own purposes you can
even fit 128 bits in 22 base64 chars with a couple pairs of bits to
spare, and IIRC you can do it with a creative decode/encode step after
killing the dashes.

And if your payment gateway uses unicode codepoints instead of ASCII
chars as units you could probably use more creative encodings ;-) ,
but probably using  a "drop the dashes" subst in the interface will be
your simpler option.

Francisco Olarte.

pgsql-general by date:

Previous
From: Dmitry Igrishin
Date:
Subject: Re: Which is the setup with lowest resources you know Postgres is used in?
Next
From: Srinivasa T N
Date:
Subject: Both type of replications from a single server?