Skip to content

Instantly share code, notes, and snippets.

@drewbanin
Created April 26, 2017 20:43
Show Gist options
  • Save drewbanin/43db27d86d5857facb7fb4aef19cc7aa to your computer and use it in GitHub Desktop.
Save drewbanin/43db27d86d5857facb7fb4aef19cc7aa to your computer and use it in GitHub Desktop.
table indexes (postgres)
-- Macros to add indexes:
-- macros/indexes.sql
{% macro index(this, column) %}
drop index if exists "{{ this.schema }}"."{{ this.name }}__index_on_{{ column }}" cascade;
create index "{{ this.name }}__index_on_{{ column }}" on {{ this }} ("{{ column }}")
{% endmacro %}
{% macro unique_index(this, column) %}
drop index if exists "{{ this.schema }}"."{{ this.name }}__index_on_{{ column }}" cascade;
create unique index "{{ this.name }}__index_on_{{ column }}" on {{ this }} ("{{ column }}")
{% endmacro %}
-- Call them like this in your model code:
-- models/my_model.sql
{{
config({
"materialized": "table",
"post-hook": [
"{{ unique_index(this, 'id') }}",
],
})
}}
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment