Snowflake

Snowflake Directory Tables: Managing Stage File Metadata with SQL

2026-03-26
更新: 2026-03-27
NicheeLab Editorial Team

Directory Tables are a Snowflake feature that lets you query file metadata (path, size, last-modified time, checksum, and more) on stages using SQL. They integrate data lake file management into Snowflake SQL operations, enabling file listing, diff detection, and Stream-driven file arrival event processing.

Enabling Directory Tables

-- Enable Directory Tables on an existing stage
ALTER STAGE raw_data_stage SET DIRECTORY = (ENABLE = TRUE);

-- Enable when creating a new stage (External Stage / S3 example)
CREATE OR REPLACE STAGE landing_stage
  URL = 's3://my-bucket/landing/'
  STORAGE_INTEGRATION = s3_integration
  DIRECTORY = (ENABLE = TRUE);

-- Enable auto-refresh at the same time
CREATE OR REPLACE STAGE landing_stage
  URL = 's3://my-bucket/landing/'
  STORAGE_INTEGRATION = s3_integration
  DIRECTORY = (
    ENABLE = TRUE
    AUTO_REFRESH = TRUE
  );

-- Internal Stage example
CREATE OR REPLACE STAGE internal_landing
  DIRECTORY = (ENABLE = TRUE);

Directory Tables Metadata Columns

ColumnTypeDescription
RELATIVE_PATHVARCHARRelative file path from the stage root
SIZENUMBERFile size in bytes
LAST_MODIFIEDTIMESTAMP_LTZFile last-modified timestamp
MD5VARCHARMD5 hash of the file (External Stage only)
ETAGVARCHARCloud storage ETag
FILE_URLVARCHARURL used to access the file from within Snowflake

Querying Directory Tables

-- List all files in the stage
SELECT * FROM DIRECTORY(@landing_stage);

-- Filter by file extension
SELECT relative_path, size, last_modified
FROM DIRECTORY(@landing_stage)
WHERE relative_path LIKE '%.parquet';

-- Files added in the last 24 hours
SELECT relative_path, size, last_modified
FROM DIRECTORY(@landing_stage)
WHERE last_modified >= DATEADD(HOUR, -24, CURRENT_TIMESTAMP());

-- Aggregate file count and size
SELECT
  SPLIT_PART(relative_path, '.', -1) AS file_extension,
  COUNT(*) AS file_count,
  SUM(size) / (1024 * 1024) AS total_size_mb
FROM DIRECTORY(@landing_stage)
GROUP BY file_extension
ORDER BY total_size_mb DESC;

Manual Metadata Refresh

-- Refresh metadata for the whole stage
ALTER STAGE landing_stage REFRESH;

-- Refresh only a specific subpath (efficient for many files)
ALTER STAGE landing_stage REFRESH SUBPATH = '/2026/03/';

When auto-refresh (AUTO_REFRESH = TRUE) is enabled, metadata is updated automatically using cloud storage event notifications as the trigger. Auto-refresh is not available on Internal Stages, so manual refresh is required there.

Cloud-Specific Auto-Refresh Setup

CloudEvent NotificationSetup Steps
AWS S3SNS topic + S3 event notificationGet the policy with SYSTEM$GET_AWS_SNS_IAM_POLICY() and apply it to SNS
Azure BlobEvent GridGenerate a SAS token with SYSTEM$GET_AZURE_SAS_TOKEN() and configure Event Grid
GCSPub/Sub notificationGet the service account with SYSTEM$GET_GCP_SERVICE_ACCOUNT() and configure it on Pub/Sub

Stream Integration for File Arrival-Driven Processing

-- Create a Stream on a Directory Table
CREATE OR REPLACE STREAM landing_file_stream
  ON STAGE landing_stage;

-- Detect newly arrived files via the Stream
SELECT *
FROM landing_file_stream
WHERE METADATA$ACTION = 'INSERT';

-- Use a Task to auto-process new files
CREATE OR REPLACE TASK process_new_files
  WAREHOUSE = etl_wh
  SCHEDULE = '5 MINUTE'
  WHEN SYSTEM$STREAM_HAS_DATA('landing_file_stream')
AS
  COPY INTO raw_data
  FROM @landing_stage
  FILES = (
    SELECT RELATIVE_PATH
    FROM landing_file_stream
    WHERE METADATA$ACTION = 'INSERT'
  )
  FILE_FORMAT = (TYPE = 'PARQUET');

File URL Functions

