Snowflake SQL Tutorial 110 - Dynamic Tables Take the Query a Materialized View Refuses

Snowflake SQL · Episode 110

In this post we are going to cover dynamic tables in Snowflake.

A dynamic table is a query you save, like a view, except that it holds rows. You write the query you actually wanted and you declare how stale the answer is allowed to be. You never write a refresh. Snowflake works out when to run it and what to change.

We will start by taking a join that a materialized view refused in Episode 109 and building it as a dynamic table instead. Then we will ask the catalog what sort of object we have just made, try to write to it, let it go out of date on purpose, and refresh it by hand.

Every query below was run on a real Snowflake account, and every result you see is the output it gave back.

Episode 110 of the Snowflake SQL: Zero to Hero series on TechBrothersIT.

Let's create some sample data

We need two tables. One holds orders. The other holds a manager for each region, so that we have something to join to.

Copy this into a Snowflake worksheet and run it once.

Sample data
-- Sample data for this post.
-- Two small tables: orders, and a manager for each region.
USE DATABASE DEMO_DB;
USE SCHEMA PUBLIC;

CREATE OR REPLACE TABLE ORDERS_T110 (
    ORDER_ID  INT,
    REGION    VARCHAR(20),
    CUSTOMER  VARCHAR(30),
    AMOUNT    NUMBER(10,2)
);

INSERT INTO ORDERS_T110 (ORDER_ID, REGION, CUSTOMER, AMOUNT)
SELECT 1, 'north', 'Ana',  100.00 UNION ALL
SELECT 2, 'north', 'Ben',  250.00 UNION ALL
SELECT 3, 'south', 'Cleo',  75.50 UNION ALL
SELECT 4, 'south', 'Dan',  310.00 UNION ALL
SELECT 5, 'east',  'Eve',   40.00 UNION ALL
SELECT 6, 'east',  'Finn',  60.00;

CREATE OR REPLACE TABLE REGIONS_T110 (
    REGION   VARCHAR(20),
    MANAGER  VARCHAR(30)
);

INSERT INTO REGIONS_T110 (REGION, MANAGER)
SELECT 'north', 'Nadia' UNION ALL
SELECT 'south', 'Sam'   UNION ALL
SELECT 'east',  'Eli';

Let's look at the orders first.

Query
SELECT ORDER_ID, REGION, CUSTOMER, AMOUNT FROM ORDERS_T110 ORDER BY ORDER_ID;
Result6 rows · 4 columns
ORDER_IDREGIONCUSTOMERAMOUNT
1northAna100.00
2northBen250.00
3southCleo75.50
4southDan310.00
5eastEve40.00
6eastFinn60.00

Six orders across three regions, with an amount on each one. Nothing unusual about it.

And the regions.

Query
SELECT REGION, MANAGER FROM REGIONS_T110 ORDER BY REGION;
Result3 rows · 2 columns
REGIONMANAGER
eastEli
northNadia
southSam

Three regions and a manager for each. Nadia has the north, Sam has the south, Eli has the east.

Let's build the join a materialized view refused

In Episode 109 we tried to save this exact join as a materialized view and Snowflake would not have it. A materialized view reads from one table, and this join reads from two.

So let's write the same join as a dynamic table. There are two things to declare that a view does not ask for: a TARGET_LAG, which is how stale we will let the answer be, and a WAREHOUSE, which is what does the refreshing.

Query
CREATE OR REPLACE DYNAMIC TABLE DT_JOINED_T110
  TARGET_LAG = '1440 minutes'
  WAREHOUSE = COMPUTE_WH
AS
SELECT O.ORDER_ID, O.REGION, R.MANAGER, O.AMOUNT
FROM ORDERS_T110 O
JOIN REGIONS_T110 R ON R.REGION = O.REGION;
Result1 row · 1 column
status
Dynamic table DT_JOINED_T110 successfully created.

Created. Same join, no refusal, different kind of object.

Now let's read from it straight away, with nothing refreshed and nothing waited for.

