Snowflake SQL Tutorial 131 - My Snowpipe Loaded Six Rows Twice And COPY_HISTORY Told Me Why

Snowflake SQL · Episode 131

In this post we are going to cover Snowpipe in Snowflake.

A pipe is a COPY INTO statement that Snowflake keeps for you and runs on its own schedule. You do not run the load. You put a file where the pipe is looking, and the pipe loads it.

A straight answer before we start. Half of Snowpipe needs cloud storage — S3, Azure or Google — and a notification integration to go with it. This was run on a trial account with no external stage, so that half is not reachable here and we are not going to pretend otherwise. We will do the half that is real and say clearly which half is which.

We will start with a stage and a table and build it up step by step. Every query below was run on a real Snowflake account, and every result you see is the output it gave back.

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

Let's create some sample data

A pipe needs somewhere to read from and somewhere to write to. So we need a stage, a file format, and an empty table for the rows to land in.

We also need a file. This post writes nothing from a laptop, so we build a second table and unload it to the stage when we are ready.

Sample data
-- Sample data for this post.
-- A stage, a CSV format, a landing table, and a table to make a file from.
USE DATABASE DEMO_DB;
USE SCHEMA PUBLIC;

CREATE OR REPLACE STAGE STAGE_T131;

CREATE OR REPLACE FILE FORMAT CSV_T131
    TYPE = CSV
    FIELD_OPTIONALLY_ENCLOSED_BY = '"'
    COMPRESSION = NONE;

CREATE OR REPLACE TABLE LANDING_T131 (
    SALE_ID INT,
    REGION  VARCHAR(10),
    AMOUNT  NUMBER(10,2)
);

CREATE OR REPLACE TABLE SOURCE_T131 (
    SALE_ID INT,
    REGION  VARCHAR(10),
    AMOUNT  NUMBER(10,2)
);

INSERT INTO SOURCE_T131 (SALE_ID, REGION, AMOUNT)
VALUES (1, 'north', 100.00), (2, 'north', 250.50),
       (3, 'south',  75.25), (4, 'south', 310.00),
       (5, 'east',   45.00), (6, 'east',   88.25);

Let's note down what the source table holds, so we have something to check the load against later.

Query
SELECT COUNT(*)    AS ROWS_IN_THE_SOURCE,
       SUM(AMOUNT) AS TOTAL_IN_THE_SOURCE
FROM SOURCE_T131;
Result1 row · 2 columns
ROWS_IN_THE_SOURCETOTAL_IN_THE_SOURCE
6869.00

Six rows, adding up to 869.00. Remember that total.

And the landing table
SELECT COUNT(*) AS ROWS_IN_THE_LANDING_TABLE FROM LANDING_T131;
Result1 row · 1 column
ROWS_IN_THE_LANDING_TABLE
0

Empty, which is where we want to start.

Let's create a pipe

CREATE PIPE, a name, the word AS, and then a COPY INTO statement. That is the whole object.

Query
CREATE OR REPLACE PIPE PIPE_T131 AS
COPY INTO LANDING_T131
FROM @STAGE_T131/in/
FILE_FORMAT = (FORMAT_NAME = 'CSV_T131');
Result1 row · 1 column
status
Pipe PIPE_T131 successfully created.

Created. Now let's ask Snowflake what it thinks it has.

SHOW PIPES
SHOW PIPES LIKE '%\\_T131' IN SCHEMA PUBLIC;
Result1 row · 15 columns · scroll →
created_onnamedatabase_nameschema_namedefinitionownernotification_channelcommentintegrationpatternerror_integrationowner_role_typeinvalid_reasonkindis_snowflake_managed
2026-10-01 13:18:14.529000-07:00PIPE_T131DEMO_DBPUBLICCOPY INTO LANDING_T131 FROM @STAGE_T131/in/ FILE_FORMAT = (FORMAT_NAME = 'CSV_T131')ACCOUNTADMINNULLNULLNULLNULLROLENULLSTAGEfalse

Fifteen columns, which is more than we need. Two things in there matter right now. The definition column is our COPY INTO, and the integration column is NULL. There is nothing for this pipe to listen to.

