snowflake

Snowflake-style 64-bit ID generator and sequence utilities for PostgreSQL

Overview

PackageVersionCategoryLicenseLanguage
snowflake2.4FUNCPostgreSQLC
IDExtensionBinLibLoadCreateTrustRelocSchema
4590snowflakeNoYesNoYesNoNosnowflake
Relatedspock lolor

works on pgedge kernel fork. Set snowflake.node (1..1023) before using snowflake.nextval().

Version

TypeRepoVersionPG VerPackageDeps
EXTPIGSTY2.41817161514snowflake-
RPMPIGSTY2.41817161514snowflake_$vpgedge_$v
DEBPIGSTY2.41817161514pgedge-$v-snowflakepgedge-$v
OS / PGPG18PG17PG16PG15PG14
el8.x86_64PIGSTY MISSPIGSTY MISSPIGSTY MISSPIGSTY MISS
el8.aarch64PIGSTY MISSPIGSTY MISSPIGSTY MISSPIGSTY MISS
el9.x86_64PIGSTY MISSPIGSTY MISSPIGSTY MISSPIGSTY MISS
el9.aarch64PIGSTY MISSPIGSTY MISSPIGSTY MISSPIGSTY MISS
el10.x86_64PIGSTY MISSPIGSTY MISSPIGSTY MISSPIGSTY MISS
el10.aarch64PIGSTY MISSPIGSTY MISSPIGSTY MISSPIGSTY MISS
d12.x86_64PIGSTY MISSPIGSTY MISSPIGSTY MISSPIGSTY MISS
d12.aarch64PIGSTY MISSPIGSTY MISSPIGSTY MISSPIGSTY MISS
d13.x86_64PIGSTY MISSPIGSTY MISSPIGSTY MISSPIGSTY MISS
d13.aarch64PIGSTY MISS
PIGSTY 2.4
PIGSTY MISSPIGSTY MISSPIGSTY MISS
u22.x86_64PIGSTY MISS
PIGSTY 2.4
PIGSTY MISSPIGSTY MISSPIGSTY MISS
u22.aarch64PIGSTY MISS
PIGSTY 2.4
PIGSTY MISSPIGSTY MISSPIGSTY MISS
u24.x86_64PIGSTY MISS
PIGSTY 2.4
PIGSTY MISSPIGSTY MISSPIGSTY MISS
u24.aarch64PIGSTY MISS
PIGSTY 2.4
PIGSTY MISSPIGSTY MISSPIGSTY 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

snowflake: Snowflake ID sequences for PostgreSQL

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

FunctionDescription
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
);

Last Modified 2026-03-12: add pg extension catalog (95749bf)