snowflake
snowflake : Snowflake-style 64-bit ID generator and sequence utilities for PostgreSQL
Overview
| ID | Extension | Package | Version | Category | License | Language |
|---|---|---|---|---|---|---|
| 4590 | snowflake | snowflake | 2.6.0 |
FUNC | PostgreSQL | C |
| Attribute | Has Binary | Has Library | Need Load | Has DDL | Relocatable | Trusted |
|---|---|---|---|---|---|---|
| --s-d-- | No | Yes | No | Yes | no | no |
| Relationships | |
|---|---|
| Schemas | snowflake |
| See Also | pg_uuidv7 sequential_uuids pg_idkit pgx_ulid pg_uuid_v8 uuid-ossp typeid permuteseq |
Packages
| Type | Repo | Version | PG Major Compatibility | Package Pattern | Dependencies |
|---|---|---|---|---|---|
| EXT | PIGSTY | 2.6.0 |
18 17 16 15 14 | snowflake |
- |
| RPM | PIGSTY | 18.6 |
18 17 16 15 14 | pgedge-$v |
- |
| DEB | PIGSTY | 18.6 |
18 17 16 15 14 | pgedge-$v |
- |
| Linux / PG | PG18 | PG17 | PG16 | PG15 | PG14 |
|---|---|---|---|---|---|
| el8.x86_64 | PIGSTY 18.6 | PIGSTY 17.11 | PIGSTY 16.15 | PIGSTY 15.19 | N/A |
| el8.aarch64 | PIGSTY 18.6 | PIGSTY 17.11 | PIGSTY 16.15 | PIGSTY 15.19 | N/A |
| el9.x86_64 | PIGSTY 18.6 | PIGSTY 17.11 | PIGSTY 16.15 | PIGSTY 15.19 | N/A |
| el9.aarch64 | PIGSTY 18.6 | PIGSTY 17.11 | PIGSTY 16.15 | PIGSTY 15.19 | N/A |
| el10.x86_64 | PIGSTY 18.6 | PIGSTY 17.11 | PIGSTY 16.15 | PIGSTY 15.19 | N/A |
| el10.aarch64 | PIGSTY 18.6 | PIGSTY 17.11 | PIGSTY 16.15 | PIGSTY 15.19 | N/A |
| d12.x86_64 | PIGSTY 18.6 | PIGSTY 17.11 | PIGSTY 16.15 | PIGSTY 15.19 | N/A |
| d12.aarch64 | PIGSTY 18.6 | PIGSTY 17.11 | PIGSTY 16.15 | PIGSTY 15.19 | N/A |
| d13.x86_64 | PIGSTY 18.6 | PIGSTY 17.11 | PIGSTY 16.15 | PIGSTY 15.19 | N/A |
| d13.aarch64 | PIGSTY 18.6 | PIGSTY 17.11 | PIGSTY 16.15 | PIGSTY 15.19 | N/A |
| u22.x86_64 | PIGSTY 18.6 | PIGSTY 17.11 | PIGSTY 16.15 | PIGSTY 15.19 | N/A |
| u22.aarch64 | PIGSTY 18.6 | PIGSTY 17.11 | PIGSTY 16.15 | PIGSTY 15.19 | N/A |
| u24.x86_64 | PIGSTY 18.6 | PIGSTY 17.11 | PIGSTY 16.15 | PIGSTY 15.19 | N/A |
| u24.aarch64 | PIGSTY 18.6 | PIGSTY 17.11 | PIGSTY 16.15 | PIGSTY 15.19 | N/A |
| u26.x86_64 | PIGSTY 18.6 | PIGSTY 17.11 | PIGSTY 16.15 | PIGSTY 15.19 | N/A |
| u26.aarch64 | PIGSTY 18.6 | PIGSTY 17.11 | PIGSTY 16.15 | PIGSTY 15.19 | N/A |
Source
github.com/pgEdge/snowflake
snowflake-2.6.0.tar.gz
Install
Make sure PGDG and PIGSTY repo available:
Install this extension with pig:
Create this extension with:
Usage
Sources:
- snowflake v2.6.0 README
- Creating a Snowflake sequence
- Converting PostgreSQL sequences
- Function reference
- v2.6.0 changelog
- v2.6.0 C implementation
- Tagged SQL API definitions
- v2.6.0 control file
snowflake generates distributed bigint identifiers from a timestamp, a per-node identifier, and an in-millisecond counter. Existing PostgreSQL sequences can be converted so table defaults continue using nextval(...) while producing Snowflake IDs.
Configuration
Assign every writable node a distinct identifier in postgresql.conf, then reload PostgreSQL:
Reusing a node identifier on concurrently writable servers can generate duplicate IDs.
Convert a Sequence
Create a normal PostgreSQL sequence, then convert its definition. Existing values in referencing columns are not rewritten.
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) |
Return Unix epoch seconds since 1970-01-01, with millisecond precision |
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 JSONB fields id (node identifier), ts, and count |
Examples
Review and Upgrade
Review converted table defaults with psql \d+ or the PostgreSQL catalogs. They should call snowflake.nextval(...) rather than the original nextval(...):
Version 2.5.0 fixes dump/restore of converted sequences whose MAXVALUE was left at the normal bigint maximum. It also repairs conversion SQL for affected sequences and adds PostgreSQL 18 support.
Caveats
- Conversion changes the sequence definition, not IDs already stored in table rows.
- A Snowflake generator can emit at most 4096 counter values per millisecond. Do not configure a sequence increment above 4096.
- Keep the node identifier stable and unique for the lifetime of concurrent writers; record it as part of cluster provisioning and failover procedures.
- Install the same extension version on every node before logical replication or rolling changes involving converted sequence definitions.
Version 2.6.0 adds PostgreSQL 19 support; the current Pigsty pgEdge bundle covers PostgreSQL 15–18. Assign snowflake.node within 1–1023; its default is intentionally invalid. The control file fixes schema snowflake and does not require shared preload. The 2.5.0 dump/restore repair remains relevant when upgrading converted sequences from earlier releases.
Use an administrator to install the C extension and convert identity definitions. ID generation requires USAGE or UPDATE on the selected sequence; the current value is defined only after that sequence has generated an ID in the same session.