Query
SELECT ORDER_ID, REGION, MANAGER, AMOUNT FROM DT_JOINED_T110 ORDER BY ORDER_ID;
Result6 rows · 4 columns
ORDER_IDREGIONMANAGERAMOUNT
1northNadia100.00
2northNadia250.00
3southSam75.50
4southSam310.00
5eastEli40.00
6eastEli60.00

It already has rows. Ana and Ben came back under Nadia, Cleo and Dan under Sam, Eve and Finn under Eli. All six orders, the moment the object existed.

Why the lag is so long

A TARGET_LAG of '1440 minutes' is one day. Everything in this post uses a lag of a day so that nothing here is waiting on a clock. Every refresh we do is asked for by hand.

Now let's try the other shapes it refused

A materialized view also turned down a window function in Episode 109. Let's write that one as a dynamic table.

Query
CREATE OR REPLACE DYNAMIC TABLE DT_RANKED_T110
  TARGET_LAG = '1440 minutes'
  WAREHOUSE = COMPUTE_WH
AS
SELECT ORDER_ID, REGION, AMOUNT,
       ROW_NUMBER() OVER (PARTITION BY REGION ORDER BY AMOUNT DESC) AS RN
FROM ORDERS_T110;
Result1 row · 1 column
status
Dynamic table DT_RANKED_T110 successfully created.

Accepted. And it is populated too.

Query
SELECT ORDER_ID, REGION, AMOUNT, RN FROM DT_RANKED_T110
ORDER BY REGION, RN;
Result6 rows · 4 columns
ORDER_IDREGIONAMOUNTRN
6east60.001
5east40.002
2north250.001
1north100.002
4south310.001
3south75.502

Six rows, with a rank inside each region. The highest amount in each region got RN of 1.

And one object holding two common table expressions and a union

Let's write one more. This one has two WITH clauses and a UNION ALL between them, which is a shape nobody expects a saved query to take.

Query
CREATE OR REPLACE DYNAMIC TABLE DT_UNIONED_T110
  TARGET_LAG = '1440 minutes'
  WAREHOUSE = COMPUTE_WH
AS
WITH BIG AS (SELECT ORDER_ID, AMOUNT FROM ORDERS_T110 WHERE AMOUNT >= 100),
     SMALL AS (SELECT ORDER_ID, AMOUNT FROM ORDERS_T110 WHERE AMOUNT < 100)
SELECT ORDER_ID, AMOUNT, 'big' AS SIZE_BAND FROM BIG
UNION ALL
SELECT ORDER_ID, AMOUNT, 'small' AS SIZE_BAND FROM SMALL;
Result1 row · 1 column
status
Dynamic table DT_UNIONED_T110 successfully created.
Query
SELECT ORDER_ID, AMOUNT, SIZE_BAND FROM DT_UNIONED_T110 ORDER BY ORDER_ID;
Result6 rows · 3 columns
ORDER_IDAMOUNTSIZE_BAND
1100.00big
2250.00big
375.50small
4310.00big
540.00small
660.00small

Six rows again, each one labelled big or small.

So let's count all of them in one row.

Query
SELECT (SELECT COUNT(*) FROM DT_JOINED_T110)   AS ROWS_FROM_A_JOIN,
       (SELECT COUNT(*) FROM DT_RANKED_T110)   AS ROWS_FROM_A_WINDOW,
       (SELECT COUNT(*) FROM DT_UNIONED_T110)  AS ROWS_FROM_A_UNION_AND_CTE;
Result1 row · 3 columns
ROWS_FROM_A_JOINROWS_FROM_A_WINDOWROWS_FROM_A_UNION_AND_CTE
666

Three dynamic tables, six rows in each. The third column is named for both the union and the common table expressions, because those two are in the same object — DT_UNIONED_T110 is one statement that has both.

What really happened in Episode 109

The spoken version of this episode calls these four shapes a materialized view refused, and the grid above has three columns. Two things are worth getting straight. Three dynamic tables were built, and the union and the common table expressions share the third one. And of these shapes, the ones a materialized view actually refused in Episode 109 were the join and the window function. A UNION ALL and a WITH clause were both accepted as materialized views in that episode, which was the surprise of it.

So what does the catalog think these are?

