snowflake
Snowflake-style 64-bit ID generator and sequence utilities for PostgreSQL
Repository
pgEdge/snowflake
https://github.com/pgEdge/snowflake
Source
snowflake-2.4.tar.gz
snowflake-2.4.tar.gz
Overview
| Package | Version | Category | License | Language |
|---|---|---|---|---|
snowflake | 2.4 | FUNC | PostgreSQL | C |
| ID | Extension | Bin | Lib | Load | Create | Trust | Reloc | Schema |
|---|---|---|---|---|---|---|---|---|
| 4590 | snowflake | No | Yes | No | Yes | No | No | snowflake |
| Related | spock lolor |
|---|
works on pgedge kernel fork. Set snowflake.node (1..1023) before using snowflake.nextval().
Version
| Type | Repo | Version | PG Ver | Package | Deps |
|---|---|---|---|---|---|
| EXT | PIGSTY | 2.4 | 1817161514 | snowflake | - |
| RPM | PIGSTY | 2.4 | 1817161514 | snowflake_$v | pgedge_$v |
| DEB | PIGSTY | 2.4 | 1817161514 | pgedge-$v-snowflake | pgedge-$v |
| OS / PG | PG18 | PG17 | PG16 | PG15 | PG14 |
|---|---|---|---|---|---|
| el8.x86_64 | PIGSTY MISS | PIGSTY 2.4 el8.x86_64.pg17 : snowflake_17 snowflake_17-2.4-1PIGSTY.el8.x86_64.rpm
| PIGSTY MISS | PIGSTY MISS | PIGSTY MISS |
| el8.aarch64 | PIGSTY MISS | PIGSTY 2.4 el8.aarch64.pg17 : snowflake_17 snowflake_17-2.4-1PIGSTY.el8.aarch64.rpm
| PIGSTY MISS | PIGSTY MISS | PIGSTY MISS |
| el9.x86_64 | PIGSTY MISS | PIGSTY 2.4 el9.x86_64.pg17 : snowflake_17 snowflake_17-2.4-1PIGSTY.el9.x86_64.rpm
| PIGSTY MISS | PIGSTY MISS | PIGSTY MISS |
| el9.aarch64 | PIGSTY MISS | PIGSTY 2.4 el9.aarch64.pg17 : snowflake_17 snowflake_17-2.4-1PIGSTY.el9.aarch64.rpm
| PIGSTY MISS | PIGSTY MISS | PIGSTY MISS |
| el10.x86_64 | PIGSTY MISS | PIGSTY 2.4 el10.x86_64.pg17 : snowflake_17 snowflake_17-2.4-1PIGSTY.el10.x86_64.rpm
| PIGSTY MISS | PIGSTY MISS | PIGSTY MISS |
| el10.aarch64 | PIGSTY MISS | PIGSTY 2.4 el10.aarch64.pg17 : snowflake_17 snowflake_17-2.4-1PIGSTY.el10.aarch64.rpm
| PIGSTY MISS | PIGSTY MISS | PIGSTY MISS |
| d12.x86_64 | PIGSTY MISS | PIGSTY 2.4 d12.x86_64.pg17 : pgedge-17-snowflake pgedge-17-snowflake_2.4-1PIGSTY~bookworm_amd64.deb
| PIGSTY MISS | PIGSTY MISS | PIGSTY MISS |
| d12.aarch64 | PIGSTY MISS | PIGSTY 2.4 d12.aarch64.pg17 : pgedge-17-snowflake pgedge-17-snowflake_2.4-1PIGSTY~bookworm_arm64.deb
| PIGSTY MISS | PIGSTY MISS | PIGSTY MISS |
| d13.x86_64 | PIGSTY MISS | PIGSTY 2.4 d13.x86_64.pg17 : pgedge-17-snowflake pgedge-17-snowflake_2.4-1PIGSTY~trixie_amd64.deb
| PIGSTY MISS | PIGSTY MISS | PIGSTY MISS |
| d13.aarch64 | PIGSTY MISS | PIGSTY 2.4 d13.aarch64.pg17 : pgedge-17-snowflake pgedge-17-snowflake_2.4-1PIGSTY~trixie_arm64.deb
| PIGSTY MISS | PIGSTY MISS | PIGSTY MISS |
| u22.x86_64 | PIGSTY MISS | PIGSTY 2.4 u22.x86_64.pg17 : pgedge-17-snowflake pgedge-17-snowflake_2.4-1PIGSTY~jammy_amd64.deb
| PIGSTY MISS | PIGSTY MISS | PIGSTY MISS |
| u22.aarch64 | PIGSTY MISS | PIGSTY 2.4 u22.aarch64.pg17 : pgedge-17-snowflake pgedge-17-snowflake_2.4-1PIGSTY~jammy_arm64.deb
| PIGSTY MISS | PIGSTY MISS | PIGSTY MISS |
| u24.x86_64 | PIGSTY MISS | PIGSTY 2.4 u24.x86_64.pg17 : pgedge-17-snowflake pgedge-17-snowflake_2.4-1PIGSTY~noble_amd64.deb
| PIGSTY MISS | PIGSTY MISS | PIGSTY MISS |
| u24.aarch64 | PIGSTY MISS | PIGSTY 2.4 u24.aarch64.pg17 : pgedge-17-snowflake pgedge-17-snowflake_2.4-1PIGSTY~noble_arm64.deb
| PIGSTY MISS | PIGSTY MISS | PIGSTY MISS |
Build
You can build the RPM / DEB packages for snowflake using pig build:
pig build pkg snowflake # build RPM / DEB packages
Install
You can install snowflake directly. First, make sure the PGDG and PIGSTY repositories are added and enabled:
pig repo add pgsql -u # Add repo and update cache
Install the extension using pig or apt/yum/dnf:
pig install snowflake; # Install for current active PG version
pig ext install -y snowflake -v 17 # PG 17
dnf install -y snowflake_17 # PG 17
apt install -y pgedge-17-snowflake # PG 17
Create Extension:
CREATE EXTENSION snowflake;
Usage
Provides int8 and sequence based unique ID generation using the Snowflake format, suitable for distributed systems.
CREATE EXTENSION snowflake;
Configuration
Set the node identifier in postgresql.conf (required, values 1-1023):
snowflake.node = 1
Functions
| Function | Description |
|---|---|
snowflake.nextval([sequence regclass]) | Generate the next Snowflake ID (uses internal sequence if none specified) |
snowflake.currval([sequence regclass]) | Return the current value of the sequence |
snowflake.get_epoch(snowflake int8) | Extract the timestamp as epoch (seconds since 2023-01-01) |
snowflake.get_count(snowflake int8) | Extract the count part (resets per millisecond) |
snowflake.get_node(snowflake int8) | Extract the node identifier |
snowflake.format(snowflake int8) | Return a JSONB with node, ts, and count fields |
Examples
-- Generate a snowflake ID
SELECT snowflake.nextval();
-- 136169504773242881
-- Use with a named sequence
CREATE SEQUENCE orders_id_seq;
SELECT snowflake.nextval('orders_id_seq'::regclass);
-- Extract components
SELECT snowflake.get_epoch(136169504773242881);
-- 1704996539.845
SELECT to_timestamp(snowflake.get_epoch(136169504773242881));
-- 2024-01-11 13:08:59.845-05
SELECT snowflake.get_node(136169504773242881);
-- 1
SELECT snowflake.format(136169504773242881);
-- {"id": 1, "ts": "2024-01-11 13:08:59.845-05", "count": 0}
-- Use as default column
CREATE TABLE orders (
id int8 DEFAULT snowflake.nextval() PRIMARY KEY,
data text
);
Feedback
Was this page helpful?
Thanks for the feedback! Please let us know how we can improve.
Sorry to hear that. Please let us know how we can improve.