-- Scoped URL (temporary URL valid within the session)
SELECT BUILD_SCOPED_FILE_URL(@landing_stage, relative_path)
FROM DIRECTORY(@landing_stage)
WHERE relative_path LIKE '%.pdf';

-- Stage URL (for internal Snowflake references)
SELECT BUILD_STAGE_FILE_URL(@landing_stage, relative_path)
FROM DIRECTORY(@landing_stage);

-- Presigned URL (for external sharing, with expiration)
SELECT GET_PRESIGNED_URL(@landing_stage, relative_path, 3600)
FROM DIRECTORY(@landing_stage)
WHERE relative_path = 'reports/2026-Q1.pdf';

Cost Estimation

-- Estimate auto-refresh cost
SELECT SYSTEM$ESTIMATE_AUTOMATIC_REFRESH_COST('landing_stage');

Best Practices

  • Enable auto-refresh only on External Stages: for Internal Stages, a common pattern is to run manual refresh via a scheduled Task
  • Use subpath-scoped refresh for efficiency: specify REFRESH SUBPATH per date partition to avoid unnecessary scans
  • Combine with Streams to build a CDC pattern: use a Stream on a Directory Table to detect file arrivals and run auto-loading via a Task
  • Choosing between LIST @stage and Directory Tables: LIST @stage returns a real-time listing but is not cached. Directory Tables cache metadata for faster queries, at the cost of refresh lag.

Check Your Understanding

Data Loading

問題 1

You enabled Directory Tables with AUTO_REFRESH = TRUE on an External Stage backed by S3. New files have been uploaded to S3 but they are not appearing in the DIRECTORY() function results. What is the most likely cause?

  1. Directory Tables only work on Internal Stages and are not supported on External Stages
  2. The SNS event notification pipeline from the S3 bucket to Snowflake is not fully configured
  3. AUTO_REFRESH requires Enterprise Edition or higher
  4. You must use the LIST @stage command instead of the DIRECTORY() function

正解: B

Auto-refresh for Directory Tables on S3 requires a notification pipeline: S3 bucket event notifications → SNS topic → Snowflake. If this configuration is incomplete, file-addition events never reach Snowflake and metadata is not auto-updated. Directory Tables work on both External and Internal Stages and are available even on Standard Edition.

Frequently Asked Questions

Do Directory Tables auto-refresh operations consume credits?

Yes. Auto-refresh is a serverless process that updates metadata in response to event notifications (AWS SNS / Azure Event Grid / GCP Pub/Sub) and is billed as Serverless Credits. Costs grow on stages with frequent file additions and deletions, so it is recommended to estimate costs ahead of time using the SYSTEM$ESTIMATE_AUTOMATIC_REFRESH_COST() function.

Can you add custom columns to Directory Tables metadata?

You cannot add custom columns to Directory Tables themselves. Only the fixed columns (RELATIVE_PATH, SIZE, LAST_MODIFIED, MD5, ETAG, FILE_URL) are provided. When custom attributes are required, a common pattern is to JOIN the Directory Tables result with an external metadata management table.

Can Directory Tables be used with both Internal and External Stages?

Yes, Directory Tables can be enabled on both stage types. However, auto-refresh is only supported on External Stages (S3 / GCS / Azure Blob). For Internal Stages, you must manually run ALTER STAGE REFRESH to update metadata.

Check what you learned with practice questions

Practice with certification-focused question sets

無料で問題を解いてみる
Author

NicheeLab Editorial Team

NicheeLab editorial team focused on data engineering and cloud certification learning. Content is structured around practical study needs and official exam domains.


Related articles
Snowflake

Snowflake Certifications: All 11 Exams Explained (2026)

Every SnowPro certification — Associate, Core, Specialty, Ad...

Snowflake

Snowflake Exam Difficulty Ranking: All 11 Certs Compared (2026)

All 11 SnowPro exams ranked by difficulty with study-time es...

Snowflake

Snowflake Study Guide: Fastest Pass Route by Exam (2026)

How to pass SnowPro certifications efficiently — official ma...

Snowflake

SnowPro Core (COF-C03): Complete Exam Guide (2026)

Pass the SnowPro Core exam — six domains, scope, sample ques...

Snowflake

SnowPro Associate Platform (SOL-C01): Complete Guide (2026)

The entry-level SnowPro Associate exam — scope, weighting, s...

Browse all Snowflake articles (103)
© 2026 NicheeLab All rights reserved.