Re: Efficient DELETE Strategies

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Christoph Haller <ch(at)rodos(dot)fzk(dot)de>
Cc: pgsql-sql(at)postgresql(dot)org, pgsql-hackers(at)postgresql(dot)org
Subject: Re: Efficient DELETE Strategies
Date: 2002-06-10 13:56:27
Message-ID: 5619.1023717387@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers pgsql-sql

Christoph Haller <ch(at)rodos(dot)fzk(dot)de> writes:
> Based on an entry in the mailing list from 30 Oct 2001
> about efficient deletes on subqueries,
> I've found two ways to do so (PostgreSQL 7.2.1):
> ...
> Is there a way to put the second form (more complicated, but faster)
> in one statement?
> Or is there even a third way to delete, which I cannot see?

The clean way to do this would be to allow extra FROM-list relations
in DELETE. We already have a similar facility for UPDATE, so it's not
clear to me why there's not one for DELETE. Then you could do, say,

DELETE FROM onfvalue , onfvalue j WHERE
j.sid= 5 AND
onfvalue.lid = j.lid AND
onfvalue.mid = j.mid AND
onfvalue.timepoint = j.timepoint AND
onfvalue.entrancetime < j.entrancetime ;

If you were using two separate tables you could force this to happen
via an implicit FROM-clause entry, much as you've done in your second
alternative --- but there's no way to set up a self-join in a DELETE
because of the lack of any place to put an alias declaration.

AFAIK this extension would be utterly trivial to implement, since all
the machinery is there already --- for 99% of the backend, it doesn't
matter whether a FROM-item is implicit or explicit. We'd only need to
argue out what the syntax should be. I could imagine

DELETE FROM relation_expr [ , table_ref [ , ... ] ]
[ WHERE bool_expr ]

or

DELETE FROM relation_expr [ FROM table_ref [ , ... ] ]
[ WHERE bool_expr ]

The two FROMs in the second form look a little weird, but they help to
make a clear separation between the deletion target table and the
merely-referenced tables. Also, the first one might look to people
like they'd be allowed to write

DELETE FROM foo FULL JOIN bar ...

which is not any part of my intention (it's very unclear what it'd
mean for the target table to be on the nullable side of an outer join).
OTOH there'd be no harm in outer joins in a separate from-clause, eg

DELETE FROM foo FROM (bar FULL JOIN baz ON ...) WHERE ...

Actually, either syntax above would support that; I guess what's really
bothering me about the first syntax is that a comma suggests a list of
things that will all be treated similarly, while in reality the first
item will be treated much differently from the rest.

Does anyone know whether other systems that support the UPDATE extension
for multiple tables also support a DELETE extension for multiple tables?
If so, what's their syntax?

A somewhat-related issue is that people keep expecting to be able to
attach an alias to the target table name in UPDATE and DELETE; seems
like we get that question every couple months. While this is clearly
disallowed by the SQL spec, it's apparently supported by some other
implementations (else we'd not get the question so much). Should we
add that extension to our syntax? Or should we continue to resist it?

regards, tom lane

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Tom Lane 2002-06-10 14:03:32 Re: tuplesort: unexpected end of data
Previous Message Karel Zak 2002-06-10 13:43:34 Re: Timestamp/Interval proposals: Part 2

Browse pgsql-sql by date

  From Date Subject
Next Message Achilleus Mantzios 2002-06-10 14:40:20 Re: multiple primary keys and reference
Previous Message Joseph Syjuco 2002-06-10 13:51:18 Re: arrays as pgsql function parameters