Have a look at the comment column in that grid as well. It is not NULL, it is an empty string, and the grid marks it as empty rather than printing a word into the cell. An absent comment and a comment set to nothing are two different things, and this is where you can see the difference.

A stage listing or a SHOW result is easier to read when we pull the columns we want out of it.

The same result, as a query
SELECT "name"              AS THE_NAME,
       "database_name"     AS THE_DATABASE,
       "integration"       AS THE_INTEGRATION,
       "pattern"           AS THE_PATTERN,
       "owner"             AS THE_OWNER
FROM TABLE(RESULT_SCAN(LAST_QUERY_ID()))
ORDER BY THE_NAME;
Result1 row · 5 columns
THE_NAMETHE_DATABASETHE_INTEGRATIONTHE_PATTERNTHE_OWNER
PIPE_T131DEMO_DBNULLNULLACCOUNTADMIN

Name, database, integration, pattern, owner. Both the integration and the pattern are NULL.

Now let's ask the catalog the same question

INFORMATION_SCHEMA.PIPES answers this more plainly than SHOW PIPES does.

Query
SELECT PIPE_NAME              AS THE_NAME,
       IS_AUTOINGEST_ENABLED  AS IS_AUTO_INGEST,
       NOTIFICATION_CHANNEL_NAME AS THE_CHANNEL,
       PIPE_SCHEMA            AS THE_SCHEMA
FROM INFORMATION_SCHEMA.PIPES
WHERE RIGHT(PIPE_NAME, 5) = '_T131';
Result1 row · 4 columns
THE_NAMEIS_AUTO_INGESTTHE_CHANNELTHE_SCHEMA
PIPE_T131NONULLPUBLIC

Is auto ingest enabled: NO. Notification channel: NULL.

That is the same fact we just read off the SHOW output, written differently. SHOW PIPES reports the absence as a NULL integration. The view reports it as the word NO and a NULL channel. The two grids are not disagreeing with each other.

Now let's see what the pipe is actually holding

This is the one we wanted to look at. DESCRIBE PIPE.

Query
DESCRIBE PIPE PIPE_T131;
Result1 row · 15 columns · scroll →
created_onnamedatabase_nameschema_namedefinitionownernotification_channelcommentintegrationpatternerror_integrationowner_role_typeinvalid_reasonkindis_snowflake_managed
2026-10-01 13:18:14.529000-07:00PIPE_T131DEMO_DBPUBLICCOPY INTO LANDING_T131 FROM @STAGE_T131/in/ FILE_FORMAT = (FORMAT_NAME = 'CSV_T131')ACCOUNTADMINNULLNULLNULLNULLROLENULLSTAGEfalse

Let's pull the definition out of that on its own.

Just the definition
SELECT "name"       AS THE_NAME,
       "definition" AS THE_COPY_STATEMENT
FROM TABLE(RESULT_SCAN(LAST_QUERY_ID()));
Result1 row · 2 columns
THE_NAMETHE_COPY_STATEMENT
PIPE_T131COPY INTO LANDING_T131 FROM @STAGE_T131/in/ FILE_FORMAT = (FORMAT_NAME = 'CSV_T131')

There is our statement, handed back word for word. COPY INTO LANDING_T131, from the stage and prefix, with the file format we named.

So a pipe is not a service you configure. It is a statement Snowflake is holding for you.

What does a brand new pipe think of itself?

There is a function for that, and it hands back a JSON string.

Query
SELECT SYSTEM$PIPE_STATUS('PIPE_T131') AS THE_STATUS;
Result1 row · 1 column
THE_STATUS
{"executionState":"RUNNING","pendingFileCount":0,"pendingHistoryRefreshJobsCount":0}

Three keys: the execution state, a pending file count, and a count of pending history refresh jobs. The state is RUNNING and there is nothing pending.

That is easier to read as columns, so let's parse it.

Query
SELECT PARSE_JSON(SYSTEM$PIPE_STATUS('PIPE_T131')):executionState::VARCHAR
           AS EXECUTION_STATE,
       PARSE_JSON(SYSTEM$PIPE_STATUS('PIPE_T131')):pendingFileCount::INT
           AS PENDING_FILES,
       PARSE_JSON(SYSTEM$PIPE_STATUS('PIPE_T131')):numOutstandingMessagesOnChannel::INT
           AS OUTSTANDING_MESSAGES;
