What you will be able to do
- Explain what a Snowflake sequence guarantees (unique values) and what it does not (no gap-free or ordered values)
- Identify sequence limitations: no CURRVAL, interval sign changes, the 64-bit range and delayed ALTER SEQUENCE effects
- Use a sequence as a column default and predict what happens when the sequence is dropped
- Post-process a previous statement's output with RESULT_SCAN(LAST_QUERY_ID())
- Cancel one query, every query in a session, or every query of a user
Key concept
Unique, not gap-free — A Snowflake sequence promises that no two callers, even concurrent ones, ever receive the same value. It does not promise that the values are contiguous or that they arrive in the order rows were loaded.
1.What a sequence guarantees, and what it does not
A sequence is a schema object that hands out unique numbers across sessions and statements, including statements running at the same moment. Teams use one for surrogate primary keys, or for any column whose values must never collide. The guarantee is about uniqueness only. Concurrent queries never see the same value, and every value within a single query is distinct.
Gaps are built into how values are handed out. Each generated value also reserves a block sized by the sequence's step, and a reserved value is never handed out while the step stays the same. Snowflake may also work out the next value as soon as the current one is used. Holes in an id column after a few concurrent loads are therefore normal behaviour. They are not a sign of rollback trouble or lost rows.
| Step (INCREMENT) | Values reserved |
|---|---|
| 2 | 100 and 101 |
| 10 | 100 to 109 |
| -5 | 96 to 100 |
Ordering is narrower still. A new statement gets values greater than the previous statement's maximum only when two things are both true: the sequence lacks the NOORDER property, and the previous statement had completed and been acknowledged before the new one was submitted. NOORDER drops the increasing-order promise, so a START 1 INCREMENT 2 sequence might produce 1, 3, 101, 5, 103. It can improve performance when many clients insert concurrently. Overlapping loads from several sessions fail the second condition, so their ids interleave whatever the property says. The only way to assign values to rows in a chosen order is single-row statements, and even those can leave gaps.
Checkpoint 1 of 9· Check yourself
A sequence was created without the NOORDER property. Session 1 starts a large INSERT ... SELECT seq1.NEXTVAL load. While that load is still running, session 2 submits its own load against the same sequence. What can you rely on for the ids that session 2 receives?
Lacking NOORDER is only one of the two conditions for increasing values. The previous statement must also have completed and been acknowledged before the new one is submitted, so overlapping loads can interleave. Uniqueness still holds, and no sequence promises gap-free values.
“The previous statement completed, and an acknowledgment was received, prior to submitting the current statement.”Source: docs.snowflake.com
Checkpoint 2 of 9· Exam question
An ETL team loads order rows with INSERT INTO orders (id, item) SELECT seq1.NEXTVAL, item FROM staging; from several concurrent sessions. Auditors notice that id values contain holes and do not always ascend in load order. The team asks for the MOST accurate explanation of this behavior.
Correct answer: A — Sequences guarantee unique values but not gap-free or ordered numbers, so skipped values under concurrent statements are expected.
- A. Correct: Snowflake guarantees uniqueness of generated values but explicitly does not guarantee gap-free or ordered numbers, especially under concurrency, so holes are normal.
- B. Incorrect: ORDER is the property that requests ordered values and it is not the cause of holes; the claim about per-cluster private blocks is not how sequences behave.
- C. Incorrect: sequence values are never rolled back by failed statements; consumed values are simply lost, which is a source of gaps rather than a partial rollback.
- D. Incorrect: Time Travel retention of old rows has no effect on sequence value consumption, so it cannot explain the missing ids.
Sources1
2.Sequence limitations: interval changes, range and no CURRVAL
Most sequence surprises come from four limitations. Each one breaks a habit carried over from another database.
Flipping the sign of the interval can produce duplicates. Suppose a sequence has returned 1, 2 and 3 and you change the increment from 1 to -1. The next values include 2 and 1 again, because uniqueness only holds while the sign of the step stays the same. Snowflake keeps no history of issued values to protect you.
ALTER SEQUENCE ... SET INCREMENT may not take effect right away. Snowflake may have computed the next value already, so the very next operation can still use the old step.
The range is finite. A sequence value is a 64-bit two's complement integer, from -2^63 to 2^63 - 1. If the next value's internal representation leaves that range, the query fails with an error and those values may be lost. Numbers never wrap around. Because of gaps, the internal value can overflow even when every returned value is in range. The fix is a smaller increment or a new sequence with a smaller start. A sequence with start 1 and increment 1 is extremely unlikely to run out.
There is no CURRVAL. In PostgreSQL or Oracle, people often insert a parent row with NEXTVAL and then read CURRVAL to stamp child rows. A Snowflake script written that way fails. Snowflake prefers bulk statements to that single-row pattern. The same parent-child linking is done with a multi-table INSERT and sequence references in nested subqueries, or by letting a column default generate the key.
Every occurrence of seq1.NEXTVAL in a query also produces its own values, so SELECT seq1.NEXTVAL a, seq1.NEXTVAL b FROM DUAL returns two different numbers. To give two columns the same value, generate it once in a subquery and refer to it twice.
SELECT seqRef.a a, seqRef.a b FROM (SELECT seq1.NEXTVAL a FROM DUAL) seqRef;The GETNEXTVAL table function avoids the extra nesting. It is a one-row table function that must be aliased, and its alias exposes a NEXTVAL attribute that many columns can read. Where it sits in the FROM clause matters. Values are generated over the join of every object listed before it, and joins cannot be reordered around it. Snowflake recommends placing it at the end of the FROM clause where possible.
Checkpoint 3 of 9· Fill the gap
A migration from Oracle needs two columns of the same row to hold the same generated key. Which expression completes the second column?
SELECT seqRef.a a, ? b FROM (SELECT seq1.NEXTVAL a FROM DUAL) seqRef;Every occurrence of seq1.NEXTVAL generates its own value, and Snowflake has no CURRVAL. The way to reuse one value is to generate it once in the subquery and reference its alias column twice.
Source: docs.snowflake.comCheckpoint 4 of 9· Check yourself
An engineer runs ALTER SEQUENCE seq1 SET INCREMENT = 10 on a sequence that used a step of 1. The very next insert still receives a value that is only 1 higher than the last one. What is the correct explanation?
Snowflake can compute the next sequence value as soon as the current one is used, so a SET INCREMENT change might not affect the next operation. This is expected behaviour, not a failed ALTER.
“A consequence of this is that an ALTER SEQUENCE ... SET INCREMENT ... command might not affect the next operation that uses the sequence.”Source: docs.snowflake.com
Checkpoint 5 of 9· Exam question
A developer migrating from PostgreSQL writes a Snowflake script that inserts a parent row using seq_ord.NEXTVAL and then fetches the value just used with SELECT seq_ord.CURRVAL. Select TWO statements about Snowflake sequences that apply to this design.(Select 2)
Correct answers: A, B — Snowflake has no CURRVAL equivalent, so the script fails and must capture the generated value in another way, such as through a table default.; Sequence values are 64-bit integers, so a query that exceeds the range fails rather than silently wrapping around to earlier numbers.
- A. Correct: Snowflake does not support CURRVAL, so a script that relies on it fails and must obtain the generated value some other way.
- B. Correct: sequence values are 64-bit two's-complement integers, and exceeding that range causes query failure instead of wrapping.
- C. Incorrect: Snowflake pre-calculates values, so an
ALTER SEQUENCEmay not affect the next call and typically takes effect after one further use. - D. Incorrect: uniqueness holds only while the sign of the interval stays the same; flipping from positive to negative can reproduce previously issued values.
- E. Incorrect: a sequence is a single schema-level object shared by all sessions, which is exactly why its values are unique across them.
Sources1
3.Using a sequence as a column default
The simplest way to generate keys is to make seq.NEXTVAL the column's default expression. Any insert that leaves the column out, or sets it to DEFAULT, gets a fresh value. This also covers the parent-key need that CURRVAL serves elsewhere.
CREATE OR REPLACE TABLE foo (k NUMBER DEFAULT seq1.NEXTVAL, v NUMBER); -- insert rows with unique keys (generated by seq1) and explicit values INSERT INTO foo (v) VALUES (100); INSERT INTO foo VALUES (DEFAULT, 101);A default is only a fallback. INSERT INTO foo VALUES (1000, 1001) stores 1000 exactly as given, and the sequence does nothing to stop that number clashing with one it issues later. Because the sequence is a separate object, one sequence can be the default for several columns, even across tables, and they will all draw from the same unique stream.
That independence works against you if the sequence is dropped. The table keeps its default expression, and every insert or update that relies on the default then fails with an error saying the identifier cannot be found.
It fails. The insert leaves k out, so Snowflake evaluates the default seq1.NEXTVAL, and the identifier no longer exists.
Checkpoint 6 of 9· Fill the gap
The second INSERT names every column of foo but should still get its key from seq1. Which token goes in the blank?
CREATE OR REPLACE TABLE foo (k NUMBER DEFAULT seq1.NEXTVAL, v NUMBER); -- insert rows with unique keys (generated by seq1) and explicit values INSERT INTO foo (v) VALUES (100); INSERT INTO foo VALUES ( ? , 101);The column default seq1.NEXTVAL runs only when the insert leaves k out or sets it to DEFAULT. NULL or 0 would be stored as explicit values. Snowflake has no CURRVAL.
Source: docs.snowflake.comSources1
4.Post-processing persisted results with RESULT_SCAN
Snowflake keeps every query result for 24 hours, whatever its size. It reuses that result only when the query text matches exactly, contains no non-reusable functions such as RANDOM, and reads table data that has not changed; Result cache (persisted query results) covers the reuse rules in full. The 100 KB threshold you may see applies only to the security token for large results, which expires after 6 hours. It has nothing to do with whether a result is stored. The reuse conditions concern the query text, its functions, the underlying data, the role's privileges and result-affecting configuration. None of them concerns the warehouse size.
You can also query a stored result directly. RESULT_SCAN returns an earlier result as a table, and LAST_QUERY_ID() points it at the statement you just ran. This is how you filter output that SQL cannot otherwise reach. SHOW, DESCRIBE and CALL all return results you cannot embed in a larger query. A stored procedure, for example, cannot be called inside a SELECT the way a function can. You can also build a complex query in steps, putting a new layer on the previous result without recomputing it.
SELECT "schema_name", "name" as "table_name", "rows" FROM table(RESULT_SCAN(LAST_QUERY_ID())) WHERE "rows" = 0;The pipe operator ->> does the same job without displaying the first command's output. Either way, the stored result must still exist. Each reuse resets the 24-hour retention, but only up to 31 days after the query first ran, and after that the result is purged.
Checkpoint 7 of 9· Fill the gap
Right after SHOW TABLES, you want only the empty tables. You can't put SHOW in a larger query, so its stored result has to be read as a table. Which function goes in the blank?
SELECT "schema_name", "name" as "table_name", "rows" FROM table( ? (LAST_QUERY_ID())) WHERE "rows" = 0;RESULT_SCAN returns a stored result as a table, so a new query can filter it. LAST_QUERY_ID() points it at the statement that just ran. GETNEXTVAL produces sequence values and does not read stored results.
Source: docs.snowflake.comCheckpoint 8 of 9· Exam question
A BI analyst reruns an identical, expensive aggregation on a large table several times during the morning and expects instant results. The administrator wants to confirm which condition would force Snowflake to ignore the persisted result and recompute. Select TWO.(Select 2)
Correct answers: A, B — The query text includes a call to RANDOM(), a function whose result is not reusable, so the persisted result is bypassed.; Rows were inserted into the underlying table after the first run, so the stored result no longer reflects the current data.
- A. Correct: non-reusable functions such as RANDOM or UUID_STRING make the query ineligible for persisted-result reuse, so it executes again.
- B. Correct: reuse requires that the underlying table data has not changed; new inserted rows invalidate the stored result.
- C. Incorrect: running again from the same worksheet is fine; persisted results are reusable across sessions as long as the other conditions hold.
- D. Incorrect: result reuse does not depend on warehouse size, and a persisted result can even be returned without a running warehouse.
- E. Incorrect: results of all sizes are persisted for 24 hours; only the access token handling differs for results larger than 100 KB.
Sources2
5.Cancelling one query, one session or one user
Cancel from the place the query is running wherever you can. In Snowsight that is the worksheet, and in code it is the ODBC or JDBC driver's cancellation API. SQL cancellation is for cases where that interface is out of reach, such as a runaway job started by another client. What you pick depends on how wide the cancellation should be.
| Scope | Statement | What it targets |
|---|---|---|
| One query | SYSTEM$CANCEL_QUERY | A single running statement |
| One session | SYSTEM$CANCEL_ALL_QUERIES | All running statements in the session whose identifier you pass |
| One user | ALTER USER ... ABORT ALL QUERIES | Every query the user has running or scheduled, on any warehouse |
Snowflake's own JDBC sample shows the session-level function at work. It reads the session id with CURRENT_SESSION(), starts a 120-second generator query, and five seconds later passes that id to SYSTEM$CANCEL_ALL_QUERIES. The query then fails with the query-cancelled SQL state.
ABORT ALL QUERIES clears everything the user has running, but it does not lock them out. The user can still sign in and start new queries a moment later. If a compromised or misbehaving account must stop entirely, use ALTER USER ... SET DISABLED = TRUE. That aborts the running and scheduled queries and also blocks new logins and new queries.
Checkpoint 9 of 9· Check yourself
Several runaway statements are running in one session that a batch client opened, and you can't reach that client's interface. You have the session identifier. The same user also has other sessions doing legitimate work that must keep running. Which SQL option fits?
SYSTEM$CANCEL_ALL_QUERIES takes a session identifier and cancels every running statement in that session. SYSTEM$CANCEL_QUERY targets only one statement. ABORT ALL QUERIES and DISABLED = TRUE act on everything the user has running, on any warehouse, so they would also kill the user's other sessions.
“This task uses the session identifier as a parameter to SYSTEM$CANCEL_ALL_QUERIES.”Source: docs.snowflake.com
Sources
(4)
Every claim above is drawn from one of these pages, quoted as it was written on the date shown.
- 1.
“Concurrent queries never observe the same value, and values within a single query are always distinct.”
↩︎ What a sequence guarantees, and what it does not“NOORDER specifies that the values are not guaranteed to be in increasing order.”
↩︎ What a sequence guarantees, and what it does not“A reserved value is never generated by the sequence as long as the step/interval is never modified.”
↩︎ What a sequence guarantees, and what it does not“Many databases provide a currval sequence reference; however, Snowflake does not.”
↩︎ Sequence limitations: interval changes, range and no CURRVAL“If the internal representation of a sequence’s next value exceeds this range (in either direction) an error results and the query fails.”
↩︎ Sequence limitations: interval changes, range and no CURRVAL“Changing the sequence interval from positive to negative (e.g. from 1 to -1), or vice versa may result in duplicates.”
↩︎ Sequence limitations: interval changes, range and no CURRVAL“A call to GETNEXTVAL must be aliased; otherwise, the generated values cannot be referenced.”
↩︎ Sequence limitations: interval changes, range and no CURRVAL“setting the value to DEFAULT in an insert or update statement will generate a new sequence value for the row.”
↩︎ Using a sequence as a column default“any attempt to insert/update the table using the default value will result in an error saying the identifier cannot be found.”
↩︎ Using a sequence as a column default“Snowflake does not guarantee generating sequence numbers without gaps.”
↩︎ Key concept“There is no guarantee that values from a sequence are contiguous (gap-free) or that the sequence values are assigned in a particular order.”
↩︎ Exam trap 1“an ALTER SEQUENCE ... SET INCREMENT ... command might not affect the next operation that uses the sequence.”
↩︎ Exam trap 2“The previous statement completed, and an acknowledgment was received, prior to submitting the current statement.”
↩︎ Checkpoint“A consequence of this is that an ALTER SEQUENCE ... SET INCREMENT ... command might not affect the next operation that uses the sequence.”
↩︎ Checkpoint - 2.
“You can perform post-processing by using the RESULT_SCAN table function.”
↩︎ Post-processing persisted results with RESULT_SCAN“UUID_STRING, RANDOM, and RANDSTR are good examples of non-reusable functions.”
↩︎ Post-processing persisted results with RESULT_SCAN“The table data contributing to the query result has not changed.”
↩︎ Post-processing persisted results with RESULT_SCAN“For persisted query results of all sizes, the cache expires after 24 hours.”
↩︎ Exam trap 3 - 3.
“The recommended way to cancel a statement is to use the interface of the application in which the query is running”
↩︎ Cancelling one query, one session or one user“This task uses the session identifier as a parameter to SYSTEM$CANCEL_ALL_QUERIES.”
↩︎ Cancelling one query, one session or one user - 4.
“Aborts all the queries and other SQL statements currently running or scheduled by the user, regardless of the warehouse”
↩︎ Cancelling one query, one session or one user“Note that the user can still log into Snowflake and initiate new queries.”
↩︎ Exam trap 4