Core Materializations
Relevant source files
The following files were used as context for generating this wiki page:
- dbt-adapters/src/dbt/include/global_project/macros/materializations/models/table.sql
- dbt-adapters/src/dbt/include/global_project/macros/materializations/models/view.sql
- dbt-adapters/src/dbt/include/global_project/macros/materializations/models/incremental/incremental.sql
- dbt-adapters/src/dbt/include/global_project/macros/materializations/models/materialized_view.sql
- dbt-adapters/src/dbt/include/global_project/macros/materializations/seeds/seed.sql
Core Materializations define the fundamental strategies for transforming and persisting data within a data warehouse using dbt. These materializations abstract the complex SQL DDL and DML required to manage different types of database objects such as tables, views, incremental tables, and materialized views.
The materialization system relies on a standardized lifecycle: setting up relations, executing pre-hooks, building the model, handling grants, persisting documentation, and performing final cleanup. This ensures consistent behavior across different database adapters while allowing for adapter-specific overrides where necessary.
Table Materialization
The table materialization creates a physical table in the database by executing a CREATE TABLE AS (CTAS) statement. It uses an atomic swap pattern to minimize downtime: data is built into an intermediate relation, then renamed to the target relation while the old relation is moved to a backup and eventually dropped.
Workflow Logic
The following diagram illustrates the lifecycle of a table materialization, emphasizing the use of intermediate and backup relations to ensure reliability.
Sources: table.sql:1-76
Key Components
| Component | Description |
|---|---|
target_relation | The final table destination. |
intermediate_relation | Temporary table where the new data is initially built. |
backup_relation | Holds the previous version of the table during the swap process. |
get_create_table_as_sql | Macro that generates the CTAS SQL statement. |
Sources: table.sql:3-16
Incremental Materialization
Incremental materializations allow dbt to only process new or modified records instead of rebuilding the entire table. This is achieved through strategies like append, merge, or delete+insert.
Data Flow and Schema Management
Incremental models handle schema changes dynamically based on the on_schema_change configuration. They compare the temporary relation (containing new data) with the existing relation to determine if columns need to be added or if types need expansion.
Sources: incremental.sql:35-64
Incremental Configurations
| Config | Type | Description |
|---|---|---|
unique_key | String/List | Used to identify rows for updates or deletions. |
incremental_strategy | String | The method used (e.g., merge, append, delete+insert). |
on_schema_change | String | Strategy for handling new columns (ignore, fail, append_new, sync_all_columns). |
Sources: incremental.sql:12-16, incremental.sql:41-50
Materialized View Materialization
The materialized_view materialization manages relations that persist results but are automatically updated by the database. Unlike standard tables, these often support configuration changes without a full rebuild.
Configuration Change Handling
When a materialized view exists, dbt checks for configuration changes. Depending on the on_configuration_change setting, it may alter the view, fail the run, or warn the user.
Sources: materialized_view.sql:63-88
Seed Materialization
Seeds are used to load CSV files into the database as tables. The materialization handles the conversion of CSV data into SQL INSERT statements or CREATE TABLE commands.
Lifecycle of a Seed
- Load Agate Table: dbt loads the CSV file into an internal Agate table structure.
- Table Existence Check:
- If it's a view, an error is raised.
- If it's a table and
full_refreshis true, it is reset. - If it doesn't exist, a new table is created.
- Data Loading: Rows are loaded using
load_csv_rows. - Post-Processing: Indexes are created and documentation is persisted.
Sources: seed.sql:1-68
Summary of Core Features
The core materializations share a common architecture designed for atomicity and metadata management. Every materialization follows a strict hook execution order: pre-hooks (outside and inside transaction), the main build statement, post-hooks (inside and outside transaction), and finally an adapter commit. This ensures that documentation, grants, and data are always in a consistent state upon completion.
Sources: table.sql:31-74, seed.sql:22-63, materialized_view.sql:110-128