Result1 row · 3 columns
EXECUTION_STATEPENDING_FILESOUTSTANDING_MESSAGES
RUNNING0NULL

Running, zero pending files, and outstanding messages NULL.

Where that NULL comes from

That last NULL is worth being careful about. We asked for numOutstandingMessagesOnChannel, and you can see from the JSON in the grid above that the status does not have a key by that name at all. Asking a VARIANT for a key it does not hold gives you NULL. So that cell is telling us the key is missing, not that a channel reported nothing.

Can we ask for AUTO_INGEST on an internal stage?

Auto ingest is the half that needs cloud storage and a notification integration. We have neither. So this one ought to be refused.

Query
CREATE OR REPLACE PIPE AUTOPIPE_T131
AUTO_INGEST = TRUE AS
COPY INTO LANDING_T131
FROM @STAGE_T131/in/
FILE_FORMAT = (FORMAT_NAME = 'CSV_T131');
Result1 row · 1 column
status
Pipe AUTOPIPE_T131 successfully created.

Created. Not refused, not warned about. AUTO_INGEST = TRUE on an internal stage is simply accepted, and the pipe sits there with nothing to listen to.

A second pipe, left where it was

Remember this second pipe. We made it to see whether the option would be refused, we left it pointed at the same prefix and the same table as the first one, and it is going to matter a great deal in a few sections' time.

Now let's put a file where the pipe is watching

Let's unload the source table into the prefix the first pipe reads from.

Query
COPY INTO @STAGE_T131/in/sales.csv FROM SOURCE_T131
FILE_FORMAT = (FORMAT_NAME = 'CSV_T131')
SINGLE = TRUE;
Result1 row · 3 columns
rows_unloadedinput_bytesoutput_bytes
69797
The stage
LIST @STAGE_T131/in/;
Result1 row · 4 columns
namesizemd5last_modified
stage_t131/in/sales.csv112ea0270064cfc01a17d9a3ae5d6addf26Thu, 1 Oct 2026 20:18:25 GMT

One file, sitting exactly where the pipe is pointed.

On an external stage, that arrival would send a notification and the pipe would hear it. On an internal stage it sends nothing. So has anything loaded?

The landing table
SELECT COUNT(*) AS ROWS_IN_THE_LANDING_TABLE FROM LANDING_T131;
Result1 row · 1 column
ROWS_IN_THE_LANDING_TABLE
0

Zero rows. The file is there and the table is empty.

And the pipe
SELECT PARSE_JSON(SYSTEM$PIPE_STATUS('PIPE_T131')):executionState::VARCHAR
           AS EXECUTION_STATE,
       PARSE_JSON(SYSTEM$PIPE_STATUS('PIPE_T131')):pendingFileCount::INT
           AS PENDING_FILES;
Result1 row · 2 columns
EXECUTION_STATEPENDING_FILES
RUNNING0

Running, nothing pending. It does not know the file exists.

Let's tell the pipe to look

ALTER PIPE ... REFRESH is the manual version of the notification that cannot arrive here. Have a look at the grid it gives back.

Query
ALTER PIPE PIPE_T131 REFRESH;
Result1 row · 2 columns
FileStatus
sales.csvSENT

Two columns, File and Status, and the status is SENT.

SENT, not LOADED. The file has been handed to the ingest service. That is all this statement promises.

The pipe status
SELECT PARSE_JSON(SYSTEM$PIPE_STATUS('PIPE_T131')):executionState::VARCHAR
           AS EXECUTION_STATE,
       PARSE_JSON(SYSTEM$PIPE_STATUS('PIPE_T131')):pendingFileCount::INT
           AS PENDING_FILES;
Result1 row · 2 columns
EXECUTION_STATEPENDING_FILES
RUNNING0

Still running, still nothing pending. The status JSON does not show us the queue here, which is not very helpful.

And the table
SELECT COUNT(*) AS ROWS_IN_THE_LANDING_TABLE FROM LANDING_T131;
Result1 row · 1 column
ROWS_IN_THE_LANDING_TABLE
0

