unnest multirange, returned order

Lists: pgsql-docspgsql-hackers
From: PG Doc comments form <noreply(at)postgresql(dot)org>
To: pgsql-docs(at)lists(dot)postgresql(dot)org
Cc: daniel(dot)fredouille(at)gmail(dot)com
Subject: unnest multirange, returned order
Date: 2023-10-02 18:42:14
Message-ID: 169627213477.3727338.17653654241633692682@wrigleys.postgresql.org
Views: Whole Thread | Raw Message | Download mbox | Resend email
Lists: pgsql-docs pgsql-hackers

The following documentation comment has been logged on the website:

Page: https://www.postgresql.org/docs/16/functions-range.html
Description:

The doc says:
* unnest ( anymultirange ) → setof anyrange
* Expands a multirange into a set of ranges. The ranges are read out in
storage order (ascending).

What is storage order ?

At first I thought that it was the order in which the different ranges are
inserted in the internal data structure. However, the following sort of
shows that it is not:
```
postgres=# select unnest('{[1,4), [8,10)}'::int4multirange + '{[-5,-3)}' -
'{[2,3)}') ;
unnest
---------
[-5,-3)
[1,2)
[3,4)
[8,10)
(4 lignes)
```
Whatever I try, it always return in range order instead of "storage order".

Some context: I ask because we have some seemingly random (and impossible to
repro in tests up to now) errors in our code. The code assumes that this
returns things in range order and as the doc is unclear to me on this point,
I cannot exclude this to be our culprit.

Thank you


From: Laurenz Albe <laurenz(dot)albe(at)cybertec(dot)at>
To: daniel(dot)fredouille(at)gmail(dot)com, pgsql-docs(at)lists(dot)postgresql(dot)org
Subject: Re: unnest multirange, returned order
Date: 2023-10-03 13:46:23
Message-ID: [email protected]
Views: Whole Thread | Raw Message | Download mbox | Resend email
Lists: pgsql-docs pgsql-hackers

On Mon, 2023-10-02 at 18:42 +0000, PG Doc comments form wrote:
> Page: https://www.postgresql.org/docs/16/functions-range.html
>
> The doc says:
> * unnest ( anymultirange ) → setof anyrange
> * Expands a multirange into a set of ranges. The ranges are read out in
> storage order (ascending).
>
> What is storage order ?
>
> At first I thought that it was the order in which the different ranges are
> inserted in the internal data structure. However, the following sort of
> shows that it is not:
> ```
> postgres=# select unnest('{[1,4), [8,10)}'::int4multirange + '{[-5,-3)}' -
> '{[2,3)}') ;
>  unnest
> ---------
>  [-5,-3)
>  [1,2)
>  [3,4)
>  [8,10)
> (4 lignes)
> ```
> Whatever I try, it always return in range order instead of "storage order".

I'd say that the storag order is the order in which PostgreSQL stores
multiranges internally:

SELECT '{[100,200),[-100,-50),[-1,2)}'::int4multirange;

int4multirange
═══════════════════════════════
{[-100,-50),[-1,2),[100,200)}
(1 row)

Yours,
Laurenz Albe


From: Daniel Fredouille <daniel(dot)fredouille(at)gmail(dot)com>
To: Laurenz Albe <laurenz(dot)albe(at)cybertec(dot)at>
Cc: pgsql-docs(at)lists(dot)postgresql(dot)org
Subject: Re: unnest multirange, returned order
Date: 2023-10-04 00:40:40
Message-ID: CAEpV6kX9PJAuQ_=5c7ZxCSASu76hkvGfVvwU6WkHTRXzApknfQ@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Lists: pgsql-docs pgsql-hackers

>
> I'd say that the storag order is the order in which PostgreSQL stores
> multiranges internally:

Right, I believe that you are right but then this information is not useful
for the developer.
If storage order is always ascending by range order then let's make it
clear, if order cannot be counted upon as it may evolve from postgres
version to version, then let's make it clear as well. WDYT ?

