【发布时间】:2023-02-20 15:16:40
【问题描述】:
我想使用 dbt 将数据插入到分区表中,但发现不支持 dbt postgres 分区。
通过另一种方式,我在 pre_hook 中创建表和分区,但在运行 dbt 时出现错误“关系‘download_counts_p’已经存在”
有什么建议吗?这是我的 SQL 和 pre_hook 配置
{{ config(
materialized = 'table',
indexes = [ ],
pre_hook=[
'CREATE TABLE IF NOT EXISTS "download_counts_p" (
"channel_id" int8 NOT NULL,
"product_id" int8 NOT NULL,
"country_code" text NOT NULL,
"year" int2 NULL,
"month" int2 NOT NULL,
"count" int8 NOT NULL,
"count" int8 NOT NULL,
"months" int8 NOT NULL
) partition by list(country_code)',
"DO $$
Declare unique_country_code varchar;
BEGIN
FOR unique_country_code IN
SELECT country_code as unique_country_code FROM download_counts group by country_code
LOOP
EXECUTE format('create table IF NOT EXISTS download_counts_p_%s partition of download_counts_p for values in (''%s'')', upper(unique_country_code), unique_country_code);
END LOOP;
END; $$;"]
)}}
select 1
【问题讨论】:
-
请做不是将代码发布为图像。请参阅此处了解更多详细信息:meta.stackoverflow.com/questions/285551
标签: database postgresql dbt