Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8242395
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T21:09:20+00:00 2026-06-07T21:09:20+00:00

I need to create a Postgres 9.1 PL/pgSQL stored procedure that, among other parameters,

  • 0

I need to create a Postgres 9.1 PL/pgSQL stored procedure that, among other parameters, takes a sequence of values that directly reference values in one of my database columns. As far as I can tell, the canonical way to do this in Postgres is an array.

This is a rather basic task, of course. My problem is scalability: My code basically works, but performs badly once the sequences passed in get large (as in a few hundreds or thousands of values):

Even rather simple SELECT statements within my stored procedure using the array in the form

SELECT <some columns>
FROM   <some tables>
WHERE  <some other select criteria>
AND    <column with values selected by array parameter>
         IN (SELECT * FROM unnest(<array parameter>))

take several seconds to execute even though the database is not very large yet and there are only tens of values in the array.

My first suspicion was that unnest(...) is the problem, but selecting only from the table with the column referenced in the array parameter is really fast:

SELECT <some columns>
FROM   <table with column ref'd in array parameter>
WHERE  <column with values selected by array parameter>
         IN (SELECT * FROM unnest(<array parameter>))

only takes a few milliseconds.

My questions:

  1. Is there an alternative to using an array as parameter ?
  2. How can I make my queries perform better ?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T21:09:23+00:00Added an answer on June 7, 2026 at 9:09 pm

    How can I make my queries perform better ?

    I would expect faster performance if you rewrite your query

    SELECT <some columns>
    FROM   <some tables>
    WHERE  <some other select criteria>
    AND    <column with values selected by array parameter>
             IN (SELECT * FROM unnest(<array parameter>));
    

    to:

    SELECT <some columns>
    FROM   (SELECT unnest(<array parameter>) AS param) x
    JOIN   <filtered table>  ON <filter column> = x.param
    JOIN   <other table> ON <join criteria>
    WHERE  <some other select criteria>;
    

    It sounds like the query planner chooses a suboptimal plan, misjudging the cost of your other WHERE criteria in comparison to the IN clause. By transforming it to an explicit JOIN clause you should get a better query plan.

    Generally, JOINs tend to be faster than large IN clauses in PostgreSQL.


    Is there an alternative to using an array as parameter ?

    Yes.
    You could create temporary table, fill it and run a query joining against it.

    CREATE TEMP TABLE x(id int);
    
    INSERT INTO x VALUES
    (1), (2), (17), (18);
    
    SELECT <some columns>
    FROM   x
    JOIN   <filtered table>  ON <filter column> = x.id
    JOIN   <other table> ON <join criteria>
    WHERE  <some other select criteria>;
    

    Or, faster yet, use a CTE for the same purpose:

    WITH x(id) AS (
        VALUES (1::int), (2), (17), (18) -- type-cast on first element is enough
        )
    SELECT <some columns>
    FROM   x
    JOIN   <filtered table>  ON <filter column> = x.id
    JOIN   <other table> ON <join criteria>
    WHERE  <some other select criteria>;
    

    As long as you want to use a function, an array parameter, unnested inside would be my choice, too. You could also use the CTE in my last example inside a function, just with unnest(arr) instead of a VALUES clause.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have simple table creating script in Postgres 9.1. I need it to create
I need to create a function that will run a query and return the
I need a real DBA's opinion. Postgres 8.3 takes 200 ms to execute this
I need create custom dialog and put JPanel into it. Is it possible?
i need create an email list sending to many emails. what is best solution
I need create clone repository. but I do not know where can I get
I need create a document word with Java. And I ask, how can I
i need create a variable with parent subclass. Example: Parent Class <?php class parentClass
I use MS SQL 2008 R2, I need create a Table with a CHECK
I have dynamically created WrapPanel (_wp) with several Borders. And I need create handler

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.