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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:58:47+00:00 2026-05-27T19:58:47+00:00

in postgresql plpgsql, create function f1( p_i int ) returns table( c1 int )

  • 0

in postgresql plpgsql,

create function f1( p_i int ) returns table( c1 int ) as $$
begin
  -- wish to exit, do not wish to return anything
  if p_i < 0 then
     -- cannot RETURN - since can only return record!
  end if;

  -- continue processing
  return query select c2 from t1 where c1 = p_i;
  ...
end;
$$ language plpgsql;

according to doc, the only way to break out of a function is RETURN. but a RETURN here requires RETURN QUERY or RETURN NEXT – there seems to be no way to simply exit out of the function.

  • 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-05-27T19:58:48+00:00Added an answer on May 27, 2026 at 7:58 pm

    If p_i < 0 is actually an error then you could raise an exception:

    if p_i < 0 then
        raise exception 'Don''t know what to do with %', p_i
    end if;
    

    If p_i < 0 should just quietly return nothing then you could do something like this:

    create or replace function f1( p_i int ) returns table( c1 int ) as $$
    begin
        if p_i < 0 then
            return;
        end if;
        return query select c2 from t1 where c1 = p_i;
    end;
    $$ language plpgsql;
    

    From the fine manual:

    39.6.1.2. RETURN NEXT and RETURN QUERY
    […]
    the individual items to return are specified by a sequence of RETURN NEXT or RETURN QUERY commands, and then a final RETURN command with no argument is used to indicate that the function has finished executing.

    Emphasis mine. So you can use your return query to return the query and just a simple return; to bail out without doing anything.

    For example, the return; version gives me things like this:

    => select * from f1(-1);
     c1 
    ----
    (0 rows)
    => select * from f1(1);
     c1 
    ----
      1
      1
      ...
    (15 rows)
    

    and the exception version does this:

    => select * from f1(-1);
    ERROR:  Don't know what to do with -1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible for a PostgreSQL plpgsql function to not return anything? I've created
I am referring to http://www.if-not-true-then-false.com/2009/11/howto-create-postgresql-table-partitioning-part-1/ To reproduce the problem, here is some simple steps
I have a function in PostgreSQL (PLPGSQL) that returns an array containing two elements.
I am using PostgreSQL 8.4 and I want to create a function that returns
I have a problem in postgres function: CREATE OR REPLACE FUNCTION linkedRepoObjects(id bigint) RETURNS
i try to create a plpgsql trigger for postgresql 8.3 which automatically partitions a
PostgreSQL 8.4 on Linux. I have a function: CREATE OR REPLACE FUNCTION production.add_customer (
I have this function in PostgreSQL, but I don't know how to return the
I am using Postgresql 8.3 and have the following simple function that will return
I have two tables like this: CREATE TABLE table1_lang ( id serial NOT NULL,

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.