Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UUID or SEQUENCE for primary key?

Tags:

postgresql

I am coming from MySQL, and in MySQL you can use AUTOINCREMENT for a row's unique id as the primary key.

I find that there is no AUTOINCREMENT in Postgresql, only SEQUENCE or UUID.I have read somewhere that we can use UUID as the primary key of a table. This has the added advantage of masking other user's id (as I want to build APIs that take the ID in as a parameter). Which should I use for Postgresql?

like image 298
Justin Leo Avatar asked Oct 22 '15 05:10

Justin Leo


People also ask

Should I use UUID for primary key?

Pros. Using UUID for a primary key brings the following advantages: UUID values are unique across tables, databases, and even servers that allow you to merge rows from different databases or distribute databases across servers. UUID values do not expose the information about your data so they are safer to use in a URL.

Can we use sequence as primary key?

If you are using a single sequence to generate all your primary keys, and want to maintain sequence, you can't cache the sequence, so that could be a significant bottleneck in performance.

Is UUID good for primary key Postgres?

Numbers generated by a sequence and UUID s are both useful as auto-generated primary keys. Use identity columns unless you need to generate primary keys outside a single database, and make sure all your primary key columns are of type bigint .

Should I use UUID in database?

If your database is or will eventually be distributed, like in the case of a local-first application, or simply if your NoSQL database is scaling up and divided upon multiple servers, I'd say that you have almost non choice : Use UUID! Just know that there is some things that you can do to improve performance.

What is the difference between a UUID and a sequence?

A sequence in PostgreSQL does exactly the same as AUTOINCREMENT in MySQL. A sequence is more efficient than a uuid because it is 8 bytes instead of 16 for the uuid. You can use a uuid as a primary key, just like most any other data type. However, I don't see how this relates to masking of an user ID.

Can I use a UUID as a primary key in MySQL?

You can use UUID as primary key in your table as it will be unique. However do keep in mind that UUID will occupy a bit more space as compared to SEQUENCE. And also they are not very fast. But yes they are for sure unique and hence you are guaranteed to get a consistent data. Koen.

What is a UUID (universally unique identifier)?

Universally unique identifiers (UUIDs) are 128-bit (16-byte) numbers that are designed to be globally unique, and as a result they make for great primary keys. UUIDs are 128-bit numbers, but they're usually represented as 32 hexadecimal digits displayed in 5 groups separated by hyphens in the form 8-4-4-4-12.

Can I use randomly generated UUID as the primary key?

For database like MySQL, Oracle, which uses clustered primary key, version 4 randomly generated UUID will hurt insertion performance if used as the primary key. This is because it requires reordering the rows to place the newly inserted row at the right position inside the clustered index.


Video Answer


2 Answers

A sequence in PostgreSQL does exactly the same as AUTOINCREMENT in MySQL. A sequence is more efficient than a uuid because it is 8 bytes instead of 16 for the uuid. You can use a uuid as a primary key, just like most any other data type.

However, I don't see how this relates to masking of an user ID. If you want to mask the ID of a certain user from other users, you should carefully manage the table privileges and/or hash the ID using - for instance - md5().

If you want to protect a table with user data from snooping hackers that are trying to guess other IDs, then the uuid type is an excellent choice. Package uuid-ossp has several flavours. The version 4 is then the best choice as it has 122 random bits (the other 6 are used for identification of the version). You can create a primary key like this:

id uuid PRIMARY KEY DEFAULT uuid_generate_v4() 

and then you will never have to worry about it anymore.


PostgreSQL 13+

You can now use the built-in function gen_random_uuid() to get a version 4 random UUID.

like image 84
Patrick Avatar answered Sep 22 '22 19:09

Patrick


You can use UUID as primary key in your table as it will be unique. However do keep in mind that UUID will occupy a bit more space as compared to SEQUENCE. And also they are not very fast. But yes they are for sure unique and hence you are guaranteed to get a consistent data.

You can also refer:

  • UUID Primary Keys in PostgreSQL
  • UUID vs. Sequences
like image 22
Rahul Tripathi Avatar answered Sep 22 '22 19:09

Rahul Tripathi