I've seen this in a migration
enable_extension 'uuid-ossp'
as far as I know uuid is a long unique string based on some RFCs, and this enable the db (in this case pg) to have a column type as a uuid
my question is - Why is this type of column needed and not just a string column? is it to replace the regular integer id column and to have a uuid as the id instead?
is there any advantage to use a uuid as the id instead of just having a string type column contain a uuid?
I was hoping to see some more people chime in here, but I think the idea of the uuid is to replace the id column for a more unique id which is useful especially when you've got a distributed database or are dealing with replication.
Pros:
users/1
(the id) which might prompt a curious user to try users/2
to see if he could access someone else's information since its obvious the sequential nature of the parameter). Obviously there are other ways of dealing with this particular issue howeverCons:
Here are some more resources which I found valuable:
It is not necessary to install that extension to use the uuid
type. The advantages of using the UUID type in instead of a text type are two. The first is the automatic constraint
select 'a'::uuid;
ERROR: invalid input syntax for uuid: "a"
Second is storage space. UUID only uses 16 bytes while the hex representation takes 33:
select
pg_column_size('0123456789abcdef0123456789abcdef'),
pg_column_size('0123456789abcdef0123456789abcdef'::uuid)
;
pg_column_size | pg_column_size
----------------+----------------
33 | 16
The uuid-ossp
extension just adds functions to generate UUID
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With