Thread

  1. How to do things like \timing on in a do loop?

    Shaozhong SHI <shishaozhong@gmail.com> — 2022-10-15T19:32:59Z

    DO
    $do$
    declare
    
    j int=1;
    
    BEGIN
    
    for j in 1..100 loop
    
    \timing on
    
    select * from a_table;
    
    end loop;
    
    end
    $$;
    
    
    It appears that \timing on is not allowed in a do statement.
    
    Any alternative to do that in a do statement?
    
    Regards,
    
    David
    
  2. Re: How to do things like \timing on in a do loop?

    Erik Brandsberg <erik@heimdalldata.com> — 2022-10-15T20:04:41Z

    You can't, as \timing is not a postgres command.  It is a psql command, so
    is not interpreted by the server itself.
    
    On Sat, Oct 15, 2022, 3:33 PM Shaozhong SHI <shishaozhong@gmail.com> wrote:
    
    > DO
    > $do$
    > declare
    >
    > j int=1;
    >
    > BEGIN
    >
    > for j in 1..100 loop
    >
    > \timing on
    >
    > select * from a_table;
    >
    > end loop;
    >
    > end
    > $$;
    >
    >
    > It appears that \timing on is not allowed in a do statement.
    >
    > Any alternative to do that in a do statement?
    >
    > Regards,
    >
    > David
    >
    
  3. Re: How to do things like \timing on in a do loop?

    hubert depesz lubaczewski <depesz@depesz.com> — 2022-10-16T09:21:35Z

    On Sat, Oct 15, 2022 at 08:32:59PM +0100, Shaozhong SHI wrote:
    > DO
    > $do$
    > declare
    > 
    > j int=1;
    > 
    > BEGIN
    > 
    > for j in 1..100 loop
    > 
    > \timing on
    > 
    > select * from a_table;
    > 
    > end loop;
    > 
    > end
    > $$;
    > 
    > 
    > It appears that \timing on is not allowed in a do statement.
    > 
    > Any alternative to do that in a do statement?
    
    Perhaps explain what you want to do, because from this snippet it is
    (for me) definitely not clear.
    
    You're mixing postgresql code (do blocks) and psql commands (\timing).
    you're trying to get timing off select, but you're not telling the code
    what to do with results of the select.
    
    So, what is the end goal in here? And how will you run it? Via psql?
    Some other db access?
    
    depesz