Ошибка при создании секционированной таблицы в постгресе
Всем привет!
Я строю динамическое секционирование таблицы по метке времени с интервалом в час и у меня не получается писать в новую таблицу.
Родительская таблица:
CREATE TABLE dwh.fact_measures_raw (
id serial NOT NULL,
equipment_id varchar NULL,
parameter_id varchar NULL,
value float8 NULL,
ts_original int8 NULL,
ts_timestamp timestamp NULL,
created_at timestamp NULL DEFAULT now(),
updated_at timestamp NULL DEFAULT now()
);
CREATE INDEX idx_ts_timestamp ON dwh.fact_measures_raw USING btree (ts_timestamp);
Текст функции, которая создает динамически новые секционные таблицы, и текст триггера перед INSERT:
CREATE OR REPLACE FUNCTION dwh.fact_raw_insert_function()
RETURNS trigger
LANGUAGE plpgsql
AS $function$
declare
partition_date text;
partition_name text;
start_of_hour text;
end_of_next_hour text;
begin partition_date := to_char(NEW.ts_timestamp, 'YYYY-MM-DD-HH');
partition_name := 'dwh.raw_data_' || partition_date;
start_of_hour := to_char((NEW.ts_timestamp), 'YYYY-MM-DD HH:DD:SS') || '-01';
end_of_next_hour := to_char((NEW.ts_timestamp + interval '1 hour'), 'YYYY-MM-DD HH:DD:SS') || '-01';
if not exists (
select
1
from
information_schema.tables
where
table_name = partition_name) then raise notice 'A partition has been created %',
partition_name;
execute format(E'CREATE TABLE %I (CHECK ( date_trunc(\'hour\', ts_timestamp) >= ''%s'' AND date_trunc(\'hour\', ts_timestamp) < ''%s''))
INHERITS (dwh.fact_measures_raw)',
partition_name,
start_of_hour,
end_of_next_hour);
end if;
execute format('INSERT INTO %I (equipment_id,parameter_id,value,ts_original,ts_timestamp) VALUES($1,$2,$3,$4,$5)',
partition_name)
using NEW.equipment_id,new.parameter_id,new.value,new.ts_original,NEW.ts_timestamp;
return null;
end $function$;
CREATE TRIGGER fact_raw_insert
BEFORE INSERT ON dwh.fact_measures_raw
FOR EACH ROW EXECUTE PROCEDURE dwh.fact_raw_insert_function();
При выполнении записи новых строк я получаю вот такую ошибку и уже голову сломал в чем проблема:
SQL Error [23514]: ERROR: new row for relation "dwh.raw_data_2020-03-23-04" violates check constraint "dwh.raw_data_2020-03-23-04_ts_timestamp_check" Detail: Failing row contains (1218483, 4161341a-ad79-4021-b6fa-f6754f34f5cb, frequency, 49.8486557006835938, 1584974047062000, 2020-03-23 16:34:07, 2020-03-23 16:54:30.598366, 2020-03-23 16:54:30.598366). Where: SQL statement "INSERT INTO "dwh.raw_data_2020-03-23-04" (equipment_id,parameter_id,value,ts_original,ts_timestamp) VALUES($1,$2,$3,$4,$5)" PL/pgSQL function dwh.fact_raw_insert_function() line 38 at EXECUTE SQL statement "insert into dwh.fact_measures_raw (equipment_id, parameter_id, value, ts_original, ts_timestamp) select id, column_name, cast(column_value as float8),cast(ts as bigint), to_timestamp( NULLIF(ts, '')::bigint/1000000) from dwh.vw_stg_raw where column_value is not null and column_name not in ('ts','created_at') and created_at < date_insert" PL/pgSQL function dwh.insert_fact_measures_raw() line 6 at SQL statement
Что я делаю не так?
Спасибо!