This is the multi-page printable view of this section.
Snowflake
- 1: Changelog
- 2: Converting a PostgreSQL Sequence to a Snowflake Sequence
- 3: Creating a Snowflake Sequence in a New Cluster
- 4: Reviewing Sequence Definitions in your Table Meta-data
- 5: Snowflake Functions
Upstream reference for Snowflake 2.6.1. Mimus 19.0.1 integrates Spock 5.0.15. Compatibility of Snowflake with Mimus is unverified.
The English source is v2.6.1, pinned at 9506e1bd5c1f. A Chinese edition of this snapshot is available. Check versions and sources before deploying.
Using a Snowflake sequence allows you to take full advantage of the benefits offered by a distributed, multi-master replication solution. Snowflake is a Postgres extension that provides an int8 and sequence based unique ID solution to optionally replace the Postgres built-in bigserial data type.
Internally, snowflakes are 64 bit integers represented externally as bigint values. The 64 bits are divided into bit fields:
- bit 63 - unused (sign of int8)
- bits 22-62 - timestamp with millisecond precision
- bits 12-21 - unique pgEdge Distributed Postgres node number
- bits 0-11 - counter within one millisecond
- The timestamp is a 41-bit unsigned value representing millisecond precision and an epoch of 2023-01-01.
- The counter is a 12-bit unsigned value that increments per ID allocation. This provides for 4096 unique IDs per millisecond, or 4 million IDs per second.
- The node number is a 10-bit unique identifier for the Postgres instance inside a global cluster. This value is set with the
snowflake.nodeGUC in thepostgresql.conffile.
Snowflake sequences let you:
- add or modify data in different regions while ensuring a unique transaction sequence.
- preserve unique transaction identifiers without manual/administrative management of a numbering scheme.
- compare ID generation times across nodes. Clock skew and the interval between ID generation and commit can affect the observed order.
Snowflake sequences also alleviate concerns that network lag could disrupt sequences in distributed transactions.
A Snowflake sequence is made up of a timestamp, a counter, and a unique node identifier; these components are sized to ensure over 18 trillion unique sequence numbers. The extension supports versatile commands and functions that simplify creating a new Snowflake sequence or converting an existing sequence.
Continue reading
- Changelog
- Converting a PostgreSQL Sequence to a Snowflake Sequence
- Creating a Snowflake Sequence in a New Cluster
- Reviewing Sequence Definitions in your Table Meta-data
- Snowflake Functions
Upstream source · 9506e1bd5c1f · Copyright (c) 2023–2026, pgEdge, Inc.; portions Copyright the PostgreSQL Global Development Group and The Regents of the University of California. PostgreSQL License.
1 - Changelog
Upstream reference for Snowflake 2.6.1. Mimus 19.0.1 integrates Spock 5.0.15. Compatibility of Snowflake with Mimus is unverified.
All notable changes to the Snowflake extension are documented here.
2.6.1
Changed
- Version bump to 2.6.1 (no catalog changes). Includes release packaging and CI updates since 2.6.0.
2.6.0
Added
- Added support for Postgres 19
2.5.0
Added
- PG18 support: added a PG18 test configuration and adjusted the Docker build to skip LLVM bitcode compilation on PG18.
Changed
- Adopted three-digit (
MAJOR.MINOR.PATCH) versioning. The extension version is now2.5.0.
Fixed
- Fixed dump/restore of snowflake-converted sequences.
convert_sequence_to_snowflake()previously leftMAXVALUEat(old last_value + 1), butsnowflake.nextval()bypassesMAXVALUEand writes snowflake-sized values, sopg_dumpcould emit asetval()that failed to restore. Sequences are now converted withAS bigint MAXVALUE 9223372036854775807, and sequences converted by earlier versions are auto-repaired on upgrade. (SUP-140) - Fixed the SQL command used to convert sequences to Snowflake.
Security / CI
- Pinned GitHub Actions to full commit SHAs to harden against supply chain attacks.
- Removed
shell=Trueand shell-injection risks from test runner and helper subprocess calls; useshlex.split()for correct argument tokenization. - Fixed a SQL injection warning and a missing parameter in
execute_sqlite_query, and suppressed Bandit B603 warnings on internal test helper subprocess calls. - Fixed the Dockerfile
ARGhandling for newer BuildKit versions. - Updated the copyright year to 2026.
Upstream source · 9506e1bd5c1f · Copyright (c) 2023–2026, pgEdge, Inc.; portions Copyright the PostgreSQL Global Development Group and The Regents of the University of California. PostgreSQL License.
2 - Converting a PostgreSQL Sequence to a Snowflake Sequence
Upstream reference for Snowflake 2.6.1. Mimus 19.0.1 integrates Spock 5.0.15. Compatibility of Snowflake with Mimus is unverified.
You cannot directly create a snowflake sequence; you must first create a sequence in Postgres and then convert it. Sequences are created explicitly with the CREATE SEQUENCE command but are also implicitly created by GENERATED { ALWAYS | BY DEFAULT } AS IDENTITY table columns or SERIAL and BIGSERIAL columns. GENERATED AS IDENTITY is the recommended ANSI SQL method.
You can use the snowflake.convert_sequence_to_snowflake() function to convert individual sequences to snowflake sequences. Note that this converts the sequence definition; existing values in a sequence column will not change. The command syntax in SQL is:
`SELECT snowflake.convert_sequence_to_snowflake('sequence_name');`
Where
sequence_name is the name of a sequence.
There are a number of ways to return a list of sequences. One way is to use the Postgres psql client to execute the following command:
Another way is to query metadata catalog information directly in psql or another SQL editor.
The second command will include data type information, which is useful for learning which sequences will be converted to the BIGINT data type (64 bits).
Example: Converting an Existing Sequence
The example that follows demonstrates using the psql command line to convert a sequence; in this example, we are using a table named orders that has three columns; the last column is a sequence named id:
After creating the table, we insert data into the orders table. We only need to provide content for the first two columns, since the sequence definition keeps track of the value of the third column and adds it as needed:
When we select the rows from our table, we can see the sequence numbers in the id column:
To convert the sequence definition for the orders table to use Snowflake sequences, invoke the following command in psql or another SQL editor:
The conversion process modifies the sequence definition to use Snowflake sequences, but does not update existing rows. If we reconnect with psql and add new rows to the table, the new row’s id will be a Snowflake sequence:
In the query results that follows, you can see the unformatted sequence values in the id column, and the same information in the format column, formatted with the snowflake.format(id) function. The rows added before the conversion to Snowflake sequences show a fixed timestamp of 2022-12-31 19:00:00-05, while the Snowflake sequences have a unique id and timestamp.
Original entries in the table display a Postgres sequence, while entries made after the conversion display Snowflake sequences:
INCREMENT and Caching
Within the same millisecond, snowflake will allow a maximum of 4096 values. Some frameworks like the ORM framework Hibernate may be configured to try to fetch a sequence value, cache and assign a range by using an INCREMENT value for the sequence. Snowflake sequences honor the increment value, but you should not use a value greater than 4096. If you would like to change the increment value, use ALTER SEQUENCE <seq_name> INCREMENT <increment_value> NO MAXVALUE.
Upstream source · 9506e1bd5c1f · Copyright (c) 2023–2026, pgEdge, Inc.; portions Copyright the PostgreSQL Global Development Group and The Regents of the University of California. PostgreSQL License.
3 - Creating a Snowflake Sequence in a New Cluster
Upstream reference for Snowflake 2.6.1. Mimus 19.0.1 integrates Spock 5.0.15. Compatibility of Snowflake with Mimus is unverified.
Before you use Snowflake sequences, each node in your cluster must have a unique snowflake.node identifier. The value ranges from 1 to 1023, and it must be distinct on every node in the cluster.
Setting snowflake.node in a managed deployment
A pgEdge deployment assigns snowflake.node for you, deriving the value from each node’s place in the cluster:
- the Control Plane sets
snowflake.nodefrom the node ordinal. - pgEdge Helm charts derive
snowflake.nodefrom each node name. - pgEdge Ansible playbooks set
snowflake.nodefrom the node zone.
When you deploy with one of these methods, no further action is needed to configure the node identifier.
Setting snowflake.node manually
For a self-managed or from-source installation, set the value yourself. Use ALTER SYSTEM to assign a unique node number, then reload the server so the change takes effect. The following example sets the node identifier to 10:
You can also add the parameter to the postgresql.conf file directly. The following line identifies the host as node 11:
After you edit postgresql.conf, reload the server for the change to take effect. You can reload from psql:
You can also reload the server with pg_ctl:
Once Snowflake is configured, you can begin to use Snowflake sequences by first converting them from Postgres sequences.
Upstream source · 9506e1bd5c1f · Copyright (c) 2023–2026, pgEdge, Inc.; portions Copyright the PostgreSQL Global Development Group and The Regents of the University of California. PostgreSQL License.
4 - Reviewing Sequence Definitions in your Table Meta-data
Upstream reference for Snowflake 2.6.1. Mimus 19.0.1 integrates Spock 5.0.15. Compatibility of Snowflake with Mimus is unverified.
If you query the table meta-data before performing a sequence conversion with the \d+ orders command, the PostgreSQL sequence is defined in the Default column:
After performing a conversion, the \d+ table_name command shows a Default value of snowflake.nextval('orders_id_seq'::regclass):
Upstream source · 9506e1bd5c1f · Copyright (c) 2023–2026, pgEdge, Inc.; portions Copyright the PostgreSQL Global Development Group and The Regents of the University of California. PostgreSQL License.
5 - Snowflake Functions
Upstream reference for Snowflake 2.6.1. Mimus 19.0.1 integrates Spock 5.0.15. Compatibility of Snowflake with Mimus is unverified.
You can query the following Snowflake functions at the psql command line to return useful information about tables that use Snowflake sequences.
snowflake.nextval([sequence regclass])
Generates the next Snowflake in the specified sequence. If no sequence is specified, the internal, database-wide sequence snowflake.id_seq is used. For example, the following query returns the next number in the orders_id_seq sequence:
snowflake.currval([sequence regclass])
Returns the current value of the specified sequence. This value is undefined until the function snowflake.nextval() has been called for the sequence in the current session. For example, the following query returns the current value of the orders_id_seq sequence:
snowflake.get_epoch(snowflake int8) Returns the Unix timestamp in seconds as a NUMERIC with three decimal places. Snowflake IDs store milliseconds relative to 2023-01-01 UTC. The function adds the offset from the Unix epoch before returning the value. The example returns 1704996539.845:
You can use the to_timestamp(snowflake.get_epoch(<value>)) function to convert the epoch into a timestamp. For example:
In v2.6.1, bits 12–21 of 136169504773242881 encode node 0 and the low 12 bits encode count 1. For this ID, get_node() returns 0, get_count() returns 1, and format() reports "id": 0 and "count": 1. The upstream transcripts below show different values. Pinned bit layout
snowflake.get_count(snowflake int8) Returns the count part of the given Snowflake as int4 value; count resets to 0 for each new millisecond. For example:
snowflake.get_node(snowflake int8) Returns the node number encoded in the ID. It reflects the snowflake.node setting at allocation time. Decoding does not read the issuing host’s configuration file. The historical upstream transcript follows.
snowflake.format(snowflake int8) Returns a jsonb object of the given Snowflake in the form: {"id": 1, "ts": "2023-10-16 17:57:26.361+00", "count": 0}. For example:
Upstream source · 9506e1bd5c1f · Copyright (c) 2023–2026, pgEdge, Inc.; portions Copyright the PostgreSQL Global Development Group and The Regents of the University of California. PostgreSQL License.