Zero rows, a moment after the refresh said SENT.

That is the whole point of a pipe, and it is why pipes exist. The load happens on Snowflake's schedule, not inside your statement.

So let's wait, and ask again

The next statement in the script does nothing but wait.

Query
SELECT SYSTEM$WAIT(60) AS WAITED;
Result1 row · 1 column
WAITED
waited 60 seconds
A wait, not a benchmark

That is a wait, not a measurement. Nothing in this course is timed and no performance claim is being made here. We needed the load to have happened before asking, so we waited, and what came back afterwards is what you see.

The landing table
SELECT COUNT(*)    AS ROWS_IN_THE_LANDING_TABLE,
       SUM(AMOUNT) AS TOTAL_IN_THE_LANDING_TABLE
FROM LANDING_T131;
Result1 row · 2 columns
ROWS_IN_THE_LANDING_TABLETOTAL_IN_THE_LANDING_TABLE
121738.00

Twelve rows, and 1738.00.

The file had six rows in it and the source table added up to 869.00. Both of those numbers have doubled, and that is the most useful thing in this episode.

Twelve rows from a six row file

Let's look at the rows themselves before we guess at anything.

Query
SELECT SALE_ID AS SALE_ID, REGION AS REGION, AMOUNT AS AMOUNT
FROM LANDING_T131
ORDER BY SALE_ID;
Result12 rows · 3 columns
SALE_IDREGIONAMOUNT
1north100.00
1north100.00
2north250.50
2north250.50
3south75.25
3south75.25
4south310.00
4south310.00
5east45.00
5east45.00
6east88.25
6east88.25

Every sale id is in there twice. One and one, two and two, all the way down. The file was loaded twice.

Let's ask the pipe about it first.

The status
SELECT PARSE_JSON(SYSTEM$PIPE_STATUS('PIPE_T131')):executionState::VARCHAR
           AS EXECUTION_STATE,
       PARSE_JSON(SYSTEM$PIPE_STATUS('PIPE_T131')):pendingFileCount::INT
           AS PENDING_FILES,
       PARSE_JSON(SYSTEM$PIPE_STATUS('PIPE_T131')):lastReceivedMessageTimestamp::VARCHAR
           AS LAST_MESSAGE;
Result1 row · 3 columns
EXECUTION_STATEPENDING_FILESLAST_MESSAGE
RUNNING0NULL

Running, nothing pending, and the last message is NULL for the same reason as before: there is no key by that name in the status JSON. The pipe has nothing to say about the rows that have just arrived.

So let's ask the load history about that table instead.

Query
SELECT FILE_NAME         AS THE_FILE,
       ROW_COUNT         AS ROWS_LOADED,
       STATUS            AS THE_STATUS,
       PIPE_NAME         AS LOADED_BY_PIPE
FROM TABLE(INFORMATION_SCHEMA.COPY_HISTORY(
    TABLE_NAME => 'LANDING_T131',
    START_TIME => DATEADD(HOUR, -1, CURRENT_TIMESTAMP())))
ORDER BY THE_FILE;
Result2 rows · 4 columns
THE_FILEROWS_LOADEDTHE_STATUSLOADED_BY_PIPE
sales.csv6LoadedPIPE_T131
sales.csv6LoadedAUTOPIPE_T131

Two rows. The same file name on both, six rows loaded on each, and both of them say Loaded.

The column that settles it is the last one. PIPE_NAME. One load came from PIPE_T131 and the other came from AUTOPIPE_T131.

AUTOPIPE_T131 is the second pipe. The one we created to see whether AUTO_INGEST would be refused, and then left pointed at the same prefix and the same landing table.

So the duplication is not one pipe running twice. It is two pipes each loading the file once.

What that grid proves

Episode 126 showed COPY INTO refusing to load a file it had already loaded. That record of what has been loaded belongs to the pipe, not to the table. A second pipe over the same prefix has a record of its own, and as far as it is concerned this file is new.
Two pipes pointed at one folder will double your data, and neither one of them is doing anything wrong.

What we cannot explain

And here is what we cannot tell you. We ran REFRESH on one pipe. Only one. Both of them loaded the file. There is no capture in this episode that says how the second one was triggered, so we are not going to offer you a tidy answer. The load history says which pipes loaded the file. It does not say what set the second one going.

