pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Stephan Szabo <sszabo@megazone.bigpanda.com>
Cc: Russell Simpkins <russellsimpkins@hotmail.com>
Cc: pgsql-sql@postgresql.org
Subject: Re: after delete trigger behavior
Date: Wed, 22 Jun 2005 15:46:41 -0400
Message-ID: <20666.1119469601@sss.pgh.pa.us> (raw)
In-Reply-To: <20050622122313.F42989@megazone.bigpanda.com>
References: <BAY103-F108CECCE4CF4F95511684B5EB0@phx.gbl>
	<20050622092239.T32802@megazone.bigpanda.com>
	<20364.1119467550@sss.pgh.pa.us>
	<20050622122313.F42989@megazone.bigpanda.com>

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'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



view thread (8+ messages)  latest in thread

Message-ID: <20666.1119469601@sss.pgh.pa.us>
Permalink:  ../20666.1119469601@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/20666.1119469601@sss.pgh.pa.us

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: tgl@sss.pgh.pa.us, sszabo@megazone.bigpanda.com, russellsimpkins@hotmail.com
  Subject: Re: after delete trigger behavior
  In-Reply-To: <20666.1119469601@sss.pgh.pa.us>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox