GraphQL and UUID type on Postgres
Tags: graphql, postgres, database, tutorial
Published: 2019-12-19 11:23:11

---
title: GraphQL and UUID type on Postgres
published: true
date: 2018-08-17 00:00:00 UTC
tags: graphql, postgres, database, tutorial
canonical_url: https://blog.hasura.io/graphql-and-uuid-type-on-postgres-767f016479e9/
---

### Hasura GraphQL Engine has implicit support for Postgres UUID type, which means that UUID values can be provided as strings.

![](https://blog.hasura.io/content/images/downloaded_images/graphql-and-uuid-type-on-postgres-767f016479e9/1-ohu0ZZXlzkOjp-NyTEr4GA.png)

Every table in a database needs a [primary key](https://hasura.io/learn/database/postgresql/core-concepts/5-postgresql-primary-key/).

A primary key’s purpose is to provide a stable, indexable reference to an entity in a table. Common practice is to use serial ids as primary keys. Serial ids are auto-incrementing integers, which are usually auto-generated by the database.

A robust alternative to a serial id is the UUID (Universally Unique Identifier) or the GUID (Globally Unique Identifier), which is a standard 128-bit number that is very hard to duplicate, thereby providing unique identifiers which are perfect for use as primary keys.

_**What is the Postgres UUID Type?**_

The Postgres UUID data type stores UUIDs as defined by RFC 4122, which are 128-bit quantities generated by algorithms that minimize the probability of having duplicate identifiers.

A UUID comprises of 32 hexadecimal digits, represented in groups of 8,4,4,4 and 12.

Eg: “12345678-abcd-ef12–345a-12345678abcd”

_**Why UUID?**_

- _**Exposing information:**_ Very frequently, the primary id is exposed to the user (REST urls, anyone?). If the primary key is easily visible to the user, this means that anyone can find out your number of users by just looking at the primary key on a freshly created user. This leaks information about the size of your userbase.
- _**Enumeration of entries:**_ Another problem is that it’s very easy to enumerate all the entries of your table with a primary key. A malicious user or a bot could easily scrape identities of all your users, or spam your API with organised actions on all your users. A UUID is universally random, and is practically impossible for external parties to predict. To be very clear though, UUIDs are not a security mechanism, the actual solution to this problem is to put up stronger access control. UUIDs just provide more protection in this case.
- _**Independent Generation:**_ The biggest problem with sequential ids is that the client doesn’t know the id of an object being inserted without talking to the database. This is even more of a problem when inserting multiple related objects with foreign keys, and creates inelegant multi step inserts. A UUID on the other hand, can be generated directly by the client making inserts very simple, and solves the foreign key problem quite nicely too.
- _**Random distribution:**_ The random distribution of the UUIDs can prevent disk hotspots, where a large number of objects are stored in a particular disk block, causing high usage on specific areas of the disk. This works very well for databases distributed over several nodes.

_**Why not UUID?**_

- _**Size:**_ The downside is that UUIDs are four times as big as a serial, and if you have size constraints, they can be a problem.
- _**Hard to debug:**_ Harder to remember, hard to debug, a small but important QoL issue. It’s easier to test/debug when you have simpler ids that you can remember compared to long complicated UUIDs.
- _**Lack of easy ordering:**_ The primary key cannot be used to determine the order of insertion, another column with timestamps or a sequence becomes necessary. This also means that clustered indexes on UUID columns are very costly in terms of performance.
- _**Performance:**_ And also, random UUIDs mean scattered indexes, and this can cause performance issues/high disk usage with high insert rates. (Databases have sequential UUID generation functions to deal with this, but this eliminates the advantage of being able to generate the UUID on the client)

_**UUIDs in the Hasura GraphQL Engine:**_

Hasura GraphQL Engine (HGE) is an open source tool that gives instant GraphQL APIs over Postgres.

HGE supports UUID types as implicits, which means that UUID values can be provided as strings.

Let’s set up a simple schema with a UUID column to run some queries on:

{% gist https://gist.github.com/paranoidsp/c9d58e9e24d25af16e0ff79dbecfef5f.js %}

Here’s a sample GraphQL query that fetches a specific object from a table with a UUID primary key:

{% gist https://gist.github.com/paranoidsp/efbf340503746357849bcfceda063ca4.js %}

Inserting an object with a UUID generated using a client side library is quite simple too:

{% gist https://gist.github.com/paranoidsp/29f440315bcfda345e3e17e64d351e96.js %}

You can try out all the above examples on a live HGE instance [here](https://postgres-uuid-hge.herokuapp.com/console?query_file=https://raw.githubusercontent.com/hasura/graphql-example-apps/master/postgres-uuid-hge/queries.graphql)!

References:

- [Hasura GraphQL Engine Docs](https://docs.hasura.io)
- [Postgres Docs](https://www.postgresql.org/docs/current/static/datatype-uuid.html)