We have three dynamic tables and two ordinary tables in the schema now. Let's ask INFORMATION_SCHEMA.TABLES about all five of them at once.

Query
SELECT TABLE_NAME, TABLE_TYPE, IS_INSERTABLE_INTO, ROW_COUNT
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'PUBLIC' AND RIGHT(TABLE_NAME, 5) = '_T110'
ORDER BY TABLE_NAME;
Result5 rows · 4 columns
TABLE_NAMETABLE_TYPEIS_INSERTABLE_INTOROW_COUNT
DT_JOINED_T110BASE TABLENO6
DT_RANKED_T110BASE TABLENO6
DT_UNIONED_T110BASE TABLENO6
ORDERS_T110BASE TABLEYES6
REGIONS_T110BASE TABLEYES3

Every one of them is a BASE TABLE. The dynamic tables are not listed as views and they are not listed as anything of their own. They are tables.

And they have a ROW_COUNT, which a view never has, because a view holds nothing to count.

Look at the column in the middle. IS_INSERTABLE_INTO says NO for the three dynamic tables and YES for ORDERS_T110 and REGIONS_T110. In this grid that is the only thing that tells them apart.

Now let's ask SHOW DYNAMIC TABLES instead

SHOW DYNAMIC TABLES knows a great deal more than the catalog view does. Here is the whole thing.

Query
SHOW DYNAMIC TABLES IN SCHEMA PUBLIC;
Result3 rows · 31 columns · scroll →
Scroll the grid sideways. This is every column SHOW DYNAMIC TABLES returns.
created_onnamedatabase_nameschema_namecluster_byrowsbytesownertarget_lagrefresh_moderefresh_mode_reasonwarehousecommenttextautomatic_clusteringscheduling_statelast_suspended_onis_cloneis_replicais_icebergdata_timestampowner_role_typeexecute_as_usersecondary_role_namesinsert_only_inputsimmutable_whereinitialization_warehousebackfill_fromschedulerfrozen_whereconfigured_refresh_mode
2026-09-28 18:26:01.695000-07:00DT_JOINED_T110DEMO_DBPUBLIC61536ACCOUNTADMIN1 dayINCREMENTALNULLCOMPUTE_WHCREATE OR REPLACE DYNAMIC TABLE DT_JOINED_T110 lag = '1 day' refresh_mode = 'AUTO' initialize = 'ON_CREATE' warehouse = COMPUTE_WH AS SELECT O.ORDER_ID, O.REGION, R.MANAGER, O.AMOUNT FROM ORDERS_T110 O JOIN REGIONS_T110 R ON R.REGION = O.REGIONOFFACTIVENULLfalsefalsefalse2026-09-28 18:26:02.712000-07:00ROLENULLNULLfalseNULLNULLNULLNULLAUTO
2026-09-28 18:26:04.462000-07:00DT_RANKED_T110DEMO_DBPUBLIC61536ACCOUNTADMIN1 dayINCREMENTALNULLCOMPUTE_WHCREATE OR REPLACE DYNAMIC TABLE DT_RANKED_T110 lag = '1 day' refresh_mode = 'AUTO' initialize = 'ON_CREATE' warehouse = COMPUTE_WH AS SELECT ORDER_ID, REGION, AMOUNT, ROW_NUMBER() OVER (PARTITION BY REGION ORDER BY AMOUNT DESC) AS RN FROM ORDERS_T110OFFACTIVENULLfalsefalsefalse2026-09-28 18:26:04.969000-07:00ROLENULLNULLfalseNULLNULLNULLNULLAUTO
2026-09-28 18:26:06.378000-07:00DT_UNIONED_T110DEMO_DBPUBLIC61536ACCOUNTADMIN1 dayINCREMENTALNULLCOMPUTE_WHCREATE OR REPLACE DYNAMIC TABLE DT_UNIONED_T110 lag = '1 day' refresh_mode = 'AUTO' initialize = 'ON_CREATE' warehouse = COMPUTE_WH AS WITH BIG AS (SELECT ORDER_ID, AMOUNT FROM ORDERS_T110 WHERE AMOUNT >= 100), SMALL AS (SELECT ORDER_ID, AMOUNT FROM ORDERS_T110 WHERE AMOUNT < 100) SELECT ORDER_ID, AMOUNT, 'big' AS SIZE_BAND FROM BIG UNION ALL SELECT ORDER_ID, AMOUNT, 'small' AS SIZE_BAND FROM SMALLOFFACTIVENULLfalsefalsefalse2026-09-28 18:26:06.866000-07:00ROLENULLNULLfalseNULLNULLNULLNULLAUTO

