Re: using limit with delete

From: Alvaro Herrera <alvherre(at)dcc(dot)uchile(dot)cl>
To: Neil Conway <neilc(at)samurai(dot)com>
Cc: Chris Smith <chris(at)interspire(dot)com>, pgsql-general(at)postgresql(dot)org
Subject: Re: using limit with delete
Date: 2005-04-07 02:20:07
Message-ID: 20050407022007.GB25320@dcc.uchile.cl
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Thu, Apr 07, 2005 at 12:02:24PM +1000, Neil Conway wrote:
> Chris Smith wrote:
> >I'm trying to use a limit clause with delete, but it doesn't work at the
> >moment
>
> It isn't in the SQL standard, and it would have undefined behavior: the
> sort order of a result set without ORDER BY is unspecified, so you would
> have no way to predict which rows DELETE would remove.
>
> >delete from table where x='1' limit 1000;
>
> You could use a subquery to achieve this:
>
> DELETE FROM table WHERE x IN
> (SELECT x FROM table ... ORDER BY ... LIMIT ...);

In particular, x can be the system column "ctid", a trick which could be
useful to remove duplicates, for example.

--
Alvaro Herrera (<alvherre[(at)]dcc(dot)uchile(dot)cl>)
"Porque Kim no hacia nada, pero, eso sí,
con extraordinario éxito" ("Kim", Kipling)

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Chris Smith 2005-04-07 02:27:07 Re: using limit with delete
Previous Message Mike Rylander 2005-04-07 02:13:42 Re: COPY command use UTF-8 encoding and NOT UNICODE(16bits)... please confirm. Should postgresql add :set CLIENT_ENCODING to 'UTF-8'; to avoid confusion