Thank you.
Daniel Fredouille

Le mar. 3 oct. 2023 à 09:46, Laurenz Albe <laurenz(dot)albe(at)cybertec(dot)at> a
écrit :

> On Mon, 2023-10-02 at 18:42 +0000, PG Doc comments form wrote:
> > Page: https://www.postgresql.org/docs/16/functions-range.html
> >
> > The doc says:
> > * unnest ( anymultirange ) → setof anyrange
> > * Expands a multirange into a set of ranges. The ranges are read out in
> > storage order (ascending).
> >
> > What is storage order ?
> >
> > At first I thought that it was the order in which the different ranges
> are
> > inserted in the internal data structure. However, the following sort of
> > shows that it is not:
> > ```
> > postgres=# select unnest('{[1,4), [8,10)}'::int4multirange + '{[-5,-3)}'
> -
> > '{[2,3)}') ;
> > unnest
> > ---------
> > [-5,-3)
> > [1,2)
> > [3,4)
> > [8,10)
> > (4 lignes)
> > ```
> > Whatever I try, it always return in range order instead of "storage
> order".
>
> I'd say that the storag order is the order in which PostgreSQL stores
> multiranges internally:
>
> SELECT '{[100,200),[-100,-50),[-1,2)}'::int4multirange;
>
> int4multirange
> ═══════════════════════════════
> {[-100,-50),[-1,2),[100,200)}
> (1 row)
>
> Yours,
> Laurenz Albe
>


From: Laurenz Albe <laurenz(dot)albe(at)cybertec(dot)at>
To: Daniel Fredouille <daniel(dot)fredouille(at)gmail(dot)com>
Cc: pgsql-docs(at)lists(dot)postgresql(dot)org
Subject: Re: unnest multirange, returned order
Date: 2023-10-04 07:20:16
Message-ID: [email protected]
Views: Whole Thread | Raw Message | Download mbox | Resend email
Lists: pgsql-docs pgsql-hackers

On Tue, 2023-10-03 at 20:40 -0400, Daniel Fredouille wrote:
> > I'd say that the storag order is the order in which PostgreSQL stores
> > multiranges internally:
>
> Right, I believe that you are right but then this information is not useful for the developer. 
> If storage order is always ascending by range order then let's make it clear,
> if order cannot be counted upon as it may evolve from postgres version to version,
> then let's make it clear as well. WDYT ?

I personally think that it is clear as it is written now.

If you have a good suggestion for an improvement, you could send it;
perhaps someone will pick it up.

Yours,
Laurenz Albe


From: Daniel Fredouille <daniel(dot)fredouille(at)gmail(dot)com>
To: Laurenz Albe <laurenz(dot)albe(at)cybertec(dot)at>
Cc: pgsql-docs(at)lists(dot)postgresql(dot)org
Subject: Re: unnest multirange, returned order
Date: 2023-10-05 00:04:41
Message-ID: CAEpV6kVJc7a3i86G5ifzn9hONxpbgvbP1g_xqidW3fMhTQ54yA@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Lists: pgsql-docs pgsql-hackers

Trying a suggestion then:

"""

unnest ( anymultirange ) → setof anyrange

Expands a multirange into a set of ranges. The ranges are read out in
storage order (ascending) and therefore cannot be relied upon.

unnest('{[1,2), [3,4)}'::int4multirange) →

[1,2)
[3,4)

"""
Daniel

Le mer. 4 oct. 2023 à 03:20, Laurenz Albe <laurenz(dot)albe(at)cybertec(dot)at> a
écrit :

> On Tue, 2023-10-03 at 20:40 -0400, Daniel Fredouille wrote:
> > > I'd say that the storag order is the order in which PostgreSQL stores
> > > multiranges internally:
> >
> > Right, I believe that you are right but then this information is not
> useful for the developer.
> > If storage order is always ascending by range order then let's make it
> clear,
> > if order cannot be counted upon as it may evolve from postgres version
> to version,
> > then let's make it clear as well. WDYT ?
>
> I personally think that it is clear as it is written now.
>
> If you have a good suggestion for an improvement, you could send it;
> perhaps someone will pick it up.
>
> Yours,
> Laurenz Albe
>


