pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Andrew Gierth <andrew@tao11.riddles.org.uk>
To: Dominique Devienne <ddevienne@gmail.com>
Cc: pgsql-general@postgresql.org
Subject: Re: Return rows in input array's order?
Date: Wed, 10 May 2023 08:49:16 +0100
Message-ID: <87o7msabdp.fsf@news-spur.riddles.org.uk> (raw)
In-Reply-To: <CAFCRh-9qxbK5cwe=vooEaTPw+wZ1xQ+FDDnpWqRYEqzDwnSdgA@mail.gmail.com>
References: <CAFCRh-9qxbK5cwe=vooEaTPw+wZ1xQ+FDDnpWqRYEqzDwnSdgA@mail.gmail.com>

>>>>> "Dominique" == Dominique Devienne <ddevienne@gmail.com> writes:

 Dominique> Hi. With an integer identity primary key table,
 Dominique> we fetch a number of rows with WHERE id = ANY($1),
 Dominique> with $1 an int[] array. The API using that query must return
 Dominique> rows in the input int[] array order, and uses a client-side
 Dominique> mapping to achieve that currently.

 Dominique> Is it possible to maintain $1's order directly in SQL?
 Dominique> Efficiently?

This is the correct way:

SELECT ... FROM unnest($1) WITH ORDINALITY AS u(id,ord)
           JOIN yourtable t ON t.id=u.id
 ORDER BY u.ord;

This doesn't assume there won't be holes (if you want, you can change it
to a left join to get a null row instead for missing ids).

The query plan you get for this should be something like:

  Nested Loop
    Function Scan on unnest
    Index Scan on yourtable_pkey

(less likely, depending on table sizes, would be a Merge Join with
similar inputs. If your table is very small you might get a hashjoin and
separate sort, but that shouldn't happen with realistic data sizes.)

Notice that this is entirely deterministic about the output ordering
without needing to do any sorting. (The planner knows that the output of
WITH ORDINALITY function scans is automatically ordered by the ordinal
column, so it will usually generate plans that take advantage of that.)
The presence of "ORDER BY u.ord" ensures that the output order is
correct regardless of plan choice.

-- 
Andrew (irc:RhodiumToad)





view thread (11+ messages)  latest in thread

Message-ID: <87o7msabdp.fsf@news-spur.riddles.org.uk>
Permalink:  ../87o7msabdp.fsf@news-spur.riddles.org.uk/
Also on:    postgresql.org/message-id/87o7msabdp.fsf@news-spur.riddles.org.uk

 · 

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: andrew@tao11.riddles.org.uk, ddevienne@gmail.com
  Subject: Re: Return rows in input array's order?
  In-Reply-To: <87o7msabdp.fsf@news-spur.riddles.org.uk>
  References: <CAFCRh-9qxbK5cwe=vooEaTPw+wZ1xQ+FDDnpWqRYEqzDwnSdgA@mail.gmail.com> <87o7msabdp.fsf@news-spur.riddles.org.uk>

* 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