From: | Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> |
---|---|
To: | Stephan Szabo <sszabo(at)megazone(dot)bigpanda(dot)com> |
Cc: | Russell Simpkins <russellsimpkins(at)hotmail(dot)com>, pgsql-sql(at)postgresql(dot)org |
Subject: | Re: after delete trigger behavior |
Date: | 2005-06-22 19:46:41 |
Message-ID: | 20666.1119469601@sss.pgh.pa.us |
Views: | Whole Thread | Raw Message | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-sql |
Stephan Szabo <sszabo(at)megazone(dot)bigpanda(dot)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's grouping 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
From | Date | Subject | |
---|---|---|---|
Next Message | Russell Simpkins | 2005-06-22 20:09:45 | Re: after delete trigger behavior |
Previous Message | Joel Fradkin | 2005-06-22 19:36:31 | encoding question |