Can a pipe be paused?

It can, with a parameter on ALTER PIPE.

Query
ALTER PIPE PIPE_T131 SET PIPE_EXECUTION_PAUSED = TRUE;
Result1 row · 1 column
status
Statement executed successfully.
The status
SELECT PARSE_JSON(SYSTEM$PIPE_STATUS('PIPE_T131')):executionState::VARCHAR
           AS EXECUTION_STATE;
Result1 row · 1 column
EXECUTION_STATE
PAUSED

PAUSED. Which is the reason for asking the status at all.

Now let's refresh a paused pipe. This one we expected to be refused.

Query
ALTER PIPE PIPE_T131 REFRESH;
Result1 row · 2 columns
FileStatus
sales.csvSENT

SENT again. The refresh is accepted either way, and the grid looks exactly the same as it did when the pipe was running.

Start it again
ALTER PIPE PIPE_T131 SET PIPE_EXECUTION_PAUSED = FALSE;
Result1 row · 1 column
status
Statement executed successfully.
The status
SELECT PARSE_JSON(SYSTEM$PIPE_STATUS('PIPE_T131')):executionState::VARCHAR
           AS EXECUTION_STATE;
Result1 row · 1 column
EXECUTION_STATE
RUNNING

Running again.

And what it refuses

First, a pipe over a stage that does not exist.

Query
CREATE OR REPLACE PIPE NOPIPE_T131 AS
COPY INTO LANDING_T131
FROM @STAGE_T131_NOPE/in/
FILE_FORMAT = (FORMAT_NAME = 'CSV_T131');
Query failed — Snowflake returned an error
002003 (02000): SQL compilation error:
Stage 'DEMO_DB.PUBLIC.STAGE_T131_NOPE' does not exist or not authorized. Your primary role ACCOUNTADMIN or one of your secondary roles must have at least one privilege granted on STAGE DEMO_DB.PUBLIC.STAGE_T131_NOPE.

Refused at create time. The stage name is checked when the pipe is created, not when it runs.

Now a pipe whose body is not a COPY INTO.

Query
CREATE OR REPLACE PIPE SELECTPIPE_T131 AS
SELECT 1 AS NOT_A_COPY;
Query failed — Snowflake returned an error
001003 (42000): SQL compilation error:
syntax error line 2 at position 0 unexpected 'SELECT'.

A syntax error at the word SELECT. Notice that it is not an error about pipes at all. The grammar simply does not allow anything but a COPY INTO in there.

And asking about a pipe that is not there, three different ways.

Its status
SELECT SYSTEM$PIPE_STATUS('NO_SUCH_PIPE_T131') AS THE_STATUS;
Query failed — Snowflake returned an error
002003 (02000): SQL compilation error:
Pipe 'NO_SUCH_PIPE_T131' does not exist or not authorized.
Refreshing it
ALTER PIPE NO_SUCH_PIPE_T131 REFRESH;
Query failed — Snowflake returned an error
002003 (02000): SQL compilation error:
Pipe 'DEMO_DB.PUBLIC.NO_SUCH_PIPE_T131' does not exist or not authorized. Your primary role ACCOUNTADMIN or one of your secondary roles must have at least one privilege granted on PIPE DEMO_DB.PUBLIC.NO_SUCH_PIPE_T131.
Describing it
DESCRIBE PIPE NO_SUCH_PIPE_T131;
Query failed — Snowflake returned an error
002003 (02000): SQL compilation error:
Pipe 'DEMO_DB.PUBLIC.NO_SUCH_PIPE_T131' does not exist or not authorized. Your primary role ACCOUNTADMIN or one of your secondary roles must have at least one privilege granted on PIPE DEMO_DB.PUBLIC.NO_SUCH_PIPE_T131.

The same code for all three, with the usual wording about an object that does not exist or is not authorized.

One last thing: the teardown

Both pipes go, then the stage, the format and the two tables.

