How to delete the oldest X number of rows?

From: "John Roberts" <jsrober(at)hotmail(dot)com>
To: pgsql-novice(at)postgresql(dot)org
Subject: How to delete the oldest X number of rows?
Date: 2005-12-12 16:29:58
Message-ID: BAY106-F2568E2920C04BCD4FC60ACBE460@phx.gbl
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

Hi,

I have a table that logs the performance of a system. I get an entry in this
table each time the system does something.

I want to keep the last 50,000 rows in the table. I'd like to run a cron job
every 5 minutes that will delete all rows > 50,000, oldest first. What's the
most efficient way to do this query/delete?

DELETE FROM log WHERE timestamp < ????

I can come up with a way to do this using COUNT(*), but I'll bet there is a
smarter way.

THANKS for your help!

John

Responses

Browse pgsql-novice by date

  From Date Subject
Next Message Mathieu Arnold 2005-12-12 16:42:38 Re: How to delete the oldest X number of rows?
Previous Message Tom Lane 2005-12-12 15:21:48 Re: transaction ID wrap limit is 2147484146 Start Postgresql with error in Logfile