Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete the Certain number of Rows from Postgresql table

Tags:

postgresql

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.

like image 628
M.S.Naidu Avatar asked Sep 14 '25 05:09

M.S.Naidu


1 Answers

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.

like image 161
khelili miliana Avatar answered Sep 15 '25 19:09

khelili miliana