Postgresql规则和返回条款

时间:2016-03-09 17:24:41

标签: postgresql postgresql-9.5

我是Postgresql的新手,我正在深入研究它的功能。 具有条件“do nothing nothing”的更新/删除规则似乎打破了每个更新/删除查询的“返回子句”。 从postgresql 9.5开始有没有解决方案?

这是我运行的简单sql代码段,用于测试:

-- clear && dropdb --if-exists graph && createdb graph -E UTF8 -T template0 && psql graph < schema.sql

create table vertices (
    id bigserial not null primary key,
    hash char(32),
    label varchar(128) not null,
    data jsonb default '{}'
);

-- default groups and users cannot be updated or deleted --
create or replace rule preventDefaultVtxUpdate as
    on update to vertices
    where ( old.label in ('group', 'user') and old.data->>'name' in ('superadmin', 'guest') )
    do instead nothing;

create or replace rule preventDefaultVtxDelete as
    on delete to vertices
    where ( old.label in ('group', 'user') and old.data->>'name' in ('superadmin', 'guest') )
    do instead nothing;

-- default groups and users
insert into vertices (label, data) values ('group', '{"name":"superadmin"}');
insert into vertices (label, data) values ('group', '{"name":"guest"}');

insert into vertices (label, data) values ('user', '{"name":"superadmin"}');
insert into vertices (label, data) values ('user', '{"name":"guest"}');

-- try
update vertices set data = '{}' returning *;
delete from vertices returning *;

大喊:

ERROR:  cannot perform UPDATE RETURNING on relation "vertices"
HINT:  You need an unconditional ON UPDATE DO INSTEAD rule with a RETURNING clause.
ERROR:  cannot perform DELETE RETURNING on relation "vertices"
HINT:  You need an unconditional ON DELETE DO INSTEAD rule with a RETURNING clause.

使用当前解决方案编辑: 评论说,我解决了重写规则作为触发器。这很丑陋但有效。 使用以下触发器而不是规则的任何性能影响?

create table vertices (
    id bigserial not null primary key,
    hash char(32),
    label varchar(128) not null,
    data jsonb default '{}'
);

create or replace function preventDefaultVtxUpdateAndDelete() returns trigger AS $preventDefaultVtxUpdateAndDelete$
    begin
        return null;
    end
$preventDefaultVtxUpdateAndDelete$ language plpgsql;

create trigger preventDefaultVtxUpdateAndDelete
    before update or delete on vertices
    for each row
        when ( old.label in ('group', 'user') and old.data->>'name' in ('superadmin', 'guest') )
        execute procedure preventDefaultVtxUpdateAndDelete();

-- default groups and users
insert into vertices (label, data) values ('group', '{"name":"superadmin"}');
insert into vertices (label, data) values ('group', '{"name":"guest"}');

insert into vertices (label, data) values ('user', '{"name":"superadmin"}');
insert into vertices (label, data) values ('user', '{"name":"guest"}');

    insert into vertices (label, data) values ('user', '{"name":"ciao"}');

-- try
select * from vertices;
update vertices set data = '{}' returning *;
delete from vertices returning *;
select * from vertices;

0 个答案:

没有答案