Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change order of the table rows in PostgreSQL

I have a table in PostgreSQL. I want to re-order the rows data physically according to a specific column (which is not primary key). In my case this column type is date. How can I do it?

like image 572
f.ashouri Avatar asked Jan 05 '13 20:01

f.ashouri


People also ask

How do I sort a table in PostgreSQL?

The ORDER BY clause in PostgreSQL is used together with the SELECT statement to sort table data. The table data can either be sorted in ascending or descending order. By default, the data is sorted in ascending order.

How do I change the order of columns in a Postgres table?

Postgres currently defines column order based on the attnum column of the pg_attribute table. The only way to change column order is either by recreating the table, or by adding columns and rotating data until you reach the desired layout.

Does Postgres maintain order?

Unfortunately it is not. If you want to retrieve rows in an order, you should use a sequence ( like Id column, primary key ) and an order by clause regarding to this id column.


1 Answers

If you have an index on that column, then the CLUSTER command will physically "order" the rows according to that index

CLUSTER [VERBOSE] table_name [ USING index_name ]

http://www.postgresql.org/docs/current/static/sql-cluster.html

Note that this "order" isn't automatically maintained, you need to run that statement on a regular basis manually.


This will however not guarantee any specific order when retrieving the rows. Not even when no joins or aggregates are involved.

Even if all you do is select * from the_table the order in which the rows are returned is still not guaranteed. For example: Postgres has a feature called "synchronized seq scan", which means that if one session starts a seq scan (select * from ...) and another session is doing the same thing, the second one piggy-backs on the first seq scan (where ever that is) and then adds the "missed" rows at the end of the result.

The only way to guarantee an order of a result set (really: the only) is to supply an order by clause.


This only makes sense (at least to me) if you have a single harddisk in your server (which is not a SSD). In that case a seq scan might be faster because all blocks might be right next to each other (which isn't guaranteed either because of the way a file system re-uses free space).

On a SSD or a proper server which uses a RAID array with many, many hard disks I can't see how this could be beneficial in any way.

like image 180
a_horse_with_no_name Avatar answered Sep 23 '22 06:09

a_horse_with_no_name