How to delete the oldest X number of rows?

Lists: pgsql-novice
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
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


From: Mathieu Arnold <mat(at)mat(dot)cc>
To: pgsql-novice(at)postgresql(dot)org
Cc: John Roberts <jsrober(at)hotmail(dot)com>
Subject: Re: How to delete the oldest X number of rows?
Date: 2005-12-12 16:42:38
Message-ID: A97C5DE9038833F04CF53C41@andromede.in.absolight.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-novice

+-le 12/12/2005 11:29 -0500, John Roberts a dit :
| 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.

Would that :
DELETE FROM log WHERE timestamp < (select timestamp from log order by
timestamp desc limit 1 offset 50000);

do ?

--
Mathieu Arnold


From: "A(dot) Kretschmer" <andreas(dot)kretschmer(at)schollglas(dot)com>
To: pgsql-novice(at)postgresql(dot)org
Subject: Re: How to delete the oldest X number of rows?
Date: 2005-12-12 16:47:16
Message-ID: 20051212164716.GA32526@webserv.wug-glas.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-novice

am 12.12.2005, um 11:29:58 -0500 mailte John Roberts folgendes:
> 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 < ????

Something like:

select timestamp from log order by timestamp limit 1 offset 49999;

to get the oldest timestamp to keep and delete the early rows?

*untestet*

delete from fol where timestamp < (select timestamp from log order by
timestamp limit 1 offset 49999);

HTH, Andreas
--
Andreas Kretschmer (Kontakt: siehe Header)
Heynitz: 035242/47212, D1: 0160/7141639
GnuPG-ID 0x3FFF606C http://wwwkeys.de.pgp.net
=== Schollglas Unternehmensgruppe ===


From: "John Roberts" <jsrober(at)hotmail(dot)com>
To: "'A(dot) Kretschmer'" <andreas(dot)kretschmer(at)schollglas(dot)com>, <pgsql-novice(at)postgresql(dot)org>
Subject: Re: How to delete the oldest X number of rows?
Date: 2005-12-13 02:28:04
Message-ID: BAY106-DAV229DB7D1EFA59C49975756BE390@phx.gbl
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-novice

Thank you! That works beautifully!

John

-----Original Message-----
From: pgsql-novice-owner(at)postgresql(dot)org
[mailto:pgsql-novice-owner(at)postgresql(dot)org] On Behalf Of A. Kretschmer
Sent: Monday, December 12, 2005 11:47 AM
To: pgsql-novice(at)postgresql(dot)org
Subject: Re: [NOVICE] How to delete the oldest X number of rows?
Importance: High

am 12.12.2005, um 11:29:58 -0500 mailte John Roberts folgendes:
> 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 < ????

Something like:

select timestamp from log order by timestamp limit 1 offset 49999;

to get the oldest timestamp to keep and delete the early rows?

*untestet*

delete from fol where timestamp < (select timestamp from log order by
timestamp limit 1 offset 49999);

HTH, Andreas
--
Andreas Kretschmer (Kontakt: siehe Header)
Heynitz: 035242/47212, D1: 0160/7141639
GnuPG-ID 0x3FFF606C http://wwwkeys.de.pgp.net
=== Schollglas Unternehmensgruppe ===

---------------------------(end of broadcast)---------------------------
TIP 5: don't forget to increase your free space map settings