That is a lot to read, so let's pull out the columns that matter. We can do that because the output of the last query is available again through RESULT_SCAN, which is the subject of the next post.

Query
SELECT "name", "target_lag", "refresh_mode", "scheduling_state", "rows"
FROM TABLE(RESULT_SCAN(LAST_QUERY_ID()))
WHERE "name" LIKE '%T110'
ORDER BY "name";
Result3 rows · 5 columns
nametarget_lagrefresh_modescheduling_staterows
DT_JOINED_T1101 dayINCREMENTALACTIVE6
DT_RANKED_T1101 dayINCREMENTALACTIVE6
DT_UNIONED_T1101 dayINCREMENTALACTIVE6

target_lag says 1 day, which is exactly what we declared, written back to us in different words. scheduling_state says ACTIVE, so Snowflake is willing to refresh them.

And refresh_mode says INCREMENTAL. That means Snowflake has worked out how to apply only what changed, rather than running the whole query again.

But we never asked for incremental

Let's look at what Snowflake stored when it created the first one.

Query
SELECT GET_DDL('TABLE', 'DT_JOINED_T110') AS THE_STORED_DEFINITION;
Result1 row · 1 column
THE_STORED_DEFINITION
create or replace dynamic table DT_JOINED_T110( ORDER_ID, REGION, MANAGER, AMOUNT ) target_lag = '1 day' refresh_mode = AUTO initialize = ON_CREATE warehouse = COMPUTE_WH as SELECT O.ORDER_ID, O.REGION, R.MANAGER, O.AMOUNT FROM ORDERS_T110 O JOIN REGIONS_T110 R ON R.REGION = O.REGION;

Two words in there we never typed. refresh_mode = AUTO is what we asked for by saying nothing, and INCREMENTAL is what AUTO turned into. And initialize = ON_CREATE is why the table had rows the moment it existed.

So one column tells you what you asked for and another tells you what you got, and neither of them is a word you wrote.

Can we write to it? It is called a table

The catalog calls it a base table, so the obvious thing to try is writing to it. Let's insert one row.

Refused
INSERT INTO DT_JOINED_T110 (ORDER_ID, REGION, MANAGER, AMOUNT)
SELECT 99, 'west', 'Wes', 1.00;
Query failed — Snowflake returned an error
002706 (23001): SQL Compilation error: Invalid Operation INSERT on Dynamic Table

Invalid Operation INSERT on Dynamic Table. Let's try an update.

Refused
UPDATE DT_JOINED_T110 SET AMOUNT = 0 WHERE ORDER_ID = 1;
Query failed — Snowflake returned an error
002706 (23001): SQL Compilation error: Invalid Operation UPDATE on Dynamic Table

The same sentence with a different verb. A delete, then.

Refused
DELETE FROM DT_JOINED_T110 WHERE ORDER_ID = 1;
Query failed — Snowflake returned an error
002706 (23001): SQL Compilation error: Invalid Operation DELETE on Dynamic Table

And a truncate, which does not name a single row.

Refused
TRUNCATE TABLE DT_JOINED_T110;
Query failed — Snowflake returned an error
002706 (23001): SQL Compilation error: Invalid Operation TRUNCATE on Dynamic Table

Four write statements, four refusals, the same error code and the same sentence every time. Only the verb changes.

And nothing got through.

Query
SELECT COUNT(*) AS ROWS_STILL_PRESENT FROM DT_JOINED_T110;
Result1 row · 1 column
ROWS_STILL_PRESENT
6

What is in a dynamic table is the query's business. It is not yours, and this is where IS_INSERTABLE_INTO of NO stops being a detail in a catalog view.

