Thread

  1. SQL - How to iterate with delay in a loop

    Subramanian,Ramachandran <ramachandran.subramanian@alte-leipziger.de> — 2026-03-06T12:07:05Z

    Hello,
    
      I am trying to insert some rows into a table with a delay between each insert. I have a simple table with a ID column and some Date-Time columns.
    
    The first two inserts work as expected and insert two rows, each one second apart.
    
    However the do loop inserts 25 more rows, with the same timestamp.
    
    I am completely new to Postgres and may have overlooked something simple.
    
    I would be grateful for your help.
    
    
    LG
    
    Ram
    
    
    drop table time_zone_demo;
    create table time_zone_demo
    (
      ID_COLUMN SERIAL PRIMARY KEY,
      SAVED_TS_NO_TZ TIMESTAMP,
      SAVED_TS_WITH_TZ TIMESTAMPTZ,
      SAVED_DATE DATE,
      SAVED_TIME TIME
    );
    
    Insert into time_zone_demo (SAVED_TS_NO_TZ,SAVED_TS_WITH_TZ,SAVED_DATE,SAVED_TIME)
    values (now(),now(),current_date,current_time) ;
    
    select pg_sleep(1);
    
    Insert into time_zone_demo (SAVED_TS_NO_TZ,SAVED_TS_WITH_TZ,SAVED_DATE,SAVED_TIME)
    values (now(),now(),current_date,current_time) ;
    
    select pg_sleep(1);
    
    do $$
    begin
            for rec in 1..25
                loop
                perform pg_sleep(1);
                Insert into time_zone_demo (SAVED_TS_NO_TZ,SAVED_TS_WITH_TZ,SAVED_DATE,SAVED_TIME)
                values (now(),now(),current_date,current_time) ;
                end loop;
    end;
    $$;
    
    
    Freundliche Grüße
    
    i. A. Ramachandran Subramanian
    Zentralbereich Informationstechnologie
    
    Alte Leipziger Lebensversicherung a.G.
    
    
    Hallesche Krankenversicherung a.G.
    
    
    
    
    
    Alte Leipziger Lebensversicherung a.G., Alte Leipziger-Platz 1, 61440 Oberursel
    Vors. des Aufsichtsrats: Dr. Walter Botermann · Vorstand: Christoph Bohn (Vors.), Dr. Jürgen Bierbaum (stv. Vors.), Frank Kettnaker, Dr. Jochen Kriegmeier, Alexander Mayer, Christian Pape, Wiltrud Pekarek, Udo Wilcsek
    Sitz Oberursel (Taunus) · Rechtsform VVaG · Amtsgericht Bad Homburg v. d. H. HRB 1583 · USt.-IdNr. DE 114106814
    
    
    
    
    
     
    Hallesche Krankenversicherung a.G.,  Löffelstraße 34-38, 70597 Stuttgart
    Vors. des Aufsichtsrats: Dr. Walter Botermann · Vorstand: Christoph Bohn (Vors.), Dr. Jürgen Bierbaum (stv. Vors.), Frank Kettnaker, Dr. Jochen Kriegmeier, Alexander Mayer, Christian Pape,
    Wiltrud Pekarek, Udo Wilcsek
    Sitz Stuttgart · Rechtsform VVaG · Amtsgericht Stuttgart HRB 2686 · USt.-IdNr. DE 147802285
    Beiträge zu privaten Kranken- und Pflegekrankenversicherungen unterliegen nicht der Versicherungsteuer (§ 4 Nr. 5 VersStG) · Versicherungsleistungen sowie Umsätze aus Versicherungsvertreter-/Maklertätigkeiten sind umsatzsteuerfrei
     
    
    
    
     
    Die Pflichtangaben der ALH Gruppe gemäß § 35a GmbHG bzw. § 80 AktG finden Sie hier: https://www.alte-leipziger.de/impressum 
    
    
    
    
    
    ______________________
    
    ALH Gruppe
    Alte Leipziger-Platz 1, 61440 Oberursel
    Tel.: +49 (6171) 66-4882
    Fax: +49 (6171) 66-800-4882
    E-Mail: ramachandran.subramanian@alte-leipziger.de
    www.alte-leipziger.de
    www.hallesche.de
    
    
  2. Re: SQL - How to iterate with delay in a loop

    Laurenz Albe <laurenz.albe@cybertec.at> — 2026-03-06T13:20:58Z

    On Fri, 2026-03-06 at 12:07 +0000, Subramanian,Ramachandran wrote:
    > I am trying to insert some rows into a table with a delay between each insert.
    > I have a simple table with a ID column and some Date-Time columns.
    >  
    > The first two inserts work as expected and insert two rows, each one second apart.
    >  
    > However the do loop inserts 25 more rows, with the same timestamp.
    
    Use clock_timestamp()::time instead of current_time.
    current_time returns the same value for each call in the same
    database transaction.  Think of it as "transaction start time".
    
    Yours,
    Laurenz Albe
    
    
    
    
  3. AW: SQL - How to iterate with delay in a loop

    Subramanian,Ramachandran <ramachandran.subramanian@alte-leipziger.de> — 2026-03-06T18:24:01Z

    Thank you so much. That worked perfectly.  😊 
    
    LG
    
    Ram
    
    
    id_column |       saved_ts_no_tz       |       saved_ts_with_tz        | saved_date |   saved_time
    -----------+----------------------------+-------------------------------+------------+-----------------
            20 | 2026-03-06 19:22:18.992382 | 2026-03-06 19:22:18.992384+01 | 2026-03-06 | 19:22:18.992385
            21 | 2026-03-06 19:22:19.993481 | 2026-03-06 19:22:19.993484+01 | 2026-03-06 | 19:22:19.993485
            22 | 2026-03-06 19:22:20.99458  | 2026-03-06 19:22:20.994583+01 | 2026-03-06 | 19:22:20.994584
            23 | 2026-03-06 19:22:21.994803 | 2026-03-06 19:22:21.994806+01 | 2026-03-06 | 19:22:21.994806
            24 | 2026-03-06 19:22:22.995918 | 2026-03-06 19:22:22.99592+01  | 2026-03-06 | 19:22:22.995921
            25 | 2026-03-06 19:22:23.997049 | 2026-03-06 19:22:23.997051+01 | 2026-03-06 | 19:22:23.997052
            26 | 2026-03-06 19:22:24.997807 | 2026-03-06 19:22:24.997809+01 | 2026-03-06 | 19:22:24.997809
            27 | 2026-03-06 19:22:25.998923 | 2026-03-06 19:22:25.998926+01 | 2026-03-06 | 19:22:25.998927
    
    
    
    Freundliche Grüße
    
    i. A. Ramachandran Subramanian
    Zentralbereich Informationstechnologie
    
    Alte Leipziger Lebensversicherung a.G.
    
    
    Hallesche Krankenversicherung a.G.
    
    
    
    
    
    
    
    Alte Leipziger Lebensversicherung a.G., Alte Leipziger-Platz 1, 61440 Oberursel
    Vors. des Aufsichtsrats: Dr. Walter Botermann · Vorstand: Christoph Bohn (Vors.), Dr. Jürgen Bierbaum (stv. Vors.), Frank Kettnaker, Dr. Jochen Kriegmeier, Alexander Mayer, Christian Pape, Wiltrud Pekarek, Udo Wilcsek
    Sitz Oberursel (Taunus) · Rechtsform VVaG · Amtsgericht Bad Homburg v. d. H. HRB 1583 · USt.-IdNr. DE 114106814
    
    
    
    
    
     
    Hallesche Krankenversicherung a.G.,  Löffelstraße 34-38, 70597 Stuttgart
    Vors. des Aufsichtsrats: Dr. Walter Botermann · Vorstand: Christoph Bohn (Vors.), Dr. Jürgen Bierbaum (stv. Vors.), Frank Kettnaker, Dr. Jochen Kriegmeier, Alexander Mayer, Christian Pape,
    Wiltrud Pekarek, Udo Wilcsek
    Sitz Stuttgart · Rechtsform VVaG · Amtsgericht Stuttgart HRB 2686 · USt.-IdNr. DE 147802285
    Beiträge zu privaten Kranken- und Pflegekrankenversicherungen unterliegen nicht der Versicherungsteuer (§ 4 Nr. 5 VersStG) · Versicherungsleistungen sowie Umsätze aus Versicherungsvertreter-/Maklertätigkeiten sind umsatzsteuerfrei
     
    
    
    
     
    Die Pflichtangaben der ALH Gruppe gemäß § 35a GmbHG bzw. § 80 AktG finden Sie hier: https://www.alte-leipziger.de/impressum 
    
    
    
    
    
    ______________________
    
    ALH Gruppe
    Alte Leipziger-Platz 1, 61440 Oberursel
    Tel.: +49 (6171) 66-4882
    Fax: +49 (6171) 66-800-4882
    E-Mail: ramachandran.subramanian@alte-leipziger.de
    www.alte-leipziger.de
    www.hallesche.de
    
    
    
    -----Ursprüngliche Nachricht-----
    Von: Laurenz Albe <laurenz.albe@cybertec.at> 
    Gesendet: Freitag, 6. März 2026 14:21
    An: Subramanian,Ramachandran IT-md-db <ramachandran.subramanian@alte-leipziger.de>; pgsql-novice@lists.postgresql.org
    Betreff: Re: SQL - How to iterate with delay in a loop
    
    On Fri, 2026-03-06 at 12:07 +0000, Subramanian,Ramachandran wrote:
    > I am trying to insert some rows into a table with a delay between each insert.
    > I have a simple table with a ID column and some Date-Time columns.
    >  
    > The first two inserts work as expected and insert two rows, each one second apart.
    >  
    > However the do loop inserts 25 more rows, with the same timestamp.
    
    Use clock_timestamp()::time instead of current_time.
    current_time returns the same value for each call in the same database transaction.  Think of it as "transaction start time".
    
    Yours,
    Laurenz Albe