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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T13:52:31+00:00 2026-06-09T13:52:31+00:00

I can’t find anything in the PostgreSQL documentation that shows how to declare a

  • 0

I can’t find anything in the PostgreSQL documentation that shows how to declare a record, or row, while declaring the tuple structure at the same time. If you don’t define you tuple structure you get the error “The tuple structure of a not-yet-assigned record is indeterminate”.

This is what I’m doing now, which works fine, but there must be a better way to do it.

CREATE OR REPLACE FUNCTION my_func()
  RETURNS TABLE (
    "a" integer,
    "b" varchar
  ) AS $$
DECLARE r record;
BEGIN

CREATE TEMP TABLE tmp_t (
    "a" integer,
    "b" varchar
);
-- Define the tuple structure of r by SELECTing an empty row into it.
-- Is there a more straight-forward way of doing this?
SELECT * INTO r
FROM tmp_t;

-- Now I can assign values to the record.
r.a := at.something FROM "another_table" at
       WHERE at.some_id = 1;

-- A related question is - how do I return the single record 'r' from
-- this function?
-- This works:
RETURN QUERY
SELECT * FROM tmp_t;

-- But this doesn't:
RETURN r;
-- ERROR:  RETURN cannot have a parameter in function returning set

END; $$ LANGUAGE plpgsql;
  • 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-09T13:52:32+00:00Added an answer on June 9, 2026 at 1:52 pm

    You are mixing the syntax for returning SETOF values with syntax for returning a single row or value.

    — A related question is – how do I return the single record ‘r’ from

    When you declare a function with RETURNS TABLE, you have to use RETURN NEXT in the body to return a row (or scalar value). And if you want to use a record variable with that it has to match the return type. Refer to the code examples further down.

    Return a single value or row

    If you just want to return a single row, there is no need for a record of undefined type. @Kevin already demonstrated two ways. I’ll add a simplified version with OUT parameters:

    CREATE OR REPLACE FUNCTION my_func(OUT a integer, OUT b text)
       AS
    $func$
    BEGIN
       a := ...;
       b := ...;
    END
    $func$ LANGUAGE plpgsql;
    

    You don’t even need to add RETURN; in the function body, the value of the declared OUT parameters will be returned automatically at the end of the function – NULL for any parameter that has not been assigned.
    And you don’t need to declare RETURNS RECORD because that’s already clear from the OUT parameters.

    Return a set of rows

    If you actually want to return multiple rows (including the possibility for 0 or 1 row), you can define the return type as RETURNS …

    • SETOF some_type, where some_type can be any registered scalar or composite type.

    • TABLE (col1 type1, col2 type2) – an ad-hoc row type definition.

    • SETOF record plus OUT parameters to define column names andtypes.
      100% equivalent to RETURNS TABLE.

    • SETOF record without further definition. But then the returned rows are undefined and you need to include a column definition list with every call (see example).

    The manual about the record type:

    Record variables are similar to row-type variables, but they have no
    predefined structure.
    They take on the actual row structure of the
    row they are assigned during a SELECT or FOR command.

    There is more, read the manual.

    You can use a record variable without assigning a defined type, you can even return such undefined records:

    CREATE OR REPLACE FUNCTION my_func()
      RETURNS SETOF record AS
    $func$
    DECLARE
        r record;
    BEGIN
        r := (1::int, 'foo'::text); RETURN NEXT r; -- works with undefined record
        r := (2::int, 'bar'::text); RETURN NEXT r;
    END
    $func$ LANGUAGE plpgsql;
    

    Call:

    SELECT * FROM my_func() AS x(a int, b text);
    

    But this is very unwieldy as you have to provide the column definition list with every call. It can generally be replaced with something more elegant:

    • If you know the type at time of function creation, declare it right away (RETURNS TABLE or friends).
    CREATE OR REPLACE FUNCTION my_func()
      RETURNS SETOF tbl_or_type AS
    $func$
    DECLARE
        r tbl_or_type;
    BEGIN
        SELECT INTO tbl_or_type  * FROM tbl WHERE id = 10;
        RETURN NEXT r;  -- type matches
    
        SELECT INTO tbl_or_type  * FROM tbl WHERE id = 12;
        RETURN NEXT r;
    
        -- Or simpler:
        RETURN QUERY
        SELECT * FROM tbl WHERE id = 14;
    END
    $func$ LANGUAGE plpgsql;
    
    • If you know the type at time of the function call, there are more elegant ways using polymorphic types:
      Refactor a PL/pgSQL function to return the output of various SELECT queries

    Your question is unclear as to what you need exactly.

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

Sidebar

Related Questions

Can not find scala.actors package in latest milestones, while it still presents in scaladocs:
Can we change the default action of the edit selected row button? Here is
Can we change the color of the divider? Apple documentations says, that we can
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
Can anyone help me with an excel formula that groups related rows and creates
Can anyone help me trying to find out why this doesn't work. The brushes
Can somebody point me to a resource that explains how to go about having
Can anyone (maybe an XSL-fan?) help me find any advantages with handling presentation of
Can a LINQ enabled app run on a machine that only has the .NET
Can we say that a truncated md5 hash is still uniformly distributed? To avoid

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.