Skip to content

Commit

Permalink
Handle >64 characters partition names
Browse files Browse the repository at this point in the history
  • Loading branch information
ggam committed May 1, 2024
1 parent 23a0854 commit 1bf527c
Showing 1 changed file with 5 additions and 8 deletions.
13 changes: 5 additions & 8 deletions dbt/include/postgres/macros/adapters.sql
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@
date_trunc('{{ partition_by_granularity }}', min({{ partition_by_field }})),
date_trunc('{{ partition_by_granularity }}', max({{ partition_by_field }})),
'1 {{ partition_by_granularity }}'::interval
) as begin_date,
(select floor(random() * 100 + 1)::int) as rand -- Random number to avoid name colissions
) as begin_date
from (
{{ sql }}
) model_subq
Expand Down Expand Up @@ -109,19 +108,17 @@
{% set existing_partitions_query %}
select
inhrelid::regclass::text as from_table_name,
replace(
inhrelid::regclass::text,
'{{ from_relation.schema }}.{{ from_relation.identifier }}', -- Current partition name
'{{ to_relation.identifier }}' -- New partition name
) as to_table_name
regexp_substr(inhrelid::regclass::text, '[^_]*$') as partition_suffix -- Get the string after the last underscore
from pg_catalog.pg_inherits
where inhparent = '{{ from_relation.schema }}.{{ from_relation.identifier }}'::regclass;
{% endset %}
{% set existing_partitions_results = run_query(existing_partitions_query) %}

-- Rename the existing partitions
{% for existing_partition in existing_partitions_results.rows %}
alter table {{ existing_partition["from_table_name"] }} rename to {{ existing_partition["to_table_name"] }};
{%- set partition_relation = make_intermediate_relation(to_relation, existing_partition['partition_suffix']) %}

alter table {{ existing_partition["from_table_name"] }} rename to {{ partition_relation.identifier }};
{% endfor %}
{%- endcall %}
{% endmacro %}
Expand Down

0 comments on commit 1bf527c

Please sign in to comment.