First field from my return table is being eaten up
The short answer: use date_trunc('day', ...) to set the time part of a timestamp to 00:00:00



The long answer: the whole function is needlessly complicated. There is no need to create a table each time and then modify the columns or even update that table. 

This can all be done with a single select statement. 

If I understood everything correctly, the query you are looking for is something like this: 

with adjusted as (
  select id, 
         case 
            when entrada &lt; inicio then inicio 
            else entrada 
         end as entrada, 
         case 
           when saida &gt; fim then fim 
           else saida
         end as saida, 
         erro
  from uptime u
  WHERE u.saida &gt;= inicio 
    and u.entrada &lt;= fim 
)
select id, 
       date_trunc('day', entrada) as entrada
       date_trunc('day', saida) as saida
       erro
       saida - entrada as updatime
from adjusted
order by u.entrada;


Note that I "truncated" the columns entrada and saida in the final select which means that the calculation of the uptime does include the time part.

So the whole function can be simplified to:

create or replace function uptimes(inicio date, fim date) 
    returns table (id integer, entrada timestamp with time zone,
                   saida timestamp with time zone, erro boolean, uptime interval) 
as
$$    
    with adjusted as (
      select id, 
             case 
                when entrada &lt; inicio then inicio 
                else entrada 
             end as entrada, 
             case 
               when saida &gt; fim then fim 
               else saida
             end as saida, 
             erro
      from uptime u
      WHERE u.saida &gt;= inicio 
        and u.entrada &lt;= fim 
    )
    select id, 
           date_trunc('day', entrada) as entrada
           date_trunc('day', saida) as saida
           erro
           saida - entrada as updatime
    from adjusted
    order by u.entrada;
$$
language sql;




It is also a good idea to use some kind of prefix for the parameter names, so that they are easier distinguishable from the column names in the query. e.g. p_inicio instead of just inicio