Now let's let it fall behind on purpose

This is the part that makes a dynamic table different from a view. A view computes every time you read it, so it is never out of date. A dynamic table holds rows, so it can be.

First let's write down where it stands.

Query
SELECT COUNT(*) AS ROWS_BEFORE, SUM(AMOUNT) AS TOTAL_BEFORE FROM DT_JOINED_T110;
Result1 row · 2 columns
ROWS_BEFORETOTAL_BEFORE
6835.50

Hold on to those two numbers. Now let's add one more order to the base table.

Query
INSERT INTO ORDERS_T110 (ORDER_ID, REGION, CUSTOMER, AMOUNT)
SELECT 7, 'north', 'Gus', 1000.00;
Result1 row · 1 column
number of rows inserted
1
Query
SELECT COUNT(*) AS ROWS_IN_THE_BASE_TABLE, SUM(AMOUNT) AS TOTAL_IN_THE_BASE
FROM ORDERS_T110;
Result1 row · 2 columns
ROWS_IN_THE_BASE_TABLETOTAL_IN_THE_BASE
71835.50

The base table has grown and so has its total. Now let's ask the dynamic table the same question.

Query
SELECT COUNT(*) AS ROWS_IN_THE_DYNAMIC_TABLE, SUM(AMOUNT) AS TOTAL_IN_IT
FROM DT_JOINED_T110;
Result1 row · 2 columns
ROWS_IN_THE_DYNAMIC_TABLETOTAL_IN_IT
6835.50

It has not moved. Same count, same total as before the insert.

Here they are side by side.

Query
SELECT (SELECT COUNT(*) FROM ORDERS_T110)     AS BASE_ROWS,
       (SELECT COUNT(*) FROM DT_JOINED_T110)  AS DYNAMIC_TABLE_ROWS;
Result1 row · 2 columns
BASE_ROWSDYNAMIC_TABLE_ROWS
76

Seven in the base table, six in the dynamic table. It is behind, and it is allowed to be behind, because we said the answer could be a day old. A view would have said seven.

Let's refresh it by hand and watch what it reports

We do not have to wait for the clock. We can ask for a refresh, and the statement hands back its own report of what it did.

Query
ALTER DYNAMIC TABLE DT_JOINED_T110 REFRESH;
Result1 row · 4 columns
dt_namestatisticsrefreshed_dt_countdata_timestamp
DEMO_DB.PUBLIC.DT_JOINED_T110{"insertedRows":1,"copiedRows":0,"deletedRows":0}12026-09-28 18:26:12.874000-07:00

Read the statistics column. One row inserted, nothing copied, nothing deleted. It did not rebuild the table, it applied the one change. And refreshed_dt_count says one object was refreshed, which will matter later on.

Now let's read the table again.

Query
SELECT ORDER_ID, REGION, MANAGER, AMOUNT FROM DT_JOINED_T110 ORDER BY ORDER_ID;
Result7 rows · 4 columns
ORDER_IDREGIONMANAGERAMOUNT
1northNadia100.00
2northNadia250.00
3southSam75.50
4southSam310.00
5eastEli40.00
6eastEli60.00
7northNadia1000.00

The seventh order is there, with Nadia against it, worked out by the join.

Query
SELECT (SELECT COUNT(*) FROM ORDERS_T110)     AS BASE_ROWS,
       (SELECT COUNT(*) FROM DT_JOINED_T110)  AS DYNAMIC_TABLE_ROWS;
Result1 row · 2 columns
BASE_ROWSDYNAMIC_TABLE_ROWS
77

Caught up, because we asked it to be.

What happens when we suspend one?

A dynamic table can have its lag changed and can be stopped. Let's do both to the ranked one and leave the other two alone.

Query
ALTER DYNAMIC TABLE DT_RANKED_T110 SET TARGET_LAG = '2880 minutes';
Result1 row · 1 column
status
Statement executed successfully.
Query
ALTER DYNAMIC TABLE DT_RANKED_T110 SUSPEND;
Result1 row · 1 column
status
Statement executed successfully.

