4

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.

2
  • There is no internal order in a Postgres database. You should delete 1500 records with regard to some column order. Commented Jun 9, 2016 at 10:29
  • There is no such thing as the "bottom 1500 rows" in a relational database. Commented Jun 9, 2016 at 11:05

1 Answer 1

6
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.

Sign up to request clarification or add additional context in comments.

1 Comment

Note that ctid IN (SELECT ctid FROM ... LIMIT n) is awfully slow. The following runs way faster: ctid = ANY(ARRAY(SELECT ctid FROM ... LIMIT n))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.