使用新列在父级 table 上插入失败
Insert failed on parent table with new column
我有一个主要的 table(付款)和一些分区的 tables (payment_AAAA_MM)。我获得了这些 tables 与这样的存储功能:
CREATE OR REPLACE FUNCTION partitioning_payment()
RETURNS trigger AS
$BODY$
DECLARE
numero integer;
month float;
year float;
monthp float;
yearp float;
months text;
monthps text;
BEGIN
month=DATE_PART('month',NEW.REQUEST_TS);
year=DATE_PART('year',NEW.REQUEST_TS);
monthp=month;
yearp=year;
months=month;
monthps=monthp;
IF month = 12 THEN
yearp=yearp+1;
monthp=1;
ELSE
monthp=monthp+1;
END IF;
monthps=monthp;
IF(length(month::text)=1) THEN
months=month::text;
months='0' || months;
END IF;
IF(length(monthp::text)=1) THEN
monthps=monthp::text;
monthps='0' || monthps;
END IF;
EXECUTE 'SELECT count(*) FROM PG_TABLES WHERE SCHEMANAME=''public'' AND TABLENAME=''payment_' || year || '_' || months || '''' INTO numero;
IF numero=0 THEN
EXECUTE 'CREATE TABLE payment_' || year || '_' || months || '(CHECK ( REQUEST_TS >= ''' || year || '-' || months || '-01 00:00:00'' and REQUEST_TS < ''' || yearp || '-' || monthps || '-01 00:00:00'' )) inherits (payment)';
END IF;
EXECUTE 'INSERT INTO payment_' || year || '_' || months || ' (id,id_request,REQUEST_TS,response)
VALUES (
'''||NEW.ID||''',
'''||NEW.ID_REQUEST||''',
'''||NEW.REQUEST_TS||''',
'''||NEW.RESPONSE||'''
)';
RETURN null; END;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
CREATE TRIGGER partitioning_payment
BEFORE INSERT
ON payment
FOR EACH ROW
EXECUTE PROCEDURE partitioning_payment();
一切正常。
现在,我必须向父级 table 添加一列(is_test 整数类型)。我在parent table中添加了column,并以这种方式修改函数
CREATE OR REPLACE FUNCTION partitioning_payment()
RETURNS trigger AS
$BODY$
DECLARE
numero integer;
month float;
year float;
monthp float;
yearp float;
months text;
monthps text;
BEGIN
month=DATE_PART('month',NEW.REQUEST_TS);
year=DATE_PART('year',NEW.REQUEST_TS);
monthp=month;
yearp=year;
months=month;
monthps=monthp;
IF month = 12 THEN
yearp=yearp+1;
monthp=1;
ELSE
monthp=monthp+1;
END IF;
monthps=monthp;
IF(length(month::text)=1) THEN
months=month::text;
months='0' || months;
END IF;
IF(length(monthp::text)=1) THEN
monthps=monthp::text;
monthps='0' || monthps;
END IF;
EXECUTE 'SELECT count(*) FROM PG_TABLES WHERE SCHEMANAME=''public'' AND TABLENAME=''payment_' || year || '_' || months || '''' INTO numero;
IF numero=0 THEN
EXECUTE 'CREATE TABLE payment_' || year || '_' || months || '(CHECK ( REQUEST_TS >= ''' || year || '-' || months || '-01 00:00:00'' and REQUEST_TS < ''' || yearp || '-' || monthps || '-01 00:00:00'' )) inherits (payment)';
END IF;
EXECUTE 'INSERT INTO payment_' || year || '_' || months || ' (id,id_request,REQUEST_TS,response,is_test)
VALUES (
'''||NEW.ID||''',
'''||NEW.ID_REQUEST||''',
'''||NEW.REQUEST_TS||''',
'''||NEW.RESPONSE||''',
'||NEW.IS_TEST||'
)';
RETURN null; END;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
但是,现在我遇到了这样的情况:
- 在父 table 和分区 table
is_test
存在
- 如果我在分区 table 上插入,我有新行
- 如果我在父 table 上进行插入,什么也不会发生
那么,我怎样才能正确添加新列并允许我在父列上插入 table?
好吧……真丢人。我已经更新了程序,但您必须删除并重新创建付款触发器 table
我有一个主要的 table(付款)和一些分区的 tables (payment_AAAA_MM)。我获得了这些 tables 与这样的存储功能:
CREATE OR REPLACE FUNCTION partitioning_payment()
RETURNS trigger AS
$BODY$
DECLARE
numero integer;
month float;
year float;
monthp float;
yearp float;
months text;
monthps text;
BEGIN
month=DATE_PART('month',NEW.REQUEST_TS);
year=DATE_PART('year',NEW.REQUEST_TS);
monthp=month;
yearp=year;
months=month;
monthps=monthp;
IF month = 12 THEN
yearp=yearp+1;
monthp=1;
ELSE
monthp=monthp+1;
END IF;
monthps=monthp;
IF(length(month::text)=1) THEN
months=month::text;
months='0' || months;
END IF;
IF(length(monthp::text)=1) THEN
monthps=monthp::text;
monthps='0' || monthps;
END IF;
EXECUTE 'SELECT count(*) FROM PG_TABLES WHERE SCHEMANAME=''public'' AND TABLENAME=''payment_' || year || '_' || months || '''' INTO numero;
IF numero=0 THEN
EXECUTE 'CREATE TABLE payment_' || year || '_' || months || '(CHECK ( REQUEST_TS >= ''' || year || '-' || months || '-01 00:00:00'' and REQUEST_TS < ''' || yearp || '-' || monthps || '-01 00:00:00'' )) inherits (payment)';
END IF;
EXECUTE 'INSERT INTO payment_' || year || '_' || months || ' (id,id_request,REQUEST_TS,response)
VALUES (
'''||NEW.ID||''',
'''||NEW.ID_REQUEST||''',
'''||NEW.REQUEST_TS||''',
'''||NEW.RESPONSE||'''
)';
RETURN null; END;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
CREATE TRIGGER partitioning_payment
BEFORE INSERT
ON payment
FOR EACH ROW
EXECUTE PROCEDURE partitioning_payment();
一切正常。
现在,我必须向父级 table 添加一列(is_test 整数类型)。我在parent table中添加了column,并以这种方式修改函数
CREATE OR REPLACE FUNCTION partitioning_payment()
RETURNS trigger AS
$BODY$
DECLARE
numero integer;
month float;
year float;
monthp float;
yearp float;
months text;
monthps text;
BEGIN
month=DATE_PART('month',NEW.REQUEST_TS);
year=DATE_PART('year',NEW.REQUEST_TS);
monthp=month;
yearp=year;
months=month;
monthps=monthp;
IF month = 12 THEN
yearp=yearp+1;
monthp=1;
ELSE
monthp=monthp+1;
END IF;
monthps=monthp;
IF(length(month::text)=1) THEN
months=month::text;
months='0' || months;
END IF;
IF(length(monthp::text)=1) THEN
monthps=monthp::text;
monthps='0' || monthps;
END IF;
EXECUTE 'SELECT count(*) FROM PG_TABLES WHERE SCHEMANAME=''public'' AND TABLENAME=''payment_' || year || '_' || months || '''' INTO numero;
IF numero=0 THEN
EXECUTE 'CREATE TABLE payment_' || year || '_' || months || '(CHECK ( REQUEST_TS >= ''' || year || '-' || months || '-01 00:00:00'' and REQUEST_TS < ''' || yearp || '-' || monthps || '-01 00:00:00'' )) inherits (payment)';
END IF;
EXECUTE 'INSERT INTO payment_' || year || '_' || months || ' (id,id_request,REQUEST_TS,response,is_test)
VALUES (
'''||NEW.ID||''',
'''||NEW.ID_REQUEST||''',
'''||NEW.REQUEST_TS||''',
'''||NEW.RESPONSE||''',
'||NEW.IS_TEST||'
)';
RETURN null; END;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
但是,现在我遇到了这样的情况:
- 在父 table 和分区 table
is_test
存在 - 如果我在分区 table 上插入,我有新行
- 如果我在父 table 上进行插入,什么也不会发生
那么,我怎样才能正确添加新列并允许我在父列上插入 table?
好吧……真丢人。我已经更新了程序,但您必须删除并重新创建付款触发器 table