Teardown
DROP PIPE IF EXISTS PIPE_T131;
Result1 row · 1 column
status
PIPE_T131 successfully dropped.
Teardown
DROP PIPE IF EXISTS AUTOPIPE_T131;
Result1 row · 1 column
status
AUTOPIPE_T131 successfully dropped.
Teardown
REMOVE @STAGE_T131/;
Result1 row · 2 columns
nameresult
stage_t131/in/sales.csvremoved
Teardown
DROP STAGE IF EXISTS STAGE_T131;
Result1 row · 1 column
status
STAGE_T131 successfully dropped.
Teardown
DROP FILE FORMAT IF EXISTS CSV_T131;
Result1 row · 1 column
status
CSV_T131 successfully dropped.
Teardown
DROP TABLE IF EXISTS LANDING_T131;
Result1 row · 1 column
status
LANDING_T131 successfully dropped.
Teardown
DROP TABLE IF EXISTS SOURCE_T131;
Result1 row · 1 column
status
SOURCE_T131 successfully dropped.

And a check that nothing is left behind.

Query
SELECT CURRENT_DATABASE()                 AS THE_DATABASE,
       CURRENT_SCHEMA()                   AS THE_SCHEMA,
       (SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES
        WHERE RIGHT(TABLE_NAME, 5) = '_T131') AS PROBE_TABLES_LEFT,
       (SELECT COUNT(*) FROM INFORMATION_SCHEMA.STAGES
        WHERE STAGE_NAME LIKE '%_T131%')      AS PROBE_STAGES_LEFT,
       (SELECT COUNT(*) FROM INFORMATION_SCHEMA.PIPES
        WHERE RIGHT(PIPE_NAME, 5) = '_T131')  AS PROBE_PIPES_LEFT,
       (SELECT COUNT(*) FROM CUSTOMERS)   AS DEMO_DB_IS_READABLE;
Result1 row · 6 columns · scroll →
THE_DATABASETHE_SCHEMAPROBE_TABLES_LEFTPROBE_STAGES_LEFTPROBE_PIPES_LEFTDEMO_DB_IS_READABLE
DEMO_DBPUBLIC00010

No probe tables, no probe stages, no probe pipes, and the demo database still reads.

And the user stage
LIST @~/t131/;
Result0 rows · 4 columns
namesizemd5last_modified

Key points

  • A pipe is a stored COPY INTO. DESCRIBE PIPE hands the statement back word for word as its definition.
  • AUTO_INGEST = TRUE on an internal stage is accepted, not refused. It just has nothing to listen to.
  • SHOW PIPES reports a NULL integration, and INFORMATION_SCHEMA.PIPES says the same thing as the word NO with a NULL channel.
  • In the SHOW PIPES grid the comment is an empty string, not a NULL. The two are not the same and the grid shows them differently.
  • A file landing on an internal stage triggers nothing. The table stays empty and the pipe does not know the file is there.
  • ALTER PIPE ... REFRESH reports SENT, not LOADED, and the table is still empty immediately afterwards.
  • The load happens on Snowflake's schedule, not inside your statement.
  • Asking the status JSON for a key it does not hold gives you NULL. That is a missing key, not a measurement.
  • A pipe's record of what it has already loaded belongs to the pipe, not to the table. A second pipe over the same prefix loads the same file again.
  • COPY_HISTORY has a PIPE_NAME column, and it is what tells you which pipes loaded a file. It does not tell you what triggered them.
  • A paused pipe still accepts a REFRESH, and the grid looks identical to a running one's.
  • A pipe over a missing stage is refused at create time, and a pipe whose body is a SELECT is a plain syntax error.
Next in this series

Episode 132 — time travel. Reading a table as it was before somebody broke it, with AT and BEFORE, and a repair of ours that put the row count back and still left the table wrong.

Every query and every result grid on this page was captured from a live Snowflake account (version 10.35.101) running the demo script for this episode. This episode writes no local files: the file the pipe loads was unloaded from a table inside the same script. The auto ingest half of Snowpipe needs cloud storage this account does not have, and the post says so rather than demonstrating it badly. The single SYSTEM$WAIT is a wait and not a measurement; nothing in this series is timed. One question this episode raises is left unanswered on purpose, and the warning beside it says which. Behaviour follows the Snowflake CREATE PIPE documentation.

No comments:

Post a Comment

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