Re: Return rows in input array's order? - Mailing list pgsql-general

From Dominique Devienne
Subject Re: Return rows in input array's order?
Date
Msg-id CAFCRh-8qneCBe4p7k0zpbCMgXy1aE3-Mq6iJNiMWZ1=o+KDiKw@mail.gmail.com
Whole thread Raw
In response to Re: Return rows in input array's order?  (Andrew Gierth <andrew@tao11.riddles.org.uk>)
List pgsql-general
On Wed, May 10, 2023 at 9:49 AM Andrew Gierth <andrew@tao11.riddles.org.uk> wrote:
 Dominique> Is it possible to maintain $1's order directly in SQL?

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;

Thanks Andrew, for spelling it out for me. Appreciated.
Also thanks to others who chimed in.

I assume that if the PK is composite, and I pass the PK tuples as separate
same-cardinality "parallel" arrays, I can "zip" those arrays back via a multi-join
using their ordinals before joining with the composite-PK table? --DD

PS: I guess the ideal plan depends both on the table itself, but also the cardinality
  of the array(s) passed in as bind variable(s) at runtime to the prepared statement, right?
  But from past posts, I got the impression the plan of a prepared statement is "fixed",
  and does not depend on "bind peeking" like it can in Oracle, to take those bound
  array's cardinality into account at PQexecPrepared-time?

PPS: This is something I actually failed to do in Oracle in the past...

pgsql-general by date:

Previous
From: Andrew Gierth
Date:
Subject: Re: Return rows in input array's order?
Next
From: Andrew Gierth
Date:
Subject: Re: Return rows in input array's order?