缺少FROM子句条目表»rowtype«

时间:2017-09-28 14:34:43

标签: postgresql plpgsql partitioning dynamic-sql postgresql-9.4

我目前正在用plpgsql语言编写一个函数来创建分区,这些分区将保存每个月的传感器数据(一个分区一个月和传感器)。我坚持这个错误:

ERROR: missing FROM-clause entry for table »curr_sensor«
SQL state: 42P01
Context: SQL-Query »CREATE TABLE sensor_1_201609 (CHECK (timestamp >= date(curr_sensor.timestamp) AND timestamp < (date(curr_sensor.timestamp) + interval '1 month'))) INHERITS (sensor_fake_data)«
PL/pgSQL-Function create_partition_per_sensor_and_month() Line 20 at EXECUTE

该功能如下:

CREATE OR REPLACE FUNCTION create_partition_per_sensor_and_month() RETURNS text as
$BODY$
-- declare variables
DECLARE
    loop_count integer := 0;
    curr_sensor sensor_fake_data%rowtype;
    curr_partition_limit timestamp;
    partition_table_name text;
    partition_index_timestamp_name text;
    partition_index_id_name text;
-- begin with the for loop...loop over every sensor and partition based on sensor and the timestamp
BEGIN                                                                   --replace mumber with variable
    FOR curr_sensor IN SELECT * FROM sensor_fake_data WHERE sensor_id = 1 ORDER BY timestamp ASC
    LOOP
        IF (loop_count = 0 OR curr_sensor.timestamp > curr_partition_limit) THEN
            curr_partition_limit := curr_sensor.timestamp + interval '1 month';
            partition_table_name := 'sensor_' || cast(curr_sensor.sensor_id as text) || '_' || to_char(date(curr_sensor.timestamp), 'YYYYMM');
            partition_index_timestamp_name := 'index_timestamp_' || cast(curr_sensor.sensor_id as text) || date(curr_sensor.timestamp);
            partition_index_id_name := 'index_id_' || cast(curr_sensor.sensor_id as text) || date(curr_sensor.timestamp);
            EXECUTE 'CREATE TABLE ' || partition_table_name || ' (CHECK (timestamp >= date(curr_sensor.timestamp) ' || 
                     'AND timestamp < (date(curr_sensor.timestamp) + interval ' || '''1 month''' || '))) ' ||
                     'INHERITS (sensor_fake_data)';
            -- add index to the new table
            EXECUTE format('CREATE INDEX %I ON %I (timestamp)', partition_index_timestamp_name, partition_table_name);
            EXECUTE format('CREATE INDEX %I ON %I (sensor_id)', partition_index_id_name, partition_table_name);
            EXECUTE format('INSERT INTO %I VALUES(curr_sensor.*)', partition_table_name);
        ELSE
            EXECUTE format('INSERT INTO %I VALUES(curr_sensor.*)', partition_table_name);
        END IF;
        loop_count := loop_count + 1;
    END LOOP;
END;
$BODY$

我不明白为什么缺少一个FROM-Clause。一般来说,我不理解错误,因为我只是想创建一个表(在第20行)并设置一些检查约束?

1 个答案:

答案 0 :(得分:1)

curr_sensor是plpgsql函数内的行/记录变量(btw缺少必需的声明LANGUAGE plpgsql。)

但是您的EXECUTE尝试创建一个带有CHECK约束的表,引用一个名为curr_sensor,这显然不存在。也不应该。

相反,传递列curr_sensor.timestamp的正确引用的(!)以连接正确的DDL语句。看起来很奇怪你不会在你的函数中使用format()来实现其他更简单的命令。用它来清理它,并正确引用:

...
   EXECUTE format('CREATE TABLE %I (CHECK (timestamp >= %L AND timestamp < %L))
                   INHERITS (sensor_fake_data)'
                 , partition_table_name
                 , curr_sensor.timestamp::date
                 , (curr_sensor.timestamp + interval '1 month')::date);
...

可能会有更多问题,我没有进一步了解。

相关:

无论哪种方式,请考虑升级到 Postgres 10 (目前是RC),它附带闪亮的新declarative partitioning以替换基于继承的分区。

我最近在dba.SE上发布了一些带有链接和解释的示例代码: