M.S.Naidu
M.S.Naidu

Reputation: 2289

Delete the Certain number of Rows from Postgresql table

I am new to postgresql, I have inserted 5000 rows in my table but I just want to delete the 1500 rows from 5000 rows. Here I don't have any constraint to delete those rows. I have to delete the top or bottom 1500 rows from the table.

I have googled a lot but I have not get any clue to delete the rows without any constraint.

Any suggestion would be great.

Upvotes: 4

Views: 5659

Answers (1)

khelili miliana
khelili miliana

Reputation: 3822

DELETE FROM YourTable
WHERE ctid IN (
SELECT ctid
FROM YourTable
ORDER BY timestamp
LIMIT 1500
)

ctid is: The physical location of the row version within its table. Note that although the ctid can be used to locate the row version very quickly, a row's ctid will change if it is updated or moved by VACUUM FULL. Therefore ctid is useless as a long-term row identifier. The OID, or even better a user-defined serial number, should be used to identify logical rows.

Upvotes: 6

Related Questions