Re: PSQL statement to delete 100 rows meeting certain criteria - Mailing list pgsql-sql

From Chris Browne
Subject Re: PSQL statement to delete 100 rows meeting certain criteria
Date
Msg-id 87ljgk2m6s.fsf@dba2.int.libertyrms.com
Whole thread Raw
List pgsql-sql
shulkae <shulkae@gmail.com> writes:
> I am newbie to postgres/SQL.
>
> I want to delete all rows exceeding 400 hours (10 days) and I want to
> limit deletion of only 100 records at a time.
>
> I was trying the following in PostgreSQL:
>
> DELETE from mytable WHERE (now() - timestamp_field  > INTERVAL '400
> hour' ) LIMIT 100;
>
> Looks like DELETE syntax doesn't support LIMIT.
>
> Is there any other way to achieve this?

Sure...

Supposing mytable has, as a unique key, column "id"...
 delete from mytable where  id in (select id from mytable          where timestamp_field < now() - '400
hours'::interval         limit 100);
 

I once set up a process where I did this exact sort of thing, complete
with a "back-off" scheme where each iteration would check a sequence to
see if a lot of new tuples had come in since last time, and:
 a) If the system was looking busy, it would only delete a small    bit of data; b) If the system was not busy at all,
itwould delete quite a bit    more obsolete data.
 
-- 
output = reverse("ofni.secnanifxunil" "@" "enworbbc")
Signs of a Klingon Programmer #1: "Our users will  know fear and cower
before our software. Ship it! Ship it and let  them flee like the dogs
they are!"


pgsql-sql by date:

Previous
From: Joshua Tolley
Date:
Subject: Re: [GENERAL] DataBase Problem
Next
From: Brian Sherwood
Date:
Subject: loading a file into a field