Now let's see what that did. We run the SHOW again so that it is the last statement, and then read three columns out of it.

Run this first
SHOW DYNAMIC TABLES IN SCHEMA PUBLIC;
Query
SELECT "name", "target_lag", "scheduling_state"
FROM TABLE(RESULT_SCAN(LAST_QUERY_ID()))
WHERE "name" LIKE '%T110'
ORDER BY "name";
Result3 rows · 3 columns
nametarget_lagscheduling_state
DT_JOINED_T1101 dayACTIVE
DT_RANKED_T1102 daysSUSPENDED
DT_UNIONED_T1101 dayACTIVE

DT_RANKED_T110 says SUSPENDED, and its lag now reads 2 days. The other two are still ACTIVE on a lag of a day.

So can we still read a suspended dynamic table?

Query
SELECT ORDER_ID, REGION, RN FROM DT_RANKED_T110 ORDER BY REGION, RN;
Result6 rows · 3 columns
ORDER_IDREGIONRN
6east1
5east2
2north1
1north2
4south1
3south2

Yes. All the rows it was holding are still there and still readable. Suspend stops the refreshing. It does not stop the reading.

Let's start it again before we move on.

Query
ALTER DYNAMIC TABLE DT_RANKED_T110 RESUME;
Result1 row · 1 column
status
Statement executed successfully.
Query
SELECT COUNT(*) AS ROWS_PRESENT FROM DT_RANKED_T110;
Result1 row · 1 column
ROWS_PRESENT
6

Now a dynamic table on top of a dynamic table

This is the best thing in the episode. A dynamic table can read from another dynamic table, and when it does it can declare its lag as DOWNSTREAM instead of a length of time. DOWNSTREAM means: keep up with whoever needs me.

Let's total the orders per manager, reading from the joined dynamic table rather than from the base table.

Query
CREATE OR REPLACE DYNAMIC TABLE DT_BY_MANAGER_T110
  TARGET_LAG = DOWNSTREAM
  WAREHOUSE = COMPUTE_WH
AS
SELECT MANAGER, COUNT(*) AS ORDERS, SUM(AMOUNT) AS TOTAL
FROM DT_JOINED_T110
GROUP BY MANAGER;
Result1 row · 1 column
status
Dynamic table DT_BY_MANAGER_T110 successfully created.
Query
SELECT MANAGER, ORDERS, TOTAL FROM DT_BY_MANAGER_T110 ORDER BY MANAGER;
Result3 rows · 3 columns
MANAGERORDERSTOTAL
Eli2100.00
Nadia31350.00
Sam2385.50

A row per manager, with a count and a total. Nadia has the most, because the order we added earlier was hers.

And the catalog reports that lag literally. Same trick as before: run the SHOW, then read it.

Run this first
SHOW DYNAMIC TABLES IN SCHEMA PUBLIC;
Query
SELECT "name", "target_lag", "refresh_mode"
FROM TABLE(RESULT_SCAN(LAST_QUERY_ID()))
WHERE "name" LIKE '%T110'
ORDER BY "name";
Result4 rows · 3 columns
nametarget_lagrefresh_mode
DT_BY_MANAGER_T110DOWNSTREAMINCREMENTAL
DT_JOINED_T1101 dayINCREMENTAL
DT_RANKED_T1102 daysINCREMENTAL
DT_UNIONED_T1101 dayINCREMENTAL

DOWNSTREAM, in the same column that says 1 day for the others. Now let's add another order and refresh only the child.

Query
INSERT INTO ORDERS_T110 (ORDER_ID, REGION, CUSTOMER, AMOUNT)
SELECT 8, 'south', 'Hana', 500.00;
Result1 row · 1 column
number of rows inserted
1
Query
ALTER DYNAMIC TABLE DT_BY_MANAGER_T110 REFRESH;
Result1 row · 4 columns
dt_namestatisticsrefreshed_dt_countdata_timestamp
DEMO_DB.PUBLIC.DT_BY_MANAGER_T110{"insertedRows":1,"copiedRows":2,"deletedRows":1}22026-09-28 18:26:22.126000-07:00

