snowflake

snowflake

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

Overview

IDExtensionPackageVersionCategoryLicenseLanguage
4590
snowflake
snowflake
2.4
FUNC
PostgreSQL
C
AttributeHas BinaryHas LibraryNeed LoadHas DDLRelocatableTrusted
--s-d--
No
Yes
No
Yes
no
no
Relationships
Schemassnowflake
See Also
spock
lolor

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

Packages

TypeRepoVersionPG Major CompatibilityPackage PatternDependencies
EXT
PIGSTY
2.4
18
17
16
15
14
snowflake-
RPM
PIGSTY
2.4
18
17
16
15
14
snowflake_$vpgedge_$v
DEB
PIGSTY
2.4
18
17
16
15
14
pgedge-$v-snowflakepgedge-$v
Linux / PGPG18PG17PG16PG15PG14
el8.x86_64
MISS
PIGSTY 2.4
MISS
MISS
MISS
el8.aarch64
MISS
PIGSTY 2.4
MISS
MISS
MISS
el9.x86_64
MISS
PIGSTY 2.4
MISS
MISS
MISS
el9.aarch64
MISS
PIGSTY 2.4
MISS
MISS
MISS
el10.x86_64
MISS
PIGSTY 2.4
MISS
MISS
MISS
el10.aarch64
MISS
PIGSTY 2.4
MISS
MISS
MISS
d12.x86_64
MISS
PIGSTY 2.4
MISS
MISS
MISS
d12.aarch64
MISS
PIGSTY 2.4
MISS
MISS
MISS
d13.x86_64
MISS
PIGSTY 2.4
MISS
MISS
MISS
d13.aarch64
MISS
PIGSTY 2.4
MISS
MISS
MISS
u22.x86_64
MISS
PIGSTY 2.4
MISS
MISS
MISS
u22.aarch64
MISS
PIGSTY 2.4
MISS
MISS
MISS
u24.x86_64
MISS
PIGSTY 2.4
MISS
MISS
MISS
u24.aarch64
MISS
PIGSTY 2.4
MISS
MISS
MISS

Source

pig build pkg snowflake;		# build rpm/deb

Install

Make sure PGDG and PIGSTY repo available:

pig repo add pgsql -u   # add both repo and update cache

Install this extension with pig:

pig install snowflake;		# install via package name, for the active PG version

pig install snowflake -v 17;   # install for PG 17

Create this extension with:

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 updated on