I have an unusual problem: I need to delete a certain number records from a table, of a particular type; in certain non-compliant DBs it would be easy to accomplish this with DELETE FROM sometable WHERE COND LIMIT X
, but in Postgresql this is clearly not an option.
In some cases I do need to delete the last N records; in other cases it doesn't matter as long as they match the overall criteria. Other than primary key and timestamp, the records are identical.
So what is an (elegant) means of duplicating this functionality? It was suggested that I could do a join on ctid, but that doesn't strike me as an especially "nice" solution; is there something cleaner–and more portable–that I can do instead?
Best Answer
It's easy with window functions. Say you want to delete the last
@N
rows when ordered bysome_column
. You assign aROW_NUMBER()
with a CTE or a derived table and thenDELETE
:or without the
USING
, in a more standard way:Test at SQLFiddle.com