Look at refreshed_dt_count. We named one object and two were refreshed. The child could not answer correctly without its parent, so it pulled the parent along with it.

The statistics are more interesting here too. One row inserted, two copied, one deleted — which is what changing a group total looks like when you apply it as a change rather than recomputing it.

Query
SELECT MANAGER, ORDERS, TOTAL FROM DT_BY_MANAGER_T110 ORDER BY MANAGER;
Result3 rows · 3 columns
MANAGERORDERSTOTAL
Eli2100.00
Nadia31350.00
Sam3885.50

Sam has gone up, because the order we added was in the south.

And now let's look at the parent, which we never touched.

Query
SELECT COUNT(*) AS PARENT_ROWS, SUM(AMOUNT) AS PARENT_TOTAL FROM DT_JOINED_T110;
Result1 row · 2 columns
PARENT_ROWSPARENT_TOTAL
82335.50
Query
SELECT (SELECT COUNT(*) FROM ORDERS_T110)        AS BASE_ROWS,
       (SELECT COUNT(*) FROM DT_JOINED_T110)     AS PARENT_ROWS,
       (SELECT SUM(ORDERS) FROM DT_BY_MANAGER_T110) AS ORDERS_THE_CHILD_COUNTS;
Result1 row · 3 columns
BASE_ROWSPARENT_ROWSORDERS_THE_CHILD_COUNTS
888

The base table, the parent dynamic table and the total the child counts all agree. One refresh, asked for in one place, and the whole chain moved.

What happens when we drop a table it depends on?

The joined dynamic table reads from two tables. Let's take one of them away.

Query
DROP TABLE REGIONS_T110;
Result1 row · 1 column
status
REGIONS_T110 successfully dropped.

Now let's see what is left in the catalog.

Query
SELECT TABLE_NAME, TABLE_TYPE
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'PUBLIC' AND RIGHT(TABLE_NAME, 5) = '_T110'
ORDER BY TABLE_NAME;
Result5 rows · 2 columns
TABLE_NAMETABLE_TYPE
DT_BY_MANAGER_T110BASE TABLE
DT_JOINED_T110BASE TABLE
DT_RANKED_T110BASE TABLE
DT_UNIONED_T110BASE TABLE
ORDERS_T110BASE TABLE

All four dynamic tables are still listed, and so is ORDERS_T110. The table we dropped is simply gone from the list. Dropping a source does not drop the objects built on it.

So can we still read the one whose source has gone?

Query
SELECT COUNT(*) AS ROWS_STILL_READABLE FROM DT_JOINED_T110;
Result1 row · 1 column
ROWS_STILL_READABLE
8

We can. It is holding rows, and rows do not need the source table to be read back.

Can we refresh it, though?

