By default, PostGIS calculates the Envelope or Extent of a polygon based on the bounding box of the polygon ((MINX, MINY), (MINX, MAXY), (MAXX, MAXY), (MAXX, MINY), (MINX, MINY)).
This gives a result like so:

However, I'm looking for a result more like this:

As far as I know, the best algorithm that I can come up with is:
- Determine angle
ato rotate the polygonXto be parallel with the x-axis - Rotate
Xadegrees, calculate the envelopeYof rotated polygonX - Rotate
Y-adegrees
How could you calculate step #1 in PostGIS?
Here's an implementation. It doesn't handle degenerate cases and you may not like the rectangle chose if the areas tie. I'm working on a submission for postgis, but this should be enough to work with until I get a patch into postgis.