Question Details

No question body available.

Tags

java sql postgresql

Answers (4)

Accepted Answer Available
Accepted Answer
July 14, 2025 Score: 4 Rep: 6,309 Quality: Medium Completeness: 30%

Correct PostgreSQL syntax, and a simple drop-in replacement for the parametrized query above, is:

select from message where id in (select from unnest(?))
July 14, 2025 Score: 6 Rep: 7,653 Quality: Medium Completeness: 80%

any(array)

You can use PostgreSQL's any operator to directly test wether the row's id is in your array:

select  from message where id = any(array[1,2]);

Or you can pass it a string formatted in a way that PostgreSQL knows to cast to an array:

select  from message where id = any('{1,2}'::bigint[]);  

(you can even simply write = any('{1,2}'), however expliciting the ::bigint[] will make PostgreSQL directly target the right type, instead of (perhaps, I'm not sure about it) casting it to a text[] then comparing each text element to a bigint id)

(see it in a fiddle)

July 14, 2025 Score: 4 Rep: 670,199 Quality: Low Completeness: 80%

While the array contains distinct elements:

SELECT *
FROM   unnest($1) id  -- use matching column name
JOIN   message USING (id);

Even a bit cheaper for big arrays as Postgres does not try to merge duplicate entries.

July 14, 2025 Score: 0 Rep: 427,774 Quality: Low Completeness: 60%

If you're using Spring, use JPA and code like this:

public interface MessageRepository extends JpaRepository {
    List findByIdIn(Collection ids);
}

You can read about JPA here.