Do you remember ST_Reclass? How about getting good classes? There are two very interesting tools in PostGIS. ST_Histogram() and ST_Quantile(). First, return SETOF record of the defined number of histogram bins. The only thing we need to set is the raster column, band, and number of bins. We can break results into min, max, count, and percent with SELECT (histogram).* syntax.
mastering_postgis=# SELECT (stat).* FROM (SELECT ST_Histogram(rast,1,6) AS stat FROM eudem.clip) AS foo;
min | max | count | percent
------------------+------------------+-------+---------------------
440.980010986328 | 549.598332722982 | 19517 | 0.103899491602119
549.598332722982 | 658.216654459635 | 76015 | 0.404668742846496
658.216654459635 | 766.834976196289 | 55312 | 0.29445553514866
766.834976196289 | 875.453297932943 | 26941 | 0.14342143788762
875.453297932943 | 984.071619669597 | 8320 | 0.0442918363544412
984.071619669597 | 1092.68994140625 | 1740 | 0.00926295616066438
(6 rows)
The second tool helps us to select class breaks. Here, the execution is even easier-only raster column and band are necessary.
mastering_postgis=# SELECT (stat).* FROM (SELECT ST_Quantile(rast,1) AS stat FROM eudem.clip) AS foo;
quantile | value
----------+------------------
0 | 440.980010986328
0.25 | 593.859985351562
0.5 | 655.929992675781
0.75 | 742.580017089844
1 | 1092.68994140625
(5 rows)
It is possible to set real quantile percents and get values with ARRAY[].
mastering_postgis=# SELECT (stat).* FROM (SELECT ST_Quantile(rast,1,ARRAY[0.3,0.6,0.88]) AS stat FROM eudem.clip) AS foo;
quantile | value
----------+------------------
0.3 | 606.700012207031
0.6 | 684.010009765625
0.88 | 806.630004882812
(3 rows)