pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: negora <public@negora.com>
To: pgsql-general@lists.postgresql.org
Subject: Re: Return rows in input array's order?
Date: Tue, 9 May 2023 11:57:46 +0200
Message-ID: <7818bc4a-fe6e-e750-8e9f-c0c2a660e61e@negora.com> (raw)
In-Reply-To: <CAFCRh-_w_b-12z1LO3u6C_enjbzGsSVN6PEZ4aZ5HYzBfdmioA@mail.gmail.com>
References: <CAFCRh-9qxbK5cwe=vooEaTPw+wZ1xQ+FDDnpWqRYEqzDwnSdgA@mail.gmail.com>
	<5C5270F1-4323-458F-99C2-1D187039EA9C@gmail.com>
	<CAFCRh-_w_b-12z1LO3u6C_enjbzGsSVN6PEZ4aZ5HYzBfdmioA@mail.gmail.com>

Hi Dominique:

Take a look to the "unnest()" function. It transforms an array into a 
set of rows. I believe I used it in the past to do something similar to 
what you need.

Another option is to use a "values" expression (in a subquery) instead 
of an array, and build the query dynamically.

Best regards.


On 09/05/2023 11:37, Dominique Devienne wrote:
> On Tue, May 9, 2023 at 11:23 AM David Wheeler <hippysoyboy@gmail.com> 
> wrote:
>
>     > Hi. With an integer identity primary key table,
>     > we fetch a number of rows with WHERE id = ANY($1),
>     > with $1 an int[] array. The API using that query must return
>     > rows in the input int[] array order, and uses a client-side
>     > mapping to achieve that currently.
>     >
>     > Is it possible to maintain $1's order directly in SQL? Efficiently?
>
>     We’ve done this before with an “order by array_index(id,
>     input_array)”. I forget the actual function consider that pseudo code
>
>
> Thanks David. I see how this would work.
>
>     It was only used for small arrays but never noticed any
>     performance issues
>
>
> Hmmm, sounds like this would be quadratic though...
>
> Each call to array_index() will be O(N), so turn the sort into O(N^2) 
> just from the array_index() calls,
> without even considering the sorting itself (which I assume is O(N log 
> N)).
>
> I wonder whether the int[] can be turned into a pseudo table with a 
> ROWNUM extra generated column that
> would then be (LEFT) JOIN'd to the accessed table, so that the 
> original array index is readily accessible.
> Would something like this be possible in Postgres' SQL?
>
> I could then skip the sort, return that original index as part of the 
> select,
> and thus be able to read the other columns directly in the correct 
> client-side re-allocated vector-slot / structure...

view thread (11+ messages)  latest in thread

Message-ID: <7818bc4a-fe6e-e750-8e9f-c0c2a660e61e@negora.com>
Permalink:  ../7818bc4a-fe6e-e750-8e9f-c0c2a660e61e@negora.com/
Also on:    postgresql.org/message-id/7818bc4a-fe6e-e750-8e9f-c0c2a660e61e@negora.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: public@negora.com
  Subject: Re: Return rows in input array's order?
  In-Reply-To: <7818bc4a-fe6e-e750-8e9f-c0c2a660e61e@negora.com>
  References: <CAFCRh-9qxbK5cwe=vooEaTPw+wZ1xQ+FDDnpWqRYEqzDwnSdgA@mail.gmail.com> <5C5270F1-4323-458F-99C2-1D187039EA9C@gmail.com> <CAFCRh-_w_b-12z1LO3u6C_enjbzGsSVN6PEZ4aZ5HYzBfdmioA@mail.gmail.com> <7818bc4a-fe6e-e750-8e9f-c0c2a660e61e@negora.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox