X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 817C5528D9 for ; Wed, 22 Jun 2005 16:49:28 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 99486-04 for ; Wed, 22 Jun 2005 19:49:21 +0000 (GMT) Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by svr1.postgresql.org (Postfix) with ESMTP id 7F5FD528BB for ; Wed, 22 Jun 2005 16:49:20 -0300 (ADT) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.13.1/8.13.1) with ESMTP id j5MJkf53020667; Wed, 22 Jun 2005 15:46:44 -0400 (EDT) To: Stephan Szabo Cc: Russell Simpkins , pgsql-sql@postgresql.org Subject: Re: after delete trigger behavior In-reply-to: <20050622122313.F42989@megazone.bigpanda.com> References: <20050622092239.T32802@megazone.bigpanda.com> <20364.1119467550@sss.pgh.pa.us> <20050622122313.F42989@megazone.bigpanda.com> Comments: In-reply-to Stephan Szabo message dated "Wed, 22 Jun 2005 12:27:20 -0700" Date: Wed, 22 Jun 2005 15:46:41 -0400 Message-ID: <20666.1119469601@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.006 tagged_above=0 required=5 tests=AWL X-Spam-Level: X-Archive-Number: 200506/278 X-Sequence-Number: 21879 Stephan Szabo 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 <> ORDER BY sort_order LOOP IF rec.sort_order != n THEN UPDATE table SET sort_order = n WHERE <>; 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