I find this a bit mysterious. The cost of getting the maximum value out of just the partition parent table (which is empty) is very cheap:
evlampts=# explain select max(timestamp) from only mcdata;
QUERY PLAN
-------------------------------------------------------------------------------------------------------
Result (cost=0.15..0.16 rows=1 width=0)
InitPlan 1 (returns $0)
-> Limit (cost=0.00..0.15 rows=1 width=8)
-> Index Scan Backward using mcdatatime_idx on mcdata (cost=0.00..19.20 rows=130 width=8)
Index Cond: ("timestamp" IS NOT NULL)
(5 rows)
Likewise, the cost of getting the maximum value out of just the newest partition is very cheap.
evlampts=# explain select max(timestamp) from mcdata_2011_09;
QUERY PLAN
------------------------------------------------------------------------------------------------------------------------------------------
Result (cost=3.17..3.18 rows=1 width=0)
InitPlan 1 (returns $0)
-> Limit (cost=0.00..3.17 rows=1 width=8)
-> Index Scan Backward using mcdata_2011_09_timestamp_idx on mcdata_2011_09 (cost=0.00..560255403.07 rows=176936192 width=8)
Index Cond: ("timestamp" IS NOT NULL)
(5 rows)
Here's the weird thing. Getting the maximum from the whole partitioned table is very expensive. PostgreSQL doesn't seem to produce efficient subplans, even though there are only two tables here (the master and one partition) and both can be queried separately efficiently.
evlampts=# explain select max(timestamp) from mcdata;
QUERY PLAN
-----------------------------------------------------------------------------------------------
Aggregate (cost=4663394.03..4663394.04 rows=1 width=8)
-> Append (cost=0.00..4221053.22 rows=176936322 width=8)
-> Seq Scan on mcdata (cost=0.00..11.30 rows=130 width=8)
-> Seq Scan on mcdata_2011_09 mcdata (cost=0.00..4221041.92 rows=176936192 width=8)
(4 rows)
Is there a configuration tunable I've missed or is this a limitation I don't understand?
Thanks!