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 8273343
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T07:27:15+00:00 2026-06-08T07:27:15+00:00

I am trying to write a PL/pgSQL function with optional arguments. It performs a

  • 0

I am trying to write a PL/pgSQL function with optional arguments. It performs a query based on a filtered set of records (if specified), otherwise performs a query on the entire data set in a table.

For example (PSEUDO CODE):

CREATE OR REPLACE FUNCTION foofunc(param1 integer, param2 date, param2 date, optional_list_of_ids=[]) RETURNS SETOF RECORD AS $$
    IF len(optional_list_of_ids) > 0 THEN
        RETURN QUERY (SELECT * from foobar where f1=param1 AND f2=param2 AND id in optional_list_of_ids);
    ELSE
        RETURN QUERY (SELECT * from foobar where f1=param1 AND f2=param2);
    ENDIF
$$ LANGUAGE SQL;

What would be the correct way to implement this function?

As an aside, I would like to know how I could call such a function in another outer function. This is how I would do it – is it correct, or is there a better way?

CREATE FUNCTION foofuncwrapper(param1 integer, param2 date, param2 date) RETURNS SETOF RECORD AS $$
BEGIN
   CREATE TABLE ids AS SELECT id from foobar where id < 100;
   RETURN QUERY (SELECT * FROM foofunc(param1, param2, ids));
END
$$ LANGUAGE SQL
  • 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-08T07:27:19+00:00Added an answer on June 8, 2026 at 7:27 am

    Since PostgreSQL 8.4 (which you seem to be running), there are default values for function parameters. If you put your parameter last and provide a default, you can simply omit it from the call:

    CREATE OR REPLACE FUNCTION foofunc(_param1 integer
                                     , _param2 date
                                     , _ids    int[] DEFAULT '{}')
      RETURNS SETOF foobar         -- declare return type!
      LANGUAGE plpgsql AS
    $func$
    BEGIN  -- required for plpgsql
       IF _ids <> '{}'::int[] THEN  -- exclude empty array and NULL
          RETURN QUERY
          SELECT *
          FROM   foobar
          WHERE  f1 = _param1
          AND    f2 = _param2
          AND    id = ANY(_ids);    -- "IN" is not proper syntax for arrays
       ELSE
          RETURN QUERY
          SELECT *
          FROM   foobar
          WHERE  f1 = _param1
          AND    f2 = _param2;
       END IF;
    END  -- required for plpgsql
    $func$;
    

    Major points:

    • The keyword DEFAULT is used to declare parameter defaults. Short alternative: =.

    • I removed the redundant param1 from the messy example.

    • Since you return SELECT * FROM foobar, declare the return type as RETURNS SETOF foobar instead of RETURNS SETOF record. The latter form with anonymous records is very unwieldy, you’d have to provide a column definition list with every call.

    • I use an array of integer (int[]) as function parameter. Adapted the IF expression and the WHERE clause accordingly.

    • IF statements are not available in plain SQL. Has to be LANGUAGE plpgsql for that.

    Call with or without _ids:

    SELECT * FROM foofunc(1, '2012-1-1'::date);
    

    Effectively the same:

    SELECT * FROM foofunc(1, '2012-1-1'::date, '{}'::int[]);
    

    You have to make sure the call is unambiguous. If you have another function of the same name and two parameters, Postgres might not know which to pick. Explicit casting (like I demonstrate) narrows it down. Else, untyped string literals work, too, but being explicit never hurts.

    Call from within another function:

    CREATE FUNCTION foofuncwrapper(_param1 integer, _param2 date)
      RETURNS SETOF foobar
      LANGUAGE plgpsql AS
    $func$
    DECLARE
       _ids int[] := '{1,2,3}';
    BEGIN
       -- whatever
    
       RETURN QUERY
       SELECT * FROM foofunc(_param1, _param2, _ids);
    END
    $func$;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying write a query to find records which don't have a matching record
I'm trying to write a function in PL/PgSQL that have to work with a
I m trying write code that after reset set up rrpmax as 3000. It
I am trying write a function that generates simulated data but if the simulated
Trying to write a function to see how often an object exists and give
Im trying to write a recursive function which returns the factorial of a number.
Trying to write a simple jQuery function that will multiple the index of an
Trying to write a matrix-multiplying function for arbitrary-sized matrices in C. I'm trying the
I've had problem when trying write a function which has a default value when
Im trying to write an if function which checks the width of an image,

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.