Refused
ALTER DYNAMIC TABLE DT_JOINED_T110 REFRESH;
Query failed — Snowflake returned an error
002742 (01000): SQL compilation error: Failed to refresh dynamic table with refresh_trigger MANUAL at data_timestamp 1790645187482 because of the error: SQL compilation error: Target table failed to refresh: SQL compilation error:
Failure during expansion of view 'DT_JOINED_T110': SQL compilation error:
Object 'DEMO_DB.PUBLIC.REGIONS_T110' does not exist or not authorized. Your primary role ACCOUNTADMIN must have at least one privilege granted on TABLE DEMO_DB.PUBLIC.REGIONS_T110. (Note: the dynamic table runs as owner role 'ACCOUNTADMIN'. Ensure this role has access to all referenced objects. See https://docs.snowflake.com/en/user-guide/dynamic-tables/troubleshoot-permissions for details.)

No. Failed to refresh dynamic table, and the reason underneath it is that the query cannot be expanded any more. The rows it already has are fine. It just cannot move.

Let's put the table back and try again.

Query
UNDROP TABLE REGIONS_T110;
Result1 row · 1 column
status
Table REGIONS_T110 successfully restored.
Query
ALTER DYNAMIC TABLE DT_JOINED_T110 REFRESH;
Result1 row · 4 columns
dt_namestatisticsrefreshed_dt_countdata_timestamp
DEMO_DB.PUBLIC.DT_JOINED_T110No new data02026-09-28 18:26:28.195000-07:00

This time it works, and the statistics column says No new data with nothing refreshed. Nothing changed while the table was away, so there was nothing to do.

Query
SELECT ORDER_ID, REGION, MANAGER, AMOUNT FROM DT_JOINED_T110 ORDER BY ORDER_ID;
Result8 rows · 4 columns
ORDER_IDREGIONMANAGERAMOUNT
1northNadia100.00
2northNadia250.00
3southSam75.50
4southSam310.00
5eastEli40.00
6eastEli60.00
7northNadia1000.00
8southSam500.00

Every order is there, each with its manager. The dynamic table kept what it had the whole time.

One last thing: let's tidy up

A dynamic table is dropped the same way as anything else, and the child goes first because it reads from the parent.

Cleanup
-- Put the schema back the way we found it.
DROP DYNAMIC TABLE IF EXISTS DT_BY_MANAGER_T110;
DROP DYNAMIC TABLE IF EXISTS DT_JOINED_T110;
DROP DYNAMIC TABLE IF EXISTS DT_RANKED_T110;
DROP DYNAMIC TABLE IF EXISTS DT_UNIONED_T110;
DROP TABLE IF EXISTS ORDERS_T110;
DROP TABLE IF EXISTS REGIONS_T110;
Query
SELECT CURRENT_DATABASE()                 AS THE_DATABASE,
       CURRENT_SCHEMA()                   AS THE_SCHEMA,
       (SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES
        WHERE RIGHT(TABLE_NAME, 5) = '_T110') AS PROBE_OBJECTS_LEFT,
       (SELECT COUNT(*) FROM CUSTOMERS)   AS DEMO_DB_IS_READABLE;
Result1 row · 4 columns
THE_DATABASETHE_SCHEMAPROBE_OBJECTS_LEFTDEMO_DB_IS_READABLE
DEMO_DBPUBLIC010

No probe objects left, and the demo database still reads. Back where we started.

Key points

  • A dynamic table takes query shapes a materialized view refuses. The join and the window function from Episode 109 were both accepted here.
  • You declare a TARGET_LAG, which is how stale the answer may be. You never write a refresh.
  • It is populated when it is created, because initialize = ON_CREATE is the default. Nothing had to be refreshed before the first read.
  • INFORMATION_SCHEMA.TABLES calls it a BASE TABLE and gives it a ROW_COUNT. IS_INSERTABLE_INTO of NO is the only column there that gives it away.
  • INSERT, UPDATE, DELETE and TRUNCATE are all refused with the same error, only the verb changing.
  • refresh_mode = AUTO is what you ask for by saying nothing. INCREMENTAL is what Snowflake chose, and SHOW DYNAMIC TABLES reports both.
  • It is allowed to be out of date, and you can watch it happen: the base table moved on and the dynamic table did not.
  • ALTER DYNAMIC TABLE ... REFRESH reports what it changed, row by row, in a statistics column.
  • SUSPEND stops the refreshing and not the reading. A suspended dynamic table still hands back everything it holds.
  • TARGET_LAG = DOWNSTREAM on a child means refreshing the child pulls its parent along with it, and refreshed_dt_count says so.
  • Drop a source table and the dynamic table keeps its rows and stops being able to refresh. UNDROP the source and it moves again.
  • Reach for one when the query is too complicated for a materialized view and an answer from an hour or a day ago is good enough.
Next in this series

Episode 111 — sequences. A sequence promises unique numbers and increasing numbers. It delivers one of those two, and SHOW SEQUENCES has a column that admits which.

Every query and every result grid on this page was captured from a live Snowflake account (version 10.34.101) running the demo script for this episode. The _T110 objects are created and dropped by the scripts above. Nothing on this page is timed, no refresh duration is quoted, and no performance claims are made anywhere in this series: the TARGET_LAG is a day throughout so that nothing waits on a clock, and every refresh is asked for by hand. Where the spoken episode and its own captured output disagree, this post follows the captures and says so. Dynamic table behaviour follows the Snowflake dynamic tables documentation.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.