gpt4 book ai didi

postgresql - 表 »rowtype« 缺少 FROM 子句条目

转载 作者:行者123 更新时间:2023-11-29 13:17:48 24 4
gpt4 key购买 nike

我目前正在用 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 子句。一般来说,我不明白这个错误,因为我只是想创建一个表(在第 20 行)并设置一些检查约束?

最佳答案

curr_sensor 是 plpgsql 函数内的一个行/记录变量(顺便说一句,缺少所需的声明 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),它带有 Shiny 的新 declarative partitioning取代基于继承的分区。

我最近在 dba.SE 上的这个相关答案中发布了一些带有链接和解释的示例代码:

关于postgresql - 表 »rowtype« 缺少 FROM 子句条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46471784/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com