Received: from maia.hub.org (unknown [200.46.204.183]) by mail.postgresql.org (Postfix) with ESMTP id ACAE463470E for ; Wed, 30 Dec 2009 18:18:00 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 09137-01-2 for ; Wed, 30 Dec 2009 22:17:49 +0000 (UTC) Received: from news.hub.org (news.hub.org [200.46.204.72]) by mail.postgresql.org (Postfix) with ESMTP id 5E60D642083 for ; Wed, 30 Dec 2009 17:42:41 -0400 (AST) Received: from news.hub.org (news.hub.org [200.46.204.72]) by news.hub.org (8.14.3/8.14.3) with ESMTP id nBULgbNL094392 for ; Wed, 30 Dec 2009 17:42:37 -0400 (AST) (envelope-from news@news.hub.org) Received: (from news@localhost) by news.hub.org (8.14.3/8.14.3/Submit) id nBULKktW087628 for pgsql-sql@postgresql.org; Wed, 30 Dec 2009 17:20:46 -0400 (AST) (envelope-from news) From: Chris Browne X-Newsgroups: pgsql.sql Subject: Re: PSQL statement to delete 100 rows meeting certain criteria Date: Wed, 30 Dec 2009 16:20:43 -0500 Organization: cbbrowne Computing Inc Lines: 37 Message-ID: <87ljgk2m6s.fsf@dba2.int.libertyrms.com> References: <7e2a8d5e-f7a7-4b7a-b35b-e361544e4b4a@p8g2000yqb.googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Complaints-To: usenet@news.hub.org User-Agent: Gnus/5.110009 (No Gnus v0.9) XEmacs/21.4.22 (linux) Cancel-Lock: sha1:Ma+ZnzSdUbmcCSCdwS1hnJz1b1o= To: pgsql-sql@postgresql.org X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.001 tagged_above=-10 required=5 tests=BAYES_50=0.001 X-Spam-Level: X-Archive-Number: 200912/90 X-Sequence-Number: 33528 shulkae 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, it would 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!"