25

I need to setup worldship to pull from one of our postgres databases. I need to have it so that the packages are sorted by id. I have no way (that i am aware of) of having worldship send the order by clause so I need to have the default for the records returned to be returned by id.

On a second note I have no idea how postgres default sorts it looks like it by the last time the record was changed so if i write a two records id 1,2 then change record 2 when I run the query it returns them with record 2 being first.

Milen A. Radev
  • 57,132
  • 21
  • 101
  • 108
Ominus
  • 5,041
  • 7
  • 38
  • 44
  • 2
    Could you create a view of the table in question whereby things are ordered by id? –  Jul 05 '11 at 16:18

4 Answers4

46

Rows are returned in an unspecified order, per sql specs, unless you add an order by clause. In Postgres, that means you'll get rows in, basically, the order that live rows read on the disk.

If you want a consistent order without needing to add an order by clause, create a view as suggested in Jack's comment.

Denis de Bernardy
  • 72,128
  • 12
  • 122
  • 148
10

There is no such thing as a "default sort". Rows in a table are not sorted.

You could fake this with a view (as suggested by Jack Maney) there is no way you can influence the order of the rows that are returned.

But if you do that, be aware that adding an additional ORDER BY to a SELECT based on that view will sort the data twice.

Another option might be to run the CLUSTER command on that table to physically order the rows on the disk according to the column you want. But this sill does not guarantee that the rows are returned in that order. Not even with a plain SELECT * FROM your_table (but chances are reasonably high for that). You will need to re-run this statement on a regular basis because the order created by the CLUSTER command is not automatically maintained.

okm
  • 23,045
  • 4
  • 78
  • 88
a_horse_with_no_name
  • 497,550
  • 91
  • 775
  • 843
8

For what it's worth, which probably isn't much, PostgreSQL's "default" ordering is based on the time the records were last updated. The most recently updated records will appear last.

Webucator
  • 1,960
  • 18
  • 28
  • Would you have a documentation reference to this? Thanks! – Zero Distraction Jan 06 '22 at 04:48
  • I couldn't find any documentation on this. PostgreSQL doesn't appear to have an _official_ default ordering, but that _in practice_, if you don't specify an order, the last updated records go to the bottom. – Webucator Jan 06 '22 at 13:08
-1

You could eventually use a sorted index, which should guarantee you order of retrieved rows in case the query plan hits the index, or if you force it, but this approach will be more than circuitous :). ORDER BY clause is the way to go as mentioned already.

Daniel Harcek
  • 301
  • 3
  • 7
  • 3
    This is wrong. Even if the retrieval is done through an index, the overall order of the result might not be ordered by the index values - and it it is most definitely not _guaranteed_ (if at all, chances are high that the index order will be kept). – a_horse_with_no_name Jun 16 '15 at 19:30
  • Its wrong. Indexes comes into picture when data is already retrieved, so it wont help. Rather it will confuse user that on which index desired data is present. – Deepak Gangore Aug 27 '19 at 10:39