I'm building on gsimes's answer as I understand the question.
with agg_temp_stat as (
select date, site_id, ip, block_id, sum(counter)::integer counter
from temp_stat
group by 1, 2, 3, 4
), upd as (
update main_stat t
set counter = counter + s.counter
from agg_tmp_stat s
where
(t.date, t.site_id, t.ip, t.block_id)
= (s.date, s.site_id, s.ip, s.block_id)
returning s.date, s.site_id, s.ip, s.block_id
)
insert into main_stat
select s.date, s.site_id, s.ip, s.block_id, s.counter
from
agg_tmp_stat s
left join
upd on
upd.date = s.date
and upd.site_id = s.site_id
and upd.ip = s.ip
and upd.block_id = s.block_id
where upd.date is null
Basically aggregates the temp table and sums the resulting counter to the already existing one.