Re: Chunk Delete

From: Gregory Stark <stark(at)enterprisedb(dot)com>
To: "Abraham\, Danny" <danny_abraham(at)bmc(dot)com>
Cc: <pgsql-general(at)postgresql(dot)org>
Subject: Re: Chunk Delete
Date: 2007-11-15 17:13:32
Message-ID: 87abpfwhxf.fsf@oxford.xeocode.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

"Abraham, Danny" <danny_abraham(at)bmc(dot)com> writes:

> Hi,
>
> I am wondering if I can do in PG Chunck Delete, like the Oracle example
> below.
>
> In Oracle we erase 50,000 records using the following:
>
> Delete <table name> where <condition> and ROWNUM < 50000;
>
> Do we have such a feature in PG?

You can still use a subquery even if you don't have a primary key. You use the
"ctid" which is Postgres's notion of the physical location of the record. You
do have to use another non-standard extension DELETE ... USING.

DELETE
FROM atable AS x
USING (SELECT ctid FROM atable LIMIT 50000) AS y
WHERE x.ctid = y.ctid;

(The ctid can change when it's updated and even be reused by another record
but within a single query like this it's safe though it's possible the query
will delete fewer than 50000 records though if one of the records is updated
while the delete is running.)

You should note this will delete 50,000 arbitrary records. Not necessarily the
50,000 oldest ones or anything useful.

--
Gregory Stark
EnterpriseDB http://www.enterprisedb.com
Ask me about EnterpriseDB's Slony Replication support!

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Madison Kelly 2007-11-15 17:13:54 PostgreSQL 8.2.5 compile problem
Previous Message Joshua D. Drake 2007-11-15 17:08:26 Re: PLpgsql debugger question