Skip to content

pg_xid is a globally unique id generator thought for the web

License

Notifications You must be signed in to change notification settings

iCyberon/pg_xid

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

pg_xid - globally Unique ID Generator for PostgreSQL

pg_xid is a globally unique ID generator extension for PostgreSQL.

It is using Mongo Object ID algorithm to generate globally unique ids: https://docs.mongodb.org/manual/reference/object-id/

  • 4-byte value representing the seconds since the Unix epoch,
  • 3-byte machine identifier,
  • 2-byte process id, and
  • 3-byte counter, starting with a random value.

The binary representation of the id is compatible with Mongo 12 bytes Object IDs.

UUIDs are 16 bytes (128 bits) and 36 chars as string representation. Twitter Snowflake ids are 8 bytes (64 bits) but require machine/data-center configuration and/or central generator servers. xid stands in between with 12 bytes (96 bits). No configuration or central generator server is required so it can be used directly.

Name Binary Size Features
UUID 16 bytes configuration free, not sortable
shortuuid 16 bytes configuration free, not sortable
Snowflake 8 bytes needs machin/DC configuration, needs central server, sortable
MongoID 12 bytes configuration free, sortable
xid 12 bytes configuration free, sortable

Features:

  • Size: 12 bytes (96 bits), smaller than UUID, larger than snowflake
  • Non configured, you don't need set a unique machine and/or data center id
  • K-ordered
  • Embedded time with 1 second precision
  • Unicity guaranteed for 16,777,216 (24 bits) unique ids per second and per host/process
  • Lock-free (i.e.: unlike UUIDv1 and v2)

References:

Inspired by Olivier Poitrey's Xid.

Build

make
make install

Install

 CREATE EXTENSION pg_xid;

Usage

SELECT xid(); -- returns: BYTEA

SELECT encode(xid(),'hex'); -- returns: TEXT

Licenses

All source code is licensed under the MIT License.

About

pg_xid is a globally unique id generator thought for the web

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Sponsor this project

Packages

No packages published