Received: from localhost (unknown [200.46.204.183]) by postgresql.org (Postfix) with ESMTP id 069E62E470D for ; Tue, 8 Jan 2008 12:31:29 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 10978-01-10 for ; Tue, 8 Jan 2008 12:31:20 -0400 (AST) Received: from py-out-1112.google.com (py-out-1112.google.com [64.233.166.181]) by postgresql.org (Postfix) with ESMTP id 48BC92E4550 for ; Tue, 8 Jan 2008 12:31:09 -0400 (AST) Received: by py-out-1112.google.com with SMTP id d37so5834261pye.29 for ; Tue, 08 Jan 2008 08:31:08 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlemail.com; s=gamma; h=domainkey-signature:received:received:message-id:date:from:to:subject:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; bh=xUYq3KdBQbxHMyyJPjnfQIVi354z5/ZQ0PYZ4H0FgaE=; b=qM7f32YJO94dmWgIqe2PHTuY3UTvTnGntsrqBn1S3isMpPSwGRsbBL1cKXukS9SmXQoWNe5GFmpTNJMCvwO4emCAiLM/L19HOu4Tmw+Zz3Po6BMn5xEP9stL967Blhf6PL6bGmtuzFYjUiOc/nJ8Wvx2xYMf50DoUzacnmP0COY= DomainKey-Signature: a=rsa-sha1; c=nofws; d=googlemail.com; s=gamma; h=message-id:date:from:to:subject:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; b=NF7a57fPep/viqwXD23nuDa9jTS8XBg0soT3rO/hctRJyo6b+O5dTIBEpBo0dSK/M+olPmMAa7UWhU8S/mlrV3aoAzkElOS+5YKMrgf2QJj7+mVXX4/Olk1zPXjgUiuAIxn9cuIEvKmyZyChUvt8EqwZBxUWoq/Nx3AvOfpMmv4= Received: by 10.35.96.11 with SMTP id y11mr26251862pyl.44.1199809868311; Tue, 08 Jan 2008 08:31:08 -0800 (PST) Received: by 10.35.82.11 with HTTP; Tue, 8 Jan 2008 08:31:08 -0800 (PST) Message-ID: Date: Tue, 8 Jan 2008 16:31:08 +0000 From: "Jamie Tufnell" To: pgsql-sql@postgresql.org Subject: Re: How to keep at-most N rows per group? periodic DELETEs or constraints or..? In-Reply-To: MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline References: X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200801/47 X-Sequence-Number: 29900 Hi codeWarrior, codeWarrior wrote: > > For user_ids that have more than 50 rows, I want to keep the most > > recent 50 and delete the rest. > How about using a trigger to call a stored procedure ? [ON INSERT to > user_item_history DO ...] [snip] Thanks for your input! I've implemented this but I'm concerned about performance. As I mentioned it's frequently being added to and this function will be called maybe a couple of times a second. In my brief initial tests it seems like this is running quite slowly... Just to make sure I haven't done anything obviously wrong, I've included my implementation below.. if you could look over it I'd really appreciate it. CREATE OR REPLACE FUNCTION user_item_history_limit() RETURNS TRIGGER AS $_$ DECLARE threshold integer = 50; numrows integer; BEGIN SELECT INTO numrows count(*) FROM user_item_history WHERE user_id = new.user_id; IF numrows > threshold THEN DELETE FROM user_item_history WHERE user_id = new.user_id AND id NOT IN ( SELECT id FROM user_item_history WHERE user_id = new.user_id ORDER BY timestamp DESC LIMIT threshold); RAISE NOTICE '% rows exceeds threshold of % for user_id %; trimming..', numrows, threshold, new.user_id; END IF; RETURN new; END; $_$ LANGUAGE plpgsql; CREATE TRIGGER user_item_history_limit AFTER INSERT ON user_item_history FOR EACH ROW EXECUTE PROCEDURE user_item_history_limit(); Any suggestions greatly appreciated! Thanks again, Jamie