Re: BUG #17827: Rule on insert into table doesn't work when excepting select from the table itself

Поиск
Список
Период
Сортировка
От Tom Lane
Тема Re: BUG #17827: Rule on insert into table doesn't work when excepting select from the table itself
Дата
Msg-id 377748.1678374108@sss.pgh.pa.us
обсуждение исходный текст
Ответ на BUG #17827: Rule on insert into table doesn't work when excepting select from the table itself  (PG Bug reporting form <noreply@postgresql.org>)
Ответы Re: BUG #17827: Rule on insert into table doesn't work when excepting select from the table itself  (Hanna Yanchurevich <hyanchurevich@spotware.com>)
Список pgsql-bugs
PG Bug reporting form <noreply@postgresql.org> writes:
> create table tbl (id int);
> create table rule_stat (msg text, id int);
> create rule rule_tbl as on insert to tbl do insert into rule_stat
> values('Rule triggered for ',new.id);

> insert into tbl 
> select 1
> except
> select id from tbl;

> table rule_stat; -- no rows

This is not a bug.  The DO ALSO command executes after the original
INSERT command, and what it executes looks basically like

insert into rule_stat
select 'Rule triggered for ', id from
(select 1
 except
 select id from tbl);

But at this point we've already completed the original INSERT,
so now there is a row with id 1 in "tbl", and thus the EXCEPT
produces nothing.

While there are use-cases for this sort of behavior, most people
find that propagating data to another table is better done with
an AFTER trigger.  It's far easier to understand what will happen.

            regards, tom lane



В списке pgsql-bugs по дате отправления:

Предыдущее
От: Tom Lane
Дата:
Сообщение: Re: PostgreSQL 14.7 "ALTER TABLE IF EXISTS" fails - ERROR: schema/relation "" does not exist
Следующее
От: "Salavessa, Joao (Senior Developer)"
Дата:
Сообщение: RE: [EXTERNAL] Re: PostgreSQL 14.7 "ALTER TABLE IF EXISTS" fails - ERROR: schema/relation "" does not exist