Subject: Re: How to update upper-bound of tstzrange ?

From: Erik Wienhold <[email-masked]>
Date: Mon, 20 May 2024 13:56:21 +0200

On 2024-05-20 12:30 +0200, Laura Smith wrote:
> Could someone kindly help me out with the correct syntax ?
> 
> My first thought was the below but that doesn't work:
> 
> update foo set upper(bar_times)=upper(bar_times)+interval '1' hour where bar_id='abc';
> ERROR:  syntax error at or near "("
> LINE 1: update event_sessions set upper(bar_times)=upper(bar_ti...

Use the constructor function:

    UPDATE foo SET bar_times = tstzrange(lower(bar_times), upper(bar_times) + interval '1' hour);

But this does not preserve the inclusivity/exclusivity of bounds from
the input range, so you may have to pass in the third argument as well.

https://www.postgresql.org/docs/current/rangetypes.html#RANGETYPES-CONSTRUCT

-- 
Erik

---

From: Laurenz Albe <[email-masked]>
Date: Tue, 21 May 2024 12:02:56 +0200

> https://www.postgresql.org/docs/current/rangetypes.html#RANGETYPES-CONSTRUCT

If you need to preserve the information whether the upper and lower bounds
are inclusive or not, you could

  UPDATE foo
  SET bar_times = tstzrange(
                     lower(bar_times),
                     upper (bar_times) + INTERVAL '1 hour',
                     CASE WHEN lower_inc(bar_times) THEN '[' ELSE '(' END ||
                     CASE WHEN upper_inc(bar_times) THEN ']' ELSE ')' END
                  )
  WHERE ...

Yours,
Laurenz Albe

---

From: Laura Smith <[email-masked]>
Date: Tue, 21 May 2024 10:37:18 +0000

Thanks all for your answers !  Much appreciated.

Sent with Proton Mail secure email.