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

  • Home
  • SEARCH
  • 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 8747029
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:17:25+00:00 2026-06-13T12:17:25+00:00

I need to loop through type RECORD items by key/index, like I can do

  • 0

I need to loop through type RECORD items by key/index, like I can do this using array structures in other programming languages.

For example:

DECLARE
    data1    record;
    data2    text;
...
BEGIN
...
FOR data1 IN
    SELECT
        *
    FROM
        sometable
LOOP

    FOR data2 IN
        SELECT
            unnest( data1 )   -- THIS IS DOESN'T WORK!
    LOOP
        RETURN NEXT data1[data2];   -- SMTH LIKE THIS
    END LOOP;

END LOOP;
  • 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-13T12:17:26+00:00Added an answer on June 13, 2026 at 12:17 pm

    As @Pavel explained, it is not simply possible to traverse a record, like you could traverse an array. But there are several ways around it – depending on your exact requirements. Ultimately, since you want to return all values in the same column, you need to cast them to the same type – text is the obvious common ground, because there is a text representation for every type.

    Quick and dirty

    Say, you have a table with an integer, a text and a date column.

    CREATE TEMP TABLE tbl(a int, b text, c date);
    INSERT INTO tbl VALUES
     (1, '1text',     '2012-10-01')
    ,(2, '2text',     '2012-10-02')
    ,(3, ',3,ex,',    '2012-10-03')  -- text with commas
    ,(4, '",4,"ex,"', '2012-10-04')  -- text with commas and double quotes
    

    Then the solution can be a simple as:

    SELECT unnest(string_to_array(trim(t::text, '()'), ','))
    FROM   tbl t;
    

    Works for the first two rows, but fails for the special cases of row 3 and 4.
    You can easily solve the problem with commas in the text representation:

    SELECT unnest(('{' || trim(t::text, '()') || '}')::text[])
    FROM   tbl t
    WHERE  a < 4;
    

    This would work fine – except for line 4 which has double quotes in the text representation. Those are escaped by doubling them up. But the array constructor would need them escaped by \. Not sure why this incompatibility is there …

    SELECT ('{' || trim(t::text, '()') || '}') FROM tbl t WHERE a = 4
    

    Yields:

    {4,""",4,""ex,""",2012-10-04}
    

    But you would need:

    SELECT '{4,"\",4,\"ex,\"",2012-10-04}'::text[];  -- works
    

    Proper solution

    If you knew the column names beforehand, a clean solution would be simple:

    SELECT unnest(ARRAY[a::text,b::text,c::text])
    FROM tbl
    

    Since you operate on records of well know type you can just query the system catalog:

    SELECT string_agg(a.attname || '::text', ',' ORDER  BY a.attnum)
    FROM   pg_catalog.pg_attribute a 
    WHERE  a.attrelid = 'tbl'::regclass
    AND    a.attnum > 0
    AND    a.attisdropped = FALSE
    

    Put this in a function with dynamic SQL:

    CREATE OR REPLACE FUNCTION unnest_table(_tbl text)
      RETURNS SETOF text LANGUAGE plpgsql AS
    $func$
    BEGIN
    
    RETURN QUERY EXECUTE '
    SELECT unnest(ARRAY[' || (
        SELECT string_agg(a.attname || '::text', ',' ORDER  BY a.attnum)
        FROM   pg_catalog.pg_attribute a 
        WHERE  a.attrelid = _tbl::regclass
        AND    a.attnum > 0
        AND    a.attisdropped = false
        ) || '])
    FROM   ' || _tbl::regclass;
    
    END
    $func$;
    

    Call:

    SELECT unnest_table('tbl') AS val
    

    Returns:

    val
    -----
    1
    1text
    2012-10-01
    2
    2text
    2012-10-02
    3
    ,3,ex,
    2012-10-03
    4
    ",4,"ex,"
    2012-10-04
    

    This works without installing additional modules. Another option is to install the hstore extension and use it like @Craig demonstrates.

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

Sidebar

Related Questions

I need to loop through a set of 2-dimensional array of hidden input fields
Okay so this is a bit complex but I need to loop through this
I need to loop through a returned array in javascript but it doesn't work,
I need to loop through the properties of a custom object type that I'm
I'm trying to get all form elements to array, then loop through them using
I need to loop through a List of Dictionaries List<Dictionary<string,string>> to populate a DataTable.
I need to loop through each day of the week (monday tues...) and compare
i have the following code ..i need to loop through end of the file
I have a nested JSON object that I need to loop through, and the
my heading kinda explains the problem. I need to loop through multiple ul tags

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.