Re: after delete trigger behavior
От | Tom Lane |
---|---|
Тема | Re: after delete trigger behavior |
Дата | |
Msg-id | 20666.1119469601@sss.pgh.pa.us обсуждение исходный текст |
Ответ на | Re: after delete trigger behavior (Stephan Szabo <sszabo@megazone.bigpanda.com>) |
Ответы |
Re: after delete trigger behavior
|
Список | pgsql-sql |
Stephan Szabo <sszabo@megazone.bigpanda.com> writes: > Is there anything we have right now that will handle this kind of thing > without requiring either updating all the counts after a deletion in a > statement trigger or once per row updating all the counts for records with > the same "a" (doing something like make a sequence and using it in a > subselect matching keys)? The best thing I can think of is your first idea, ie, renumbering all the rows in a statement-level AFTER DELETE trigger. Something like (untested) DECLARE rec record; n integer := 1;BEGIN FOR rec IN SELECT * FROM table WHERE <<grouping cols = rec'sgrouping cols>> ORDER BY sort_order LOOP IF rec.sort_order != n THEN UPDATE table SET sort_order= n WHERE <<primary key = rec's primary key>>; END IF; n := n + 1; END LOOP;END; Ugly as this is, it's at least linear in the number of rows to be changed; the originally proposed trigger was O(N^2) in the number of rows affected, and would surely be intolerably slow for multiple deletes in a reasonably sized table. Given an index on the grouping columns plus sort_order, it could even be reasonably fast (don't forget to make the ORDER BY match the index). regards, tom lane
В списке pgsql-sql по дате отправления: