Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add materialized view configuration for postgres #4590

Merged
merged 18 commits into from
Dec 13, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 35 additions & 11 deletions website/docs/reference/resource-configs/postgres-configs.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,21 +108,43 @@ models:

## Materialized views

The Postgres adapter supports [materialized views](https://www.postgresql.org/docs/current/rules-materializedviews.html).
Indexes are the only configuration that is specific to `dbt-postgres`.
The remaining configuration follows the general [materialized view](/docs/build/materializations#materialized-view) configuration.
There are also some limitations that we hope to address in the next version.
The Postgres adapter supports [materialized views](https://www.postgresql.org/docs/current/rules-materializedviews.html)
with the following configuration parameters:

### Monitored configuration changes
| Parameter | Type | Required | Default | Change Monitoring Support | Reference |
|---------------------------|------------|----------|-----------|---------------------------|---------------------|
| `on_configuration_change` | STRING | NO | `'apply'` | N/A | |
mikealfare marked this conversation as resolved.
Show resolved Hide resolved
| `indexes` | LIST[DICT] | NO | | ALTER | [Indexes](#indexes) |
mikealfare marked this conversation as resolved.
Show resolved Hide resolved

The settings below are monitored for changes applicable to `on_configuration_change`.
#### Sample model file:

#### Indexes
<File name='postgres_materialized_view.sql'>

Index changes (`CREATE`, `DROP`) can be applied without the need to rebuild the materialized view.
This differs from a table model, where the table needs to be dropped and re-created to update the indexes.
If the `indexes` portion of the `config` block is updated, the changes will be detected and applied
directly to the materialized view in place.
```sql
{{ config(
materialized='materialized_view',
on_configuration_change='{ apply | continue | fail }'
indexes=[
{
'columns': ['<column_name>', ...],
'unique': <bool>},
mikealfare marked this conversation as resolved.
Show resolved Hide resolved
'type': '{ HASH | B-TREE | GIST | SP-GIST | GIN | BRIN }',
mikealfare marked this conversation as resolved.
Show resolved Hide resolved
]
mikealfare marked this conversation as resolved.
Show resolved Hide resolved
) }}

select * from {{ ref('my_base_table') }}
```

</File>

The `indexes` parameter corresponds to that of a table, as linked above.
matthewshaver marked this conversation as resolved.
Show resolved Hide resolved
It's worth noting that, unlike with tables, dbt will monitor this parameter for changes and apply the changes without dropping the materialized view.
matthewshaver marked this conversation as resolved.
Show resolved Hide resolved
This happens via a `DROP/CREATE` of the indexes, which could be thought of as a `ALTER` of the materialized view.
matthewshaver marked this conversation as resolved.
Show resolved Hide resolved

Find more information about materialized view parameters in the Postgres docs:
- [CREATE MATERIALIZED VIEW](https://www.postgresql.org/docs/current/sql-creatematerializedview.html)

<VersionBlock lastVersion="1.6">
mikealfare marked this conversation as resolved.
Show resolved Hide resolved

### Limitations

Expand All @@ -138,3 +160,5 @@ If the user changes the model's config to `materialized="materialized_view"`, th
The solution is to execute `DROP TABLE my_model` on the data warehouse before trying the model again.

</VersionBlock>

</VersionBlock>
Loading