PostgreSQL:更新意味着跨分区移动
(注意:更新了下面采用的答案。)
对于 PostgreSQL 8.1(或更高版本)分区表,如何定义 UPDATE
触发器和过程来“移动”记录从一个分区到另一个分区,如果UPDATE
意味着对定义分区隔离的约束字段的更改?
例如,我有一个表记录分为活动记录和非活动记录,如下所示:
create table RECORDS (RECORD varchar(64) not null, ACTIVE boolean default true);
create table ACTIVE_RECORDS ( check (ACTIVE) ) inherits RECORDS;
create table INACTIVE_RECORDS ( check (not ACTIVE) ) inherits RECORDS;
INSERT 触发器和函数运行良好:新的活动记录放入一个表中,新的非活动记录放入另一个表中。我希望对 ACTIVE 字段进行 UPDATE 来将记录从一个后代表“移动”到另一个后代表,但遇到一个错误,表明这可能是不可能的。
触发器规范和错误消息:
pg=> CREATE OR REPLACE FUNCTION record_update()
RETURNS TRIGGER AS $$
BEGIN
IF (NEW.active = OLD.active) THEN
RETURN NEW;
ELSIF (NEW.active) THEN
INSERT INTO active_records VALUES (NEW.*);
DELETE FROM inactive_records WHERE record = NEW.record;
ELSE
INSERT INTO inactive_records VALUES (NEW.*);
DELETE FROM active_records WHERE record = NEW.record;
END IF;
RETURN NULL;
END;
$$
LANGUAGE plpgsql;
pg=> CREATE TRIGGER record_update_trigger
BEFORE UPDATE ON records
FOR EACH ROW EXECUTE PROCEDURE record_update();
pg=> select * from RECORDS;
record | active
--------+--------
foo | t -- 'foo' record actually in table ACTIVE_RECORDS
bar | f -- 'bar' record actually in table INACTIVE_RECORDS
(2 rows)
pg=> update RECORDS set ACTIVE = false where RECORD = 'foo';
ERROR: new row for relation "active_records" violates check constraint "active_records_active_check"
使用触发器过程(返回 NULL 等)建议我在调用触发器之前检查约束并引发错误,这意味着我当前的方法将不起作用。这可以开始工作吗?
附加答案
pg的[列表分区][2]似乎是实现此目的的最简单方法:
-- untested!
create table RECORDS (..., ACTIVE boolean...)
partition by list(ACTIVE) (
partition ACTIVE_RECORDS values (true),
partition INACTIVE_RECORDS values (false)
)
更新/答案
下面是我最终使用了更新触发过程,分配给每个分区的过程相同。完全归功于 Bell,他的回答为我提供了在分区上触发的关键见解:
CREATE OR REPLACE FUNCTION record_update()
RETURNS TRIGGER AS $$
BEGIN
IF ( (TG_TABLE_NAME = 'active_records' AND NOT NEW.active)
OR
(TG_TABLE_NAME = 'inactive_records' AND NEW.active) ) THEN
DELETE FROM records WHERE record = NEW.record;
INSERT INTO records VALUES (NEW.*);
RETURN NULL;
END IF;
RETURN NEW;
END;
$$
LANGUAGE plpgsql;
(Note: updated with adopted answer below.)
For a PostgreSQL 8.1 (or later) partitioned table, how does one define an UPDATE
trigger and procedure to "move" a record from one partition to the other, if the UPDATE
implies a change to the constrained field that defines the partition segregation?
For example, I've a table records partitioned into active and inactive records like so:
create table RECORDS (RECORD varchar(64) not null, ACTIVE boolean default true);
create table ACTIVE_RECORDS ( check (ACTIVE) ) inherits RECORDS;
create table INACTIVE_RECORDS ( check (not ACTIVE) ) inherits RECORDS;
The INSERT
trigger and function work well: new active records get put in one table, and new inactive records in another. I would like UPDATE
s to the ACTIVE field to "move" a record from one one descendant table to the other, but am encountering an error which suggests that this may not be possible.
Trigger specification and error message:
pg=> CREATE OR REPLACE FUNCTION record_update()
RETURNS TRIGGER AS $
BEGIN
IF (NEW.active = OLD.active) THEN
RETURN NEW;
ELSIF (NEW.active) THEN
INSERT INTO active_records VALUES (NEW.*);
DELETE FROM inactive_records WHERE record = NEW.record;
ELSE
INSERT INTO inactive_records VALUES (NEW.*);
DELETE FROM active_records WHERE record = NEW.record;
END IF;
RETURN NULL;
END;
$
LANGUAGE plpgsql;
pg=> CREATE TRIGGER record_update_trigger
BEFORE UPDATE ON records
FOR EACH ROW EXECUTE PROCEDURE record_update();
pg=> select * from RECORDS;
record | active
--------+--------
foo | t -- 'foo' record actually in table ACTIVE_RECORDS
bar | f -- 'bar' record actually in table INACTIVE_RECORDS
(2 rows)
pg=> update RECORDS set ACTIVE = false where RECORD = 'foo';
ERROR: new row for relation "active_records" violates check constraint "active_records_active_check"
Playing with the trigger procedure (returning NULL and so forth) suggests to me that the constraint is checked, and the error raised, before my trigger is invoked, meaning that my current approach won't work. Can this be gotten to work?
ADDITIONAL ANSWER
pg's [list partitioning][2] appears to be the easiest way to accomplish this:
-- untested!
create table RECORDS (..., ACTIVE boolean...)
partition by list(ACTIVE) (
partition ACTIVE_RECORDS values (true),
partition INACTIVE_RECORDS values (false)
)
UPDATE/ANSWER
Below is the UPDATE
trigger procedure I ended up using, the same procedure assigned to each of the partitions. Credit is entirely to Bell, whose answer gave me the key insight to trigger on the partitions:
CREATE OR REPLACE FUNCTION record_update()
RETURNS TRIGGER AS $
BEGIN
IF ( (TG_TABLE_NAME = 'active_records' AND NOT NEW.active)
OR
(TG_TABLE_NAME = 'inactive_records' AND NEW.active) ) THEN
DELETE FROM records WHERE record = NEW.record;
INSERT INTO records VALUES (NEW.*);
RETURN NULL;
END IF;
RETURN NEW;
END;
$
LANGUAGE plpgsql;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它可以工作,只需要为每个分区定义执行移动的触发器,而不是整个表。因此,像表定义和 INSERT 触发器一样开始
...让我们有一些测试数据...
现在是分区上的触发器。 if NEW.active = OLD.active 检查隐式检查 active 的值,因为我们首先知道表中允许包含哪些内容。
...并测试结果...
It can be made to work, the trigger that does the move just needs to be defined for each partition, not the whole table. So start as you did for table definitions and the INSERT trigger
... let's have some test data ...
Now the triggers on the partitions. The if NEW.active = OLD.active check is implicit in checking the value of active since we know what's allowed to be in the table in the first place.
... and test the results ...
请注意,您可以
按列表
分区,并让数据库完成在分区之间移动行的所有艰苦工作。(未经 8.4 测试,但很可能有效,正如 pilcrow 的评论)。
在以下示例中,使用主键中的列之一创建了一个表,并
按列表
对其进行分区。然后,我们插入一些应以
t_product
或t_default
结尾的数据,具体取决于doc_type
的值。我们检查行是否自动移至右侧表格
现在,让我们将
PRODUCT
转换为ARTICLE
看看会发生什么。可以看到该行不再位于
t_product
分区中,而是位于
t_default
分区中。Beware that you can partition
by list
and let the database do all the hard work to move rows among partitions.(untested for 8.4 but most probably working, as for pilcrow comment).
In the following example, a table is created and partitioned
by list
, using one of the columns in the primary key.Then we insert some data that should end in
t_product
ort_default
, depending on the value ofdoc_type
.We check rows are automatically moved to the right table
Now, let us convert a
PRODUCT
into anARTICLE
to see what happens.It can be seen the row is not in the
t_product
partition anymorebut in the
t_default
partition.