From: Daniel Fredouille <daniel(dot)fredouille(at)gmail(dot)com>
To: Laurenz Albe <laurenz(dot)albe(at)cybertec(dot)at>
Cc: pgsql-docs(at)lists(dot)postgresql(dot)org
Subject: Re: unnest multirange, returned order
Date: 2023-10-05 00:12:19
Message-ID: CAEpV6kWcAVFvmtw9amVubL7F9LWX==nE5N96uFrGm4XX6aFdVQ@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Lists: pgsql-docs pgsql-hackers

Sorry correcting my own suggestion:

"""

unnest ( anymultirange ) → setof anyrange

Expands a multirange into a set of ranges. The ranges are read out in
storage order (ascending) and therefore order cannot be relied upon.

unnest('{[1,2), [3,4)}'::int4multirange) →

[1,2)
[3,4)

"""

Le mer. 4 oct. 2023 à 20:04, Daniel Fredouille <daniel(dot)fredouille(at)gmail(dot)com>
a écrit :

> Trying a suggestion then:
>
> """
>
> unnest ( anymultirange ) → setof anyrange
>
> Expands a multirange into a set of ranges. The ranges are read out in
> storage order (ascending) and therefore cannot be relied upon.
>
> unnest('{[1,2), [3,4)}'::int4multirange) →
>
> [1,2)
> [3,4)
>
> """
> Daniel
>
> Le mer. 4 oct. 2023 à 03:20, Laurenz Albe <laurenz(dot)albe(at)cybertec(dot)at> a
> écrit :
>
>> On Tue, 2023-10-03 at 20:40 -0400, Daniel Fredouille wrote:
>> > > I'd say that the storag order is the order in which PostgreSQL stores
>> > > multiranges internally:
>> >
>> > Right, I believe that you are right but then this information is not
>> useful for the developer.
>> > If storage order is always ascending by range order then let's make it
>> clear,
>> > if order cannot be counted upon as it may evolve from postgres version
>> to version,
>> > then let's make it clear as well. WDYT ?
>>
>> I personally think that it is clear as it is written now.
>>
>> If you have a good suggestion for an improvement, you could send it;
>> perhaps someone will pick it up.
>>
>> Yours,
>> Laurenz Albe
>>
>


From: Laurenz Albe <laurenz(dot)albe(at)cybertec(dot)at>
To: Daniel Fredouille <daniel(dot)fredouille(at)gmail(dot)com>
Cc: pgsql-docs(at)lists(dot)postgresql(dot)org
Subject: Re: unnest multirange, returned order
Date: 2023-10-05 06:50:24
Message-ID: [email protected]
Views: Whole Thread | Raw Message | Download mbox | Resend email
Lists: pgsql-docs pgsql-hackers

On Wed, 2023-10-04 at 20:12 -0400, Daniel Fredouille wrote:
> unnest ( anymultirange ) → setof anyrange
> Expands a multirange into a set of ranges. The ranges are read out in storage order (ascending) and therefore order cannot be relied upon.

That's not true. The order is deterministic and can be relied on.

How about the attached patch, which does away with the confusing
mention of "storage order"?

Yours,
Laurenz Albe

Attachment Content-Type Size
0001-Clarify-the-result-order-of-unnest-multirange.patch text/x-patch 1.3 KB

From: Daniel Fredouille <daniel(dot)fredouille(at)gmail(dot)com>
To: Laurenz Albe <laurenz(dot)albe(at)cybertec(dot)at>
Cc: pgsql-docs(at)lists(dot)postgresql(dot)org
Subject: Re: unnest multirange, returned order
Date: 2023-10-13 19:33:57
Message-ID: CAEpV6kUr-7aS6v-azW3eeaw7p0r-xZzk4FtcNEN8HM13Orfc9g@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Lists: pgsql-docs pgsql-hackers

