1

我有一张桌子:

dw_readings(date_key, time_key, account_key, reading_value, consumption, processed_date) 

date_key按年份划分。我现在需要向reading_id主表添加一列以支持新功能,但是使用该alter table语句似乎无法正常工作。在应用alter添加新reading_id列之后,尽管在语句中设置了值,但任何insert into dw_readings结果都reading_id被设置为;这可以通过 Java JDBC 和 pgAdmin 重现。但是,正确设置工作的语句允许我设置列值。nullinsertupdatereading_id

正在使用以下语句更改表

ALTER dw_readings ADD COLUMN reading_id INTEGER;

我需要知道的是如何正确地将新列添加到分区表中,以便插入正常工作。

4

3 回答 3

2

我设法找到了代码中的问题。问题与为分区创建的规则集有关。

    CREATE OR REPLACE RULE dw_readings_insert_y2009 AS
       ON INSERT TO dw_readings
       WHERE new.date_key >= 20090101 AND new.date_key < 20100101
       DO INSTEAD 
    INSERT INTO dw_readings_y2009 (date_key, time_key, account_key, reading_value, consumption, processed_date) 
      VALUES (new.date_key, new.time_key, new.account_key, new.reading_value, new.consumption, new.processed_date);

此规则不包括新列,因此将始终为 reading_id 插入 null。解决方案是将 reading_id 添加到 DO INSTEAD 的 INSERT 语句中。

于 2012-04-20T11:40:49.183 回答
1

I can't reproduce it. Note that when you create a new column all the pre existing rows will have a null value in that column:

create table dw_readings(date_key date);
create table dw_readings_2012(
    check (extract(year from date_key) = 2012)
)   inherits(dw_readings);
;
insert into dw_readings_2012 (date_key) values ('2012-01-01'::date);
alter table dw_readings add column reading_id integer;
insert into dw_readings_2012 (date_key, reading_id) values ('2012-01-02'::date, 2);
select *
from dw_readings
;
  date_key  | reading_id 
------------+------------
 2012-01-01 |           
 2012-01-02 |          2
(2 rows)
于 2012-04-05T12:58:17.840 回答
0

正如@xstrike 提到的,重新加载规则应该足够了。例如,如果您在插入时有符文,则必须在每个分区上替换它:

CREATE OR REPLACE RULE table_partition_201608_rule
AS ON INSERT TO table_master WHERE date BETWEEN '2016-08-01 00:00:00.000' AND '2016-08-31 23:59:59.999'
DO INSTEAD (
  INSERT INTO table_partition_201608 VALUES (NEW.*)
);
于 2016-08-31T20:09:42.937 回答