I ran into this exact issue myself and made the following package for this use case:
dbt-postgres-utils
- Installation
Include the following in your packages.yml file:
packages:
- package: sgoley/postgres_utils
version: 0.2.0
then you can run dbt deps
and the dbt package manager will setup the related macros in your project locally under project_dir/dbt_modules/postgres_utils
.
- Usage
After that, table models can have an index or unique index built with a posthook like:
{{
config({
"post-hook": [
"{{ postgres_utils.index(this, 'id')}}",
],
})
}}
Of course if you add this to a view model, your dbt run
will throw an error.
Feel free to make additional requests, submissions, or more here on the project repo:
github: sgoley/dbt-postgres-utils
Additional features I'm working on are:
- convert syntax of
index
and uindex
functions to "create or replace"
- specify index types (btree, hash, etc.)