Hi,

sorry it took me some time to reply. Yes, the patch is perfect if this is
indeed the behavior.

cheers
Daniel

Le jeu. 5 oct. 2023 à 02:50, Laurenz Albe <laurenz(dot)albe(at)cybertec(dot)at> a
écrit :

> On Wed, 2023-10-04 at 20:12 -0400, Daniel Fredouille wrote:
> > unnest ( anymultirange ) → setof anyrange
> > Expands a multirange into a set of ranges. The ranges are read out in
> storage order (ascending) and therefore order cannot be relied upon.
>
> That's not true. The order is deterministic and can be relied on.
>
> How about the attached patch, which does away with the confusing
> mention of "storage order"?
>
> Yours,
> Laurenz Albe
>


From: Laurenz Albe <laurenz(dot)albe(at)cybertec(dot)at>
To: Daniel Fredouille <daniel(dot)fredouille(at)gmail(dot)com>
Cc: pgsql-hackers(at)lists(dot)postgresql(dot)org
Subject: Re: unnest multirange, returned order
Date: 2023-10-27 06:48:49
Message-ID: [email protected]
Views: Whole Thread | Raw Message | Download mbox | Resend email
Lists: pgsql-docs pgsql-hackers

On Fri, 2023-10-13 at 15:33 -0400, Daniel Fredouille wrote:
> sorry it took me some time to reply. Yes, the patch is perfect if this is indeed the behavior.

I'm sending a reply to the hackers list so that I can add the patch to the commitfest.

Tiny as the patch is, I don't want it to fall between the cracks.

Yours,
Laurenz Albe


From: Jeff Davis <pgsql(at)j-davis(dot)com>
To: Laurenz Albe <laurenz(dot)albe(at)cybertec(dot)at>, Daniel Fredouille <daniel(dot)fredouille(at)gmail(dot)com>
Cc: pgsql-hackers(at)lists(dot)postgresql(dot)org
Subject: Re: unnest multirange, returned order
Date: 2023-10-27 23:08:37
Message-ID: [email protected]
Views: Whole Thread | Raw Message | Download mbox | Resend email
Lists: pgsql-docs pgsql-hackers

On Fri, 2023-10-27 at 08:48 +0200, Laurenz Albe wrote:
> On Fri, 2023-10-13 at 15:33 -0400, Daniel Fredouille wrote:
> > sorry it took me some time to reply. Yes, the patch is perfect if
> > this is indeed the behavior.
>
> I'm sending a reply to the hackers list so that I can add the patch
> to the commitfest.
>
> Tiny as the patch is, I don't want it to fall between the cracks.

Committed with adjusted wording. Thank you!

--
Jeff Davis
PostgreSQL Contributor Team - AWS


From: Laurenz Albe <laurenz(dot)albe(at)cybertec(dot)at>
To: Jeff Davis <pgsql(at)j-davis(dot)com>, Daniel Fredouille <daniel(dot)fredouille(at)gmail(dot)com>
Cc: pgsql-hackers(at)lists(dot)postgresql(dot)org
Subject: Re: unnest multirange, returned order
Date: 2023-10-28 08:53:58
Message-ID: [email protected]
Views: Whole Thread | Raw Message | Download mbox | Resend email
Lists: pgsql-docs pgsql-hackers

On Fri, 2023-10-27 at 16:08 -0700, Jeff Davis wrote:
> On Fri, 2023-10-27 at 08:48 +0200, Laurenz Albe wrote:
> > On Fri, 2023-10-13 at 15:33 -0400, Daniel Fredouille wrote:
> > > sorry it took me some time to reply. Yes, the patch is perfect if
> > > this is indeed the behavior.
> >
> > I'm sending a reply to the hackers list so that I can add the patch
> > to the commitfest.
> >
> > Tiny as the patch is, I don't want it to fall between the cracks.
>
> Committed with adjusted wording. Thank you!

Thanks!

Yours,
Laurenz Albe