#postgresql #pg_dump

Data ordering in pg_dump output

Question

littlek notes on Stackoverflow:

I am finding that pg_dump will not always dump the database in the same way. It will dump things in a different order every time. Therefore, when I do a diff on the two database dumps, the comparison will result in the two files being different, when they are actually the same, just in a different order.

And then they ask: Is there a different way I can go about doing the pg_dump?

My answer

As of May 2010 a patch to pg_dump exists that may be helpful to all interested in this matter - it adds --ordered option to this utility:

Using --ordered will order the data by primary key or unique index, if one exists, and use the "smallest" ordering (i.e. least number of columns required for a unique order).

Note that --ordered could crush your database server if you try to order very large tables, so use judiciously.

I didn't test it, but I guess it's worth a try.