How it works...

Our first two steps aren't all that interesting; we merely create the shard schema and a sequence named table_id_seq for the IDs needed for value increments. Our design saves on implementation complexity using the same sequence for every table within a shard, but this is not a requirement.

The bulk of the work is defined in the next_unique_id function we create. We start the function with the epoch variable, set to the beginning of 2016. This is an arbitrary starting date and could have been any date in the past. The important thing to remember is that this value is used as a baseline for how long the IDs will remain unique.

Next, we have this section of code:

epoch_ms = floor( 
  extract(EPOCH FROM epoch) * 1000 
); 

The extract ...

Get PostgreSQL High Availability Cookbook - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.