Why postgres returns unordered data in select query, after updation of row?

Put very simply the “default order” is whatever it happens to read from the disk. Updating a row will not change the row in place… Usually it marks the old row as deleted and writes a new one.

When postgres reads rows from pages of memory, it will (probably) read them in the order they are stored on the page. It will read pages in whatever order it thinks is quickest (that may or may not be how they appear on disk). It can change based on whether or not it decides to use an index. So it can suddenly change without your app asking for anything different.

If you don’t specify an order by it will not take any action to re-order them.

NEVER rely on the default order. It is undefined behaviour.

Leave a Comment