You are on page 1of 4

Get unlimited access to the best of Medium for less than $1/week.

Become a member

“Event Table” on Snowflake

“EVENT TABLE” → Logging


transactions natively on Snowflake
Somen Swain · Follow
Published in Snowflake · 5 min read · 4 days ago

15 1

In this blog we would be discussing about “event tables”. Event tables


designed to capture a speci c set of event within Snow ake. Event table
should not be considered as a standard table since it is speci cally designed
for logging and tracing activities natively within the platform.

Snow ake’s telemetry APIs are what allows to collect and spread events. The
telemetry APIs are supported across various languages for UDFs, UDTFs,
and stored procedures. These are the primitives that can be utilised
independently for formulating questions, as well as being utilised when
creating Native Applications.

Below are the speci c operations that can be performed on an event table
currently:

Event Table Operations

If we observe here it is worth noticing that in event tables we cannot


explicitly insert or update data the data.

There are various methods which speci es how the event table can be used
by the code handler. They are namely:

1. Event Tables : This when de ned would specify name of the event
table for logging messages from stored procedures and UDFs in this
account.

2. Log Level : Speci es the severity level of messages that should be


ingested and made available in the active event table. They can have
values like ‘trace’, ‘debug’, ‘info’, ‘warn’, ‘error’

3. Trace Level : Speci es the verbosity of trace events that should be


ingested and made available in the active event table.

Creating and setting up event tables


Let us see on how to create the event tables within Snow ake, there are
primarily 3 steps involved to create the event table. They are given as below:

1. Create an event table.


2. Associate the event table with the account.
3. Set the level for log and trace the events.

CREATE EVENT TABLE DEMO_DB_1.DEMO_SCHEMA_1.DEMO_EVENT_TBL; --- Creating an ev


ent table.
ALTER ACCOUNT SET EVENT_TABLE = DEMO_DB_1.DEMO_SCHEMA_1.DEMO_EVENT_TBL; --- As
sociate event table with an account.
ALTER SESSION SET LOG_LEVEL = INFO; --- Associate event table with an account.

Above are the commands through which the event tables can be created and
associated with an account.

Use Case: Tracking DML within Stored Procedure


Now, that we have got fair understanding on how event table can be
con gured. Let us now see how this can be implemented. I have taken a use
case like when we create a stored procedure and within it we do multiple
operations like inserts, updates and deletes. Then in this case at the end of
execution of each step of DML we need to track how many records got
impacted/a ected with those operations and event table can be helpful here
to track them.

The code snippet is given as below:

Step 1 :: Set up the event table.


CREATE EVENT TABLE DEMO_DB_1.DEMO_SCHEMA_1.DEMO_EVENT_TBL; --- Creating an ev
ent table.
ALTER ACCOUNT SET EVENT_TABLE = DEMO_DB_1.DEMO_SCHEMA_1.DEMO_EVENT_TBL; --- As
sociate event table with an account.
ALTER SESSION SET LOG_LEVEL = INFO; --- Associate event table with an account.

Step 2 :: Creating the procedure.

CREATE OR REPLACE PROCEDURE DEMO_EVENT_TBL_PROC(OTPT VARCHAR)


