Answer the question
In order to leave comments, you need to log in
How to do a check constraint correctly?
There is a table
insert INTO events select
floor(random()*(6-3+1)+1)*4,
floor(random()*(25-10+1))*543,
floor(random()*(6-3+1)+1)*4,
floor(random()*(25-10+1))*543,
floor(random()*(25-10+1))*543,
'2017-08-20 11:34:58.408739' ,
nextval('events_id_seq'::regclass),
floor(random()*(25-10+1))*543
from
generate_series(1, 60000) ;
Answer the question
In order to leave comments, you need to log in
next_month := date_part( 'year', NEW.dtime)::text ||'-'|| date_part( 'month', NEW.dtime )::text ||'-31';
table_part := table_master
|| '' || to_char(NOW(), 'YYYY')::text
|| 'm' || to_char(NOW(), 'MM')::text;
now configured the trigger like this|:
DECLARE
table_master varchar(255) := 'events';
table_part varchar(255) := '';
curr_month varchar(255) := '';
next_month varchar(255) := '';
month_number integer := 0;
month_number_text varchar(255) :='';
BEGIN
month_number := date_part( 'month', NEW.dtime);
IF month_number < 10 THEN
month_number_text := '0'|| month_number::text;
END IF;
-- Даём имя партиции --------------------------------------------------
table_part := table_master
|| '' || to_char(NEW.dtime::date, 'YYYY')::text
|| 'm' || to_char(NEW.dtime::date, 'MM')::text;
--date '2001-12-28' + interval '1 month'
curr_month := date_part( 'year', NEW.dtime)::text ||'-'||to_char(NEW.dtime::date, 'MM')::text ||'-01';
-- next_month := date_part( 'year', NEW.dtime)::text ||'-'|| month_number_text ||'-31';
next_month := NEW.dtime + interval '1 month';
next_month := to_char(next_month::date, 'YYYY-MM')||'-01';
-- Проверяем партицию на существование --------------------------------
PERFORM
1
FROM
pg_class
WHERE
relname = table_part
LIMIT
1;
-- Если её ещё нет, то создаём --------------------------------------------
IF NOT FOUND
THEN
-- Cоздаём партицию, наследуя мастер-таблицу --------------------------
EXECUTE '
CREATE TABLE ' || table_part || ' ( like '|| table_master ||' including all ) inherits ('|| table_master ||')' ;
EXECUTE '
alter table ' || table_part || ' add CONSTRAINT dtime_check CHECK (dtime >= '''|| curr_month||'''::timestamp AND dtime < '''||next_month||'''::timestamp)';
END IF;
-- Вставляем данные в партицию --------------------------------------------
EXECUTE '
INSERT INTO ' || table_part || '
SELECT ( (' || quote_literal(NEW) || ')::' || TG_RELNAME || ' ).*';
RETURN NULL;
END;
DECLARE
table_master varchar(255) := 'events';
table_part varchar(255) := '';
curr_month varchar(255) := '';
next_month varchar(255) := '';
month_number integer := 0;
month_number_text varchar(255) :='';
BEGIN
month_number := date_part( 'month', NEW.dtime);
IF month_number < 10 THEN
month_number_text := '0'|| month_number::text;
END IF;
-- Даём имя партиции --------------------------------------------------
table_part := table_master
|| '' || to_char(NEW.dtime::date, 'YYYY')::text
|| 'm' || to_char(NEW.dtime::date, 'MM')::text;
--date '2001-12-28' + interval '1 month'
curr_month := date_part( 'year', NEW.dtime)::text ||'-'||to_char(NEW.dtime::date, 'MM')::text ||'-01';
-- next_month := date_part( 'year', NEW.dtime)::text ||'-'|| month_number_text ||'-31';
next_month := NEW.dtime + interval '1 month';
next_month := to_char(next_month::date, 'YYYY-MM')||'-01';
-- Проверяем партицию на существование --------------------------------
PERFORM
1
FROM
pg_class
WHERE
relname = table_part
LIMIT
1;
-- Если её ещё нет, то создаём --------------------------------------------
IF NOT FOUND
THEN
-- Cоздаём партицию, наследуя мастер-таблицу --------------------------
EXECUTE '
CREATE TABLE ' || table_part || ' ( like '|| table_master ||' including all ) inherits ('|| table_master ||')' ;
EXECUTE '
alter table ' || table_part || ' add CONSTRAINT dtime_check CHECK (dtime >= '''|| curr_month||'''::timestamp AND dtime < '''||next_month||'''::timestamp)';
END IF;
-- Вставляем данные в партицию --------------------------------------------
EXECUTE '
INSERT INTO ' || table_part || '
SELECT ( (' || quote_literal(NEW) || ')::' || TG_RELNAME || ' ).*';
RETURN NULL;
END;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question