Re: Custom functions for default values for columns on insert
В списке pgsql-general по дате отправления:
| От | Tom Lane |
|---|---|
| Тема | Re: Custom functions for default values for columns on insert |
| Дата | |
| Msg-id | 23595.1187152123@sss.pgh.pa.us обсуждение исходный текст |
| Ответ на | Custom functions for default values for columns on insert ("Lim Berger" <straightfwd007@gmail.com>) |
| Список | pgsql-general |
"Lim Berger" <straightfwd007@gmail.com> writes:
> create table test (id serial primary key, nowd timestamp without time
> zone, processed_id varchar(10));
> create or replace rule test_ins as on insert to test
> DO UPDATE test
> SET processed_id = MYFUNCTION(NEW.id)
> WHERE id = NEW.id
> ;
I think you are getting burnt by the fact that a rule is a macro
and therefore subject to multiple-evaluation-of-arguments hazards.
In particular, the reference to NEW.id probably results in an extra
evaluation (or two?) of nextval() on the serial sequence.
Even if this worked, it'd be horrendously inefficient, because of having
to apply the full machinery of UPDATE to fix up the row. Instead you
should use a BEFORE INSERT trigger to apply the change to the NEW
record before it ever gets stored.
regards, tom lane
В списке pgsql-general по дате отправления:
Сайт использует файлы cookie для корректной работы и повышения удобства. Нажимая кнопку «Принять» или продолжая пользоваться сайтом, вы соглашаетесь на их использование в соответствии с Политикой в отношении обработки cookie ООО «ППГ», в том числе на передачу данных из файлов cookie сторонним статистическим и рекламным службам. Вы можете управлять настройками cookie через параметры вашего браузера