RETURNS VARCHAR NOT NULL
LANGUAGE SQL
EXECUTE AS OWNER
AS
$$
DECLARE
dmlcount integer;
COL varchar;
QUERY STRING;
counts int := 0;
BEGIN
LET START_DATE TIMESTAMP := CURRENT_TIMESTAMP();
SYSTEM$LOG('INFO', 'Procedure started at: ' || TO_CHAR(START_DATE, 'YYYY-MM-D
D HH24:mi:ss'));
--- To Perform deletes
QUERY := 'DELETE FROM DEMO_DB_1.DEMO_SCHEMA_1.CUSTOMER WHERE C_CUSTOMER_ID=''
AAAAAAAABEMAMPEA''';
EXECUTE IMMEDIATE :QUERY;
SELECT $1 INTO counts FROM table(result_scan(last_query_id()));
IF (counts > 0) then
SYSTEM$LOG('INFO', 'Total number of records deleted are: ' || :counts);
END IF;

--- To perform inserts


QUERY := 'INSERT INTO DEMO_DB_1.DEMO_SCHEMA_1.BKP_CUSTOMER SELECT * FROM DEMO
_DB_1.DEMO_SCHEMA_1.CUSTOMER LIMIT 10';
EXECUTE IMMEDIATE :QUERY;
SELECT $1 INTO counts FROM table(result_scan(last_query_id()));
IF (counts > 0) then
SYSTEM$LOG('INFO', 'Total number of records inserted are: ' || :counts);
END IF;

--- To perform updates


QUERY := 'UPDATE DEMO_DB_1.DEMO_SCHEMA_1.CUSTOMER SET C_CUSTOMER_SK=''8362700
0'' WHERE C_CURRENT_HDEMO_SK>=''3320''';
EXECUTE IMMEDIATE :QUERY;
SELECT $1 INTO counts FROM table(result_scan(last_query_id()));
IF (counts > 0) then
SYSTEM$LOG('INFO', 'Total number of records updated are: ' || :counts);
END IF;

LET END_DATE TIMESTAMP := CURRENT_TIMESTAMP();


SYSTEM$LOG('INFO', 'Procedure completed at: ' || TO_CHAR(END_DATE, 'YYYY-MM-D
D HH24:mi:ss'));
RETURN OTPT;
END;
$$
;

Step 2 :: Running the procedure.


CALL DEMO_EVENT_TBL_PROC('A');

If we see over here all our event table capture calls never includes the event
table itself but still the logs are captured since that is implicitly handled by
Snow ake.

Implicit population of the event table

Now, let us the result of the event table on Snow ake:

The impacted record count

Now, this is one such example how we can use event table within the
Snow ake platform. Additionally while executing this use case I could
observe some behavior summarizing them as below.
Other additional properties.

You can get more information about event tables by reading through the
below Snow ake documentation:

https://docs.snow ake.com/en/developer-guide/logging-
tracing/tutorials/logging-tracing-getting-started

THANK YOU !!
Please keep reading my blogs it is only going to encourage me in posting
more such content. You can nd me on LinkedIn by clicking here and on
Medium here. Happy Learning :)

To get the details of the code please go the link event_table_code

Awarded as “Data Superhero by Snow ake for year 2023”, click here for
more details.

Disclaimer: The views expressed here are mine alone and do not necessarily re ect
the view of my current, former, or future employers.

Snowflake Logging And Monitoring Cloud Cloud Computing Data Superhero

15 1

Written by Somen Follow

Swain
315 Followers · Writer for Snowflake

Snowflake Data Superhero 2023 | AWS Solution Architect Associate | Snowflake Advanced
Architect | Principal-Data Engineering at LTIMindtree

More from Somen Swain and Snowflake

Somen Swain in Snowflake Bowei Chen in Snowflake

Data Governance with Tag based Aggregation Placement — An


masking policy & Governance… Adaptive Query Optimization for…
Data governance is one of the very important Snowflake’s Data Cloud is backed by a data
concept which should be understood and… platform designed from the ground up to…

6 min read · Jun 23 8 min read · Aug 11

32 4 51

Anurag Gupta in Snowflake

Snowflake Python APIs — Python


Everywhere! Somen Swain
Python plays a crucial role in our Snowflake
strategy for App, Data Engineering & ML. It is… SNOWPRO Advanced: Architect
4 min read · Jul 31
Exam — Preparation tips for…
I recently passed the Snowflake Advanced
Architect exam. This is really a tough exam…
28 2
7 min read · Apr 13, 2022

18 2
See all from Somen Swain See all from Snowflake

Recommended from Medium

Matt Feeser in In the weeds

Eylon Steiner in Infostrux Engineering Blog Automated Ingestion into


Snowflake via DMS/s3
Costs Control Series Part 1: How To
10 min read · Aug 4
Save Money With Snowflake
Explore Snowflake Cost Drivers, how to 3 1
Control, Alert and Monitor

10 min read · Aug 20

Lists

New_Reading_List
174 stories · 84 saves

Ranjeeta Pegu

Simple Snowflake Data Ingestion


App
Happy Tuesday! Today I will create a fun app
using streamlit to ingest data into Snowflake.

3 min read · Aug 15

Akash Solanke

Data lineage in Snowflake


Historically, Data lineage was used to
understand the data journey through a data…

3 min read · Jun 20

Aitor Murguzur

Eleanor Watson
Deploying Microsoft Fabric Azure
Capacity Using Terraform
Starburst vs. Snowflake
Fabric licensing introduces F SKUs for
Starburst and Snowflake are two popular
provisioning Fabric capacity on Azure,…
cloud-based data warehousing solutions use…
2 min read · Aug 22
3 min read · May 12

1
52

See more recommendations

Help Status Writers Blog Careers Privacy Terms About Text to speech Teams

You might also like