"Aggregate (cost=139934.30..139934.31 rows=1 width=8)"
" -> Bitmap Heap Scan on params (cost=908.12..139837.85 rows=38578 width=8)"
" Recheck Cond: (counter_id = ANY ('{123,345}'::integer[]))"
" -> Bitmap Index Scan on counter_id_time (cost=0.00..898.48 rows=38578 width=0)"
" Index Cond: (counter_id = ANY ('{123,345}'::integer[]))"
"Result (cost=4.59..4.60 rows=1 width=0) (actual time=36.492..36.492 rows=1 loops=1)"
" Buffers: shared hit=1 read=4"
" InitPlan 1 (returns $0)"
" -> Limit (cost=0.57..4.59 rows=1 width=8) (actual time=36.487..36.487 rows=1 loops=1)"
" Buffers: shared hit=1 read=4"
" -> Index Only Scan Backward using counter_id_time on params (cost=0.57..77534.35 rows=19289 width=8) (actual time=36.486..36.486 rows=1 loops=1)"
" Index Cond: ((counter_id = 8888) AND ("time" IS NOT NULL))"
" Heap Fetches: 1"
" Buffers: shared hit=1 read=4"
"Planning time: 0.198 ms"
"Execution time: 36.525 ms"
"Aggregate (cost=139934.30..139934.31 rows=1 width=8) (actual time=81536.479..81536.479 rows=1 loops=1)"
" Buffers: shared hit=222 read=19201"
" -> Bitmap Heap Scan on params (cost=908.12..139837.85 rows=38578 width=8) (actual time=50.128..81522.183 rows=19420 loops=1)"
" Recheck Cond: (counter_id = ANY ('{8888,8888}'::integer[]))"
" Heap Blocks: exact=19343"
" Buffers: shared hit=222 read=19201"
" -> Bitmap Index Scan on counter_id_time (cost=0.00..898.48 rows=38578 width=0) (actual time=47.687..47.687 rows=19420 loops=1)"
" Index Cond: (counter_id = ANY ('{8888,8888}'::integer[]))"
" Buffers: shared hit=4 read=76"
"Planning time: 0.202 ms"
"Execution time: 81536.674 ms"
"Aggregate (cost=139934.30..139934.31 rows=1 width=8) (actual time=132113.630..132113.630 rows=1 loops=1)"
" Buffers: shared hit=112 read=39422"
" -> Bitmap Heap Scan on params (cost=908.12..139837.85 rows=38578 width=8) (actual time=111.489..132081.067 rows=39693 loops=1)"
" Recheck Cond: (counter_id = ANY ('{9876,6789}'::integer[]))"
" Heap Blocks: exact=39373"
" Buffers: shared hit=112 read=39422"
" -> Bitmap Index Scan on counter_id_time (cost=0.00..898.48 rows=38578 width=0) (actual time=93.849..93.849 rows=39693 loops=1)"
" Index Cond: (counter_id = ANY ('{9876,6789}'::integer[]))"
" Buffers: shared hit=2 read=159"
"Planning time: 0.238 ms"
"Execution time: 132113.965 ms"
SELECT MAX(time) FROM params WHERE counter_id IN (6001, 15998);
SELECT LEAST(COALESCE(t1, t2), COALESCE(t2, t1)) FROM
(SELECT MIN(TIME) t1 FROM params WHERE counter_id = 11858 AND time >= 3629232000000) p1,
(SELECT MAX(TIME) t2 FROM params WHERE counter_id = 11858 AND time < 3629232000000) p2;