Strange behavior of query with pg_sleep I have plpgsql anonymous block. PostgreSQL 10. I store current time to variable, raise notice with value of the variable, sleep for 20 seconds, store current time to another variable, store both variables into table.
What I expect to see: difference in time between 1st and 2nd variables would be 20 seconds. Instead, I see what query execute for 20 seconds as expected, but values of variables are same. How it can be? 

Code to reproduce:

CREATE schema if not exists tmd;

CREATE table if not exists tmd.gt_recommended_log (
    id bigserial NOT NULL,
    start_ts timestamp NOT NULL,
    end_ts timestamp null,
    CONSTRAINT gt_recommended_log_pkey PRIMARY KEY (id)
);

delete from tmd.gt_recommended_log;

do $$
declare _a timestamp;
declare _b timestamp;
begin
   _a := now();
  raise notice '%s', _a::varchar(30);

  perform pg_sleep(20);

  _b := now();

  insert into tmd.gt_recommended_log (start_ts, end_ts)
      values(_a, _b);

  raise notice '%s', _b::varchar(30);
  raise notice '%s', now()::varchar(30);

end ; $$ language plpgsql;

select * from tmd.gt_recommended_log;
Found explanation. 

Now() contains timestamp of transaction start.

It is necessary to use TIMEOFDAY().